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 | * |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 14 | #include <linux/delay.h> |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 15 | #include <linux/etherdevice.h> |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 16 | #include <linux/ethtool.h> |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 17 | #include <linux/if_bridge.h> |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 18 | #include <linux/jiffies.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 19 | #include <linux/list.h> |
Paul Gortmaker | 2bbba27 | 2012-01-24 10:41:40 +0000 | [diff] [blame] | 20 | #include <linux/module.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 21 | #include <linux/netdevice.h> |
Andrew Lunn | c8c1b39 | 2015-11-20 03:56:24 +0100 | [diff] [blame] | 22 | #include <linux/gpio/consumer.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 23 | #include <linux/phy.h> |
Ben Hutchings | c8f0b86 | 2011-11-27 17:06:08 +0000 | [diff] [blame] | 24 | #include <net/dsa.h> |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 25 | #include <net/switchdev.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 26 | #include "mv88e6xxx.h" |
| 27 | |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 28 | static void assert_smi_lock(struct dsa_switch *ds) |
| 29 | { |
| 30 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 31 | |
| 32 | if (unlikely(!mutex_is_locked(&ps->smi_mutex))) { |
| 33 | dev_err(ds->master_dev, "SMI lock not held!\n"); |
| 34 | dump_stack(); |
| 35 | } |
| 36 | } |
| 37 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 38 | /* If the switch's ADDR[4:0] strap pins are strapped to zero, it will |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 39 | * use all 32 SMI bus addresses on its SMI bus, and all switch registers |
| 40 | * will be directly accessible on some {device address,register address} |
| 41 | * pair. If the ADDR[4:0] pins are not strapped to zero, the switch |
| 42 | * will only respond to SMI transactions to that specific address, and |
| 43 | * an indirect addressing mechanism needs to be used to access its |
| 44 | * registers. |
| 45 | */ |
| 46 | static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr) |
| 47 | { |
| 48 | int ret; |
| 49 | int i; |
| 50 | |
| 51 | for (i = 0; i < 16; i++) { |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 52 | ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 53 | if (ret < 0) |
| 54 | return ret; |
| 55 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 56 | if ((ret & SMI_CMD_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | return -ETIMEDOUT; |
| 61 | } |
| 62 | |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 63 | static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, |
| 64 | int reg) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 65 | { |
| 66 | int ret; |
| 67 | |
| 68 | if (sw_addr == 0) |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 69 | return mdiobus_read_nested(bus, addr, reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 70 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 71 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 72 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 73 | if (ret < 0) |
| 74 | return ret; |
| 75 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 76 | /* Transmit the read command. */ |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 77 | ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD, |
| 78 | SMI_CMD_OP_22_READ | (addr << 5) | reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 79 | if (ret < 0) |
| 80 | return ret; |
| 81 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 82 | /* Wait for the read command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 83 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 84 | if (ret < 0) |
| 85 | return ret; |
| 86 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 87 | /* Read the data. */ |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 88 | ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 89 | if (ret < 0) |
| 90 | return ret; |
| 91 | |
| 92 | return ret & 0xffff; |
| 93 | } |
| 94 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 95 | static int _mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 96 | { |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 97 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 98 | int ret; |
| 99 | |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 100 | assert_smi_lock(ds); |
| 101 | |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 102 | ret = __mv88e6xxx_reg_read(ps->bus, ps->sw_addr, addr, reg); |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 103 | if (ret < 0) |
| 104 | return ret; |
| 105 | |
| 106 | dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 107 | addr, reg, ret); |
| 108 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 109 | return ret; |
| 110 | } |
| 111 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 112 | int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg) |
| 113 | { |
| 114 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 115 | int ret; |
| 116 | |
| 117 | mutex_lock(&ps->smi_mutex); |
| 118 | ret = _mv88e6xxx_reg_read(ds, addr, reg); |
| 119 | mutex_unlock(&ps->smi_mutex); |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 124 | static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr, |
| 125 | int reg, u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 126 | { |
| 127 | int ret; |
| 128 | |
| 129 | if (sw_addr == 0) |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 130 | return mdiobus_write_nested(bus, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 131 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 132 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 133 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 134 | if (ret < 0) |
| 135 | return ret; |
| 136 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 137 | /* Transmit the data to write. */ |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 138 | ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 142 | /* Transmit the write command. */ |
Neil Armstrong | 6e899e6 | 2015-10-22 10:37:53 +0200 | [diff] [blame] | 143 | ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD, |
| 144 | SMI_CMD_OP_22_WRITE | (addr << 5) | reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 145 | if (ret < 0) |
| 146 | return ret; |
| 147 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 148 | /* Wait for the write command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 149 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 150 | if (ret < 0) |
| 151 | return ret; |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 156 | static int _mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, |
| 157 | u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 158 | { |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 159 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 160 | |
Vivien Didelot | 3996a4f | 2015-10-30 18:56:45 -0400 | [diff] [blame] | 161 | assert_smi_lock(ds); |
| 162 | |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 163 | dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 164 | addr, reg, val); |
| 165 | |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 166 | return __mv88e6xxx_reg_write(ps->bus, ps->sw_addr, addr, reg, val); |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val) |
| 170 | { |
| 171 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 172 | int ret; |
| 173 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 174 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | 8d6d09e | 2015-03-26 18:36:31 -0700 | [diff] [blame] | 175 | ret = _mv88e6xxx_reg_write(ds, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 176 | mutex_unlock(&ps->smi_mutex); |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 181 | int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr) |
| 182 | { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 183 | int err; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 184 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 185 | err = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_MAC_01, |
| 186 | (addr[0] << 8) | addr[1]); |
| 187 | if (err) |
| 188 | return err; |
| 189 | |
| 190 | err = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_MAC_23, |
| 191 | (addr[2] << 8) | addr[3]); |
| 192 | if (err) |
| 193 | return err; |
| 194 | |
| 195 | return mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_MAC_45, |
| 196 | (addr[4] << 8) | addr[5]); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 199 | int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr) |
| 200 | { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 201 | int ret; |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 202 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 203 | |
| 204 | for (i = 0; i < 6; i++) { |
| 205 | int j; |
| 206 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 207 | /* Write the MAC address byte. */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 208 | ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, |
| 209 | GLOBAL2_SWITCH_MAC_BUSY | |
| 210 | (i << 8) | addr[i]); |
| 211 | if (ret) |
| 212 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 213 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 214 | /* Wait for the write to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 215 | for (j = 0; j < 16; j++) { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 216 | ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, |
| 217 | GLOBAL2_SWITCH_MAC); |
| 218 | if (ret < 0) |
| 219 | return ret; |
| 220 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 221 | if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 222 | break; |
| 223 | } |
| 224 | if (j == 16) |
| 225 | return -ETIMEDOUT; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 231 | static int _mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 232 | { |
| 233 | if (addr >= 0) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 234 | return _mv88e6xxx_reg_read(ds, addr, regnum); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 235 | return 0xffff; |
| 236 | } |
| 237 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 238 | static int _mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, |
| 239 | u16 val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 240 | { |
| 241 | if (addr >= 0) |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 242 | return _mv88e6xxx_reg_write(ds, addr, regnum, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 246 | #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU |
| 247 | static int mv88e6xxx_ppu_disable(struct dsa_switch *ds) |
| 248 | { |
| 249 | int ret; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 250 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 251 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 252 | ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_CONTROL); |
| 253 | if (ret < 0) |
| 254 | return ret; |
| 255 | |
| 256 | ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_CONTROL, |
| 257 | ret & ~GLOBAL_CONTROL_PPU_ENABLE); |
| 258 | if (ret) |
| 259 | return ret; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 260 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 261 | timeout = jiffies + 1 * HZ; |
| 262 | while (time_before(jiffies, timeout)) { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 263 | ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATUS); |
| 264 | if (ret < 0) |
| 265 | return ret; |
| 266 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 267 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 268 | if ((ret & GLOBAL_STATUS_PPU_MASK) != |
| 269 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 270 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | return -ETIMEDOUT; |
| 274 | } |
| 275 | |
| 276 | static int mv88e6xxx_ppu_enable(struct dsa_switch *ds) |
| 277 | { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 278 | int ret, err; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 279 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 280 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 281 | ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_CONTROL); |
| 282 | if (ret < 0) |
| 283 | return ret; |
| 284 | |
| 285 | err = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_CONTROL, |
| 286 | ret | GLOBAL_CONTROL_PPU_ENABLE); |
| 287 | if (err) |
| 288 | return err; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 289 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 290 | timeout = jiffies + 1 * HZ; |
| 291 | while (time_before(jiffies, timeout)) { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 292 | ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATUS); |
| 293 | if (ret < 0) |
| 294 | return ret; |
| 295 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 296 | usleep_range(1000, 2000); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 297 | if ((ret & GLOBAL_STATUS_PPU_MASK) == |
| 298 | GLOBAL_STATUS_PPU_POLLING) |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 299 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | return -ETIMEDOUT; |
| 303 | } |
| 304 | |
| 305 | static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly) |
| 306 | { |
| 307 | struct mv88e6xxx_priv_state *ps; |
| 308 | |
| 309 | ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work); |
| 310 | if (mutex_trylock(&ps->ppu_mutex)) { |
Andrew Lunn | 7543a6d | 2016-04-13 02:40:40 +0200 | [diff] [blame] | 311 | struct dsa_switch *ds = ps->ds; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 312 | |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 313 | if (mv88e6xxx_ppu_enable(ds) == 0) |
| 314 | ps->ppu_disabled = 0; |
| 315 | mutex_unlock(&ps->ppu_mutex); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps) |
| 320 | { |
| 321 | struct mv88e6xxx_priv_state *ps = (void *)_ps; |
| 322 | |
| 323 | schedule_work(&ps->ppu_work); |
| 324 | } |
| 325 | |
| 326 | static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds) |
| 327 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 328 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 329 | int ret; |
| 330 | |
| 331 | mutex_lock(&ps->ppu_mutex); |
| 332 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 333 | /* If the PHY polling unit is enabled, disable it so that |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 334 | * we can access the PHY registers. If it was already |
| 335 | * disabled, cancel the timer that is going to re-enable |
| 336 | * it. |
| 337 | */ |
| 338 | if (!ps->ppu_disabled) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 339 | ret = mv88e6xxx_ppu_disable(ds); |
| 340 | if (ret < 0) { |
| 341 | mutex_unlock(&ps->ppu_mutex); |
| 342 | return ret; |
| 343 | } |
| 344 | ps->ppu_disabled = 1; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 345 | } else { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 346 | del_timer(&ps->ppu_timer); |
| 347 | ret = 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds) |
| 354 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 355 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 356 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 357 | /* Schedule a timer to re-enable the PHY polling unit. */ |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 358 | mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10)); |
| 359 | mutex_unlock(&ps->ppu_mutex); |
| 360 | } |
| 361 | |
| 362 | void mv88e6xxx_ppu_state_init(struct dsa_switch *ds) |
| 363 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 364 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 365 | |
| 366 | mutex_init(&ps->ppu_mutex); |
| 367 | INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work); |
| 368 | init_timer(&ps->ppu_timer); |
| 369 | ps->ppu_timer.data = (unsigned long)ps; |
| 370 | ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer; |
| 371 | } |
| 372 | |
| 373 | int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum) |
| 374 | { |
| 375 | int ret; |
| 376 | |
| 377 | ret = mv88e6xxx_ppu_access_get(ds); |
| 378 | if (ret >= 0) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 379 | ret = mv88e6xxx_reg_read(ds, addr, regnum); |
| 380 | mv88e6xxx_ppu_access_put(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr, |
| 387 | int regnum, u16 val) |
| 388 | { |
| 389 | int ret; |
| 390 | |
| 391 | ret = mv88e6xxx_ppu_access_get(ds); |
| 392 | if (ret >= 0) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 393 | ret = mv88e6xxx_reg_write(ds, addr, regnum, val); |
| 394 | mv88e6xxx_ppu_access_put(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | return ret; |
| 398 | } |
| 399 | #endif |
| 400 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 401 | static bool mv88e6xxx_6065_family(struct dsa_switch *ds) |
| 402 | { |
| 403 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 404 | |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 405 | return ps->info->family == MV88E6XXX_FAMILY_6065; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static bool mv88e6xxx_6095_family(struct dsa_switch *ds) |
| 409 | { |
| 410 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 411 | |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 412 | return ps->info->family == MV88E6XXX_FAMILY_6095; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static bool mv88e6xxx_6097_family(struct dsa_switch *ds) |
| 416 | { |
| 417 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 418 | |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 419 | return ps->info->family == MV88E6XXX_FAMILY_6097; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static bool mv88e6xxx_6165_family(struct dsa_switch *ds) |
| 423 | { |
| 424 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 425 | |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 426 | return ps->info->family == MV88E6XXX_FAMILY_6165; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static bool mv88e6xxx_6185_family(struct dsa_switch *ds) |
| 430 | { |
| 431 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 432 | |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 433 | return ps->info->family == MV88E6XXX_FAMILY_6185; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 436 | static bool mv88e6xxx_6320_family(struct dsa_switch *ds) |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 437 | { |
| 438 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 439 | |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 440 | return ps->info->family == MV88E6XXX_FAMILY_6320; |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 443 | static bool mv88e6xxx_6351_family(struct dsa_switch *ds) |
| 444 | { |
| 445 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 446 | |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 447 | return ps->info->family == MV88E6XXX_FAMILY_6351; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 450 | static bool mv88e6xxx_6352_family(struct dsa_switch *ds) |
| 451 | { |
| 452 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 453 | |
Vivien Didelot | 2235647 | 2016-04-17 13:24:00 -0400 | [diff] [blame] | 454 | return ps->info->family == MV88E6XXX_FAMILY_6352; |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 457 | static unsigned int mv88e6xxx_num_databases(struct dsa_switch *ds) |
| 458 | { |
| 459 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 460 | |
| 461 | /* The following devices have 4-bit identifiers for 16 databases */ |
| 462 | if (ps->id == PORT_SWITCH_ID_6061) |
| 463 | return 16; |
| 464 | |
| 465 | /* The following devices have 6-bit identifiers for 64 databases */ |
| 466 | if (ps->id == PORT_SWITCH_ID_6065) |
| 467 | return 64; |
| 468 | |
| 469 | /* The following devices have 8-bit identifiers for 256 databases */ |
| 470 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) |
| 471 | return 256; |
| 472 | |
| 473 | /* The following devices have 12-bit identifiers for 4096 databases */ |
| 474 | if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) || |
| 475 | mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) |
| 476 | return 4096; |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 481 | static bool mv88e6xxx_has_fid_reg(struct dsa_switch *ds) |
| 482 | { |
| 483 | /* Does the device have dedicated FID registers for ATU and VTU ops? */ |
| 484 | if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) || |
| 485 | mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) |
| 486 | return true; |
| 487 | |
| 488 | return false; |
| 489 | } |
| 490 | |
Vivien Didelot | 2e7bd5e | 2016-03-31 16:53:41 -0400 | [diff] [blame] | 491 | static bool mv88e6xxx_has_stu(struct dsa_switch *ds) |
| 492 | { |
| 493 | /* Does the device have STU and dedicated SID registers for VTU ops? */ |
| 494 | if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) || |
| 495 | mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) |
| 496 | return true; |
| 497 | |
| 498 | return false; |
| 499 | } |
| 500 | |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 501 | /* We expect the switch to perform auto negotiation if there is a real |
| 502 | * phy. However, in the case of a fixed link phy, we force the port |
| 503 | * settings from the fixed link settings. |
| 504 | */ |
| 505 | void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port, |
| 506 | struct phy_device *phydev) |
| 507 | { |
| 508 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 4905287 | 2015-09-29 01:53:48 +0200 | [diff] [blame] | 509 | u32 reg; |
| 510 | int ret; |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 511 | |
| 512 | if (!phy_is_pseudo_fixed_link(phydev)) |
| 513 | return; |
| 514 | |
| 515 | mutex_lock(&ps->smi_mutex); |
| 516 | |
| 517 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL); |
| 518 | if (ret < 0) |
| 519 | goto out; |
| 520 | |
| 521 | reg = ret & ~(PORT_PCS_CTRL_LINK_UP | |
| 522 | PORT_PCS_CTRL_FORCE_LINK | |
| 523 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 524 | PORT_PCS_CTRL_FORCE_DUPLEX | |
| 525 | PORT_PCS_CTRL_UNFORCED); |
| 526 | |
| 527 | reg |= PORT_PCS_CTRL_FORCE_LINK; |
| 528 | if (phydev->link) |
| 529 | reg |= PORT_PCS_CTRL_LINK_UP; |
| 530 | |
| 531 | if (mv88e6xxx_6065_family(ds) && phydev->speed > SPEED_100) |
| 532 | goto out; |
| 533 | |
| 534 | switch (phydev->speed) { |
| 535 | case SPEED_1000: |
| 536 | reg |= PORT_PCS_CTRL_1000; |
| 537 | break; |
| 538 | case SPEED_100: |
| 539 | reg |= PORT_PCS_CTRL_100; |
| 540 | break; |
| 541 | case SPEED_10: |
| 542 | reg |= PORT_PCS_CTRL_10; |
| 543 | break; |
| 544 | default: |
| 545 | pr_info("Unknown speed"); |
| 546 | goto out; |
| 547 | } |
| 548 | |
| 549 | reg |= PORT_PCS_CTRL_FORCE_DUPLEX; |
| 550 | if (phydev->duplex == DUPLEX_FULL) |
| 551 | reg |= PORT_PCS_CTRL_DUPLEX_FULL; |
| 552 | |
Andrew Lunn | e7e72ac | 2015-08-31 15:56:51 +0200 | [diff] [blame] | 553 | if ((mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds)) && |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 554 | (port >= ps->info->num_ports - 2)) { |
Andrew Lunn | e7e72ac | 2015-08-31 15:56:51 +0200 | [diff] [blame] | 555 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) |
| 556 | reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK; |
| 557 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) |
| 558 | reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK; |
| 559 | if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) |
| 560 | reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK | |
| 561 | PORT_PCS_CTRL_RGMII_DELAY_TXCLK); |
| 562 | } |
Andrew Lunn | dea8702 | 2015-08-31 15:56:47 +0200 | [diff] [blame] | 563 | _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_PCS_CTRL, reg); |
| 564 | |
| 565 | out: |
| 566 | mutex_unlock(&ps->smi_mutex); |
| 567 | } |
| 568 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 569 | static int _mv88e6xxx_stats_wait(struct dsa_switch *ds) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 570 | { |
| 571 | int ret; |
| 572 | int i; |
| 573 | |
| 574 | for (i = 0; i < 10; i++) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 575 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_OP); |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 576 | if ((ret & GLOBAL_STATS_OP_BUSY) == 0) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | return -ETIMEDOUT; |
| 581 | } |
| 582 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 583 | static int _mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 584 | { |
| 585 | int ret; |
| 586 | |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 587 | if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds)) |
Andrew Lunn | f3a8b6b | 2015-04-02 04:06:40 +0200 | [diff] [blame] | 588 | port = (port + 1) << 5; |
| 589 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 590 | /* Snapshot the hardware statistics counters for this port. */ |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 591 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP, |
| 592 | GLOBAL_STATS_OP_CAPTURE_PORT | |
| 593 | GLOBAL_STATS_OP_HIST_RX_TX | port); |
| 594 | if (ret < 0) |
| 595 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 596 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 597 | /* Wait for the snapshotting to complete. */ |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 598 | ret = _mv88e6xxx_stats_wait(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 599 | if (ret < 0) |
| 600 | return ret; |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 605 | static void _mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 606 | { |
| 607 | u32 _val; |
| 608 | int ret; |
| 609 | |
| 610 | *val = 0; |
| 611 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 612 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP, |
| 613 | GLOBAL_STATS_OP_READ_CAPTURED | |
| 614 | GLOBAL_STATS_OP_HIST_RX_TX | stat); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 615 | if (ret < 0) |
| 616 | return; |
| 617 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 618 | ret = _mv88e6xxx_stats_wait(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 619 | if (ret < 0) |
| 620 | return; |
| 621 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 622 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_32); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 623 | if (ret < 0) |
| 624 | return; |
| 625 | |
| 626 | _val = ret << 16; |
| 627 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 628 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_STATS_COUNTER_01); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 629 | if (ret < 0) |
| 630 | return; |
| 631 | |
| 632 | *val = _val | ret; |
| 633 | } |
| 634 | |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 635 | static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = { |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 636 | { "in_good_octets", 8, 0x00, BANK0, }, |
| 637 | { "in_bad_octets", 4, 0x02, BANK0, }, |
| 638 | { "in_unicast", 4, 0x04, BANK0, }, |
| 639 | { "in_broadcasts", 4, 0x06, BANK0, }, |
| 640 | { "in_multicasts", 4, 0x07, BANK0, }, |
| 641 | { "in_pause", 4, 0x16, BANK0, }, |
| 642 | { "in_undersize", 4, 0x18, BANK0, }, |
| 643 | { "in_fragments", 4, 0x19, BANK0, }, |
| 644 | { "in_oversize", 4, 0x1a, BANK0, }, |
| 645 | { "in_jabber", 4, 0x1b, BANK0, }, |
| 646 | { "in_rx_error", 4, 0x1c, BANK0, }, |
| 647 | { "in_fcs_error", 4, 0x1d, BANK0, }, |
| 648 | { "out_octets", 8, 0x0e, BANK0, }, |
| 649 | { "out_unicast", 4, 0x10, BANK0, }, |
| 650 | { "out_broadcasts", 4, 0x13, BANK0, }, |
| 651 | { "out_multicasts", 4, 0x12, BANK0, }, |
| 652 | { "out_pause", 4, 0x15, BANK0, }, |
| 653 | { "excessive", 4, 0x11, BANK0, }, |
| 654 | { "collisions", 4, 0x1e, BANK0, }, |
| 655 | { "deferred", 4, 0x05, BANK0, }, |
| 656 | { "single", 4, 0x14, BANK0, }, |
| 657 | { "multiple", 4, 0x17, BANK0, }, |
| 658 | { "out_fcs_error", 4, 0x03, BANK0, }, |
| 659 | { "late", 4, 0x1f, BANK0, }, |
| 660 | { "hist_64bytes", 4, 0x08, BANK0, }, |
| 661 | { "hist_65_127bytes", 4, 0x09, BANK0, }, |
| 662 | { "hist_128_255bytes", 4, 0x0a, BANK0, }, |
| 663 | { "hist_256_511bytes", 4, 0x0b, BANK0, }, |
| 664 | { "hist_512_1023bytes", 4, 0x0c, BANK0, }, |
| 665 | { "hist_1024_max_bytes", 4, 0x0d, BANK0, }, |
| 666 | { "sw_in_discards", 4, 0x10, PORT, }, |
| 667 | { "sw_in_filtered", 2, 0x12, PORT, }, |
| 668 | { "sw_out_filtered", 2, 0x13, PORT, }, |
| 669 | { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 670 | { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 671 | { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 672 | { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 673 | { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 674 | { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 675 | { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 676 | { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 677 | { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 678 | { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 679 | { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 680 | { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 681 | { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 682 | { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 683 | { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 684 | { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 685 | { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 686 | { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 687 | { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 688 | { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 689 | { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 690 | { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 691 | { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 692 | { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 693 | { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
| 694 | { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, }, |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 695 | }; |
| 696 | |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 697 | static bool mv88e6xxx_has_stat(struct dsa_switch *ds, |
| 698 | struct mv88e6xxx_hw_stat *stat) |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 699 | { |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 700 | switch (stat->type) { |
| 701 | case BANK0: |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 702 | return true; |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 703 | case BANK1: |
| 704 | return mv88e6xxx_6320_family(ds); |
| 705 | case PORT: |
| 706 | return mv88e6xxx_6095_family(ds) || |
| 707 | mv88e6xxx_6185_family(ds) || |
| 708 | mv88e6xxx_6097_family(ds) || |
| 709 | mv88e6xxx_6165_family(ds) || |
| 710 | mv88e6xxx_6351_family(ds) || |
| 711 | mv88e6xxx_6352_family(ds); |
Andrew Lunn | e413e7e | 2015-04-02 04:06:38 +0200 | [diff] [blame] | 712 | } |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 713 | return false; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 716 | static uint64_t _mv88e6xxx_get_ethtool_stat(struct dsa_switch *ds, |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 717 | struct mv88e6xxx_hw_stat *s, |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 718 | int port) |
| 719 | { |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 720 | u32 low; |
| 721 | u32 high = 0; |
| 722 | int ret; |
| 723 | u64 value; |
| 724 | |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 725 | switch (s->type) { |
| 726 | case PORT: |
| 727 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), s->reg); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 728 | if (ret < 0) |
| 729 | return UINT64_MAX; |
| 730 | |
| 731 | low = ret; |
| 732 | if (s->sizeof_stat == 4) { |
| 733 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 734 | s->reg + 1); |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 735 | if (ret < 0) |
| 736 | return UINT64_MAX; |
| 737 | high = ret; |
| 738 | } |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 739 | break; |
| 740 | case BANK0: |
| 741 | case BANK1: |
Andrew Lunn | 80c4627 | 2015-06-20 18:42:30 +0200 | [diff] [blame] | 742 | _mv88e6xxx_stats_read(ds, s->reg, &low); |
| 743 | if (s->sizeof_stat == 8) |
| 744 | _mv88e6xxx_stats_read(ds, s->reg + 1, &high); |
| 745 | } |
| 746 | value = (((u64)high) << 16) | low; |
| 747 | return value; |
| 748 | } |
| 749 | |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 750 | void mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data) |
| 751 | { |
| 752 | struct mv88e6xxx_hw_stat *stat; |
| 753 | int i, j; |
| 754 | |
| 755 | for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) { |
| 756 | stat = &mv88e6xxx_hw_stats[i]; |
| 757 | if (mv88e6xxx_has_stat(ds, stat)) { |
| 758 | memcpy(data + j * ETH_GSTRING_LEN, stat->string, |
| 759 | ETH_GSTRING_LEN); |
| 760 | j++; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | int mv88e6xxx_get_sset_count(struct dsa_switch *ds) |
| 766 | { |
| 767 | struct mv88e6xxx_hw_stat *stat; |
| 768 | int i, j; |
| 769 | |
| 770 | for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) { |
| 771 | stat = &mv88e6xxx_hw_stats[i]; |
| 772 | if (mv88e6xxx_has_stat(ds, stat)) |
| 773 | j++; |
| 774 | } |
| 775 | return j; |
| 776 | } |
| 777 | |
| 778 | void |
| 779 | mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, |
| 780 | int port, uint64_t *data) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 781 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 782 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 783 | struct mv88e6xxx_hw_stat *stat; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 784 | int ret; |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 785 | int i, j; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 786 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 787 | mutex_lock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 788 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 789 | ret = _mv88e6xxx_stats_snapshot(ds, port); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 790 | if (ret < 0) { |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 791 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 792 | return; |
| 793 | } |
Andrew Lunn | f5e2ed0 | 2015-12-23 13:23:17 +0100 | [diff] [blame] | 794 | for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) { |
| 795 | stat = &mv88e6xxx_hw_stats[i]; |
| 796 | if (mv88e6xxx_has_stat(ds, stat)) { |
| 797 | data[j] = _mv88e6xxx_get_ethtool_stat(ds, stat, port); |
| 798 | j++; |
| 799 | } |
| 800 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 801 | |
Andrew Lunn | 3188823 | 2015-05-06 01:09:54 +0200 | [diff] [blame] | 802 | mutex_unlock(&ps->smi_mutex); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 803 | } |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 804 | |
Guenter Roeck | a1ab91f3 | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 805 | int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port) |
| 806 | { |
| 807 | return 32 * sizeof(u16); |
| 808 | } |
| 809 | |
| 810 | void mv88e6xxx_get_regs(struct dsa_switch *ds, int port, |
| 811 | struct ethtool_regs *regs, void *_p) |
| 812 | { |
| 813 | u16 *p = _p; |
| 814 | int i; |
| 815 | |
| 816 | regs->version = 0; |
| 817 | |
| 818 | memset(p, 0xff, 32 * sizeof(u16)); |
| 819 | |
| 820 | for (i = 0; i < 32; i++) { |
| 821 | int ret; |
| 822 | |
| 823 | ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i); |
| 824 | if (ret >= 0) |
| 825 | p[i] = ret; |
| 826 | } |
| 827 | } |
| 828 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 829 | static int _mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, |
| 830 | u16 mask) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 831 | { |
| 832 | unsigned long timeout = jiffies + HZ / 10; |
| 833 | |
| 834 | while (time_before(jiffies, timeout)) { |
| 835 | int ret; |
| 836 | |
| 837 | ret = _mv88e6xxx_reg_read(ds, reg, offset); |
| 838 | if (ret < 0) |
| 839 | return ret; |
| 840 | if (!(ret & mask)) |
| 841 | return 0; |
| 842 | |
| 843 | usleep_range(1000, 2000); |
| 844 | } |
| 845 | return -ETIMEDOUT; |
| 846 | } |
| 847 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 848 | static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask) |
| 849 | { |
| 850 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 851 | int ret; |
| 852 | |
| 853 | mutex_lock(&ps->smi_mutex); |
| 854 | ret = _mv88e6xxx_wait(ds, reg, offset, mask); |
| 855 | mutex_unlock(&ps->smi_mutex); |
| 856 | |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | static int _mv88e6xxx_phy_wait(struct dsa_switch *ds) |
| 861 | { |
| 862 | return _mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 863 | GLOBAL2_SMI_OP_BUSY); |
| 864 | } |
| 865 | |
| 866 | int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds) |
| 867 | { |
| 868 | return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 869 | GLOBAL2_EEPROM_OP_LOAD); |
| 870 | } |
| 871 | |
| 872 | int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds) |
| 873 | { |
| 874 | return mv88e6xxx_wait(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP, |
| 875 | GLOBAL2_EEPROM_OP_BUSY); |
| 876 | } |
| 877 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 878 | static int _mv88e6xxx_atu_wait(struct dsa_switch *ds) |
| 879 | { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 880 | return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_ATU_OP, |
| 881 | GLOBAL_ATU_OP_BUSY); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 884 | static int _mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr, |
| 885 | int regnum) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 886 | { |
| 887 | int ret; |
| 888 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 889 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 890 | GLOBAL2_SMI_OP_22_READ | (addr << 5) | |
| 891 | regnum); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 892 | if (ret < 0) |
| 893 | return ret; |
| 894 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 895 | ret = _mv88e6xxx_phy_wait(ds); |
| 896 | if (ret < 0) |
| 897 | return ret; |
| 898 | |
| 899 | return _mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 902 | static int _mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr, |
| 903 | int regnum, u16 val) |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 904 | { |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 905 | int ret; |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 906 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 907 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_DATA, val); |
| 908 | if (ret < 0) |
| 909 | return ret; |
| 910 | |
| 911 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SMI_OP, |
| 912 | GLOBAL2_SMI_OP_22_WRITE | (addr << 5) | |
| 913 | regnum); |
| 914 | |
| 915 | return _mv88e6xxx_phy_wait(ds); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 918 | int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e) |
| 919 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 920 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 921 | int reg; |
| 922 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 923 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 924 | |
| 925 | reg = _mv88e6xxx_phy_read_indirect(ds, port, 16); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 926 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 927 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 928 | |
| 929 | e->eee_enabled = !!(reg & 0x0200); |
| 930 | e->tx_lpi_enabled = !!(reg & 0x0100); |
| 931 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 932 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 933 | if (reg < 0) |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 934 | goto out; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 935 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 936 | e->eee_active = !!(reg & PORT_STATUS_EEE); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 937 | reg = 0; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 938 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 939 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 940 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 941 | return reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | int mv88e6xxx_set_eee(struct dsa_switch *ds, int port, |
| 945 | struct phy_device *phydev, struct ethtool_eee *e) |
| 946 | { |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 947 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 948 | int reg; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 949 | int ret; |
| 950 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 951 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 952 | |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 953 | ret = _mv88e6xxx_phy_read_indirect(ds, port, 16); |
| 954 | if (ret < 0) |
| 955 | goto out; |
| 956 | |
| 957 | reg = ret & ~0x0300; |
| 958 | if (e->eee_enabled) |
| 959 | reg |= 0x0200; |
| 960 | if (e->tx_lpi_enabled) |
| 961 | reg |= 0x0100; |
| 962 | |
| 963 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 16, reg); |
| 964 | out: |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 965 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 2f40c69 | 2015-04-02 04:06:37 +0200 | [diff] [blame] | 966 | |
| 967 | return ret; |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 968 | } |
| 969 | |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 970 | static int _mv88e6xxx_atu_cmd(struct dsa_switch *ds, u16 fid, u16 cmd) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 971 | { |
| 972 | int ret; |
| 973 | |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 974 | if (mv88e6xxx_has_fid_reg(ds)) { |
| 975 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_FID, fid); |
| 976 | if (ret < 0) |
| 977 | return ret; |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 978 | } else if (mv88e6xxx_num_databases(ds) == 256) { |
| 979 | /* ATU DBNum[7:4] are located in ATU Control 15:12 */ |
| 980 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_CONTROL); |
| 981 | if (ret < 0) |
| 982 | return ret; |
| 983 | |
| 984 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_CONTROL, |
| 985 | (ret & 0xfff) | |
| 986 | ((fid << 8) & 0xf000)); |
| 987 | if (ret < 0) |
| 988 | return ret; |
| 989 | |
| 990 | /* ATU DBNum[3:0] are located in ATU Operation 3:0 */ |
| 991 | cmd |= fid & 0xf; |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 992 | } |
| 993 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 994 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_OP, cmd); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 995 | if (ret < 0) |
| 996 | return ret; |
| 997 | |
| 998 | return _mv88e6xxx_atu_wait(ds); |
| 999 | } |
| 1000 | |
Vivien Didelot | 37705b7 | 2015-09-04 14:34:11 -0400 | [diff] [blame] | 1001 | static int _mv88e6xxx_atu_data_write(struct dsa_switch *ds, |
| 1002 | struct mv88e6xxx_atu_entry *entry) |
| 1003 | { |
| 1004 | u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK; |
| 1005 | |
| 1006 | if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 1007 | unsigned int mask, shift; |
| 1008 | |
| 1009 | if (entry->trunk) { |
| 1010 | data |= GLOBAL_ATU_DATA_TRUNK; |
| 1011 | mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK; |
| 1012 | shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT; |
| 1013 | } else { |
| 1014 | mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK; |
| 1015 | shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT; |
| 1016 | } |
| 1017 | |
| 1018 | data |= (entry->portv_trunkid << shift) & mask; |
| 1019 | } |
| 1020 | |
| 1021 | return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_DATA, data); |
| 1022 | } |
| 1023 | |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1024 | static int _mv88e6xxx_atu_flush_move(struct dsa_switch *ds, |
| 1025 | struct mv88e6xxx_atu_entry *entry, |
| 1026 | bool static_too) |
| 1027 | { |
| 1028 | int op; |
| 1029 | int err; |
| 1030 | |
| 1031 | err = _mv88e6xxx_atu_wait(ds); |
| 1032 | if (err) |
| 1033 | return err; |
| 1034 | |
| 1035 | err = _mv88e6xxx_atu_data_write(ds, entry); |
| 1036 | if (err) |
| 1037 | return err; |
| 1038 | |
| 1039 | if (entry->fid) { |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1040 | op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB : |
| 1041 | GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB; |
| 1042 | } else { |
| 1043 | op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL : |
| 1044 | GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC; |
| 1045 | } |
| 1046 | |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1047 | return _mv88e6xxx_atu_cmd(ds, entry->fid, op); |
Vivien Didelot | 7fb5e75 | 2015-09-04 14:34:12 -0400 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | static int _mv88e6xxx_atu_flush(struct dsa_switch *ds, u16 fid, bool static_too) |
| 1051 | { |
| 1052 | struct mv88e6xxx_atu_entry entry = { |
| 1053 | .fid = fid, |
| 1054 | .state = 0, /* EntryState bits must be 0 */ |
| 1055 | }; |
| 1056 | |
| 1057 | return _mv88e6xxx_atu_flush_move(ds, &entry, static_too); |
| 1058 | } |
| 1059 | |
Vivien Didelot | 9f4d55d | 2015-09-04 14:34:15 -0400 | [diff] [blame] | 1060 | static int _mv88e6xxx_atu_move(struct dsa_switch *ds, u16 fid, int from_port, |
| 1061 | int to_port, bool static_too) |
| 1062 | { |
| 1063 | struct mv88e6xxx_atu_entry entry = { |
| 1064 | .trunk = false, |
| 1065 | .fid = fid, |
| 1066 | }; |
| 1067 | |
| 1068 | /* EntryState bits must be 0xF */ |
| 1069 | entry.state = GLOBAL_ATU_DATA_STATE_MASK; |
| 1070 | |
| 1071 | /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */ |
| 1072 | entry.portv_trunkid = (to_port & 0x0f) << 4; |
| 1073 | entry.portv_trunkid |= from_port & 0x0f; |
| 1074 | |
| 1075 | return _mv88e6xxx_atu_flush_move(ds, &entry, static_too); |
| 1076 | } |
| 1077 | |
| 1078 | static int _mv88e6xxx_atu_remove(struct dsa_switch *ds, u16 fid, int port, |
| 1079 | bool static_too) |
| 1080 | { |
| 1081 | /* Destination port 0xF means remove the entries */ |
| 1082 | return _mv88e6xxx_atu_move(ds, fid, port, 0x0f, static_too); |
| 1083 | } |
| 1084 | |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1085 | static const char * const mv88e6xxx_port_state_names[] = { |
| 1086 | [PORT_CONTROL_STATE_DISABLED] = "Disabled", |
| 1087 | [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening", |
| 1088 | [PORT_CONTROL_STATE_LEARNING] = "Learning", |
| 1089 | [PORT_CONTROL_STATE_FORWARDING] = "Forwarding", |
| 1090 | }; |
| 1091 | |
| 1092 | static int _mv88e6xxx_port_state(struct dsa_switch *ds, int port, u8 state) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1093 | { |
Geert Uytterhoeven | c3ffe6d | 2015-04-16 20:49:14 +0200 | [diff] [blame] | 1094 | int reg, ret = 0; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1095 | u8 oldstate; |
| 1096 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1097 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL); |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1098 | if (reg < 0) |
| 1099 | return reg; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1100 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1101 | oldstate = reg & PORT_CONTROL_STATE_MASK; |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1102 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1103 | if (oldstate != state) { |
| 1104 | /* Flush forwarding database if we're moving a port |
| 1105 | * from Learning or Forwarding state to Disabled or |
| 1106 | * Blocking or Listening state. |
| 1107 | */ |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1108 | if ((oldstate == PORT_CONTROL_STATE_LEARNING || |
| 1109 | oldstate == PORT_CONTROL_STATE_FORWARDING) |
| 1110 | && (state == PORT_CONTROL_STATE_DISABLED || |
| 1111 | state == PORT_CONTROL_STATE_BLOCKING)) { |
Vivien Didelot | 2b8157b | 2015-09-04 14:34:16 -0400 | [diff] [blame] | 1112 | ret = _mv88e6xxx_atu_remove(ds, 0, port, false); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1113 | if (ret) |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1114 | return ret; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1115 | } |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1116 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1117 | reg = (reg & ~PORT_CONTROL_STATE_MASK) | state; |
| 1118 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL, |
| 1119 | reg); |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1120 | if (ret) |
| 1121 | return ret; |
| 1122 | |
| 1123 | netdev_dbg(ds->ports[port], "PortState %s (was %s)\n", |
| 1124 | mv88e6xxx_port_state_names[state], |
| 1125 | mv88e6xxx_port_state_names[oldstate]); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1128 | return ret; |
| 1129 | } |
| 1130 | |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1131 | static int _mv88e6xxx_port_based_vlan_map(struct dsa_switch *ds, int port) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1132 | { |
| 1133 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1134 | struct net_device *bridge = ps->ports[port].bridge_dev; |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 1135 | const u16 mask = (1 << ps->info->num_ports) - 1; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1136 | u16 output_ports = 0; |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame] | 1137 | int reg; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1138 | int i; |
| 1139 | |
| 1140 | /* allow CPU port or DSA link(s) to send frames to every port */ |
| 1141 | if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) { |
| 1142 | output_ports = mask; |
| 1143 | } else { |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 1144 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 1145 | /* allow sending frames to every group member */ |
| 1146 | if (bridge && ps->ports[i].bridge_dev == bridge) |
| 1147 | output_ports |= BIT(i); |
| 1148 | |
| 1149 | /* allow sending frames to CPU port and DSA link(s) */ |
| 1150 | if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)) |
| 1151 | output_ports |= BIT(i); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | /* prevent frames from going back out of the port they came in on */ |
| 1156 | output_ports &= ~BIT(port); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1157 | |
Vivien Didelot | ede8098 | 2015-10-11 18:08:35 -0400 | [diff] [blame] | 1158 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN); |
| 1159 | if (reg < 0) |
| 1160 | return reg; |
| 1161 | |
| 1162 | reg &= ~mask; |
| 1163 | reg |= output_ports & mask; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1164 | |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1165 | return _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, reg); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Vivien Didelot | 43c44a9 | 2016-04-06 11:55:03 -0400 | [diff] [blame] | 1168 | void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port, u8 state) |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1169 | { |
| 1170 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1171 | int stp_state; |
| 1172 | |
| 1173 | switch (state) { |
| 1174 | case BR_STATE_DISABLED: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1175 | stp_state = PORT_CONTROL_STATE_DISABLED; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1176 | break; |
| 1177 | case BR_STATE_BLOCKING: |
| 1178 | case BR_STATE_LISTENING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1179 | stp_state = PORT_CONTROL_STATE_BLOCKING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1180 | break; |
| 1181 | case BR_STATE_LEARNING: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1182 | stp_state = PORT_CONTROL_STATE_LEARNING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1183 | break; |
| 1184 | case BR_STATE_FORWARDING: |
| 1185 | default: |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1186 | stp_state = PORT_CONTROL_STATE_FORWARDING; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1187 | break; |
| 1188 | } |
| 1189 | |
Vivien Didelot | 43c44a9 | 2016-04-06 11:55:03 -0400 | [diff] [blame] | 1190 | /* mv88e6xxx_port_stp_state_set may be called with softirqs disabled, |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1191 | * so we can not update the port state directly but need to schedule it. |
| 1192 | */ |
Vivien Didelot | d715fa6 | 2016-02-12 12:09:38 -0500 | [diff] [blame] | 1193 | ps->ports[port].state = stp_state; |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 1194 | set_bit(port, ps->port_state_update_mask); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1195 | schedule_work(&ps->bridge_work); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1198 | static int _mv88e6xxx_port_pvid(struct dsa_switch *ds, int port, u16 *new, |
| 1199 | u16 *old) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1200 | { |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1201 | u16 pvid; |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1202 | int ret; |
| 1203 | |
| 1204 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_DEFAULT_VLAN); |
| 1205 | if (ret < 0) |
| 1206 | return ret; |
| 1207 | |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1208 | pvid = ret & PORT_DEFAULT_VLAN_MASK; |
| 1209 | |
| 1210 | if (new) { |
| 1211 | ret &= ~PORT_DEFAULT_VLAN_MASK; |
| 1212 | ret |= *new & PORT_DEFAULT_VLAN_MASK; |
| 1213 | |
| 1214 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 1215 | PORT_DEFAULT_VLAN, ret); |
| 1216 | if (ret < 0) |
| 1217 | return ret; |
| 1218 | |
| 1219 | netdev_dbg(ds->ports[port], "DefaultVID %d (was %d)\n", *new, |
| 1220 | pvid); |
| 1221 | } |
| 1222 | |
| 1223 | if (old) |
| 1224 | *old = pvid; |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1229 | static int _mv88e6xxx_port_pvid_get(struct dsa_switch *ds, int port, u16 *pvid) |
| 1230 | { |
| 1231 | return _mv88e6xxx_port_pvid(ds, port, NULL, pvid); |
| 1232 | } |
| 1233 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1234 | static int _mv88e6xxx_port_pvid_set(struct dsa_switch *ds, int port, u16 pvid) |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1235 | { |
Vivien Didelot | 5da9603 | 2016-03-07 18:24:39 -0500 | [diff] [blame] | 1236 | return _mv88e6xxx_port_pvid(ds, port, &pvid, NULL); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1237 | } |
| 1238 | |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 1239 | static int _mv88e6xxx_vtu_wait(struct dsa_switch *ds) |
| 1240 | { |
| 1241 | return _mv88e6xxx_wait(ds, REG_GLOBAL, GLOBAL_VTU_OP, |
| 1242 | GLOBAL_VTU_OP_BUSY); |
| 1243 | } |
| 1244 | |
| 1245 | static int _mv88e6xxx_vtu_cmd(struct dsa_switch *ds, u16 op) |
| 1246 | { |
| 1247 | int ret; |
| 1248 | |
| 1249 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_OP, op); |
| 1250 | if (ret < 0) |
| 1251 | return ret; |
| 1252 | |
| 1253 | return _mv88e6xxx_vtu_wait(ds); |
| 1254 | } |
| 1255 | |
| 1256 | static int _mv88e6xxx_vtu_stu_flush(struct dsa_switch *ds) |
| 1257 | { |
| 1258 | int ret; |
| 1259 | |
| 1260 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1261 | if (ret < 0) |
| 1262 | return ret; |
| 1263 | |
| 1264 | return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_FLUSH_ALL); |
| 1265 | } |
| 1266 | |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1267 | static int _mv88e6xxx_vtu_stu_data_read(struct dsa_switch *ds, |
| 1268 | struct mv88e6xxx_vtu_stu_entry *entry, |
| 1269 | unsigned int nibble_offset) |
| 1270 | { |
| 1271 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1272 | u16 regs[3]; |
| 1273 | int i; |
| 1274 | int ret; |
| 1275 | |
| 1276 | for (i = 0; i < 3; ++i) { |
| 1277 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1278 | GLOBAL_VTU_DATA_0_3 + i); |
| 1279 | if (ret < 0) |
| 1280 | return ret; |
| 1281 | |
| 1282 | regs[i] = ret; |
| 1283 | } |
| 1284 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 1285 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1286 | unsigned int shift = (i % 4) * 4 + nibble_offset; |
| 1287 | u16 reg = regs[i / 4]; |
| 1288 | |
| 1289 | entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK; |
| 1290 | } |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1295 | static int _mv88e6xxx_vtu_stu_data_write(struct dsa_switch *ds, |
| 1296 | struct mv88e6xxx_vtu_stu_entry *entry, |
| 1297 | unsigned int nibble_offset) |
| 1298 | { |
| 1299 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1300 | u16 regs[3] = { 0 }; |
| 1301 | int i; |
| 1302 | int ret; |
| 1303 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 1304 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1305 | unsigned int shift = (i % 4) * 4 + nibble_offset; |
| 1306 | u8 data = entry->data[i]; |
| 1307 | |
| 1308 | regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift; |
| 1309 | } |
| 1310 | |
| 1311 | for (i = 0; i < 3; ++i) { |
| 1312 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, |
| 1313 | GLOBAL_VTU_DATA_0_3 + i, regs[i]); |
| 1314 | if (ret < 0) |
| 1315 | return ret; |
| 1316 | } |
| 1317 | |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 1321 | static int _mv88e6xxx_vtu_vid_write(struct dsa_switch *ds, u16 vid) |
| 1322 | { |
| 1323 | return _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, |
| 1324 | vid & GLOBAL_VTU_VID_MASK); |
| 1325 | } |
| 1326 | |
| 1327 | static int _mv88e6xxx_vtu_getnext(struct dsa_switch *ds, |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1328 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1329 | { |
| 1330 | struct mv88e6xxx_vtu_stu_entry next = { 0 }; |
| 1331 | int ret; |
| 1332 | |
| 1333 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1334 | if (ret < 0) |
| 1335 | return ret; |
| 1336 | |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1337 | ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_VTU_GET_NEXT); |
| 1338 | if (ret < 0) |
| 1339 | return ret; |
| 1340 | |
| 1341 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID); |
| 1342 | if (ret < 0) |
| 1343 | return ret; |
| 1344 | |
| 1345 | next.vid = ret & GLOBAL_VTU_VID_MASK; |
| 1346 | next.valid = !!(ret & GLOBAL_VTU_VID_VALID); |
| 1347 | |
| 1348 | if (next.valid) { |
| 1349 | ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 0); |
| 1350 | if (ret < 0) |
| 1351 | return ret; |
| 1352 | |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1353 | if (mv88e6xxx_has_fid_reg(ds)) { |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1354 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1355 | GLOBAL_VTU_FID); |
| 1356 | if (ret < 0) |
| 1357 | return ret; |
| 1358 | |
| 1359 | next.fid = ret & GLOBAL_VTU_FID_MASK; |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1360 | } else if (mv88e6xxx_num_databases(ds) == 256) { |
| 1361 | /* VTU DBNum[7:4] are located in VTU Operation 11:8, and |
| 1362 | * VTU DBNum[3:0] are located in VTU Operation 3:0 |
| 1363 | */ |
| 1364 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1365 | GLOBAL_VTU_OP); |
| 1366 | if (ret < 0) |
| 1367 | return ret; |
| 1368 | |
| 1369 | next.fid = (ret & 0xf00) >> 4; |
| 1370 | next.fid |= ret & 0xf; |
Vivien Didelot | 2e7bd5e | 2016-03-31 16:53:41 -0400 | [diff] [blame] | 1371 | } |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1372 | |
Vivien Didelot | 2e7bd5e | 2016-03-31 16:53:41 -0400 | [diff] [blame] | 1373 | if (mv88e6xxx_has_stu(ds)) { |
Vivien Didelot | b8fee95 | 2015-08-13 12:52:19 -0400 | [diff] [blame] | 1374 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1375 | GLOBAL_VTU_SID); |
| 1376 | if (ret < 0) |
| 1377 | return ret; |
| 1378 | |
| 1379 | next.sid = ret & GLOBAL_VTU_SID_MASK; |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | *entry = next; |
| 1384 | return 0; |
| 1385 | } |
| 1386 | |
Vivien Didelot | ceff5ef | 2016-02-23 12:13:55 -0500 | [diff] [blame] | 1387 | int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port, |
| 1388 | struct switchdev_obj_port_vlan *vlan, |
| 1389 | int (*cb)(struct switchdev_obj *obj)) |
| 1390 | { |
| 1391 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1392 | struct mv88e6xxx_vtu_stu_entry next; |
| 1393 | u16 pvid; |
| 1394 | int err; |
| 1395 | |
| 1396 | mutex_lock(&ps->smi_mutex); |
| 1397 | |
| 1398 | err = _mv88e6xxx_port_pvid_get(ds, port, &pvid); |
| 1399 | if (err) |
| 1400 | goto unlock; |
| 1401 | |
| 1402 | err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK); |
| 1403 | if (err) |
| 1404 | goto unlock; |
| 1405 | |
| 1406 | do { |
| 1407 | err = _mv88e6xxx_vtu_getnext(ds, &next); |
| 1408 | if (err) |
| 1409 | break; |
| 1410 | |
| 1411 | if (!next.valid) |
| 1412 | break; |
| 1413 | |
| 1414 | if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) |
| 1415 | continue; |
| 1416 | |
| 1417 | /* reinit and dump this VLAN obj */ |
| 1418 | vlan->vid_begin = vlan->vid_end = next.vid; |
| 1419 | vlan->flags = 0; |
| 1420 | |
| 1421 | if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED) |
| 1422 | vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
| 1423 | |
| 1424 | if (next.vid == pvid) |
| 1425 | vlan->flags |= BRIDGE_VLAN_INFO_PVID; |
| 1426 | |
| 1427 | err = cb(&vlan->obj); |
| 1428 | if (err) |
| 1429 | break; |
| 1430 | } while (next.vid < GLOBAL_VTU_VID_MASK); |
| 1431 | |
| 1432 | unlock: |
| 1433 | mutex_unlock(&ps->smi_mutex); |
| 1434 | |
| 1435 | return err; |
| 1436 | } |
| 1437 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1438 | static int _mv88e6xxx_vtu_loadpurge(struct dsa_switch *ds, |
| 1439 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1440 | { |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1441 | u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1442 | u16 reg = 0; |
| 1443 | int ret; |
| 1444 | |
| 1445 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1446 | if (ret < 0) |
| 1447 | return ret; |
| 1448 | |
| 1449 | if (!entry->valid) |
| 1450 | goto loadpurge; |
| 1451 | |
| 1452 | /* Write port member tags */ |
| 1453 | ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 0); |
| 1454 | if (ret < 0) |
| 1455 | return ret; |
| 1456 | |
Vivien Didelot | 2e7bd5e | 2016-03-31 16:53:41 -0400 | [diff] [blame] | 1457 | if (mv88e6xxx_has_stu(ds)) { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1458 | reg = entry->sid & GLOBAL_VTU_SID_MASK; |
| 1459 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg); |
| 1460 | if (ret < 0) |
| 1461 | return ret; |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1462 | } |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1463 | |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 1464 | if (mv88e6xxx_has_fid_reg(ds)) { |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1465 | reg = entry->fid & GLOBAL_VTU_FID_MASK; |
| 1466 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_FID, reg); |
| 1467 | if (ret < 0) |
| 1468 | return ret; |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1469 | } else if (mv88e6xxx_num_databases(ds) == 256) { |
| 1470 | /* VTU DBNum[7:4] are located in VTU Operation 11:8, and |
| 1471 | * VTU DBNum[3:0] are located in VTU Operation 3:0 |
| 1472 | */ |
| 1473 | op |= (entry->fid & 0xf0) << 8; |
| 1474 | op |= entry->fid & 0xf; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | reg = GLOBAL_VTU_VID_VALID; |
| 1478 | loadpurge: |
| 1479 | reg |= entry->vid & GLOBAL_VTU_VID_MASK; |
| 1480 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg); |
| 1481 | if (ret < 0) |
| 1482 | return ret; |
| 1483 | |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1484 | return _mv88e6xxx_vtu_cmd(ds, op); |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1485 | } |
| 1486 | |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1487 | static int _mv88e6xxx_stu_getnext(struct dsa_switch *ds, u8 sid, |
| 1488 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1489 | { |
| 1490 | struct mv88e6xxx_vtu_stu_entry next = { 0 }; |
| 1491 | int ret; |
| 1492 | |
| 1493 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1494 | if (ret < 0) |
| 1495 | return ret; |
| 1496 | |
| 1497 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, |
| 1498 | sid & GLOBAL_VTU_SID_MASK); |
| 1499 | if (ret < 0) |
| 1500 | return ret; |
| 1501 | |
| 1502 | ret = _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_GET_NEXT); |
| 1503 | if (ret < 0) |
| 1504 | return ret; |
| 1505 | |
| 1506 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_SID); |
| 1507 | if (ret < 0) |
| 1508 | return ret; |
| 1509 | |
| 1510 | next.sid = ret & GLOBAL_VTU_SID_MASK; |
| 1511 | |
| 1512 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_VTU_VID); |
| 1513 | if (ret < 0) |
| 1514 | return ret; |
| 1515 | |
| 1516 | next.valid = !!(ret & GLOBAL_VTU_VID_VALID); |
| 1517 | |
| 1518 | if (next.valid) { |
| 1519 | ret = _mv88e6xxx_vtu_stu_data_read(ds, &next, 2); |
| 1520 | if (ret < 0) |
| 1521 | return ret; |
| 1522 | } |
| 1523 | |
| 1524 | *entry = next; |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | static int _mv88e6xxx_stu_loadpurge(struct dsa_switch *ds, |
| 1529 | struct mv88e6xxx_vtu_stu_entry *entry) |
| 1530 | { |
| 1531 | u16 reg = 0; |
| 1532 | int ret; |
| 1533 | |
| 1534 | ret = _mv88e6xxx_vtu_wait(ds); |
| 1535 | if (ret < 0) |
| 1536 | return ret; |
| 1537 | |
| 1538 | if (!entry->valid) |
| 1539 | goto loadpurge; |
| 1540 | |
| 1541 | /* Write port states */ |
| 1542 | ret = _mv88e6xxx_vtu_stu_data_write(ds, entry, 2); |
| 1543 | if (ret < 0) |
| 1544 | return ret; |
| 1545 | |
| 1546 | reg = GLOBAL_VTU_VID_VALID; |
| 1547 | loadpurge: |
| 1548 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_VID, reg); |
| 1549 | if (ret < 0) |
| 1550 | return ret; |
| 1551 | |
| 1552 | reg = entry->sid & GLOBAL_VTU_SID_MASK; |
| 1553 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_VTU_SID, reg); |
| 1554 | if (ret < 0) |
| 1555 | return ret; |
| 1556 | |
| 1557 | return _mv88e6xxx_vtu_cmd(ds, GLOBAL_VTU_OP_STU_LOAD_PURGE); |
| 1558 | } |
| 1559 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1560 | static int _mv88e6xxx_port_fid(struct dsa_switch *ds, int port, u16 *new, |
| 1561 | u16 *old) |
| 1562 | { |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1563 | u16 upper_mask; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1564 | u16 fid; |
| 1565 | int ret; |
| 1566 | |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1567 | if (mv88e6xxx_num_databases(ds) == 4096) |
| 1568 | upper_mask = 0xff; |
Vivien Didelot | 11ea809 | 2016-03-31 16:53:44 -0400 | [diff] [blame] | 1569 | else if (mv88e6xxx_num_databases(ds) == 256) |
| 1570 | upper_mask = 0xf; |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1571 | else |
| 1572 | return -EOPNOTSUPP; |
| 1573 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1574 | /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */ |
| 1575 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_BASE_VLAN); |
| 1576 | if (ret < 0) |
| 1577 | return ret; |
| 1578 | |
| 1579 | fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12; |
| 1580 | |
| 1581 | if (new) { |
| 1582 | ret &= ~PORT_BASE_VLAN_FID_3_0_MASK; |
| 1583 | ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK; |
| 1584 | |
| 1585 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_BASE_VLAN, |
| 1586 | ret); |
| 1587 | if (ret < 0) |
| 1588 | return ret; |
| 1589 | } |
| 1590 | |
| 1591 | /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */ |
| 1592 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL_1); |
| 1593 | if (ret < 0) |
| 1594 | return ret; |
| 1595 | |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1596 | fid |= (ret & upper_mask) << 4; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1597 | |
| 1598 | if (new) { |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1599 | ret &= ~upper_mask; |
| 1600 | ret |= (*new >> 4) & upper_mask; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1601 | |
| 1602 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, |
| 1603 | ret); |
| 1604 | if (ret < 0) |
| 1605 | return ret; |
| 1606 | |
| 1607 | netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid); |
| 1608 | } |
| 1609 | |
| 1610 | if (old) |
| 1611 | *old = fid; |
| 1612 | |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | static int _mv88e6xxx_port_fid_get(struct dsa_switch *ds, int port, u16 *fid) |
| 1617 | { |
| 1618 | return _mv88e6xxx_port_fid(ds, port, NULL, fid); |
| 1619 | } |
| 1620 | |
| 1621 | static int _mv88e6xxx_port_fid_set(struct dsa_switch *ds, int port, u16 fid) |
| 1622 | { |
| 1623 | return _mv88e6xxx_port_fid(ds, port, &fid, NULL); |
| 1624 | } |
| 1625 | |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1626 | static int _mv88e6xxx_fid_new(struct dsa_switch *ds, u16 *fid) |
| 1627 | { |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1628 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1629 | DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID); |
| 1630 | struct mv88e6xxx_vtu_stu_entry vlan; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1631 | int i, err; |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1632 | |
| 1633 | bitmap_zero(fid_bitmap, MV88E6XXX_N_FID); |
| 1634 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1635 | /* Set every FID bit used by the (un)bridged ports */ |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 1636 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 1637 | err = _mv88e6xxx_port_fid_get(ds, i, fid); |
| 1638 | if (err) |
| 1639 | return err; |
| 1640 | |
| 1641 | set_bit(*fid, fid_bitmap); |
| 1642 | } |
| 1643 | |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1644 | /* Set every FID bit used by the VLAN entries */ |
| 1645 | err = _mv88e6xxx_vtu_vid_write(ds, GLOBAL_VTU_VID_MASK); |
| 1646 | if (err) |
| 1647 | return err; |
| 1648 | |
| 1649 | do { |
| 1650 | err = _mv88e6xxx_vtu_getnext(ds, &vlan); |
| 1651 | if (err) |
| 1652 | return err; |
| 1653 | |
| 1654 | if (!vlan.valid) |
| 1655 | break; |
| 1656 | |
| 1657 | set_bit(vlan.fid, fid_bitmap); |
| 1658 | } while (vlan.vid < GLOBAL_VTU_VID_MASK); |
| 1659 | |
| 1660 | /* The reset value 0x000 is used to indicate that multiple address |
| 1661 | * databases are not needed. Return the next positive available. |
| 1662 | */ |
| 1663 | *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1); |
Vivien Didelot | f74df0b | 2016-03-31 16:53:43 -0400 | [diff] [blame] | 1664 | if (unlikely(*fid >= mv88e6xxx_num_databases(ds))) |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1665 | return -ENOSPC; |
| 1666 | |
| 1667 | /* Clear the database */ |
| 1668 | return _mv88e6xxx_atu_flush(ds, *fid, true); |
| 1669 | } |
| 1670 | |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1671 | static int _mv88e6xxx_vtu_new(struct dsa_switch *ds, u16 vid, |
| 1672 | struct mv88e6xxx_vtu_stu_entry *entry) |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1673 | { |
| 1674 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1675 | struct mv88e6xxx_vtu_stu_entry vlan = { |
| 1676 | .valid = true, |
| 1677 | .vid = vid, |
| 1678 | }; |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 1679 | int i, err; |
| 1680 | |
| 1681 | err = _mv88e6xxx_fid_new(ds, &vlan.fid); |
| 1682 | if (err) |
| 1683 | return err; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1684 | |
Vivien Didelot | 3d131f0 | 2015-11-03 10:52:52 -0500 | [diff] [blame] | 1685 | /* exclude all ports except the CPU and DSA ports */ |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 1686 | for (i = 0; i < ps->info->num_ports; ++i) |
Vivien Didelot | 3d131f0 | 2015-11-03 10:52:52 -0500 | [diff] [blame] | 1687 | vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i) |
| 1688 | ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED |
| 1689 | : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1690 | |
| 1691 | if (mv88e6xxx_6097_family(ds) || mv88e6xxx_6165_family(ds) || |
| 1692 | mv88e6xxx_6351_family(ds) || mv88e6xxx_6352_family(ds)) { |
| 1693 | struct mv88e6xxx_vtu_stu_entry vstp; |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1694 | |
| 1695 | /* Adding a VTU entry requires a valid STU entry. As VSTP is not |
| 1696 | * implemented, only one STU entry is needed to cover all VTU |
| 1697 | * entries. Thus, validate the SID 0. |
| 1698 | */ |
| 1699 | vlan.sid = 0; |
| 1700 | err = _mv88e6xxx_stu_getnext(ds, GLOBAL_VTU_SID_MASK, &vstp); |
| 1701 | if (err) |
| 1702 | return err; |
| 1703 | |
| 1704 | if (vstp.sid != vlan.sid || !vstp.valid) { |
| 1705 | memset(&vstp, 0, sizeof(vstp)); |
| 1706 | vstp.valid = true; |
| 1707 | vstp.sid = vlan.sid; |
| 1708 | |
| 1709 | err = _mv88e6xxx_stu_loadpurge(ds, &vstp); |
| 1710 | if (err) |
| 1711 | return err; |
| 1712 | } |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | *entry = vlan; |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1719 | static int _mv88e6xxx_vtu_get(struct dsa_switch *ds, u16 vid, |
| 1720 | struct mv88e6xxx_vtu_stu_entry *entry, bool creat) |
| 1721 | { |
| 1722 | int err; |
| 1723 | |
| 1724 | if (!vid) |
| 1725 | return -EINVAL; |
| 1726 | |
| 1727 | err = _mv88e6xxx_vtu_vid_write(ds, vid - 1); |
| 1728 | if (err) |
| 1729 | return err; |
| 1730 | |
| 1731 | err = _mv88e6xxx_vtu_getnext(ds, entry); |
| 1732 | if (err) |
| 1733 | return err; |
| 1734 | |
| 1735 | if (entry->vid != vid || !entry->valid) { |
| 1736 | if (!creat) |
| 1737 | return -EOPNOTSUPP; |
| 1738 | /* -ENOENT would've been more appropriate, but switchdev expects |
| 1739 | * -EOPNOTSUPP to inform bridge about an eventual software VLAN. |
| 1740 | */ |
| 1741 | |
| 1742 | err = _mv88e6xxx_vtu_new(ds, vid, entry); |
| 1743 | } |
| 1744 | |
| 1745 | return err; |
| 1746 | } |
| 1747 | |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 1748 | static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port, |
| 1749 | u16 vid_begin, u16 vid_end) |
| 1750 | { |
| 1751 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1752 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 1753 | int i, err; |
| 1754 | |
| 1755 | if (!vid_begin) |
| 1756 | return -EOPNOTSUPP; |
| 1757 | |
| 1758 | mutex_lock(&ps->smi_mutex); |
| 1759 | |
| 1760 | err = _mv88e6xxx_vtu_vid_write(ds, vid_begin - 1); |
| 1761 | if (err) |
| 1762 | goto unlock; |
| 1763 | |
| 1764 | do { |
| 1765 | err = _mv88e6xxx_vtu_getnext(ds, &vlan); |
| 1766 | if (err) |
| 1767 | goto unlock; |
| 1768 | |
| 1769 | if (!vlan.valid) |
| 1770 | break; |
| 1771 | |
| 1772 | if (vlan.vid > vid_end) |
| 1773 | break; |
| 1774 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 1775 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 1776 | if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i)) |
| 1777 | continue; |
| 1778 | |
| 1779 | if (vlan.data[i] == |
| 1780 | GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) |
| 1781 | continue; |
| 1782 | |
| 1783 | if (ps->ports[i].bridge_dev == |
| 1784 | ps->ports[port].bridge_dev) |
| 1785 | break; /* same bridge, check next VLAN */ |
| 1786 | |
| 1787 | netdev_warn(ds->ports[port], |
| 1788 | "hardware VLAN %d already used by %s\n", |
| 1789 | vlan.vid, |
| 1790 | netdev_name(ps->ports[i].bridge_dev)); |
| 1791 | err = -EOPNOTSUPP; |
| 1792 | goto unlock; |
| 1793 | } |
| 1794 | } while (vlan.vid < vid_end); |
| 1795 | |
| 1796 | unlock: |
| 1797 | mutex_unlock(&ps->smi_mutex); |
| 1798 | |
| 1799 | return err; |
| 1800 | } |
| 1801 | |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 1802 | static const char * const mv88e6xxx_port_8021q_mode_names[] = { |
| 1803 | [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled", |
| 1804 | [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback", |
| 1805 | [PORT_CONTROL_2_8021Q_CHECK] = "Check", |
| 1806 | [PORT_CONTROL_2_8021Q_SECURE] = "Secure", |
| 1807 | }; |
| 1808 | |
| 1809 | int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port, |
| 1810 | bool vlan_filtering) |
| 1811 | { |
| 1812 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1813 | u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE : |
| 1814 | PORT_CONTROL_2_8021Q_DISABLED; |
| 1815 | int ret; |
| 1816 | |
| 1817 | mutex_lock(&ps->smi_mutex); |
| 1818 | |
| 1819 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_CONTROL_2); |
| 1820 | if (ret < 0) |
| 1821 | goto unlock; |
| 1822 | |
| 1823 | old = ret & PORT_CONTROL_2_8021Q_MASK; |
| 1824 | |
Vivien Didelot | 5220ef1 | 2016-03-07 18:24:52 -0500 | [diff] [blame] | 1825 | if (new != old) { |
| 1826 | ret &= ~PORT_CONTROL_2_8021Q_MASK; |
| 1827 | ret |= new & PORT_CONTROL_2_8021Q_MASK; |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 1828 | |
Vivien Didelot | 5220ef1 | 2016-03-07 18:24:52 -0500 | [diff] [blame] | 1829 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_2, |
| 1830 | ret); |
| 1831 | if (ret < 0) |
| 1832 | goto unlock; |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 1833 | |
Vivien Didelot | 5220ef1 | 2016-03-07 18:24:52 -0500 | [diff] [blame] | 1834 | netdev_dbg(ds->ports[port], "802.1Q Mode %s (was %s)\n", |
| 1835 | mv88e6xxx_port_8021q_mode_names[new], |
| 1836 | mv88e6xxx_port_8021q_mode_names[old]); |
| 1837 | } |
| 1838 | |
| 1839 | ret = 0; |
Vivien Didelot | 214cdb9 | 2016-02-26 13:16:08 -0500 | [diff] [blame] | 1840 | unlock: |
| 1841 | mutex_unlock(&ps->smi_mutex); |
| 1842 | |
| 1843 | return ret; |
| 1844 | } |
| 1845 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1846 | int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port, |
| 1847 | const struct switchdev_obj_port_vlan *vlan, |
| 1848 | struct switchdev_trans *trans) |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1849 | { |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 1850 | int err; |
| 1851 | |
Vivien Didelot | da9c359 | 2016-02-12 12:09:40 -0500 | [diff] [blame] | 1852 | /* If the requested port doesn't belong to the same bridge as the VLAN |
| 1853 | * members, do not support it (yet) and fallback to software VLAN. |
| 1854 | */ |
| 1855 | err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin, |
| 1856 | vlan->vid_end); |
| 1857 | if (err) |
| 1858 | return err; |
| 1859 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1860 | /* We don't need any dynamic resource from the kernel (yet), |
| 1861 | * so skip the prepare phase. |
| 1862 | */ |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
| 1866 | static int _mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, u16 vid, |
| 1867 | bool untagged) |
| 1868 | { |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1869 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 1870 | int err; |
| 1871 | |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1872 | err = _mv88e6xxx_vtu_get(ds, vid, &vlan, true); |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 1873 | if (err) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1874 | return err; |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 1875 | |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1876 | vlan.data[port] = untagged ? |
| 1877 | GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED : |
| 1878 | GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED; |
| 1879 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1880 | return _mv88e6xxx_vtu_loadpurge(ds, &vlan); |
| 1881 | } |
| 1882 | |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 1883 | void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port, |
| 1884 | const struct switchdev_obj_port_vlan *vlan, |
| 1885 | struct switchdev_trans *trans) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1886 | { |
| 1887 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1888 | bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; |
| 1889 | bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; |
| 1890 | u16 vid; |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1891 | |
| 1892 | mutex_lock(&ps->smi_mutex); |
| 1893 | |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 1894 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) |
| 1895 | if (_mv88e6xxx_port_vlan_add(ds, port, vid, untagged)) |
| 1896 | netdev_err(ds->ports[port], "failed to add VLAN %d%c\n", |
| 1897 | vid, untagged ? 'u' : 't'); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1898 | |
Vivien Didelot | 4d5770b | 2016-04-06 11:55:05 -0400 | [diff] [blame] | 1899 | if (pvid && _mv88e6xxx_port_pvid_set(ds, port, vlan->vid_end)) |
| 1900 | netdev_err(ds->ports[port], "failed to set PVID %d\n", |
| 1901 | vlan->vid_end); |
| 1902 | |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1903 | mutex_unlock(&ps->smi_mutex); |
Vivien Didelot | 0d3b33e | 2015-08-13 12:52:22 -0400 | [diff] [blame] | 1904 | } |
| 1905 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1906 | static int _mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, u16 vid) |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1907 | { |
| 1908 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1909 | struct mv88e6xxx_vtu_stu_entry vlan; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1910 | int i, err; |
| 1911 | |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1912 | err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false); |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 1913 | if (err) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1914 | return err; |
Vivien Didelot | 36d04ba | 2015-10-22 09:34:39 -0400 | [diff] [blame] | 1915 | |
Vivien Didelot | 2fb5ef0 | 2016-02-26 13:16:01 -0500 | [diff] [blame] | 1916 | /* Tell switchdev if this VLAN is handled in software */ |
| 1917 | if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) |
Vivien Didelot | 3c06f08 | 2016-02-05 14:04:39 -0500 | [diff] [blame] | 1918 | return -EOPNOTSUPP; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1919 | |
| 1920 | vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER; |
| 1921 | |
| 1922 | /* keep the VLAN unless all ports are excluded */ |
Vivien Didelot | f02bdff | 2015-10-11 18:08:36 -0400 | [diff] [blame] | 1923 | vlan.valid = false; |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 1924 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | 3d131f0 | 2015-11-03 10:52:52 -0500 | [diff] [blame] | 1925 | 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] | 1926 | continue; |
| 1927 | |
| 1928 | if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) { |
Vivien Didelot | f02bdff | 2015-10-11 18:08:36 -0400 | [diff] [blame] | 1929 | vlan.valid = true; |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1930 | break; |
| 1931 | } |
| 1932 | } |
| 1933 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1934 | err = _mv88e6xxx_vtu_loadpurge(ds, &vlan); |
| 1935 | if (err) |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1936 | return err; |
| 1937 | |
| 1938 | return _mv88e6xxx_atu_remove(ds, vlan.fid, port, false); |
| 1939 | } |
| 1940 | |
| 1941 | int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port, |
| 1942 | const struct switchdev_obj_port_vlan *vlan) |
| 1943 | { |
| 1944 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 1945 | u16 pvid, vid; |
| 1946 | int err = 0; |
| 1947 | |
| 1948 | mutex_lock(&ps->smi_mutex); |
| 1949 | |
| 1950 | err = _mv88e6xxx_port_pvid_get(ds, port, &pvid); |
| 1951 | if (err) |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1952 | goto unlock; |
| 1953 | |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1954 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { |
| 1955 | err = _mv88e6xxx_port_vlan_del(ds, port, vid); |
| 1956 | if (err) |
| 1957 | goto unlock; |
| 1958 | |
| 1959 | if (vid == pvid) { |
Vivien Didelot | 46fbe5e | 2016-02-26 13:16:07 -0500 | [diff] [blame] | 1960 | err = _mv88e6xxx_port_pvid_set(ds, port, 0); |
Vivien Didelot | 76e398a | 2015-11-01 12:33:55 -0500 | [diff] [blame] | 1961 | if (err) |
| 1962 | goto unlock; |
| 1963 | } |
| 1964 | } |
| 1965 | |
Vivien Didelot | 7dad08d | 2015-08-13 12:52:21 -0400 | [diff] [blame] | 1966 | unlock: |
| 1967 | mutex_unlock(&ps->smi_mutex); |
| 1968 | |
| 1969 | return err; |
| 1970 | } |
| 1971 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1972 | static int _mv88e6xxx_atu_mac_write(struct dsa_switch *ds, |
| 1973 | const unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1974 | { |
| 1975 | int i, ret; |
| 1976 | |
| 1977 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1978 | ret = _mv88e6xxx_reg_write( |
| 1979 | ds, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i, |
| 1980 | (addr[i * 2] << 8) | addr[i * 2 + 1]); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1981 | if (ret < 0) |
| 1982 | return ret; |
| 1983 | } |
| 1984 | |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Vivien Didelot | c5723ac | 2015-08-10 09:09:48 -0400 | [diff] [blame] | 1988 | static int _mv88e6xxx_atu_mac_read(struct dsa_switch *ds, unsigned char *addr) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1989 | { |
| 1990 | int i, ret; |
| 1991 | |
| 1992 | for (i = 0; i < 3; i++) { |
Andrew Lunn | cca8b13 | 2015-04-02 04:06:39 +0200 | [diff] [blame] | 1993 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, |
| 1994 | GLOBAL_ATU_MAC_01 + i); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 1995 | if (ret < 0) |
| 1996 | return ret; |
| 1997 | addr[i * 2] = ret >> 8; |
| 1998 | addr[i * 2 + 1] = ret & 0xff; |
| 1999 | } |
| 2000 | |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2004 | static int _mv88e6xxx_atu_load(struct dsa_switch *ds, |
| 2005 | struct mv88e6xxx_atu_entry *entry) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2006 | { |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2007 | int ret; |
| 2008 | |
| 2009 | ret = _mv88e6xxx_atu_wait(ds); |
| 2010 | if (ret < 0) |
| 2011 | return ret; |
| 2012 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2013 | ret = _mv88e6xxx_atu_mac_write(ds, entry->mac); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2014 | if (ret < 0) |
| 2015 | return ret; |
| 2016 | |
Vivien Didelot | 37705b7 | 2015-09-04 14:34:11 -0400 | [diff] [blame] | 2017 | ret = _mv88e6xxx_atu_data_write(ds, entry); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2018 | if (ret < 0) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2019 | return ret; |
| 2020 | |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 2021 | return _mv88e6xxx_atu_cmd(ds, entry->fid, GLOBAL_ATU_OP_LOAD_DB); |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2022 | } |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2023 | |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2024 | static int _mv88e6xxx_port_fdb_load(struct dsa_switch *ds, int port, |
| 2025 | const unsigned char *addr, u16 vid, |
| 2026 | u8 state) |
| 2027 | { |
| 2028 | struct mv88e6xxx_atu_entry entry = { 0 }; |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 2029 | struct mv88e6xxx_vtu_stu_entry vlan; |
| 2030 | int err; |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2031 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2032 | /* Null VLAN ID corresponds to the port private database */ |
| 2033 | if (vid == 0) |
| 2034 | err = _mv88e6xxx_port_fid_get(ds, port, &vlan.fid); |
| 2035 | else |
| 2036 | err = _mv88e6xxx_vtu_get(ds, vid, &vlan, false); |
Vivien Didelot | 3285f9e | 2016-02-26 13:16:03 -0500 | [diff] [blame] | 2037 | if (err) |
| 2038 | return err; |
| 2039 | |
| 2040 | entry.fid = vlan.fid; |
Vivien Didelot | fd231c8 | 2015-08-10 09:09:50 -0400 | [diff] [blame] | 2041 | entry.state = state; |
| 2042 | ether_addr_copy(entry.mac, addr); |
| 2043 | if (state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 2044 | entry.trunk = false; |
| 2045 | entry.portv_trunkid = BIT(port); |
| 2046 | } |
| 2047 | |
| 2048 | return _mv88e6xxx_atu_load(ds, &entry); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
Vivien Didelot | 146a320 | 2015-10-08 11:35:12 -0400 | [diff] [blame] | 2051 | int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port, |
| 2052 | const struct switchdev_obj_port_fdb *fdb, |
| 2053 | struct switchdev_trans *trans) |
| 2054 | { |
| 2055 | /* We don't need any dynamic resource from the kernel (yet), |
| 2056 | * so skip the prepare phase. |
| 2057 | */ |
| 2058 | return 0; |
| 2059 | } |
| 2060 | |
Vivien Didelot | 8497aa6 | 2016-04-06 11:55:04 -0400 | [diff] [blame] | 2061 | void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port, |
| 2062 | const struct switchdev_obj_port_fdb *fdb, |
| 2063 | struct switchdev_trans *trans) |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2064 | { |
Vivien Didelot | 1f36faf | 2015-10-08 11:35:13 -0400 | [diff] [blame] | 2065 | int state = is_multicast_ether_addr(fdb->addr) ? |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2066 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 2067 | GLOBAL_ATU_DATA_STATE_UC_STATIC; |
| 2068 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 6630e23 | 2015-08-06 01:44:07 -0400 | [diff] [blame] | 2069 | |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2070 | mutex_lock(&ps->smi_mutex); |
Vivien Didelot | 8497aa6 | 2016-04-06 11:55:04 -0400 | [diff] [blame] | 2071 | if (_mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, state)) |
| 2072 | netdev_err(ds->ports[port], "failed to load MAC address\n"); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2073 | mutex_unlock(&ps->smi_mutex); |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port, |
Vivien Didelot | 8057b3e | 2015-10-08 11:35:14 -0400 | [diff] [blame] | 2077 | const struct switchdev_obj_port_fdb *fdb) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2078 | { |
| 2079 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2080 | int ret; |
| 2081 | |
| 2082 | mutex_lock(&ps->smi_mutex); |
Vivien Didelot | 8057b3e | 2015-10-08 11:35:14 -0400 | [diff] [blame] | 2083 | ret = _mv88e6xxx_port_fdb_load(ds, port, fdb->addr, fdb->vid, |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2084 | GLOBAL_ATU_DATA_STATE_UNUSED); |
| 2085 | mutex_unlock(&ps->smi_mutex); |
| 2086 | |
| 2087 | return ret; |
| 2088 | } |
| 2089 | |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2090 | static int _mv88e6xxx_atu_getnext(struct dsa_switch *ds, u16 fid, |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2091 | struct mv88e6xxx_atu_entry *entry) |
David S. Miller | cdf0969 | 2015-08-11 12:00:37 -0700 | [diff] [blame] | 2092 | { |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2093 | struct mv88e6xxx_atu_entry next = { 0 }; |
| 2094 | int ret; |
| 2095 | |
| 2096 | next.fid = fid; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2097 | |
| 2098 | ret = _mv88e6xxx_atu_wait(ds); |
| 2099 | if (ret < 0) |
| 2100 | return ret; |
| 2101 | |
Vivien Didelot | b426e5f | 2016-03-31 16:53:42 -0400 | [diff] [blame] | 2102 | ret = _mv88e6xxx_atu_cmd(ds, fid, GLOBAL_ATU_OP_GET_NEXT_DB); |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2103 | if (ret < 0) |
| 2104 | return ret; |
| 2105 | |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2106 | ret = _mv88e6xxx_atu_mac_read(ds, next.mac); |
| 2107 | if (ret < 0) |
| 2108 | return ret; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2109 | |
Vivien Didelot | 1d19404 | 2015-08-10 09:09:51 -0400 | [diff] [blame] | 2110 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, GLOBAL_ATU_DATA); |
| 2111 | if (ret < 0) |
| 2112 | return ret; |
| 2113 | |
| 2114 | next.state = ret & GLOBAL_ATU_DATA_STATE_MASK; |
| 2115 | if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) { |
| 2116 | unsigned int mask, shift; |
| 2117 | |
| 2118 | if (ret & GLOBAL_ATU_DATA_TRUNK) { |
| 2119 | next.trunk = true; |
| 2120 | mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK; |
| 2121 | shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT; |
| 2122 | } else { |
| 2123 | next.trunk = false; |
| 2124 | mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK; |
| 2125 | shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT; |
| 2126 | } |
| 2127 | |
| 2128 | next.portv_trunkid = (ret & mask) >> shift; |
| 2129 | } |
| 2130 | |
| 2131 | *entry = next; |
Guenter Roeck | defb05b | 2015-03-26 18:36:38 -0700 | [diff] [blame] | 2132 | return 0; |
| 2133 | } |
| 2134 | |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2135 | static int _mv88e6xxx_port_fdb_dump_one(struct dsa_switch *ds, u16 fid, u16 vid, |
| 2136 | int port, |
| 2137 | struct switchdev_obj_port_fdb *fdb, |
| 2138 | int (*cb)(struct switchdev_obj *obj)) |
| 2139 | { |
| 2140 | struct mv88e6xxx_atu_entry addr = { |
| 2141 | .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, |
| 2142 | }; |
| 2143 | int err; |
| 2144 | |
| 2145 | err = _mv88e6xxx_atu_mac_write(ds, addr.mac); |
| 2146 | if (err) |
| 2147 | return err; |
| 2148 | |
| 2149 | do { |
| 2150 | err = _mv88e6xxx_atu_getnext(ds, fid, &addr); |
| 2151 | if (err) |
| 2152 | break; |
| 2153 | |
| 2154 | if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED) |
| 2155 | break; |
| 2156 | |
| 2157 | if (!addr.trunk && addr.portv_trunkid & BIT(port)) { |
| 2158 | bool is_static = addr.state == |
| 2159 | (is_multicast_ether_addr(addr.mac) ? |
| 2160 | GLOBAL_ATU_DATA_STATE_MC_STATIC : |
| 2161 | GLOBAL_ATU_DATA_STATE_UC_STATIC); |
| 2162 | |
| 2163 | fdb->vid = vid; |
| 2164 | ether_addr_copy(fdb->addr, addr.mac); |
| 2165 | fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
| 2166 | |
| 2167 | err = cb(&fdb->obj); |
| 2168 | if (err) |
| 2169 | break; |
| 2170 | } |
| 2171 | } while (!is_broadcast_ether_addr(addr.mac)); |
| 2172 | |
| 2173 | return err; |
| 2174 | } |
| 2175 | |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2176 | int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port, |
| 2177 | struct switchdev_obj_port_fdb *fdb, |
| 2178 | int (*cb)(struct switchdev_obj *obj)) |
| 2179 | { |
| 2180 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2181 | struct mv88e6xxx_vtu_stu_entry vlan = { |
| 2182 | .vid = GLOBAL_VTU_VID_MASK, /* all ones */ |
| 2183 | }; |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2184 | u16 fid; |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2185 | int err; |
| 2186 | |
| 2187 | mutex_lock(&ps->smi_mutex); |
| 2188 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2189 | /* Dump port's default Filtering Information Database (VLAN ID 0) */ |
| 2190 | err = _mv88e6xxx_port_fid_get(ds, port, &fid); |
| 2191 | if (err) |
| 2192 | goto unlock; |
| 2193 | |
| 2194 | err = _mv88e6xxx_port_fdb_dump_one(ds, fid, 0, port, fdb, cb); |
| 2195 | if (err) |
| 2196 | goto unlock; |
| 2197 | |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2198 | /* Dump VLANs' Filtering Information Databases */ |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2199 | err = _mv88e6xxx_vtu_vid_write(ds, vlan.vid); |
| 2200 | if (err) |
| 2201 | goto unlock; |
| 2202 | |
| 2203 | do { |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2204 | err = _mv88e6xxx_vtu_getnext(ds, &vlan); |
| 2205 | if (err) |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2206 | break; |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2207 | |
| 2208 | if (!vlan.valid) |
| 2209 | break; |
| 2210 | |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2211 | err = _mv88e6xxx_port_fdb_dump_one(ds, vlan.fid, vlan.vid, port, |
| 2212 | fdb, cb); |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2213 | if (err) |
Vivien Didelot | 74b6ba0 | 2016-02-26 13:16:02 -0500 | [diff] [blame] | 2214 | break; |
Vivien Didelot | f33475b | 2015-10-22 09:34:41 -0400 | [diff] [blame] | 2215 | } while (vlan.vid < GLOBAL_VTU_VID_MASK); |
| 2216 | |
| 2217 | unlock: |
| 2218 | mutex_unlock(&ps->smi_mutex); |
| 2219 | |
| 2220 | return err; |
| 2221 | } |
| 2222 | |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2223 | int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port, |
| 2224 | struct net_device *bridge) |
Vivien Didelot | e79a8bc | 2015-11-04 17:23:40 -0500 | [diff] [blame] | 2225 | { |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2226 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2227 | u16 fid; |
| 2228 | int i, err; |
| 2229 | |
| 2230 | mutex_lock(&ps->smi_mutex); |
| 2231 | |
| 2232 | /* Get or create the bridge FID and assign it to the port */ |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2233 | for (i = 0; i < ps->info->num_ports; ++i) |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2234 | if (ps->ports[i].bridge_dev == bridge) |
| 2235 | break; |
| 2236 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2237 | if (i < ps->info->num_ports) |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2238 | err = _mv88e6xxx_port_fid_get(ds, i, &fid); |
| 2239 | else |
| 2240 | err = _mv88e6xxx_fid_new(ds, &fid); |
| 2241 | if (err) |
| 2242 | goto unlock; |
| 2243 | |
| 2244 | err = _mv88e6xxx_port_fid_set(ds, port, fid); |
| 2245 | if (err) |
| 2246 | goto unlock; |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2247 | |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2248 | /* Assign the bridge and remap each port's VLANTable */ |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2249 | ps->ports[port].bridge_dev = bridge; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2250 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2251 | for (i = 0; i < ps->info->num_ports; ++i) { |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2252 | if (ps->ports[i].bridge_dev == bridge) { |
| 2253 | err = _mv88e6xxx_port_based_vlan_map(ds, i); |
| 2254 | if (err) |
| 2255 | break; |
| 2256 | } |
| 2257 | } |
| 2258 | |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2259 | unlock: |
| 2260 | mutex_unlock(&ps->smi_mutex); |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2261 | |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2262 | return err; |
Vivien Didelot | e79a8bc | 2015-11-04 17:23:40 -0500 | [diff] [blame] | 2263 | } |
| 2264 | |
Vivien Didelot | 16bfa70 | 2016-03-13 16:21:33 -0400 | [diff] [blame] | 2265 | void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port) |
Vivien Didelot | e79a8bc | 2015-11-04 17:23:40 -0500 | [diff] [blame] | 2266 | { |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2267 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2268 | struct net_device *bridge = ps->ports[port].bridge_dev; |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2269 | u16 fid; |
Vivien Didelot | 16bfa70 | 2016-03-13 16:21:33 -0400 | [diff] [blame] | 2270 | int i; |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2271 | |
| 2272 | mutex_lock(&ps->smi_mutex); |
| 2273 | |
| 2274 | /* Give the port a fresh Filtering Information Database */ |
Vivien Didelot | 16bfa70 | 2016-03-13 16:21:33 -0400 | [diff] [blame] | 2275 | if (_mv88e6xxx_fid_new(ds, &fid) || |
| 2276 | _mv88e6xxx_port_fid_set(ds, port, fid)) |
| 2277 | netdev_warn(ds->ports[port], "failed to assign a new FID\n"); |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2278 | |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2279 | /* Unassign the bridge and remap each port's VLANTable */ |
Vivien Didelot | a669275 | 2016-02-12 12:09:39 -0500 | [diff] [blame] | 2280 | ps->ports[port].bridge_dev = NULL; |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2281 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2282 | for (i = 0; i < ps->info->num_ports; ++i) |
Vivien Didelot | 16bfa70 | 2016-03-13 16:21:33 -0400 | [diff] [blame] | 2283 | if (i == port || ps->ports[i].bridge_dev == bridge) |
| 2284 | if (_mv88e6xxx_port_based_vlan_map(ds, i)) |
| 2285 | netdev_warn(ds->ports[i], "failed to remap\n"); |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2286 | |
Vivien Didelot | 466dfa0 | 2016-02-26 13:16:05 -0500 | [diff] [blame] | 2287 | mutex_unlock(&ps->smi_mutex); |
Vivien Didelot | 66d9cd0 | 2016-02-05 14:07:14 -0500 | [diff] [blame] | 2288 | } |
| 2289 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 2290 | static void mv88e6xxx_bridge_work(struct work_struct *work) |
| 2291 | { |
| 2292 | struct mv88e6xxx_priv_state *ps; |
| 2293 | struct dsa_switch *ds; |
| 2294 | int port; |
| 2295 | |
| 2296 | ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work); |
Andrew Lunn | 7543a6d | 2016-04-13 02:40:40 +0200 | [diff] [blame] | 2297 | ds = ps->ds; |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 2298 | |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 2299 | mutex_lock(&ps->smi_mutex); |
| 2300 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2301 | for (port = 0; port < ps->info->num_ports; ++port) |
Vivien Didelot | 2d9deae | 2016-03-07 18:24:17 -0500 | [diff] [blame] | 2302 | if (test_and_clear_bit(port, ps->port_state_update_mask) && |
| 2303 | _mv88e6xxx_port_state(ds, port, ps->ports[port].state)) |
| 2304 | netdev_warn(ds->ports[port], "failed to update state to %s\n", |
| 2305 | mv88e6xxx_port_state_names[ps->ports[port].state]); |
| 2306 | |
| 2307 | mutex_unlock(&ps->smi_mutex); |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 2308 | } |
| 2309 | |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2310 | static int _mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page, |
| 2311 | int reg, int val) |
| 2312 | { |
| 2313 | int ret; |
| 2314 | |
| 2315 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
| 2316 | if (ret < 0) |
| 2317 | goto restore_page_0; |
| 2318 | |
| 2319 | ret = _mv88e6xxx_phy_write_indirect(ds, port, reg, val); |
| 2320 | restore_page_0: |
| 2321 | _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
| 2322 | |
| 2323 | return ret; |
| 2324 | } |
| 2325 | |
| 2326 | static int _mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, |
| 2327 | int reg) |
| 2328 | { |
| 2329 | int ret; |
| 2330 | |
| 2331 | ret = _mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
| 2332 | if (ret < 0) |
| 2333 | goto restore_page_0; |
| 2334 | |
| 2335 | ret = _mv88e6xxx_phy_read_indirect(ds, port, reg); |
| 2336 | restore_page_0: |
| 2337 | _mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
| 2338 | |
| 2339 | return ret; |
| 2340 | } |
| 2341 | |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2342 | static int mv88e6xxx_power_on_serdes(struct dsa_switch *ds) |
| 2343 | { |
| 2344 | int ret; |
| 2345 | |
| 2346 | ret = _mv88e6xxx_phy_page_read(ds, REG_FIBER_SERDES, PAGE_FIBER_SERDES, |
| 2347 | MII_BMCR); |
| 2348 | if (ret < 0) |
| 2349 | return ret; |
| 2350 | |
| 2351 | if (ret & BMCR_PDOWN) { |
| 2352 | ret &= ~BMCR_PDOWN; |
| 2353 | ret = _mv88e6xxx_phy_page_write(ds, REG_FIBER_SERDES, |
| 2354 | PAGE_FIBER_SERDES, MII_BMCR, |
| 2355 | ret); |
| 2356 | } |
| 2357 | |
| 2358 | return ret; |
| 2359 | } |
| 2360 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 2361 | static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port) |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2362 | { |
| 2363 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Vivien Didelot | f02bdff | 2015-10-11 18:08:36 -0400 | [diff] [blame] | 2364 | int ret; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2365 | u16 reg; |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2366 | |
| 2367 | mutex_lock(&ps->smi_mutex); |
| 2368 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2369 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2370 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2371 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2372 | mv88e6xxx_6065_family(ds) || mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2373 | /* MAC Forcing register: don't force link, speed, |
| 2374 | * duplex or flow control state to any particular |
| 2375 | * values on physical ports, but force the CPU port |
| 2376 | * and all DSA ports to their maximum bandwidth and |
| 2377 | * full duplex. |
| 2378 | */ |
| 2379 | reg = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_PCS_CTRL); |
Andrew Lunn | 60045cb | 2015-08-17 23:52:51 +0200 | [diff] [blame] | 2380 | 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] | 2381 | reg &= ~PORT_PCS_CTRL_UNFORCED; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2382 | reg |= PORT_PCS_CTRL_FORCE_LINK | |
| 2383 | PORT_PCS_CTRL_LINK_UP | |
| 2384 | PORT_PCS_CTRL_DUPLEX_FULL | |
| 2385 | PORT_PCS_CTRL_FORCE_DUPLEX; |
| 2386 | if (mv88e6xxx_6065_family(ds)) |
| 2387 | reg |= PORT_PCS_CTRL_100; |
| 2388 | else |
| 2389 | reg |= PORT_PCS_CTRL_1000; |
| 2390 | } else { |
| 2391 | reg |= PORT_PCS_CTRL_UNFORCED; |
| 2392 | } |
| 2393 | |
| 2394 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2395 | PORT_PCS_CTRL, reg); |
| 2396 | if (ret) |
| 2397 | goto abort; |
| 2398 | } |
| 2399 | |
| 2400 | /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock, |
| 2401 | * disable Header mode, enable IGMP/MLD snooping, disable VLAN |
| 2402 | * tunneling, determine priority by looking at 802.1p and IP |
| 2403 | * priority fields (IP prio has precedence), and set STP state |
| 2404 | * to Forwarding. |
| 2405 | * |
| 2406 | * If this is the CPU link, use DSA or EDSA tagging depending |
| 2407 | * on which tagging mode was configured. |
| 2408 | * |
| 2409 | * If this is a link to another switch, use DSA tagging mode. |
| 2410 | * |
| 2411 | * If this is the upstream port for this switch, enable |
| 2412 | * forwarding of unknown unicasts and multicasts. |
| 2413 | */ |
| 2414 | reg = 0; |
| 2415 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2416 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2417 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2418 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2419 | reg = PORT_CONTROL_IGMP_MLD_SNOOP | |
| 2420 | PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP | |
| 2421 | PORT_CONTROL_STATE_FORWARDING; |
| 2422 | if (dsa_is_cpu_port(ds, port)) { |
| 2423 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) |
| 2424 | reg |= PORT_CONTROL_DSA_TAG; |
| 2425 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2426 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2427 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2428 | if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA) |
| 2429 | reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA; |
| 2430 | else |
| 2431 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
Andrew Lunn | c047a1f | 2015-09-29 01:50:56 +0200 | [diff] [blame] | 2432 | reg |= PORT_CONTROL_FORWARD_UNKNOWN | |
| 2433 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2437 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2438 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6065_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2439 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2440 | if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA) |
| 2441 | reg |= PORT_CONTROL_EGRESS_ADD_TAG; |
| 2442 | } |
| 2443 | } |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2444 | if (dsa_is_dsa_port(ds, port)) { |
| 2445 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) |
| 2446 | reg |= PORT_CONTROL_DSA_TAG; |
| 2447 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2448 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2449 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2450 | reg |= PORT_CONTROL_FRAME_MODE_DSA; |
Andrew Lunn | 6083ce7 | 2015-08-17 23:52:52 +0200 | [diff] [blame] | 2451 | } |
| 2452 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2453 | if (port == dsa_upstream_port(ds)) |
| 2454 | reg |= PORT_CONTROL_FORWARD_UNKNOWN | |
| 2455 | PORT_CONTROL_FORWARD_UNKNOWN_MC; |
| 2456 | } |
| 2457 | if (reg) { |
| 2458 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2459 | PORT_CONTROL, reg); |
| 2460 | if (ret) |
| 2461 | goto abort; |
| 2462 | } |
| 2463 | |
Patrick Uiterwijk | 13a7ebb | 2016-03-30 01:39:41 +0000 | [diff] [blame] | 2464 | /* If this port is connected to a SerDes, make sure the SerDes is not |
| 2465 | * powered down. |
| 2466 | */ |
| 2467 | if (mv88e6xxx_6352_family(ds)) { |
| 2468 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(port), PORT_STATUS); |
| 2469 | if (ret < 0) |
| 2470 | goto abort; |
| 2471 | ret &= PORT_STATUS_CMODE_MASK; |
| 2472 | if ((ret == PORT_STATUS_CMODE_100BASE_X) || |
| 2473 | (ret == PORT_STATUS_CMODE_1000BASE_X) || |
| 2474 | (ret == PORT_STATUS_CMODE_SGMII)) { |
| 2475 | ret = mv88e6xxx_power_on_serdes(ds); |
| 2476 | if (ret < 0) |
| 2477 | goto abort; |
| 2478 | } |
| 2479 | } |
| 2480 | |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2481 | /* 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] | 2482 | * 10240 bytes, disable 802.1q tags checking, don't discard tagged or |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2483 | * untagged frames on this port, do a destination address lookup on all |
| 2484 | * received packets as usual, disable ARP mirroring and don't send a |
| 2485 | * copy of all transmitted/received frames on this port to the CPU. |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2486 | */ |
| 2487 | reg = 0; |
| 2488 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2489 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Vivien Didelot | f93dd04 | 2016-03-31 16:53:45 -0400 | [diff] [blame] | 2490 | mv88e6xxx_6095_family(ds) || mv88e6xxx_6320_family(ds) || |
| 2491 | mv88e6xxx_6185_family(ds)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2492 | reg = PORT_CONTROL_2_MAP_DA; |
| 2493 | |
| 2494 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2495 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6320_family(ds)) |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2496 | reg |= PORT_CONTROL_2_JUMBO_10240; |
| 2497 | |
| 2498 | if (mv88e6xxx_6095_family(ds) || mv88e6xxx_6185_family(ds)) { |
| 2499 | /* Set the upstream port this port should use */ |
| 2500 | reg |= dsa_upstream_port(ds); |
| 2501 | /* enable forwarding of unknown multicast addresses to |
| 2502 | * the upstream port |
| 2503 | */ |
| 2504 | if (port == dsa_upstream_port(ds)) |
| 2505 | reg |= PORT_CONTROL_2_FORWARD_UNKNOWN; |
| 2506 | } |
| 2507 | |
Vivien Didelot | 46fbe5e | 2016-02-26 13:16:07 -0500 | [diff] [blame] | 2508 | reg |= PORT_CONTROL_2_8021Q_DISABLED; |
Vivien Didelot | 8efdda4 | 2015-08-13 12:52:23 -0400 | [diff] [blame] | 2509 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2510 | if (reg) { |
| 2511 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2512 | PORT_CONTROL_2, reg); |
| 2513 | if (ret) |
| 2514 | goto abort; |
| 2515 | } |
| 2516 | |
| 2517 | /* Port Association Vector: when learning source addresses |
| 2518 | * of packets, add the address to the address database using |
| 2519 | * a port bitmap that has only the bit for this port set and |
| 2520 | * the other bits clear. |
| 2521 | */ |
Andrew Lunn | 4c7ea3c | 2015-11-03 10:52:36 -0500 | [diff] [blame] | 2522 | reg = 1 << port; |
| 2523 | /* Disable learning for DSA and CPU ports */ |
| 2524 | if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) |
| 2525 | reg = PORT_ASSOC_VECTOR_LOCKED_PORT; |
| 2526 | |
| 2527 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_ASSOC_VECTOR, reg); |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2528 | if (ret) |
| 2529 | goto abort; |
| 2530 | |
| 2531 | /* Egress rate control 2: disable egress rate control. */ |
| 2532 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_RATE_CONTROL_2, |
| 2533 | 0x0000); |
| 2534 | if (ret) |
| 2535 | goto abort; |
| 2536 | |
| 2537 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2538 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2539 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2540 | /* Do not limit the period of time that this port can |
| 2541 | * be paused for by the remote end or the period of |
| 2542 | * time that this port can pause the remote end. |
| 2543 | */ |
| 2544 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2545 | PORT_PAUSE_CTRL, 0x0000); |
| 2546 | if (ret) |
| 2547 | goto abort; |
| 2548 | |
| 2549 | /* Port ATU control: disable limiting the number of |
| 2550 | * address database entries that this port is allowed |
| 2551 | * to use. |
| 2552 | */ |
| 2553 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2554 | PORT_ATU_CONTROL, 0x0000); |
| 2555 | /* Priority Override: disable DA, SA and VTU priority |
| 2556 | * override. |
| 2557 | */ |
| 2558 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2559 | PORT_PRI_OVERRIDE, 0x0000); |
| 2560 | if (ret) |
| 2561 | goto abort; |
| 2562 | |
| 2563 | /* Port Ethertype: use the Ethertype DSA Ethertype |
| 2564 | * value. |
| 2565 | */ |
| 2566 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2567 | PORT_ETH_TYPE, ETH_P_EDSA); |
| 2568 | if (ret) |
| 2569 | goto abort; |
| 2570 | /* Tag Remap: use an identity 802.1p prio -> switch |
| 2571 | * prio mapping. |
| 2572 | */ |
| 2573 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2574 | PORT_TAG_REGMAP_0123, 0x3210); |
| 2575 | if (ret) |
| 2576 | goto abort; |
| 2577 | |
| 2578 | /* Tag Remap 2: use an identity 802.1p prio -> switch |
| 2579 | * prio mapping. |
| 2580 | */ |
| 2581 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2582 | PORT_TAG_REGMAP_4567, 0x7654); |
| 2583 | if (ret) |
| 2584 | goto abort; |
| 2585 | } |
| 2586 | |
| 2587 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2588 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2589 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
| 2590 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2591 | /* Rate Control: disable ingress rate limiting. */ |
| 2592 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), |
| 2593 | PORT_RATE_CONTROL, 0x0001); |
| 2594 | if (ret) |
| 2595 | goto abort; |
| 2596 | } |
| 2597 | |
Guenter Roeck | 366f0a0 | 2015-03-26 18:36:30 -0700 | [diff] [blame] | 2598 | /* Port Control 1: disable trunking, disable sending |
| 2599 | * learning messages to this port. |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2600 | */ |
Vivien Didelot | 614f03f | 2015-04-20 17:19:23 -0400 | [diff] [blame] | 2601 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_CONTROL_1, 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2602 | if (ret) |
| 2603 | goto abort; |
| 2604 | |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2605 | /* Port based VLAN map: give each port its own address |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2606 | * database, and allow bidirectional communication between the |
| 2607 | * CPU and DSA port(s), and the other ports. |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2608 | */ |
Vivien Didelot | 2db9ce1 | 2016-02-26 13:16:04 -0500 | [diff] [blame] | 2609 | ret = _mv88e6xxx_port_fid_set(ds, port, port + 1); |
| 2610 | if (ret) |
| 2611 | goto abort; |
| 2612 | |
Vivien Didelot | b7666ef | 2016-02-26 13:16:06 -0500 | [diff] [blame] | 2613 | ret = _mv88e6xxx_port_based_vlan_map(ds, port); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2614 | if (ret) |
| 2615 | goto abort; |
| 2616 | |
| 2617 | /* Default VLAN ID and priority: don't set a default VLAN |
| 2618 | * ID, and set the default packet priority to zero. |
| 2619 | */ |
Vivien Didelot | 47cf1e6 | 2015-04-20 17:43:26 -0400 | [diff] [blame] | 2620 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), PORT_DEFAULT_VLAN, |
| 2621 | 0x0000); |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame] | 2622 | abort: |
| 2623 | mutex_unlock(&ps->smi_mutex); |
| 2624 | return ret; |
| 2625 | } |
| 2626 | |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 2627 | int mv88e6xxx_setup_ports(struct dsa_switch *ds) |
| 2628 | { |
| 2629 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2630 | int ret; |
| 2631 | int i; |
| 2632 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2633 | for (i = 0; i < ps->info->num_ports; i++) { |
Andrew Lunn | dbde9e6 | 2015-05-06 01:09:48 +0200 | [diff] [blame] | 2634 | ret = mv88e6xxx_setup_port(ds, i); |
| 2635 | if (ret < 0) |
| 2636 | return ret; |
| 2637 | } |
| 2638 | return 0; |
| 2639 | } |
| 2640 | |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 2641 | int mv88e6xxx_setup_common(struct dsa_switch *ds) |
| 2642 | { |
| 2643 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2644 | |
Andrew Lunn | 7543a6d | 2016-04-13 02:40:40 +0200 | [diff] [blame] | 2645 | ps->ds = ds; |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 2646 | mutex_init(&ps->smi_mutex); |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 2647 | |
Guenter Roeck | facd95b | 2015-03-26 18:36:35 -0700 | [diff] [blame] | 2648 | INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work); |
| 2649 | |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 2650 | return 0; |
| 2651 | } |
| 2652 | |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2653 | int mv88e6xxx_setup_global(struct dsa_switch *ds) |
| 2654 | { |
| 2655 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2656 | int err; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2657 | int i; |
| 2658 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2659 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2660 | /* Set the default address aging time to 5 minutes, and |
| 2661 | * enable address learn messages to be sent to all message |
| 2662 | * ports. |
| 2663 | */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2664 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_ATU_CONTROL, |
| 2665 | 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL); |
| 2666 | if (err) |
| 2667 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2668 | |
| 2669 | /* Configure the IP ToS mapping registers. */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2670 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000); |
| 2671 | if (err) |
| 2672 | goto unlock; |
| 2673 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000); |
| 2674 | if (err) |
| 2675 | goto unlock; |
| 2676 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555); |
| 2677 | if (err) |
| 2678 | goto unlock; |
| 2679 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555); |
| 2680 | if (err) |
| 2681 | goto unlock; |
| 2682 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa); |
| 2683 | if (err) |
| 2684 | goto unlock; |
| 2685 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa); |
| 2686 | if (err) |
| 2687 | goto unlock; |
| 2688 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff); |
| 2689 | if (err) |
| 2690 | goto unlock; |
| 2691 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff); |
| 2692 | if (err) |
| 2693 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2694 | |
| 2695 | /* Configure the IEEE 802.1p priority mapping register. */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2696 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41); |
| 2697 | if (err) |
| 2698 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2699 | |
| 2700 | /* Send all frames with destination addresses matching |
| 2701 | * 01:80:c2:00:00:0x to the CPU port. |
| 2702 | */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2703 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff); |
| 2704 | if (err) |
| 2705 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2706 | |
| 2707 | /* Ignore removed tag data on doubly tagged packets, disable |
| 2708 | * flow control messages, force flow control priority to the |
| 2709 | * highest, and send all special multicast frames to the CPU |
| 2710 | * port at the highest priority. |
| 2711 | */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2712 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, |
| 2713 | 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 | |
| 2714 | GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI); |
| 2715 | if (err) |
| 2716 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2717 | |
| 2718 | /* Program the DSA routing table. */ |
| 2719 | for (i = 0; i < 32; i++) { |
| 2720 | int nexthop = 0x1f; |
| 2721 | |
| 2722 | if (ds->pd->rtable && |
| 2723 | i != ds->index && i < ds->dst->pd->nr_chips) |
| 2724 | nexthop = ds->pd->rtable[i] & 0x1f; |
| 2725 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2726 | err = _mv88e6xxx_reg_write( |
| 2727 | ds, REG_GLOBAL2, |
| 2728 | GLOBAL2_DEVICE_MAPPING, |
| 2729 | GLOBAL2_DEVICE_MAPPING_UPDATE | |
| 2730 | (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop); |
| 2731 | if (err) |
| 2732 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | /* Clear all trunk masks. */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2736 | for (i = 0; i < 8; i++) { |
| 2737 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, |
| 2738 | 0x8000 | |
| 2739 | (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2740 | ((1 << ps->info->num_ports) - 1)); |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2741 | if (err) |
| 2742 | goto unlock; |
| 2743 | } |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2744 | |
| 2745 | /* Clear all trunk mappings. */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2746 | for (i = 0; i < 16; i++) { |
| 2747 | err = _mv88e6xxx_reg_write( |
| 2748 | ds, REG_GLOBAL2, |
| 2749 | GLOBAL2_TRUNK_MAPPING, |
| 2750 | GLOBAL2_TRUNK_MAPPING_UPDATE | |
| 2751 | (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT)); |
| 2752 | if (err) |
| 2753 | goto unlock; |
| 2754 | } |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2755 | |
| 2756 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2757 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
| 2758 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2759 | /* Send all frames with destination addresses matching |
| 2760 | * 01:80:c2:00:00:2x to the CPU port. |
| 2761 | */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2762 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, |
| 2763 | GLOBAL2_MGMT_EN_2X, 0xffff); |
| 2764 | if (err) |
| 2765 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2766 | |
| 2767 | /* Initialise cross-chip port VLAN table to reset |
| 2768 | * defaults. |
| 2769 | */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2770 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, |
| 2771 | GLOBAL2_PVT_ADDR, 0x9000); |
| 2772 | if (err) |
| 2773 | goto unlock; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2774 | |
| 2775 | /* Clear the priority override table. */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2776 | for (i = 0; i < 16; i++) { |
| 2777 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, |
| 2778 | GLOBAL2_PRIO_OVERRIDE, |
| 2779 | 0x8000 | (i << 8)); |
| 2780 | if (err) |
| 2781 | goto unlock; |
| 2782 | } |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | if (mv88e6xxx_6352_family(ds) || mv88e6xxx_6351_family(ds) || |
| 2786 | mv88e6xxx_6165_family(ds) || mv88e6xxx_6097_family(ds) || |
Aleksey S. Kazantsev | 7c3d0d6 | 2015-07-07 20:38:15 -0700 | [diff] [blame] | 2787 | mv88e6xxx_6185_family(ds) || mv88e6xxx_6095_family(ds) || |
| 2788 | mv88e6xxx_6320_family(ds)) { |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2789 | /* Disable ingress rate limiting by resetting all |
| 2790 | * ingress rate limit registers to their initial |
| 2791 | * state. |
| 2792 | */ |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2793 | for (i = 0; i < ps->info->num_ports; i++) { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2794 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL2, |
| 2795 | GLOBAL2_INGRESS_OP, |
| 2796 | 0x9000 | (i << 8)); |
| 2797 | if (err) |
| 2798 | goto unlock; |
| 2799 | } |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2800 | } |
| 2801 | |
Andrew Lunn | db687a5 | 2015-06-20 21:31:29 +0200 | [diff] [blame] | 2802 | /* Clear the statistics counters for all ports */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2803 | err = _mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_STATS_OP, |
| 2804 | GLOBAL_STATS_OP_FLUSH_ALL); |
| 2805 | if (err) |
| 2806 | goto unlock; |
Andrew Lunn | db687a5 | 2015-06-20 21:31:29 +0200 | [diff] [blame] | 2807 | |
| 2808 | /* Wait for the flush to complete. */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2809 | err = _mv88e6xxx_stats_wait(ds); |
| 2810 | if (err < 0) |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 2811 | goto unlock; |
| 2812 | |
Vivien Didelot | c161d0a | 2015-09-04 14:34:13 -0400 | [diff] [blame] | 2813 | /* Clear all ATU entries */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2814 | err = _mv88e6xxx_atu_flush(ds, 0, true); |
| 2815 | if (err < 0) |
Vivien Didelot | c161d0a | 2015-09-04 14:34:13 -0400 | [diff] [blame] | 2816 | goto unlock; |
| 2817 | |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 2818 | /* Clear all the VTU and STU entries */ |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2819 | err = _mv88e6xxx_vtu_stu_flush(ds); |
Vivien Didelot | 6b17e86 | 2015-08-13 12:52:18 -0400 | [diff] [blame] | 2820 | unlock: |
Vivien Didelot | 24751e2 | 2015-08-03 09:17:44 -0400 | [diff] [blame] | 2821 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | db687a5 | 2015-06-20 21:31:29 +0200 | [diff] [blame] | 2822 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2823 | return err; |
Andrew Lunn | 54d792f | 2015-05-06 01:09:47 +0200 | [diff] [blame] | 2824 | } |
| 2825 | |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2826 | int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active) |
| 2827 | { |
| 2828 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2829 | u16 is_reset = (ppu_active ? 0x8800 : 0xc800); |
Andrew Lunn | c8c1b39 | 2015-11-20 03:56:24 +0100 | [diff] [blame] | 2830 | struct gpio_desc *gpiod = ds->pd->reset; |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2831 | unsigned long timeout; |
| 2832 | int ret; |
| 2833 | int i; |
| 2834 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2835 | mutex_lock(&ps->smi_mutex); |
| 2836 | |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2837 | /* Set all ports to the disabled state. */ |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2838 | for (i = 0; i < ps->info->num_ports; i++) { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2839 | ret = _mv88e6xxx_reg_read(ds, REG_PORT(i), PORT_CONTROL); |
| 2840 | if (ret < 0) |
| 2841 | goto unlock; |
| 2842 | |
| 2843 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(i), PORT_CONTROL, |
| 2844 | ret & 0xfffc); |
| 2845 | if (ret) |
| 2846 | goto unlock; |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | /* Wait for transmit queues to drain. */ |
| 2850 | usleep_range(2000, 4000); |
| 2851 | |
Andrew Lunn | c8c1b39 | 2015-11-20 03:56:24 +0100 | [diff] [blame] | 2852 | /* If there is a gpio connected to the reset pin, toggle it */ |
| 2853 | if (gpiod) { |
| 2854 | gpiod_set_value_cansleep(gpiod, 1); |
| 2855 | usleep_range(10000, 20000); |
| 2856 | gpiod_set_value_cansleep(gpiod, 0); |
| 2857 | usleep_range(10000, 20000); |
| 2858 | } |
| 2859 | |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2860 | /* Reset the switch. Keep the PPU active if requested. The PPU |
| 2861 | * needs to be active to support indirect phy register access |
| 2862 | * through global registers 0x18 and 0x19. |
| 2863 | */ |
| 2864 | if (ppu_active) |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2865 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x04, 0xc000); |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2866 | else |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2867 | ret = _mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x04, 0xc400); |
| 2868 | if (ret) |
| 2869 | goto unlock; |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2870 | |
| 2871 | /* Wait up to one second for reset to complete. */ |
| 2872 | timeout = jiffies + 1 * HZ; |
| 2873 | while (time_before(jiffies, timeout)) { |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2874 | ret = _mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x00); |
| 2875 | if (ret < 0) |
| 2876 | goto unlock; |
| 2877 | |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2878 | if ((ret & is_reset) == is_reset) |
| 2879 | break; |
| 2880 | usleep_range(1000, 2000); |
| 2881 | } |
| 2882 | if (time_after(jiffies, timeout)) |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2883 | ret = -ETIMEDOUT; |
| 2884 | else |
| 2885 | ret = 0; |
| 2886 | unlock: |
| 2887 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2888 | |
Andrew Lunn | 48ace4e | 2016-04-14 23:47:12 +0200 | [diff] [blame] | 2889 | return ret; |
Andrew Lunn | 143a830 | 2015-04-02 04:06:34 +0200 | [diff] [blame] | 2890 | } |
| 2891 | |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2892 | int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg) |
| 2893 | { |
| 2894 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2895 | int ret; |
| 2896 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2897 | mutex_lock(&ps->smi_mutex); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2898 | ret = _mv88e6xxx_phy_page_read(ds, port, page, reg); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2899 | mutex_unlock(&ps->smi_mutex); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2900 | |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2901 | return ret; |
| 2902 | } |
| 2903 | |
| 2904 | int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page, |
| 2905 | int reg, int val) |
| 2906 | { |
| 2907 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2908 | int ret; |
| 2909 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2910 | mutex_lock(&ps->smi_mutex); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2911 | ret = _mv88e6xxx_phy_page_write(ds, port, page, reg, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2912 | mutex_unlock(&ps->smi_mutex); |
Patrick Uiterwijk | 75baacf | 2016-03-30 01:39:40 +0000 | [diff] [blame] | 2913 | |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2914 | return ret; |
| 2915 | } |
| 2916 | |
| 2917 | static int mv88e6xxx_port_to_phy_addr(struct dsa_switch *ds, int port) |
| 2918 | { |
| 2919 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2920 | |
Vivien Didelot | 009a2b9 | 2016-04-17 13:24:01 -0400 | [diff] [blame^] | 2921 | if (port >= 0 && port < ps->info->num_ports) |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2922 | return port; |
| 2923 | return -EINVAL; |
| 2924 | } |
| 2925 | |
| 2926 | int |
| 2927 | mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum) |
| 2928 | { |
| 2929 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2930 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2931 | int ret; |
| 2932 | |
| 2933 | if (addr < 0) |
| 2934 | return addr; |
| 2935 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2936 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2937 | ret = _mv88e6xxx_phy_read(ds, addr, regnum); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2938 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2939 | return ret; |
| 2940 | } |
| 2941 | |
| 2942 | int |
| 2943 | mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val) |
| 2944 | { |
| 2945 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2946 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2947 | int ret; |
| 2948 | |
| 2949 | if (addr < 0) |
| 2950 | return addr; |
| 2951 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2952 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2953 | ret = _mv88e6xxx_phy_write(ds, addr, regnum, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2954 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2955 | return ret; |
| 2956 | } |
| 2957 | |
| 2958 | int |
| 2959 | mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int port, int regnum) |
| 2960 | { |
| 2961 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2962 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2963 | int ret; |
| 2964 | |
| 2965 | if (addr < 0) |
| 2966 | return addr; |
| 2967 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2968 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2969 | ret = _mv88e6xxx_phy_read_indirect(ds, addr, regnum); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2970 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2971 | return ret; |
| 2972 | } |
| 2973 | |
| 2974 | int |
| 2975 | mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int port, int regnum, |
| 2976 | u16 val) |
| 2977 | { |
| 2978 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2979 | int addr = mv88e6xxx_port_to_phy_addr(ds, port); |
| 2980 | int ret; |
| 2981 | |
| 2982 | if (addr < 0) |
| 2983 | return addr; |
| 2984 | |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2985 | mutex_lock(&ps->smi_mutex); |
Andrew Lunn | fd3a0ee | 2015-04-02 04:06:36 +0200 | [diff] [blame] | 2986 | ret = _mv88e6xxx_phy_write_indirect(ds, addr, regnum, val); |
Andrew Lunn | 3898c14 | 2015-05-06 01:09:53 +0200 | [diff] [blame] | 2987 | mutex_unlock(&ps->smi_mutex); |
Andrew Lunn | 49143585 | 2015-04-02 04:06:35 +0200 | [diff] [blame] | 2988 | return ret; |
| 2989 | } |
| 2990 | |
Guenter Roeck | c22995c | 2015-07-25 09:42:28 -0700 | [diff] [blame] | 2991 | #ifdef CONFIG_NET_DSA_HWMON |
| 2992 | |
| 2993 | static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp) |
| 2994 | { |
| 2995 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 2996 | int ret; |
| 2997 | int val; |
| 2998 | |
| 2999 | *temp = 0; |
| 3000 | |
| 3001 | mutex_lock(&ps->smi_mutex); |
| 3002 | |
| 3003 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6); |
| 3004 | if (ret < 0) |
| 3005 | goto error; |
| 3006 | |
| 3007 | /* Enable temperature sensor */ |
| 3008 | ret = _mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
| 3009 | if (ret < 0) |
| 3010 | goto error; |
| 3011 | |
| 3012 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5)); |
| 3013 | if (ret < 0) |
| 3014 | goto error; |
| 3015 | |
| 3016 | /* Wait for temperature to stabilize */ |
| 3017 | usleep_range(10000, 12000); |
| 3018 | |
| 3019 | val = _mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
| 3020 | if (val < 0) { |
| 3021 | ret = val; |
| 3022 | goto error; |
| 3023 | } |
| 3024 | |
| 3025 | /* Disable temperature sensor */ |
| 3026 | ret = _mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5)); |
| 3027 | if (ret < 0) |
| 3028 | goto error; |
| 3029 | |
| 3030 | *temp = ((val & 0x1f) - 5) * 5; |
| 3031 | |
| 3032 | error: |
| 3033 | _mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0); |
| 3034 | mutex_unlock(&ps->smi_mutex); |
| 3035 | return ret; |
| 3036 | } |
| 3037 | |
| 3038 | static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp) |
| 3039 | { |
| 3040 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 3041 | int ret; |
| 3042 | |
| 3043 | *temp = 0; |
| 3044 | |
| 3045 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27); |
| 3046 | if (ret < 0) |
| 3047 | return ret; |
| 3048 | |
| 3049 | *temp = (ret & 0xff) - 25; |
| 3050 | |
| 3051 | return 0; |
| 3052 | } |
| 3053 | |
| 3054 | int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp) |
| 3055 | { |
| 3056 | if (mv88e6xxx_6320_family(ds) || mv88e6xxx_6352_family(ds)) |
| 3057 | return mv88e63xx_get_temp(ds, temp); |
| 3058 | |
| 3059 | return mv88e61xx_get_temp(ds, temp); |
| 3060 | } |
| 3061 | |
| 3062 | int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp) |
| 3063 | { |
| 3064 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 3065 | int ret; |
| 3066 | |
| 3067 | if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds)) |
| 3068 | return -EOPNOTSUPP; |
| 3069 | |
| 3070 | *temp = 0; |
| 3071 | |
| 3072 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26); |
| 3073 | if (ret < 0) |
| 3074 | return ret; |
| 3075 | |
| 3076 | *temp = (((ret >> 8) & 0x1f) * 5) - 25; |
| 3077 | |
| 3078 | return 0; |
| 3079 | } |
| 3080 | |
| 3081 | int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp) |
| 3082 | { |
| 3083 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 3084 | int ret; |
| 3085 | |
| 3086 | if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds)) |
| 3087 | return -EOPNOTSUPP; |
| 3088 | |
| 3089 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26); |
| 3090 | if (ret < 0) |
| 3091 | return ret; |
| 3092 | temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f); |
| 3093 | return mv88e6xxx_phy_page_write(ds, phy, 6, 26, |
| 3094 | (ret & 0xe0ff) | (temp << 8)); |
| 3095 | } |
| 3096 | |
| 3097 | int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm) |
| 3098 | { |
| 3099 | int phy = mv88e6xxx_6320_family(ds) ? 3 : 0; |
| 3100 | int ret; |
| 3101 | |
| 3102 | if (!mv88e6xxx_6320_family(ds) && !mv88e6xxx_6352_family(ds)) |
| 3103 | return -EOPNOTSUPP; |
| 3104 | |
| 3105 | *alarm = false; |
| 3106 | |
| 3107 | ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26); |
| 3108 | if (ret < 0) |
| 3109 | return ret; |
| 3110 | |
| 3111 | *alarm = !!(ret & 0x40); |
| 3112 | |
| 3113 | return 0; |
| 3114 | } |
| 3115 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 3116 | |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3117 | static const struct mv88e6xxx_info * |
| 3118 | mv88e6xxx_lookup_info(unsigned int prod_num, const struct mv88e6xxx_info *table, |
Vivien Didelot | 0209d14 | 2016-04-17 13:23:55 -0400 | [diff] [blame] | 3119 | unsigned int num) |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3120 | { |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3121 | int i; |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3122 | |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3123 | for (i = 0; i < num; ++i) |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3124 | if (table[i].prod_num == prod_num) |
| 3125 | return &table[i]; |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3126 | |
Vivien Didelot | b9b3771 | 2015-10-30 19:39:48 -0400 | [diff] [blame] | 3127 | return NULL; |
| 3128 | } |
| 3129 | |
Vivien Didelot | 0209d14 | 2016-04-17 13:23:55 -0400 | [diff] [blame] | 3130 | const char *mv88e6xxx_drv_probe(struct device *dsa_dev, struct device *host_dev, |
| 3131 | int sw_addr, void **priv, |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3132 | const struct mv88e6xxx_info *table, |
Vivien Didelot | 0209d14 | 2016-04-17 13:23:55 -0400 | [diff] [blame] | 3133 | unsigned int num) |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3134 | { |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3135 | const struct mv88e6xxx_info *info; |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3136 | struct mv88e6xxx_priv_state *ps; |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3137 | struct mii_bus *bus; |
Vivien Didelot | 0209d14 | 2016-04-17 13:23:55 -0400 | [diff] [blame] | 3138 | const char *name; |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3139 | int id, prod_num, rev; |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3140 | |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3141 | bus = dsa_host_dev_to_mii_bus(host_dev); |
Andrew Lunn | c156913 | 2016-04-13 02:40:45 +0200 | [diff] [blame] | 3142 | if (!bus) |
| 3143 | return NULL; |
| 3144 | |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3145 | id = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID); |
| 3146 | if (id < 0) |
| 3147 | return NULL; |
| 3148 | |
| 3149 | prod_num = (id & 0xfff0) >> 4; |
| 3150 | rev = id & 0x000f; |
| 3151 | |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3152 | info = mv88e6xxx_lookup_info(prod_num, table, num); |
| 3153 | if (!info) |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3154 | return NULL; |
| 3155 | |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3156 | name = info->name; |
| 3157 | |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3158 | ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL); |
| 3159 | if (!ps) |
| 3160 | return NULL; |
| 3161 | |
| 3162 | ps->bus = bus; |
| 3163 | ps->sw_addr = sw_addr; |
Vivien Didelot | f6271e6 | 2016-04-17 13:23:59 -0400 | [diff] [blame] | 3164 | ps->info = info; |
Vivien Didelot | a439c06 | 2016-04-17 13:23:58 -0400 | [diff] [blame] | 3165 | ps->id = id & 0xfff0; |
| 3166 | |
| 3167 | *priv = ps; |
| 3168 | |
| 3169 | dev_info(&ps->bus->dev, "switch 0x%x probed: %s, revision %u\n", |
| 3170 | prod_num, name, rev); |
| 3171 | |
Andrew Lunn | a77d43f | 2016-04-13 02:40:42 +0200 | [diff] [blame] | 3172 | return name; |
| 3173 | } |
| 3174 | |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3175 | static int __init mv88e6xxx_init(void) |
| 3176 | { |
| 3177 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 3178 | register_switch_driver(&mv88e6131_switch_driver); |
| 3179 | #endif |
Andrew Lunn | ca3dfa5 | 2016-03-12 00:01:36 +0100 | [diff] [blame] | 3180 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123) |
| 3181 | register_switch_driver(&mv88e6123_switch_driver); |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3182 | #endif |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 3183 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) |
| 3184 | register_switch_driver(&mv88e6352_switch_driver); |
| 3185 | #endif |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 3186 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 3187 | register_switch_driver(&mv88e6171_switch_driver); |
| 3188 | #endif |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3189 | return 0; |
| 3190 | } |
| 3191 | module_init(mv88e6xxx_init); |
| 3192 | |
| 3193 | static void __exit mv88e6xxx_cleanup(void) |
| 3194 | { |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 3195 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 3196 | unregister_switch_driver(&mv88e6171_switch_driver); |
| 3197 | #endif |
Vivien Didelot | 4212b54 | 2015-05-01 10:43:52 -0400 | [diff] [blame] | 3198 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) |
| 3199 | unregister_switch_driver(&mv88e6352_switch_driver); |
| 3200 | #endif |
Andrew Lunn | ca3dfa5 | 2016-03-12 00:01:36 +0100 | [diff] [blame] | 3201 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123) |
| 3202 | unregister_switch_driver(&mv88e6123_switch_driver); |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 3203 | #endif |
| 3204 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 3205 | unregister_switch_driver(&mv88e6131_switch_driver); |
| 3206 | #endif |
| 3207 | } |
| 3208 | module_exit(mv88e6xxx_cleanup); |
Ben Hutchings | 3d825ed | 2011-11-25 14:37:16 +0000 | [diff] [blame] | 3209 | |
| 3210 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
| 3211 | MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips"); |
| 3212 | MODULE_LICENSE("GPL"); |