blob: 1e5ca8e0f48ef8e4e21f38508bb200d545bb6a04 [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 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400470static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
471 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200472{
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
Vivien Didelotf81ec902016-05-09 13:22:58 -0400717static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
718 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100719{
Andrew Lunn158bc062016-04-28 21:24:06 -0400720 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100721 struct mv88e6xxx_hw_stat *stat;
722 int i, j;
723
724 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
725 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400726 if (mv88e6xxx_has_stat(ps, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100727 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
728 ETH_GSTRING_LEN);
729 j++;
730 }
731 }
732}
733
Vivien Didelotf81ec902016-05-09 13:22:58 -0400734static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100735{
Andrew Lunn158bc062016-04-28 21:24:06 -0400736 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100737 struct mv88e6xxx_hw_stat *stat;
738 int i, j;
739
740 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
741 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400742 if (mv88e6xxx_has_stat(ps, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100743 j++;
744 }
745 return j;
746}
747
Vivien Didelotf81ec902016-05-09 13:22:58 -0400748static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
749 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
Vivien Didelotf81ec902016-05-09 13:22:58 -0400774static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700775{
776 return 32 * sizeof(u16);
777}
778
Vivien Didelotf81ec902016-05-09 13:22:58 -0400779static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
780 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700781{
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
Vivien Didelotf81ec902016-05-09 13:22:58 -0400879static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
880 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400881{
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
Vivien Didelotf81ec902016-05-09 13:22:58 -0400985static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
986 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400987{
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
Vivien Didelotf81ec902016-05-09 13:22:58 -04001107static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1108 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001109{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001110 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001111 int reg;
1112
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001113 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1114 return -EOPNOTSUPP;
1115
Andrew Lunn3898c142015-05-06 01:09:53 +02001116 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001117
Andrew Lunn158bc062016-04-28 21:24:06 -04001118 reg = _mv88e6xxx_phy_read_indirect(ps, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001119 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001120 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001121
1122 e->eee_enabled = !!(reg & 0x0200);
1123 e->tx_lpi_enabled = !!(reg & 0x0100);
1124
Andrew Lunn158bc062016-04-28 21:24:06 -04001125 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001126 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001127 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001128
Andrew Lunncca8b132015-04-02 04:06:39 +02001129 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001130 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001131
Andrew Lunn2f40c692015-04-02 04:06:37 +02001132out:
Andrew Lunn3898c142015-05-06 01:09:53 +02001133 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001134 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001135}
1136
Vivien Didelotf81ec902016-05-09 13:22:58 -04001137static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1138 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001139{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001140 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1141 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001142 int ret;
1143
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001144 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1145 return -EOPNOTSUPP;
1146
Andrew Lunn3898c142015-05-06 01:09:53 +02001147 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001148
Andrew Lunn158bc062016-04-28 21:24:06 -04001149 ret = _mv88e6xxx_phy_read_indirect(ps, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001150 if (ret < 0)
1151 goto out;
1152
1153 reg = ret & ~0x0300;
1154 if (e->eee_enabled)
1155 reg |= 0x0200;
1156 if (e->tx_lpi_enabled)
1157 reg |= 0x0100;
1158
Andrew Lunn158bc062016-04-28 21:24:06 -04001159 ret = _mv88e6xxx_phy_write_indirect(ps, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001160out:
Andrew Lunn3898c142015-05-06 01:09:53 +02001161 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001162
1163 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001164}
1165
Andrew Lunn158bc062016-04-28 21:24:06 -04001166static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_priv_state *ps, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001167{
1168 int ret;
1169
Andrew Lunn158bc062016-04-28 21:24:06 -04001170 if (mv88e6xxx_has_fid_reg(ps)) {
1171 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_FID, fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001172 if (ret < 0)
1173 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001174 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001175 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001176 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -04001177 if (ret < 0)
1178 return ret;
1179
Andrew Lunn158bc062016-04-28 21:24:06 -04001180 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001181 (ret & 0xfff) |
1182 ((fid << 8) & 0xf000));
1183 if (ret < 0)
1184 return ret;
1185
1186 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1187 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001188 }
1189
Andrew Lunn158bc062016-04-28 21:24:06 -04001190 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001191 if (ret < 0)
1192 return ret;
1193
Andrew Lunn158bc062016-04-28 21:24:06 -04001194 return _mv88e6xxx_atu_wait(ps);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001195}
1196
Andrew Lunn158bc062016-04-28 21:24:06 -04001197static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot37705b72015-09-04 14:34:11 -04001198 struct mv88e6xxx_atu_entry *entry)
1199{
1200 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1201
1202 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1203 unsigned int mask, shift;
1204
1205 if (entry->trunk) {
1206 data |= GLOBAL_ATU_DATA_TRUNK;
1207 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1208 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1209 } else {
1210 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1211 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1212 }
1213
1214 data |= (entry->portv_trunkid << shift) & mask;
1215 }
1216
Andrew Lunn158bc062016-04-28 21:24:06 -04001217 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001218}
1219
Andrew Lunn158bc062016-04-28 21:24:06 -04001220static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001221 struct mv88e6xxx_atu_entry *entry,
1222 bool static_too)
1223{
1224 int op;
1225 int err;
1226
Andrew Lunn158bc062016-04-28 21:24:06 -04001227 err = _mv88e6xxx_atu_wait(ps);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001228 if (err)
1229 return err;
1230
Andrew Lunn158bc062016-04-28 21:24:06 -04001231 err = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001232 if (err)
1233 return err;
1234
1235 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001236 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1237 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1238 } else {
1239 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1240 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1241 }
1242
Andrew Lunn158bc062016-04-28 21:24:06 -04001243 return _mv88e6xxx_atu_cmd(ps, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001244}
1245
Andrew Lunn158bc062016-04-28 21:24:06 -04001246static int _mv88e6xxx_atu_flush(struct mv88e6xxx_priv_state *ps,
1247 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001248{
1249 struct mv88e6xxx_atu_entry entry = {
1250 .fid = fid,
1251 .state = 0, /* EntryState bits must be 0 */
1252 };
1253
Andrew Lunn158bc062016-04-28 21:24:06 -04001254 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001255}
1256
Andrew Lunn158bc062016-04-28 21:24:06 -04001257static int _mv88e6xxx_atu_move(struct mv88e6xxx_priv_state *ps, u16 fid,
1258 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001259{
1260 struct mv88e6xxx_atu_entry entry = {
1261 .trunk = false,
1262 .fid = fid,
1263 };
1264
1265 /* EntryState bits must be 0xF */
1266 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1267
1268 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1269 entry.portv_trunkid = (to_port & 0x0f) << 4;
1270 entry.portv_trunkid |= from_port & 0x0f;
1271
Andrew Lunn158bc062016-04-28 21:24:06 -04001272 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001273}
1274
Andrew Lunn158bc062016-04-28 21:24:06 -04001275static int _mv88e6xxx_atu_remove(struct mv88e6xxx_priv_state *ps, u16 fid,
1276 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001277{
1278 /* Destination port 0xF means remove the entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001279 return _mv88e6xxx_atu_move(ps, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001280}
1281
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001282static const char * const mv88e6xxx_port_state_names[] = {
1283 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1284 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1285 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1286 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1287};
1288
Andrew Lunn158bc062016-04-28 21:24:06 -04001289static int _mv88e6xxx_port_state(struct mv88e6xxx_priv_state *ps, int port,
1290 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001291{
Andrew Lunn158bc062016-04-28 21:24:06 -04001292 struct dsa_switch *ds = ps->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001293 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001294 u8 oldstate;
1295
Andrew Lunn158bc062016-04-28 21:24:06 -04001296 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001297 if (reg < 0)
1298 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001299
Andrew Lunncca8b132015-04-02 04:06:39 +02001300 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001301
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001302 if (oldstate != state) {
1303 /* Flush forwarding database if we're moving a port
1304 * from Learning or Forwarding state to Disabled or
1305 * Blocking or Listening state.
1306 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001307 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1308 oldstate == PORT_CONTROL_STATE_FORWARDING)
1309 && (state == PORT_CONTROL_STATE_DISABLED ||
1310 state == PORT_CONTROL_STATE_BLOCKING)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001311 ret = _mv88e6xxx_atu_remove(ps, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001312 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001313 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001314 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001315
Andrew Lunncca8b132015-04-02 04:06:39 +02001316 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Andrew Lunn158bc062016-04-28 21:24:06 -04001317 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001318 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001319 if (ret)
1320 return ret;
1321
1322 netdev_dbg(ds->ports[port], "PortState %s (was %s)\n",
1323 mv88e6xxx_port_state_names[state],
1324 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001325 }
1326
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001327 return ret;
1328}
1329
Andrew Lunn158bc062016-04-28 21:24:06 -04001330static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_priv_state *ps,
1331 int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001332{
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001333 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot009a2b92016-04-17 13:24:01 -04001334 const u16 mask = (1 << ps->info->num_ports) - 1;
Andrew Lunn158bc062016-04-28 21:24:06 -04001335 struct dsa_switch *ds = ps->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001336 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001337 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001338 int i;
1339
1340 /* allow CPU port or DSA link(s) to send frames to every port */
1341 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1342 output_ports = mask;
1343 } else {
Vivien Didelot009a2b92016-04-17 13:24:01 -04001344 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001345 /* allow sending frames to every group member */
1346 if (bridge && ps->ports[i].bridge_dev == bridge)
1347 output_ports |= BIT(i);
1348
1349 /* allow sending frames to CPU port and DSA link(s) */
1350 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1351 output_ports |= BIT(i);
1352 }
1353 }
1354
1355 /* prevent frames from going back out of the port they came in on */
1356 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001357
Andrew Lunn158bc062016-04-28 21:24:06 -04001358 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001359 if (reg < 0)
1360 return reg;
1361
1362 reg &= ~mask;
1363 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001364
Andrew Lunn158bc062016-04-28 21:24:06 -04001365 return _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001366}
1367
Vivien Didelotf81ec902016-05-09 13:22:58 -04001368static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1369 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001370{
1371 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1372 int stp_state;
1373
Vivien Didelot936f2342016-05-09 13:22:46 -04001374 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_PORTSTATE))
1375 return;
1376
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001377 switch (state) {
1378 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001379 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001380 break;
1381 case BR_STATE_BLOCKING:
1382 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001383 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001384 break;
1385 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001386 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001387 break;
1388 case BR_STATE_FORWARDING:
1389 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001390 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001391 break;
1392 }
1393
Vivien Didelot43c44a92016-04-06 11:55:03 -04001394 /* mv88e6xxx_port_stp_state_set may be called with softirqs disabled,
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001395 * so we can not update the port state directly but need to schedule it.
1396 */
Vivien Didelotd715fa62016-02-12 12:09:38 -05001397 ps->ports[port].state = stp_state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001398 set_bit(port, ps->port_state_update_mask);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001399 schedule_work(&ps->bridge_work);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001400}
1401
Andrew Lunn158bc062016-04-28 21:24:06 -04001402static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port,
1403 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001404{
Andrew Lunn158bc062016-04-28 21:24:06 -04001405 struct dsa_switch *ds = ps->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001406 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001407 int ret;
1408
Andrew Lunn158bc062016-04-28 21:24:06 -04001409 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001410 if (ret < 0)
1411 return ret;
1412
Vivien Didelot5da96032016-03-07 18:24:39 -05001413 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1414
1415 if (new) {
1416 ret &= ~PORT_DEFAULT_VLAN_MASK;
1417 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1418
Andrew Lunn158bc062016-04-28 21:24:06 -04001419 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001420 PORT_DEFAULT_VLAN, ret);
1421 if (ret < 0)
1422 return ret;
1423
1424 netdev_dbg(ds->ports[port], "DefaultVID %d (was %d)\n", *new,
1425 pvid);
1426 }
1427
1428 if (old)
1429 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001430
1431 return 0;
1432}
1433
Andrew Lunn158bc062016-04-28 21:24:06 -04001434static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps,
1435 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001436{
Andrew Lunn158bc062016-04-28 21:24:06 -04001437 return _mv88e6xxx_port_pvid(ps, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001438}
1439
Andrew Lunn158bc062016-04-28 21:24:06 -04001440static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps,
1441 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001442{
Andrew Lunn158bc062016-04-28 21:24:06 -04001443 return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001444}
1445
Andrew Lunn158bc062016-04-28 21:24:06 -04001446static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001447{
Andrew Lunn158bc062016-04-28 21:24:06 -04001448 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001449 GLOBAL_VTU_OP_BUSY);
1450}
1451
Andrew Lunn158bc062016-04-28 21:24:06 -04001452static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001453{
1454 int ret;
1455
Andrew Lunn158bc062016-04-28 21:24:06 -04001456 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001457 if (ret < 0)
1458 return ret;
1459
Andrew Lunn158bc062016-04-28 21:24:06 -04001460 return _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001461}
1462
Andrew Lunn158bc062016-04-28 21:24:06 -04001463static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001464{
1465 int ret;
1466
Andrew Lunn158bc062016-04-28 21:24:06 -04001467 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001468 if (ret < 0)
1469 return ret;
1470
Andrew Lunn158bc062016-04-28 21:24:06 -04001471 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001472}
1473
Andrew Lunn158bc062016-04-28 21:24:06 -04001474static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001475 struct mv88e6xxx_vtu_stu_entry *entry,
1476 unsigned int nibble_offset)
1477{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001478 u16 regs[3];
1479 int i;
1480 int ret;
1481
1482 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001483 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001484 GLOBAL_VTU_DATA_0_3 + i);
1485 if (ret < 0)
1486 return ret;
1487
1488 regs[i] = ret;
1489 }
1490
Vivien Didelot009a2b92016-04-17 13:24:01 -04001491 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001492 unsigned int shift = (i % 4) * 4 + nibble_offset;
1493 u16 reg = regs[i / 4];
1494
1495 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1496 }
1497
1498 return 0;
1499}
1500
Andrew Lunn158bc062016-04-28 21:24:06 -04001501static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001502 struct mv88e6xxx_vtu_stu_entry *entry,
1503 unsigned int nibble_offset)
1504{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001505 u16 regs[3] = { 0 };
1506 int i;
1507 int ret;
1508
Vivien Didelot009a2b92016-04-17 13:24:01 -04001509 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001510 unsigned int shift = (i % 4) * 4 + nibble_offset;
1511 u8 data = entry->data[i];
1512
1513 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1514 }
1515
1516 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001517 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001518 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1519 if (ret < 0)
1520 return ret;
1521 }
1522
1523 return 0;
1524}
1525
Andrew Lunn158bc062016-04-28 21:24:06 -04001526static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001527{
Andrew Lunn158bc062016-04-28 21:24:06 -04001528 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001529 vid & GLOBAL_VTU_VID_MASK);
1530}
1531
Andrew Lunn158bc062016-04-28 21:24:06 -04001532static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001533 struct mv88e6xxx_vtu_stu_entry *entry)
1534{
1535 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1536 int ret;
1537
Andrew Lunn158bc062016-04-28 21:24:06 -04001538 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001539 if (ret < 0)
1540 return ret;
1541
Andrew Lunn158bc062016-04-28 21:24:06 -04001542 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001543 if (ret < 0)
1544 return ret;
1545
Andrew Lunn158bc062016-04-28 21:24:06 -04001546 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001547 if (ret < 0)
1548 return ret;
1549
1550 next.vid = ret & GLOBAL_VTU_VID_MASK;
1551 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1552
1553 if (next.valid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001554 ret = _mv88e6xxx_vtu_stu_data_read(ps, &next, 0);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001555 if (ret < 0)
1556 return ret;
1557
Andrew Lunn158bc062016-04-28 21:24:06 -04001558 if (mv88e6xxx_has_fid_reg(ps)) {
1559 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001560 GLOBAL_VTU_FID);
1561 if (ret < 0)
1562 return ret;
1563
1564 next.fid = ret & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001565 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001566 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1567 * VTU DBNum[3:0] are located in VTU Operation 3:0
1568 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001569 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001570 GLOBAL_VTU_OP);
1571 if (ret < 0)
1572 return ret;
1573
1574 next.fid = (ret & 0xf00) >> 4;
1575 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001576 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001577
Andrew Lunn158bc062016-04-28 21:24:06 -04001578 if (mv88e6xxx_has_stu(ps)) {
1579 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001580 GLOBAL_VTU_SID);
1581 if (ret < 0)
1582 return ret;
1583
1584 next.sid = ret & GLOBAL_VTU_SID_MASK;
1585 }
1586 }
1587
1588 *entry = next;
1589 return 0;
1590}
1591
Vivien Didelotf81ec902016-05-09 13:22:58 -04001592static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1593 struct switchdev_obj_port_vlan *vlan,
1594 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001595{
1596 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1597 struct mv88e6xxx_vtu_stu_entry next;
1598 u16 pvid;
1599 int err;
1600
Vivien Didelot54d77b52016-05-09 13:22:47 -04001601 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
1602 return -EOPNOTSUPP;
1603
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001604 mutex_lock(&ps->smi_mutex);
1605
Andrew Lunn158bc062016-04-28 21:24:06 -04001606 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001607 if (err)
1608 goto unlock;
1609
Andrew Lunn158bc062016-04-28 21:24:06 -04001610 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001611 if (err)
1612 goto unlock;
1613
1614 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001615 err = _mv88e6xxx_vtu_getnext(ps, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001616 if (err)
1617 break;
1618
1619 if (!next.valid)
1620 break;
1621
1622 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1623 continue;
1624
1625 /* reinit and dump this VLAN obj */
1626 vlan->vid_begin = vlan->vid_end = next.vid;
1627 vlan->flags = 0;
1628
1629 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1630 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1631
1632 if (next.vid == pvid)
1633 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1634
1635 err = cb(&vlan->obj);
1636 if (err)
1637 break;
1638 } while (next.vid < GLOBAL_VTU_VID_MASK);
1639
1640unlock:
1641 mutex_unlock(&ps->smi_mutex);
1642
1643 return err;
1644}
1645
Andrew Lunn158bc062016-04-28 21:24:06 -04001646static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001647 struct mv88e6xxx_vtu_stu_entry *entry)
1648{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001649 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001650 u16 reg = 0;
1651 int ret;
1652
Andrew Lunn158bc062016-04-28 21:24:06 -04001653 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001654 if (ret < 0)
1655 return ret;
1656
1657 if (!entry->valid)
1658 goto loadpurge;
1659
1660 /* Write port member tags */
Andrew Lunn158bc062016-04-28 21:24:06 -04001661 ret = _mv88e6xxx_vtu_stu_data_write(ps, entry, 0);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001662 if (ret < 0)
1663 return ret;
1664
Andrew Lunn158bc062016-04-28 21:24:06 -04001665 if (mv88e6xxx_has_stu(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001666 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001667 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001668 if (ret < 0)
1669 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001670 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001671
Andrew Lunn158bc062016-04-28 21:24:06 -04001672 if (mv88e6xxx_has_fid_reg(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001673 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001674 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001675 if (ret < 0)
1676 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001677 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001678 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1679 * VTU DBNum[3:0] are located in VTU Operation 3:0
1680 */
1681 op |= (entry->fid & 0xf0) << 8;
1682 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001683 }
1684
1685 reg = GLOBAL_VTU_VID_VALID;
1686loadpurge:
1687 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001688 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001689 if (ret < 0)
1690 return ret;
1691
Andrew Lunn158bc062016-04-28 21:24:06 -04001692 return _mv88e6xxx_vtu_cmd(ps, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001693}
1694
Andrew Lunn158bc062016-04-28 21:24:06 -04001695static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001696 struct mv88e6xxx_vtu_stu_entry *entry)
1697{
1698 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1699 int ret;
1700
Andrew Lunn158bc062016-04-28 21:24:06 -04001701 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001702 if (ret < 0)
1703 return ret;
1704
Andrew Lunn158bc062016-04-28 21:24:06 -04001705 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001706 sid & GLOBAL_VTU_SID_MASK);
1707 if (ret < 0)
1708 return ret;
1709
Andrew Lunn158bc062016-04-28 21:24:06 -04001710 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001711 if (ret < 0)
1712 return ret;
1713
Andrew Lunn158bc062016-04-28 21:24:06 -04001714 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001715 if (ret < 0)
1716 return ret;
1717
1718 next.sid = ret & GLOBAL_VTU_SID_MASK;
1719
Andrew Lunn158bc062016-04-28 21:24:06 -04001720 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001721 if (ret < 0)
1722 return ret;
1723
1724 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1725
1726 if (next.valid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001727 ret = _mv88e6xxx_vtu_stu_data_read(ps, &next, 2);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001728 if (ret < 0)
1729 return ret;
1730 }
1731
1732 *entry = next;
1733 return 0;
1734}
1735
Andrew Lunn158bc062016-04-28 21:24:06 -04001736static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001737 struct mv88e6xxx_vtu_stu_entry *entry)
1738{
1739 u16 reg = 0;
1740 int ret;
1741
Andrew Lunn158bc062016-04-28 21:24:06 -04001742 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001743 if (ret < 0)
1744 return ret;
1745
1746 if (!entry->valid)
1747 goto loadpurge;
1748
1749 /* Write port states */
Andrew Lunn158bc062016-04-28 21:24:06 -04001750 ret = _mv88e6xxx_vtu_stu_data_write(ps, entry, 2);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001751 if (ret < 0)
1752 return ret;
1753
1754 reg = GLOBAL_VTU_VID_VALID;
1755loadpurge:
Andrew Lunn158bc062016-04-28 21:24:06 -04001756 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001757 if (ret < 0)
1758 return ret;
1759
1760 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001761 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001762 if (ret < 0)
1763 return ret;
1764
Andrew Lunn158bc062016-04-28 21:24:06 -04001765 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001766}
1767
Andrew Lunn158bc062016-04-28 21:24:06 -04001768static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port,
1769 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001770{
Andrew Lunn158bc062016-04-28 21:24:06 -04001771 struct dsa_switch *ds = ps->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001772 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001773 u16 fid;
1774 int ret;
1775
Andrew Lunn158bc062016-04-28 21:24:06 -04001776 if (mv88e6xxx_num_databases(ps) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001777 upper_mask = 0xff;
Andrew Lunn158bc062016-04-28 21:24:06 -04001778 else if (mv88e6xxx_num_databases(ps) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001779 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001780 else
1781 return -EOPNOTSUPP;
1782
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001783 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001784 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001785 if (ret < 0)
1786 return ret;
1787
1788 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1789
1790 if (new) {
1791 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1792 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1793
Andrew Lunn158bc062016-04-28 21:24:06 -04001794 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001795 ret);
1796 if (ret < 0)
1797 return ret;
1798 }
1799
1800 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001801 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001802 if (ret < 0)
1803 return ret;
1804
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001805 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001806
1807 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001808 ret &= ~upper_mask;
1809 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001810
Andrew Lunn158bc062016-04-28 21:24:06 -04001811 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001812 ret);
1813 if (ret < 0)
1814 return ret;
1815
1816 netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid);
1817 }
1818
1819 if (old)
1820 *old = fid;
1821
1822 return 0;
1823}
1824
Andrew Lunn158bc062016-04-28 21:24:06 -04001825static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps,
1826 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001827{
Andrew Lunn158bc062016-04-28 21:24:06 -04001828 return _mv88e6xxx_port_fid(ps, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001829}
1830
Andrew Lunn158bc062016-04-28 21:24:06 -04001831static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps,
1832 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001833{
Andrew Lunn158bc062016-04-28 21:24:06 -04001834 return _mv88e6xxx_port_fid(ps, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001835}
1836
Andrew Lunn158bc062016-04-28 21:24:06 -04001837static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001838{
1839 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1840 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001841 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001842
1843 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1844
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001845 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001846 for (i = 0; i < ps->info->num_ports; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001847 err = _mv88e6xxx_port_fid_get(ps, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001848 if (err)
1849 return err;
1850
1851 set_bit(*fid, fid_bitmap);
1852 }
1853
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001854 /* Set every FID bit used by the VLAN entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001855 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001856 if (err)
1857 return err;
1858
1859 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001860 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001861 if (err)
1862 return err;
1863
1864 if (!vlan.valid)
1865 break;
1866
1867 set_bit(vlan.fid, fid_bitmap);
1868 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1869
1870 /* The reset value 0x000 is used to indicate that multiple address
1871 * databases are not needed. Return the next positive available.
1872 */
1873 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Andrew Lunn158bc062016-04-28 21:24:06 -04001874 if (unlikely(*fid >= mv88e6xxx_num_databases(ps)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001875 return -ENOSPC;
1876
1877 /* Clear the database */
Andrew Lunn158bc062016-04-28 21:24:06 -04001878 return _mv88e6xxx_atu_flush(ps, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001879}
1880
Andrew Lunn158bc062016-04-28 21:24:06 -04001881static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001882 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001883{
Andrew Lunn158bc062016-04-28 21:24:06 -04001884 struct dsa_switch *ds = ps->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001885 struct mv88e6xxx_vtu_stu_entry vlan = {
1886 .valid = true,
1887 .vid = vid,
1888 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001889 int i, err;
1890
Andrew Lunn158bc062016-04-28 21:24:06 -04001891 err = _mv88e6xxx_fid_new(ps, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001892 if (err)
1893 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001894
Vivien Didelot3d131f02015-11-03 10:52:52 -05001895 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001896 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001897 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1898 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1899 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001900
Andrew Lunn158bc062016-04-28 21:24:06 -04001901 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
1902 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001903 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001904
1905 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1906 * implemented, only one STU entry is needed to cover all VTU
1907 * entries. Thus, validate the SID 0.
1908 */
1909 vlan.sid = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04001910 err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001911 if (err)
1912 return err;
1913
1914 if (vstp.sid != vlan.sid || !vstp.valid) {
1915 memset(&vstp, 0, sizeof(vstp));
1916 vstp.valid = true;
1917 vstp.sid = vlan.sid;
1918
Andrew Lunn158bc062016-04-28 21:24:06 -04001919 err = _mv88e6xxx_stu_loadpurge(ps, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001920 if (err)
1921 return err;
1922 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001923 }
1924
1925 *entry = vlan;
1926 return 0;
1927}
1928
Andrew Lunn158bc062016-04-28 21:24:06 -04001929static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001930 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1931{
1932 int err;
1933
1934 if (!vid)
1935 return -EINVAL;
1936
Andrew Lunn158bc062016-04-28 21:24:06 -04001937 err = _mv88e6xxx_vtu_vid_write(ps, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001938 if (err)
1939 return err;
1940
Andrew Lunn158bc062016-04-28 21:24:06 -04001941 err = _mv88e6xxx_vtu_getnext(ps, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001942 if (err)
1943 return err;
1944
1945 if (entry->vid != vid || !entry->valid) {
1946 if (!creat)
1947 return -EOPNOTSUPP;
1948 /* -ENOENT would've been more appropriate, but switchdev expects
1949 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1950 */
1951
Andrew Lunn158bc062016-04-28 21:24:06 -04001952 err = _mv88e6xxx_vtu_new(ps, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001953 }
1954
1955 return err;
1956}
1957
Vivien Didelotda9c3592016-02-12 12:09:40 -05001958static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1959 u16 vid_begin, u16 vid_end)
1960{
1961 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1962 struct mv88e6xxx_vtu_stu_entry vlan;
1963 int i, err;
1964
1965 if (!vid_begin)
1966 return -EOPNOTSUPP;
1967
1968 mutex_lock(&ps->smi_mutex);
1969
Andrew Lunn158bc062016-04-28 21:24:06 -04001970 err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001971 if (err)
1972 goto unlock;
1973
1974 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001975 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001976 if (err)
1977 goto unlock;
1978
1979 if (!vlan.valid)
1980 break;
1981
1982 if (vlan.vid > vid_end)
1983 break;
1984
Vivien Didelot009a2b92016-04-17 13:24:01 -04001985 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001986 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1987 continue;
1988
1989 if (vlan.data[i] ==
1990 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1991 continue;
1992
1993 if (ps->ports[i].bridge_dev ==
1994 ps->ports[port].bridge_dev)
1995 break; /* same bridge, check next VLAN */
1996
1997 netdev_warn(ds->ports[port],
1998 "hardware VLAN %d already used by %s\n",
1999 vlan.vid,
2000 netdev_name(ps->ports[i].bridge_dev));
2001 err = -EOPNOTSUPP;
2002 goto unlock;
2003 }
2004 } while (vlan.vid < vid_end);
2005
2006unlock:
2007 mutex_unlock(&ps->smi_mutex);
2008
2009 return err;
2010}
2011
Vivien Didelot214cdb92016-02-26 13:16:08 -05002012static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2013 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2014 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2015 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2016 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2017};
2018
Vivien Didelotf81ec902016-05-09 13:22:58 -04002019static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2020 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05002021{
2022 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2023 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2024 PORT_CONTROL_2_8021Q_DISABLED;
2025 int ret;
2026
Vivien Didelot54d77b52016-05-09 13:22:47 -04002027 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2028 return -EOPNOTSUPP;
2029
Vivien Didelot214cdb92016-02-26 13:16:08 -05002030 mutex_lock(&ps->smi_mutex);
2031
Andrew Lunn158bc062016-04-28 21:24:06 -04002032 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002033 if (ret < 0)
2034 goto unlock;
2035
2036 old = ret & PORT_CONTROL_2_8021Q_MASK;
2037
Vivien Didelot5220ef12016-03-07 18:24:52 -05002038 if (new != old) {
2039 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2040 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002041
Andrew Lunn158bc062016-04-28 21:24:06 -04002042 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002043 ret);
2044 if (ret < 0)
2045 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002046
Vivien Didelot5220ef12016-03-07 18:24:52 -05002047 netdev_dbg(ds->ports[port], "802.1Q Mode %s (was %s)\n",
2048 mv88e6xxx_port_8021q_mode_names[new],
2049 mv88e6xxx_port_8021q_mode_names[old]);
2050 }
2051
2052 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002053unlock:
2054 mutex_unlock(&ps->smi_mutex);
2055
2056 return ret;
2057}
2058
Vivien Didelotf81ec902016-05-09 13:22:58 -04002059static int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2060 const struct switchdev_obj_port_vlan *vlan,
2061 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002062{
Vivien Didelot54d77b52016-05-09 13:22:47 -04002063 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002064 int err;
2065
Vivien Didelot54d77b52016-05-09 13:22:47 -04002066 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2067 return -EOPNOTSUPP;
2068
Vivien Didelotda9c3592016-02-12 12:09:40 -05002069 /* If the requested port doesn't belong to the same bridge as the VLAN
2070 * members, do not support it (yet) and fallback to software VLAN.
2071 */
2072 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2073 vlan->vid_end);
2074 if (err)
2075 return err;
2076
Vivien Didelot76e398a2015-11-01 12:33:55 -05002077 /* We don't need any dynamic resource from the kernel (yet),
2078 * so skip the prepare phase.
2079 */
2080 return 0;
2081}
2082
Andrew Lunn158bc062016-04-28 21:24:06 -04002083static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port,
2084 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002085{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002086 struct mv88e6xxx_vtu_stu_entry vlan;
2087 int err;
2088
Andrew Lunn158bc062016-04-28 21:24:06 -04002089 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002090 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002091 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002092
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002093 vlan.data[port] = untagged ?
2094 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2095 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2096
Andrew Lunn158bc062016-04-28 21:24:06 -04002097 return _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002098}
2099
Vivien Didelotf81ec902016-05-09 13:22:58 -04002100static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2101 const struct switchdev_obj_port_vlan *vlan,
2102 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002103{
2104 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2105 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2106 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2107 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002108
Vivien Didelot54d77b52016-05-09 13:22:47 -04002109 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2110 return;
2111
Vivien Didelot76e398a2015-11-01 12:33:55 -05002112 mutex_lock(&ps->smi_mutex);
2113
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002114 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Andrew Lunn158bc062016-04-28 21:24:06 -04002115 if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged))
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002116 netdev_err(ds->ports[port], "failed to add VLAN %d%c\n",
2117 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002118
Andrew Lunn158bc062016-04-28 21:24:06 -04002119 if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end))
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002120 netdev_err(ds->ports[port], "failed to set PVID %d\n",
2121 vlan->vid_end);
2122
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002123 mutex_unlock(&ps->smi_mutex);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002124}
2125
Andrew Lunn158bc062016-04-28 21:24:06 -04002126static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps,
2127 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002128{
Andrew Lunn158bc062016-04-28 21:24:06 -04002129 struct dsa_switch *ds = ps->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002130 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002131 int i, err;
2132
Andrew Lunn158bc062016-04-28 21:24:06 -04002133 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002134 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002135 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002136
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002137 /* Tell switchdev if this VLAN is handled in software */
2138 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002139 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002140
2141 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2142
2143 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002144 vlan.valid = false;
Vivien Didelot009a2b92016-04-17 13:24:01 -04002145 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002146 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002147 continue;
2148
2149 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002150 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002151 break;
2152 }
2153 }
2154
Andrew Lunn158bc062016-04-28 21:24:06 -04002155 err = _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002156 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002157 return err;
2158
Andrew Lunn158bc062016-04-28 21:24:06 -04002159 return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002160}
2161
Vivien Didelotf81ec902016-05-09 13:22:58 -04002162static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2163 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002164{
2165 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2166 u16 pvid, vid;
2167 int err = 0;
2168
Vivien Didelot54d77b52016-05-09 13:22:47 -04002169 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2170 return -EOPNOTSUPP;
2171
Vivien Didelot76e398a2015-11-01 12:33:55 -05002172 mutex_lock(&ps->smi_mutex);
2173
Andrew Lunn158bc062016-04-28 21:24:06 -04002174 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002175 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002176 goto unlock;
2177
Vivien Didelot76e398a2015-11-01 12:33:55 -05002178 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002179 err = _mv88e6xxx_port_vlan_del(ps, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002180 if (err)
2181 goto unlock;
2182
2183 if (vid == pvid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002184 err = _mv88e6xxx_port_pvid_set(ps, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002185 if (err)
2186 goto unlock;
2187 }
2188 }
2189
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002190unlock:
2191 mutex_unlock(&ps->smi_mutex);
2192
2193 return err;
2194}
2195
Andrew Lunn158bc062016-04-28 21:24:06 -04002196static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002197 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002198{
2199 int i, ret;
2200
2201 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002202 ret = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002203 ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002204 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002205 if (ret < 0)
2206 return ret;
2207 }
2208
2209 return 0;
2210}
2211
Andrew Lunn158bc062016-04-28 21:24:06 -04002212static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps,
2213 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002214{
2215 int i, ret;
2216
2217 for (i = 0; i < 3; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002218 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002219 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002220 if (ret < 0)
2221 return ret;
2222 addr[i * 2] = ret >> 8;
2223 addr[i * 2 + 1] = ret & 0xff;
2224 }
2225
2226 return 0;
2227}
2228
Andrew Lunn158bc062016-04-28 21:24:06 -04002229static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002230 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002231{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002232 int ret;
2233
Andrew Lunn158bc062016-04-28 21:24:06 -04002234 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002235 if (ret < 0)
2236 return ret;
2237
Andrew Lunn158bc062016-04-28 21:24:06 -04002238 ret = _mv88e6xxx_atu_mac_write(ps, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002239 if (ret < 0)
2240 return ret;
2241
Andrew Lunn158bc062016-04-28 21:24:06 -04002242 ret = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002243 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002244 return ret;
2245
Andrew Lunn158bc062016-04-28 21:24:06 -04002246 return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002247}
David S. Millercdf09692015-08-11 12:00:37 -07002248
Andrew Lunn158bc062016-04-28 21:24:06 -04002249static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002250 const unsigned char *addr, u16 vid,
2251 u8 state)
2252{
2253 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002254 struct mv88e6xxx_vtu_stu_entry vlan;
2255 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002256
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002257 /* Null VLAN ID corresponds to the port private database */
2258 if (vid == 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04002259 err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002260 else
Andrew Lunn158bc062016-04-28 21:24:06 -04002261 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002262 if (err)
2263 return err;
2264
2265 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002266 entry.state = state;
2267 ether_addr_copy(entry.mac, addr);
2268 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2269 entry.trunk = false;
2270 entry.portv_trunkid = BIT(port);
2271 }
2272
Andrew Lunn158bc062016-04-28 21:24:06 -04002273 return _mv88e6xxx_atu_load(ps, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002274}
2275
Vivien Didelotf81ec902016-05-09 13:22:58 -04002276static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2277 const struct switchdev_obj_port_fdb *fdb,
2278 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002279{
Vivien Didelot2672f822016-05-09 13:22:48 -04002280 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2281
2282 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2283 return -EOPNOTSUPP;
2284
Vivien Didelot146a3202015-10-08 11:35:12 -04002285 /* We don't need any dynamic resource from the kernel (yet),
2286 * so skip the prepare phase.
2287 */
2288 return 0;
2289}
2290
Vivien Didelotf81ec902016-05-09 13:22:58 -04002291static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2292 const struct switchdev_obj_port_fdb *fdb,
2293 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002294{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002295 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002296 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2297 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2298 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002299
Vivien Didelot2672f822016-05-09 13:22:48 -04002300 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2301 return;
2302
David S. Millercdf09692015-08-11 12:00:37 -07002303 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002304 if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state))
Vivien Didelot8497aa62016-04-06 11:55:04 -04002305 netdev_err(ds->ports[port], "failed to load MAC address\n");
David S. Millercdf09692015-08-11 12:00:37 -07002306 mutex_unlock(&ps->smi_mutex);
David S. Millercdf09692015-08-11 12:00:37 -07002307}
2308
Vivien Didelotf81ec902016-05-09 13:22:58 -04002309static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2310 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002311{
2312 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2313 int ret;
2314
Vivien Didelot2672f822016-05-09 13:22:48 -04002315 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2316 return -EOPNOTSUPP;
2317
David S. Millercdf09692015-08-11 12:00:37 -07002318 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002319 ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002320 GLOBAL_ATU_DATA_STATE_UNUSED);
2321 mutex_unlock(&ps->smi_mutex);
2322
2323 return ret;
2324}
2325
Andrew Lunn158bc062016-04-28 21:24:06 -04002326static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002327 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002328{
Vivien Didelot1d194042015-08-10 09:09:51 -04002329 struct mv88e6xxx_atu_entry next = { 0 };
2330 int ret;
2331
2332 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002333
Andrew Lunn158bc062016-04-28 21:24:06 -04002334 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002335 if (ret < 0)
2336 return ret;
2337
Andrew Lunn158bc062016-04-28 21:24:06 -04002338 ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002339 if (ret < 0)
2340 return ret;
2341
Andrew Lunn158bc062016-04-28 21:24:06 -04002342 ret = _mv88e6xxx_atu_mac_read(ps, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002343 if (ret < 0)
2344 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002345
Andrew Lunn158bc062016-04-28 21:24:06 -04002346 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002347 if (ret < 0)
2348 return ret;
2349
2350 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2351 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2352 unsigned int mask, shift;
2353
2354 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2355 next.trunk = true;
2356 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2357 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2358 } else {
2359 next.trunk = false;
2360 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2361 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2362 }
2363
2364 next.portv_trunkid = (ret & mask) >> shift;
2365 }
2366
2367 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002368 return 0;
2369}
2370
Andrew Lunn158bc062016-04-28 21:24:06 -04002371static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps,
2372 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002373 struct switchdev_obj_port_fdb *fdb,
2374 int (*cb)(struct switchdev_obj *obj))
2375{
2376 struct mv88e6xxx_atu_entry addr = {
2377 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2378 };
2379 int err;
2380
Andrew Lunn158bc062016-04-28 21:24:06 -04002381 err = _mv88e6xxx_atu_mac_write(ps, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002382 if (err)
2383 return err;
2384
2385 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002386 err = _mv88e6xxx_atu_getnext(ps, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002387 if (err)
2388 break;
2389
2390 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2391 break;
2392
2393 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2394 bool is_static = addr.state ==
2395 (is_multicast_ether_addr(addr.mac) ?
2396 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2397 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2398
2399 fdb->vid = vid;
2400 ether_addr_copy(fdb->addr, addr.mac);
2401 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2402
2403 err = cb(&fdb->obj);
2404 if (err)
2405 break;
2406 }
2407 } while (!is_broadcast_ether_addr(addr.mac));
2408
2409 return err;
2410}
2411
Vivien Didelotf81ec902016-05-09 13:22:58 -04002412static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2413 struct switchdev_obj_port_fdb *fdb,
2414 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002415{
2416 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2417 struct mv88e6xxx_vtu_stu_entry vlan = {
2418 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2419 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002420 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002421 int err;
2422
Vivien Didelot2672f822016-05-09 13:22:48 -04002423 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2424 return -EOPNOTSUPP;
2425
Vivien Didelotf33475b2015-10-22 09:34:41 -04002426 mutex_lock(&ps->smi_mutex);
2427
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002428 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Andrew Lunn158bc062016-04-28 21:24:06 -04002429 err = _mv88e6xxx_port_fid_get(ps, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002430 if (err)
2431 goto unlock;
2432
Andrew Lunn158bc062016-04-28 21:24:06 -04002433 err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002434 if (err)
2435 goto unlock;
2436
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002437 /* Dump VLANs' Filtering Information Databases */
Andrew Lunn158bc062016-04-28 21:24:06 -04002438 err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002439 if (err)
2440 goto unlock;
2441
2442 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002443 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002444 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002445 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002446
2447 if (!vlan.valid)
2448 break;
2449
Andrew Lunn158bc062016-04-28 21:24:06 -04002450 err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002451 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002452 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002453 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002454 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2455
2456unlock:
2457 mutex_unlock(&ps->smi_mutex);
2458
2459 return err;
2460}
2461
Vivien Didelotf81ec902016-05-09 13:22:58 -04002462static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2463 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002464{
Vivien Didelota6692752016-02-12 12:09:39 -05002465 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002466 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002467
Vivien Didelot936f2342016-05-09 13:22:46 -04002468 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2469 return -EOPNOTSUPP;
2470
Vivien Didelot466dfa02016-02-26 13:16:05 -05002471 mutex_lock(&ps->smi_mutex);
2472
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002473 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002474 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002475
Vivien Didelot009a2b92016-04-17 13:24:01 -04002476 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002477 if (ps->ports[i].bridge_dev == bridge) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002478 err = _mv88e6xxx_port_based_vlan_map(ps, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002479 if (err)
2480 break;
2481 }
2482 }
2483
Vivien Didelot466dfa02016-02-26 13:16:05 -05002484 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002485
Vivien Didelot466dfa02016-02-26 13:16:05 -05002486 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002487}
2488
Vivien Didelotf81ec902016-05-09 13:22:58 -04002489static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002490{
Vivien Didelota6692752016-02-12 12:09:39 -05002491 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002492 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002493 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002494
Vivien Didelot936f2342016-05-09 13:22:46 -04002495 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2496 return;
2497
Vivien Didelot466dfa02016-02-26 13:16:05 -05002498 mutex_lock(&ps->smi_mutex);
2499
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002500 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002501 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002502
Vivien Didelot009a2b92016-04-17 13:24:01 -04002503 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot16bfa702016-03-13 16:21:33 -04002504 if (i == port || ps->ports[i].bridge_dev == bridge)
Andrew Lunn158bc062016-04-28 21:24:06 -04002505 if (_mv88e6xxx_port_based_vlan_map(ps, i))
Vivien Didelot16bfa702016-03-13 16:21:33 -04002506 netdev_warn(ds->ports[i], "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002507
Vivien Didelot466dfa02016-02-26 13:16:05 -05002508 mutex_unlock(&ps->smi_mutex);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002509}
2510
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002511static void mv88e6xxx_bridge_work(struct work_struct *work)
2512{
2513 struct mv88e6xxx_priv_state *ps;
2514 struct dsa_switch *ds;
2515 int port;
2516
2517 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
Andrew Lunn7543a6d2016-04-13 02:40:40 +02002518 ds = ps->ds;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002519
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002520 mutex_lock(&ps->smi_mutex);
2521
Vivien Didelot009a2b92016-04-17 13:24:01 -04002522 for (port = 0; port < ps->info->num_ports; ++port)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002523 if (test_and_clear_bit(port, ps->port_state_update_mask) &&
Andrew Lunn158bc062016-04-28 21:24:06 -04002524 _mv88e6xxx_port_state(ps, port, ps->ports[port].state))
2525 netdev_warn(ds->ports[port],
2526 "failed to update state to %s\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002527 mv88e6xxx_port_state_names[ps->ports[port].state]);
2528
2529 mutex_unlock(&ps->smi_mutex);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002530}
2531
Andrew Lunn158bc062016-04-28 21:24:06 -04002532static int _mv88e6xxx_phy_page_write(struct mv88e6xxx_priv_state *ps,
2533 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002534{
2535 int ret;
2536
Andrew Lunn158bc062016-04-28 21:24:06 -04002537 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002538 if (ret < 0)
2539 goto restore_page_0;
2540
Andrew Lunn158bc062016-04-28 21:24:06 -04002541 ret = _mv88e6xxx_phy_write_indirect(ps, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002542restore_page_0:
Andrew Lunn158bc062016-04-28 21:24:06 -04002543 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002544
2545 return ret;
2546}
2547
Andrew Lunn158bc062016-04-28 21:24:06 -04002548static int _mv88e6xxx_phy_page_read(struct mv88e6xxx_priv_state *ps,
2549 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002550{
2551 int ret;
2552
Andrew Lunn158bc062016-04-28 21:24:06 -04002553 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002554 if (ret < 0)
2555 goto restore_page_0;
2556
Andrew Lunn158bc062016-04-28 21:24:06 -04002557 ret = _mv88e6xxx_phy_read_indirect(ps, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002558restore_page_0:
Andrew Lunn158bc062016-04-28 21:24:06 -04002559 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002560
2561 return ret;
2562}
2563
Vivien Didelot552238b2016-05-09 13:22:49 -04002564static int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps)
2565{
2566 bool ppu_active = mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE);
2567 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2568 struct gpio_desc *gpiod = ps->ds->pd->reset;
2569 unsigned long timeout;
2570 int ret;
2571 int i;
2572
2573 /* Set all ports to the disabled state. */
2574 for (i = 0; i < ps->info->num_ports; i++) {
2575 ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL);
2576 if (ret < 0)
2577 return ret;
2578
2579 ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL,
2580 ret & 0xfffc);
2581 if (ret)
2582 return ret;
2583 }
2584
2585 /* Wait for transmit queues to drain. */
2586 usleep_range(2000, 4000);
2587
2588 /* If there is a gpio connected to the reset pin, toggle it */
2589 if (gpiod) {
2590 gpiod_set_value_cansleep(gpiod, 1);
2591 usleep_range(10000, 20000);
2592 gpiod_set_value_cansleep(gpiod, 0);
2593 usleep_range(10000, 20000);
2594 }
2595
2596 /* Reset the switch. Keep the PPU active if requested. The PPU
2597 * needs to be active to support indirect phy register access
2598 * through global registers 0x18 and 0x19.
2599 */
2600 if (ppu_active)
2601 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000);
2602 else
2603 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400);
2604 if (ret)
2605 return ret;
2606
2607 /* Wait up to one second for reset to complete. */
2608 timeout = jiffies + 1 * HZ;
2609 while (time_before(jiffies, timeout)) {
2610 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00);
2611 if (ret < 0)
2612 return ret;
2613
2614 if ((ret & is_reset) == is_reset)
2615 break;
2616 usleep_range(1000, 2000);
2617 }
2618 if (time_after(jiffies, timeout))
2619 ret = -ETIMEDOUT;
2620 else
2621 ret = 0;
2622
2623 return ret;
2624}
2625
Andrew Lunn158bc062016-04-28 21:24:06 -04002626static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002627{
2628 int ret;
2629
Andrew Lunn158bc062016-04-28 21:24:06 -04002630 ret = _mv88e6xxx_phy_page_read(ps, REG_FIBER_SERDES, PAGE_FIBER_SERDES,
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002631 MII_BMCR);
2632 if (ret < 0)
2633 return ret;
2634
2635 if (ret & BMCR_PDOWN) {
2636 ret &= ~BMCR_PDOWN;
Andrew Lunn158bc062016-04-28 21:24:06 -04002637 ret = _mv88e6xxx_phy_page_write(ps, REG_FIBER_SERDES,
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002638 PAGE_FIBER_SERDES, MII_BMCR,
2639 ret);
2640 }
2641
2642 return ret;
2643}
2644
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002645static int mv88e6xxx_setup_port(struct mv88e6xxx_priv_state *ps, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002646{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002647 struct dsa_switch *ds = ps->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002648 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002649 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002650
Andrew Lunn158bc062016-04-28 21:24:06 -04002651 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2652 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2653 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2654 mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002655 /* MAC Forcing register: don't force link, speed,
2656 * duplex or flow control state to any particular
2657 * values on physical ports, but force the CPU port
2658 * and all DSA ports to their maximum bandwidth and
2659 * full duplex.
2660 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002661 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002662 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002663 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002664 reg |= PORT_PCS_CTRL_FORCE_LINK |
2665 PORT_PCS_CTRL_LINK_UP |
2666 PORT_PCS_CTRL_DUPLEX_FULL |
2667 PORT_PCS_CTRL_FORCE_DUPLEX;
Andrew Lunn158bc062016-04-28 21:24:06 -04002668 if (mv88e6xxx_6065_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002669 reg |= PORT_PCS_CTRL_100;
2670 else
2671 reg |= PORT_PCS_CTRL_1000;
2672 } else {
2673 reg |= PORT_PCS_CTRL_UNFORCED;
2674 }
2675
Andrew Lunn158bc062016-04-28 21:24:06 -04002676 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002677 PORT_PCS_CTRL, reg);
2678 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002679 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002680 }
2681
2682 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2683 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2684 * tunneling, determine priority by looking at 802.1p and IP
2685 * priority fields (IP prio has precedence), and set STP state
2686 * to Forwarding.
2687 *
2688 * If this is the CPU link, use DSA or EDSA tagging depending
2689 * on which tagging mode was configured.
2690 *
2691 * If this is a link to another switch, use DSA tagging mode.
2692 *
2693 * If this is the upstream port for this switch, enable
2694 * forwarding of unknown unicasts and multicasts.
2695 */
2696 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002697 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2698 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2699 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2700 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002701 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2702 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2703 PORT_CONTROL_STATE_FORWARDING;
2704 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002705 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002706 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002707 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2708 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2709 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002710 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2711 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2712 else
2713 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002714 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2715 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002716 }
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_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2721 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002722 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2723 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2724 }
2725 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002726 if (dsa_is_dsa_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002727 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002728 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002729 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2730 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2731 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002732 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002733 }
2734
Andrew Lunn54d792f2015-05-06 01:09:47 +02002735 if (port == dsa_upstream_port(ds))
2736 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2737 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2738 }
2739 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002740 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002741 PORT_CONTROL, reg);
2742 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002743 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002744 }
2745
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002746 /* If this port is connected to a SerDes, make sure the SerDes is not
2747 * powered down.
2748 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002749 if (mv88e6xxx_6352_family(ps)) {
2750 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002751 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002752 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002753 ret &= PORT_STATUS_CMODE_MASK;
2754 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2755 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2756 (ret == PORT_STATUS_CMODE_SGMII)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002757 ret = mv88e6xxx_power_on_serdes(ps);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002758 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002759 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002760 }
2761 }
2762
Vivien Didelot8efdda42015-08-13 12:52:23 -04002763 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002764 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002765 * untagged frames on this port, do a destination address lookup on all
2766 * received packets as usual, disable ARP mirroring and don't send a
2767 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002768 */
2769 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002770 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2771 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2772 mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) ||
2773 mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002774 reg = PORT_CONTROL_2_MAP_DA;
2775
Andrew Lunn158bc062016-04-28 21:24:06 -04002776 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2777 mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002778 reg |= PORT_CONTROL_2_JUMBO_10240;
2779
Andrew Lunn158bc062016-04-28 21:24:06 -04002780 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002781 /* Set the upstream port this port should use */
2782 reg |= dsa_upstream_port(ds);
2783 /* enable forwarding of unknown multicast addresses to
2784 * the upstream port
2785 */
2786 if (port == dsa_upstream_port(ds))
2787 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2788 }
2789
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002790 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002791
Andrew Lunn54d792f2015-05-06 01:09:47 +02002792 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002793 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002794 PORT_CONTROL_2, reg);
2795 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002796 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002797 }
2798
2799 /* Port Association Vector: when learning source addresses
2800 * of packets, add the address to the address database using
2801 * a port bitmap that has only the bit for this port set and
2802 * the other bits clear.
2803 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002804 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002805 /* Disable learning for CPU port */
2806 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002807 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002808
Andrew Lunn158bc062016-04-28 21:24:06 -04002809 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002810 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002811 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002812
2813 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002814 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002815 0x0000);
2816 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002817 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002818
Andrew Lunn158bc062016-04-28 21:24:06 -04002819 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2820 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2821 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002822 /* Do not limit the period of time that this port can
2823 * be paused for by the remote end or the period of
2824 * time that this port can pause the remote end.
2825 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002826 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002827 PORT_PAUSE_CTRL, 0x0000);
2828 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002829 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002830
2831 /* Port ATU control: disable limiting the number of
2832 * address database entries that this port is allowed
2833 * to use.
2834 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002835 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002836 PORT_ATU_CONTROL, 0x0000);
2837 /* Priority Override: disable DA, SA and VTU priority
2838 * override.
2839 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002840 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002841 PORT_PRI_OVERRIDE, 0x0000);
2842 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002843 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002844
2845 /* Port Ethertype: use the Ethertype DSA Ethertype
2846 * value.
2847 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002848 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002849 PORT_ETH_TYPE, ETH_P_EDSA);
2850 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002851 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002852 /* Tag Remap: use an identity 802.1p prio -> switch
2853 * prio mapping.
2854 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002855 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002856 PORT_TAG_REGMAP_0123, 0x3210);
2857 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002858 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002859
2860 /* Tag Remap 2: use an identity 802.1p prio -> switch
2861 * prio mapping.
2862 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002863 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002864 PORT_TAG_REGMAP_4567, 0x7654);
2865 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002866 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002867 }
2868
Andrew Lunn158bc062016-04-28 21:24:06 -04002869 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2870 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2871 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2872 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002873 /* Rate Control: disable ingress rate limiting. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002874 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002875 PORT_RATE_CONTROL, 0x0001);
2876 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002877 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002878 }
2879
Guenter Roeck366f0a02015-03-26 18:36:30 -07002880 /* Port Control 1: disable trunking, disable sending
2881 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002882 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002883 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002884 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002885 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002886
Vivien Didelot207afda2016-04-14 14:42:09 -04002887 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002888 * database, and allow bidirectional communication between the
2889 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002890 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002891 ret = _mv88e6xxx_port_fid_set(ps, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002892 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002893 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002894
Andrew Lunn158bc062016-04-28 21:24:06 -04002895 ret = _mv88e6xxx_port_based_vlan_map(ps, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002896 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002897 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002898
2899 /* Default VLAN ID and priority: don't set a default VLAN
2900 * ID, and set the default packet priority to zero.
2901 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002902 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002903 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002904 if (ret)
2905 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002906
Andrew Lunndbde9e62015-05-06 01:09:48 +02002907 return 0;
2908}
2909
Vivien Didelot08a01262016-05-09 13:22:50 -04002910static int mv88e6xxx_setup_global(struct mv88e6xxx_priv_state *ps)
2911{
Vivien Didelotb0745e872016-05-09 13:22:53 -04002912 struct dsa_switch *ds = ps->ds;
2913 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002914 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002915 int err;
2916 int i;
2917
Vivien Didelot119477b2016-05-09 13:22:51 -04002918 /* Enable the PHY Polling Unit if present, don't discard any packets,
2919 * and mask all interrupt sources.
2920 */
2921 reg = 0;
2922 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU) ||
2923 mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE))
2924 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2925
2926 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, reg);
2927 if (err)
2928 return err;
2929
Vivien Didelotb0745e872016-05-09 13:22:53 -04002930 /* Configure the upstream port, and configure it as the port to which
2931 * ingress and egress and ARP monitor frames are to be sent.
2932 */
2933 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2934 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2935 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2936 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
2937 if (err)
2938 return err;
2939
Vivien Didelot50484ff2016-05-09 13:22:54 -04002940 /* Disable remote management, and set the switch's DSA device number. */
2941 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL_2,
2942 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2943 (ds->index & 0x1f));
2944 if (err)
2945 return err;
2946
Vivien Didelot08a01262016-05-09 13:22:50 -04002947 /* Set the default address aging time to 5 minutes, and
2948 * enable address learn messages to be sent to all message
2949 * ports.
2950 */
2951 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2952 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2953 if (err)
2954 return err;
2955
2956 /* Configure the IP ToS mapping registers. */
2957 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2958 if (err)
2959 return err;
2960 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2961 if (err)
2962 return err;
2963 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2964 if (err)
2965 return err;
2966 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2967 if (err)
2968 return err;
2969 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2970 if (err)
2971 return err;
2972 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2973 if (err)
2974 return err;
2975 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2976 if (err)
2977 return err;
2978 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2979 if (err)
2980 return err;
2981
2982 /* Configure the IEEE 802.1p priority mapping register. */
2983 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2984 if (err)
2985 return err;
2986
2987 /* Send all frames with destination addresses matching
2988 * 01:80:c2:00:00:0x to the CPU port.
2989 */
2990 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
2991 if (err)
2992 return err;
2993
2994 /* Ignore removed tag data on doubly tagged packets, disable
2995 * flow control messages, force flow control priority to the
2996 * highest, and send all special multicast frames to the CPU
2997 * port at the highest priority.
2998 */
2999 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
3000 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
3001 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
3002 if (err)
3003 return err;
3004
3005 /* Program the DSA routing table. */
3006 for (i = 0; i < 32; i++) {
3007 int nexthop = 0x1f;
3008
3009 if (ps->ds->pd->rtable &&
3010 i != ps->ds->index && i < ps->ds->dst->pd->nr_chips)
3011 nexthop = ps->ds->pd->rtable[i] & 0x1f;
3012
3013 err = _mv88e6xxx_reg_write(
3014 ps, REG_GLOBAL2,
3015 GLOBAL2_DEVICE_MAPPING,
3016 GLOBAL2_DEVICE_MAPPING_UPDATE |
3017 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
3018 if (err)
3019 return err;
3020 }
3021
3022 /* Clear all trunk masks. */
3023 for (i = 0; i < 8; i++) {
3024 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
3025 0x8000 |
3026 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
3027 ((1 << ps->info->num_ports) - 1));
3028 if (err)
3029 return err;
3030 }
3031
3032 /* Clear all trunk mappings. */
3033 for (i = 0; i < 16; i++) {
3034 err = _mv88e6xxx_reg_write(
3035 ps, REG_GLOBAL2,
3036 GLOBAL2_TRUNK_MAPPING,
3037 GLOBAL2_TRUNK_MAPPING_UPDATE |
3038 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
3039 if (err)
3040 return err;
3041 }
3042
3043 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3044 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3045 mv88e6xxx_6320_family(ps)) {
3046 /* Send all frames with destination addresses matching
3047 * 01:80:c2:00:00:2x to the CPU port.
3048 */
3049 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3050 GLOBAL2_MGMT_EN_2X, 0xffff);
3051 if (err)
3052 return err;
3053
3054 /* Initialise cross-chip port VLAN table to reset
3055 * defaults.
3056 */
3057 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3058 GLOBAL2_PVT_ADDR, 0x9000);
3059 if (err)
3060 return err;
3061
3062 /* Clear the priority override table. */
3063 for (i = 0; i < 16; i++) {
3064 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3065 GLOBAL2_PRIO_OVERRIDE,
3066 0x8000 | (i << 8));
3067 if (err)
3068 return err;
3069 }
3070 }
3071
3072 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3073 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3074 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
3075 mv88e6xxx_6320_family(ps)) {
3076 /* Disable ingress rate limiting by resetting all
3077 * ingress rate limit registers to their initial
3078 * state.
3079 */
3080 for (i = 0; i < ps->info->num_ports; i++) {
3081 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3082 GLOBAL2_INGRESS_OP,
3083 0x9000 | (i << 8));
3084 if (err)
3085 return err;
3086 }
3087 }
3088
3089 /* Clear the statistics counters for all ports */
3090 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
3091 GLOBAL_STATS_OP_FLUSH_ALL);
3092 if (err)
3093 return err;
3094
3095 /* Wait for the flush to complete. */
3096 err = _mv88e6xxx_stats_wait(ps);
3097 if (err)
3098 return err;
3099
3100 /* Clear all ATU entries */
3101 err = _mv88e6xxx_atu_flush(ps, 0, true);
3102 if (err)
3103 return err;
3104
3105 /* Clear all the VTU and STU entries */
3106 err = _mv88e6xxx_vtu_stu_flush(ps);
3107 if (err < 0)
3108 return err;
3109
3110 return err;
3111}
3112
Vivien Didelotf81ec902016-05-09 13:22:58 -04003113static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003114{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003115 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003116 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003117 int i;
3118
3119 ps->ds = ds;
Vivien Didelot552238b2016-05-09 13:22:49 -04003120
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003121 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003122
Guenter Roeckfacd95b2015-03-26 18:36:35 -07003123 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
3124
Vivien Didelotd24645b2016-05-09 13:22:41 -04003125 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
3126 mutex_init(&ps->eeprom_mutex);
3127
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003128 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3129 mv88e6xxx_ppu_state_init(ps);
3130
Vivien Didelot552238b2016-05-09 13:22:49 -04003131 mutex_lock(&ps->smi_mutex);
3132
3133 err = mv88e6xxx_switch_reset(ps);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003134 if (err)
3135 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003136
Vivien Didelot08a01262016-05-09 13:22:50 -04003137 err = mv88e6xxx_setup_global(ps);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003138 if (err)
3139 goto unlock;
3140
3141 for (i = 0; i < ps->info->num_ports; i++) {
3142 err = mv88e6xxx_setup_port(ps, i);
3143 if (err)
3144 goto unlock;
3145 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003146
Vivien Didelot6b17e862015-08-13 12:52:18 -04003147unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04003148 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02003149
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003150 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003151}
3152
Andrew Lunn491435852015-04-02 04:06:35 +02003153int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
3154{
3155 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3156 int ret;
3157
Andrew Lunn3898c142015-05-06 01:09:53 +02003158 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04003159 ret = _mv88e6xxx_phy_page_read(ps, port, page, reg);
Andrew Lunn3898c142015-05-06 01:09:53 +02003160 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003161
Andrew Lunn491435852015-04-02 04:06:35 +02003162 return ret;
3163}
3164
3165int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
3166 int reg, int val)
3167{
3168 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3169 int ret;
3170
Andrew Lunn3898c142015-05-06 01:09:53 +02003171 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04003172 ret = _mv88e6xxx_phy_page_write(ps, port, page, reg, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02003173 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003174
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003175 return ret;
3176}
3177
Andrew Lunn158bc062016-04-28 21:24:06 -04003178static int mv88e6xxx_port_to_phy_addr(struct mv88e6xxx_priv_state *ps,
3179 int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003180{
Vivien Didelot009a2b92016-04-17 13:24:01 -04003181 if (port >= 0 && port < ps->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003182 return port;
3183 return -EINVAL;
3184}
3185
Vivien Didelotf81ec902016-05-09 13:22:58 -04003186static int mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003187{
3188 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003189 int addr = mv88e6xxx_port_to_phy_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003190 int ret;
3191
3192 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003193 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003194
Andrew Lunn3898c142015-05-06 01:09:53 +02003195 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003196
3197 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3198 ret = mv88e6xxx_phy_read_ppu(ps, addr, regnum);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003199 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3200 ret = _mv88e6xxx_phy_read_indirect(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003201 else
3202 ret = _mv88e6xxx_phy_read(ps, addr, regnum);
3203
Andrew Lunn3898c142015-05-06 01:09:53 +02003204 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003205 return ret;
3206}
3207
Vivien Didelotf81ec902016-05-09 13:22:58 -04003208static int mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum,
3209 u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003210{
3211 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn158bc062016-04-28 21:24:06 -04003212 int addr = mv88e6xxx_port_to_phy_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003213 int ret;
3214
3215 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003216 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003217
Andrew Lunn3898c142015-05-06 01:09:53 +02003218 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003219
3220 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3221 ret = mv88e6xxx_phy_write_ppu(ps, addr, regnum, val);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003222 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3223 ret = _mv88e6xxx_phy_write_indirect(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003224 else
3225 ret = _mv88e6xxx_phy_write(ps, addr, regnum, val);
3226
Andrew Lunn3898c142015-05-06 01:09:53 +02003227 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003228 return ret;
3229}
3230
Guenter Roeckc22995c2015-07-25 09:42:28 -07003231#ifdef CONFIG_NET_DSA_HWMON
3232
3233static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3234{
3235 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3236 int ret;
3237 int val;
3238
3239 *temp = 0;
3240
3241 mutex_lock(&ps->smi_mutex);
3242
Andrew Lunn158bc062016-04-28 21:24:06 -04003243 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003244 if (ret < 0)
3245 goto error;
3246
3247 /* Enable temperature sensor */
Andrew Lunn158bc062016-04-28 21:24:06 -04003248 ret = _mv88e6xxx_phy_read(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003249 if (ret < 0)
3250 goto error;
3251
Andrew Lunn158bc062016-04-28 21:24:06 -04003252 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003253 if (ret < 0)
3254 goto error;
3255
3256 /* Wait for temperature to stabilize */
3257 usleep_range(10000, 12000);
3258
Andrew Lunn158bc062016-04-28 21:24:06 -04003259 val = _mv88e6xxx_phy_read(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003260 if (val < 0) {
3261 ret = val;
3262 goto error;
3263 }
3264
3265 /* Disable temperature sensor */
Andrew Lunn158bc062016-04-28 21:24:06 -04003266 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003267 if (ret < 0)
3268 goto error;
3269
3270 *temp = ((val & 0x1f) - 5) * 5;
3271
3272error:
Andrew Lunn158bc062016-04-28 21:24:06 -04003273 _mv88e6xxx_phy_write(ps, 0x0, 0x16, 0x0);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003274 mutex_unlock(&ps->smi_mutex);
3275 return ret;
3276}
3277
3278static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3279{
Andrew Lunn158bc062016-04-28 21:24:06 -04003280 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3281 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003282 int ret;
3283
3284 *temp = 0;
3285
3286 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
3287 if (ret < 0)
3288 return ret;
3289
3290 *temp = (ret & 0xff) - 25;
3291
3292 return 0;
3293}
3294
Vivien Didelotf81ec902016-05-09 13:22:58 -04003295static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003296{
Andrew Lunn158bc062016-04-28 21:24:06 -04003297 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3298
Vivien Didelot6594f612016-05-09 13:22:42 -04003299 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP))
3300 return -EOPNOTSUPP;
3301
Andrew Lunn158bc062016-04-28 21:24:06 -04003302 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003303 return mv88e63xx_get_temp(ds, temp);
3304
3305 return mv88e61xx_get_temp(ds, temp);
3306}
3307
Vivien Didelotf81ec902016-05-09 13:22:58 -04003308static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003309{
Andrew Lunn158bc062016-04-28 21:24:06 -04003310 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3311 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003312 int ret;
3313
Vivien Didelot6594f612016-05-09 13:22:42 -04003314 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003315 return -EOPNOTSUPP;
3316
3317 *temp = 0;
3318
3319 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3320 if (ret < 0)
3321 return ret;
3322
3323 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3324
3325 return 0;
3326}
3327
Vivien Didelotf81ec902016-05-09 13:22:58 -04003328static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003329{
Andrew Lunn158bc062016-04-28 21:24:06 -04003330 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3331 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003332 int ret;
3333
Vivien Didelot6594f612016-05-09 13:22:42 -04003334 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003335 return -EOPNOTSUPP;
3336
3337 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3338 if (ret < 0)
3339 return ret;
3340 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3341 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
3342 (ret & 0xe0ff) | (temp << 8));
3343}
3344
Vivien Didelotf81ec902016-05-09 13:22:58 -04003345static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003346{
Andrew Lunn158bc062016-04-28 21:24:06 -04003347 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3348 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003349 int ret;
3350
Vivien Didelot6594f612016-05-09 13:22:42 -04003351 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003352 return -EOPNOTSUPP;
3353
3354 *alarm = false;
3355
3356 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3357 if (ret < 0)
3358 return ret;
3359
3360 *alarm = !!(ret & 0x40);
3361
3362 return 0;
3363}
3364#endif /* CONFIG_NET_DSA_HWMON */
3365
Vivien Didelotf81ec902016-05-09 13:22:58 -04003366static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3367 [MV88E6085] = {
3368 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3369 .family = MV88E6XXX_FAMILY_6097,
3370 .name = "Marvell 88E6085",
3371 .num_databases = 4096,
3372 .num_ports = 10,
3373 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3374 },
3375
3376 [MV88E6095] = {
3377 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3378 .family = MV88E6XXX_FAMILY_6095,
3379 .name = "Marvell 88E6095/88E6095F",
3380 .num_databases = 256,
3381 .num_ports = 11,
3382 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3383 },
3384
3385 [MV88E6123] = {
3386 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3387 .family = MV88E6XXX_FAMILY_6165,
3388 .name = "Marvell 88E6123",
3389 .num_databases = 4096,
3390 .num_ports = 3,
3391 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3392 },
3393
3394 [MV88E6131] = {
3395 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3396 .family = MV88E6XXX_FAMILY_6185,
3397 .name = "Marvell 88E6131",
3398 .num_databases = 256,
3399 .num_ports = 8,
3400 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3401 },
3402
3403 [MV88E6161] = {
3404 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3405 .family = MV88E6XXX_FAMILY_6165,
3406 .name = "Marvell 88E6161",
3407 .num_databases = 4096,
3408 .num_ports = 6,
3409 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3410 },
3411
3412 [MV88E6165] = {
3413 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3414 .family = MV88E6XXX_FAMILY_6165,
3415 .name = "Marvell 88E6165",
3416 .num_databases = 4096,
3417 .num_ports = 6,
3418 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3419 },
3420
3421 [MV88E6171] = {
3422 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3423 .family = MV88E6XXX_FAMILY_6351,
3424 .name = "Marvell 88E6171",
3425 .num_databases = 4096,
3426 .num_ports = 7,
3427 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3428 },
3429
3430 [MV88E6172] = {
3431 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3432 .family = MV88E6XXX_FAMILY_6352,
3433 .name = "Marvell 88E6172",
3434 .num_databases = 4096,
3435 .num_ports = 7,
3436 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3437 },
3438
3439 [MV88E6175] = {
3440 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3441 .family = MV88E6XXX_FAMILY_6351,
3442 .name = "Marvell 88E6175",
3443 .num_databases = 4096,
3444 .num_ports = 7,
3445 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3446 },
3447
3448 [MV88E6176] = {
3449 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3450 .family = MV88E6XXX_FAMILY_6352,
3451 .name = "Marvell 88E6176",
3452 .num_databases = 4096,
3453 .num_ports = 7,
3454 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3455 },
3456
3457 [MV88E6185] = {
3458 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3459 .family = MV88E6XXX_FAMILY_6185,
3460 .name = "Marvell 88E6185",
3461 .num_databases = 256,
3462 .num_ports = 10,
3463 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3464 },
3465
3466 [MV88E6240] = {
3467 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3468 .family = MV88E6XXX_FAMILY_6352,
3469 .name = "Marvell 88E6240",
3470 .num_databases = 4096,
3471 .num_ports = 7,
3472 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3473 },
3474
3475 [MV88E6320] = {
3476 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3477 .family = MV88E6XXX_FAMILY_6320,
3478 .name = "Marvell 88E6320",
3479 .num_databases = 4096,
3480 .num_ports = 7,
3481 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3482 },
3483
3484 [MV88E6321] = {
3485 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3486 .family = MV88E6XXX_FAMILY_6320,
3487 .name = "Marvell 88E6321",
3488 .num_databases = 4096,
3489 .num_ports = 7,
3490 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3491 },
3492
3493 [MV88E6350] = {
3494 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3495 .family = MV88E6XXX_FAMILY_6351,
3496 .name = "Marvell 88E6350",
3497 .num_databases = 4096,
3498 .num_ports = 7,
3499 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3500 },
3501
3502 [MV88E6351] = {
3503 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3504 .family = MV88E6XXX_FAMILY_6351,
3505 .name = "Marvell 88E6351",
3506 .num_databases = 4096,
3507 .num_ports = 7,
3508 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3509 },
3510
3511 [MV88E6352] = {
3512 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3513 .family = MV88E6XXX_FAMILY_6352,
3514 .name = "Marvell 88E6352",
3515 .num_databases = 4096,
3516 .num_ports = 7,
3517 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3518 },
3519};
3520
Vivien Didelotf6271e62016-04-17 13:23:59 -04003521static const struct mv88e6xxx_info *
3522mv88e6xxx_lookup_info(unsigned int prod_num, const struct mv88e6xxx_info *table,
Vivien Didelot0209d142016-04-17 13:23:55 -04003523 unsigned int num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003524{
Vivien Didelota439c062016-04-17 13:23:58 -04003525 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003526
Vivien Didelotb9b37712015-10-30 19:39:48 -04003527 for (i = 0; i < num; ++i)
Vivien Didelotf6271e62016-04-17 13:23:59 -04003528 if (table[i].prod_num == prod_num)
3529 return &table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003530
Vivien Didelotb9b37712015-10-30 19:39:48 -04003531 return NULL;
3532}
3533
Vivien Didelotf81ec902016-05-09 13:22:58 -04003534static const char *mv88e6xxx_probe(struct device *dsa_dev,
3535 struct device *host_dev, int sw_addr,
3536 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003537{
Vivien Didelotf6271e62016-04-17 13:23:59 -04003538 const struct mv88e6xxx_info *info;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003539 struct mv88e6xxx_priv_state *ps;
Vivien Didelota439c062016-04-17 13:23:58 -04003540 struct mii_bus *bus;
Vivien Didelot0209d142016-04-17 13:23:55 -04003541 const char *name;
Vivien Didelota439c062016-04-17 13:23:58 -04003542 int id, prod_num, rev;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003543
Vivien Didelota439c062016-04-17 13:23:58 -04003544 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003545 if (!bus)
3546 return NULL;
3547
Vivien Didelota439c062016-04-17 13:23:58 -04003548 id = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
3549 if (id < 0)
3550 return NULL;
3551
3552 prod_num = (id & 0xfff0) >> 4;
3553 rev = id & 0x000f;
3554
Vivien Didelotf81ec902016-05-09 13:22:58 -04003555 info = mv88e6xxx_lookup_info(prod_num, mv88e6xxx_table,
3556 ARRAY_SIZE(mv88e6xxx_table));
Vivien Didelotf6271e62016-04-17 13:23:59 -04003557 if (!info)
Vivien Didelota439c062016-04-17 13:23:58 -04003558 return NULL;
3559
Vivien Didelotf6271e62016-04-17 13:23:59 -04003560 name = info->name;
3561
Vivien Didelota439c062016-04-17 13:23:58 -04003562 ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
3563 if (!ps)
3564 return NULL;
3565
3566 ps->bus = bus;
3567 ps->sw_addr = sw_addr;
Vivien Didelotf6271e62016-04-17 13:23:59 -04003568 ps->info = info;
Vivien Didelota439c062016-04-17 13:23:58 -04003569
3570 *priv = ps;
3571
3572 dev_info(&ps->bus->dev, "switch 0x%x probed: %s, revision %u\n",
3573 prod_num, name, rev);
3574
Andrew Lunna77d43f2016-04-13 02:40:42 +02003575 return name;
3576}
3577
Vivien Didelotf81ec902016-05-09 13:22:58 -04003578struct dsa_switch_driver mv88e6xxx_switch_driver = {
3579 .tag_protocol = DSA_TAG_PROTO_EDSA,
3580 .probe = mv88e6xxx_probe,
3581 .setup = mv88e6xxx_setup,
3582 .set_addr = mv88e6xxx_set_addr,
3583 .phy_read = mv88e6xxx_phy_read,
3584 .phy_write = mv88e6xxx_phy_write,
3585 .adjust_link = mv88e6xxx_adjust_link,
3586 .get_strings = mv88e6xxx_get_strings,
3587 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3588 .get_sset_count = mv88e6xxx_get_sset_count,
3589 .set_eee = mv88e6xxx_set_eee,
3590 .get_eee = mv88e6xxx_get_eee,
3591#ifdef CONFIG_NET_DSA_HWMON
3592 .get_temp = mv88e6xxx_get_temp,
3593 .get_temp_limit = mv88e6xxx_get_temp_limit,
3594 .set_temp_limit = mv88e6xxx_set_temp_limit,
3595 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3596#endif
3597 .get_eeprom = mv88e6xxx_get_eeprom,
3598 .set_eeprom = mv88e6xxx_set_eeprom,
3599 .get_regs_len = mv88e6xxx_get_regs_len,
3600 .get_regs = mv88e6xxx_get_regs,
3601 .port_bridge_join = mv88e6xxx_port_bridge_join,
3602 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3603 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3604 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3605 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3606 .port_vlan_add = mv88e6xxx_port_vlan_add,
3607 .port_vlan_del = mv88e6xxx_port_vlan_del,
3608 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3609 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3610 .port_fdb_add = mv88e6xxx_port_fdb_add,
3611 .port_fdb_del = mv88e6xxx_port_fdb_del,
3612 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3613};
3614
Ben Hutchings98e67302011-11-25 14:36:19 +00003615static int __init mv88e6xxx_init(void)
3616{
Vivien Didelotf81ec902016-05-09 13:22:58 -04003617 register_switch_driver(&mv88e6xxx_switch_driver);
3618
Ben Hutchings98e67302011-11-25 14:36:19 +00003619 return 0;
3620}
3621module_init(mv88e6xxx_init);
3622
3623static void __exit mv88e6xxx_cleanup(void)
3624{
Vivien Didelotf81ec902016-05-09 13:22:58 -04003625 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003626}
3627module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003628
Vivien Didelotf81ec902016-05-09 13:22:58 -04003629MODULE_ALIAS("platform:mv88e6085");
3630MODULE_ALIAS("platform:mv88e6095");
3631MODULE_ALIAS("platform:mv88e6095f");
3632MODULE_ALIAS("platform:mv88e6123");
3633MODULE_ALIAS("platform:mv88e6131");
3634MODULE_ALIAS("platform:mv88e6161");
3635MODULE_ALIAS("platform:mv88e6165");
3636MODULE_ALIAS("platform:mv88e6171");
3637MODULE_ALIAS("platform:mv88e6172");
3638MODULE_ALIAS("platform:mv88e6175");
3639MODULE_ALIAS("platform:mv88e6176");
3640MODULE_ALIAS("platform:mv88e6320");
3641MODULE_ALIAS("platform:mv88e6321");
3642MODULE_ALIAS("platform:mv88e6350");
3643MODULE_ALIAS("platform:mv88e6351");
3644MODULE_ALIAS("platform:mv88e6352");
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003645MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3646MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3647MODULE_LICENSE("GPL");