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