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