blob: d6b0f78e959810842d0ee489f471c7a9a679cfb3 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
Vivien Didelot0d3cd4b2016-06-21 12:28:19 -04002 * Marvell 88e6xxx Ethernet switch single-chip support
3 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 * Copyright (c) 2008 Marvell Semiconductor
5 *
Vivien Didelotb8fee952015-08-13 12:52:19 -04006 * Copyright (c) 2015 CMC Electronics, Inc.
7 * Added support for VLAN Table Unit operations
8 *
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02009 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
10 *
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
Barry Grussling19b2f972013-01-08 16:05:54 +000017#include <linux/delay.h>
Guenter Roeckdefb05b2015-03-26 18:36:38 -070018#include <linux/etherdevice.h>
Andrew Lunndea87022015-08-31 15:56:47 +020019#include <linux/ethtool.h>
Guenter Roeckfacd95b2015-03-26 18:36:35 -070020#include <linux/if_bridge.h>
Barry Grussling19b2f972013-01-08 16:05:54 +000021#include <linux/jiffies.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000022#include <linux/list.h>
Andrew Lunn14c7b3c2016-05-10 23:27:21 +020023#include <linux/mdio.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000024#include <linux/module.h>
Vivien Didelotcaac8542016-06-20 13:14:09 -040025#include <linux/of_device.h>
Andrew Lunnb516d452016-06-04 21:17:06 +020026#include <linux/of_mdio.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include <linux/netdevice.h>
Andrew Lunnc8c1b39a2015-11-20 03:56:24 +010028#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000029#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000030#include <net/dsa.h>
Vivien Didelot1f36faf2015-10-08 11:35:13 -040031#include <net/switchdev.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000032#include "mv88e6xxx.h"
33
Vivien Didelotfad09c72016-06-21 12:28:20 -040034static void assert_reg_lock(struct mv88e6xxx_chip *chip)
Vivien Didelot3996a4f2015-10-30 18:56:45 -040035{
Vivien Didelotfad09c72016-06-21 12:28:20 -040036 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
37 dev_err(chip->dev, "Switch registers lock not held!\n");
Vivien Didelot3996a4f2015-10-30 18:56:45 -040038 dump_stack();
39 }
40}
41
Vivien Didelot914b32f2016-06-20 13:14:11 -040042/* The switch ADDR[4:1] configuration pins define the chip SMI device address
43 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
44 *
45 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
46 * is the only device connected to the SMI master. In this mode it responds to
47 * all 32 possible SMI addresses, and thus maps directly the internal devices.
48 *
49 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
50 * multiple devices to share the SMI interface. In this mode it responds to only
51 * 2 registers, used to indirectly access the internal SMI devices.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000052 */
Vivien Didelot914b32f2016-06-20 13:14:11 -040053
Vivien Didelotfad09c72016-06-21 12:28:20 -040054static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040055 int addr, int reg, u16 *val)
56{
Vivien Didelotfad09c72016-06-21 12:28:20 -040057 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040058 return -EOPNOTSUPP;
59
Vivien Didelotfad09c72016-06-21 12:28:20 -040060 return chip->smi_ops->read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040061}
62
Vivien Didelotfad09c72016-06-21 12:28:20 -040063static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040064 int addr, int reg, u16 val)
65{
Vivien Didelotfad09c72016-06-21 12:28:20 -040066 if (!chip->smi_ops)
Vivien Didelot914b32f2016-06-20 13:14:11 -040067 return -EOPNOTSUPP;
68
Vivien Didelotfad09c72016-06-21 12:28:20 -040069 return chip->smi_ops->write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040070}
71
Vivien Didelotfad09c72016-06-21 12:28:20 -040072static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040073 int addr, int reg, u16 *val)
74{
75 int ret;
76
Vivien Didelotfad09c72016-06-21 12:28:20 -040077 ret = mdiobus_read_nested(chip->bus, addr, reg);
Vivien Didelot914b32f2016-06-20 13:14:11 -040078 if (ret < 0)
79 return ret;
80
81 *val = ret & 0xffff;
82
83 return 0;
84}
85
Vivien Didelotfad09c72016-06-21 12:28:20 -040086static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -040087 int addr, int reg, u16 val)
88{
89 int ret;
90
Vivien Didelotfad09c72016-06-21 12:28:20 -040091 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -040092 if (ret < 0)
93 return ret;
94
95 return 0;
96}
97
98static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
99 .read = mv88e6xxx_smi_single_chip_read,
100 .write = mv88e6xxx_smi_single_chip_write,
101};
102
Vivien Didelotfad09c72016-06-21 12:28:20 -0400103static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000104{
105 int ret;
106 int i;
107
108 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400109 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000110 if (ret < 0)
111 return ret;
112
Andrew Lunncca8b132015-04-02 04:06:39 +0200113 if ((ret & SMI_CMD_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000114 return 0;
115 }
116
117 return -ETIMEDOUT;
118}
119
Vivien Didelotfad09c72016-06-21 12:28:20 -0400120static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400121 int addr, int reg, u16 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000122{
123 int ret;
124
Barry Grussling3675c8d2013-01-08 16:05:53 +0000125 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400126 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000127 if (ret < 0)
128 return ret;
129
Barry Grussling3675c8d2013-01-08 16:05:53 +0000130 /* Transmit the read command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400131 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Neil Armstrong6e899e62015-10-22 10:37:53 +0200132 SMI_CMD_OP_22_READ | (addr << 5) | reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000133 if (ret < 0)
134 return ret;
135
Barry Grussling3675c8d2013-01-08 16:05:53 +0000136 /* Wait for the read command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400137 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000138 if (ret < 0)
139 return ret;
140
Barry Grussling3675c8d2013-01-08 16:05:53 +0000141 /* Read the data. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400142 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000143 if (ret < 0)
144 return ret;
145
Vivien Didelot914b32f2016-06-20 13:14:11 -0400146 *val = ret & 0xffff;
147
148 return 0;
149}
150
Vivien Didelotfad09c72016-06-21 12:28:20 -0400151static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400152 int addr, int reg, u16 val)
153{
154 int ret;
155
156 /* Wait for the bus to become free. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400157 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400158 if (ret < 0)
159 return ret;
160
161 /* Transmit the data to write. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400162 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400163 if (ret < 0)
164 return ret;
165
166 /* Transmit the write command. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400167 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400168 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
169 if (ret < 0)
170 return ret;
171
172 /* Wait for the write command to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400173 ret = mv88e6xxx_smi_multi_chip_wait(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400174 if (ret < 0)
175 return ret;
176
177 return 0;
178}
179
180static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
181 .read = mv88e6xxx_smi_multi_chip_read,
182 .write = mv88e6xxx_smi_multi_chip_write,
183};
184
Vivien Didelotfad09c72016-06-21 12:28:20 -0400185static int mv88e6xxx_read(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400186 int addr, int reg, u16 *val)
187{
188 int err;
189
Vivien Didelotfad09c72016-06-21 12:28:20 -0400190 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400191
Vivien Didelotfad09c72016-06-21 12:28:20 -0400192 err = mv88e6xxx_smi_read(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400193 if (err)
194 return err;
195
Vivien Didelotfad09c72016-06-21 12:28:20 -0400196 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400197 addr, reg, *val);
198
199 return 0;
200}
201
Vivien Didelotfad09c72016-06-21 12:28:20 -0400202static int mv88e6xxx_write(struct mv88e6xxx_chip *chip,
Vivien Didelot914b32f2016-06-20 13:14:11 -0400203 int addr, int reg, u16 val)
204{
205 int err;
206
Vivien Didelotfad09c72016-06-21 12:28:20 -0400207 assert_reg_lock(chip);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400208
Vivien Didelotfad09c72016-06-21 12:28:20 -0400209 err = mv88e6xxx_smi_write(chip, addr, reg, val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400210 if (err)
211 return err;
212
Vivien Didelotfad09c72016-06-21 12:28:20 -0400213 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
Vivien Didelot914b32f2016-06-20 13:14:11 -0400214 addr, reg, val);
215
216 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000217}
218
Vivien Didelote57e5e72016-08-15 17:19:00 -0400219static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
220 int reg, u16 *val)
221{
222 int addr = phy; /* PHY devices addresses start at 0x0 */
223
224 if (!chip->phy_ops)
225 return -EOPNOTSUPP;
226
227 return chip->phy_ops->read(chip, addr, reg, val);
228}
229
230static int mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy,
231 int reg, u16 val)
232{
233 int addr = phy; /* PHY devices addresses start at 0x0 */
234
235 if (!chip->phy_ops)
236 return -EOPNOTSUPP;
237
238 return chip->phy_ops->write(chip, addr, reg, val);
239}
240
Vivien Didelot09cb7df2016-08-15 17:19:01 -0400241static int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
242{
243 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PHY_PAGE))
244 return -EOPNOTSUPP;
245
246 return mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
247}
248
249static void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
250{
251 int err;
252
253 /* Restore PHY page Copper 0x0 for access via the registered MDIO bus */
254 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, PHY_PAGE_COPPER);
255 if (unlikely(err)) {
256 dev_err(chip->dev, "failed to restore PHY %d page Copper (%d)\n",
257 phy, err);
258 }
259}
260
261static int mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
262 u8 page, int reg, u16 *val)
263{
264 int err;
265
266 /* There is no paging for registers 22 */
267 if (reg == PHY_PAGE)
268 return -EINVAL;
269
270 err = mv88e6xxx_phy_page_get(chip, phy, page);
271 if (!err) {
272 err = mv88e6xxx_phy_read(chip, phy, reg, val);
273 mv88e6xxx_phy_page_put(chip, phy);
274 }
275
276 return err;
277}
278
279static int mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
280 u8 page, int reg, u16 val)
281{
282 int err;
283
284 /* There is no paging for registers 22 */
285 if (reg == PHY_PAGE)
286 return -EINVAL;
287
288 err = mv88e6xxx_phy_page_get(chip, phy, page);
289 if (!err) {
290 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
291 mv88e6xxx_phy_page_put(chip, phy);
292 }
293
294 return err;
295}
296
297static int mv88e6xxx_serdes_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
298{
299 return mv88e6xxx_phy_page_read(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
300 reg, val);
301}
302
303static int mv88e6xxx_serdes_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
304{
305 return mv88e6xxx_phy_page_write(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
306 reg, val);
307}
308
Vivien Didelot2d79af62016-08-15 17:18:57 -0400309static int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg,
310 u16 mask)
311{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200312 int i;
Vivien Didelot2d79af62016-08-15 17:18:57 -0400313
Andrew Lunn6441e6692016-08-19 00:01:55 +0200314 for (i = 0; i < 16; i++) {
Vivien Didelot2d79af62016-08-15 17:18:57 -0400315 u16 val;
316 int err;
317
318 err = mv88e6xxx_read(chip, addr, reg, &val);
319 if (err)
320 return err;
321
322 if (!(val & mask))
323 return 0;
324
325 usleep_range(1000, 2000);
326 }
327
Andrew Lunn30853552016-08-19 00:01:57 +0200328 dev_err(chip->dev, "Timeout while waiting for switch\n");
Vivien Didelot2d79af62016-08-15 17:18:57 -0400329 return -ETIMEDOUT;
330}
331
Vivien Didelotf22ab642016-07-18 20:45:31 -0400332/* Indirect write to single pointer-data register with an Update bit */
333static int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg,
334 u16 update)
335{
336 u16 val;
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200337 int err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400338
339 /* Wait until the previous operation is completed */
Andrew Lunn0f02b4f2016-08-19 00:01:56 +0200340 err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
341 if (err)
342 return err;
Vivien Didelotf22ab642016-07-18 20:45:31 -0400343
344 /* Set the Update bit to trigger a write operation */
345 val = BIT(15) | update;
346
347 return mv88e6xxx_write(chip, addr, reg, val);
348}
349
Vivien Didelotfad09c72016-06-21 12:28:20 -0400350static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000351{
Vivien Didelot914b32f2016-06-20 13:14:11 -0400352 u16 val;
353 int err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000354
Vivien Didelotfad09c72016-06-21 12:28:20 -0400355 err = mv88e6xxx_read(chip, addr, reg, &val);
Vivien Didelot914b32f2016-06-20 13:14:11 -0400356 if (err)
357 return err;
Vivien Didelot3996a4f2015-10-30 18:56:45 -0400358
Vivien Didelot914b32f2016-06-20 13:14:11 -0400359 return val;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000360}
361
Vivien Didelotfad09c72016-06-21 12:28:20 -0400362static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
Andrew Lunn158bc062016-04-28 21:24:06 -0400363 int reg, u16 val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000364{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400365 return mv88e6xxx_write(chip, addr, reg, val);
Guenter Roeck8d6d09e2015-03-26 18:36:31 -0700366}
367
Vivien Didelotfad09c72016-06-21 12:28:20 -0400368static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000369{
370 int ret;
Andrew Lunn6441e6692016-08-19 00:01:55 +0200371 int i;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000372
Vivien Didelotfad09c72016-06-21 12:28:20 -0400373 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200374 if (ret < 0)
375 return ret;
376
Vivien Didelotfad09c72016-06-21 12:28:20 -0400377 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot8c9983a2016-05-09 13:22:39 -0400378 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200379 if (ret)
380 return ret;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000381
Andrew Lunn6441e6692016-08-19 00:01:55 +0200382 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400383 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200384 if (ret < 0)
385 return ret;
386
Barry Grussling19b2f972013-01-08 16:05:54 +0000387 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200388 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
389 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000390 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000391 }
392
393 return -ETIMEDOUT;
394}
395
Vivien Didelotfad09c72016-06-21 12:28:20 -0400396static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000397{
Andrew Lunn6441e6692016-08-19 00:01:55 +0200398 int ret, err, i;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000399
Vivien Didelotfad09c72016-06-21 12:28:20 -0400400 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200401 if (ret < 0)
402 return ret;
403
Vivien Didelotfad09c72016-06-21 12:28:20 -0400404 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
Vivien Didelot762eb672016-06-04 21:16:54 +0200405 ret | GLOBAL_CONTROL_PPU_ENABLE);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200406 if (err)
407 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000408
Andrew Lunn6441e6692016-08-19 00:01:55 +0200409 for (i = 0; i < 16; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400410 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
Andrew Lunn48ace4e2016-04-14 23:47:12 +0200411 if (ret < 0)
412 return ret;
413
Barry Grussling19b2f972013-01-08 16:05:54 +0000414 usleep_range(1000, 2000);
Andrew Lunncca8b132015-04-02 04:06:39 +0200415 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
416 GLOBAL_STATUS_PPU_POLLING)
Barry Grussling85686582013-01-08 16:05:56 +0000417 return 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000418 }
419
420 return -ETIMEDOUT;
421}
422
423static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
424{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400425 struct mv88e6xxx_chip *chip;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000426
Vivien Didelotfad09c72016-06-21 12:28:20 -0400427 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
Vivien Didelot762eb672016-06-04 21:16:54 +0200428
Vivien Didelotfad09c72016-06-21 12:28:20 -0400429 mutex_lock(&chip->reg_lock);
Vivien Didelot762eb672016-06-04 21:16:54 +0200430
Vivien Didelotfad09c72016-06-21 12:28:20 -0400431 if (mutex_trylock(&chip->ppu_mutex)) {
432 if (mv88e6xxx_ppu_enable(chip) == 0)
433 chip->ppu_disabled = 0;
434 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000435 }
Vivien Didelot762eb672016-06-04 21:16:54 +0200436
Vivien Didelotfad09c72016-06-21 12:28:20 -0400437 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000438}
439
440static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
441{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400442 struct mv88e6xxx_chip *chip = (void *)_ps;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000443
Vivien Didelotfad09c72016-06-21 12:28:20 -0400444 schedule_work(&chip->ppu_work);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000445}
446
Vivien Didelotfad09c72016-06-21 12:28:20 -0400447static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000448{
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000449 int ret;
450
Vivien Didelotfad09c72016-06-21 12:28:20 -0400451 mutex_lock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000452
Barry Grussling3675c8d2013-01-08 16:05:53 +0000453 /* If the PHY polling unit is enabled, disable it so that
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000454 * we can access the PHY registers. If it was already
455 * disabled, cancel the timer that is going to re-enable
456 * it.
457 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400458 if (!chip->ppu_disabled) {
459 ret = mv88e6xxx_ppu_disable(chip);
Barry Grussling85686582013-01-08 16:05:56 +0000460 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400461 mutex_unlock(&chip->ppu_mutex);
Barry Grussling85686582013-01-08 16:05:56 +0000462 return ret;
463 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400464 chip->ppu_disabled = 1;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000465 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400466 del_timer(&chip->ppu_timer);
Barry Grussling85686582013-01-08 16:05:56 +0000467 ret = 0;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000468 }
469
470 return ret;
471}
472
Vivien Didelotfad09c72016-06-21 12:28:20 -0400473static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000474{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000475 /* Schedule a timer to re-enable the PHY polling unit. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400476 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
477 mutex_unlock(&chip->ppu_mutex);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000478}
479
Vivien Didelotfad09c72016-06-21 12:28:20 -0400480static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000481{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400482 mutex_init(&chip->ppu_mutex);
483 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
484 init_timer(&chip->ppu_timer);
485 chip->ppu_timer.data = (unsigned long)chip;
486 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000487}
488
Andrew Lunn930188c2016-08-22 16:01:03 +0200489static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
490{
491 del_timer_sync(&chip->ppu_timer);
492}
493
Vivien Didelote57e5e72016-08-15 17:19:00 -0400494static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
495 int reg, u16 *val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000496{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400497 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000498
Vivien Didelote57e5e72016-08-15 17:19:00 -0400499 err = mv88e6xxx_ppu_access_get(chip);
500 if (!err) {
501 err = mv88e6xxx_read(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400502 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000503 }
504
Vivien Didelote57e5e72016-08-15 17:19:00 -0400505 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000506}
507
Vivien Didelote57e5e72016-08-15 17:19:00 -0400508static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
509 int reg, u16 val)
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000510{
Vivien Didelote57e5e72016-08-15 17:19:00 -0400511 int err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000512
Vivien Didelote57e5e72016-08-15 17:19:00 -0400513 err = mv88e6xxx_ppu_access_get(chip);
514 if (!err) {
515 err = mv88e6xxx_write(chip, addr, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -0400516 mv88e6xxx_ppu_access_put(chip);
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000517 }
518
Vivien Didelote57e5e72016-08-15 17:19:00 -0400519 return err;
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000520}
Lennert Buytenhek2e5f0322008-10-07 13:45:18 +0000521
Vivien Didelote57e5e72016-08-15 17:19:00 -0400522static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
523 .read = mv88e6xxx_phy_ppu_read,
524 .write = mv88e6xxx_phy_ppu_write,
525};
526
Vivien Didelotfad09c72016-06-21 12:28:20 -0400527static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200528{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400529 return chip->info->family == MV88E6XXX_FAMILY_6065;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200530}
531
Vivien Didelotfad09c72016-06-21 12:28:20 -0400532static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200533{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400534 return chip->info->family == MV88E6XXX_FAMILY_6095;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200535}
536
Vivien Didelotfad09c72016-06-21 12:28:20 -0400537static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200538{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400539 return chip->info->family == MV88E6XXX_FAMILY_6097;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200540}
541
Vivien Didelotfad09c72016-06-21 12:28:20 -0400542static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200543{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400544 return chip->info->family == MV88E6XXX_FAMILY_6165;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200545}
546
Vivien Didelotfad09c72016-06-21 12:28:20 -0400547static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200548{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400549 return chip->info->family == MV88E6XXX_FAMILY_6185;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200550}
551
Vivien Didelotfad09c72016-06-21 12:28:20 -0400552static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700553{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400554 return chip->info->family == MV88E6XXX_FAMILY_6320;
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700555}
556
Vivien Didelotfad09c72016-06-21 12:28:20 -0400557static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
Andrew Lunn54d792f2015-05-06 01:09:47 +0200558{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400559 return chip->info->family == MV88E6XXX_FAMILY_6351;
Andrew Lunn54d792f2015-05-06 01:09:47 +0200560}
561
Vivien Didelotfad09c72016-06-21 12:28:20 -0400562static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200563{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400564 return chip->info->family == MV88E6XXX_FAMILY_6352;
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200565}
566
Vivien Didelotfad09c72016-06-21 12:28:20 -0400567static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400568{
Vivien Didelotfad09c72016-06-21 12:28:20 -0400569 return chip->info->num_databases;
Vivien Didelotf74df0b2016-03-31 16:53:43 -0400570}
571
Vivien Didelotfad09c72016-06-21 12:28:20 -0400572static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400573{
574 /* Does the device have dedicated FID registers for ATU and VTU ops? */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400575 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
576 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400577 return true;
578
579 return false;
580}
581
Andrew Lunndea87022015-08-31 15:56:47 +0200582/* We expect the switch to perform auto negotiation if there is a real
583 * phy. However, in the case of a fixed link phy, we force the port
584 * settings from the fixed link settings.
585 */
Vivien Didelotf81ec902016-05-09 13:22:58 -0400586static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
587 struct phy_device *phydev)
Andrew Lunndea87022015-08-31 15:56:47 +0200588{
Vivien Didelot04bed142016-08-31 18:06:13 -0400589 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn49052872015-09-29 01:53:48 +0200590 u32 reg;
591 int ret;
Andrew Lunndea87022015-08-31 15:56:47 +0200592
593 if (!phy_is_pseudo_fixed_link(phydev))
594 return;
595
Vivien Didelotfad09c72016-06-21 12:28:20 -0400596 mutex_lock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200597
Vivien Didelotfad09c72016-06-21 12:28:20 -0400598 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunndea87022015-08-31 15:56:47 +0200599 if (ret < 0)
600 goto out;
601
602 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
603 PORT_PCS_CTRL_FORCE_LINK |
604 PORT_PCS_CTRL_DUPLEX_FULL |
605 PORT_PCS_CTRL_FORCE_DUPLEX |
606 PORT_PCS_CTRL_UNFORCED);
607
608 reg |= PORT_PCS_CTRL_FORCE_LINK;
609 if (phydev->link)
Vivien Didelot57d32312016-06-20 13:13:58 -0400610 reg |= PORT_PCS_CTRL_LINK_UP;
Andrew Lunndea87022015-08-31 15:56:47 +0200611
Vivien Didelotfad09c72016-06-21 12:28:20 -0400612 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
Andrew Lunndea87022015-08-31 15:56:47 +0200613 goto out;
614
615 switch (phydev->speed) {
616 case SPEED_1000:
617 reg |= PORT_PCS_CTRL_1000;
618 break;
619 case SPEED_100:
620 reg |= PORT_PCS_CTRL_100;
621 break;
622 case SPEED_10:
623 reg |= PORT_PCS_CTRL_10;
624 break;
625 default:
626 pr_info("Unknown speed");
627 goto out;
628 }
629
630 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
631 if (phydev->duplex == DUPLEX_FULL)
632 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
633
Vivien Didelotfad09c72016-06-21 12:28:20 -0400634 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
635 (port >= chip->info->num_ports - 2)) {
Andrew Lunne7e72ac2015-08-31 15:56:51 +0200636 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
637 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
638 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
639 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
640 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
641 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
642 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
643 }
Vivien Didelotfad09c72016-06-21 12:28:20 -0400644 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
Andrew Lunndea87022015-08-31 15:56:47 +0200645
646out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400647 mutex_unlock(&chip->reg_lock);
Andrew Lunndea87022015-08-31 15:56:47 +0200648}
649
Vivien Didelotfad09c72016-06-21 12:28:20 -0400650static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000651{
652 int ret;
653 int i;
654
655 for (i = 0; i < 10; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400656 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
Andrew Lunncca8b132015-04-02 04:06:39 +0200657 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000658 return 0;
659 }
660
661 return -ETIMEDOUT;
662}
663
Vivien Didelotfad09c72016-06-21 12:28:20 -0400664static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000665{
666 int ret;
667
Vivien Didelotfad09c72016-06-21 12:28:20 -0400668 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Andrew Lunnf3a8b6b2015-04-02 04:06:40 +0200669 port = (port + 1) << 5;
670
Barry Grussling3675c8d2013-01-08 16:05:53 +0000671 /* Snapshot the hardware statistics counters for this port. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400672 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200673 GLOBAL_STATS_OP_CAPTURE_PORT |
674 GLOBAL_STATS_OP_HIST_RX_TX | port);
675 if (ret < 0)
676 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000677
Barry Grussling3675c8d2013-01-08 16:05:53 +0000678 /* Wait for the snapshotting to complete. */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400679 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000680 if (ret < 0)
681 return ret;
682
683 return 0;
684}
685
Vivien Didelotfad09c72016-06-21 12:28:20 -0400686static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -0400687 int stat, u32 *val)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000688{
689 u32 _val;
690 int ret;
691
692 *val = 0;
693
Vivien Didelotfad09c72016-06-21 12:28:20 -0400694 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
Andrew Lunn31888232015-05-06 01:09:54 +0200695 GLOBAL_STATS_OP_READ_CAPTURED |
696 GLOBAL_STATS_OP_HIST_RX_TX | stat);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000697 if (ret < 0)
698 return;
699
Vivien Didelotfad09c72016-06-21 12:28:20 -0400700 ret = _mv88e6xxx_stats_wait(chip);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000701 if (ret < 0)
702 return;
703
Vivien Didelotfad09c72016-06-21 12:28:20 -0400704 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000705 if (ret < 0)
706 return;
707
708 _val = ret << 16;
709
Vivien Didelotfad09c72016-06-21 12:28:20 -0400710 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000711 if (ret < 0)
712 return;
713
714 *val = _val | ret;
715}
716
Andrew Lunne413e7e2015-04-02 04:06:38 +0200717static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100718 { "in_good_octets", 8, 0x00, BANK0, },
719 { "in_bad_octets", 4, 0x02, BANK0, },
720 { "in_unicast", 4, 0x04, BANK0, },
721 { "in_broadcasts", 4, 0x06, BANK0, },
722 { "in_multicasts", 4, 0x07, BANK0, },
723 { "in_pause", 4, 0x16, BANK0, },
724 { "in_undersize", 4, 0x18, BANK0, },
725 { "in_fragments", 4, 0x19, BANK0, },
726 { "in_oversize", 4, 0x1a, BANK0, },
727 { "in_jabber", 4, 0x1b, BANK0, },
728 { "in_rx_error", 4, 0x1c, BANK0, },
729 { "in_fcs_error", 4, 0x1d, BANK0, },
730 { "out_octets", 8, 0x0e, BANK0, },
731 { "out_unicast", 4, 0x10, BANK0, },
732 { "out_broadcasts", 4, 0x13, BANK0, },
733 { "out_multicasts", 4, 0x12, BANK0, },
734 { "out_pause", 4, 0x15, BANK0, },
735 { "excessive", 4, 0x11, BANK0, },
736 { "collisions", 4, 0x1e, BANK0, },
737 { "deferred", 4, 0x05, BANK0, },
738 { "single", 4, 0x14, BANK0, },
739 { "multiple", 4, 0x17, BANK0, },
740 { "out_fcs_error", 4, 0x03, BANK0, },
741 { "late", 4, 0x1f, BANK0, },
742 { "hist_64bytes", 4, 0x08, BANK0, },
743 { "hist_65_127bytes", 4, 0x09, BANK0, },
744 { "hist_128_255bytes", 4, 0x0a, BANK0, },
745 { "hist_256_511bytes", 4, 0x0b, BANK0, },
746 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
747 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
748 { "sw_in_discards", 4, 0x10, PORT, },
749 { "sw_in_filtered", 2, 0x12, PORT, },
750 { "sw_out_filtered", 2, 0x13, PORT, },
751 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
752 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
753 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
755 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
756 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
766 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
767 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
769 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
770 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
771 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
772 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
773 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
774 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
775 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
776 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
Andrew Lunne413e7e2015-04-02 04:06:38 +0200777};
778
Vivien Didelotfad09c72016-06-21 12:28:20 -0400779static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100780 struct mv88e6xxx_hw_stat *stat)
Andrew Lunne413e7e2015-04-02 04:06:38 +0200781{
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100782 switch (stat->type) {
783 case BANK0:
Andrew Lunne413e7e2015-04-02 04:06:38 +0200784 return true;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100785 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400786 return mv88e6xxx_6320_family(chip);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100787 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400788 return mv88e6xxx_6095_family(chip) ||
789 mv88e6xxx_6185_family(chip) ||
790 mv88e6xxx_6097_family(chip) ||
791 mv88e6xxx_6165_family(chip) ||
792 mv88e6xxx_6351_family(chip) ||
793 mv88e6xxx_6352_family(chip);
Andrew Lunne413e7e2015-04-02 04:06:38 +0200794 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100795 return false;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000796}
797
Vivien Didelotfad09c72016-06-21 12:28:20 -0400798static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100799 struct mv88e6xxx_hw_stat *s,
Andrew Lunn80c46272015-06-20 18:42:30 +0200800 int port)
801{
Andrew Lunn80c46272015-06-20 18:42:30 +0200802 u32 low;
803 u32 high = 0;
804 int ret;
805 u64 value;
806
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100807 switch (s->type) {
808 case PORT:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400809 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
Andrew Lunn80c46272015-06-20 18:42:30 +0200810 if (ret < 0)
811 return UINT64_MAX;
812
813 low = ret;
814 if (s->sizeof_stat == 4) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400815 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100816 s->reg + 1);
Andrew Lunn80c46272015-06-20 18:42:30 +0200817 if (ret < 0)
818 return UINT64_MAX;
819 high = ret;
820 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100821 break;
822 case BANK0:
823 case BANK1:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400824 _mv88e6xxx_stats_read(chip, s->reg, &low);
Andrew Lunn80c46272015-06-20 18:42:30 +0200825 if (s->sizeof_stat == 8)
Vivien Didelotfad09c72016-06-21 12:28:20 -0400826 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
Andrew Lunn80c46272015-06-20 18:42:30 +0200827 }
828 value = (((u64)high) << 16) | low;
829 return value;
830}
831
Vivien Didelotf81ec902016-05-09 13:22:58 -0400832static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
833 uint8_t *data)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100834{
Vivien Didelot04bed142016-08-31 18:06:13 -0400835 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100836 struct mv88e6xxx_hw_stat *stat;
837 int i, j;
838
839 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
840 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400841 if (mv88e6xxx_has_stat(chip, stat)) {
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100842 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
843 ETH_GSTRING_LEN);
844 j++;
845 }
846 }
847}
848
Vivien Didelotf81ec902016-05-09 13:22:58 -0400849static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100850{
Vivien Didelot04bed142016-08-31 18:06:13 -0400851 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100852 struct mv88e6xxx_hw_stat *stat;
853 int i, j;
854
855 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
856 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400857 if (mv88e6xxx_has_stat(chip, stat))
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100858 j++;
859 }
860 return j;
861}
862
Vivien Didelotf81ec902016-05-09 13:22:58 -0400863static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
864 uint64_t *data)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000865{
Vivien Didelot04bed142016-08-31 18:06:13 -0400866 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100867 struct mv88e6xxx_hw_stat *stat;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000868 int ret;
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100869 int i, j;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000870
Vivien Didelotfad09c72016-06-21 12:28:20 -0400871 mutex_lock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000872
Vivien Didelotfad09c72016-06-21 12:28:20 -0400873 ret = _mv88e6xxx_stats_snapshot(chip, port);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000874 if (ret < 0) {
Vivien Didelotfad09c72016-06-21 12:28:20 -0400875 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000876 return;
877 }
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100878 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
879 stat = &mv88e6xxx_hw_stats[i];
Vivien Didelotfad09c72016-06-21 12:28:20 -0400880 if (mv88e6xxx_has_stat(chip, stat)) {
881 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
Andrew Lunnf5e2ed02015-12-23 13:23:17 +0100882 j++;
883 }
884 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000885
Vivien Didelotfad09c72016-06-21 12:28:20 -0400886 mutex_unlock(&chip->reg_lock);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000887}
Ben Hutchings98e67302011-11-25 14:36:19 +0000888
Vivien Didelotf81ec902016-05-09 13:22:58 -0400889static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700890{
891 return 32 * sizeof(u16);
892}
893
Vivien Didelotf81ec902016-05-09 13:22:58 -0400894static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
895 struct ethtool_regs *regs, void *_p)
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700896{
Vivien Didelot04bed142016-08-31 18:06:13 -0400897 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700898 u16 *p = _p;
899 int i;
900
901 regs->version = 0;
902
903 memset(p, 0xff, 32 * sizeof(u16));
904
Vivien Didelotfad09c72016-06-21 12:28:20 -0400905 mutex_lock(&chip->reg_lock);
Vivien Didelot23062512016-05-09 13:22:45 -0400906
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700907 for (i = 0; i < 32; i++) {
908 int ret;
909
Vivien Didelotfad09c72016-06-21 12:28:20 -0400910 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700911 if (ret >= 0)
912 p[i] = ret;
913 }
Vivien Didelot23062512016-05-09 13:22:45 -0400914
Vivien Didelotfad09c72016-06-21 12:28:20 -0400915 mutex_unlock(&chip->reg_lock);
Guenter Roecka1ab91f2014-10-29 10:45:05 -0700916}
917
Vivien Didelotfad09c72016-06-21 12:28:20 -0400918static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700919{
Vivien Didelot2d79af62016-08-15 17:18:57 -0400920 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
921 GLOBAL_ATU_OP_BUSY);
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700922}
923
Vivien Didelotf81ec902016-05-09 13:22:58 -0400924static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
925 struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800926{
Vivien Didelot04bed142016-08-31 18:06:13 -0400927 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400928 u16 reg;
929 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800930
Vivien Didelotfad09c72016-06-21 12:28:20 -0400931 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400932 return -EOPNOTSUPP;
933
Vivien Didelotfad09c72016-06-21 12:28:20 -0400934 mutex_lock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200935
Vivien Didelot9c938292016-08-15 17:19:02 -0400936 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
937 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200938 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800939
940 e->eee_enabled = !!(reg & 0x0200);
941 e->tx_lpi_enabled = !!(reg & 0x0100);
942
Vivien Didelot9c938292016-08-15 17:19:02 -0400943 err = mv88e6xxx_read(chip, REG_PORT(port), PORT_STATUS, &reg);
944 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200945 goto out;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800946
Andrew Lunncca8b132015-04-02 04:06:39 +0200947 e->eee_active = !!(reg & PORT_STATUS_EEE);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200948out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400949 mutex_unlock(&chip->reg_lock);
Vivien Didelot9c938292016-08-15 17:19:02 -0400950
951 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800952}
953
Vivien Didelotf81ec902016-05-09 13:22:58 -0400954static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
955 struct phy_device *phydev, struct ethtool_eee *e)
Guenter Roeck11b3b452015-03-06 22:23:51 -0800956{
Vivien Didelot04bed142016-08-31 18:06:13 -0400957 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -0400958 u16 reg;
959 int err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800960
Vivien Didelotfad09c72016-06-21 12:28:20 -0400961 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
Vivien Didelotaadbdb82016-05-09 13:22:44 -0400962 return -EOPNOTSUPP;
963
Vivien Didelotfad09c72016-06-21 12:28:20 -0400964 mutex_lock(&chip->reg_lock);
Guenter Roeck11b3b452015-03-06 22:23:51 -0800965
Vivien Didelot9c938292016-08-15 17:19:02 -0400966 err = mv88e6xxx_phy_read(chip, port, 16, &reg);
967 if (err)
Andrew Lunn2f40c692015-04-02 04:06:37 +0200968 goto out;
969
Vivien Didelot9c938292016-08-15 17:19:02 -0400970 reg &= ~0x0300;
Andrew Lunn2f40c692015-04-02 04:06:37 +0200971 if (e->eee_enabled)
972 reg |= 0x0200;
973 if (e->tx_lpi_enabled)
974 reg |= 0x0100;
975
Vivien Didelot9c938292016-08-15 17:19:02 -0400976 err = mv88e6xxx_phy_write(chip, port, 16, reg);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200977out:
Vivien Didelotfad09c72016-06-21 12:28:20 -0400978 mutex_unlock(&chip->reg_lock);
Andrew Lunn2f40c692015-04-02 04:06:37 +0200979
Vivien Didelot9c938292016-08-15 17:19:02 -0400980 return err;
Guenter Roeck11b3b452015-03-06 22:23:51 -0800981}
982
Vivien Didelotfad09c72016-06-21 12:28:20 -0400983static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
Guenter Roeckfacd95b2015-03-26 18:36:35 -0700984{
985 int ret;
986
Vivien Didelotfad09c72016-06-21 12:28:20 -0400987 if (mv88e6xxx_has_fid_reg(chip)) {
988 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
989 fid);
Vivien Didelotb426e5f2016-03-31 16:53:42 -0400990 if (ret < 0)
991 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -0400992 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -0400993 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -0400994 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
Vivien Didelot11ea8092016-03-31 16:53:44 -0400995 if (ret < 0)
996 return ret;
997
Vivien Didelotfad09c72016-06-21 12:28:20 -0400998 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
Vivien Didelot11ea8092016-03-31 16:53:44 -0400999 (ret & 0xfff) |
1000 ((fid << 8) & 0xf000));
1001 if (ret < 0)
1002 return ret;
1003
1004 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1005 cmd |= fid & 0xf;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001006 }
1007
Vivien Didelotfad09c72016-06-21 12:28:20 -04001008 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001009 if (ret < 0)
1010 return ret;
1011
Vivien Didelotfad09c72016-06-21 12:28:20 -04001012 return _mv88e6xxx_atu_wait(chip);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001013}
1014
Vivien Didelotfad09c72016-06-21 12:28:20 -04001015static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot37705b72015-09-04 14:34:11 -04001016 struct mv88e6xxx_atu_entry *entry)
1017{
1018 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1019
1020 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1021 unsigned int mask, shift;
1022
1023 if (entry->trunk) {
1024 data |= GLOBAL_ATU_DATA_TRUNK;
1025 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1026 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1027 } else {
1028 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1029 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1030 }
1031
1032 data |= (entry->portv_trunkid << shift) & mask;
1033 }
1034
Vivien Didelotfad09c72016-06-21 12:28:20 -04001035 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
Vivien Didelot37705b72015-09-04 14:34:11 -04001036}
1037
Vivien Didelotfad09c72016-06-21 12:28:20 -04001038static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001039 struct mv88e6xxx_atu_entry *entry,
1040 bool static_too)
1041{
1042 int op;
1043 int err;
1044
Vivien Didelotfad09c72016-06-21 12:28:20 -04001045 err = _mv88e6xxx_atu_wait(chip);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001046 if (err)
1047 return err;
1048
Vivien Didelotfad09c72016-06-21 12:28:20 -04001049 err = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001050 if (err)
1051 return err;
1052
1053 if (entry->fid) {
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001054 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1055 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1056 } else {
1057 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1058 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1059 }
1060
Vivien Didelotfad09c72016-06-21 12:28:20 -04001061 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001062}
1063
Vivien Didelotfad09c72016-06-21 12:28:20 -04001064static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001065 u16 fid, bool static_too)
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001066{
1067 struct mv88e6xxx_atu_entry entry = {
1068 .fid = fid,
1069 .state = 0, /* EntryState bits must be 0 */
1070 };
1071
Vivien Didelotfad09c72016-06-21 12:28:20 -04001072 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot7fb5e752015-09-04 14:34:12 -04001073}
1074
Vivien Didelotfad09c72016-06-21 12:28:20 -04001075static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001076 int from_port, int to_port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001077{
1078 struct mv88e6xxx_atu_entry entry = {
1079 .trunk = false,
1080 .fid = fid,
1081 };
1082
1083 /* EntryState bits must be 0xF */
1084 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1085
1086 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1087 entry.portv_trunkid = (to_port & 0x0f) << 4;
1088 entry.portv_trunkid |= from_port & 0x0f;
1089
Vivien Didelotfad09c72016-06-21 12:28:20 -04001090 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001091}
1092
Vivien Didelotfad09c72016-06-21 12:28:20 -04001093static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
Andrew Lunn158bc062016-04-28 21:24:06 -04001094 int port, bool static_too)
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001095{
1096 /* Destination port 0xF means remove the entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001097 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
Vivien Didelot9f4d55d2015-09-04 14:34:15 -04001098}
1099
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001100static const char * const mv88e6xxx_port_state_names[] = {
1101 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1102 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1103 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1104 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1105};
1106
Vivien Didelotfad09c72016-06-21 12:28:20 -04001107static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001108 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001109{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001110 struct dsa_switch *ds = chip->ds;
Geert Uytterhoevenc3ffe6d2015-04-16 20:49:14 +02001111 int reg, ret = 0;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001112 u8 oldstate;
1113
Vivien Didelotfad09c72016-06-21 12:28:20 -04001114 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001115 if (reg < 0)
1116 return reg;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001117
Andrew Lunncca8b132015-04-02 04:06:39 +02001118 oldstate = reg & PORT_CONTROL_STATE_MASK;
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001119
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001120 if (oldstate != state) {
1121 /* Flush forwarding database if we're moving a port
1122 * from Learning or Forwarding state to Disabled or
1123 * Blocking or Listening state.
1124 */
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001125 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
Vivien Didelot57d32312016-06-20 13:13:58 -04001126 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1127 (state == PORT_CONTROL_STATE_DISABLED ||
1128 state == PORT_CONTROL_STATE_BLOCKING)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001129 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001130 if (ret)
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001131 return ret;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001132 }
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001133
Andrew Lunncca8b132015-04-02 04:06:39 +02001134 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001135 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
Andrew Lunncca8b132015-04-02 04:06:39 +02001136 reg);
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001137 if (ret)
1138 return ret;
1139
Andrew Lunnc8b09802016-06-04 21:16:57 +02001140 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
Vivien Didelot2d9deae2016-03-07 18:24:17 -05001141 mv88e6xxx_port_state_names[state],
1142 mv88e6xxx_port_state_names[oldstate]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001143 }
1144
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001145 return ret;
1146}
1147
Vivien Didelotfad09c72016-06-21 12:28:20 -04001148static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001149{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001150 struct net_device *bridge = chip->ports[port].bridge_dev;
1151 const u16 mask = (1 << chip->info->num_ports) - 1;
1152 struct dsa_switch *ds = chip->ds;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001153 u16 output_ports = 0;
Vivien Didelotede80982015-10-11 18:08:35 -04001154 int reg;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001155 int i;
1156
1157 /* allow CPU port or DSA link(s) to send frames to every port */
1158 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1159 output_ports = mask;
1160 } else {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001161 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001162 /* allow sending frames to every group member */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001163 if (bridge && chip->ports[i].bridge_dev == bridge)
Vivien Didelotb7666ef2016-02-26 13:16:06 -05001164 output_ports |= BIT(i);
1165
1166 /* allow sending frames to CPU port and DSA link(s) */
1167 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1168 output_ports |= BIT(i);
1169 }
1170 }
1171
1172 /* prevent frames from going back out of the port they came in on */
1173 output_ports &= ~BIT(port);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001174
Vivien Didelotfad09c72016-06-21 12:28:20 -04001175 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelotede80982015-10-11 18:08:35 -04001176 if (reg < 0)
1177 return reg;
1178
1179 reg &= ~mask;
1180 reg |= output_ports & mask;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001181
Vivien Didelotfad09c72016-06-21 12:28:20 -04001182 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001183}
1184
Vivien Didelotf81ec902016-05-09 13:22:58 -04001185static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1186 u8 state)
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001187{
Vivien Didelot04bed142016-08-31 18:06:13 -04001188 struct mv88e6xxx_chip *chip = ds->priv;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001189 int stp_state;
Vivien Didelot553eb542016-05-13 20:38:23 -04001190 int err;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001191
1192 switch (state) {
1193 case BR_STATE_DISABLED:
Andrew Lunncca8b132015-04-02 04:06:39 +02001194 stp_state = PORT_CONTROL_STATE_DISABLED;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001195 break;
1196 case BR_STATE_BLOCKING:
1197 case BR_STATE_LISTENING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001198 stp_state = PORT_CONTROL_STATE_BLOCKING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001199 break;
1200 case BR_STATE_LEARNING:
Andrew Lunncca8b132015-04-02 04:06:39 +02001201 stp_state = PORT_CONTROL_STATE_LEARNING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001202 break;
1203 case BR_STATE_FORWARDING:
1204 default:
Andrew Lunncca8b132015-04-02 04:06:39 +02001205 stp_state = PORT_CONTROL_STATE_FORWARDING;
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001206 break;
1207 }
1208
Vivien Didelotfad09c72016-06-21 12:28:20 -04001209 mutex_lock(&chip->reg_lock);
1210 err = _mv88e6xxx_port_state(chip, port, stp_state);
1211 mutex_unlock(&chip->reg_lock);
Vivien Didelot553eb542016-05-13 20:38:23 -04001212
1213 if (err)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001214 netdev_err(ds->ports[port].netdev,
1215 "failed to update state to %s\n",
Vivien Didelot553eb542016-05-13 20:38:23 -04001216 mv88e6xxx_port_state_names[stp_state]);
Guenter Roeckfacd95b2015-03-26 18:36:35 -07001217}
1218
Vivien Didelotfad09c72016-06-21 12:28:20 -04001219static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001220 u16 *new, u16 *old)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001221{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001222 struct dsa_switch *ds = chip->ds;
Vivien Didelot5da96032016-03-07 18:24:39 -05001223 u16 pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001224 int ret;
1225
Vivien Didelotfad09c72016-06-21 12:28:20 -04001226 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001227 if (ret < 0)
1228 return ret;
1229
Vivien Didelot5da96032016-03-07 18:24:39 -05001230 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1231
1232 if (new) {
1233 ret &= ~PORT_DEFAULT_VLAN_MASK;
1234 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1235
Vivien Didelotfad09c72016-06-21 12:28:20 -04001236 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Vivien Didelot5da96032016-03-07 18:24:39 -05001237 PORT_DEFAULT_VLAN, ret);
1238 if (ret < 0)
1239 return ret;
1240
Andrew Lunnc8b09802016-06-04 21:16:57 +02001241 netdev_dbg(ds->ports[port].netdev,
1242 "DefaultVID %d (was %d)\n", *new, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001243 }
1244
1245 if (old)
1246 *old = pvid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001247
1248 return 0;
1249}
1250
Vivien Didelotfad09c72016-06-21 12:28:20 -04001251static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001252 int port, u16 *pvid)
Vivien Didelot5da96032016-03-07 18:24:39 -05001253{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001254 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
Vivien Didelot5da96032016-03-07 18:24:39 -05001255}
1256
Vivien Didelotfad09c72016-06-21 12:28:20 -04001257static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001258 int port, u16 pvid)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001259{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001260 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001261}
1262
Vivien Didelotfad09c72016-06-21 12:28:20 -04001263static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001264{
Vivien Didelot2d79af62016-08-15 17:18:57 -04001265 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1266 GLOBAL_VTU_OP_BUSY);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001267}
1268
Vivien Didelotfad09c72016-06-21 12:28:20 -04001269static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001270{
1271 int ret;
1272
Vivien Didelotfad09c72016-06-21 12:28:20 -04001273 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001274 if (ret < 0)
1275 return ret;
1276
Vivien Didelotfad09c72016-06-21 12:28:20 -04001277 return _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001278}
1279
Vivien Didelotfad09c72016-06-21 12:28:20 -04001280static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
Vivien Didelot6b17e862015-08-13 12:52:18 -04001281{
1282 int ret;
1283
Vivien Didelotfad09c72016-06-21 12:28:20 -04001284 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001285 if (ret < 0)
1286 return ret;
1287
Vivien Didelotfad09c72016-06-21 12:28:20 -04001288 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
Vivien Didelot6b17e862015-08-13 12:52:18 -04001289}
1290
Vivien Didelotfad09c72016-06-21 12:28:20 -04001291static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001292 struct mv88e6xxx_vtu_stu_entry *entry,
1293 unsigned int nibble_offset)
1294{
Vivien Didelotb8fee952015-08-13 12:52:19 -04001295 u16 regs[3];
1296 int i;
1297 int ret;
1298
1299 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001300 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001301 GLOBAL_VTU_DATA_0_3 + i);
1302 if (ret < 0)
1303 return ret;
1304
1305 regs[i] = ret;
1306 }
1307
Vivien Didelotfad09c72016-06-21 12:28:20 -04001308 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotb8fee952015-08-13 12:52:19 -04001309 unsigned int shift = (i % 4) * 4 + nibble_offset;
1310 u16 reg = regs[i / 4];
1311
1312 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1313 }
1314
1315 return 0;
1316}
1317
Vivien Didelotfad09c72016-06-21 12:28:20 -04001318static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001319 struct mv88e6xxx_vtu_stu_entry *entry)
1320{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001321 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001322}
1323
Vivien Didelotfad09c72016-06-21 12:28:20 -04001324static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001325 struct mv88e6xxx_vtu_stu_entry *entry)
1326{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001327 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001328}
1329
Vivien Didelotfad09c72016-06-21 12:28:20 -04001330static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001331 struct mv88e6xxx_vtu_stu_entry *entry,
1332 unsigned int nibble_offset)
1333{
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001334 u16 regs[3] = { 0 };
1335 int i;
1336 int ret;
1337
Vivien Didelotfad09c72016-06-21 12:28:20 -04001338 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001339 unsigned int shift = (i % 4) * 4 + nibble_offset;
1340 u8 data = entry->data[i];
1341
1342 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1343 }
1344
1345 for (i = 0; i < 3; ++i) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001346 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001347 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1348 if (ret < 0)
1349 return ret;
1350 }
1351
1352 return 0;
1353}
1354
Vivien Didelotfad09c72016-06-21 12:28:20 -04001355static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001356 struct mv88e6xxx_vtu_stu_entry *entry)
1357{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001358 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001359}
1360
Vivien Didelotfad09c72016-06-21 12:28:20 -04001361static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001362 struct mv88e6xxx_vtu_stu_entry *entry)
1363{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001364 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
Vivien Didelot15d7d7d2016-05-10 15:44:28 -04001365}
1366
Vivien Didelotfad09c72016-06-21 12:28:20 -04001367static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001368{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001369 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001370 vid & GLOBAL_VTU_VID_MASK);
1371}
1372
Vivien Didelotfad09c72016-06-21 12:28:20 -04001373static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001374 struct mv88e6xxx_vtu_stu_entry *entry)
1375{
1376 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1377 int ret;
1378
Vivien Didelotfad09c72016-06-21 12:28:20 -04001379 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001380 if (ret < 0)
1381 return ret;
1382
Vivien Didelotfad09c72016-06-21 12:28:20 -04001383 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001384 if (ret < 0)
1385 return ret;
1386
Vivien Didelotfad09c72016-06-21 12:28:20 -04001387 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001388 if (ret < 0)
1389 return ret;
1390
1391 next.vid = ret & GLOBAL_VTU_VID_MASK;
1392 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1393
1394 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001395 ret = mv88e6xxx_vtu_data_read(chip, &next);
Vivien Didelotb8fee952015-08-13 12:52:19 -04001396 if (ret < 0)
1397 return ret;
1398
Vivien Didelotfad09c72016-06-21 12:28:20 -04001399 if (mv88e6xxx_has_fid_reg(chip)) {
1400 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001401 GLOBAL_VTU_FID);
1402 if (ret < 0)
1403 return ret;
1404
1405 next.fid = ret & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001406 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001407 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1408 * VTU DBNum[3:0] are located in VTU Operation 3:0
1409 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001410 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelot11ea8092016-03-31 16:53:44 -04001411 GLOBAL_VTU_OP);
1412 if (ret < 0)
1413 return ret;
1414
1415 next.fid = (ret & 0xf00) >> 4;
1416 next.fid |= ret & 0xf;
Vivien Didelot2e7bd5e2016-03-31 16:53:41 -04001417 }
Vivien Didelotb8fee952015-08-13 12:52:19 -04001418
Vivien Didelotfad09c72016-06-21 12:28:20 -04001419 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1420 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Vivien Didelotb8fee952015-08-13 12:52:19 -04001421 GLOBAL_VTU_SID);
1422 if (ret < 0)
1423 return ret;
1424
1425 next.sid = ret & GLOBAL_VTU_SID_MASK;
1426 }
1427 }
1428
1429 *entry = next;
1430 return 0;
1431}
1432
Vivien Didelotf81ec902016-05-09 13:22:58 -04001433static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1434 struct switchdev_obj_port_vlan *vlan,
1435 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001436{
Vivien Didelot04bed142016-08-31 18:06:13 -04001437 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001438 struct mv88e6xxx_vtu_stu_entry next;
1439 u16 pvid;
1440 int err;
1441
Vivien Didelotfad09c72016-06-21 12:28:20 -04001442 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001443 return -EOPNOTSUPP;
1444
Vivien Didelotfad09c72016-06-21 12:28:20 -04001445 mutex_lock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001446
Vivien Didelotfad09c72016-06-21 12:28:20 -04001447 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001448 if (err)
1449 goto unlock;
1450
Vivien Didelotfad09c72016-06-21 12:28:20 -04001451 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001452 if (err)
1453 goto unlock;
1454
1455 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001456 err = _mv88e6xxx_vtu_getnext(chip, &next);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001457 if (err)
1458 break;
1459
1460 if (!next.valid)
1461 break;
1462
1463 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1464 continue;
1465
1466 /* reinit and dump this VLAN obj */
Vivien Didelot57d32312016-06-20 13:13:58 -04001467 vlan->vid_begin = next.vid;
1468 vlan->vid_end = next.vid;
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001469 vlan->flags = 0;
1470
1471 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1472 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1473
1474 if (next.vid == pvid)
1475 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1476
1477 err = cb(&vlan->obj);
1478 if (err)
1479 break;
1480 } while (next.vid < GLOBAL_VTU_VID_MASK);
1481
1482unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001483 mutex_unlock(&chip->reg_lock);
Vivien Didelotceff5ef2016-02-23 12:13:55 -05001484
1485 return err;
1486}
1487
Vivien Didelotfad09c72016-06-21 12:28:20 -04001488static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001489 struct mv88e6xxx_vtu_stu_entry *entry)
1490{
Vivien Didelot11ea8092016-03-31 16:53:44 -04001491 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001492 u16 reg = 0;
1493 int ret;
1494
Vivien Didelotfad09c72016-06-21 12:28:20 -04001495 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001496 if (ret < 0)
1497 return ret;
1498
1499 if (!entry->valid)
1500 goto loadpurge;
1501
1502 /* Write port member tags */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001503 ret = mv88e6xxx_vtu_data_write(chip, entry);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001504 if (ret < 0)
1505 return ret;
1506
Vivien Didelotfad09c72016-06-21 12:28:20 -04001507 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001508 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001509 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1510 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001511 if (ret < 0)
1512 return ret;
Vivien Didelotb426e5f2016-03-31 16:53:42 -04001513 }
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001514
Vivien Didelotfad09c72016-06-21 12:28:20 -04001515 if (mv88e6xxx_has_fid_reg(chip)) {
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001516 reg = entry->fid & GLOBAL_VTU_FID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001517 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1518 reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001519 if (ret < 0)
1520 return ret;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001521 } else if (mv88e6xxx_num_databases(chip) == 256) {
Vivien Didelot11ea8092016-03-31 16:53:44 -04001522 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1523 * VTU DBNum[3:0] are located in VTU Operation 3:0
1524 */
1525 op |= (entry->fid & 0xf0) << 8;
1526 op |= entry->fid & 0xf;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001527 }
1528
1529 reg = GLOBAL_VTU_VID_VALID;
1530loadpurge:
1531 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001532 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001533 if (ret < 0)
1534 return ret;
1535
Vivien Didelotfad09c72016-06-21 12:28:20 -04001536 return _mv88e6xxx_vtu_cmd(chip, op);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001537}
1538
Vivien Didelotfad09c72016-06-21 12:28:20 -04001539static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001540 struct mv88e6xxx_vtu_stu_entry *entry)
1541{
1542 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1543 int ret;
1544
Vivien Didelotfad09c72016-06-21 12:28:20 -04001545 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001546 if (ret < 0)
1547 return ret;
1548
Vivien Didelotfad09c72016-06-21 12:28:20 -04001549 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001550 sid & GLOBAL_VTU_SID_MASK);
1551 if (ret < 0)
1552 return ret;
1553
Vivien Didelotfad09c72016-06-21 12:28:20 -04001554 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001555 if (ret < 0)
1556 return ret;
1557
Vivien Didelotfad09c72016-06-21 12:28:20 -04001558 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001559 if (ret < 0)
1560 return ret;
1561
1562 next.sid = ret & GLOBAL_VTU_SID_MASK;
1563
Vivien Didelotfad09c72016-06-21 12:28:20 -04001564 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001565 if (ret < 0)
1566 return ret;
1567
1568 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1569
1570 if (next.valid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001571 ret = mv88e6xxx_stu_data_read(chip, &next);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001572 if (ret < 0)
1573 return ret;
1574 }
1575
1576 *entry = next;
1577 return 0;
1578}
1579
Vivien Didelotfad09c72016-06-21 12:28:20 -04001580static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001581 struct mv88e6xxx_vtu_stu_entry *entry)
1582{
1583 u16 reg = 0;
1584 int ret;
1585
Vivien Didelotfad09c72016-06-21 12:28:20 -04001586 ret = _mv88e6xxx_vtu_wait(chip);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001587 if (ret < 0)
1588 return ret;
1589
1590 if (!entry->valid)
1591 goto loadpurge;
1592
1593 /* Write port states */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001594 ret = mv88e6xxx_stu_data_write(chip, entry);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001595 if (ret < 0)
1596 return ret;
1597
1598 reg = GLOBAL_VTU_VID_VALID;
1599loadpurge:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001600 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001601 if (ret < 0)
1602 return ret;
1603
1604 reg = entry->sid & GLOBAL_VTU_SID_MASK;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001605 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001606 if (ret < 0)
1607 return ret;
1608
Vivien Didelotfad09c72016-06-21 12:28:20 -04001609 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001610}
1611
Vivien Didelotfad09c72016-06-21 12:28:20 -04001612static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001613 u16 *new, u16 *old)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001614{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001615 struct dsa_switch *ds = chip->ds;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001616 u16 upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001617 u16 fid;
1618 int ret;
1619
Vivien Didelotfad09c72016-06-21 12:28:20 -04001620 if (mv88e6xxx_num_databases(chip) == 4096)
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001621 upper_mask = 0xff;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001622 else if (mv88e6xxx_num_databases(chip) == 256)
Vivien Didelot11ea8092016-03-31 16:53:44 -04001623 upper_mask = 0xf;
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001624 else
1625 return -EOPNOTSUPP;
1626
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001627 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001628 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001629 if (ret < 0)
1630 return ret;
1631
1632 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1633
1634 if (new) {
1635 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1636 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1637
Vivien Didelotfad09c72016-06-21 12:28:20 -04001638 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001639 ret);
1640 if (ret < 0)
1641 return ret;
1642 }
1643
1644 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001645 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001646 if (ret < 0)
1647 return ret;
1648
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001649 fid |= (ret & upper_mask) << 4;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001650
1651 if (new) {
Vivien Didelotf74df0b2016-03-31 16:53:43 -04001652 ret &= ~upper_mask;
1653 ret |= (*new >> 4) & upper_mask;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001654
Vivien Didelotfad09c72016-06-21 12:28:20 -04001655 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001656 ret);
1657 if (ret < 0)
1658 return ret;
1659
Andrew Lunnc8b09802016-06-04 21:16:57 +02001660 netdev_dbg(ds->ports[port].netdev,
1661 "FID %d (was %d)\n", *new, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001662 }
1663
1664 if (old)
1665 *old = fid;
1666
1667 return 0;
1668}
1669
Vivien Didelotfad09c72016-06-21 12:28:20 -04001670static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001671 int port, u16 *fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001672{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001673 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001674}
1675
Vivien Didelotfad09c72016-06-21 12:28:20 -04001676static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001677 int port, u16 fid)
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001678{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001679 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001680}
1681
Vivien Didelotfad09c72016-06-21 12:28:20 -04001682static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001683{
1684 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1685 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001686 int i, err;
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001687
1688 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1689
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001690 /* Set every FID bit used by the (un)bridged ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001691 for (i = 0; i < chip->info->num_ports; ++i) {
1692 err = _mv88e6xxx_port_fid_get(chip, i, fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05001693 if (err)
1694 return err;
1695
1696 set_bit(*fid, fid_bitmap);
1697 }
1698
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001699 /* Set every FID bit used by the VLAN entries */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001700 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001701 if (err)
1702 return err;
1703
1704 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001705 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001706 if (err)
1707 return err;
1708
1709 if (!vlan.valid)
1710 break;
1711
1712 set_bit(vlan.fid, fid_bitmap);
1713 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1714
1715 /* The reset value 0x000 is used to indicate that multiple address
1716 * databases are not needed. Return the next positive available.
1717 */
1718 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
Vivien Didelotfad09c72016-06-21 12:28:20 -04001719 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001720 return -ENOSPC;
1721
1722 /* Clear the database */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001723 return _mv88e6xxx_atu_flush(chip, *fid, true);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001724}
1725
Vivien Didelotfad09c72016-06-21 12:28:20 -04001726static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001727 struct mv88e6xxx_vtu_stu_entry *entry)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001728{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001729 struct dsa_switch *ds = chip->ds;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001730 struct mv88e6xxx_vtu_stu_entry vlan = {
1731 .valid = true,
1732 .vid = vid,
1733 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001734 int i, err;
1735
Vivien Didelotfad09c72016-06-21 12:28:20 -04001736 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05001737 if (err)
1738 return err;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001739
Vivien Didelot3d131f02015-11-03 10:52:52 -05001740 /* exclude all ports except the CPU and DSA ports */
Vivien Didelotfad09c72016-06-21 12:28:20 -04001741 for (i = 0; i < chip->info->num_ports; ++i)
Vivien Didelot3d131f02015-11-03 10:52:52 -05001742 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1743 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1744 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001745
Vivien Didelotfad09c72016-06-21 12:28:20 -04001746 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1747 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001748 struct mv88e6xxx_vtu_stu_entry vstp;
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001749
1750 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1751 * implemented, only one STU entry is needed to cover all VTU
1752 * entries. Thus, validate the SID 0.
1753 */
1754 vlan.sid = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001755 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001756 if (err)
1757 return err;
1758
1759 if (vstp.sid != vlan.sid || !vstp.valid) {
1760 memset(&vstp, 0, sizeof(vstp));
1761 vstp.valid = true;
1762 vstp.sid = vlan.sid;
1763
Vivien Didelotfad09c72016-06-21 12:28:20 -04001764 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001765 if (err)
1766 return err;
1767 }
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001768 }
1769
1770 *entry = vlan;
1771 return 0;
1772}
1773
Vivien Didelotfad09c72016-06-21 12:28:20 -04001774static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001775 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1776{
1777 int err;
1778
1779 if (!vid)
1780 return -EINVAL;
1781
Vivien Didelotfad09c72016-06-21 12:28:20 -04001782 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001783 if (err)
1784 return err;
1785
Vivien Didelotfad09c72016-06-21 12:28:20 -04001786 err = _mv88e6xxx_vtu_getnext(chip, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001787 if (err)
1788 return err;
1789
1790 if (entry->vid != vid || !entry->valid) {
1791 if (!creat)
1792 return -EOPNOTSUPP;
1793 /* -ENOENT would've been more appropriate, but switchdev expects
1794 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1795 */
1796
Vivien Didelotfad09c72016-06-21 12:28:20 -04001797 err = _mv88e6xxx_vtu_new(chip, vid, entry);
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001798 }
1799
1800 return err;
1801}
1802
Vivien Didelotda9c3592016-02-12 12:09:40 -05001803static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1804 u16 vid_begin, u16 vid_end)
1805{
Vivien Didelot04bed142016-08-31 18:06:13 -04001806 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001807 struct mv88e6xxx_vtu_stu_entry vlan;
1808 int i, err;
1809
1810 if (!vid_begin)
1811 return -EOPNOTSUPP;
1812
Vivien Didelotfad09c72016-06-21 12:28:20 -04001813 mutex_lock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001814
Vivien Didelotfad09c72016-06-21 12:28:20 -04001815 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001816 if (err)
1817 goto unlock;
1818
1819 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04001820 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001821 if (err)
1822 goto unlock;
1823
1824 if (!vlan.valid)
1825 break;
1826
1827 if (vlan.vid > vid_end)
1828 break;
1829
Vivien Didelotfad09c72016-06-21 12:28:20 -04001830 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelotda9c3592016-02-12 12:09:40 -05001831 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1832 continue;
1833
1834 if (vlan.data[i] ==
1835 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1836 continue;
1837
Vivien Didelotfad09c72016-06-21 12:28:20 -04001838 if (chip->ports[i].bridge_dev ==
1839 chip->ports[port].bridge_dev)
Vivien Didelotda9c3592016-02-12 12:09:40 -05001840 break; /* same bridge, check next VLAN */
1841
Andrew Lunnc8b09802016-06-04 21:16:57 +02001842 netdev_warn(ds->ports[port].netdev,
Vivien Didelotda9c3592016-02-12 12:09:40 -05001843 "hardware VLAN %d already used by %s\n",
1844 vlan.vid,
Vivien Didelotfad09c72016-06-21 12:28:20 -04001845 netdev_name(chip->ports[i].bridge_dev));
Vivien Didelotda9c3592016-02-12 12:09:40 -05001846 err = -EOPNOTSUPP;
1847 goto unlock;
1848 }
1849 } while (vlan.vid < vid_end);
1850
1851unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001852 mutex_unlock(&chip->reg_lock);
Vivien Didelotda9c3592016-02-12 12:09:40 -05001853
1854 return err;
1855}
1856
Vivien Didelot214cdb92016-02-26 13:16:08 -05001857static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1858 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1859 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1860 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1861 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1862};
1863
Vivien Didelotf81ec902016-05-09 13:22:58 -04001864static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1865 bool vlan_filtering)
Vivien Didelot214cdb92016-02-26 13:16:08 -05001866{
Vivien Didelot04bed142016-08-31 18:06:13 -04001867 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001868 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1869 PORT_CONTROL_2_8021Q_DISABLED;
1870 int ret;
1871
Vivien Didelotfad09c72016-06-21 12:28:20 -04001872 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001873 return -EOPNOTSUPP;
1874
Vivien Didelotfad09c72016-06-21 12:28:20 -04001875 mutex_lock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001876
Vivien Didelotfad09c72016-06-21 12:28:20 -04001877 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001878 if (ret < 0)
1879 goto unlock;
1880
1881 old = ret & PORT_CONTROL_2_8021Q_MASK;
1882
Vivien Didelot5220ef12016-03-07 18:24:52 -05001883 if (new != old) {
1884 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1885 ret |= new & PORT_CONTROL_2_8021Q_MASK;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001886
Vivien Didelotfad09c72016-06-21 12:28:20 -04001887 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
Vivien Didelot5220ef12016-03-07 18:24:52 -05001888 ret);
1889 if (ret < 0)
1890 goto unlock;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001891
Andrew Lunnc8b09802016-06-04 21:16:57 +02001892 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
Vivien Didelot5220ef12016-03-07 18:24:52 -05001893 mv88e6xxx_port_8021q_mode_names[new],
1894 mv88e6xxx_port_8021q_mode_names[old]);
1895 }
1896
1897 ret = 0;
Vivien Didelot214cdb92016-02-26 13:16:08 -05001898unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04001899 mutex_unlock(&chip->reg_lock);
Vivien Didelot214cdb92016-02-26 13:16:08 -05001900
1901 return ret;
1902}
1903
Vivien Didelot57d32312016-06-20 13:13:58 -04001904static int
1905mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1906 const struct switchdev_obj_port_vlan *vlan,
1907 struct switchdev_trans *trans)
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001908{
Vivien Didelot04bed142016-08-31 18:06:13 -04001909 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotda9c3592016-02-12 12:09:40 -05001910 int err;
1911
Vivien Didelotfad09c72016-06-21 12:28:20 -04001912 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001913 return -EOPNOTSUPP;
1914
Vivien Didelotda9c3592016-02-12 12:09:40 -05001915 /* If the requested port doesn't belong to the same bridge as the VLAN
1916 * members, do not support it (yet) and fallback to software VLAN.
1917 */
1918 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1919 vlan->vid_end);
1920 if (err)
1921 return err;
1922
Vivien Didelot76e398a2015-11-01 12:33:55 -05001923 /* We don't need any dynamic resource from the kernel (yet),
1924 * so skip the prepare phase.
1925 */
1926 return 0;
1927}
1928
Vivien Didelotfad09c72016-06-21 12:28:20 -04001929static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
Andrew Lunn158bc062016-04-28 21:24:06 -04001930 u16 vid, bool untagged)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001931{
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001932 struct mv88e6xxx_vtu_stu_entry vlan;
1933 int err;
1934
Vivien Didelotfad09c72016-06-21 12:28:20 -04001935 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001936 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001937 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001938
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001939 vlan.data[port] = untagged ?
1940 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1941 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1942
Vivien Didelotfad09c72016-06-21 12:28:20 -04001943 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001944}
1945
Vivien Didelotf81ec902016-05-09 13:22:58 -04001946static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1947 const struct switchdev_obj_port_vlan *vlan,
1948 struct switchdev_trans *trans)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001949{
Vivien Didelot04bed142016-08-31 18:06:13 -04001950 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001951 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1952 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1953 u16 vid;
Vivien Didelot76e398a2015-11-01 12:33:55 -05001954
Vivien Didelotfad09c72016-06-21 12:28:20 -04001955 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04001956 return;
1957
Vivien Didelotfad09c72016-06-21 12:28:20 -04001958 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05001959
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001960 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
Vivien Didelotfad09c72016-06-21 12:28:20 -04001961 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001962 netdev_err(ds->ports[port].netdev,
1963 "failed to add VLAN %d%c\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001964 vid, untagged ? 'u' : 't');
Vivien Didelot76e398a2015-11-01 12:33:55 -05001965
Vivien Didelotfad09c72016-06-21 12:28:20 -04001966 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
Andrew Lunnc8b09802016-06-04 21:16:57 +02001967 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
Vivien Didelot4d5770b2016-04-06 11:55:05 -04001968 vlan->vid_end);
1969
Vivien Didelotfad09c72016-06-21 12:28:20 -04001970 mutex_unlock(&chip->reg_lock);
Vivien Didelot0d3b33e2015-08-13 12:52:22 -04001971}
1972
Vivien Didelotfad09c72016-06-21 12:28:20 -04001973static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04001974 int port, u16 vid)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001975{
Vivien Didelotfad09c72016-06-21 12:28:20 -04001976 struct dsa_switch *ds = chip->ds;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001977 struct mv88e6xxx_vtu_stu_entry vlan;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001978 int i, err;
1979
Vivien Didelotfad09c72016-06-21 12:28:20 -04001980 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001981 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05001982 return err;
Vivien Didelot36d04ba2015-10-22 09:34:39 -04001983
Vivien Didelot2fb5ef02016-02-26 13:16:01 -05001984 /* Tell switchdev if this VLAN is handled in software */
1985 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
Vivien Didelot3c06f082016-02-05 14:04:39 -05001986 return -EOPNOTSUPP;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001987
1988 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1989
1990 /* keep the VLAN unless all ports are excluded */
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001991 vlan.valid = false;
Vivien Didelotfad09c72016-06-21 12:28:20 -04001992 for (i = 0; i < chip->info->num_ports; ++i) {
Vivien Didelot3d131f02015-11-03 10:52:52 -05001993 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001994 continue;
1995
1996 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
Vivien Didelotf02bdff2015-10-11 18:08:36 -04001997 vlan.valid = true;
Vivien Didelot7dad08d2015-08-13 12:52:21 -04001998 break;
1999 }
2000 }
2001
Vivien Didelotfad09c72016-06-21 12:28:20 -04002002 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002003 if (err)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002004 return err;
2005
Vivien Didelotfad09c72016-06-21 12:28:20 -04002006 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002007}
2008
Vivien Didelotf81ec902016-05-09 13:22:58 -04002009static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2010 const struct switchdev_obj_port_vlan *vlan)
Vivien Didelot76e398a2015-11-01 12:33:55 -05002011{
Vivien Didelot04bed142016-08-31 18:06:13 -04002012 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot76e398a2015-11-01 12:33:55 -05002013 u16 pvid, vid;
2014 int err = 0;
2015
Vivien Didelotfad09c72016-06-21 12:28:20 -04002016 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
Vivien Didelot54d77b52016-05-09 13:22:47 -04002017 return -EOPNOTSUPP;
2018
Vivien Didelotfad09c72016-06-21 12:28:20 -04002019 mutex_lock(&chip->reg_lock);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002020
Vivien Didelotfad09c72016-06-21 12:28:20 -04002021 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002022 if (err)
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002023 goto unlock;
2024
Vivien Didelot76e398a2015-11-01 12:33:55 -05002025 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002026 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002027 if (err)
2028 goto unlock;
2029
2030 if (vid == pvid) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002031 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
Vivien Didelot76e398a2015-11-01 12:33:55 -05002032 if (err)
2033 goto unlock;
2034 }
2035 }
2036
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002037unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04002038 mutex_unlock(&chip->reg_lock);
Vivien Didelot7dad08d2015-08-13 12:52:21 -04002039
2040 return err;
2041}
2042
Vivien Didelotfad09c72016-06-21 12:28:20 -04002043static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
Vivien Didelotc5723ac2015-08-10 09:09:48 -04002044 const unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002045{
2046 int i, ret;
2047
2048 for (i = 0; i < 3; i++) {
Andrew Lunncca8b132015-04-02 04:06:39 +02002049 ret = _mv88e6xxx_reg_write(
Vivien Didelotfad09c72016-06-21 12:28:20 -04002050 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
Andrew Lunncca8b132015-04-02 04:06:39 +02002051 (addr[i * 2] << 8) | addr[i * 2 + 1]);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002052 if (ret < 0)
2053 return ret;
2054 }
2055
2056 return 0;
2057}
2058
Vivien Didelotfad09c72016-06-21 12:28:20 -04002059static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
Andrew Lunn158bc062016-04-28 21:24:06 -04002060 unsigned char *addr)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002061{
2062 int i, ret;
2063
2064 for (i = 0; i < 3; i++) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002065 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
Andrew Lunncca8b132015-04-02 04:06:39 +02002066 GLOBAL_ATU_MAC_01 + i);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002067 if (ret < 0)
2068 return ret;
2069 addr[i * 2] = ret >> 8;
2070 addr[i * 2 + 1] = ret & 0xff;
2071 }
2072
2073 return 0;
2074}
2075
Vivien Didelotfad09c72016-06-21 12:28:20 -04002076static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
Vivien Didelotfd231c82015-08-10 09:09:50 -04002077 struct mv88e6xxx_atu_entry *entry)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002078{
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002079 int ret;
2080
Vivien Didelotfad09c72016-06-21 12:28:20 -04002081 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002082 if (ret < 0)
2083 return ret;
2084
Vivien Didelotfad09c72016-06-21 12:28:20 -04002085 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002086 if (ret < 0)
2087 return ret;
2088
Vivien Didelotfad09c72016-06-21 12:28:20 -04002089 ret = _mv88e6xxx_atu_data_write(chip, entry);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002090 if (ret < 0)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002091 return ret;
2092
Vivien Didelotfad09c72016-06-21 12:28:20 -04002093 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
Vivien Didelotfd231c82015-08-10 09:09:50 -04002094}
David S. Millercdf09692015-08-11 12:00:37 -07002095
Vivien Didelot83dabd12016-08-31 11:50:04 -04002096static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2097 const unsigned char *addr, u16 vid,
2098 u8 state)
Vivien Didelotfd231c82015-08-10 09:09:50 -04002099{
2100 struct mv88e6xxx_atu_entry entry = { 0 };
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002101 struct mv88e6xxx_vtu_stu_entry vlan;
2102 int err;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002103
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002104 /* Null VLAN ID corresponds to the port private database */
2105 if (vid == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002106 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002107 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002108 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
Vivien Didelot3285f9e2016-02-26 13:16:03 -05002109 if (err)
2110 return err;
2111
2112 entry.fid = vlan.fid;
Vivien Didelotfd231c82015-08-10 09:09:50 -04002113 entry.state = state;
2114 ether_addr_copy(entry.mac, addr);
2115 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2116 entry.trunk = false;
2117 entry.portv_trunkid = BIT(port);
2118 }
2119
Vivien Didelotfad09c72016-06-21 12:28:20 -04002120 return _mv88e6xxx_atu_load(chip, &entry);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002121}
2122
Vivien Didelotf81ec902016-05-09 13:22:58 -04002123static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2124 const struct switchdev_obj_port_fdb *fdb,
2125 struct switchdev_trans *trans)
Vivien Didelot146a3202015-10-08 11:35:12 -04002126{
2127 /* We don't need any dynamic resource from the kernel (yet),
2128 * so skip the prepare phase.
2129 */
2130 return 0;
2131}
2132
Vivien Didelotf81ec902016-05-09 13:22:58 -04002133static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2134 const struct switchdev_obj_port_fdb *fdb,
2135 struct switchdev_trans *trans)
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002136{
Vivien Didelot04bed142016-08-31 18:06:13 -04002137 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot6630e232015-08-06 01:44:07 -04002138
Vivien Didelotfad09c72016-06-21 12:28:20 -04002139 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002140 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2141 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2142 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
Vivien Didelotfad09c72016-06-21 12:28:20 -04002143 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002144}
2145
Vivien Didelotf81ec902016-05-09 13:22:58 -04002146static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2147 const struct switchdev_obj_port_fdb *fdb)
David S. Millercdf09692015-08-11 12:00:37 -07002148{
Vivien Didelot04bed142016-08-31 18:06:13 -04002149 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot83dabd12016-08-31 11:50:04 -04002150 int err;
David S. Millercdf09692015-08-11 12:00:37 -07002151
Vivien Didelotfad09c72016-06-21 12:28:20 -04002152 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002153 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2154 GLOBAL_ATU_DATA_STATE_UNUSED);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002155 mutex_unlock(&chip->reg_lock);
David S. Millercdf09692015-08-11 12:00:37 -07002156
Vivien Didelot83dabd12016-08-31 11:50:04 -04002157 return err;
David S. Millercdf09692015-08-11 12:00:37 -07002158}
2159
Vivien Didelotfad09c72016-06-21 12:28:20 -04002160static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
Vivien Didelot1d194042015-08-10 09:09:51 -04002161 struct mv88e6xxx_atu_entry *entry)
David S. Millercdf09692015-08-11 12:00:37 -07002162{
Vivien Didelot1d194042015-08-10 09:09:51 -04002163 struct mv88e6xxx_atu_entry next = { 0 };
2164 int ret;
2165
2166 next.fid = fid;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002167
Vivien Didelotfad09c72016-06-21 12:28:20 -04002168 ret = _mv88e6xxx_atu_wait(chip);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002169 if (ret < 0)
2170 return ret;
2171
Vivien Didelotfad09c72016-06-21 12:28:20 -04002172 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002173 if (ret < 0)
2174 return ret;
2175
Vivien Didelotfad09c72016-06-21 12:28:20 -04002176 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
Vivien Didelot1d194042015-08-10 09:09:51 -04002177 if (ret < 0)
2178 return ret;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002179
Vivien Didelotfad09c72016-06-21 12:28:20 -04002180 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
Vivien Didelot1d194042015-08-10 09:09:51 -04002181 if (ret < 0)
2182 return ret;
2183
2184 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2185 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2186 unsigned int mask, shift;
2187
2188 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2189 next.trunk = true;
2190 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2191 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2192 } else {
2193 next.trunk = false;
2194 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2195 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2196 }
2197
2198 next.portv_trunkid = (ret & mask) >> shift;
2199 }
2200
2201 *entry = next;
Guenter Roeckdefb05b2015-03-26 18:36:38 -07002202 return 0;
2203}
2204
Vivien Didelot83dabd12016-08-31 11:50:04 -04002205static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2206 u16 fid, u16 vid, int port,
2207 struct switchdev_obj *obj,
2208 int (*cb)(struct switchdev_obj *obj))
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002209{
2210 struct mv88e6xxx_atu_entry addr = {
2211 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2212 };
2213 int err;
2214
Vivien Didelotfad09c72016-06-21 12:28:20 -04002215 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002216 if (err)
2217 return err;
2218
2219 do {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002220 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002221 if (err)
Vivien Didelot83dabd12016-08-31 11:50:04 -04002222 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002223
2224 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2225 break;
2226
Vivien Didelot83dabd12016-08-31 11:50:04 -04002227 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2228 continue;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002229
Vivien Didelot83dabd12016-08-31 11:50:04 -04002230 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2231 struct switchdev_obj_port_fdb *fdb;
2232
2233 if (!is_unicast_ether_addr(addr.mac))
2234 continue;
2235
2236 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002237 fdb->vid = vid;
2238 ether_addr_copy(fdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002239 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2240 fdb->ndm_state = NUD_NOARP;
2241 else
2242 fdb->ndm_state = NUD_REACHABLE;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04002243 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2244 struct switchdev_obj_port_mdb *mdb;
2245
2246 if (!is_multicast_ether_addr(addr.mac))
2247 continue;
2248
2249 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2250 mdb->vid = vid;
2251 ether_addr_copy(mdb->addr, addr.mac);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002252 } else {
2253 return -EOPNOTSUPP;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002254 }
Vivien Didelot83dabd12016-08-31 11:50:04 -04002255
2256 err = cb(obj);
2257 if (err)
2258 return err;
Vivien Didelot74b6ba02016-02-26 13:16:02 -05002259 } while (!is_broadcast_ether_addr(addr.mac));
2260
2261 return err;
2262}
2263
Vivien Didelot83dabd12016-08-31 11:50:04 -04002264static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2265 struct switchdev_obj *obj,
2266 int (*cb)(struct switchdev_obj *obj))
2267{
2268 struct mv88e6xxx_vtu_stu_entry vlan = {
2269 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2270 };
2271 u16 fid;
2272 int err;
2273
2274 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2275 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2276 if (err)
2277 return err;
2278
2279 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2280 if (err)
2281 return err;
2282
2283 /* Dump VLANs' Filtering Information Databases */
2284 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2285 if (err)
2286 return err;
2287
2288 do {
2289 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2290 if (err)
2291 return err;
2292
2293 if (!vlan.valid)
2294 break;
2295
2296 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2297 obj, cb);
2298 if (err)
2299 return err;
2300 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2301
2302 return err;
2303}
2304
Vivien Didelotf81ec902016-05-09 13:22:58 -04002305static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2306 struct switchdev_obj_port_fdb *fdb,
2307 int (*cb)(struct switchdev_obj *obj))
Vivien Didelotf33475b2015-10-22 09:34:41 -04002308{
Vivien Didelot04bed142016-08-31 18:06:13 -04002309 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotf33475b2015-10-22 09:34:41 -04002310 int err;
2311
Vivien Didelotfad09c72016-06-21 12:28:20 -04002312 mutex_lock(&chip->reg_lock);
Vivien Didelot83dabd12016-08-31 11:50:04 -04002313 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002314 mutex_unlock(&chip->reg_lock);
Vivien Didelotf33475b2015-10-22 09:34:41 -04002315
2316 return err;
2317}
2318
Vivien Didelotf81ec902016-05-09 13:22:58 -04002319static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2320 struct net_device *bridge)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002321{
Vivien Didelot04bed142016-08-31 18:06:13 -04002322 struct mv88e6xxx_chip *chip = ds->priv;
Colin Ian King1d9619d2016-04-25 23:11:22 +01002323 int i, err = 0;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002324
Vivien Didelotfad09c72016-06-21 12:28:20 -04002325 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002326
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002327 /* Assign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002328 chip->ports[port].bridge_dev = bridge;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002329
Vivien Didelotfad09c72016-06-21 12:28:20 -04002330 for (i = 0; i < chip->info->num_ports; ++i) {
2331 if (chip->ports[i].bridge_dev == bridge) {
2332 err = _mv88e6xxx_port_based_vlan_map(chip, i);
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002333 if (err)
2334 break;
2335 }
2336 }
2337
Vivien Didelotfad09c72016-06-21 12:28:20 -04002338 mutex_unlock(&chip->reg_lock);
Vivien Didelota6692752016-02-12 12:09:39 -05002339
Vivien Didelot466dfa02016-02-26 13:16:05 -05002340 return err;
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002341}
2342
Vivien Didelotf81ec902016-05-09 13:22:58 -04002343static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
Vivien Didelote79a8bc2015-11-04 17:23:40 -05002344{
Vivien Didelot04bed142016-08-31 18:06:13 -04002345 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002346 struct net_device *bridge = chip->ports[port].bridge_dev;
Vivien Didelot16bfa702016-03-13 16:21:33 -04002347 int i;
Vivien Didelot466dfa02016-02-26 13:16:05 -05002348
Vivien Didelotfad09c72016-06-21 12:28:20 -04002349 mutex_lock(&chip->reg_lock);
Vivien Didelot466dfa02016-02-26 13:16:05 -05002350
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002351 /* Unassign the bridge and remap each port's VLANTable */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002352 chip->ports[port].bridge_dev = NULL;
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002353
Vivien Didelotfad09c72016-06-21 12:28:20 -04002354 for (i = 0; i < chip->info->num_ports; ++i)
2355 if (i == port || chip->ports[i].bridge_dev == bridge)
2356 if (_mv88e6xxx_port_based_vlan_map(chip, i))
Andrew Lunnc8b09802016-06-04 21:16:57 +02002357 netdev_warn(ds->ports[i].netdev,
2358 "failed to remap\n");
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002359
Vivien Didelotfad09c72016-06-21 12:28:20 -04002360 mutex_unlock(&chip->reg_lock);
Vivien Didelot66d9cd02016-02-05 14:07:14 -05002361}
2362
Vivien Didelotfad09c72016-06-21 12:28:20 -04002363static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
Vivien Didelot552238b2016-05-09 13:22:49 -04002364{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002365 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
Vivien Didelot552238b2016-05-09 13:22:49 -04002366 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
Vivien Didelotfad09c72016-06-21 12:28:20 -04002367 struct gpio_desc *gpiod = chip->reset;
Vivien Didelot552238b2016-05-09 13:22:49 -04002368 unsigned long timeout;
2369 int ret;
2370 int i;
2371
2372 /* Set all ports to the disabled state. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002373 for (i = 0; i < chip->info->num_ports; i++) {
2374 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
Vivien Didelot552238b2016-05-09 13:22:49 -04002375 if (ret < 0)
2376 return ret;
2377
Vivien Didelotfad09c72016-06-21 12:28:20 -04002378 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
Vivien Didelot552238b2016-05-09 13:22:49 -04002379 ret & 0xfffc);
2380 if (ret)
2381 return ret;
2382 }
2383
2384 /* Wait for transmit queues to drain. */
2385 usleep_range(2000, 4000);
2386
2387 /* If there is a gpio connected to the reset pin, toggle it */
2388 if (gpiod) {
2389 gpiod_set_value_cansleep(gpiod, 1);
2390 usleep_range(10000, 20000);
2391 gpiod_set_value_cansleep(gpiod, 0);
2392 usleep_range(10000, 20000);
2393 }
2394
2395 /* Reset the switch. Keep the PPU active if requested. The PPU
2396 * needs to be active to support indirect phy register access
2397 * through global registers 0x18 and 0x19.
2398 */
2399 if (ppu_active)
Vivien Didelotfad09c72016-06-21 12:28:20 -04002400 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
Vivien Didelot552238b2016-05-09 13:22:49 -04002401 else
Vivien Didelotfad09c72016-06-21 12:28:20 -04002402 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
Vivien Didelot552238b2016-05-09 13:22:49 -04002403 if (ret)
2404 return ret;
2405
2406 /* Wait up to one second for reset to complete. */
2407 timeout = jiffies + 1 * HZ;
2408 while (time_before(jiffies, timeout)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002409 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
Vivien Didelot552238b2016-05-09 13:22:49 -04002410 if (ret < 0)
2411 return ret;
2412
2413 if ((ret & is_reset) == is_reset)
2414 break;
2415 usleep_range(1000, 2000);
2416 }
2417 if (time_after(jiffies, timeout))
2418 ret = -ETIMEDOUT;
2419 else
2420 ret = 0;
2421
2422 return ret;
2423}
2424
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002425static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002426{
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002427 u16 val;
2428 int err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002429
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002430 /* Clear Power Down bit */
2431 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2432 if (err)
2433 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002434
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002435 if (val & BMCR_PDOWN) {
2436 val &= ~BMCR_PDOWN;
2437 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002438 }
2439
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002440 return err;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002441}
2442
Vivien Didelot8f6345b2016-07-20 18:18:36 -04002443static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port,
2444 int reg, u16 *val)
2445{
2446 int addr = chip->info->port_base_addr + port;
2447
2448 if (port >= chip->info->num_ports)
2449 return -EINVAL;
2450
2451 return mv88e6xxx_read(chip, addr, reg, val);
2452}
2453
Vivien Didelotfad09c72016-06-21 12:28:20 -04002454static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
Guenter Roeckd827e882015-03-26 18:36:29 -07002455{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002456 struct dsa_switch *ds = chip->ds;
Vivien Didelotf02bdff2015-10-11 18:08:36 -04002457 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002458 u16 reg;
Guenter Roeckd827e882015-03-26 18:36:29 -07002459
Vivien Didelotfad09c72016-06-21 12:28:20 -04002460 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2461 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2462 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2463 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002464 /* MAC Forcing register: don't force link, speed,
2465 * duplex or flow control state to any particular
2466 * values on physical ports, but force the CPU port
2467 * and all DSA ports to their maximum bandwidth and
2468 * full duplex.
2469 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002470 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
Andrew Lunn60045cb2015-08-17 23:52:51 +02002471 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
Russell King53adc9e2015-09-21 21:42:59 +01002472 reg &= ~PORT_PCS_CTRL_UNFORCED;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002473 reg |= PORT_PCS_CTRL_FORCE_LINK |
2474 PORT_PCS_CTRL_LINK_UP |
2475 PORT_PCS_CTRL_DUPLEX_FULL |
2476 PORT_PCS_CTRL_FORCE_DUPLEX;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002477 if (mv88e6xxx_6065_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002478 reg |= PORT_PCS_CTRL_100;
2479 else
2480 reg |= PORT_PCS_CTRL_1000;
2481 } else {
2482 reg |= PORT_PCS_CTRL_UNFORCED;
2483 }
2484
Vivien Didelotfad09c72016-06-21 12:28:20 -04002485 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002486 PORT_PCS_CTRL, reg);
2487 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002488 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002489 }
2490
2491 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2492 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2493 * tunneling, determine priority by looking at 802.1p and IP
2494 * priority fields (IP prio has precedence), and set STP state
2495 * to Forwarding.
2496 *
2497 * If this is the CPU link, use DSA or EDSA tagging depending
2498 * on which tagging mode was configured.
2499 *
2500 * If this is a link to another switch, use DSA tagging mode.
2501 *
2502 * If this is the upstream port for this switch, enable
2503 * forwarding of unknown unicasts and multicasts.
2504 */
2505 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002506 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2507 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2508 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2509 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002510 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2511 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2512 PORT_CONTROL_STATE_FORWARDING;
2513 if (dsa_is_cpu_port(ds, port)) {
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002514 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
Andrew Lunn5377b802016-06-04 21:17:02 +02002515 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
Andrew Lunnc047a1f2015-09-29 01:50:56 +02002516 PORT_CONTROL_FORWARD_UNKNOWN_MC;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002517 else
2518 reg |= PORT_CONTROL_DSA_TAG;
Jamie Lentinf027e0c2016-08-22 16:01:04 +02002519 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2520 PORT_CONTROL_FORWARD_UNKNOWN;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002521 }
Andrew Lunn6083ce72015-08-17 23:52:52 +02002522 if (dsa_is_dsa_port(ds, port)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002523 if (mv88e6xxx_6095_family(chip) ||
2524 mv88e6xxx_6185_family(chip))
Andrew Lunn6083ce72015-08-17 23:52:52 +02002525 reg |= PORT_CONTROL_DSA_TAG;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002526 if (mv88e6xxx_6352_family(chip) ||
2527 mv88e6xxx_6351_family(chip) ||
2528 mv88e6xxx_6165_family(chip) ||
2529 mv88e6xxx_6097_family(chip) ||
2530 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002531 reg |= PORT_CONTROL_FRAME_MODE_DSA;
Andrew Lunn6083ce72015-08-17 23:52:52 +02002532 }
2533
Andrew Lunn54d792f2015-05-06 01:09:47 +02002534 if (port == dsa_upstream_port(ds))
2535 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2536 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2537 }
2538 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002539 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002540 PORT_CONTROL, reg);
2541 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002542 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002543 }
2544
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002545 /* If this port is connected to a SerDes, make sure the SerDes is not
2546 * powered down.
2547 */
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002548 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002549 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002550 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002551 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002552 ret &= PORT_STATUS_CMODE_MASK;
2553 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2554 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2555 (ret == PORT_STATUS_CMODE_SGMII)) {
Vivien Didelot09cb7df2016-08-15 17:19:01 -04002556 ret = mv88e6xxx_serdes_power_on(chip);
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002557 if (ret < 0)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002558 return ret;
Patrick Uiterwijk13a7ebb2016-03-30 01:39:41 +00002559 }
2560 }
2561
Vivien Didelot8efdda42015-08-13 12:52:23 -04002562 /* Port Control 2: don't force a good FCS, set the maximum frame size to
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002563 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
Vivien Didelot8efdda42015-08-13 12:52:23 -04002564 * untagged frames on this port, do a destination address lookup on all
2565 * received packets as usual, disable ARP mirroring and don't send a
2566 * copy of all transmitted/received frames on this port to the CPU.
Andrew Lunn54d792f2015-05-06 01:09:47 +02002567 */
2568 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002569 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2570 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2571 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2572 mv88e6xxx_6185_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002573 reg = PORT_CONTROL_2_MAP_DA;
2574
Vivien Didelotfad09c72016-06-21 12:28:20 -04002575 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2576 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
Andrew Lunn54d792f2015-05-06 01:09:47 +02002577 reg |= PORT_CONTROL_2_JUMBO_10240;
2578
Vivien Didelotfad09c72016-06-21 12:28:20 -04002579 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002580 /* Set the upstream port this port should use */
2581 reg |= dsa_upstream_port(ds);
2582 /* enable forwarding of unknown multicast addresses to
2583 * the upstream port
2584 */
2585 if (port == dsa_upstream_port(ds))
2586 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2587 }
2588
Vivien Didelot46fbe5e2016-02-26 13:16:07 -05002589 reg |= PORT_CONTROL_2_8021Q_DISABLED;
Vivien Didelot8efdda42015-08-13 12:52:23 -04002590
Andrew Lunn54d792f2015-05-06 01:09:47 +02002591 if (reg) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002592 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002593 PORT_CONTROL_2, reg);
2594 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002595 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002596 }
2597
2598 /* Port Association Vector: when learning source addresses
2599 * of packets, add the address to the address database using
2600 * a port bitmap that has only the bit for this port set and
2601 * the other bits clear.
2602 */
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002603 reg = 1 << port;
Vivien Didelot996ecb82016-04-14 14:42:08 -04002604 /* Disable learning for CPU port */
2605 if (dsa_is_cpu_port(ds, port))
Vivien Didelot65fa4022016-04-14 14:42:07 -04002606 reg = 0;
Andrew Lunn4c7ea3c2015-11-03 10:52:36 -05002607
Vivien Didelotfad09c72016-06-21 12:28:20 -04002608 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2609 reg);
Andrew Lunn54d792f2015-05-06 01:09:47 +02002610 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002611 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002612
2613 /* Egress rate control 2: disable egress rate control. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002614 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
Andrew Lunn54d792f2015-05-06 01:09:47 +02002615 0x0000);
2616 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002617 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002618
Vivien Didelotfad09c72016-06-21 12:28:20 -04002619 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2620 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2621 mv88e6xxx_6320_family(chip)) {
Andrew Lunn54d792f2015-05-06 01:09:47 +02002622 /* Do not limit the period of time that this port can
2623 * be paused for by the remote end or the period of
2624 * time that this port can pause the remote end.
2625 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002626 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002627 PORT_PAUSE_CTRL, 0x0000);
2628 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002629 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002630
2631 /* Port ATU control: disable limiting the number of
2632 * address database entries that this port is allowed
2633 * to use.
2634 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002635 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002636 PORT_ATU_CONTROL, 0x0000);
2637 /* Priority Override: disable DA, SA and VTU priority
2638 * override.
2639 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002640 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002641 PORT_PRI_OVERRIDE, 0x0000);
2642 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002643 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002644
2645 /* Port Ethertype: use the Ethertype DSA Ethertype
2646 * value.
2647 */
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02002648 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
2649 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2650 PORT_ETH_TYPE, ETH_P_EDSA);
2651 if (ret)
2652 return ret;
2653 }
2654
Andrew Lunn54d792f2015-05-06 01:09:47 +02002655 /* Tag Remap: use an identity 802.1p prio -> switch
2656 * prio mapping.
2657 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002658 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002659 PORT_TAG_REGMAP_0123, 0x3210);
2660 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002661 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002662
2663 /* Tag Remap 2: use an identity 802.1p prio -> switch
2664 * prio mapping.
2665 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002666 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002667 PORT_TAG_REGMAP_4567, 0x7654);
2668 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002669 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002670 }
2671
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002672 /* Rate Control: disable ingress rate limiting. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002673 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2674 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
Vivien Didelotfad09c72016-06-21 12:28:20 -04002675 mv88e6xxx_6320_family(chip)) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04002676 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
Andrew Lunn54d792f2015-05-06 01:09:47 +02002677 PORT_RATE_CONTROL, 0x0001);
2678 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002679 return ret;
Jamie Lentin1bc261f2016-08-22 22:47:08 +01002680 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
2681 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2682 PORT_RATE_CONTROL, 0x0000);
2683 if (ret)
2684 return ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +02002685 }
2686
Guenter Roeck366f0a02015-03-26 18:36:30 -07002687 /* Port Control 1: disable trunking, disable sending
2688 * learning messages to this port.
Guenter Roeckd827e882015-03-26 18:36:29 -07002689 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002690 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2691 0x0000);
Guenter Roeckd827e882015-03-26 18:36:29 -07002692 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002693 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002694
Vivien Didelot207afda2016-04-14 14:42:09 -04002695 /* Port based VLAN map: give each port the same default address
Vivien Didelotb7666ef2016-02-26 13:16:06 -05002696 * database, and allow bidirectional communication between the
2697 * CPU and DSA port(s), and the other ports.
Guenter Roeckd827e882015-03-26 18:36:29 -07002698 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002699 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002700 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002701 return ret;
Vivien Didelot2db9ce12016-02-26 13:16:04 -05002702
Vivien Didelotfad09c72016-06-21 12:28:20 -04002703 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
Guenter Roeckd827e882015-03-26 18:36:29 -07002704 if (ret)
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002705 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002706
2707 /* Default VLAN ID and priority: don't set a default VLAN
2708 * ID, and set the default packet priority to zero.
2709 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002710 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
Vivien Didelot47cf1e652015-04-20 17:43:26 -04002711 0x0000);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04002712 if (ret)
2713 return ret;
Guenter Roeckd827e882015-03-26 18:36:29 -07002714
Andrew Lunndbde9e62015-05-06 01:09:48 +02002715 return 0;
2716}
2717
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002718static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2719{
2720 int err;
2721
2722 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2723 (addr[0] << 8) | addr[1]);
2724 if (err)
2725 return err;
2726
2727 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2728 (addr[2] << 8) | addr[3]);
2729 if (err)
2730 return err;
2731
2732 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2733 (addr[4] << 8) | addr[5]);
2734}
2735
Vivien Didelotacddbd22016-07-18 20:45:39 -04002736static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2737 unsigned int msecs)
2738{
2739 const unsigned int coeff = chip->info->age_time_coeff;
2740 const unsigned int min = 0x01 * coeff;
2741 const unsigned int max = 0xff * coeff;
2742 u8 age_time;
2743 u16 val;
2744 int err;
2745
2746 if (msecs < min || msecs > max)
2747 return -ERANGE;
2748
2749 /* Round to nearest multiple of coeff */
2750 age_time = (msecs + coeff / 2) / coeff;
2751
2752 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2753 if (err)
2754 return err;
2755
2756 /* AgeTime is 11:4 bits */
2757 val &= ~0xff0;
2758 val |= age_time << 4;
2759
2760 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
2761}
2762
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002763static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2764 unsigned int ageing_time)
2765{
Vivien Didelot04bed142016-08-31 18:06:13 -04002766 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot2cfcd962016-07-18 20:45:40 -04002767 int err;
2768
2769 mutex_lock(&chip->reg_lock);
2770 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2771 mutex_unlock(&chip->reg_lock);
2772
2773 return err;
2774}
2775
Vivien Didelot97299342016-07-18 20:45:30 -04002776static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
Vivien Didelot08a01262016-05-09 13:22:50 -04002777{
Vivien Didelotfad09c72016-06-21 12:28:20 -04002778 struct dsa_switch *ds = chip->ds;
Vivien Didelotb0745e872016-05-09 13:22:53 -04002779 u32 upstream_port = dsa_upstream_port(ds);
Vivien Didelot119477b2016-05-09 13:22:51 -04002780 u16 reg;
Vivien Didelot08a01262016-05-09 13:22:50 -04002781 int err;
Vivien Didelot08a01262016-05-09 13:22:50 -04002782
Vivien Didelot119477b2016-05-09 13:22:51 -04002783 /* Enable the PHY Polling Unit if present, don't discard any packets,
2784 * and mask all interrupt sources.
2785 */
2786 reg = 0;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002787 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2788 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
Vivien Didelot119477b2016-05-09 13:22:51 -04002789 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2790
Vivien Didelotfad09c72016-06-21 12:28:20 -04002791 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
Vivien Didelot119477b2016-05-09 13:22:51 -04002792 if (err)
2793 return err;
2794
Vivien Didelotb0745e872016-05-09 13:22:53 -04002795 /* Configure the upstream port, and configure it as the port to which
2796 * ingress and egress and ARP monitor frames are to be sent.
2797 */
2798 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2799 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2800 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002801 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
2802 reg);
Vivien Didelotb0745e872016-05-09 13:22:53 -04002803 if (err)
2804 return err;
2805
Vivien Didelot50484ff2016-05-09 13:22:54 -04002806 /* Disable remote management, and set the switch's DSA device number. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002807 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
Vivien Didelot50484ff2016-05-09 13:22:54 -04002808 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2809 (ds->index & 0x1f));
2810 if (err)
2811 return err;
2812
Vivien Didelotacddbd22016-07-18 20:45:39 -04002813 /* Clear all the VTU and STU entries */
2814 err = _mv88e6xxx_vtu_stu_flush(chip);
2815 if (err < 0)
2816 return err;
2817
Vivien Didelot08a01262016-05-09 13:22:50 -04002818 /* Set the default address aging time to 5 minutes, and
2819 * enable address learn messages to be sent to all message
2820 * ports.
2821 */
Vivien Didelotacddbd22016-07-18 20:45:39 -04002822 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2823 GLOBAL_ATU_CONTROL_LEARN2ALL);
Vivien Didelot08a01262016-05-09 13:22:50 -04002824 if (err)
2825 return err;
2826
Vivien Didelotacddbd22016-07-18 20:45:39 -04002827 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2828 if (err)
Vivien Didelot97299342016-07-18 20:45:30 -04002829 return err;
2830
2831 /* Clear all ATU entries */
2832 err = _mv88e6xxx_atu_flush(chip, 0, true);
2833 if (err)
2834 return err;
2835
Vivien Didelot08a01262016-05-09 13:22:50 -04002836 /* Configure the IP ToS mapping registers. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002837 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002838 if (err)
2839 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002840 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
Vivien Didelot08a01262016-05-09 13:22:50 -04002841 if (err)
2842 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002843 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002844 if (err)
2845 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002846 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
Vivien Didelot08a01262016-05-09 13:22:50 -04002847 if (err)
2848 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002849 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002850 if (err)
2851 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002852 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
Vivien Didelot08a01262016-05-09 13:22:50 -04002853 if (err)
2854 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002855 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002856 if (err)
2857 return err;
Vivien Didelotfad09c72016-06-21 12:28:20 -04002858 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
Vivien Didelot08a01262016-05-09 13:22:50 -04002859 if (err)
2860 return err;
2861
2862 /* Configure the IEEE 802.1p priority mapping register. */
Vivien Didelotfad09c72016-06-21 12:28:20 -04002863 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
Vivien Didelot08a01262016-05-09 13:22:50 -04002864 if (err)
2865 return err;
2866
Vivien Didelot97299342016-07-18 20:45:30 -04002867 /* Clear the statistics counters for all ports */
2868 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
2869 GLOBAL_STATS_OP_FLUSH_ALL);
2870 if (err)
2871 return err;
2872
2873 /* Wait for the flush to complete. */
2874 err = _mv88e6xxx_stats_wait(chip);
2875 if (err)
2876 return err;
2877
2878 return 0;
2879}
2880
Vivien Didelotf22ab642016-07-18 20:45:31 -04002881static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
2882 int target, int port)
2883{
2884 u16 val = (target << 8) | (port & 0xf);
2885
2886 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
2887}
2888
2889static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
2890{
2891 int target, port;
2892 int err;
2893
2894 /* Initialize the routing port to the 32 possible target devices */
2895 for (target = 0; target < 32; ++target) {
2896 port = 0xf;
2897
2898 if (target < DSA_MAX_SWITCHES) {
2899 port = chip->ds->rtable[target];
2900 if (port == DSA_RTABLE_NONE)
2901 port = 0xf;
2902 }
2903
2904 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
2905 if (err)
2906 break;
2907 }
2908
2909 return err;
2910}
2911
Vivien Didelot51540412016-07-18 20:45:32 -04002912static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
2913 bool hask, u16 mask)
2914{
2915 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2916 u16 val = (num << 12) | (mask & port_mask);
2917
2918 if (hask)
2919 val |= GLOBAL2_TRUNK_MASK_HASK;
2920
2921 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
2922}
2923
2924static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
2925 u16 map)
2926{
2927 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2928 u16 val = (id << 11) | (map & port_mask);
2929
2930 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
2931}
2932
2933static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
2934{
2935 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2936 int i, err;
2937
2938 /* Clear all eight possible Trunk Mask vectors */
2939 for (i = 0; i < 8; ++i) {
2940 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
2941 if (err)
2942 return err;
2943 }
2944
2945 /* Clear all sixteen possible Trunk ID routing vectors */
2946 for (i = 0; i < 16; ++i) {
2947 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
2948 if (err)
2949 return err;
2950 }
2951
2952 return 0;
2953}
2954
Vivien Didelot8ec61c72016-07-18 20:45:37 -04002955static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
2956{
2957 int port, err;
2958
2959 /* Init all Ingress Rate Limit resources of all ports */
2960 for (port = 0; port < chip->info->num_ports; ++port) {
2961 /* XXX newer chips (like 88E6390) have different 2-bit ops */
2962 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2963 GLOBAL2_IRL_CMD_OP_INIT_ALL |
2964 (port << 8));
2965 if (err)
2966 break;
2967
2968 /* Wait for the operation to complete */
Vivien Didelot2d79af62016-08-15 17:18:57 -04002969 err = mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2970 GLOBAL2_IRL_CMD_BUSY);
Vivien Didelot8ec61c72016-07-18 20:45:37 -04002971 if (err)
2972 break;
2973 }
2974
2975 return err;
2976}
2977
Vivien Didelot3b4caa12016-07-18 20:45:34 -04002978/* Indirect write to the Switch MAC/WoL/WoF register */
2979static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
2980 unsigned int pointer, u8 data)
2981{
2982 u16 val = (pointer << 8) | data;
2983
2984 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, val);
2985}
2986
2987static int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2988{
2989 int i, err;
2990
2991 for (i = 0; i < 6; i++) {
2992 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
2993 if (err)
2994 break;
2995 }
2996
2997 return err;
2998}
2999
Vivien Didelot9bda8892016-07-18 20:45:36 -04003000static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
3001 u8 data)
3002{
3003 u16 val = (pointer << 8) | (data & 0x7);
3004
3005 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, val);
3006}
3007
3008static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
3009{
3010 int i, err;
3011
3012 /* Clear all sixteen possible Priority Override entries */
3013 for (i = 0; i < 16; i++) {
3014 err = mv88e6xxx_g2_pot_write(chip, i, 0);
3015 if (err)
3016 break;
3017 }
3018
3019 return err;
3020}
3021
Vivien Didelot855b1932016-07-20 18:18:35 -04003022static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
3023{
Vivien Didelot2d79af62016-08-15 17:18:57 -04003024 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD,
3025 GLOBAL2_EEPROM_CMD_BUSY |
3026 GLOBAL2_EEPROM_CMD_RUNNING);
Vivien Didelot855b1932016-07-20 18:18:35 -04003027}
3028
3029static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3030{
3031 int err;
3032
3033 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, cmd);
3034 if (err)
3035 return err;
3036
3037 return mv88e6xxx_g2_eeprom_wait(chip);
3038}
3039
3040static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
3041 u8 addr, u16 *data)
3042{
3043 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
3044 int err;
3045
3046 err = mv88e6xxx_g2_eeprom_wait(chip);
3047 if (err)
3048 return err;
3049
3050 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3051 if (err)
3052 return err;
3053
3054 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3055}
3056
3057static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
3058 u8 addr, u16 data)
3059{
3060 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
3061 int err;
3062
3063 err = mv88e6xxx_g2_eeprom_wait(chip);
3064 if (err)
3065 return err;
3066
3067 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3068 if (err)
3069 return err;
3070
3071 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3072}
3073
Vivien Didelot57c67cf2016-08-15 17:18:59 -04003074static int mv88e6xxx_g2_smi_phy_wait(struct mv88e6xxx_chip *chip)
3075{
3076 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD,
3077 GLOBAL2_SMI_PHY_CMD_BUSY);
3078}
3079
3080static int mv88e6xxx_g2_smi_phy_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3081{
3082 int err;
3083
3084 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD, cmd);
3085 if (err)
3086 return err;
3087
3088 return mv88e6xxx_g2_smi_phy_wait(chip);
3089}
3090
3091static int mv88e6xxx_g2_smi_phy_read(struct mv88e6xxx_chip *chip, int addr,
3092 int reg, u16 *val)
3093{
3094 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_READ_DATA | (addr << 5) | reg;
3095 int err;
3096
3097 err = mv88e6xxx_g2_smi_phy_wait(chip);
3098 if (err)
3099 return err;
3100
3101 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3102 if (err)
3103 return err;
3104
3105 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3106}
3107
3108static int mv88e6xxx_g2_smi_phy_write(struct mv88e6xxx_chip *chip, int addr,
3109 int reg, u16 val)
3110{
3111 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_WRITE_DATA | (addr << 5) | reg;
3112 int err;
3113
3114 err = mv88e6xxx_g2_smi_phy_wait(chip);
3115 if (err)
3116 return err;
3117
3118 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3119 if (err)
3120 return err;
3121
3122 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3123}
3124
Vivien Didelote57e5e72016-08-15 17:19:00 -04003125static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
3126 .read = mv88e6xxx_g2_smi_phy_read,
3127 .write = mv88e6xxx_g2_smi_phy_write,
3128};
3129
Vivien Didelot97299342016-07-18 20:45:30 -04003130static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3131{
Vivien Didelot47395ed2016-07-18 20:45:33 -04003132 u16 reg;
Vivien Didelot97299342016-07-18 20:45:30 -04003133 int err;
Vivien Didelot97299342016-07-18 20:45:30 -04003134
Vivien Didelot47395ed2016-07-18 20:45:33 -04003135 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3136 /* Consider the frames with reserved multicast destination
3137 * addresses matching 01:80:c2:00:00:2x as MGMT.
3138 */
3139 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3140 0xffff);
3141 if (err)
3142 return err;
3143 }
3144
3145 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3146 /* Consider the frames with reserved multicast destination
3147 * addresses matching 01:80:c2:00:00:0x as MGMT.
3148 */
3149 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3150 0xffff);
3151 if (err)
3152 return err;
3153 }
Vivien Didelot08a01262016-05-09 13:22:50 -04003154
3155 /* Ignore removed tag data on doubly tagged packets, disable
3156 * flow control messages, force flow control priority to the
3157 * highest, and send all special multicast frames to the CPU
3158 * port at the highest priority.
3159 */
Vivien Didelot47395ed2016-07-18 20:45:33 -04003160 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3161 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3162 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3163 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3164 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
Vivien Didelot08a01262016-05-09 13:22:50 -04003165 if (err)
3166 return err;
3167
3168 /* Program the DSA routing table. */
Vivien Didelotf22ab642016-07-18 20:45:31 -04003169 err = mv88e6xxx_g2_set_device_mapping(chip);
3170 if (err)
3171 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003172
Vivien Didelot51540412016-07-18 20:45:32 -04003173 /* Clear all trunk masks and mapping. */
3174 err = mv88e6xxx_g2_clear_trunk(chip);
3175 if (err)
3176 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003177
Vivien Didelot8ec61c72016-07-18 20:45:37 -04003178 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
3179 /* Disable ingress rate limiting by resetting all per port
3180 * ingress rate limit resources to their initial state.
3181 */
3182 err = mv88e6xxx_g2_clear_irl(chip);
3183 if (err)
3184 return err;
3185 }
3186
Vivien Didelot63ed8802016-07-18 20:45:35 -04003187 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
3188 /* Initialize Cross-chip Port VLAN Table to reset defaults */
3189 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_PVT_ADDR,
3190 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
3191 if (err)
3192 return err;
3193 }
3194
Vivien Didelot9bda8892016-07-18 20:45:36 -04003195 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
Vivien Didelot08a01262016-05-09 13:22:50 -04003196 /* Clear the priority override table. */
Vivien Didelot9bda8892016-07-18 20:45:36 -04003197 err = mv88e6xxx_g2_clear_pot(chip);
3198 if (err)
3199 return err;
Vivien Didelot08a01262016-05-09 13:22:50 -04003200 }
3201
Vivien Didelot97299342016-07-18 20:45:30 -04003202 return 0;
Vivien Didelot08a01262016-05-09 13:22:50 -04003203}
3204
Vivien Didelotf81ec902016-05-09 13:22:58 -04003205static int mv88e6xxx_setup(struct dsa_switch *ds)
Guenter Roeckacdaffc2015-03-26 18:36:28 -07003206{
Vivien Didelot04bed142016-08-31 18:06:13 -04003207 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot552238b2016-05-09 13:22:49 -04003208 int err;
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003209 int i;
3210
Vivien Didelotfad09c72016-06-21 12:28:20 -04003211 chip->ds = ds;
3212 ds->slave_mii_bus = chip->mdio_bus;
Vivien Didelot552238b2016-05-09 13:22:49 -04003213
Vivien Didelotfad09c72016-06-21 12:28:20 -04003214 mutex_lock(&chip->reg_lock);
Vivien Didelot552238b2016-05-09 13:22:49 -04003215
Vivien Didelotfad09c72016-06-21 12:28:20 -04003216 err = mv88e6xxx_switch_reset(chip);
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003217 if (err)
3218 goto unlock;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003219
Vivien Didelot97299342016-07-18 20:45:30 -04003220 /* Setup Switch Port Registers */
3221 for (i = 0; i < chip->info->num_ports; i++) {
3222 err = mv88e6xxx_setup_port(chip, i);
3223 if (err)
3224 goto unlock;
3225 }
3226
3227 /* Setup Switch Global 1 Registers */
3228 err = mv88e6xxx_g1_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003229 if (err)
3230 goto unlock;
3231
Vivien Didelot97299342016-07-18 20:45:30 -04003232 /* Setup Switch Global 2 Registers */
3233 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3234 err = mv88e6xxx_g2_setup(chip);
Vivien Didelota1a6a4d2016-05-09 13:22:56 -04003235 if (err)
3236 goto unlock;
3237 }
Andrew Lunn54d792f2015-05-06 01:09:47 +02003238
Vivien Didelot6b17e862015-08-13 12:52:18 -04003239unlock:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003240 mutex_unlock(&chip->reg_lock);
Andrew Lunndb687a52015-06-20 21:31:29 +02003241
Andrew Lunn48ace4e2016-04-14 23:47:12 +02003242 return err;
Andrew Lunn54d792f2015-05-06 01:09:47 +02003243}
3244
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003245static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3246{
Vivien Didelot04bed142016-08-31 18:06:13 -04003247 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot3b4caa12016-07-18 20:45:34 -04003248 int err;
3249
3250 mutex_lock(&chip->reg_lock);
3251
3252 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
3253 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
3254 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
3255 else
3256 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
3257
3258 mutex_unlock(&chip->reg_lock);
3259
3260 return err;
3261}
3262
Vivien Didelote57e5e72016-08-15 17:19:00 -04003263static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003264{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003265 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04003266 u16 val;
3267 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003268
Vivien Didelote57e5e72016-08-15 17:19:00 -04003269 if (phy >= chip->info->num_ports)
Andrew Lunn158bc062016-04-28 21:24:06 -04003270 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003271
Vivien Didelotfad09c72016-06-21 12:28:20 -04003272 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003273 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003274 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003275
3276 return err ? err : val;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003277}
3278
Vivien Didelote57e5e72016-08-15 17:19:00 -04003279static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003280{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003281 struct mv88e6xxx_chip *chip = bus->priv;
Vivien Didelote57e5e72016-08-15 17:19:00 -04003282 int err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003283
Vivien Didelote57e5e72016-08-15 17:19:00 -04003284 if (phy >= chip->info->num_ports)
Andrew Lunn158bc062016-04-28 21:24:06 -04003285 return 0xffff;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003286
Vivien Didelotfad09c72016-06-21 12:28:20 -04003287 mutex_lock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003288 err = mv88e6xxx_phy_write(chip, phy, reg, val);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003289 mutex_unlock(&chip->reg_lock);
Vivien Didelote57e5e72016-08-15 17:19:00 -04003290
3291 return err;
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +02003292}
3293
Vivien Didelotfad09c72016-06-21 12:28:20 -04003294static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
Andrew Lunnb516d452016-06-04 21:17:06 +02003295 struct device_node *np)
3296{
3297 static int index;
3298 struct mii_bus *bus;
3299 int err;
3300
Andrew Lunnb516d452016-06-04 21:17:06 +02003301 if (np)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003302 chip->mdio_np = of_get_child_by_name(np, "mdio");
Andrew Lunnb516d452016-06-04 21:17:06 +02003303
Vivien Didelotfad09c72016-06-21 12:28:20 -04003304 bus = devm_mdiobus_alloc(chip->dev);
Andrew Lunnb516d452016-06-04 21:17:06 +02003305 if (!bus)
3306 return -ENOMEM;
3307
Vivien Didelotfad09c72016-06-21 12:28:20 -04003308 bus->priv = (void *)chip;
Andrew Lunnb516d452016-06-04 21:17:06 +02003309 if (np) {
3310 bus->name = np->full_name;
3311 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3312 } else {
3313 bus->name = "mv88e6xxx SMI";
3314 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3315 }
3316
3317 bus->read = mv88e6xxx_mdio_read;
3318 bus->write = mv88e6xxx_mdio_write;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003319 bus->parent = chip->dev;
Andrew Lunnb516d452016-06-04 21:17:06 +02003320
Vivien Didelotfad09c72016-06-21 12:28:20 -04003321 if (chip->mdio_np)
3322 err = of_mdiobus_register(bus, chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003323 else
3324 err = mdiobus_register(bus);
3325 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04003326 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
Andrew Lunnb516d452016-06-04 21:17:06 +02003327 goto out;
3328 }
Vivien Didelotfad09c72016-06-21 12:28:20 -04003329 chip->mdio_bus = bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003330
3331 return 0;
3332
3333out:
Vivien Didelotfad09c72016-06-21 12:28:20 -04003334 if (chip->mdio_np)
3335 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003336
3337 return err;
3338}
3339
Vivien Didelotfad09c72016-06-21 12:28:20 -04003340static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
Andrew Lunnb516d452016-06-04 21:17:06 +02003341
3342{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003343 struct mii_bus *bus = chip->mdio_bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003344
3345 mdiobus_unregister(bus);
3346
Vivien Didelotfad09c72016-06-21 12:28:20 -04003347 if (chip->mdio_np)
3348 of_node_put(chip->mdio_np);
Andrew Lunnb516d452016-06-04 21:17:06 +02003349}
3350
Guenter Roeckc22995c2015-07-25 09:42:28 -07003351#ifdef CONFIG_NET_DSA_HWMON
3352
3353static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3354{
Vivien Didelot04bed142016-08-31 18:06:13 -04003355 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot9c938292016-08-15 17:19:02 -04003356 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003357 int ret;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003358
3359 *temp = 0;
3360
Vivien Didelotfad09c72016-06-21 12:28:20 -04003361 mutex_lock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003362
Vivien Didelot9c938292016-08-15 17:19:02 -04003363 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003364 if (ret < 0)
3365 goto error;
3366
3367 /* Enable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003368 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003369 if (ret < 0)
3370 goto error;
3371
Vivien Didelot9c938292016-08-15 17:19:02 -04003372 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003373 if (ret < 0)
3374 goto error;
3375
3376 /* Wait for temperature to stabilize */
3377 usleep_range(10000, 12000);
3378
Vivien Didelot9c938292016-08-15 17:19:02 -04003379 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3380 if (ret < 0)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003381 goto error;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003382
3383 /* Disable temperature sensor */
Vivien Didelot9c938292016-08-15 17:19:02 -04003384 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
Guenter Roeckc22995c2015-07-25 09:42:28 -07003385 if (ret < 0)
3386 goto error;
3387
3388 *temp = ((val & 0x1f) - 5) * 5;
3389
3390error:
Vivien Didelot9c938292016-08-15 17:19:02 -04003391 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
Vivien Didelotfad09c72016-06-21 12:28:20 -04003392 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003393 return ret;
3394}
3395
3396static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3397{
Vivien Didelot04bed142016-08-31 18:06:13 -04003398 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003399 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003400 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003401 int ret;
3402
3403 *temp = 0;
3404
Vivien Didelot9c938292016-08-15 17:19:02 -04003405 mutex_lock(&chip->reg_lock);
3406 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3407 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003408 if (ret < 0)
3409 return ret;
3410
Vivien Didelot9c938292016-08-15 17:19:02 -04003411 *temp = (val & 0xff) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003412
3413 return 0;
3414}
3415
Vivien Didelotf81ec902016-05-09 13:22:58 -04003416static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003417{
Vivien Didelot04bed142016-08-31 18:06:13 -04003418 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn158bc062016-04-28 21:24:06 -04003419
Vivien Didelotfad09c72016-06-21 12:28:20 -04003420 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
Vivien Didelot6594f612016-05-09 13:22:42 -04003421 return -EOPNOTSUPP;
3422
Vivien Didelotfad09c72016-06-21 12:28:20 -04003423 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003424 return mv88e63xx_get_temp(ds, temp);
3425
3426 return mv88e61xx_get_temp(ds, temp);
3427}
3428
Vivien Didelotf81ec902016-05-09 13:22:58 -04003429static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003430{
Vivien Didelot04bed142016-08-31 18:06:13 -04003431 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003432 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003433 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003434 int ret;
3435
Vivien Didelotfad09c72016-06-21 12:28:20 -04003436 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003437 return -EOPNOTSUPP;
3438
3439 *temp = 0;
3440
Vivien Didelot9c938292016-08-15 17:19:02 -04003441 mutex_lock(&chip->reg_lock);
3442 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3443 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003444 if (ret < 0)
3445 return ret;
3446
Vivien Didelot9c938292016-08-15 17:19:02 -04003447 *temp = (((val >> 8) & 0x1f) * 5) - 25;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003448
3449 return 0;
3450}
3451
Vivien Didelotf81ec902016-05-09 13:22:58 -04003452static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003453{
Vivien Didelot04bed142016-08-31 18:06:13 -04003454 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003455 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003456 u16 val;
3457 int err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003458
Vivien Didelotfad09c72016-06-21 12:28:20 -04003459 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003460 return -EOPNOTSUPP;
3461
Vivien Didelot9c938292016-08-15 17:19:02 -04003462 mutex_lock(&chip->reg_lock);
3463 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3464 if (err)
3465 goto unlock;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003466 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Vivien Didelot9c938292016-08-15 17:19:02 -04003467 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3468 (val & 0xe0ff) | (temp << 8));
3469unlock:
3470 mutex_unlock(&chip->reg_lock);
3471
3472 return err;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003473}
3474
Vivien Didelotf81ec902016-05-09 13:22:58 -04003475static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
Guenter Roeckc22995c2015-07-25 09:42:28 -07003476{
Vivien Didelot04bed142016-08-31 18:06:13 -04003477 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelotfad09c72016-06-21 12:28:20 -04003478 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
Vivien Didelot9c938292016-08-15 17:19:02 -04003479 u16 val;
Guenter Roeckc22995c2015-07-25 09:42:28 -07003480 int ret;
3481
Vivien Didelotfad09c72016-06-21 12:28:20 -04003482 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
Guenter Roeckc22995c2015-07-25 09:42:28 -07003483 return -EOPNOTSUPP;
3484
3485 *alarm = false;
3486
Vivien Didelot9c938292016-08-15 17:19:02 -04003487 mutex_lock(&chip->reg_lock);
3488 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3489 mutex_unlock(&chip->reg_lock);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003490 if (ret < 0)
3491 return ret;
3492
Vivien Didelot9c938292016-08-15 17:19:02 -04003493 *alarm = !!(val & 0x40);
Guenter Roeckc22995c2015-07-25 09:42:28 -07003494
3495 return 0;
3496}
3497#endif /* CONFIG_NET_DSA_HWMON */
3498
Vivien Didelot855b1932016-07-20 18:18:35 -04003499static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3500{
Vivien Didelot04bed142016-08-31 18:06:13 -04003501 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003502
3503 return chip->eeprom_len;
3504}
3505
3506static int mv88e6xxx_get_eeprom16(struct mv88e6xxx_chip *chip,
3507 struct ethtool_eeprom *eeprom, u8 *data)
3508{
3509 unsigned int offset = eeprom->offset;
3510 unsigned int len = eeprom->len;
3511 u16 val;
3512 int err;
3513
3514 eeprom->len = 0;
3515
3516 if (offset & 1) {
3517 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3518 if (err)
3519 return err;
3520
3521 *data++ = (val >> 8) & 0xff;
3522
3523 offset++;
3524 len--;
3525 eeprom->len++;
3526 }
3527
3528 while (len >= 2) {
3529 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3530 if (err)
3531 return err;
3532
3533 *data++ = val & 0xff;
3534 *data++ = (val >> 8) & 0xff;
3535
3536 offset += 2;
3537 len -= 2;
3538 eeprom->len += 2;
3539 }
3540
3541 if (len) {
3542 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3543 if (err)
3544 return err;
3545
3546 *data++ = val & 0xff;
3547
3548 offset++;
3549 len--;
3550 eeprom->len++;
3551 }
3552
3553 return 0;
3554}
3555
3556static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3557 struct ethtool_eeprom *eeprom, u8 *data)
3558{
Vivien Didelot04bed142016-08-31 18:06:13 -04003559 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003560 int err;
3561
3562 mutex_lock(&chip->reg_lock);
3563
3564 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3565 err = mv88e6xxx_get_eeprom16(chip, eeprom, data);
3566 else
3567 err = -EOPNOTSUPP;
3568
3569 mutex_unlock(&chip->reg_lock);
3570
3571 if (err)
3572 return err;
3573
3574 eeprom->magic = 0xc3ec4951;
3575
3576 return 0;
3577}
3578
3579static int mv88e6xxx_set_eeprom16(struct mv88e6xxx_chip *chip,
3580 struct ethtool_eeprom *eeprom, u8 *data)
3581{
3582 unsigned int offset = eeprom->offset;
3583 unsigned int len = eeprom->len;
3584 u16 val;
3585 int err;
3586
3587 /* Ensure the RO WriteEn bit is set */
3588 err = mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, &val);
3589 if (err)
3590 return err;
3591
3592 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
3593 return -EROFS;
3594
3595 eeprom->len = 0;
3596
3597 if (offset & 1) {
3598 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3599 if (err)
3600 return err;
3601
3602 val = (*data++ << 8) | (val & 0xff);
3603
3604 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3605 if (err)
3606 return err;
3607
3608 offset++;
3609 len--;
3610 eeprom->len++;
3611 }
3612
3613 while (len >= 2) {
3614 val = *data++;
3615 val |= *data++ << 8;
3616
3617 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3618 if (err)
3619 return err;
3620
3621 offset += 2;
3622 len -= 2;
3623 eeprom->len += 2;
3624 }
3625
3626 if (len) {
3627 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3628 if (err)
3629 return err;
3630
3631 val = (val & 0xff00) | *data++;
3632
3633 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3634 if (err)
3635 return err;
3636
3637 offset++;
3638 len--;
3639 eeprom->len++;
3640 }
3641
3642 return 0;
3643}
3644
3645static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3646 struct ethtool_eeprom *eeprom, u8 *data)
3647{
Vivien Didelot04bed142016-08-31 18:06:13 -04003648 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot855b1932016-07-20 18:18:35 -04003649 int err;
3650
3651 if (eeprom->magic != 0xc3ec4951)
3652 return -EINVAL;
3653
3654 mutex_lock(&chip->reg_lock);
3655
3656 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3657 err = mv88e6xxx_set_eeprom16(chip, eeprom, data);
3658 else
3659 err = -EOPNOTSUPP;
3660
3661 mutex_unlock(&chip->reg_lock);
3662
3663 return err;
3664}
3665
Vivien Didelotf81ec902016-05-09 13:22:58 -04003666static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3667 [MV88E6085] = {
3668 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3669 .family = MV88E6XXX_FAMILY_6097,
3670 .name = "Marvell 88E6085",
3671 .num_databases = 4096,
3672 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003673 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003674 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003675 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3676 },
3677
3678 [MV88E6095] = {
3679 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3680 .family = MV88E6XXX_FAMILY_6095,
3681 .name = "Marvell 88E6095/88E6095F",
3682 .num_databases = 256,
3683 .num_ports = 11,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003684 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003685 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003686 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3687 },
3688
3689 [MV88E6123] = {
3690 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3691 .family = MV88E6XXX_FAMILY_6165,
3692 .name = "Marvell 88E6123",
3693 .num_databases = 4096,
3694 .num_ports = 3,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003695 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003696 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003697 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3698 },
3699
3700 [MV88E6131] = {
3701 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3702 .family = MV88E6XXX_FAMILY_6185,
3703 .name = "Marvell 88E6131",
3704 .num_databases = 256,
3705 .num_ports = 8,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003706 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003707 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003708 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3709 },
3710
3711 [MV88E6161] = {
3712 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3713 .family = MV88E6XXX_FAMILY_6165,
3714 .name = "Marvell 88E6161",
3715 .num_databases = 4096,
3716 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003717 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003718 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003719 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3720 },
3721
3722 [MV88E6165] = {
3723 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3724 .family = MV88E6XXX_FAMILY_6165,
3725 .name = "Marvell 88E6165",
3726 .num_databases = 4096,
3727 .num_ports = 6,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003728 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003729 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003730 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3731 },
3732
3733 [MV88E6171] = {
3734 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3735 .family = MV88E6XXX_FAMILY_6351,
3736 .name = "Marvell 88E6171",
3737 .num_databases = 4096,
3738 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003739 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003740 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003741 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3742 },
3743
3744 [MV88E6172] = {
3745 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3746 .family = MV88E6XXX_FAMILY_6352,
3747 .name = "Marvell 88E6172",
3748 .num_databases = 4096,
3749 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003750 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003751 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003752 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3753 },
3754
3755 [MV88E6175] = {
3756 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3757 .family = MV88E6XXX_FAMILY_6351,
3758 .name = "Marvell 88E6175",
3759 .num_databases = 4096,
3760 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003761 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003762 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003763 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3764 },
3765
3766 [MV88E6176] = {
3767 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3768 .family = MV88E6XXX_FAMILY_6352,
3769 .name = "Marvell 88E6176",
3770 .num_databases = 4096,
3771 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003772 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003773 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003774 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3775 },
3776
3777 [MV88E6185] = {
3778 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3779 .family = MV88E6XXX_FAMILY_6185,
3780 .name = "Marvell 88E6185",
3781 .num_databases = 256,
3782 .num_ports = 10,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003783 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003784 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003785 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3786 },
3787
3788 [MV88E6240] = {
3789 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3790 .family = MV88E6XXX_FAMILY_6352,
3791 .name = "Marvell 88E6240",
3792 .num_databases = 4096,
3793 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003794 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003795 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003796 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3797 },
3798
3799 [MV88E6320] = {
3800 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3801 .family = MV88E6XXX_FAMILY_6320,
3802 .name = "Marvell 88E6320",
3803 .num_databases = 4096,
3804 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003805 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003806 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003807 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3808 },
3809
3810 [MV88E6321] = {
3811 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3812 .family = MV88E6XXX_FAMILY_6320,
3813 .name = "Marvell 88E6321",
3814 .num_databases = 4096,
3815 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003816 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003817 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003818 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3819 },
3820
3821 [MV88E6350] = {
3822 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3823 .family = MV88E6XXX_FAMILY_6351,
3824 .name = "Marvell 88E6350",
3825 .num_databases = 4096,
3826 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003827 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003828 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003829 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3830 },
3831
3832 [MV88E6351] = {
3833 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3834 .family = MV88E6XXX_FAMILY_6351,
3835 .name = "Marvell 88E6351",
3836 .num_databases = 4096,
3837 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003838 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003839 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003840 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3841 },
3842
3843 [MV88E6352] = {
3844 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3845 .family = MV88E6XXX_FAMILY_6352,
3846 .name = "Marvell 88E6352",
3847 .num_databases = 4096,
3848 .num_ports = 7,
Vivien Didelot9dddd472016-06-20 13:14:10 -04003849 .port_base_addr = 0x10,
Vivien Didelotacddbd22016-07-18 20:45:39 -04003850 .age_time_coeff = 15000,
Vivien Didelotf81ec902016-05-09 13:22:58 -04003851 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3852 },
3853};
3854
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003855static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
Vivien Didelotb9b37712015-10-30 19:39:48 -04003856{
Vivien Didelota439c062016-04-17 13:23:58 -04003857 int i;
Vivien Didelotb9b37712015-10-30 19:39:48 -04003858
Vivien Didelot5f7c0362016-06-20 13:14:04 -04003859 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3860 if (mv88e6xxx_table[i].prod_num == prod_num)
3861 return &mv88e6xxx_table[i];
Vivien Didelotb9b37712015-10-30 19:39:48 -04003862
Vivien Didelotb9b37712015-10-30 19:39:48 -04003863 return NULL;
3864}
3865
Vivien Didelotfad09c72016-06-21 12:28:20 -04003866static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003867{
3868 const struct mv88e6xxx_info *info;
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003869 unsigned int prod_num, rev;
3870 u16 id;
3871 int err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003872
Vivien Didelot8f6345b2016-07-20 18:18:36 -04003873 mutex_lock(&chip->reg_lock);
3874 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3875 mutex_unlock(&chip->reg_lock);
3876 if (err)
3877 return err;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003878
3879 prod_num = (id & 0xfff0) >> 4;
3880 rev = id & 0x000f;
3881
3882 info = mv88e6xxx_lookup_info(prod_num);
3883 if (!info)
3884 return -ENODEV;
3885
Vivien Didelotcaac8542016-06-20 13:14:09 -04003886 /* Update the compatible info with the probed one */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003887 chip->info = info;
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003888
Vivien Didelotfad09c72016-06-21 12:28:20 -04003889 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3890 chip->info->prod_num, chip->info->name, rev);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003891
3892 return 0;
3893}
3894
Vivien Didelotfad09c72016-06-21 12:28:20 -04003895static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
Vivien Didelot469d7292016-06-20 13:14:06 -04003896{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003897 struct mv88e6xxx_chip *chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003898
Vivien Didelotfad09c72016-06-21 12:28:20 -04003899 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3900 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003901 return NULL;
3902
Vivien Didelotfad09c72016-06-21 12:28:20 -04003903 chip->dev = dev;
Vivien Didelot469d7292016-06-20 13:14:06 -04003904
Vivien Didelotfad09c72016-06-21 12:28:20 -04003905 mutex_init(&chip->reg_lock);
Vivien Didelot469d7292016-06-20 13:14:06 -04003906
Vivien Didelotfad09c72016-06-21 12:28:20 -04003907 return chip;
Vivien Didelot469d7292016-06-20 13:14:06 -04003908}
3909
Vivien Didelote57e5e72016-08-15 17:19:00 -04003910static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
3911 .read = mv88e6xxx_read,
3912 .write = mv88e6xxx_write,
3913};
3914
3915static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3916{
3917 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3918 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3919 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3920 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3921 mv88e6xxx_ppu_state_init(chip);
3922 } else {
3923 chip->phy_ops = &mv88e6xxx_phy_ops;
3924 }
3925}
3926
Andrew Lunn930188c2016-08-22 16:01:03 +02003927static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3928{
3929 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3930 mv88e6xxx_ppu_state_destroy(chip);
3931 }
3932}
3933
Vivien Didelotfad09c72016-06-21 12:28:20 -04003934static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003935 struct mii_bus *bus, int sw_addr)
3936{
3937 /* ADDR[0] pin is unavailable externally and considered zero */
3938 if (sw_addr & 0x1)
3939 return -EINVAL;
3940
Vivien Didelot914b32f2016-06-20 13:14:11 -04003941 if (sw_addr == 0)
Vivien Didelotfad09c72016-06-21 12:28:20 -04003942 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
Vivien Didelota0ffff22016-08-15 17:18:58 -04003943 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
Vivien Didelotfad09c72016-06-21 12:28:20 -04003944 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
Vivien Didelot914b32f2016-06-20 13:14:11 -04003945 else
3946 return -EINVAL;
3947
Vivien Didelotfad09c72016-06-21 12:28:20 -04003948 chip->bus = bus;
3949 chip->sw_addr = sw_addr;
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003950
3951 return 0;
3952}
3953
Andrew Lunn7b314362016-08-22 16:01:01 +02003954static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3955{
Vivien Didelot04bed142016-08-31 18:06:13 -04003956 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn2bbb33b2016-08-22 16:01:02 +02003957
3958 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3959 return DSA_TAG_PROTO_EDSA;
3960
3961 return DSA_TAG_PROTO_DSA;
Andrew Lunn7b314362016-08-22 16:01:01 +02003962}
3963
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02003964static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3965 struct device *host_dev, int sw_addr,
3966 void **priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +02003967{
Vivien Didelotfad09c72016-06-21 12:28:20 -04003968 struct mv88e6xxx_chip *chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003969 struct mii_bus *bus;
Andrew Lunnb516d452016-06-04 21:17:06 +02003970 int err;
Andrew Lunna77d43f2016-04-13 02:40:42 +02003971
Vivien Didelota439c062016-04-17 13:23:58 -04003972 bus = dsa_host_dev_to_mii_bus(host_dev);
Andrew Lunnc1569132016-04-13 02:40:45 +02003973 if (!bus)
3974 return NULL;
3975
Vivien Didelotfad09c72016-06-21 12:28:20 -04003976 chip = mv88e6xxx_alloc_chip(dsa_dev);
3977 if (!chip)
Vivien Didelot469d7292016-06-20 13:14:06 -04003978 return NULL;
3979
Vivien Didelotcaac8542016-06-20 13:14:09 -04003980 /* Legacy SMI probing will only support chips similar to 88E6085 */
Vivien Didelotfad09c72016-06-21 12:28:20 -04003981 chip->info = &mv88e6xxx_table[MV88E6085];
Vivien Didelotcaac8542016-06-20 13:14:09 -04003982
Vivien Didelotfad09c72016-06-21 12:28:20 -04003983 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04003984 if (err)
3985 goto free;
3986
Vivien Didelotfad09c72016-06-21 12:28:20 -04003987 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04003988 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003989 goto free;
Vivien Didelota439c062016-04-17 13:23:58 -04003990
Vivien Didelote57e5e72016-08-15 17:19:00 -04003991 mv88e6xxx_phy_init(chip);
3992
Vivien Didelotfad09c72016-06-21 12:28:20 -04003993 err = mv88e6xxx_mdio_register(chip, NULL);
Andrew Lunnb516d452016-06-04 21:17:06 +02003994 if (err)
Vivien Didelot469d7292016-06-20 13:14:06 -04003995 goto free;
Andrew Lunnb516d452016-06-04 21:17:06 +02003996
Vivien Didelotfad09c72016-06-21 12:28:20 -04003997 *priv = chip;
Vivien Didelota439c062016-04-17 13:23:58 -04003998
Vivien Didelotfad09c72016-06-21 12:28:20 -04003999 return chip->info->name;
Vivien Didelot469d7292016-06-20 13:14:06 -04004000free:
Vivien Didelotfad09c72016-06-21 12:28:20 -04004001 devm_kfree(dsa_dev, chip);
Vivien Didelot469d7292016-06-20 13:14:06 -04004002
4003 return NULL;
Andrew Lunna77d43f2016-04-13 02:40:42 +02004004}
4005
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004006static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
4007 const struct switchdev_obj_port_mdb *mdb,
4008 struct switchdev_trans *trans)
4009{
4010 /* We don't need any dynamic resource from the kernel (yet),
4011 * so skip the prepare phase.
4012 */
4013
4014 return 0;
4015}
4016
4017static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
4018 const struct switchdev_obj_port_mdb *mdb,
4019 struct switchdev_trans *trans)
4020{
Vivien Didelot04bed142016-08-31 18:06:13 -04004021 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004022
4023 mutex_lock(&chip->reg_lock);
4024 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
4025 GLOBAL_ATU_DATA_STATE_MC_STATIC))
4026 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
4027 mutex_unlock(&chip->reg_lock);
4028}
4029
4030static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
4031 const struct switchdev_obj_port_mdb *mdb)
4032{
Vivien Didelot04bed142016-08-31 18:06:13 -04004033 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004034 int err;
4035
4036 mutex_lock(&chip->reg_lock);
4037 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
4038 GLOBAL_ATU_DATA_STATE_UNUSED);
4039 mutex_unlock(&chip->reg_lock);
4040
4041 return err;
4042}
4043
4044static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
4045 struct switchdev_obj_port_mdb *mdb,
4046 int (*cb)(struct switchdev_obj *obj))
4047{
Vivien Didelot04bed142016-08-31 18:06:13 -04004048 struct mv88e6xxx_chip *chip = ds->priv;
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004049 int err;
4050
4051 mutex_lock(&chip->reg_lock);
4052 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
4053 mutex_unlock(&chip->reg_lock);
4054
4055 return err;
4056}
4057
Vivien Didelot9d490b42016-08-23 12:38:56 -04004058static struct dsa_switch_ops mv88e6xxx_switch_ops = {
Andrew Lunnfcdce7d2016-05-10 23:27:20 +02004059 .probe = mv88e6xxx_drv_probe,
Andrew Lunn7b314362016-08-22 16:01:01 +02004060 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004061 .setup = mv88e6xxx_setup,
4062 .set_addr = mv88e6xxx_set_addr,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004063 .adjust_link = mv88e6xxx_adjust_link,
4064 .get_strings = mv88e6xxx_get_strings,
4065 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
4066 .get_sset_count = mv88e6xxx_get_sset_count,
4067 .set_eee = mv88e6xxx_set_eee,
4068 .get_eee = mv88e6xxx_get_eee,
4069#ifdef CONFIG_NET_DSA_HWMON
4070 .get_temp = mv88e6xxx_get_temp,
4071 .get_temp_limit = mv88e6xxx_get_temp_limit,
4072 .set_temp_limit = mv88e6xxx_set_temp_limit,
4073 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
4074#endif
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004075 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004076 .get_eeprom = mv88e6xxx_get_eeprom,
4077 .set_eeprom = mv88e6xxx_set_eeprom,
4078 .get_regs_len = mv88e6xxx_get_regs_len,
4079 .get_regs = mv88e6xxx_get_regs,
Vivien Didelot2cfcd962016-07-18 20:45:40 -04004080 .set_ageing_time = mv88e6xxx_set_ageing_time,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004081 .port_bridge_join = mv88e6xxx_port_bridge_join,
4082 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
4083 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
4084 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
4085 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
4086 .port_vlan_add = mv88e6xxx_port_vlan_add,
4087 .port_vlan_del = mv88e6xxx_port_vlan_del,
4088 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
4089 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
4090 .port_fdb_add = mv88e6xxx_port_fdb_add,
4091 .port_fdb_del = mv88e6xxx_port_fdb_del,
4092 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
Vivien Didelot7df8fbd2016-08-31 11:50:05 -04004093 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
4094 .port_mdb_add = mv88e6xxx_port_mdb_add,
4095 .port_mdb_del = mv88e6xxx_port_mdb_del,
4096 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
Vivien Didelotf81ec902016-05-09 13:22:58 -04004097};
4098
Vivien Didelotfad09c72016-06-21 12:28:20 -04004099static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004100 struct device_node *np)
4101{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004102 struct device *dev = chip->dev;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004103 struct dsa_switch *ds;
4104
4105 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4106 if (!ds)
4107 return -ENOMEM;
4108
4109 ds->dev = dev;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004110 ds->priv = chip;
Vivien Didelot9d490b42016-08-23 12:38:56 -04004111 ds->ops = &mv88e6xxx_switch_ops;
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004112
4113 dev_set_drvdata(dev, ds);
4114
4115 return dsa_register_switch(ds, np);
4116}
4117
Vivien Didelotfad09c72016-06-21 12:28:20 -04004118static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004119{
Vivien Didelotfad09c72016-06-21 12:28:20 -04004120 dsa_unregister_switch(chip->ds);
Vivien Didelotb7e66a52016-06-20 13:14:02 -04004121}
4122
Vivien Didelot57d32312016-06-20 13:13:58 -04004123static int mv88e6xxx_probe(struct mdio_device *mdiodev)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004124{
4125 struct device *dev = &mdiodev->dev;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004126 struct device_node *np = dev->of_node;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004127 const struct mv88e6xxx_info *compat_info;
Vivien Didelotfad09c72016-06-21 12:28:20 -04004128 struct mv88e6xxx_chip *chip;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004129 u32 eeprom_len;
Andrew Lunn52638f72016-05-10 23:27:22 +02004130 int err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004131
Vivien Didelotcaac8542016-06-20 13:14:09 -04004132 compat_info = of_device_get_match_data(dev);
4133 if (!compat_info)
4134 return -EINVAL;
4135
Vivien Didelotfad09c72016-06-21 12:28:20 -04004136 chip = mv88e6xxx_alloc_chip(dev);
4137 if (!chip)
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004138 return -ENOMEM;
4139
Vivien Didelotfad09c72016-06-21 12:28:20 -04004140 chip->info = compat_info;
Vivien Didelotcaac8542016-06-20 13:14:09 -04004141
Vivien Didelotfad09c72016-06-21 12:28:20 -04004142 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
Vivien Didelot4a70c4a2016-06-20 13:14:07 -04004143 if (err)
4144 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004145
Vivien Didelotfad09c72016-06-21 12:28:20 -04004146 err = mv88e6xxx_detect(chip);
Vivien Didelotbc46a3d2016-06-20 13:14:08 -04004147 if (err)
4148 return err;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004149
Vivien Didelote57e5e72016-08-15 17:19:00 -04004150 mv88e6xxx_phy_init(chip);
4151
Vivien Didelotfad09c72016-06-21 12:28:20 -04004152 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4153 if (IS_ERR(chip->reset))
4154 return PTR_ERR(chip->reset);
Andrew Lunn52638f72016-05-10 23:27:22 +02004155
Vivien Didelot855b1932016-07-20 18:18:35 -04004156 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004157 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
Vivien Didelotfad09c72016-06-21 12:28:20 -04004158 chip->eeprom_len = eeprom_len;
Andrew Lunnf8cd8752016-05-10 23:27:25 +02004159
Vivien Didelotfad09c72016-06-21 12:28:20 -04004160 err = mv88e6xxx_mdio_register(chip, np);
Andrew Lunnb516d452016-06-04 21:17:06 +02004161 if (err)
4162 return err;
4163
Vivien Didelotfad09c72016-06-21 12:28:20 -04004164 err = mv88e6xxx_register_switch(chip, np);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004165 if (err) {
Vivien Didelotfad09c72016-06-21 12:28:20 -04004166 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn83c0afa2016-06-04 21:17:07 +02004167 return err;
4168 }
4169
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004170 return 0;
4171}
4172
4173static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4174{
4175 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
Vivien Didelot04bed142016-08-31 18:06:13 -04004176 struct mv88e6xxx_chip *chip = ds->priv;
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004177
Andrew Lunn930188c2016-08-22 16:01:03 +02004178 mv88e6xxx_phy_destroy(chip);
Vivien Didelotfad09c72016-06-21 12:28:20 -04004179 mv88e6xxx_unregister_switch(chip);
4180 mv88e6xxx_mdio_unregister(chip);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004181}
4182
4183static const struct of_device_id mv88e6xxx_of_match[] = {
Vivien Didelotcaac8542016-06-20 13:14:09 -04004184 {
4185 .compatible = "marvell,mv88e6085",
4186 .data = &mv88e6xxx_table[MV88E6085],
4187 },
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004188 { /* sentinel */ },
4189};
4190
4191MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4192
4193static struct mdio_driver mv88e6xxx_driver = {
4194 .probe = mv88e6xxx_probe,
4195 .remove = mv88e6xxx_remove,
4196 .mdiodrv.driver = {
4197 .name = "mv88e6085",
4198 .of_match_table = mv88e6xxx_of_match,
4199 },
4200};
4201
Ben Hutchings98e67302011-11-25 14:36:19 +00004202static int __init mv88e6xxx_init(void)
4203{
Vivien Didelot9d490b42016-08-23 12:38:56 -04004204 register_switch_driver(&mv88e6xxx_switch_ops);
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004205 return mdio_driver_register(&mv88e6xxx_driver);
Ben Hutchings98e67302011-11-25 14:36:19 +00004206}
4207module_init(mv88e6xxx_init);
4208
4209static void __exit mv88e6xxx_cleanup(void)
4210{
Andrew Lunn14c7b3c2016-05-10 23:27:21 +02004211 mdio_driver_unregister(&mv88e6xxx_driver);
Vivien Didelot9d490b42016-08-23 12:38:56 -04004212 unregister_switch_driver(&mv88e6xxx_switch_ops);
Ben Hutchings98e67302011-11-25 14:36:19 +00004213}
4214module_exit(mv88e6xxx_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +00004215
4216MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4217MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4218MODULE_LICENSE("GPL");