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