blob: 1aee42d1c5f26eb60d37078434e75f8e1434345e [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
Vivien Didelot3996a4f2015-10-30 18:56:45 -040028static void assert_smi_lock(struct dsa_switch *ds)
29{
30 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
31
32 if (unlikely(!mutex_is_locked(&ps->smi_mutex))) {
33 dev_err(ds->master_dev, "SMI lock not held!\n");
34 dump_stack();
35 }
36}
37
Barry Grussling3675c8d2013-01-08 16:05:53 +000038/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000039 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
40 * will be directly accessible on some {device address,register address}
41 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
42 * will only respond to SMI transactions to that specific address, and
43 * an indirect addressing mechanism needs to be used to access its
44 * registers.
45 */
46static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
47{
48 int ret;
49 int i;
50
51 for (i = 0; i < 16; i++) {
Neil Armstrong6e899e62015-10-22 10:37:53 +020052 ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000053 if (ret < 0)
54 return ret;
55
Andrew Lunncca8b132015-04-02 04:06:39 +020056 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000057 return 0;
58 }
59
60 return -ETIMEDOUT;
61}
62
Vivien Didelotb9b37712015-10-30 19:39:48 -040063static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr,
64 int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000065{
66 int ret;
67
68 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +020069 return mdiobus_read_nested(bus, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000070
Barry Grussling3675c8d2013-01-08 16:05:53 +000071 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
73 if (ret < 0)
74 return ret;
75
Barry Grussling3675c8d2013-01-08 16:05:53 +000076 /* Transmit the read command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020077 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
78 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000079 if (ret < 0)
80 return ret;
81
Barry Grussling3675c8d2013-01-08 16:05:53 +000082 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000083 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
84 if (ret < 0)
85 return ret;
86
Barry Grussling3675c8d2013-01-08 16:05:53 +000087 /* Read the data. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020088 ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000089 if (ret < 0)
90 return ret;
91
92 return ret & 0xffff;
93}
94
Guenter Roeck8d6d09e2015-03-26 18:36:31 -070095static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000096{
Guenter Roeckb184e492014-10-17 12:30:58 -070097 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000098 int ret;
99
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400100 assert_smi_lock(ds);
101
Guenter Roeckb184e492014-10-17 12:30:58 -0700102 if (bus == NULL)
103 return -EINVAL;
104
Guenter Roeckb184e492014-10-17 12:30:58 -0700105 ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500106 if (ret < 0)
107 return ret;
108
109 dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
110 addr, reg, ret);
111
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112 return ret;
113}
114
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700115int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
116{
117 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
118 int ret;
119
120 mutex_lock(&ps->smi_mutex);
121 ret = _mv88e6xxx_reg_read(ds, addr, reg);
122 mutex_unlock(&ps->smi_mutex);
123
124 return ret;
125}
126
Vivien Didelotb9b37712015-10-30 19:39:48 -0400127static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
128 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000129{
130 int ret;
131
132 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +0200133 return mdiobus_write_nested(bus, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000134
Barry Grussling3675c8d2013-01-08 16:05:53 +0000135 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000136 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
137 if (ret < 0)
138 return ret;
139
Barry Grussling3675c8d2013-01-08 16:05:53 +0000140 /* Transmit the data to write. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200141 ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000142 if (ret < 0)
143 return ret;
144
Barry Grussling3675c8d2013-01-08 16:05:53 +0000145 /* Transmit the write command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200146 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
147 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000148 if (ret < 0)
149 return ret;
150
Barry Grussling3675c8d2013-01-08 16:05:53 +0000151 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000152 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
153 if (ret < 0)
154 return ret;
155
156 return 0;
157}
158
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700159static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
160 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000161{
Guenter Roeckb184e492014-10-17 12:30:58 -0700162 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000163
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400164 assert_smi_lock(ds);
165
Guenter Roeckb184e492014-10-17 12:30:58 -0700166 if (bus == NULL)
167 return -EINVAL;
168
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500169 dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
170 addr, reg, val);
171
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700172 return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
173}
174
175int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
176{
177 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
178 int ret;
179
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000180 mutex_lock(&ps->smi_mutex);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700181 ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000182 mutex_unlock(&ps->smi_mutex);
183
184 return ret;
185}
186
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000187int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
188{
Andrew Lunncca8b132015-04-02 04:06:39 +0200189 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
190 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
191 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000192
193 return 0;
194}
195
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000196int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
197{
198 int i;
199 int ret;
200
201 for (i = 0; i < 6; i++) {
202 int j;
203
Barry Grussling3675c8d2013-01-08 16:05:53 +0000204 /* Write the MAC address byte. */
Andrew Lunncca8b132015-04-02 04:06:39 +0200205 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
206 GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000207
Barry Grussling3675c8d2013-01-08 16:05:53 +0000208 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000209 for (j = 0; j < 16; j++) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200210 ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC);
211 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000212 break;
213 }
214 if (j == 16)
215 return -ETIMEDOUT;
216 }
217
218 return 0;
219}
220
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200221static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000222{
223 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200224 return _mv88e6xxx_reg_read(ds, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000225 return 0xffff;
226}
227
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200228static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum,
229 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000230{
231 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200232 return _mv88e6xxx_reg_write(ds, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000233 return 0;
234}
235
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000236#ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
237static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
238{
239 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000240 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000241
Andrew Lunncca8b132015-04-02 04:06:39 +0200242 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
243 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
244 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000245
Barry Grussling19b2f972013-01-08 16:05:54 +0000246 timeout = jiffies + 1 * HZ;
247 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200248 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000249 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200250 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
251 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000252 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000253 }
254
255 return -ETIMEDOUT;
256}
257
258static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
259{
260 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000261 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000262
Andrew Lunncca8b132015-04-02 04:06:39 +0200263 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
264 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000265
Barry Grussling19b2f972013-01-08 16:05:54 +0000266 timeout = jiffies + 1 * HZ;
267 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200268 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000269 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200270 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
271 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000272 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000273 }
274
275 return -ETIMEDOUT;
276}
277
278static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
279{
280 struct mv88e6xxx_priv_state *ps;
281
282 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
283 if (mutex_trylock(&ps->ppu_mutex)) {
Barry Grussling85686582013-01-08 16:05:56 +0000284 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000285
Barry Grussling85686582013-01-08 16:05:56 +0000286 if (mv88e6xxx_ppu_enable(ds) == 0)
287 ps->ppu_disabled = 0;
288 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000289 }
290}
291
292static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
293{
294 struct mv88e6xxx_priv_state *ps = (void *)_ps;
295
296 schedule_work(&ps->ppu_work);
297}
298
299static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
300{
Florian Fainellia22adce2014-04-28 11:14:28 -0700301 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000302 int ret;
303
304 mutex_lock(&ps->ppu_mutex);
305
Barry Grussling3675c8d2013-01-08 16:05:53 +0000306 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000307 * we can access the PHY registers. If it was already
308 * disabled, cancel the timer that is going to re-enable
309 * it.
310 */
311 if (!ps->ppu_disabled) {
Barry Grussling85686582013-01-08 16:05:56 +0000312 ret = mv88e6xxx_ppu_disable(ds);
313 if (ret < 0) {
314 mutex_unlock(&ps->ppu_mutex);
315 return ret;
316 }
317 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000318 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000319 del_timer(&ps->ppu_timer);
320 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000321 }
322
323 return ret;
324}
325
326static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
327{
Florian Fainellia22adce2014-04-28 11:14:28 -0700328 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000329
Barry Grussling3675c8d2013-01-08 16:05:53 +0000330 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000331 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
332 mutex_unlock(&ps->ppu_mutex);
333}
334
335void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
336{
Florian Fainellia22adce2014-04-28 11:14:28 -0700337 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000338
339 mutex_init(&ps->ppu_mutex);
340 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
341 init_timer(&ps->ppu_timer);
342 ps->ppu_timer.data = (unsigned long)ps;
343 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
344}
345
346int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
347{
348 int ret;
349
350 ret = mv88e6xxx_ppu_access_get(ds);
351 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000352 ret = mv88e6xxx_reg_read(ds, addr, regnum);
353 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000354 }
355
356 return ret;
357}
358
359int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
360 int regnum, u16 val)
361{
362 int ret;
363
364 ret = mv88e6xxx_ppu_access_get(ds);
365 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000366 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
367 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000368 }
369
370 return ret;
371}
372#endif
373
Andrew Lunn54d792f2015-05-06 01:09:47 +0200374static bool mv88e6xxx_6065_family(struct dsa_switch *ds)
375{
376 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
377
378 switch (ps->id) {
379 case PORT_SWITCH_ID_6031:
380 case PORT_SWITCH_ID_6061:
381 case PORT_SWITCH_ID_6035:
382 case PORT_SWITCH_ID_6065:
383 return true;
384 }
385 return false;
386}
387
388static bool mv88e6xxx_6095_family(struct dsa_switch *ds)
389{
390 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
391
392 switch (ps->id) {
393 case PORT_SWITCH_ID_6092:
394 case PORT_SWITCH_ID_6095:
395 return true;
396 }
397 return false;
398}
399
400static bool mv88e6xxx_6097_family(struct dsa_switch *ds)
401{
402 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
403
404 switch (ps->id) {
405 case PORT_SWITCH_ID_6046:
406 case PORT_SWITCH_ID_6085:
407 case PORT_SWITCH_ID_6096:
408 case PORT_SWITCH_ID_6097:
409 return true;
410 }
411 return false;
412}
413
414static bool mv88e6xxx_6165_family(struct dsa_switch *ds)
415{
416 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
417
418 switch (ps->id) {
419 case PORT_SWITCH_ID_6123:
420 case PORT_SWITCH_ID_6161:
421 case PORT_SWITCH_ID_6165:
422 return true;
423 }
424 return false;
425}
426
427static bool mv88e6xxx_6185_family(struct dsa_switch *ds)
428{
429 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
430
431 switch (ps->id) {
432 case PORT_SWITCH_ID_6121:
433 case PORT_SWITCH_ID_6122:
434 case PORT_SWITCH_ID_6152:
435 case PORT_SWITCH_ID_6155:
436 case PORT_SWITCH_ID_6182:
437 case PORT_SWITCH_ID_6185:
438 case PORT_SWITCH_ID_6108:
439 case PORT_SWITCH_ID_6131:
440 return true;
441 }
442 return false;
443}
444
Guenter Roeckc22995c2015-07-25 09:42:28 -0700445static bool mv88e6xxx_6320_family(struct dsa_switch *ds)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700446{
447 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
448
449 switch (ps->id) {
450 case PORT_SWITCH_ID_6320:
451 case PORT_SWITCH_ID_6321:
452 return true;
453 }
454 return false;
455}
456
Andrew Lunn54d792f2015-05-06 01:09:47 +0200457static bool mv88e6xxx_6351_family(struct dsa_switch *ds)
458{
459 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
460
461 switch (ps->id) {
462 case PORT_SWITCH_ID_6171:
463 case PORT_SWITCH_ID_6175:
464 case PORT_SWITCH_ID_6350:
465 case PORT_SWITCH_ID_6351:
466 return true;
467 }
468 return false;
469}
470
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200471static bool mv88e6xxx_6352_family(struct dsa_switch *ds)
472{
473 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
474
475 switch (ps->id) {
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200476 case PORT_SWITCH_ID_6172:
477 case PORT_SWITCH_ID_6176:
Andrew Lunn54d792f2015-05-06 01:09:47 +0200478 case PORT_SWITCH_ID_6240:
479 case PORT_SWITCH_ID_6352:
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200480 return true;
481 }
482 return false;
483}
484
Andrew Lunndea87022015-08-31 15:56:47 +0200485/* We expect the switch to perform auto negotiation if there is a real
486 * phy. However, in the case of a fixed link phy, we force the port
487 * settings from the fixed link settings.
488 */
489void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
490 struct phy_device *phydev)
491{
492 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200493 u32 reg;
494 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200495
496 if (!phy_is_pseudo_fixed_link(phydev))
497 return;
498
499 mutex_lock(&ps->smi_mutex);
500
501 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
502 if (ret < 0)
503 goto out;
504
505 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
506 PORT_PCS_CTRL_FORCE_LINK |
507 PORT_PCS_CTRL_DUPLEX_FULL |
508 PORT_PCS_CTRL_FORCE_DUPLEX |
509 PORT_PCS_CTRL_UNFORCED);
510
511 reg |= PORT_PCS_CTRL_FORCE_LINK;
512 if (phydev->link)
513 reg |= PORT_PCS_CTRL_LINK_UP;
514
515 if (mv88e6xxx_6065_family(ds) && phydev->speed > SPEED_100)
516 goto out;
517
518 switch (phydev->speed) {
519 case SPEED_1000:
520 reg |= PORT_PCS_CTRL_1000;
521 break;
522 case SPEED_100:
523 reg |= PORT_PCS_CTRL_100;
524 break;
525 case SPEED_10:
526 reg |= PORT_PCS_CTRL_10;
527 break;
528 default:
529 pr_info("Unknown speed");
530 goto out;
531 }
532
533 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
534 if (phydev->duplex == DUPLEX_FULL)
535 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
536
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200537 if ((mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds)) &&
538 (port >= ps->num_ports - 2)) {
539 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
540 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
541 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
542 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
543 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
544 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
545 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
546 }
Andrew Lunndea87022015-08-31 15:56:47 +0200547 _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_PCS_CTRL, reg);
548
549out:
550 mutex_unlock(&ps->smi_mutex);
551}
552
Andrew Lunn31888232015-05-06 01:09:54 +0200553static int _mv88e6xxx_stats_wait(struct dsa_switch *ds)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000554{
555 int ret;
556 int i;
557
558 for (i = 0; i < 10; i++) {
Andrew Lunn31888232015-05-06 01:09:54 +0200559 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200560 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000561 return 0;
562 }
563
564 return -ETIMEDOUT;
565}
566
Andrew Lunn31888232015-05-06 01:09:54 +0200567static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000568{
569 int ret;
570
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700571 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200572 port = (port + 1) << 5;
573
Barry Grussling3675c8d2013-01-08 16:05:53 +0000574 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn31888232015-05-06 01:09:54 +0200575 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
576 GLOBAL_STATS_OP_CAPTURE_PORT |
577 GLOBAL_STATS_OP_HIST_RX_TX | port);
578 if (ret < 0)
579 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000580
Barry Grussling3675c8d2013-01-08 16:05:53 +0000581 /* Wait for the snapshotting to complete. */
Andrew Lunn31888232015-05-06 01:09:54 +0200582 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000583 if (ret < 0)
584 return ret;
585
586 return 0;
587}
588
Andrew Lunn31888232015-05-06 01:09:54 +0200589static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000590{
591 u32 _val;
592 int ret;
593
594 *val = 0;
595
Andrew Lunn31888232015-05-06 01:09:54 +0200596 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
597 GLOBAL_STATS_OP_READ_CAPTURED |
598 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000599 if (ret < 0)
600 return;
601
Andrew Lunn31888232015-05-06 01:09:54 +0200602 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000603 if (ret < 0)
604 return;
605
Andrew Lunn31888232015-05-06 01:09:54 +0200606 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000607 if (ret < 0)
608 return;
609
610 _val = ret << 16;
611
Andrew Lunn31888232015-05-06 01:09:54 +0200612 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000613 if (ret < 0)
614 return;
615
616 *val = _val | ret;
617}
618
Andrew Lunne413e7e2015-04-02 04:06:38 +0200619static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100620 { "in_good_octets", 8, 0x00, BANK0, },
621 { "in_bad_octets", 4, 0x02, BANK0, },
622 { "in_unicast", 4, 0x04, BANK0, },
623 { "in_broadcasts", 4, 0x06, BANK0, },
624 { "in_multicasts", 4, 0x07, BANK0, },
625 { "in_pause", 4, 0x16, BANK0, },
626 { "in_undersize", 4, 0x18, BANK0, },
627 { "in_fragments", 4, 0x19, BANK0, },
628 { "in_oversize", 4, 0x1a, BANK0, },
629 { "in_jabber", 4, 0x1b, BANK0, },
630 { "in_rx_error", 4, 0x1c, BANK0, },
631 { "in_fcs_error", 4, 0x1d, BANK0, },
632 { "out_octets", 8, 0x0e, BANK0, },
633 { "out_unicast", 4, 0x10, BANK0, },
634 { "out_broadcasts", 4, 0x13, BANK0, },
635 { "out_multicasts", 4, 0x12, BANK0, },
636 { "out_pause", 4, 0x15, BANK0, },
637 { "excessive", 4, 0x11, BANK0, },
638 { "collisions", 4, 0x1e, BANK0, },
639 { "deferred", 4, 0x05, BANK0, },
640 { "single", 4, 0x14, BANK0, },
641 { "multiple", 4, 0x17, BANK0, },
642 { "out_fcs_error", 4, 0x03, BANK0, },
643 { "late", 4, 0x1f, BANK0, },
644 { "hist_64bytes", 4, 0x08, BANK0, },
645 { "hist_65_127bytes", 4, 0x09, BANK0, },
646 { "hist_128_255bytes", 4, 0x0a, BANK0, },
647 { "hist_256_511bytes", 4, 0x0b, BANK0, },
648 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
649 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
650 { "sw_in_discards", 4, 0x10, PORT, },
651 { "sw_in_filtered", 2, 0x12, PORT, },
652 { "sw_out_filtered", 2, 0x13, PORT, },
653 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
654 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
655 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
656 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
657 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
658 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
659 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
660 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
661 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
662 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
663 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
664 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
665 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
666 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
667 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
668 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
669 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
670 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
671 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
672 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
673 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
674 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
675 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
676 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
677 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
678 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200679};
680
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100681static bool mv88e6xxx_has_stat(struct dsa_switch *ds,
682 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200683{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100684 switch (stat->type) {
685 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200686 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100687 case BANK1:
688 return mv88e6xxx_6320_family(ds);
689 case PORT:
690 return mv88e6xxx_6095_family(ds) ||
691 mv88e6xxx_6185_family(ds) ||
692 mv88e6xxx_6097_family(ds) ||
693 mv88e6xxx_6165_family(ds) ||
694 mv88e6xxx_6351_family(ds) ||
695 mv88e6xxx_6352_family(ds);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200696 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100697 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000698}
699
Andrew Lunn80c46272015-06-20 18:42:30 +0200700static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100701 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200702 int port)
703{
Andrew Lunn80c46272015-06-20 18:42:30 +0200704 u32 low;
705 u32 high = 0;
706 int ret;
707 u64 value;
708
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100709 switch (s->type) {
710 case PORT:
711 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200712 if (ret < 0)
713 return UINT64_MAX;
714
715 low = ret;
716 if (s->sizeof_stat == 4) {
717 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100718 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200719 if (ret < 0)
720 return UINT64_MAX;
721 high = ret;
722 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100723 break;
724 case BANK0:
725 case BANK1:
Andrew Lunn80c46272015-06-20 18:42:30 +0200726 _mv88e6xxx_stats_read(ds, s->reg, &low);
727 if (s->sizeof_stat == 8)
728 _mv88e6xxx_stats_read(ds, s->reg + 1, &high);
729 }
730 value = (((u64)high) << 16) | low;
731 return value;
732}
733
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100734void mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
735{
736 struct mv88e6xxx_hw_stat *stat;
737 int i, j;
738
739 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
740 stat = &mv88e6xxx_hw_stats[i];
741 if (mv88e6xxx_has_stat(ds, stat)) {
742 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
743 ETH_GSTRING_LEN);
744 j++;
745 }
746 }
747}
748
749int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
750{
751 struct mv88e6xxx_hw_stat *stat;
752 int i, j;
753
754 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
755 stat = &mv88e6xxx_hw_stats[i];
756 if (mv88e6xxx_has_stat(ds, stat))
757 j++;
758 }
759 return j;
760}
761
762void
763mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
764 int port, uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000765{
Florian Fainellia22adce2014-04-28 11:14:28 -0700766 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100767 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000768 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100769 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000770
Andrew Lunn31888232015-05-06 01:09:54 +0200771 mutex_lock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000772
Andrew Lunn31888232015-05-06 01:09:54 +0200773 ret = _mv88e6xxx_stats_snapshot(ds, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000774 if (ret < 0) {
Andrew Lunn31888232015-05-06 01:09:54 +0200775 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000776 return;
777 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100778 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
779 stat = &mv88e6xxx_hw_stats[i];
780 if (mv88e6xxx_has_stat(ds, stat)) {
781 data[j] = _mv88e6xxx_get_ethtool_stat(ds, stat, port);
782 j++;
783 }
784 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000785
Andrew Lunn31888232015-05-06 01:09:54 +0200786 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000787}
Ben Hutchings98e67302011-11-25 14:36:19 +0000788
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700789int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
790{
791 return 32 * sizeof(u16);
792}
793
794void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
795 struct ethtool_regs *regs, void *_p)
796{
797 u16 *p = _p;
798 int i;
799
800 regs->version = 0;
801
802 memset(p, 0xff, 32 * sizeof(u16));
803
804 for (i = 0; i < 32; i++) {
805 int ret;
806
807 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
808 if (ret >= 0)
809 p[i] = ret;
810 }
811}
812
Andrew Lunn3898c142015-05-06 01:09:53 +0200813static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset,
814 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700815{
816 unsigned long timeout = jiffies + HZ / 10;
817
818 while (time_before(jiffies, timeout)) {
819 int ret;
820
821 ret = _mv88e6xxx_reg_read(ds, reg, offset);
822 if (ret < 0)
823 return ret;
824 if (!(ret & mask))
825 return 0;
826
827 usleep_range(1000, 2000);
828 }
829 return -ETIMEDOUT;
830}
831
Andrew Lunn3898c142015-05-06 01:09:53 +0200832static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
833{
834 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
835 int ret;
836
837 mutex_lock(&ps->smi_mutex);
838 ret = _mv88e6xxx_wait(ds, reg, offset, mask);
839 mutex_unlock(&ps->smi_mutex);
840
841 return ret;
842}
843
844static int _mv88e6xxx_phy_wait(struct dsa_switch *ds)
845{
846 return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
847 GLOBAL2_SMI_OP_BUSY);
848}
849
850int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
851{
852 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
853 GLOBAL2_EEPROM_OP_LOAD);
854}
855
856int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
857{
858 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
859 GLOBAL2_EEPROM_OP_BUSY);
860}
861
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700862static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
863{
Andrew Lunncca8b132015-04-02 04:06:39 +0200864 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP,
865 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700866}
867
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200868static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr,
869 int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +0100870{
871 int ret;
872
Andrew Lunn3898c142015-05-06 01:09:53 +0200873 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
874 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
875 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +0100876 if (ret < 0)
877 return ret;
878
Andrew Lunn3898c142015-05-06 01:09:53 +0200879 ret = _mv88e6xxx_phy_wait(ds);
880 if (ret < 0)
881 return ret;
882
883 return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunnf3044682015-02-14 19:17:50 +0100884}
885
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200886static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr,
887 int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +0100888{
Andrew Lunn3898c142015-05-06 01:09:53 +0200889 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100890
Andrew Lunn3898c142015-05-06 01:09:53 +0200891 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
892 if (ret < 0)
893 return ret;
894
895 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
896 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
897 regnum);
898
899 return _mv88e6xxx_phy_wait(ds);
Andrew Lunnf3044682015-02-14 19:17:50 +0100900}
901
Guenter Roeck11b3b452015-03-06 22:23:51 -0800902int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
903{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200904 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800905 int reg;
906
Andrew Lunn3898c142015-05-06 01:09:53 +0200907 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200908
909 reg = _mv88e6xxx_phy_read_indirect(ds, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800910 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200911 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800912
913 e->eee_enabled = !!(reg & 0x0200);
914 e->tx_lpi_enabled = !!(reg & 0x0100);
915
Andrew Lunn3898c142015-05-06 01:09:53 +0200916 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800917 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200918 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800919
Andrew Lunncca8b132015-04-02 04:06:39 +0200920 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200921 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800922
Andrew Lunn2f40c692015-04-02 04:06:37 +0200923out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200924 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200925 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800926}
927
928int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
929 struct phy_device *phydev, struct ethtool_eee *e)
930{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200931 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
932 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800933 int ret;
934
Andrew Lunn3898c142015-05-06 01:09:53 +0200935 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800936
Andrew Lunn2f40c692015-04-02 04:06:37 +0200937 ret = _mv88e6xxx_phy_read_indirect(ds, port, 16);
938 if (ret < 0)
939 goto out;
940
941 reg = ret & ~0x0300;
942 if (e->eee_enabled)
943 reg |= 0x0200;
944 if (e->tx_lpi_enabled)
945 reg |= 0x0100;
946
947 ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg);
948out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200949 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200950
951 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800952}
953
Vivien Didelot70cc99d2015-09-04 14:34:10 -0400954static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700955{
956 int ret;
957
Andrew Lunncca8b132015-04-02 04:06:39 +0200958 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700959 if (ret < 0)
960 return ret;
961
962 return _mv88e6xxx_atu_wait(ds);
963}
964
Vivien Didelot37705b72015-09-04 14:34:11 -0400965static int _mv88e6xxx_atu_data_write(struct dsa_switch *ds,
966 struct mv88e6xxx_atu_entry *entry)
967{
968 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
969
970 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
971 unsigned int mask, shift;
972
973 if (entry->trunk) {
974 data |= GLOBAL_ATU_DATA_TRUNK;
975 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
976 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
977 } else {
978 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
979 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
980 }
981
982 data |= (entry->portv_trunkid << shift) & mask;
983 }
984
985 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, data);
986}
987
Vivien Didelot7fb5e752015-09-04 14:34:12 -0400988static int _mv88e6xxx_atu_flush_move(struct dsa_switch *ds,
989 struct mv88e6xxx_atu_entry *entry,
990 bool static_too)
991{
992 int op;
993 int err;
994
995 err = _mv88e6xxx_atu_wait(ds);
996 if (err)
997 return err;
998
999 err = _mv88e6xxx_atu_data_write(ds, entry);
1000 if (err)
1001 return err;
1002
1003 if (entry->fid) {
1004 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID,
1005 entry->fid);
1006 if (err)
1007 return err;
1008
1009 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1010 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1011 } else {
1012 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1013 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1014 }
1015
1016 return _mv88e6xxx_atu_cmd(ds, op);
1017}
1018
1019static int _mv88e6xxx_atu_flush(struct dsa_switch *ds, u16 fid, bool static_too)
1020{
1021 struct mv88e6xxx_atu_entry entry = {
1022 .fid = fid,
1023 .state = 0, /* EntryState bits must be 0 */
1024 };
1025
1026 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1027}
1028
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001029static int _mv88e6xxx_atu_move(struct dsa_switch *ds, u16 fid, int from_port,
1030 int to_port, bool static_too)
1031{
1032 struct mv88e6xxx_atu_entry entry = {
1033 .trunk = false,
1034 .fid = fid,
1035 };
1036
1037 /* EntryState bits must be 0xF */
1038 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1039
1040 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1041 entry.portv_trunkid = (to_port & 0x0f) << 4;
1042 entry.portv_trunkid |= from_port & 0x0f;
1043
1044 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1045}
1046
1047static int _mv88e6xxx_atu_remove(struct dsa_switch *ds, u16 fid, int port,
1048 bool static_too)
1049{
1050 /* Destination port 0xF means remove the entries */
1051 return _mv88e6xxx_atu_move(ds, fid, port, 0x0f, static_too);
1052}
1053
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001054static const char * const mv88e6xxx_port_state_names[] = {
1055 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1056 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1057 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1058 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1059};
1060
1061static int _mv88e6xxx_port_state(struct dsa_switch *ds, int port, u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001062{
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001063 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001064 u8 oldstate;
1065
Andrew Lunncca8b132015-04-02 04:06:39 +02001066 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001067 if (reg < 0)
1068 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001069
Andrew Lunncca8b132015-04-02 04:06:39 +02001070 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001071
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001072 if (oldstate != state) {
1073 /* Flush forwarding database if we're moving a port
1074 * from Learning or Forwarding state to Disabled or
1075 * Blocking or Listening state.
1076 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001077 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1078 oldstate == PORT_CONTROL_STATE_FORWARDING)
1079 && (state == PORT_CONTROL_STATE_DISABLED ||
1080 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelot2b8157b2015-09-04 14:34:16 -04001081 ret = _mv88e6xxx_atu_remove(ds, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001082 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001083 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001084 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001085
Andrew Lunncca8b132015-04-02 04:06:39 +02001086 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1087 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
1088 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001089 if (ret)
1090 return ret;
1091
1092 netdev_dbg(ds->ports[port], "PortState %s (was %s)\n",
1093 mv88e6xxx_port_state_names[state],
1094 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001095 }
1096
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001097 return ret;
1098}
1099
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001100static int _mv88e6xxx_port_based_vlan_map(struct dsa_switch *ds, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001101{
1102 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001103 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelotede80982015-10-11 18:08:35 -04001104 const u16 mask = (1 << ps->num_ports) - 1;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001105 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001106 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001107 int i;
1108
1109 /* allow CPU port or DSA link(s) to send frames to every port */
1110 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1111 output_ports = mask;
1112 } else {
1113 for (i = 0; i < ps->num_ports; ++i) {
1114 /* allow sending frames to every group member */
1115 if (bridge && ps->ports[i].bridge_dev == bridge)
1116 output_ports |= BIT(i);
1117
1118 /* allow sending frames to CPU port and DSA link(s) */
1119 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1120 output_ports |= BIT(i);
1121 }
1122 }
1123
1124 /* prevent frames from going back out of the port they came in on */
1125 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001126
Vivien Didelotede80982015-10-11 18:08:35 -04001127 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1128 if (reg < 0)
1129 return reg;
1130
1131 reg &= ~mask;
1132 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001133
Andrew Lunncca8b132015-04-02 04:06:39 +02001134 return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001135}
1136
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001137int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state)
1138{
1139 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1140 int stp_state;
1141
1142 switch (state) {
1143 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001144 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001145 break;
1146 case BR_STATE_BLOCKING:
1147 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001148 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001149 break;
1150 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001151 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001152 break;
1153 case BR_STATE_FORWARDING:
1154 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001155 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001156 break;
1157 }
1158
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001159 /* mv88e6xxx_port_stp_update may be called with softirqs disabled,
1160 * so we can not update the port state directly but need to schedule it.
1161 */
Vivien Didelotd715fa62016-02-12 12:09:38 -05001162 ps->ports[port].state = stp_state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001163 set_bit(port, ps->port_state_update_mask);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001164 schedule_work(&ps->bridge_work);
1165
1166 return 0;
1167}
1168
Vivien Didelot5da96032016-03-07 18:24:39 -05001169static int _mv88e6xxx_port_pvid(struct dsa_switch *ds, int port, u16 *new,
1170 u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001171{
Vivien Didelot5da96032016-03-07 18:24:39 -05001172 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001173 int ret;
1174
1175 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN);
1176 if (ret < 0)
1177 return ret;
1178
Vivien Didelot5da96032016-03-07 18:24:39 -05001179 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1180
1181 if (new) {
1182 ret &= ~PORT_DEFAULT_VLAN_MASK;
1183 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1184
1185 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1186 PORT_DEFAULT_VLAN, ret);
1187 if (ret < 0)
1188 return ret;
1189
1190 netdev_dbg(ds->ports[port], "DefaultVID %d (was %d)\n", *new,
1191 pvid);
1192 }
1193
1194 if (old)
1195 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001196
1197 return 0;
1198}
1199
Vivien Didelot5da96032016-03-07 18:24:39 -05001200static int _mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid)
1201{
1202 return _mv88e6xxx_port_pvid(ds, port, NULL, pvid);
1203}
1204
Vivien Didelot76e398a2015-11-01 12:33:55 -05001205static int _mv88e6xxx_port_pvid_set(struct dsa_switch *ds, int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001206{
Vivien Didelot5da96032016-03-07 18:24:39 -05001207 return _mv88e6xxx_port_pvid(ds, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001208}
1209
Vivien Didelot6b17e862015-08-13 12:52:18 -04001210static int _mv88e6xxx_vtu_wait(struct dsa_switch *ds)
1211{
1212 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_VTU_OP,
1213 GLOBAL_VTU_OP_BUSY);
1214}
1215
1216static int _mv88e6xxx_vtu_cmd(struct dsa_switch *ds, u16 op)
1217{
1218 int ret;
1219
1220 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_OP, op);
1221 if (ret < 0)
1222 return ret;
1223
1224 return _mv88e6xxx_vtu_wait(ds);
1225}
1226
1227static int _mv88e6xxx_vtu_stu_flush(struct dsa_switch *ds)
1228{
1229 int ret;
1230
1231 ret = _mv88e6xxx_vtu_wait(ds);
1232 if (ret < 0)
1233 return ret;
1234
1235 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_FLUSH_ALL);
1236}
1237
Vivien Didelotb8fee952015-08-13 12:52:19 -04001238static int _mv88e6xxx_vtu_stu_data_read(struct dsa_switch *ds,
1239 struct mv88e6xxx_vtu_stu_entry *entry,
1240 unsigned int nibble_offset)
1241{
1242 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1243 u16 regs[3];
1244 int i;
1245 int ret;
1246
1247 for (i = 0; i < 3; ++i) {
1248 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1249 GLOBAL_VTU_DATA_0_3 + i);
1250 if (ret < 0)
1251 return ret;
1252
1253 regs[i] = ret;
1254 }
1255
1256 for (i = 0; i < ps->num_ports; ++i) {
1257 unsigned int shift = (i % 4) * 4 + nibble_offset;
1258 u16 reg = regs[i / 4];
1259
1260 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1261 }
1262
1263 return 0;
1264}
1265
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001266static int _mv88e6xxx_vtu_stu_data_write(struct dsa_switch *ds,
1267 struct mv88e6xxx_vtu_stu_entry *entry,
1268 unsigned int nibble_offset)
1269{
1270 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1271 u16 regs[3] = { 0 };
1272 int i;
1273 int ret;
1274
1275 for (i = 0; i < ps->num_ports; ++i) {
1276 unsigned int shift = (i % 4) * 4 + nibble_offset;
1277 u8 data = entry->data[i];
1278
1279 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1280 }
1281
1282 for (i = 0; i < 3; ++i) {
1283 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL,
1284 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1285 if (ret < 0)
1286 return ret;
1287 }
1288
1289 return 0;
1290}
1291
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001292static int _mv88e6xxx_vtu_vid_write(struct dsa_switch *ds, u16 vid)
1293{
1294 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID,
1295 vid & GLOBAL_VTU_VID_MASK);
1296}
1297
1298static int _mv88e6xxx_vtu_getnext(struct dsa_switch *ds,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001299 struct mv88e6xxx_vtu_stu_entry *entry)
1300{
1301 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1302 int ret;
1303
1304 ret = _mv88e6xxx_vtu_wait(ds);
1305 if (ret < 0)
1306 return ret;
1307
Vivien Didelotb8fee952015-08-13 12:52:19 -04001308 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_GET_NEXT);
1309 if (ret < 0)
1310 return ret;
1311
1312 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1313 if (ret < 0)
1314 return ret;
1315
1316 next.vid = ret & GLOBAL_VTU_VID_MASK;
1317 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1318
1319 if (next.valid) {
1320 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 0);
1321 if (ret < 0)
1322 return ret;
1323
1324 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1325 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1326 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1327 GLOBAL_VTU_FID);
1328 if (ret < 0)
1329 return ret;
1330
1331 next.fid = ret & GLOBAL_VTU_FID_MASK;
1332
1333 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1334 GLOBAL_VTU_SID);
1335 if (ret < 0)
1336 return ret;
1337
1338 next.sid = ret & GLOBAL_VTU_SID_MASK;
1339 }
1340 }
1341
1342 *entry = next;
1343 return 0;
1344}
1345
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001346int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1347 struct switchdev_obj_port_vlan *vlan,
1348 int (*cb)(struct switchdev_obj *obj))
1349{
1350 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1351 struct mv88e6xxx_vtu_stu_entry next;
1352 u16 pvid;
1353 int err;
1354
1355 mutex_lock(&ps->smi_mutex);
1356
1357 err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1358 if (err)
1359 goto unlock;
1360
1361 err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1362 if (err)
1363 goto unlock;
1364
1365 do {
1366 err = _mv88e6xxx_vtu_getnext(ds, &next);
1367 if (err)
1368 break;
1369
1370 if (!next.valid)
1371 break;
1372
1373 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1374 continue;
1375
1376 /* reinit and dump this VLAN obj */
1377 vlan->vid_begin = vlan->vid_end = next.vid;
1378 vlan->flags = 0;
1379
1380 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1381 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1382
1383 if (next.vid == pvid)
1384 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1385
1386 err = cb(&vlan->obj);
1387 if (err)
1388 break;
1389 } while (next.vid < GLOBAL_VTU_VID_MASK);
1390
1391unlock:
1392 mutex_unlock(&ps->smi_mutex);
1393
1394 return err;
1395}
1396
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001397static int _mv88e6xxx_vtu_loadpurge(struct dsa_switch *ds,
1398 struct mv88e6xxx_vtu_stu_entry *entry)
1399{
1400 u16 reg = 0;
1401 int ret;
1402
1403 ret = _mv88e6xxx_vtu_wait(ds);
1404 if (ret < 0)
1405 return ret;
1406
1407 if (!entry->valid)
1408 goto loadpurge;
1409
1410 /* Write port member tags */
1411 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 0);
1412 if (ret < 0)
1413 return ret;
1414
1415 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1416 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1417 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1418 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1419 if (ret < 0)
1420 return ret;
1421
1422 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1423 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_FID, reg);
1424 if (ret < 0)
1425 return ret;
1426 }
1427
1428 reg = GLOBAL_VTU_VID_VALID;
1429loadpurge:
1430 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1431 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1432 if (ret < 0)
1433 return ret;
1434
1435 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_LOAD_PURGE);
1436}
1437
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001438static int _mv88e6xxx_stu_getnext(struct dsa_switch *ds, u8 sid,
1439 struct mv88e6xxx_vtu_stu_entry *entry)
1440{
1441 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1442 int ret;
1443
1444 ret = _mv88e6xxx_vtu_wait(ds);
1445 if (ret < 0)
1446 return ret;
1447
1448 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID,
1449 sid & GLOBAL_VTU_SID_MASK);
1450 if (ret < 0)
1451 return ret;
1452
1453 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_GET_NEXT);
1454 if (ret < 0)
1455 return ret;
1456
1457 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_SID);
1458 if (ret < 0)
1459 return ret;
1460
1461 next.sid = ret & GLOBAL_VTU_SID_MASK;
1462
1463 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1464 if (ret < 0)
1465 return ret;
1466
1467 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1468
1469 if (next.valid) {
1470 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 2);
1471 if (ret < 0)
1472 return ret;
1473 }
1474
1475 *entry = next;
1476 return 0;
1477}
1478
1479static int _mv88e6xxx_stu_loadpurge(struct dsa_switch *ds,
1480 struct mv88e6xxx_vtu_stu_entry *entry)
1481{
1482 u16 reg = 0;
1483 int ret;
1484
1485 ret = _mv88e6xxx_vtu_wait(ds);
1486 if (ret < 0)
1487 return ret;
1488
1489 if (!entry->valid)
1490 goto loadpurge;
1491
1492 /* Write port states */
1493 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 2);
1494 if (ret < 0)
1495 return ret;
1496
1497 reg = GLOBAL_VTU_VID_VALID;
1498loadpurge:
1499 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1500 if (ret < 0)
1501 return ret;
1502
1503 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1504 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1505 if (ret < 0)
1506 return ret;
1507
1508 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1509}
1510
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001511static int _mv88e6xxx_port_fid(struct dsa_switch *ds, int port, u16 *new,
1512 u16 *old)
1513{
1514 u16 fid;
1515 int ret;
1516
1517 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1518 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1519 if (ret < 0)
1520 return ret;
1521
1522 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1523
1524 if (new) {
1525 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1526 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1527
1528 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN,
1529 ret);
1530 if (ret < 0)
1531 return ret;
1532 }
1533
1534 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1535 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL_1);
1536 if (ret < 0)
1537 return ret;
1538
1539 fid |= (ret & PORT_CONTROL_1_FID_11_4_MASK) << 4;
1540
1541 if (new) {
1542 ret &= ~PORT_CONTROL_1_FID_11_4_MASK;
1543 ret |= (*new >> 4) & PORT_CONTROL_1_FID_11_4_MASK;
1544
1545 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1,
1546 ret);
1547 if (ret < 0)
1548 return ret;
1549
1550 netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid);
1551 }
1552
1553 if (old)
1554 *old = fid;
1555
1556 return 0;
1557}
1558
1559static int _mv88e6xxx_port_fid_get(struct dsa_switch *ds, int port, u16 *fid)
1560{
1561 return _mv88e6xxx_port_fid(ds, port, NULL, fid);
1562}
1563
1564static int _mv88e6xxx_port_fid_set(struct dsa_switch *ds, int port, u16 fid)
1565{
1566 return _mv88e6xxx_port_fid(ds, port, &fid, NULL);
1567}
1568
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001569static int _mv88e6xxx_fid_new(struct dsa_switch *ds, u16 *fid)
1570{
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001571 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001572 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1573 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001574 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001575
1576 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1577
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001578 /* Set every FID bit used by the (un)bridged ports */
1579 for (i = 0; i < ps->num_ports; ++i) {
1580 err = _mv88e6xxx_port_fid_get(ds, i, fid);
1581 if (err)
1582 return err;
1583
1584 set_bit(*fid, fid_bitmap);
1585 }
1586
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001587 /* Set every FID bit used by the VLAN entries */
1588 err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1589 if (err)
1590 return err;
1591
1592 do {
1593 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1594 if (err)
1595 return err;
1596
1597 if (!vlan.valid)
1598 break;
1599
1600 set_bit(vlan.fid, fid_bitmap);
1601 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1602
1603 /* The reset value 0x000 is used to indicate that multiple address
1604 * databases are not needed. Return the next positive available.
1605 */
1606 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1607 if (unlikely(*fid == MV88E6XXX_N_FID))
1608 return -ENOSPC;
1609
1610 /* Clear the database */
1611 return _mv88e6xxx_atu_flush(ds, *fid, true);
1612}
1613
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001614static int _mv88e6xxx_vtu_new(struct dsa_switch *ds, u16 vid,
1615 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001616{
1617 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1618 struct mv88e6xxx_vtu_stu_entry vlan = {
1619 .valid = true,
1620 .vid = vid,
1621 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001622 int i, err;
1623
1624 err = _mv88e6xxx_fid_new(ds, &vlan.fid);
1625 if (err)
1626 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001627
Vivien Didelot3d131f02015-11-03 10:52:52 -05001628 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001629 for (i = 0; i < ps->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001630 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1631 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1632 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001633
1634 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1635 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1636 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001637
1638 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1639 * implemented, only one STU entry is needed to cover all VTU
1640 * entries. Thus, validate the SID 0.
1641 */
1642 vlan.sid = 0;
1643 err = _mv88e6xxx_stu_getnext(ds, GLOBAL_VTU_SID_MASK, &vstp);
1644 if (err)
1645 return err;
1646
1647 if (vstp.sid != vlan.sid || !vstp.valid) {
1648 memset(&vstp, 0, sizeof(vstp));
1649 vstp.valid = true;
1650 vstp.sid = vlan.sid;
1651
1652 err = _mv88e6xxx_stu_loadpurge(ds, &vstp);
1653 if (err)
1654 return err;
1655 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001656 }
1657
1658 *entry = vlan;
1659 return 0;
1660}
1661
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001662static int _mv88e6xxx_vtu_get(struct dsa_switch *ds, u16 vid,
1663 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1664{
1665 int err;
1666
1667 if (!vid)
1668 return -EINVAL;
1669
1670 err = _mv88e6xxx_vtu_vid_write(ds, vid - 1);
1671 if (err)
1672 return err;
1673
1674 err = _mv88e6xxx_vtu_getnext(ds, entry);
1675 if (err)
1676 return err;
1677
1678 if (entry->vid != vid || !entry->valid) {
1679 if (!creat)
1680 return -EOPNOTSUPP;
1681 /* -ENOENT would've been more appropriate, but switchdev expects
1682 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1683 */
1684
1685 err = _mv88e6xxx_vtu_new(ds, vid, entry);
1686 }
1687
1688 return err;
1689}
1690
Vivien Didelotda9c3592016-02-12 12:09:40 -05001691static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1692 u16 vid_begin, u16 vid_end)
1693{
1694 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1695 struct mv88e6xxx_vtu_stu_entry vlan;
1696 int i, err;
1697
1698 if (!vid_begin)
1699 return -EOPNOTSUPP;
1700
1701 mutex_lock(&ps->smi_mutex);
1702
1703 err = _mv88e6xxx_vtu_vid_write(ds, vid_begin - 1);
1704 if (err)
1705 goto unlock;
1706
1707 do {
1708 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1709 if (err)
1710 goto unlock;
1711
1712 if (!vlan.valid)
1713 break;
1714
1715 if (vlan.vid > vid_end)
1716 break;
1717
1718 for (i = 0; i < ps->num_ports; ++i) {
1719 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1720 continue;
1721
1722 if (vlan.data[i] ==
1723 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1724 continue;
1725
1726 if (ps->ports[i].bridge_dev ==
1727 ps->ports[port].bridge_dev)
1728 break; /* same bridge, check next VLAN */
1729
1730 netdev_warn(ds->ports[port],
1731 "hardware VLAN %d already used by %s\n",
1732 vlan.vid,
1733 netdev_name(ps->ports[i].bridge_dev));
1734 err = -EOPNOTSUPP;
1735 goto unlock;
1736 }
1737 } while (vlan.vid < vid_end);
1738
1739unlock:
1740 mutex_unlock(&ps->smi_mutex);
1741
1742 return err;
1743}
1744
Vivien Didelot214cdb92016-02-26 13:16:08 -05001745static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1746 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1747 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1748 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1749 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1750};
1751
1752int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1753 bool vlan_filtering)
1754{
1755 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1756 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1757 PORT_CONTROL_2_8021Q_DISABLED;
1758 int ret;
1759
1760 mutex_lock(&ps->smi_mutex);
1761
1762 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL_2);
1763 if (ret < 0)
1764 goto unlock;
1765
1766 old = ret & PORT_CONTROL_2_8021Q_MASK;
1767
1768 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1769 ret |= new & PORT_CONTROL_2_8021Q_MASK;
1770
1771 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_2, ret);
1772 if (ret < 0)
1773 goto unlock;
1774
1775 netdev_dbg(ds->ports[port], "802.1Q Mode: %s (was %s)\n",
1776 mv88e6xxx_port_8021q_mode_names[new],
1777 mv88e6xxx_port_8021q_mode_names[old]);
1778unlock:
1779 mutex_unlock(&ps->smi_mutex);
1780
1781 return ret;
1782}
1783
Vivien Didelot76e398a2015-11-01 12:33:55 -05001784int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1785 const struct switchdev_obj_port_vlan *vlan,
1786 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001787{
Vivien Didelotda9c3592016-02-12 12:09:40 -05001788 int err;
1789
Vivien Didelotda9c3592016-02-12 12:09:40 -05001790 /* If the requested port doesn't belong to the same bridge as the VLAN
1791 * members, do not support it (yet) and fallback to software VLAN.
1792 */
1793 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1794 vlan->vid_end);
1795 if (err)
1796 return err;
1797
Vivien Didelot76e398a2015-11-01 12:33:55 -05001798 /* We don't need any dynamic resource from the kernel (yet),
1799 * so skip the prepare phase.
1800 */
1801 return 0;
1802}
1803
1804static int _mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, u16 vid,
1805 bool untagged)
1806{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001807 struct mv88e6xxx_vtu_stu_entry vlan;
1808 int err;
1809
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001810 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001811 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001812 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001813
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001814 vlan.data[port] = untagged ?
1815 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1816 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1817
Vivien Didelot76e398a2015-11-01 12:33:55 -05001818 return _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1819}
1820
1821int mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1822 const struct switchdev_obj_port_vlan *vlan,
1823 struct switchdev_trans *trans)
1824{
1825 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1826 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1827 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1828 u16 vid;
1829 int err = 0;
1830
1831 mutex_lock(&ps->smi_mutex);
1832
1833 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1834 err = _mv88e6xxx_port_vlan_add(ds, port, vid, untagged);
1835 if (err)
1836 goto unlock;
1837 }
1838
1839 /* no PVID with ranges, otherwise it's a bug */
1840 if (pvid)
Russell Kingdb0e51a2016-01-24 09:22:05 +00001841 err = _mv88e6xxx_port_pvid_set(ds, port, vlan->vid_end);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001842unlock:
1843 mutex_unlock(&ps->smi_mutex);
1844
1845 return err;
1846}
1847
Vivien Didelot76e398a2015-11-01 12:33:55 -05001848static int _mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001849{
1850 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1851 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001852 int i, err;
1853
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001854 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001855 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001856 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001857
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001858 /* Tell switchdev if this VLAN is handled in software */
1859 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001860 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001861
1862 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1863
1864 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001865 vlan.valid = false;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001866 for (i = 0; i < ps->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001867 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001868 continue;
1869
1870 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001871 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001872 break;
1873 }
1874 }
1875
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001876 err = _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1877 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001878 return err;
1879
1880 return _mv88e6xxx_atu_remove(ds, vlan.fid, port, false);
1881}
1882
1883int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1884 const struct switchdev_obj_port_vlan *vlan)
1885{
1886 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1887 u16 pvid, vid;
1888 int err = 0;
1889
1890 mutex_lock(&ps->smi_mutex);
1891
1892 err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1893 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001894 goto unlock;
1895
Vivien Didelot76e398a2015-11-01 12:33:55 -05001896 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1897 err = _mv88e6xxx_port_vlan_del(ds, port, vid);
1898 if (err)
1899 goto unlock;
1900
1901 if (vid == pvid) {
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05001902 err = _mv88e6xxx_port_pvid_set(ds, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001903 if (err)
1904 goto unlock;
1905 }
1906 }
1907
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001908unlock:
1909 mutex_unlock(&ps->smi_mutex);
1910
1911 return err;
1912}
1913
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001914static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds,
1915 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001916{
1917 int i, ret;
1918
1919 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001920 ret = _mv88e6xxx_reg_write(
1921 ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
1922 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001923 if (ret < 0)
1924 return ret;
1925 }
1926
1927 return 0;
1928}
1929
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001930static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001931{
1932 int i, ret;
1933
1934 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001935 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1936 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001937 if (ret < 0)
1938 return ret;
1939 addr[i * 2] = ret >> 8;
1940 addr[i * 2 + 1] = ret & 0xff;
1941 }
1942
1943 return 0;
1944}
1945
Vivien Didelotfd231c82015-08-10 09:09:50 -04001946static int _mv88e6xxx_atu_load(struct dsa_switch *ds,
1947 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001948{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001949 int ret;
1950
1951 ret = _mv88e6xxx_atu_wait(ds);
1952 if (ret < 0)
1953 return ret;
1954
Vivien Didelotfd231c82015-08-10 09:09:50 -04001955 ret = _mv88e6xxx_atu_mac_write(ds, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001956 if (ret < 0)
1957 return ret;
1958
Vivien Didelot37705b72015-09-04 14:34:11 -04001959 ret = _mv88e6xxx_atu_data_write(ds, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04001960 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001961 return ret;
1962
Vivien Didelot70cc99d2015-09-04 14:34:10 -04001963 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, entry->fid);
1964 if (ret < 0)
1965 return ret;
1966
1967 return _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04001968}
David S. Millercdf09692015-08-11 12:00:37 -07001969
Vivien Didelotfd231c82015-08-10 09:09:50 -04001970static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port,
1971 const unsigned char *addr, u16 vid,
1972 u8 state)
1973{
1974 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001975 struct mv88e6xxx_vtu_stu_entry vlan;
1976 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04001977
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001978 /* Null VLAN ID corresponds to the port private database */
1979 if (vid == 0)
1980 err = _mv88e6xxx_port_fid_get(ds, port, &vlan.fid);
1981 else
1982 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001983 if (err)
1984 return err;
1985
1986 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04001987 entry.state = state;
1988 ether_addr_copy(entry.mac, addr);
1989 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1990 entry.trunk = false;
1991 entry.portv_trunkid = BIT(port);
1992 }
1993
1994 return _mv88e6xxx_atu_load(ds, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001995}
1996
Vivien Didelot146a3202015-10-08 11:35:12 -04001997int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
1998 const struct switchdev_obj_port_fdb *fdb,
1999 struct switchdev_trans *trans)
2000{
2001 /* We don't need any dynamic resource from the kernel (yet),
2002 * so skip the prepare phase.
2003 */
2004 return 0;
2005}
2006
David S. Millercdf09692015-08-11 12:00:37 -07002007int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002008 const struct switchdev_obj_port_fdb *fdb,
2009 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002010{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002011 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002012 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2013 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2014 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002015 int ret;
2016
David S. Millercdf09692015-08-11 12:00:37 -07002017 mutex_lock(&ps->smi_mutex);
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002018 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, state);
David S. Millercdf09692015-08-11 12:00:37 -07002019 mutex_unlock(&ps->smi_mutex);
2020
2021 return ret;
2022}
2023
2024int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
Vivien Didelot8057b3e2015-10-08 11:35:14 -04002025 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002026{
2027 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2028 int ret;
2029
2030 mutex_lock(&ps->smi_mutex);
Vivien Didelot8057b3e2015-10-08 11:35:14 -04002031 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002032 GLOBAL_ATU_DATA_STATE_UNUSED);
2033 mutex_unlock(&ps->smi_mutex);
2034
2035 return ret;
2036}
2037
Vivien Didelot1d194042015-08-10 09:09:51 -04002038static int _mv88e6xxx_atu_getnext(struct dsa_switch *ds, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002039 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002040{
Vivien Didelot1d194042015-08-10 09:09:51 -04002041 struct mv88e6xxx_atu_entry next = { 0 };
2042 int ret;
2043
2044 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002045
2046 ret = _mv88e6xxx_atu_wait(ds);
2047 if (ret < 0)
2048 return ret;
2049
Vivien Didelot70cc99d2015-09-04 14:34:10 -04002050 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid);
2051 if (ret < 0)
2052 return ret;
2053
2054 ret = _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002055 if (ret < 0)
2056 return ret;
2057
Vivien Didelot1d194042015-08-10 09:09:51 -04002058 ret = _mv88e6xxx_atu_mac_read(ds, next.mac);
2059 if (ret < 0)
2060 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002061
Vivien Didelot1d194042015-08-10 09:09:51 -04002062 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
2063 if (ret < 0)
2064 return ret;
2065
2066 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2067 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2068 unsigned int mask, shift;
2069
2070 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2071 next.trunk = true;
2072 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2073 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2074 } else {
2075 next.trunk = false;
2076 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2077 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2078 }
2079
2080 next.portv_trunkid = (ret & mask) >> shift;
2081 }
2082
2083 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002084 return 0;
2085}
2086
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002087static int _mv88e6xxx_port_fdb_dump_one(struct dsa_switch *ds, u16 fid, u16 vid,
2088 int port,
2089 struct switchdev_obj_port_fdb *fdb,
2090 int (*cb)(struct switchdev_obj *obj))
2091{
2092 struct mv88e6xxx_atu_entry addr = {
2093 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2094 };
2095 int err;
2096
2097 err = _mv88e6xxx_atu_mac_write(ds, addr.mac);
2098 if (err)
2099 return err;
2100
2101 do {
2102 err = _mv88e6xxx_atu_getnext(ds, fid, &addr);
2103 if (err)
2104 break;
2105
2106 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2107 break;
2108
2109 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2110 bool is_static = addr.state ==
2111 (is_multicast_ether_addr(addr.mac) ?
2112 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2113 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2114
2115 fdb->vid = vid;
2116 ether_addr_copy(fdb->addr, addr.mac);
2117 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2118
2119 err = cb(&fdb->obj);
2120 if (err)
2121 break;
2122 }
2123 } while (!is_broadcast_ether_addr(addr.mac));
2124
2125 return err;
2126}
2127
Vivien Didelotf33475b2015-10-22 09:34:41 -04002128int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2129 struct switchdev_obj_port_fdb *fdb,
2130 int (*cb)(struct switchdev_obj *obj))
2131{
2132 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2133 struct mv88e6xxx_vtu_stu_entry vlan = {
2134 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2135 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002136 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002137 int err;
2138
2139 mutex_lock(&ps->smi_mutex);
2140
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002141 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2142 err = _mv88e6xxx_port_fid_get(ds, port, &fid);
2143 if (err)
2144 goto unlock;
2145
2146 err = _mv88e6xxx_port_fdb_dump_one(ds, fid, 0, port, fdb, cb);
2147 if (err)
2148 goto unlock;
2149
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002150 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotf33475b2015-10-22 09:34:41 -04002151 err = _mv88e6xxx_vtu_vid_write(ds, vlan.vid);
2152 if (err)
2153 goto unlock;
2154
2155 do {
Vivien Didelotf33475b2015-10-22 09:34:41 -04002156 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
2157 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002158 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002159
2160 if (!vlan.valid)
2161 break;
2162
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002163 err = _mv88e6xxx_port_fdb_dump_one(ds, vlan.fid, vlan.vid, port,
2164 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002165 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002166 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002167 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2168
2169unlock:
2170 mutex_unlock(&ps->smi_mutex);
2171
2172 return err;
2173}
2174
Vivien Didelota6692752016-02-12 12:09:39 -05002175int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2176 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002177{
Vivien Didelota6692752016-02-12 12:09:39 -05002178 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002179 u16 fid;
2180 int i, err;
2181
2182 mutex_lock(&ps->smi_mutex);
2183
2184 /* Get or create the bridge FID and assign it to the port */
2185 for (i = 0; i < ps->num_ports; ++i)
2186 if (ps->ports[i].bridge_dev == bridge)
2187 break;
2188
2189 if (i < ps->num_ports)
2190 err = _mv88e6xxx_port_fid_get(ds, i, &fid);
2191 else
2192 err = _mv88e6xxx_fid_new(ds, &fid);
2193 if (err)
2194 goto unlock;
2195
2196 err = _mv88e6xxx_port_fid_set(ds, port, fid);
2197 if (err)
2198 goto unlock;
Vivien Didelota6692752016-02-12 12:09:39 -05002199
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002200 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002201 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002202
2203 for (i = 0; i < ps->num_ports; ++i) {
2204 if (ps->ports[i].bridge_dev == bridge) {
2205 err = _mv88e6xxx_port_based_vlan_map(ds, i);
2206 if (err)
2207 break;
2208 }
2209 }
2210
Vivien Didelot466dfa02016-02-26 13:16:05 -05002211unlock:
2212 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002213
Vivien Didelot466dfa02016-02-26 13:16:05 -05002214 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002215}
2216
Vivien Didelota6692752016-02-12 12:09:39 -05002217int mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002218{
Vivien Didelota6692752016-02-12 12:09:39 -05002219 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002220 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002221 u16 fid;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002222 int i, err;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002223
2224 mutex_lock(&ps->smi_mutex);
2225
2226 /* Give the port a fresh Filtering Information Database */
2227 err = _mv88e6xxx_fid_new(ds, &fid);
2228 if (err)
2229 goto unlock;
2230
2231 err = _mv88e6xxx_port_fid_set(ds, port, fid);
2232 if (err)
2233 goto unlock;
Vivien Didelota6692752016-02-12 12:09:39 -05002234
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002235 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002236 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002237
2238 for (i = 0; i < ps->num_ports; ++i) {
2239 if (i == port || ps->ports[i].bridge_dev == bridge) {
2240 err = _mv88e6xxx_port_based_vlan_map(ds, i);
2241 if (err)
2242 break;
2243 }
2244 }
2245
Vivien Didelot466dfa02016-02-26 13:16:05 -05002246unlock:
2247 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002248
Vivien Didelot466dfa02016-02-26 13:16:05 -05002249 return err;
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002250}
2251
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002252static void mv88e6xxx_bridge_work(struct work_struct *work)
2253{
2254 struct mv88e6xxx_priv_state *ps;
2255 struct dsa_switch *ds;
2256 int port;
2257
2258 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
2259 ds = ((struct dsa_switch *)ps) - 1;
2260
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002261 mutex_lock(&ps->smi_mutex);
2262
2263 for (port = 0; port < ps->num_ports; ++port)
2264 if (test_and_clear_bit(port, ps->port_state_update_mask) &&
2265 _mv88e6xxx_port_state(ds, port, ps->ports[port].state))
2266 netdev_warn(ds->ports[port], "failed to update state to %s\n",
2267 mv88e6xxx_port_state_names[ps->ports[port].state]);
2268
2269 mutex_unlock(&ps->smi_mutex);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002270}
2271
Andrew Lunndbde9e62015-05-06 01:09:48 +02002272static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002273{
2274 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002275 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002276 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002277
2278 mutex_lock(&ps->smi_mutex);
2279
Andrew Lunn54d792f2015-05-06 01:09:47 +02002280 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2281 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2282 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002283 mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002284 /* MAC Forcing register: don't force link, speed,
2285 * duplex or flow control state to any particular
2286 * values on physical ports, but force the CPU port
2287 * and all DSA ports to their maximum bandwidth and
2288 * full duplex.
2289 */
2290 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002291 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002292 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002293 reg |= PORT_PCS_CTRL_FORCE_LINK |
2294 PORT_PCS_CTRL_LINK_UP |
2295 PORT_PCS_CTRL_DUPLEX_FULL |
2296 PORT_PCS_CTRL_FORCE_DUPLEX;
2297 if (mv88e6xxx_6065_family(ds))
2298 reg |= PORT_PCS_CTRL_100;
2299 else
2300 reg |= PORT_PCS_CTRL_1000;
2301 } else {
2302 reg |= PORT_PCS_CTRL_UNFORCED;
2303 }
2304
2305 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2306 PORT_PCS_CTRL, reg);
2307 if (ret)
2308 goto abort;
2309 }
2310
2311 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2312 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2313 * tunneling, determine priority by looking at 802.1p and IP
2314 * priority fields (IP prio has precedence), and set STP state
2315 * to Forwarding.
2316 *
2317 * If this is the CPU link, use DSA or EDSA tagging depending
2318 * on which tagging mode was configured.
2319 *
2320 * If this is a link to another switch, use DSA tagging mode.
2321 *
2322 * If this is the upstream port for this switch, enable
2323 * forwarding of unknown unicasts and multicasts.
2324 */
2325 reg = 0;
2326 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2327 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2328 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002329 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002330 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2331 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2332 PORT_CONTROL_STATE_FORWARDING;
2333 if (dsa_is_cpu_port(ds, port)) {
2334 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2335 reg |= PORT_CONTROL_DSA_TAG;
2336 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002337 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2338 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002339 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2340 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2341 else
2342 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002343 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2344 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002345 }
2346
2347 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2348 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2349 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002350 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002351 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2352 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2353 }
2354 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002355 if (dsa_is_dsa_port(ds, port)) {
2356 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2357 reg |= PORT_CONTROL_DSA_TAG;
2358 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2359 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2360 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002361 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002362 }
2363
Andrew Lunn54d792f2015-05-06 01:09:47 +02002364 if (port == dsa_upstream_port(ds))
2365 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2366 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2367 }
2368 if (reg) {
2369 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2370 PORT_CONTROL, reg);
2371 if (ret)
2372 goto abort;
2373 }
2374
Vivien Didelot8efdda42015-08-13 12:52:23 -04002375 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002376 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002377 * untagged frames on this port, do a destination address lookup on all
2378 * received packets as usual, disable ARP mirroring and don't send a
2379 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002380 */
2381 reg = 0;
2382 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2383 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002384 mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002385 reg = PORT_CONTROL_2_MAP_DA;
2386
2387 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002388 mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002389 reg |= PORT_CONTROL_2_JUMBO_10240;
2390
2391 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) {
2392 /* Set the upstream port this port should use */
2393 reg |= dsa_upstream_port(ds);
2394 /* enable forwarding of unknown multicast addresses to
2395 * the upstream port
2396 */
2397 if (port == dsa_upstream_port(ds))
2398 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2399 }
2400
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002401 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002402
Andrew Lunn54d792f2015-05-06 01:09:47 +02002403 if (reg) {
2404 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2405 PORT_CONTROL_2, reg);
2406 if (ret)
2407 goto abort;
2408 }
2409
2410 /* Port Association Vector: when learning source addresses
2411 * of packets, add the address to the address database using
2412 * a port bitmap that has only the bit for this port set and
2413 * the other bits clear.
2414 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002415 reg = 1 << port;
2416 /* Disable learning for DSA and CPU ports */
2417 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2418 reg = PORT_ASSOC_VECTOR_LOCKED_PORT;
2419
2420 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002421 if (ret)
2422 goto abort;
2423
2424 /* Egress rate control 2: disable egress rate control. */
2425 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2,
2426 0x0000);
2427 if (ret)
2428 goto abort;
2429
2430 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002431 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2432 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002433 /* Do not limit the period of time that this port can
2434 * be paused for by the remote end or the period of
2435 * time that this port can pause the remote end.
2436 */
2437 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2438 PORT_PAUSE_CTRL, 0x0000);
2439 if (ret)
2440 goto abort;
2441
2442 /* Port ATU control: disable limiting the number of
2443 * address database entries that this port is allowed
2444 * to use.
2445 */
2446 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2447 PORT_ATU_CONTROL, 0x0000);
2448 /* Priority Override: disable DA, SA and VTU priority
2449 * override.
2450 */
2451 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2452 PORT_PRI_OVERRIDE, 0x0000);
2453 if (ret)
2454 goto abort;
2455
2456 /* Port Ethertype: use the Ethertype DSA Ethertype
2457 * value.
2458 */
2459 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2460 PORT_ETH_TYPE, ETH_P_EDSA);
2461 if (ret)
2462 goto abort;
2463 /* Tag Remap: use an identity 802.1p prio -> switch
2464 * prio mapping.
2465 */
2466 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2467 PORT_TAG_REGMAP_0123, 0x3210);
2468 if (ret)
2469 goto abort;
2470
2471 /* Tag Remap 2: use an identity 802.1p prio -> switch
2472 * prio mapping.
2473 */
2474 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2475 PORT_TAG_REGMAP_4567, 0x7654);
2476 if (ret)
2477 goto abort;
2478 }
2479
2480 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2481 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002482 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2483 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002484 /* Rate Control: disable ingress rate limiting. */
2485 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2486 PORT_RATE_CONTROL, 0x0001);
2487 if (ret)
2488 goto abort;
2489 }
2490
Guenter Roeck366f0a02015-03-26 18:36:30 -07002491 /* Port Control 1: disable trunking, disable sending
2492 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002493 */
Vivien Didelot614f03f2015-04-20 17:19:23 -04002494 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002495 if (ret)
2496 goto abort;
2497
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002498 /* Port based VLAN map: give each port its own address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002499 * database, and allow bidirectional communication between the
2500 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002501 */
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002502 ret = _mv88e6xxx_port_fid_set(ds, port, port + 1);
2503 if (ret)
2504 goto abort;
2505
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002506 ret = _mv88e6xxx_port_based_vlan_map(ds, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002507 if (ret)
2508 goto abort;
2509
2510 /* Default VLAN ID and priority: don't set a default VLAN
2511 * ID, and set the default packet priority to zero.
2512 */
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002513 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
2514 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002515abort:
2516 mutex_unlock(&ps->smi_mutex);
2517 return ret;
2518}
2519
Andrew Lunndbde9e62015-05-06 01:09:48 +02002520int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2521{
2522 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2523 int ret;
2524 int i;
2525
2526 for (i = 0; i < ps->num_ports; i++) {
2527 ret = mv88e6xxx_setup_port(ds, i);
2528 if (ret < 0)
2529 return ret;
2530 }
2531 return 0;
2532}
2533
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002534int mv88e6xxx_setup_common(struct dsa_switch *ds)
2535{
2536 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2537
2538 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002539
Andrew Lunncca8b132015-04-02 04:06:39 +02002540 ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
Andrew Lunna8f064c2015-03-26 18:36:40 -07002541
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002542 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
2543
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002544 return 0;
2545}
2546
Andrew Lunn54d792f2015-05-06 01:09:47 +02002547int mv88e6xxx_setup_global(struct dsa_switch *ds)
2548{
2549 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot24751e22015-08-03 09:17:44 -04002550 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002551 int i;
2552
2553 /* Set the default address aging time to 5 minutes, and
2554 * enable address learn messages to be sent to all message
2555 * ports.
2556 */
2557 REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
2558 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2559
2560 /* Configure the IP ToS mapping registers. */
2561 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2562 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2563 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2564 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2565 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2566 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2567 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2568 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2569
2570 /* Configure the IEEE 802.1p priority mapping register. */
2571 REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2572
2573 /* Send all frames with destination addresses matching
2574 * 01:80:c2:00:00:0x to the CPU port.
2575 */
2576 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
2577
2578 /* Ignore removed tag data on doubly tagged packets, disable
2579 * flow control messages, force flow control priority to the
2580 * highest, and send all special multicast frames to the CPU
2581 * port at the highest priority.
2582 */
2583 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
2584 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
2585 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
2586
2587 /* Program the DSA routing table. */
2588 for (i = 0; i < 32; i++) {
2589 int nexthop = 0x1f;
2590
2591 if (ds->pd->rtable &&
2592 i != ds->index && i < ds->dst->pd->nr_chips)
2593 nexthop = ds->pd->rtable[i] & 0x1f;
2594
2595 REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
2596 GLOBAL2_DEVICE_MAPPING_UPDATE |
2597 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) |
2598 nexthop);
2599 }
2600
2601 /* Clear all trunk masks. */
2602 for (i = 0; i < 8; i++)
2603 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
2604 0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
2605 ((1 << ps->num_ports) - 1));
2606
2607 /* Clear all trunk mappings. */
2608 for (i = 0; i < 16; i++)
2609 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING,
2610 GLOBAL2_TRUNK_MAPPING_UPDATE |
2611 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
2612
2613 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002614 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2615 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002616 /* Send all frames with destination addresses matching
2617 * 01:80:c2:00:00:2x to the CPU port.
2618 */
2619 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff);
2620
2621 /* Initialise cross-chip port VLAN table to reset
2622 * defaults.
2623 */
2624 REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000);
2625
2626 /* Clear the priority override table. */
2627 for (i = 0; i < 16; i++)
2628 REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE,
2629 0x8000 | (i << 8));
2630 }
2631
2632 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2633 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002634 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2635 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002636 /* Disable ingress rate limiting by resetting all
2637 * ingress rate limit registers to their initial
2638 * state.
2639 */
2640 for (i = 0; i < ps->num_ports; i++)
2641 REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP,
2642 0x9000 | (i << 8));
2643 }
2644
Andrew Lunndb687a52015-06-20 21:31:29 +02002645 /* Clear the statistics counters for all ports */
2646 REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP, GLOBAL_STATS_OP_FLUSH_ALL);
2647
2648 /* Wait for the flush to complete. */
Vivien Didelot24751e22015-08-03 09:17:44 -04002649 mutex_lock(&ps->smi_mutex);
2650 ret = _mv88e6xxx_stats_wait(ds);
Vivien Didelot6b17e862015-08-13 12:52:18 -04002651 if (ret < 0)
2652 goto unlock;
2653
Vivien Didelotc161d0a2015-09-04 14:34:13 -04002654 /* Clear all ATU entries */
2655 ret = _mv88e6xxx_atu_flush(ds, 0, true);
2656 if (ret < 0)
2657 goto unlock;
2658
Vivien Didelot6b17e862015-08-13 12:52:18 -04002659 /* Clear all the VTU and STU entries */
2660 ret = _mv88e6xxx_vtu_stu_flush(ds);
2661unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04002662 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02002663
Vivien Didelot24751e22015-08-03 09:17:44 -04002664 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002665}
2666
Andrew Lunn143a8302015-04-02 04:06:34 +02002667int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
2668{
2669 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2670 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +01002671 struct gpio_desc *gpiod = ds->pd->reset;
Andrew Lunn143a8302015-04-02 04:06:34 +02002672 unsigned long timeout;
2673 int ret;
2674 int i;
2675
2676 /* Set all ports to the disabled state. */
2677 for (i = 0; i < ps->num_ports; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002678 ret = REG_READ(REG_PORT(i), PORT_CONTROL);
2679 REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc);
Andrew Lunn143a8302015-04-02 04:06:34 +02002680 }
2681
2682 /* Wait for transmit queues to drain. */
2683 usleep_range(2000, 4000);
2684
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +01002685 /* If there is a gpio connected to the reset pin, toggle it */
2686 if (gpiod) {
2687 gpiod_set_value_cansleep(gpiod, 1);
2688 usleep_range(10000, 20000);
2689 gpiod_set_value_cansleep(gpiod, 0);
2690 usleep_range(10000, 20000);
2691 }
2692
Andrew Lunn143a8302015-04-02 04:06:34 +02002693 /* Reset the switch. Keep the PPU active if requested. The PPU
2694 * needs to be active to support indirect phy register access
2695 * through global registers 0x18 and 0x19.
2696 */
2697 if (ppu_active)
2698 REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
2699 else
2700 REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
2701
2702 /* Wait up to one second for reset to complete. */
2703 timeout = jiffies + 1 * HZ;
2704 while (time_before(jiffies, timeout)) {
2705 ret = REG_READ(REG_GLOBAL, 0x00);
2706 if ((ret & is_reset) == is_reset)
2707 break;
2708 usleep_range(1000, 2000);
2709 }
2710 if (time_after(jiffies, timeout))
2711 return -ETIMEDOUT;
2712
2713 return 0;
2714}
2715
Andrew Lunn491435852015-04-02 04:06:35 +02002716int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
2717{
2718 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2719 int ret;
2720
Andrew Lunn3898c142015-05-06 01:09:53 +02002721 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002722 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02002723 if (ret < 0)
2724 goto error;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002725 ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
Andrew Lunn491435852015-04-02 04:06:35 +02002726error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002727 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02002728 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002729 return ret;
2730}
2731
2732int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2733 int reg, int val)
2734{
2735 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2736 int ret;
2737
Andrew Lunn3898c142015-05-06 01:09:53 +02002738 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002739 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02002740 if (ret < 0)
2741 goto error;
2742
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002743 ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
Andrew Lunn491435852015-04-02 04:06:35 +02002744error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002745 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02002746 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002747 return ret;
2748}
2749
2750static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
2751{
2752 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2753
2754 if (port >= 0 && port < ps->num_ports)
2755 return port;
2756 return -EINVAL;
2757}
2758
2759int
2760mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
2761{
2762 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2763 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2764 int ret;
2765
2766 if (addr < 0)
2767 return addr;
2768
Andrew Lunn3898c142015-05-06 01:09:53 +02002769 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002770 ret = _mv88e6xxx_phy_read(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002771 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002772 return ret;
2773}
2774
2775int
2776mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2777{
2778 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2779 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2780 int ret;
2781
2782 if (addr < 0)
2783 return addr;
2784
Andrew Lunn3898c142015-05-06 01:09:53 +02002785 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002786 ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002787 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002788 return ret;
2789}
2790
2791int
2792mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
2793{
2794 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2795 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2796 int ret;
2797
2798 if (addr < 0)
2799 return addr;
2800
Andrew Lunn3898c142015-05-06 01:09:53 +02002801 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002802 ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002803 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002804 return ret;
2805}
2806
2807int
2808mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
2809 u16 val)
2810{
2811 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2812 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2813 int ret;
2814
2815 if (addr < 0)
2816 return addr;
2817
Andrew Lunn3898c142015-05-06 01:09:53 +02002818 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002819 ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002820 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002821 return ret;
2822}
2823
Guenter Roeckc22995c2015-07-25 09:42:28 -07002824#ifdef CONFIG_NET_DSA_HWMON
2825
2826static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2827{
2828 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2829 int ret;
2830 int val;
2831
2832 *temp = 0;
2833
2834 mutex_lock(&ps->smi_mutex);
2835
2836 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
2837 if (ret < 0)
2838 goto error;
2839
2840 /* Enable temperature sensor */
2841 ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2842 if (ret < 0)
2843 goto error;
2844
2845 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
2846 if (ret < 0)
2847 goto error;
2848
2849 /* Wait for temperature to stabilize */
2850 usleep_range(10000, 12000);
2851
2852 val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2853 if (val < 0) {
2854 ret = val;
2855 goto error;
2856 }
2857
2858 /* Disable temperature sensor */
2859 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
2860 if (ret < 0)
2861 goto error;
2862
2863 *temp = ((val & 0x1f) - 5) * 5;
2864
2865error:
2866 _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
2867 mutex_unlock(&ps->smi_mutex);
2868 return ret;
2869}
2870
2871static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
2872{
2873 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2874 int ret;
2875
2876 *temp = 0;
2877
2878 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
2879 if (ret < 0)
2880 return ret;
2881
2882 *temp = (ret & 0xff) - 25;
2883
2884 return 0;
2885}
2886
2887int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
2888{
2889 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
2890 return mv88e63xx_get_temp(ds, temp);
2891
2892 return mv88e61xx_get_temp(ds, temp);
2893}
2894
2895int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
2896{
2897 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2898 int ret;
2899
2900 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2901 return -EOPNOTSUPP;
2902
2903 *temp = 0;
2904
2905 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2906 if (ret < 0)
2907 return ret;
2908
2909 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
2910
2911 return 0;
2912}
2913
2914int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
2915{
2916 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2917 int ret;
2918
2919 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2920 return -EOPNOTSUPP;
2921
2922 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2923 if (ret < 0)
2924 return ret;
2925 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
2926 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
2927 (ret & 0xe0ff) | (temp << 8));
2928}
2929
2930int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
2931{
2932 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2933 int ret;
2934
2935 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2936 return -EOPNOTSUPP;
2937
2938 *alarm = false;
2939
2940 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2941 if (ret < 0)
2942 return ret;
2943
2944 *alarm = !!(ret & 0x40);
2945
2946 return 0;
2947}
2948#endif /* CONFIG_NET_DSA_HWMON */
2949
Vivien Didelotb9b37712015-10-30 19:39:48 -04002950char *mv88e6xxx_lookup_name(struct device *host_dev, int sw_addr,
2951 const struct mv88e6xxx_switch_id *table,
2952 unsigned int num)
2953{
2954 struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
2955 int i, ret;
2956
2957 if (!bus)
2958 return NULL;
2959
2960 ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
2961 if (ret < 0)
2962 return NULL;
2963
2964 /* Look up the exact switch ID */
2965 for (i = 0; i < num; ++i)
2966 if (table[i].id == ret)
2967 return table[i].name;
2968
2969 /* Look up only the product number */
2970 for (i = 0; i < num; ++i) {
2971 if (table[i].id == (ret & PORT_SWITCH_ID_PROD_NUM_MASK)) {
2972 dev_warn(host_dev, "unknown revision %d, using base switch 0x%x\n",
2973 ret & PORT_SWITCH_ID_REV_MASK,
2974 ret & PORT_SWITCH_ID_PROD_NUM_MASK);
2975 return table[i].name;
2976 }
2977 }
2978
2979 return NULL;
2980}
2981
Ben Hutchings98e67302011-11-25 14:36:19 +00002982static int __init mv88e6xxx_init(void)
2983{
2984#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2985 register_switch_driver(&mv88e6131_switch_driver);
2986#endif
2987#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2988 register_switch_driver(&mv88e6123_61_65_switch_driver);
2989#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07002990#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2991 register_switch_driver(&mv88e6352_switch_driver);
2992#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02002993#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2994 register_switch_driver(&mv88e6171_switch_driver);
2995#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00002996 return 0;
2997}
2998module_init(mv88e6xxx_init);
2999
3000static void __exit mv88e6xxx_cleanup(void)
3001{
Andrew Lunn42f27252014-09-12 23:58:44 +02003002#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3003 unregister_switch_driver(&mv88e6171_switch_driver);
3004#endif
Vivien Didelot4212b542015-05-01 10:43:52 -04003005#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3006 unregister_switch_driver(&mv88e6352_switch_driver);
3007#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00003008#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
3009 unregister_switch_driver(&mv88e6123_61_65_switch_driver);
3010#endif
3011#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3012 unregister_switch_driver(&mv88e6131_switch_driver);
3013#endif
3014}
3015module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003016
3017MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3018MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3019MODULE_LICENSE("GPL");