blob: 4b6baa6e90cd3cbef624ca6a93cc17a5ce795f3d [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
3 * Copyright (c) 2008 Marvell Semiconductor
4 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04005 * Copyright (c) 2015 CMC Electronics, Inc.
6 * Added support for VLAN Table Unit operations
7 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
Andrew Lunn87c8cef2015-06-20 18:42:28 +020014#include <linux/debugfs.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000015#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070016#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020017#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070018#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000019#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000020#include <linux/list.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000021#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022#include <linux/netdevice.h>
23#include <linux/phy.h>
Andrew Lunn87c8cef2015-06-20 18:42:28 +020024#include <linux/seq_file.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000025#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040026#include <net/switchdev.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include "mv88e6xxx.h"
28
Andrew Lunn16fe24f2015-05-06 01:09:55 +020029/* MDIO bus access can be nested in the case of PHYs connected to the
30 * internal MDIO bus of the switch, which is accessed via MDIO bus of
31 * the Ethernet interface. Avoid lockdep false positives by using
32 * mutex_lock_nested().
33 */
34static int mv88e6xxx_mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
35{
36 int ret;
37
38 mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
39 ret = bus->read(bus, addr, regnum);
40 mutex_unlock(&bus->mdio_lock);
41
42 return ret;
43}
44
45static int mv88e6xxx_mdiobus_write(struct mii_bus *bus, int addr, u32 regnum,
46 u16 val)
47{
48 int ret;
49
50 mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
51 ret = bus->write(bus, addr, regnum, val);
52 mutex_unlock(&bus->mdio_lock);
53
54 return ret;
55}
56
Barry Grussling3675c8d2013-01-08 16:05:53 +000057/* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000058 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
59 * will be directly accessible on some {device address,register address}
60 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
61 * will only respond to SMI transactions to that specific address, and
62 * an indirect addressing mechanism needs to be used to access its
63 * registers.
64 */
65static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
66{
67 int ret;
68 int i;
69
70 for (i = 0; i < 16; i++) {
Andrew Lunn16fe24f2015-05-06 01:09:55 +020071 ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072 if (ret < 0)
73 return ret;
74
Andrew Lunncca8b132015-04-02 04:06:39 +020075 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000076 return 0;
77 }
78
79 return -ETIMEDOUT;
80}
81
82int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
83{
84 int ret;
85
86 if (sw_addr == 0)
Andrew Lunn16fe24f2015-05-06 01:09:55 +020087 return mv88e6xxx_mdiobus_read(bus, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000088
Barry Grussling3675c8d2013-01-08 16:05:53 +000089 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000090 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
91 if (ret < 0)
92 return ret;
93
Barry Grussling3675c8d2013-01-08 16:05:53 +000094 /* Transmit the read command. */
Andrew Lunn16fe24f2015-05-06 01:09:55 +020095 ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD,
96 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000097 if (ret < 0)
98 return ret;
99
Barry Grussling3675c8d2013-01-08 16:05:53 +0000100 /* Wait for the read command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000101 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
102 if (ret < 0)
103 return ret;
104
Barry Grussling3675c8d2013-01-08 16:05:53 +0000105 /* Read the data. */
Andrew Lunn16fe24f2015-05-06 01:09:55 +0200106 ret = mv88e6xxx_mdiobus_read(bus, sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000107 if (ret < 0)
108 return ret;
109
110 return ret & 0xffff;
111}
112
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700113/* Must be called with SMI mutex held */
114static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000115{
Guenter Roeckb184e492014-10-17 12:30:58 -0700116 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000117 int ret;
118
Guenter Roeckb184e492014-10-17 12:30:58 -0700119 if (bus == NULL)
120 return -EINVAL;
121
Guenter Roeckb184e492014-10-17 12:30:58 -0700122 ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500123 if (ret < 0)
124 return ret;
125
126 dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
127 addr, reg, ret);
128
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000129 return ret;
130}
131
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700132int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
133{
134 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
135 int ret;
136
137 mutex_lock(&ps->smi_mutex);
138 ret = _mv88e6xxx_reg_read(ds, addr, reg);
139 mutex_unlock(&ps->smi_mutex);
140
141 return ret;
142}
143
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000144int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
145 int reg, u16 val)
146{
147 int ret;
148
149 if (sw_addr == 0)
Andrew Lunn16fe24f2015-05-06 01:09:55 +0200150 return mv88e6xxx_mdiobus_write(bus, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000151
Barry Grussling3675c8d2013-01-08 16:05:53 +0000152 /* Wait for the bus to become free. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000153 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
154 if (ret < 0)
155 return ret;
156
Barry Grussling3675c8d2013-01-08 16:05:53 +0000157 /* Transmit the data to write. */
Andrew Lunn16fe24f2015-05-06 01:09:55 +0200158 ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_DATA, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000159 if (ret < 0)
160 return ret;
161
Barry Grussling3675c8d2013-01-08 16:05:53 +0000162 /* Transmit the write command. */
Andrew Lunn16fe24f2015-05-06 01:09:55 +0200163 ret = mv88e6xxx_mdiobus_write(bus, sw_addr, SMI_CMD,
164 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000165 if (ret < 0)
166 return ret;
167
Barry Grussling3675c8d2013-01-08 16:05:53 +0000168 /* Wait for the write command to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000169 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
170 if (ret < 0)
171 return ret;
172
173 return 0;
174}
175
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700176/* Must be called with SMI mutex held */
177static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg,
178 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000179{
Guenter Roeckb184e492014-10-17 12:30:58 -0700180 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000181
Guenter Roeckb184e492014-10-17 12:30:58 -0700182 if (bus == NULL)
183 return -EINVAL;
184
Vivien Didelotbb92ea52015-01-23 16:10:36 -0500185 dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
186 addr, reg, val);
187
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700188 return __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
189}
190
191int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
192{
193 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
194 int ret;
195
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000196 mutex_lock(&ps->smi_mutex);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700197 ret = _mv88e6xxx_reg_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000198 mutex_unlock(&ps->smi_mutex);
199
200 return ret;
201}
202
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000203int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
204{
Andrew Lunncca8b132015-04-02 04:06:39 +0200205 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
206 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
207 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000208
209 return 0;
210}
211
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000212int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
213{
214 int i;
215 int ret;
216
217 for (i = 0; i < 6; i++) {
218 int j;
219
Barry Grussling3675c8d2013-01-08 16:05:53 +0000220 /* Write the MAC address byte. */
Andrew Lunncca8b132015-04-02 04:06:39 +0200221 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
222 GLOBAL2_SWITCH_MAC_BUSY | (i << 8) | addr[i]);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000223
Barry Grussling3675c8d2013-01-08 16:05:53 +0000224 /* Wait for the write to complete. */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000225 for (j = 0; j < 16; j++) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200226 ret = REG_READ(REG_GLOBAL2, GLOBAL2_SWITCH_MAC);
227 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000228 break;
229 }
230 if (j == 16)
231 return -ETIMEDOUT;
232 }
233
234 return 0;
235}
236
Andrew Lunn3898c142015-05-06 01:09:53 +0200237/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200238static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000239{
240 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200241 return _mv88e6xxx_reg_read(ds, addr, regnum);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000242 return 0xffff;
243}
244
Andrew Lunn3898c142015-05-06 01:09:53 +0200245/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200246static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum,
247 u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000248{
249 if (addr >= 0)
Andrew Lunn3898c142015-05-06 01:09:53 +0200250 return _mv88e6xxx_reg_write(ds, addr, regnum, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000251 return 0;
252}
253
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000254#ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
255static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
256{
257 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000258 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000259
Andrew Lunncca8b132015-04-02 04:06:39 +0200260 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
261 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
262 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000263
Barry Grussling19b2f972013-01-08 16:05:54 +0000264 timeout = jiffies + 1 * HZ;
265 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200266 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000267 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200268 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
269 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000270 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000271 }
272
273 return -ETIMEDOUT;
274}
275
276static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
277{
278 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +0000279 unsigned long timeout;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000280
Andrew Lunncca8b132015-04-02 04:06:39 +0200281 ret = REG_READ(REG_GLOBAL, GLOBAL_CONTROL);
282 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, ret | GLOBAL_CONTROL_PPU_ENABLE);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000283
Barry Grussling19b2f972013-01-08 16:05:54 +0000284 timeout = jiffies + 1 * HZ;
285 while (time_before(jiffies, timeout)) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200286 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
Barry Grussling19b2f972013-01-08 16:05:54 +0000287 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200288 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
289 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000290 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000291 }
292
293 return -ETIMEDOUT;
294}
295
296static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
297{
298 struct mv88e6xxx_priv_state *ps;
299
300 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
301 if (mutex_trylock(&ps->ppu_mutex)) {
Barry Grussling85686582013-01-08 16:05:56 +0000302 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000303
Barry Grussling85686582013-01-08 16:05:56 +0000304 if (mv88e6xxx_ppu_enable(ds) == 0)
305 ps->ppu_disabled = 0;
306 mutex_unlock(&ps->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000307 }
308}
309
310static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
311{
312 struct mv88e6xxx_priv_state *ps = (void *)_ps;
313
314 schedule_work(&ps->ppu_work);
315}
316
317static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
318{
Florian Fainellia22adce2014-04-28 11:14:28 -0700319 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000320 int ret;
321
322 mutex_lock(&ps->ppu_mutex);
323
Barry Grussling3675c8d2013-01-08 16:05:53 +0000324 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000325 * we can access the PHY registers. If it was already
326 * disabled, cancel the timer that is going to re-enable
327 * it.
328 */
329 if (!ps->ppu_disabled) {
Barry Grussling85686582013-01-08 16:05:56 +0000330 ret = mv88e6xxx_ppu_disable(ds);
331 if (ret < 0) {
332 mutex_unlock(&ps->ppu_mutex);
333 return ret;
334 }
335 ps->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000336 } else {
Barry Grussling85686582013-01-08 16:05:56 +0000337 del_timer(&ps->ppu_timer);
338 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000339 }
340
341 return ret;
342}
343
344static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
345{
Florian Fainellia22adce2014-04-28 11:14:28 -0700346 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000347
Barry Grussling3675c8d2013-01-08 16:05:53 +0000348 /* Schedule a timer to re-enable the PHY polling unit. */
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000349 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
350 mutex_unlock(&ps->ppu_mutex);
351}
352
353void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
354{
Florian Fainellia22adce2014-04-28 11:14:28 -0700355 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000356
357 mutex_init(&ps->ppu_mutex);
358 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
359 init_timer(&ps->ppu_timer);
360 ps->ppu_timer.data = (unsigned long)ps;
361 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
362}
363
364int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
365{
366 int ret;
367
368 ret = mv88e6xxx_ppu_access_get(ds);
369 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000370 ret = mv88e6xxx_reg_read(ds, addr, regnum);
371 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000372 }
373
374 return ret;
375}
376
377int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
378 int regnum, u16 val)
379{
380 int ret;
381
382 ret = mv88e6xxx_ppu_access_get(ds);
383 if (ret >= 0) {
Barry Grussling85686582013-01-08 16:05:56 +0000384 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
385 mv88e6xxx_ppu_access_put(ds);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000386 }
387
388 return ret;
389}
390#endif
391
Andrew Lunn54d792f2015-05-06 01:09:47 +0200392static bool mv88e6xxx_6065_family(struct dsa_switch *ds)
393{
394 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
395
396 switch (ps->id) {
397 case PORT_SWITCH_ID_6031:
398 case PORT_SWITCH_ID_6061:
399 case PORT_SWITCH_ID_6035:
400 case PORT_SWITCH_ID_6065:
401 return true;
402 }
403 return false;
404}
405
406static bool mv88e6xxx_6095_family(struct dsa_switch *ds)
407{
408 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
409
410 switch (ps->id) {
411 case PORT_SWITCH_ID_6092:
412 case PORT_SWITCH_ID_6095:
413 return true;
414 }
415 return false;
416}
417
418static bool mv88e6xxx_6097_family(struct dsa_switch *ds)
419{
420 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
421
422 switch (ps->id) {
423 case PORT_SWITCH_ID_6046:
424 case PORT_SWITCH_ID_6085:
425 case PORT_SWITCH_ID_6096:
426 case PORT_SWITCH_ID_6097:
427 return true;
428 }
429 return false;
430}
431
432static bool mv88e6xxx_6165_family(struct dsa_switch *ds)
433{
434 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
435
436 switch (ps->id) {
437 case PORT_SWITCH_ID_6123:
438 case PORT_SWITCH_ID_6161:
439 case PORT_SWITCH_ID_6165:
440 return true;
441 }
442 return false;
443}
444
445static bool mv88e6xxx_6185_family(struct dsa_switch *ds)
446{
447 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
448
449 switch (ps->id) {
450 case PORT_SWITCH_ID_6121:
451 case PORT_SWITCH_ID_6122:
452 case PORT_SWITCH_ID_6152:
453 case PORT_SWITCH_ID_6155:
454 case PORT_SWITCH_ID_6182:
455 case PORT_SWITCH_ID_6185:
456 case PORT_SWITCH_ID_6108:
457 case PORT_SWITCH_ID_6131:
458 return true;
459 }
460 return false;
461}
462
Guenter Roeckc22995c2015-07-25 09:42:28 -0700463static bool mv88e6xxx_6320_family(struct dsa_switch *ds)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700464{
465 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
466
467 switch (ps->id) {
468 case PORT_SWITCH_ID_6320:
469 case PORT_SWITCH_ID_6321:
470 return true;
471 }
472 return false;
473}
474
Andrew Lunn54d792f2015-05-06 01:09:47 +0200475static bool mv88e6xxx_6351_family(struct dsa_switch *ds)
476{
477 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
478
479 switch (ps->id) {
480 case PORT_SWITCH_ID_6171:
481 case PORT_SWITCH_ID_6175:
482 case PORT_SWITCH_ID_6350:
483 case PORT_SWITCH_ID_6351:
484 return true;
485 }
486 return false;
487}
488
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200489static bool mv88e6xxx_6352_family(struct dsa_switch *ds)
490{
491 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
492
493 switch (ps->id) {
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200494 case PORT_SWITCH_ID_6172:
495 case PORT_SWITCH_ID_6176:
Andrew Lunn54d792f2015-05-06 01:09:47 +0200496 case PORT_SWITCH_ID_6240:
497 case PORT_SWITCH_ID_6352:
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200498 return true;
499 }
500 return false;
501}
502
Andrew Lunndea87022015-08-31 15:56:47 +0200503/* We expect the switch to perform auto negotiation if there is a real
504 * phy. However, in the case of a fixed link phy, we force the port
505 * settings from the fixed link settings.
506 */
507void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
508 struct phy_device *phydev)
509{
510 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn49052872015-09-29 01:53:48 +0200511 u32 reg;
512 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200513
514 if (!phy_is_pseudo_fixed_link(phydev))
515 return;
516
517 mutex_lock(&ps->smi_mutex);
518
519 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
520 if (ret < 0)
521 goto out;
522
523 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
524 PORT_PCS_CTRL_FORCE_LINK |
525 PORT_PCS_CTRL_DUPLEX_FULL |
526 PORT_PCS_CTRL_FORCE_DUPLEX |
527 PORT_PCS_CTRL_UNFORCED);
528
529 reg |= PORT_PCS_CTRL_FORCE_LINK;
530 if (phydev->link)
531 reg |= PORT_PCS_CTRL_LINK_UP;
532
533 if (mv88e6xxx_6065_family(ds) && phydev->speed > SPEED_100)
534 goto out;
535
536 switch (phydev->speed) {
537 case SPEED_1000:
538 reg |= PORT_PCS_CTRL_1000;
539 break;
540 case SPEED_100:
541 reg |= PORT_PCS_CTRL_100;
542 break;
543 case SPEED_10:
544 reg |= PORT_PCS_CTRL_10;
545 break;
546 default:
547 pr_info("Unknown speed");
548 goto out;
549 }
550
551 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
552 if (phydev->duplex == DUPLEX_FULL)
553 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
554
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200555 if ((mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds)) &&
556 (port >= ps->num_ports - 2)) {
557 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
558 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
559 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
560 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
561 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
562 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
563 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
564 }
Andrew Lunndea87022015-08-31 15:56:47 +0200565 _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_PCS_CTRL, reg);
566
567out:
568 mutex_unlock(&ps->smi_mutex);
569}
570
Andrew Lunn31888232015-05-06 01:09:54 +0200571/* Must be called with SMI mutex held */
572static int _mv88e6xxx_stats_wait(struct dsa_switch *ds)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000573{
574 int ret;
575 int i;
576
577 for (i = 0; i < 10; i++) {
Andrew Lunn31888232015-05-06 01:09:54 +0200578 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200579 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000580 return 0;
581 }
582
583 return -ETIMEDOUT;
584}
585
Andrew Lunn31888232015-05-06 01:09:54 +0200586/* Must be called with SMI mutex held */
587static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000588{
589 int ret;
590
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700591 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200592 port = (port + 1) << 5;
593
Barry Grussling3675c8d2013-01-08 16:05:53 +0000594 /* Snapshot the hardware statistics counters for this port. */
Andrew Lunn31888232015-05-06 01:09:54 +0200595 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
596 GLOBAL_STATS_OP_CAPTURE_PORT |
597 GLOBAL_STATS_OP_HIST_RX_TX | port);
598 if (ret < 0)
599 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000600
Barry Grussling3675c8d2013-01-08 16:05:53 +0000601 /* Wait for the snapshotting to complete. */
Andrew Lunn31888232015-05-06 01:09:54 +0200602 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000603 if (ret < 0)
604 return ret;
605
606 return 0;
607}
608
Andrew Lunn31888232015-05-06 01:09:54 +0200609/* Must be called with SMI mutex held */
610static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000611{
612 u32 _val;
613 int ret;
614
615 *val = 0;
616
Andrew Lunn31888232015-05-06 01:09:54 +0200617 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP,
618 GLOBAL_STATS_OP_READ_CAPTURED |
619 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000620 if (ret < 0)
621 return;
622
Andrew Lunn31888232015-05-06 01:09:54 +0200623 ret = _mv88e6xxx_stats_wait(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000624 if (ret < 0)
625 return;
626
Andrew Lunn31888232015-05-06 01:09:54 +0200627 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000628 if (ret < 0)
629 return;
630
631 _val = ret << 16;
632
Andrew Lunn31888232015-05-06 01:09:54 +0200633 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000634 if (ret < 0)
635 return;
636
637 *val = _val | ret;
638}
639
Andrew Lunne413e7e2015-04-02 04:06:38 +0200640static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
641 { "in_good_octets", 8, 0x00, },
642 { "in_bad_octets", 4, 0x02, },
643 { "in_unicast", 4, 0x04, },
644 { "in_broadcasts", 4, 0x06, },
645 { "in_multicasts", 4, 0x07, },
646 { "in_pause", 4, 0x16, },
647 { "in_undersize", 4, 0x18, },
648 { "in_fragments", 4, 0x19, },
649 { "in_oversize", 4, 0x1a, },
650 { "in_jabber", 4, 0x1b, },
651 { "in_rx_error", 4, 0x1c, },
652 { "in_fcs_error", 4, 0x1d, },
653 { "out_octets", 8, 0x0e, },
654 { "out_unicast", 4, 0x10, },
655 { "out_broadcasts", 4, 0x13, },
656 { "out_multicasts", 4, 0x12, },
657 { "out_pause", 4, 0x15, },
658 { "excessive", 4, 0x11, },
659 { "collisions", 4, 0x1e, },
660 { "deferred", 4, 0x05, },
661 { "single", 4, 0x14, },
662 { "multiple", 4, 0x17, },
663 { "out_fcs_error", 4, 0x03, },
664 { "late", 4, 0x1f, },
665 { "hist_64bytes", 4, 0x08, },
666 { "hist_65_127bytes", 4, 0x09, },
667 { "hist_128_255bytes", 4, 0x0a, },
668 { "hist_256_511bytes", 4, 0x0b, },
669 { "hist_512_1023bytes", 4, 0x0c, },
670 { "hist_1024_max_bytes", 4, 0x0d, },
671 /* Not all devices have the following counters */
672 { "sw_in_discards", 4, 0x110, },
673 { "sw_in_filtered", 2, 0x112, },
674 { "sw_out_filtered", 2, 0x113, },
675
676};
677
678static bool have_sw_in_discards(struct dsa_switch *ds)
679{
680 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
681
682 switch (ps->id) {
Andrew Lunncca8b132015-04-02 04:06:39 +0200683 case PORT_SWITCH_ID_6095: case PORT_SWITCH_ID_6161:
684 case PORT_SWITCH_ID_6165: case PORT_SWITCH_ID_6171:
685 case PORT_SWITCH_ID_6172: case PORT_SWITCH_ID_6176:
686 case PORT_SWITCH_ID_6182: case PORT_SWITCH_ID_6185:
687 case PORT_SWITCH_ID_6352:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200688 return true;
689 default:
690 return false;
691 }
692}
693
694static void _mv88e6xxx_get_strings(struct dsa_switch *ds,
695 int nr_stats,
696 struct mv88e6xxx_hw_stat *stats,
697 int port, uint8_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000698{
699 int i;
700
701 for (i = 0; i < nr_stats; i++) {
702 memcpy(data + i * ETH_GSTRING_LEN,
703 stats[i].string, ETH_GSTRING_LEN);
704 }
705}
706
Andrew Lunn80c46272015-06-20 18:42:30 +0200707static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds,
708 int stat,
709 struct mv88e6xxx_hw_stat *stats,
710 int port)
711{
712 struct mv88e6xxx_hw_stat *s = stats + stat;
713 u32 low;
714 u32 high = 0;
715 int ret;
716 u64 value;
717
718 if (s->reg >= 0x100) {
719 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
720 s->reg - 0x100);
721 if (ret < 0)
722 return UINT64_MAX;
723
724 low = ret;
725 if (s->sizeof_stat == 4) {
726 ret = _mv88e6xxx_reg_read(ds, REG_PORT(port),
727 s->reg - 0x100 + 1);
728 if (ret < 0)
729 return UINT64_MAX;
730 high = ret;
731 }
732 } else {
733 _mv88e6xxx_stats_read(ds, s->reg, &low);
734 if (s->sizeof_stat == 8)
735 _mv88e6xxx_stats_read(ds, s->reg + 1, &high);
736 }
737 value = (((u64)high) << 16) | low;
738 return value;
739}
740
Andrew Lunne413e7e2015-04-02 04:06:38 +0200741static void _mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
742 int nr_stats,
743 struct mv88e6xxx_hw_stat *stats,
744 int port, uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000745{
Florian Fainellia22adce2014-04-28 11:14:28 -0700746 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000747 int ret;
748 int i;
749
Andrew Lunn31888232015-05-06 01:09:54 +0200750 mutex_lock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000751
Andrew Lunn31888232015-05-06 01:09:54 +0200752 ret = _mv88e6xxx_stats_snapshot(ds, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000753 if (ret < 0) {
Andrew Lunn31888232015-05-06 01:09:54 +0200754 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000755 return;
756 }
757
Barry Grussling3675c8d2013-01-08 16:05:53 +0000758 /* Read each of the counters. */
Andrew Lunn80c46272015-06-20 18:42:30 +0200759 for (i = 0; i < nr_stats; i++)
760 data[i] = _mv88e6xxx_get_ethtool_stat(ds, i, stats, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000761
Andrew Lunn31888232015-05-06 01:09:54 +0200762 mutex_unlock(&ps->smi_mutex);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000763}
Ben Hutchings98e67302011-11-25 14:36:19 +0000764
Andrew Lunne413e7e2015-04-02 04:06:38 +0200765/* All the statistics in the table */
766void
767mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
768{
769 if (have_sw_in_discards(ds))
770 _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
771 mv88e6xxx_hw_stats, port, data);
772 else
773 _mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
774 mv88e6xxx_hw_stats, port, data);
775}
776
777int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
778{
779 if (have_sw_in_discards(ds))
780 return ARRAY_SIZE(mv88e6xxx_hw_stats);
781 return ARRAY_SIZE(mv88e6xxx_hw_stats) - 3;
782}
783
784void
785mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
786 int port, uint64_t *data)
787{
788 if (have_sw_in_discards(ds))
789 _mv88e6xxx_get_ethtool_stats(
790 ds, ARRAY_SIZE(mv88e6xxx_hw_stats),
791 mv88e6xxx_hw_stats, port, data);
792 else
793 _mv88e6xxx_get_ethtool_stats(
794 ds, ARRAY_SIZE(mv88e6xxx_hw_stats) - 3,
795 mv88e6xxx_hw_stats, port, data);
796}
797
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700798int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
799{
800 return 32 * sizeof(u16);
801}
802
803void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
804 struct ethtool_regs *regs, void *_p)
805{
806 u16 *p = _p;
807 int i;
808
809 regs->version = 0;
810
811 memset(p, 0xff, 32 * sizeof(u16));
812
813 for (i = 0; i < 32; i++) {
814 int ret;
815
816 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
817 if (ret >= 0)
818 p[i] = ret;
819 }
820}
821
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700822/* Must be called with SMI lock held */
Andrew Lunn3898c142015-05-06 01:09:53 +0200823static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset,
824 u16 mask)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700825{
826 unsigned long timeout = jiffies + HZ / 10;
827
828 while (time_before(jiffies, timeout)) {
829 int ret;
830
831 ret = _mv88e6xxx_reg_read(ds, reg, offset);
832 if (ret < 0)
833 return ret;
834 if (!(ret & mask))
835 return 0;
836
837 usleep_range(1000, 2000);
838 }
839 return -ETIMEDOUT;
840}
841
Andrew Lunn3898c142015-05-06 01:09:53 +0200842static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask)
843{
844 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
845 int ret;
846
847 mutex_lock(&ps->smi_mutex);
848 ret = _mv88e6xxx_wait(ds, reg, offset, mask);
849 mutex_unlock(&ps->smi_mutex);
850
851 return ret;
852}
853
854static int _mv88e6xxx_phy_wait(struct dsa_switch *ds)
855{
856 return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
857 GLOBAL2_SMI_OP_BUSY);
858}
859
860int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
861{
862 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
863 GLOBAL2_EEPROM_OP_LOAD);
864}
865
866int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
867{
868 return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
869 GLOBAL2_EEPROM_OP_BUSY);
870}
871
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700872/* Must be called with SMI lock held */
873static int _mv88e6xxx_atu_wait(struct dsa_switch *ds)
874{
Andrew Lunncca8b132015-04-02 04:06:39 +0200875 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP,
876 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700877}
878
Andrew Lunn56d95e22015-06-20 18:42:33 +0200879/* Must be called with SMI lock held */
880static int _mv88e6xxx_scratch_wait(struct dsa_switch *ds)
881{
882 return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SCRATCH_MISC,
883 GLOBAL2_SCRATCH_BUSY);
884}
885
Andrew Lunn3898c142015-05-06 01:09:53 +0200886/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200887static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr,
888 int regnum)
Andrew Lunnf3044682015-02-14 19:17:50 +0100889{
890 int ret;
891
Andrew Lunn3898c142015-05-06 01:09:53 +0200892 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
893 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
894 regnum);
Andrew Lunnf3044682015-02-14 19:17:50 +0100895 if (ret < 0)
896 return ret;
897
Andrew Lunn3898c142015-05-06 01:09:53 +0200898 ret = _mv88e6xxx_phy_wait(ds);
899 if (ret < 0)
900 return ret;
901
902 return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA);
Andrew Lunnf3044682015-02-14 19:17:50 +0100903}
904
Andrew Lunn3898c142015-05-06 01:09:53 +0200905/* Must be called with SMI mutex held */
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200906static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr,
907 int regnum, u16 val)
Andrew Lunnf3044682015-02-14 19:17:50 +0100908{
Andrew Lunn3898c142015-05-06 01:09:53 +0200909 int ret;
Andrew Lunnf3044682015-02-14 19:17:50 +0100910
Andrew Lunn3898c142015-05-06 01:09:53 +0200911 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
912 if (ret < 0)
913 return ret;
914
915 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP,
916 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
917 regnum);
918
919 return _mv88e6xxx_phy_wait(ds);
Andrew Lunnf3044682015-02-14 19:17:50 +0100920}
921
Guenter Roeck11b3b452015-03-06 22:23:51 -0800922int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
923{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200924 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800925 int reg;
926
Andrew Lunn3898c142015-05-06 01:09:53 +0200927 mutex_lock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200928
929 reg = _mv88e6xxx_phy_read_indirect(ds, port, 16);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800930 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200931 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800932
933 e->eee_enabled = !!(reg & 0x0200);
934 e->tx_lpi_enabled = !!(reg & 0x0100);
935
Andrew Lunn3898c142015-05-06 01:09:53 +0200936 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800937 if (reg < 0)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200938 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800939
Andrew Lunncca8b132015-04-02 04:06:39 +0200940 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200941 reg = 0;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800942
Andrew Lunn2f40c692015-04-02 04:06:37 +0200943out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200944 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200945 return reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800946}
947
948int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
949 struct phy_device *phydev, struct ethtool_eee *e)
950{
Andrew Lunn2f40c692015-04-02 04:06:37 +0200951 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
952 int reg;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800953 int ret;
954
Andrew Lunn3898c142015-05-06 01:09:53 +0200955 mutex_lock(&ps->smi_mutex);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800956
Andrew Lunn2f40c692015-04-02 04:06:37 +0200957 ret = _mv88e6xxx_phy_read_indirect(ds, port, 16);
958 if (ret < 0)
959 goto out;
960
961 reg = ret & ~0x0300;
962 if (e->eee_enabled)
963 reg |= 0x0200;
964 if (e->tx_lpi_enabled)
965 reg |= 0x0100;
966
967 ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg);
968out:
Andrew Lunn3898c142015-05-06 01:09:53 +0200969 mutex_unlock(&ps->smi_mutex);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200970
971 return ret;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800972}
973
Vivien Didelot70cc99d2015-09-04 14:34:10 -0400974static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700975{
976 int ret;
977
Andrew Lunncca8b132015-04-02 04:06:39 +0200978 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700979 if (ret < 0)
980 return ret;
981
982 return _mv88e6xxx_atu_wait(ds);
983}
984
Vivien Didelot37705b72015-09-04 14:34:11 -0400985static int _mv88e6xxx_atu_data_write(struct dsa_switch *ds,
986 struct mv88e6xxx_atu_entry *entry)
987{
988 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
989
990 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
991 unsigned int mask, shift;
992
993 if (entry->trunk) {
994 data |= GLOBAL_ATU_DATA_TRUNK;
995 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
996 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
997 } else {
998 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
999 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1000 }
1001
1002 data |= (entry->portv_trunkid << shift) & mask;
1003 }
1004
1005 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1006}
1007
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001008static int _mv88e6xxx_atu_flush_move(struct dsa_switch *ds,
1009 struct mv88e6xxx_atu_entry *entry,
1010 bool static_too)
1011{
1012 int op;
1013 int err;
1014
1015 err = _mv88e6xxx_atu_wait(ds);
1016 if (err)
1017 return err;
1018
1019 err = _mv88e6xxx_atu_data_write(ds, entry);
1020 if (err)
1021 return err;
1022
1023 if (entry->fid) {
1024 err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID,
1025 entry->fid);
1026 if (err)
1027 return err;
1028
1029 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1030 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1031 } else {
1032 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1033 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1034 }
1035
1036 return _mv88e6xxx_atu_cmd(ds, op);
1037}
1038
1039static int _mv88e6xxx_atu_flush(struct dsa_switch *ds, u16 fid, bool static_too)
1040{
1041 struct mv88e6xxx_atu_entry entry = {
1042 .fid = fid,
1043 .state = 0, /* EntryState bits must be 0 */
1044 };
1045
1046 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1047}
1048
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001049static int _mv88e6xxx_atu_move(struct dsa_switch *ds, u16 fid, int from_port,
1050 int to_port, bool static_too)
1051{
1052 struct mv88e6xxx_atu_entry entry = {
1053 .trunk = false,
1054 .fid = fid,
1055 };
1056
1057 /* EntryState bits must be 0xF */
1058 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1059
1060 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1061 entry.portv_trunkid = (to_port & 0x0f) << 4;
1062 entry.portv_trunkid |= from_port & 0x0f;
1063
1064 return _mv88e6xxx_atu_flush_move(ds, &entry, static_too);
1065}
1066
1067static int _mv88e6xxx_atu_remove(struct dsa_switch *ds, u16 fid, int port,
1068 bool static_too)
1069{
1070 /* Destination port 0xF means remove the entries */
1071 return _mv88e6xxx_atu_move(ds, fid, port, 0x0f, static_too);
1072}
1073
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001074static int mv88e6xxx_set_port_state(struct dsa_switch *ds, int port, u8 state)
1075{
1076 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001077 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001078 u8 oldstate;
1079
1080 mutex_lock(&ps->smi_mutex);
1081
Andrew Lunncca8b132015-04-02 04:06:39 +02001082 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL);
Guenter Roeck538cc282015-04-15 22:12:42 -07001083 if (reg < 0) {
1084 ret = reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001085 goto abort;
Guenter Roeck538cc282015-04-15 22:12:42 -07001086 }
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001087
Andrew Lunncca8b132015-04-02 04:06:39 +02001088 oldstate = reg & PORT_CONTROL_STATE_MASK;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001089 if (oldstate != state) {
1090 /* Flush forwarding database if we're moving a port
1091 * from Learning or Forwarding state to Disabled or
1092 * Blocking or Listening state.
1093 */
Andrew Lunncca8b132015-04-02 04:06:39 +02001094 if (oldstate >= PORT_CONTROL_STATE_LEARNING &&
1095 state <= PORT_CONTROL_STATE_BLOCKING) {
Vivien Didelot2b8157b2015-09-04 14:34:16 -04001096 ret = _mv88e6xxx_atu_remove(ds, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001097 if (ret)
1098 goto abort;
1099 }
Andrew Lunncca8b132015-04-02 04:06:39 +02001100 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1101 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL,
1102 reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001103 }
1104
1105abort:
1106 mutex_unlock(&ps->smi_mutex);
1107 return ret;
1108}
1109
Vivien Didelotede80982015-10-11 18:08:35 -04001110static int _mv88e6xxx_port_vlan_map_set(struct dsa_switch *ds, int port,
1111 u16 output_ports)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001112{
1113 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotede80982015-10-11 18:08:35 -04001114 const u16 mask = (1 << ps->num_ports) - 1;
1115 int reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001116
Vivien Didelotede80982015-10-11 18:08:35 -04001117 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN);
1118 if (reg < 0)
1119 return reg;
1120
1121 reg &= ~mask;
1122 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001123
Andrew Lunncca8b132015-04-02 04:06:39 +02001124 return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001125}
1126
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001127int mv88e6xxx_port_stp_update(struct dsa_switch *ds, int port, u8 state)
1128{
1129 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1130 int stp_state;
1131
1132 switch (state) {
1133 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001134 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001135 break;
1136 case BR_STATE_BLOCKING:
1137 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001138 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001139 break;
1140 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001141 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001142 break;
1143 case BR_STATE_FORWARDING:
1144 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001145 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001146 break;
1147 }
1148
1149 netdev_dbg(ds->ports[port], "port state %d [%d]\n", state, stp_state);
1150
1151 /* mv88e6xxx_port_stp_update may be called with softirqs disabled,
1152 * so we can not update the port state directly but need to schedule it.
1153 */
1154 ps->port_state[port] = stp_state;
1155 set_bit(port, &ps->port_state_update_mask);
1156 schedule_work(&ps->bridge_work);
1157
1158 return 0;
1159}
1160
Vivien Didelotb8fee952015-08-13 12:52:19 -04001161int mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid)
1162{
1163 int ret;
1164
1165 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN);
1166 if (ret < 0)
1167 return ret;
1168
1169 *pvid = ret & PORT_DEFAULT_VLAN_MASK;
1170
1171 return 0;
1172}
1173
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001174int mv88e6xxx_port_pvid_set(struct dsa_switch *ds, int port, u16 pvid)
1175{
1176 return mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
1177 pvid & PORT_DEFAULT_VLAN_MASK);
1178}
1179
Vivien Didelot6b17e862015-08-13 12:52:18 -04001180static int _mv88e6xxx_vtu_wait(struct dsa_switch *ds)
1181{
1182 return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_VTU_OP,
1183 GLOBAL_VTU_OP_BUSY);
1184}
1185
1186static int _mv88e6xxx_vtu_cmd(struct dsa_switch *ds, u16 op)
1187{
1188 int ret;
1189
1190 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_OP, op);
1191 if (ret < 0)
1192 return ret;
1193
1194 return _mv88e6xxx_vtu_wait(ds);
1195}
1196
1197static int _mv88e6xxx_vtu_stu_flush(struct dsa_switch *ds)
1198{
1199 int ret;
1200
1201 ret = _mv88e6xxx_vtu_wait(ds);
1202 if (ret < 0)
1203 return ret;
1204
1205 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_FLUSH_ALL);
1206}
1207
Vivien Didelotb8fee952015-08-13 12:52:19 -04001208static int _mv88e6xxx_vtu_stu_data_read(struct dsa_switch *ds,
1209 struct mv88e6xxx_vtu_stu_entry *entry,
1210 unsigned int nibble_offset)
1211{
1212 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1213 u16 regs[3];
1214 int i;
1215 int ret;
1216
1217 for (i = 0; i < 3; ++i) {
1218 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1219 GLOBAL_VTU_DATA_0_3 + i);
1220 if (ret < 0)
1221 return ret;
1222
1223 regs[i] = ret;
1224 }
1225
1226 for (i = 0; i < ps->num_ports; ++i) {
1227 unsigned int shift = (i % 4) * 4 + nibble_offset;
1228 u16 reg = regs[i / 4];
1229
1230 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1231 }
1232
1233 return 0;
1234}
1235
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001236static int _mv88e6xxx_vtu_stu_data_write(struct dsa_switch *ds,
1237 struct mv88e6xxx_vtu_stu_entry *entry,
1238 unsigned int nibble_offset)
1239{
1240 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1241 u16 regs[3] = { 0 };
1242 int i;
1243 int ret;
1244
1245 for (i = 0; i < ps->num_ports; ++i) {
1246 unsigned int shift = (i % 4) * 4 + nibble_offset;
1247 u8 data = entry->data[i];
1248
1249 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1250 }
1251
1252 for (i = 0; i < 3; ++i) {
1253 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL,
1254 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1255 if (ret < 0)
1256 return ret;
1257 }
1258
1259 return 0;
1260}
1261
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001262static int _mv88e6xxx_vtu_vid_write(struct dsa_switch *ds, u16 vid)
1263{
1264 return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID,
1265 vid & GLOBAL_VTU_VID_MASK);
1266}
1267
1268static int _mv88e6xxx_vtu_getnext(struct dsa_switch *ds,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001269 struct mv88e6xxx_vtu_stu_entry *entry)
1270{
1271 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1272 int ret;
1273
1274 ret = _mv88e6xxx_vtu_wait(ds);
1275 if (ret < 0)
1276 return ret;
1277
Vivien Didelotb8fee952015-08-13 12:52:19 -04001278 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_GET_NEXT);
1279 if (ret < 0)
1280 return ret;
1281
1282 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1283 if (ret < 0)
1284 return ret;
1285
1286 next.vid = ret & GLOBAL_VTU_VID_MASK;
1287 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1288
1289 if (next.valid) {
1290 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 0);
1291 if (ret < 0)
1292 return ret;
1293
1294 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1295 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1296 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1297 GLOBAL_VTU_FID);
1298 if (ret < 0)
1299 return ret;
1300
1301 next.fid = ret & GLOBAL_VTU_FID_MASK;
1302
1303 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1304 GLOBAL_VTU_SID);
1305 if (ret < 0)
1306 return ret;
1307
1308 next.sid = ret & GLOBAL_VTU_SID_MASK;
1309 }
1310 }
1311
1312 *entry = next;
1313 return 0;
1314}
1315
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001316static int _mv88e6xxx_vtu_loadpurge(struct dsa_switch *ds,
1317 struct mv88e6xxx_vtu_stu_entry *entry)
1318{
1319 u16 reg = 0;
1320 int ret;
1321
1322 ret = _mv88e6xxx_vtu_wait(ds);
1323 if (ret < 0)
1324 return ret;
1325
1326 if (!entry->valid)
1327 goto loadpurge;
1328
1329 /* Write port member tags */
1330 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 0);
1331 if (ret < 0)
1332 return ret;
1333
1334 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1335 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1336 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1337 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1338 if (ret < 0)
1339 return ret;
1340
1341 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1342 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_FID, reg);
1343 if (ret < 0)
1344 return ret;
1345 }
1346
1347 reg = GLOBAL_VTU_VID_VALID;
1348loadpurge:
1349 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1350 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1351 if (ret < 0)
1352 return ret;
1353
1354 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_LOAD_PURGE);
1355}
1356
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001357static int _mv88e6xxx_stu_getnext(struct dsa_switch *ds, u8 sid,
1358 struct mv88e6xxx_vtu_stu_entry *entry)
1359{
1360 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1361 int ret;
1362
1363 ret = _mv88e6xxx_vtu_wait(ds);
1364 if (ret < 0)
1365 return ret;
1366
1367 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID,
1368 sid & GLOBAL_VTU_SID_MASK);
1369 if (ret < 0)
1370 return ret;
1371
1372 ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_GET_NEXT);
1373 if (ret < 0)
1374 return ret;
1375
1376 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_SID);
1377 if (ret < 0)
1378 return ret;
1379
1380 next.sid = ret & GLOBAL_VTU_SID_MASK;
1381
1382 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID);
1383 if (ret < 0)
1384 return ret;
1385
1386 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1387
1388 if (next.valid) {
1389 ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 2);
1390 if (ret < 0)
1391 return ret;
1392 }
1393
1394 *entry = next;
1395 return 0;
1396}
1397
1398static int _mv88e6xxx_stu_loadpurge(struct dsa_switch *ds,
1399 struct mv88e6xxx_vtu_stu_entry *entry)
1400{
1401 u16 reg = 0;
1402 int ret;
1403
1404 ret = _mv88e6xxx_vtu_wait(ds);
1405 if (ret < 0)
1406 return ret;
1407
1408 if (!entry->valid)
1409 goto loadpurge;
1410
1411 /* Write port states */
1412 ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 2);
1413 if (ret < 0)
1414 return ret;
1415
1416 reg = GLOBAL_VTU_VID_VALID;
1417loadpurge:
1418 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1419 if (ret < 0)
1420 return ret;
1421
1422 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1423 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1424 if (ret < 0)
1425 return ret;
1426
1427 return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1428}
1429
1430static int _mv88e6xxx_vlan_init(struct dsa_switch *ds, u16 vid,
1431 struct mv88e6xxx_vtu_stu_entry *entry)
1432{
1433 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1434 struct mv88e6xxx_vtu_stu_entry vlan = {
1435 .valid = true,
1436 .vid = vid,
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001437 .fid = vid, /* We use one FID per VLAN */
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001438 };
1439 int i;
1440
1441 /* exclude all ports except the CPU */
1442 for (i = 0; i < ps->num_ports; ++i)
1443 vlan.data[i] = dsa_is_cpu_port(ds, i) ?
1444 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED :
1445 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1446
1447 if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) ||
1448 mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) {
1449 struct mv88e6xxx_vtu_stu_entry vstp;
1450 int err;
1451
1452 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1453 * implemented, only one STU entry is needed to cover all VTU
1454 * entries. Thus, validate the SID 0.
1455 */
1456 vlan.sid = 0;
1457 err = _mv88e6xxx_stu_getnext(ds, GLOBAL_VTU_SID_MASK, &vstp);
1458 if (err)
1459 return err;
1460
1461 if (vstp.sid != vlan.sid || !vstp.valid) {
1462 memset(&vstp, 0, sizeof(vstp));
1463 vstp.valid = true;
1464 vstp.sid = vlan.sid;
1465
1466 err = _mv88e6xxx_stu_loadpurge(ds, &vstp);
1467 if (err)
1468 return err;
1469 }
1470
Vivien Didelot7c400012015-09-04 14:34:14 -04001471 /* Clear all MAC addresses from the new database */
1472 err = _mv88e6xxx_atu_flush(ds, vlan.fid, true);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001473 if (err)
1474 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001475 }
1476
1477 *entry = vlan;
1478 return 0;
1479}
1480
1481int mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, u16 vid,
1482 bool untagged)
1483{
1484 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1485 struct mv88e6xxx_vtu_stu_entry vlan;
1486 int err;
1487
1488 mutex_lock(&ps->smi_mutex);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001489
1490 err = _mv88e6xxx_vtu_vid_write(ds, vid - 1);
1491 if (err)
1492 goto unlock;
1493
1494 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001495 if (err)
1496 goto unlock;
1497
1498 if (vlan.vid != vid || !vlan.valid) {
1499 err = _mv88e6xxx_vlan_init(ds, vid, &vlan);
1500 if (err)
1501 goto unlock;
1502 }
1503
1504 vlan.data[port] = untagged ?
1505 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1506 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1507
1508 err = _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1509unlock:
1510 mutex_unlock(&ps->smi_mutex);
1511
1512 return err;
1513}
1514
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001515int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, u16 vid)
1516{
1517 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1518 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001519 int i, err;
1520
1521 mutex_lock(&ps->smi_mutex);
1522
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001523 err = _mv88e6xxx_vtu_vid_write(ds, vid - 1);
1524 if (err)
1525 goto unlock;
1526
1527 err = _mv88e6xxx_vtu_getnext(ds, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001528 if (err)
1529 goto unlock;
1530
1531 if (vlan.vid != vid || !vlan.valid ||
1532 vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
1533 err = -ENOENT;
1534 goto unlock;
1535 }
1536
1537 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1538
1539 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001540 vlan.valid = false;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001541 for (i = 0; i < ps->num_ports; ++i) {
1542 if (dsa_is_cpu_port(ds, i))
1543 continue;
1544
1545 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001546 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001547 break;
1548 }
1549 }
1550
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001551 err = _mv88e6xxx_vtu_loadpurge(ds, &vlan);
1552 if (err)
1553 goto unlock;
1554
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001555 err = _mv88e6xxx_atu_remove(ds, vlan.fid, port, false);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001556unlock:
1557 mutex_unlock(&ps->smi_mutex);
1558
1559 return err;
1560}
1561
Vivien Didelot02512b62015-08-13 12:52:20 -04001562static int _mv88e6xxx_port_vtu_getnext(struct dsa_switch *ds, int port, u16 vid,
1563 struct mv88e6xxx_vtu_stu_entry *entry)
1564{
1565 int err;
1566
1567 do {
1568 if (vid == 4095)
1569 return -ENOENT;
1570
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001571 err = _mv88e6xxx_vtu_vid_write(ds, vid);
1572 if (err)
1573 return err;
1574
1575 err = _mv88e6xxx_vtu_getnext(ds, entry);
Vivien Didelot02512b62015-08-13 12:52:20 -04001576 if (err)
1577 return err;
1578
1579 if (!entry->valid)
1580 return -ENOENT;
1581
1582 vid = entry->vid;
1583 } while (entry->data[port] != GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED &&
1584 entry->data[port] != GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED);
1585
1586 return 0;
1587}
1588
Vivien Didelotb8fee952015-08-13 12:52:19 -04001589int mv88e6xxx_vlan_getnext(struct dsa_switch *ds, u16 *vid,
1590 unsigned long *ports, unsigned long *untagged)
1591{
1592 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1593 struct mv88e6xxx_vtu_stu_entry next;
1594 int port;
1595 int err;
1596
1597 if (*vid == 4095)
1598 return -ENOENT;
1599
1600 mutex_lock(&ps->smi_mutex);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001601 err = _mv88e6xxx_vtu_vid_write(ds, *vid);
1602 if (err)
1603 goto unlock;
1604
1605 err = _mv88e6xxx_vtu_getnext(ds, &next);
1606unlock:
Vivien Didelotb8fee952015-08-13 12:52:19 -04001607 mutex_unlock(&ps->smi_mutex);
1608
1609 if (err)
1610 return err;
1611
1612 if (!next.valid)
1613 return -ENOENT;
1614
1615 *vid = next.vid;
1616
1617 for (port = 0; port < ps->num_ports; ++port) {
1618 clear_bit(port, ports);
1619 clear_bit(port, untagged);
1620
1621 if (dsa_is_cpu_port(ds, port))
1622 continue;
1623
1624 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED ||
1625 next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1626 set_bit(port, ports);
1627
1628 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1629 set_bit(port, untagged);
1630 }
1631
1632 return 0;
1633}
1634
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001635static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds,
1636 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001637{
1638 int i, ret;
1639
1640 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001641 ret = _mv88e6xxx_reg_write(
1642 ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
1643 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001644 if (ret < 0)
1645 return ret;
1646 }
1647
1648 return 0;
1649}
1650
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001651static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001652{
1653 int i, ret;
1654
1655 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02001656 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL,
1657 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001658 if (ret < 0)
1659 return ret;
1660 addr[i * 2] = ret >> 8;
1661 addr[i * 2 + 1] = ret & 0xff;
1662 }
1663
1664 return 0;
1665}
1666
Vivien Didelotfd231c82015-08-10 09:09:50 -04001667static int _mv88e6xxx_atu_load(struct dsa_switch *ds,
1668 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001669{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001670 int ret;
1671
1672 ret = _mv88e6xxx_atu_wait(ds);
1673 if (ret < 0)
1674 return ret;
1675
Vivien Didelotfd231c82015-08-10 09:09:50 -04001676 ret = _mv88e6xxx_atu_mac_write(ds, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001677 if (ret < 0)
1678 return ret;
1679
Vivien Didelot37705b72015-09-04 14:34:11 -04001680 ret = _mv88e6xxx_atu_data_write(ds, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04001681 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001682 return ret;
1683
Vivien Didelot70cc99d2015-09-04 14:34:10 -04001684 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, entry->fid);
1685 if (ret < 0)
1686 return ret;
1687
1688 return _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04001689}
David S. Millercdf09692015-08-11 12:00:37 -07001690
Vivien Didelotfd231c82015-08-10 09:09:50 -04001691static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port,
1692 const unsigned char *addr, u16 vid,
1693 u8 state)
1694{
1695 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelotfd231c82015-08-10 09:09:50 -04001696
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001697 entry.fid = vid; /* We use one FID per VLAN */
Vivien Didelotfd231c82015-08-10 09:09:50 -04001698 entry.state = state;
1699 ether_addr_copy(entry.mac, addr);
1700 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1701 entry.trunk = false;
1702 entry.portv_trunkid = BIT(port);
1703 }
1704
1705 return _mv88e6xxx_atu_load(ds, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001706}
1707
Vivien Didelot146a3202015-10-08 11:35:12 -04001708int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
1709 const struct switchdev_obj_port_fdb *fdb,
1710 struct switchdev_trans *trans)
1711{
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001712 /* We don't use per-port FDB */
1713 if (fdb->vid == 0)
1714 return -EOPNOTSUPP;
1715
Vivien Didelot146a3202015-10-08 11:35:12 -04001716 /* We don't need any dynamic resource from the kernel (yet),
1717 * so skip the prepare phase.
1718 */
1719 return 0;
1720}
1721
David S. Millercdf09692015-08-11 12:00:37 -07001722int mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
Vivien Didelot1f36faf2015-10-08 11:35:13 -04001723 const struct switchdev_obj_port_fdb *fdb,
1724 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001725{
Vivien Didelot1f36faf2015-10-08 11:35:13 -04001726 int state = is_multicast_ether_addr(fdb->addr) ?
David S. Millercdf09692015-08-11 12:00:37 -07001727 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1728 GLOBAL_ATU_DATA_STATE_UC_STATIC;
1729 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot6630e232015-08-06 01:44:07 -04001730 int ret;
1731
David S. Millercdf09692015-08-11 12:00:37 -07001732 mutex_lock(&ps->smi_mutex);
Vivien Didelot1f36faf2015-10-08 11:35:13 -04001733 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, state);
David S. Millercdf09692015-08-11 12:00:37 -07001734 mutex_unlock(&ps->smi_mutex);
1735
1736 return ret;
1737}
1738
1739int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
Vivien Didelot8057b3e2015-10-08 11:35:14 -04001740 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07001741{
1742 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1743 int ret;
1744
1745 mutex_lock(&ps->smi_mutex);
Vivien Didelot8057b3e2015-10-08 11:35:14 -04001746 ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid,
David S. Millercdf09692015-08-11 12:00:37 -07001747 GLOBAL_ATU_DATA_STATE_UNUSED);
1748 mutex_unlock(&ps->smi_mutex);
1749
1750 return ret;
1751}
1752
Vivien Didelot1d194042015-08-10 09:09:51 -04001753static int _mv88e6xxx_atu_getnext(struct dsa_switch *ds, u16 fid,
1754 const unsigned char *addr,
1755 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07001756{
Vivien Didelot1d194042015-08-10 09:09:51 -04001757 struct mv88e6xxx_atu_entry next = { 0 };
1758 int ret;
1759
1760 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001761
1762 ret = _mv88e6xxx_atu_wait(ds);
1763 if (ret < 0)
1764 return ret;
1765
Vivien Didelotc5723ac2015-08-10 09:09:48 -04001766 ret = _mv88e6xxx_atu_mac_write(ds, addr);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001767 if (ret < 0)
1768 return ret;
1769
Vivien Didelot70cc99d2015-09-04 14:34:10 -04001770 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid);
1771 if (ret < 0)
1772 return ret;
1773
1774 ret = _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001775 if (ret < 0)
1776 return ret;
1777
Vivien Didelot1d194042015-08-10 09:09:51 -04001778 ret = _mv88e6xxx_atu_mac_read(ds, next.mac);
1779 if (ret < 0)
1780 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001781
Vivien Didelot1d194042015-08-10 09:09:51 -04001782 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
1783 if (ret < 0)
1784 return ret;
1785
1786 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
1787 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1788 unsigned int mask, shift;
1789
1790 if (ret & GLOBAL_ATU_DATA_TRUNK) {
1791 next.trunk = true;
1792 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1793 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1794 } else {
1795 next.trunk = false;
1796 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1797 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1798 }
1799
1800 next.portv_trunkid = (ret & mask) >> shift;
1801 }
1802
1803 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001804 return 0;
1805}
1806
David S. Millercdf09692015-08-11 12:00:37 -07001807/* get next entry for port */
1808int mv88e6xxx_port_fdb_getnext(struct dsa_switch *ds, int port,
Vivien Didelot2a778e12015-08-10 09:09:49 -04001809 unsigned char *addr, u16 *vid, bool *is_static)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001810{
1811 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot1d194042015-08-10 09:09:51 -04001812 struct mv88e6xxx_atu_entry next;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001813 u16 fid = *vid; /* We use one FID per VLAN */
Vivien Didelot87820512015-08-06 01:44:08 -04001814 int ret;
1815
1816 mutex_lock(&ps->smi_mutex);
Vivien Didelot1d194042015-08-10 09:09:51 -04001817
Vivien Didelot1d194042015-08-10 09:09:51 -04001818 do {
1819 if (is_broadcast_ether_addr(addr)) {
Vivien Didelot02512b62015-08-13 12:52:20 -04001820 struct mv88e6xxx_vtu_stu_entry vtu;
1821
1822 ret = _mv88e6xxx_port_vtu_getnext(ds, port, *vid, &vtu);
1823 if (ret < 0)
1824 goto unlock;
1825
1826 *vid = vtu.vid;
1827 fid = vtu.fid;
Vivien Didelot1d194042015-08-10 09:09:51 -04001828 }
1829
1830 ret = _mv88e6xxx_atu_getnext(ds, fid, addr, &next);
1831 if (ret < 0)
1832 goto unlock;
1833
1834 ether_addr_copy(addr, next.mac);
1835
1836 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
1837 continue;
1838 } while (next.trunk || (next.portv_trunkid & BIT(port)) == 0);
1839
1840 *is_static = next.state == (is_multicast_ether_addr(addr) ?
1841 GLOBAL_ATU_DATA_STATE_MC_STATIC :
1842 GLOBAL_ATU_DATA_STATE_UC_STATIC);
1843unlock:
Guenter Roeckdefb05b2015-03-26 18:36:38 -07001844 mutex_unlock(&ps->smi_mutex);
1845
1846 return ret;
1847}
1848
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001849static void mv88e6xxx_bridge_work(struct work_struct *work)
1850{
1851 struct mv88e6xxx_priv_state *ps;
1852 struct dsa_switch *ds;
1853 int port;
1854
1855 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
1856 ds = ((struct dsa_switch *)ps) - 1;
1857
1858 while (ps->port_state_update_mask) {
1859 port = __ffs(ps->port_state_update_mask);
1860 clear_bit(port, &ps->port_state_update_mask);
1861 mv88e6xxx_set_port_state(ds, port, ps->port_state[port]);
1862 }
1863}
1864
Andrew Lunndbde9e62015-05-06 01:09:48 +02001865static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07001866{
1867 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001868 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02001869 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07001870
1871 mutex_lock(&ps->smi_mutex);
1872
Andrew Lunn54d792f2015-05-06 01:09:47 +02001873 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1874 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1875 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001876 mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02001877 /* MAC Forcing register: don't force link, speed,
1878 * duplex or flow control state to any particular
1879 * values on physical ports, but force the CPU port
1880 * and all DSA ports to their maximum bandwidth and
1881 * full duplex.
1882 */
1883 reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02001884 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01001885 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02001886 reg |= PORT_PCS_CTRL_FORCE_LINK |
1887 PORT_PCS_CTRL_LINK_UP |
1888 PORT_PCS_CTRL_DUPLEX_FULL |
1889 PORT_PCS_CTRL_FORCE_DUPLEX;
1890 if (mv88e6xxx_6065_family(ds))
1891 reg |= PORT_PCS_CTRL_100;
1892 else
1893 reg |= PORT_PCS_CTRL_1000;
1894 } else {
1895 reg |= PORT_PCS_CTRL_UNFORCED;
1896 }
1897
1898 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1899 PORT_PCS_CTRL, reg);
1900 if (ret)
1901 goto abort;
1902 }
1903
1904 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
1905 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
1906 * tunneling, determine priority by looking at 802.1p and IP
1907 * priority fields (IP prio has precedence), and set STP state
1908 * to Forwarding.
1909 *
1910 * If this is the CPU link, use DSA or EDSA tagging depending
1911 * on which tagging mode was configured.
1912 *
1913 * If this is a link to another switch, use DSA tagging mode.
1914 *
1915 * If this is the upstream port for this switch, enable
1916 * forwarding of unknown unicasts and multicasts.
1917 */
1918 reg = 0;
1919 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1920 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1921 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001922 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02001923 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
1924 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
1925 PORT_CONTROL_STATE_FORWARDING;
1926 if (dsa_is_cpu_port(ds, port)) {
1927 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
1928 reg |= PORT_CONTROL_DSA_TAG;
1929 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001930 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1931 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02001932 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
1933 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
1934 else
1935 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunnc047a1f2015-09-29 01:50:56 +02001936 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
1937 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn54d792f2015-05-06 01:09:47 +02001938 }
1939
1940 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1941 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1942 mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001943 mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02001944 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
1945 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
1946 }
1947 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02001948 if (dsa_is_dsa_port(ds, port)) {
1949 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds))
1950 reg |= PORT_CONTROL_DSA_TAG;
1951 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1952 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
1953 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02001954 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02001955 }
1956
Andrew Lunn54d792f2015-05-06 01:09:47 +02001957 if (port == dsa_upstream_port(ds))
1958 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
1959 PORT_CONTROL_FORWARD_UNKNOWN_MC;
1960 }
1961 if (reg) {
1962 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1963 PORT_CONTROL, reg);
1964 if (ret)
1965 goto abort;
1966 }
1967
Vivien Didelot8efdda42015-08-13 12:52:23 -04001968 /* Port Control 2: don't force a good FCS, set the maximum frame size to
1969 * 10240 bytes, enable secure 802.1q tags, don't discard tagged or
1970 * untagged frames on this port, do a destination address lookup on all
1971 * received packets as usual, disable ARP mirroring and don't send a
1972 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02001973 */
1974 reg = 0;
1975 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
1976 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001977 mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02001978 reg = PORT_CONTROL_2_MAP_DA;
1979
1980 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07001981 mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds))
Andrew Lunn54d792f2015-05-06 01:09:47 +02001982 reg |= PORT_CONTROL_2_JUMBO_10240;
1983
1984 if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) {
1985 /* Set the upstream port this port should use */
1986 reg |= dsa_upstream_port(ds);
1987 /* enable forwarding of unknown multicast addresses to
1988 * the upstream port
1989 */
1990 if (port == dsa_upstream_port(ds))
1991 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
1992 }
1993
Vivien Didelot5fe7f682015-10-11 18:08:38 -04001994 reg |= PORT_CONTROL_2_8021Q_SECURE;
Vivien Didelot8efdda42015-08-13 12:52:23 -04001995
Andrew Lunn54d792f2015-05-06 01:09:47 +02001996 if (reg) {
1997 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
1998 PORT_CONTROL_2, reg);
1999 if (ret)
2000 goto abort;
2001 }
2002
2003 /* Port Association Vector: when learning source addresses
2004 * of packets, add the address to the address database using
2005 * a port bitmap that has only the bit for this port set and
2006 * the other bits clear.
2007 */
2008 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR,
2009 1 << port);
2010 if (ret)
2011 goto abort;
2012
2013 /* Egress rate control 2: disable egress rate control. */
2014 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2,
2015 0x0000);
2016 if (ret)
2017 goto abort;
2018
2019 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002020 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2021 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002022 /* Do not limit the period of time that this port can
2023 * be paused for by the remote end or the period of
2024 * time that this port can pause the remote end.
2025 */
2026 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2027 PORT_PAUSE_CTRL, 0x0000);
2028 if (ret)
2029 goto abort;
2030
2031 /* Port ATU control: disable limiting the number of
2032 * address database entries that this port is allowed
2033 * to use.
2034 */
2035 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2036 PORT_ATU_CONTROL, 0x0000);
2037 /* Priority Override: disable DA, SA and VTU priority
2038 * override.
2039 */
2040 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2041 PORT_PRI_OVERRIDE, 0x0000);
2042 if (ret)
2043 goto abort;
2044
2045 /* Port Ethertype: use the Ethertype DSA Ethertype
2046 * value.
2047 */
2048 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2049 PORT_ETH_TYPE, ETH_P_EDSA);
2050 if (ret)
2051 goto abort;
2052 /* Tag Remap: use an identity 802.1p prio -> switch
2053 * prio mapping.
2054 */
2055 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2056 PORT_TAG_REGMAP_0123, 0x3210);
2057 if (ret)
2058 goto abort;
2059
2060 /* Tag Remap 2: use an identity 802.1p prio -> switch
2061 * prio mapping.
2062 */
2063 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2064 PORT_TAG_REGMAP_4567, 0x7654);
2065 if (ret)
2066 goto abort;
2067 }
2068
2069 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2070 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002071 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2072 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002073 /* Rate Control: disable ingress rate limiting. */
2074 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port),
2075 PORT_RATE_CONTROL, 0x0001);
2076 if (ret)
2077 goto abort;
2078 }
2079
Guenter Roeck366f0a02015-03-26 18:36:30 -07002080 /* Port Control 1: disable trunking, disable sending
2081 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002082 */
Vivien Didelot614f03f2015-04-20 17:19:23 -04002083 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002084 if (ret)
2085 goto abort;
2086
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002087 /* Port based VLAN map: do not give each port its own address
Vivien Didelot5fe7f682015-10-11 18:08:38 -04002088 * database, and allow every port to egress frames on all other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002089 */
Vivien Didelot5fe7f682015-10-11 18:08:38 -04002090 reg = BIT(ps->num_ports) - 1; /* all ports */
Vivien Didelotede80982015-10-11 18:08:35 -04002091 ret = _mv88e6xxx_port_vlan_map_set(ds, port, reg & ~port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002092 if (ret)
2093 goto abort;
2094
2095 /* Default VLAN ID and priority: don't set a default VLAN
2096 * ID, and set the default packet priority to zero.
2097 */
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002098 ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN,
2099 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002100abort:
2101 mutex_unlock(&ps->smi_mutex);
2102 return ret;
2103}
2104
Andrew Lunndbde9e62015-05-06 01:09:48 +02002105int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2106{
2107 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2108 int ret;
2109 int i;
2110
2111 for (i = 0; i < ps->num_ports; i++) {
2112 ret = mv88e6xxx_setup_port(ds, i);
2113 if (ret < 0)
2114 return ret;
2115 }
2116 return 0;
2117}
2118
Andrew Lunn87c8cef2015-06-20 18:42:28 +02002119static int mv88e6xxx_regs_show(struct seq_file *s, void *p)
2120{
2121 struct dsa_switch *ds = s->private;
2122
2123 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2124 int reg, port;
2125
2126 seq_puts(s, " GLOBAL GLOBAL2 ");
2127 for (port = 0 ; port < ps->num_ports; port++)
2128 seq_printf(s, " %2d ", port);
2129 seq_puts(s, "\n");
2130
2131 for (reg = 0; reg < 32; reg++) {
2132 seq_printf(s, "%2x: ", reg);
2133 seq_printf(s, " %4x %4x ",
2134 mv88e6xxx_reg_read(ds, REG_GLOBAL, reg),
2135 mv88e6xxx_reg_read(ds, REG_GLOBAL2, reg));
2136
2137 for (port = 0 ; port < ps->num_ports; port++)
2138 seq_printf(s, "%4x ",
2139 mv88e6xxx_reg_read(ds, REG_PORT(port), reg));
2140 seq_puts(s, "\n");
2141 }
2142
2143 return 0;
2144}
2145
2146static int mv88e6xxx_regs_open(struct inode *inode, struct file *file)
2147{
2148 return single_open(file, mv88e6xxx_regs_show, inode->i_private);
2149}
2150
2151static const struct file_operations mv88e6xxx_regs_fops = {
2152 .open = mv88e6xxx_regs_open,
2153 .read = seq_read,
2154 .llseek = no_llseek,
2155 .release = single_release,
2156 .owner = THIS_MODULE,
2157};
2158
Andrew Lunn8a0a2652015-06-20 18:42:29 +02002159static void mv88e6xxx_atu_show_header(struct seq_file *s)
2160{
2161 seq_puts(s, "DB T/P Vec State Addr\n");
2162}
2163
2164static void mv88e6xxx_atu_show_entry(struct seq_file *s, int dbnum,
2165 unsigned char *addr, int data)
2166{
2167 bool trunk = !!(data & GLOBAL_ATU_DATA_TRUNK);
2168 int portvec = ((data & GLOBAL_ATU_DATA_PORT_VECTOR_MASK) >>
2169 GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT);
2170 int state = data & GLOBAL_ATU_DATA_STATE_MASK;
2171
2172 seq_printf(s, "%03x %5s %10pb %x %pM\n",
2173 dbnum, (trunk ? "Trunk" : "Port"), &portvec, state, addr);
2174}
2175
2176static int mv88e6xxx_atu_show_db(struct seq_file *s, struct dsa_switch *ds,
2177 int dbnum)
2178{
2179 unsigned char bcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2180 unsigned char addr[6];
2181 int ret, data, state;
2182
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002183 ret = _mv88e6xxx_atu_mac_write(ds, bcast);
Andrew Lunn8a0a2652015-06-20 18:42:29 +02002184 if (ret < 0)
2185 return ret;
2186
2187 do {
Vivien Didelot70cc99d2015-09-04 14:34:10 -04002188 ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID,
2189 dbnum);
Andrew Lunn8a0a2652015-06-20 18:42:29 +02002190 if (ret < 0)
2191 return ret;
Vivien Didelot70cc99d2015-09-04 14:34:10 -04002192
2193 ret = _mv88e6xxx_atu_cmd(ds, GLOBAL_ATU_OP_GET_NEXT_DB);
2194 if (ret < 0)
2195 return ret;
2196
Andrew Lunn8a0a2652015-06-20 18:42:29 +02002197 data = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA);
2198 if (data < 0)
2199 return data;
2200
2201 state = data & GLOBAL_ATU_DATA_STATE_MASK;
2202 if (state == GLOBAL_ATU_DATA_STATE_UNUSED)
2203 break;
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002204 ret = _mv88e6xxx_atu_mac_read(ds, addr);
Andrew Lunn8a0a2652015-06-20 18:42:29 +02002205 if (ret < 0)
2206 return ret;
2207 mv88e6xxx_atu_show_entry(s, dbnum, addr, data);
2208 } while (state != GLOBAL_ATU_DATA_STATE_UNUSED);
2209
2210 return 0;
2211}
2212
2213static int mv88e6xxx_atu_show(struct seq_file *s, void *p)
2214{
2215 struct dsa_switch *ds = s->private;
2216 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2217 int dbnum;
2218
2219 mv88e6xxx_atu_show_header(s);
2220
2221 for (dbnum = 0; dbnum < 255; dbnum++) {
2222 mutex_lock(&ps->smi_mutex);
2223 mv88e6xxx_atu_show_db(s, ds, dbnum);
2224 mutex_unlock(&ps->smi_mutex);
2225 }
2226
2227 return 0;
2228}
2229
2230static int mv88e6xxx_atu_open(struct inode *inode, struct file *file)
2231{
2232 return single_open(file, mv88e6xxx_atu_show, inode->i_private);
2233}
2234
2235static const struct file_operations mv88e6xxx_atu_fops = {
2236 .open = mv88e6xxx_atu_open,
2237 .read = seq_read,
2238 .llseek = no_llseek,
2239 .release = single_release,
2240 .owner = THIS_MODULE,
2241};
2242
Andrew Lunn532c7a32015-06-20 18:42:31 +02002243static void mv88e6xxx_stats_show_header(struct seq_file *s,
2244 struct mv88e6xxx_priv_state *ps)
2245{
2246 int port;
2247
2248 seq_puts(s, " Statistic ");
2249 for (port = 0 ; port < ps->num_ports; port++)
2250 seq_printf(s, "Port %2d ", port);
2251 seq_puts(s, "\n");
2252}
2253
2254static int mv88e6xxx_stats_show(struct seq_file *s, void *p)
2255{
2256 struct dsa_switch *ds = s->private;
2257 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2258 struct mv88e6xxx_hw_stat *stats = mv88e6xxx_hw_stats;
2259 int port, stat, max_stats;
2260 uint64_t value;
2261
2262 if (have_sw_in_discards(ds))
2263 max_stats = ARRAY_SIZE(mv88e6xxx_hw_stats);
2264 else
2265 max_stats = ARRAY_SIZE(mv88e6xxx_hw_stats) - 3;
2266
2267 mv88e6xxx_stats_show_header(s, ps);
2268
2269 mutex_lock(&ps->smi_mutex);
2270
2271 for (stat = 0; stat < max_stats; stat++) {
2272 seq_printf(s, "%19s: ", stats[stat].string);
2273 for (port = 0 ; port < ps->num_ports; port++) {
2274 _mv88e6xxx_stats_snapshot(ds, port);
2275 value = _mv88e6xxx_get_ethtool_stat(ds, stat, stats,
2276 port);
2277 seq_printf(s, "%8llu ", value);
2278 }
2279 seq_puts(s, "\n");
2280 }
2281 mutex_unlock(&ps->smi_mutex);
2282
2283 return 0;
2284}
2285
2286static int mv88e6xxx_stats_open(struct inode *inode, struct file *file)
2287{
2288 return single_open(file, mv88e6xxx_stats_show, inode->i_private);
2289}
2290
2291static const struct file_operations mv88e6xxx_stats_fops = {
2292 .open = mv88e6xxx_stats_open,
2293 .read = seq_read,
2294 .llseek = no_llseek,
2295 .release = single_release,
2296 .owner = THIS_MODULE,
2297};
2298
Andrew Lunnd35bd872015-06-20 18:42:32 +02002299static int mv88e6xxx_device_map_show(struct seq_file *s, void *p)
2300{
2301 struct dsa_switch *ds = s->private;
2302 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2303 int target, ret;
2304
2305 seq_puts(s, "Target Port\n");
2306
2307 mutex_lock(&ps->smi_mutex);
2308 for (target = 0; target < 32; target++) {
2309 ret = _mv88e6xxx_reg_write(
2310 ds, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
2311 target << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT);
2312 if (ret < 0)
2313 goto out;
2314 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL2,
2315 GLOBAL2_DEVICE_MAPPING);
2316 seq_printf(s, " %2d %2d\n", target,
2317 ret & GLOBAL2_DEVICE_MAPPING_PORT_MASK);
2318 }
2319out:
2320 mutex_unlock(&ps->smi_mutex);
2321
2322 return 0;
2323}
2324
2325static int mv88e6xxx_device_map_open(struct inode *inode, struct file *file)
2326{
2327 return single_open(file, mv88e6xxx_device_map_show, inode->i_private);
2328}
2329
2330static const struct file_operations mv88e6xxx_device_map_fops = {
2331 .open = mv88e6xxx_device_map_open,
2332 .read = seq_read,
2333 .llseek = no_llseek,
2334 .release = single_release,
2335 .owner = THIS_MODULE,
2336};
2337
Andrew Lunn56d95e22015-06-20 18:42:33 +02002338static int mv88e6xxx_scratch_show(struct seq_file *s, void *p)
2339{
2340 struct dsa_switch *ds = s->private;
2341 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2342 int reg, ret;
2343
2344 seq_puts(s, "Register Value\n");
2345
2346 mutex_lock(&ps->smi_mutex);
2347 for (reg = 0; reg < 0x80; reg++) {
2348 ret = _mv88e6xxx_reg_write(
2349 ds, REG_GLOBAL2, GLOBAL2_SCRATCH_MISC,
2350 reg << GLOBAL2_SCRATCH_REGISTER_SHIFT);
2351 if (ret < 0)
2352 goto out;
2353
2354 ret = _mv88e6xxx_scratch_wait(ds);
2355 if (ret < 0)
2356 goto out;
2357
2358 ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL2,
2359 GLOBAL2_SCRATCH_MISC);
2360 seq_printf(s, " %2x %2x\n", reg,
2361 ret & GLOBAL2_SCRATCH_VALUE_MASK);
2362 }
2363out:
2364 mutex_unlock(&ps->smi_mutex);
2365
2366 return 0;
2367}
2368
2369static int mv88e6xxx_scratch_open(struct inode *inode, struct file *file)
2370{
2371 return single_open(file, mv88e6xxx_scratch_show, inode->i_private);
2372}
2373
2374static const struct file_operations mv88e6xxx_scratch_fops = {
2375 .open = mv88e6xxx_scratch_open,
2376 .read = seq_read,
2377 .llseek = no_llseek,
2378 .release = single_release,
2379 .owner = THIS_MODULE,
2380};
2381
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002382int mv88e6xxx_setup_common(struct dsa_switch *ds)
2383{
2384 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Andrew Lunn87c8cef2015-06-20 18:42:28 +02002385 char *name;
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002386
2387 mutex_init(&ps->smi_mutex);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002388
Andrew Lunncca8b132015-04-02 04:06:39 +02002389 ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
Andrew Lunna8f064c2015-03-26 18:36:40 -07002390
Guenter Roeckfacd95b2015-03-26 18:36:35 -07002391 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
2392
Andrew Lunn87c8cef2015-06-20 18:42:28 +02002393 name = kasprintf(GFP_KERNEL, "dsa%d", ds->index);
2394 ps->dbgfs = debugfs_create_dir(name, NULL);
2395 kfree(name);
2396
2397 debugfs_create_file("regs", S_IRUGO, ps->dbgfs, ds,
2398 &mv88e6xxx_regs_fops);
2399
Andrew Lunn8a0a2652015-06-20 18:42:29 +02002400 debugfs_create_file("atu", S_IRUGO, ps->dbgfs, ds,
2401 &mv88e6xxx_atu_fops);
2402
Andrew Lunn532c7a32015-06-20 18:42:31 +02002403 debugfs_create_file("stats", S_IRUGO, ps->dbgfs, ds,
2404 &mv88e6xxx_stats_fops);
2405
Andrew Lunnd35bd872015-06-20 18:42:32 +02002406 debugfs_create_file("device_map", S_IRUGO, ps->dbgfs, ds,
2407 &mv88e6xxx_device_map_fops);
Andrew Lunn56d95e22015-06-20 18:42:33 +02002408
2409 debugfs_create_file("scratch", S_IRUGO, ps->dbgfs, ds,
2410 &mv88e6xxx_scratch_fops);
Guenter Roeckacdaffc2015-03-26 18:36:28 -07002411 return 0;
2412}
2413
Andrew Lunn54d792f2015-05-06 01:09:47 +02002414int mv88e6xxx_setup_global(struct dsa_switch *ds)
2415{
2416 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
Vivien Didelot24751e22015-08-03 09:17:44 -04002417 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002418 int i;
2419
2420 /* Set the default address aging time to 5 minutes, and
2421 * enable address learn messages to be sent to all message
2422 * ports.
2423 */
2424 REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
2425 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2426
2427 /* Configure the IP ToS mapping registers. */
2428 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2429 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2430 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2431 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2432 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2433 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2434 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2435 REG_WRITE(REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2436
2437 /* Configure the IEEE 802.1p priority mapping register. */
2438 REG_WRITE(REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2439
2440 /* Send all frames with destination addresses matching
2441 * 01:80:c2:00:00:0x to the CPU port.
2442 */
2443 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
2444
2445 /* Ignore removed tag data on doubly tagged packets, disable
2446 * flow control messages, force flow control priority to the
2447 * highest, and send all special multicast frames to the CPU
2448 * port at the highest priority.
2449 */
2450 REG_WRITE(REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
2451 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
2452 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
2453
2454 /* Program the DSA routing table. */
2455 for (i = 0; i < 32; i++) {
2456 int nexthop = 0x1f;
2457
2458 if (ds->pd->rtable &&
2459 i != ds->index && i < ds->dst->pd->nr_chips)
2460 nexthop = ds->pd->rtable[i] & 0x1f;
2461
2462 REG_WRITE(REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING,
2463 GLOBAL2_DEVICE_MAPPING_UPDATE |
2464 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) |
2465 nexthop);
2466 }
2467
2468 /* Clear all trunk masks. */
2469 for (i = 0; i < 8; i++)
2470 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
2471 0x8000 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
2472 ((1 << ps->num_ports) - 1));
2473
2474 /* Clear all trunk mappings. */
2475 for (i = 0; i < 16; i++)
2476 REG_WRITE(REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING,
2477 GLOBAL2_TRUNK_MAPPING_UPDATE |
2478 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
2479
2480 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002481 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
2482 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002483 /* Send all frames with destination addresses matching
2484 * 01:80:c2:00:00:2x to the CPU port.
2485 */
2486 REG_WRITE(REG_GLOBAL2, GLOBAL2_MGMT_EN_2X, 0xffff);
2487
2488 /* Initialise cross-chip port VLAN table to reset
2489 * defaults.
2490 */
2491 REG_WRITE(REG_GLOBAL2, GLOBAL2_PVT_ADDR, 0x9000);
2492
2493 /* Clear the priority override table. */
2494 for (i = 0; i < 16; i++)
2495 REG_WRITE(REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE,
2496 0x8000 | (i << 8));
2497 }
2498
2499 if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) ||
2500 mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) ||
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -07002501 mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) ||
2502 mv88e6xxx_6320_family(ds)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002503 /* Disable ingress rate limiting by resetting all
2504 * ingress rate limit registers to their initial
2505 * state.
2506 */
2507 for (i = 0; i < ps->num_ports; i++)
2508 REG_WRITE(REG_GLOBAL2, GLOBAL2_INGRESS_OP,
2509 0x9000 | (i << 8));
2510 }
2511
Andrew Lunndb687a52015-06-20 21:31:29 +02002512 /* Clear the statistics counters for all ports */
2513 REG_WRITE(REG_GLOBAL, GLOBAL_STATS_OP, GLOBAL_STATS_OP_FLUSH_ALL);
2514
2515 /* Wait for the flush to complete. */
Vivien Didelot24751e22015-08-03 09:17:44 -04002516 mutex_lock(&ps->smi_mutex);
2517 ret = _mv88e6xxx_stats_wait(ds);
Vivien Didelot6b17e862015-08-13 12:52:18 -04002518 if (ret < 0)
2519 goto unlock;
2520
Vivien Didelotc161d0a2015-09-04 14:34:13 -04002521 /* Clear all ATU entries */
2522 ret = _mv88e6xxx_atu_flush(ds, 0, true);
2523 if (ret < 0)
2524 goto unlock;
2525
Vivien Didelot6b17e862015-08-13 12:52:18 -04002526 /* Clear all the VTU and STU entries */
2527 ret = _mv88e6xxx_vtu_stu_flush(ds);
2528unlock:
Vivien Didelot24751e22015-08-03 09:17:44 -04002529 mutex_unlock(&ps->smi_mutex);
Andrew Lunndb687a52015-06-20 21:31:29 +02002530
Vivien Didelot24751e22015-08-03 09:17:44 -04002531 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002532}
2533
Andrew Lunn143a8302015-04-02 04:06:34 +02002534int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active)
2535{
2536 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2537 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2538 unsigned long timeout;
2539 int ret;
2540 int i;
2541
2542 /* Set all ports to the disabled state. */
2543 for (i = 0; i < ps->num_ports; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002544 ret = REG_READ(REG_PORT(i), PORT_CONTROL);
2545 REG_WRITE(REG_PORT(i), PORT_CONTROL, ret & 0xfffc);
Andrew Lunn143a8302015-04-02 04:06:34 +02002546 }
2547
2548 /* Wait for transmit queues to drain. */
2549 usleep_range(2000, 4000);
2550
2551 /* Reset the switch. Keep the PPU active if requested. The PPU
2552 * needs to be active to support indirect phy register access
2553 * through global registers 0x18 and 0x19.
2554 */
2555 if (ppu_active)
2556 REG_WRITE(REG_GLOBAL, 0x04, 0xc000);
2557 else
2558 REG_WRITE(REG_GLOBAL, 0x04, 0xc400);
2559
2560 /* Wait up to one second for reset to complete. */
2561 timeout = jiffies + 1 * HZ;
2562 while (time_before(jiffies, timeout)) {
2563 ret = REG_READ(REG_GLOBAL, 0x00);
2564 if ((ret & is_reset) == is_reset)
2565 break;
2566 usleep_range(1000, 2000);
2567 }
2568 if (time_after(jiffies, timeout))
2569 return -ETIMEDOUT;
2570
2571 return 0;
2572}
2573
Andrew Lunn491435852015-04-02 04:06:35 +02002574int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
2575{
2576 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2577 int ret;
2578
Andrew Lunn3898c142015-05-06 01:09:53 +02002579 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002580 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02002581 if (ret < 0)
2582 goto error;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002583 ret = _mv88e6xxx_phy_read_indirect(ds, port, reg);
Andrew Lunn491435852015-04-02 04:06:35 +02002584error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002585 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02002586 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002587 return ret;
2588}
2589
2590int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
2591 int reg, int val)
2592{
2593 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2594 int ret;
2595
Andrew Lunn3898c142015-05-06 01:09:53 +02002596 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002597 ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page);
Andrew Lunn491435852015-04-02 04:06:35 +02002598 if (ret < 0)
2599 goto error;
2600
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002601 ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val);
Andrew Lunn491435852015-04-02 04:06:35 +02002602error:
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002603 _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0);
Andrew Lunn3898c142015-05-06 01:09:53 +02002604 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002605 return ret;
2606}
2607
2608static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port)
2609{
2610 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2611
2612 if (port >= 0 && port < ps->num_ports)
2613 return port;
2614 return -EINVAL;
2615}
2616
2617int
2618mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
2619{
2620 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2621 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2622 int ret;
2623
2624 if (addr < 0)
2625 return addr;
2626
Andrew Lunn3898c142015-05-06 01:09:53 +02002627 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002628 ret = _mv88e6xxx_phy_read(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002629 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002630 return ret;
2631}
2632
2633int
2634mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2635{
2636 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2637 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2638 int ret;
2639
2640 if (addr < 0)
2641 return addr;
2642
Andrew Lunn3898c142015-05-06 01:09:53 +02002643 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002644 ret = _mv88e6xxx_phy_write(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002645 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002646 return ret;
2647}
2648
2649int
2650mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum)
2651{
2652 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2653 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2654 int ret;
2655
2656 if (addr < 0)
2657 return addr;
2658
Andrew Lunn3898c142015-05-06 01:09:53 +02002659 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002660 ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum);
Andrew Lunn3898c142015-05-06 01:09:53 +02002661 mutex_unlock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002662 return ret;
2663}
2664
2665int
2666mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum,
2667 u16 val)
2668{
2669 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2670 int addr = mv88e6xxx_port_to_phy_addr(ds, port);
2671 int ret;
2672
2673 if (addr < 0)
2674 return addr;
2675
Andrew Lunn3898c142015-05-06 01:09:53 +02002676 mutex_lock(&ps->smi_mutex);
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02002677 ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val);
Andrew Lunn3898c142015-05-06 01:09:53 +02002678 mutex_unlock(&ps->smi_mutex);
Andrew Lunn491435852015-04-02 04:06:35 +02002679 return ret;
2680}
2681
Guenter Roeckc22995c2015-07-25 09:42:28 -07002682#ifdef CONFIG_NET_DSA_HWMON
2683
2684static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
2685{
2686 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2687 int ret;
2688 int val;
2689
2690 *temp = 0;
2691
2692 mutex_lock(&ps->smi_mutex);
2693
2694 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
2695 if (ret < 0)
2696 goto error;
2697
2698 /* Enable temperature sensor */
2699 ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2700 if (ret < 0)
2701 goto error;
2702
2703 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
2704 if (ret < 0)
2705 goto error;
2706
2707 /* Wait for temperature to stabilize */
2708 usleep_range(10000, 12000);
2709
2710 val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a);
2711 if (val < 0) {
2712 ret = val;
2713 goto error;
2714 }
2715
2716 /* Disable temperature sensor */
2717 ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
2718 if (ret < 0)
2719 goto error;
2720
2721 *temp = ((val & 0x1f) - 5) * 5;
2722
2723error:
2724 _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
2725 mutex_unlock(&ps->smi_mutex);
2726 return ret;
2727}
2728
2729static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
2730{
2731 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2732 int ret;
2733
2734 *temp = 0;
2735
2736 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
2737 if (ret < 0)
2738 return ret;
2739
2740 *temp = (ret & 0xff) - 25;
2741
2742 return 0;
2743}
2744
2745int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
2746{
2747 if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds))
2748 return mv88e63xx_get_temp(ds, temp);
2749
2750 return mv88e61xx_get_temp(ds, temp);
2751}
2752
2753int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
2754{
2755 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2756 int ret;
2757
2758 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2759 return -EOPNOTSUPP;
2760
2761 *temp = 0;
2762
2763 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2764 if (ret < 0)
2765 return ret;
2766
2767 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
2768
2769 return 0;
2770}
2771
2772int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
2773{
2774 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2775 int ret;
2776
2777 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2778 return -EOPNOTSUPP;
2779
2780 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2781 if (ret < 0)
2782 return ret;
2783 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
2784 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
2785 (ret & 0xe0ff) | (temp << 8));
2786}
2787
2788int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
2789{
2790 int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
2791 int ret;
2792
2793 if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds))
2794 return -EOPNOTSUPP;
2795
2796 *alarm = false;
2797
2798 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
2799 if (ret < 0)
2800 return ret;
2801
2802 *alarm = !!(ret & 0x40);
2803
2804 return 0;
2805}
2806#endif /* CONFIG_NET_DSA_HWMON */
2807
Ben Hutchings98e67302011-11-25 14:36:19 +00002808static int __init mv88e6xxx_init(void)
2809{
2810#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2811 register_switch_driver(&mv88e6131_switch_driver);
2812#endif
2813#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2814 register_switch_driver(&mv88e6123_61_65_switch_driver);
2815#endif
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07002816#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2817 register_switch_driver(&mv88e6352_switch_driver);
2818#endif
Andrew Lunn42f27252014-09-12 23:58:44 +02002819#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2820 register_switch_driver(&mv88e6171_switch_driver);
2821#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00002822 return 0;
2823}
2824module_init(mv88e6xxx_init);
2825
2826static void __exit mv88e6xxx_cleanup(void)
2827{
Andrew Lunn42f27252014-09-12 23:58:44 +02002828#if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
2829 unregister_switch_driver(&mv88e6171_switch_driver);
2830#endif
Vivien Didelot4212b542015-05-01 10:43:52 -04002831#if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
2832 unregister_switch_driver(&mv88e6352_switch_driver);
2833#endif
Ben Hutchings98e67302011-11-25 14:36:19 +00002834#if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
2835 unregister_switch_driver(&mv88e6123_61_65_switch_driver);
2836#endif
2837#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
2838 unregister_switch_driver(&mv88e6131_switch_driver);
2839#endif
2840}
2841module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00002842
2843MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
2844MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
2845MODULE_LICENSE("GPL");