Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1 | /* |
Vivien Didelot | 0d3cd4b | 2016-06-21 12:28:19 -0400 | [diff] [blame^] | 2 | * Marvell 88e6xxx Ethernet switch single-chip support |
| 3 | * |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 4 | * Copyright (c) 2008 Marvell Semiconductor |
| 5 | * |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 6 | * Copyright (c) 2015 CMC Electronics, Inc. |
| 7 | * Added support for VLAN Table Unit operations |
| 8 | * |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 9 | * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch> |
| 10 | * |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 11 | * 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 Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 17 | #include <linux/delay.h> |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 18 | #include <linux/etherdevice.h> |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 19 | #include <linux/ethtool.h> |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 20 | #include <linux/if_bridge.h> |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 21 | #include <linux/jiffies.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 22 | #include <linux/list.h> |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 23 | #include <linux/mdio.h> |
Paul Gortmaker | 2bbba27 | 2012-01-24 10:41:40 +0000 | [diff] [blame] | 24 | #include <linux/module.h> |
Vivien Didelot | caac854 | 2016-06-20 13:14:09 -0400 | [diff] [blame] | 25 | #include <linux/of_device.h> |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 26 | #include <linux/of_mdio.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 27 | #include <linux/netdevice.h> |
Andrew Lunn | c8c1b39a | 2015-11-20 03:56:24 +0100 | [diff] [blame] | 28 | #include <linux/gpio/consumer.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 29 | #include <linux/phy.h> |
Ben Hutchings | c8f0b86 | 2011-11-27 17:06:08 +0000 | [diff] [blame] | 30 | #include <net/dsa.h> |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 31 | #include <net/switchdev.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 32 | #include "mv88e6xxx.h" |
| 33 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 34 | static void assert_reg_lock(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 35 | { |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 36 | if (unlikely(!mutex_is_locked(&ps->reg_lock))) { |
| 37 | dev_err(ps->dev, "Switch registers lock not held!\n"); |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 38 | dump_stack(); |
| 39 | } |
| 40 | } |
| 41 | |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 42 | /* 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 Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 52 | */ |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 53 | |
| 54 | static int mv88e6xxx_smi_read(struct mv88e6xxx_priv_state *ps, |
| 55 | int addr, int reg, u16 *val) |
| 56 | { |
| 57 | if (!ps->smi_ops) |
| 58 | return -EOPNOTSUPP; |
| 59 | |
| 60 | return ps->smi_ops->read(ps, addr, reg, val); |
| 61 | } |
| 62 | |
| 63 | static int mv88e6xxx_smi_write(struct mv88e6xxx_priv_state *ps, |
| 64 | int addr, int reg, u16 val) |
| 65 | { |
| 66 | if (!ps->smi_ops) |
| 67 | return -EOPNOTSUPP; |
| 68 | |
| 69 | return ps->smi_ops->write(ps, addr, reg, val); |
| 70 | } |
| 71 | |
| 72 | static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_priv_state *ps, |
| 73 | int addr, int reg, u16 *val) |
| 74 | { |
| 75 | int ret; |
| 76 | |
| 77 | ret = mdiobus_read_nested(ps->bus, addr, reg); |
| 78 | if (ret < 0) |
| 79 | return ret; |
| 80 | |
| 81 | *val = ret & 0xffff; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_priv_state *ps, |
| 87 | int addr, int reg, u16 val) |
| 88 | { |
| 89 | int ret; |
| 90 | |
| 91 | ret = mdiobus_write_nested(ps->bus, addr, reg, val); |
| 92 | if (ret < 0) |
| 93 | return ret; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static 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 | |
| 103 | static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 104 | { |
| 105 | int ret; |
| 106 | int i; |
| 107 | |
| 108 | for (i = 0; i < 16; i++) { |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 109 | ret = mdiobus_read_nested(ps->bus, ps->sw_addr, SMI_CMD); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 110 | if (ret < 0) |
| 111 | return ret; |
| 112 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 113 | if ((ret & SMI_CMD_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | return -ETIMEDOUT; |
| 118 | } |
| 119 | |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 120 | static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_priv_state *ps, |
| 121 | int addr, int reg, u16 *val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 122 | { |
| 123 | int ret; |
| 124 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 125 | /* Wait for the bus to become free. */ |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 126 | ret = mv88e6xxx_smi_multi_chip_wait(ps); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 127 | if (ret < 0) |
| 128 | return ret; |
| 129 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 130 | /* Transmit the read command. */ |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 131 | ret = mdiobus_write_nested(ps->bus, ps->sw_addr, SMI_CMD, |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 132 | SMI_CMD_OP_22_READ | (addr << 5) | reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 133 | if (ret < 0) |
| 134 | return ret; |
| 135 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 136 | /* Wait for the read command to complete. */ |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 137 | ret = mv88e6xxx_smi_multi_chip_wait(ps); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 138 | if (ret < 0) |
| 139 | return ret; |
| 140 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 141 | /* Read the data. */ |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 142 | ret = mdiobus_read_nested(ps->bus, ps->sw_addr, SMI_DATA); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 143 | if (ret < 0) |
| 144 | return ret; |
| 145 | |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 146 | *val = ret & 0xffff; |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_priv_state *ps, |
| 152 | int addr, int reg, u16 val) |
| 153 | { |
| 154 | int ret; |
| 155 | |
| 156 | /* Wait for the bus to become free. */ |
| 157 | ret = mv88e6xxx_smi_multi_chip_wait(ps); |
| 158 | if (ret < 0) |
| 159 | return ret; |
| 160 | |
| 161 | /* Transmit the data to write. */ |
| 162 | ret = mdiobus_write_nested(ps->bus, ps->sw_addr, SMI_DATA, val); |
| 163 | if (ret < 0) |
| 164 | return ret; |
| 165 | |
| 166 | /* Transmit the write command. */ |
| 167 | ret = mdiobus_write_nested(ps->bus, ps->sw_addr, SMI_CMD, |
| 168 | SMI_CMD_OP_22_WRITE | (addr << 5) | reg); |
| 169 | if (ret < 0) |
| 170 | return ret; |
| 171 | |
| 172 | /* Wait for the write command to complete. */ |
| 173 | ret = mv88e6xxx_smi_multi_chip_wait(ps); |
| 174 | if (ret < 0) |
| 175 | return ret; |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static 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 | |
| 185 | static int mv88e6xxx_read(struct mv88e6xxx_priv_state *ps, |
| 186 | int addr, int reg, u16 *val) |
| 187 | { |
| 188 | int err; |
| 189 | |
| 190 | assert_reg_lock(ps); |
| 191 | |
| 192 | err = mv88e6xxx_smi_read(ps, addr, reg, val); |
| 193 | if (err) |
| 194 | return err; |
| 195 | |
| 196 | dev_dbg(ps->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 197 | addr, reg, *val); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int mv88e6xxx_write(struct mv88e6xxx_priv_state *ps, |
| 203 | int addr, int reg, u16 val) |
| 204 | { |
| 205 | int err; |
| 206 | |
| 207 | assert_reg_lock(ps); |
| 208 | |
| 209 | err = mv88e6xxx_smi_write(ps, addr, reg, val); |
| 210 | if (err) |
| 211 | return err; |
| 212 | |
| 213 | dev_dbg(ps->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 214 | addr, reg, val); |
| 215 | |
| 216 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 219 | static int _mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, |
| 220 | int addr, int reg) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 221 | { |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 222 | u16 val; |
| 223 | int err; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 224 | |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 225 | err = mv88e6xxx_read(ps, addr, reg, &val); |
| 226 | if (err) |
| 227 | return err; |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 228 | |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 229 | return val; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 232 | static int mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, int addr, |
| 233 | int reg) |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 234 | { |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 235 | int ret; |
| 236 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 237 | mutex_lock(&ps->reg_lock); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 238 | ret = _mv88e6xxx_reg_read(ps, addr, reg); |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 239 | mutex_unlock(&ps->reg_lock); |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 244 | static int _mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr, |
| 245 | int reg, u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 246 | { |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 247 | return mv88e6xxx_write(ps, addr, reg, val); |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 250 | static int mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr, |
| 251 | int reg, u16 val) |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 252 | { |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 253 | int ret; |
| 254 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 255 | mutex_lock(&ps->reg_lock); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 256 | ret = _mv88e6xxx_reg_write(ps, addr, reg, val); |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 257 | mutex_unlock(&ps->reg_lock); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
Vivien Didelot | 1d13a06 | 2016-05-09 13:22:43 -0400 | [diff] [blame] | 262 | static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 263 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 264 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 265 | int err; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 266 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 267 | err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_01, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 268 | (addr[0] << 8) | addr[1]); |
| 269 | if (err) |
| 270 | return err; |
| 271 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 272 | err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_23, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 273 | (addr[2] << 8) | addr[3]); |
| 274 | if (err) |
| 275 | return err; |
| 276 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 277 | return mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_45, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 278 | (addr[4] << 8) | addr[5]); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Vivien Didelot | 1d13a06 | 2016-05-09 13:22:43 -0400 | [diff] [blame] | 281 | static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 282 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 283 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 284 | int ret; |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 285 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 286 | |
| 287 | for (i = 0; i < 6; i++) { |
| 288 | int j; |
| 289 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 290 | /* Write the MAC address byte. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 291 | ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 292 | GLOBAL2_SWITCH_MAC_BUSY | |
| 293 | (i << 8) | addr[i]); |
| 294 | if (ret) |
| 295 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 296 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 297 | /* Wait for the write to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 298 | for (j = 0; j < 16; j++) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 299 | ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 300 | GLOBAL2_SWITCH_MAC); |
| 301 | if (ret < 0) |
| 302 | return ret; |
| 303 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 304 | if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 305 | break; |
| 306 | } |
| 307 | if (j == 16) |
| 308 | return -ETIMEDOUT; |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 314 | static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr) |
Vivien Didelot | 1d13a06 | 2016-05-09 13:22:43 -0400 | [diff] [blame] | 315 | { |
| 316 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 317 | |
| 318 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SWITCH_MAC)) |
| 319 | return mv88e6xxx_set_addr_indirect(ds, addr); |
| 320 | else |
| 321 | return mv88e6xxx_set_addr_direct(ds, addr); |
| 322 | } |
| 323 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 324 | static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_priv_state *ps, |
| 325 | int addr, int regnum) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 326 | { |
| 327 | if (addr >= 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 328 | return _mv88e6xxx_reg_read(ps, addr, regnum); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 329 | return 0xffff; |
| 330 | } |
| 331 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 332 | static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_priv_state *ps, |
| 333 | int addr, int regnum, u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 334 | { |
| 335 | if (addr >= 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 336 | return _mv88e6xxx_reg_write(ps, addr, regnum, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 340 | static int mv88e6xxx_ppu_disable(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 341 | { |
| 342 | int ret; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 343 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 344 | |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 345 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 346 | if (ret < 0) |
| 347 | return ret; |
| 348 | |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 349 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, |
| 350 | ret & ~GLOBAL_CONTROL_PPU_ENABLE); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 351 | if (ret) |
| 352 | return ret; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 353 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 354 | timeout = jiffies + 1 * HZ; |
| 355 | while (time_before(jiffies, timeout)) { |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 356 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 357 | if (ret < 0) |
| 358 | return ret; |
| 359 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 360 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 361 | if ((ret & GLOBAL_STATUS_PPU_MASK) != |
| 362 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 363 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | return -ETIMEDOUT; |
| 367 | } |
| 368 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 369 | static int mv88e6xxx_ppu_enable(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 370 | { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 371 | int ret, err; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 372 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 373 | |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 374 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 375 | if (ret < 0) |
| 376 | return ret; |
| 377 | |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 378 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, |
| 379 | ret | GLOBAL_CONTROL_PPU_ENABLE); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 380 | if (err) |
| 381 | return err; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 382 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 383 | timeout = jiffies + 1 * HZ; |
| 384 | while (time_before(jiffies, timeout)) { |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 385 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 386 | if (ret < 0) |
| 387 | return ret; |
| 388 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 389 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 390 | if ((ret & GLOBAL_STATUS_PPU_MASK) == |
| 391 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 392 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | return -ETIMEDOUT; |
| 396 | } |
| 397 | |
| 398 | static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly) |
| 399 | { |
| 400 | struct mv88e6xxx_priv_state *ps; |
| 401 | |
| 402 | ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work); |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 403 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 404 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 405 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 406 | if (mutex_trylock(&ps->ppu_mutex)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 407 | if (mv88e6xxx_ppu_enable(ps) == 0) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 408 | ps->ppu_disabled = 0; |
| 409 | mutex_unlock(&ps->ppu_mutex); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 410 | } |
Vivien Didelot | 762eb67 | 2016-06-04 21:16:54 +0200 | [diff] [blame] | 411 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 412 | mutex_unlock(&ps->reg_lock); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps) |
| 416 | { |
| 417 | struct mv88e6xxx_priv_state *ps = (void *)_ps; |
| 418 | |
| 419 | schedule_work(&ps->ppu_work); |
| 420 | } |
| 421 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 422 | static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 423 | { |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 424 | int ret; |
| 425 | |
| 426 | mutex_lock(&ps->ppu_mutex); |
| 427 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 428 | /* If the PHY polling unit is enabled, disable it so that |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 429 | * we can access the PHY registers. If it was already |
| 430 | * disabled, cancel the timer that is going to re-enable |
| 431 | * it. |
| 432 | */ |
| 433 | if (!ps->ppu_disabled) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 434 | ret = mv88e6xxx_ppu_disable(ps); |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 435 | if (ret < 0) { |
| 436 | mutex_unlock(&ps->ppu_mutex); |
| 437 | return ret; |
| 438 | } |
| 439 | ps->ppu_disabled = 1; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 440 | } else { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 441 | del_timer(&ps->ppu_timer); |
| 442 | ret = 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 448 | static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 449 | { |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 450 | /* Schedule a timer to re-enable the PHY polling unit. */ |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 451 | mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10)); |
| 452 | mutex_unlock(&ps->ppu_mutex); |
| 453 | } |
| 454 | |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 455 | static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 456 | { |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 457 | mutex_init(&ps->ppu_mutex); |
| 458 | INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work); |
| 459 | init_timer(&ps->ppu_timer); |
| 460 | ps->ppu_timer.data = (unsigned long)ps; |
| 461 | ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer; |
| 462 | } |
| 463 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 464 | static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_priv_state *ps, int addr, |
| 465 | int regnum) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 466 | { |
| 467 | int ret; |
| 468 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 469 | ret = mv88e6xxx_ppu_access_get(ps); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 470 | if (ret >= 0) { |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 471 | ret = _mv88e6xxx_reg_read(ps, addr, regnum); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 472 | mv88e6xxx_ppu_access_put(ps); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 478 | static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_priv_state *ps, int addr, |
| 479 | int regnum, u16 val) |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 480 | { |
| 481 | int ret; |
| 482 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 483 | ret = mv88e6xxx_ppu_access_get(ps); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 484 | if (ret >= 0) { |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 485 | ret = _mv88e6xxx_reg_write(ps, addr, regnum, val); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 486 | mv88e6xxx_ppu_access_put(ps); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | return ret; |
| 490 | } |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 491 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 492 | static bool mv88e6xxx_6065_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 493 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 494 | return ps->info->family == MV88E6XXX_FAMILY_6065; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 497 | static bool mv88e6xxx_6095_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 498 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 499 | return ps->info->family == MV88E6XXX_FAMILY_6095; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 502 | static bool mv88e6xxx_6097_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 503 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 504 | return ps->info->family == MV88E6XXX_FAMILY_6097; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 507 | static bool mv88e6xxx_6165_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 508 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 509 | return ps->info->family == MV88E6XXX_FAMILY_6165; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 510 | } |
| 511 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 512 | static bool mv88e6xxx_6185_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 513 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 514 | return ps->info->family == MV88E6XXX_FAMILY_6185; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 517 | static bool mv88e6xxx_6320_family(struct mv88e6xxx_priv_state *ps) |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 518 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 519 | return ps->info->family == MV88E6XXX_FAMILY_6320; |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 522 | static bool mv88e6xxx_6351_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 523 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 524 | return ps->info->family == MV88E6XXX_FAMILY_6351; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 527 | static bool mv88e6xxx_6352_family(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 528 | { |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 529 | return ps->info->family == MV88E6XXX_FAMILY_6352; |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 532 | static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 533 | { |
Vivien Didelot | cd5a2c8 | 2016-04-17 13:24:02 -0400 | [diff] [blame] | 534 | return ps->info->num_databases; |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 535 | } |
| 536 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 537 | static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 538 | { |
| 539 | /* Does the device have dedicated FID registers for ATU and VTU ops? */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 540 | if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) || |
| 541 | mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 542 | return true; |
| 543 | |
| 544 | return false; |
| 545 | } |
| 546 | |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 547 | /* We expect the switch to perform auto negotiation if there is a real |
| 548 | * phy. However, in the case of a fixed link phy, we force the port |
| 549 | * settings from the fixed link settings. |
| 550 | */ |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 551 | static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port, |
| 552 | struct phy_device *phydev) |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 553 | { |
| 554 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 4905287 | 2015-09-29 01:53:48 +0200 | [diff] [blame] | 555 | u32 reg; |
| 556 | int ret; |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 557 | |
| 558 | if (!phy_is_pseudo_fixed_link(phydev)) |
| 559 | return; |
| 560 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 561 | mutex_lock(&ps->reg_lock); |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 562 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 563 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL); |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 564 | if (ret < 0) |
| 565 | goto out; |
| 566 | |
| 567 | reg = ret & ~(PORT_PCS_CTRL_LINK_UP | |
| 568 | PORT_PCS_CTRL_FORCE_LINK | |
| 569 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 570 | PORT_PCS_CTRL_FORCE_DUPLEX | |
| 571 | PORT_PCS_CTRL_UNFORCED); |
| 572 | |
| 573 | reg |= PORT_PCS_CTRL_FORCE_LINK; |
| 574 | if (phydev->link) |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 575 | reg |= PORT_PCS_CTRL_LINK_UP; |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 576 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 577 | if (mv88e6xxx_6065_family(ps) && phydev->speed > SPEED_100) |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 578 | goto out; |
| 579 | |
| 580 | switch (phydev->speed) { |
| 581 | case SPEED_1000: |
| 582 | reg |= PORT_PCS_CTRL_1000; |
| 583 | break; |
| 584 | case SPEED_100: |
| 585 | reg |= PORT_PCS_CTRL_100; |
| 586 | break; |
| 587 | case SPEED_10: |
| 588 | reg |= PORT_PCS_CTRL_10; |
| 589 | break; |
| 590 | default: |
| 591 | pr_info("Unknown speed"); |
| 592 | goto out; |
| 593 | } |
| 594 | |
| 595 | reg |= PORT_PCS_CTRL_FORCE_DUPLEX; |
| 596 | if (phydev->duplex == DUPLEX_FULL) |
| 597 | reg |= PORT_PCS_CTRL_DUPLEX_FULL; |
| 598 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 599 | if ((mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps)) && |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 600 | (port >= ps->info->num_ports - 2)) { |
Andrew Lunn | e7e72ac | 2015-08-31 15:56:51 +0200 | [diff] [blame] | 601 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) |
| 602 | reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK; |
| 603 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) |
| 604 | reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK; |
| 605 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) |
| 606 | reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK | |
| 607 | PORT_PCS_CTRL_RGMII_DELAY_TXCLK); |
| 608 | } |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 609 | _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_PCS_CTRL, reg); |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 610 | |
| 611 | out: |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 612 | mutex_unlock(&ps->reg_lock); |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 613 | } |
| 614 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 615 | static int _mv88e6xxx_stats_wait(struct mv88e6xxx_priv_state *ps) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 616 | { |
| 617 | int ret; |
| 618 | int i; |
| 619 | |
| 620 | for (i = 0; i < 10; i++) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 621 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_OP); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 622 | if ((ret & GLOBAL_STATS_OP_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | return -ETIMEDOUT; |
| 627 | } |
| 628 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 629 | static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_priv_state *ps, |
| 630 | int port) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 631 | { |
| 632 | int ret; |
| 633 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 634 | if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps)) |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 635 | port = (port + 1) << 5; |
| 636 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 637 | /* Snapshot the hardware statistics counters for this port. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 638 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP, |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 639 | GLOBAL_STATS_OP_CAPTURE_PORT | |
| 640 | GLOBAL_STATS_OP_HIST_RX_TX | port); |
| 641 | if (ret < 0) |
| 642 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 643 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 644 | /* Wait for the snapshotting to complete. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 645 | ret = _mv88e6xxx_stats_wait(ps); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 646 | if (ret < 0) |
| 647 | return ret; |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 652 | static void _mv88e6xxx_stats_read(struct mv88e6xxx_priv_state *ps, |
| 653 | int stat, u32 *val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 654 | { |
| 655 | u32 _val; |
| 656 | int ret; |
| 657 | |
| 658 | *val = 0; |
| 659 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 660 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP, |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 661 | GLOBAL_STATS_OP_READ_CAPTURED | |
| 662 | GLOBAL_STATS_OP_HIST_RX_TX | stat); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 663 | if (ret < 0) |
| 664 | return; |
| 665 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 666 | ret = _mv88e6xxx_stats_wait(ps); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 667 | if (ret < 0) |
| 668 | return; |
| 669 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 670 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_32); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 671 | if (ret < 0) |
| 672 | return; |
| 673 | |
| 674 | _val = ret << 16; |
| 675 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 676 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_01); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 677 | if (ret < 0) |
| 678 | return; |
| 679 | |
| 680 | *val = _val | ret; |
| 681 | } |
| 682 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 683 | static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = { |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 684 | { "in_good_octets", 8, 0x00, BANK0, }, |
| 685 | { "in_bad_octets", 4, 0x02, BANK0, }, |
| 686 | { "in_unicast", 4, 0x04, BANK0, }, |
| 687 | { "in_broadcasts", 4, 0x06, BANK0, }, |
| 688 | { "in_multicasts", 4, 0x07, BANK0, }, |
| 689 | { "in_pause", 4, 0x16, BANK0, }, |
| 690 | { "in_undersize", 4, 0x18, BANK0, }, |
| 691 | { "in_fragments", 4, 0x19, BANK0, }, |
| 692 | { "in_oversize", 4, 0x1a, BANK0, }, |
| 693 | { "in_jabber", 4, 0x1b, BANK0, }, |
| 694 | { "in_rx_error", 4, 0x1c, BANK0, }, |
| 695 | { "in_fcs_error", 4, 0x1d, BANK0, }, |
| 696 | { "out_octets", 8, 0x0e, BANK0, }, |
| 697 | { "out_unicast", 4, 0x10, BANK0, }, |
| 698 | { "out_broadcasts", 4, 0x13, BANK0, }, |
| 699 | { "out_multicasts", 4, 0x12, BANK0, }, |
| 700 | { "out_pause", 4, 0x15, BANK0, }, |
| 701 | { "excessive", 4, 0x11, BANK0, }, |
| 702 | { "collisions", 4, 0x1e, BANK0, }, |
| 703 | { "deferred", 4, 0x05, BANK0, }, |
| 704 | { "single", 4, 0x14, BANK0, }, |
| 705 | { "multiple", 4, 0x17, BANK0, }, |
| 706 | { "out_fcs_error", 4, 0x03, BANK0, }, |
| 707 | { "late", 4, 0x1f, BANK0, }, |
| 708 | { "hist_64bytes", 4, 0x08, BANK0, }, |
| 709 | { "hist_65_127bytes", 4, 0x09, BANK0, }, |
| 710 | { "hist_128_255bytes", 4, 0x0a, BANK0, }, |
| 711 | { "hist_256_511bytes", 4, 0x0b, BANK0, }, |
| 712 | { "hist_512_1023bytes", 4, 0x0c, BANK0, }, |
| 713 | { "hist_1024_max_bytes", 4, 0x0d, BANK0, }, |
| 714 | { "sw_in_discards", 4, 0x10, PORT, }, |
| 715 | { "sw_in_filtered", 2, 0x12, PORT, }, |
| 716 | { "sw_out_filtered", 2, 0x13, PORT, }, |
| 717 | { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 718 | { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 719 | { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 720 | { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 721 | { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 722 | { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 723 | { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 724 | { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 725 | { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 726 | { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 727 | { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 728 | { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 729 | { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 730 | { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 731 | { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 732 | { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 733 | { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 734 | { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 735 | { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 736 | { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 737 | { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 738 | { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 739 | { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 740 | { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 741 | { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 742 | { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 743 | }; |
| 744 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 745 | static bool mv88e6xxx_has_stat(struct mv88e6xxx_priv_state *ps, |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 746 | struct mv88e6xxx_hw_stat *stat) |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 747 | { |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 748 | switch (stat->type) { |
| 749 | case BANK0: |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 750 | return true; |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 751 | case BANK1: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 752 | return mv88e6xxx_6320_family(ps); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 753 | case PORT: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 754 | return mv88e6xxx_6095_family(ps) || |
| 755 | mv88e6xxx_6185_family(ps) || |
| 756 | mv88e6xxx_6097_family(ps) || |
| 757 | mv88e6xxx_6165_family(ps) || |
| 758 | mv88e6xxx_6351_family(ps) || |
| 759 | mv88e6xxx_6352_family(ps); |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 760 | } |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 761 | return false; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 764 | static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_priv_state *ps, |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 765 | struct mv88e6xxx_hw_stat *s, |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 766 | int port) |
| 767 | { |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 768 | u32 low; |
| 769 | u32 high = 0; |
| 770 | int ret; |
| 771 | u64 value; |
| 772 | |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 773 | switch (s->type) { |
| 774 | case PORT: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 775 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), s->reg); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 776 | if (ret < 0) |
| 777 | return UINT64_MAX; |
| 778 | |
| 779 | low = ret; |
| 780 | if (s->sizeof_stat == 4) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 781 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 782 | s->reg + 1); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 783 | if (ret < 0) |
| 784 | return UINT64_MAX; |
| 785 | high = ret; |
| 786 | } |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 787 | break; |
| 788 | case BANK0: |
| 789 | case BANK1: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 790 | _mv88e6xxx_stats_read(ps, s->reg, &low); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 791 | if (s->sizeof_stat == 8) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 792 | _mv88e6xxx_stats_read(ps, s->reg + 1, &high); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 793 | } |
| 794 | value = (((u64)high) << 16) | low; |
| 795 | return value; |
| 796 | } |
| 797 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 798 | static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port, |
| 799 | uint8_t *data) |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 800 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 801 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 802 | struct mv88e6xxx_hw_stat *stat; |
| 803 | int i, j; |
| 804 | |
| 805 | for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) { |
| 806 | stat = &mv88e6xxx_hw_stats[i]; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 807 | if (mv88e6xxx_has_stat(ps, stat)) { |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 808 | memcpy(data + j * ETH_GSTRING_LEN, stat->string, |
| 809 | ETH_GSTRING_LEN); |
| 810 | j++; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 815 | static int mv88e6xxx_get_sset_count(struct dsa_switch *ds) |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 816 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 817 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 818 | struct mv88e6xxx_hw_stat *stat; |
| 819 | int i, j; |
| 820 | |
| 821 | for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) { |
| 822 | stat = &mv88e6xxx_hw_stats[i]; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 823 | if (mv88e6xxx_has_stat(ps, stat)) |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 824 | j++; |
| 825 | } |
| 826 | return j; |
| 827 | } |
| 828 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 829 | static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port, |
| 830 | uint64_t *data) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 831 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 832 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 833 | struct mv88e6xxx_hw_stat *stat; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 834 | int ret; |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 835 | int i, j; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 836 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 837 | mutex_lock(&ps->reg_lock); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 838 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 839 | ret = _mv88e6xxx_stats_snapshot(ps, port); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 840 | if (ret < 0) { |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 841 | mutex_unlock(&ps->reg_lock); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 842 | return; |
| 843 | } |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 844 | for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) { |
| 845 | stat = &mv88e6xxx_hw_stats[i]; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 846 | if (mv88e6xxx_has_stat(ps, stat)) { |
| 847 | data[j] = _mv88e6xxx_get_ethtool_stat(ps, stat, port); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 848 | j++; |
| 849 | } |
| 850 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 851 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 852 | mutex_unlock(&ps->reg_lock); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 853 | } |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 854 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 855 | static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port) |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 856 | { |
| 857 | return 32 * sizeof(u16); |
| 858 | } |
| 859 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 860 | static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port, |
| 861 | struct ethtool_regs *regs, void *_p) |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 862 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 863 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 864 | u16 *p = _p; |
| 865 | int i; |
| 866 | |
| 867 | regs->version = 0; |
| 868 | |
| 869 | memset(p, 0xff, 32 * sizeof(u16)); |
| 870 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 871 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 2306251 | 2016-05-09 13:22:45 -0400 | [diff] [blame] | 872 | |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 873 | for (i = 0; i < 32; i++) { |
| 874 | int ret; |
| 875 | |
Vivien Didelot | 2306251 | 2016-05-09 13:22:45 -0400 | [diff] [blame] | 876 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), i); |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 877 | if (ret >= 0) |
| 878 | p[i] = ret; |
| 879 | } |
Vivien Didelot | 2306251 | 2016-05-09 13:22:45 -0400 | [diff] [blame] | 880 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 881 | mutex_unlock(&ps->reg_lock); |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 884 | static int _mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, int offset, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 885 | u16 mask) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 886 | { |
| 887 | unsigned long timeout = jiffies + HZ / 10; |
| 888 | |
| 889 | while (time_before(jiffies, timeout)) { |
| 890 | int ret; |
| 891 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 892 | ret = _mv88e6xxx_reg_read(ps, reg, offset); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 893 | if (ret < 0) |
| 894 | return ret; |
| 895 | if (!(ret & mask)) |
| 896 | return 0; |
| 897 | |
| 898 | usleep_range(1000, 2000); |
| 899 | } |
| 900 | return -ETIMEDOUT; |
| 901 | } |
| 902 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 903 | static int mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, |
| 904 | int offset, u16 mask) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 905 | { |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 906 | int ret; |
| 907 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 908 | mutex_lock(&ps->reg_lock); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 909 | ret = _mv88e6xxx_wait(ps, reg, offset, mask); |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 910 | mutex_unlock(&ps->reg_lock); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 911 | |
| 912 | return ret; |
| 913 | } |
| 914 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 915 | static int mv88e6xxx_mdio_wait(struct mv88e6xxx_priv_state *ps) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 916 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 917 | return _mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_SMI_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 918 | GLOBAL2_SMI_OP_BUSY); |
| 919 | } |
| 920 | |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 921 | static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 922 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 923 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 924 | |
| 925 | return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 926 | GLOBAL2_EEPROM_OP_LOAD); |
| 927 | } |
| 928 | |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 929 | static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 930 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 931 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 932 | |
| 933 | return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 934 | GLOBAL2_EEPROM_OP_BUSY); |
| 935 | } |
| 936 | |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 937 | static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr) |
| 938 | { |
| 939 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 940 | int ret; |
| 941 | |
| 942 | mutex_lock(&ps->eeprom_mutex); |
| 943 | |
| 944 | ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 945 | GLOBAL2_EEPROM_OP_READ | |
| 946 | (addr & GLOBAL2_EEPROM_OP_ADDR_MASK)); |
| 947 | if (ret < 0) |
| 948 | goto error; |
| 949 | |
| 950 | ret = mv88e6xxx_eeprom_busy_wait(ds); |
| 951 | if (ret < 0) |
| 952 | goto error; |
| 953 | |
| 954 | ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA); |
| 955 | error: |
| 956 | mutex_unlock(&ps->eeprom_mutex); |
| 957 | return ret; |
| 958 | } |
| 959 | |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 960 | static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds) |
| 961 | { |
| 962 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 963 | |
| 964 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM)) |
| 965 | return ps->eeprom_len; |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 970 | static int mv88e6xxx_get_eeprom(struct dsa_switch *ds, |
| 971 | struct ethtool_eeprom *eeprom, u8 *data) |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 972 | { |
| 973 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 974 | int offset; |
| 975 | int len; |
| 976 | int ret; |
| 977 | |
| 978 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM)) |
| 979 | return -EOPNOTSUPP; |
| 980 | |
| 981 | offset = eeprom->offset; |
| 982 | len = eeprom->len; |
| 983 | eeprom->len = 0; |
| 984 | |
| 985 | eeprom->magic = 0xc3ec4951; |
| 986 | |
| 987 | ret = mv88e6xxx_eeprom_load_wait(ds); |
| 988 | if (ret < 0) |
| 989 | return ret; |
| 990 | |
| 991 | if (offset & 1) { |
| 992 | int word; |
| 993 | |
| 994 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 995 | if (word < 0) |
| 996 | return word; |
| 997 | |
| 998 | *data++ = (word >> 8) & 0xff; |
| 999 | |
| 1000 | offset++; |
| 1001 | len--; |
| 1002 | eeprom->len++; |
| 1003 | } |
| 1004 | |
| 1005 | while (len >= 2) { |
| 1006 | int word; |
| 1007 | |
| 1008 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 1009 | if (word < 0) |
| 1010 | return word; |
| 1011 | |
| 1012 | *data++ = word & 0xff; |
| 1013 | *data++ = (word >> 8) & 0xff; |
| 1014 | |
| 1015 | offset += 2; |
| 1016 | len -= 2; |
| 1017 | eeprom->len += 2; |
| 1018 | } |
| 1019 | |
| 1020 | if (len) { |
| 1021 | int word; |
| 1022 | |
| 1023 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 1024 | if (word < 0) |
| 1025 | return word; |
| 1026 | |
| 1027 | *data++ = word & 0xff; |
| 1028 | |
| 1029 | offset++; |
| 1030 | len--; |
| 1031 | eeprom->len++; |
| 1032 | } |
| 1033 | |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds) |
| 1038 | { |
| 1039 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1040 | int ret; |
| 1041 | |
| 1042 | ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP); |
| 1043 | if (ret < 0) |
| 1044 | return ret; |
| 1045 | |
| 1046 | if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN)) |
| 1047 | return -EROFS; |
| 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr, |
| 1053 | u16 data) |
| 1054 | { |
| 1055 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1056 | int ret; |
| 1057 | |
| 1058 | mutex_lock(&ps->eeprom_mutex); |
| 1059 | |
| 1060 | ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data); |
| 1061 | if (ret < 0) |
| 1062 | goto error; |
| 1063 | |
| 1064 | ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 1065 | GLOBAL2_EEPROM_OP_WRITE | |
| 1066 | (addr & GLOBAL2_EEPROM_OP_ADDR_MASK)); |
| 1067 | if (ret < 0) |
| 1068 | goto error; |
| 1069 | |
| 1070 | ret = mv88e6xxx_eeprom_busy_wait(ds); |
| 1071 | error: |
| 1072 | mutex_unlock(&ps->eeprom_mutex); |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 1076 | static int mv88e6xxx_set_eeprom(struct dsa_switch *ds, |
| 1077 | struct ethtool_eeprom *eeprom, u8 *data) |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 1078 | { |
| 1079 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1080 | int offset; |
| 1081 | int ret; |
| 1082 | int len; |
| 1083 | |
| 1084 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM)) |
| 1085 | return -EOPNOTSUPP; |
| 1086 | |
| 1087 | if (eeprom->magic != 0xc3ec4951) |
| 1088 | return -EINVAL; |
| 1089 | |
| 1090 | ret = mv88e6xxx_eeprom_is_readonly(ds); |
| 1091 | if (ret) |
| 1092 | return ret; |
| 1093 | |
| 1094 | offset = eeprom->offset; |
| 1095 | len = eeprom->len; |
| 1096 | eeprom->len = 0; |
| 1097 | |
| 1098 | ret = mv88e6xxx_eeprom_load_wait(ds); |
| 1099 | if (ret < 0) |
| 1100 | return ret; |
| 1101 | |
| 1102 | if (offset & 1) { |
| 1103 | int word; |
| 1104 | |
| 1105 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 1106 | if (word < 0) |
| 1107 | return word; |
| 1108 | |
| 1109 | word = (*data++ << 8) | (word & 0xff); |
| 1110 | |
| 1111 | ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word); |
| 1112 | if (ret < 0) |
| 1113 | return ret; |
| 1114 | |
| 1115 | offset++; |
| 1116 | len--; |
| 1117 | eeprom->len++; |
| 1118 | } |
| 1119 | |
| 1120 | while (len >= 2) { |
| 1121 | int word; |
| 1122 | |
| 1123 | word = *data++; |
| 1124 | word |= *data++ << 8; |
| 1125 | |
| 1126 | ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word); |
| 1127 | if (ret < 0) |
| 1128 | return ret; |
| 1129 | |
| 1130 | offset += 2; |
| 1131 | len -= 2; |
| 1132 | eeprom->len += 2; |
| 1133 | } |
| 1134 | |
| 1135 | if (len) { |
| 1136 | int word; |
| 1137 | |
| 1138 | word = mv88e6xxx_read_eeprom_word(ds, offset >> 1); |
| 1139 | if (word < 0) |
| 1140 | return word; |
| 1141 | |
| 1142 | word = (word & 0xff00) | *data++; |
| 1143 | |
| 1144 | ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word); |
| 1145 | if (ret < 0) |
| 1146 | return ret; |
| 1147 | |
| 1148 | offset++; |
| 1149 | len--; |
| 1150 | eeprom->len++; |
| 1151 | } |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1156 | static int _mv88e6xxx_atu_wait(struct mv88e6xxx_priv_state *ps) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1157 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1158 | return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_ATU_OP, |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1159 | GLOBAL_ATU_OP_BUSY); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1162 | static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_priv_state *ps, |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1163 | int addr, int regnum) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1164 | { |
| 1165 | int ret; |
| 1166 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1167 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1168 | GLOBAL2_SMI_OP_22_READ | (addr << 5) | |
| 1169 | regnum); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1170 | if (ret < 0) |
| 1171 | return ret; |
| 1172 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1173 | ret = mv88e6xxx_mdio_wait(ps); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1174 | if (ret < 0) |
| 1175 | return ret; |
| 1176 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1177 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA); |
| 1178 | |
| 1179 | return ret; |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1182 | static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_priv_state *ps, |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1183 | int addr, int regnum, u16 val) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1184 | { |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1185 | int ret; |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1186 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1187 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1188 | if (ret < 0) |
| 1189 | return ret; |
| 1190 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1191 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP, |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 1192 | GLOBAL2_SMI_OP_22_WRITE | (addr << 5) | |
| 1193 | regnum); |
| 1194 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1195 | return mv88e6xxx_mdio_wait(ps); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 1196 | } |
| 1197 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 1198 | static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, |
| 1199 | struct ethtool_eee *e) |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1200 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1201 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1202 | int reg; |
| 1203 | |
Vivien Didelot | aadbdb8 | 2016-05-09 13:22:44 -0400 | [diff] [blame] | 1204 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE)) |
| 1205 | return -EOPNOTSUPP; |
| 1206 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 1207 | mutex_lock(&ps->reg_lock); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1208 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1209 | reg = mv88e6xxx_mdio_read_indirect(ps, port, 16); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1210 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1211 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1212 | |
| 1213 | e->eee_enabled = !!(reg & 0x0200); |
| 1214 | e->tx_lpi_enabled = !!(reg & 0x0100); |
| 1215 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1216 | reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1217 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1218 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1219 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1220 | e->eee_active = !!(reg & PORT_STATUS_EEE); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1221 | reg = 0; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1222 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1223 | out: |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 1224 | mutex_unlock(&ps->reg_lock); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1225 | return reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1226 | } |
| 1227 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 1228 | static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port, |
| 1229 | struct phy_device *phydev, struct ethtool_eee *e) |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1230 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1231 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1232 | int reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1233 | int ret; |
| 1234 | |
Vivien Didelot | aadbdb8 | 2016-05-09 13:22:44 -0400 | [diff] [blame] | 1235 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE)) |
| 1236 | return -EOPNOTSUPP; |
| 1237 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 1238 | mutex_lock(&ps->reg_lock); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1239 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1240 | ret = mv88e6xxx_mdio_read_indirect(ps, port, 16); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1241 | if (ret < 0) |
| 1242 | goto out; |
| 1243 | |
| 1244 | reg = ret & ~0x0300; |
| 1245 | if (e->eee_enabled) |
| 1246 | reg |= 0x0200; |
| 1247 | if (e->tx_lpi_enabled) |
| 1248 | reg |= 0x0100; |
| 1249 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 1250 | ret = mv88e6xxx_mdio_write_indirect(ps, port, 16, reg); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1251 | out: |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 1252 | mutex_unlock(&ps->reg_lock); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 1253 | |
| 1254 | return ret; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1257 | static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_priv_state *ps, u16 fid, u16 cmd) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1258 | { |
| 1259 | int ret; |
| 1260 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1261 | if (mv88e6xxx_has_fid_reg(ps)) { |
| 1262 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_FID, fid); |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1263 | if (ret < 0) |
| 1264 | return ret; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1265 | } else if (mv88e6xxx_num_databases(ps) == 256) { |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1266 | /* ATU DBNum[7:4] are located in ATU Control 15:12 */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1267 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL); |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1268 | if (ret < 0) |
| 1269 | return ret; |
| 1270 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1271 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL, |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1272 | (ret & 0xfff) | |
| 1273 | ((fid << 8) & 0xf000)); |
| 1274 | if (ret < 0) |
| 1275 | return ret; |
| 1276 | |
| 1277 | /* ATU DBNum[3:0] are located in ATU Operation 3:0 */ |
| 1278 | cmd |= fid & 0xf; |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1281 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_OP, cmd); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1282 | if (ret < 0) |
| 1283 | return ret; |
| 1284 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1285 | return _mv88e6xxx_atu_wait(ps); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1288 | static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 37705b7 | 2015-09-04 14:34:11 -0400 | [diff] [blame] | 1289 | struct mv88e6xxx_atu_entry *entry) |
| 1290 | { |
| 1291 | u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK; |
| 1292 | |
| 1293 | if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 1294 | unsigned int mask, shift; |
| 1295 | |
| 1296 | if (entry->trunk) { |
| 1297 | data |= GLOBAL_ATU_DATA_TRUNK; |
| 1298 | mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK; |
| 1299 | shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT; |
| 1300 | } else { |
| 1301 | mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK; |
| 1302 | shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT; |
| 1303 | } |
| 1304 | |
| 1305 | data |= (entry->portv_trunkid << shift) & mask; |
| 1306 | } |
| 1307 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1308 | return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_DATA, data); |
Vivien Didelot | 37705b7 | 2015-09-04 14:34:11 -0400 | [diff] [blame] | 1309 | } |
| 1310 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1311 | static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1312 | struct mv88e6xxx_atu_entry *entry, |
| 1313 | bool static_too) |
| 1314 | { |
| 1315 | int op; |
| 1316 | int err; |
| 1317 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1318 | err = _mv88e6xxx_atu_wait(ps); |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1319 | if (err) |
| 1320 | return err; |
| 1321 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1322 | err = _mv88e6xxx_atu_data_write(ps, entry); |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1323 | if (err) |
| 1324 | return err; |
| 1325 | |
| 1326 | if (entry->fid) { |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1327 | op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB : |
| 1328 | GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB; |
| 1329 | } else { |
| 1330 | op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL : |
| 1331 | GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC; |
| 1332 | } |
| 1333 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1334 | return _mv88e6xxx_atu_cmd(ps, entry->fid, op); |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1337 | static int _mv88e6xxx_atu_flush(struct mv88e6xxx_priv_state *ps, |
| 1338 | u16 fid, bool static_too) |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1339 | { |
| 1340 | struct mv88e6xxx_atu_entry entry = { |
| 1341 | .fid = fid, |
| 1342 | .state = 0, /* EntryState bits must be 0 */ |
| 1343 | }; |
| 1344 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1345 | return _mv88e6xxx_atu_flush_move(ps, &entry, static_too); |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1346 | } |
| 1347 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1348 | static int _mv88e6xxx_atu_move(struct mv88e6xxx_priv_state *ps, u16 fid, |
| 1349 | int from_port, int to_port, bool static_too) |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1350 | { |
| 1351 | struct mv88e6xxx_atu_entry entry = { |
| 1352 | .trunk = false, |
| 1353 | .fid = fid, |
| 1354 | }; |
| 1355 | |
| 1356 | /* EntryState bits must be 0xF */ |
| 1357 | entry.state = GLOBAL_ATU_DATA_STATE_MASK; |
| 1358 | |
| 1359 | /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */ |
| 1360 | entry.portv_trunkid = (to_port & 0x0f) << 4; |
| 1361 | entry.portv_trunkid |= from_port & 0x0f; |
| 1362 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1363 | return _mv88e6xxx_atu_flush_move(ps, &entry, static_too); |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1364 | } |
| 1365 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1366 | static int _mv88e6xxx_atu_remove(struct mv88e6xxx_priv_state *ps, u16 fid, |
| 1367 | int port, bool static_too) |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1368 | { |
| 1369 | /* Destination port 0xF means remove the entries */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1370 | return _mv88e6xxx_atu_move(ps, fid, port, 0x0f, static_too); |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1371 | } |
| 1372 | |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1373 | static const char * const mv88e6xxx_port_state_names[] = { |
| 1374 | [PORT_CONTROL_STATE_DISABLED] = "Disabled", |
| 1375 | [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening", |
| 1376 | [PORT_CONTROL_STATE_LEARNING] = "Learning", |
| 1377 | [PORT_CONTROL_STATE_FORWARDING] = "Forwarding", |
| 1378 | }; |
| 1379 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1380 | static int _mv88e6xxx_port_state(struct mv88e6xxx_priv_state *ps, int port, |
| 1381 | u8 state) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1382 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1383 | struct dsa_switch *ds = ps->ds; |
Geert Uytterhoeven | c3ffe6d | 2015-04-16 20:49:14 +0200 | [diff] [blame] | 1384 | int reg, ret = 0; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1385 | u8 oldstate; |
| 1386 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1387 | reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL); |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1388 | if (reg < 0) |
| 1389 | return reg; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1390 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1391 | oldstate = reg & PORT_CONTROL_STATE_MASK; |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1392 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1393 | if (oldstate != state) { |
| 1394 | /* Flush forwarding database if we're moving a port |
| 1395 | * from Learning or Forwarding state to Disabled or |
| 1396 | * Blocking or Listening state. |
| 1397 | */ |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1398 | if ((oldstate == PORT_CONTROL_STATE_LEARNING || |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 1399 | oldstate == PORT_CONTROL_STATE_FORWARDING) && |
| 1400 | (state == PORT_CONTROL_STATE_DISABLED || |
| 1401 | state == PORT_CONTROL_STATE_BLOCKING)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1402 | ret = _mv88e6xxx_atu_remove(ps, 0, port, false); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1403 | if (ret) |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1404 | return ret; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1405 | } |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1406 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1407 | reg = (reg & ~PORT_CONTROL_STATE_MASK) | state; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1408 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL, |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1409 | reg); |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1410 | if (ret) |
| 1411 | return ret; |
| 1412 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1413 | netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n", |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1414 | mv88e6xxx_port_state_names[state], |
| 1415 | mv88e6xxx_port_state_names[oldstate]); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1418 | return ret; |
| 1419 | } |
| 1420 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1421 | static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_priv_state *ps, |
| 1422 | int port) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1423 | { |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1424 | struct net_device *bridge = ps->ports[port].bridge_dev; |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1425 | const u16 mask = (1 << ps->info->num_ports) - 1; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1426 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1427 | u16 output_ports = 0; |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame] | 1428 | int reg; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1429 | int i; |
| 1430 | |
| 1431 | /* allow CPU port or DSA link(s) to send frames to every port */ |
| 1432 | if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) { |
| 1433 | output_ports = mask; |
| 1434 | } else { |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1435 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1436 | /* allow sending frames to every group member */ |
| 1437 | if (bridge && ps->ports[i].bridge_dev == bridge) |
| 1438 | output_ports |= BIT(i); |
| 1439 | |
| 1440 | /* allow sending frames to CPU port and DSA link(s) */ |
| 1441 | if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)) |
| 1442 | output_ports |= BIT(i); |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | /* prevent frames from going back out of the port they came in on */ |
| 1447 | output_ports &= ~BIT(port); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1448 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1449 | reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN); |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame] | 1450 | if (reg < 0) |
| 1451 | return reg; |
| 1452 | |
| 1453 | reg &= ~mask; |
| 1454 | reg |= output_ports & mask; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1455 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1456 | return _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, reg); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 1459 | static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port, |
| 1460 | u8 state) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1461 | { |
| 1462 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1463 | int stp_state; |
Vivien Didelot | 553eb54 | 2016-05-13 20:38:23 -0400 | [diff] [blame] | 1464 | int err; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1465 | |
Vivien Didelot | 936f234 | 2016-05-09 13:22:46 -0400 | [diff] [blame] | 1466 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_PORTSTATE)) |
| 1467 | return; |
| 1468 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1469 | switch (state) { |
| 1470 | case BR_STATE_DISABLED: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1471 | stp_state = PORT_CONTROL_STATE_DISABLED; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1472 | break; |
| 1473 | case BR_STATE_BLOCKING: |
| 1474 | case BR_STATE_LISTENING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1475 | stp_state = PORT_CONTROL_STATE_BLOCKING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1476 | break; |
| 1477 | case BR_STATE_LEARNING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1478 | stp_state = PORT_CONTROL_STATE_LEARNING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1479 | break; |
| 1480 | case BR_STATE_FORWARDING: |
| 1481 | default: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1482 | stp_state = PORT_CONTROL_STATE_FORWARDING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1483 | break; |
| 1484 | } |
| 1485 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 1486 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 553eb54 | 2016-05-13 20:38:23 -0400 | [diff] [blame] | 1487 | err = _mv88e6xxx_port_state(ps, port, stp_state); |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 1488 | mutex_unlock(&ps->reg_lock); |
Vivien Didelot | 553eb54 | 2016-05-13 20:38:23 -0400 | [diff] [blame] | 1489 | |
| 1490 | if (err) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1491 | netdev_err(ds->ports[port].netdev, |
| 1492 | "failed to update state to %s\n", |
Vivien Didelot | 553eb54 | 2016-05-13 20:38:23 -0400 | [diff] [blame] | 1493 | mv88e6xxx_port_state_names[stp_state]); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1496 | static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port, |
| 1497 | u16 *new, u16 *old) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1498 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1499 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1500 | u16 pvid; |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1501 | int ret; |
| 1502 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1503 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1504 | if (ret < 0) |
| 1505 | return ret; |
| 1506 | |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1507 | pvid = ret & PORT_DEFAULT_VLAN_MASK; |
| 1508 | |
| 1509 | if (new) { |
| 1510 | ret &= ~PORT_DEFAULT_VLAN_MASK; |
| 1511 | ret |= *new & PORT_DEFAULT_VLAN_MASK; |
| 1512 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1513 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1514 | PORT_DEFAULT_VLAN, ret); |
| 1515 | if (ret < 0) |
| 1516 | return ret; |
| 1517 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1518 | netdev_dbg(ds->ports[port].netdev, |
| 1519 | "DefaultVID %d (was %d)\n", *new, pvid); |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | if (old) |
| 1523 | *old = pvid; |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1524 | |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1528 | static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps, |
| 1529 | int port, u16 *pvid) |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1530 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1531 | return _mv88e6xxx_port_pvid(ps, port, NULL, pvid); |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1532 | } |
| 1533 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1534 | static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps, |
| 1535 | int port, u16 pvid) |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1536 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1537 | return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1538 | } |
| 1539 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1540 | static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1541 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1542 | return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP, |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1543 | GLOBAL_VTU_OP_BUSY); |
| 1544 | } |
| 1545 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1546 | static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op) |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1547 | { |
| 1548 | int ret; |
| 1549 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1550 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1551 | if (ret < 0) |
| 1552 | return ret; |
| 1553 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1554 | return _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1557 | static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps) |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1558 | { |
| 1559 | int ret; |
| 1560 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1561 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1562 | if (ret < 0) |
| 1563 | return ret; |
| 1564 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1565 | return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1566 | } |
| 1567 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1568 | static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1569 | struct mv88e6xxx_vtu_stu_entry *entry, |
| 1570 | unsigned int nibble_offset) |
| 1571 | { |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1572 | u16 regs[3]; |
| 1573 | int i; |
| 1574 | int ret; |
| 1575 | |
| 1576 | for (i = 0; i < 3; ++i) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1577 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1578 | GLOBAL_VTU_DATA_0_3 + i); |
| 1579 | if (ret < 0) |
| 1580 | return ret; |
| 1581 | |
| 1582 | regs[i] = ret; |
| 1583 | } |
| 1584 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1585 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1586 | unsigned int shift = (i % 4) * 4 + nibble_offset; |
| 1587 | u16 reg = regs[i / 4]; |
| 1588 | |
| 1589 | entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK; |
| 1590 | } |
| 1591 | |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1595 | static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_priv_state *ps, |
| 1596 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1597 | { |
| 1598 | return _mv88e6xxx_vtu_stu_data_read(ps, entry, 0); |
| 1599 | } |
| 1600 | |
| 1601 | static int mv88e6xxx_stu_data_read(struct mv88e6xxx_priv_state *ps, |
| 1602 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1603 | { |
| 1604 | return _mv88e6xxx_vtu_stu_data_read(ps, entry, 2); |
| 1605 | } |
| 1606 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1607 | static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1608 | struct mv88e6xxx_vtu_stu_entry *entry, |
| 1609 | unsigned int nibble_offset) |
| 1610 | { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1611 | u16 regs[3] = { 0 }; |
| 1612 | int i; |
| 1613 | int ret; |
| 1614 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1615 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1616 | unsigned int shift = (i % 4) * 4 + nibble_offset; |
| 1617 | u8 data = entry->data[i]; |
| 1618 | |
| 1619 | regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift; |
| 1620 | } |
| 1621 | |
| 1622 | for (i = 0; i < 3; ++i) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1623 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1624 | GLOBAL_VTU_DATA_0_3 + i, regs[i]); |
| 1625 | if (ret < 0) |
| 1626 | return ret; |
| 1627 | } |
| 1628 | |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1632 | static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_priv_state *ps, |
| 1633 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1634 | { |
| 1635 | return _mv88e6xxx_vtu_stu_data_write(ps, entry, 0); |
| 1636 | } |
| 1637 | |
| 1638 | static int mv88e6xxx_stu_data_write(struct mv88e6xxx_priv_state *ps, |
| 1639 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1640 | { |
| 1641 | return _mv88e6xxx_vtu_stu_data_write(ps, entry, 2); |
| 1642 | } |
| 1643 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1644 | static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid) |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 1645 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1646 | return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 1647 | vid & GLOBAL_VTU_VID_MASK); |
| 1648 | } |
| 1649 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1650 | static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1651 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1652 | { |
| 1653 | struct mv88e6xxx_vtu_stu_entry next = { 0 }; |
| 1654 | int ret; |
| 1655 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1656 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1657 | if (ret < 0) |
| 1658 | return ret; |
| 1659 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1660 | ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT); |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1661 | if (ret < 0) |
| 1662 | return ret; |
| 1663 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1664 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID); |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1665 | if (ret < 0) |
| 1666 | return ret; |
| 1667 | |
| 1668 | next.vid = ret & GLOBAL_VTU_VID_MASK; |
| 1669 | next.valid = !!(ret & GLOBAL_VTU_VID_VALID); |
| 1670 | |
| 1671 | if (next.valid) { |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1672 | ret = mv88e6xxx_vtu_data_read(ps, &next); |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1673 | if (ret < 0) |
| 1674 | return ret; |
| 1675 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1676 | if (mv88e6xxx_has_fid_reg(ps)) { |
| 1677 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1678 | GLOBAL_VTU_FID); |
| 1679 | if (ret < 0) |
| 1680 | return ret; |
| 1681 | |
| 1682 | next.fid = ret & GLOBAL_VTU_FID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1683 | } else if (mv88e6xxx_num_databases(ps) == 256) { |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1684 | /* VTU DBNum[7:4] are located in VTU Operation 11:8, and |
| 1685 | * VTU DBNum[3:0] are located in VTU Operation 3:0 |
| 1686 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1687 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1688 | GLOBAL_VTU_OP); |
| 1689 | if (ret < 0) |
| 1690 | return ret; |
| 1691 | |
| 1692 | next.fid = (ret & 0xf00) >> 4; |
| 1693 | next.fid |= ret & 0xf; |
Vivien Didelot | 2e7bd5e | 2016-03-31 16:53:41 -0400 | [diff] [blame] | 1694 | } |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1695 | |
Vivien Didelot | cb9b902 | 2016-05-10 15:44:29 -0400 | [diff] [blame] | 1696 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1697 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1698 | GLOBAL_VTU_SID); |
| 1699 | if (ret < 0) |
| 1700 | return ret; |
| 1701 | |
| 1702 | next.sid = ret & GLOBAL_VTU_SID_MASK; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | *entry = next; |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 1710 | static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port, |
| 1711 | struct switchdev_obj_port_vlan *vlan, |
| 1712 | int (*cb)(struct switchdev_obj *obj)) |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1713 | { |
| 1714 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1715 | struct mv88e6xxx_vtu_stu_entry next; |
| 1716 | u16 pvid; |
| 1717 | int err; |
| 1718 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 1719 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 1720 | return -EOPNOTSUPP; |
| 1721 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 1722 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1723 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1724 | err = _mv88e6xxx_port_pvid_get(ps, port, &pvid); |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1725 | if (err) |
| 1726 | goto unlock; |
| 1727 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1728 | err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK); |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1729 | if (err) |
| 1730 | goto unlock; |
| 1731 | |
| 1732 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1733 | err = _mv88e6xxx_vtu_getnext(ps, &next); |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1734 | if (err) |
| 1735 | break; |
| 1736 | |
| 1737 | if (!next.valid) |
| 1738 | break; |
| 1739 | |
| 1740 | if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) |
| 1741 | continue; |
| 1742 | |
| 1743 | /* reinit and dump this VLAN obj */ |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 1744 | vlan->vid_begin = next.vid; |
| 1745 | vlan->vid_end = next.vid; |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1746 | vlan->flags = 0; |
| 1747 | |
| 1748 | if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED) |
| 1749 | vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
| 1750 | |
| 1751 | if (next.vid == pvid) |
| 1752 | vlan->flags |= BRIDGE_VLAN_INFO_PVID; |
| 1753 | |
| 1754 | err = cb(&vlan->obj); |
| 1755 | if (err) |
| 1756 | break; |
| 1757 | } while (next.vid < GLOBAL_VTU_VID_MASK); |
| 1758 | |
| 1759 | unlock: |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 1760 | mutex_unlock(&ps->reg_lock); |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1761 | |
| 1762 | return err; |
| 1763 | } |
| 1764 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1765 | static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1766 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1767 | { |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1768 | u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1769 | u16 reg = 0; |
| 1770 | int ret; |
| 1771 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1772 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1773 | if (ret < 0) |
| 1774 | return ret; |
| 1775 | |
| 1776 | if (!entry->valid) |
| 1777 | goto loadpurge; |
| 1778 | |
| 1779 | /* Write port member tags */ |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1780 | ret = mv88e6xxx_vtu_data_write(ps, entry); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1781 | if (ret < 0) |
| 1782 | return ret; |
| 1783 | |
Vivien Didelot | cb9b902 | 2016-05-10 15:44:29 -0400 | [diff] [blame] | 1784 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_STU)) { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1785 | reg = entry->sid & GLOBAL_VTU_SID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1786 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1787 | if (ret < 0) |
| 1788 | return ret; |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1789 | } |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1790 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1791 | if (mv88e6xxx_has_fid_reg(ps)) { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1792 | reg = entry->fid & GLOBAL_VTU_FID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1793 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1794 | if (ret < 0) |
| 1795 | return ret; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1796 | } else if (mv88e6xxx_num_databases(ps) == 256) { |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1797 | /* VTU DBNum[7:4] are located in VTU Operation 11:8, and |
| 1798 | * VTU DBNum[3:0] are located in VTU Operation 3:0 |
| 1799 | */ |
| 1800 | op |= (entry->fid & 0xf0) << 8; |
| 1801 | op |= entry->fid & 0xf; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | reg = GLOBAL_VTU_VID_VALID; |
| 1805 | loadpurge: |
| 1806 | reg |= entry->vid & GLOBAL_VTU_VID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1807 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1808 | if (ret < 0) |
| 1809 | return ret; |
| 1810 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1811 | return _mv88e6xxx_vtu_cmd(ps, op); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1812 | } |
| 1813 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1814 | static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid, |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1815 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1816 | { |
| 1817 | struct mv88e6xxx_vtu_stu_entry next = { 0 }; |
| 1818 | int ret; |
| 1819 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1820 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1821 | if (ret < 0) |
| 1822 | return ret; |
| 1823 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1824 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1825 | sid & GLOBAL_VTU_SID_MASK); |
| 1826 | if (ret < 0) |
| 1827 | return ret; |
| 1828 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1829 | ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1830 | if (ret < 0) |
| 1831 | return ret; |
| 1832 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1833 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1834 | if (ret < 0) |
| 1835 | return ret; |
| 1836 | |
| 1837 | next.sid = ret & GLOBAL_VTU_SID_MASK; |
| 1838 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1839 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1840 | if (ret < 0) |
| 1841 | return ret; |
| 1842 | |
| 1843 | next.valid = !!(ret & GLOBAL_VTU_VID_VALID); |
| 1844 | |
| 1845 | if (next.valid) { |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1846 | ret = mv88e6xxx_stu_data_read(ps, &next); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1847 | if (ret < 0) |
| 1848 | return ret; |
| 1849 | } |
| 1850 | |
| 1851 | *entry = next; |
| 1852 | return 0; |
| 1853 | } |
| 1854 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1855 | static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1856 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1857 | { |
| 1858 | u16 reg = 0; |
| 1859 | int ret; |
| 1860 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1861 | ret = _mv88e6xxx_vtu_wait(ps); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1862 | if (ret < 0) |
| 1863 | return ret; |
| 1864 | |
| 1865 | if (!entry->valid) |
| 1866 | goto loadpurge; |
| 1867 | |
| 1868 | /* Write port states */ |
Vivien Didelot | 15d7d7d | 2016-05-10 15:44:28 -0400 | [diff] [blame] | 1869 | ret = mv88e6xxx_stu_data_write(ps, entry); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1870 | if (ret < 0) |
| 1871 | return ret; |
| 1872 | |
| 1873 | reg = GLOBAL_VTU_VID_VALID; |
| 1874 | loadpurge: |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1875 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1876 | if (ret < 0) |
| 1877 | return ret; |
| 1878 | |
| 1879 | reg = entry->sid & GLOBAL_VTU_SID_MASK; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1880 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1881 | if (ret < 0) |
| 1882 | return ret; |
| 1883 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1884 | return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1885 | } |
| 1886 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1887 | static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port, |
| 1888 | u16 *new, u16 *old) |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1889 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1890 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1891 | u16 upper_mask; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1892 | u16 fid; |
| 1893 | int ret; |
| 1894 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1895 | if (mv88e6xxx_num_databases(ps) == 4096) |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1896 | upper_mask = 0xff; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1897 | else if (mv88e6xxx_num_databases(ps) == 256) |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1898 | upper_mask = 0xf; |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1899 | else |
| 1900 | return -EOPNOTSUPP; |
| 1901 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1902 | /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1903 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1904 | if (ret < 0) |
| 1905 | return ret; |
| 1906 | |
| 1907 | fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12; |
| 1908 | |
| 1909 | if (new) { |
| 1910 | ret &= ~PORT_BASE_VLAN_FID_3_0_MASK; |
| 1911 | ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK; |
| 1912 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1913 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1914 | ret); |
| 1915 | if (ret < 0) |
| 1916 | return ret; |
| 1917 | } |
| 1918 | |
| 1919 | /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1920 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1921 | if (ret < 0) |
| 1922 | return ret; |
| 1923 | |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1924 | fid |= (ret & upper_mask) << 4; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1925 | |
| 1926 | if (new) { |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1927 | ret &= ~upper_mask; |
| 1928 | ret |= (*new >> 4) & upper_mask; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1929 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1930 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1931 | ret); |
| 1932 | if (ret < 0) |
| 1933 | return ret; |
| 1934 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 1935 | netdev_dbg(ds->ports[port].netdev, |
| 1936 | "FID %d (was %d)\n", *new, fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | if (old) |
| 1940 | *old = fid; |
| 1941 | |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1945 | static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps, |
| 1946 | int port, u16 *fid) |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1947 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1948 | return _mv88e6xxx_port_fid(ps, port, NULL, fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1949 | } |
| 1950 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1951 | static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps, |
| 1952 | int port, u16 fid) |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1953 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1954 | return _mv88e6xxx_port_fid(ps, port, &fid, NULL); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1955 | } |
| 1956 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1957 | static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid) |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1958 | { |
| 1959 | DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID); |
| 1960 | struct mv88e6xxx_vtu_stu_entry vlan; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1961 | int i, err; |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1962 | |
| 1963 | bitmap_zero(fid_bitmap, MV88E6XXX_N_FID); |
| 1964 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1965 | /* Set every FID bit used by the (un)bridged ports */ |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 1966 | for (i = 0; i < ps->info->num_ports; ++i) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1967 | err = _mv88e6xxx_port_fid_get(ps, i, fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1968 | if (err) |
| 1969 | return err; |
| 1970 | |
| 1971 | set_bit(*fid, fid_bitmap); |
| 1972 | } |
| 1973 | |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1974 | /* Set every FID bit used by the VLAN entries */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1975 | err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1976 | if (err) |
| 1977 | return err; |
| 1978 | |
| 1979 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1980 | err = _mv88e6xxx_vtu_getnext(ps, &vlan); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1981 | if (err) |
| 1982 | return err; |
| 1983 | |
| 1984 | if (!vlan.valid) |
| 1985 | break; |
| 1986 | |
| 1987 | set_bit(vlan.fid, fid_bitmap); |
| 1988 | } while (vlan.vid < GLOBAL_VTU_VID_MASK); |
| 1989 | |
| 1990 | /* The reset value 0x000 is used to indicate that multiple address |
| 1991 | * databases are not needed. Return the next positive available. |
| 1992 | */ |
| 1993 | *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1994 | if (unlikely(*fid >= mv88e6xxx_num_databases(ps))) |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1995 | return -ENOSPC; |
| 1996 | |
| 1997 | /* Clear the database */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 1998 | return _mv88e6xxx_atu_flush(ps, *fid, true); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1999 | } |
| 2000 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2001 | static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid, |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 2002 | struct mv88e6xxx_vtu_stu_entry *entry) |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2003 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2004 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2005 | struct mv88e6xxx_vtu_stu_entry vlan = { |
| 2006 | .valid = true, |
| 2007 | .vid = vid, |
| 2008 | }; |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 2009 | int i, err; |
| 2010 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2011 | err = _mv88e6xxx_fid_new(ps, &vlan.fid); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 2012 | if (err) |
| 2013 | return err; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2014 | |
Vivien Didelot | 3d131f0 | 2015-11-03 10:52:52 -0500 | [diff] [blame] | 2015 | /* exclude all ports except the CPU and DSA ports */ |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 2016 | for (i = 0; i < ps->info->num_ports; ++i) |
Vivien Didelot | 3d131f0 | 2015-11-03 10:52:52 -0500 | [diff] [blame] | 2017 | vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i) |
| 2018 | ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED |
| 2019 | : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2020 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2021 | if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) || |
| 2022 | mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) { |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2023 | struct mv88e6xxx_vtu_stu_entry vstp; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2024 | |
| 2025 | /* Adding a VTU entry requires a valid STU entry. As VSTP is not |
| 2026 | * implemented, only one STU entry is needed to cover all VTU |
| 2027 | * entries. Thus, validate the SID 0. |
| 2028 | */ |
| 2029 | vlan.sid = 0; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2030 | err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2031 | if (err) |
| 2032 | return err; |
| 2033 | |
| 2034 | if (vstp.sid != vlan.sid || !vstp.valid) { |
| 2035 | memset(&vstp, 0, sizeof(vstp)); |
| 2036 | vstp.valid = true; |
| 2037 | vstp.sid = vlan.sid; |
| 2038 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2039 | err = _mv88e6xxx_stu_loadpurge(ps, &vstp); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2040 | if (err) |
| 2041 | return err; |
| 2042 | } |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | *entry = vlan; |
| 2046 | return 0; |
| 2047 | } |
| 2048 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2049 | static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid, |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 2050 | struct mv88e6xxx_vtu_stu_entry *entry, bool creat) |
| 2051 | { |
| 2052 | int err; |
| 2053 | |
| 2054 | if (!vid) |
| 2055 | return -EINVAL; |
| 2056 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2057 | err = _mv88e6xxx_vtu_vid_write(ps, vid - 1); |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 2058 | if (err) |
| 2059 | return err; |
| 2060 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2061 | err = _mv88e6xxx_vtu_getnext(ps, entry); |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 2062 | if (err) |
| 2063 | return err; |
| 2064 | |
| 2065 | if (entry->vid != vid || !entry->valid) { |
| 2066 | if (!creat) |
| 2067 | return -EOPNOTSUPP; |
| 2068 | /* -ENOENT would've been more appropriate, but switchdev expects |
| 2069 | * -EOPNOTSUPP to inform bridge about an eventual software VLAN. |
| 2070 | */ |
| 2071 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2072 | err = _mv88e6xxx_vtu_new(ps, vid, entry); |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | return err; |
| 2076 | } |
| 2077 | |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2078 | static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port, |
| 2079 | u16 vid_begin, u16 vid_end) |
| 2080 | { |
| 2081 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2082 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 2083 | int i, err; |
| 2084 | |
| 2085 | if (!vid_begin) |
| 2086 | return -EOPNOTSUPP; |
| 2087 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2088 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2089 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2090 | err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1); |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2091 | if (err) |
| 2092 | goto unlock; |
| 2093 | |
| 2094 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2095 | err = _mv88e6xxx_vtu_getnext(ps, &vlan); |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2096 | if (err) |
| 2097 | goto unlock; |
| 2098 | |
| 2099 | if (!vlan.valid) |
| 2100 | break; |
| 2101 | |
| 2102 | if (vlan.vid > vid_end) |
| 2103 | break; |
| 2104 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 2105 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2106 | if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i)) |
| 2107 | continue; |
| 2108 | |
| 2109 | if (vlan.data[i] == |
| 2110 | GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) |
| 2111 | continue; |
| 2112 | |
| 2113 | if (ps->ports[i].bridge_dev == |
| 2114 | ps->ports[port].bridge_dev) |
| 2115 | break; /* same bridge, check next VLAN */ |
| 2116 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2117 | netdev_warn(ds->ports[port].netdev, |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2118 | "hardware VLAN %d already used by %s\n", |
| 2119 | vlan.vid, |
| 2120 | netdev_name(ps->ports[i].bridge_dev)); |
| 2121 | err = -EOPNOTSUPP; |
| 2122 | goto unlock; |
| 2123 | } |
| 2124 | } while (vlan.vid < vid_end); |
| 2125 | |
| 2126 | unlock: |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2127 | mutex_unlock(&ps->reg_lock); |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2128 | |
| 2129 | return err; |
| 2130 | } |
| 2131 | |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2132 | static const char * const mv88e6xxx_port_8021q_mode_names[] = { |
| 2133 | [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled", |
| 2134 | [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback", |
| 2135 | [PORT_CONTROL_2_8021Q_CHECK] = "Check", |
| 2136 | [PORT_CONTROL_2_8021Q_SECURE] = "Secure", |
| 2137 | }; |
| 2138 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2139 | static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port, |
| 2140 | bool vlan_filtering) |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2141 | { |
| 2142 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2143 | u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE : |
| 2144 | PORT_CONTROL_2_8021Q_DISABLED; |
| 2145 | int ret; |
| 2146 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2147 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 2148 | return -EOPNOTSUPP; |
| 2149 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2150 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2151 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2152 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2); |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2153 | if (ret < 0) |
| 2154 | goto unlock; |
| 2155 | |
| 2156 | old = ret & PORT_CONTROL_2_8021Q_MASK; |
| 2157 | |
Vivien Didelot | 5220ef1 | 2016-03-07 18:24:52 -0500 | [diff] [blame] | 2158 | if (new != old) { |
| 2159 | ret &= ~PORT_CONTROL_2_8021Q_MASK; |
| 2160 | ret |= new & PORT_CONTROL_2_8021Q_MASK; |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2161 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2162 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2, |
Vivien Didelot | 5220ef1 | 2016-03-07 18:24:52 -0500 | [diff] [blame] | 2163 | ret); |
| 2164 | if (ret < 0) |
| 2165 | goto unlock; |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2166 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2167 | netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n", |
Vivien Didelot | 5220ef1 | 2016-03-07 18:24:52 -0500 | [diff] [blame] | 2168 | mv88e6xxx_port_8021q_mode_names[new], |
| 2169 | mv88e6xxx_port_8021q_mode_names[old]); |
| 2170 | } |
| 2171 | |
| 2172 | ret = 0; |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2173 | unlock: |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2174 | mutex_unlock(&ps->reg_lock); |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 2175 | |
| 2176 | return ret; |
| 2177 | } |
| 2178 | |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 2179 | static int |
| 2180 | mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port, |
| 2181 | const struct switchdev_obj_port_vlan *vlan, |
| 2182 | struct switchdev_trans *trans) |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2183 | { |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2184 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2185 | int err; |
| 2186 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2187 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 2188 | return -EOPNOTSUPP; |
| 2189 | |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 2190 | /* If the requested port doesn't belong to the same bridge as the VLAN |
| 2191 | * members, do not support it (yet) and fallback to software VLAN. |
| 2192 | */ |
| 2193 | err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin, |
| 2194 | vlan->vid_end); |
| 2195 | if (err) |
| 2196 | return err; |
| 2197 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2198 | /* We don't need any dynamic resource from the kernel (yet), |
| 2199 | * so skip the prepare phase. |
| 2200 | */ |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2204 | static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port, |
| 2205 | u16 vid, bool untagged) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2206 | { |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2207 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 2208 | int err; |
| 2209 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2210 | err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true); |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 2211 | if (err) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2212 | return err; |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 2213 | |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2214 | vlan.data[port] = untagged ? |
| 2215 | GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED : |
| 2216 | GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED; |
| 2217 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2218 | return _mv88e6xxx_vtu_loadpurge(ps, &vlan); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2219 | } |
| 2220 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2221 | static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, |
| 2222 | const struct switchdev_obj_port_vlan *vlan, |
| 2223 | struct switchdev_trans *trans) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2224 | { |
| 2225 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2226 | bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; |
| 2227 | bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; |
| 2228 | u16 vid; |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2229 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2230 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 2231 | return; |
| 2232 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2233 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2234 | |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 2235 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2236 | if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged)) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2237 | netdev_err(ds->ports[port].netdev, |
| 2238 | "failed to add VLAN %d%c\n", |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 2239 | vid, untagged ? 'u' : 't'); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2240 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2241 | if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end)) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2242 | netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n", |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 2243 | vlan->vid_end); |
| 2244 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2245 | mutex_unlock(&ps->reg_lock); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 2246 | } |
| 2247 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2248 | static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps, |
| 2249 | int port, u16 vid) |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2250 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2251 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2252 | struct mv88e6xxx_vtu_stu_entry vlan; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2253 | int i, err; |
| 2254 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2255 | err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false); |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 2256 | if (err) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2257 | return err; |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 2258 | |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 2259 | /* Tell switchdev if this VLAN is handled in software */ |
| 2260 | if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) |
Vivien Didelot | 3c06f08 | 2016-02-05 14:04:39 -0500 | [diff] [blame] | 2261 | return -EOPNOTSUPP; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2262 | |
| 2263 | vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER; |
| 2264 | |
| 2265 | /* keep the VLAN unless all ports are excluded */ |
Vivien Didelot | f02bdff | 2015-10-11 18:08:36 -0400 | [diff] [blame] | 2266 | vlan.valid = false; |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 2267 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | 3d131f0 | 2015-11-03 10:52:52 -0500 | [diff] [blame] | 2268 | if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)) |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2269 | continue; |
| 2270 | |
| 2271 | if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) { |
Vivien Didelot | f02bdff | 2015-10-11 18:08:36 -0400 | [diff] [blame] | 2272 | vlan.valid = true; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2273 | break; |
| 2274 | } |
| 2275 | } |
| 2276 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2277 | err = _mv88e6xxx_vtu_loadpurge(ps, &vlan); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2278 | if (err) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2279 | return err; |
| 2280 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2281 | return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2282 | } |
| 2283 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2284 | static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, |
| 2285 | const struct switchdev_obj_port_vlan *vlan) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2286 | { |
| 2287 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2288 | u16 pvid, vid; |
| 2289 | int err = 0; |
| 2290 | |
Vivien Didelot | 54d77b5 | 2016-05-09 13:22:47 -0400 | [diff] [blame] | 2291 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU)) |
| 2292 | return -EOPNOTSUPP; |
| 2293 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2294 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2295 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2296 | err = _mv88e6xxx_port_pvid_get(ps, port, &pvid); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2297 | if (err) |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2298 | goto unlock; |
| 2299 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2300 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2301 | err = _mv88e6xxx_port_vlan_del(ps, port, vid); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2302 | if (err) |
| 2303 | goto unlock; |
| 2304 | |
| 2305 | if (vid == pvid) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2306 | err = _mv88e6xxx_port_pvid_set(ps, port, 0); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 2307 | if (err) |
| 2308 | goto unlock; |
| 2309 | } |
| 2310 | } |
| 2311 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2312 | unlock: |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2313 | mutex_unlock(&ps->reg_lock); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 2314 | |
| 2315 | return err; |
| 2316 | } |
| 2317 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2318 | static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 2319 | const unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2320 | { |
| 2321 | int i, ret; |
| 2322 | |
| 2323 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 2324 | ret = _mv88e6xxx_reg_write( |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2325 | ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i, |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 2326 | (addr[i * 2] << 8) | addr[i * 2 + 1]); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2327 | if (ret < 0) |
| 2328 | return ret; |
| 2329 | } |
| 2330 | |
| 2331 | return 0; |
| 2332 | } |
| 2333 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2334 | static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps, |
| 2335 | unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2336 | { |
| 2337 | int i, ret; |
| 2338 | |
| 2339 | for (i = 0; i < 3; i++) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2340 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 2341 | GLOBAL_ATU_MAC_01 + i); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2342 | if (ret < 0) |
| 2343 | return ret; |
| 2344 | addr[i * 2] = ret >> 8; |
| 2345 | addr[i * 2 + 1] = ret & 0xff; |
| 2346 | } |
| 2347 | |
| 2348 | return 0; |
| 2349 | } |
| 2350 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2351 | static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps, |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2352 | struct mv88e6xxx_atu_entry *entry) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2353 | { |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2354 | int ret; |
| 2355 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2356 | ret = _mv88e6xxx_atu_wait(ps); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2357 | if (ret < 0) |
| 2358 | return ret; |
| 2359 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2360 | ret = _mv88e6xxx_atu_mac_write(ps, entry->mac); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2361 | if (ret < 0) |
| 2362 | return ret; |
| 2363 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2364 | ret = _mv88e6xxx_atu_data_write(ps, entry); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2365 | if (ret < 0) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2366 | return ret; |
| 2367 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2368 | return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2369 | } |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2370 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2371 | static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port, |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2372 | const unsigned char *addr, u16 vid, |
| 2373 | u8 state) |
| 2374 | { |
| 2375 | struct mv88e6xxx_atu_entry entry = { 0 }; |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 2376 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 2377 | int err; |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2378 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2379 | /* Null VLAN ID corresponds to the port private database */ |
| 2380 | if (vid == 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2381 | err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2382 | else |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2383 | err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 2384 | if (err) |
| 2385 | return err; |
| 2386 | |
| 2387 | entry.fid = vlan.fid; |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2388 | entry.state = state; |
| 2389 | ether_addr_copy(entry.mac, addr); |
| 2390 | if (state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 2391 | entry.trunk = false; |
| 2392 | entry.portv_trunkid = BIT(port); |
| 2393 | } |
| 2394 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2395 | return _mv88e6xxx_atu_load(ps, &entry); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2398 | static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port, |
| 2399 | const struct switchdev_obj_port_fdb *fdb, |
| 2400 | struct switchdev_trans *trans) |
Vivien Didelot | 146a320 | 2015-10-08 11:35:12 -0400 | [diff] [blame] | 2401 | { |
Vivien Didelot | 2672f82 | 2016-05-09 13:22:48 -0400 | [diff] [blame] | 2402 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2403 | |
| 2404 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU)) |
| 2405 | return -EOPNOTSUPP; |
| 2406 | |
Vivien Didelot | 146a320 | 2015-10-08 11:35:12 -0400 | [diff] [blame] | 2407 | /* We don't need any dynamic resource from the kernel (yet), |
| 2408 | * so skip the prepare phase. |
| 2409 | */ |
| 2410 | return 0; |
| 2411 | } |
| 2412 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2413 | static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port, |
| 2414 | const struct switchdev_obj_port_fdb *fdb, |
| 2415 | struct switchdev_trans *trans) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2416 | { |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 2417 | int state = is_multicast_ether_addr(fdb->addr) ? |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2418 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 2419 | GLOBAL_ATU_DATA_STATE_UC_STATIC; |
| 2420 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 6630e23 | 2015-08-06 01:44:07 -0400 | [diff] [blame] | 2421 | |
Vivien Didelot | 2672f82 | 2016-05-09 13:22:48 -0400 | [diff] [blame] | 2422 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU)) |
| 2423 | return; |
| 2424 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2425 | mutex_lock(&ps->reg_lock); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2426 | if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state)) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2427 | netdev_err(ds->ports[port].netdev, |
| 2428 | "failed to load MAC address\n"); |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2429 | mutex_unlock(&ps->reg_lock); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2432 | static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port, |
| 2433 | const struct switchdev_obj_port_fdb *fdb) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2434 | { |
| 2435 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2436 | int ret; |
| 2437 | |
Vivien Didelot | 2672f82 | 2016-05-09 13:22:48 -0400 | [diff] [blame] | 2438 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU)) |
| 2439 | return -EOPNOTSUPP; |
| 2440 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2441 | mutex_lock(&ps->reg_lock); |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2442 | ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2443 | GLOBAL_ATU_DATA_STATE_UNUSED); |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2444 | mutex_unlock(&ps->reg_lock); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2445 | |
| 2446 | return ret; |
| 2447 | } |
| 2448 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2449 | static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid, |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2450 | struct mv88e6xxx_atu_entry *entry) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2451 | { |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2452 | struct mv88e6xxx_atu_entry next = { 0 }; |
| 2453 | int ret; |
| 2454 | |
| 2455 | next.fid = fid; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2456 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2457 | ret = _mv88e6xxx_atu_wait(ps); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2458 | if (ret < 0) |
| 2459 | return ret; |
| 2460 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2461 | ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2462 | if (ret < 0) |
| 2463 | return ret; |
| 2464 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2465 | ret = _mv88e6xxx_atu_mac_read(ps, next.mac); |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2466 | if (ret < 0) |
| 2467 | return ret; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2468 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2469 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA); |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2470 | if (ret < 0) |
| 2471 | return ret; |
| 2472 | |
| 2473 | next.state = ret & GLOBAL_ATU_DATA_STATE_MASK; |
| 2474 | if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 2475 | unsigned int mask, shift; |
| 2476 | |
| 2477 | if (ret & GLOBAL_ATU_DATA_TRUNK) { |
| 2478 | next.trunk = true; |
| 2479 | mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK; |
| 2480 | shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT; |
| 2481 | } else { |
| 2482 | next.trunk = false; |
| 2483 | mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK; |
| 2484 | shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT; |
| 2485 | } |
| 2486 | |
| 2487 | next.portv_trunkid = (ret & mask) >> shift; |
| 2488 | } |
| 2489 | |
| 2490 | *entry = next; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2491 | return 0; |
| 2492 | } |
| 2493 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2494 | static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps, |
| 2495 | u16 fid, u16 vid, int port, |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2496 | struct switchdev_obj_port_fdb *fdb, |
| 2497 | int (*cb)(struct switchdev_obj *obj)) |
| 2498 | { |
| 2499 | struct mv88e6xxx_atu_entry addr = { |
| 2500 | .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, |
| 2501 | }; |
| 2502 | int err; |
| 2503 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2504 | err = _mv88e6xxx_atu_mac_write(ps, addr.mac); |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2505 | if (err) |
| 2506 | return err; |
| 2507 | |
| 2508 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2509 | err = _mv88e6xxx_atu_getnext(ps, fid, &addr); |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2510 | if (err) |
| 2511 | break; |
| 2512 | |
| 2513 | if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED) |
| 2514 | break; |
| 2515 | |
| 2516 | if (!addr.trunk && addr.portv_trunkid & BIT(port)) { |
| 2517 | bool is_static = addr.state == |
| 2518 | (is_multicast_ether_addr(addr.mac) ? |
| 2519 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 2520 | GLOBAL_ATU_DATA_STATE_UC_STATIC); |
| 2521 | |
| 2522 | fdb->vid = vid; |
| 2523 | ether_addr_copy(fdb->addr, addr.mac); |
| 2524 | fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
| 2525 | |
| 2526 | err = cb(&fdb->obj); |
| 2527 | if (err) |
| 2528 | break; |
| 2529 | } |
| 2530 | } while (!is_broadcast_ether_addr(addr.mac)); |
| 2531 | |
| 2532 | return err; |
| 2533 | } |
| 2534 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2535 | static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port, |
| 2536 | struct switchdev_obj_port_fdb *fdb, |
| 2537 | int (*cb)(struct switchdev_obj *obj)) |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2538 | { |
| 2539 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2540 | struct mv88e6xxx_vtu_stu_entry vlan = { |
| 2541 | .vid = GLOBAL_VTU_VID_MASK, /* all ones */ |
| 2542 | }; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2543 | u16 fid; |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2544 | int err; |
| 2545 | |
Vivien Didelot | 2672f82 | 2016-05-09 13:22:48 -0400 | [diff] [blame] | 2546 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU)) |
| 2547 | return -EOPNOTSUPP; |
| 2548 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2549 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2550 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2551 | /* Dump port's default Filtering Information Database (VLAN ID 0) */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2552 | err = _mv88e6xxx_port_fid_get(ps, port, &fid); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2553 | if (err) |
| 2554 | goto unlock; |
| 2555 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2556 | err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2557 | if (err) |
| 2558 | goto unlock; |
| 2559 | |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2560 | /* Dump VLANs' Filtering Information Databases */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2561 | err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid); |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2562 | if (err) |
| 2563 | goto unlock; |
| 2564 | |
| 2565 | do { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2566 | err = _mv88e6xxx_vtu_getnext(ps, &vlan); |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2567 | if (err) |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2568 | break; |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2569 | |
| 2570 | if (!vlan.valid) |
| 2571 | break; |
| 2572 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2573 | err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port, |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2574 | fdb, cb); |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2575 | if (err) |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2576 | break; |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2577 | } while (vlan.vid < GLOBAL_VTU_VID_MASK); |
| 2578 | |
| 2579 | unlock: |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2580 | mutex_unlock(&ps->reg_lock); |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2581 | |
| 2582 | return err; |
| 2583 | } |
| 2584 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2585 | static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port, |
| 2586 | struct net_device *bridge) |
Vivien Didelot | e79a8bc | 2015-11-04 17:23:40 -0500 | [diff] [blame] | 2587 | { |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2588 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Colin Ian King | 1d9619d | 2016-04-25 23:11:22 +0100 | [diff] [blame] | 2589 | int i, err = 0; |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2590 | |
Vivien Didelot | 936f234 | 2016-05-09 13:22:46 -0400 | [diff] [blame] | 2591 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE)) |
| 2592 | return -EOPNOTSUPP; |
| 2593 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2594 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2595 | |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2596 | /* Assign the bridge and remap each port's VLANTable */ |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2597 | ps->ports[port].bridge_dev = bridge; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2598 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 2599 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2600 | if (ps->ports[i].bridge_dev == bridge) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2601 | err = _mv88e6xxx_port_based_vlan_map(ps, i); |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2602 | if (err) |
| 2603 | break; |
| 2604 | } |
| 2605 | } |
| 2606 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2607 | mutex_unlock(&ps->reg_lock); |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2608 | |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2609 | return err; |
Vivien Didelot | e79a8bc | 2015-11-04 17:23:40 -0500 | [diff] [blame] | 2610 | } |
| 2611 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 2612 | static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port) |
Vivien Didelot | e79a8bc | 2015-11-04 17:23:40 -0500 | [diff] [blame] | 2613 | { |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2614 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2615 | struct net_device *bridge = ps->ports[port].bridge_dev; |
Vivien Didelot | 16bfa70 | 2016-03-13 16:21:33 -0400 | [diff] [blame] | 2616 | int i; |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2617 | |
Vivien Didelot | 936f234 | 2016-05-09 13:22:46 -0400 | [diff] [blame] | 2618 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE)) |
| 2619 | return; |
| 2620 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2621 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2622 | |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2623 | /* Unassign the bridge and remap each port's VLANTable */ |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2624 | ps->ports[port].bridge_dev = NULL; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2625 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 2626 | for (i = 0; i < ps->info->num_ports; ++i) |
Vivien Didelot | 16bfa70 | 2016-03-13 16:21:33 -0400 | [diff] [blame] | 2627 | if (i == port || ps->ports[i].bridge_dev == bridge) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2628 | if (_mv88e6xxx_port_based_vlan_map(ps, i)) |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 2629 | netdev_warn(ds->ports[i].netdev, |
| 2630 | "failed to remap\n"); |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2631 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 2632 | mutex_unlock(&ps->reg_lock); |
Vivien Didelot | 66d9cd0 | 2016-02-05 14:07:14 -0500 | [diff] [blame] | 2633 | } |
| 2634 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2635 | static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_priv_state *ps, |
| 2636 | int port, int page, int reg, int val) |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2637 | { |
| 2638 | int ret; |
| 2639 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2640 | ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2641 | if (ret < 0) |
| 2642 | goto restore_page_0; |
| 2643 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2644 | ret = mv88e6xxx_mdio_write_indirect(ps, port, reg, val); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2645 | restore_page_0: |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2646 | mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2647 | |
| 2648 | return ret; |
| 2649 | } |
| 2650 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2651 | static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_priv_state *ps, |
| 2652 | int port, int page, int reg) |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2653 | { |
| 2654 | int ret; |
| 2655 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2656 | ret = mv88e6xxx_mdio_write_indirect(ps, port, 0x16, page); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2657 | if (ret < 0) |
| 2658 | goto restore_page_0; |
| 2659 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2660 | ret = mv88e6xxx_mdio_read_indirect(ps, port, reg); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2661 | restore_page_0: |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2662 | mv88e6xxx_mdio_write_indirect(ps, port, 0x16, 0x0); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2663 | |
| 2664 | return ret; |
| 2665 | } |
| 2666 | |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 2667 | static int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps) |
| 2668 | { |
| 2669 | bool ppu_active = mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE); |
| 2670 | u16 is_reset = (ppu_active ? 0x8800 : 0xc800); |
Andrew Lunn | 52638f7 | 2016-05-10 23:27:22 +0200 | [diff] [blame] | 2671 | struct gpio_desc *gpiod = ps->reset; |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 2672 | unsigned long timeout; |
| 2673 | int ret; |
| 2674 | int i; |
| 2675 | |
| 2676 | /* Set all ports to the disabled state. */ |
| 2677 | for (i = 0; i < ps->info->num_ports; i++) { |
| 2678 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL); |
| 2679 | if (ret < 0) |
| 2680 | return ret; |
| 2681 | |
| 2682 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL, |
| 2683 | ret & 0xfffc); |
| 2684 | if (ret) |
| 2685 | return ret; |
| 2686 | } |
| 2687 | |
| 2688 | /* Wait for transmit queues to drain. */ |
| 2689 | usleep_range(2000, 4000); |
| 2690 | |
| 2691 | /* If there is a gpio connected to the reset pin, toggle it */ |
| 2692 | if (gpiod) { |
| 2693 | gpiod_set_value_cansleep(gpiod, 1); |
| 2694 | usleep_range(10000, 20000); |
| 2695 | gpiod_set_value_cansleep(gpiod, 0); |
| 2696 | usleep_range(10000, 20000); |
| 2697 | } |
| 2698 | |
| 2699 | /* Reset the switch. Keep the PPU active if requested. The PPU |
| 2700 | * needs to be active to support indirect phy register access |
| 2701 | * through global registers 0x18 and 0x19. |
| 2702 | */ |
| 2703 | if (ppu_active) |
| 2704 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000); |
| 2705 | else |
| 2706 | ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400); |
| 2707 | if (ret) |
| 2708 | return ret; |
| 2709 | |
| 2710 | /* Wait up to one second for reset to complete. */ |
| 2711 | timeout = jiffies + 1 * HZ; |
| 2712 | while (time_before(jiffies, timeout)) { |
| 2713 | ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00); |
| 2714 | if (ret < 0) |
| 2715 | return ret; |
| 2716 | |
| 2717 | if ((ret & is_reset) == is_reset) |
| 2718 | break; |
| 2719 | usleep_range(1000, 2000); |
| 2720 | } |
| 2721 | if (time_after(jiffies, timeout)) |
| 2722 | ret = -ETIMEDOUT; |
| 2723 | else |
| 2724 | ret = 0; |
| 2725 | |
| 2726 | return ret; |
| 2727 | } |
| 2728 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2729 | static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps) |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2730 | { |
| 2731 | int ret; |
| 2732 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2733 | ret = _mv88e6xxx_mdio_page_read(ps, REG_FIBER_SERDES, |
| 2734 | PAGE_FIBER_SERDES, MII_BMCR); |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2735 | if (ret < 0) |
| 2736 | return ret; |
| 2737 | |
| 2738 | if (ret & BMCR_PDOWN) { |
| 2739 | ret &= ~BMCR_PDOWN; |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 2740 | ret = _mv88e6xxx_mdio_page_write(ps, REG_FIBER_SERDES, |
| 2741 | PAGE_FIBER_SERDES, MII_BMCR, |
| 2742 | ret); |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | return ret; |
| 2746 | } |
| 2747 | |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2748 | static int mv88e6xxx_setup_port(struct mv88e6xxx_priv_state *ps, int port) |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2749 | { |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2750 | struct dsa_switch *ds = ps->ds; |
Vivien Didelot | f02bdff | 2015-10-11 18:08:36 -0400 | [diff] [blame] | 2751 | int ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2752 | u16 reg; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2753 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2754 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2755 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2756 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) || |
| 2757 | mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2758 | /* MAC Forcing register: don't force link, speed, |
| 2759 | * duplex or flow control state to any particular |
| 2760 | * values on physical ports, but force the CPU port |
| 2761 | * and all DSA ports to their maximum bandwidth and |
| 2762 | * full duplex. |
| 2763 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2764 | reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL); |
Andrew Lunn | 60045cb | 2015-08-17 23:52:51 +0200 | [diff] [blame] | 2765 | if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) { |
Russell King | 53adc9e | 2015-09-21 21:42:59 +0100 | [diff] [blame] | 2766 | reg &= ~PORT_PCS_CTRL_UNFORCED; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2767 | reg |= PORT_PCS_CTRL_FORCE_LINK | |
| 2768 | PORT_PCS_CTRL_LINK_UP | |
| 2769 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 2770 | PORT_PCS_CTRL_FORCE_DUPLEX; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2771 | if (mv88e6xxx_6065_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2772 | reg |= PORT_PCS_CTRL_100; |
| 2773 | else |
| 2774 | reg |= PORT_PCS_CTRL_1000; |
| 2775 | } else { |
| 2776 | reg |= PORT_PCS_CTRL_UNFORCED; |
| 2777 | } |
| 2778 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2779 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2780 | PORT_PCS_CTRL, reg); |
| 2781 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2782 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock, |
| 2786 | * disable Header mode, enable IGMP/MLD snooping, disable VLAN |
| 2787 | * tunneling, determine priority by looking at 802.1p and IP |
| 2788 | * priority fields (IP prio has precedence), and set STP state |
| 2789 | * to Forwarding. |
| 2790 | * |
| 2791 | * If this is the CPU link, use DSA or EDSA tagging depending |
| 2792 | * on which tagging mode was configured. |
| 2793 | * |
| 2794 | * If this is a link to another switch, use DSA tagging mode. |
| 2795 | * |
| 2796 | * If this is the upstream port for this switch, enable |
| 2797 | * forwarding of unknown unicasts and multicasts. |
| 2798 | */ |
| 2799 | reg = 0; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2800 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2801 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2802 | mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) || |
| 2803 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2804 | reg = PORT_CONTROL_IGMP_MLD_SNOOP | |
| 2805 | PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP | |
| 2806 | PORT_CONTROL_STATE_FORWARDING; |
| 2807 | if (dsa_is_cpu_port(ds, port)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2808 | if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2809 | reg |= PORT_CONTROL_DSA_TAG; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2810 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2811 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2812 | mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 5377b80 | 2016-06-04 21:17:02 +0200 | [diff] [blame] | 2813 | reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA | |
| 2814 | PORT_CONTROL_FORWARD_UNKNOWN | |
Andrew Lunn | c047a1f | 2015-09-29 01:50:56 +0200 | [diff] [blame] | 2815 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2816 | } |
| 2817 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2818 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2819 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2820 | mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) || |
| 2821 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) { |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 2822 | reg |= PORT_CONTROL_EGRESS_ADD_TAG; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2823 | } |
| 2824 | } |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2825 | if (dsa_is_dsa_port(ds, port)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2826 | if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2827 | reg |= PORT_CONTROL_DSA_TAG; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2828 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2829 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2830 | mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2831 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2832 | } |
| 2833 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2834 | if (port == dsa_upstream_port(ds)) |
| 2835 | reg |= PORT_CONTROL_FORWARD_UNKNOWN | |
| 2836 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
| 2837 | } |
| 2838 | if (reg) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2839 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2840 | PORT_CONTROL, reg); |
| 2841 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2842 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2843 | } |
| 2844 | |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2845 | /* If this port is connected to a SerDes, make sure the SerDes is not |
| 2846 | * powered down. |
| 2847 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2848 | if (mv88e6xxx_6352_family(ps)) { |
| 2849 | ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS); |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2850 | if (ret < 0) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2851 | return ret; |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2852 | ret &= PORT_STATUS_CMODE_MASK; |
| 2853 | if ((ret == PORT_STATUS_CMODE_100BASE_X) || |
| 2854 | (ret == PORT_STATUS_CMODE_1000BASE_X) || |
| 2855 | (ret == PORT_STATUS_CMODE_SGMII)) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2856 | ret = mv88e6xxx_power_on_serdes(ps); |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2857 | if (ret < 0) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2858 | return ret; |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2862 | /* Port Control 2: don't force a good FCS, set the maximum frame size to |
Vivien Didelot | 46fbe5e | 2016-02-26 13:16:07 -0500 | [diff] [blame] | 2863 | * 10240 bytes, disable 802.1q tags checking, don't discard tagged or |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2864 | * untagged frames on this port, do a destination address lookup on all |
| 2865 | * received packets as usual, disable ARP mirroring and don't send a |
| 2866 | * copy of all transmitted/received frames on this port to the CPU. |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2867 | */ |
| 2868 | reg = 0; |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2869 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2870 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2871 | mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) || |
| 2872 | mv88e6xxx_6185_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2873 | reg = PORT_CONTROL_2_MAP_DA; |
| 2874 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2875 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2876 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2877 | reg |= PORT_CONTROL_2_JUMBO_10240; |
| 2878 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2879 | if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2880 | /* Set the upstream port this port should use */ |
| 2881 | reg |= dsa_upstream_port(ds); |
| 2882 | /* enable forwarding of unknown multicast addresses to |
| 2883 | * the upstream port |
| 2884 | */ |
| 2885 | if (port == dsa_upstream_port(ds)) |
| 2886 | reg |= PORT_CONTROL_2_FORWARD_UNKNOWN; |
| 2887 | } |
| 2888 | |
Vivien Didelot | 46fbe5e | 2016-02-26 13:16:07 -0500 | [diff] [blame] | 2889 | reg |= PORT_CONTROL_2_8021Q_DISABLED; |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2890 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2891 | if (reg) { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2892 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2893 | PORT_CONTROL_2, reg); |
| 2894 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2895 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | /* Port Association Vector: when learning source addresses |
| 2899 | * of packets, add the address to the address database using |
| 2900 | * a port bitmap that has only the bit for this port set and |
| 2901 | * the other bits clear. |
| 2902 | */ |
Andrew Lunn | 4c7ea3c | 2015-11-03 10:52:36 -0500 | [diff] [blame] | 2903 | reg = 1 << port; |
Vivien Didelot | 996ecb8 | 2016-04-14 14:42:08 -0400 | [diff] [blame] | 2904 | /* Disable learning for CPU port */ |
| 2905 | if (dsa_is_cpu_port(ds, port)) |
Vivien Didelot | 65fa402 | 2016-04-14 14:42:07 -0400 | [diff] [blame] | 2906 | reg = 0; |
Andrew Lunn | 4c7ea3c | 2015-11-03 10:52:36 -0500 | [diff] [blame] | 2907 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2908 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg); |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2909 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2910 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2911 | |
| 2912 | /* Egress rate control 2: disable egress rate control. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2913 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2, |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2914 | 0x0000); |
| 2915 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2916 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2917 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2918 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2919 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2920 | mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2921 | /* Do not limit the period of time that this port can |
| 2922 | * be paused for by the remote end or the period of |
| 2923 | * time that this port can pause the remote end. |
| 2924 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2925 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2926 | PORT_PAUSE_CTRL, 0x0000); |
| 2927 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2928 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2929 | |
| 2930 | /* Port ATU control: disable limiting the number of |
| 2931 | * address database entries that this port is allowed |
| 2932 | * to use. |
| 2933 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2934 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2935 | PORT_ATU_CONTROL, 0x0000); |
| 2936 | /* Priority Override: disable DA, SA and VTU priority |
| 2937 | * override. |
| 2938 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2939 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2940 | PORT_PRI_OVERRIDE, 0x0000); |
| 2941 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2942 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2943 | |
| 2944 | /* Port Ethertype: use the Ethertype DSA Ethertype |
| 2945 | * value. |
| 2946 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2947 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2948 | PORT_ETH_TYPE, ETH_P_EDSA); |
| 2949 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2950 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2951 | /* Tag Remap: use an identity 802.1p prio -> switch |
| 2952 | * prio mapping. |
| 2953 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2954 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2955 | PORT_TAG_REGMAP_0123, 0x3210); |
| 2956 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2957 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2958 | |
| 2959 | /* Tag Remap 2: use an identity 802.1p prio -> switch |
| 2960 | * prio mapping. |
| 2961 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2962 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2963 | PORT_TAG_REGMAP_4567, 0x7654); |
| 2964 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2965 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2966 | } |
| 2967 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2968 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 2969 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 2970 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) || |
| 2971 | mv88e6xxx_6320_family(ps)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2972 | /* Rate Control: disable ingress rate limiting. */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2973 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2974 | PORT_RATE_CONTROL, 0x0001); |
| 2975 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2976 | return ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2977 | } |
| 2978 | |
Guenter Roeck | 366f0a0 | 2015-03-26 18:36:30 -0700 | [diff] [blame] | 2979 | /* Port Control 1: disable trunking, disable sending |
| 2980 | * learning messages to this port. |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2981 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2982 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2983 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2984 | return ret; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2985 | |
Vivien Didelot | 207afda | 2016-04-14 14:42:09 -0400 | [diff] [blame] | 2986 | /* Port based VLAN map: give each port the same default address |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2987 | * database, and allow bidirectional communication between the |
| 2988 | * CPU and DSA port(s), and the other ports. |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2989 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2990 | ret = _mv88e6xxx_port_fid_set(ps, port, 0); |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2991 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2992 | return ret; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2993 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 2994 | ret = _mv88e6xxx_port_based_vlan_map(ps, port); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2995 | if (ret) |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 2996 | return ret; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2997 | |
| 2998 | /* Default VLAN ID and priority: don't set a default VLAN |
| 2999 | * ID, and set the default packet priority to zero. |
| 3000 | */ |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3001 | ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN, |
Vivien Didelot | 47cf1e65 | 2015-04-20 17:43:26 -0400 | [diff] [blame] | 3002 | 0x0000); |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 3003 | if (ret) |
| 3004 | return ret; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 3005 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 3006 | return 0; |
| 3007 | } |
| 3008 | |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 3009 | static int mv88e6xxx_setup_global(struct mv88e6xxx_priv_state *ps) |
| 3010 | { |
Vivien Didelot | b0745e87 | 2016-05-09 13:22:53 -0400 | [diff] [blame] | 3011 | struct dsa_switch *ds = ps->ds; |
| 3012 | u32 upstream_port = dsa_upstream_port(ds); |
Vivien Didelot | 119477b | 2016-05-09 13:22:51 -0400 | [diff] [blame] | 3013 | u16 reg; |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 3014 | int err; |
| 3015 | int i; |
| 3016 | |
Vivien Didelot | 119477b | 2016-05-09 13:22:51 -0400 | [diff] [blame] | 3017 | /* Enable the PHY Polling Unit if present, don't discard any packets, |
| 3018 | * and mask all interrupt sources. |
| 3019 | */ |
| 3020 | reg = 0; |
| 3021 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU) || |
| 3022 | mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE)) |
| 3023 | reg |= GLOBAL_CONTROL_PPU_ENABLE; |
| 3024 | |
| 3025 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, reg); |
| 3026 | if (err) |
| 3027 | return err; |
| 3028 | |
Vivien Didelot | b0745e87 | 2016-05-09 13:22:53 -0400 | [diff] [blame] | 3029 | /* Configure the upstream port, and configure it as the port to which |
| 3030 | * ingress and egress and ARP monitor frames are to be sent. |
| 3031 | */ |
| 3032 | reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT | |
| 3033 | upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT | |
| 3034 | upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT; |
| 3035 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg); |
| 3036 | if (err) |
| 3037 | return err; |
| 3038 | |
Vivien Didelot | 50484ff | 2016-05-09 13:22:54 -0400 | [diff] [blame] | 3039 | /* Disable remote management, and set the switch's DSA device number. */ |
| 3040 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL_2, |
| 3041 | GLOBAL_CONTROL_2_MULTIPLE_CASCADE | |
| 3042 | (ds->index & 0x1f)); |
| 3043 | if (err) |
| 3044 | return err; |
| 3045 | |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 3046 | /* Set the default address aging time to 5 minutes, and |
| 3047 | * enable address learn messages to be sent to all message |
| 3048 | * ports. |
| 3049 | */ |
| 3050 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL, |
| 3051 | 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL); |
| 3052 | if (err) |
| 3053 | return err; |
| 3054 | |
| 3055 | /* Configure the IP ToS mapping registers. */ |
| 3056 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000); |
| 3057 | if (err) |
| 3058 | return err; |
| 3059 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000); |
| 3060 | if (err) |
| 3061 | return err; |
| 3062 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555); |
| 3063 | if (err) |
| 3064 | return err; |
| 3065 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555); |
| 3066 | if (err) |
| 3067 | return err; |
| 3068 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa); |
| 3069 | if (err) |
| 3070 | return err; |
| 3071 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa); |
| 3072 | if (err) |
| 3073 | return err; |
| 3074 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff); |
| 3075 | if (err) |
| 3076 | return err; |
| 3077 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff); |
| 3078 | if (err) |
| 3079 | return err; |
| 3080 | |
| 3081 | /* Configure the IEEE 802.1p priority mapping register. */ |
| 3082 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41); |
| 3083 | if (err) |
| 3084 | return err; |
| 3085 | |
| 3086 | /* Send all frames with destination addresses matching |
| 3087 | * 01:80:c2:00:00:0x to the CPU port. |
| 3088 | */ |
| 3089 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff); |
| 3090 | if (err) |
| 3091 | return err; |
| 3092 | |
| 3093 | /* Ignore removed tag data on doubly tagged packets, disable |
| 3094 | * flow control messages, force flow control priority to the |
| 3095 | * highest, and send all special multicast frames to the CPU |
| 3096 | * port at the highest priority. |
| 3097 | */ |
| 3098 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, |
| 3099 | 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 | |
| 3100 | GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI); |
| 3101 | if (err) |
| 3102 | return err; |
| 3103 | |
| 3104 | /* Program the DSA routing table. */ |
| 3105 | for (i = 0; i < 32; i++) { |
| 3106 | int nexthop = 0x1f; |
| 3107 | |
Andrew Lunn | 66472fc | 2016-06-04 21:17:00 +0200 | [diff] [blame] | 3108 | if (i != ds->index && i < DSA_MAX_SWITCHES) |
| 3109 | nexthop = ds->rtable[i] & 0x1f; |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 3110 | |
| 3111 | err = _mv88e6xxx_reg_write( |
| 3112 | ps, REG_GLOBAL2, |
| 3113 | GLOBAL2_DEVICE_MAPPING, |
| 3114 | GLOBAL2_DEVICE_MAPPING_UPDATE | |
| 3115 | (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop); |
| 3116 | if (err) |
| 3117 | return err; |
| 3118 | } |
| 3119 | |
| 3120 | /* Clear all trunk masks. */ |
| 3121 | for (i = 0; i < 8; i++) { |
| 3122 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, |
| 3123 | 0x8000 | |
| 3124 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) | |
| 3125 | ((1 << ps->info->num_ports) - 1)); |
| 3126 | if (err) |
| 3127 | return err; |
| 3128 | } |
| 3129 | |
| 3130 | /* Clear all trunk mappings. */ |
| 3131 | for (i = 0; i < 16; i++) { |
| 3132 | err = _mv88e6xxx_reg_write( |
| 3133 | ps, REG_GLOBAL2, |
| 3134 | GLOBAL2_TRUNK_MAPPING, |
| 3135 | GLOBAL2_TRUNK_MAPPING_UPDATE | |
| 3136 | (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT)); |
| 3137 | if (err) |
| 3138 | return err; |
| 3139 | } |
| 3140 | |
| 3141 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 3142 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 3143 | mv88e6xxx_6320_family(ps)) { |
| 3144 | /* Send all frames with destination addresses matching |
| 3145 | * 01:80:c2:00:00:2x to the CPU port. |
| 3146 | */ |
| 3147 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, |
| 3148 | GLOBAL2_MGMT_EN_2X, 0xffff); |
| 3149 | if (err) |
| 3150 | return err; |
| 3151 | |
| 3152 | /* Initialise cross-chip port VLAN table to reset |
| 3153 | * defaults. |
| 3154 | */ |
| 3155 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, |
| 3156 | GLOBAL2_PVT_ADDR, 0x9000); |
| 3157 | if (err) |
| 3158 | return err; |
| 3159 | |
| 3160 | /* Clear the priority override table. */ |
| 3161 | for (i = 0; i < 16; i++) { |
| 3162 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, |
| 3163 | GLOBAL2_PRIO_OVERRIDE, |
| 3164 | 0x8000 | (i << 8)); |
| 3165 | if (err) |
| 3166 | return err; |
| 3167 | } |
| 3168 | } |
| 3169 | |
| 3170 | if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) || |
| 3171 | mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) || |
| 3172 | mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) || |
| 3173 | mv88e6xxx_6320_family(ps)) { |
| 3174 | /* Disable ingress rate limiting by resetting all |
| 3175 | * ingress rate limit registers to their initial |
| 3176 | * state. |
| 3177 | */ |
| 3178 | for (i = 0; i < ps->info->num_ports; i++) { |
| 3179 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, |
| 3180 | GLOBAL2_INGRESS_OP, |
| 3181 | 0x9000 | (i << 8)); |
| 3182 | if (err) |
| 3183 | return err; |
| 3184 | } |
| 3185 | } |
| 3186 | |
| 3187 | /* Clear the statistics counters for all ports */ |
| 3188 | err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP, |
| 3189 | GLOBAL_STATS_OP_FLUSH_ALL); |
| 3190 | if (err) |
| 3191 | return err; |
| 3192 | |
| 3193 | /* Wait for the flush to complete. */ |
| 3194 | err = _mv88e6xxx_stats_wait(ps); |
| 3195 | if (err) |
| 3196 | return err; |
| 3197 | |
| 3198 | /* Clear all ATU entries */ |
| 3199 | err = _mv88e6xxx_atu_flush(ps, 0, true); |
| 3200 | if (err) |
| 3201 | return err; |
| 3202 | |
| 3203 | /* Clear all the VTU and STU entries */ |
| 3204 | err = _mv88e6xxx_vtu_stu_flush(ps); |
| 3205 | if (err < 0) |
| 3206 | return err; |
| 3207 | |
| 3208 | return err; |
| 3209 | } |
| 3210 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3211 | static int mv88e6xxx_setup(struct dsa_switch *ds) |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 3212 | { |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 3213 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 3214 | int err; |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 3215 | int i; |
| 3216 | |
| 3217 | ps->ds = ds; |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3218 | ds->slave_mii_bus = ps->mdio_bus; |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 3219 | |
Vivien Didelot | d24645b | 2016-05-09 13:22:41 -0400 | [diff] [blame] | 3220 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM)) |
| 3221 | mutex_init(&ps->eeprom_mutex); |
| 3222 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3223 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 552238b | 2016-05-09 13:22:49 -0400 | [diff] [blame] | 3224 | |
| 3225 | err = mv88e6xxx_switch_reset(ps); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 3226 | if (err) |
| 3227 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 3228 | |
Vivien Didelot | 08a0126 | 2016-05-09 13:22:50 -0400 | [diff] [blame] | 3229 | err = mv88e6xxx_setup_global(ps); |
Vivien Didelot | a1a6a4d | 2016-05-09 13:22:56 -0400 | [diff] [blame] | 3230 | if (err) |
| 3231 | goto unlock; |
| 3232 | |
| 3233 | for (i = 0; i < ps->info->num_ports; i++) { |
| 3234 | err = mv88e6xxx_setup_port(ps, i); |
| 3235 | if (err) |
| 3236 | goto unlock; |
| 3237 | } |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 3238 | |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 3239 | unlock: |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3240 | mutex_unlock(&ps->reg_lock); |
Andrew Lunn | db687a5 | 2015-06-20 21:31:29 +0200 | [diff] [blame] | 3241 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 3242 | return err; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 3243 | } |
| 3244 | |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 3245 | static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page, |
| 3246 | int reg) |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 3247 | { |
| 3248 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3249 | int ret; |
| 3250 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3251 | mutex_lock(&ps->reg_lock); |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3252 | ret = _mv88e6xxx_mdio_page_read(ps, port, page, reg); |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3253 | mutex_unlock(&ps->reg_lock); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 3254 | |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 3255 | return ret; |
| 3256 | } |
| 3257 | |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 3258 | static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page, |
| 3259 | int reg, int val) |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 3260 | { |
| 3261 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3262 | int ret; |
| 3263 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3264 | mutex_lock(&ps->reg_lock); |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3265 | ret = _mv88e6xxx_mdio_page_write(ps, port, page, reg, val); |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3266 | mutex_unlock(&ps->reg_lock); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 3267 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3268 | return ret; |
| 3269 | } |
| 3270 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3271 | static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_priv_state *ps, |
| 3272 | int port) |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3273 | { |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame] | 3274 | if (port >= 0 && port < ps->info->num_ports) |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3275 | return port; |
| 3276 | return -EINVAL; |
| 3277 | } |
| 3278 | |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3279 | static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum) |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3280 | { |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3281 | struct mv88e6xxx_priv_state *ps = bus->priv; |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3282 | int addr = mv88e6xxx_port_to_mdio_addr(ps, port); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3283 | int ret; |
| 3284 | |
| 3285 | if (addr < 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3286 | return 0xffff; |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3287 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3288 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3289 | |
| 3290 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU)) |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3291 | ret = mv88e6xxx_mdio_read_ppu(ps, addr, regnum); |
Vivien Didelot | 6d5834a | 2016-05-09 13:22:40 -0400 | [diff] [blame] | 3292 | else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY)) |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3293 | ret = mv88e6xxx_mdio_read_indirect(ps, addr, regnum); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3294 | else |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3295 | ret = mv88e6xxx_mdio_read_direct(ps, addr, regnum); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3296 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3297 | mutex_unlock(&ps->reg_lock); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3298 | return ret; |
| 3299 | } |
| 3300 | |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3301 | static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum, |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3302 | u16 val) |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3303 | { |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3304 | struct mv88e6xxx_priv_state *ps = bus->priv; |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3305 | int addr = mv88e6xxx_port_to_mdio_addr(ps, port); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3306 | int ret; |
| 3307 | |
| 3308 | if (addr < 0) |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3309 | return 0xffff; |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3310 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3311 | mutex_lock(&ps->reg_lock); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3312 | |
| 3313 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU)) |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3314 | ret = mv88e6xxx_mdio_write_ppu(ps, addr, regnum, val); |
Vivien Didelot | 6d5834a | 2016-05-09 13:22:40 -0400 | [diff] [blame] | 3315 | else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY)) |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3316 | ret = mv88e6xxx_mdio_write_indirect(ps, addr, regnum, val); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3317 | else |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3318 | ret = mv88e6xxx_mdio_write_direct(ps, addr, regnum, val); |
Vivien Didelot | 8c9983a | 2016-05-09 13:22:39 -0400 | [diff] [blame] | 3319 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3320 | mutex_unlock(&ps->reg_lock); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 3321 | return ret; |
| 3322 | } |
| 3323 | |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3324 | static int mv88e6xxx_mdio_register(struct mv88e6xxx_priv_state *ps, |
| 3325 | struct device_node *np) |
| 3326 | { |
| 3327 | static int index; |
| 3328 | struct mii_bus *bus; |
| 3329 | int err; |
| 3330 | |
| 3331 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU)) |
| 3332 | mv88e6xxx_ppu_state_init(ps); |
| 3333 | |
| 3334 | if (np) |
| 3335 | ps->mdio_np = of_get_child_by_name(np, "mdio"); |
| 3336 | |
| 3337 | bus = devm_mdiobus_alloc(ps->dev); |
| 3338 | if (!bus) |
| 3339 | return -ENOMEM; |
| 3340 | |
| 3341 | bus->priv = (void *)ps; |
| 3342 | if (np) { |
| 3343 | bus->name = np->full_name; |
| 3344 | snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name); |
| 3345 | } else { |
| 3346 | bus->name = "mv88e6xxx SMI"; |
| 3347 | snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++); |
| 3348 | } |
| 3349 | |
| 3350 | bus->read = mv88e6xxx_mdio_read; |
| 3351 | bus->write = mv88e6xxx_mdio_write; |
| 3352 | bus->parent = ps->dev; |
| 3353 | |
| 3354 | if (ps->mdio_np) |
| 3355 | err = of_mdiobus_register(bus, ps->mdio_np); |
| 3356 | else |
| 3357 | err = mdiobus_register(bus); |
| 3358 | if (err) { |
| 3359 | dev_err(ps->dev, "Cannot register MDIO bus (%d)\n", err); |
| 3360 | goto out; |
| 3361 | } |
| 3362 | ps->mdio_bus = bus; |
| 3363 | |
| 3364 | return 0; |
| 3365 | |
| 3366 | out: |
| 3367 | if (ps->mdio_np) |
| 3368 | of_node_put(ps->mdio_np); |
| 3369 | |
| 3370 | return err; |
| 3371 | } |
| 3372 | |
| 3373 | static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_priv_state *ps) |
| 3374 | |
| 3375 | { |
| 3376 | struct mii_bus *bus = ps->mdio_bus; |
| 3377 | |
| 3378 | mdiobus_unregister(bus); |
| 3379 | |
| 3380 | if (ps->mdio_np) |
| 3381 | of_node_put(ps->mdio_np); |
| 3382 | } |
| 3383 | |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3384 | #ifdef CONFIG_NET_DSA_HWMON |
| 3385 | |
| 3386 | static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp) |
| 3387 | { |
| 3388 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3389 | int ret; |
| 3390 | int val; |
| 3391 | |
| 3392 | *temp = 0; |
| 3393 | |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3394 | mutex_lock(&ps->reg_lock); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3395 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3396 | ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x6); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3397 | if (ret < 0) |
| 3398 | goto error; |
| 3399 | |
| 3400 | /* Enable temperature sensor */ |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3401 | ret = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3402 | if (ret < 0) |
| 3403 | goto error; |
| 3404 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3405 | ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret | (1 << 5)); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3406 | if (ret < 0) |
| 3407 | goto error; |
| 3408 | |
| 3409 | /* Wait for temperature to stabilize */ |
| 3410 | usleep_range(10000, 12000); |
| 3411 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3412 | val = mv88e6xxx_mdio_read_direct(ps, 0x0, 0x1a); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3413 | if (val < 0) { |
| 3414 | ret = val; |
| 3415 | goto error; |
| 3416 | } |
| 3417 | |
| 3418 | /* Disable temperature sensor */ |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3419 | ret = mv88e6xxx_mdio_write_direct(ps, 0x0, 0x1a, ret & ~(1 << 5)); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3420 | if (ret < 0) |
| 3421 | goto error; |
| 3422 | |
| 3423 | *temp = ((val & 0x1f) - 5) * 5; |
| 3424 | |
| 3425 | error: |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3426 | mv88e6xxx_mdio_write_direct(ps, 0x0, 0x16, 0x0); |
Vivien Didelot | 9f8b3ee | 2016-06-20 13:14:05 -0400 | [diff] [blame] | 3427 | mutex_unlock(&ps->reg_lock); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3428 | return ret; |
| 3429 | } |
| 3430 | |
| 3431 | static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp) |
| 3432 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3433 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3434 | int phy = mv88e6xxx_6320_family(ps) ? 3 : 0; |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3435 | int ret; |
| 3436 | |
| 3437 | *temp = 0; |
| 3438 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3439 | ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3440 | if (ret < 0) |
| 3441 | return ret; |
| 3442 | |
| 3443 | *temp = (ret & 0xff) - 25; |
| 3444 | |
| 3445 | return 0; |
| 3446 | } |
| 3447 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3448 | static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3449 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3450 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3451 | |
Vivien Didelot | 6594f61 | 2016-05-09 13:22:42 -0400 | [diff] [blame] | 3452 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP)) |
| 3453 | return -EOPNOTSUPP; |
| 3454 | |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3455 | if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps)) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3456 | return mv88e63xx_get_temp(ds, temp); |
| 3457 | |
| 3458 | return mv88e61xx_get_temp(ds, temp); |
| 3459 | } |
| 3460 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3461 | static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3462 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3463 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3464 | int phy = mv88e6xxx_6320_family(ps) ? 3 : 0; |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3465 | int ret; |
| 3466 | |
Vivien Didelot | 6594f61 | 2016-05-09 13:22:42 -0400 | [diff] [blame] | 3467 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT)) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3468 | return -EOPNOTSUPP; |
| 3469 | |
| 3470 | *temp = 0; |
| 3471 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3472 | ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3473 | if (ret < 0) |
| 3474 | return ret; |
| 3475 | |
| 3476 | *temp = (((ret >> 8) & 0x1f) * 5) - 25; |
| 3477 | |
| 3478 | return 0; |
| 3479 | } |
| 3480 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3481 | static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3482 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3483 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3484 | int phy = mv88e6xxx_6320_family(ps) ? 3 : 0; |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3485 | int ret; |
| 3486 | |
Vivien Didelot | 6594f61 | 2016-05-09 13:22:42 -0400 | [diff] [blame] | 3487 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT)) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3488 | return -EOPNOTSUPP; |
| 3489 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3490 | ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3491 | if (ret < 0) |
| 3492 | return ret; |
| 3493 | temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f); |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3494 | return mv88e6xxx_mdio_page_write(ds, phy, 6, 26, |
| 3495 | (ret & 0xe0ff) | (temp << 8)); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3498 | static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3499 | { |
Andrew Lunn | 158bc06 | 2016-04-28 21:24:06 -0400 | [diff] [blame] | 3500 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3501 | int phy = mv88e6xxx_6320_family(ps) ? 3 : 0; |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3502 | int ret; |
| 3503 | |
Vivien Didelot | 6594f61 | 2016-05-09 13:22:42 -0400 | [diff] [blame] | 3504 | if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT)) |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3505 | return -EOPNOTSUPP; |
| 3506 | |
| 3507 | *alarm = false; |
| 3508 | |
Andrew Lunn | 03a4a54 | 2016-06-04 21:17:05 +0200 | [diff] [blame] | 3509 | ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26); |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 3510 | if (ret < 0) |
| 3511 | return ret; |
| 3512 | |
| 3513 | *alarm = !!(ret & 0x40); |
| 3514 | |
| 3515 | return 0; |
| 3516 | } |
| 3517 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 3518 | |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3519 | static const struct mv88e6xxx_info mv88e6xxx_table[] = { |
| 3520 | [MV88E6085] = { |
| 3521 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6085, |
| 3522 | .family = MV88E6XXX_FAMILY_6097, |
| 3523 | .name = "Marvell 88E6085", |
| 3524 | .num_databases = 4096, |
| 3525 | .num_ports = 10, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3526 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3527 | .flags = MV88E6XXX_FLAGS_FAMILY_6097, |
| 3528 | }, |
| 3529 | |
| 3530 | [MV88E6095] = { |
| 3531 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6095, |
| 3532 | .family = MV88E6XXX_FAMILY_6095, |
| 3533 | .name = "Marvell 88E6095/88E6095F", |
| 3534 | .num_databases = 256, |
| 3535 | .num_ports = 11, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3536 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3537 | .flags = MV88E6XXX_FLAGS_FAMILY_6095, |
| 3538 | }, |
| 3539 | |
| 3540 | [MV88E6123] = { |
| 3541 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6123, |
| 3542 | .family = MV88E6XXX_FAMILY_6165, |
| 3543 | .name = "Marvell 88E6123", |
| 3544 | .num_databases = 4096, |
| 3545 | .num_ports = 3, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3546 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3547 | .flags = MV88E6XXX_FLAGS_FAMILY_6165, |
| 3548 | }, |
| 3549 | |
| 3550 | [MV88E6131] = { |
| 3551 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6131, |
| 3552 | .family = MV88E6XXX_FAMILY_6185, |
| 3553 | .name = "Marvell 88E6131", |
| 3554 | .num_databases = 256, |
| 3555 | .num_ports = 8, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3556 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3557 | .flags = MV88E6XXX_FLAGS_FAMILY_6185, |
| 3558 | }, |
| 3559 | |
| 3560 | [MV88E6161] = { |
| 3561 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6161, |
| 3562 | .family = MV88E6XXX_FAMILY_6165, |
| 3563 | .name = "Marvell 88E6161", |
| 3564 | .num_databases = 4096, |
| 3565 | .num_ports = 6, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3566 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3567 | .flags = MV88E6XXX_FLAGS_FAMILY_6165, |
| 3568 | }, |
| 3569 | |
| 3570 | [MV88E6165] = { |
| 3571 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6165, |
| 3572 | .family = MV88E6XXX_FAMILY_6165, |
| 3573 | .name = "Marvell 88E6165", |
| 3574 | .num_databases = 4096, |
| 3575 | .num_ports = 6, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3576 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3577 | .flags = MV88E6XXX_FLAGS_FAMILY_6165, |
| 3578 | }, |
| 3579 | |
| 3580 | [MV88E6171] = { |
| 3581 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6171, |
| 3582 | .family = MV88E6XXX_FAMILY_6351, |
| 3583 | .name = "Marvell 88E6171", |
| 3584 | .num_databases = 4096, |
| 3585 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3586 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3587 | .flags = MV88E6XXX_FLAGS_FAMILY_6351, |
| 3588 | }, |
| 3589 | |
| 3590 | [MV88E6172] = { |
| 3591 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6172, |
| 3592 | .family = MV88E6XXX_FAMILY_6352, |
| 3593 | .name = "Marvell 88E6172", |
| 3594 | .num_databases = 4096, |
| 3595 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3596 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3597 | .flags = MV88E6XXX_FLAGS_FAMILY_6352, |
| 3598 | }, |
| 3599 | |
| 3600 | [MV88E6175] = { |
| 3601 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6175, |
| 3602 | .family = MV88E6XXX_FAMILY_6351, |
| 3603 | .name = "Marvell 88E6175", |
| 3604 | .num_databases = 4096, |
| 3605 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3606 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3607 | .flags = MV88E6XXX_FLAGS_FAMILY_6351, |
| 3608 | }, |
| 3609 | |
| 3610 | [MV88E6176] = { |
| 3611 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6176, |
| 3612 | .family = MV88E6XXX_FAMILY_6352, |
| 3613 | .name = "Marvell 88E6176", |
| 3614 | .num_databases = 4096, |
| 3615 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3616 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3617 | .flags = MV88E6XXX_FLAGS_FAMILY_6352, |
| 3618 | }, |
| 3619 | |
| 3620 | [MV88E6185] = { |
| 3621 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6185, |
| 3622 | .family = MV88E6XXX_FAMILY_6185, |
| 3623 | .name = "Marvell 88E6185", |
| 3624 | .num_databases = 256, |
| 3625 | .num_ports = 10, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3626 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3627 | .flags = MV88E6XXX_FLAGS_FAMILY_6185, |
| 3628 | }, |
| 3629 | |
| 3630 | [MV88E6240] = { |
| 3631 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6240, |
| 3632 | .family = MV88E6XXX_FAMILY_6352, |
| 3633 | .name = "Marvell 88E6240", |
| 3634 | .num_databases = 4096, |
| 3635 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3636 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3637 | .flags = MV88E6XXX_FLAGS_FAMILY_6352, |
| 3638 | }, |
| 3639 | |
| 3640 | [MV88E6320] = { |
| 3641 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6320, |
| 3642 | .family = MV88E6XXX_FAMILY_6320, |
| 3643 | .name = "Marvell 88E6320", |
| 3644 | .num_databases = 4096, |
| 3645 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3646 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3647 | .flags = MV88E6XXX_FLAGS_FAMILY_6320, |
| 3648 | }, |
| 3649 | |
| 3650 | [MV88E6321] = { |
| 3651 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6321, |
| 3652 | .family = MV88E6XXX_FAMILY_6320, |
| 3653 | .name = "Marvell 88E6321", |
| 3654 | .num_databases = 4096, |
| 3655 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3656 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3657 | .flags = MV88E6XXX_FLAGS_FAMILY_6320, |
| 3658 | }, |
| 3659 | |
| 3660 | [MV88E6350] = { |
| 3661 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6350, |
| 3662 | .family = MV88E6XXX_FAMILY_6351, |
| 3663 | .name = "Marvell 88E6350", |
| 3664 | .num_databases = 4096, |
| 3665 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3666 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3667 | .flags = MV88E6XXX_FLAGS_FAMILY_6351, |
| 3668 | }, |
| 3669 | |
| 3670 | [MV88E6351] = { |
| 3671 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6351, |
| 3672 | .family = MV88E6XXX_FAMILY_6351, |
| 3673 | .name = "Marvell 88E6351", |
| 3674 | .num_databases = 4096, |
| 3675 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3676 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3677 | .flags = MV88E6XXX_FLAGS_FAMILY_6351, |
| 3678 | }, |
| 3679 | |
| 3680 | [MV88E6352] = { |
| 3681 | .prod_num = PORT_SWITCH_ID_PROD_NUM_6352, |
| 3682 | .family = MV88E6XXX_FAMILY_6352, |
| 3683 | .name = "Marvell 88E6352", |
| 3684 | .num_databases = 4096, |
| 3685 | .num_ports = 7, |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3686 | .port_base_addr = 0x10, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3687 | .flags = MV88E6XXX_FLAGS_FAMILY_6352, |
| 3688 | }, |
| 3689 | }; |
| 3690 | |
Vivien Didelot | 5f7c036 | 2016-06-20 13:14:04 -0400 | [diff] [blame] | 3691 | static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num) |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3692 | { |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3693 | int i; |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3694 | |
Vivien Didelot | 5f7c036 | 2016-06-20 13:14:04 -0400 | [diff] [blame] | 3695 | for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i) |
| 3696 | if (mv88e6xxx_table[i].prod_num == prod_num) |
| 3697 | return &mv88e6xxx_table[i]; |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3698 | |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3699 | return NULL; |
| 3700 | } |
| 3701 | |
Vivien Didelot | bc46a3d | 2016-06-20 13:14:08 -0400 | [diff] [blame] | 3702 | static int mv88e6xxx_detect(struct mv88e6xxx_priv_state *ps) |
| 3703 | { |
| 3704 | const struct mv88e6xxx_info *info; |
| 3705 | int id, prod_num, rev; |
| 3706 | |
Vivien Didelot | 9dddd47 | 2016-06-20 13:14:10 -0400 | [diff] [blame] | 3707 | id = mv88e6xxx_reg_read(ps, ps->info->port_base_addr, PORT_SWITCH_ID); |
Vivien Didelot | bc46a3d | 2016-06-20 13:14:08 -0400 | [diff] [blame] | 3708 | if (id < 0) |
| 3709 | return id; |
| 3710 | |
| 3711 | prod_num = (id & 0xfff0) >> 4; |
| 3712 | rev = id & 0x000f; |
| 3713 | |
| 3714 | info = mv88e6xxx_lookup_info(prod_num); |
| 3715 | if (!info) |
| 3716 | return -ENODEV; |
| 3717 | |
Vivien Didelot | caac854 | 2016-06-20 13:14:09 -0400 | [diff] [blame] | 3718 | /* Update the compatible info with the probed one */ |
Vivien Didelot | bc46a3d | 2016-06-20 13:14:08 -0400 | [diff] [blame] | 3719 | ps->info = info; |
| 3720 | |
| 3721 | dev_info(ps->dev, "switch 0x%x detected: %s, revision %u\n", |
| 3722 | ps->info->prod_num, ps->info->name, rev); |
| 3723 | |
| 3724 | return 0; |
| 3725 | } |
| 3726 | |
Vivien Didelot | 469d729 | 2016-06-20 13:14:06 -0400 | [diff] [blame] | 3727 | static struct mv88e6xxx_priv_state *mv88e6xxx_alloc_chip(struct device *dev) |
| 3728 | { |
| 3729 | struct mv88e6xxx_priv_state *ps; |
| 3730 | |
| 3731 | ps = devm_kzalloc(dev, sizeof(*ps), GFP_KERNEL); |
| 3732 | if (!ps) |
| 3733 | return NULL; |
| 3734 | |
| 3735 | ps->dev = dev; |
| 3736 | |
| 3737 | mutex_init(&ps->reg_lock); |
| 3738 | |
| 3739 | return ps; |
| 3740 | } |
| 3741 | |
Vivien Didelot | 4a70c4a | 2016-06-20 13:14:07 -0400 | [diff] [blame] | 3742 | static int mv88e6xxx_smi_init(struct mv88e6xxx_priv_state *ps, |
| 3743 | struct mii_bus *bus, int sw_addr) |
| 3744 | { |
| 3745 | /* ADDR[0] pin is unavailable externally and considered zero */ |
| 3746 | if (sw_addr & 0x1) |
| 3747 | return -EINVAL; |
| 3748 | |
Vivien Didelot | 914b32f | 2016-06-20 13:14:11 -0400 | [diff] [blame] | 3749 | if (sw_addr == 0) |
| 3750 | ps->smi_ops = &mv88e6xxx_smi_single_chip_ops; |
| 3751 | else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_MULTI_CHIP)) |
| 3752 | ps->smi_ops = &mv88e6xxx_smi_multi_chip_ops; |
| 3753 | else |
| 3754 | return -EINVAL; |
| 3755 | |
Vivien Didelot | 4a70c4a | 2016-06-20 13:14:07 -0400 | [diff] [blame] | 3756 | ps->bus = bus; |
| 3757 | ps->sw_addr = sw_addr; |
| 3758 | |
| 3759 | return 0; |
| 3760 | } |
| 3761 | |
Andrew Lunn | fcdce7d | 2016-05-10 23:27:20 +0200 | [diff] [blame] | 3762 | static const char *mv88e6xxx_drv_probe(struct device *dsa_dev, |
| 3763 | struct device *host_dev, int sw_addr, |
| 3764 | void **priv) |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3765 | { |
| 3766 | struct mv88e6xxx_priv_state *ps; |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3767 | struct mii_bus *bus; |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3768 | int err; |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3769 | |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3770 | bus = dsa_host_dev_to_mii_bus(host_dev); |
Andrew Lunn | c156913 | 2016-04-13 02:40:45 +0200 | [diff] [blame] | 3771 | if (!bus) |
| 3772 | return NULL; |
| 3773 | |
Vivien Didelot | 469d729 | 2016-06-20 13:14:06 -0400 | [diff] [blame] | 3774 | ps = mv88e6xxx_alloc_chip(dsa_dev); |
| 3775 | if (!ps) |
| 3776 | return NULL; |
| 3777 | |
Vivien Didelot | caac854 | 2016-06-20 13:14:09 -0400 | [diff] [blame] | 3778 | /* Legacy SMI probing will only support chips similar to 88E6085 */ |
| 3779 | ps->info = &mv88e6xxx_table[MV88E6085]; |
| 3780 | |
Vivien Didelot | 4a70c4a | 2016-06-20 13:14:07 -0400 | [diff] [blame] | 3781 | err = mv88e6xxx_smi_init(ps, bus, sw_addr); |
| 3782 | if (err) |
| 3783 | goto free; |
| 3784 | |
Vivien Didelot | bc46a3d | 2016-06-20 13:14:08 -0400 | [diff] [blame] | 3785 | err = mv88e6xxx_detect(ps); |
| 3786 | if (err) |
Vivien Didelot | 469d729 | 2016-06-20 13:14:06 -0400 | [diff] [blame] | 3787 | goto free; |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3788 | |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3789 | err = mv88e6xxx_mdio_register(ps, NULL); |
| 3790 | if (err) |
Vivien Didelot | 469d729 | 2016-06-20 13:14:06 -0400 | [diff] [blame] | 3791 | goto free; |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3792 | |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3793 | *priv = ps; |
| 3794 | |
Vivien Didelot | bc46a3d | 2016-06-20 13:14:08 -0400 | [diff] [blame] | 3795 | return ps->info->name; |
Vivien Didelot | 469d729 | 2016-06-20 13:14:06 -0400 | [diff] [blame] | 3796 | free: |
| 3797 | devm_kfree(dsa_dev, ps); |
| 3798 | |
| 3799 | return NULL; |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3800 | } |
| 3801 | |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 3802 | static struct dsa_switch_driver mv88e6xxx_switch_driver = { |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3803 | .tag_protocol = DSA_TAG_PROTO_EDSA, |
Andrew Lunn | fcdce7d | 2016-05-10 23:27:20 +0200 | [diff] [blame] | 3804 | .probe = mv88e6xxx_drv_probe, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3805 | .setup = mv88e6xxx_setup, |
| 3806 | .set_addr = mv88e6xxx_set_addr, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3807 | .adjust_link = mv88e6xxx_adjust_link, |
| 3808 | .get_strings = mv88e6xxx_get_strings, |
| 3809 | .get_ethtool_stats = mv88e6xxx_get_ethtool_stats, |
| 3810 | .get_sset_count = mv88e6xxx_get_sset_count, |
| 3811 | .set_eee = mv88e6xxx_set_eee, |
| 3812 | .get_eee = mv88e6xxx_get_eee, |
| 3813 | #ifdef CONFIG_NET_DSA_HWMON |
| 3814 | .get_temp = mv88e6xxx_get_temp, |
| 3815 | .get_temp_limit = mv88e6xxx_get_temp_limit, |
| 3816 | .set_temp_limit = mv88e6xxx_set_temp_limit, |
| 3817 | .get_temp_alarm = mv88e6xxx_get_temp_alarm, |
| 3818 | #endif |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 3819 | .get_eeprom_len = mv88e6xxx_get_eeprom_len, |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3820 | .get_eeprom = mv88e6xxx_get_eeprom, |
| 3821 | .set_eeprom = mv88e6xxx_set_eeprom, |
| 3822 | .get_regs_len = mv88e6xxx_get_regs_len, |
| 3823 | .get_regs = mv88e6xxx_get_regs, |
| 3824 | .port_bridge_join = mv88e6xxx_port_bridge_join, |
| 3825 | .port_bridge_leave = mv88e6xxx_port_bridge_leave, |
| 3826 | .port_stp_state_set = mv88e6xxx_port_stp_state_set, |
| 3827 | .port_vlan_filtering = mv88e6xxx_port_vlan_filtering, |
| 3828 | .port_vlan_prepare = mv88e6xxx_port_vlan_prepare, |
| 3829 | .port_vlan_add = mv88e6xxx_port_vlan_add, |
| 3830 | .port_vlan_del = mv88e6xxx_port_vlan_del, |
| 3831 | .port_vlan_dump = mv88e6xxx_port_vlan_dump, |
| 3832 | .port_fdb_prepare = mv88e6xxx_port_fdb_prepare, |
| 3833 | .port_fdb_add = mv88e6xxx_port_fdb_add, |
| 3834 | .port_fdb_del = mv88e6xxx_port_fdb_del, |
| 3835 | .port_fdb_dump = mv88e6xxx_port_fdb_dump, |
| 3836 | }; |
| 3837 | |
Vivien Didelot | b7e66a5 | 2016-06-20 13:14:02 -0400 | [diff] [blame] | 3838 | static int mv88e6xxx_register_switch(struct mv88e6xxx_priv_state *ps, |
| 3839 | struct device_node *np) |
| 3840 | { |
| 3841 | struct device *dev = ps->dev; |
| 3842 | struct dsa_switch *ds; |
| 3843 | |
| 3844 | ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); |
| 3845 | if (!ds) |
| 3846 | return -ENOMEM; |
| 3847 | |
| 3848 | ds->dev = dev; |
| 3849 | ds->priv = ps; |
| 3850 | ds->drv = &mv88e6xxx_switch_driver; |
| 3851 | |
| 3852 | dev_set_drvdata(dev, ds); |
| 3853 | |
| 3854 | return dsa_register_switch(ds, np); |
| 3855 | } |
| 3856 | |
| 3857 | static void mv88e6xxx_unregister_switch(struct mv88e6xxx_priv_state *ps) |
| 3858 | { |
| 3859 | dsa_unregister_switch(ps->ds); |
| 3860 | } |
| 3861 | |
Vivien Didelot | 57d3231 | 2016-06-20 13:13:58 -0400 | [diff] [blame] | 3862 | static int mv88e6xxx_probe(struct mdio_device *mdiodev) |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3863 | { |
| 3864 | struct device *dev = &mdiodev->dev; |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 3865 | struct device_node *np = dev->of_node; |
Vivien Didelot | caac854 | 2016-06-20 13:14:09 -0400 | [diff] [blame] | 3866 | const struct mv88e6xxx_info *compat_info; |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3867 | struct mv88e6xxx_priv_state *ps; |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 3868 | u32 eeprom_len; |
Andrew Lunn | 52638f7 | 2016-05-10 23:27:22 +0200 | [diff] [blame] | 3869 | int err; |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3870 | |
Vivien Didelot | caac854 | 2016-06-20 13:14:09 -0400 | [diff] [blame] | 3871 | compat_info = of_device_get_match_data(dev); |
| 3872 | if (!compat_info) |
| 3873 | return -EINVAL; |
| 3874 | |
Vivien Didelot | 469d729 | 2016-06-20 13:14:06 -0400 | [diff] [blame] | 3875 | ps = mv88e6xxx_alloc_chip(dev); |
Vivien Didelot | b7e66a5 | 2016-06-20 13:14:02 -0400 | [diff] [blame] | 3876 | if (!ps) |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3877 | return -ENOMEM; |
| 3878 | |
Vivien Didelot | caac854 | 2016-06-20 13:14:09 -0400 | [diff] [blame] | 3879 | ps->info = compat_info; |
| 3880 | |
Vivien Didelot | 4a70c4a | 2016-06-20 13:14:07 -0400 | [diff] [blame] | 3881 | err = mv88e6xxx_smi_init(ps, mdiodev->bus, mdiodev->addr); |
| 3882 | if (err) |
| 3883 | return err; |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3884 | |
Vivien Didelot | bc46a3d | 2016-06-20 13:14:08 -0400 | [diff] [blame] | 3885 | err = mv88e6xxx_detect(ps); |
| 3886 | if (err) |
| 3887 | return err; |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3888 | |
Vivien Didelot | c6d19ab | 2016-06-20 13:14:03 -0400 | [diff] [blame] | 3889 | ps->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS); |
| 3890 | if (IS_ERR(ps->reset)) |
| 3891 | return PTR_ERR(ps->reset); |
Andrew Lunn | 52638f7 | 2016-05-10 23:27:22 +0200 | [diff] [blame] | 3892 | |
Andrew Lunn | f8cd875 | 2016-05-10 23:27:25 +0200 | [diff] [blame] | 3893 | if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM) && |
| 3894 | !of_property_read_u32(np, "eeprom-length", &eeprom_len)) |
| 3895 | ps->eeprom_len = eeprom_len; |
| 3896 | |
Vivien Didelot | aa8ac39 | 2016-06-20 13:14:00 -0400 | [diff] [blame] | 3897 | err = mv88e6xxx_mdio_register(ps, np); |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3898 | if (err) |
| 3899 | return err; |
| 3900 | |
Vivien Didelot | b7e66a5 | 2016-06-20 13:14:02 -0400 | [diff] [blame] | 3901 | err = mv88e6xxx_register_switch(ps, np); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 3902 | if (err) { |
| 3903 | mv88e6xxx_mdio_unregister(ps); |
| 3904 | return err; |
| 3905 | } |
| 3906 | |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3907 | return 0; |
| 3908 | } |
| 3909 | |
| 3910 | static void mv88e6xxx_remove(struct mdio_device *mdiodev) |
| 3911 | { |
| 3912 | struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev); |
| 3913 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 3914 | |
Vivien Didelot | b7e66a5 | 2016-06-20 13:14:02 -0400 | [diff] [blame] | 3915 | mv88e6xxx_unregister_switch(ps); |
Andrew Lunn | b516d45 | 2016-06-04 21:17:06 +0200 | [diff] [blame] | 3916 | mv88e6xxx_mdio_unregister(ps); |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | static const struct of_device_id mv88e6xxx_of_match[] = { |
Vivien Didelot | caac854 | 2016-06-20 13:14:09 -0400 | [diff] [blame] | 3920 | { |
| 3921 | .compatible = "marvell,mv88e6085", |
| 3922 | .data = &mv88e6xxx_table[MV88E6085], |
| 3923 | }, |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3924 | { /* sentinel */ }, |
| 3925 | }; |
| 3926 | |
| 3927 | MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match); |
| 3928 | |
| 3929 | static struct mdio_driver mv88e6xxx_driver = { |
| 3930 | .probe = mv88e6xxx_probe, |
| 3931 | .remove = mv88e6xxx_remove, |
| 3932 | .mdiodrv.driver = { |
| 3933 | .name = "mv88e6085", |
| 3934 | .of_match_table = mv88e6xxx_of_match, |
| 3935 | }, |
| 3936 | }; |
| 3937 | |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3938 | static int __init mv88e6xxx_init(void) |
| 3939 | { |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3940 | register_switch_driver(&mv88e6xxx_switch_driver); |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3941 | return mdio_driver_register(&mv88e6xxx_driver); |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3942 | } |
| 3943 | module_init(mv88e6xxx_init); |
| 3944 | |
| 3945 | static void __exit mv88e6xxx_cleanup(void) |
| 3946 | { |
Andrew Lunn | 14c7b3c | 2016-05-10 23:27:21 +0200 | [diff] [blame] | 3947 | mdio_driver_unregister(&mv88e6xxx_driver); |
Vivien Didelot | f81ec90 | 2016-05-09 13:22:58 -0400 | [diff] [blame] | 3948 | unregister_switch_driver(&mv88e6xxx_switch_driver); |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3949 | } |
| 3950 | module_exit(mv88e6xxx_cleanup); |
Ben Hutchings | 3d825ed | 2011-11-25 14:37:16 +0000 | [diff] [blame] | 3951 | |
| 3952 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
| 3953 | MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips"); |
| 3954 | MODULE_LICENSE("GPL"); |