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