blob: ec28465ee323a58bbb08e04e08ba764057fd4343 [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 *
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02008 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
9 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 */
15
Barry Grussling19b2f972013-01-08 16:05:54 +000016#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070017#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020018#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070019#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000020#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000021#include <linux/list.h>
Andrew Lunn14c7b3c2016-05-10 23:27:21 +020022#include <linux/mdio.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000023#include <linux/module.h>
Andrew Lunnb516d452016-06-04 21:17:06 +020024#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000025#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010026#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000028#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040029#include <net/switchdev.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000030#include "mv88e6xxx.h"
31
Andrew Lunn158bc062016-04-28 21:24:06 -040032static void assert_smi_lock(struct mv88e6xxx_priv_state *ps)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040033{
Vivien Didelot3996a4f2015-10-30 18:56:45 -040034 if (unlikely(!mutex_is_locked(&ps->smi_mutex))) {
Andrew Lunn158bc062016-04-28 21:24:06 -040035 dev_err(ps->dev, "SMI lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040036 dump_stack();
37 }
38}
39
Barry Grussling3675c8d2013-01-08 16:05:53 +000040/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000041 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
42 * will be directly accessible on some {device address,register address}
43 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
44 * will only respond to SMI transactions to that specific address, and
45 * an indirect addressing mechanism needs to be used to access its
46 * registers.
47 */
48static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
49{
50 int ret;
51 int i;
52
53 for (i = 0; i < 16; i++) {
Neil Armstrong6e899e62015-10-22 10:37:53 +020054 ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000055 if (ret < 0)
56 return ret;
57
Andrew Lunncca8b132015-04-02 04:06:39 +020058 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000059 return 0;
60 }
61
62 return -ETIMEDOUT;
63}
64
Vivien Didelotb9b37712015-10-30 19:39:48 -040065static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr,
66 int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000067{
68 int ret;
69
70 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +020071 return mdiobus_read_nested(bus, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072
Barry Grussling3675c8d2013-01-08 16:05:53 +000073 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000074 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
75 if (ret < 0)
76 return ret;
77
Barry Grussling3675c8d2013-01-08 16:05:53 +000078 /* Transmit the read command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020079 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
80 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000081 if (ret < 0)
82 return ret;
83
Barry Grussling3675c8d2013-01-08 16:05:53 +000084 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000085 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
86 if (ret < 0)
87 return ret;
88
Barry Grussling3675c8d2013-01-08 16:05:53 +000089 /* Read the data. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020090 ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000091 if (ret < 0)
92 return ret;
93
94 return ret & 0xffff;
95}
96
Andrew Lunn158bc062016-04-28 21:24:06 -040097static int _mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps,
98 int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000099{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000100 int ret;
101
Andrew Lunn158bc062016-04-28 21:24:06 -0400102 assert_smi_lock(ps);
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400103
Andrew Lunna77d43f2016-04-13 02:40:42 +0200104 ret = __mv88e6xxx_reg_read(ps->bus, ps->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500105 if (ret < 0)
106 return ret;
107
Andrew Lunn158bc062016-04-28 21:24:06 -0400108 dev_dbg(ps->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500109 addr, reg, ret);
110
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000111 return ret;
112}
113
Vivien Didelot57d32312016-06-20 13:13:58 -0400114static int mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, int addr,
115 int reg)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700116{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700117 int ret;
118
119 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -0400120 ret = _mv88e6xxx_reg_read(ps, addr, reg);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700121 mutex_unlock(&ps->smi_mutex);
122
123 return ret;
124}
125
Vivien Didelotb9b37712015-10-30 19:39:48 -0400126static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
127 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000128{
129 int ret;
130
131 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +0200132 return mdiobus_write_nested(bus, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000133
Barry Grussling3675c8d2013-01-08 16:05:53 +0000134 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000135 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
136 if (ret < 0)
137 return ret;
138
Barry Grussling3675c8d2013-01-08 16:05:53 +0000139 /* Transmit the data to write. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200140 ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000141 if (ret < 0)
142 return ret;
143
Barry Grussling3675c8d2013-01-08 16:05:53 +0000144 /* Transmit the write command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200145 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
146 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000147 if (ret < 0)
148 return ret;
149
Barry Grussling3675c8d2013-01-08 16:05:53 +0000150 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000151 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
152 if (ret < 0)
153 return ret;
154
155 return 0;
156}
157
Andrew Lunn158bc062016-04-28 21:24:06 -0400158static int _mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
159 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000160{
Andrew Lunn158bc062016-04-28 21:24:06 -0400161 assert_smi_lock(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000162
Andrew Lunn158bc062016-04-28 21:24:06 -0400163 dev_dbg(ps->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500164 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
Vivien Didelot57d32312016-06-20 13:13:58 -0400169static int mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
170 int reg, u16 val)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700171{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700172 int ret;
173
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000174 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -0400175 ret = _mv88e6xxx_reg_write(ps, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000176 mutex_unlock(&ps->smi_mutex);
177
178 return ret;
179}
180
Vivien Didelot1d13a062016-05-09 13:22:43 -0400181static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000182{
Andrew Lunn158bc062016-04-28 21:24:06 -0400183 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200184 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000185
Andrew Lunn158bc062016-04-28 21:24:06 -0400186 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_01,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200187 (addr[0] << 8) | addr[1]);
188 if (err)
189 return err;
190
Andrew Lunn158bc062016-04-28 21:24:06 -0400191 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_23,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200192 (addr[2] << 8) | addr[3]);
193 if (err)
194 return err;
195
Andrew Lunn158bc062016-04-28 21:24:06 -0400196 return mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_45,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200197 (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000198}
199
Vivien Didelot1d13a062016-05-09 13:22:43 -0400200static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000201{
Andrew Lunn158bc062016-04-28 21:24:06 -0400202 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000203 int ret;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200204 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000205
206 for (i = 0; i < 6; i++) {
207 int j;
208
Barry Grussling3675c8d2013-01-08 16:05:53 +0000209 /* Write the MAC address byte. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400210 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200211 GLOBAL2_SWITCH_MAC_BUSY |
212 (i << 8) | addr[i]);
213 if (ret)
214 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000215
Barry Grussling3675c8d2013-01-08 16:05:53 +0000216 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000217 for (j = 0; j < 16; j++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400218 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200219 GLOBAL2_SWITCH_MAC);
220 if (ret < 0)
221 return ret;
222
Andrew Lunncca8b132015-04-02 04:06:39 +0200223 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000224 break;
225 }
226 if (j == 16)
227 return -ETIMEDOUT;
228 }
229
230 return 0;
231}
232
Vivien Didelot57d32312016-06-20 13:13:58 -0400233static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
Vivien Didelot1d13a062016-05-09 13:22:43 -0400234{
235 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
236
237 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SWITCH_MAC))
238 return mv88e6xxx_set_addr_indirect(ds, addr);
239 else
240 return mv88e6xxx_set_addr_direct(ds, addr);
241}
242
Andrew Lunn03a4a542016-06-04 21:17:05 +0200243static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_priv_state *ps,
244 int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000245{
246 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400247 return _mv88e6xxx_reg_read(ps, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000248 return 0xffff;
249}
250
Andrew Lunn03a4a542016-06-04 21:17:05 +0200251static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_priv_state *ps,
252 int addr, int regnum, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000253{
254 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400255 return _mv88e6xxx_reg_write(ps, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000256 return 0;
257}
258
Andrew Lunn158bc062016-04-28 21:24:06 -0400259static int mv88e6xxx_ppu_disable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000260{
261 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000262 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000263
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400264 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200265 if (ret < 0)
266 return ret;
267
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400268 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
269 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200270 if (ret)
271 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000272
Barry Grussling19b2f972013-01-08 16:05:54 +0000273 timeout = jiffies + 1 * HZ;
274 while (time_before(jiffies, timeout)) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400275 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200276 if (ret < 0)
277 return ret;
278
Barry Grussling19b2f972013-01-08 16:05:54 +0000279 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200280 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
281 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000282 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000283 }
284
285 return -ETIMEDOUT;
286}
287
Andrew Lunn158bc062016-04-28 21:24:06 -0400288static int mv88e6xxx_ppu_enable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000289{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200290 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000291 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000292
Vivien Didelot762eb672016-06-04 21:16:54 +0200293 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200294 if (ret < 0)
295 return ret;
296
Vivien Didelot762eb672016-06-04 21:16:54 +0200297 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
298 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200299 if (err)
300 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000301
Barry Grussling19b2f972013-01-08 16:05:54 +0000302 timeout = jiffies + 1 * HZ;
303 while (time_before(jiffies, timeout)) {
Vivien Didelot762eb672016-06-04 21:16:54 +0200304 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200305 if (ret < 0)
306 return ret;
307
Barry Grussling19b2f972013-01-08 16:05:54 +0000308 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200309 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
310 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000311 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000312 }
313
314 return -ETIMEDOUT;
315}
316
317static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
318{
319 struct mv88e6xxx_priv_state *ps;
320
321 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200322
323 mutex_lock(&ps->smi_mutex);
324
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000325 if (mutex_trylock(&ps->ppu_mutex)) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400326 if (mv88e6xxx_ppu_enable(ps) == 0)
Barry Grussling85686582013-01-08 16:05:56 +0000327 ps->ppu_disabled = 0;
328 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000329 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200330
331 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000332}
333
334static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
335{
336 struct mv88e6xxx_priv_state *ps = (void *)_ps;
337
338 schedule_work(&ps->ppu_work);
339}
340
Andrew Lunn158bc062016-04-28 21:24:06 -0400341static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000342{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000343 int ret;
344
345 mutex_lock(&ps->ppu_mutex);
346
Barry Grussling3675c8d2013-01-08 16:05:53 +0000347 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000348 * we can access the PHY registers. If it was already
349 * disabled, cancel the timer that is going to re-enable
350 * it.
351 */
352 if (!ps->ppu_disabled) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400353 ret = mv88e6xxx_ppu_disable(ps);
Barry Grussling85686582013-01-08 16:05:56 +0000354 if (ret < 0) {
355 mutex_unlock(&ps->ppu_mutex);
356 return ret;
357 }
358 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000359 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000360 del_timer(&ps->ppu_timer);
361 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000362 }
363
364 return ret;
365}
366
Andrew Lunn158bc062016-04-28 21:24:06 -0400367static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000368{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000369 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000370 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
371 mutex_unlock(&ps->ppu_mutex);
372}
373
Vivien Didelot57d32312016-06-20 13:13:58 -0400374static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000375{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000376 mutex_init(&ps->ppu_mutex);
377 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
378 init_timer(&ps->ppu_timer);
379 ps->ppu_timer.data = (unsigned long)ps;
380 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
381}
382
Andrew Lunn03a4a542016-06-04 21:17:05 +0200383static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_priv_state *ps, int addr,
384 int regnum)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000385{
386 int ret;
387
Andrew Lunn158bc062016-04-28 21:24:06 -0400388 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000389 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400390 ret = _mv88e6xxx_reg_read(ps, addr, regnum);
Andrew Lunn158bc062016-04-28 21:24:06 -0400391 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000392 }
393
394 return ret;
395}
396
Andrew Lunn03a4a542016-06-04 21:17:05 +0200397static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_priv_state *ps, int addr,
398 int regnum, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000399{
400 int ret;
401
Andrew Lunn158bc062016-04-28 21:24:06 -0400402 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000403 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400404 ret = _mv88e6xxx_reg_write(ps, addr, regnum, val);
Andrew Lunn158bc062016-04-28 21:24:06 -0400405 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000406 }
407
408 return ret;
409}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000410
Andrew Lunn158bc062016-04-28 21:24:06 -0400411static bool mv88e6xxx_6065_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200412{
Vivien Didelot22356472016-04-17 13:24:00 -0400413 return ps->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200414}
415
Andrew Lunn158bc062016-04-28 21:24:06 -0400416static bool mv88e6xxx_6095_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200417{
Vivien Didelot22356472016-04-17 13:24:00 -0400418 return ps->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200419}
420
Andrew Lunn158bc062016-04-28 21:24:06 -0400421static bool mv88e6xxx_6097_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200422{
Vivien Didelot22356472016-04-17 13:24:00 -0400423 return ps->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200424}
425
Andrew Lunn158bc062016-04-28 21:24:06 -0400426static bool mv88e6xxx_6165_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200427{
Vivien Didelot22356472016-04-17 13:24:00 -0400428 return ps->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200429}
430
Andrew Lunn158bc062016-04-28 21:24:06 -0400431static bool mv88e6xxx_6185_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200432{
Vivien Didelot22356472016-04-17 13:24:00 -0400433 return ps->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200434}
435
Andrew Lunn158bc062016-04-28 21:24:06 -0400436static bool mv88e6xxx_6320_family(struct mv88e6xxx_priv_state *ps)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700437{
Vivien Didelot22356472016-04-17 13:24:00 -0400438 return ps->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700439}
440
Andrew Lunn158bc062016-04-28 21:24:06 -0400441static bool mv88e6xxx_6351_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200442{
Vivien Didelot22356472016-04-17 13:24:00 -0400443 return ps->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200444}
445
Andrew Lunn158bc062016-04-28 21:24:06 -0400446static bool mv88e6xxx_6352_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200447{
Vivien Didelot22356472016-04-17 13:24:00 -0400448 return ps->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200449}
450
Andrew Lunn158bc062016-04-28 21:24:06 -0400451static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_priv_state *ps)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400452{
Vivien Didelotcd5a2c82016-04-17 13:24:02 -0400453 return ps->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400454}
455
Andrew Lunn158bc062016-04-28 21:24:06 -0400456static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_priv_state *ps)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400457{
458 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Andrew Lunn158bc062016-04-28 21:24:06 -0400459 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
460 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400461 return true;
462
463 return false;
464}
465
Andrew Lunndea87022015-08-31 15:56:47 +0200466/* We expect the switch to perform auto negotiation if there is a real
467 * phy. However, in the case of a fixed link phy, we force the port
468 * settings from the fixed link settings.
469 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400470static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
471 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200472{
473 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200474 u32 reg;
475 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200476
477 if (!phy_is_pseudo_fixed_link(phydev))
478 return;
479
480 mutex_lock(&ps->smi_mutex);
481
Andrew Lunn158bc062016-04-28 21:24:06 -0400482 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200483 if (ret < 0)
484 goto out;
485
486 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
487 PORT_PCS_CTRL_FORCE_LINK |
488 PORT_PCS_CTRL_DUPLEX_FULL |
489 PORT_PCS_CTRL_FORCE_DUPLEX |
490 PORT_PCS_CTRL_UNFORCED);
491
492 reg |= PORT_PCS_CTRL_FORCE_LINK;
493 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400494 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200495
Andrew Lunn158bc062016-04-28 21:24:06 -0400496 if (mv88e6xxx_6065_family(ps) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200497 goto out;
498
499 switch (phydev->speed) {
500 case SPEED_1000:
501 reg |= PORT_PCS_CTRL_1000;
502 break;
503 case SPEED_100:
504 reg |= PORT_PCS_CTRL_100;
505 break;
506 case SPEED_10:
507 reg |= PORT_PCS_CTRL_10;
508 break;
509 default:
510 pr_info("Unknown speed");
511 goto out;
512 }
513
514 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
515 if (phydev->duplex == DUPLEX_FULL)
516 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
517
Andrew Lunn158bc062016-04-28 21:24:06 -0400518 if ((mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps)) &&
Vivien Didelot009a2b92016-04-17 13:24:01 -0400519 (port >= ps->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200520 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
521 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
522 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
523 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
524 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
525 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
526 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
527 }
Andrew Lunn158bc062016-04-28 21:24:06 -0400528 _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200529
530out:
531 mutex_unlock(&ps->smi_mutex);
532}
533
Andrew Lunn158bc062016-04-28 21:24:06 -0400534static int _mv88e6xxx_stats_wait(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000535{
536 int ret;
537 int i;
538
539 for (i = 0; i < 10; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400540 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200541 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000542 return 0;
543 }
544
545 return -ETIMEDOUT;
546}
547
Andrew Lunn158bc062016-04-28 21:24:06 -0400548static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_priv_state *ps,
549 int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000550{
551 int ret;
552
Andrew Lunn158bc062016-04-28 21:24:06 -0400553 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200554 port = (port + 1) << 5;
555
Barry Grussling3675c8d2013-01-08 16:05:53 +0000556 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400557 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200558 GLOBAL_STATS_OP_CAPTURE_PORT |
559 GLOBAL_STATS_OP_HIST_RX_TX | port);
560 if (ret < 0)
561 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000562
Barry Grussling3675c8d2013-01-08 16:05:53 +0000563 /* Wait for the snapshotting to complete. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400564 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000565 if (ret < 0)
566 return ret;
567
568 return 0;
569}
570
Andrew Lunn158bc062016-04-28 21:24:06 -0400571static void _mv88e6xxx_stats_read(struct mv88e6xxx_priv_state *ps,
572 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000573{
574 u32 _val;
575 int ret;
576
577 *val = 0;
578
Andrew Lunn158bc062016-04-28 21:24:06 -0400579 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200580 GLOBAL_STATS_OP_READ_CAPTURED |
581 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000582 if (ret < 0)
583 return;
584
Andrew Lunn158bc062016-04-28 21:24:06 -0400585 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000586 if (ret < 0)
587 return;
588
Andrew Lunn158bc062016-04-28 21:24:06 -0400589 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000590 if (ret < 0)
591 return;
592
593 _val = ret << 16;
594
Andrew Lunn158bc062016-04-28 21:24:06 -0400595 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000596 if (ret < 0)
597 return;
598
599 *val = _val | ret;
600}
601
Andrew Lunne413e7e2015-04-02 04:06:38 +0200602static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100603 { "in_good_octets", 8, 0x00, BANK0, },
604 { "in_bad_octets", 4, 0x02, BANK0, },
605 { "in_unicast", 4, 0x04, BANK0, },
606 { "in_broadcasts", 4, 0x06, BANK0, },
607 { "in_multicasts", 4, 0x07, BANK0, },
608 { "in_pause", 4, 0x16, BANK0, },
609 { "in_undersize", 4, 0x18, BANK0, },
610 { "in_fragments", 4, 0x19, BANK0, },
611 { "in_oversize", 4, 0x1a, BANK0, },
612 { "in_jabber", 4, 0x1b, BANK0, },
613 { "in_rx_error", 4, 0x1c, BANK0, },
614 { "in_fcs_error", 4, 0x1d, BANK0, },
615 { "out_octets", 8, 0x0e, BANK0, },
616 { "out_unicast", 4, 0x10, BANK0, },
617 { "out_broadcasts", 4, 0x13, BANK0, },
618 { "out_multicasts", 4, 0x12, BANK0, },
619 { "out_pause", 4, 0x15, BANK0, },
620 { "excessive", 4, 0x11, BANK0, },
621 { "collisions", 4, 0x1e, BANK0, },
622 { "deferred", 4, 0x05, BANK0, },
623 { "single", 4, 0x14, BANK0, },
624 { "multiple", 4, 0x17, BANK0, },
625 { "out_fcs_error", 4, 0x03, BANK0, },
626 { "late", 4, 0x1f, BANK0, },
627 { "hist_64bytes", 4, 0x08, BANK0, },
628 { "hist_65_127bytes", 4, 0x09, BANK0, },
629 { "hist_128_255bytes", 4, 0x0a, BANK0, },
630 { "hist_256_511bytes", 4, 0x0b, BANK0, },
631 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
632 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
633 { "sw_in_discards", 4, 0x10, PORT, },
634 { "sw_in_filtered", 2, 0x12, PORT, },
635 { "sw_out_filtered", 2, 0x13, PORT, },
636 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
637 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
638 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
639 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
640 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
641 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
642 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
643 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
644 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
645 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
646 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
647 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
648 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
649 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
650 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
651 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
652 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
653 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
654 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
655 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
656 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
657 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
658 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
659 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
660 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
661 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200662};
663
Andrew Lunn158bc062016-04-28 21:24:06 -0400664static bool mv88e6xxx_has_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100665 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200666{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100667 switch (stat->type) {
668 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200669 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100670 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400671 return mv88e6xxx_6320_family(ps);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100672 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400673 return mv88e6xxx_6095_family(ps) ||
674 mv88e6xxx_6185_family(ps) ||
675 mv88e6xxx_6097_family(ps) ||
676 mv88e6xxx_6165_family(ps) ||
677 mv88e6xxx_6351_family(ps) ||
678 mv88e6xxx_6352_family(ps);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200679 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100680 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000681}
682
Andrew Lunn158bc062016-04-28 21:24:06 -0400683static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100684 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200685 int port)
686{
Andrew Lunn80c46272015-06-20 18:42:30 +0200687 u32 low;
688 u32 high = 0;
689 int ret;
690 u64 value;
691
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100692 switch (s->type) {
693 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400694 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200695 if (ret < 0)
696 return UINT64_MAX;
697
698 low = ret;
699 if (s->sizeof_stat == 4) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400700 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100701 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200702 if (ret < 0)
703 return UINT64_MAX;
704 high = ret;
705 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100706 break;
707 case BANK0:
708 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400709 _mv88e6xxx_stats_read(ps, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200710 if (s->sizeof_stat == 8)
Andrew Lunn158bc062016-04-28 21:24:06 -0400711 _mv88e6xxx_stats_read(ps, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200712 }
713 value = (((u64)high) << 16) | low;
714 return value;
715}
716
Vivien Didelotf81ec902016-05-09 13:22:58 -0400717static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
718 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100719{
Andrew Lunn158bc062016-04-28 21:24:06 -0400720 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100721 struct mv88e6xxx_hw_stat *stat;
722 int i, j;
723
724 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
725 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400726 if (mv88e6xxx_has_stat(ps, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100727 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
728 ETH_GSTRING_LEN);
729 j++;
730 }
731 }
732}
733
Vivien Didelotf81ec902016-05-09 13:22:58 -0400734static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100735{
Andrew Lunn158bc062016-04-28 21:24:06 -0400736 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100737 struct mv88e6xxx_hw_stat *stat;
738 int i, j;
739
740 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
741 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400742 if (mv88e6xxx_has_stat(ps, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100743 j++;
744 }
745 return j;
746}
747
Vivien Didelotf81ec902016-05-09 13:22:58 -0400748static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
749 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000750{
Florian Fainellia22adce2014-04-28 11:14:28 -0700751 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100752 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000753 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100754 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000755
Andrew Lunn31888232015-05-06 01:09:54 +0200756 mutex_lock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000757
Andrew Lunn158bc062016-04-28 21:24:06 -0400758 ret = _mv88e6xxx_stats_snapshot(ps, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000759 if (ret < 0) {
Andrew Lunn31888232015-05-06 01:09:54 +0200760 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000761 return;
762 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100763 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
764 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400765 if (mv88e6xxx_has_stat(ps, stat)) {
766 data[j] = _mv88e6xxx_get_ethtool_stat(ps, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100767 j++;
768 }
769 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000770
Andrew Lunn31888232015-05-06 01:09:54 +0200771 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000772}
Ben Hutchings98e67302011-11-25 14:36:19 +0000773
Vivien Didelotf81ec902016-05-09 13:22:58 -0400774static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700775{
776 return 32 * sizeof(u16);
777}
778
Vivien Didelotf81ec902016-05-09 13:22:58 -0400779static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
780 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700781{
Andrew Lunn158bc062016-04-28 21:24:06 -0400782 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700783 u16 *p = _p;
784 int i;
785
786 regs->version = 0;
787
788 memset(p, 0xff, 32 * sizeof(u16));
789
Vivien Didelot23062512016-05-09 13:22:45 -0400790 mutex_lock(&ps->smi_mutex);
791
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700792 for (i = 0; i < 32; i++) {
793 int ret;
794
Vivien Didelot23062512016-05-09 13:22:45 -0400795 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700796 if (ret >= 0)
797 p[i] = ret;
798 }
Vivien Didelot23062512016-05-09 13:22:45 -0400799
800 mutex_unlock(&ps->smi_mutex);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700801}
802
Andrew Lunn158bc062016-04-28 21:24:06 -0400803static int _mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, int offset,
Andrew Lunn3898c142015-05-06 01:09:53 +0200804 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700805{
806 unsigned long timeout = jiffies + HZ / 10;
807
808 while (time_before(jiffies, timeout)) {
809 int ret;
810
Andrew Lunn158bc062016-04-28 21:24:06 -0400811 ret = _mv88e6xxx_reg_read(ps, reg, offset);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700812 if (ret < 0)
813 return ret;
814 if (!(ret & mask))
815 return 0;
816
817 usleep_range(1000, 2000);
818 }
819 return -ETIMEDOUT;
820}
821
Andrew Lunn158bc062016-04-28 21:24:06 -0400822static int mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg,
823 int offset, u16 mask)
Andrew Lunn3898c142015-05-06 01:09:53 +0200824{
Andrew Lunn3898c142015-05-06 01:09:53 +0200825 int ret;
826
827 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -0400828 ret = _mv88e6xxx_wait(ps, reg, offset, mask);
Andrew Lunn3898c142015-05-06 01:09:53 +0200829 mutex_unlock(&ps->smi_mutex);
830
831 return ret;
832}
833
Andrew Lunn03a4a542016-06-04 21:17:05 +0200834static int mv88e6xxx_mdio_wait(struct mv88e6xxx_priv_state *ps)
Andrew Lunn3898c142015-05-06 01:09:53 +0200835{
Andrew Lunn158bc062016-04-28 21:24:06 -0400836 return _mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200837 GLOBAL2_SMI_OP_BUSY);
838}
839
Vivien Didelotd24645b2016-05-09 13:22:41 -0400840static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200841{
Andrew Lunn158bc062016-04-28 21:24:06 -0400842 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
843
844 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200845 GLOBAL2_EEPROM_OP_LOAD);
846}
847
Vivien Didelotd24645b2016-05-09 13:22:41 -0400848static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200849{
Andrew Lunn158bc062016-04-28 21:24:06 -0400850 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
851
852 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200853 GLOBAL2_EEPROM_OP_BUSY);
854}
855
Vivien Didelotd24645b2016-05-09 13:22:41 -0400856static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
857{
858 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
859 int ret;
860
861 mutex_lock(&ps->eeprom_mutex);
862
863 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
864 GLOBAL2_EEPROM_OP_READ |
865 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
866 if (ret < 0)
867 goto error;
868
869 ret = mv88e6xxx_eeprom_busy_wait(ds);
870 if (ret < 0)
871 goto error;
872
873 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
874error:
875 mutex_unlock(&ps->eeprom_mutex);
876 return ret;
877}
878
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200879static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
880{
881 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
882
883 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
884 return ps->eeprom_len;
885
886 return 0;
887}
888
Vivien Didelotf81ec902016-05-09 13:22:58 -0400889static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
890 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400891{
892 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
893 int offset;
894 int len;
895 int ret;
896
897 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
898 return -EOPNOTSUPP;
899
900 offset = eeprom->offset;
901 len = eeprom->len;
902 eeprom->len = 0;
903
904 eeprom->magic = 0xc3ec4951;
905
906 ret = mv88e6xxx_eeprom_load_wait(ds);
907 if (ret < 0)
908 return ret;
909
910 if (offset & 1) {
911 int word;
912
913 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
914 if (word < 0)
915 return word;
916
917 *data++ = (word >> 8) & 0xff;
918
919 offset++;
920 len--;
921 eeprom->len++;
922 }
923
924 while (len >= 2) {
925 int word;
926
927 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
928 if (word < 0)
929 return word;
930
931 *data++ = word & 0xff;
932 *data++ = (word >> 8) & 0xff;
933
934 offset += 2;
935 len -= 2;
936 eeprom->len += 2;
937 }
938
939 if (len) {
940 int word;
941
942 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
943 if (word < 0)
944 return word;
945
946 *data++ = word & 0xff;
947
948 offset++;
949 len--;
950 eeprom->len++;
951 }
952
953 return 0;
954}
955
956static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
957{
958 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
959 int ret;
960
961 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
962 if (ret < 0)
963 return ret;
964
965 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
966 return -EROFS;
967
968 return 0;
969}
970
971static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
972 u16 data)
973{
974 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
975 int ret;
976
977 mutex_lock(&ps->eeprom_mutex);
978
979 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
980 if (ret < 0)
981 goto error;
982
983 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
984 GLOBAL2_EEPROM_OP_WRITE |
985 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
986 if (ret < 0)
987 goto error;
988
989 ret = mv88e6xxx_eeprom_busy_wait(ds);
990error:
991 mutex_unlock(&ps->eeprom_mutex);
992 return ret;
993}
994
Vivien Didelotf81ec902016-05-09 13:22:58 -0400995static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
996 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400997{
998 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
999 int offset;
1000 int ret;
1001 int len;
1002
1003 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
1004 return -EOPNOTSUPP;
1005
1006 if (eeprom->magic != 0xc3ec4951)
1007 return -EINVAL;
1008
1009 ret = mv88e6xxx_eeprom_is_readonly(ds);
1010 if (ret)
1011 return ret;
1012
1013 offset = eeprom->offset;
1014 len = eeprom->len;
1015 eeprom->len = 0;
1016
1017 ret = mv88e6xxx_eeprom_load_wait(ds);
1018 if (ret < 0)
1019 return ret;
1020
1021 if (offset & 1) {
1022 int word;
1023
1024 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1025 if (word < 0)
1026 return word;
1027
1028 word = (*data++ << 8) | (word & 0xff);
1029
1030 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1031 if (ret < 0)
1032 return ret;
1033
1034 offset++;
1035 len--;
1036 eeprom->len++;
1037 }
1038
1039 while (len >= 2) {
1040 int word;
1041
1042 word = *data++;
1043 word |= *data++ << 8;
1044
1045 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1046 if (ret < 0)
1047 return ret;
1048
1049 offset += 2;
1050 len -= 2;
1051 eeprom->len += 2;
1052 }
1053
1054 if (len) {
1055 int word;
1056
1057 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1058 if (word < 0)
1059 return word;
1060
1061 word = (word & 0xff00) | *data++;
1062
1063 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1064 if (ret < 0)
1065 return ret;
1066
1067 offset++;
1068 len--;
1069 eeprom->len++;
1070 }
1071
1072 return 0;
1073}
1074
Andrew Lunn158bc062016-04-28 21:24:06 -04001075static int _mv88e6xxx_atu_wait(struct mv88e6xxx_priv_state *ps)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001076{
Andrew Lunn158bc062016-04-28 21:24:06 -04001077 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_ATU_OP,
Andrew Lunncca8b132015-04-02 04:06:39 +02001078 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001079}
1080
Andrew Lunn03a4a542016-06-04 21:17:05 +02001081static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_priv_state *ps,
Andrew Lunn158bc062016-04-28 21:24:06 -04001082 int addr, int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +01001083{
1084 int ret;
1085
Andrew Lunn158bc062016-04-28 21:24:06 -04001086 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001087 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1088 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +01001089 if (ret < 0)
1090 return ret;
1091
Andrew Lunn03a4a542016-06-04 21:17:05 +02001092 ret = mv88e6xxx_mdio_wait(ps);
Andrew Lunn3898c142015-05-06 01:09:53 +02001093 if (ret < 0)
1094 return ret;
1095
Andrew Lunn158bc062016-04-28 21:24:06 -04001096 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA);
1097
1098 return ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001099}
1100
Andrew Lunn03a4a542016-06-04 21:17:05 +02001101static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_priv_state *ps,
Andrew Lunn158bc062016-04-28 21:24:06 -04001102 int addr, int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +01001103{
Andrew Lunn3898c142015-05-06 01:09:53 +02001104 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001105
Andrew Lunn158bc062016-04-28 21:24:06 -04001106 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02001107 if (ret < 0)
1108 return ret;
1109
Andrew Lunn158bc062016-04-28 21:24:06 -04001110 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001111 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1112 regnum);
1113
Andrew Lunn03a4a542016-06-04 21:17:05 +02001114 return mv88e6xxx_mdio_wait(ps);
Andrew Lunnf3044682015-02-14 19:17:50 +01001115}
1116
Vivien Didelotf81ec902016-05-09 13:22:58 -04001117static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1118 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001119{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001120 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001121 int reg;
1122
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001123 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1124 return -EOPNOTSUPP;
1125
Andrew Lunn3898c142015-05-06 01:09:53 +02001126 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001127
Andrew Lunn03a4a542016-06-04 21:17:05 +02001128 reg = mv88e6xxx_mdio_read_indirect(ps, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001129 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001130 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001131
1132 e->eee_enabled = !!(reg & 0x0200);
1133 e->tx_lpi_enabled = !!(reg & 0x0100);
1134
Andrew Lunn158bc062016-04-28 21:24:06 -04001135 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001136 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001137 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001138
Andrew Lunncca8b132015-04-02 04:06:39 +02001139 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001140 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001141
Andrew Lunn2f40c692015-04-02 04:06:37 +02001142out:
Andrew Lunn3898c142015-05-06 01:09:53 +02001143 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001144 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001145}
1146
Vivien Didelotf81ec902016-05-09 13:22:58 -04001147static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1148 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001149{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001150 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1151 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001152 int ret;
1153
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001154 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1155 return -EOPNOTSUPP;
1156
Andrew Lunn3898c142015-05-06 01:09:53 +02001157 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001158
Andrew Lunn03a4a542016-06-04 21:17:05 +02001159 ret = mv88e6xxx_mdio_read_indirect(ps, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001160 if (ret < 0)
1161 goto out;
1162
1163 reg = ret & ~0x0300;
1164 if (e->eee_enabled)
1165 reg |= 0x0200;
1166 if (e->tx_lpi_enabled)
1167 reg |= 0x0100;
1168
Andrew Lunn03a4a542016-06-04 21:17:05 +02001169 ret = mv88e6xxx_mdio_write_indirect(ps, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001170out:
Andrew Lunn3898c142015-05-06 01:09:53 +02001171 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001172
1173 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001174}
1175
Andrew Lunn158bc062016-04-28 21:24:06 -04001176static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_priv_state *ps, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001177{
1178 int ret;
1179
Andrew Lunn158bc062016-04-28 21:24:06 -04001180 if (mv88e6xxx_has_fid_reg(ps)) {
1181 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_FID, fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001182 if (ret < 0)
1183 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001184 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001185 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001186 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -04001187 if (ret < 0)
1188 return ret;
1189
Andrew Lunn158bc062016-04-28 21:24:06 -04001190 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001191 (ret & 0xfff) |
1192 ((fid << 8) & 0xf000));
1193 if (ret < 0)
1194 return ret;
1195
1196 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1197 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001198 }
1199
Andrew Lunn158bc062016-04-28 21:24:06 -04001200 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001201 if (ret < 0)
1202 return ret;
1203
Andrew Lunn158bc062016-04-28 21:24:06 -04001204 return _mv88e6xxx_atu_wait(ps);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001205}
1206
Andrew Lunn158bc062016-04-28 21:24:06 -04001207static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot37705b72015-09-04 14:34:11 -04001208 struct mv88e6xxx_atu_entry *entry)
1209{
1210 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1211
1212 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1213 unsigned int mask, shift;
1214
1215 if (entry->trunk) {
1216 data |= GLOBAL_ATU_DATA_TRUNK;
1217 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1218 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1219 } else {
1220 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1221 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1222 }
1223
1224 data |= (entry->portv_trunkid << shift) & mask;
1225 }
1226
Andrew Lunn158bc062016-04-28 21:24:06 -04001227 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001228}
1229
Andrew Lunn158bc062016-04-28 21:24:06 -04001230static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001231 struct mv88e6xxx_atu_entry *entry,
1232 bool static_too)
1233{
1234 int op;
1235 int err;
1236
Andrew Lunn158bc062016-04-28 21:24:06 -04001237 err = _mv88e6xxx_atu_wait(ps);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001238 if (err)
1239 return err;
1240
Andrew Lunn158bc062016-04-28 21:24:06 -04001241 err = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001242 if (err)
1243 return err;
1244
1245 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001246 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1247 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1248 } else {
1249 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1250 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1251 }
1252
Andrew Lunn158bc062016-04-28 21:24:06 -04001253 return _mv88e6xxx_atu_cmd(ps, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001254}
1255
Andrew Lunn158bc062016-04-28 21:24:06 -04001256static int _mv88e6xxx_atu_flush(struct mv88e6xxx_priv_state *ps,
1257 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001258{
1259 struct mv88e6xxx_atu_entry entry = {
1260 .fid = fid,
1261 .state = 0, /* EntryState bits must be 0 */
1262 };
1263
Andrew Lunn158bc062016-04-28 21:24:06 -04001264 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001265}
1266
Andrew Lunn158bc062016-04-28 21:24:06 -04001267static int _mv88e6xxx_atu_move(struct mv88e6xxx_priv_state *ps, u16 fid,
1268 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001269{
1270 struct mv88e6xxx_atu_entry entry = {
1271 .trunk = false,
1272 .fid = fid,
1273 };
1274
1275 /* EntryState bits must be 0xF */
1276 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1277
1278 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1279 entry.portv_trunkid = (to_port & 0x0f) << 4;
1280 entry.portv_trunkid |= from_port & 0x0f;
1281
Andrew Lunn158bc062016-04-28 21:24:06 -04001282 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001283}
1284
Andrew Lunn158bc062016-04-28 21:24:06 -04001285static int _mv88e6xxx_atu_remove(struct mv88e6xxx_priv_state *ps, u16 fid,
1286 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001287{
1288 /* Destination port 0xF means remove the entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001289 return _mv88e6xxx_atu_move(ps, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001290}
1291
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001292static const char * const mv88e6xxx_port_state_names[] = {
1293 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1294 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1295 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1296 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1297};
1298
Andrew Lunn158bc062016-04-28 21:24:06 -04001299static int _mv88e6xxx_port_state(struct mv88e6xxx_priv_state *ps, int port,
1300 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001301{
Andrew Lunn158bc062016-04-28 21:24:06 -04001302 struct dsa_switch *ds = ps->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001303 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001304 u8 oldstate;
1305
Andrew Lunn158bc062016-04-28 21:24:06 -04001306 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001307 if (reg < 0)
1308 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001309
Andrew Lunncca8b132015-04-02 04:06:39 +02001310 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001311
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001312 if (oldstate != state) {
1313 /* Flush forwarding database if we're moving a port
1314 * from Learning or Forwarding state to Disabled or
1315 * Blocking or Listening state.
1316 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001317 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001318 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1319 (state == PORT_CONTROL_STATE_DISABLED ||
1320 state == PORT_CONTROL_STATE_BLOCKING)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001321 ret = _mv88e6xxx_atu_remove(ps, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001322 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001323 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001324 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001325
Andrew Lunncca8b132015-04-02 04:06:39 +02001326 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Andrew Lunn158bc062016-04-28 21:24:06 -04001327 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001328 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001329 if (ret)
1330 return ret;
1331
Andrew Lunnc8b09802016-06-04 21:16:57 +02001332 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001333 mv88e6xxx_port_state_names[state],
1334 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001335 }
1336
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001337 return ret;
1338}
1339
Andrew Lunn158bc062016-04-28 21:24:06 -04001340static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_priv_state *ps,
1341 int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001342{
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001343 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot009a2b92016-04-17 13:24:01 -04001344 const u16 mask = (1 << ps->info->num_ports) - 1;
Andrew Lunn158bc062016-04-28 21:24:06 -04001345 struct dsa_switch *ds = ps->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001346 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001347 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001348 int i;
1349
1350 /* allow CPU port or DSA link(s) to send frames to every port */
1351 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1352 output_ports = mask;
1353 } else {
Vivien Didelot009a2b92016-04-17 13:24:01 -04001354 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001355 /* allow sending frames to every group member */
1356 if (bridge && ps->ports[i].bridge_dev == bridge)
1357 output_ports |= BIT(i);
1358
1359 /* allow sending frames to CPU port and DSA link(s) */
1360 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1361 output_ports |= BIT(i);
1362 }
1363 }
1364
1365 /* prevent frames from going back out of the port they came in on */
1366 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001367
Andrew Lunn158bc062016-04-28 21:24:06 -04001368 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001369 if (reg < 0)
1370 return reg;
1371
1372 reg &= ~mask;
1373 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001374
Andrew Lunn158bc062016-04-28 21:24:06 -04001375 return _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001376}
1377
Vivien Didelotf81ec902016-05-09 13:22:58 -04001378static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1379 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001380{
1381 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1382 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001383 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001384
Vivien Didelot936f2342016-05-09 13:22:46 -04001385 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_PORTSTATE))
1386 return;
1387
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001388 switch (state) {
1389 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001390 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001391 break;
1392 case BR_STATE_BLOCKING:
1393 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001394 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001395 break;
1396 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001397 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001398 break;
1399 case BR_STATE_FORWARDING:
1400 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001401 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001402 break;
1403 }
1404
Vivien Didelot553eb542016-05-13 20:38:23 -04001405 mutex_lock(&ps->smi_mutex);
1406 err = _mv88e6xxx_port_state(ps, port, stp_state);
1407 mutex_unlock(&ps->smi_mutex);
1408
1409 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001410 netdev_err(ds->ports[port].netdev,
1411 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001412 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001413}
1414
Andrew Lunn158bc062016-04-28 21:24:06 -04001415static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port,
1416 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001417{
Andrew Lunn158bc062016-04-28 21:24:06 -04001418 struct dsa_switch *ds = ps->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001419 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001420 int ret;
1421
Andrew Lunn158bc062016-04-28 21:24:06 -04001422 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001423 if (ret < 0)
1424 return ret;
1425
Vivien Didelot5da96032016-03-07 18:24:39 -05001426 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1427
1428 if (new) {
1429 ret &= ~PORT_DEFAULT_VLAN_MASK;
1430 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1431
Andrew Lunn158bc062016-04-28 21:24:06 -04001432 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001433 PORT_DEFAULT_VLAN, ret);
1434 if (ret < 0)
1435 return ret;
1436
Andrew Lunnc8b09802016-06-04 21:16:57 +02001437 netdev_dbg(ds->ports[port].netdev,
1438 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001439 }
1440
1441 if (old)
1442 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001443
1444 return 0;
1445}
1446
Andrew Lunn158bc062016-04-28 21:24:06 -04001447static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps,
1448 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001449{
Andrew Lunn158bc062016-04-28 21:24:06 -04001450 return _mv88e6xxx_port_pvid(ps, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001451}
1452
Andrew Lunn158bc062016-04-28 21:24:06 -04001453static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps,
1454 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001455{
Andrew Lunn158bc062016-04-28 21:24:06 -04001456 return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001457}
1458
Andrew Lunn158bc062016-04-28 21:24:06 -04001459static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001460{
Andrew Lunn158bc062016-04-28 21:24:06 -04001461 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001462 GLOBAL_VTU_OP_BUSY);
1463}
1464
Andrew Lunn158bc062016-04-28 21:24:06 -04001465static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001466{
1467 int ret;
1468
Andrew Lunn158bc062016-04-28 21:24:06 -04001469 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001470 if (ret < 0)
1471 return ret;
1472
Andrew Lunn158bc062016-04-28 21:24:06 -04001473 return _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001474}
1475
Andrew Lunn158bc062016-04-28 21:24:06 -04001476static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001477{
1478 int ret;
1479
Andrew Lunn158bc062016-04-28 21:24:06 -04001480 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001481 if (ret < 0)
1482 return ret;
1483
Andrew Lunn158bc062016-04-28 21:24:06 -04001484 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001485}
1486
Andrew Lunn158bc062016-04-28 21:24:06 -04001487static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001488 struct mv88e6xxx_vtu_stu_entry *entry,
1489 unsigned int nibble_offset)
1490{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001491 u16 regs[3];
1492 int i;
1493 int ret;
1494
1495 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001496 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001497 GLOBAL_VTU_DATA_0_3 + i);
1498 if (ret < 0)
1499 return ret;
1500
1501 regs[i] = ret;
1502 }
1503
Vivien Didelot009a2b92016-04-17 13:24:01 -04001504 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001505 unsigned int shift = (i % 4) * 4 + nibble_offset;
1506 u16 reg = regs[i / 4];
1507
1508 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1509 }
1510
1511 return 0;
1512}
1513
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001514static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_priv_state *ps,
1515 struct mv88e6xxx_vtu_stu_entry *entry)
1516{
1517 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 0);
1518}
1519
1520static int mv88e6xxx_stu_data_read(struct mv88e6xxx_priv_state *ps,
1521 struct mv88e6xxx_vtu_stu_entry *entry)
1522{
1523 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 2);
1524}
1525
Andrew Lunn158bc062016-04-28 21:24:06 -04001526static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001527 struct mv88e6xxx_vtu_stu_entry *entry,
1528 unsigned int nibble_offset)
1529{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001530 u16 regs[3] = { 0 };
1531 int i;
1532 int ret;
1533
Vivien Didelot009a2b92016-04-17 13:24:01 -04001534 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001535 unsigned int shift = (i % 4) * 4 + nibble_offset;
1536 u8 data = entry->data[i];
1537
1538 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1539 }
1540
1541 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001542 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001543 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1544 if (ret < 0)
1545 return ret;
1546 }
1547
1548 return 0;
1549}
1550
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001551static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_priv_state *ps,
1552 struct mv88e6xxx_vtu_stu_entry *entry)
1553{
1554 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 0);
1555}
1556
1557static int mv88e6xxx_stu_data_write(struct mv88e6xxx_priv_state *ps,
1558 struct mv88e6xxx_vtu_stu_entry *entry)
1559{
1560 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 2);
1561}
1562
Andrew Lunn158bc062016-04-28 21:24:06 -04001563static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001564{
Andrew Lunn158bc062016-04-28 21:24:06 -04001565 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001566 vid & GLOBAL_VTU_VID_MASK);
1567}
1568
Andrew Lunn158bc062016-04-28 21:24:06 -04001569static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001570 struct mv88e6xxx_vtu_stu_entry *entry)
1571{
1572 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1573 int ret;
1574
Andrew Lunn158bc062016-04-28 21:24:06 -04001575 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001576 if (ret < 0)
1577 return ret;
1578
Andrew Lunn158bc062016-04-28 21:24:06 -04001579 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001580 if (ret < 0)
1581 return ret;
1582
Andrew Lunn158bc062016-04-28 21:24:06 -04001583 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001584 if (ret < 0)
1585 return ret;
1586
1587 next.vid = ret & GLOBAL_VTU_VID_MASK;
1588 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1589
1590 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001591 ret = mv88e6xxx_vtu_data_read(ps, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001592 if (ret < 0)
1593 return ret;
1594
Andrew Lunn158bc062016-04-28 21:24:06 -04001595 if (mv88e6xxx_has_fid_reg(ps)) {
1596 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001597 GLOBAL_VTU_FID);
1598 if (ret < 0)
1599 return ret;
1600
1601 next.fid = ret & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001602 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001603 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1604 * VTU DBNum[3:0] are located in VTU Operation 3:0
1605 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001606 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001607 GLOBAL_VTU_OP);
1608 if (ret < 0)
1609 return ret;
1610
1611 next.fid = (ret & 0xf00) >> 4;
1612 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001613 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001614
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001615 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001616 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001617 GLOBAL_VTU_SID);
1618 if (ret < 0)
1619 return ret;
1620
1621 next.sid = ret & GLOBAL_VTU_SID_MASK;
1622 }
1623 }
1624
1625 *entry = next;
1626 return 0;
1627}
1628
Vivien Didelotf81ec902016-05-09 13:22:58 -04001629static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1630 struct switchdev_obj_port_vlan *vlan,
1631 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001632{
1633 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1634 struct mv88e6xxx_vtu_stu_entry next;
1635 u16 pvid;
1636 int err;
1637
Vivien Didelot54d77b52016-05-09 13:22:47 -04001638 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
1639 return -EOPNOTSUPP;
1640
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001641 mutex_lock(&ps->smi_mutex);
1642
Andrew Lunn158bc062016-04-28 21:24:06 -04001643 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001644 if (err)
1645 goto unlock;
1646
Andrew Lunn158bc062016-04-28 21:24:06 -04001647 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001648 if (err)
1649 goto unlock;
1650
1651 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001652 err = _mv88e6xxx_vtu_getnext(ps, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001653 if (err)
1654 break;
1655
1656 if (!next.valid)
1657 break;
1658
1659 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1660 continue;
1661
1662 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001663 vlan->vid_begin = next.vid;
1664 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001665 vlan->flags = 0;
1666
1667 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1668 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1669
1670 if (next.vid == pvid)
1671 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1672
1673 err = cb(&vlan->obj);
1674 if (err)
1675 break;
1676 } while (next.vid < GLOBAL_VTU_VID_MASK);
1677
1678unlock:
1679 mutex_unlock(&ps->smi_mutex);
1680
1681 return err;
1682}
1683
Andrew Lunn158bc062016-04-28 21:24:06 -04001684static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001685 struct mv88e6xxx_vtu_stu_entry *entry)
1686{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001687 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001688 u16 reg = 0;
1689 int ret;
1690
Andrew Lunn158bc062016-04-28 21:24:06 -04001691 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001692 if (ret < 0)
1693 return ret;
1694
1695 if (!entry->valid)
1696 goto loadpurge;
1697
1698 /* Write port member tags */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001699 ret = mv88e6xxx_vtu_data_write(ps, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001700 if (ret < 0)
1701 return ret;
1702
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001703 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001704 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001705 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001706 if (ret < 0)
1707 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001708 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001709
Andrew Lunn158bc062016-04-28 21:24:06 -04001710 if (mv88e6xxx_has_fid_reg(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001711 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001712 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001713 if (ret < 0)
1714 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001715 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001716 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1717 * VTU DBNum[3:0] are located in VTU Operation 3:0
1718 */
1719 op |= (entry->fid & 0xf0) << 8;
1720 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001721 }
1722
1723 reg = GLOBAL_VTU_VID_VALID;
1724loadpurge:
1725 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001726 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001727 if (ret < 0)
1728 return ret;
1729
Andrew Lunn158bc062016-04-28 21:24:06 -04001730 return _mv88e6xxx_vtu_cmd(ps, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001731}
1732
Andrew Lunn158bc062016-04-28 21:24:06 -04001733static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001734 struct mv88e6xxx_vtu_stu_entry *entry)
1735{
1736 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1737 int ret;
1738
Andrew Lunn158bc062016-04-28 21:24:06 -04001739 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001740 if (ret < 0)
1741 return ret;
1742
Andrew Lunn158bc062016-04-28 21:24:06 -04001743 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001744 sid & GLOBAL_VTU_SID_MASK);
1745 if (ret < 0)
1746 return ret;
1747
Andrew Lunn158bc062016-04-28 21:24:06 -04001748 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001749 if (ret < 0)
1750 return ret;
1751
Andrew Lunn158bc062016-04-28 21:24:06 -04001752 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001753 if (ret < 0)
1754 return ret;
1755
1756 next.sid = ret & GLOBAL_VTU_SID_MASK;
1757
Andrew Lunn158bc062016-04-28 21:24:06 -04001758 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001759 if (ret < 0)
1760 return ret;
1761
1762 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1763
1764 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001765 ret = mv88e6xxx_stu_data_read(ps, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001766 if (ret < 0)
1767 return ret;
1768 }
1769
1770 *entry = next;
1771 return 0;
1772}
1773
Andrew Lunn158bc062016-04-28 21:24:06 -04001774static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001775 struct mv88e6xxx_vtu_stu_entry *entry)
1776{
1777 u16 reg = 0;
1778 int ret;
1779
Andrew Lunn158bc062016-04-28 21:24:06 -04001780 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001781 if (ret < 0)
1782 return ret;
1783
1784 if (!entry->valid)
1785 goto loadpurge;
1786
1787 /* Write port states */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001788 ret = mv88e6xxx_stu_data_write(ps, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001789 if (ret < 0)
1790 return ret;
1791
1792 reg = GLOBAL_VTU_VID_VALID;
1793loadpurge:
Andrew Lunn158bc062016-04-28 21:24:06 -04001794 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001795 if (ret < 0)
1796 return ret;
1797
1798 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001799 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001800 if (ret < 0)
1801 return ret;
1802
Andrew Lunn158bc062016-04-28 21:24:06 -04001803 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001804}
1805
Andrew Lunn158bc062016-04-28 21:24:06 -04001806static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port,
1807 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001808{
Andrew Lunn158bc062016-04-28 21:24:06 -04001809 struct dsa_switch *ds = ps->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001810 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001811 u16 fid;
1812 int ret;
1813
Andrew Lunn158bc062016-04-28 21:24:06 -04001814 if (mv88e6xxx_num_databases(ps) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001815 upper_mask = 0xff;
Andrew Lunn158bc062016-04-28 21:24:06 -04001816 else if (mv88e6xxx_num_databases(ps) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001817 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001818 else
1819 return -EOPNOTSUPP;
1820
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001821 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001822 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001823 if (ret < 0)
1824 return ret;
1825
1826 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1827
1828 if (new) {
1829 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1830 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1831
Andrew Lunn158bc062016-04-28 21:24:06 -04001832 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001833 ret);
1834 if (ret < 0)
1835 return ret;
1836 }
1837
1838 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001839 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001840 if (ret < 0)
1841 return ret;
1842
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001843 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001844
1845 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001846 ret &= ~upper_mask;
1847 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001848
Andrew Lunn158bc062016-04-28 21:24:06 -04001849 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001850 ret);
1851 if (ret < 0)
1852 return ret;
1853
Andrew Lunnc8b09802016-06-04 21:16:57 +02001854 netdev_dbg(ds->ports[port].netdev,
1855 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001856 }
1857
1858 if (old)
1859 *old = fid;
1860
1861 return 0;
1862}
1863
Andrew Lunn158bc062016-04-28 21:24:06 -04001864static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps,
1865 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001866{
Andrew Lunn158bc062016-04-28 21:24:06 -04001867 return _mv88e6xxx_port_fid(ps, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001868}
1869
Andrew Lunn158bc062016-04-28 21:24:06 -04001870static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps,
1871 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001872{
Andrew Lunn158bc062016-04-28 21:24:06 -04001873 return _mv88e6xxx_port_fid(ps, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001874}
1875
Andrew Lunn158bc062016-04-28 21:24:06 -04001876static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001877{
1878 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1879 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001880 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001881
1882 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1883
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001884 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001885 for (i = 0; i < ps->info->num_ports; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001886 err = _mv88e6xxx_port_fid_get(ps, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001887 if (err)
1888 return err;
1889
1890 set_bit(*fid, fid_bitmap);
1891 }
1892
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001893 /* Set every FID bit used by the VLAN entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001894 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001895 if (err)
1896 return err;
1897
1898 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001899 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001900 if (err)
1901 return err;
1902
1903 if (!vlan.valid)
1904 break;
1905
1906 set_bit(vlan.fid, fid_bitmap);
1907 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1908
1909 /* The reset value 0x000 is used to indicate that multiple address
1910 * databases are not needed. Return the next positive available.
1911 */
1912 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Andrew Lunn158bc062016-04-28 21:24:06 -04001913 if (unlikely(*fid >= mv88e6xxx_num_databases(ps)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001914 return -ENOSPC;
1915
1916 /* Clear the database */
Andrew Lunn158bc062016-04-28 21:24:06 -04001917 return _mv88e6xxx_atu_flush(ps, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001918}
1919
Andrew Lunn158bc062016-04-28 21:24:06 -04001920static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001921 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001922{
Andrew Lunn158bc062016-04-28 21:24:06 -04001923 struct dsa_switch *ds = ps->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001924 struct mv88e6xxx_vtu_stu_entry vlan = {
1925 .valid = true,
1926 .vid = vid,
1927 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001928 int i, err;
1929
Andrew Lunn158bc062016-04-28 21:24:06 -04001930 err = _mv88e6xxx_fid_new(ps, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001931 if (err)
1932 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001933
Vivien Didelot3d131f02015-11-03 10:52:52 -05001934 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001935 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001936 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1937 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1938 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001939
Andrew Lunn158bc062016-04-28 21:24:06 -04001940 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
1941 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001942 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001943
1944 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1945 * implemented, only one STU entry is needed to cover all VTU
1946 * entries. Thus, validate the SID 0.
1947 */
1948 vlan.sid = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04001949 err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001950 if (err)
1951 return err;
1952
1953 if (vstp.sid != vlan.sid || !vstp.valid) {
1954 memset(&vstp, 0, sizeof(vstp));
1955 vstp.valid = true;
1956 vstp.sid = vlan.sid;
1957
Andrew Lunn158bc062016-04-28 21:24:06 -04001958 err = _mv88e6xxx_stu_loadpurge(ps, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001959 if (err)
1960 return err;
1961 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001962 }
1963
1964 *entry = vlan;
1965 return 0;
1966}
1967
Andrew Lunn158bc062016-04-28 21:24:06 -04001968static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001969 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1970{
1971 int err;
1972
1973 if (!vid)
1974 return -EINVAL;
1975
Andrew Lunn158bc062016-04-28 21:24:06 -04001976 err = _mv88e6xxx_vtu_vid_write(ps, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001977 if (err)
1978 return err;
1979
Andrew Lunn158bc062016-04-28 21:24:06 -04001980 err = _mv88e6xxx_vtu_getnext(ps, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001981 if (err)
1982 return err;
1983
1984 if (entry->vid != vid || !entry->valid) {
1985 if (!creat)
1986 return -EOPNOTSUPP;
1987 /* -ENOENT would've been more appropriate, but switchdev expects
1988 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1989 */
1990
Andrew Lunn158bc062016-04-28 21:24:06 -04001991 err = _mv88e6xxx_vtu_new(ps, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001992 }
1993
1994 return err;
1995}
1996
Vivien Didelotda9c3592016-02-12 12:09:40 -05001997static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1998 u16 vid_begin, u16 vid_end)
1999{
2000 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2001 struct mv88e6xxx_vtu_stu_entry vlan;
2002 int i, err;
2003
2004 if (!vid_begin)
2005 return -EOPNOTSUPP;
2006
2007 mutex_lock(&ps->smi_mutex);
2008
Andrew Lunn158bc062016-04-28 21:24:06 -04002009 err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002010 if (err)
2011 goto unlock;
2012
2013 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002014 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002015 if (err)
2016 goto unlock;
2017
2018 if (!vlan.valid)
2019 break;
2020
2021 if (vlan.vid > vid_end)
2022 break;
2023
Vivien Didelot009a2b92016-04-17 13:24:01 -04002024 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05002025 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
2026 continue;
2027
2028 if (vlan.data[i] ==
2029 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2030 continue;
2031
2032 if (ps->ports[i].bridge_dev ==
2033 ps->ports[port].bridge_dev)
2034 break; /* same bridge, check next VLAN */
2035
Andrew Lunnc8b09802016-06-04 21:16:57 +02002036 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05002037 "hardware VLAN %d already used by %s\n",
2038 vlan.vid,
2039 netdev_name(ps->ports[i].bridge_dev));
2040 err = -EOPNOTSUPP;
2041 goto unlock;
2042 }
2043 } while (vlan.vid < vid_end);
2044
2045unlock:
2046 mutex_unlock(&ps->smi_mutex);
2047
2048 return err;
2049}
2050
Vivien Didelot214cdb92016-02-26 13:16:08 -05002051static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2052 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2053 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2054 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2055 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2056};
2057
Vivien Didelotf81ec902016-05-09 13:22:58 -04002058static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2059 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05002060{
2061 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2062 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2063 PORT_CONTROL_2_8021Q_DISABLED;
2064 int ret;
2065
Vivien Didelot54d77b52016-05-09 13:22:47 -04002066 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2067 return -EOPNOTSUPP;
2068
Vivien Didelot214cdb92016-02-26 13:16:08 -05002069 mutex_lock(&ps->smi_mutex);
2070
Andrew Lunn158bc062016-04-28 21:24:06 -04002071 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002072 if (ret < 0)
2073 goto unlock;
2074
2075 old = ret & PORT_CONTROL_2_8021Q_MASK;
2076
Vivien Didelot5220ef12016-03-07 18:24:52 -05002077 if (new != old) {
2078 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2079 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002080
Andrew Lunn158bc062016-04-28 21:24:06 -04002081 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002082 ret);
2083 if (ret < 0)
2084 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002085
Andrew Lunnc8b09802016-06-04 21:16:57 +02002086 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05002087 mv88e6xxx_port_8021q_mode_names[new],
2088 mv88e6xxx_port_8021q_mode_names[old]);
2089 }
2090
2091 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002092unlock:
2093 mutex_unlock(&ps->smi_mutex);
2094
2095 return ret;
2096}
2097
Vivien Didelot57d32312016-06-20 13:13:58 -04002098static int
2099mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2100 const struct switchdev_obj_port_vlan *vlan,
2101 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002102{
Vivien Didelot54d77b52016-05-09 13:22:47 -04002103 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002104 int err;
2105
Vivien Didelot54d77b52016-05-09 13:22:47 -04002106 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2107 return -EOPNOTSUPP;
2108
Vivien Didelotda9c3592016-02-12 12:09:40 -05002109 /* If the requested port doesn't belong to the same bridge as the VLAN
2110 * members, do not support it (yet) and fallback to software VLAN.
2111 */
2112 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2113 vlan->vid_end);
2114 if (err)
2115 return err;
2116
Vivien Didelot76e398a2015-11-01 12:33:55 -05002117 /* We don't need any dynamic resource from the kernel (yet),
2118 * so skip the prepare phase.
2119 */
2120 return 0;
2121}
2122
Andrew Lunn158bc062016-04-28 21:24:06 -04002123static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port,
2124 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002125{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002126 struct mv88e6xxx_vtu_stu_entry vlan;
2127 int err;
2128
Andrew Lunn158bc062016-04-28 21:24:06 -04002129 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002130 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002131 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002132
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002133 vlan.data[port] = untagged ?
2134 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2135 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2136
Andrew Lunn158bc062016-04-28 21:24:06 -04002137 return _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002138}
2139
Vivien Didelotf81ec902016-05-09 13:22:58 -04002140static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2141 const struct switchdev_obj_port_vlan *vlan,
2142 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002143{
2144 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2145 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2146 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2147 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002148
Vivien Didelot54d77b52016-05-09 13:22:47 -04002149 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2150 return;
2151
Vivien Didelot76e398a2015-11-01 12:33:55 -05002152 mutex_lock(&ps->smi_mutex);
2153
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002154 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Andrew Lunn158bc062016-04-28 21:24:06 -04002155 if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002156 netdev_err(ds->ports[port].netdev,
2157 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002158 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002159
Andrew Lunn158bc062016-04-28 21:24:06 -04002160 if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002161 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002162 vlan->vid_end);
2163
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002164 mutex_unlock(&ps->smi_mutex);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002165}
2166
Andrew Lunn158bc062016-04-28 21:24:06 -04002167static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps,
2168 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002169{
Andrew Lunn158bc062016-04-28 21:24:06 -04002170 struct dsa_switch *ds = ps->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002171 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002172 int i, err;
2173
Andrew Lunn158bc062016-04-28 21:24:06 -04002174 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002175 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002176 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002177
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002178 /* Tell switchdev if this VLAN is handled in software */
2179 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002180 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002181
2182 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2183
2184 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002185 vlan.valid = false;
Vivien Didelot009a2b92016-04-17 13:24:01 -04002186 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002187 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002188 continue;
2189
2190 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002191 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002192 break;
2193 }
2194 }
2195
Andrew Lunn158bc062016-04-28 21:24:06 -04002196 err = _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002197 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002198 return err;
2199
Andrew Lunn158bc062016-04-28 21:24:06 -04002200 return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002201}
2202
Vivien Didelotf81ec902016-05-09 13:22:58 -04002203static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2204 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002205{
2206 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2207 u16 pvid, vid;
2208 int err = 0;
2209
Vivien Didelot54d77b52016-05-09 13:22:47 -04002210 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2211 return -EOPNOTSUPP;
2212
Vivien Didelot76e398a2015-11-01 12:33:55 -05002213 mutex_lock(&ps->smi_mutex);
2214
Andrew Lunn158bc062016-04-28 21:24:06 -04002215 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002216 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002217 goto unlock;
2218
Vivien Didelot76e398a2015-11-01 12:33:55 -05002219 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002220 err = _mv88e6xxx_port_vlan_del(ps, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002221 if (err)
2222 goto unlock;
2223
2224 if (vid == pvid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002225 err = _mv88e6xxx_port_pvid_set(ps, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002226 if (err)
2227 goto unlock;
2228 }
2229 }
2230
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002231unlock:
2232 mutex_unlock(&ps->smi_mutex);
2233
2234 return err;
2235}
2236
Andrew Lunn158bc062016-04-28 21:24:06 -04002237static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002238 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002239{
2240 int i, ret;
2241
2242 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002243 ret = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002244 ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002245 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002246 if (ret < 0)
2247 return ret;
2248 }
2249
2250 return 0;
2251}
2252
Andrew Lunn158bc062016-04-28 21:24:06 -04002253static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps,
2254 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002255{
2256 int i, ret;
2257
2258 for (i = 0; i < 3; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002259 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002260 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002261 if (ret < 0)
2262 return ret;
2263 addr[i * 2] = ret >> 8;
2264 addr[i * 2 + 1] = ret & 0xff;
2265 }
2266
2267 return 0;
2268}
2269
Andrew Lunn158bc062016-04-28 21:24:06 -04002270static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002271 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002272{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002273 int ret;
2274
Andrew Lunn158bc062016-04-28 21:24:06 -04002275 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002276 if (ret < 0)
2277 return ret;
2278
Andrew Lunn158bc062016-04-28 21:24:06 -04002279 ret = _mv88e6xxx_atu_mac_write(ps, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002280 if (ret < 0)
2281 return ret;
2282
Andrew Lunn158bc062016-04-28 21:24:06 -04002283 ret = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002284 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002285 return ret;
2286
Andrew Lunn158bc062016-04-28 21:24:06 -04002287 return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002288}
David S. Millercdf09692015-08-11 12:00:37 -07002289
Andrew Lunn158bc062016-04-28 21:24:06 -04002290static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002291 const unsigned char *addr, u16 vid,
2292 u8 state)
2293{
2294 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002295 struct mv88e6xxx_vtu_stu_entry vlan;
2296 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002297
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002298 /* Null VLAN ID corresponds to the port private database */
2299 if (vid == 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04002300 err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002301 else
Andrew Lunn158bc062016-04-28 21:24:06 -04002302 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002303 if (err)
2304 return err;
2305
2306 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002307 entry.state = state;
2308 ether_addr_copy(entry.mac, addr);
2309 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2310 entry.trunk = false;
2311 entry.portv_trunkid = BIT(port);
2312 }
2313
Andrew Lunn158bc062016-04-28 21:24:06 -04002314 return _mv88e6xxx_atu_load(ps, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002315}
2316
Vivien Didelotf81ec902016-05-09 13:22:58 -04002317static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2318 const struct switchdev_obj_port_fdb *fdb,
2319 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002320{
Vivien Didelot2672f822016-05-09 13:22:48 -04002321 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2322
2323 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2324 return -EOPNOTSUPP;
2325
Vivien Didelot146a3202015-10-08 11:35:12 -04002326 /* We don't need any dynamic resource from the kernel (yet),
2327 * so skip the prepare phase.
2328 */
2329 return 0;
2330}
2331
Vivien Didelotf81ec902016-05-09 13:22:58 -04002332static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2333 const struct switchdev_obj_port_fdb *fdb,
2334 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002335{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002336 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002337 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2338 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2339 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002340
Vivien Didelot2672f822016-05-09 13:22:48 -04002341 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2342 return;
2343
David S. Millercdf09692015-08-11 12:00:37 -07002344 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002345 if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002346 netdev_err(ds->ports[port].netdev,
2347 "failed to load MAC address\n");
David S. Millercdf09692015-08-11 12:00:37 -07002348 mutex_unlock(&ps->smi_mutex);
David S. Millercdf09692015-08-11 12:00:37 -07002349}
2350
Vivien Didelotf81ec902016-05-09 13:22:58 -04002351static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2352 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002353{
2354 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2355 int ret;
2356
Vivien Didelot2672f822016-05-09 13:22:48 -04002357 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2358 return -EOPNOTSUPP;
2359
David S. Millercdf09692015-08-11 12:00:37 -07002360 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002361 ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002362 GLOBAL_ATU_DATA_STATE_UNUSED);
2363 mutex_unlock(&ps->smi_mutex);
2364
2365 return ret;
2366}
2367
Andrew Lunn158bc062016-04-28 21:24:06 -04002368static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002369 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002370{
Vivien Didelot1d194042015-08-10 09:09:51 -04002371 struct mv88e6xxx_atu_entry next = { 0 };
2372 int ret;
2373
2374 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002375
Andrew Lunn158bc062016-04-28 21:24:06 -04002376 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002377 if (ret < 0)
2378 return ret;
2379
Andrew Lunn158bc062016-04-28 21:24:06 -04002380 ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002381 if (ret < 0)
2382 return ret;
2383
Andrew Lunn158bc062016-04-28 21:24:06 -04002384 ret = _mv88e6xxx_atu_mac_read(ps, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002385 if (ret < 0)
2386 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002387
Andrew Lunn158bc062016-04-28 21:24:06 -04002388 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002389 if (ret < 0)
2390 return ret;
2391
2392 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2393 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2394 unsigned int mask, shift;
2395
2396 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2397 next.trunk = true;
2398 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2399 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2400 } else {
2401 next.trunk = false;
2402 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2403 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2404 }
2405
2406 next.portv_trunkid = (ret & mask) >> shift;
2407 }
2408
2409 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002410 return 0;
2411}
2412
Andrew Lunn158bc062016-04-28 21:24:06 -04002413static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps,
2414 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002415 struct switchdev_obj_port_fdb *fdb,
2416 int (*cb)(struct switchdev_obj *obj))
2417{
2418 struct mv88e6xxx_atu_entry addr = {
2419 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2420 };
2421 int err;
2422
Andrew Lunn158bc062016-04-28 21:24:06 -04002423 err = _mv88e6xxx_atu_mac_write(ps, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002424 if (err)
2425 return err;
2426
2427 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002428 err = _mv88e6xxx_atu_getnext(ps, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002429 if (err)
2430 break;
2431
2432 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2433 break;
2434
2435 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2436 bool is_static = addr.state ==
2437 (is_multicast_ether_addr(addr.mac) ?
2438 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2439 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2440
2441 fdb->vid = vid;
2442 ether_addr_copy(fdb->addr, addr.mac);
2443 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2444
2445 err = cb(&fdb->obj);
2446 if (err)
2447 break;
2448 }
2449 } while (!is_broadcast_ether_addr(addr.mac));
2450
2451 return err;
2452}
2453
Vivien Didelotf81ec902016-05-09 13:22:58 -04002454static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2455 struct switchdev_obj_port_fdb *fdb,
2456 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002457{
2458 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2459 struct mv88e6xxx_vtu_stu_entry vlan = {
2460 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2461 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002462 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002463 int err;
2464
Vivien Didelot2672f822016-05-09 13:22:48 -04002465 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2466 return -EOPNOTSUPP;
2467
Vivien Didelotf33475b2015-10-22 09:34:41 -04002468 mutex_lock(&ps->smi_mutex);
2469
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002470 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Andrew Lunn158bc062016-04-28 21:24:06 -04002471 err = _mv88e6xxx_port_fid_get(ps, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002472 if (err)
2473 goto unlock;
2474
Andrew Lunn158bc062016-04-28 21:24:06 -04002475 err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002476 if (err)
2477 goto unlock;
2478
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002479 /* Dump VLANs' Filtering Information Databases */
Andrew Lunn158bc062016-04-28 21:24:06 -04002480 err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002481 if (err)
2482 goto unlock;
2483
2484 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002485 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002486 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002487 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002488
2489 if (!vlan.valid)
2490 break;
2491
Andrew Lunn158bc062016-04-28 21:24:06 -04002492 err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002493 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002494 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002495 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002496 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2497
2498unlock:
2499 mutex_unlock(&ps->smi_mutex);
2500
2501 return err;
2502}
2503
Vivien Didelotf81ec902016-05-09 13:22:58 -04002504static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2505 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002506{
Vivien Didelota6692752016-02-12 12:09:39 -05002507 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002508 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002509
Vivien Didelot936f2342016-05-09 13:22:46 -04002510 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2511 return -EOPNOTSUPP;
2512
Vivien Didelot466dfa02016-02-26 13:16:05 -05002513 mutex_lock(&ps->smi_mutex);
2514
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002515 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002516 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002517
Vivien Didelot009a2b92016-04-17 13:24:01 -04002518 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002519 if (ps->ports[i].bridge_dev == bridge) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002520 err = _mv88e6xxx_port_based_vlan_map(ps, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002521 if (err)
2522 break;
2523 }
2524 }
2525
Vivien Didelot466dfa02016-02-26 13:16:05 -05002526 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002527
Vivien Didelot466dfa02016-02-26 13:16:05 -05002528 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002529}
2530
Vivien Didelotf81ec902016-05-09 13:22:58 -04002531static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002532{
Vivien Didelota6692752016-02-12 12:09:39 -05002533 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002534 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002535 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002536
Vivien Didelot936f2342016-05-09 13:22:46 -04002537 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2538 return;
2539
Vivien Didelot466dfa02016-02-26 13:16:05 -05002540 mutex_lock(&ps->smi_mutex);
2541
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002542 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002543 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002544
Vivien Didelot009a2b92016-04-17 13:24:01 -04002545 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot16bfa702016-03-13 16:21:33 -04002546 if (i == port || ps->ports[i].bridge_dev == bridge)
Andrew Lunn158bc062016-04-28 21:24:06 -04002547 if (_mv88e6xxx_port_based_vlan_map(ps, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002548 netdev_warn(ds->ports[i].netdev,
2549 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002550
Vivien Didelot466dfa02016-02-26 13:16:05 -05002551 mutex_unlock(&ps->smi_mutex);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002552}
2553
Andrew Lunn03a4a542016-06-04 21:17:05 +02002554static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_priv_state *ps,
2555 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002556{
2557 int ret;
2558
Andrew Lunn03a4a542016-06-04 21:17:05 +02002559 ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002560 if (ret < 0)
2561 goto restore_page_0;
2562
Andrew Lunn03a4a542016-06-04 21:17:05 +02002563 ret = mv88e6xxx_mdio_write_indirect(ps, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002564restore_page_0:
Andrew Lunn03a4a542016-06-04 21:17:05 +02002565 mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002566
2567 return ret;
2568}
2569
Andrew Lunn03a4a542016-06-04 21:17:05 +02002570static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_priv_state *ps,
2571 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002572{
2573 int ret;
2574
Andrew Lunn03a4a542016-06-04 21:17:05 +02002575 ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002576 if (ret < 0)
2577 goto restore_page_0;
2578
Andrew Lunn03a4a542016-06-04 21:17:05 +02002579 ret = mv88e6xxx_mdio_read_indirect(ps, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002580restore_page_0:
Andrew Lunn03a4a542016-06-04 21:17:05 +02002581 mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002582
2583 return ret;
2584}
2585
Vivien Didelot552238b2016-05-09 13:22:49 -04002586static int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps)
2587{
2588 bool ppu_active = mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE);
2589 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunn52638f72016-05-10 23:27:22 +02002590 struct gpio_desc *gpiod = ps->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002591 unsigned long timeout;
2592 int ret;
2593 int i;
2594
2595 /* Set all ports to the disabled state. */
2596 for (i = 0; i < ps->info->num_ports; i++) {
2597 ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL);
2598 if (ret < 0)
2599 return ret;
2600
2601 ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL,
2602 ret & 0xfffc);
2603 if (ret)
2604 return ret;
2605 }
2606
2607 /* Wait for transmit queues to drain. */
2608 usleep_range(2000, 4000);
2609
2610 /* If there is a gpio connected to the reset pin, toggle it */
2611 if (gpiod) {
2612 gpiod_set_value_cansleep(gpiod, 1);
2613 usleep_range(10000, 20000);
2614 gpiod_set_value_cansleep(gpiod, 0);
2615 usleep_range(10000, 20000);
2616 }
2617
2618 /* Reset the switch. Keep the PPU active if requested. The PPU
2619 * needs to be active to support indirect phy register access
2620 * through global registers 0x18 and 0x19.
2621 */
2622 if (ppu_active)
2623 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000);
2624 else
2625 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400);
2626 if (ret)
2627 return ret;
2628
2629 /* Wait up to one second for reset to complete. */
2630 timeout = jiffies + 1 * HZ;
2631 while (time_before(jiffies, timeout)) {
2632 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00);
2633 if (ret < 0)
2634 return ret;
2635
2636 if ((ret & is_reset) == is_reset)
2637 break;
2638 usleep_range(1000, 2000);
2639 }
2640 if (time_after(jiffies, timeout))
2641 ret = -ETIMEDOUT;
2642 else
2643 ret = 0;
2644
2645 return ret;
2646}
2647
Andrew Lunn158bc062016-04-28 21:24:06 -04002648static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002649{
2650 int ret;
2651
Andrew Lunn03a4a542016-06-04 21:17:05 +02002652 ret = _mv88e6xxx_mdio_page_read(ps, REG_FIBER_SERDES,
2653 PAGE_FIBER_SERDES, MII_BMCR);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002654 if (ret < 0)
2655 return ret;
2656
2657 if (ret & BMCR_PDOWN) {
2658 ret &= ~BMCR_PDOWN;
Andrew Lunn03a4a542016-06-04 21:17:05 +02002659 ret = _mv88e6xxx_mdio_page_write(ps, REG_FIBER_SERDES,
2660 PAGE_FIBER_SERDES, MII_BMCR,
2661 ret);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002662 }
2663
2664 return ret;
2665}
2666
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002667static int mv88e6xxx_setup_port(struct mv88e6xxx_priv_state *ps, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002668{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002669 struct dsa_switch *ds = ps->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002670 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002671 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002672
Andrew Lunn158bc062016-04-28 21:24:06 -04002673 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2674 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2675 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2676 mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002677 /* MAC Forcing register: don't force link, speed,
2678 * duplex or flow control state to any particular
2679 * values on physical ports, but force the CPU port
2680 * and all DSA ports to their maximum bandwidth and
2681 * full duplex.
2682 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002683 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002684 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002685 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002686 reg |= PORT_PCS_CTRL_FORCE_LINK |
2687 PORT_PCS_CTRL_LINK_UP |
2688 PORT_PCS_CTRL_DUPLEX_FULL |
2689 PORT_PCS_CTRL_FORCE_DUPLEX;
Andrew Lunn158bc062016-04-28 21:24:06 -04002690 if (mv88e6xxx_6065_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002691 reg |= PORT_PCS_CTRL_100;
2692 else
2693 reg |= PORT_PCS_CTRL_1000;
2694 } else {
2695 reg |= PORT_PCS_CTRL_UNFORCED;
2696 }
2697
Andrew Lunn158bc062016-04-28 21:24:06 -04002698 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002699 PORT_PCS_CTRL, reg);
2700 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002701 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002702 }
2703
2704 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2705 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2706 * tunneling, determine priority by looking at 802.1p and IP
2707 * priority fields (IP prio has precedence), and set STP state
2708 * to Forwarding.
2709 *
2710 * If this is the CPU link, use DSA or EDSA tagging depending
2711 * on which tagging mode was configured.
2712 *
2713 * If this is a link to another switch, use DSA tagging mode.
2714 *
2715 * If this is the upstream port for this switch, enable
2716 * forwarding of unknown unicasts and multicasts.
2717 */
2718 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002719 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2720 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2721 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2722 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002723 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2724 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2725 PORT_CONTROL_STATE_FORWARDING;
2726 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002727 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002728 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002729 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2730 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2731 mv88e6xxx_6320_family(ps)) {
Andrew Lunn5377b802016-06-04 21:17:02 +02002732 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2733 PORT_CONTROL_FORWARD_UNKNOWN |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002734 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002735 }
2736
Andrew Lunn158bc062016-04-28 21:24:06 -04002737 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2738 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2739 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2740 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) {
Vivien Didelot57d32312016-06-20 13:13:58 -04002741 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002742 }
2743 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002744 if (dsa_is_dsa_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002745 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002746 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002747 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2748 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2749 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002750 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002751 }
2752
Andrew Lunn54d792f2015-05-06 01:09:47 +02002753 if (port == dsa_upstream_port(ds))
2754 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2755 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2756 }
2757 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002758 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002759 PORT_CONTROL, reg);
2760 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002761 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002762 }
2763
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002764 /* If this port is connected to a SerDes, make sure the SerDes is not
2765 * powered down.
2766 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002767 if (mv88e6xxx_6352_family(ps)) {
2768 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002769 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002770 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002771 ret &= PORT_STATUS_CMODE_MASK;
2772 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2773 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2774 (ret == PORT_STATUS_CMODE_SGMII)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002775 ret = mv88e6xxx_power_on_serdes(ps);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002776 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002777 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002778 }
2779 }
2780
Vivien Didelot8efdda42015-08-13 12:52:23 -04002781 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002782 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002783 * untagged frames on this port, do a destination address lookup on all
2784 * received packets as usual, disable ARP mirroring and don't send a
2785 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002786 */
2787 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002788 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2789 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2790 mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) ||
2791 mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002792 reg = PORT_CONTROL_2_MAP_DA;
2793
Andrew Lunn158bc062016-04-28 21:24:06 -04002794 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2795 mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002796 reg |= PORT_CONTROL_2_JUMBO_10240;
2797
Andrew Lunn158bc062016-04-28 21:24:06 -04002798 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002799 /* Set the upstream port this port should use */
2800 reg |= dsa_upstream_port(ds);
2801 /* enable forwarding of unknown multicast addresses to
2802 * the upstream port
2803 */
2804 if (port == dsa_upstream_port(ds))
2805 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2806 }
2807
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002808 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002809
Andrew Lunn54d792f2015-05-06 01:09:47 +02002810 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002811 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002812 PORT_CONTROL_2, reg);
2813 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002814 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002815 }
2816
2817 /* Port Association Vector: when learning source addresses
2818 * of packets, add the address to the address database using
2819 * a port bitmap that has only the bit for this port set and
2820 * the other bits clear.
2821 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002822 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002823 /* Disable learning for CPU port */
2824 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002825 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002826
Andrew Lunn158bc062016-04-28 21:24:06 -04002827 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002828 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002829 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002830
2831 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002832 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002833 0x0000);
2834 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002835 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002836
Andrew Lunn158bc062016-04-28 21:24:06 -04002837 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2838 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2839 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002840 /* Do not limit the period of time that this port can
2841 * be paused for by the remote end or the period of
2842 * time that this port can pause the remote end.
2843 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002844 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002845 PORT_PAUSE_CTRL, 0x0000);
2846 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002847 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002848
2849 /* Port ATU control: disable limiting the number of
2850 * address database entries that this port is allowed
2851 * to use.
2852 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002853 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002854 PORT_ATU_CONTROL, 0x0000);
2855 /* Priority Override: disable DA, SA and VTU priority
2856 * override.
2857 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002858 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002859 PORT_PRI_OVERRIDE, 0x0000);
2860 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002861 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002862
2863 /* Port Ethertype: use the Ethertype DSA Ethertype
2864 * value.
2865 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002866 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002867 PORT_ETH_TYPE, ETH_P_EDSA);
2868 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002869 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002870 /* Tag Remap: use an identity 802.1p prio -> switch
2871 * prio mapping.
2872 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002873 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002874 PORT_TAG_REGMAP_0123, 0x3210);
2875 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002876 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002877
2878 /* Tag Remap 2: use an identity 802.1p prio -> switch
2879 * prio mapping.
2880 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002881 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002882 PORT_TAG_REGMAP_4567, 0x7654);
2883 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002884 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002885 }
2886
Andrew Lunn158bc062016-04-28 21:24:06 -04002887 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2888 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2889 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2890 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002891 /* Rate Control: disable ingress rate limiting. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002892 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002893 PORT_RATE_CONTROL, 0x0001);
2894 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002895 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002896 }
2897
Guenter Roeck366f0a02015-03-26 18:36:30 -07002898 /* Port Control 1: disable trunking, disable sending
2899 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002900 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002901 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002902 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002903 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002904
Vivien Didelot207afda2016-04-14 14:42:09 -04002905 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002906 * database, and allow bidirectional communication between the
2907 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002908 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002909 ret = _mv88e6xxx_port_fid_set(ps, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002910 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002911 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002912
Andrew Lunn158bc062016-04-28 21:24:06 -04002913 ret = _mv88e6xxx_port_based_vlan_map(ps, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002914 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002915 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002916
2917 /* Default VLAN ID and priority: don't set a default VLAN
2918 * ID, and set the default packet priority to zero.
2919 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002920 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002921 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002922 if (ret)
2923 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002924
Andrew Lunndbde9e62015-05-06 01:09:48 +02002925 return 0;
2926}
2927
Vivien Didelot08a01262016-05-09 13:22:50 -04002928static int mv88e6xxx_setup_global(struct mv88e6xxx_priv_state *ps)
2929{
Vivien Didelotb0745e872016-05-09 13:22:53 -04002930 struct dsa_switch *ds = ps->ds;
2931 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002932 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002933 int err;
2934 int i;
2935
Vivien Didelot119477b2016-05-09 13:22:51 -04002936 /* Enable the PHY Polling Unit if present, don't discard any packets,
2937 * and mask all interrupt sources.
2938 */
2939 reg = 0;
2940 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU) ||
2941 mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE))
2942 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2943
2944 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, reg);
2945 if (err)
2946 return err;
2947
Vivien Didelotb0745e872016-05-09 13:22:53 -04002948 /* Configure the upstream port, and configure it as the port to which
2949 * ingress and egress and ARP monitor frames are to be sent.
2950 */
2951 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2952 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2953 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2954 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
2955 if (err)
2956 return err;
2957
Vivien Didelot50484ff2016-05-09 13:22:54 -04002958 /* Disable remote management, and set the switch's DSA device number. */
2959 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL_2,
2960 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2961 (ds->index & 0x1f));
2962 if (err)
2963 return err;
2964
Vivien Didelot08a01262016-05-09 13:22:50 -04002965 /* Set the default address aging time to 5 minutes, and
2966 * enable address learn messages to be sent to all message
2967 * ports.
2968 */
2969 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2970 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2971 if (err)
2972 return err;
2973
2974 /* Configure the IP ToS mapping registers. */
2975 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2976 if (err)
2977 return err;
2978 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2979 if (err)
2980 return err;
2981 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2982 if (err)
2983 return err;
2984 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2985 if (err)
2986 return err;
2987 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2988 if (err)
2989 return err;
2990 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2991 if (err)
2992 return err;
2993 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2994 if (err)
2995 return err;
2996 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2997 if (err)
2998 return err;
2999
3000 /* Configure the IEEE 802.1p priority mapping register. */
3001 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
3002 if (err)
3003 return err;
3004
3005 /* Send all frames with destination addresses matching
3006 * 01:80:c2:00:00:0x to the CPU port.
3007 */
3008 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
3009 if (err)
3010 return err;
3011
3012 /* Ignore removed tag data on doubly tagged packets, disable
3013 * flow control messages, force flow control priority to the
3014 * highest, and send all special multicast frames to the CPU
3015 * port at the highest priority.
3016 */
3017 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
3018 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
3019 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
3020 if (err)
3021 return err;
3022
3023 /* Program the DSA routing table. */
3024 for (i = 0; i < 32; i++) {
3025 int nexthop = 0x1f;
3026
Andrew Lunn66472fc2016-06-04 21:17:00 +02003027 if (i != ds->index && i < DSA_MAX_SWITCHES)
3028 nexthop = ds->rtable[i] & 0x1f;
Vivien Didelot08a01262016-05-09 13:22:50 -04003029
3030 err = _mv88e6xxx_reg_write(
3031 ps, REG_GLOBAL2,
3032 GLOBAL2_DEVICE_MAPPING,
3033 GLOBAL2_DEVICE_MAPPING_UPDATE |
3034 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
3035 if (err)
3036 return err;
3037 }
3038
3039 /* Clear all trunk masks. */
3040 for (i = 0; i < 8; i++) {
3041 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
3042 0x8000 |
3043 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
3044 ((1 << ps->info->num_ports) - 1));
3045 if (err)
3046 return err;
3047 }
3048
3049 /* Clear all trunk mappings. */
3050 for (i = 0; i < 16; i++) {
3051 err = _mv88e6xxx_reg_write(
3052 ps, REG_GLOBAL2,
3053 GLOBAL2_TRUNK_MAPPING,
3054 GLOBAL2_TRUNK_MAPPING_UPDATE |
3055 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
3056 if (err)
3057 return err;
3058 }
3059
3060 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3061 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3062 mv88e6xxx_6320_family(ps)) {
3063 /* Send all frames with destination addresses matching
3064 * 01:80:c2:00:00:2x to the CPU port.
3065 */
3066 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3067 GLOBAL2_MGMT_EN_2X, 0xffff);
3068 if (err)
3069 return err;
3070
3071 /* Initialise cross-chip port VLAN table to reset
3072 * defaults.
3073 */
3074 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3075 GLOBAL2_PVT_ADDR, 0x9000);
3076 if (err)
3077 return err;
3078
3079 /* Clear the priority override table. */
3080 for (i = 0; i < 16; i++) {
3081 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3082 GLOBAL2_PRIO_OVERRIDE,
3083 0x8000 | (i << 8));
3084 if (err)
3085 return err;
3086 }
3087 }
3088
3089 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3090 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3091 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
3092 mv88e6xxx_6320_family(ps)) {
3093 /* Disable ingress rate limiting by resetting all
3094 * ingress rate limit registers to their initial
3095 * state.
3096 */
3097 for (i = 0; i < ps->info->num_ports; i++) {
3098 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3099 GLOBAL2_INGRESS_OP,
3100 0x9000 | (i << 8));
3101 if (err)
3102 return err;
3103 }
3104 }
3105
3106 /* Clear the statistics counters for all ports */
3107 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
3108 GLOBAL_STATS_OP_FLUSH_ALL);
3109 if (err)
3110 return err;
3111
3112 /* Wait for the flush to complete. */
3113 err = _mv88e6xxx_stats_wait(ps);
3114 if (err)
3115 return err;
3116
3117 /* Clear all ATU entries */
3118 err = _mv88e6xxx_atu_flush(ps, 0, true);
3119 if (err)
3120 return err;
3121
3122 /* Clear all the VTU and STU entries */
3123 err = _mv88e6xxx_vtu_stu_flush(ps);
3124 if (err < 0)
3125 return err;
3126
3127 return err;
3128}
3129
Vivien Didelotf81ec902016-05-09 13:22:58 -04003130static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003131{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003132 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003133 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003134 int i;
3135
3136 ps->ds = ds;
Andrew Lunnb516d452016-06-04 21:17:06 +02003137 ds->slave_mii_bus = ps->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003138
Vivien Didelotd24645b2016-05-09 13:22:41 -04003139 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
3140 mutex_init(&ps->eeprom_mutex);
3141
Vivien Didelot552238b2016-05-09 13:22:49 -04003142 mutex_lock(&ps->smi_mutex);
3143
3144 err = mv88e6xxx_switch_reset(ps);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003145 if (err)
3146 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003147
Vivien Didelot08a01262016-05-09 13:22:50 -04003148 err = mv88e6xxx_setup_global(ps);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003149 if (err)
3150 goto unlock;
3151
3152 for (i = 0; i < ps->info->num_ports; i++) {
3153 err = mv88e6xxx_setup_port(ps, i);
3154 if (err)
3155 goto unlock;
3156 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003157
Vivien Didelot6b17e862015-08-13 12:52:18 -04003158unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04003159 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02003160
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003161 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003162}
3163
Vivien Didelot57d32312016-06-20 13:13:58 -04003164static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
3165 int reg)
Andrew Lunn491435852015-04-02 04:06:35 +02003166{
3167 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3168 int ret;
3169
Andrew Lunn3898c142015-05-06 01:09:53 +02003170 mutex_lock(&ps->smi_mutex);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003171 ret = _mv88e6xxx_mdio_page_read(ps, port, page, reg);
Andrew Lunn3898c142015-05-06 01:09:53 +02003172 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003173
Andrew Lunn491435852015-04-02 04:06:35 +02003174 return ret;
3175}
3176
Vivien Didelot57d32312016-06-20 13:13:58 -04003177static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3178 int reg, int val)
Andrew Lunn491435852015-04-02 04:06:35 +02003179{
3180 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3181 int ret;
3182
Andrew Lunn3898c142015-05-06 01:09:53 +02003183 mutex_lock(&ps->smi_mutex);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003184 ret = _mv88e6xxx_mdio_page_write(ps, port, page, reg, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02003185 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003186
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003187 return ret;
3188}
3189
Andrew Lunn03a4a542016-06-04 21:17:05 +02003190static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_priv_state *ps,
3191 int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003192{
Vivien Didelot009a2b92016-04-17 13:24:01 -04003193 if (port >= 0 && port < ps->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003194 return port;
3195 return -EINVAL;
3196}
3197
Andrew Lunnb516d452016-06-04 21:17:06 +02003198static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003199{
Andrew Lunnb516d452016-06-04 21:17:06 +02003200 struct mv88e6xxx_priv_state *ps = bus->priv;
Andrew Lunn03a4a542016-06-04 21:17:05 +02003201 int addr = mv88e6xxx_port_to_mdio_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003202 int ret;
3203
3204 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003205 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003206
Andrew Lunn3898c142015-05-06 01:09:53 +02003207 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003208
3209 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003210 ret = mv88e6xxx_mdio_read_ppu(ps, addr, regnum);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003211 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003212 ret = mv88e6xxx_mdio_read_indirect(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003213 else
Andrew Lunn03a4a542016-06-04 21:17:05 +02003214 ret = mv88e6xxx_mdio_read_direct(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003215
Andrew Lunn3898c142015-05-06 01:09:53 +02003216 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003217 return ret;
3218}
3219
Andrew Lunnb516d452016-06-04 21:17:06 +02003220static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
Andrew Lunn03a4a542016-06-04 21:17:05 +02003221 u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003222{
Andrew Lunnb516d452016-06-04 21:17:06 +02003223 struct mv88e6xxx_priv_state *ps = bus->priv;
Andrew Lunn03a4a542016-06-04 21:17:05 +02003224 int addr = mv88e6xxx_port_to_mdio_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003225 int ret;
3226
3227 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003228 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003229
Andrew Lunn3898c142015-05-06 01:09:53 +02003230 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003231
3232 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003233 ret = mv88e6xxx_mdio_write_ppu(ps, addr, regnum, val);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003234 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003235 ret = mv88e6xxx_mdio_write_indirect(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003236 else
Andrew Lunn03a4a542016-06-04 21:17:05 +02003237 ret = mv88e6xxx_mdio_write_direct(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003238
Andrew Lunn3898c142015-05-06 01:09:53 +02003239 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003240 return ret;
3241}
3242
Andrew Lunnb516d452016-06-04 21:17:06 +02003243static int mv88e6xxx_mdio_register(struct mv88e6xxx_priv_state *ps,
3244 struct device_node *np)
3245{
3246 static int index;
3247 struct mii_bus *bus;
3248 int err;
3249
3250 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3251 mv88e6xxx_ppu_state_init(ps);
3252
3253 if (np)
3254 ps->mdio_np = of_get_child_by_name(np, "mdio");
3255
3256 bus = devm_mdiobus_alloc(ps->dev);
3257 if (!bus)
3258 return -ENOMEM;
3259
3260 bus->priv = (void *)ps;
3261 if (np) {
3262 bus->name = np->full_name;
3263 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3264 } else {
3265 bus->name = "mv88e6xxx SMI";
3266 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3267 }
3268
3269 bus->read = mv88e6xxx_mdio_read;
3270 bus->write = mv88e6xxx_mdio_write;
3271 bus->parent = ps->dev;
3272
3273 if (ps->mdio_np)
3274 err = of_mdiobus_register(bus, ps->mdio_np);
3275 else
3276 err = mdiobus_register(bus);
3277 if (err) {
3278 dev_err(ps->dev, "Cannot register MDIO bus (%d)\n", err);
3279 goto out;
3280 }
3281 ps->mdio_bus = bus;
3282
3283 return 0;
3284
3285out:
3286 if (ps->mdio_np)
3287 of_node_put(ps->mdio_np);
3288
3289 return err;
3290}
3291
3292static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_priv_state *ps)
3293
3294{
3295 struct mii_bus *bus = ps->mdio_bus;
3296
3297 mdiobus_unregister(bus);
3298
3299 if (ps->mdio_np)
3300 of_node_put(ps->mdio_np);
3301}
3302
Guenter Roeckc22995c2015-07-25 09:42:28 -07003303#ifdef CONFIG_NET_DSA_HWMON
3304
3305static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3306{
3307 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3308 int ret;
3309 int val;
3310
3311 *temp = 0;
3312
3313 mutex_lock(&ps->smi_mutex);
3314
Andrew Lunn03a4a542016-06-04 21:17:05 +02003315 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003316 if (ret < 0)
3317 goto error;
3318
3319 /* Enable temperature sensor */
Andrew Lunn03a4a542016-06-04 21:17:05 +02003320 ret = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003321 if (ret < 0)
3322 goto error;
3323
Andrew Lunn03a4a542016-06-04 21:17:05 +02003324 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003325 if (ret < 0)
3326 goto error;
3327
3328 /* Wait for temperature to stabilize */
3329 usleep_range(10000, 12000);
3330
Andrew Lunn03a4a542016-06-04 21:17:05 +02003331 val = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003332 if (val < 0) {
3333 ret = val;
3334 goto error;
3335 }
3336
3337 /* Disable temperature sensor */
Andrew Lunn03a4a542016-06-04 21:17:05 +02003338 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003339 if (ret < 0)
3340 goto error;
3341
3342 *temp = ((val & 0x1f) - 5) * 5;
3343
3344error:
Andrew Lunn03a4a542016-06-04 21:17:05 +02003345 mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x0);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003346 mutex_unlock(&ps->smi_mutex);
3347 return ret;
3348}
3349
3350static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3351{
Andrew Lunn158bc062016-04-28 21:24:06 -04003352 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3353 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003354 int ret;
3355
3356 *temp = 0;
3357
Andrew Lunn03a4a542016-06-04 21:17:05 +02003358 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003359 if (ret < 0)
3360 return ret;
3361
3362 *temp = (ret & 0xff) - 25;
3363
3364 return 0;
3365}
3366
Vivien Didelotf81ec902016-05-09 13:22:58 -04003367static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003368{
Andrew Lunn158bc062016-04-28 21:24:06 -04003369 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3370
Vivien Didelot6594f612016-05-09 13:22:42 -04003371 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP))
3372 return -EOPNOTSUPP;
3373
Andrew Lunn158bc062016-04-28 21:24:06 -04003374 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003375 return mv88e63xx_get_temp(ds, temp);
3376
3377 return mv88e61xx_get_temp(ds, temp);
3378}
3379
Vivien Didelotf81ec902016-05-09 13:22:58 -04003380static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003381{
Andrew Lunn158bc062016-04-28 21:24:06 -04003382 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3383 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003384 int ret;
3385
Vivien Didelot6594f612016-05-09 13:22:42 -04003386 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003387 return -EOPNOTSUPP;
3388
3389 *temp = 0;
3390
Andrew Lunn03a4a542016-06-04 21:17:05 +02003391 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003392 if (ret < 0)
3393 return ret;
3394
3395 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3396
3397 return 0;
3398}
3399
Vivien Didelotf81ec902016-05-09 13:22:58 -04003400static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003401{
Andrew Lunn158bc062016-04-28 21:24:06 -04003402 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3403 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003404 int ret;
3405
Vivien Didelot6594f612016-05-09 13:22:42 -04003406 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003407 return -EOPNOTSUPP;
3408
Andrew Lunn03a4a542016-06-04 21:17:05 +02003409 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003410 if (ret < 0)
3411 return ret;
3412 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003413 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3414 (ret & 0xe0ff) | (temp << 8));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003415}
3416
Vivien Didelotf81ec902016-05-09 13:22:58 -04003417static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003418{
Andrew Lunn158bc062016-04-28 21:24:06 -04003419 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3420 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003421 int ret;
3422
Vivien Didelot6594f612016-05-09 13:22:42 -04003423 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003424 return -EOPNOTSUPP;
3425
3426 *alarm = false;
3427
Andrew Lunn03a4a542016-06-04 21:17:05 +02003428 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003429 if (ret < 0)
3430 return ret;
3431
3432 *alarm = !!(ret & 0x40);
3433
3434 return 0;
3435}
3436#endif /* CONFIG_NET_DSA_HWMON */
3437
Vivien Didelotf81ec902016-05-09 13:22:58 -04003438static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3439 [MV88E6085] = {
3440 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3441 .family = MV88E6XXX_FAMILY_6097,
3442 .name = "Marvell 88E6085",
3443 .num_databases = 4096,
3444 .num_ports = 10,
3445 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3446 },
3447
3448 [MV88E6095] = {
3449 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3450 .family = MV88E6XXX_FAMILY_6095,
3451 .name = "Marvell 88E6095/88E6095F",
3452 .num_databases = 256,
3453 .num_ports = 11,
3454 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3455 },
3456
3457 [MV88E6123] = {
3458 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3459 .family = MV88E6XXX_FAMILY_6165,
3460 .name = "Marvell 88E6123",
3461 .num_databases = 4096,
3462 .num_ports = 3,
3463 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3464 },
3465
3466 [MV88E6131] = {
3467 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3468 .family = MV88E6XXX_FAMILY_6185,
3469 .name = "Marvell 88E6131",
3470 .num_databases = 256,
3471 .num_ports = 8,
3472 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3473 },
3474
3475 [MV88E6161] = {
3476 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3477 .family = MV88E6XXX_FAMILY_6165,
3478 .name = "Marvell 88E6161",
3479 .num_databases = 4096,
3480 .num_ports = 6,
3481 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3482 },
3483
3484 [MV88E6165] = {
3485 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3486 .family = MV88E6XXX_FAMILY_6165,
3487 .name = "Marvell 88E6165",
3488 .num_databases = 4096,
3489 .num_ports = 6,
3490 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3491 },
3492
3493 [MV88E6171] = {
3494 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3495 .family = MV88E6XXX_FAMILY_6351,
3496 .name = "Marvell 88E6171",
3497 .num_databases = 4096,
3498 .num_ports = 7,
3499 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3500 },
3501
3502 [MV88E6172] = {
3503 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3504 .family = MV88E6XXX_FAMILY_6352,
3505 .name = "Marvell 88E6172",
3506 .num_databases = 4096,
3507 .num_ports = 7,
3508 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3509 },
3510
3511 [MV88E6175] = {
3512 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3513 .family = MV88E6XXX_FAMILY_6351,
3514 .name = "Marvell 88E6175",
3515 .num_databases = 4096,
3516 .num_ports = 7,
3517 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3518 },
3519
3520 [MV88E6176] = {
3521 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3522 .family = MV88E6XXX_FAMILY_6352,
3523 .name = "Marvell 88E6176",
3524 .num_databases = 4096,
3525 .num_ports = 7,
3526 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3527 },
3528
3529 [MV88E6185] = {
3530 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3531 .family = MV88E6XXX_FAMILY_6185,
3532 .name = "Marvell 88E6185",
3533 .num_databases = 256,
3534 .num_ports = 10,
3535 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3536 },
3537
3538 [MV88E6240] = {
3539 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3540 .family = MV88E6XXX_FAMILY_6352,
3541 .name = "Marvell 88E6240",
3542 .num_databases = 4096,
3543 .num_ports = 7,
3544 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3545 },
3546
3547 [MV88E6320] = {
3548 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3549 .family = MV88E6XXX_FAMILY_6320,
3550 .name = "Marvell 88E6320",
3551 .num_databases = 4096,
3552 .num_ports = 7,
3553 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3554 },
3555
3556 [MV88E6321] = {
3557 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3558 .family = MV88E6XXX_FAMILY_6320,
3559 .name = "Marvell 88E6321",
3560 .num_databases = 4096,
3561 .num_ports = 7,
3562 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3563 },
3564
3565 [MV88E6350] = {
3566 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3567 .family = MV88E6XXX_FAMILY_6351,
3568 .name = "Marvell 88E6350",
3569 .num_databases = 4096,
3570 .num_ports = 7,
3571 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3572 },
3573
3574 [MV88E6351] = {
3575 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3576 .family = MV88E6XXX_FAMILY_6351,
3577 .name = "Marvell 88E6351",
3578 .num_databases = 4096,
3579 .num_ports = 7,
3580 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3581 },
3582
3583 [MV88E6352] = {
3584 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3585 .family = MV88E6XXX_FAMILY_6352,
3586 .name = "Marvell 88E6352",
3587 .num_databases = 4096,
3588 .num_ports = 7,
3589 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3590 },
3591};
3592
Vivien Didelotf6271e62016-04-17 13:23:59 -04003593static const struct mv88e6xxx_info *
3594mv88e6xxx_lookup_info(unsigned int prod_num, const struct mv88e6xxx_info *table,
Vivien Didelot0209d142016-04-17 13:23:55 -04003595 unsigned int num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003596{
Vivien Didelota439c062016-04-17 13:23:58 -04003597 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003598
Vivien Didelotb9b37712015-10-30 19:39:48 -04003599 for (i = 0; i < num; ++i)
Vivien Didelotf6271e62016-04-17 13:23:59 -04003600 if (table[i].prod_num == prod_num)
3601 return &table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003602
Vivien Didelotb9b37712015-10-30 19:39:48 -04003603 return NULL;
3604}
3605
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003606static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3607 struct device *host_dev, int sw_addr,
3608 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003609{
Vivien Didelotf6271e62016-04-17 13:23:59 -04003610 const struct mv88e6xxx_info *info;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003611 struct mv88e6xxx_priv_state *ps;
Vivien Didelota439c062016-04-17 13:23:58 -04003612 struct mii_bus *bus;
Vivien Didelot0209d142016-04-17 13:23:55 -04003613 const char *name;
Vivien Didelota439c062016-04-17 13:23:58 -04003614 int id, prod_num, rev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003615 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003616
Vivien Didelota439c062016-04-17 13:23:58 -04003617 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003618 if (!bus)
3619 return NULL;
3620
Vivien Didelota439c062016-04-17 13:23:58 -04003621 id = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
3622 if (id < 0)
3623 return NULL;
3624
3625 prod_num = (id & 0xfff0) >> 4;
3626 rev = id & 0x000f;
3627
Vivien Didelotf81ec902016-05-09 13:22:58 -04003628 info = mv88e6xxx_lookup_info(prod_num, mv88e6xxx_table,
3629 ARRAY_SIZE(mv88e6xxx_table));
Vivien Didelotf6271e62016-04-17 13:23:59 -04003630 if (!info)
Vivien Didelota439c062016-04-17 13:23:58 -04003631 return NULL;
3632
Vivien Didelotf6271e62016-04-17 13:23:59 -04003633 name = info->name;
3634
Vivien Didelota439c062016-04-17 13:23:58 -04003635 ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
3636 if (!ps)
3637 return NULL;
3638
3639 ps->bus = bus;
3640 ps->sw_addr = sw_addr;
Vivien Didelotf6271e62016-04-17 13:23:59 -04003641 ps->info = info;
Andrew Lunnb516d452016-06-04 21:17:06 +02003642 ps->dev = dsa_dev;
Andrew Lunnb6819572016-05-10 23:27:19 +02003643 mutex_init(&ps->smi_mutex);
Vivien Didelota439c062016-04-17 13:23:58 -04003644
Andrew Lunnb516d452016-06-04 21:17:06 +02003645 err = mv88e6xxx_mdio_register(ps, NULL);
3646 if (err)
3647 return NULL;
3648
Vivien Didelota439c062016-04-17 13:23:58 -04003649 *priv = ps;
3650
3651 dev_info(&ps->bus->dev, "switch 0x%x probed: %s, revision %u\n",
3652 prod_num, name, rev);
3653
Andrew Lunna77d43f2016-04-13 02:40:42 +02003654 return name;
3655}
3656
Vivien Didelot57d32312016-06-20 13:13:58 -04003657static struct dsa_switch_driver mv88e6xxx_switch_driver = {
Vivien Didelotf81ec902016-05-09 13:22:58 -04003658 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003659 .probe = mv88e6xxx_drv_probe,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003660 .setup = mv88e6xxx_setup,
3661 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003662 .adjust_link = mv88e6xxx_adjust_link,
3663 .get_strings = mv88e6xxx_get_strings,
3664 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3665 .get_sset_count = mv88e6xxx_get_sset_count,
3666 .set_eee = mv88e6xxx_set_eee,
3667 .get_eee = mv88e6xxx_get_eee,
3668#ifdef CONFIG_NET_DSA_HWMON
3669 .get_temp = mv88e6xxx_get_temp,
3670 .get_temp_limit = mv88e6xxx_get_temp_limit,
3671 .set_temp_limit = mv88e6xxx_set_temp_limit,
3672 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3673#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003674 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003675 .get_eeprom = mv88e6xxx_get_eeprom,
3676 .set_eeprom = mv88e6xxx_set_eeprom,
3677 .get_regs_len = mv88e6xxx_get_regs_len,
3678 .get_regs = mv88e6xxx_get_regs,
3679 .port_bridge_join = mv88e6xxx_port_bridge_join,
3680 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3681 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3682 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3683 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3684 .port_vlan_add = mv88e6xxx_port_vlan_add,
3685 .port_vlan_del = mv88e6xxx_port_vlan_del,
3686 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3687 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3688 .port_fdb_add = mv88e6xxx_port_fdb_add,
3689 .port_fdb_del = mv88e6xxx_port_fdb_del,
3690 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3691};
3692
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003693static int mv88e6xxx_register_switch(struct mv88e6xxx_priv_state *ps,
3694 struct device_node *np)
3695{
3696 struct device *dev = ps->dev;
3697 struct dsa_switch *ds;
3698
3699 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3700 if (!ds)
3701 return -ENOMEM;
3702
3703 ds->dev = dev;
3704 ds->priv = ps;
3705 ds->drv = &mv88e6xxx_switch_driver;
3706
3707 dev_set_drvdata(dev, ds);
3708
3709 return dsa_register_switch(ds, np);
3710}
3711
3712static void mv88e6xxx_unregister_switch(struct mv88e6xxx_priv_state *ps)
3713{
3714 dsa_unregister_switch(ps->ds);
3715}
3716
Vivien Didelot57d32312016-06-20 13:13:58 -04003717static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003718{
3719 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003720 struct device_node *np = dev->of_node;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003721 struct mv88e6xxx_priv_state *ps;
3722 int id, prod_num, rev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003723 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003724 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003725
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003726 ps = devm_kzalloc(dev, sizeof(*ps), GFP_KERNEL);
3727 if (!ps)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003728 return -ENOMEM;
3729
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003730 ps->dev = dev;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003731 ps->bus = mdiodev->bus;
3732 ps->sw_addr = mdiodev->addr;
3733 mutex_init(&ps->smi_mutex);
3734
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003735 id = mv88e6xxx_reg_read(ps, REG_PORT(0), PORT_SWITCH_ID);
3736 if (id < 0)
3737 return id;
3738
3739 prod_num = (id & 0xfff0) >> 4;
3740 rev = id & 0x000f;
3741
3742 ps->info = mv88e6xxx_lookup_info(prod_num, mv88e6xxx_table,
3743 ARRAY_SIZE(mv88e6xxx_table));
3744 if (!ps->info)
3745 return -ENODEV;
3746
Vivien Didelotc6d19ab2016-06-20 13:14:03 -04003747 ps->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3748 if (IS_ERR(ps->reset))
3749 return PTR_ERR(ps->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02003750
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003751 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM) &&
3752 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
3753 ps->eeprom_len = eeprom_len;
3754
Vivien Didelotaa8ac392016-06-20 13:14:00 -04003755 err = mv88e6xxx_mdio_register(ps, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003756 if (err)
3757 return err;
3758
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003759 err = mv88e6xxx_register_switch(ps, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02003760 if (err) {
3761 mv88e6xxx_mdio_unregister(ps);
3762 return err;
3763 }
3764
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003765 dev_info(dev, "switch 0x%x probed: %s, revision %u\n",
3766 prod_num, ps->info->name, rev);
3767
3768 return 0;
3769}
3770
3771static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3772{
3773 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
3774 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3775
Vivien Didelotb7e66a52016-06-20 13:14:02 -04003776 mv88e6xxx_unregister_switch(ps);
Andrew Lunnb516d452016-06-04 21:17:06 +02003777 mv88e6xxx_mdio_unregister(ps);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003778}
3779
3780static const struct of_device_id mv88e6xxx_of_match[] = {
3781 { .compatible = "marvell,mv88e6085" },
3782 { /* sentinel */ },
3783};
3784
3785MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3786
3787static struct mdio_driver mv88e6xxx_driver = {
3788 .probe = mv88e6xxx_probe,
3789 .remove = mv88e6xxx_remove,
3790 .mdiodrv.driver = {
3791 .name = "mv88e6085",
3792 .of_match_table = mv88e6xxx_of_match,
3793 },
3794};
3795
Ben Hutchings98e67302011-11-25 14:36:19 +00003796static int __init mv88e6xxx_init(void)
3797{
Vivien Didelotf81ec902016-05-09 13:22:58 -04003798 register_switch_driver(&mv88e6xxx_switch_driver);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003799 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003800}
3801module_init(mv88e6xxx_init);
3802
3803static void __exit mv88e6xxx_cleanup(void)
3804{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003805 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelotf81ec902016-05-09 13:22:58 -04003806 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003807}
3808module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003809
3810MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3811MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3812MODULE_LICENSE("GPL");