blob: c952d91a5b887763d116086596a3397b6e4fc3ea [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 Lunnc8c1b392015-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{
Andrew Lunna77d43f2016-04-13 02:40:42 +020097 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000098 int ret;
99
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400100 assert_smi_lock(ds);
101
Andrew Lunna77d43f2016-04-13 02:40:42 +0200102 ret = __mv88e6xxx_reg_read(ps->bus, ps->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500103 if (ret < 0)
104 return ret;
105
106 dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
107 addr, reg, ret);
108
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000109 return ret;
110}
111
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700112int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
113{
114 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
115 int ret;
116
117 mutex_lock(&ps->smi_mutex);
118 ret = _mv88e6xxx_reg_read(ds, addr, reg);
119 mutex_unlock(&ps->smi_mutex);
120
121 return ret;
122}
123
Vivien Didelotb9b37712015-10-30 19:39:48 -0400124static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
125 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000126{
127 int ret;
128
129 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +0200130 return mdiobus_write_nested(bus, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000131
Barry Grussling3675c8d2013-01-08 16:05:53 +0000132 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000133 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
134 if (ret < 0)
135 return ret;
136
Barry Grussling3675c8d2013-01-08 16:05:53 +0000137 /* Transmit the data to write. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200138 ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000139 if (ret < 0)
140 return ret;
141
Barry Grussling3675c8d2013-01-08 16:05:53 +0000142 /* Transmit the write command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200143 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
144 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000145 if (ret < 0)
146 return ret;
147
Barry Grussling3675c8d2013-01-08 16:05:53 +0000148 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000149 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
150 if (ret < 0)
151 return ret;
152
153 return 0;
154}
155
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700156static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
157 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000158{
Andrew Lunna77d43f2016-04-13 02:40:42 +0200159 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000160
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400161 assert_smi_lock(ds);
162
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500163 dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
164 addr, reg, val);
165
Andrew Lunna77d43f2016-04-13 02:40:42 +0200166 return __mv88e6xxx_reg_write(ps->bus, ps->sw_addr, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700167}
168
169int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
170{
171 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
172 int ret;
173
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000174 mutex_lock(&ps->smi_mutex);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700175 ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000176 mutex_unlock(&ps->smi_mutex);
177
178 return ret;
179}
180
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000181int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
182{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200183 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000184
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200185 err = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_MAC_01,
186 (addr[0] << 8) | addr[1]);
187 if (err)
188 return err;
189
190 err = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_MAC_23,
191 (addr[2] << 8) | addr[3]);
192 if (err)
193 return err;
194
195 return mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_MAC_45,
196 (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000197}
198
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000199int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
200{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000201 int ret;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200202 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000203
204 for (i = 0; i < 6; i++) {
205 int j;
206
Barry Grussling3675c8d2013-01-08 16:05:53 +0000207 /* Write the MAC address byte. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200208 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
209 GLOBAL2_SWITCH_MAC_BUSY |
210 (i << 8) | addr[i]);
211 if (ret)
212 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000213
Barry Grussling3675c8d2013-01-08 16:05:53 +0000214 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000215 for (j = 0; j < 16; j++) {
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200216 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2,
217 GLOBAL2_SWITCH_MAC);
218 if (ret < 0)
219 return ret;
220
Andrew Lunncca8b132015-04-02 04:06:39 +0200221 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000222 break;
223 }
224 if (j == 16)
225 return -ETIMEDOUT;
226 }
227
228 return 0;
229}
230
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200231static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000232{
233 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200234 return _mv88e6xxx_reg_read(ds, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000235 return 0xffff;
236}
237
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200238static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum,
239 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000240{
241 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200242 return _mv88e6xxx_reg_write(ds, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000243 return 0;
244}
245
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000246#ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
247static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
248{
249 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000250 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000251
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200252 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_CONTROL);
253 if (ret < 0)
254 return ret;
255
256 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_CONTROL,
257 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
258 if (ret)
259 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000260
Barry Grussling19b2f972013-01-08 16:05:54 +0000261 timeout = jiffies + 1 * HZ;
262 while (time_before(jiffies, timeout)) {
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200263 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATUS);
264 if (ret < 0)
265 return ret;
266
Barry Grussling19b2f972013-01-08 16:05:54 +0000267 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200268 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
269 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000270 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000271 }
272
273 return -ETIMEDOUT;
274}
275
276static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
277{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200278 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000279 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000280
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200281 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_CONTROL);
282 if (ret < 0)
283 return ret;
284
285 err = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_CONTROL,
286 ret | GLOBAL_CONTROL_PPU_ENABLE);
287 if (err)
288 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000289
Barry Grussling19b2f972013-01-08 16:05:54 +0000290 timeout = jiffies + 1 * HZ;
291 while (time_before(jiffies, timeout)) {
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200292 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATUS);
293 if (ret < 0)
294 return ret;
295
Barry Grussling19b2f972013-01-08 16:05:54 +0000296 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200297 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
298 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000299 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000300 }
301
302 return -ETIMEDOUT;
303}
304
305static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
306{
307 struct mv88e6xxx_priv_state *ps;
308
309 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
310 if (mutex_trylock(&ps->ppu_mutex)) {
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200311 struct dsa_switch *ds = ps->ds;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000312
Barry Grussling85686582013-01-08 16:05:56 +0000313 if (mv88e6xxx_ppu_enable(ds) == 0)
314 ps->ppu_disabled = 0;
315 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000316 }
317}
318
319static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
320{
321 struct mv88e6xxx_priv_state *ps = (void *)_ps;
322
323 schedule_work(&ps->ppu_work);
324}
325
326static int mv88e6xxx_ppu_access_get(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 int ret;
330
331 mutex_lock(&ps->ppu_mutex);
332
Barry Grussling3675c8d2013-01-08 16:05:53 +0000333 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000334 * we can access the PHY registers. If it was already
335 * disabled, cancel the timer that is going to re-enable
336 * it.
337 */
338 if (!ps->ppu_disabled) {
Barry Grussling85686582013-01-08 16:05:56 +0000339 ret = mv88e6xxx_ppu_disable(ds);
340 if (ret < 0) {
341 mutex_unlock(&ps->ppu_mutex);
342 return ret;
343 }
344 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000345 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000346 del_timer(&ps->ppu_timer);
347 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000348 }
349
350 return ret;
351}
352
353static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
354{
Florian Fainellia22adce2014-04-28 11:14:28 -0700355 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000356
Barry Grussling3675c8d2013-01-08 16:05:53 +0000357 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000358 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
359 mutex_unlock(&ps->ppu_mutex);
360}
361
362void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
363{
Florian Fainellia22adce2014-04-28 11:14:28 -0700364 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000365
366 mutex_init(&ps->ppu_mutex);
367 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
368 init_timer(&ps->ppu_timer);
369 ps->ppu_timer.data = (unsigned long)ps;
370 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
371}
372
373int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
374{
375 int ret;
376
377 ret = mv88e6xxx_ppu_access_get(ds);
378 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000379 ret = mv88e6xxx_reg_read(ds, addr, regnum);
380 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000381 }
382
383 return ret;
384}
385
386int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
387 int regnum, u16 val)
388{
389 int ret;
390
391 ret = mv88e6xxx_ppu_access_get(ds);
392 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000393 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
394 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000395 }
396
397 return ret;
398}
399#endif
400
Andrew Lunn54d792f2015-05-06 01:09:47 +0200401static bool mv88e6xxx_6065_family(struct dsa_switch *ds)
402{
403 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
404
Vivien Didelot22356472016-04-17 13:24:00 -0400405 return ps->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200406}
407
408static bool mv88e6xxx_6095_family(struct dsa_switch *ds)
409{
410 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
411
Vivien Didelot22356472016-04-17 13:24:00 -0400412 return ps->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200413}
414
415static bool mv88e6xxx_6097_family(struct dsa_switch *ds)
416{
417 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
418
Vivien Didelot22356472016-04-17 13:24:00 -0400419 return ps->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200420}
421
422static bool mv88e6xxx_6165_family(struct dsa_switch *ds)
423{
424 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
425
Vivien Didelot22356472016-04-17 13:24:00 -0400426 return ps->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200427}
428
429static bool mv88e6xxx_6185_family(struct dsa_switch *ds)
430{
431 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
432
Vivien Didelot22356472016-04-17 13:24:00 -0400433 return ps->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200434}
435
Guenter Roeckc22995c2015-07-25 09:42:28 -0700436static bool mv88e6xxx_6320_family(struct dsa_switch *ds)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700437{
438 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
439
Vivien Didelot22356472016-04-17 13:24:00 -0400440 return ps->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700441}
442
Andrew Lunn54d792f2015-05-06 01:09:47 +0200443static bool mv88e6xxx_6351_family(struct dsa_switch *ds)
444{
445 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
446
Vivien Didelot22356472016-04-17 13:24:00 -0400447 return ps->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200448}
449
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200450static bool mv88e6xxx_6352_family(struct dsa_switch *ds)
451{
452 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
453
Vivien Didelot22356472016-04-17 13:24:00 -0400454 return ps->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200455}
456
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400457static unsigned int mv88e6xxx_num_databases(struct dsa_switch *ds)
458{
459 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
460
461 /* The following devices have 4-bit identifiers for 16 databases */
462 if (ps->id == PORT_SWITCH_ID_6061)
463 return 16;
464
465 /* The following devices have 6-bit identifiers for 64 databases */
466 if (ps->id == PORT_SWITCH_ID_6065)
467 return 64;
468
469 /* The following devices have 8-bit identifiers for 256 databases */
470 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
471 return 256;
472
473 /* The following devices have 12-bit identifiers for 4096 databases */
474 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
475 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds))
476 return 4096;
477
478 return 0;
479}
480
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400481static bool mv88e6xxx_has_fid_reg(struct dsa_switch *ds)
482{
483 /* Does the device have dedicated FID registers for ATU and VTU ops? */
484 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
485 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds))
486 return true;
487
488 return false;
489}
490
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -0400491static bool mv88e6xxx_has_stu(struct dsa_switch *ds)
492{
493 /* Does the device have STU and dedicated SID registers for VTU ops? */
494 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
495 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds))
496 return true;
497
498 return false;
499}
500
Andrew Lunndea87022015-08-31 15:56:47 +0200501/* We expect the switch to perform auto negotiation if there is a real
502 * phy. However, in the case of a fixed link phy, we force the port
503 * settings from the fixed link settings.
504 */
505void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
506 struct phy_device *phydev)
507{
508 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200509 u32 reg;
510 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200511
512 if (!phy_is_pseudo_fixed_link(phydev))
513 return;
514
515 mutex_lock(&ps->smi_mutex);
516
517 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
518 if (ret < 0)
519 goto out;
520
521 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
522 PORT_PCS_CTRL_FORCE_LINK |
523 PORT_PCS_CTRL_DUPLEX_FULL |
524 PORT_PCS_CTRL_FORCE_DUPLEX |
525 PORT_PCS_CTRL_UNFORCED);
526
527 reg |= PORT_PCS_CTRL_FORCE_LINK;
528 if (phydev->link)
529 reg |= PORT_PCS_CTRL_LINK_UP;
530
531 if (mv88e6xxx_6065_family(ds) && phydev->speed > SPEED_100)
532 goto out;
533
534 switch (phydev->speed) {
535 case SPEED_1000:
536 reg |= PORT_PCS_CTRL_1000;
537 break;
538 case SPEED_100:
539 reg |= PORT_PCS_CTRL_100;
540 break;
541 case SPEED_10:
542 reg |= PORT_PCS_CTRL_10;
543 break;
544 default:
545 pr_info("Unknown speed");
546 goto out;
547 }
548
549 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
550 if (phydev->duplex == DUPLEX_FULL)
551 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
552
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200553 if ((mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds)) &&
Vivien Didelot009a2b92016-04-17 13:24:01 -0400554 (port >= ps->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200555 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
556 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
557 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
558 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
559 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
560 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
561 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
562 }
Andrew Lunndea87022015-08-31 15:56:47 +0200563 _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_PCS_CTRL, reg);
564
565out:
566 mutex_unlock(&ps->smi_mutex);
567}
568
Andrew Lunn31888232015-05-06 01:09:54 +0200569static int _mv88e6xxx_stats_wait(struct dsa_switch *ds)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000570{
571 int ret;
572 int i;
573
574 for (i = 0; i < 10; i++) {
Andrew Lunn31888232015-05-06 01:09:54 +0200575 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200576 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000577 return 0;
578 }
579
580 return -ETIMEDOUT;
581}
582
Andrew Lunn31888232015-05-06 01:09:54 +0200583static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000584{
585 int ret;
586
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700587 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200588 port = (port + 1) << 5;
589
Barry Grussling3675c8d2013-01-08 16:05:53 +0000590 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn31888232015-05-06 01:09:54 +0200591 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
592 GLOBAL_STATS_OP_CAPTURE_PORT |
593 GLOBAL_STATS_OP_HIST_RX_TX | port);
594 if (ret < 0)
595 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000596
Barry Grussling3675c8d2013-01-08 16:05:53 +0000597 /* Wait for the snapshotting to complete. */
Andrew Lunn31888232015-05-06 01:09:54 +0200598 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000599 if (ret < 0)
600 return ret;
601
602 return 0;
603}
604
Andrew Lunn31888232015-05-06 01:09:54 +0200605static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000606{
607 u32 _val;
608 int ret;
609
610 *val = 0;
611
Andrew Lunn31888232015-05-06 01:09:54 +0200612 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
613 GLOBAL_STATS_OP_READ_CAPTURED |
614 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000615 if (ret < 0)
616 return;
617
Andrew Lunn31888232015-05-06 01:09:54 +0200618 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000619 if (ret < 0)
620 return;
621
Andrew Lunn31888232015-05-06 01:09:54 +0200622 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000623 if (ret < 0)
624 return;
625
626 _val = ret << 16;
627
Andrew Lunn31888232015-05-06 01:09:54 +0200628 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000629 if (ret < 0)
630 return;
631
632 *val = _val | ret;
633}
634
Andrew Lunne413e7e2015-04-02 04:06:38 +0200635static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100636 { "in_good_octets", 8, 0x00, BANK0, },
637 { "in_bad_octets", 4, 0x02, BANK0, },
638 { "in_unicast", 4, 0x04, BANK0, },
639 { "in_broadcasts", 4, 0x06, BANK0, },
640 { "in_multicasts", 4, 0x07, BANK0, },
641 { "in_pause", 4, 0x16, BANK0, },
642 { "in_undersize", 4, 0x18, BANK0, },
643 { "in_fragments", 4, 0x19, BANK0, },
644 { "in_oversize", 4, 0x1a, BANK0, },
645 { "in_jabber", 4, 0x1b, BANK0, },
646 { "in_rx_error", 4, 0x1c, BANK0, },
647 { "in_fcs_error", 4, 0x1d, BANK0, },
648 { "out_octets", 8, 0x0e, BANK0, },
649 { "out_unicast", 4, 0x10, BANK0, },
650 { "out_broadcasts", 4, 0x13, BANK0, },
651 { "out_multicasts", 4, 0x12, BANK0, },
652 { "out_pause", 4, 0x15, BANK0, },
653 { "excessive", 4, 0x11, BANK0, },
654 { "collisions", 4, 0x1e, BANK0, },
655 { "deferred", 4, 0x05, BANK0, },
656 { "single", 4, 0x14, BANK0, },
657 { "multiple", 4, 0x17, BANK0, },
658 { "out_fcs_error", 4, 0x03, BANK0, },
659 { "late", 4, 0x1f, BANK0, },
660 { "hist_64bytes", 4, 0x08, BANK0, },
661 { "hist_65_127bytes", 4, 0x09, BANK0, },
662 { "hist_128_255bytes", 4, 0x0a, BANK0, },
663 { "hist_256_511bytes", 4, 0x0b, BANK0, },
664 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
665 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
666 { "sw_in_discards", 4, 0x10, PORT, },
667 { "sw_in_filtered", 2, 0x12, PORT, },
668 { "sw_out_filtered", 2, 0x13, PORT, },
669 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
670 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
671 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
672 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
673 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
674 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
675 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
676 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
677 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
678 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
679 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
680 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
681 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
682 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
683 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
684 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
685 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
686 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
687 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
688 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
689 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
690 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
691 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
692 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
693 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
694 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200695};
696
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100697static bool mv88e6xxx_has_stat(struct dsa_switch *ds,
698 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200699{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100700 switch (stat->type) {
701 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200702 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100703 case BANK1:
704 return mv88e6xxx_6320_family(ds);
705 case PORT:
706 return mv88e6xxx_6095_family(ds) ||
707 mv88e6xxx_6185_family(ds) ||
708 mv88e6xxx_6097_family(ds) ||
709 mv88e6xxx_6165_family(ds) ||
710 mv88e6xxx_6351_family(ds) ||
711 mv88e6xxx_6352_family(ds);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200712 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100713 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000714}
715
Andrew Lunn80c46272015-06-20 18:42:30 +0200716static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100717 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200718 int port)
719{
Andrew Lunn80c46272015-06-20 18:42:30 +0200720 u32 low;
721 u32 high = 0;
722 int ret;
723 u64 value;
724
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100725 switch (s->type) {
726 case PORT:
727 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200728 if (ret < 0)
729 return UINT64_MAX;
730
731 low = ret;
732 if (s->sizeof_stat == 4) {
733 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100734 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200735 if (ret < 0)
736 return UINT64_MAX;
737 high = ret;
738 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100739 break;
740 case BANK0:
741 case BANK1:
Andrew Lunn80c46272015-06-20 18:42:30 +0200742 _mv88e6xxx_stats_read(ds, s->reg, &low);
743 if (s->sizeof_stat == 8)
744 _mv88e6xxx_stats_read(ds, s->reg + 1, &high);
745 }
746 value = (((u64)high) << 16) | low;
747 return value;
748}
749
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100750void mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
751{
752 struct mv88e6xxx_hw_stat *stat;
753 int i, j;
754
755 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
756 stat = &mv88e6xxx_hw_stats[i];
757 if (mv88e6xxx_has_stat(ds, stat)) {
758 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
759 ETH_GSTRING_LEN);
760 j++;
761 }
762 }
763}
764
765int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
766{
767 struct mv88e6xxx_hw_stat *stat;
768 int i, j;
769
770 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
771 stat = &mv88e6xxx_hw_stats[i];
772 if (mv88e6xxx_has_stat(ds, stat))
773 j++;
774 }
775 return j;
776}
777
778void
779mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
780 int port, uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000781{
Florian Fainellia22adce2014-04-28 11:14:28 -0700782 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100783 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000784 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100785 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000786
Andrew Lunn31888232015-05-06 01:09:54 +0200787 mutex_lock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000788
Andrew Lunn31888232015-05-06 01:09:54 +0200789 ret = _mv88e6xxx_stats_snapshot(ds, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000790 if (ret < 0) {
Andrew Lunn31888232015-05-06 01:09:54 +0200791 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000792 return;
793 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100794 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
795 stat = &mv88e6xxx_hw_stats[i];
796 if (mv88e6xxx_has_stat(ds, stat)) {
797 data[j] = _mv88e6xxx_get_ethtool_stat(ds, stat, port);
798 j++;
799 }
800 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000801
Andrew Lunn31888232015-05-06 01:09:54 +0200802 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000803}
Ben Hutchings98e67302011-11-25 14:36:19 +0000804
Guenter Roecka1ab91f32014-10-29 10:45:05 -0700805int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
806{
807 return 32 * sizeof(u16);
808}
809
810void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
811 struct ethtool_regs *regs, void *_p)
812{
813 u16 *p = _p;
814 int i;
815
816 regs->version = 0;
817
818 memset(p, 0xff, 32 * sizeof(u16));
819
820 for (i = 0; i < 32; i++) {
821 int ret;
822
823 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
824 if (ret >= 0)
825 p[i] = ret;
826 }
827}
828
Andrew Lunn3898c142015-05-06 01:09:53 +0200829static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset,
830 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700831{
832 unsigned long timeout = jiffies + HZ / 10;
833
834 while (time_before(jiffies, timeout)) {
835 int ret;
836
837 ret = _mv88e6xxx_reg_read(ds, reg, offset);
838 if (ret < 0)
839 return ret;
840 if (!(ret & mask))
841 return 0;
842
843 usleep_range(1000, 2000);
844 }
845 return -ETIMEDOUT;
846}
847
Andrew Lunn3898c142015-05-06 01:09:53 +0200848static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
849{
850 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
851 int ret;
852
853 mutex_lock(&ps->smi_mutex);
854 ret = _mv88e6xxx_wait(ds, reg, offset, mask);
855 mutex_unlock(&ps->smi_mutex);
856
857 return ret;
858}
859
860static int _mv88e6xxx_phy_wait(struct dsa_switch *ds)
861{
862 return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
863 GLOBAL2_SMI_OP_BUSY);
864}
865
866int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
867{
868 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
869 GLOBAL2_EEPROM_OP_LOAD);
870}
871
872int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
873{
874 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
875 GLOBAL2_EEPROM_OP_BUSY);
876}
877
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700878static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
879{
Andrew Lunncca8b132015-04-02 04:06:39 +0200880 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP,
881 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700882}
883
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200884static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr,
885 int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +0100886{
887 int ret;
888
Andrew Lunn3898c142015-05-06 01:09:53 +0200889 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
890 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
891 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +0100892 if (ret < 0)
893 return ret;
894
Andrew Lunn3898c142015-05-06 01:09:53 +0200895 ret = _mv88e6xxx_phy_wait(ds);
896 if (ret < 0)
897 return ret;
898
899 return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunnf3044682015-02-14 19:17:50 +0100900}
901
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200902static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr,
903 int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +0100904{
Andrew Lunn3898c142015-05-06 01:09:53 +0200905 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100906
Andrew Lunn3898c142015-05-06 01:09:53 +0200907 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
908 if (ret < 0)
909 return ret;
910
911 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
912 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
913 regnum);
914
915 return _mv88e6xxx_phy_wait(ds);
Andrew Lunnf3044682015-02-14 19:17:50 +0100916}
917
Guenter Roeck11b3b452015-03-06 22:23:51 -0800918int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
919{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200920 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800921 int reg;
922
Andrew Lunn3898c142015-05-06 01:09:53 +0200923 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200924
925 reg = _mv88e6xxx_phy_read_indirect(ds, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800926 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200927 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800928
929 e->eee_enabled = !!(reg & 0x0200);
930 e->tx_lpi_enabled = !!(reg & 0x0100);
931
Andrew Lunn3898c142015-05-06 01:09:53 +0200932 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800933 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200934 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800935
Andrew Lunncca8b132015-04-02 04:06:39 +0200936 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200937 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800938
Andrew Lunn2f40c692015-04-02 04:06:37 +0200939out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200940 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200941 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800942}
943
944int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
945 struct phy_device *phydev, struct ethtool_eee *e)
946{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200947 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
948 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800949 int ret;
950
Andrew Lunn3898c142015-05-06 01:09:53 +0200951 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800952
Andrew Lunn2f40c692015-04-02 04:06:37 +0200953 ret = _mv88e6xxx_phy_read_indirect(ds, port, 16);
954 if (ret < 0)
955 goto out;
956
957 reg = ret & ~0x0300;
958 if (e->eee_enabled)
959 reg |= 0x0200;
960 if (e->tx_lpi_enabled)
961 reg |= 0x0100;
962
963 ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg);
964out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200965 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200966
967 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800968}
969
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400970static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700971{
972 int ret;
973
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400974 if (mv88e6xxx_has_fid_reg(ds)) {
975 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid);
976 if (ret < 0)
977 return ret;
Vivien Didelot11ea8092016-03-31 16:53:44 -0400978 } else if (mv88e6xxx_num_databases(ds) == 256) {
979 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
980 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_CONTROL);
981 if (ret < 0)
982 return ret;
983
984 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_CONTROL,
985 (ret & 0xfff) |
986 ((fid << 8) & 0xf000));
987 if (ret < 0)
988 return ret;
989
990 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
991 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400992 }
993
Andrew Lunncca8b132015-04-02 04:06:39 +0200994 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700995 if (ret < 0)
996 return ret;
997
998 return _mv88e6xxx_atu_wait(ds);
999}
1000
Vivien Didelot37705b72015-09-04 14:34:11 -04001001static int _mv88e6xxx_atu_data_write(struct dsa_switch *ds,
1002 struct mv88e6xxx_atu_entry *entry)
1003{
1004 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1005
1006 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1007 unsigned int mask, shift;
1008
1009 if (entry->trunk) {
1010 data |= GLOBAL_ATU_DATA_TRUNK;
1011 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1012 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1013 } else {
1014 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1015 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1016 }
1017
1018 data |= (entry->portv_trunkid << shift) & mask;
1019 }
1020
1021 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1022}
1023
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001024static int _mv88e6xxx_atu_flush_move(struct dsa_switch *ds,
1025 struct mv88e6xxx_atu_entry *entry,
1026 bool static_too)
1027{
1028 int op;
1029 int err;
1030
1031 err = _mv88e6xxx_atu_wait(ds);
1032 if (err)
1033 return err;
1034
1035 err = _mv88e6xxx_atu_data_write(ds, entry);
1036 if (err)
1037 return err;
1038
1039 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001040 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1041 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1042 } else {
1043 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1044 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1045 }
1046
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001047 return _mv88e6xxx_atu_cmd(ds, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001048}
1049
1050static int _mv88e6xxx_atu_flush(struct dsa_switch *ds, u16 fid, bool static_too)
1051{
1052 struct mv88e6xxx_atu_entry entry = {
1053 .fid = fid,
1054 .state = 0, /* EntryState bits must be 0 */
1055 };
1056
1057 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1058}
1059
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001060static int _mv88e6xxx_atu_move(struct dsa_switch *ds, u16 fid, int from_port,
1061 int to_port, bool static_too)
1062{
1063 struct mv88e6xxx_atu_entry entry = {
1064 .trunk = false,
1065 .fid = fid,
1066 };
1067
1068 /* EntryState bits must be 0xF */
1069 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1070
1071 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1072 entry.portv_trunkid = (to_port & 0x0f) << 4;
1073 entry.portv_trunkid |= from_port & 0x0f;
1074
1075 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1076}
1077
1078static int _mv88e6xxx_atu_remove(struct dsa_switch *ds, u16 fid, int port,
1079 bool static_too)
1080{
1081 /* Destination port 0xF means remove the entries */
1082 return _mv88e6xxx_atu_move(ds, fid, port, 0x0f, static_too);
1083}
1084
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001085static const char * const mv88e6xxx_port_state_names[] = {
1086 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1087 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1088 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1089 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1090};
1091
1092static int _mv88e6xxx_port_state(struct dsa_switch *ds, int port, u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001093{
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001094 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001095 u8 oldstate;
1096
Andrew Lunncca8b132015-04-02 04:06:39 +02001097 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001098 if (reg < 0)
1099 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001100
Andrew Lunncca8b132015-04-02 04:06:39 +02001101 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001102
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001103 if (oldstate != state) {
1104 /* Flush forwarding database if we're moving a port
1105 * from Learning or Forwarding state to Disabled or
1106 * Blocking or Listening state.
1107 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001108 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1109 oldstate == PORT_CONTROL_STATE_FORWARDING)
1110 && (state == PORT_CONTROL_STATE_DISABLED ||
1111 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelot2b8157b2015-09-04 14:34:16 -04001112 ret = _mv88e6xxx_atu_remove(ds, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001113 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001114 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001115 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001116
Andrew Lunncca8b132015-04-02 04:06:39 +02001117 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1118 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
1119 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001120 if (ret)
1121 return ret;
1122
1123 netdev_dbg(ds->ports[port], "PortState %s (was %s)\n",
1124 mv88e6xxx_port_state_names[state],
1125 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001126 }
1127
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001128 return ret;
1129}
1130
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001131static int _mv88e6xxx_port_based_vlan_map(struct dsa_switch *ds, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001132{
1133 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001134 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot009a2b92016-04-17 13:24:01 -04001135 const u16 mask = (1 << ps->info->num_ports) - 1;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001136 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001137 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001138 int i;
1139
1140 /* allow CPU port or DSA link(s) to send frames to every port */
1141 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1142 output_ports = mask;
1143 } else {
Vivien Didelot009a2b92016-04-17 13:24:01 -04001144 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001145 /* allow sending frames to every group member */
1146 if (bridge && ps->ports[i].bridge_dev == bridge)
1147 output_ports |= BIT(i);
1148
1149 /* allow sending frames to CPU port and DSA link(s) */
1150 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1151 output_ports |= BIT(i);
1152 }
1153 }
1154
1155 /* prevent frames from going back out of the port they came in on */
1156 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001157
Vivien Didelotede80982015-10-11 18:08:35 -04001158 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1159 if (reg < 0)
1160 return reg;
1161
1162 reg &= ~mask;
1163 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001164
Andrew Lunncca8b132015-04-02 04:06:39 +02001165 return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001166}
1167
Vivien Didelot43c44a92016-04-06 11:55:03 -04001168void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001169{
1170 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1171 int stp_state;
1172
1173 switch (state) {
1174 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001175 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001176 break;
1177 case BR_STATE_BLOCKING:
1178 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001179 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001180 break;
1181 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001182 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001183 break;
1184 case BR_STATE_FORWARDING:
1185 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001186 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001187 break;
1188 }
1189
Vivien Didelot43c44a92016-04-06 11:55:03 -04001190 /* mv88e6xxx_port_stp_state_set may be called with softirqs disabled,
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001191 * so we can not update the port state directly but need to schedule it.
1192 */
Vivien Didelotd715fa62016-02-12 12:09:38 -05001193 ps->ports[port].state = stp_state;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001194 set_bit(port, ps->port_state_update_mask);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001195 schedule_work(&ps->bridge_work);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001196}
1197
Vivien Didelot5da96032016-03-07 18:24:39 -05001198static int _mv88e6xxx_port_pvid(struct dsa_switch *ds, int port, u16 *new,
1199 u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001200{
Vivien Didelot5da96032016-03-07 18:24:39 -05001201 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001202 int ret;
1203
1204 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN);
1205 if (ret < 0)
1206 return ret;
1207
Vivien Didelot5da96032016-03-07 18:24:39 -05001208 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1209
1210 if (new) {
1211 ret &= ~PORT_DEFAULT_VLAN_MASK;
1212 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1213
1214 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1215 PORT_DEFAULT_VLAN, ret);
1216 if (ret < 0)
1217 return ret;
1218
1219 netdev_dbg(ds->ports[port], "DefaultVID %d (was %d)\n", *new,
1220 pvid);
1221 }
1222
1223 if (old)
1224 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001225
1226 return 0;
1227}
1228
Vivien Didelot5da96032016-03-07 18:24:39 -05001229static int _mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid)
1230{
1231 return _mv88e6xxx_port_pvid(ds, port, NULL, pvid);
1232}
1233
Vivien Didelot76e398a2015-11-01 12:33:55 -05001234static int _mv88e6xxx_port_pvid_set(struct dsa_switch *ds, int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001235{
Vivien Didelot5da96032016-03-07 18:24:39 -05001236 return _mv88e6xxx_port_pvid(ds, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001237}
1238
Vivien Didelot6b17e862015-08-13 12:52:18 -04001239static int _mv88e6xxx_vtu_wait(struct dsa_switch *ds)
1240{
1241 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_VTU_OP,
1242 GLOBAL_VTU_OP_BUSY);
1243}
1244
1245static int _mv88e6xxx_vtu_cmd(struct dsa_switch *ds, u16 op)
1246{
1247 int ret;
1248
1249 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_OP, op);
1250 if (ret < 0)
1251 return ret;
1252
1253 return _mv88e6xxx_vtu_wait(ds);
1254}
1255
1256static int _mv88e6xxx_vtu_stu_flush(struct dsa_switch *ds)
1257{
1258 int ret;
1259
1260 ret = _mv88e6xxx_vtu_wait(ds);
1261 if (ret < 0)
1262 return ret;
1263
1264 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_FLUSH_ALL);
1265}
1266
Vivien Didelotb8fee952015-08-13 12:52:19 -04001267static int _mv88e6xxx_vtu_stu_data_read(struct dsa_switch *ds,
1268 struct mv88e6xxx_vtu_stu_entry *entry,
1269 unsigned int nibble_offset)
1270{
1271 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1272 u16 regs[3];
1273 int i;
1274 int ret;
1275
1276 for (i = 0; i < 3; ++i) {
1277 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1278 GLOBAL_VTU_DATA_0_3 + i);
1279 if (ret < 0)
1280 return ret;
1281
1282 regs[i] = ret;
1283 }
1284
Vivien Didelot009a2b92016-04-17 13:24:01 -04001285 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001286 unsigned int shift = (i % 4) * 4 + nibble_offset;
1287 u16 reg = regs[i / 4];
1288
1289 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1290 }
1291
1292 return 0;
1293}
1294
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001295static int _mv88e6xxx_vtu_stu_data_write(struct dsa_switch *ds,
1296 struct mv88e6xxx_vtu_stu_entry *entry,
1297 unsigned int nibble_offset)
1298{
1299 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1300 u16 regs[3] = { 0 };
1301 int i;
1302 int ret;
1303
Vivien Didelot009a2b92016-04-17 13:24:01 -04001304 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001305 unsigned int shift = (i % 4) * 4 + nibble_offset;
1306 u8 data = entry->data[i];
1307
1308 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1309 }
1310
1311 for (i = 0; i < 3; ++i) {
1312 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL,
1313 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1314 if (ret < 0)
1315 return ret;
1316 }
1317
1318 return 0;
1319}
1320
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001321static int _mv88e6xxx_vtu_vid_write(struct dsa_switch *ds, u16 vid)
1322{
1323 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID,
1324 vid & GLOBAL_VTU_VID_MASK);
1325}
1326
1327static int _mv88e6xxx_vtu_getnext(struct dsa_switch *ds,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001328 struct mv88e6xxx_vtu_stu_entry *entry)
1329{
1330 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1331 int ret;
1332
1333 ret = _mv88e6xxx_vtu_wait(ds);
1334 if (ret < 0)
1335 return ret;
1336
Vivien Didelotb8fee952015-08-13 12:52:19 -04001337 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_GET_NEXT);
1338 if (ret < 0)
1339 return ret;
1340
1341 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1342 if (ret < 0)
1343 return ret;
1344
1345 next.vid = ret & GLOBAL_VTU_VID_MASK;
1346 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1347
1348 if (next.valid) {
1349 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 0);
1350 if (ret < 0)
1351 return ret;
1352
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001353 if (mv88e6xxx_has_fid_reg(ds)) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001354 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1355 GLOBAL_VTU_FID);
1356 if (ret < 0)
1357 return ret;
1358
1359 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001360 } else if (mv88e6xxx_num_databases(ds) == 256) {
1361 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1362 * VTU DBNum[3:0] are located in VTU Operation 3:0
1363 */
1364 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1365 GLOBAL_VTU_OP);
1366 if (ret < 0)
1367 return ret;
1368
1369 next.fid = (ret & 0xf00) >> 4;
1370 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001371 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001372
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001373 if (mv88e6xxx_has_stu(ds)) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001374 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1375 GLOBAL_VTU_SID);
1376 if (ret < 0)
1377 return ret;
1378
1379 next.sid = ret & GLOBAL_VTU_SID_MASK;
1380 }
1381 }
1382
1383 *entry = next;
1384 return 0;
1385}
1386
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001387int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1388 struct switchdev_obj_port_vlan *vlan,
1389 int (*cb)(struct switchdev_obj *obj))
1390{
1391 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1392 struct mv88e6xxx_vtu_stu_entry next;
1393 u16 pvid;
1394 int err;
1395
1396 mutex_lock(&ps->smi_mutex);
1397
1398 err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1399 if (err)
1400 goto unlock;
1401
1402 err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1403 if (err)
1404 goto unlock;
1405
1406 do {
1407 err = _mv88e6xxx_vtu_getnext(ds, &next);
1408 if (err)
1409 break;
1410
1411 if (!next.valid)
1412 break;
1413
1414 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1415 continue;
1416
1417 /* reinit and dump this VLAN obj */
1418 vlan->vid_begin = vlan->vid_end = next.vid;
1419 vlan->flags = 0;
1420
1421 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1422 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1423
1424 if (next.vid == pvid)
1425 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1426
1427 err = cb(&vlan->obj);
1428 if (err)
1429 break;
1430 } while (next.vid < GLOBAL_VTU_VID_MASK);
1431
1432unlock:
1433 mutex_unlock(&ps->smi_mutex);
1434
1435 return err;
1436}
1437
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001438static int _mv88e6xxx_vtu_loadpurge(struct dsa_switch *ds,
1439 struct mv88e6xxx_vtu_stu_entry *entry)
1440{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001441 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001442 u16 reg = 0;
1443 int ret;
1444
1445 ret = _mv88e6xxx_vtu_wait(ds);
1446 if (ret < 0)
1447 return ret;
1448
1449 if (!entry->valid)
1450 goto loadpurge;
1451
1452 /* Write port member tags */
1453 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 0);
1454 if (ret < 0)
1455 return ret;
1456
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001457 if (mv88e6xxx_has_stu(ds)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001458 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1459 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1460 if (ret < 0)
1461 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001462 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001463
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001464 if (mv88e6xxx_has_fid_reg(ds)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001465 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1466 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_FID, reg);
1467 if (ret < 0)
1468 return ret;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001469 } else if (mv88e6xxx_num_databases(ds) == 256) {
1470 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1471 * VTU DBNum[3:0] are located in VTU Operation 3:0
1472 */
1473 op |= (entry->fid & 0xf0) << 8;
1474 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001475 }
1476
1477 reg = GLOBAL_VTU_VID_VALID;
1478loadpurge:
1479 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1480 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1481 if (ret < 0)
1482 return ret;
1483
Vivien Didelot11ea8092016-03-31 16:53:44 -04001484 return _mv88e6xxx_vtu_cmd(ds, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001485}
1486
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001487static int _mv88e6xxx_stu_getnext(struct dsa_switch *ds, u8 sid,
1488 struct mv88e6xxx_vtu_stu_entry *entry)
1489{
1490 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1491 int ret;
1492
1493 ret = _mv88e6xxx_vtu_wait(ds);
1494 if (ret < 0)
1495 return ret;
1496
1497 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID,
1498 sid & GLOBAL_VTU_SID_MASK);
1499 if (ret < 0)
1500 return ret;
1501
1502 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_GET_NEXT);
1503 if (ret < 0)
1504 return ret;
1505
1506 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_SID);
1507 if (ret < 0)
1508 return ret;
1509
1510 next.sid = ret & GLOBAL_VTU_SID_MASK;
1511
1512 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1513 if (ret < 0)
1514 return ret;
1515
1516 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1517
1518 if (next.valid) {
1519 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 2);
1520 if (ret < 0)
1521 return ret;
1522 }
1523
1524 *entry = next;
1525 return 0;
1526}
1527
1528static int _mv88e6xxx_stu_loadpurge(struct dsa_switch *ds,
1529 struct mv88e6xxx_vtu_stu_entry *entry)
1530{
1531 u16 reg = 0;
1532 int ret;
1533
1534 ret = _mv88e6xxx_vtu_wait(ds);
1535 if (ret < 0)
1536 return ret;
1537
1538 if (!entry->valid)
1539 goto loadpurge;
1540
1541 /* Write port states */
1542 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 2);
1543 if (ret < 0)
1544 return ret;
1545
1546 reg = GLOBAL_VTU_VID_VALID;
1547loadpurge:
1548 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1549 if (ret < 0)
1550 return ret;
1551
1552 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1553 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1554 if (ret < 0)
1555 return ret;
1556
1557 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1558}
1559
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001560static int _mv88e6xxx_port_fid(struct dsa_switch *ds, int port, u16 *new,
1561 u16 *old)
1562{
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001563 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001564 u16 fid;
1565 int ret;
1566
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001567 if (mv88e6xxx_num_databases(ds) == 4096)
1568 upper_mask = 0xff;
Vivien Didelot11ea8092016-03-31 16:53:44 -04001569 else if (mv88e6xxx_num_databases(ds) == 256)
1570 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001571 else
1572 return -EOPNOTSUPP;
1573
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001574 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1575 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1576 if (ret < 0)
1577 return ret;
1578
1579 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1580
1581 if (new) {
1582 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1583 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1584
1585 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN,
1586 ret);
1587 if (ret < 0)
1588 return ret;
1589 }
1590
1591 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1592 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL_1);
1593 if (ret < 0)
1594 return ret;
1595
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001596 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001597
1598 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001599 ret &= ~upper_mask;
1600 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001601
1602 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1,
1603 ret);
1604 if (ret < 0)
1605 return ret;
1606
1607 netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid);
1608 }
1609
1610 if (old)
1611 *old = fid;
1612
1613 return 0;
1614}
1615
1616static int _mv88e6xxx_port_fid_get(struct dsa_switch *ds, int port, u16 *fid)
1617{
1618 return _mv88e6xxx_port_fid(ds, port, NULL, fid);
1619}
1620
1621static int _mv88e6xxx_port_fid_set(struct dsa_switch *ds, int port, u16 fid)
1622{
1623 return _mv88e6xxx_port_fid(ds, port, &fid, NULL);
1624}
1625
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001626static int _mv88e6xxx_fid_new(struct dsa_switch *ds, u16 *fid)
1627{
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001628 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001629 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1630 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001631 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001632
1633 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1634
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001635 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001636 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001637 err = _mv88e6xxx_port_fid_get(ds, i, fid);
1638 if (err)
1639 return err;
1640
1641 set_bit(*fid, fid_bitmap);
1642 }
1643
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001644 /* Set every FID bit used by the VLAN entries */
1645 err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK);
1646 if (err)
1647 return err;
1648
1649 do {
1650 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1651 if (err)
1652 return err;
1653
1654 if (!vlan.valid)
1655 break;
1656
1657 set_bit(vlan.fid, fid_bitmap);
1658 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1659
1660 /* The reset value 0x000 is used to indicate that multiple address
1661 * databases are not needed. Return the next positive available.
1662 */
1663 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001664 if (unlikely(*fid >= mv88e6xxx_num_databases(ds)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001665 return -ENOSPC;
1666
1667 /* Clear the database */
1668 return _mv88e6xxx_atu_flush(ds, *fid, true);
1669}
1670
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001671static int _mv88e6xxx_vtu_new(struct dsa_switch *ds, u16 vid,
1672 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001673{
1674 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1675 struct mv88e6xxx_vtu_stu_entry vlan = {
1676 .valid = true,
1677 .vid = vid,
1678 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001679 int i, err;
1680
1681 err = _mv88e6xxx_fid_new(ds, &vlan.fid);
1682 if (err)
1683 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001684
Vivien Didelot3d131f02015-11-03 10:52:52 -05001685 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001686 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001687 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1688 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1689 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001690
1691 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1692 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1693 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001694
1695 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1696 * implemented, only one STU entry is needed to cover all VTU
1697 * entries. Thus, validate the SID 0.
1698 */
1699 vlan.sid = 0;
1700 err = _mv88e6xxx_stu_getnext(ds, GLOBAL_VTU_SID_MASK, &vstp);
1701 if (err)
1702 return err;
1703
1704 if (vstp.sid != vlan.sid || !vstp.valid) {
1705 memset(&vstp, 0, sizeof(vstp));
1706 vstp.valid = true;
1707 vstp.sid = vlan.sid;
1708
1709 err = _mv88e6xxx_stu_loadpurge(ds, &vstp);
1710 if (err)
1711 return err;
1712 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001713 }
1714
1715 *entry = vlan;
1716 return 0;
1717}
1718
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001719static int _mv88e6xxx_vtu_get(struct dsa_switch *ds, u16 vid,
1720 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1721{
1722 int err;
1723
1724 if (!vid)
1725 return -EINVAL;
1726
1727 err = _mv88e6xxx_vtu_vid_write(ds, vid - 1);
1728 if (err)
1729 return err;
1730
1731 err = _mv88e6xxx_vtu_getnext(ds, entry);
1732 if (err)
1733 return err;
1734
1735 if (entry->vid != vid || !entry->valid) {
1736 if (!creat)
1737 return -EOPNOTSUPP;
1738 /* -ENOENT would've been more appropriate, but switchdev expects
1739 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1740 */
1741
1742 err = _mv88e6xxx_vtu_new(ds, vid, entry);
1743 }
1744
1745 return err;
1746}
1747
Vivien Didelotda9c3592016-02-12 12:09:40 -05001748static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1749 u16 vid_begin, u16 vid_end)
1750{
1751 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1752 struct mv88e6xxx_vtu_stu_entry vlan;
1753 int i, err;
1754
1755 if (!vid_begin)
1756 return -EOPNOTSUPP;
1757
1758 mutex_lock(&ps->smi_mutex);
1759
1760 err = _mv88e6xxx_vtu_vid_write(ds, vid_begin - 1);
1761 if (err)
1762 goto unlock;
1763
1764 do {
1765 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
1766 if (err)
1767 goto unlock;
1768
1769 if (!vlan.valid)
1770 break;
1771
1772 if (vlan.vid > vid_end)
1773 break;
1774
Vivien Didelot009a2b92016-04-17 13:24:01 -04001775 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001776 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1777 continue;
1778
1779 if (vlan.data[i] ==
1780 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1781 continue;
1782
1783 if (ps->ports[i].bridge_dev ==
1784 ps->ports[port].bridge_dev)
1785 break; /* same bridge, check next VLAN */
1786
1787 netdev_warn(ds->ports[port],
1788 "hardware VLAN %d already used by %s\n",
1789 vlan.vid,
1790 netdev_name(ps->ports[i].bridge_dev));
1791 err = -EOPNOTSUPP;
1792 goto unlock;
1793 }
1794 } while (vlan.vid < vid_end);
1795
1796unlock:
1797 mutex_unlock(&ps->smi_mutex);
1798
1799 return err;
1800}
1801
Vivien Didelot214cdb92016-02-26 13:16:08 -05001802static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1803 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1804 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1805 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1806 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1807};
1808
1809int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1810 bool vlan_filtering)
1811{
1812 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1813 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1814 PORT_CONTROL_2_8021Q_DISABLED;
1815 int ret;
1816
1817 mutex_lock(&ps->smi_mutex);
1818
1819 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL_2);
1820 if (ret < 0)
1821 goto unlock;
1822
1823 old = ret & PORT_CONTROL_2_8021Q_MASK;
1824
Vivien Didelot5220ef12016-03-07 18:24:52 -05001825 if (new != old) {
1826 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1827 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001828
Vivien Didelot5220ef12016-03-07 18:24:52 -05001829 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_2,
1830 ret);
1831 if (ret < 0)
1832 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001833
Vivien Didelot5220ef12016-03-07 18:24:52 -05001834 netdev_dbg(ds->ports[port], "802.1Q Mode %s (was %s)\n",
1835 mv88e6xxx_port_8021q_mode_names[new],
1836 mv88e6xxx_port_8021q_mode_names[old]);
1837 }
1838
1839 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001840unlock:
1841 mutex_unlock(&ps->smi_mutex);
1842
1843 return ret;
1844}
1845
Vivien Didelot76e398a2015-11-01 12:33:55 -05001846int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1847 const struct switchdev_obj_port_vlan *vlan,
1848 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001849{
Vivien Didelotda9c3592016-02-12 12:09:40 -05001850 int err;
1851
Vivien Didelotda9c3592016-02-12 12:09:40 -05001852 /* If the requested port doesn't belong to the same bridge as the VLAN
1853 * members, do not support it (yet) and fallback to software VLAN.
1854 */
1855 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1856 vlan->vid_end);
1857 if (err)
1858 return err;
1859
Vivien Didelot76e398a2015-11-01 12:33:55 -05001860 /* We don't need any dynamic resource from the kernel (yet),
1861 * so skip the prepare phase.
1862 */
1863 return 0;
1864}
1865
1866static int _mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, u16 vid,
1867 bool untagged)
1868{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001869 struct mv88e6xxx_vtu_stu_entry vlan;
1870 int err;
1871
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001872 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001873 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001874 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001875
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001876 vlan.data[port] = untagged ?
1877 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1878 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1879
Vivien Didelot76e398a2015-11-01 12:33:55 -05001880 return _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1881}
1882
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001883void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1884 const struct switchdev_obj_port_vlan *vlan,
1885 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001886{
1887 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1888 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1889 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1890 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001891
1892 mutex_lock(&ps->smi_mutex);
1893
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001894 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
1895 if (_mv88e6xxx_port_vlan_add(ds, port, vid, untagged))
1896 netdev_err(ds->ports[port], "failed to add VLAN %d%c\n",
1897 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001898
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001899 if (pvid && _mv88e6xxx_port_pvid_set(ds, port, vlan->vid_end))
1900 netdev_err(ds->ports[port], "failed to set PVID %d\n",
1901 vlan->vid_end);
1902
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001903 mutex_unlock(&ps->smi_mutex);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001904}
1905
Vivien Didelot76e398a2015-11-01 12:33:55 -05001906static int _mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001907{
1908 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1909 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001910 int i, err;
1911
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001912 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001913 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001914 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001915
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001916 /* Tell switchdev if this VLAN is handled in software */
1917 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001918 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001919
1920 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1921
1922 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001923 vlan.valid = false;
Vivien Didelot009a2b92016-04-17 13:24:01 -04001924 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001925 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001926 continue;
1927
1928 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001929 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001930 break;
1931 }
1932 }
1933
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001934 err = _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1935 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001936 return err;
1937
1938 return _mv88e6xxx_atu_remove(ds, vlan.fid, port, false);
1939}
1940
1941int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1942 const struct switchdev_obj_port_vlan *vlan)
1943{
1944 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1945 u16 pvid, vid;
1946 int err = 0;
1947
1948 mutex_lock(&ps->smi_mutex);
1949
1950 err = _mv88e6xxx_port_pvid_get(ds, port, &pvid);
1951 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001952 goto unlock;
1953
Vivien Didelot76e398a2015-11-01 12:33:55 -05001954 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1955 err = _mv88e6xxx_port_vlan_del(ds, port, vid);
1956 if (err)
1957 goto unlock;
1958
1959 if (vid == pvid) {
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05001960 err = _mv88e6xxx_port_pvid_set(ds, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001961 if (err)
1962 goto unlock;
1963 }
1964 }
1965
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001966unlock:
1967 mutex_unlock(&ps->smi_mutex);
1968
1969 return err;
1970}
1971
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001972static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds,
1973 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001974{
1975 int i, ret;
1976
1977 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001978 ret = _mv88e6xxx_reg_write(
1979 ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
1980 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001981 if (ret < 0)
1982 return ret;
1983 }
1984
1985 return 0;
1986}
1987
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001988static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001989{
1990 int i, ret;
1991
1992 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001993 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1994 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001995 if (ret < 0)
1996 return ret;
1997 addr[i * 2] = ret >> 8;
1998 addr[i * 2 + 1] = ret & 0xff;
1999 }
2000
2001 return 0;
2002}
2003
Vivien Didelotfd231c82015-08-10 09:09:50 -04002004static int _mv88e6xxx_atu_load(struct dsa_switch *ds,
2005 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002006{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002007 int ret;
2008
2009 ret = _mv88e6xxx_atu_wait(ds);
2010 if (ret < 0)
2011 return ret;
2012
Vivien Didelotfd231c82015-08-10 09:09:50 -04002013 ret = _mv88e6xxx_atu_mac_write(ds, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002014 if (ret < 0)
2015 return ret;
2016
Vivien Didelot37705b72015-09-04 14:34:11 -04002017 ret = _mv88e6xxx_atu_data_write(ds, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002018 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002019 return ret;
2020
Vivien Didelotb426e5f2016-03-31 16:53:42 -04002021 return _mv88e6xxx_atu_cmd(ds, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002022}
David S. Millercdf09692015-08-11 12:00:37 -07002023
Vivien Didelotfd231c82015-08-10 09:09:50 -04002024static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port,
2025 const unsigned char *addr, u16 vid,
2026 u8 state)
2027{
2028 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002029 struct mv88e6xxx_vtu_stu_entry vlan;
2030 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002031
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002032 /* Null VLAN ID corresponds to the port private database */
2033 if (vid == 0)
2034 err = _mv88e6xxx_port_fid_get(ds, port, &vlan.fid);
2035 else
2036 err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002037 if (err)
2038 return err;
2039
2040 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002041 entry.state = state;
2042 ether_addr_copy(entry.mac, addr);
2043 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2044 entry.trunk = false;
2045 entry.portv_trunkid = BIT(port);
2046 }
2047
2048 return _mv88e6xxx_atu_load(ds, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002049}
2050
Vivien Didelot146a3202015-10-08 11:35:12 -04002051int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2052 const struct switchdev_obj_port_fdb *fdb,
2053 struct switchdev_trans *trans)
2054{
2055 /* We don't need any dynamic resource from the kernel (yet),
2056 * so skip the prepare phase.
2057 */
2058 return 0;
2059}
2060
Vivien Didelot8497aa62016-04-06 11:55:04 -04002061void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2062 const struct switchdev_obj_port_fdb *fdb,
2063 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002064{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002065 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002066 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2067 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2068 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002069
David S. Millercdf09692015-08-11 12:00:37 -07002070 mutex_lock(&ps->smi_mutex);
Vivien Didelot8497aa62016-04-06 11:55:04 -04002071 if (_mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, state))
2072 netdev_err(ds->ports[port], "failed to load MAC address\n");
David S. Millercdf09692015-08-11 12:00:37 -07002073 mutex_unlock(&ps->smi_mutex);
David S. Millercdf09692015-08-11 12:00:37 -07002074}
2075
2076int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
Vivien Didelot8057b3e2015-10-08 11:35:14 -04002077 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002078{
2079 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2080 int ret;
2081
2082 mutex_lock(&ps->smi_mutex);
Vivien Didelot8057b3e2015-10-08 11:35:14 -04002083 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002084 GLOBAL_ATU_DATA_STATE_UNUSED);
2085 mutex_unlock(&ps->smi_mutex);
2086
2087 return ret;
2088}
2089
Vivien Didelot1d194042015-08-10 09:09:51 -04002090static int _mv88e6xxx_atu_getnext(struct dsa_switch *ds, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002091 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002092{
Vivien Didelot1d194042015-08-10 09:09:51 -04002093 struct mv88e6xxx_atu_entry next = { 0 };
2094 int ret;
2095
2096 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002097
2098 ret = _mv88e6xxx_atu_wait(ds);
2099 if (ret < 0)
2100 return ret;
2101
Vivien Didelotb426e5f2016-03-31 16:53:42 -04002102 ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002103 if (ret < 0)
2104 return ret;
2105
Vivien Didelot1d194042015-08-10 09:09:51 -04002106 ret = _mv88e6xxx_atu_mac_read(ds, next.mac);
2107 if (ret < 0)
2108 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002109
Vivien Didelot1d194042015-08-10 09:09:51 -04002110 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
2111 if (ret < 0)
2112 return ret;
2113
2114 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2115 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2116 unsigned int mask, shift;
2117
2118 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2119 next.trunk = true;
2120 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2121 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2122 } else {
2123 next.trunk = false;
2124 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2125 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2126 }
2127
2128 next.portv_trunkid = (ret & mask) >> shift;
2129 }
2130
2131 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002132 return 0;
2133}
2134
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002135static int _mv88e6xxx_port_fdb_dump_one(struct dsa_switch *ds, u16 fid, u16 vid,
2136 int port,
2137 struct switchdev_obj_port_fdb *fdb,
2138 int (*cb)(struct switchdev_obj *obj))
2139{
2140 struct mv88e6xxx_atu_entry addr = {
2141 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2142 };
2143 int err;
2144
2145 err = _mv88e6xxx_atu_mac_write(ds, addr.mac);
2146 if (err)
2147 return err;
2148
2149 do {
2150 err = _mv88e6xxx_atu_getnext(ds, fid, &addr);
2151 if (err)
2152 break;
2153
2154 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2155 break;
2156
2157 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2158 bool is_static = addr.state ==
2159 (is_multicast_ether_addr(addr.mac) ?
2160 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2161 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2162
2163 fdb->vid = vid;
2164 ether_addr_copy(fdb->addr, addr.mac);
2165 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2166
2167 err = cb(&fdb->obj);
2168 if (err)
2169 break;
2170 }
2171 } while (!is_broadcast_ether_addr(addr.mac));
2172
2173 return err;
2174}
2175
Vivien Didelotf33475b2015-10-22 09:34:41 -04002176int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2177 struct switchdev_obj_port_fdb *fdb,
2178 int (*cb)(struct switchdev_obj *obj))
2179{
2180 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2181 struct mv88e6xxx_vtu_stu_entry vlan = {
2182 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2183 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002184 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002185 int err;
2186
2187 mutex_lock(&ps->smi_mutex);
2188
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002189 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2190 err = _mv88e6xxx_port_fid_get(ds, port, &fid);
2191 if (err)
2192 goto unlock;
2193
2194 err = _mv88e6xxx_port_fdb_dump_one(ds, fid, 0, port, fdb, cb);
2195 if (err)
2196 goto unlock;
2197
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002198 /* Dump VLANs' Filtering Information Databases */
Vivien Didelotf33475b2015-10-22 09:34:41 -04002199 err = _mv88e6xxx_vtu_vid_write(ds, vlan.vid);
2200 if (err)
2201 goto unlock;
2202
2203 do {
Vivien Didelotf33475b2015-10-22 09:34:41 -04002204 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
2205 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002206 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002207
2208 if (!vlan.valid)
2209 break;
2210
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002211 err = _mv88e6xxx_port_fdb_dump_one(ds, vlan.fid, vlan.vid, port,
2212 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002213 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002214 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002215 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2216
2217unlock:
2218 mutex_unlock(&ps->smi_mutex);
2219
2220 return err;
2221}
2222
Vivien Didelota6692752016-02-12 12:09:39 -05002223int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2224 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002225{
Vivien Didelota6692752016-02-12 12:09:39 -05002226 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002227 u16 fid;
2228 int i, err;
2229
2230 mutex_lock(&ps->smi_mutex);
2231
2232 /* Get or create the bridge FID and assign it to the port */
Vivien Didelot009a2b92016-04-17 13:24:01 -04002233 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot466dfa02016-02-26 13:16:05 -05002234 if (ps->ports[i].bridge_dev == bridge)
2235 break;
2236
Vivien Didelot009a2b92016-04-17 13:24:01 -04002237 if (i < ps->info->num_ports)
Vivien Didelot466dfa02016-02-26 13:16:05 -05002238 err = _mv88e6xxx_port_fid_get(ds, i, &fid);
2239 else
2240 err = _mv88e6xxx_fid_new(ds, &fid);
2241 if (err)
2242 goto unlock;
2243
2244 err = _mv88e6xxx_port_fid_set(ds, port, fid);
2245 if (err)
2246 goto unlock;
Vivien Didelota6692752016-02-12 12:09:39 -05002247
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002248 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002249 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002250
Vivien Didelot009a2b92016-04-17 13:24:01 -04002251 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002252 if (ps->ports[i].bridge_dev == bridge) {
2253 err = _mv88e6xxx_port_based_vlan_map(ds, i);
2254 if (err)
2255 break;
2256 }
2257 }
2258
Vivien Didelot466dfa02016-02-26 13:16:05 -05002259unlock:
2260 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002261
Vivien Didelot466dfa02016-02-26 13:16:05 -05002262 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002263}
2264
Vivien Didelot16bfa702016-03-13 16:21:33 -04002265void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002266{
Vivien Didelota6692752016-02-12 12:09:39 -05002267 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002268 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002269 u16 fid;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002270 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002271
2272 mutex_lock(&ps->smi_mutex);
2273
2274 /* Give the port a fresh Filtering Information Database */
Vivien Didelot16bfa702016-03-13 16:21:33 -04002275 if (_mv88e6xxx_fid_new(ds, &fid) ||
2276 _mv88e6xxx_port_fid_set(ds, port, fid))
2277 netdev_warn(ds->ports[port], "failed to assign a new FID\n");
Vivien Didelota6692752016-02-12 12:09:39 -05002278
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002279 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002280 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002281
Vivien Didelot009a2b92016-04-17 13:24:01 -04002282 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot16bfa702016-03-13 16:21:33 -04002283 if (i == port || ps->ports[i].bridge_dev == bridge)
2284 if (_mv88e6xxx_port_based_vlan_map(ds, i))
2285 netdev_warn(ds->ports[i], "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002286
Vivien Didelot466dfa02016-02-26 13:16:05 -05002287 mutex_unlock(&ps->smi_mutex);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002288}
2289
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002290static void mv88e6xxx_bridge_work(struct work_struct *work)
2291{
2292 struct mv88e6xxx_priv_state *ps;
2293 struct dsa_switch *ds;
2294 int port;
2295
2296 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
Andrew Lunn7543a6d2016-04-13 02:40:40 +02002297 ds = ps->ds;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002298
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002299 mutex_lock(&ps->smi_mutex);
2300
Vivien Didelot009a2b92016-04-17 13:24:01 -04002301 for (port = 0; port < ps->info->num_ports; ++port)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05002302 if (test_and_clear_bit(port, ps->port_state_update_mask) &&
2303 _mv88e6xxx_port_state(ds, port, ps->ports[port].state))
2304 netdev_warn(ds->ports[port], "failed to update state to %s\n",
2305 mv88e6xxx_port_state_names[ps->ports[port].state]);
2306
2307 mutex_unlock(&ps->smi_mutex);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002308}
2309
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002310static int _mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2311 int reg, int val)
2312{
2313 int ret;
2314
2315 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
2316 if (ret < 0)
2317 goto restore_page_0;
2318
2319 ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
2320restore_page_0:
2321 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
2322
2323 return ret;
2324}
2325
2326static int _mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page,
2327 int reg)
2328{
2329 int ret;
2330
2331 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
2332 if (ret < 0)
2333 goto restore_page_0;
2334
2335 ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
2336restore_page_0:
2337 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
2338
2339 return ret;
2340}
2341
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002342static int mv88e6xxx_power_on_serdes(struct dsa_switch *ds)
2343{
2344 int ret;
2345
2346 ret = _mv88e6xxx_phy_page_read(ds, REG_FIBER_SERDES, PAGE_FIBER_SERDES,
2347 MII_BMCR);
2348 if (ret < 0)
2349 return ret;
2350
2351 if (ret & BMCR_PDOWN) {
2352 ret &= ~BMCR_PDOWN;
2353 ret = _mv88e6xxx_phy_page_write(ds, REG_FIBER_SERDES,
2354 PAGE_FIBER_SERDES, MII_BMCR,
2355 ret);
2356 }
2357
2358 return ret;
2359}
2360
Andrew Lunndbde9e62015-05-06 01:09:48 +02002361static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002362{
2363 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002364 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002365 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002366
2367 mutex_lock(&ps->smi_mutex);
2368
Andrew Lunn54d792f2015-05-06 01:09:47 +02002369 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2370 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2371 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002372 mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002373 /* MAC Forcing register: don't force link, speed,
2374 * duplex or flow control state to any particular
2375 * values on physical ports, but force the CPU port
2376 * and all DSA ports to their maximum bandwidth and
2377 * full duplex.
2378 */
2379 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002380 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002381 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002382 reg |= PORT_PCS_CTRL_FORCE_LINK |
2383 PORT_PCS_CTRL_LINK_UP |
2384 PORT_PCS_CTRL_DUPLEX_FULL |
2385 PORT_PCS_CTRL_FORCE_DUPLEX;
2386 if (mv88e6xxx_6065_family(ds))
2387 reg |= PORT_PCS_CTRL_100;
2388 else
2389 reg |= PORT_PCS_CTRL_1000;
2390 } else {
2391 reg |= PORT_PCS_CTRL_UNFORCED;
2392 }
2393
2394 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2395 PORT_PCS_CTRL, reg);
2396 if (ret)
2397 goto abort;
2398 }
2399
2400 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2401 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2402 * tunneling, determine priority by looking at 802.1p and IP
2403 * priority fields (IP prio has precedence), and set STP state
2404 * to Forwarding.
2405 *
2406 * If this is the CPU link, use DSA or EDSA tagging depending
2407 * on which tagging mode was configured.
2408 *
2409 * If this is a link to another switch, use DSA tagging mode.
2410 *
2411 * If this is the upstream port for this switch, enable
2412 * forwarding of unknown unicasts and multicasts.
2413 */
2414 reg = 0;
2415 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2416 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2417 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002418 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002419 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2420 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2421 PORT_CONTROL_STATE_FORWARDING;
2422 if (dsa_is_cpu_port(ds, port)) {
2423 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2424 reg |= PORT_CONTROL_DSA_TAG;
2425 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002426 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2427 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002428 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2429 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2430 else
2431 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002432 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2433 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002434 }
2435
2436 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2437 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2438 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002439 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002440 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2441 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2442 }
2443 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002444 if (dsa_is_dsa_port(ds, port)) {
2445 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
2446 reg |= PORT_CONTROL_DSA_TAG;
2447 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2448 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2449 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002450 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002451 }
2452
Andrew Lunn54d792f2015-05-06 01:09:47 +02002453 if (port == dsa_upstream_port(ds))
2454 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2455 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2456 }
2457 if (reg) {
2458 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2459 PORT_CONTROL, reg);
2460 if (ret)
2461 goto abort;
2462 }
2463
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002464 /* If this port is connected to a SerDes, make sure the SerDes is not
2465 * powered down.
2466 */
2467 if (mv88e6xxx_6352_family(ds)) {
2468 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
2469 if (ret < 0)
2470 goto abort;
2471 ret &= PORT_STATUS_CMODE_MASK;
2472 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2473 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2474 (ret == PORT_STATUS_CMODE_SGMII)) {
2475 ret = mv88e6xxx_power_on_serdes(ds);
2476 if (ret < 0)
2477 goto abort;
2478 }
2479 }
2480
Vivien Didelot8efdda42015-08-13 12:52:23 -04002481 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002482 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002483 * untagged frames on this port, do a destination address lookup on all
2484 * received packets as usual, disable ARP mirroring and don't send a
2485 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002486 */
2487 reg = 0;
2488 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2489 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Vivien Didelotf93dd042016-03-31 16:53:45 -04002490 mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds) ||
2491 mv88e6xxx_6185_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002492 reg = PORT_CONTROL_2_MAP_DA;
2493
2494 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002495 mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002496 reg |= PORT_CONTROL_2_JUMBO_10240;
2497
2498 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) {
2499 /* Set the upstream port this port should use */
2500 reg |= dsa_upstream_port(ds);
2501 /* enable forwarding of unknown multicast addresses to
2502 * the upstream port
2503 */
2504 if (port == dsa_upstream_port(ds))
2505 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2506 }
2507
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002508 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002509
Andrew Lunn54d792f2015-05-06 01:09:47 +02002510 if (reg) {
2511 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2512 PORT_CONTROL_2, reg);
2513 if (ret)
2514 goto abort;
2515 }
2516
2517 /* Port Association Vector: when learning source addresses
2518 * of packets, add the address to the address database using
2519 * a port bitmap that has only the bit for this port set and
2520 * the other bits clear.
2521 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002522 reg = 1 << port;
2523 /* Disable learning for DSA and CPU ports */
2524 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
2525 reg = PORT_ASSOC_VECTOR_LOCKED_PORT;
2526
2527 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002528 if (ret)
2529 goto abort;
2530
2531 /* Egress rate control 2: disable egress rate control. */
2532 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2,
2533 0x0000);
2534 if (ret)
2535 goto abort;
2536
2537 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002538 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2539 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002540 /* Do not limit the period of time that this port can
2541 * be paused for by the remote end or the period of
2542 * time that this port can pause the remote end.
2543 */
2544 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2545 PORT_PAUSE_CTRL, 0x0000);
2546 if (ret)
2547 goto abort;
2548
2549 /* Port ATU control: disable limiting the number of
2550 * address database entries that this port is allowed
2551 * to use.
2552 */
2553 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2554 PORT_ATU_CONTROL, 0x0000);
2555 /* Priority Override: disable DA, SA and VTU priority
2556 * override.
2557 */
2558 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2559 PORT_PRI_OVERRIDE, 0x0000);
2560 if (ret)
2561 goto abort;
2562
2563 /* Port Ethertype: use the Ethertype DSA Ethertype
2564 * value.
2565 */
2566 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2567 PORT_ETH_TYPE, ETH_P_EDSA);
2568 if (ret)
2569 goto abort;
2570 /* Tag Remap: use an identity 802.1p prio -> switch
2571 * prio mapping.
2572 */
2573 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2574 PORT_TAG_REGMAP_0123, 0x3210);
2575 if (ret)
2576 goto abort;
2577
2578 /* Tag Remap 2: use an identity 802.1p prio -> switch
2579 * prio mapping.
2580 */
2581 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2582 PORT_TAG_REGMAP_4567, 0x7654);
2583 if (ret)
2584 goto abort;
2585 }
2586
2587 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2588 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002589 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2590 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002591 /* Rate Control: disable ingress rate limiting. */
2592 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2593 PORT_RATE_CONTROL, 0x0001);
2594 if (ret)
2595 goto abort;
2596 }
2597
Guenter Roeck366f0a02015-03-26 18:36:30 -07002598 /* Port Control 1: disable trunking, disable sending
2599 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002600 */
Vivien Didelot614f03f2015-04-20 17:19:23 -04002601 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002602 if (ret)
2603 goto abort;
2604
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002605 /* Port based VLAN map: give each port its own address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002606 * database, and allow bidirectional communication between the
2607 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002608 */
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002609 ret = _mv88e6xxx_port_fid_set(ds, port, port + 1);
2610 if (ret)
2611 goto abort;
2612
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002613 ret = _mv88e6xxx_port_based_vlan_map(ds, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002614 if (ret)
2615 goto abort;
2616
2617 /* Default VLAN ID and priority: don't set a default VLAN
2618 * ID, and set the default packet priority to zero.
2619 */
Vivien Didelot47cf1e62015-04-20 17:43:26 -04002620 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
2621 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002622abort:
2623 mutex_unlock(&ps->smi_mutex);
2624 return ret;
2625}
2626
Andrew Lunndbde9e62015-05-06 01:09:48 +02002627int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2628{
2629 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2630 int ret;
2631 int i;
2632
Vivien Didelot009a2b92016-04-17 13:24:01 -04002633 for (i = 0; i < ps->info->num_ports; i++) {
Andrew Lunndbde9e62015-05-06 01:09:48 +02002634 ret = mv88e6xxx_setup_port(ds, i);
2635 if (ret < 0)
2636 return ret;
2637 }
2638 return 0;
2639}
2640
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002641int mv88e6xxx_setup_common(struct dsa_switch *ds)
2642{
2643 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2644
Andrew Lunn7543a6d2016-04-13 02:40:40 +02002645 ps->ds = ds;
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002646 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002647
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002648 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
2649
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002650 return 0;
2651}
2652
Andrew Lunn54d792f2015-05-06 01:09:47 +02002653int mv88e6xxx_setup_global(struct dsa_switch *ds)
2654{
2655 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002656 int err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002657 int i;
2658
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002659 mutex_lock(&ps->smi_mutex);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002660 /* Set the default address aging time to 5 minutes, and
2661 * enable address learn messages to be sent to all message
2662 * ports.
2663 */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002664 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2665 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2666 if (err)
2667 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002668
2669 /* Configure the IP ToS mapping registers. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002670 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2671 if (err)
2672 goto unlock;
2673 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2674 if (err)
2675 goto unlock;
2676 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2677 if (err)
2678 goto unlock;
2679 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2680 if (err)
2681 goto unlock;
2682 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2683 if (err)
2684 goto unlock;
2685 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2686 if (err)
2687 goto unlock;
2688 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2689 if (err)
2690 goto unlock;
2691 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2692 if (err)
2693 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002694
2695 /* Configure the IEEE 802.1p priority mapping register. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002696 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2697 if (err)
2698 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002699
2700 /* Send all frames with destination addresses matching
2701 * 01:80:c2:00:00:0x to the CPU port.
2702 */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002703 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
2704 if (err)
2705 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002706
2707 /* Ignore removed tag data on doubly tagged packets, disable
2708 * flow control messages, force flow control priority to the
2709 * highest, and send all special multicast frames to the CPU
2710 * port at the highest priority.
2711 */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002712 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
2713 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
2714 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
2715 if (err)
2716 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002717
2718 /* Program the DSA routing table. */
2719 for (i = 0; i < 32; i++) {
2720 int nexthop = 0x1f;
2721
2722 if (ds->pd->rtable &&
2723 i != ds->index && i < ds->dst->pd->nr_chips)
2724 nexthop = ds->pd->rtable[i] & 0x1f;
2725
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002726 err = _mv88e6xxx_reg_write(
2727 ds, REG_GLOBAL2,
2728 GLOBAL2_DEVICE_MAPPING,
2729 GLOBAL2_DEVICE_MAPPING_UPDATE |
2730 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
2731 if (err)
2732 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002733 }
2734
2735 /* Clear all trunk masks. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002736 for (i = 0; i < 8; i++) {
2737 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
2738 0x8000 |
2739 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
Vivien Didelot009a2b92016-04-17 13:24:01 -04002740 ((1 << ps->info->num_ports) - 1));
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002741 if (err)
2742 goto unlock;
2743 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002744
2745 /* Clear all trunk mappings. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002746 for (i = 0; i < 16; i++) {
2747 err = _mv88e6xxx_reg_write(
2748 ds, REG_GLOBAL2,
2749 GLOBAL2_TRUNK_MAPPING,
2750 GLOBAL2_TRUNK_MAPPING_UPDATE |
2751 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
2752 if (err)
2753 goto unlock;
2754 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002755
2756 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002757 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2758 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002759 /* Send all frames with destination addresses matching
2760 * 01:80:c2:00:00:2x to the CPU port.
2761 */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002762 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2,
2763 GLOBAL2_MGMT_EN_2X, 0xffff);
2764 if (err)
2765 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002766
2767 /* Initialise cross-chip port VLAN table to reset
2768 * defaults.
2769 */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002770 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2,
2771 GLOBAL2_PVT_ADDR, 0x9000);
2772 if (err)
2773 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002774
2775 /* Clear the priority override table. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002776 for (i = 0; i < 16; i++) {
2777 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2,
2778 GLOBAL2_PRIO_OVERRIDE,
2779 0x8000 | (i << 8));
2780 if (err)
2781 goto unlock;
2782 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002783 }
2784
2785 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2786 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002787 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2788 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002789 /* Disable ingress rate limiting by resetting all
2790 * ingress rate limit registers to their initial
2791 * state.
2792 */
Vivien Didelot009a2b92016-04-17 13:24:01 -04002793 for (i = 0; i < ps->info->num_ports; i++) {
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002794 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2,
2795 GLOBAL2_INGRESS_OP,
2796 0x9000 | (i << 8));
2797 if (err)
2798 goto unlock;
2799 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02002800 }
2801
Andrew Lunndb687a52015-06-20 21:31:29 +02002802 /* Clear the statistics counters for all ports */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002803 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
2804 GLOBAL_STATS_OP_FLUSH_ALL);
2805 if (err)
2806 goto unlock;
Andrew Lunndb687a52015-06-20 21:31:29 +02002807
2808 /* Wait for the flush to complete. */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002809 err = _mv88e6xxx_stats_wait(ds);
2810 if (err < 0)
Vivien Didelot6b17e862015-08-13 12:52:18 -04002811 goto unlock;
2812
Vivien Didelotc161d0a2015-09-04 14:34:13 -04002813 /* Clear all ATU entries */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002814 err = _mv88e6xxx_atu_flush(ds, 0, true);
2815 if (err < 0)
Vivien Didelotc161d0a2015-09-04 14:34:13 -04002816 goto unlock;
2817
Vivien Didelot6b17e862015-08-13 12:52:18 -04002818 /* Clear all the VTU and STU entries */
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002819 err = _mv88e6xxx_vtu_stu_flush(ds);
Vivien Didelot6b17e862015-08-13 12:52:18 -04002820unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04002821 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02002822
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002823 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002824}
2825
Andrew Lunn143a8302015-04-02 04:06:34 +02002826int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
2827{
2828 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2829 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunnc8c1b392015-11-20 03:56:24 +01002830 struct gpio_desc *gpiod = ds->pd->reset;
Andrew Lunn143a8302015-04-02 04:06:34 +02002831 unsigned long timeout;
2832 int ret;
2833 int i;
2834
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002835 mutex_lock(&ps->smi_mutex);
2836
Andrew Lunn143a8302015-04-02 04:06:34 +02002837 /* Set all ports to the disabled state. */
Vivien Didelot009a2b92016-04-17 13:24:01 -04002838 for (i = 0; i < ps->info->num_ports; i++) {
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002839 ret = _mv88e6xxx_reg_read(ds, REG_PORT(i), PORT_CONTROL);
2840 if (ret < 0)
2841 goto unlock;
2842
2843 ret = _mv88e6xxx_reg_write(ds, REG_PORT(i), PORT_CONTROL,
2844 ret & 0xfffc);
2845 if (ret)
2846 goto unlock;
Andrew Lunn143a8302015-04-02 04:06:34 +02002847 }
2848
2849 /* Wait for transmit queues to drain. */
2850 usleep_range(2000, 4000);
2851
Andrew Lunnc8c1b392015-11-20 03:56:24 +01002852 /* If there is a gpio connected to the reset pin, toggle it */
2853 if (gpiod) {
2854 gpiod_set_value_cansleep(gpiod, 1);
2855 usleep_range(10000, 20000);
2856 gpiod_set_value_cansleep(gpiod, 0);
2857 usleep_range(10000, 20000);
2858 }
2859
Andrew Lunn143a8302015-04-02 04:06:34 +02002860 /* Reset the switch. Keep the PPU active if requested. The PPU
2861 * needs to be active to support indirect phy register access
2862 * through global registers 0x18 and 0x19.
2863 */
2864 if (ppu_active)
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002865 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x04, 0xc000);
Andrew Lunn143a8302015-04-02 04:06:34 +02002866 else
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002867 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x04, 0xc400);
2868 if (ret)
2869 goto unlock;
Andrew Lunn143a8302015-04-02 04:06:34 +02002870
2871 /* Wait up to one second for reset to complete. */
2872 timeout = jiffies + 1 * HZ;
2873 while (time_before(jiffies, timeout)) {
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002874 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x00);
2875 if (ret < 0)
2876 goto unlock;
2877
Andrew Lunn143a8302015-04-02 04:06:34 +02002878 if ((ret & is_reset) == is_reset)
2879 break;
2880 usleep_range(1000, 2000);
2881 }
2882 if (time_after(jiffies, timeout))
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002883 ret = -ETIMEDOUT;
2884 else
2885 ret = 0;
2886unlock:
2887 mutex_unlock(&ps->smi_mutex);
Andrew Lunn143a8302015-04-02 04:06:34 +02002888
Andrew Lunn48ace4e2016-04-14 23:47:12 +02002889 return ret;
Andrew Lunn143a8302015-04-02 04:06:34 +02002890}
2891
Andrew Lunn491435852015-04-02 04:06:35 +02002892int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
2893{
2894 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2895 int ret;
2896
Andrew Lunn3898c142015-05-06 01:09:53 +02002897 mutex_lock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002898 ret = _mv88e6xxx_phy_page_read(ds, port, page, reg);
Andrew Lunn3898c142015-05-06 01:09:53 +02002899 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002900
Andrew Lunn491435852015-04-02 04:06:35 +02002901 return ret;
2902}
2903
2904int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2905 int reg, int val)
2906{
2907 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2908 int ret;
2909
Andrew Lunn3898c142015-05-06 01:09:53 +02002910 mutex_lock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002911 ret = _mv88e6xxx_phy_page_write(ds, port, page, reg, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002912 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002913
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002914 return ret;
2915}
2916
2917static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
2918{
2919 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2920
Vivien Didelot009a2b92016-04-17 13:24:01 -04002921 if (port >= 0 && port < ps->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002922 return port;
2923 return -EINVAL;
2924}
2925
2926int
2927mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
2928{
2929 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2930 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2931 int ret;
2932
2933 if (addr < 0)
2934 return addr;
2935
Andrew Lunn3898c142015-05-06 01:09:53 +02002936 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002937 ret = _mv88e6xxx_phy_read(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002938 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002939 return ret;
2940}
2941
2942int
2943mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2944{
2945 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2946 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2947 int ret;
2948
2949 if (addr < 0)
2950 return addr;
2951
Andrew Lunn3898c142015-05-06 01:09:53 +02002952 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002953 ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002954 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002955 return ret;
2956}
2957
2958int
2959mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
2960{
2961 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2962 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2963 int ret;
2964
2965 if (addr < 0)
2966 return addr;
2967
Andrew Lunn3898c142015-05-06 01:09:53 +02002968 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002969 ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002970 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002971 return ret;
2972}
2973
2974int
2975mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
2976 u16 val)
2977{
2978 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2979 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2980 int ret;
2981
2982 if (addr < 0)
2983 return addr;
2984
Andrew Lunn3898c142015-05-06 01:09:53 +02002985 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002986 ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002987 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002988 return ret;
2989}
2990
Guenter Roeckc22995c2015-07-25 09:42:28 -07002991#ifdef CONFIG_NET_DSA_HWMON
2992
2993static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2994{
2995 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2996 int ret;
2997 int val;
2998
2999 *temp = 0;
3000
3001 mutex_lock(&ps->smi_mutex);
3002
3003 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
3004 if (ret < 0)
3005 goto error;
3006
3007 /* Enable temperature sensor */
3008 ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
3009 if (ret < 0)
3010 goto error;
3011
3012 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
3013 if (ret < 0)
3014 goto error;
3015
3016 /* Wait for temperature to stabilize */
3017 usleep_range(10000, 12000);
3018
3019 val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
3020 if (val < 0) {
3021 ret = val;
3022 goto error;
3023 }
3024
3025 /* Disable temperature sensor */
3026 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
3027 if (ret < 0)
3028 goto error;
3029
3030 *temp = ((val & 0x1f) - 5) * 5;
3031
3032error:
3033 _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
3034 mutex_unlock(&ps->smi_mutex);
3035 return ret;
3036}
3037
3038static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3039{
3040 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
3041 int ret;
3042
3043 *temp = 0;
3044
3045 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
3046 if (ret < 0)
3047 return ret;
3048
3049 *temp = (ret & 0xff) - 25;
3050
3051 return 0;
3052}
3053
3054int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3055{
3056 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
3057 return mv88e63xx_get_temp(ds, temp);
3058
3059 return mv88e61xx_get_temp(ds, temp);
3060}
3061
3062int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3063{
3064 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
3065 int ret;
3066
3067 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
3068 return -EOPNOTSUPP;
3069
3070 *temp = 0;
3071
3072 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3073 if (ret < 0)
3074 return ret;
3075
3076 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3077
3078 return 0;
3079}
3080
3081int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3082{
3083 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
3084 int ret;
3085
3086 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
3087 return -EOPNOTSUPP;
3088
3089 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3090 if (ret < 0)
3091 return ret;
3092 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3093 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
3094 (ret & 0xe0ff) | (temp << 8));
3095}
3096
3097int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3098{
3099 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
3100 int ret;
3101
3102 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
3103 return -EOPNOTSUPP;
3104
3105 *alarm = false;
3106
3107 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3108 if (ret < 0)
3109 return ret;
3110
3111 *alarm = !!(ret & 0x40);
3112
3113 return 0;
3114}
3115#endif /* CONFIG_NET_DSA_HWMON */
3116
Vivien Didelotf6271e62016-04-17 13:23:59 -04003117static const struct mv88e6xxx_info *
3118mv88e6xxx_lookup_info(unsigned int prod_num, const struct mv88e6xxx_info *table,
Vivien Didelot0209d142016-04-17 13:23:55 -04003119 unsigned int num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003120{
Vivien Didelota439c062016-04-17 13:23:58 -04003121 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003122
Vivien Didelotb9b37712015-10-30 19:39:48 -04003123 for (i = 0; i < num; ++i)
Vivien Didelotf6271e62016-04-17 13:23:59 -04003124 if (table[i].prod_num == prod_num)
3125 return &table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003126
Vivien Didelotb9b37712015-10-30 19:39:48 -04003127 return NULL;
3128}
3129
Vivien Didelot0209d142016-04-17 13:23:55 -04003130const char *mv88e6xxx_drv_probe(struct device *dsa_dev, struct device *host_dev,
3131 int sw_addr, void **priv,
Vivien Didelotf6271e62016-04-17 13:23:59 -04003132 const struct mv88e6xxx_info *table,
Vivien Didelot0209d142016-04-17 13:23:55 -04003133 unsigned int num)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003134{
Vivien Didelotf6271e62016-04-17 13:23:59 -04003135 const struct mv88e6xxx_info *info;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003136 struct mv88e6xxx_priv_state *ps;
Vivien Didelota439c062016-04-17 13:23:58 -04003137 struct mii_bus *bus;
Vivien Didelot0209d142016-04-17 13:23:55 -04003138 const char *name;
Vivien Didelota439c062016-04-17 13:23:58 -04003139 int id, prod_num, rev;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003140
Vivien Didelota439c062016-04-17 13:23:58 -04003141 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003142 if (!bus)
3143 return NULL;
3144
Vivien Didelota439c062016-04-17 13:23:58 -04003145 id = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
3146 if (id < 0)
3147 return NULL;
3148
3149 prod_num = (id & 0xfff0) >> 4;
3150 rev = id & 0x000f;
3151
Vivien Didelotf6271e62016-04-17 13:23:59 -04003152 info = mv88e6xxx_lookup_info(prod_num, table, num);
3153 if (!info)
Vivien Didelota439c062016-04-17 13:23:58 -04003154 return NULL;
3155
Vivien Didelotf6271e62016-04-17 13:23:59 -04003156 name = info->name;
3157
Vivien Didelota439c062016-04-17 13:23:58 -04003158 ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
3159 if (!ps)
3160 return NULL;
3161
3162 ps->bus = bus;
3163 ps->sw_addr = sw_addr;
Vivien Didelotf6271e62016-04-17 13:23:59 -04003164 ps->info = info;
Vivien Didelota439c062016-04-17 13:23:58 -04003165 ps->id = id & 0xfff0;
3166
3167 *priv = ps;
3168
3169 dev_info(&ps->bus->dev, "switch 0x%x probed: %s, revision %u\n",
3170 prod_num, name, rev);
3171
Andrew Lunna77d43f2016-04-13 02:40:42 +02003172 return name;
3173}
3174
Ben Hutchings98e67302011-11-25 14:36:19 +00003175static int __init mv88e6xxx_init(void)
3176{
3177#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3178 register_switch_driver(&mv88e6131_switch_driver);
3179#endif
Andrew Lunnca3dfa52016-03-12 00:01:36 +01003180#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3181 register_switch_driver(&mv88e6123_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003182#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07003183#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3184 register_switch_driver(&mv88e6352_switch_driver);
3185#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02003186#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3187 register_switch_driver(&mv88e6171_switch_driver);
3188#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00003189 return 0;
3190}
3191module_init(mv88e6xxx_init);
3192
3193static void __exit mv88e6xxx_cleanup(void)
3194{
Andrew Lunn42f27252014-09-12 23:58:44 +02003195#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3196 unregister_switch_driver(&mv88e6171_switch_driver);
3197#endif
Vivien Didelot4212b542015-05-01 10:43:52 -04003198#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3199 unregister_switch_driver(&mv88e6352_switch_driver);
3200#endif
Andrew Lunnca3dfa52016-03-12 00:01:36 +01003201#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3202 unregister_switch_driver(&mv88e6123_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003203#endif
3204#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3205 unregister_switch_driver(&mv88e6131_switch_driver);
3206#endif
3207}
3208module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003209
3210MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3211MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3212MODULE_LICENSE("GPL");