blob: c361036e7f9cce76688b39c58f8419d08ae0a8f9 [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 Lunn158bc062016-04-28 21:24:06 -0400241static int _mv88e6xxx_phy_read(struct mv88e6xxx_priv_state *ps, int addr,
242 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 Lunn158bc062016-04-28 21:24:06 -0400249static int _mv88e6xxx_phy_write(struct mv88e6xxx_priv_state *ps, int addr,
250 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
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400381static int mv88e6xxx_phy_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
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400395static int mv88e6xxx_phy_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 Lunn158bc062016-04-28 21:24:06 -0400832static int _mv88e6xxx_phy_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 Lunn158bc062016-04-28 21:24:06 -04001079static int _mv88e6xxx_phy_read_indirect(struct mv88e6xxx_priv_state *ps,
1080 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 Lunn158bc062016-04-28 21:24:06 -04001090 ret = _mv88e6xxx_phy_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 Lunn158bc062016-04-28 21:24:06 -04001099static int _mv88e6xxx_phy_write_indirect(struct mv88e6xxx_priv_state *ps,
1100 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 Lunn158bc062016-04-28 21:24:06 -04001112 return _mv88e6xxx_phy_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 Lunn158bc062016-04-28 21:24:06 -04001126 reg = _mv88e6xxx_phy_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 Lunn158bc062016-04-28 21:24:06 -04001157 ret = _mv88e6xxx_phy_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 Lunn158bc062016-04-28 21:24:06 -04001167 ret = _mv88e6xxx_phy_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
1330 netdev_dbg(ds->ports[port], "PortState %s (was %s)\n",
1331 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)
1408 netdev_err(ds->ports[port], "failed to update state to %s\n",
1409 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001410}
1411
Andrew Lunn158bc062016-04-28 21:24:06 -04001412static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port,
1413 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001414{
Andrew Lunn158bc062016-04-28 21:24:06 -04001415 struct dsa_switch *ds = ps->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001416 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001417 int ret;
1418
Andrew Lunn158bc062016-04-28 21:24:06 -04001419 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001420 if (ret < 0)
1421 return ret;
1422
Vivien Didelot5da96032016-03-07 18:24:39 -05001423 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1424
1425 if (new) {
1426 ret &= ~PORT_DEFAULT_VLAN_MASK;
1427 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1428
Andrew Lunn158bc062016-04-28 21:24:06 -04001429 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001430 PORT_DEFAULT_VLAN, ret);
1431 if (ret < 0)
1432 return ret;
1433
1434 netdev_dbg(ds->ports[port], "DefaultVID %d (was %d)\n", *new,
1435 pvid);
1436 }
1437
1438 if (old)
1439 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001440
1441 return 0;
1442}
1443
Andrew Lunn158bc062016-04-28 21:24:06 -04001444static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps,
1445 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001446{
Andrew Lunn158bc062016-04-28 21:24:06 -04001447 return _mv88e6xxx_port_pvid(ps, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001448}
1449
Andrew Lunn158bc062016-04-28 21:24:06 -04001450static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps,
1451 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001452{
Andrew Lunn158bc062016-04-28 21:24:06 -04001453 return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001454}
1455
Andrew Lunn158bc062016-04-28 21:24:06 -04001456static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001457{
Andrew Lunn158bc062016-04-28 21:24:06 -04001458 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP,
Vivien Didelot6b17e862015-08-13 12:52:18 -04001459 GLOBAL_VTU_OP_BUSY);
1460}
1461
Andrew Lunn158bc062016-04-28 21:24:06 -04001462static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001463{
1464 int ret;
1465
Andrew Lunn158bc062016-04-28 21:24:06 -04001466 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001467 if (ret < 0)
1468 return ret;
1469
Andrew Lunn158bc062016-04-28 21:24:06 -04001470 return _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001471}
1472
Andrew Lunn158bc062016-04-28 21:24:06 -04001473static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001474{
1475 int ret;
1476
Andrew Lunn158bc062016-04-28 21:24:06 -04001477 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001478 if (ret < 0)
1479 return ret;
1480
Andrew Lunn158bc062016-04-28 21:24:06 -04001481 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001482}
1483
Andrew Lunn158bc062016-04-28 21:24:06 -04001484static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001485 struct mv88e6xxx_vtu_stu_entry *entry,
1486 unsigned int nibble_offset)
1487{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001488 u16 regs[3];
1489 int i;
1490 int ret;
1491
1492 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001493 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001494 GLOBAL_VTU_DATA_0_3 + i);
1495 if (ret < 0)
1496 return ret;
1497
1498 regs[i] = ret;
1499 }
1500
Vivien Didelot009a2b92016-04-17 13:24:01 -04001501 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001502 unsigned int shift = (i % 4) * 4 + nibble_offset;
1503 u16 reg = regs[i / 4];
1504
1505 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1506 }
1507
1508 return 0;
1509}
1510
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001511static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_priv_state *ps,
1512 struct mv88e6xxx_vtu_stu_entry *entry)
1513{
1514 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 0);
1515}
1516
1517static int mv88e6xxx_stu_data_read(struct mv88e6xxx_priv_state *ps,
1518 struct mv88e6xxx_vtu_stu_entry *entry)
1519{
1520 return _mv88e6xxx_vtu_stu_data_read(ps, entry, 2);
1521}
1522
Andrew Lunn158bc062016-04-28 21:24:06 -04001523static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001524 struct mv88e6xxx_vtu_stu_entry *entry,
1525 unsigned int nibble_offset)
1526{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001527 u16 regs[3] = { 0 };
1528 int i;
1529 int ret;
1530
Vivien Didelot009a2b92016-04-17 13:24:01 -04001531 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001532 unsigned int shift = (i % 4) * 4 + nibble_offset;
1533 u8 data = entry->data[i];
1534
1535 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1536 }
1537
1538 for (i = 0; i < 3; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001539 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001540 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1541 if (ret < 0)
1542 return ret;
1543 }
1544
1545 return 0;
1546}
1547
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001548static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_priv_state *ps,
1549 struct mv88e6xxx_vtu_stu_entry *entry)
1550{
1551 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 0);
1552}
1553
1554static int mv88e6xxx_stu_data_write(struct mv88e6xxx_priv_state *ps,
1555 struct mv88e6xxx_vtu_stu_entry *entry)
1556{
1557 return _mv88e6xxx_vtu_stu_data_write(ps, entry, 2);
1558}
1559
Andrew Lunn158bc062016-04-28 21:24:06 -04001560static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001561{
Andrew Lunn158bc062016-04-28 21:24:06 -04001562 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001563 vid & GLOBAL_VTU_VID_MASK);
1564}
1565
Andrew Lunn158bc062016-04-28 21:24:06 -04001566static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001567 struct mv88e6xxx_vtu_stu_entry *entry)
1568{
1569 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1570 int ret;
1571
Andrew Lunn158bc062016-04-28 21:24:06 -04001572 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001573 if (ret < 0)
1574 return ret;
1575
Andrew Lunn158bc062016-04-28 21:24:06 -04001576 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001577 if (ret < 0)
1578 return ret;
1579
Andrew Lunn158bc062016-04-28 21:24:06 -04001580 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001581 if (ret < 0)
1582 return ret;
1583
1584 next.vid = ret & GLOBAL_VTU_VID_MASK;
1585 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1586
1587 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001588 ret = mv88e6xxx_vtu_data_read(ps, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001589 if (ret < 0)
1590 return ret;
1591
Andrew Lunn158bc062016-04-28 21:24:06 -04001592 if (mv88e6xxx_has_fid_reg(ps)) {
1593 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001594 GLOBAL_VTU_FID);
1595 if (ret < 0)
1596 return ret;
1597
1598 next.fid = ret & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001599 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001600 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1601 * VTU DBNum[3:0] are located in VTU Operation 3:0
1602 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001603 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001604 GLOBAL_VTU_OP);
1605 if (ret < 0)
1606 return ret;
1607
1608 next.fid = (ret & 0xf00) >> 4;
1609 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001610 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001611
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001612 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001613 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001614 GLOBAL_VTU_SID);
1615 if (ret < 0)
1616 return ret;
1617
1618 next.sid = ret & GLOBAL_VTU_SID_MASK;
1619 }
1620 }
1621
1622 *entry = next;
1623 return 0;
1624}
1625
Vivien Didelotf81ec902016-05-09 13:22:58 -04001626static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1627 struct switchdev_obj_port_vlan *vlan,
1628 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001629{
1630 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1631 struct mv88e6xxx_vtu_stu_entry next;
1632 u16 pvid;
1633 int err;
1634
Vivien Didelot54d77b52016-05-09 13:22:47 -04001635 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
1636 return -EOPNOTSUPP;
1637
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001638 mutex_lock(&ps->smi_mutex);
1639
Andrew Lunn158bc062016-04-28 21:24:06 -04001640 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001641 if (err)
1642 goto unlock;
1643
Andrew Lunn158bc062016-04-28 21:24:06 -04001644 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001645 if (err)
1646 goto unlock;
1647
1648 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001649 err = _mv88e6xxx_vtu_getnext(ps, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001650 if (err)
1651 break;
1652
1653 if (!next.valid)
1654 break;
1655
1656 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1657 continue;
1658
1659 /* reinit and dump this VLAN obj */
1660 vlan->vid_begin = vlan->vid_end = next.vid;
1661 vlan->flags = 0;
1662
1663 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1664 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1665
1666 if (next.vid == pvid)
1667 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1668
1669 err = cb(&vlan->obj);
1670 if (err)
1671 break;
1672 } while (next.vid < GLOBAL_VTU_VID_MASK);
1673
1674unlock:
1675 mutex_unlock(&ps->smi_mutex);
1676
1677 return err;
1678}
1679
Andrew Lunn158bc062016-04-28 21:24:06 -04001680static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001681 struct mv88e6xxx_vtu_stu_entry *entry)
1682{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001683 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001684 u16 reg = 0;
1685 int ret;
1686
Andrew Lunn158bc062016-04-28 21:24:06 -04001687 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001688 if (ret < 0)
1689 return ret;
1690
1691 if (!entry->valid)
1692 goto loadpurge;
1693
1694 /* Write port member tags */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001695 ret = mv88e6xxx_vtu_data_write(ps, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001696 if (ret < 0)
1697 return ret;
1698
Vivien Didelotcb9b9022016-05-10 15:44:29 -04001699 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001700 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001701 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001702 if (ret < 0)
1703 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001704 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001705
Andrew Lunn158bc062016-04-28 21:24:06 -04001706 if (mv88e6xxx_has_fid_reg(ps)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001707 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001708 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001709 if (ret < 0)
1710 return ret;
Andrew Lunn158bc062016-04-28 21:24:06 -04001711 } else if (mv88e6xxx_num_databases(ps) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001712 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1713 * VTU DBNum[3:0] are located in VTU Operation 3:0
1714 */
1715 op |= (entry->fid & 0xf0) << 8;
1716 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001717 }
1718
1719 reg = GLOBAL_VTU_VID_VALID;
1720loadpurge:
1721 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001722 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001723 if (ret < 0)
1724 return ret;
1725
Andrew Lunn158bc062016-04-28 21:24:06 -04001726 return _mv88e6xxx_vtu_cmd(ps, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001727}
1728
Andrew Lunn158bc062016-04-28 21:24:06 -04001729static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001730 struct mv88e6xxx_vtu_stu_entry *entry)
1731{
1732 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1733 int ret;
1734
Andrew Lunn158bc062016-04-28 21:24:06 -04001735 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001736 if (ret < 0)
1737 return ret;
1738
Andrew Lunn158bc062016-04-28 21:24:06 -04001739 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001740 sid & GLOBAL_VTU_SID_MASK);
1741 if (ret < 0)
1742 return ret;
1743
Andrew Lunn158bc062016-04-28 21:24:06 -04001744 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001745 if (ret < 0)
1746 return ret;
1747
Andrew Lunn158bc062016-04-28 21:24:06 -04001748 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001749 if (ret < 0)
1750 return ret;
1751
1752 next.sid = ret & GLOBAL_VTU_SID_MASK;
1753
Andrew Lunn158bc062016-04-28 21:24:06 -04001754 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001755 if (ret < 0)
1756 return ret;
1757
1758 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1759
1760 if (next.valid) {
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001761 ret = mv88e6xxx_stu_data_read(ps, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001762 if (ret < 0)
1763 return ret;
1764 }
1765
1766 *entry = next;
1767 return 0;
1768}
1769
Andrew Lunn158bc062016-04-28 21:24:06 -04001770static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001771 struct mv88e6xxx_vtu_stu_entry *entry)
1772{
1773 u16 reg = 0;
1774 int ret;
1775
Andrew Lunn158bc062016-04-28 21:24:06 -04001776 ret = _mv88e6xxx_vtu_wait(ps);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001777 if (ret < 0)
1778 return ret;
1779
1780 if (!entry->valid)
1781 goto loadpurge;
1782
1783 /* Write port states */
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001784 ret = mv88e6xxx_stu_data_write(ps, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001785 if (ret < 0)
1786 return ret;
1787
1788 reg = GLOBAL_VTU_VID_VALID;
1789loadpurge:
Andrew Lunn158bc062016-04-28 21:24:06 -04001790 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001791 if (ret < 0)
1792 return ret;
1793
1794 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Andrew Lunn158bc062016-04-28 21:24:06 -04001795 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001796 if (ret < 0)
1797 return ret;
1798
Andrew Lunn158bc062016-04-28 21:24:06 -04001799 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001800}
1801
Andrew Lunn158bc062016-04-28 21:24:06 -04001802static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port,
1803 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001804{
Andrew Lunn158bc062016-04-28 21:24:06 -04001805 struct dsa_switch *ds = ps->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001806 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001807 u16 fid;
1808 int ret;
1809
Andrew Lunn158bc062016-04-28 21:24:06 -04001810 if (mv88e6xxx_num_databases(ps) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001811 upper_mask = 0xff;
Andrew Lunn158bc062016-04-28 21:24:06 -04001812 else if (mv88e6xxx_num_databases(ps) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001813 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001814 else
1815 return -EOPNOTSUPP;
1816
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001817 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001818 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001819 if (ret < 0)
1820 return ret;
1821
1822 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1823
1824 if (new) {
1825 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1826 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1827
Andrew Lunn158bc062016-04-28 21:24:06 -04001828 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001829 ret);
1830 if (ret < 0)
1831 return ret;
1832 }
1833
1834 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Andrew Lunn158bc062016-04-28 21:24:06 -04001835 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001836 if (ret < 0)
1837 return ret;
1838
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001839 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001840
1841 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001842 ret &= ~upper_mask;
1843 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001844
Andrew Lunn158bc062016-04-28 21:24:06 -04001845 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001846 ret);
1847 if (ret < 0)
1848 return ret;
1849
1850 netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid);
1851 }
1852
1853 if (old)
1854 *old = fid;
1855
1856 return 0;
1857}
1858
Andrew Lunn158bc062016-04-28 21:24:06 -04001859static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps,
1860 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001861{
Andrew Lunn158bc062016-04-28 21:24:06 -04001862 return _mv88e6xxx_port_fid(ps, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001863}
1864
Andrew Lunn158bc062016-04-28 21:24:06 -04001865static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps,
1866 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001867{
Andrew Lunn158bc062016-04-28 21:24:06 -04001868 return _mv88e6xxx_port_fid(ps, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001869}
1870
Andrew Lunn158bc062016-04-28 21:24:06 -04001871static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001872{
1873 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1874 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001875 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001876
1877 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1878
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001879 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001880 for (i = 0; i < ps->info->num_ports; ++i) {
Andrew Lunn158bc062016-04-28 21:24:06 -04001881 err = _mv88e6xxx_port_fid_get(ps, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001882 if (err)
1883 return err;
1884
1885 set_bit(*fid, fid_bitmap);
1886 }
1887
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001888 /* Set every FID bit used by the VLAN entries */
Andrew Lunn158bc062016-04-28 21:24:06 -04001889 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001890 if (err)
1891 return err;
1892
1893 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04001894 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001895 if (err)
1896 return err;
1897
1898 if (!vlan.valid)
1899 break;
1900
1901 set_bit(vlan.fid, fid_bitmap);
1902 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1903
1904 /* The reset value 0x000 is used to indicate that multiple address
1905 * databases are not needed. Return the next positive available.
1906 */
1907 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Andrew Lunn158bc062016-04-28 21:24:06 -04001908 if (unlikely(*fid >= mv88e6xxx_num_databases(ps)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001909 return -ENOSPC;
1910
1911 /* Clear the database */
Andrew Lunn158bc062016-04-28 21:24:06 -04001912 return _mv88e6xxx_atu_flush(ps, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001913}
1914
Andrew Lunn158bc062016-04-28 21:24:06 -04001915static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001916 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001917{
Andrew Lunn158bc062016-04-28 21:24:06 -04001918 struct dsa_switch *ds = ps->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001919 struct mv88e6xxx_vtu_stu_entry vlan = {
1920 .valid = true,
1921 .vid = vid,
1922 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001923 int i, err;
1924
Andrew Lunn158bc062016-04-28 21:24:06 -04001925 err = _mv88e6xxx_fid_new(ps, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001926 if (err)
1927 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001928
Vivien Didelot3d131f02015-11-03 10:52:52 -05001929 /* exclude all ports except the CPU and DSA ports */
Vivien Didelot009a2b92016-04-17 13:24:01 -04001930 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001931 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1932 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1933 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001934
Andrew Lunn158bc062016-04-28 21:24:06 -04001935 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
1936 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001937 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001938
1939 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1940 * implemented, only one STU entry is needed to cover all VTU
1941 * entries. Thus, validate the SID 0.
1942 */
1943 vlan.sid = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04001944 err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001945 if (err)
1946 return err;
1947
1948 if (vstp.sid != vlan.sid || !vstp.valid) {
1949 memset(&vstp, 0, sizeof(vstp));
1950 vstp.valid = true;
1951 vstp.sid = vlan.sid;
1952
Andrew Lunn158bc062016-04-28 21:24:06 -04001953 err = _mv88e6xxx_stu_loadpurge(ps, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001954 if (err)
1955 return err;
1956 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001957 }
1958
1959 *entry = vlan;
1960 return 0;
1961}
1962
Andrew Lunn158bc062016-04-28 21:24:06 -04001963static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001964 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1965{
1966 int err;
1967
1968 if (!vid)
1969 return -EINVAL;
1970
Andrew Lunn158bc062016-04-28 21:24:06 -04001971 err = _mv88e6xxx_vtu_vid_write(ps, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001972 if (err)
1973 return err;
1974
Andrew Lunn158bc062016-04-28 21:24:06 -04001975 err = _mv88e6xxx_vtu_getnext(ps, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001976 if (err)
1977 return err;
1978
1979 if (entry->vid != vid || !entry->valid) {
1980 if (!creat)
1981 return -EOPNOTSUPP;
1982 /* -ENOENT would've been more appropriate, but switchdev expects
1983 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1984 */
1985
Andrew Lunn158bc062016-04-28 21:24:06 -04001986 err = _mv88e6xxx_vtu_new(ps, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001987 }
1988
1989 return err;
1990}
1991
Vivien Didelotda9c3592016-02-12 12:09:40 -05001992static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1993 u16 vid_begin, u16 vid_end)
1994{
1995 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1996 struct mv88e6xxx_vtu_stu_entry vlan;
1997 int i, err;
1998
1999 if (!vid_begin)
2000 return -EOPNOTSUPP;
2001
2002 mutex_lock(&ps->smi_mutex);
2003
Andrew Lunn158bc062016-04-28 21:24:06 -04002004 err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002005 if (err)
2006 goto unlock;
2007
2008 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002009 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002010 if (err)
2011 goto unlock;
2012
2013 if (!vlan.valid)
2014 break;
2015
2016 if (vlan.vid > vid_end)
2017 break;
2018
Vivien Didelot009a2b92016-04-17 13:24:01 -04002019 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05002020 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
2021 continue;
2022
2023 if (vlan.data[i] ==
2024 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2025 continue;
2026
2027 if (ps->ports[i].bridge_dev ==
2028 ps->ports[port].bridge_dev)
2029 break; /* same bridge, check next VLAN */
2030
2031 netdev_warn(ds->ports[port],
2032 "hardware VLAN %d already used by %s\n",
2033 vlan.vid,
2034 netdev_name(ps->ports[i].bridge_dev));
2035 err = -EOPNOTSUPP;
2036 goto unlock;
2037 }
2038 } while (vlan.vid < vid_end);
2039
2040unlock:
2041 mutex_unlock(&ps->smi_mutex);
2042
2043 return err;
2044}
2045
Vivien Didelot214cdb92016-02-26 13:16:08 -05002046static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2047 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2048 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2049 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2050 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2051};
2052
Vivien Didelotf81ec902016-05-09 13:22:58 -04002053static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2054 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05002055{
2056 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2057 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2058 PORT_CONTROL_2_8021Q_DISABLED;
2059 int ret;
2060
Vivien Didelot54d77b52016-05-09 13:22:47 -04002061 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2062 return -EOPNOTSUPP;
2063
Vivien Didelot214cdb92016-02-26 13:16:08 -05002064 mutex_lock(&ps->smi_mutex);
2065
Andrew Lunn158bc062016-04-28 21:24:06 -04002066 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05002067 if (ret < 0)
2068 goto unlock;
2069
2070 old = ret & PORT_CONTROL_2_8021Q_MASK;
2071
Vivien Didelot5220ef12016-03-07 18:24:52 -05002072 if (new != old) {
2073 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2074 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002075
Andrew Lunn158bc062016-04-28 21:24:06 -04002076 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05002077 ret);
2078 if (ret < 0)
2079 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002080
Vivien Didelot5220ef12016-03-07 18:24:52 -05002081 netdev_dbg(ds->ports[port], "802.1Q Mode %s (was %s)\n",
2082 mv88e6xxx_port_8021q_mode_names[new],
2083 mv88e6xxx_port_8021q_mode_names[old]);
2084 }
2085
2086 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05002087unlock:
2088 mutex_unlock(&ps->smi_mutex);
2089
2090 return ret;
2091}
2092
Vivien Didelotf81ec902016-05-09 13:22:58 -04002093static int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2094 const struct switchdev_obj_port_vlan *vlan,
2095 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002096{
Vivien Didelot54d77b52016-05-09 13:22:47 -04002097 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotda9c3592016-02-12 12:09:40 -05002098 int err;
2099
Vivien Didelot54d77b52016-05-09 13:22:47 -04002100 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2101 return -EOPNOTSUPP;
2102
Vivien Didelotda9c3592016-02-12 12:09:40 -05002103 /* If the requested port doesn't belong to the same bridge as the VLAN
2104 * members, do not support it (yet) and fallback to software VLAN.
2105 */
2106 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2107 vlan->vid_end);
2108 if (err)
2109 return err;
2110
Vivien Didelot76e398a2015-11-01 12:33:55 -05002111 /* We don't need any dynamic resource from the kernel (yet),
2112 * so skip the prepare phase.
2113 */
2114 return 0;
2115}
2116
Andrew Lunn158bc062016-04-28 21:24:06 -04002117static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port,
2118 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002119{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002120 struct mv88e6xxx_vtu_stu_entry vlan;
2121 int err;
2122
Andrew Lunn158bc062016-04-28 21:24:06 -04002123 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002124 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002125 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002126
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002127 vlan.data[port] = untagged ?
2128 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2129 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2130
Andrew Lunn158bc062016-04-28 21:24:06 -04002131 return _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002132}
2133
Vivien Didelotf81ec902016-05-09 13:22:58 -04002134static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2135 const struct switchdev_obj_port_vlan *vlan,
2136 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002137{
2138 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2139 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2140 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2141 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002142
Vivien Didelot54d77b52016-05-09 13:22:47 -04002143 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2144 return;
2145
Vivien Didelot76e398a2015-11-01 12:33:55 -05002146 mutex_lock(&ps->smi_mutex);
2147
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002148 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Andrew Lunn158bc062016-04-28 21:24:06 -04002149 if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged))
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002150 netdev_err(ds->ports[port], "failed to add VLAN %d%c\n",
2151 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05002152
Andrew Lunn158bc062016-04-28 21:24:06 -04002153 if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end))
Vivien Didelot4d5770b2016-04-06 11:55:05 -04002154 netdev_err(ds->ports[port], "failed to set PVID %d\n",
2155 vlan->vid_end);
2156
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002157 mutex_unlock(&ps->smi_mutex);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04002158}
2159
Andrew Lunn158bc062016-04-28 21:24:06 -04002160static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps,
2161 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002162{
Andrew Lunn158bc062016-04-28 21:24:06 -04002163 struct dsa_switch *ds = ps->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002164 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002165 int i, err;
2166
Andrew Lunn158bc062016-04-28 21:24:06 -04002167 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002168 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002169 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04002170
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05002171 /* Tell switchdev if this VLAN is handled in software */
2172 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05002173 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002174
2175 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2176
2177 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002178 vlan.valid = false;
Vivien Didelot009a2b92016-04-17 13:24:01 -04002179 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05002180 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002181 continue;
2182
2183 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002184 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002185 break;
2186 }
2187 }
2188
Andrew Lunn158bc062016-04-28 21:24:06 -04002189 err = _mv88e6xxx_vtu_loadpurge(ps, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002190 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002191 return err;
2192
Andrew Lunn158bc062016-04-28 21:24:06 -04002193 return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002194}
2195
Vivien Didelotf81ec902016-05-09 13:22:58 -04002196static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2197 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002198{
2199 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2200 u16 pvid, vid;
2201 int err = 0;
2202
Vivien Didelot54d77b52016-05-09 13:22:47 -04002203 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2204 return -EOPNOTSUPP;
2205
Vivien Didelot76e398a2015-11-01 12:33:55 -05002206 mutex_lock(&ps->smi_mutex);
2207
Andrew Lunn158bc062016-04-28 21:24:06 -04002208 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002209 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002210 goto unlock;
2211
Vivien Didelot76e398a2015-11-01 12:33:55 -05002212 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002213 err = _mv88e6xxx_port_vlan_del(ps, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002214 if (err)
2215 goto unlock;
2216
2217 if (vid == pvid) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002218 err = _mv88e6xxx_port_pvid_set(ps, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002219 if (err)
2220 goto unlock;
2221 }
2222 }
2223
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002224unlock:
2225 mutex_unlock(&ps->smi_mutex);
2226
2227 return err;
2228}
2229
Andrew Lunn158bc062016-04-28 21:24:06 -04002230static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002231 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002232{
2233 int i, ret;
2234
2235 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002236 ret = _mv88e6xxx_reg_write(
Andrew Lunn158bc062016-04-28 21:24:06 -04002237 ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002238 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002239 if (ret < 0)
2240 return ret;
2241 }
2242
2243 return 0;
2244}
2245
Andrew Lunn158bc062016-04-28 21:24:06 -04002246static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps,
2247 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002248{
2249 int i, ret;
2250
2251 for (i = 0; i < 3; i++) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002252 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002253 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002254 if (ret < 0)
2255 return ret;
2256 addr[i * 2] = ret >> 8;
2257 addr[i * 2 + 1] = ret & 0xff;
2258 }
2259
2260 return 0;
2261}
2262
Andrew Lunn158bc062016-04-28 21:24:06 -04002263static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002264 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002265{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002266 int ret;
2267
Andrew Lunn158bc062016-04-28 21:24:06 -04002268 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002269 if (ret < 0)
2270 return ret;
2271
Andrew Lunn158bc062016-04-28 21:24:06 -04002272 ret = _mv88e6xxx_atu_mac_write(ps, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002273 if (ret < 0)
2274 return ret;
2275
Andrew Lunn158bc062016-04-28 21:24:06 -04002276 ret = _mv88e6xxx_atu_data_write(ps, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002277 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002278 return ret;
2279
Andrew Lunn158bc062016-04-28 21:24:06 -04002280 return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002281}
David S. Millercdf09692015-08-11 12:00:37 -07002282
Andrew Lunn158bc062016-04-28 21:24:06 -04002283static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002284 const unsigned char *addr, u16 vid,
2285 u8 state)
2286{
2287 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002288 struct mv88e6xxx_vtu_stu_entry vlan;
2289 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002290
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002291 /* Null VLAN ID corresponds to the port private database */
2292 if (vid == 0)
Andrew Lunn158bc062016-04-28 21:24:06 -04002293 err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002294 else
Andrew Lunn158bc062016-04-28 21:24:06 -04002295 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002296 if (err)
2297 return err;
2298
2299 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002300 entry.state = state;
2301 ether_addr_copy(entry.mac, addr);
2302 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2303 entry.trunk = false;
2304 entry.portv_trunkid = BIT(port);
2305 }
2306
Andrew Lunn158bc062016-04-28 21:24:06 -04002307 return _mv88e6xxx_atu_load(ps, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002308}
2309
Vivien Didelotf81ec902016-05-09 13:22:58 -04002310static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2311 const struct switchdev_obj_port_fdb *fdb,
2312 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002313{
Vivien Didelot2672f822016-05-09 13:22:48 -04002314 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2315
2316 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2317 return -EOPNOTSUPP;
2318
Vivien Didelot146a3202015-10-08 11:35:12 -04002319 /* We don't need any dynamic resource from the kernel (yet),
2320 * so skip the prepare phase.
2321 */
2322 return 0;
2323}
2324
Vivien Didelotf81ec902016-05-09 13:22:58 -04002325static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2326 const struct switchdev_obj_port_fdb *fdb,
2327 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002328{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04002329 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07002330 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2331 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2332 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04002333
Vivien Didelot2672f822016-05-09 13:22:48 -04002334 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2335 return;
2336
David S. Millercdf09692015-08-11 12:00:37 -07002337 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002338 if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state))
Vivien Didelot8497aa62016-04-06 11:55:04 -04002339 netdev_err(ds->ports[port], "failed to load MAC address\n");
David S. Millercdf09692015-08-11 12:00:37 -07002340 mutex_unlock(&ps->smi_mutex);
David S. Millercdf09692015-08-11 12:00:37 -07002341}
2342
Vivien Didelotf81ec902016-05-09 13:22:58 -04002343static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2344 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002345{
2346 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2347 int ret;
2348
Vivien Didelot2672f822016-05-09 13:22:48 -04002349 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2350 return -EOPNOTSUPP;
2351
David S. Millercdf09692015-08-11 12:00:37 -07002352 mutex_lock(&ps->smi_mutex);
Andrew Lunn158bc062016-04-28 21:24:06 -04002353 ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07002354 GLOBAL_ATU_DATA_STATE_UNUSED);
2355 mutex_unlock(&ps->smi_mutex);
2356
2357 return ret;
2358}
2359
Andrew Lunn158bc062016-04-28 21:24:06 -04002360static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002361 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002362{
Vivien Didelot1d194042015-08-10 09:09:51 -04002363 struct mv88e6xxx_atu_entry next = { 0 };
2364 int ret;
2365
2366 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002367
Andrew Lunn158bc062016-04-28 21:24:06 -04002368 ret = _mv88e6xxx_atu_wait(ps);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002369 if (ret < 0)
2370 return ret;
2371
Andrew Lunn158bc062016-04-28 21:24:06 -04002372 ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
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_mac_read(ps, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002377 if (ret < 0)
2378 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002379
Andrew Lunn158bc062016-04-28 21:24:06 -04002380 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002381 if (ret < 0)
2382 return ret;
2383
2384 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2385 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2386 unsigned int mask, shift;
2387
2388 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2389 next.trunk = true;
2390 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2391 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2392 } else {
2393 next.trunk = false;
2394 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2395 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2396 }
2397
2398 next.portv_trunkid = (ret & mask) >> shift;
2399 }
2400
2401 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002402 return 0;
2403}
2404
Andrew Lunn158bc062016-04-28 21:24:06 -04002405static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps,
2406 u16 fid, u16 vid, int port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002407 struct switchdev_obj_port_fdb *fdb,
2408 int (*cb)(struct switchdev_obj *obj))
2409{
2410 struct mv88e6xxx_atu_entry addr = {
2411 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2412 };
2413 int err;
2414
Andrew Lunn158bc062016-04-28 21:24:06 -04002415 err = _mv88e6xxx_atu_mac_write(ps, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002416 if (err)
2417 return err;
2418
2419 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002420 err = _mv88e6xxx_atu_getnext(ps, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002421 if (err)
2422 break;
2423
2424 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2425 break;
2426
2427 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2428 bool is_static = addr.state ==
2429 (is_multicast_ether_addr(addr.mac) ?
2430 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2431 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2432
2433 fdb->vid = vid;
2434 ether_addr_copy(fdb->addr, addr.mac);
2435 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2436
2437 err = cb(&fdb->obj);
2438 if (err)
2439 break;
2440 }
2441 } while (!is_broadcast_ether_addr(addr.mac));
2442
2443 return err;
2444}
2445
Vivien Didelotf81ec902016-05-09 13:22:58 -04002446static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2447 struct switchdev_obj_port_fdb *fdb,
2448 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002449{
2450 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2451 struct mv88e6xxx_vtu_stu_entry vlan = {
2452 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2453 };
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002454 u16 fid;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002455 int err;
2456
Vivien Didelot2672f822016-05-09 13:22:48 -04002457 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2458 return -EOPNOTSUPP;
2459
Vivien Didelotf33475b2015-10-22 09:34:41 -04002460 mutex_lock(&ps->smi_mutex);
2461
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002462 /* Dump port's default Filtering Information Database (VLAN ID 0) */
Andrew Lunn158bc062016-04-28 21:24:06 -04002463 err = _mv88e6xxx_port_fid_get(ps, port, &fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002464 if (err)
2465 goto unlock;
2466
Andrew Lunn158bc062016-04-28 21:24:06 -04002467 err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002468 if (err)
2469 goto unlock;
2470
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002471 /* Dump VLANs' Filtering Information Databases */
Andrew Lunn158bc062016-04-28 21:24:06 -04002472 err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002473 if (err)
2474 goto unlock;
2475
2476 do {
Andrew Lunn158bc062016-04-28 21:24:06 -04002477 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002478 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002479 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002480
2481 if (!vlan.valid)
2482 break;
2483
Andrew Lunn158bc062016-04-28 21:24:06 -04002484 err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port,
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002485 fdb, cb);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002486 if (err)
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002487 break;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002488 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2489
2490unlock:
2491 mutex_unlock(&ps->smi_mutex);
2492
2493 return err;
2494}
2495
Vivien Didelotf81ec902016-05-09 13:22:58 -04002496static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2497 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002498{
Vivien Didelota6692752016-02-12 12:09:39 -05002499 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Colin Ian King1d9619d2016-04-25 23:11:22 +01002500 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002501
Vivien Didelot936f2342016-05-09 13:22:46 -04002502 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2503 return -EOPNOTSUPP;
2504
Vivien Didelot466dfa02016-02-26 13:16:05 -05002505 mutex_lock(&ps->smi_mutex);
2506
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002507 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002508 ps->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002509
Vivien Didelot009a2b92016-04-17 13:24:01 -04002510 for (i = 0; i < ps->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002511 if (ps->ports[i].bridge_dev == bridge) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002512 err = _mv88e6xxx_port_based_vlan_map(ps, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002513 if (err)
2514 break;
2515 }
2516 }
2517
Vivien Didelot466dfa02016-02-26 13:16:05 -05002518 mutex_unlock(&ps->smi_mutex);
Vivien Didelota6692752016-02-12 12:09:39 -05002519
Vivien Didelot466dfa02016-02-26 13:16:05 -05002520 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002521}
2522
Vivien Didelotf81ec902016-05-09 13:22:58 -04002523static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002524{
Vivien Didelota6692752016-02-12 12:09:39 -05002525 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002526 struct net_device *bridge = ps->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002527 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002528
Vivien Didelot936f2342016-05-09 13:22:46 -04002529 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2530 return;
2531
Vivien Didelot466dfa02016-02-26 13:16:05 -05002532 mutex_lock(&ps->smi_mutex);
2533
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002534 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelota6692752016-02-12 12:09:39 -05002535 ps->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002536
Vivien Didelot009a2b92016-04-17 13:24:01 -04002537 for (i = 0; i < ps->info->num_ports; ++i)
Vivien Didelot16bfa702016-03-13 16:21:33 -04002538 if (i == port || ps->ports[i].bridge_dev == bridge)
Andrew Lunn158bc062016-04-28 21:24:06 -04002539 if (_mv88e6xxx_port_based_vlan_map(ps, i))
Vivien Didelot16bfa702016-03-13 16:21:33 -04002540 netdev_warn(ds->ports[i], "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002541
Vivien Didelot466dfa02016-02-26 13:16:05 -05002542 mutex_unlock(&ps->smi_mutex);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002543}
2544
Andrew Lunn158bc062016-04-28 21:24:06 -04002545static int _mv88e6xxx_phy_page_write(struct mv88e6xxx_priv_state *ps,
2546 int port, int page, int reg, int val)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002547{
2548 int ret;
2549
Andrew Lunn158bc062016-04-28 21:24:06 -04002550 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002551 if (ret < 0)
2552 goto restore_page_0;
2553
Andrew Lunn158bc062016-04-28 21:24:06 -04002554 ret = _mv88e6xxx_phy_write_indirect(ps, port, reg, val);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002555restore_page_0:
Andrew Lunn158bc062016-04-28 21:24:06 -04002556 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002557
2558 return ret;
2559}
2560
Andrew Lunn158bc062016-04-28 21:24:06 -04002561static int _mv88e6xxx_phy_page_read(struct mv88e6xxx_priv_state *ps,
2562 int port, int page, int reg)
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002563{
2564 int ret;
2565
Andrew Lunn158bc062016-04-28 21:24:06 -04002566 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002567 if (ret < 0)
2568 goto restore_page_0;
2569
Andrew Lunn158bc062016-04-28 21:24:06 -04002570 ret = _mv88e6xxx_phy_read_indirect(ps, port, reg);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002571restore_page_0:
Andrew Lunn158bc062016-04-28 21:24:06 -04002572 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
Patrick Uiterwijk75baacf2016-03-30 01:39:40 +00002573
2574 return ret;
2575}
2576
Vivien Didelot552238b2016-05-09 13:22:49 -04002577static int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps)
2578{
2579 bool ppu_active = mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE);
2580 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Andrew Lunn52638f72016-05-10 23:27:22 +02002581 struct gpio_desc *gpiod = ps->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002582 unsigned long timeout;
2583 int ret;
2584 int i;
2585
2586 /* Set all ports to the disabled state. */
2587 for (i = 0; i < ps->info->num_ports; i++) {
2588 ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL);
2589 if (ret < 0)
2590 return ret;
2591
2592 ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL,
2593 ret & 0xfffc);
2594 if (ret)
2595 return ret;
2596 }
2597
2598 /* Wait for transmit queues to drain. */
2599 usleep_range(2000, 4000);
2600
2601 /* If there is a gpio connected to the reset pin, toggle it */
2602 if (gpiod) {
2603 gpiod_set_value_cansleep(gpiod, 1);
2604 usleep_range(10000, 20000);
2605 gpiod_set_value_cansleep(gpiod, 0);
2606 usleep_range(10000, 20000);
2607 }
2608
2609 /* Reset the switch. Keep the PPU active if requested. The PPU
2610 * needs to be active to support indirect phy register access
2611 * through global registers 0x18 and 0x19.
2612 */
2613 if (ppu_active)
2614 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000);
2615 else
2616 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400);
2617 if (ret)
2618 return ret;
2619
2620 /* Wait up to one second for reset to complete. */
2621 timeout = jiffies + 1 * HZ;
2622 while (time_before(jiffies, timeout)) {
2623 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00);
2624 if (ret < 0)
2625 return ret;
2626
2627 if ((ret & is_reset) == is_reset)
2628 break;
2629 usleep_range(1000, 2000);
2630 }
2631 if (time_after(jiffies, timeout))
2632 ret = -ETIMEDOUT;
2633 else
2634 ret = 0;
2635
2636 return ret;
2637}
2638
Andrew Lunn158bc062016-04-28 21:24:06 -04002639static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002640{
2641 int ret;
2642
Andrew Lunn158bc062016-04-28 21:24:06 -04002643 ret = _mv88e6xxx_phy_page_read(ps, REG_FIBER_SERDES, PAGE_FIBER_SERDES,
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002644 MII_BMCR);
2645 if (ret < 0)
2646 return ret;
2647
2648 if (ret & BMCR_PDOWN) {
2649 ret &= ~BMCR_PDOWN;
Andrew Lunn158bc062016-04-28 21:24:06 -04002650 ret = _mv88e6xxx_phy_page_write(ps, REG_FIBER_SERDES,
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002651 PAGE_FIBER_SERDES, MII_BMCR,
2652 ret);
2653 }
2654
2655 return ret;
2656}
2657
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002658static int mv88e6xxx_setup_port(struct mv88e6xxx_priv_state *ps, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002659{
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002660 struct dsa_switch *ds = ps->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002661 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002662 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002663
Andrew Lunn158bc062016-04-28 21:24:06 -04002664 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2665 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2666 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2667 mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002668 /* MAC Forcing register: don't force link, speed,
2669 * duplex or flow control state to any particular
2670 * values on physical ports, but force the CPU port
2671 * and all DSA ports to their maximum bandwidth and
2672 * full duplex.
2673 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002674 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002675 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002676 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002677 reg |= PORT_PCS_CTRL_FORCE_LINK |
2678 PORT_PCS_CTRL_LINK_UP |
2679 PORT_PCS_CTRL_DUPLEX_FULL |
2680 PORT_PCS_CTRL_FORCE_DUPLEX;
Andrew Lunn158bc062016-04-28 21:24:06 -04002681 if (mv88e6xxx_6065_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002682 reg |= PORT_PCS_CTRL_100;
2683 else
2684 reg |= PORT_PCS_CTRL_1000;
2685 } else {
2686 reg |= PORT_PCS_CTRL_UNFORCED;
2687 }
2688
Andrew Lunn158bc062016-04-28 21:24:06 -04002689 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002690 PORT_PCS_CTRL, reg);
2691 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002692 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002693 }
2694
2695 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2696 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2697 * tunneling, determine priority by looking at 802.1p and IP
2698 * priority fields (IP prio has precedence), and set STP state
2699 * to Forwarding.
2700 *
2701 * If this is the CPU link, use DSA or EDSA tagging depending
2702 * on which tagging mode was configured.
2703 *
2704 * If this is a link to another switch, use DSA tagging mode.
2705 *
2706 * If this is the upstream port for this switch, enable
2707 * forwarding of unknown unicasts and multicasts.
2708 */
2709 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002710 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2711 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2712 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2713 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002714 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2715 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2716 PORT_CONTROL_STATE_FORWARDING;
2717 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002718 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002719 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002720 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2721 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2722 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002723 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2724 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2725 else
2726 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002727 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2728 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002729 }
2730
Andrew Lunn158bc062016-04-28 21:24:06 -04002731 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2732 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2733 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2734 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002735 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2736 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2737 }
2738 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002739 if (dsa_is_dsa_port(ds, port)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002740 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002741 reg |= PORT_CONTROL_DSA_TAG;
Andrew Lunn158bc062016-04-28 21:24:06 -04002742 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2743 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2744 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002745 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002746 }
2747
Andrew Lunn54d792f2015-05-06 01:09:47 +02002748 if (port == dsa_upstream_port(ds))
2749 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2750 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2751 }
2752 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002753 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002754 PORT_CONTROL, reg);
2755 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002756 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002757 }
2758
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002759 /* If this port is connected to a SerDes, make sure the SerDes is not
2760 * powered down.
2761 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002762 if (mv88e6xxx_6352_family(ps)) {
2763 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002764 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002765 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002766 ret &= PORT_STATUS_CMODE_MASK;
2767 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2768 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2769 (ret == PORT_STATUS_CMODE_SGMII)) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002770 ret = mv88e6xxx_power_on_serdes(ps);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002771 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002772 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002773 }
2774 }
2775
Vivien Didelot8efdda42015-08-13 12:52:23 -04002776 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002777 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002778 * untagged frames on this port, do a destination address lookup on all
2779 * received packets as usual, disable ARP mirroring and don't send a
2780 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002781 */
2782 reg = 0;
Andrew Lunn158bc062016-04-28 21:24:06 -04002783 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2784 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2785 mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) ||
2786 mv88e6xxx_6185_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002787 reg = PORT_CONTROL_2_MAP_DA;
2788
Andrew Lunn158bc062016-04-28 21:24:06 -04002789 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2790 mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002791 reg |= PORT_CONTROL_2_JUMBO_10240;
2792
Andrew Lunn158bc062016-04-28 21:24:06 -04002793 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002794 /* Set the upstream port this port should use */
2795 reg |= dsa_upstream_port(ds);
2796 /* enable forwarding of unknown multicast addresses to
2797 * the upstream port
2798 */
2799 if (port == dsa_upstream_port(ds))
2800 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2801 }
2802
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002803 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002804
Andrew Lunn54d792f2015-05-06 01:09:47 +02002805 if (reg) {
Andrew Lunn158bc062016-04-28 21:24:06 -04002806 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002807 PORT_CONTROL_2, reg);
2808 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002809 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002810 }
2811
2812 /* Port Association Vector: when learning source addresses
2813 * of packets, add the address to the address database using
2814 * a port bitmap that has only the bit for this port set and
2815 * the other bits clear.
2816 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002817 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002818 /* Disable learning for CPU port */
2819 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002820 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002821
Andrew Lunn158bc062016-04-28 21:24:06 -04002822 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002823 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002824 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002825
2826 /* Egress rate control 2: disable egress rate control. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002827 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002828 0x0000);
2829 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002830 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002831
Andrew Lunn158bc062016-04-28 21:24:06 -04002832 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2833 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2834 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002835 /* Do not limit the period of time that this port can
2836 * be paused for by the remote end or the period of
2837 * time that this port can pause the remote end.
2838 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002839 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002840 PORT_PAUSE_CTRL, 0x0000);
2841 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002842 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002843
2844 /* Port ATU control: disable limiting the number of
2845 * address database entries that this port is allowed
2846 * to use.
2847 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002848 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002849 PORT_ATU_CONTROL, 0x0000);
2850 /* Priority Override: disable DA, SA and VTU priority
2851 * override.
2852 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002853 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002854 PORT_PRI_OVERRIDE, 0x0000);
2855 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002856 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002857
2858 /* Port Ethertype: use the Ethertype DSA Ethertype
2859 * value.
2860 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002861 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002862 PORT_ETH_TYPE, ETH_P_EDSA);
2863 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002864 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002865 /* Tag Remap: use an identity 802.1p prio -> switch
2866 * prio mapping.
2867 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002868 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002869 PORT_TAG_REGMAP_0123, 0x3210);
2870 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002871 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002872
2873 /* Tag Remap 2: use an identity 802.1p prio -> switch
2874 * prio mapping.
2875 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002876 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002877 PORT_TAG_REGMAP_4567, 0x7654);
2878 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002879 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002880 }
2881
Andrew Lunn158bc062016-04-28 21:24:06 -04002882 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2883 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2884 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2885 mv88e6xxx_6320_family(ps)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002886 /* Rate Control: disable ingress rate limiting. */
Andrew Lunn158bc062016-04-28 21:24:06 -04002887 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002888 PORT_RATE_CONTROL, 0x0001);
2889 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002890 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002891 }
2892
Guenter Roeck366f0a02015-03-26 18:36:30 -07002893 /* Port Control 1: disable trunking, disable sending
2894 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002895 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002896 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002897 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002898 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002899
Vivien Didelot207afda2016-04-14 14:42:09 -04002900 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002901 * database, and allow bidirectional communication between the
2902 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002903 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002904 ret = _mv88e6xxx_port_fid_set(ps, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002905 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002906 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002907
Andrew Lunn158bc062016-04-28 21:24:06 -04002908 ret = _mv88e6xxx_port_based_vlan_map(ps, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002909 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002910 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002911
2912 /* Default VLAN ID and priority: don't set a default VLAN
2913 * ID, and set the default packet priority to zero.
2914 */
Andrew Lunn158bc062016-04-28 21:24:06 -04002915 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002916 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002917 if (ret)
2918 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002919
Andrew Lunndbde9e62015-05-06 01:09:48 +02002920 return 0;
2921}
2922
Vivien Didelot08a01262016-05-09 13:22:50 -04002923static int mv88e6xxx_setup_global(struct mv88e6xxx_priv_state *ps)
2924{
Vivien Didelotb0745e872016-05-09 13:22:53 -04002925 struct dsa_switch *ds = ps->ds;
2926 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002927 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002928 int err;
2929 int i;
2930
Vivien Didelot119477b2016-05-09 13:22:51 -04002931 /* Enable the PHY Polling Unit if present, don't discard any packets,
2932 * and mask all interrupt sources.
2933 */
2934 reg = 0;
2935 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU) ||
2936 mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE))
2937 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2938
2939 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, reg);
2940 if (err)
2941 return err;
2942
Vivien Didelotb0745e872016-05-09 13:22:53 -04002943 /* Configure the upstream port, and configure it as the port to which
2944 * ingress and egress and ARP monitor frames are to be sent.
2945 */
2946 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2947 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2948 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2949 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
2950 if (err)
2951 return err;
2952
Vivien Didelot50484ff2016-05-09 13:22:54 -04002953 /* Disable remote management, and set the switch's DSA device number. */
2954 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL_2,
2955 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2956 (ds->index & 0x1f));
2957 if (err)
2958 return err;
2959
Vivien Didelot08a01262016-05-09 13:22:50 -04002960 /* Set the default address aging time to 5 minutes, and
2961 * enable address learn messages to be sent to all message
2962 * ports.
2963 */
2964 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2965 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2966 if (err)
2967 return err;
2968
2969 /* Configure the IP ToS mapping registers. */
2970 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2971 if (err)
2972 return err;
2973 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2974 if (err)
2975 return err;
2976 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2977 if (err)
2978 return err;
2979 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2980 if (err)
2981 return err;
2982 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2983 if (err)
2984 return err;
2985 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2986 if (err)
2987 return err;
2988 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2989 if (err)
2990 return err;
2991 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2992 if (err)
2993 return err;
2994
2995 /* Configure the IEEE 802.1p priority mapping register. */
2996 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2997 if (err)
2998 return err;
2999
3000 /* Send all frames with destination addresses matching
3001 * 01:80:c2:00:00:0x to the CPU port.
3002 */
3003 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
3004 if (err)
3005 return err;
3006
3007 /* Ignore removed tag data on doubly tagged packets, disable
3008 * flow control messages, force flow control priority to the
3009 * highest, and send all special multicast frames to the CPU
3010 * port at the highest priority.
3011 */
3012 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
3013 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
3014 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
3015 if (err)
3016 return err;
3017
3018 /* Program the DSA routing table. */
3019 for (i = 0; i < 32; i++) {
3020 int nexthop = 0x1f;
3021
Andrew Lunnff049552016-05-10 23:27:24 +02003022 if (ps->ds->cd->rtable &&
Vivien Didelot08a01262016-05-09 13:22:50 -04003023 i != ps->ds->index && i < ps->ds->dst->pd->nr_chips)
Andrew Lunnff049552016-05-10 23:27:24 +02003024 nexthop = ps->ds->cd->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 Lunn491435852015-04-02 04:06:35 +02003162int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
3163{
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 Lunn158bc062016-04-28 21:24:06 -04003168 ret = _mv88e6xxx_phy_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
3174int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
3175 int reg, int val)
3176{
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 Lunn158bc062016-04-28 21:24:06 -04003181 ret = _mv88e6xxx_phy_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 Lunn158bc062016-04-28 21:24:06 -04003187static int mv88e6xxx_port_to_phy_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
Vivien Didelotf81ec902016-05-09 13:22:58 -04003195static int mv88e6xxx_phy_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 Lunn158bc062016-04-28 21:24:06 -04003198 int addr = mv88e6xxx_port_to_phy_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))
3207 ret = mv88e6xxx_phy_read_ppu(ps, addr, regnum);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003208 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3209 ret = _mv88e6xxx_phy_read_indirect(ps, addr, regnum);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003210 else
3211 ret = _mv88e6xxx_phy_read(ps, addr, regnum);
3212
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
Vivien Didelotf81ec902016-05-09 13:22:58 -04003217static int mv88e6xxx_phy_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 Lunn158bc062016-04-28 21:24:06 -04003221 int addr = mv88e6xxx_port_to_phy_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))
3230 ret = mv88e6xxx_phy_write_ppu(ps, addr, regnum, val);
Vivien Didelot6d5834a2016-05-09 13:22:40 -04003231 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3232 ret = _mv88e6xxx_phy_write_indirect(ps, addr, regnum, val);
Vivien Didelot8c9983a2016-05-09 13:22:39 -04003233 else
3234 ret = _mv88e6xxx_phy_write(ps, addr, regnum, val);
3235
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 Lunn158bc062016-04-28 21:24:06 -04003252 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003253 if (ret < 0)
3254 goto error;
3255
3256 /* Enable temperature sensor */
Andrew Lunn158bc062016-04-28 21:24:06 -04003257 ret = _mv88e6xxx_phy_read(ps, 0x0, 0x1a);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003258 if (ret < 0)
3259 goto error;
3260
Andrew Lunn158bc062016-04-28 21:24:06 -04003261 ret = _mv88e6xxx_phy_write(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 Lunn158bc062016-04-28 21:24:06 -04003268 val = _mv88e6xxx_phy_read(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 Lunn158bc062016-04-28 21:24:06 -04003275 ret = _mv88e6xxx_phy_write(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 Lunn158bc062016-04-28 21:24:06 -04003282 _mv88e6xxx_phy_write(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
3295 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
3296 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
3328 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3329 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
3346 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3347 if (ret < 0)
3348 return ret;
3349 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3350 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
3351 (ret & 0xe0ff) | (temp << 8));
3352}
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
3365 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3366 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,
3593 .phy_read = mv88e6xxx_phy_read,
3594 .phy_write = mv88e6xxx_phy_write,
3595 .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");