blob: b4b2f05134baaae6b1628e5144da3428d85bebb1 [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
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001054static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state)
1055{
1056 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001057 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001058 u8 oldstate;
1059
1060 mutex_lock(&ps->smi_mutex);
1061
Andrew Lunncca8b132015-04-02 04:06:39 +02001062 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
Guenter Roeck538cc282015-04-15 22:12:42 -07001063 if (reg < 0) {
1064 ret = reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001065 goto abort;
Guenter Roeck538cc282015-04-15 22:12:42 -07001066 }
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001067
Andrew Lunncca8b132015-04-02 04:06:39 +02001068 oldstate = reg & PORT_CONTROL_STATE_MASK;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001069 if (oldstate != state) {
1070 /* Flush forwarding database if we're moving a port
1071 * from Learning or Forwarding state to Disabled or
1072 * Blocking or Listening state.
1073 */
Andrew Lunncca8b132015-04-02 04:06:39 +02001074 if (oldstate >= PORT_CONTROL_STATE_LEARNING &&
1075 state <= PORT_CONTROL_STATE_BLOCKING) {
Vivien Didelot2b8157b2015-09-04 14:34:16 -04001076 ret = _mv88e6xxx_atu_remove(ds, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001077 if (ret)
1078 goto abort;
1079 }
Andrew Lunncca8b132015-04-02 04:06:39 +02001080 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1081 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
1082 reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001083 }
1084
1085abort:
1086 mutex_unlock(&ps->smi_mutex);
1087 return ret;
1088}
1089
Vivien Didelotede80982015-10-11 18:08:35 -04001090static int _mv88e6xxx_port_vlan_map_set(struct dsa_switch *ds, int port,
1091 u16 output_ports)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001092{
1093 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotede80982015-10-11 18:08:35 -04001094 const u16 mask = (1 << ps->num_ports) - 1;
1095 int reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001096
Vivien Didelotede80982015-10-11 18:08:35 -04001097 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1098 if (reg < 0)
1099 return reg;
1100
1101 reg &= ~mask;
1102 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001103
Andrew Lunncca8b132015-04-02 04:06:39 +02001104 return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001105}
1106
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001107int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state)
1108{
1109 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1110 int stp_state;
1111
1112 switch (state) {
1113 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001114 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001115 break;
1116 case BR_STATE_BLOCKING:
1117 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001118 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001119 break;
1120 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001121 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001122 break;
1123 case BR_STATE_FORWARDING:
1124 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001125 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001126 break;
1127 }
1128
1129 netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state);
1130
1131 /* mv88e6xxx_port_stp_update may be called with softirqs disabled,
1132 * so we can not update the port state directly but need to schedule it.
1133 */
Vivien Didelotd715fa62016-02-12 12:09:38 -05001134 ps->ports[port].state = stp_state;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001135 set_bit(port, &ps->port_state_update_mask);
1136 schedule_work(&ps->bridge_work);
1137
1138 return 0;
1139}
1140
Vivien Didelot76e398a2015-11-01 12:33:55 -05001141static int _mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid)
1142{
1143 int ret;
1144
1145 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN);
1146 if (ret < 0)
1147 return ret;
1148
1149 *pvid = ret & PORT_DEFAULT_VLAN_MASK;
1150
1151 return 0;
1152}
1153
Vivien Didelot76e398a2015-11-01 12:33:55 -05001154static int _mv88e6xxx_port_pvid_set(struct dsa_switch *ds, int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001155{
Vivien Didelot76e398a2015-11-01 12:33:55 -05001156 return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001157 pvid & PORT_DEFAULT_VLAN_MASK);
1158}
1159
Vivien Didelot6b17e862015-08-13 12:52:18 -04001160static int _mv88e6xxx_vtu_wait(struct dsa_switch *ds)
1161{
1162 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_VTU_OP,
1163 GLOBAL_VTU_OP_BUSY);
1164}
1165
1166static int _mv88e6xxx_vtu_cmd(struct dsa_switch *ds, u16 op)
1167{
1168 int ret;
1169
1170 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_OP, op);
1171 if (ret < 0)
1172 return ret;
1173
1174 return _mv88e6xxx_vtu_wait(ds);
1175}
1176
1177static int _mv88e6xxx_vtu_stu_flush(struct dsa_switch *ds)
1178{
1179 int ret;
1180
1181 ret = _mv88e6xxx_vtu_wait(ds);
1182 if (ret < 0)
1183 return ret;
1184
1185 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_FLUSH_ALL);
1186}
1187
Vivien Didelotb8fee952015-08-13 12:52:19 -04001188static int _mv88e6xxx_vtu_stu_data_read(struct dsa_switch *ds,
1189 struct mv88e6xxx_vtu_stu_entry *entry,
1190 unsigned int nibble_offset)
1191{
1192 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1193 u16 regs[3];
1194 int i;
1195 int ret;
1196
1197 for (i = 0; i < 3; ++i) {
1198 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1199 GLOBAL_VTU_DATA_0_3 + i);
1200 if (ret < 0)
1201 return ret;
1202
1203 regs[i] = ret;
1204 }
1205
1206 for (i = 0; i < ps->num_ports; ++i) {
1207 unsigned int shift = (i % 4) * 4 + nibble_offset;
1208 u16 reg = regs[i / 4];
1209
1210 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1211 }
1212
1213 return 0;
1214}
1215
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001216static int _mv88e6xxx_vtu_stu_data_write(struct dsa_switch *ds,
1217 struct mv88e6xxx_vtu_stu_entry *entry,
1218 unsigned int nibble_offset)
1219{
1220 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1221 u16 regs[3] = { 0 };
1222 int i;
1223 int ret;
1224
1225 for (i = 0; i < ps->num_ports; ++i) {
1226 unsigned int shift = (i % 4) * 4 + nibble_offset;
1227 u8 data = entry->data[i];
1228
1229 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1230 }
1231
1232 for (i = 0; i < 3; ++i) {
1233 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL,
1234 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1235 if (ret < 0)
1236 return ret;
1237 }
1238
1239 return 0;
1240}
1241
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001242static int _mv88e6xxx_vtu_vid_write(struct dsa_switch *ds, u16 vid)
1243{
1244 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID,
1245 vid & GLOBAL_VTU_VID_MASK);
1246}
1247
1248static int _mv88e6xxx_vtu_getnext(struct dsa_switch *ds,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001249 struct mv88e6xxx_vtu_stu_entry *entry)
1250{
1251 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1252 int ret;
1253
1254 ret = _mv88e6xxx_vtu_wait(ds);
1255 if (ret < 0)
1256 return ret;
1257
Vivien Didelotb8fee952015-08-13 12:52:19 -04001258 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_GET_NEXT);
1259 if (ret < 0)
1260 return ret;
1261
1262 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1263 if (ret < 0)
1264 return ret;
1265
1266 next.vid = ret & GLOBAL_VTU_VID_MASK;
1267 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1268
1269 if (next.valid) {
1270 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 0);
1271 if (ret < 0)
1272 return ret;
1273
1274 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1275 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1276 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1277 GLOBAL_VTU_FID);
1278 if (ret < 0)
1279 return ret;
1280
1281 next.fid = ret & GLOBAL_VTU_FID_MASK;
1282
1283 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1284 GLOBAL_VTU_SID);
1285 if (ret < 0)
1286 return ret;
1287
1288 next.sid = ret & GLOBAL_VTU_SID_MASK;
1289 }
1290 }
1291
1292 *entry = next;
1293 return 0;
1294}
1295
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001296int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1297 struct switchdev_obj_port_vlan *vlan,
1298 int (*cb)(struct switchdev_obj *obj))
1299{
1300 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1301 struct mv88e6xxx_vtu_stu_entry next;
1302 u16 pvid;
1303 int err;
1304
1305 mutex_lock(&ps->smi_mutex);
1306
1307 err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1308 if (err)
1309 goto unlock;
1310
1311 err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1312 if (err)
1313 goto unlock;
1314
1315 do {
1316 err = _mv88e6xxx_vtu_getnext(ds, &next);
1317 if (err)
1318 break;
1319
1320 if (!next.valid)
1321 break;
1322
1323 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1324 continue;
1325
1326 /* reinit and dump this VLAN obj */
1327 vlan->vid_begin = vlan->vid_end = next.vid;
1328 vlan->flags = 0;
1329
1330 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1331 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1332
1333 if (next.vid == pvid)
1334 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1335
1336 err = cb(&vlan->obj);
1337 if (err)
1338 break;
1339 } while (next.vid < GLOBAL_VTU_VID_MASK);
1340
1341unlock:
1342 mutex_unlock(&ps->smi_mutex);
1343
1344 return err;
1345}
1346
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001347static int _mv88e6xxx_vtu_loadpurge(struct dsa_switch *ds,
1348 struct mv88e6xxx_vtu_stu_entry *entry)
1349{
1350 u16 reg = 0;
1351 int ret;
1352
1353 ret = _mv88e6xxx_vtu_wait(ds);
1354 if (ret < 0)
1355 return ret;
1356
1357 if (!entry->valid)
1358 goto loadpurge;
1359
1360 /* Write port member tags */
1361 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 0);
1362 if (ret < 0)
1363 return ret;
1364
1365 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1366 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1367 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1368 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1369 if (ret < 0)
1370 return ret;
1371
1372 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1373 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_FID, reg);
1374 if (ret < 0)
1375 return ret;
1376 }
1377
1378 reg = GLOBAL_VTU_VID_VALID;
1379loadpurge:
1380 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1381 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1382 if (ret < 0)
1383 return ret;
1384
1385 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_LOAD_PURGE);
1386}
1387
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001388static int _mv88e6xxx_stu_getnext(struct dsa_switch *ds, u8 sid,
1389 struct mv88e6xxx_vtu_stu_entry *entry)
1390{
1391 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1392 int ret;
1393
1394 ret = _mv88e6xxx_vtu_wait(ds);
1395 if (ret < 0)
1396 return ret;
1397
1398 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID,
1399 sid & GLOBAL_VTU_SID_MASK);
1400 if (ret < 0)
1401 return ret;
1402
1403 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_GET_NEXT);
1404 if (ret < 0)
1405 return ret;
1406
1407 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_SID);
1408 if (ret < 0)
1409 return ret;
1410
1411 next.sid = ret & GLOBAL_VTU_SID_MASK;
1412
1413 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1414 if (ret < 0)
1415 return ret;
1416
1417 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1418
1419 if (next.valid) {
1420 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 2);
1421 if (ret < 0)
1422 return ret;
1423 }
1424
1425 *entry = next;
1426 return 0;
1427}
1428
1429static int _mv88e6xxx_stu_loadpurge(struct dsa_switch *ds,
1430 struct mv88e6xxx_vtu_stu_entry *entry)
1431{
1432 u16 reg = 0;
1433 int ret;
1434
1435 ret = _mv88e6xxx_vtu_wait(ds);
1436 if (ret < 0)
1437 return ret;
1438
1439 if (!entry->valid)
1440 goto loadpurge;
1441
1442 /* Write port states */
1443 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 2);
1444 if (ret < 0)
1445 return ret;
1446
1447 reg = GLOBAL_VTU_VID_VALID;
1448loadpurge:
1449 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1450 if (ret < 0)
1451 return ret;
1452
1453 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1454 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1455 if (ret < 0)
1456 return ret;
1457
1458 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1459}
1460
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001461static int _mv88e6xxx_fid_new(struct dsa_switch *ds, u16 *fid)
1462{
1463 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1464 struct mv88e6xxx_vtu_stu_entry vlan;
1465 int err;
1466
1467 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1468
1469 /* Set every FID bit used by the VLAN entries */
1470 err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1471 if (err)
1472 return err;
1473
1474 do {
1475 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1476 if (err)
1477 return err;
1478
1479 if (!vlan.valid)
1480 break;
1481
1482 set_bit(vlan.fid, fid_bitmap);
1483 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1484
1485 /* The reset value 0x000 is used to indicate that multiple address
1486 * databases are not needed. Return the next positive available.
1487 */
1488 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1489 if (unlikely(*fid == MV88E6XXX_N_FID))
1490 return -ENOSPC;
1491
1492 /* Clear the database */
1493 return _mv88e6xxx_atu_flush(ds, *fid, true);
1494}
1495
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001496static int _mv88e6xxx_vtu_new(struct dsa_switch *ds, u16 vid,
1497 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001498{
1499 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1500 struct mv88e6xxx_vtu_stu_entry vlan = {
1501 .valid = true,
1502 .vid = vid,
1503 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001504 int i, err;
1505
1506 err = _mv88e6xxx_fid_new(ds, &vlan.fid);
1507 if (err)
1508 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001509
Vivien Didelot3d131f02015-11-03 10:52:52 -05001510 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001511 for (i = 0; i < ps->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001512 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1513 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1514 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001515
1516 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1517 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1518 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001519
1520 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1521 * implemented, only one STU entry is needed to cover all VTU
1522 * entries. Thus, validate the SID 0.
1523 */
1524 vlan.sid = 0;
1525 err = _mv88e6xxx_stu_getnext(ds, GLOBAL_VTU_SID_MASK, &vstp);
1526 if (err)
1527 return err;
1528
1529 if (vstp.sid != vlan.sid || !vstp.valid) {
1530 memset(&vstp, 0, sizeof(vstp));
1531 vstp.valid = true;
1532 vstp.sid = vlan.sid;
1533
1534 err = _mv88e6xxx_stu_loadpurge(ds, &vstp);
1535 if (err)
1536 return err;
1537 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001538 }
1539
1540 *entry = vlan;
1541 return 0;
1542}
1543
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001544static int _mv88e6xxx_vtu_get(struct dsa_switch *ds, u16 vid,
1545 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1546{
1547 int err;
1548
1549 if (!vid)
1550 return -EINVAL;
1551
1552 err = _mv88e6xxx_vtu_vid_write(ds, vid - 1);
1553 if (err)
1554 return err;
1555
1556 err = _mv88e6xxx_vtu_getnext(ds, entry);
1557 if (err)
1558 return err;
1559
1560 if (entry->vid != vid || !entry->valid) {
1561 if (!creat)
1562 return -EOPNOTSUPP;
1563 /* -ENOENT would've been more appropriate, but switchdev expects
1564 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1565 */
1566
1567 err = _mv88e6xxx_vtu_new(ds, vid, entry);
1568 }
1569
1570 return err;
1571}
1572
Vivien Didelotda9c3592016-02-12 12:09:40 -05001573static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1574 u16 vid_begin, u16 vid_end)
1575{
1576 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1577 struct mv88e6xxx_vtu_stu_entry vlan;
1578 int i, err;
1579
1580 if (!vid_begin)
1581 return -EOPNOTSUPP;
1582
1583 mutex_lock(&ps->smi_mutex);
1584
1585 err = _mv88e6xxx_vtu_vid_write(ds, vid_begin - 1);
1586 if (err)
1587 goto unlock;
1588
1589 do {
1590 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1591 if (err)
1592 goto unlock;
1593
1594 if (!vlan.valid)
1595 break;
1596
1597 if (vlan.vid > vid_end)
1598 break;
1599
1600 for (i = 0; i < ps->num_ports; ++i) {
1601 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1602 continue;
1603
1604 if (vlan.data[i] ==
1605 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1606 continue;
1607
1608 if (ps->ports[i].bridge_dev ==
1609 ps->ports[port].bridge_dev)
1610 break; /* same bridge, check next VLAN */
1611
1612 netdev_warn(ds->ports[port],
1613 "hardware VLAN %d already used by %s\n",
1614 vlan.vid,
1615 netdev_name(ps->ports[i].bridge_dev));
1616 err = -EOPNOTSUPP;
1617 goto unlock;
1618 }
1619 } while (vlan.vid < vid_end);
1620
1621unlock:
1622 mutex_unlock(&ps->smi_mutex);
1623
1624 return err;
1625}
1626
Vivien Didelot76e398a2015-11-01 12:33:55 -05001627int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1628 const struct switchdev_obj_port_vlan *vlan,
1629 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001630{
Vivien Didelotda9c3592016-02-12 12:09:40 -05001631 int err;
1632
Vivien Didelote79a8bc2015-11-04 17:23:40 -05001633 /* We reserve a few VLANs to isolate unbridged ports */
1634 if (vlan->vid_end >= 4000)
1635 return -EOPNOTSUPP;
1636
Vivien Didelotda9c3592016-02-12 12:09:40 -05001637 /* If the requested port doesn't belong to the same bridge as the VLAN
1638 * members, do not support it (yet) and fallback to software VLAN.
1639 */
1640 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1641 vlan->vid_end);
1642 if (err)
1643 return err;
1644
Vivien Didelot76e398a2015-11-01 12:33:55 -05001645 /* We don't need any dynamic resource from the kernel (yet),
1646 * so skip the prepare phase.
1647 */
1648 return 0;
1649}
1650
1651static int _mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, u16 vid,
1652 bool untagged)
1653{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001654 struct mv88e6xxx_vtu_stu_entry vlan;
1655 int err;
1656
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001657 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001658 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001659 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001660
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001661 vlan.data[port] = untagged ?
1662 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1663 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1664
Vivien Didelot76e398a2015-11-01 12:33:55 -05001665 return _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1666}
1667
1668int mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1669 const struct switchdev_obj_port_vlan *vlan,
1670 struct switchdev_trans *trans)
1671{
1672 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1673 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1674 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1675 u16 vid;
1676 int err = 0;
1677
1678 mutex_lock(&ps->smi_mutex);
1679
1680 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1681 err = _mv88e6xxx_port_vlan_add(ds, port, vid, untagged);
1682 if (err)
1683 goto unlock;
1684 }
1685
1686 /* no PVID with ranges, otherwise it's a bug */
1687 if (pvid)
Russell Kingdb0e51a2016-01-24 09:22:05 +00001688 err = _mv88e6xxx_port_pvid_set(ds, port, vlan->vid_end);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001689unlock:
1690 mutex_unlock(&ps->smi_mutex);
1691
1692 return err;
1693}
1694
Vivien Didelot76e398a2015-11-01 12:33:55 -05001695static int _mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001696{
1697 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1698 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001699 int i, err;
1700
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001701 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001702 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001703 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001704
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001705 /* Tell switchdev if this VLAN is handled in software */
1706 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001707 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001708
1709 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1710
1711 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001712 vlan.valid = false;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001713 for (i = 0; i < ps->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001714 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001715 continue;
1716
1717 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001718 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001719 break;
1720 }
1721 }
1722
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001723 err = _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1724 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001725 return err;
1726
1727 return _mv88e6xxx_atu_remove(ds, vlan.fid, port, false);
1728}
1729
1730int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1731 const struct switchdev_obj_port_vlan *vlan)
1732{
1733 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05001734 const u16 defpvid = 4000 + ds->index * DSA_MAX_PORTS + port;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001735 u16 pvid, vid;
1736 int err = 0;
1737
1738 mutex_lock(&ps->smi_mutex);
1739
1740 err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1741 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001742 goto unlock;
1743
Vivien Didelot76e398a2015-11-01 12:33:55 -05001744 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1745 err = _mv88e6xxx_port_vlan_del(ds, port, vid);
1746 if (err)
1747 goto unlock;
1748
1749 if (vid == pvid) {
Vivien Didelot66d9cd02016-02-05 14:07:14 -05001750 /* restore reserved VLAN ID */
1751 err = _mv88e6xxx_port_pvid_set(ds, port, defpvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001752 if (err)
1753 goto unlock;
1754 }
1755 }
1756
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001757unlock:
1758 mutex_unlock(&ps->smi_mutex);
1759
1760 return err;
1761}
1762
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001763static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds,
1764 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001765{
1766 int i, ret;
1767
1768 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001769 ret = _mv88e6xxx_reg_write(
1770 ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
1771 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001772 if (ret < 0)
1773 return ret;
1774 }
1775
1776 return 0;
1777}
1778
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001779static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001780{
1781 int i, ret;
1782
1783 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001784 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1785 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001786 if (ret < 0)
1787 return ret;
1788 addr[i * 2] = ret >> 8;
1789 addr[i * 2 + 1] = ret & 0xff;
1790 }
1791
1792 return 0;
1793}
1794
Vivien Didelotfd231c82015-08-10 09:09:50 -04001795static int _mv88e6xxx_atu_load(struct dsa_switch *ds,
1796 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001797{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001798 int ret;
1799
1800 ret = _mv88e6xxx_atu_wait(ds);
1801 if (ret < 0)
1802 return ret;
1803
Vivien Didelotfd231c82015-08-10 09:09:50 -04001804 ret = _mv88e6xxx_atu_mac_write(ds, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001805 if (ret < 0)
1806 return ret;
1807
Vivien Didelot37705b72015-09-04 14:34:11 -04001808 ret = _mv88e6xxx_atu_data_write(ds, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04001809 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001810 return ret;
1811
Vivien Didelot70cc99d2015-09-04 14:34:10 -04001812 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, entry->fid);
1813 if (ret < 0)
1814 return ret;
1815
1816 return _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04001817}
David S. Millercdf09692015-08-11 12:00:37 -07001818
Vivien Didelotfd231c82015-08-10 09:09:50 -04001819static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port,
1820 const unsigned char *addr, u16 vid,
1821 u8 state)
1822{
1823 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001824 struct mv88e6xxx_vtu_stu_entry vlan;
1825 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04001826
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001827 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
1828 if (err)
1829 return err;
1830
1831 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04001832 entry.state = state;
1833 ether_addr_copy(entry.mac, addr);
1834 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1835 entry.trunk = false;
1836 entry.portv_trunkid = BIT(port);
1837 }
1838
1839 return _mv88e6xxx_atu_load(ds, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001840}
1841
Vivien Didelot146a3202015-10-08 11:35:12 -04001842int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
1843 const struct switchdev_obj_port_fdb *fdb,
1844 struct switchdev_trans *trans)
1845{
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001846 /* We don't use per-port FDB */
1847 if (fdb->vid == 0)
1848 return -EOPNOTSUPP;
1849
Vivien Didelot146a3202015-10-08 11:35:12 -04001850 /* We don't need any dynamic resource from the kernel (yet),
1851 * so skip the prepare phase.
1852 */
1853 return 0;
1854}
1855
David S. Millercdf09692015-08-11 12:00:37 -07001856int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
Vivien Didelot1f36faf2015-10-08 11:35:13 -04001857 const struct switchdev_obj_port_fdb *fdb,
1858 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001859{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04001860 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07001861 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1862 GLOBAL_ATU_DATA_STATE_UC_STATIC;
1863 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04001864 int ret;
1865
David S. Millercdf09692015-08-11 12:00:37 -07001866 mutex_lock(&ps->smi_mutex);
Vivien Didelot1f36faf2015-10-08 11:35:13 -04001867 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, state);
David S. Millercdf09692015-08-11 12:00:37 -07001868 mutex_unlock(&ps->smi_mutex);
1869
1870 return ret;
1871}
1872
1873int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
Vivien Didelot8057b3e2015-10-08 11:35:14 -04001874 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07001875{
1876 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1877 int ret;
1878
1879 mutex_lock(&ps->smi_mutex);
Vivien Didelot8057b3e2015-10-08 11:35:14 -04001880 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07001881 GLOBAL_ATU_DATA_STATE_UNUSED);
1882 mutex_unlock(&ps->smi_mutex);
1883
1884 return ret;
1885}
1886
Vivien Didelot1d194042015-08-10 09:09:51 -04001887static int _mv88e6xxx_atu_getnext(struct dsa_switch *ds, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04001888 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07001889{
Vivien Didelot1d194042015-08-10 09:09:51 -04001890 struct mv88e6xxx_atu_entry next = { 0 };
1891 int ret;
1892
1893 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001894
1895 ret = _mv88e6xxx_atu_wait(ds);
1896 if (ret < 0)
1897 return ret;
1898
Vivien Didelot70cc99d2015-09-04 14:34:10 -04001899 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid);
1900 if (ret < 0)
1901 return ret;
1902
1903 ret = _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001904 if (ret < 0)
1905 return ret;
1906
Vivien Didelot1d194042015-08-10 09:09:51 -04001907 ret = _mv88e6xxx_atu_mac_read(ds, next.mac);
1908 if (ret < 0)
1909 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001910
Vivien Didelot1d194042015-08-10 09:09:51 -04001911 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
1912 if (ret < 0)
1913 return ret;
1914
1915 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
1916 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1917 unsigned int mask, shift;
1918
1919 if (ret & GLOBAL_ATU_DATA_TRUNK) {
1920 next.trunk = true;
1921 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1922 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1923 } else {
1924 next.trunk = false;
1925 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1926 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1927 }
1928
1929 next.portv_trunkid = (ret & mask) >> shift;
1930 }
1931
1932 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001933 return 0;
1934}
1935
Vivien Didelot74b6ba02016-02-26 13:16:02 -05001936static int _mv88e6xxx_port_fdb_dump_one(struct dsa_switch *ds, u16 fid, u16 vid,
1937 int port,
1938 struct switchdev_obj_port_fdb *fdb,
1939 int (*cb)(struct switchdev_obj *obj))
1940{
1941 struct mv88e6xxx_atu_entry addr = {
1942 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
1943 };
1944 int err;
1945
1946 err = _mv88e6xxx_atu_mac_write(ds, addr.mac);
1947 if (err)
1948 return err;
1949
1950 do {
1951 err = _mv88e6xxx_atu_getnext(ds, fid, &addr);
1952 if (err)
1953 break;
1954
1955 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
1956 break;
1957
1958 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
1959 bool is_static = addr.state ==
1960 (is_multicast_ether_addr(addr.mac) ?
1961 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1962 GLOBAL_ATU_DATA_STATE_UC_STATIC);
1963
1964 fdb->vid = vid;
1965 ether_addr_copy(fdb->addr, addr.mac);
1966 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
1967
1968 err = cb(&fdb->obj);
1969 if (err)
1970 break;
1971 }
1972 } while (!is_broadcast_ether_addr(addr.mac));
1973
1974 return err;
1975}
1976
Vivien Didelotf33475b2015-10-22 09:34:41 -04001977int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
1978 struct switchdev_obj_port_fdb *fdb,
1979 int (*cb)(struct switchdev_obj *obj))
1980{
1981 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1982 struct mv88e6xxx_vtu_stu_entry vlan = {
1983 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
1984 };
1985 int err;
1986
1987 mutex_lock(&ps->smi_mutex);
1988
Vivien Didelot74b6ba02016-02-26 13:16:02 -05001989 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotf33475b2015-10-22 09:34:41 -04001990 err = _mv88e6xxx_vtu_vid_write(ds, vlan.vid);
1991 if (err)
1992 goto unlock;
1993
1994 do {
Vivien Didelotf33475b2015-10-22 09:34:41 -04001995 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1996 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05001997 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04001998
1999 if (!vlan.valid)
2000 break;
2001
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002002 err = _mv88e6xxx_port_fdb_dump_one(ds, vlan.fid, vlan.vid, port,
2003 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002004 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002005 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002006 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2007
2008unlock:
2009 mutex_unlock(&ps->smi_mutex);
2010
2011 return err;
2012}
2013
Vivien Didelota6692752016-02-12 12:09:39 -05002014int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2015 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002016{
Vivien Didelota6692752016-02-12 12:09:39 -05002017 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2018
2019 ps->ports[port].bridge_dev = bridge;
2020
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002021 return 0;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002022}
2023
Vivien Didelota6692752016-02-12 12:09:39 -05002024int mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002025{
Vivien Didelota6692752016-02-12 12:09:39 -05002026 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2027
2028 ps->ports[port].bridge_dev = NULL;
2029
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002030 return 0;
2031}
2032
2033static int mv88e6xxx_setup_port_default_vlan(struct dsa_switch *ds, int port)
2034{
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002035 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2036 const u16 pvid = 4000 + ds->index * DSA_MAX_PORTS + port;
2037 int err;
2038
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002039 mutex_lock(&ps->smi_mutex);
2040 err = _mv88e6xxx_port_vlan_add(ds, port, pvid, true);
2041 if (!err)
2042 err = _mv88e6xxx_port_pvid_set(ds, port, pvid);
2043 mutex_unlock(&ps->smi_mutex);
2044 return err;
2045}
2046
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002047static void mv88e6xxx_bridge_work(struct work_struct *work)
2048{
2049 struct mv88e6xxx_priv_state *ps;
2050 struct dsa_switch *ds;
2051 int port;
2052
2053 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
2054 ds = ((struct dsa_switch *)ps) - 1;
2055
2056 while (ps->port_state_update_mask) {
2057 port = __ffs(ps->port_state_update_mask);
2058 clear_bit(port, &ps->port_state_update_mask);
Vivien Didelotd715fa62016-02-12 12:09:38 -05002059 mv88e6xxx_set_port_state(ds, port, ps->ports[port].state);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002060 }
2061}
2062
Andrew Lunndbde9e62015-05-06 01:09:48 +02002063static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002064{
2065 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002066 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002067 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002068
2069 mutex_lock(&ps->smi_mutex);
2070
Andrew Lunn54d792f2015-05-06 01:09:47 +02002071 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2072 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2073 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002074 mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002075 /* MAC Forcing register: don't force link, speed,
2076 * duplex or flow control state to any particular
2077 * values on physical ports, but force the CPU port
2078 * and all DSA ports to their maximum bandwidth and
2079 * full duplex.
2080 */
2081 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002082 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002083 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002084 reg |= PORT_PCS_CTRL_FORCE_LINK |
2085 PORT_PCS_CTRL_LINK_UP |
2086 PORT_PCS_CTRL_DUPLEX_FULL |
2087 PORT_PCS_CTRL_FORCE_DUPLEX;
2088 if (mv88e6xxx_6065_family(ds))
2089 reg |= PORT_PCS_CTRL_100;
2090 else
2091 reg |= PORT_PCS_CTRL_1000;
2092 } else {
2093 reg |= PORT_PCS_CTRL_UNFORCED;
2094 }
2095
2096 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2097 PORT_PCS_CTRL, reg);
2098 if (ret)
2099 goto abort;
2100 }
2101
2102 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2103 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2104 * tunneling, determine priority by looking at 802.1p and IP
2105 * priority fields (IP prio has precedence), and set STP state
2106 * to Forwarding.
2107 *
2108 * If this is the CPU link, use DSA or EDSA tagging depending
2109 * on which tagging mode was configured.
2110 *
2111 * If this is a link to another switch, use DSA tagging mode.
2112 *
2113 * If this is the upstream port for this switch, enable
2114 * forwarding of unknown unicasts and multicasts.
2115 */
2116 reg = 0;
2117 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2118 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2119 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002120 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002121 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2122 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2123 PORT_CONTROL_STATE_FORWARDING;
2124 if (dsa_is_cpu_port(ds, port)) {
2125 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2126 reg |= PORT_CONTROL_DSA_TAG;
2127 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002128 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2129 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002130 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2131 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2132 else
2133 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002134 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2135 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002136 }
2137
2138 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2139 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2140 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002141 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002142 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2143 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2144 }
2145 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002146 if (dsa_is_dsa_port(ds, port)) {
2147 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2148 reg |= PORT_CONTROL_DSA_TAG;
2149 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2150 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2151 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002152 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002153 }
2154
Andrew Lunn54d792f2015-05-06 01:09:47 +02002155 if (port == dsa_upstream_port(ds))
2156 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2157 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2158 }
2159 if (reg) {
2160 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2161 PORT_CONTROL, reg);
2162 if (ret)
2163 goto abort;
2164 }
2165
Vivien Didelot8efdda42015-08-13 12:52:23 -04002166 /* Port Control 2: don't force a good FCS, set the maximum frame size to
2167 * 10240 bytes, enable secure 802.1q tags, don't discard tagged or
2168 * untagged frames on this port, do a destination address lookup on all
2169 * received packets as usual, disable ARP mirroring and don't send a
2170 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002171 */
2172 reg = 0;
2173 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2174 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002175 mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002176 reg = PORT_CONTROL_2_MAP_DA;
2177
2178 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002179 mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002180 reg |= PORT_CONTROL_2_JUMBO_10240;
2181
2182 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) {
2183 /* Set the upstream port this port should use */
2184 reg |= dsa_upstream_port(ds);
2185 /* enable forwarding of unknown multicast addresses to
2186 * the upstream port
2187 */
2188 if (port == dsa_upstream_port(ds))
2189 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2190 }
2191
Vivien Didelot5fe7f682015-10-11 18:08:38 -04002192 reg |= PORT_CONTROL_2_8021Q_SECURE;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002193
Andrew Lunn54d792f2015-05-06 01:09:47 +02002194 if (reg) {
2195 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2196 PORT_CONTROL_2, reg);
2197 if (ret)
2198 goto abort;
2199 }
2200
2201 /* Port Association Vector: when learning source addresses
2202 * of packets, add the address to the address database using
2203 * a port bitmap that has only the bit for this port set and
2204 * the other bits clear.
2205 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002206 reg = 1 << port;
2207 /* Disable learning for DSA and CPU ports */
2208 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2209 reg = PORT_ASSOC_VECTOR_LOCKED_PORT;
2210
2211 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002212 if (ret)
2213 goto abort;
2214
2215 /* Egress rate control 2: disable egress rate control. */
2216 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2,
2217 0x0000);
2218 if (ret)
2219 goto abort;
2220
2221 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002222 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2223 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002224 /* Do not limit the period of time that this port can
2225 * be paused for by the remote end or the period of
2226 * time that this port can pause the remote end.
2227 */
2228 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2229 PORT_PAUSE_CTRL, 0x0000);
2230 if (ret)
2231 goto abort;
2232
2233 /* Port ATU control: disable limiting the number of
2234 * address database entries that this port is allowed
2235 * to use.
2236 */
2237 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2238 PORT_ATU_CONTROL, 0x0000);
2239 /* Priority Override: disable DA, SA and VTU priority
2240 * override.
2241 */
2242 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2243 PORT_PRI_OVERRIDE, 0x0000);
2244 if (ret)
2245 goto abort;
2246
2247 /* Port Ethertype: use the Ethertype DSA Ethertype
2248 * value.
2249 */
2250 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2251 PORT_ETH_TYPE, ETH_P_EDSA);
2252 if (ret)
2253 goto abort;
2254 /* Tag Remap: use an identity 802.1p prio -> switch
2255 * prio mapping.
2256 */
2257 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2258 PORT_TAG_REGMAP_0123, 0x3210);
2259 if (ret)
2260 goto abort;
2261
2262 /* Tag Remap 2: use an identity 802.1p prio -> switch
2263 * prio mapping.
2264 */
2265 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2266 PORT_TAG_REGMAP_4567, 0x7654);
2267 if (ret)
2268 goto abort;
2269 }
2270
2271 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2272 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002273 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2274 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002275 /* Rate Control: disable ingress rate limiting. */
2276 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2277 PORT_RATE_CONTROL, 0x0001);
2278 if (ret)
2279 goto abort;
2280 }
2281
Guenter Roeck366f0a02015-03-26 18:36:30 -07002282 /* Port Control 1: disable trunking, disable sending
2283 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002284 */
Vivien Didelot614f03f2015-04-20 17:19:23 -04002285 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002286 if (ret)
2287 goto abort;
2288
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002289 /* Port based VLAN map: do not give each port its own address
Vivien Didelot5fe7f682015-10-11 18:08:38 -04002290 * database, and allow every port to egress frames on all other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002291 */
Vivien Didelot5fe7f682015-10-11 18:08:38 -04002292 reg = BIT(ps->num_ports) - 1; /* all ports */
Vivien Didelotbe1faa92016-01-28 16:54:37 -05002293 reg &= ~BIT(port); /* except itself */
2294 ret = _mv88e6xxx_port_vlan_map_set(ds, port, reg);
Guenter Roeckd827e882015-03-26 18:36:29 -07002295 if (ret)
2296 goto abort;
2297
2298 /* Default VLAN ID and priority: don't set a default VLAN
2299 * ID, and set the default packet priority to zero.
2300 */
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002301 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
2302 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002303abort:
2304 mutex_unlock(&ps->smi_mutex);
2305 return ret;
2306}
2307
Andrew Lunndbde9e62015-05-06 01:09:48 +02002308int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2309{
2310 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2311 int ret;
2312 int i;
2313
2314 for (i = 0; i < ps->num_ports; i++) {
2315 ret = mv88e6xxx_setup_port(ds, i);
2316 if (ret < 0)
2317 return ret;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002318
2319 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
2320 continue;
2321
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002322 ret = mv88e6xxx_setup_port_default_vlan(ds, i);
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002323 if (ret < 0)
2324 return ret;
Andrew Lunndbde9e62015-05-06 01:09:48 +02002325 }
2326 return 0;
2327}
2328
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002329int mv88e6xxx_setup_common(struct dsa_switch *ds)
2330{
2331 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2332
2333 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002334
Andrew Lunncca8b132015-04-02 04:06:39 +02002335 ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
Andrew Lunna8f064c2015-03-26 18:36:40 -07002336
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002337 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
2338
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002339 return 0;
2340}
2341
Andrew Lunn54d792f2015-05-06 01:09:47 +02002342int mv88e6xxx_setup_global(struct dsa_switch *ds)
2343{
2344 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot24751e22015-08-03 09:17:44 -04002345 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002346 int i;
2347
2348 /* Set the default address aging time to 5 minutes, and
2349 * enable address learn messages to be sent to all message
2350 * ports.
2351 */
2352 REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
2353 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2354
2355 /* Configure the IP ToS mapping registers. */
2356 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2357 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2358 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2359 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2360 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2361 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2362 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2363 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2364
2365 /* Configure the IEEE 802.1p priority mapping register. */
2366 REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2367
2368 /* Send all frames with destination addresses matching
2369 * 01:80:c2:00:00:0x to the CPU port.
2370 */
2371 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
2372
2373 /* Ignore removed tag data on doubly tagged packets, disable
2374 * flow control messages, force flow control priority to the
2375 * highest, and send all special multicast frames to the CPU
2376 * port at the highest priority.
2377 */
2378 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
2379 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
2380 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
2381
2382 /* Program the DSA routing table. */
2383 for (i = 0; i < 32; i++) {
2384 int nexthop = 0x1f;
2385
2386 if (ds->pd->rtable &&
2387 i != ds->index && i < ds->dst->pd->nr_chips)
2388 nexthop = ds->pd->rtable[i] & 0x1f;
2389
2390 REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
2391 GLOBAL2_DEVICE_MAPPING_UPDATE |
2392 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) |
2393 nexthop);
2394 }
2395
2396 /* Clear all trunk masks. */
2397 for (i = 0; i < 8; i++)
2398 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
2399 0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
2400 ((1 << ps->num_ports) - 1));
2401
2402 /* Clear all trunk mappings. */
2403 for (i = 0; i < 16; i++)
2404 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING,
2405 GLOBAL2_TRUNK_MAPPING_UPDATE |
2406 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
2407
2408 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002409 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2410 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002411 /* Send all frames with destination addresses matching
2412 * 01:80:c2:00:00:2x to the CPU port.
2413 */
2414 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff);
2415
2416 /* Initialise cross-chip port VLAN table to reset
2417 * defaults.
2418 */
2419 REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000);
2420
2421 /* Clear the priority override table. */
2422 for (i = 0; i < 16; i++)
2423 REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE,
2424 0x8000 | (i << 8));
2425 }
2426
2427 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2428 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002429 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2430 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002431 /* Disable ingress rate limiting by resetting all
2432 * ingress rate limit registers to their initial
2433 * state.
2434 */
2435 for (i = 0; i < ps->num_ports; i++)
2436 REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP,
2437 0x9000 | (i << 8));
2438 }
2439
Andrew Lunndb687a52015-06-20 21:31:29 +02002440 /* Clear the statistics counters for all ports */
2441 REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP, GLOBAL_STATS_OP_FLUSH_ALL);
2442
2443 /* Wait for the flush to complete. */
Vivien Didelot24751e22015-08-03 09:17:44 -04002444 mutex_lock(&ps->smi_mutex);
2445 ret = _mv88e6xxx_stats_wait(ds);
Vivien Didelot6b17e862015-08-13 12:52:18 -04002446 if (ret < 0)
2447 goto unlock;
2448
Vivien Didelotc161d0a2015-09-04 14:34:13 -04002449 /* Clear all ATU entries */
2450 ret = _mv88e6xxx_atu_flush(ds, 0, true);
2451 if (ret < 0)
2452 goto unlock;
2453
Vivien Didelot6b17e862015-08-13 12:52:18 -04002454 /* Clear all the VTU and STU entries */
2455 ret = _mv88e6xxx_vtu_stu_flush(ds);
2456unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04002457 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02002458
Vivien Didelot24751e22015-08-03 09:17:44 -04002459 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002460}
2461
Andrew Lunn143a8302015-04-02 04:06:34 +02002462int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
2463{
2464 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2465 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +01002466 struct gpio_desc *gpiod = ds->pd->reset;
Andrew Lunn143a8302015-04-02 04:06:34 +02002467 unsigned long timeout;
2468 int ret;
2469 int i;
2470
2471 /* Set all ports to the disabled state. */
2472 for (i = 0; i < ps->num_ports; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002473 ret = REG_READ(REG_PORT(i), PORT_CONTROL);
2474 REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc);
Andrew Lunn143a8302015-04-02 04:06:34 +02002475 }
2476
2477 /* Wait for transmit queues to drain. */
2478 usleep_range(2000, 4000);
2479
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +01002480 /* If there is a gpio connected to the reset pin, toggle it */
2481 if (gpiod) {
2482 gpiod_set_value_cansleep(gpiod, 1);
2483 usleep_range(10000, 20000);
2484 gpiod_set_value_cansleep(gpiod, 0);
2485 usleep_range(10000, 20000);
2486 }
2487
Andrew Lunn143a8302015-04-02 04:06:34 +02002488 /* Reset the switch. Keep the PPU active if requested. The PPU
2489 * needs to be active to support indirect phy register access
2490 * through global registers 0x18 and 0x19.
2491 */
2492 if (ppu_active)
2493 REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
2494 else
2495 REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
2496
2497 /* Wait up to one second for reset to complete. */
2498 timeout = jiffies + 1 * HZ;
2499 while (time_before(jiffies, timeout)) {
2500 ret = REG_READ(REG_GLOBAL, 0x00);
2501 if ((ret & is_reset) == is_reset)
2502 break;
2503 usleep_range(1000, 2000);
2504 }
2505 if (time_after(jiffies, timeout))
2506 return -ETIMEDOUT;
2507
2508 return 0;
2509}
2510
Andrew Lunn491435852015-04-02 04:06:35 +02002511int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
2512{
2513 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2514 int ret;
2515
Andrew Lunn3898c142015-05-06 01:09:53 +02002516 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002517 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02002518 if (ret < 0)
2519 goto error;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002520 ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
Andrew Lunn491435852015-04-02 04:06:35 +02002521error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002522 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02002523 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002524 return ret;
2525}
2526
2527int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2528 int reg, int val)
2529{
2530 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2531 int ret;
2532
Andrew Lunn3898c142015-05-06 01:09:53 +02002533 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002534 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02002535 if (ret < 0)
2536 goto error;
2537
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002538 ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
Andrew Lunn491435852015-04-02 04:06:35 +02002539error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002540 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02002541 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002542 return ret;
2543}
2544
2545static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
2546{
2547 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2548
2549 if (port >= 0 && port < ps->num_ports)
2550 return port;
2551 return -EINVAL;
2552}
2553
2554int
2555mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
2556{
2557 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2558 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2559 int ret;
2560
2561 if (addr < 0)
2562 return addr;
2563
Andrew Lunn3898c142015-05-06 01:09:53 +02002564 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002565 ret = _mv88e6xxx_phy_read(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002566 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002567 return ret;
2568}
2569
2570int
2571mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2572{
2573 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2574 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2575 int ret;
2576
2577 if (addr < 0)
2578 return addr;
2579
Andrew Lunn3898c142015-05-06 01:09:53 +02002580 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002581 ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002582 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002583 return ret;
2584}
2585
2586int
2587mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
2588{
2589 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2590 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2591 int ret;
2592
2593 if (addr < 0)
2594 return addr;
2595
Andrew Lunn3898c142015-05-06 01:09:53 +02002596 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002597 ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002598 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002599 return ret;
2600}
2601
2602int
2603mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
2604 u16 val)
2605{
2606 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2607 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2608 int ret;
2609
2610 if (addr < 0)
2611 return addr;
2612
Andrew Lunn3898c142015-05-06 01:09:53 +02002613 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002614 ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002615 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002616 return ret;
2617}
2618
Guenter Roeckc22995c2015-07-25 09:42:28 -07002619#ifdef CONFIG_NET_DSA_HWMON
2620
2621static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2622{
2623 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2624 int ret;
2625 int val;
2626
2627 *temp = 0;
2628
2629 mutex_lock(&ps->smi_mutex);
2630
2631 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
2632 if (ret < 0)
2633 goto error;
2634
2635 /* Enable temperature sensor */
2636 ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2637 if (ret < 0)
2638 goto error;
2639
2640 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
2641 if (ret < 0)
2642 goto error;
2643
2644 /* Wait for temperature to stabilize */
2645 usleep_range(10000, 12000);
2646
2647 val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2648 if (val < 0) {
2649 ret = val;
2650 goto error;
2651 }
2652
2653 /* Disable temperature sensor */
2654 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
2655 if (ret < 0)
2656 goto error;
2657
2658 *temp = ((val & 0x1f) - 5) * 5;
2659
2660error:
2661 _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
2662 mutex_unlock(&ps->smi_mutex);
2663 return ret;
2664}
2665
2666static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
2667{
2668 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2669 int ret;
2670
2671 *temp = 0;
2672
2673 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
2674 if (ret < 0)
2675 return ret;
2676
2677 *temp = (ret & 0xff) - 25;
2678
2679 return 0;
2680}
2681
2682int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
2683{
2684 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
2685 return mv88e63xx_get_temp(ds, temp);
2686
2687 return mv88e61xx_get_temp(ds, temp);
2688}
2689
2690int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
2691{
2692 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2693 int ret;
2694
2695 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2696 return -EOPNOTSUPP;
2697
2698 *temp = 0;
2699
2700 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2701 if (ret < 0)
2702 return ret;
2703
2704 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
2705
2706 return 0;
2707}
2708
2709int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
2710{
2711 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2712 int ret;
2713
2714 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2715 return -EOPNOTSUPP;
2716
2717 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2718 if (ret < 0)
2719 return ret;
2720 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
2721 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
2722 (ret & 0xe0ff) | (temp << 8));
2723}
2724
2725int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
2726{
2727 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2728 int ret;
2729
2730 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2731 return -EOPNOTSUPP;
2732
2733 *alarm = false;
2734
2735 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2736 if (ret < 0)
2737 return ret;
2738
2739 *alarm = !!(ret & 0x40);
2740
2741 return 0;
2742}
2743#endif /* CONFIG_NET_DSA_HWMON */
2744
Vivien Didelotb9b37712015-10-30 19:39:48 -04002745char *mv88e6xxx_lookup_name(struct device *host_dev, int sw_addr,
2746 const struct mv88e6xxx_switch_id *table,
2747 unsigned int num)
2748{
2749 struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
2750 int i, ret;
2751
2752 if (!bus)
2753 return NULL;
2754
2755 ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
2756 if (ret < 0)
2757 return NULL;
2758
2759 /* Look up the exact switch ID */
2760 for (i = 0; i < num; ++i)
2761 if (table[i].id == ret)
2762 return table[i].name;
2763
2764 /* Look up only the product number */
2765 for (i = 0; i < num; ++i) {
2766 if (table[i].id == (ret & PORT_SWITCH_ID_PROD_NUM_MASK)) {
2767 dev_warn(host_dev, "unknown revision %d, using base switch 0x%x\n",
2768 ret & PORT_SWITCH_ID_REV_MASK,
2769 ret & PORT_SWITCH_ID_PROD_NUM_MASK);
2770 return table[i].name;
2771 }
2772 }
2773
2774 return NULL;
2775}
2776
Ben Hutchings98e67302011-11-25 14:36:19 +00002777static int __init mv88e6xxx_init(void)
2778{
2779#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2780 register_switch_driver(&mv88e6131_switch_driver);
2781#endif
2782#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2783 register_switch_driver(&mv88e6123_61_65_switch_driver);
2784#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07002785#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2786 register_switch_driver(&mv88e6352_switch_driver);
2787#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02002788#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2789 register_switch_driver(&mv88e6171_switch_driver);
2790#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00002791 return 0;
2792}
2793module_init(mv88e6xxx_init);
2794
2795static void __exit mv88e6xxx_cleanup(void)
2796{
Andrew Lunn42f27252014-09-12 23:58:44 +02002797#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2798 unregister_switch_driver(&mv88e6171_switch_driver);
2799#endif
Vivien Didelot4212b542015-05-01 10:43:52 -04002800#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2801 unregister_switch_driver(&mv88e6352_switch_driver);
2802#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00002803#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2804 unregister_switch_driver(&mv88e6123_61_65_switch_driver);
2805#endif
2806#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2807 unregister_switch_driver(&mv88e6131_switch_driver);
2808#endif
2809}
2810module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00002811
2812MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
2813MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
2814MODULE_LICENSE("GPL");