blob: b8e65e604f756d45b80b765220e38bc87c024b72 [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>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000024#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010025#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000026#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000027#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040028#include <net/switchdev.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000029#include "mv88e6xxx.h"
30
Andrew Lunn158bc062016-04-28 21:24:06 -040031static void assert_smi_lock(struct mv88e6xxx_priv_state *ps)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040032{
Vivien Didelot3996a4f2015-10-30 18:56:45 -040033 if (unlikely(!mutex_is_locked(&ps->smi_mutex))) {
Andrew Lunn158bc062016-04-28 21:24:06 -040034 dev_err(ps->dev, "SMI lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040035 dump_stack();
36 }
37}
38
Barry Grussling3675c8d2013-01-08 16:05:53 +000039/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000040 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
41 * will be directly accessible on some {device address,register address}
42 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
43 * will only respond to SMI transactions to that specific address, and
44 * an indirect addressing mechanism needs to be used to access its
45 * registers.
46 */
47static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
48{
49 int ret;
50 int i;
51
52 for (i = 0; i < 16; i++) {
Neil Armstrong6e899e62015-10-22 10:37:53 +020053 ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000054 if (ret < 0)
55 return ret;
56
Andrew Lunncca8b132015-04-02 04:06:39 +020057 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000058 return 0;
59 }
60
61 return -ETIMEDOUT;
62}
63
Vivien Didelotb9b37712015-10-30 19:39:48 -040064static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr,
65 int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000066{
67 int ret;
68
69 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +020070 return mdiobus_read_nested(bus, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071
Barry Grussling3675c8d2013-01-08 16:05:53 +000072 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000073 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
74 if (ret < 0)
75 return ret;
76
Barry Grussling3675c8d2013-01-08 16:05:53 +000077 /* Transmit the read command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020078 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
79 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000080 if (ret < 0)
81 return ret;
82
Barry Grussling3675c8d2013-01-08 16:05:53 +000083 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000084 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
85 if (ret < 0)
86 return ret;
87
Barry Grussling3675c8d2013-01-08 16:05:53 +000088 /* Read the data. */
Neil Armstrong6e899e62015-10-22 10:37:53 +020089 ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000090 if (ret < 0)
91 return ret;
92
93 return ret & 0xffff;
94}
95
Andrew Lunn158bc062016-04-28 21:24:06 -040096static int _mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps,
97 int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000098{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000099 int ret;
100
Andrew Lunn158bc062016-04-28 21:24:06 -0400101 assert_smi_lock(ps);
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400102
Andrew Lunna77d43f2016-04-13 02:40:42 +0200103 ret = __mv88e6xxx_reg_read(ps->bus, ps->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500104 if (ret < 0)
105 return ret;
106
Andrew Lunn158bc062016-04-28 21:24:06 -0400107 dev_dbg(ps->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500108 addr, reg, ret);
109
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110 return ret;
111}
112
Andrew Lunn158bc062016-04-28 21:24:06 -0400113int mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, int addr, int reg)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700114{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700115 int ret;
116
117 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -0400118 ret = _mv88e6xxx_reg_read(ps, addr, reg);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700119 mutex_unlock(&ps->smi_mutex);
120
121 return ret;
122}
123
Vivien Didelotb9b37712015-10-30 19:39:48 -0400124static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
125 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000126{
127 int ret;
128
129 if (sw_addr == 0)
Neil Armstrong6e899e62015-10-22 10:37:53 +0200130 return mdiobus_write_nested(bus, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000131
Barry Grussling3675c8d2013-01-08 16:05:53 +0000132 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000133 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
134 if (ret < 0)
135 return ret;
136
Barry Grussling3675c8d2013-01-08 16:05:53 +0000137 /* Transmit the data to write. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200138 ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000139 if (ret < 0)
140 return ret;
141
Barry Grussling3675c8d2013-01-08 16:05:53 +0000142 /* Transmit the write command. */
Neil Armstrong6e899e62015-10-22 10:37:53 +0200143 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
144 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000145 if (ret < 0)
146 return ret;
147
Barry Grussling3675c8d2013-01-08 16:05:53 +0000148 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000149 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
150 if (ret < 0)
151 return ret;
152
153 return 0;
154}
155
Andrew Lunn158bc062016-04-28 21:24:06 -0400156static int _mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
157 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000158{
Andrew Lunn158bc062016-04-28 21:24:06 -0400159 assert_smi_lock(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000160
Andrew Lunn158bc062016-04-28 21:24:06 -0400161 dev_dbg(ps->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500162 addr, reg, val);
163
Andrew Lunna77d43f2016-04-13 02:40:42 +0200164 return __mv88e6xxx_reg_write(ps->bus, ps->sw_addr, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700165}
166
Andrew Lunn158bc062016-04-28 21:24:06 -0400167int mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
168 int reg, u16 val)
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700169{
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700170 int ret;
171
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000172 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -0400173 ret = _mv88e6xxx_reg_write(ps, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000174 mutex_unlock(&ps->smi_mutex);
175
176 return ret;
177}
178
Vivien Didelot1d13a062016-05-09 13:22:43 -0400179static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000180{
Andrew Lunn158bc062016-04-28 21:24:06 -0400181 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200182 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000183
Andrew Lunn158bc062016-04-28 21:24:06 -0400184 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_01,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200185 (addr[0] << 8) | addr[1]);
186 if (err)
187 return err;
188
Andrew Lunn158bc062016-04-28 21:24:06 -0400189 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_23,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200190 (addr[2] << 8) | addr[3]);
191 if (err)
192 return err;
193
Andrew Lunn158bc062016-04-28 21:24:06 -0400194 return mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_45,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200195 (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000196}
197
Vivien Didelot1d13a062016-05-09 13:22:43 -0400198static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000199{
Andrew Lunn158bc062016-04-28 21:24:06 -0400200 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000201 int ret;
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200202 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000203
204 for (i = 0; i < 6; i++) {
205 int j;
206
Barry Grussling3675c8d2013-01-08 16:05:53 +0000207 /* Write the MAC address byte. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400208 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200209 GLOBAL2_SWITCH_MAC_BUSY |
210 (i << 8) | addr[i]);
211 if (ret)
212 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000213
Barry Grussling3675c8d2013-01-08 16:05:53 +0000214 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000215 for (j = 0; j < 16; j++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400216 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2,
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200217 GLOBAL2_SWITCH_MAC);
218 if (ret < 0)
219 return ret;
220
Andrew Lunncca8b132015-04-02 04:06:39 +0200221 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000222 break;
223 }
224 if (j == 16)
225 return -ETIMEDOUT;
226 }
227
228 return 0;
229}
230
Vivien Didelot1d13a062016-05-09 13:22:43 -0400231int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
232{
233 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
234
235 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SWITCH_MAC))
236 return mv88e6xxx_set_addr_indirect(ds, addr);
237 else
238 return mv88e6xxx_set_addr_direct(ds, addr);
239}
240
Andrew Lunn03a4a542016-06-04 21:17:05 +0200241static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_priv_state *ps,
242 int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000243{
244 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400245 return _mv88e6xxx_reg_read(ps, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000246 return 0xffff;
247}
248
Andrew Lunn03a4a542016-06-04 21:17:05 +0200249static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_priv_state *ps,
250 int addr, int regnum, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000251{
252 if (addr >= 0)
Andrew Lunn158bc062016-04-28 21:24:06 -0400253 return _mv88e6xxx_reg_write(ps, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000254 return 0;
255}
256
Andrew Lunn158bc062016-04-28 21:24:06 -0400257static int mv88e6xxx_ppu_disable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000258{
259 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000260 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000261
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400262 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200263 if (ret < 0)
264 return ret;
265
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400266 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
267 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200268 if (ret)
269 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000270
Barry Grussling19b2f972013-01-08 16:05:54 +0000271 timeout = jiffies + 1 * HZ;
272 while (time_before(jiffies, timeout)) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400273 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200274 if (ret < 0)
275 return ret;
276
Barry Grussling19b2f972013-01-08 16:05:54 +0000277 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200278 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
279 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000280 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000281 }
282
283 return -ETIMEDOUT;
284}
285
Andrew Lunn158bc062016-04-28 21:24:06 -0400286static int mv88e6xxx_ppu_enable(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000287{
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200288 int ret, err;
Barry Grussling19b2f972013-01-08 16:05:54 +0000289 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000290
Vivien Didelot762eb672016-06-04 21:16:54 +0200291 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200292 if (ret < 0)
293 return ret;
294
Vivien Didelot762eb672016-06-04 21:16:54 +0200295 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
296 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200297 if (err)
298 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000299
Barry Grussling19b2f972013-01-08 16:05:54 +0000300 timeout = jiffies + 1 * HZ;
301 while (time_before(jiffies, timeout)) {
Vivien Didelot762eb672016-06-04 21:16:54 +0200302 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200303 if (ret < 0)
304 return ret;
305
Barry Grussling19b2f972013-01-08 16:05:54 +0000306 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200307 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
308 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000309 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000310 }
311
312 return -ETIMEDOUT;
313}
314
315static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
316{
317 struct mv88e6xxx_priv_state *ps;
318
319 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200320
321 mutex_lock(&ps->smi_mutex);
322
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000323 if (mutex_trylock(&ps->ppu_mutex)) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400324 if (mv88e6xxx_ppu_enable(ps) == 0)
Barry Grussling85686582013-01-08 16:05:56 +0000325 ps->ppu_disabled = 0;
326 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000327 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200328
329 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000330}
331
332static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
333{
334 struct mv88e6xxx_priv_state *ps = (void *)_ps;
335
336 schedule_work(&ps->ppu_work);
337}
338
Andrew Lunn158bc062016-04-28 21:24:06 -0400339static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000340{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000341 int ret;
342
343 mutex_lock(&ps->ppu_mutex);
344
Barry Grussling3675c8d2013-01-08 16:05:53 +0000345 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000346 * we can access the PHY registers. If it was already
347 * disabled, cancel the timer that is going to re-enable
348 * it.
349 */
350 if (!ps->ppu_disabled) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400351 ret = mv88e6xxx_ppu_disable(ps);
Barry Grussling85686582013-01-08 16:05:56 +0000352 if (ret < 0) {
353 mutex_unlock(&ps->ppu_mutex);
354 return ret;
355 }
356 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000357 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000358 del_timer(&ps->ppu_timer);
359 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000360 }
361
362 return ret;
363}
364
Andrew Lunn158bc062016-04-28 21:24:06 -0400365static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000366{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000367 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000368 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
369 mutex_unlock(&ps->ppu_mutex);
370}
371
Andrew Lunn158bc062016-04-28 21:24:06 -0400372void mv88e6xxx_ppu_state_init(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000373{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000374 mutex_init(&ps->ppu_mutex);
375 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
376 init_timer(&ps->ppu_timer);
377 ps->ppu_timer.data = (unsigned long)ps;
378 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
379}
380
Andrew Lunn03a4a542016-06-04 21:17:05 +0200381static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_priv_state *ps, int addr,
382 int regnum)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000383{
384 int ret;
385
Andrew Lunn158bc062016-04-28 21:24:06 -0400386 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000387 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400388 ret = _mv88e6xxx_reg_read(ps, addr, regnum);
Andrew Lunn158bc062016-04-28 21:24:06 -0400389 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000390 }
391
392 return ret;
393}
394
Andrew Lunn03a4a542016-06-04 21:17:05 +0200395static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_priv_state *ps, int addr,
396 int regnum, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000397{
398 int ret;
399
Andrew Lunn158bc062016-04-28 21:24:06 -0400400 ret = mv88e6xxx_ppu_access_get(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000401 if (ret >= 0) {
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400402 ret = _mv88e6xxx_reg_write(ps, addr, regnum, val);
Andrew Lunn158bc062016-04-28 21:24:06 -0400403 mv88e6xxx_ppu_access_put(ps);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000404 }
405
406 return ret;
407}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000408
Andrew Lunn158bc062016-04-28 21:24:06 -0400409static bool mv88e6xxx_6065_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200410{
Vivien Didelot22356472016-04-17 13:24:00 -0400411 return ps->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200412}
413
Andrew Lunn158bc062016-04-28 21:24:06 -0400414static bool mv88e6xxx_6095_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200415{
Vivien Didelot22356472016-04-17 13:24:00 -0400416 return ps->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200417}
418
Andrew Lunn158bc062016-04-28 21:24:06 -0400419static bool mv88e6xxx_6097_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200420{
Vivien Didelot22356472016-04-17 13:24:00 -0400421 return ps->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200422}
423
Andrew Lunn158bc062016-04-28 21:24:06 -0400424static bool mv88e6xxx_6165_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200425{
Vivien Didelot22356472016-04-17 13:24:00 -0400426 return ps->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200427}
428
Andrew Lunn158bc062016-04-28 21:24:06 -0400429static bool mv88e6xxx_6185_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200430{
Vivien Didelot22356472016-04-17 13:24:00 -0400431 return ps->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200432}
433
Andrew Lunn158bc062016-04-28 21:24:06 -0400434static bool mv88e6xxx_6320_family(struct mv88e6xxx_priv_state *ps)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700435{
Vivien Didelot22356472016-04-17 13:24:00 -0400436 return ps->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700437}
438
Andrew Lunn158bc062016-04-28 21:24:06 -0400439static bool mv88e6xxx_6351_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200440{
Vivien Didelot22356472016-04-17 13:24:00 -0400441 return ps->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200442}
443
Andrew Lunn158bc062016-04-28 21:24:06 -0400444static bool mv88e6xxx_6352_family(struct mv88e6xxx_priv_state *ps)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200445{
Vivien Didelot22356472016-04-17 13:24:00 -0400446 return ps->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200447}
448
Andrew Lunn158bc062016-04-28 21:24:06 -0400449static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_priv_state *ps)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400450{
Vivien Didelotcd5a2c82016-04-17 13:24:02 -0400451 return ps->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400452}
453
Andrew Lunn158bc062016-04-28 21:24:06 -0400454static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_priv_state *ps)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400455{
456 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Andrew Lunn158bc062016-04-28 21:24:06 -0400457 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
458 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400459 return true;
460
461 return false;
462}
463
Andrew Lunndea87022015-08-31 15:56:47 +0200464/* We expect the switch to perform auto negotiation if there is a real
465 * phy. However, in the case of a fixed link phy, we force the port
466 * settings from the fixed link settings.
467 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400468static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
469 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200470{
471 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200472 u32 reg;
473 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200474
475 if (!phy_is_pseudo_fixed_link(phydev))
476 return;
477
478 mutex_lock(&ps->smi_mutex);
479
Andrew Lunn158bc062016-04-28 21:24:06 -0400480 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200481 if (ret < 0)
482 goto out;
483
484 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
485 PORT_PCS_CTRL_FORCE_LINK |
486 PORT_PCS_CTRL_DUPLEX_FULL |
487 PORT_PCS_CTRL_FORCE_DUPLEX |
488 PORT_PCS_CTRL_UNFORCED);
489
490 reg |= PORT_PCS_CTRL_FORCE_LINK;
491 if (phydev->link)
492 reg |= PORT_PCS_CTRL_LINK_UP;
493
Andrew Lunn158bc062016-04-28 21:24:06 -0400494 if (mv88e6xxx_6065_family(ps) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200495 goto out;
496
497 switch (phydev->speed) {
498 case SPEED_1000:
499 reg |= PORT_PCS_CTRL_1000;
500 break;
501 case SPEED_100:
502 reg |= PORT_PCS_CTRL_100;
503 break;
504 case SPEED_10:
505 reg |= PORT_PCS_CTRL_10;
506 break;
507 default:
508 pr_info("Unknown speed");
509 goto out;
510 }
511
512 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
513 if (phydev->duplex == DUPLEX_FULL)
514 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
515
Andrew Lunn158bc062016-04-28 21:24:06 -0400516 if ((mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps)) &&
Vivien Didelot009a2b92016-04-17 13:24:01 -0400517 (port >= ps->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200518 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
519 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
520 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
521 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
522 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
523 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
524 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
525 }
Andrew Lunn158bc062016-04-28 21:24:06 -0400526 _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200527
528out:
529 mutex_unlock(&ps->smi_mutex);
530}
531
Andrew Lunn158bc062016-04-28 21:24:06 -0400532static int _mv88e6xxx_stats_wait(struct mv88e6xxx_priv_state *ps)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000533{
534 int ret;
535 int i;
536
537 for (i = 0; i < 10; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400538 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200539 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000540 return 0;
541 }
542
543 return -ETIMEDOUT;
544}
545
Andrew Lunn158bc062016-04-28 21:24:06 -0400546static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_priv_state *ps,
547 int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000548{
549 int ret;
550
Andrew Lunn158bc062016-04-28 21:24:06 -0400551 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200552 port = (port + 1) << 5;
553
Barry Grussling3675c8d2013-01-08 16:05:53 +0000554 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400555 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200556 GLOBAL_STATS_OP_CAPTURE_PORT |
557 GLOBAL_STATS_OP_HIST_RX_TX | port);
558 if (ret < 0)
559 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000560
Barry Grussling3675c8d2013-01-08 16:05:53 +0000561 /* Wait for the snapshotting to complete. */
Andrew Lunn158bc062016-04-28 21:24:06 -0400562 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000563 if (ret < 0)
564 return ret;
565
566 return 0;
567}
568
Andrew Lunn158bc062016-04-28 21:24:06 -0400569static void _mv88e6xxx_stats_read(struct mv88e6xxx_priv_state *ps,
570 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000571{
572 u32 _val;
573 int ret;
574
575 *val = 0;
576
Andrew Lunn158bc062016-04-28 21:24:06 -0400577 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200578 GLOBAL_STATS_OP_READ_CAPTURED |
579 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000580 if (ret < 0)
581 return;
582
Andrew Lunn158bc062016-04-28 21:24:06 -0400583 ret = _mv88e6xxx_stats_wait(ps);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000584 if (ret < 0)
585 return;
586
Andrew Lunn158bc062016-04-28 21:24:06 -0400587 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000588 if (ret < 0)
589 return;
590
591 _val = ret << 16;
592
Andrew Lunn158bc062016-04-28 21:24:06 -0400593 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000594 if (ret < 0)
595 return;
596
597 *val = _val | ret;
598}
599
Andrew Lunne413e7e2015-04-02 04:06:38 +0200600static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100601 { "in_good_octets", 8, 0x00, BANK0, },
602 { "in_bad_octets", 4, 0x02, BANK0, },
603 { "in_unicast", 4, 0x04, BANK0, },
604 { "in_broadcasts", 4, 0x06, BANK0, },
605 { "in_multicasts", 4, 0x07, BANK0, },
606 { "in_pause", 4, 0x16, BANK0, },
607 { "in_undersize", 4, 0x18, BANK0, },
608 { "in_fragments", 4, 0x19, BANK0, },
609 { "in_oversize", 4, 0x1a, BANK0, },
610 { "in_jabber", 4, 0x1b, BANK0, },
611 { "in_rx_error", 4, 0x1c, BANK0, },
612 { "in_fcs_error", 4, 0x1d, BANK0, },
613 { "out_octets", 8, 0x0e, BANK0, },
614 { "out_unicast", 4, 0x10, BANK0, },
615 { "out_broadcasts", 4, 0x13, BANK0, },
616 { "out_multicasts", 4, 0x12, BANK0, },
617 { "out_pause", 4, 0x15, BANK0, },
618 { "excessive", 4, 0x11, BANK0, },
619 { "collisions", 4, 0x1e, BANK0, },
620 { "deferred", 4, 0x05, BANK0, },
621 { "single", 4, 0x14, BANK0, },
622 { "multiple", 4, 0x17, BANK0, },
623 { "out_fcs_error", 4, 0x03, BANK0, },
624 { "late", 4, 0x1f, BANK0, },
625 { "hist_64bytes", 4, 0x08, BANK0, },
626 { "hist_65_127bytes", 4, 0x09, BANK0, },
627 { "hist_128_255bytes", 4, 0x0a, BANK0, },
628 { "hist_256_511bytes", 4, 0x0b, BANK0, },
629 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
630 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
631 { "sw_in_discards", 4, 0x10, PORT, },
632 { "sw_in_filtered", 2, 0x12, PORT, },
633 { "sw_out_filtered", 2, 0x13, PORT, },
634 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
635 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
636 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
637 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
638 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
639 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
640 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
641 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
642 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
643 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
644 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
645 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
646 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
647 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
648 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
649 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
650 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
651 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
652 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
653 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
654 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
655 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
656 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
657 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
658 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
659 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200660};
661
Andrew Lunn158bc062016-04-28 21:24:06 -0400662static bool mv88e6xxx_has_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100663 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200664{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100665 switch (stat->type) {
666 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200667 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100668 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400669 return mv88e6xxx_6320_family(ps);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100670 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400671 return mv88e6xxx_6095_family(ps) ||
672 mv88e6xxx_6185_family(ps) ||
673 mv88e6xxx_6097_family(ps) ||
674 mv88e6xxx_6165_family(ps) ||
675 mv88e6xxx_6351_family(ps) ||
676 mv88e6xxx_6352_family(ps);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200677 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100678 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000679}
680
Andrew Lunn158bc062016-04-28 21:24:06 -0400681static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_priv_state *ps,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100682 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200683 int port)
684{
Andrew Lunn80c46272015-06-20 18:42:30 +0200685 u32 low;
686 u32 high = 0;
687 int ret;
688 u64 value;
689
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100690 switch (s->type) {
691 case PORT:
Andrew Lunn158bc062016-04-28 21:24:06 -0400692 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200693 if (ret < 0)
694 return UINT64_MAX;
695
696 low = ret;
697 if (s->sizeof_stat == 4) {
Andrew Lunn158bc062016-04-28 21:24:06 -0400698 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100699 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200700 if (ret < 0)
701 return UINT64_MAX;
702 high = ret;
703 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100704 break;
705 case BANK0:
706 case BANK1:
Andrew Lunn158bc062016-04-28 21:24:06 -0400707 _mv88e6xxx_stats_read(ps, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200708 if (s->sizeof_stat == 8)
Andrew Lunn158bc062016-04-28 21:24:06 -0400709 _mv88e6xxx_stats_read(ps, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200710 }
711 value = (((u64)high) << 16) | low;
712 return value;
713}
714
Vivien Didelotf81ec902016-05-09 13:22:58 -0400715static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
716 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100717{
Andrew Lunn158bc062016-04-28 21:24:06 -0400718 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100719 struct mv88e6xxx_hw_stat *stat;
720 int i, j;
721
722 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
723 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400724 if (mv88e6xxx_has_stat(ps, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100725 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
726 ETH_GSTRING_LEN);
727 j++;
728 }
729 }
730}
731
Vivien Didelotf81ec902016-05-09 13:22:58 -0400732static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100733{
Andrew Lunn158bc062016-04-28 21:24:06 -0400734 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100735 struct mv88e6xxx_hw_stat *stat;
736 int i, j;
737
738 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
739 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400740 if (mv88e6xxx_has_stat(ps, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100741 j++;
742 }
743 return j;
744}
745
Vivien Didelotf81ec902016-05-09 13:22:58 -0400746static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
747 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000748{
Florian Fainellia22adce2014-04-28 11:14:28 -0700749 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100750 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000751 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100752 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000753
Andrew Lunn31888232015-05-06 01:09:54 +0200754 mutex_lock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000755
Andrew Lunn158bc062016-04-28 21:24:06 -0400756 ret = _mv88e6xxx_stats_snapshot(ps, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000757 if (ret < 0) {
Andrew Lunn31888232015-05-06 01:09:54 +0200758 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000759 return;
760 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100761 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
762 stat = &mv88e6xxx_hw_stats[i];
Andrew Lunn158bc062016-04-28 21:24:06 -0400763 if (mv88e6xxx_has_stat(ps, stat)) {
764 data[j] = _mv88e6xxx_get_ethtool_stat(ps, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100765 j++;
766 }
767 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000768
Andrew Lunn31888232015-05-06 01:09:54 +0200769 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000770}
Ben Hutchings98e67302011-11-25 14:36:19 +0000771
Vivien Didelotf81ec902016-05-09 13:22:58 -0400772static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700773{
774 return 32 * sizeof(u16);
775}
776
Vivien Didelotf81ec902016-05-09 13:22:58 -0400777static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
778 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700779{
Andrew Lunn158bc062016-04-28 21:24:06 -0400780 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700781 u16 *p = _p;
782 int i;
783
784 regs->version = 0;
785
786 memset(p, 0xff, 32 * sizeof(u16));
787
Vivien Didelot23062512016-05-09 13:22:45 -0400788 mutex_lock(&ps->smi_mutex);
789
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700790 for (i = 0; i < 32; i++) {
791 int ret;
792
Vivien Didelot23062512016-05-09 13:22:45 -0400793 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700794 if (ret >= 0)
795 p[i] = ret;
796 }
Vivien Didelot23062512016-05-09 13:22:45 -0400797
798 mutex_unlock(&ps->smi_mutex);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700799}
800
Andrew Lunn158bc062016-04-28 21:24:06 -0400801static int _mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, int offset,
Andrew Lunn3898c142015-05-06 01:09:53 +0200802 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700803{
804 unsigned long timeout = jiffies + HZ / 10;
805
806 while (time_before(jiffies, timeout)) {
807 int ret;
808
Andrew Lunn158bc062016-04-28 21:24:06 -0400809 ret = _mv88e6xxx_reg_read(ps, reg, offset);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700810 if (ret < 0)
811 return ret;
812 if (!(ret & mask))
813 return 0;
814
815 usleep_range(1000, 2000);
816 }
817 return -ETIMEDOUT;
818}
819
Andrew Lunn158bc062016-04-28 21:24:06 -0400820static int mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg,
821 int offset, u16 mask)
Andrew Lunn3898c142015-05-06 01:09:53 +0200822{
Andrew Lunn3898c142015-05-06 01:09:53 +0200823 int ret;
824
825 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -0400826 ret = _mv88e6xxx_wait(ps, reg, offset, mask);
Andrew Lunn3898c142015-05-06 01:09:53 +0200827 mutex_unlock(&ps->smi_mutex);
828
829 return ret;
830}
831
Andrew Lunn03a4a542016-06-04 21:17:05 +0200832static int mv88e6xxx_mdio_wait(struct mv88e6xxx_priv_state *ps)
Andrew Lunn3898c142015-05-06 01:09:53 +0200833{
Andrew Lunn158bc062016-04-28 21:24:06 -0400834 return _mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200835 GLOBAL2_SMI_OP_BUSY);
836}
837
Vivien Didelotd24645b2016-05-09 13:22:41 -0400838static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200839{
Andrew Lunn158bc062016-04-28 21:24:06 -0400840 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
841
842 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200843 GLOBAL2_EEPROM_OP_LOAD);
844}
845
Vivien Didelotd24645b2016-05-09 13:22:41 -0400846static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
Andrew Lunn3898c142015-05-06 01:09:53 +0200847{
Andrew Lunn158bc062016-04-28 21:24:06 -0400848 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
849
850 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +0200851 GLOBAL2_EEPROM_OP_BUSY);
852}
853
Vivien Didelotd24645b2016-05-09 13:22:41 -0400854static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
855{
856 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
857 int ret;
858
859 mutex_lock(&ps->eeprom_mutex);
860
861 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
862 GLOBAL2_EEPROM_OP_READ |
863 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
864 if (ret < 0)
865 goto error;
866
867 ret = mv88e6xxx_eeprom_busy_wait(ds);
868 if (ret < 0)
869 goto error;
870
871 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
872error:
873 mutex_unlock(&ps->eeprom_mutex);
874 return ret;
875}
876
Andrew Lunnf8cd8752016-05-10 23:27:25 +0200877static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
878{
879 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
880
881 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
882 return ps->eeprom_len;
883
884 return 0;
885}
886
Vivien Didelotf81ec902016-05-09 13:22:58 -0400887static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
888 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400889{
890 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
891 int offset;
892 int len;
893 int ret;
894
895 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
896 return -EOPNOTSUPP;
897
898 offset = eeprom->offset;
899 len = eeprom->len;
900 eeprom->len = 0;
901
902 eeprom->magic = 0xc3ec4951;
903
904 ret = mv88e6xxx_eeprom_load_wait(ds);
905 if (ret < 0)
906 return ret;
907
908 if (offset & 1) {
909 int word;
910
911 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
912 if (word < 0)
913 return word;
914
915 *data++ = (word >> 8) & 0xff;
916
917 offset++;
918 len--;
919 eeprom->len++;
920 }
921
922 while (len >= 2) {
923 int word;
924
925 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
926 if (word < 0)
927 return word;
928
929 *data++ = word & 0xff;
930 *data++ = (word >> 8) & 0xff;
931
932 offset += 2;
933 len -= 2;
934 eeprom->len += 2;
935 }
936
937 if (len) {
938 int word;
939
940 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
941 if (word < 0)
942 return word;
943
944 *data++ = word & 0xff;
945
946 offset++;
947 len--;
948 eeprom->len++;
949 }
950
951 return 0;
952}
953
954static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
955{
956 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
957 int ret;
958
959 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
960 if (ret < 0)
961 return ret;
962
963 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
964 return -EROFS;
965
966 return 0;
967}
968
969static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
970 u16 data)
971{
972 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
973 int ret;
974
975 mutex_lock(&ps->eeprom_mutex);
976
977 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
978 if (ret < 0)
979 goto error;
980
981 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
982 GLOBAL2_EEPROM_OP_WRITE |
983 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
984 if (ret < 0)
985 goto error;
986
987 ret = mv88e6xxx_eeprom_busy_wait(ds);
988error:
989 mutex_unlock(&ps->eeprom_mutex);
990 return ret;
991}
992
Vivien Didelotf81ec902016-05-09 13:22:58 -0400993static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
994 struct ethtool_eeprom *eeprom, u8 *data)
Vivien Didelotd24645b2016-05-09 13:22:41 -0400995{
996 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
997 int offset;
998 int ret;
999 int len;
1000
1001 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
1002 return -EOPNOTSUPP;
1003
1004 if (eeprom->magic != 0xc3ec4951)
1005 return -EINVAL;
1006
1007 ret = mv88e6xxx_eeprom_is_readonly(ds);
1008 if (ret)
1009 return ret;
1010
1011 offset = eeprom->offset;
1012 len = eeprom->len;
1013 eeprom->len = 0;
1014
1015 ret = mv88e6xxx_eeprom_load_wait(ds);
1016 if (ret < 0)
1017 return ret;
1018
1019 if (offset & 1) {
1020 int word;
1021
1022 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1023 if (word < 0)
1024 return word;
1025
1026 word = (*data++ << 8) | (word & 0xff);
1027
1028 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1029 if (ret < 0)
1030 return ret;
1031
1032 offset++;
1033 len--;
1034 eeprom->len++;
1035 }
1036
1037 while (len >= 2) {
1038 int word;
1039
1040 word = *data++;
1041 word |= *data++ << 8;
1042
1043 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1044 if (ret < 0)
1045 return ret;
1046
1047 offset += 2;
1048 len -= 2;
1049 eeprom->len += 2;
1050 }
1051
1052 if (len) {
1053 int word;
1054
1055 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1056 if (word < 0)
1057 return word;
1058
1059 word = (word & 0xff00) | *data++;
1060
1061 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1062 if (ret < 0)
1063 return ret;
1064
1065 offset++;
1066 len--;
1067 eeprom->len++;
1068 }
1069
1070 return 0;
1071}
1072
Andrew Lunn158bc062016-04-28 21:24:06 -04001073static int _mv88e6xxx_atu_wait(struct mv88e6xxx_priv_state *ps)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001074{
Andrew Lunn158bc062016-04-28 21:24:06 -04001075 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_ATU_OP,
Andrew Lunncca8b132015-04-02 04:06:39 +02001076 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001077}
1078
Andrew Lunn03a4a542016-06-04 21:17:05 +02001079static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_priv_state *ps,
Andrew Lunn158bc062016-04-28 21:24:06 -04001080 int addr, int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +01001081{
1082 int ret;
1083
Andrew Lunn158bc062016-04-28 21:24:06 -04001084 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001085 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1086 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +01001087 if (ret < 0)
1088 return ret;
1089
Andrew Lunn03a4a542016-06-04 21:17:05 +02001090 ret = mv88e6xxx_mdio_wait(ps);
Andrew Lunn3898c142015-05-06 01:09:53 +02001091 if (ret < 0)
1092 return ret;
1093
Andrew Lunn158bc062016-04-28 21:24:06 -04001094 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA);
1095
1096 return ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001097}
1098
Andrew Lunn03a4a542016-06-04 21:17:05 +02001099static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_priv_state *ps,
Andrew Lunn158bc062016-04-28 21:24:06 -04001100 int addr, int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +01001101{
Andrew Lunn3898c142015-05-06 01:09:53 +02001102 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +01001103
Andrew Lunn158bc062016-04-28 21:24:06 -04001104 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02001105 if (ret < 0)
1106 return ret;
1107
Andrew Lunn158bc062016-04-28 21:24:06 -04001108 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
Andrew Lunn3898c142015-05-06 01:09:53 +02001109 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1110 regnum);
1111
Andrew Lunn03a4a542016-06-04 21:17:05 +02001112 return mv88e6xxx_mdio_wait(ps);
Andrew Lunnf3044682015-02-14 19:17:50 +01001113}
1114
Vivien Didelotf81ec902016-05-09 13:22:58 -04001115static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1116 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001117{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001118 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001119 int reg;
1120
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001121 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1122 return -EOPNOTSUPP;
1123
Andrew Lunn3898c142015-05-06 01:09:53 +02001124 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001125
Andrew Lunn03a4a542016-06-04 21:17:05 +02001126 reg = mv88e6xxx_mdio_read_indirect(ps, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001127 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001128 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001129
1130 e->eee_enabled = !!(reg & 0x0200);
1131 e->tx_lpi_enabled = !!(reg & 0x0100);
1132
Andrew Lunn158bc062016-04-28 21:24:06 -04001133 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001134 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +02001135 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001136
Andrew Lunncca8b132015-04-02 04:06:39 +02001137 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001138 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001139
Andrew Lunn2f40c692015-04-02 04:06:37 +02001140out:
Andrew Lunn3898c142015-05-06 01:09:53 +02001141 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001142 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001143}
1144
Vivien Didelotf81ec902016-05-09 13:22:58 -04001145static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1146 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -08001147{
Andrew Lunn2f40c692015-04-02 04:06:37 +02001148 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1149 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001150 int ret;
1151
Vivien Didelotaadbdb82016-05-09 13:22:44 -04001152 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1153 return -EOPNOTSUPP;
1154
Andrew Lunn3898c142015-05-06 01:09:53 +02001155 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -08001156
Andrew Lunn03a4a542016-06-04 21:17:05 +02001157 ret = mv88e6xxx_mdio_read_indirect(ps, port, 16);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001158 if (ret < 0)
1159 goto out;
1160
1161 reg = ret & ~0x0300;
1162 if (e->eee_enabled)
1163 reg |= 0x0200;
1164 if (e->tx_lpi_enabled)
1165 reg |= 0x0100;
1166
Andrew Lunn03a4a542016-06-04 21:17:05 +02001167 ret = mv88e6xxx_mdio_write_indirect(ps, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001168out:
Andrew Lunn3898c142015-05-06 01:09:53 +02001169 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +02001170
1171 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -08001172}
1173
Andrew Lunn158bc062016-04-28 21:24:06 -04001174static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_priv_state *ps, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001175{
1176 int ret;
1177
Andrew Lunn158bc062016-04-28 21:24:06 -04001178 if (mv88e6xxx_has_fid_reg(ps)) {
1179 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_FID, fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001180 if (ret < 0)
1181 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001182 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001183 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001184 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -04001185 if (ret < 0)
1186 return ret;
1187
Andrew Lunn158bc062016-04-28 21:24:06 -04001188 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001189 (ret & 0xfff) |
1190 ((fid << 8) & 0xf000));
1191 if (ret < 0)
1192 return ret;
1193
1194 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1195 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001196 }
1197
Andrew Lunn158bc062016-04-28 21:24:06 -04001198 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001199 if (ret < 0)
1200 return ret;
1201
Andrew Lunn158bc062016-04-28 21:24:06 -04001202 return _mv88e6xxx_atu_wait(ps);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001203}
1204
Andrew Lunn158bc062016-04-28 21:24:06 -04001205static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot37705b72015-09-04 14:34:11 -04001206 struct mv88e6xxx_atu_entry *entry)
1207{
1208 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1209
1210 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1211 unsigned int mask, shift;
1212
1213 if (entry->trunk) {
1214 data |= GLOBAL_ATU_DATA_TRUNK;
1215 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1216 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1217 } else {
1218 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1219 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1220 }
1221
1222 data |= (entry->portv_trunkid << shift) & mask;
1223 }
1224
Andrew Lunn158bc062016-04-28 21:24:06 -04001225 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001226}
1227
Andrew Lunn158bc062016-04-28 21:24:06 -04001228static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001229 struct mv88e6xxx_atu_entry *entry,
1230 bool static_too)
1231{
1232 int op;
1233 int err;
1234
Andrew Lunn158bc062016-04-28 21:24:06 -04001235 err = _mv88e6xxx_atu_wait(ps);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001236 if (err)
1237 return err;
1238
Andrew Lunn158bc062016-04-28 21:24:06 -04001239 err = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001240 if (err)
1241 return err;
1242
1243 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001244 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1245 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1246 } else {
1247 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1248 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1249 }
1250
Andrew Lunn158bc062016-04-28 21:24:06 -04001251 return _mv88e6xxx_atu_cmd(ps, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001252}
1253
Andrew Lunn158bc062016-04-28 21:24:06 -04001254static int _mv88e6xxx_atu_flush(struct mv88e6xxx_priv_state *ps,
1255 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001256{
1257 struct mv88e6xxx_atu_entry entry = {
1258 .fid = fid,
1259 .state = 0, /* EntryState bits must be 0 */
1260 };
1261
Andrew Lunn158bc062016-04-28 21:24:06 -04001262 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001263}
1264
Andrew Lunn158bc062016-04-28 21:24:06 -04001265static int _mv88e6xxx_atu_move(struct mv88e6xxx_priv_state *ps, u16 fid,
1266 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001267{
1268 struct mv88e6xxx_atu_entry entry = {
1269 .trunk = false,
1270 .fid = fid,
1271 };
1272
1273 /* EntryState bits must be 0xF */
1274 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1275
1276 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1277 entry.portv_trunkid = (to_port & 0x0f) << 4;
1278 entry.portv_trunkid |= from_port & 0x0f;
1279
Andrew Lunn158bc062016-04-28 21:24:06 -04001280 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001281}
1282
Andrew Lunn158bc062016-04-28 21:24:06 -04001283static int _mv88e6xxx_atu_remove(struct mv88e6xxx_priv_state *ps, u16 fid,
1284 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001285{
1286 /* Destination port 0xF means remove the entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001287 return _mv88e6xxx_atu_move(ps, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001288}
1289
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001290static const char * const mv88e6xxx_port_state_names[] = {
1291 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1292 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1293 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1294 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1295};
1296
Andrew Lunn158bc062016-04-28 21:24:06 -04001297static int _mv88e6xxx_port_state(struct mv88e6xxx_priv_state *ps, int port,
1298 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001299{
Andrew Lunn158bc062016-04-28 21:24:06 -04001300 struct dsa_switch *ds = ps->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001301 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001302 u8 oldstate;
1303
Andrew Lunn158bc062016-04-28 21:24:06 -04001304 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001305 if (reg < 0)
1306 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001307
Andrew Lunncca8b132015-04-02 04:06:39 +02001308 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001309
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001310 if (oldstate != state) {
1311 /* Flush forwarding database if we're moving a port
1312 * from Learning or Forwarding state to Disabled or
1313 * Blocking or Listening state.
1314 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001315 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1316 oldstate == PORT_CONTROL_STATE_FORWARDING)
1317 && (state == PORT_CONTROL_STATE_DISABLED ||
1318 state == PORT_CONTROL_STATE_BLOCKING)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001319 ret = _mv88e6xxx_atu_remove(ps, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001320 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001321 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001322 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001323
Andrew Lunncca8b132015-04-02 04:06:39 +02001324 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Andrew Lunn158bc062016-04-28 21:24:06 -04001325 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001326 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001327 if (ret)
1328 return ret;
1329
Andrew Lunnc8b09802016-06-04 21:16:57 +02001330 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001331 mv88e6xxx_port_state_names[state],
1332 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001333 }
1334
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001335 return ret;
1336}
1337
Andrew Lunn158bc062016-04-28 21:24:06 -04001338static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_priv_state *ps,
1339 int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001340{
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001341 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot009a2b92016-04-17 13:24:01 -04001342 const u16 mask = (1 << ps->info->num_ports) - 1;
Andrew Lunn158bc062016-04-28 21:24:06 -04001343 struct dsa_switch *ds = ps->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001344 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001345 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001346 int i;
1347
1348 /* allow CPU port or DSA link(s) to send frames to every port */
1349 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1350 output_ports = mask;
1351 } else {
Vivien Didelot009a2b92016-04-17 13:24:01 -04001352 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001353 /* allow sending frames to every group member */
1354 if (bridge && ps->ports[i].bridge_dev == bridge)
1355 output_ports |= BIT(i);
1356
1357 /* allow sending frames to CPU port and DSA link(s) */
1358 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1359 output_ports |= BIT(i);
1360 }
1361 }
1362
1363 /* prevent frames from going back out of the port they came in on */
1364 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001365
Andrew Lunn158bc062016-04-28 21:24:06 -04001366 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001367 if (reg < 0)
1368 return reg;
1369
1370 reg &= ~mask;
1371 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001372
Andrew Lunn158bc062016-04-28 21:24:06 -04001373 return _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001374}
1375
Vivien Didelotf81ec902016-05-09 13:22:58 -04001376static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1377 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001378{
1379 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1380 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001381 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001382
Vivien Didelot936f2342016-05-09 13:22:46 -04001383 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_PORTSTATE))
1384 return;
1385
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001386 switch (state) {
1387 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001388 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001389 break;
1390 case BR_STATE_BLOCKING:
1391 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001392 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001393 break;
1394 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001395 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001396 break;
1397 case BR_STATE_FORWARDING:
1398 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001399 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001400 break;
1401 }
1402
Vivien Didelot553eb542016-05-13 20:38:23 -04001403 mutex_lock(&ps->smi_mutex);
1404 err = _mv88e6xxx_port_state(ps, port, stp_state);
1405 mutex_unlock(&ps->smi_mutex);
1406
1407 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001408 netdev_err(ds->ports[port].netdev,
1409 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001410 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001411}
1412
Andrew Lunn158bc062016-04-28 21:24:06 -04001413static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port,
1414 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001415{
Andrew Lunn158bc062016-04-28 21:24:06 -04001416 struct dsa_switch *ds = ps->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001417 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001418 int ret;
1419
Andrew Lunn158bc062016-04-28 21:24:06 -04001420 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001421 if (ret < 0)
1422 return ret;
1423
Vivien Didelot5da96032016-03-07 18:24:39 -05001424 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1425
1426 if (new) {
1427 ret &= ~PORT_DEFAULT_VLAN_MASK;
1428 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1429
Andrew Lunn158bc062016-04-28 21:24:06 -04001430 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001431 PORT_DEFAULT_VLAN, ret);
1432 if (ret < 0)
1433 return ret;
1434
Andrew Lunnc8b09802016-06-04 21:16:57 +02001435 netdev_dbg(ds->ports[port].netdev,
1436 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001437 }
1438
1439 if (old)
1440 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001441
1442 return 0;
1443}
1444
Andrew Lunn158bc062016-04-28 21:24:06 -04001445static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps,
1446 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001447{
Andrew Lunn158bc062016-04-28 21:24:06 -04001448 return _mv88e6xxx_port_pvid(ps, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001449}
1450
Andrew Lunn158bc062016-04-28 21:24:06 -04001451static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps,
1452 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001453{
Andrew Lunn158bc062016-04-28 21:24:06 -04001454 return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001455}
1456
Andrew Lunn158bc062016-04-28 21:24:06 -04001457static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001458{
Andrew Lunn158bc062016-04-28 21:24:06 -04001459 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001460 GLOBAL_VTU_OP_BUSY);
1461}
1462
Andrew Lunn158bc062016-04-28 21:24:06 -04001463static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001464{
1465 int ret;
1466
Andrew Lunn158bc062016-04-28 21:24:06 -04001467 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001468 if (ret < 0)
1469 return ret;
1470
Andrew Lunn158bc062016-04-28 21:24:06 -04001471 return _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001472}
1473
Andrew Lunn158bc062016-04-28 21:24:06 -04001474static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001475{
1476 int ret;
1477
Andrew Lunn158bc062016-04-28 21:24:06 -04001478 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001479 if (ret < 0)
1480 return ret;
1481
Andrew Lunn158bc062016-04-28 21:24:06 -04001482 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001483}
1484
Andrew Lunn158bc062016-04-28 21:24:06 -04001485static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001486 struct mv88e6xxx_vtu_stu_entry *entry,
1487 unsigned int nibble_offset)
1488{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001489 u16 regs[3];
1490 int i;
1491 int ret;
1492
1493 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001494 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001495 GLOBAL_VTU_DATA_0_3 + i);
1496 if (ret < 0)
1497 return ret;
1498
1499 regs[i] = ret;
1500 }
1501
Vivien Didelot009a2b92016-04-17 13:24:01 -04001502 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001503 unsigned int shift = (i % 4) * 4 + nibble_offset;
1504 u16 reg = regs[i / 4];
1505
1506 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1507 }
1508
1509 return 0;
1510}
1511
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001512static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_priv_state *ps,
1513 struct mv88e6xxx_vtu_stu_entry *entry)
1514{
1515 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 0);
1516}
1517
1518static int mv88e6xxx_stu_data_read(struct mv88e6xxx_priv_state *ps,
1519 struct mv88e6xxx_vtu_stu_entry *entry)
1520{
1521 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 2);
1522}
1523
Andrew Lunn158bc062016-04-28 21:24:06 -04001524static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001525 struct mv88e6xxx_vtu_stu_entry *entry,
1526 unsigned int nibble_offset)
1527{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001528 u16 regs[3] = { 0 };
1529 int i;
1530 int ret;
1531
Vivien Didelot009a2b92016-04-17 13:24:01 -04001532 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001533 unsigned int shift = (i % 4) * 4 + nibble_offset;
1534 u8 data = entry->data[i];
1535
1536 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1537 }
1538
1539 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001540 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001541 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1542 if (ret < 0)
1543 return ret;
1544 }
1545
1546 return 0;
1547}
1548
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001549static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_priv_state *ps,
1550 struct mv88e6xxx_vtu_stu_entry *entry)
1551{
1552 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 0);
1553}
1554
1555static int mv88e6xxx_stu_data_write(struct mv88e6xxx_priv_state *ps,
1556 struct mv88e6xxx_vtu_stu_entry *entry)
1557{
1558 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 2);
1559}
1560
Andrew Lunn158bc062016-04-28 21:24:06 -04001561static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001562{
Andrew Lunn158bc062016-04-28 21:24:06 -04001563 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001564 vid & GLOBAL_VTU_VID_MASK);
1565}
1566
Andrew Lunn158bc062016-04-28 21:24:06 -04001567static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001568 struct mv88e6xxx_vtu_stu_entry *entry)
1569{
1570 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1571 int ret;
1572
Andrew Lunn158bc062016-04-28 21:24:06 -04001573 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001574 if (ret < 0)
1575 return ret;
1576
Andrew Lunn158bc062016-04-28 21:24:06 -04001577 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001578 if (ret < 0)
1579 return ret;
1580
Andrew Lunn158bc062016-04-28 21:24:06 -04001581 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001582 if (ret < 0)
1583 return ret;
1584
1585 next.vid = ret & GLOBAL_VTU_VID_MASK;
1586 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1587
1588 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001589 ret = mv88e6xxx_vtu_data_read(ps, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001590 if (ret < 0)
1591 return ret;
1592
Andrew Lunn158bc062016-04-28 21:24:06 -04001593 if (mv88e6xxx_has_fid_reg(ps)) {
1594 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001595 GLOBAL_VTU_FID);
1596 if (ret < 0)
1597 return ret;
1598
1599 next.fid = ret & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001600 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001601 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1602 * VTU DBNum[3:0] are located in VTU Operation 3:0
1603 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001604 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001605 GLOBAL_VTU_OP);
1606 if (ret < 0)
1607 return ret;
1608
1609 next.fid = (ret & 0xf00) >> 4;
1610 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001611 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001612
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001613 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001614 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001615 GLOBAL_VTU_SID);
1616 if (ret < 0)
1617 return ret;
1618
1619 next.sid = ret & GLOBAL_VTU_SID_MASK;
1620 }
1621 }
1622
1623 *entry = next;
1624 return 0;
1625}
1626
Vivien Didelotf81ec902016-05-09 13:22:58 -04001627static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1628 struct switchdev_obj_port_vlan *vlan,
1629 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001630{
1631 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1632 struct mv88e6xxx_vtu_stu_entry next;
1633 u16 pvid;
1634 int err;
1635
Vivien Didelot54d77b52016-05-09 13:22:47 -04001636 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
1637 return -EOPNOTSUPP;
1638
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001639 mutex_lock(&ps->smi_mutex);
1640
Andrew Lunn158bc062016-04-28 21:24:06 -04001641 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001642 if (err)
1643 goto unlock;
1644
Andrew Lunn158bc062016-04-28 21:24:06 -04001645 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001646 if (err)
1647 goto unlock;
1648
1649 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001650 err = _mv88e6xxx_vtu_getnext(ps, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001651 if (err)
1652 break;
1653
1654 if (!next.valid)
1655 break;
1656
1657 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1658 continue;
1659
1660 /* reinit and dump this VLAN obj */
1661 vlan->vid_begin = vlan->vid_end = next.vid;
1662 vlan->flags = 0;
1663
1664 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1665 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1666
1667 if (next.vid == pvid)
1668 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1669
1670 err = cb(&vlan->obj);
1671 if (err)
1672 break;
1673 } while (next.vid < GLOBAL_VTU_VID_MASK);
1674
1675unlock:
1676 mutex_unlock(&ps->smi_mutex);
1677
1678 return err;
1679}
1680
Andrew Lunn158bc062016-04-28 21:24:06 -04001681static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001682 struct mv88e6xxx_vtu_stu_entry *entry)
1683{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001684 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001685 u16 reg = 0;
1686 int ret;
1687
Andrew Lunn158bc062016-04-28 21:24:06 -04001688 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001689 if (ret < 0)
1690 return ret;
1691
1692 if (!entry->valid)
1693 goto loadpurge;
1694
1695 /* Write port member tags */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001696 ret = mv88e6xxx_vtu_data_write(ps, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001697 if (ret < 0)
1698 return ret;
1699
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001700 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001701 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001702 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001703 if (ret < 0)
1704 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001705 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001706
Andrew Lunn158bc062016-04-28 21:24:06 -04001707 if (mv88e6xxx_has_fid_reg(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001708 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001709 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001710 if (ret < 0)
1711 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001712 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001713 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1714 * VTU DBNum[3:0] are located in VTU Operation 3:0
1715 */
1716 op |= (entry->fid & 0xf0) << 8;
1717 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001718 }
1719
1720 reg = GLOBAL_VTU_VID_VALID;
1721loadpurge:
1722 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001723 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001724 if (ret < 0)
1725 return ret;
1726
Andrew Lunn158bc062016-04-28 21:24:06 -04001727 return _mv88e6xxx_vtu_cmd(ps, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001728}
1729
Andrew Lunn158bc062016-04-28 21:24:06 -04001730static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001731 struct mv88e6xxx_vtu_stu_entry *entry)
1732{
1733 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1734 int ret;
1735
Andrew Lunn158bc062016-04-28 21:24:06 -04001736 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001737 if (ret < 0)
1738 return ret;
1739
Andrew Lunn158bc062016-04-28 21:24:06 -04001740 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001741 sid & GLOBAL_VTU_SID_MASK);
1742 if (ret < 0)
1743 return ret;
1744
Andrew Lunn158bc062016-04-28 21:24:06 -04001745 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001746 if (ret < 0)
1747 return ret;
1748
Andrew Lunn158bc062016-04-28 21:24:06 -04001749 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001750 if (ret < 0)
1751 return ret;
1752
1753 next.sid = ret & GLOBAL_VTU_SID_MASK;
1754
Andrew Lunn158bc062016-04-28 21:24:06 -04001755 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001756 if (ret < 0)
1757 return ret;
1758
1759 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1760
1761 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001762 ret = mv88e6xxx_stu_data_read(ps, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001763 if (ret < 0)
1764 return ret;
1765 }
1766
1767 *entry = next;
1768 return 0;
1769}
1770
Andrew Lunn158bc062016-04-28 21:24:06 -04001771static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001772 struct mv88e6xxx_vtu_stu_entry *entry)
1773{
1774 u16 reg = 0;
1775 int ret;
1776
Andrew Lunn158bc062016-04-28 21:24:06 -04001777 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001778 if (ret < 0)
1779 return ret;
1780
1781 if (!entry->valid)
1782 goto loadpurge;
1783
1784 /* Write port states */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001785 ret = mv88e6xxx_stu_data_write(ps, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001786 if (ret < 0)
1787 return ret;
1788
1789 reg = GLOBAL_VTU_VID_VALID;
1790loadpurge:
Andrew Lunn158bc062016-04-28 21:24:06 -04001791 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001792 if (ret < 0)
1793 return ret;
1794
1795 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001796 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001797 if (ret < 0)
1798 return ret;
1799
Andrew Lunn158bc062016-04-28 21:24:06 -04001800 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001801}
1802
Andrew Lunn158bc062016-04-28 21:24:06 -04001803static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port,
1804 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001805{
Andrew Lunn158bc062016-04-28 21:24:06 -04001806 struct dsa_switch *ds = ps->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001807 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001808 u16 fid;
1809 int ret;
1810
Andrew Lunn158bc062016-04-28 21:24:06 -04001811 if (mv88e6xxx_num_databases(ps) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001812 upper_mask = 0xff;
Andrew Lunn158bc062016-04-28 21:24:06 -04001813 else if (mv88e6xxx_num_databases(ps) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001814 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001815 else
1816 return -EOPNOTSUPP;
1817
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001818 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001819 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001820 if (ret < 0)
1821 return ret;
1822
1823 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1824
1825 if (new) {
1826 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1827 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1828
Andrew Lunn158bc062016-04-28 21:24:06 -04001829 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001830 ret);
1831 if (ret < 0)
1832 return ret;
1833 }
1834
1835 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001836 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001837 if (ret < 0)
1838 return ret;
1839
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001840 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001841
1842 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001843 ret &= ~upper_mask;
1844 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001845
Andrew Lunn158bc062016-04-28 21:24:06 -04001846 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001847 ret);
1848 if (ret < 0)
1849 return ret;
1850
Andrew Lunnc8b09802016-06-04 21:16:57 +02001851 netdev_dbg(ds->ports[port].netdev,
1852 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001853 }
1854
1855 if (old)
1856 *old = fid;
1857
1858 return 0;
1859}
1860
Andrew Lunn158bc062016-04-28 21:24:06 -04001861static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps,
1862 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001863{
Andrew Lunn158bc062016-04-28 21:24:06 -04001864 return _mv88e6xxx_port_fid(ps, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001865}
1866
Andrew Lunn158bc062016-04-28 21:24:06 -04001867static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps,
1868 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001869{
Andrew Lunn158bc062016-04-28 21:24:06 -04001870 return _mv88e6xxx_port_fid(ps, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001871}
1872
Andrew Lunn158bc062016-04-28 21:24:06 -04001873static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001874{
1875 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1876 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001877 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001878
1879 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1880
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001881 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001882 for (i = 0; i < ps->info->num_ports; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001883 err = _mv88e6xxx_port_fid_get(ps, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001884 if (err)
1885 return err;
1886
1887 set_bit(*fid, fid_bitmap);
1888 }
1889
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001890 /* Set every FID bit used by the VLAN entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001891 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001892 if (err)
1893 return err;
1894
1895 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001896 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001897 if (err)
1898 return err;
1899
1900 if (!vlan.valid)
1901 break;
1902
1903 set_bit(vlan.fid, fid_bitmap);
1904 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1905
1906 /* The reset value 0x000 is used to indicate that multiple address
1907 * databases are not needed. Return the next positive available.
1908 */
1909 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Andrew Lunn158bc062016-04-28 21:24:06 -04001910 if (unlikely(*fid >= mv88e6xxx_num_databases(ps)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001911 return -ENOSPC;
1912
1913 /* Clear the database */
Andrew Lunn158bc062016-04-28 21:24:06 -04001914 return _mv88e6xxx_atu_flush(ps, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001915}
1916
Andrew Lunn158bc062016-04-28 21:24:06 -04001917static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001918 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001919{
Andrew Lunn158bc062016-04-28 21:24:06 -04001920 struct dsa_switch *ds = ps->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001921 struct mv88e6xxx_vtu_stu_entry vlan = {
1922 .valid = true,
1923 .vid = vid,
1924 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001925 int i, err;
1926
Andrew Lunn158bc062016-04-28 21:24:06 -04001927 err = _mv88e6xxx_fid_new(ps, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001928 if (err)
1929 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001930
Vivien Didelot3d131f02015-11-03 10:52:52 -05001931 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001932 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001933 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1934 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1935 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001936
Andrew Lunn158bc062016-04-28 21:24:06 -04001937 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
1938 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001939 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001940
1941 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1942 * implemented, only one STU entry is needed to cover all VTU
1943 * entries. Thus, validate the SID 0.
1944 */
1945 vlan.sid = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04001946 err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001947 if (err)
1948 return err;
1949
1950 if (vstp.sid != vlan.sid || !vstp.valid) {
1951 memset(&vstp, 0, sizeof(vstp));
1952 vstp.valid = true;
1953 vstp.sid = vlan.sid;
1954
Andrew Lunn158bc062016-04-28 21:24:06 -04001955 err = _mv88e6xxx_stu_loadpurge(ps, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001956 if (err)
1957 return err;
1958 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001959 }
1960
1961 *entry = vlan;
1962 return 0;
1963}
1964
Andrew Lunn158bc062016-04-28 21:24:06 -04001965static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001966 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1967{
1968 int err;
1969
1970 if (!vid)
1971 return -EINVAL;
1972
Andrew Lunn158bc062016-04-28 21:24:06 -04001973 err = _mv88e6xxx_vtu_vid_write(ps, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001974 if (err)
1975 return err;
1976
Andrew Lunn158bc062016-04-28 21:24:06 -04001977 err = _mv88e6xxx_vtu_getnext(ps, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001978 if (err)
1979 return err;
1980
1981 if (entry->vid != vid || !entry->valid) {
1982 if (!creat)
1983 return -EOPNOTSUPP;
1984 /* -ENOENT would've been more appropriate, but switchdev expects
1985 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1986 */
1987
Andrew Lunn158bc062016-04-28 21:24:06 -04001988 err = _mv88e6xxx_vtu_new(ps, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001989 }
1990
1991 return err;
1992}
1993
Vivien Didelotda9c3592016-02-12 12:09:40 -05001994static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1995 u16 vid_begin, u16 vid_end)
1996{
1997 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1998 struct mv88e6xxx_vtu_stu_entry vlan;
1999 int i, err;
2000
2001 if (!vid_begin)
2002 return -EOPNOTSUPP;
2003
2004 mutex_lock(&ps->smi_mutex);
2005
Andrew Lunn158bc062016-04-28 21:24:06 -04002006 err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002007 if (err)
2008 goto unlock;
2009
2010 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002011 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002012 if (err)
2013 goto unlock;
2014
2015 if (!vlan.valid)
2016 break;
2017
2018 if (vlan.vid > vid_end)
2019 break;
2020
Vivien Didelot009a2b92016-04-17 13:24:01 -04002021 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05002022 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
2023 continue;
2024
2025 if (vlan.data[i] ==
2026 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2027 continue;
2028
2029 if (ps->ports[i].bridge_dev ==
2030 ps->ports[port].bridge_dev)
2031 break; /* same bridge, check next VLAN */
2032
Andrew Lunnc8b09802016-06-04 21:16:57 +02002033 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05002034 "hardware VLAN %d already used by %s\n",
2035 vlan.vid,
2036 netdev_name(ps->ports[i].bridge_dev));
2037 err = -EOPNOTSUPP;
2038 goto unlock;
2039 }
2040 } while (vlan.vid < vid_end);
2041
2042unlock:
2043 mutex_unlock(&ps->smi_mutex);
2044
2045 return err;
2046}
2047
Vivien Didelot214cdb92016-02-26 13:16:08 -05002048static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2049 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2050 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2051 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2052 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2053};
2054
Vivien Didelotf81ec902016-05-09 13:22:58 -04002055static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2056 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05002057{
2058 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2059 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2060 PORT_CONTROL_2_8021Q_DISABLED;
2061 int ret;
2062
Vivien Didelot54d77b52016-05-09 13:22:47 -04002063 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2064 return -EOPNOTSUPP;
2065
Vivien Didelot214cdb92016-02-26 13:16:08 -05002066 mutex_lock(&ps->smi_mutex);
2067
Andrew Lunn158bc062016-04-28 21:24:06 -04002068 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002069 if (ret < 0)
2070 goto unlock;
2071
2072 old = ret & PORT_CONTROL_2_8021Q_MASK;
2073
Vivien Didelot5220ef12016-03-07 18:24:52 -05002074 if (new != old) {
2075 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2076 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002077
Andrew Lunn158bc062016-04-28 21:24:06 -04002078 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002079 ret);
2080 if (ret < 0)
2081 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002082
Andrew Lunnc8b09802016-06-04 21:16:57 +02002083 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05002084 mv88e6xxx_port_8021q_mode_names[new],
2085 mv88e6xxx_port_8021q_mode_names[old]);
2086 }
2087
2088 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002089unlock:
2090 mutex_unlock(&ps->smi_mutex);
2091
2092 return ret;
2093}
2094
Vivien Didelotf81ec902016-05-09 13:22:58 -04002095static int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2096 const struct switchdev_obj_port_vlan *vlan,
2097 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002098{
Vivien Didelot54d77b52016-05-09 13:22:47 -04002099 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002100 int err;
2101
Vivien Didelot54d77b52016-05-09 13:22:47 -04002102 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2103 return -EOPNOTSUPP;
2104
Vivien Didelotda9c3592016-02-12 12:09:40 -05002105 /* If the requested port doesn't belong to the same bridge as the VLAN
2106 * members, do not support it (yet) and fallback to software VLAN.
2107 */
2108 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2109 vlan->vid_end);
2110 if (err)
2111 return err;
2112
Vivien Didelot76e398a2015-11-01 12:33:55 -05002113 /* We don't need any dynamic resource from the kernel (yet),
2114 * so skip the prepare phase.
2115 */
2116 return 0;
2117}
2118
Andrew Lunn158bc062016-04-28 21:24:06 -04002119static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port,
2120 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002121{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002122 struct mv88e6xxx_vtu_stu_entry vlan;
2123 int err;
2124
Andrew Lunn158bc062016-04-28 21:24:06 -04002125 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002126 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002127 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002128
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002129 vlan.data[port] = untagged ?
2130 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2131 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2132
Andrew Lunn158bc062016-04-28 21:24:06 -04002133 return _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002134}
2135
Vivien Didelotf81ec902016-05-09 13:22:58 -04002136static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2137 const struct switchdev_obj_port_vlan *vlan,
2138 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002139{
2140 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2141 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2142 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2143 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002144
Vivien Didelot54d77b52016-05-09 13:22:47 -04002145 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2146 return;
2147
Vivien Didelot76e398a2015-11-01 12:33:55 -05002148 mutex_lock(&ps->smi_mutex);
2149
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002150 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Andrew Lunn158bc062016-04-28 21:24:06 -04002151 if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002152 netdev_err(ds->ports[port].netdev,
2153 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002154 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002155
Andrew Lunn158bc062016-04-28 21:24:06 -04002156 if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002157 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002158 vlan->vid_end);
2159
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002160 mutex_unlock(&ps->smi_mutex);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002161}
2162
Andrew Lunn158bc062016-04-28 21:24:06 -04002163static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps,
2164 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002165{
Andrew Lunn158bc062016-04-28 21:24:06 -04002166 struct dsa_switch *ds = ps->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002167 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002168 int i, err;
2169
Andrew Lunn158bc062016-04-28 21:24:06 -04002170 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002171 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002172 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002173
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002174 /* Tell switchdev if this VLAN is handled in software */
2175 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002176 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002177
2178 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2179
2180 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002181 vlan.valid = false;
Vivien Didelot009a2b92016-04-17 13:24:01 -04002182 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002183 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002184 continue;
2185
2186 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002187 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002188 break;
2189 }
2190 }
2191
Andrew Lunn158bc062016-04-28 21:24:06 -04002192 err = _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002193 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002194 return err;
2195
Andrew Lunn158bc062016-04-28 21:24:06 -04002196 return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002197}
2198
Vivien Didelotf81ec902016-05-09 13:22:58 -04002199static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2200 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002201{
2202 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2203 u16 pvid, vid;
2204 int err = 0;
2205
Vivien Didelot54d77b52016-05-09 13:22:47 -04002206 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2207 return -EOPNOTSUPP;
2208
Vivien Didelot76e398a2015-11-01 12:33:55 -05002209 mutex_lock(&ps->smi_mutex);
2210
Andrew Lunn158bc062016-04-28 21:24:06 -04002211 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002212 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002213 goto unlock;
2214
Vivien Didelot76e398a2015-11-01 12:33:55 -05002215 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002216 err = _mv88e6xxx_port_vlan_del(ps, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002217 if (err)
2218 goto unlock;
2219
2220 if (vid == pvid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002221 err = _mv88e6xxx_port_pvid_set(ps, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002222 if (err)
2223 goto unlock;
2224 }
2225 }
2226
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002227unlock:
2228 mutex_unlock(&ps->smi_mutex);
2229
2230 return err;
2231}
2232
Andrew Lunn158bc062016-04-28 21:24:06 -04002233static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002234 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002235{
2236 int i, ret;
2237
2238 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002239 ret = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002240 ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002241 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002242 if (ret < 0)
2243 return ret;
2244 }
2245
2246 return 0;
2247}
2248
Andrew Lunn158bc062016-04-28 21:24:06 -04002249static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps,
2250 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002251{
2252 int i, ret;
2253
2254 for (i = 0; i < 3; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002255 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002256 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002257 if (ret < 0)
2258 return ret;
2259 addr[i * 2] = ret >> 8;
2260 addr[i * 2 + 1] = ret & 0xff;
2261 }
2262
2263 return 0;
2264}
2265
Andrew Lunn158bc062016-04-28 21:24:06 -04002266static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002267 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002268{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002269 int ret;
2270
Andrew Lunn158bc062016-04-28 21:24:06 -04002271 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002272 if (ret < 0)
2273 return ret;
2274
Andrew Lunn158bc062016-04-28 21:24:06 -04002275 ret = _mv88e6xxx_atu_mac_write(ps, entry->mac);
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_data_write(ps, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002280 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002281 return ret;
2282
Andrew Lunn158bc062016-04-28 21:24:06 -04002283 return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002284}
David S. Millercdf09692015-08-11 12:00:37 -07002285
Andrew Lunn158bc062016-04-28 21:24:06 -04002286static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002287 const unsigned char *addr, u16 vid,
2288 u8 state)
2289{
2290 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002291 struct mv88e6xxx_vtu_stu_entry vlan;
2292 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002293
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002294 /* Null VLAN ID corresponds to the port private database */
2295 if (vid == 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04002296 err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002297 else
Andrew Lunn158bc062016-04-28 21:24:06 -04002298 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002299 if (err)
2300 return err;
2301
2302 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002303 entry.state = state;
2304 ether_addr_copy(entry.mac, addr);
2305 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2306 entry.trunk = false;
2307 entry.portv_trunkid = BIT(port);
2308 }
2309
Andrew Lunn158bc062016-04-28 21:24:06 -04002310 return _mv88e6xxx_atu_load(ps, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002311}
2312
Vivien Didelotf81ec902016-05-09 13:22:58 -04002313static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2314 const struct switchdev_obj_port_fdb *fdb,
2315 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002316{
Vivien Didelot2672f822016-05-09 13:22:48 -04002317 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2318
2319 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2320 return -EOPNOTSUPP;
2321
Vivien Didelot146a3202015-10-08 11:35:12 -04002322 /* We don't need any dynamic resource from the kernel (yet),
2323 * so skip the prepare phase.
2324 */
2325 return 0;
2326}
2327
Vivien Didelotf81ec902016-05-09 13:22:58 -04002328static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2329 const struct switchdev_obj_port_fdb *fdb,
2330 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002331{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002332 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002333 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2334 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2335 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002336
Vivien Didelot2672f822016-05-09 13:22:48 -04002337 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2338 return;
2339
David S. Millercdf09692015-08-11 12:00:37 -07002340 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002341 if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002342 netdev_err(ds->ports[port].netdev,
2343 "failed to load MAC address\n");
David S. Millercdf09692015-08-11 12:00:37 -07002344 mutex_unlock(&ps->smi_mutex);
David S. Millercdf09692015-08-11 12:00:37 -07002345}
2346
Vivien Didelotf81ec902016-05-09 13:22:58 -04002347static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2348 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002349{
2350 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2351 int ret;
2352
Vivien Didelot2672f822016-05-09 13:22:48 -04002353 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2354 return -EOPNOTSUPP;
2355
David S. Millercdf09692015-08-11 12:00:37 -07002356 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002357 ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002358 GLOBAL_ATU_DATA_STATE_UNUSED);
2359 mutex_unlock(&ps->smi_mutex);
2360
2361 return ret;
2362}
2363
Andrew Lunn158bc062016-04-28 21:24:06 -04002364static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002365 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002366{
Vivien Didelot1d194042015-08-10 09:09:51 -04002367 struct mv88e6xxx_atu_entry next = { 0 };
2368 int ret;
2369
2370 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002371
Andrew Lunn158bc062016-04-28 21:24:06 -04002372 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002373 if (ret < 0)
2374 return ret;
2375
Andrew Lunn158bc062016-04-28 21:24:06 -04002376 ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
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_mac_read(ps, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002381 if (ret < 0)
2382 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002383
Andrew Lunn158bc062016-04-28 21:24:06 -04002384 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002385 if (ret < 0)
2386 return ret;
2387
2388 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2389 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2390 unsigned int mask, shift;
2391
2392 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2393 next.trunk = true;
2394 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2395 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2396 } else {
2397 next.trunk = false;
2398 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2399 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2400 }
2401
2402 next.portv_trunkid = (ret & mask) >> shift;
2403 }
2404
2405 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002406 return 0;
2407}
2408
Andrew Lunn158bc062016-04-28 21:24:06 -04002409static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps,
2410 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002411 struct switchdev_obj_port_fdb *fdb,
2412 int (*cb)(struct switchdev_obj *obj))
2413{
2414 struct mv88e6xxx_atu_entry addr = {
2415 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2416 };
2417 int err;
2418
Andrew Lunn158bc062016-04-28 21:24:06 -04002419 err = _mv88e6xxx_atu_mac_write(ps, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002420 if (err)
2421 return err;
2422
2423 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002424 err = _mv88e6xxx_atu_getnext(ps, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002425 if (err)
2426 break;
2427
2428 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2429 break;
2430
2431 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2432 bool is_static = addr.state ==
2433 (is_multicast_ether_addr(addr.mac) ?
2434 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2435 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2436
2437 fdb->vid = vid;
2438 ether_addr_copy(fdb->addr, addr.mac);
2439 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2440
2441 err = cb(&fdb->obj);
2442 if (err)
2443 break;
2444 }
2445 } while (!is_broadcast_ether_addr(addr.mac));
2446
2447 return err;
2448}
2449
Vivien Didelotf81ec902016-05-09 13:22:58 -04002450static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2451 struct switchdev_obj_port_fdb *fdb,
2452 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002453{
2454 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2455 struct mv88e6xxx_vtu_stu_entry vlan = {
2456 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2457 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002458 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002459 int err;
2460
Vivien Didelot2672f822016-05-09 13:22:48 -04002461 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2462 return -EOPNOTSUPP;
2463
Vivien Didelotf33475b2015-10-22 09:34:41 -04002464 mutex_lock(&ps->smi_mutex);
2465
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002466 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Andrew Lunn158bc062016-04-28 21:24:06 -04002467 err = _mv88e6xxx_port_fid_get(ps, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002468 if (err)
2469 goto unlock;
2470
Andrew Lunn158bc062016-04-28 21:24:06 -04002471 err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002472 if (err)
2473 goto unlock;
2474
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002475 /* Dump VLANs' Filtering Information Databases */
Andrew Lunn158bc062016-04-28 21:24:06 -04002476 err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002477 if (err)
2478 goto unlock;
2479
2480 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002481 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002482 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002483 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002484
2485 if (!vlan.valid)
2486 break;
2487
Andrew Lunn158bc062016-04-28 21:24:06 -04002488 err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002489 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002490 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002491 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002492 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2493
2494unlock:
2495 mutex_unlock(&ps->smi_mutex);
2496
2497 return err;
2498}
2499
Vivien Didelotf81ec902016-05-09 13:22:58 -04002500static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2501 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002502{
Vivien Didelota6692752016-02-12 12:09:39 -05002503 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002504 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002505
Vivien Didelot936f2342016-05-09 13:22:46 -04002506 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2507 return -EOPNOTSUPP;
2508
Vivien Didelot466dfa02016-02-26 13:16:05 -05002509 mutex_lock(&ps->smi_mutex);
2510
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002511 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002512 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002513
Vivien Didelot009a2b92016-04-17 13:24:01 -04002514 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002515 if (ps->ports[i].bridge_dev == bridge) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002516 err = _mv88e6xxx_port_based_vlan_map(ps, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002517 if (err)
2518 break;
2519 }
2520 }
2521
Vivien Didelot466dfa02016-02-26 13:16:05 -05002522 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002523
Vivien Didelot466dfa02016-02-26 13:16:05 -05002524 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002525}
2526
Vivien Didelotf81ec902016-05-09 13:22:58 -04002527static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002528{
Vivien Didelota6692752016-02-12 12:09:39 -05002529 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002530 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002531 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002532
Vivien Didelot936f2342016-05-09 13:22:46 -04002533 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2534 return;
2535
Vivien Didelot466dfa02016-02-26 13:16:05 -05002536 mutex_lock(&ps->smi_mutex);
2537
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002538 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002539 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002540
Vivien Didelot009a2b92016-04-17 13:24:01 -04002541 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot16bfa702016-03-13 16:21:33 -04002542 if (i == port || ps->ports[i].bridge_dev == bridge)
Andrew Lunn158bc062016-04-28 21:24:06 -04002543 if (_mv88e6xxx_port_based_vlan_map(ps, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002544 netdev_warn(ds->ports[i].netdev,
2545 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002546
Vivien Didelot466dfa02016-02-26 13:16:05 -05002547 mutex_unlock(&ps->smi_mutex);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002548}
2549
Andrew Lunn03a4a542016-06-04 21:17:05 +02002550static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_priv_state *ps,
2551 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002552{
2553 int ret;
2554
Andrew Lunn03a4a542016-06-04 21:17:05 +02002555 ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002556 if (ret < 0)
2557 goto restore_page_0;
2558
Andrew Lunn03a4a542016-06-04 21:17:05 +02002559 ret = mv88e6xxx_mdio_write_indirect(ps, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002560restore_page_0:
Andrew Lunn03a4a542016-06-04 21:17:05 +02002561 mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002562
2563 return ret;
2564}
2565
Andrew Lunn03a4a542016-06-04 21:17:05 +02002566static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_priv_state *ps,
2567 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002568{
2569 int ret;
2570
Andrew Lunn03a4a542016-06-04 21:17:05 +02002571 ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002572 if (ret < 0)
2573 goto restore_page_0;
2574
Andrew Lunn03a4a542016-06-04 21:17:05 +02002575 ret = mv88e6xxx_mdio_read_indirect(ps, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002576restore_page_0:
Andrew Lunn03a4a542016-06-04 21:17:05 +02002577 mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002578
2579 return ret;
2580}
2581
Vivien Didelot552238b2016-05-09 13:22:49 -04002582static int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps)
2583{
2584 bool ppu_active = mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE);
2585 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunn52638f72016-05-10 23:27:22 +02002586 struct gpio_desc *gpiod = ps->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002587 unsigned long timeout;
2588 int ret;
2589 int i;
2590
2591 /* Set all ports to the disabled state. */
2592 for (i = 0; i < ps->info->num_ports; i++) {
2593 ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL);
2594 if (ret < 0)
2595 return ret;
2596
2597 ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL,
2598 ret & 0xfffc);
2599 if (ret)
2600 return ret;
2601 }
2602
2603 /* Wait for transmit queues to drain. */
2604 usleep_range(2000, 4000);
2605
2606 /* If there is a gpio connected to the reset pin, toggle it */
2607 if (gpiod) {
2608 gpiod_set_value_cansleep(gpiod, 1);
2609 usleep_range(10000, 20000);
2610 gpiod_set_value_cansleep(gpiod, 0);
2611 usleep_range(10000, 20000);
2612 }
2613
2614 /* Reset the switch. Keep the PPU active if requested. The PPU
2615 * needs to be active to support indirect phy register access
2616 * through global registers 0x18 and 0x19.
2617 */
2618 if (ppu_active)
2619 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000);
2620 else
2621 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400);
2622 if (ret)
2623 return ret;
2624
2625 /* Wait up to one second for reset to complete. */
2626 timeout = jiffies + 1 * HZ;
2627 while (time_before(jiffies, timeout)) {
2628 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00);
2629 if (ret < 0)
2630 return ret;
2631
2632 if ((ret & is_reset) == is_reset)
2633 break;
2634 usleep_range(1000, 2000);
2635 }
2636 if (time_after(jiffies, timeout))
2637 ret = -ETIMEDOUT;
2638 else
2639 ret = 0;
2640
2641 return ret;
2642}
2643
Andrew Lunn158bc062016-04-28 21:24:06 -04002644static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002645{
2646 int ret;
2647
Andrew Lunn03a4a542016-06-04 21:17:05 +02002648 ret = _mv88e6xxx_mdio_page_read(ps, REG_FIBER_SERDES,
2649 PAGE_FIBER_SERDES, MII_BMCR);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002650 if (ret < 0)
2651 return ret;
2652
2653 if (ret & BMCR_PDOWN) {
2654 ret &= ~BMCR_PDOWN;
Andrew Lunn03a4a542016-06-04 21:17:05 +02002655 ret = _mv88e6xxx_mdio_page_write(ps, REG_FIBER_SERDES,
2656 PAGE_FIBER_SERDES, MII_BMCR,
2657 ret);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002658 }
2659
2660 return ret;
2661}
2662
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002663static int mv88e6xxx_setup_port(struct mv88e6xxx_priv_state *ps, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002664{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002665 struct dsa_switch *ds = ps->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002666 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002667 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002668
Andrew Lunn158bc062016-04-28 21:24:06 -04002669 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2670 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2671 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2672 mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002673 /* MAC Forcing register: don't force link, speed,
2674 * duplex or flow control state to any particular
2675 * values on physical ports, but force the CPU port
2676 * and all DSA ports to their maximum bandwidth and
2677 * full duplex.
2678 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002679 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002680 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002681 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002682 reg |= PORT_PCS_CTRL_FORCE_LINK |
2683 PORT_PCS_CTRL_LINK_UP |
2684 PORT_PCS_CTRL_DUPLEX_FULL |
2685 PORT_PCS_CTRL_FORCE_DUPLEX;
Andrew Lunn158bc062016-04-28 21:24:06 -04002686 if (mv88e6xxx_6065_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002687 reg |= PORT_PCS_CTRL_100;
2688 else
2689 reg |= PORT_PCS_CTRL_1000;
2690 } else {
2691 reg |= PORT_PCS_CTRL_UNFORCED;
2692 }
2693
Andrew Lunn158bc062016-04-28 21:24:06 -04002694 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002695 PORT_PCS_CTRL, reg);
2696 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002697 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002698 }
2699
2700 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2701 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2702 * tunneling, determine priority by looking at 802.1p and IP
2703 * priority fields (IP prio has precedence), and set STP state
2704 * to Forwarding.
2705 *
2706 * If this is the CPU link, use DSA or EDSA tagging depending
2707 * on which tagging mode was configured.
2708 *
2709 * If this is a link to another switch, use DSA tagging mode.
2710 *
2711 * If this is the upstream port for this switch, enable
2712 * forwarding of unknown unicasts and multicasts.
2713 */
2714 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002715 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2716 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2717 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2718 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002719 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2720 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2721 PORT_CONTROL_STATE_FORWARDING;
2722 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002723 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002724 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002725 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2726 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2727 mv88e6xxx_6320_family(ps)) {
Andrew Lunn5377b802016-06-04 21:17:02 +02002728 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2729 PORT_CONTROL_FORWARD_UNKNOWN |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002730 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002731 }
2732
Andrew Lunn158bc062016-04-28 21:24:06 -04002733 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2734 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2735 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2736 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002737 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2738 }
2739 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002740 if (dsa_is_dsa_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002741 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002742 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002743 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2744 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2745 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002746 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002747 }
2748
Andrew Lunn54d792f2015-05-06 01:09:47 +02002749 if (port == dsa_upstream_port(ds))
2750 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2751 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2752 }
2753 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002754 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002755 PORT_CONTROL, reg);
2756 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002757 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002758 }
2759
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002760 /* If this port is connected to a SerDes, make sure the SerDes is not
2761 * powered down.
2762 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002763 if (mv88e6xxx_6352_family(ps)) {
2764 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002765 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002766 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002767 ret &= PORT_STATUS_CMODE_MASK;
2768 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2769 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2770 (ret == PORT_STATUS_CMODE_SGMII)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002771 ret = mv88e6xxx_power_on_serdes(ps);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002772 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002773 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002774 }
2775 }
2776
Vivien Didelot8efdda42015-08-13 12:52:23 -04002777 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002778 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002779 * untagged frames on this port, do a destination address lookup on all
2780 * received packets as usual, disable ARP mirroring and don't send a
2781 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002782 */
2783 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002784 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2785 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2786 mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) ||
2787 mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002788 reg = PORT_CONTROL_2_MAP_DA;
2789
Andrew Lunn158bc062016-04-28 21:24:06 -04002790 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2791 mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002792 reg |= PORT_CONTROL_2_JUMBO_10240;
2793
Andrew Lunn158bc062016-04-28 21:24:06 -04002794 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002795 /* Set the upstream port this port should use */
2796 reg |= dsa_upstream_port(ds);
2797 /* enable forwarding of unknown multicast addresses to
2798 * the upstream port
2799 */
2800 if (port == dsa_upstream_port(ds))
2801 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2802 }
2803
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002804 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002805
Andrew Lunn54d792f2015-05-06 01:09:47 +02002806 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002807 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002808 PORT_CONTROL_2, reg);
2809 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002810 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002811 }
2812
2813 /* Port Association Vector: when learning source addresses
2814 * of packets, add the address to the address database using
2815 * a port bitmap that has only the bit for this port set and
2816 * the other bits clear.
2817 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002818 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002819 /* Disable learning for CPU port */
2820 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002821 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002822
Andrew Lunn158bc062016-04-28 21:24:06 -04002823 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002824 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002825 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002826
2827 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002828 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002829 0x0000);
2830 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002831 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002832
Andrew Lunn158bc062016-04-28 21:24:06 -04002833 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2834 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2835 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002836 /* Do not limit the period of time that this port can
2837 * be paused for by the remote end or the period of
2838 * time that this port can pause the remote end.
2839 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002840 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002841 PORT_PAUSE_CTRL, 0x0000);
2842 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002843 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002844
2845 /* Port ATU control: disable limiting the number of
2846 * address database entries that this port is allowed
2847 * to use.
2848 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002849 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002850 PORT_ATU_CONTROL, 0x0000);
2851 /* Priority Override: disable DA, SA and VTU priority
2852 * override.
2853 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002854 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002855 PORT_PRI_OVERRIDE, 0x0000);
2856 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002857 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002858
2859 /* Port Ethertype: use the Ethertype DSA Ethertype
2860 * value.
2861 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002862 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002863 PORT_ETH_TYPE, ETH_P_EDSA);
2864 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002865 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002866 /* Tag Remap: use an identity 802.1p prio -> switch
2867 * prio mapping.
2868 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002869 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002870 PORT_TAG_REGMAP_0123, 0x3210);
2871 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002872 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002873
2874 /* Tag Remap 2: use an identity 802.1p prio -> switch
2875 * prio mapping.
2876 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002877 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002878 PORT_TAG_REGMAP_4567, 0x7654);
2879 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002880 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002881 }
2882
Andrew Lunn158bc062016-04-28 21:24:06 -04002883 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2884 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2885 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2886 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002887 /* Rate Control: disable ingress rate limiting. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002888 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002889 PORT_RATE_CONTROL, 0x0001);
2890 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002891 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002892 }
2893
Guenter Roeck366f0a02015-03-26 18:36:30 -07002894 /* Port Control 1: disable trunking, disable sending
2895 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002896 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002897 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002898 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002899 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002900
Vivien Didelot207afda2016-04-14 14:42:09 -04002901 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002902 * database, and allow bidirectional communication between the
2903 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002904 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002905 ret = _mv88e6xxx_port_fid_set(ps, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002906 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002907 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002908
Andrew Lunn158bc062016-04-28 21:24:06 -04002909 ret = _mv88e6xxx_port_based_vlan_map(ps, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002910 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002911 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002912
2913 /* Default VLAN ID and priority: don't set a default VLAN
2914 * ID, and set the default packet priority to zero.
2915 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002916 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002917 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002918 if (ret)
2919 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002920
Andrew Lunndbde9e62015-05-06 01:09:48 +02002921 return 0;
2922}
2923
Vivien Didelot08a01262016-05-09 13:22:50 -04002924static int mv88e6xxx_setup_global(struct mv88e6xxx_priv_state *ps)
2925{
Vivien Didelotb0745e872016-05-09 13:22:53 -04002926 struct dsa_switch *ds = ps->ds;
2927 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002928 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002929 int err;
2930 int i;
2931
Vivien Didelot119477b2016-05-09 13:22:51 -04002932 /* Enable the PHY Polling Unit if present, don't discard any packets,
2933 * and mask all interrupt sources.
2934 */
2935 reg = 0;
2936 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU) ||
2937 mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE))
2938 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2939
2940 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, reg);
2941 if (err)
2942 return err;
2943
Vivien Didelotb0745e872016-05-09 13:22:53 -04002944 /* Configure the upstream port, and configure it as the port to which
2945 * ingress and egress and ARP monitor frames are to be sent.
2946 */
2947 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2948 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2949 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2950 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
2951 if (err)
2952 return err;
2953
Vivien Didelot50484ff2016-05-09 13:22:54 -04002954 /* Disable remote management, and set the switch's DSA device number. */
2955 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL_2,
2956 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2957 (ds->index & 0x1f));
2958 if (err)
2959 return err;
2960
Vivien Didelot08a01262016-05-09 13:22:50 -04002961 /* Set the default address aging time to 5 minutes, and
2962 * enable address learn messages to be sent to all message
2963 * ports.
2964 */
2965 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2966 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2967 if (err)
2968 return err;
2969
2970 /* Configure the IP ToS mapping registers. */
2971 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2972 if (err)
2973 return err;
2974 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2975 if (err)
2976 return err;
2977 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2978 if (err)
2979 return err;
2980 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2981 if (err)
2982 return err;
2983 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2984 if (err)
2985 return err;
2986 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2987 if (err)
2988 return err;
2989 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2990 if (err)
2991 return err;
2992 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2993 if (err)
2994 return err;
2995
2996 /* Configure the IEEE 802.1p priority mapping register. */
2997 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2998 if (err)
2999 return err;
3000
3001 /* Send all frames with destination addresses matching
3002 * 01:80:c2:00:00:0x to the CPU port.
3003 */
3004 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
3005 if (err)
3006 return err;
3007
3008 /* Ignore removed tag data on doubly tagged packets, disable
3009 * flow control messages, force flow control priority to the
3010 * highest, and send all special multicast frames to the CPU
3011 * port at the highest priority.
3012 */
3013 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
3014 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
3015 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
3016 if (err)
3017 return err;
3018
3019 /* Program the DSA routing table. */
3020 for (i = 0; i < 32; i++) {
3021 int nexthop = 0x1f;
3022
Andrew Lunn66472fc2016-06-04 21:17:00 +02003023 if (i != ds->index && i < DSA_MAX_SWITCHES)
3024 nexthop = ds->rtable[i] & 0x1f;
Vivien Didelot08a01262016-05-09 13:22:50 -04003025
3026 err = _mv88e6xxx_reg_write(
3027 ps, REG_GLOBAL2,
3028 GLOBAL2_DEVICE_MAPPING,
3029 GLOBAL2_DEVICE_MAPPING_UPDATE |
3030 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
3031 if (err)
3032 return err;
3033 }
3034
3035 /* Clear all trunk masks. */
3036 for (i = 0; i < 8; i++) {
3037 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
3038 0x8000 |
3039 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
3040 ((1 << ps->info->num_ports) - 1));
3041 if (err)
3042 return err;
3043 }
3044
3045 /* Clear all trunk mappings. */
3046 for (i = 0; i < 16; i++) {
3047 err = _mv88e6xxx_reg_write(
3048 ps, REG_GLOBAL2,
3049 GLOBAL2_TRUNK_MAPPING,
3050 GLOBAL2_TRUNK_MAPPING_UPDATE |
3051 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
3052 if (err)
3053 return err;
3054 }
3055
3056 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3057 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3058 mv88e6xxx_6320_family(ps)) {
3059 /* Send all frames with destination addresses matching
3060 * 01:80:c2:00:00:2x to the CPU port.
3061 */
3062 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3063 GLOBAL2_MGMT_EN_2X, 0xffff);
3064 if (err)
3065 return err;
3066
3067 /* Initialise cross-chip port VLAN table to reset
3068 * defaults.
3069 */
3070 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3071 GLOBAL2_PVT_ADDR, 0x9000);
3072 if (err)
3073 return err;
3074
3075 /* Clear the priority override table. */
3076 for (i = 0; i < 16; i++) {
3077 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3078 GLOBAL2_PRIO_OVERRIDE,
3079 0x8000 | (i << 8));
3080 if (err)
3081 return err;
3082 }
3083 }
3084
3085 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3086 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3087 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
3088 mv88e6xxx_6320_family(ps)) {
3089 /* Disable ingress rate limiting by resetting all
3090 * ingress rate limit registers to their initial
3091 * state.
3092 */
3093 for (i = 0; i < ps->info->num_ports; i++) {
3094 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3095 GLOBAL2_INGRESS_OP,
3096 0x9000 | (i << 8));
3097 if (err)
3098 return err;
3099 }
3100 }
3101
3102 /* Clear the statistics counters for all ports */
3103 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
3104 GLOBAL_STATS_OP_FLUSH_ALL);
3105 if (err)
3106 return err;
3107
3108 /* Wait for the flush to complete. */
3109 err = _mv88e6xxx_stats_wait(ps);
3110 if (err)
3111 return err;
3112
3113 /* Clear all ATU entries */
3114 err = _mv88e6xxx_atu_flush(ps, 0, true);
3115 if (err)
3116 return err;
3117
3118 /* Clear all the VTU and STU entries */
3119 err = _mv88e6xxx_vtu_stu_flush(ps);
3120 if (err < 0)
3121 return err;
3122
3123 return err;
3124}
3125
Vivien Didelotf81ec902016-05-09 13:22:58 -04003126static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003127{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003128 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot552238b2016-05-09 13:22:49 -04003129 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003130 int i;
3131
3132 ps->ds = ds;
Vivien Didelot552238b2016-05-09 13:22:49 -04003133
Vivien Didelotd24645b2016-05-09 13:22:41 -04003134 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
3135 mutex_init(&ps->eeprom_mutex);
3136
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003137 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3138 mv88e6xxx_ppu_state_init(ps);
3139
Vivien Didelot552238b2016-05-09 13:22:49 -04003140 mutex_lock(&ps->smi_mutex);
3141
3142 err = mv88e6xxx_switch_reset(ps);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003143 if (err)
3144 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003145
Vivien Didelot08a01262016-05-09 13:22:50 -04003146 err = mv88e6xxx_setup_global(ps);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003147 if (err)
3148 goto unlock;
3149
3150 for (i = 0; i < ps->info->num_ports; i++) {
3151 err = mv88e6xxx_setup_port(ps, i);
3152 if (err)
3153 goto unlock;
3154 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003155
Vivien Didelot6b17e862015-08-13 12:52:18 -04003156unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04003157 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02003158
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003159 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003160}
3161
Andrew Lunn03a4a542016-06-04 21:17:05 +02003162int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page, int reg)
Andrew Lunn491435852015-04-02 04:06:35 +02003163{
3164 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3165 int ret;
3166
Andrew Lunn3898c142015-05-06 01:09:53 +02003167 mutex_lock(&ps->smi_mutex);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003168 ret = _mv88e6xxx_mdio_page_read(ps, port, page, reg);
Andrew Lunn3898c142015-05-06 01:09:53 +02003169 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003170
Andrew Lunn491435852015-04-02 04:06:35 +02003171 return ret;
3172}
3173
Andrew Lunn03a4a542016-06-04 21:17:05 +02003174int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3175 int reg, int val)
Andrew Lunn491435852015-04-02 04:06:35 +02003176{
3177 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3178 int ret;
3179
Andrew Lunn3898c142015-05-06 01:09:53 +02003180 mutex_lock(&ps->smi_mutex);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003181 ret = _mv88e6xxx_mdio_page_write(ps, port, page, reg, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02003182 mutex_unlock(&ps->smi_mutex);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00003183
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003184 return ret;
3185}
3186
Andrew Lunn03a4a542016-06-04 21:17:05 +02003187static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_priv_state *ps,
3188 int port)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003189{
Vivien Didelot009a2b92016-04-17 13:24:01 -04003190 if (port >= 0 && port < ps->info->num_ports)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003191 return port;
3192 return -EINVAL;
3193}
3194
Andrew Lunn03a4a542016-06-04 21:17:05 +02003195static int mv88e6xxx_mdio_read(struct dsa_switch *ds, int port, int regnum)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003196{
3197 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003198 int addr = mv88e6xxx_port_to_mdio_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003199 int ret;
3200
3201 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003202 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003203
Andrew Lunn3898c142015-05-06 01:09:53 +02003204 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003205
3206 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003207 ret = mv88e6xxx_mdio_read_ppu(ps, addr, regnum);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003208 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003209 ret = mv88e6xxx_mdio_read_indirect(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003210 else
Andrew Lunn03a4a542016-06-04 21:17:05 +02003211 ret = mv88e6xxx_mdio_read_direct(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003212
Andrew Lunn3898c142015-05-06 01:09:53 +02003213 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003214 return ret;
3215}
3216
Andrew Lunn03a4a542016-06-04 21:17:05 +02003217static int mv88e6xxx_mdio_write(struct dsa_switch *ds, int port, int regnum,
3218 u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003219{
3220 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003221 int addr = mv88e6xxx_port_to_mdio_addr(ps, port);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003222 int ret;
3223
3224 if (addr < 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04003225 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003226
Andrew Lunn3898c142015-05-06 01:09:53 +02003227 mutex_lock(&ps->smi_mutex);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003228
3229 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003230 ret = mv88e6xxx_mdio_write_ppu(ps, addr, regnum, val);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003231 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
Andrew Lunn03a4a542016-06-04 21:17:05 +02003232 ret = mv88e6xxx_mdio_write_indirect(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003233 else
Andrew Lunn03a4a542016-06-04 21:17:05 +02003234 ret = mv88e6xxx_mdio_write_direct(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003235
Andrew Lunn3898c142015-05-06 01:09:53 +02003236 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003237 return ret;
3238}
3239
Guenter Roeckc22995c2015-07-25 09:42:28 -07003240#ifdef CONFIG_NET_DSA_HWMON
3241
3242static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3243{
3244 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3245 int ret;
3246 int val;
3247
3248 *temp = 0;
3249
3250 mutex_lock(&ps->smi_mutex);
3251
Andrew Lunn03a4a542016-06-04 21:17:05 +02003252 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003253 if (ret < 0)
3254 goto error;
3255
3256 /* Enable temperature sensor */
Andrew Lunn03a4a542016-06-04 21:17:05 +02003257 ret = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003258 if (ret < 0)
3259 goto error;
3260
Andrew Lunn03a4a542016-06-04 21:17:05 +02003261 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003262 if (ret < 0)
3263 goto error;
3264
3265 /* Wait for temperature to stabilize */
3266 usleep_range(10000, 12000);
3267
Andrew Lunn03a4a542016-06-04 21:17:05 +02003268 val = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003269 if (val < 0) {
3270 ret = val;
3271 goto error;
3272 }
3273
3274 /* Disable temperature sensor */
Andrew Lunn03a4a542016-06-04 21:17:05 +02003275 ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003276 if (ret < 0)
3277 goto error;
3278
3279 *temp = ((val & 0x1f) - 5) * 5;
3280
3281error:
Andrew Lunn03a4a542016-06-04 21:17:05 +02003282 mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x0);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003283 mutex_unlock(&ps->smi_mutex);
3284 return ret;
3285}
3286
3287static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3288{
Andrew Lunn158bc062016-04-28 21:24:06 -04003289 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3290 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003291 int ret;
3292
3293 *temp = 0;
3294
Andrew Lunn03a4a542016-06-04 21:17:05 +02003295 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003296 if (ret < 0)
3297 return ret;
3298
3299 *temp = (ret & 0xff) - 25;
3300
3301 return 0;
3302}
3303
Vivien Didelotf81ec902016-05-09 13:22:58 -04003304static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003305{
Andrew Lunn158bc062016-04-28 21:24:06 -04003306 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3307
Vivien Didelot6594f612016-05-09 13:22:42 -04003308 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP))
3309 return -EOPNOTSUPP;
3310
Andrew Lunn158bc062016-04-28 21:24:06 -04003311 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003312 return mv88e63xx_get_temp(ds, temp);
3313
3314 return mv88e61xx_get_temp(ds, temp);
3315}
3316
Vivien Didelotf81ec902016-05-09 13:22:58 -04003317static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003318{
Andrew Lunn158bc062016-04-28 21:24:06 -04003319 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3320 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003321 int ret;
3322
Vivien Didelot6594f612016-05-09 13:22:42 -04003323 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003324 return -EOPNOTSUPP;
3325
3326 *temp = 0;
3327
Andrew Lunn03a4a542016-06-04 21:17:05 +02003328 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003329 if (ret < 0)
3330 return ret;
3331
3332 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3333
3334 return 0;
3335}
3336
Vivien Didelotf81ec902016-05-09 13:22:58 -04003337static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003338{
Andrew Lunn158bc062016-04-28 21:24:06 -04003339 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3340 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003341 int ret;
3342
Vivien Didelot6594f612016-05-09 13:22:42 -04003343 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003344 return -EOPNOTSUPP;
3345
Andrew Lunn03a4a542016-06-04 21:17:05 +02003346 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003347 if (ret < 0)
3348 return ret;
3349 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn03a4a542016-06-04 21:17:05 +02003350 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3351 (ret & 0xe0ff) | (temp << 8));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003352}
3353
Vivien Didelotf81ec902016-05-09 13:22:58 -04003354static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003355{
Andrew Lunn158bc062016-04-28 21:24:06 -04003356 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3357 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003358 int ret;
3359
Vivien Didelot6594f612016-05-09 13:22:42 -04003360 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003361 return -EOPNOTSUPP;
3362
3363 *alarm = false;
3364
Andrew Lunn03a4a542016-06-04 21:17:05 +02003365 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003366 if (ret < 0)
3367 return ret;
3368
3369 *alarm = !!(ret & 0x40);
3370
3371 return 0;
3372}
3373#endif /* CONFIG_NET_DSA_HWMON */
3374
Vivien Didelotf81ec902016-05-09 13:22:58 -04003375static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3376 [MV88E6085] = {
3377 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3378 .family = MV88E6XXX_FAMILY_6097,
3379 .name = "Marvell 88E6085",
3380 .num_databases = 4096,
3381 .num_ports = 10,
3382 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3383 },
3384
3385 [MV88E6095] = {
3386 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3387 .family = MV88E6XXX_FAMILY_6095,
3388 .name = "Marvell 88E6095/88E6095F",
3389 .num_databases = 256,
3390 .num_ports = 11,
3391 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3392 },
3393
3394 [MV88E6123] = {
3395 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3396 .family = MV88E6XXX_FAMILY_6165,
3397 .name = "Marvell 88E6123",
3398 .num_databases = 4096,
3399 .num_ports = 3,
3400 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3401 },
3402
3403 [MV88E6131] = {
3404 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3405 .family = MV88E6XXX_FAMILY_6185,
3406 .name = "Marvell 88E6131",
3407 .num_databases = 256,
3408 .num_ports = 8,
3409 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3410 },
3411
3412 [MV88E6161] = {
3413 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3414 .family = MV88E6XXX_FAMILY_6165,
3415 .name = "Marvell 88E6161",
3416 .num_databases = 4096,
3417 .num_ports = 6,
3418 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3419 },
3420
3421 [MV88E6165] = {
3422 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3423 .family = MV88E6XXX_FAMILY_6165,
3424 .name = "Marvell 88E6165",
3425 .num_databases = 4096,
3426 .num_ports = 6,
3427 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3428 },
3429
3430 [MV88E6171] = {
3431 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3432 .family = MV88E6XXX_FAMILY_6351,
3433 .name = "Marvell 88E6171",
3434 .num_databases = 4096,
3435 .num_ports = 7,
3436 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3437 },
3438
3439 [MV88E6172] = {
3440 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3441 .family = MV88E6XXX_FAMILY_6352,
3442 .name = "Marvell 88E6172",
3443 .num_databases = 4096,
3444 .num_ports = 7,
3445 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3446 },
3447
3448 [MV88E6175] = {
3449 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3450 .family = MV88E6XXX_FAMILY_6351,
3451 .name = "Marvell 88E6175",
3452 .num_databases = 4096,
3453 .num_ports = 7,
3454 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3455 },
3456
3457 [MV88E6176] = {
3458 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3459 .family = MV88E6XXX_FAMILY_6352,
3460 .name = "Marvell 88E6176",
3461 .num_databases = 4096,
3462 .num_ports = 7,
3463 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3464 },
3465
3466 [MV88E6185] = {
3467 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3468 .family = MV88E6XXX_FAMILY_6185,
3469 .name = "Marvell 88E6185",
3470 .num_databases = 256,
3471 .num_ports = 10,
3472 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3473 },
3474
3475 [MV88E6240] = {
3476 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3477 .family = MV88E6XXX_FAMILY_6352,
3478 .name = "Marvell 88E6240",
3479 .num_databases = 4096,
3480 .num_ports = 7,
3481 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3482 },
3483
3484 [MV88E6320] = {
3485 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3486 .family = MV88E6XXX_FAMILY_6320,
3487 .name = "Marvell 88E6320",
3488 .num_databases = 4096,
3489 .num_ports = 7,
3490 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3491 },
3492
3493 [MV88E6321] = {
3494 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3495 .family = MV88E6XXX_FAMILY_6320,
3496 .name = "Marvell 88E6321",
3497 .num_databases = 4096,
3498 .num_ports = 7,
3499 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3500 },
3501
3502 [MV88E6350] = {
3503 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3504 .family = MV88E6XXX_FAMILY_6351,
3505 .name = "Marvell 88E6350",
3506 .num_databases = 4096,
3507 .num_ports = 7,
3508 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3509 },
3510
3511 [MV88E6351] = {
3512 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3513 .family = MV88E6XXX_FAMILY_6351,
3514 .name = "Marvell 88E6351",
3515 .num_databases = 4096,
3516 .num_ports = 7,
3517 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3518 },
3519
3520 [MV88E6352] = {
3521 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3522 .family = MV88E6XXX_FAMILY_6352,
3523 .name = "Marvell 88E6352",
3524 .num_databases = 4096,
3525 .num_ports = 7,
3526 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3527 },
3528};
3529
Vivien Didelotf6271e62016-04-17 13:23:59 -04003530static const struct mv88e6xxx_info *
3531mv88e6xxx_lookup_info(unsigned int prod_num, const struct mv88e6xxx_info *table,
Vivien Didelot0209d142016-04-17 13:23:55 -04003532 unsigned int num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003533{
Vivien Didelota439c062016-04-17 13:23:58 -04003534 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003535
Vivien Didelotb9b37712015-10-30 19:39:48 -04003536 for (i = 0; i < num; ++i)
Vivien Didelotf6271e62016-04-17 13:23:59 -04003537 if (table[i].prod_num == prod_num)
3538 return &table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003539
Vivien Didelotb9b37712015-10-30 19:39:48 -04003540 return NULL;
3541}
3542
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003543static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3544 struct device *host_dev, int sw_addr,
3545 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003546{
Vivien Didelotf6271e62016-04-17 13:23:59 -04003547 const struct mv88e6xxx_info *info;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003548 struct mv88e6xxx_priv_state *ps;
Vivien Didelota439c062016-04-17 13:23:58 -04003549 struct mii_bus *bus;
Vivien Didelot0209d142016-04-17 13:23:55 -04003550 const char *name;
Vivien Didelota439c062016-04-17 13:23:58 -04003551 int id, prod_num, rev;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003552
Vivien Didelota439c062016-04-17 13:23:58 -04003553 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003554 if (!bus)
3555 return NULL;
3556
Vivien Didelota439c062016-04-17 13:23:58 -04003557 id = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
3558 if (id < 0)
3559 return NULL;
3560
3561 prod_num = (id & 0xfff0) >> 4;
3562 rev = id & 0x000f;
3563
Vivien Didelotf81ec902016-05-09 13:22:58 -04003564 info = mv88e6xxx_lookup_info(prod_num, mv88e6xxx_table,
3565 ARRAY_SIZE(mv88e6xxx_table));
Vivien Didelotf6271e62016-04-17 13:23:59 -04003566 if (!info)
Vivien Didelota439c062016-04-17 13:23:58 -04003567 return NULL;
3568
Vivien Didelotf6271e62016-04-17 13:23:59 -04003569 name = info->name;
3570
Vivien Didelota439c062016-04-17 13:23:58 -04003571 ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
3572 if (!ps)
3573 return NULL;
3574
3575 ps->bus = bus;
3576 ps->sw_addr = sw_addr;
Vivien Didelotf6271e62016-04-17 13:23:59 -04003577 ps->info = info;
Andrew Lunnb6819572016-05-10 23:27:19 +02003578 mutex_init(&ps->smi_mutex);
Vivien Didelota439c062016-04-17 13:23:58 -04003579
3580 *priv = ps;
3581
3582 dev_info(&ps->bus->dev, "switch 0x%x probed: %s, revision %u\n",
3583 prod_num, name, rev);
3584
Andrew Lunna77d43f2016-04-13 02:40:42 +02003585 return name;
3586}
3587
Vivien Didelotf81ec902016-05-09 13:22:58 -04003588struct dsa_switch_driver mv88e6xxx_switch_driver = {
3589 .tag_protocol = DSA_TAG_PROTO_EDSA,
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003590 .probe = mv88e6xxx_drv_probe,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003591 .setup = mv88e6xxx_setup,
3592 .set_addr = mv88e6xxx_set_addr,
Andrew Lunn03a4a542016-06-04 21:17:05 +02003593 .phy_read = mv88e6xxx_mdio_read,
3594 .phy_write = mv88e6xxx_mdio_write,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003595 .adjust_link = mv88e6xxx_adjust_link,
3596 .get_strings = mv88e6xxx_get_strings,
3597 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3598 .get_sset_count = mv88e6xxx_get_sset_count,
3599 .set_eee = mv88e6xxx_set_eee,
3600 .get_eee = mv88e6xxx_get_eee,
3601#ifdef CONFIG_NET_DSA_HWMON
3602 .get_temp = mv88e6xxx_get_temp,
3603 .get_temp_limit = mv88e6xxx_get_temp_limit,
3604 .set_temp_limit = mv88e6xxx_set_temp_limit,
3605 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3606#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003607 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003608 .get_eeprom = mv88e6xxx_get_eeprom,
3609 .set_eeprom = mv88e6xxx_set_eeprom,
3610 .get_regs_len = mv88e6xxx_get_regs_len,
3611 .get_regs = mv88e6xxx_get_regs,
3612 .port_bridge_join = mv88e6xxx_port_bridge_join,
3613 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3614 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3615 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3616 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3617 .port_vlan_add = mv88e6xxx_port_vlan_add,
3618 .port_vlan_del = mv88e6xxx_port_vlan_del,
3619 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3620 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3621 .port_fdb_add = mv88e6xxx_port_fdb_add,
3622 .port_fdb_del = mv88e6xxx_port_fdb_del,
3623 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3624};
3625
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003626int mv88e6xxx_probe(struct mdio_device *mdiodev)
3627{
3628 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003629 struct device_node *np = dev->of_node;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003630 struct mv88e6xxx_priv_state *ps;
3631 int id, prod_num, rev;
3632 struct dsa_switch *ds;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003633 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02003634 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003635
3636 ds = devm_kzalloc(dev, sizeof(*ds) + sizeof(*ps), GFP_KERNEL);
3637 if (!ds)
3638 return -ENOMEM;
3639
3640 ps = (struct mv88e6xxx_priv_state *)(ds + 1);
3641 ds->priv = ps;
Andrew Lunnc33063d2016-05-10 23:27:23 +02003642 ds->dev = dev;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003643 ps->dev = dev;
3644 ps->ds = ds;
3645 ps->bus = mdiodev->bus;
3646 ps->sw_addr = mdiodev->addr;
3647 mutex_init(&ps->smi_mutex);
3648
3649 get_device(&ps->bus->dev);
3650
3651 ds->drv = &mv88e6xxx_switch_driver;
3652
3653 id = mv88e6xxx_reg_read(ps, REG_PORT(0), PORT_SWITCH_ID);
3654 if (id < 0)
3655 return id;
3656
3657 prod_num = (id & 0xfff0) >> 4;
3658 rev = id & 0x000f;
3659
3660 ps->info = mv88e6xxx_lookup_info(prod_num, mv88e6xxx_table,
3661 ARRAY_SIZE(mv88e6xxx_table));
3662 if (!ps->info)
3663 return -ENODEV;
3664
Andrew Lunn52638f72016-05-10 23:27:22 +02003665 ps->reset = devm_gpiod_get(&mdiodev->dev, "reset", GPIOD_ASIS);
3666 if (IS_ERR(ps->reset)) {
3667 err = PTR_ERR(ps->reset);
3668 if (err == -ENOENT) {
3669 /* Optional, so not an error */
3670 ps->reset = NULL;
3671 } else {
3672 return err;
3673 }
3674 }
3675
Andrew Lunnf8cd8752016-05-10 23:27:25 +02003676 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM) &&
3677 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
3678 ps->eeprom_len = eeprom_len;
3679
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003680 dev_set_drvdata(dev, ds);
3681
3682 dev_info(dev, "switch 0x%x probed: %s, revision %u\n",
3683 prod_num, ps->info->name, rev);
3684
3685 return 0;
3686}
3687
3688static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3689{
3690 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
3691 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3692
3693 put_device(&ps->bus->dev);
3694}
3695
3696static const struct of_device_id mv88e6xxx_of_match[] = {
3697 { .compatible = "marvell,mv88e6085" },
3698 { /* sentinel */ },
3699};
3700
3701MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3702
3703static struct mdio_driver mv88e6xxx_driver = {
3704 .probe = mv88e6xxx_probe,
3705 .remove = mv88e6xxx_remove,
3706 .mdiodrv.driver = {
3707 .name = "mv88e6085",
3708 .of_match_table = mv88e6xxx_of_match,
3709 },
3710};
3711
Ben Hutchings98e67302011-11-25 14:36:19 +00003712static int __init mv88e6xxx_init(void)
3713{
Vivien Didelotf81ec902016-05-09 13:22:58 -04003714 register_switch_driver(&mv88e6xxx_switch_driver);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003715 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003716}
3717module_init(mv88e6xxx_init);
3718
3719static void __exit mv88e6xxx_cleanup(void)
3720{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02003721 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelotf81ec902016-05-09 13:22:58 -04003722 unregister_switch_driver(&mv88e6xxx_switch_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00003723}
3724module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00003725
3726MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3727MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3728MODULE_LICENSE("GPL");