Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0 OR MIT) |
| 2 | /* |
| 3 | * Microsemi Ocelot Switch driver |
| 4 | * |
| 5 | * Copyright (c) 2017 Microsemi Corporation |
| 6 | */ |
Vladimir Oltean | 40d3f29 | 2021-02-14 00:37:56 +0200 | [diff] [blame] | 7 | #include <linux/dsa/ocelot.h> |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 8 | #include <linux/if_bridge.h> |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 9 | #include <linux/ptp_classify.h> |
Vladimir Oltean | 2096805 | 2020-09-30 01:27:26 +0300 | [diff] [blame] | 10 | #include <soc/mscc/ocelot_vcap.h> |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 11 | #include "ocelot.h" |
Vladimir Oltean | 3c83654 | 2020-06-20 18:43:45 +0300 | [diff] [blame] | 12 | #include "ocelot_vcap.h" |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 13 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 14 | #define TABLE_UPDATE_SLEEP_US 10 |
| 15 | #define TABLE_UPDATE_TIMEOUT_US 100000 |
| 16 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 17 | struct ocelot_mact_entry { |
| 18 | u8 mac[ETH_ALEN]; |
| 19 | u16 vid; |
| 20 | enum macaccess_entry_type type; |
| 21 | }; |
| 22 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 23 | /* Caller must hold &ocelot->mact_lock */ |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 24 | static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) |
| 25 | { |
| 26 | return ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 27 | } |
| 28 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 29 | /* Caller must hold &ocelot->mact_lock */ |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 30 | static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) |
| 31 | { |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 32 | u32 val; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 33 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 34 | return readx_poll_timeout(ocelot_mact_read_macaccess, |
| 35 | ocelot, val, |
| 36 | (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == |
| 37 | MACACCESS_CMD_IDLE, |
| 38 | TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 41 | /* Caller must hold &ocelot->mact_lock */ |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 42 | static void ocelot_mact_select(struct ocelot *ocelot, |
| 43 | const unsigned char mac[ETH_ALEN], |
| 44 | unsigned int vid) |
| 45 | { |
| 46 | u32 macl = 0, mach = 0; |
| 47 | |
| 48 | /* Set the MAC address to handle and the vlan associated in a format |
| 49 | * understood by the hardware. |
| 50 | */ |
| 51 | mach |= vid << 16; |
| 52 | mach |= mac[0] << 8; |
| 53 | mach |= mac[1] << 0; |
| 54 | macl |= mac[2] << 24; |
| 55 | macl |= mac[3] << 16; |
| 56 | macl |= mac[4] << 8; |
| 57 | macl |= mac[5] << 0; |
| 58 | |
| 59 | ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); |
| 60 | ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); |
| 61 | |
| 62 | } |
| 63 | |
Xiaoliang Yang | 0568c3b | 2021-11-18 18:11:57 +0800 | [diff] [blame] | 64 | static int __ocelot_mact_learn(struct ocelot *ocelot, int port, |
| 65 | const unsigned char mac[ETH_ALEN], |
| 66 | unsigned int vid, enum macaccess_entry_type type) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 67 | { |
Alban Bedel | 584b7cf | 2021-01-19 15:06:38 +0100 | [diff] [blame] | 68 | u32 cmd = ANA_TABLES_MACACCESS_VALID | |
| 69 | ANA_TABLES_MACACCESS_DEST_IDX(port) | |
| 70 | ANA_TABLES_MACACCESS_ENTRYTYPE(type) | |
| 71 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN); |
| 72 | unsigned int mc_ports; |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 73 | int err; |
Alban Bedel | 584b7cf | 2021-01-19 15:06:38 +0100 | [diff] [blame] | 74 | |
| 75 | /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */ |
| 76 | if (type == ENTRYTYPE_MACv4) |
| 77 | mc_ports = (mac[1] << 8) | mac[2]; |
| 78 | else if (type == ENTRYTYPE_MACv6) |
| 79 | mc_ports = (mac[0] << 8) | mac[1]; |
| 80 | else |
| 81 | mc_ports = 0; |
| 82 | |
| 83 | if (mc_ports & BIT(ocelot->num_phys_ports)) |
| 84 | cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY; |
| 85 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 86 | ocelot_mact_select(ocelot, mac, vid); |
| 87 | |
| 88 | /* Issue a write command */ |
Alban Bedel | 584b7cf | 2021-01-19 15:06:38 +0100 | [diff] [blame] | 89 | ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 90 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 91 | err = ocelot_mact_wait_for_completion(ocelot); |
| 92 | |
Xiaoliang Yang | 0568c3b | 2021-11-18 18:11:57 +0800 | [diff] [blame] | 93 | return err; |
| 94 | } |
| 95 | |
| 96 | int ocelot_mact_learn(struct ocelot *ocelot, int port, |
| 97 | const unsigned char mac[ETH_ALEN], |
| 98 | unsigned int vid, enum macaccess_entry_type type) |
| 99 | { |
| 100 | int ret; |
| 101 | |
| 102 | mutex_lock(&ocelot->mact_lock); |
| 103 | ret = __ocelot_mact_learn(ocelot, port, mac, vid, type); |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 104 | mutex_unlock(&ocelot->mact_lock); |
| 105 | |
Xiaoliang Yang | 0568c3b | 2021-11-18 18:11:57 +0800 | [diff] [blame] | 106 | return ret; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 107 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 108 | EXPORT_SYMBOL(ocelot_mact_learn); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 109 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 110 | int ocelot_mact_forget(struct ocelot *ocelot, |
| 111 | const unsigned char mac[ETH_ALEN], unsigned int vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 112 | { |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 113 | int err; |
| 114 | |
| 115 | mutex_lock(&ocelot->mact_lock); |
| 116 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 117 | ocelot_mact_select(ocelot, mac, vid); |
| 118 | |
| 119 | /* Issue a forget command */ |
| 120 | ocelot_write(ocelot, |
| 121 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), |
| 122 | ANA_TABLES_MACACCESS); |
| 123 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 124 | err = ocelot_mact_wait_for_completion(ocelot); |
| 125 | |
| 126 | mutex_unlock(&ocelot->mact_lock); |
| 127 | |
| 128 | return err; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 129 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 130 | EXPORT_SYMBOL(ocelot_mact_forget); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 131 | |
Xiaoliang Yang | 0568c3b | 2021-11-18 18:11:57 +0800 | [diff] [blame] | 132 | int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx, |
| 133 | const unsigned char mac[ETH_ALEN], |
| 134 | unsigned int vid, enum macaccess_entry_type *type) |
| 135 | { |
| 136 | int val; |
| 137 | |
| 138 | mutex_lock(&ocelot->mact_lock); |
| 139 | |
| 140 | ocelot_mact_select(ocelot, mac, vid); |
| 141 | |
| 142 | /* Issue a read command with MACACCESS_VALID=1. */ |
| 143 | ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID | |
| 144 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), |
| 145 | ANA_TABLES_MACACCESS); |
| 146 | |
| 147 | if (ocelot_mact_wait_for_completion(ocelot)) { |
| 148 | mutex_unlock(&ocelot->mact_lock); |
| 149 | return -ETIMEDOUT; |
| 150 | } |
| 151 | |
| 152 | /* Read back the entry flags */ |
| 153 | val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 154 | |
| 155 | mutex_unlock(&ocelot->mact_lock); |
| 156 | |
| 157 | if (!(val & ANA_TABLES_MACACCESS_VALID)) |
| 158 | return -ENOENT; |
| 159 | |
| 160 | *dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val); |
| 161 | *type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | EXPORT_SYMBOL(ocelot_mact_lookup); |
| 166 | |
| 167 | int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx, |
| 168 | const unsigned char mac[ETH_ALEN], |
| 169 | unsigned int vid, |
| 170 | enum macaccess_entry_type type, |
| 171 | int sfid, int ssid) |
| 172 | { |
| 173 | int ret; |
| 174 | |
| 175 | mutex_lock(&ocelot->mact_lock); |
| 176 | |
| 177 | ocelot_write(ocelot, |
| 178 | (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) | |
| 179 | ANA_TABLES_STREAMDATA_SFID(sfid) | |
| 180 | (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) | |
| 181 | ANA_TABLES_STREAMDATA_SSID(ssid), |
| 182 | ANA_TABLES_STREAMDATA); |
| 183 | |
| 184 | ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type); |
| 185 | |
| 186 | mutex_unlock(&ocelot->mact_lock); |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | EXPORT_SYMBOL(ocelot_mact_learn_streamdata); |
| 191 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 192 | static void ocelot_mact_init(struct ocelot *ocelot) |
| 193 | { |
| 194 | /* Configure the learning mode entries attributes: |
| 195 | * - Do not copy the frame to the CPU extraction queues. |
| 196 | * - Use the vlan and mac_cpoy for dmac lookup. |
| 197 | */ |
| 198 | ocelot_rmw(ocelot, 0, |
| 199 | ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS |
| 200 | | ANA_AGENCTRL_LEARN_FWD_KILL |
| 201 | | ANA_AGENCTRL_LEARN_IGNORE_VLAN, |
| 202 | ANA_AGENCTRL); |
| 203 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 204 | /* Clear the MAC table. We are not concurrent with anyone, so |
| 205 | * holding &ocelot->mact_lock is pointless. |
| 206 | */ |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 207 | ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); |
| 208 | } |
| 209 | |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 210 | static void ocelot_vcap_enable(struct ocelot *ocelot, int port) |
Horatiu Vultur | b596229 | 2019-05-31 09:16:56 +0200 | [diff] [blame] | 211 | { |
| 212 | ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | |
| 213 | ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 214 | ANA_PORT_VCAP_S2_CFG, port); |
Xiaoliang Yang | 75944fd | 2020-10-02 15:02:23 +0300 | [diff] [blame] | 215 | |
| 216 | ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA, |
| 217 | ANA_PORT_VCAP_CFG, port); |
Xiaoliang Yang | 2f17c05 | 2020-10-02 15:02:24 +0300 | [diff] [blame] | 218 | |
| 219 | ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN, |
| 220 | REW_PORT_CFG_ES0_EN, |
| 221 | REW_PORT_CFG, port); |
Horatiu Vultur | b596229 | 2019-05-31 09:16:56 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 224 | static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) |
| 225 | { |
| 226 | return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); |
| 227 | } |
| 228 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 229 | static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) |
| 230 | { |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 231 | u32 val; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 232 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 233 | return readx_poll_timeout(ocelot_vlant_read_vlanaccess, |
| 234 | ocelot, |
| 235 | val, |
| 236 | (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == |
| 237 | ANA_TABLES_VLANACCESS_CMD_IDLE, |
| 238 | TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 241 | static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) |
| 242 | { |
| 243 | /* Select the VID to configure */ |
| 244 | ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), |
| 245 | ANA_TABLES_VLANTIDX); |
| 246 | /* Set the vlan port members mask and issue a write command */ |
| 247 | ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | |
| 248 | ANA_TABLES_VLANACCESS_CMD_WRITE, |
| 249 | ANA_TABLES_VLANACCESS); |
| 250 | |
| 251 | return ocelot_vlant_wait_for_completion(ocelot); |
| 252 | } |
| 253 | |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 254 | static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port) |
| 255 | { |
| 256 | struct ocelot_bridge_vlan *vlan; |
| 257 | int num_untagged = 0; |
| 258 | |
| 259 | list_for_each_entry(vlan, &ocelot->vlans, list) { |
| 260 | if (!(vlan->portmask & BIT(port))) |
| 261 | continue; |
| 262 | |
| 263 | if (vlan->untagged & BIT(port)) |
| 264 | num_untagged++; |
| 265 | } |
| 266 | |
| 267 | return num_untagged; |
| 268 | } |
| 269 | |
| 270 | static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port) |
| 271 | { |
| 272 | struct ocelot_bridge_vlan *vlan; |
| 273 | int num_tagged = 0; |
| 274 | |
| 275 | list_for_each_entry(vlan, &ocelot->vlans, list) { |
| 276 | if (!(vlan->portmask & BIT(port))) |
| 277 | continue; |
| 278 | |
| 279 | if (!(vlan->untagged & BIT(port))) |
| 280 | num_tagged++; |
| 281 | } |
| 282 | |
| 283 | return num_tagged; |
| 284 | } |
| 285 | |
| 286 | /* We use native VLAN when we have to mix egress-tagged VLANs with exactly |
| 287 | * _one_ egress-untagged VLAN (_the_ native VLAN) |
| 288 | */ |
| 289 | static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port) |
| 290 | { |
| 291 | return ocelot_port_num_tagged_vlans(ocelot, port) && |
| 292 | ocelot_port_num_untagged_vlans(ocelot, port) == 1; |
| 293 | } |
| 294 | |
| 295 | static struct ocelot_bridge_vlan * |
| 296 | ocelot_port_find_native_vlan(struct ocelot *ocelot, int port) |
| 297 | { |
| 298 | struct ocelot_bridge_vlan *vlan; |
| 299 | |
| 300 | list_for_each_entry(vlan, &ocelot->vlans, list) |
| 301 | if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port)) |
| 302 | return vlan; |
| 303 | |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable, |
| 308 | * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness |
| 309 | * state of the port. |
| 310 | */ |
| 311 | static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port) |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 312 | { |
| 313 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 314 | enum ocelot_port_tag_config tag_cfg; |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 315 | bool uses_native_vlan = false; |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 316 | |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 317 | if (ocelot_port->vlan_aware) { |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 318 | uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port); |
| 319 | |
| 320 | if (uses_native_vlan) |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 321 | tag_cfg = OCELOT_PORT_TAG_NATIVE; |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 322 | else if (ocelot_port_num_untagged_vlans(ocelot, port)) |
| 323 | tag_cfg = OCELOT_PORT_TAG_DISABLED; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 324 | else |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 325 | tag_cfg = OCELOT_PORT_TAG_TRUNK; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 326 | } else { |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 327 | tag_cfg = OCELOT_PORT_TAG_DISABLED; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 328 | } |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 329 | |
Vladimir Oltean | 62a22bc | 2021-10-20 20:58:48 +0300 | [diff] [blame] | 330 | ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg), |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 331 | REW_TAG_CFG_TAG_CFG_M, |
| 332 | REW_TAG_CFG, port); |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 333 | |
| 334 | if (uses_native_vlan) { |
| 335 | struct ocelot_bridge_vlan *native_vlan; |
| 336 | |
| 337 | /* Not having a native VLAN is impossible, because |
| 338 | * ocelot_port_num_untagged_vlans has returned 1. |
| 339 | * So there is no use in checking for NULL here. |
| 340 | */ |
| 341 | native_vlan = ocelot_port_find_native_vlan(ocelot, port); |
| 342 | |
| 343 | ocelot_rmw_gix(ocelot, |
| 344 | REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid), |
| 345 | REW_PORT_VLAN_CFG_PORT_VID_M, |
| 346 | REW_PORT_VLAN_CFG, port); |
| 347 | } |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 350 | /* Default vlan to clasify for untagged frames (may be zero) */ |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 351 | static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 352 | const struct ocelot_bridge_vlan *pvid_vlan) |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 353 | { |
| 354 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 355 | u16 pvid = OCELOT_VLAN_UNAWARE_PVID; |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 356 | u32 val = 0; |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 357 | |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 358 | ocelot_port->pvid_vlan = pvid_vlan; |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 359 | |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 360 | if (ocelot_port->vlan_aware && pvid_vlan) |
| 361 | pvid = pvid_vlan->vid; |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 362 | |
| 363 | ocelot_rmw_gix(ocelot, |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 364 | ANA_PORT_VLAN_CFG_VLAN_VID(pvid), |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 365 | ANA_PORT_VLAN_CFG_VLAN_VID_M, |
| 366 | ANA_PORT_VLAN_CFG, port); |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 367 | |
| 368 | /* If there's no pvid, we should drop not only untagged traffic (which |
| 369 | * happens automatically), but also 802.1p traffic which gets |
| 370 | * classified to VLAN 0, but that is always in our RX filter, so it |
| 371 | * would get accepted were it not for this setting. |
| 372 | */ |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 373 | if (!pvid_vlan && ocelot_port->vlan_aware) |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 374 | val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | |
| 375 | ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; |
| 376 | |
| 377 | ocelot_rmw_gix(ocelot, val, |
| 378 | ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | |
| 379 | ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, |
| 380 | ANA_PORT_DROP_CFG, port); |
Vladimir Oltean | 75e5a55 | 2020-10-31 12:29:10 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 383 | static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot, |
| 384 | u16 vid) |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 385 | { |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 386 | struct ocelot_bridge_vlan *vlan; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 387 | |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 388 | list_for_each_entry(vlan, &ocelot->vlans, list) |
| 389 | if (vlan->vid == vid) |
| 390 | return vlan; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 391 | |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 392 | return NULL; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 393 | } |
| 394 | |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 395 | static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid, |
| 396 | bool untagged) |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 397 | { |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 398 | struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid); |
| 399 | unsigned long portmask; |
| 400 | int err; |
| 401 | |
| 402 | if (vlan) { |
| 403 | portmask = vlan->portmask | BIT(port); |
| 404 | |
| 405 | err = ocelot_vlant_set_mask(ocelot, vid, portmask); |
| 406 | if (err) |
| 407 | return err; |
| 408 | |
| 409 | vlan->portmask = portmask; |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 410 | /* Bridge VLANs can be overwritten with a different |
| 411 | * egress-tagging setting, so make sure to override an untagged |
| 412 | * with a tagged VID if that's going on. |
| 413 | */ |
| 414 | if (untagged) |
| 415 | vlan->untagged |= BIT(port); |
| 416 | else |
| 417 | vlan->untagged &= ~BIT(port); |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | vlan = kzalloc(sizeof(*vlan), GFP_KERNEL); |
| 423 | if (!vlan) |
| 424 | return -ENOMEM; |
| 425 | |
| 426 | portmask = BIT(port); |
| 427 | |
| 428 | err = ocelot_vlant_set_mask(ocelot, vid, portmask); |
| 429 | if (err) { |
| 430 | kfree(vlan); |
| 431 | return err; |
| 432 | } |
| 433 | |
| 434 | vlan->vid = vid; |
| 435 | vlan->portmask = portmask; |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 436 | if (untagged) |
| 437 | vlan->untagged = BIT(port); |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 438 | INIT_LIST_HEAD(&vlan->list); |
| 439 | list_add_tail(&vlan->list, &ocelot->vlans); |
| 440 | |
| 441 | return 0; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid) |
| 445 | { |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 446 | struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid); |
| 447 | unsigned long portmask; |
| 448 | int err; |
| 449 | |
| 450 | if (!vlan) |
| 451 | return 0; |
| 452 | |
| 453 | portmask = vlan->portmask & ~BIT(port); |
| 454 | |
| 455 | err = ocelot_vlant_set_mask(ocelot, vid, portmask); |
| 456 | if (err) |
| 457 | return err; |
| 458 | |
| 459 | vlan->portmask = portmask; |
| 460 | if (vlan->portmask) |
| 461 | return 0; |
| 462 | |
| 463 | list_del(&vlan->list); |
| 464 | kfree(vlan); |
| 465 | |
| 466 | return 0; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 467 | } |
| 468 | |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 469 | int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, |
Vladimir Oltean | 3b95d1b | 2021-08-19 20:40:07 +0300 | [diff] [blame] | 470 | bool vlan_aware, struct netlink_ext_ack *extack) |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 471 | { |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 472 | struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1]; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 473 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 474 | struct ocelot_vcap_filter *filter; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 475 | u32 val; |
| 476 | |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 477 | list_for_each_entry(filter, &block->rules, list) { |
| 478 | if (filter->ingress_port_mask & BIT(port) && |
| 479 | filter->action.vid_replace_ena) { |
Vladimir Oltean | 3b95d1b | 2021-08-19 20:40:07 +0300 | [diff] [blame] | 480 | NL_SET_ERR_MSG_MOD(extack, |
| 481 | "Cannot change VLAN state with vlan modify rules active"); |
Vladimir Oltean | bae33f2 | 2021-01-09 02:01:50 +0200 | [diff] [blame] | 482 | return -EBUSY; |
Vladimir Oltean | 70edfae | 2020-10-08 14:56:58 +0300 | [diff] [blame] | 483 | } |
Vladimir Oltean | 70edfae | 2020-10-08 14:56:58 +0300 | [diff] [blame] | 484 | } |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 485 | |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 486 | ocelot_port->vlan_aware = vlan_aware; |
| 487 | |
| 488 | if (vlan_aware) |
| 489 | val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 490 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); |
| 491 | else |
| 492 | val = 0; |
| 493 | ocelot_rmw_gix(ocelot, val, |
| 494 | ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 495 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, |
| 496 | ANA_PORT_VLAN_CFG, port); |
| 497 | |
Vladimir Oltean | c3e58a75 | 2020-10-31 12:29:12 +0200 | [diff] [blame] | 498 | ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan); |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 499 | ocelot_port_manage_port_tag(ocelot, port); |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 500 | |
| 501 | return 0; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 502 | } |
| 503 | EXPORT_SYMBOL(ocelot_port_vlan_filtering); |
| 504 | |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 505 | int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid, |
Vladimir Oltean | 01af940 | 2021-08-19 20:40:06 +0300 | [diff] [blame] | 506 | bool untagged, struct netlink_ext_ack *extack) |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 507 | { |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 508 | if (untagged) { |
| 509 | /* We are adding an egress-tagged VLAN */ |
| 510 | if (ocelot_port_uses_native_vlan(ocelot, port)) { |
| 511 | NL_SET_ERR_MSG_MOD(extack, |
| 512 | "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN"); |
| 513 | return -EBUSY; |
| 514 | } |
| 515 | } else { |
| 516 | /* We are adding an egress-tagged VLAN */ |
| 517 | if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) { |
| 518 | NL_SET_ERR_MSG_MOD(extack, |
| 519 | "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs"); |
| 520 | return -EBUSY; |
| 521 | } |
Vladimir Oltean | 2f0402f | 2020-10-31 12:29:15 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | EXPORT_SYMBOL(ocelot_vlan_prepare); |
| 527 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 528 | int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, |
| 529 | bool untagged) |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 530 | { |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 531 | int err; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 532 | |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 533 | err = ocelot_vlan_member_add(ocelot, port, vid, untagged); |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 534 | if (err) |
| 535 | return err; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 536 | |
| 537 | /* Default ingress vlan classification */ |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 538 | if (pvid) |
| 539 | ocelot_port_set_pvid(ocelot, port, |
| 540 | ocelot_bridge_vlan_find(ocelot, vid)); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 541 | |
| 542 | /* Untagged egress vlan clasification */ |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 543 | ocelot_port_manage_port_tag(ocelot, port); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 544 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 545 | return 0; |
| 546 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 547 | EXPORT_SYMBOL(ocelot_vlan_add); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 548 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 549 | int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) |
Vladimir Oltean | 9855934 | 2019-11-09 15:02:48 +0200 | [diff] [blame] | 550 | { |
| 551 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 552 | int err; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 553 | |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 554 | err = ocelot_vlan_member_del(ocelot, port, vid); |
| 555 | if (err) |
| 556 | return err; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 557 | |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 558 | /* Ingress */ |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 559 | if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid) |
| 560 | ocelot_port_set_pvid(ocelot, port, NULL); |
Vladimir Oltean | be0576f | 2020-10-31 12:29:14 +0200 | [diff] [blame] | 561 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 562 | /* Egress */ |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 563 | ocelot_port_manage_port_tag(ocelot, port); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 564 | |
| 565 | return 0; |
| 566 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 567 | EXPORT_SYMBOL(ocelot_vlan_del); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 568 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 569 | static void ocelot_vlan_init(struct ocelot *ocelot) |
| 570 | { |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 571 | unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 572 | u16 port, vid; |
| 573 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 574 | /* Clear VLAN table, by default all ports are members of all VLANs */ |
| 575 | ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, |
| 576 | ANA_TABLES_VLANACCESS); |
| 577 | ocelot_vlant_wait_for_completion(ocelot); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 578 | |
| 579 | /* Configure the port VLAN memberships */ |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 580 | for (vid = 1; vid < VLAN_N_VID; vid++) |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 581 | ocelot_vlant_set_mask(ocelot, vid, 0); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 582 | |
| 583 | /* Because VLAN filtering is enabled, we need VID 0 to get untagged |
| 584 | * traffic. It is added automatically if 8021q module is loaded, but |
| 585 | * we can't rely on it since module may be not loaded. |
| 586 | */ |
Vladimir Oltean | bfbab31 | 2021-10-20 20:58:51 +0300 | [diff] [blame] | 587 | ocelot_vlant_set_mask(ocelot, OCELOT_VLAN_UNAWARE_PVID, all_ports); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 588 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 589 | /* Set vlan ingress filter mask to all ports but the CPU port by |
| 590 | * default. |
| 591 | */ |
Vladimir Oltean | bbf6a2d | 2021-08-19 20:40:08 +0300 | [diff] [blame] | 592 | ocelot_write(ocelot, all_ports, ANA_VLANMASK); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 593 | |
| 594 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 595 | ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); |
| 596 | ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); |
| 597 | } |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 600 | static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port) |
| 601 | { |
| 602 | return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port); |
| 603 | } |
| 604 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 605 | static int ocelot_port_flush(struct ocelot *ocelot, int port) |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 606 | { |
Vladimir Oltean | 1650bdb | 2021-06-08 14:15:35 +0300 | [diff] [blame] | 607 | unsigned int pause_ena; |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 608 | int err, val; |
| 609 | |
| 610 | /* Disable dequeuing from the egress queues */ |
| 611 | ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS, |
| 612 | QSYS_PORT_MODE_DEQUEUE_DIS, |
| 613 | QSYS_PORT_MODE, port); |
| 614 | |
| 615 | /* Disable flow control */ |
Vladimir Oltean | 1650bdb | 2021-06-08 14:15:35 +0300 | [diff] [blame] | 616 | ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena); |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 617 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); |
| 618 | |
| 619 | /* Disable priority flow control */ |
| 620 | ocelot_fields_write(ocelot, port, |
| 621 | QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0); |
| 622 | |
| 623 | /* Wait at least the time it takes to receive a frame of maximum length |
| 624 | * at the port. |
| 625 | * Worst-case delays for 10 kilobyte jumbo frames are: |
| 626 | * 8 ms on a 10M port |
| 627 | * 800 μs on a 100M port |
| 628 | * 80 μs on a 1G port |
| 629 | * 32 μs on a 2.5G port |
| 630 | */ |
| 631 | usleep_range(8000, 10000); |
| 632 | |
| 633 | /* Disable half duplex backpressure. */ |
| 634 | ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE, |
| 635 | SYS_FRONT_PORT_MODE, port); |
| 636 | |
| 637 | /* Flush the queues associated with the port. */ |
| 638 | ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA, |
| 639 | REW_PORT_CFG, port); |
| 640 | |
| 641 | /* Enable dequeuing from the egress queues. */ |
| 642 | ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE, |
| 643 | port); |
| 644 | |
| 645 | /* Wait until flushing is complete. */ |
| 646 | err = read_poll_timeout(ocelot_read_eq_avail, val, !val, |
| 647 | 100, 2000000, false, ocelot, port); |
| 648 | |
| 649 | /* Clear flushing again. */ |
| 650 | ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port); |
| 651 | |
Vladimir Oltean | 1650bdb | 2021-06-08 14:15:35 +0300 | [diff] [blame] | 652 | /* Re-enable flow control */ |
| 653 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena); |
| 654 | |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 655 | return err; |
| 656 | } |
Vladimir Oltean | eb4733d | 2021-02-08 19:36:27 +0200 | [diff] [blame] | 657 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 658 | void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port, |
| 659 | unsigned int link_an_mode, |
| 660 | phy_interface_t interface, |
| 661 | unsigned long quirks) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 662 | { |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 663 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 664 | int err; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 665 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 666 | ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA, |
| 667 | DEV_MAC_ENA_CFG); |
| 668 | |
| 669 | ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); |
| 670 | |
| 671 | err = ocelot_port_flush(ocelot, port); |
| 672 | if (err) |
| 673 | dev_err(ocelot->dev, "failed to flush port %d: %d\n", |
| 674 | port, err); |
| 675 | |
| 676 | /* Put the port in reset. */ |
| 677 | if (interface != PHY_INTERFACE_MODE_QSGMII || |
| 678 | !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP)) |
| 679 | ocelot_port_rmwl(ocelot_port, |
| 680 | DEV_CLOCK_CFG_MAC_TX_RST | |
Wan Jiabing | 74a3bc4 | 2021-10-11 10:27:41 +0800 | [diff] [blame] | 681 | DEV_CLOCK_CFG_MAC_RX_RST, |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 682 | DEV_CLOCK_CFG_MAC_TX_RST | |
Wan Jiabing | 74a3bc4 | 2021-10-11 10:27:41 +0800 | [diff] [blame] | 683 | DEV_CLOCK_CFG_MAC_RX_RST, |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 684 | DEV_CLOCK_CFG); |
| 685 | } |
| 686 | EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down); |
| 687 | |
| 688 | void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port, |
| 689 | struct phy_device *phydev, |
| 690 | unsigned int link_an_mode, |
| 691 | phy_interface_t interface, |
| 692 | int speed, int duplex, |
| 693 | bool tx_pause, bool rx_pause, |
| 694 | unsigned long quirks) |
| 695 | { |
| 696 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 697 | int mac_speed, mode = 0; |
| 698 | u32 mac_fc_cfg; |
| 699 | |
| 700 | /* The MAC might be integrated in systems where the MAC speed is fixed |
| 701 | * and it's the PCS who is performing the rate adaptation, so we have |
| 702 | * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG |
| 703 | * (which is also its default value). |
| 704 | */ |
| 705 | if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) || |
| 706 | speed == SPEED_1000) { |
| 707 | mac_speed = OCELOT_SPEED_1000; |
| 708 | mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; |
| 709 | } else if (speed == SPEED_2500) { |
| 710 | mac_speed = OCELOT_SPEED_2500; |
| 711 | mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; |
| 712 | } else if (speed == SPEED_100) { |
| 713 | mac_speed = OCELOT_SPEED_100; |
| 714 | } else { |
| 715 | mac_speed = OCELOT_SPEED_10; |
| 716 | } |
| 717 | |
| 718 | if (duplex == DUPLEX_FULL) |
| 719 | mode |= DEV_MAC_MODE_CFG_FDX_ENA; |
| 720 | |
| 721 | ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG); |
| 722 | |
| 723 | /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and |
| 724 | * PORT_RST bits in DEV_CLOCK_CFG. |
| 725 | */ |
| 726 | ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed), |
| 727 | DEV_CLOCK_CFG); |
| 728 | |
| 729 | switch (speed) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 730 | case SPEED_10: |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 731 | mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 732 | break; |
| 733 | case SPEED_100: |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 734 | mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 735 | break; |
| 736 | case SPEED_1000: |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 737 | case SPEED_2500: |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 738 | mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 739 | break; |
| 740 | default: |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 741 | dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n", |
| 742 | port, speed); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 743 | return; |
| 744 | } |
| 745 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 746 | /* Handle RX pause in all cases, with 2500base-X this is used for rate |
| 747 | * adaptation. |
| 748 | */ |
| 749 | mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 750 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 751 | if (tx_pause) |
| 752 | mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA | |
| 753 | SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | |
| 754 | SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | |
| 755 | SYS_MAC_FC_CFG_ZERO_PAUSE_ENA; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 756 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 757 | /* Flow control. Link speed is only used here to evaluate the time |
| 758 | * specification in incoming pause frames. |
| 759 | */ |
| 760 | ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 761 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 762 | ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); |
Vladimir Oltean | 1ba8f65 | 2020-02-29 16:31:11 +0200 | [diff] [blame] | 763 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 764 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause); |
Vladimir Oltean | 1ba8f65 | 2020-02-29 16:31:11 +0200 | [diff] [blame] | 765 | |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 766 | /* Undo the effects of ocelot_phylink_mac_link_down: |
| 767 | * enable MAC module |
| 768 | */ |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 769 | ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 770 | DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); |
| 771 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 772 | /* Core: Enable port for frame transfer */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 773 | ocelot_fields_write(ocelot, port, |
| 774 | QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 775 | } |
Vladimir Oltean | e6e12df | 2021-08-15 04:47:48 +0300 | [diff] [blame] | 776 | EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 777 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 778 | static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, |
| 779 | struct sk_buff *clone) |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 780 | { |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 781 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 782 | unsigned long flags; |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 783 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 784 | spin_lock_irqsave(&ocelot->ts_id_lock, flags); |
| 785 | |
| 786 | if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID || |
| 787 | ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) { |
| 788 | spin_unlock_irqrestore(&ocelot->ts_id_lock, flags); |
| 789 | return -EBUSY; |
| 790 | } |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 791 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 792 | skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; |
Yangbo Lu | c4b364c | 2021-04-27 12:22:00 +0800 | [diff] [blame] | 793 | /* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */ |
| 794 | OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id; |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 795 | |
Vladimir Oltean | c57fe00 | 2021-10-12 14:40:35 +0300 | [diff] [blame] | 796 | ocelot_port->ts_id++; |
| 797 | if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID) |
| 798 | ocelot_port->ts_id = 0; |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 799 | |
| 800 | ocelot_port->ptp_skbs_in_flight++; |
| 801 | ocelot->ptp_skbs_in_flight++; |
| 802 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 803 | skb_queue_tail(&ocelot_port->tx_skbs, clone); |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 804 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 805 | spin_unlock_irqrestore(&ocelot->ts_id_lock, flags); |
| 806 | |
| 807 | return 0; |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 808 | } |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 809 | |
Vladimir Oltean | fba0128 | 2021-10-12 14:40:38 +0300 | [diff] [blame] | 810 | static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb, |
| 811 | unsigned int ptp_class) |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 812 | { |
| 813 | struct ptp_header *hdr; |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 814 | u8 msgtype, twostep; |
| 815 | |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 816 | hdr = ptp_parse_header(skb, ptp_class); |
| 817 | if (!hdr) |
| 818 | return false; |
| 819 | |
| 820 | msgtype = ptp_get_msgtype(hdr, ptp_class); |
| 821 | twostep = hdr->flag_field[0] & 0x2; |
| 822 | |
| 823 | if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) |
| 824 | return true; |
| 825 | |
| 826 | return false; |
| 827 | } |
| 828 | |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 829 | int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port, |
| 830 | struct sk_buff *skb, |
| 831 | struct sk_buff **clone) |
| 832 | { |
| 833 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 834 | u8 ptp_cmd = ocelot_port->ptp_cmd; |
Vladimir Oltean | fba0128 | 2021-10-12 14:40:38 +0300 | [diff] [blame] | 835 | unsigned int ptp_class; |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 836 | int err; |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 837 | |
Vladimir Oltean | fba0128 | 2021-10-12 14:40:38 +0300 | [diff] [blame] | 838 | /* Don't do anything if PTP timestamping not enabled */ |
| 839 | if (!ptp_cmd) |
| 840 | return 0; |
| 841 | |
| 842 | ptp_class = ptp_classify_raw(skb); |
| 843 | if (ptp_class == PTP_CLASS_NONE) |
| 844 | return -EINVAL; |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 845 | |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 846 | /* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */ |
| 847 | if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) { |
Vladimir Oltean | fba0128 | 2021-10-12 14:40:38 +0300 | [diff] [blame] | 848 | if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) { |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 849 | OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | /* Fall back to two-step timestamping */ |
| 854 | ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; |
| 855 | } |
| 856 | |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 857 | if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) { |
| 858 | *clone = skb_clone_sk(skb); |
| 859 | if (!(*clone)) |
| 860 | return -ENOMEM; |
| 861 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 862 | err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone); |
| 863 | if (err) |
| 864 | return err; |
| 865 | |
Yangbo Lu | 39e5308 | 2021-04-27 12:22:03 +0800 | [diff] [blame] | 866 | OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd; |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 867 | OCELOT_SKB_CB(*clone)->ptp_class = ptp_class; |
Yangbo Lu | 682eaad | 2021-04-27 12:22:02 +0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | return 0; |
| 871 | } |
| 872 | EXPORT_SYMBOL(ocelot_port_txtstamp_request); |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 873 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 874 | static void ocelot_get_hwtimestamp(struct ocelot *ocelot, |
| 875 | struct timespec64 *ts) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 876 | { |
| 877 | unsigned long flags; |
| 878 | u32 val; |
| 879 | |
| 880 | spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); |
| 881 | |
| 882 | /* Read current PTP time to get seconds */ |
| 883 | val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); |
| 884 | |
| 885 | val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); |
| 886 | val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); |
| 887 | ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); |
| 888 | ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); |
| 889 | |
| 890 | /* Read packet HW timestamp from FIFO */ |
| 891 | val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); |
| 892 | ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); |
| 893 | |
| 894 | /* Sec has incremented since the ts was registered */ |
| 895 | if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) |
| 896 | ts->tv_sec--; |
| 897 | |
| 898 | spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); |
| 899 | } |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 900 | |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 901 | static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid) |
| 902 | { |
| 903 | struct ptp_header *hdr; |
| 904 | |
| 905 | hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class); |
| 906 | if (WARN_ON(!hdr)) |
| 907 | return false; |
| 908 | |
| 909 | return seqid == ntohs(hdr->sequence_id); |
| 910 | } |
| 911 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 912 | void ocelot_get_txtstamp(struct ocelot *ocelot) |
| 913 | { |
| 914 | int budget = OCELOT_PTP_QUEUE_SZ; |
| 915 | |
| 916 | while (budget--) { |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 917 | struct sk_buff *skb, *skb_tmp, *skb_match = NULL; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 918 | struct skb_shared_hwtstamps shhwtstamps; |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 919 | u32 val, id, seqid, txport; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 920 | struct ocelot_port *port; |
| 921 | struct timespec64 ts; |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 922 | unsigned long flags; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 923 | |
| 924 | val = ocelot_read(ocelot, SYS_PTP_STATUS); |
| 925 | |
| 926 | /* Check if a timestamp can be retrieved */ |
| 927 | if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) |
| 928 | break; |
| 929 | |
| 930 | WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); |
| 931 | |
| 932 | /* Retrieve the ts ID and Tx port */ |
| 933 | id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); |
| 934 | txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 935 | seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 936 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 937 | port = ocelot->ports[txport]; |
| 938 | |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 939 | spin_lock(&ocelot->ts_id_lock); |
| 940 | port->ptp_skbs_in_flight--; |
| 941 | ocelot->ptp_skbs_in_flight--; |
| 942 | spin_unlock(&ocelot->ts_id_lock); |
| 943 | |
| 944 | /* Retrieve its associated skb */ |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 945 | try_again: |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 946 | spin_lock_irqsave(&port->tx_skbs.lock, flags); |
| 947 | |
| 948 | skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { |
Yangbo Lu | c4b364c | 2021-04-27 12:22:00 +0800 | [diff] [blame] | 949 | if (OCELOT_SKB_CB(skb)->ts_id != id) |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 950 | continue; |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 951 | __skb_unlink(skb, &port->tx_skbs); |
| 952 | skb_match = skb; |
Yangbo Lu | fc62c09 | 2019-11-27 15:27:56 +0800 | [diff] [blame] | 953 | break; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 954 | } |
| 955 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 956 | spin_unlock_irqrestore(&port->tx_skbs.lock, flags); |
| 957 | |
Vladimir Oltean | 9fde506 | 2021-10-12 14:40:37 +0300 | [diff] [blame] | 958 | if (WARN_ON(!skb_match)) |
| 959 | continue; |
| 960 | |
Vladimir Oltean | ebb4c6a | 2021-10-12 14:40:39 +0300 | [diff] [blame] | 961 | if (!ocelot_validate_ptp_skb(skb_match, seqid)) { |
| 962 | dev_err_ratelimited(ocelot->dev, |
| 963 | "port %d received stale TX timestamp for seqid %d, discarding\n", |
| 964 | txport, seqid); |
| 965 | dev_kfree_skb_any(skb); |
| 966 | goto try_again; |
| 967 | } |
| 968 | |
laurent brando | 5fd8220 | 2020-07-27 18:26:14 +0800 | [diff] [blame] | 969 | /* Get the h/w timestamp */ |
| 970 | ocelot_get_hwtimestamp(ocelot, &ts); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 971 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 972 | /* Set the timestamp into the skb */ |
| 973 | memset(&shhwtstamps, 0, sizeof(shhwtstamps)); |
| 974 | shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 975 | skb_complete_tx_timestamp(skb_match, &shhwtstamps); |
laurent brando | 5fd8220 | 2020-07-27 18:26:14 +0800 | [diff] [blame] | 976 | |
| 977 | /* Next ts */ |
| 978 | ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | EXPORT_SYMBOL(ocelot_get_txtstamp); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 982 | |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 983 | static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh, |
| 984 | u32 *rval) |
| 985 | { |
| 986 | u32 bytes_valid, val; |
| 987 | |
| 988 | val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 989 | if (val == XTR_NOT_READY) { |
| 990 | if (ifh) |
| 991 | return -EIO; |
| 992 | |
| 993 | do { |
| 994 | val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 995 | } while (val == XTR_NOT_READY); |
| 996 | } |
| 997 | |
| 998 | switch (val) { |
| 999 | case XTR_ABORT: |
| 1000 | return -EIO; |
| 1001 | case XTR_EOF_0: |
| 1002 | case XTR_EOF_1: |
| 1003 | case XTR_EOF_2: |
| 1004 | case XTR_EOF_3: |
| 1005 | case XTR_PRUNED: |
| 1006 | bytes_valid = XTR_VALID_BYTES(val); |
| 1007 | val = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1008 | if (val == XTR_ESCAPE) |
| 1009 | *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1010 | else |
| 1011 | *rval = val; |
| 1012 | |
| 1013 | return bytes_valid; |
| 1014 | case XTR_ESCAPE: |
| 1015 | *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1016 | |
| 1017 | return 4; |
| 1018 | default: |
| 1019 | *rval = val; |
| 1020 | |
| 1021 | return 4; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh) |
| 1026 | { |
| 1027 | int i, err = 0; |
| 1028 | |
| 1029 | for (i = 0; i < OCELOT_TAG_LEN / 4; i++) { |
| 1030 | err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]); |
| 1031 | if (err != 4) |
| 1032 | return (err < 0) ? err : -EIO; |
| 1033 | } |
| 1034 | |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb) |
| 1039 | { |
| 1040 | struct skb_shared_hwtstamps *shhwtstamps; |
Horatiu Vultur | 2ed2c5f | 2021-03-16 21:10:19 +0100 | [diff] [blame] | 1041 | u64 tod_in_ns, full_ts_in_ns; |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1042 | u64 timestamp, src_port, len; |
| 1043 | u32 xfh[OCELOT_TAG_LEN / 4]; |
| 1044 | struct net_device *dev; |
| 1045 | struct timespec64 ts; |
| 1046 | struct sk_buff *skb; |
| 1047 | int sz, buf_len; |
| 1048 | u32 val, *buf; |
| 1049 | int err; |
| 1050 | |
| 1051 | err = ocelot_xtr_poll_xfh(ocelot, grp, xfh); |
| 1052 | if (err) |
| 1053 | return err; |
| 1054 | |
| 1055 | ocelot_xfh_get_src_port(xfh, &src_port); |
| 1056 | ocelot_xfh_get_len(xfh, &len); |
| 1057 | ocelot_xfh_get_rew_val(xfh, ×tamp); |
| 1058 | |
| 1059 | if (WARN_ON(src_port >= ocelot->num_phys_ports)) |
| 1060 | return -EINVAL; |
| 1061 | |
| 1062 | dev = ocelot->ops->port_to_netdev(ocelot, src_port); |
| 1063 | if (!dev) |
| 1064 | return -EINVAL; |
| 1065 | |
| 1066 | skb = netdev_alloc_skb(dev, len); |
| 1067 | if (unlikely(!skb)) { |
| 1068 | netdev_err(dev, "Unable to allocate sk_buff\n"); |
| 1069 | return -ENOMEM; |
| 1070 | } |
| 1071 | |
| 1072 | buf_len = len - ETH_FCS_LEN; |
| 1073 | buf = (u32 *)skb_put(skb, buf_len); |
| 1074 | |
| 1075 | len = 0; |
| 1076 | do { |
| 1077 | sz = ocelot_rx_frame_word(ocelot, grp, false, &val); |
| 1078 | if (sz < 0) { |
| 1079 | err = sz; |
| 1080 | goto out_free_skb; |
| 1081 | } |
| 1082 | *buf++ = val; |
| 1083 | len += sz; |
| 1084 | } while (len < buf_len); |
| 1085 | |
| 1086 | /* Read the FCS */ |
| 1087 | sz = ocelot_rx_frame_word(ocelot, grp, false, &val); |
| 1088 | if (sz < 0) { |
| 1089 | err = sz; |
| 1090 | goto out_free_skb; |
| 1091 | } |
| 1092 | |
| 1093 | /* Update the statistics if part of the FCS was read before */ |
| 1094 | len -= ETH_FCS_LEN - sz; |
| 1095 | |
| 1096 | if (unlikely(dev->features & NETIF_F_RXFCS)) { |
| 1097 | buf = (u32 *)skb_put(skb, ETH_FCS_LEN); |
| 1098 | *buf = val; |
| 1099 | } |
| 1100 | |
| 1101 | if (ocelot->ptp) { |
| 1102 | ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); |
| 1103 | |
| 1104 | tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec); |
| 1105 | if ((tod_in_ns & 0xffffffff) < timestamp) |
| 1106 | full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) | |
| 1107 | timestamp; |
| 1108 | else |
| 1109 | full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) | |
| 1110 | timestamp; |
| 1111 | |
| 1112 | shhwtstamps = skb_hwtstamps(skb); |
| 1113 | memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); |
| 1114 | shhwtstamps->hwtstamp = full_ts_in_ns; |
| 1115 | } |
| 1116 | |
| 1117 | /* Everything we see on an interface that is in the HW bridge |
| 1118 | * has already been forwarded. |
| 1119 | */ |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1120 | if (ocelot->ports[src_port]->bridge) |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1121 | skb->offload_fwd_mark = 1; |
| 1122 | |
| 1123 | skb->protocol = eth_type_trans(skb, dev); |
Horatiu Vultur | d8ea7ff | 2021-02-16 22:42:03 +0100 | [diff] [blame] | 1124 | |
Vladimir Oltean | 924ee31 | 2021-02-14 00:37:59 +0200 | [diff] [blame] | 1125 | *nskb = skb; |
| 1126 | |
| 1127 | return 0; |
| 1128 | |
| 1129 | out_free_skb: |
| 1130 | kfree_skb(skb); |
| 1131 | return err; |
| 1132 | } |
| 1133 | EXPORT_SYMBOL(ocelot_xtr_poll_frame); |
| 1134 | |
Vladimir Oltean | 137ffbc | 2021-02-14 00:37:54 +0200 | [diff] [blame] | 1135 | bool ocelot_can_inject(struct ocelot *ocelot, int grp) |
| 1136 | { |
| 1137 | u32 val = ocelot_read(ocelot, QS_INJ_STATUS); |
| 1138 | |
| 1139 | if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp)))) |
| 1140 | return false; |
| 1141 | if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))) |
| 1142 | return false; |
| 1143 | |
| 1144 | return true; |
| 1145 | } |
| 1146 | EXPORT_SYMBOL(ocelot_can_inject); |
| 1147 | |
| 1148 | void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp, |
| 1149 | u32 rew_op, struct sk_buff *skb) |
| 1150 | { |
Vladimir Oltean | 40d3f29 | 2021-02-14 00:37:56 +0200 | [diff] [blame] | 1151 | u32 ifh[OCELOT_TAG_LEN / 4] = {0}; |
Vladimir Oltean | 137ffbc | 2021-02-14 00:37:54 +0200 | [diff] [blame] | 1152 | unsigned int i, count, last; |
| 1153 | |
| 1154 | ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | |
| 1155 | QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp); |
| 1156 | |
Vladimir Oltean | 40d3f29 | 2021-02-14 00:37:56 +0200 | [diff] [blame] | 1157 | ocelot_ifh_set_bypass(ifh, 1); |
Vladimir Oltean | 1f778d5 | 2021-02-15 15:31:43 +0200 | [diff] [blame] | 1158 | ocelot_ifh_set_dest(ifh, BIT_ULL(port)); |
Vladimir Oltean | 40d3f29 | 2021-02-14 00:37:56 +0200 | [diff] [blame] | 1159 | ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C); |
Vladimir Oltean | e8c0722 | 2021-10-01 18:15:27 +0300 | [diff] [blame] | 1160 | ocelot_ifh_set_vlan_tci(ifh, skb_vlan_tag_get(skb)); |
Vladimir Oltean | 40d3f29 | 2021-02-14 00:37:56 +0200 | [diff] [blame] | 1161 | ocelot_ifh_set_rew_op(ifh, rew_op); |
Vladimir Oltean | 137ffbc | 2021-02-14 00:37:54 +0200 | [diff] [blame] | 1162 | |
| 1163 | for (i = 0; i < OCELOT_TAG_LEN / 4; i++) |
Vladimir Oltean | 40d3f29 | 2021-02-14 00:37:56 +0200 | [diff] [blame] | 1164 | ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp); |
Vladimir Oltean | 137ffbc | 2021-02-14 00:37:54 +0200 | [diff] [blame] | 1165 | |
| 1166 | count = DIV_ROUND_UP(skb->len, 4); |
| 1167 | last = skb->len % 4; |
| 1168 | for (i = 0; i < count; i++) |
| 1169 | ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp); |
| 1170 | |
| 1171 | /* Add padding */ |
| 1172 | while (i < (OCELOT_BUFFER_CELL_SZ / 4)) { |
| 1173 | ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); |
| 1174 | i++; |
| 1175 | } |
| 1176 | |
| 1177 | /* Indicate EOF and valid bytes in last word */ |
| 1178 | ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) | |
| 1179 | QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) | |
| 1180 | QS_INJ_CTRL_EOF, |
| 1181 | QS_INJ_CTRL, grp); |
| 1182 | |
| 1183 | /* Add dummy CRC */ |
| 1184 | ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp); |
| 1185 | skb_tx_timestamp(skb); |
| 1186 | |
| 1187 | skb->dev->stats.tx_packets++; |
| 1188 | skb->dev->stats.tx_bytes += skb->len; |
| 1189 | } |
| 1190 | EXPORT_SYMBOL(ocelot_port_inject_frame); |
| 1191 | |
Vladimir Oltean | 0a6f17c | 2021-02-14 00:38:01 +0200 | [diff] [blame] | 1192 | void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp) |
| 1193 | { |
| 1194 | while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) |
| 1195 | ocelot_read_rix(ocelot, QS_XTR_RD, grp); |
| 1196 | } |
| 1197 | EXPORT_SYMBOL(ocelot_drain_cpu_queue); |
| 1198 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1199 | int ocelot_fdb_add(struct ocelot *ocelot, int port, |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 1200 | const unsigned char *addr, u16 vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1201 | { |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 1202 | int pgid = port; |
| 1203 | |
| 1204 | if (port == ocelot->npi) |
| 1205 | pgid = PGID_CPU; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1206 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 1207 | return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1208 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1209 | EXPORT_SYMBOL(ocelot_fdb_add); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1210 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1211 | int ocelot_fdb_del(struct ocelot *ocelot, int port, |
| 1212 | const unsigned char *addr, u16 vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1213 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1214 | return ocelot_mact_forget(ocelot, addr, vid); |
| 1215 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1216 | EXPORT_SYMBOL(ocelot_fdb_del); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1217 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1218 | int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, |
| 1219 | bool is_static, void *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1220 | { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1221 | struct ocelot_dump_ctx *dump = data; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1222 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 1223 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 1224 | struct nlmsghdr *nlh; |
| 1225 | struct ndmsg *ndm; |
| 1226 | |
| 1227 | if (dump->idx < dump->cb->args[2]) |
| 1228 | goto skip; |
| 1229 | |
| 1230 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 1231 | sizeof(*ndm), NLM_F_MULTI); |
| 1232 | if (!nlh) |
| 1233 | return -EMSGSIZE; |
| 1234 | |
| 1235 | ndm = nlmsg_data(nlh); |
| 1236 | ndm->ndm_family = AF_BRIDGE; |
| 1237 | ndm->ndm_pad1 = 0; |
| 1238 | ndm->ndm_pad2 = 0; |
| 1239 | ndm->ndm_flags = NTF_SELF; |
| 1240 | ndm->ndm_type = 0; |
| 1241 | ndm->ndm_ifindex = dump->dev->ifindex; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1242 | ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1243 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1244 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1245 | goto nla_put_failure; |
| 1246 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1247 | if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1248 | goto nla_put_failure; |
| 1249 | |
| 1250 | nlmsg_end(dump->skb, nlh); |
| 1251 | |
| 1252 | skip: |
| 1253 | dump->idx++; |
| 1254 | return 0; |
| 1255 | |
| 1256 | nla_put_failure: |
| 1257 | nlmsg_cancel(dump->skb, nlh); |
| 1258 | return -EMSGSIZE; |
| 1259 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1260 | EXPORT_SYMBOL(ocelot_port_fdb_do_dump); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1261 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1262 | /* Caller must hold &ocelot->mact_lock */ |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1263 | static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, |
| 1264 | struct ocelot_mact_entry *entry) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1265 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1266 | u32 val, dst, macl, mach; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1267 | char mac[ETH_ALEN]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1268 | |
| 1269 | /* Set row and column to read from */ |
| 1270 | ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); |
| 1271 | ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); |
| 1272 | |
| 1273 | /* Issue a read command */ |
| 1274 | ocelot_write(ocelot, |
| 1275 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), |
| 1276 | ANA_TABLES_MACACCESS); |
| 1277 | |
| 1278 | if (ocelot_mact_wait_for_completion(ocelot)) |
| 1279 | return -ETIMEDOUT; |
| 1280 | |
| 1281 | /* Read the entry flags */ |
| 1282 | val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 1283 | if (!(val & ANA_TABLES_MACACCESS_VALID)) |
| 1284 | return -EINVAL; |
| 1285 | |
| 1286 | /* If the entry read has another port configured as its destination, |
| 1287 | * do not report it. |
| 1288 | */ |
| 1289 | dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1290 | if (dst != port) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1291 | return -EINVAL; |
| 1292 | |
| 1293 | /* Get the entry's MAC address and VLAN id */ |
| 1294 | macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); |
| 1295 | mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); |
| 1296 | |
| 1297 | mac[0] = (mach >> 8) & 0xff; |
| 1298 | mac[1] = (mach >> 0) & 0xff; |
| 1299 | mac[2] = (macl >> 24) & 0xff; |
| 1300 | mac[3] = (macl >> 16) & 0xff; |
| 1301 | mac[4] = (macl >> 8) & 0xff; |
| 1302 | mac[5] = (macl >> 0) & 0xff; |
| 1303 | |
| 1304 | entry->vid = (mach >> 16) & 0xfff; |
| 1305 | ether_addr_copy(entry->mac, mac); |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1310 | int ocelot_fdb_dump(struct ocelot *ocelot, int port, |
| 1311 | dsa_fdb_dump_cb_t *cb, void *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1312 | { |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1313 | int err = 0; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1314 | int i, j; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1315 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1316 | /* We could take the lock just around ocelot_mact_read, but doing so |
| 1317 | * thousands of times in a row seems rather pointless and inefficient. |
| 1318 | */ |
| 1319 | mutex_lock(&ocelot->mact_lock); |
| 1320 | |
Vladimir Oltean | 21ce7f3 | 2020-05-04 01:20:26 +0300 | [diff] [blame] | 1321 | /* Loop through all the mac tables entries. */ |
| 1322 | for (i = 0; i < ocelot->num_mact_rows; i++) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1323 | for (j = 0; j < 4; j++) { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1324 | struct ocelot_mact_entry entry; |
| 1325 | bool is_static; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1326 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1327 | err = ocelot_mact_read(ocelot, port, i, j, &entry); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1328 | /* If the entry is invalid (wrong port, invalid...), |
| 1329 | * skip it. |
| 1330 | */ |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1331 | if (err == -EINVAL) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1332 | continue; |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1333 | else if (err) |
| 1334 | break; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1335 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1336 | is_static = (entry.type == ENTRYTYPE_LOCKED); |
| 1337 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1338 | err = cb(entry.mac, entry.vid, is_static, data); |
| 1339 | if (err) |
| 1340 | break; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 1344 | mutex_unlock(&ocelot->mact_lock); |
| 1345 | |
| 1346 | return err; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1347 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1348 | EXPORT_SYMBOL(ocelot_fdb_dump); |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 1349 | |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 1350 | int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1351 | { |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1352 | return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, |
| 1353 | sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; |
| 1354 | } |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 1355 | EXPORT_SYMBOL(ocelot_hwstamp_get); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1356 | |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 1357 | int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1358 | { |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 1359 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1360 | struct hwtstamp_config cfg; |
| 1361 | |
| 1362 | if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) |
| 1363 | return -EFAULT; |
| 1364 | |
| 1365 | /* reserved for future extensions */ |
| 1366 | if (cfg.flags) |
| 1367 | return -EINVAL; |
| 1368 | |
| 1369 | /* Tx type sanity check */ |
| 1370 | switch (cfg.tx_type) { |
| 1371 | case HWTSTAMP_TX_ON: |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 1372 | ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1373 | break; |
| 1374 | case HWTSTAMP_TX_ONESTEP_SYNC: |
| 1375 | /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we |
| 1376 | * need to update the origin time. |
| 1377 | */ |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 1378 | ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1379 | break; |
| 1380 | case HWTSTAMP_TX_OFF: |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 1381 | ocelot_port->ptp_cmd = 0; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1382 | break; |
| 1383 | default: |
| 1384 | return -ERANGE; |
| 1385 | } |
| 1386 | |
| 1387 | mutex_lock(&ocelot->ptp_lock); |
| 1388 | |
| 1389 | switch (cfg.rx_filter) { |
| 1390 | case HWTSTAMP_FILTER_NONE: |
| 1391 | break; |
| 1392 | case HWTSTAMP_FILTER_ALL: |
| 1393 | case HWTSTAMP_FILTER_SOME: |
| 1394 | case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: |
| 1395 | case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: |
| 1396 | case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: |
| 1397 | case HWTSTAMP_FILTER_NTP_ALL: |
| 1398 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: |
| 1399 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: |
| 1400 | case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: |
| 1401 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: |
| 1402 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: |
| 1403 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: |
| 1404 | case HWTSTAMP_FILTER_PTP_V2_EVENT: |
| 1405 | case HWTSTAMP_FILTER_PTP_V2_SYNC: |
| 1406 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: |
| 1407 | cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; |
| 1408 | break; |
| 1409 | default: |
| 1410 | mutex_unlock(&ocelot->ptp_lock); |
| 1411 | return -ERANGE; |
| 1412 | } |
| 1413 | |
| 1414 | /* Commit back the result & save it */ |
| 1415 | memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); |
| 1416 | mutex_unlock(&ocelot->ptp_lock); |
| 1417 | |
| 1418 | return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; |
| 1419 | } |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 1420 | EXPORT_SYMBOL(ocelot_hwstamp_set); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1421 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1422 | void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1423 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1424 | int i; |
| 1425 | |
| 1426 | if (sset != ETH_SS_STATS) |
| 1427 | return; |
| 1428 | |
| 1429 | for (i = 0; i < ocelot->num_stats; i++) |
| 1430 | memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, |
| 1431 | ETH_GSTRING_LEN); |
| 1432 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1433 | EXPORT_SYMBOL(ocelot_get_strings); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1434 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 1435 | static void ocelot_update_stats(struct ocelot *ocelot) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1436 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1437 | int i, j; |
| 1438 | |
| 1439 | mutex_lock(&ocelot->stats_lock); |
| 1440 | |
| 1441 | for (i = 0; i < ocelot->num_phys_ports; i++) { |
| 1442 | /* Configure the port to read the stats from */ |
| 1443 | ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); |
| 1444 | |
| 1445 | for (j = 0; j < ocelot->num_stats; j++) { |
| 1446 | u32 val; |
| 1447 | unsigned int idx = i * ocelot->num_stats + j; |
| 1448 | |
| 1449 | val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, |
| 1450 | ocelot->stats_layout[j].offset); |
| 1451 | |
| 1452 | if (val < (ocelot->stats[idx] & U32_MAX)) |
| 1453 | ocelot->stats[idx] += (u64)1 << 32; |
| 1454 | |
| 1455 | ocelot->stats[idx] = (ocelot->stats[idx] & |
| 1456 | ~(u64)U32_MAX) + val; |
| 1457 | } |
| 1458 | } |
| 1459 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 1460 | mutex_unlock(&ocelot->stats_lock); |
| 1461 | } |
| 1462 | |
| 1463 | static void ocelot_check_stats_work(struct work_struct *work) |
| 1464 | { |
| 1465 | struct delayed_work *del_work = to_delayed_work(work); |
| 1466 | struct ocelot *ocelot = container_of(del_work, struct ocelot, |
| 1467 | stats_work); |
| 1468 | |
| 1469 | ocelot_update_stats(ocelot); |
| 1470 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1471 | queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, |
| 1472 | OCELOT_STATS_CHECK_DELAY); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1473 | } |
| 1474 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1475 | void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1476 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1477 | int i; |
| 1478 | |
| 1479 | /* check and update now */ |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 1480 | ocelot_update_stats(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1481 | |
| 1482 | /* Copy all counters */ |
| 1483 | for (i = 0; i < ocelot->num_stats; i++) |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1484 | *data++ = ocelot->stats[port * ocelot->num_stats + i]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1485 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1486 | EXPORT_SYMBOL(ocelot_get_ethtool_stats); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1487 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1488 | int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 1489 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1490 | if (sset != ETH_SS_STATS) |
| 1491 | return -EOPNOTSUPP; |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 1492 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1493 | return ocelot->num_stats; |
| 1494 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1495 | EXPORT_SYMBOL(ocelot_get_sset_count); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1496 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1497 | int ocelot_get_ts_info(struct ocelot *ocelot, int port, |
| 1498 | struct ethtool_ts_info *info) |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 1499 | { |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1500 | info->phc_index = ocelot->ptp_clock ? |
| 1501 | ptp_clock_index(ocelot->ptp_clock) : -1; |
Yangbo Lu | d2b09a8 | 2020-04-20 10:46:46 +0800 | [diff] [blame] | 1502 | if (info->phc_index == -1) { |
| 1503 | info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | |
| 1504 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 1505 | SOF_TIMESTAMPING_SOFTWARE; |
| 1506 | return 0; |
| 1507 | } |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1508 | info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | |
| 1509 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 1510 | SOF_TIMESTAMPING_SOFTWARE | |
| 1511 | SOF_TIMESTAMPING_TX_HARDWARE | |
| 1512 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 1513 | SOF_TIMESTAMPING_RAW_HARDWARE; |
| 1514 | info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | |
| 1515 | BIT(HWTSTAMP_TX_ONESTEP_SYNC); |
| 1516 | info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL); |
| 1517 | |
| 1518 | return 0; |
| 1519 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1520 | EXPORT_SYMBOL(ocelot_get_ts_info); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1521 | |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1522 | static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond, |
| 1523 | bool only_active_ports) |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 1524 | { |
| 1525 | u32 mask = 0; |
| 1526 | int port; |
| 1527 | |
| 1528 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1529 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1530 | |
| 1531 | if (!ocelot_port) |
| 1532 | continue; |
| 1533 | |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1534 | if (ocelot_port->bond == bond) { |
| 1535 | if (only_active_ports && !ocelot_port->lag_tx_active) |
| 1536 | continue; |
| 1537 | |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 1538 | mask |= BIT(port); |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1539 | } |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | return mask; |
| 1543 | } |
| 1544 | |
Vladimir Oltean | acc64f5 | 2021-09-22 19:03:38 -0700 | [diff] [blame] | 1545 | static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port, |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1546 | struct net_device *bridge) |
| 1547 | { |
Vladimir Oltean | acc64f5 | 2021-09-22 19:03:38 -0700 | [diff] [blame] | 1548 | struct ocelot_port *ocelot_port = ocelot->ports[src_port]; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1549 | u32 mask = 0; |
| 1550 | int port; |
| 1551 | |
Vladimir Oltean | acc64f5 | 2021-09-22 19:03:38 -0700 | [diff] [blame] | 1552 | if (!ocelot_port || ocelot_port->bridge != bridge || |
| 1553 | ocelot_port->stp_state != BR_STATE_FORWARDING) |
| 1554 | return 0; |
| 1555 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1556 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
Vladimir Oltean | acc64f5 | 2021-09-22 19:03:38 -0700 | [diff] [blame] | 1557 | ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1558 | |
| 1559 | if (!ocelot_port) |
| 1560 | continue; |
| 1561 | |
| 1562 | if (ocelot_port->stp_state == BR_STATE_FORWARDING && |
| 1563 | ocelot_port->bridge == bridge) |
| 1564 | mask |= BIT(port); |
| 1565 | } |
| 1566 | |
| 1567 | return mask; |
| 1568 | } |
| 1569 | |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1570 | static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot) |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1571 | { |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1572 | u32 mask = 0; |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1573 | int port; |
| 1574 | |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1575 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1576 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1577 | |
| 1578 | if (!ocelot_port) |
| 1579 | continue; |
| 1580 | |
| 1581 | if (ocelot_port->is_dsa_8021q_cpu) |
| 1582 | mask |= BIT(port); |
| 1583 | } |
| 1584 | |
| 1585 | return mask; |
| 1586 | } |
| 1587 | |
| 1588 | void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot) |
| 1589 | { |
| 1590 | unsigned long cpu_fwd_mask; |
| 1591 | int port; |
| 1592 | |
| 1593 | /* If a DSA tag_8021q CPU exists, it needs to be included in the |
| 1594 | * regular forwarding path of the front ports regardless of whether |
| 1595 | * those are bridged or standalone. |
| 1596 | * If DSA tag_8021q is not used, this returns 0, which is fine because |
| 1597 | * the hardware-based CPU port module can be a destination for packets |
| 1598 | * even if it isn't part of PGID_SRC. |
| 1599 | */ |
| 1600 | cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot); |
| 1601 | |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1602 | /* Apply FWD mask. The loop is needed to add/remove the current port as |
| 1603 | * a source for the other ports. |
| 1604 | */ |
| 1605 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1606 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1607 | unsigned long mask; |
| 1608 | |
| 1609 | if (!ocelot_port) { |
| 1610 | /* Unused ports can't send anywhere */ |
| 1611 | mask = 0; |
| 1612 | } else if (ocelot_port->is_dsa_8021q_cpu) { |
| 1613 | /* The DSA tag_8021q CPU ports need to be able to |
| 1614 | * forward packets to all other ports except for |
| 1615 | * themselves |
| 1616 | */ |
| 1617 | mask = GENMASK(ocelot->num_phys_ports - 1, 0); |
| 1618 | mask &= ~cpu_fwd_mask; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1619 | } else if (ocelot_port->bridge) { |
| 1620 | struct net_device *bridge = ocelot_port->bridge; |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1621 | struct net_device *bond = ocelot_port->bond; |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1622 | |
Vladimir Oltean | acc64f5 | 2021-09-22 19:03:38 -0700 | [diff] [blame] | 1623 | mask = ocelot_get_bridge_fwd_mask(ocelot, port, bridge); |
Vladimir Oltean | c193014 | 2021-08-17 19:04:25 +0300 | [diff] [blame] | 1624 | mask |= cpu_fwd_mask; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1625 | mask &= ~BIT(port); |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1626 | if (bond) { |
| 1627 | mask &= ~ocelot_get_bond_mask(ocelot, bond, |
| 1628 | false); |
| 1629 | } |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1630 | } else { |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1631 | /* Standalone ports forward only to DSA tag_8021q CPU |
| 1632 | * ports (if those exist), or to the hardware CPU port |
| 1633 | * module otherwise. |
| 1634 | */ |
| 1635 | mask = cpu_fwd_mask; |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1636 | } |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1637 | |
| 1638 | ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1639 | } |
| 1640 | } |
Vladimir Oltean | e21268e | 2021-01-29 03:00:09 +0200 | [diff] [blame] | 1641 | EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1642 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1643 | void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1644 | { |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame] | 1645 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1646 | u32 learn_ena = 0; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1647 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1648 | ocelot_port->stp_state = state; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1649 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1650 | if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) && |
| 1651 | ocelot_port->learn_ena) |
| 1652 | learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1653 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1654 | ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA, |
| 1655 | ANA_PORT_PORT_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1656 | |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 1657 | ocelot_apply_bridge_fwd_mask(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1658 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1659 | EXPORT_SYMBOL(ocelot_bridge_stp_state_set); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1660 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1661 | void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 1662 | { |
Vladimir Oltean | c0d7ecc | 2020-05-04 01:20:27 +0300 | [diff] [blame] | 1663 | unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); |
| 1664 | |
| 1665 | /* Setting AGE_PERIOD to zero effectively disables automatic aging, |
| 1666 | * which is clearly not what our intention is. So avoid that. |
| 1667 | */ |
| 1668 | if (!age_period) |
| 1669 | age_period = 1; |
| 1670 | |
| 1671 | ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1672 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1673 | EXPORT_SYMBOL(ocelot_set_ageing_time); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1674 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1675 | static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, |
| 1676 | const unsigned char *addr, |
| 1677 | u16 vid) |
| 1678 | { |
| 1679 | struct ocelot_multicast *mc; |
| 1680 | |
| 1681 | list_for_each_entry(mc, &ocelot->multicast, list) { |
| 1682 | if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) |
| 1683 | return mc; |
| 1684 | } |
| 1685 | |
| 1686 | return NULL; |
| 1687 | } |
| 1688 | |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1689 | static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) |
| 1690 | { |
| 1691 | if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) |
| 1692 | return ENTRYTYPE_MACv4; |
| 1693 | if (addr[0] == 0x33 && addr[1] == 0x33) |
| 1694 | return ENTRYTYPE_MACv6; |
Vladimir Oltean | 7c31314 | 2020-10-29 04:27:34 +0200 | [diff] [blame] | 1695 | return ENTRYTYPE_LOCKED; |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1696 | } |
| 1697 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1698 | static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index, |
| 1699 | unsigned long ports) |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1700 | { |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1701 | struct ocelot_pgid *pgid; |
| 1702 | |
| 1703 | pgid = kzalloc(sizeof(*pgid), GFP_KERNEL); |
| 1704 | if (!pgid) |
| 1705 | return ERR_PTR(-ENOMEM); |
| 1706 | |
| 1707 | pgid->ports = ports; |
| 1708 | pgid->index = index; |
| 1709 | refcount_set(&pgid->refcount, 1); |
| 1710 | list_add_tail(&pgid->list, &ocelot->pgids); |
| 1711 | |
| 1712 | return pgid; |
| 1713 | } |
| 1714 | |
| 1715 | static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid) |
| 1716 | { |
| 1717 | if (!refcount_dec_and_test(&pgid->refcount)) |
| 1718 | return; |
| 1719 | |
| 1720 | list_del(&pgid->list); |
| 1721 | kfree(pgid); |
| 1722 | } |
| 1723 | |
| 1724 | static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot, |
| 1725 | const struct ocelot_multicast *mc) |
| 1726 | { |
| 1727 | struct ocelot_pgid *pgid; |
| 1728 | int index; |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1729 | |
| 1730 | /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and |
| 1731 | * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the |
| 1732 | * destination mask table (PGID), the destination set is programmed as |
| 1733 | * part of the entry MAC address.", and the DEST_IDX is set to 0. |
| 1734 | */ |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1735 | if (mc->entry_type == ENTRYTYPE_MACv4 || |
| 1736 | mc->entry_type == ENTRYTYPE_MACv6) |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1737 | return ocelot_pgid_alloc(ocelot, 0, mc->ports); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1738 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1739 | list_for_each_entry(pgid, &ocelot->pgids, list) { |
| 1740 | /* When searching for a nonreserved multicast PGID, ignore the |
| 1741 | * dummy PGID of zero that we have for MACv4/MACv6 entries |
| 1742 | */ |
| 1743 | if (pgid->index && pgid->ports == mc->ports) { |
| 1744 | refcount_inc(&pgid->refcount); |
| 1745 | return pgid; |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | /* Search for a free index in the nonreserved multicast PGID area */ |
| 1750 | for_each_nonreserved_multicast_dest_pgid(ocelot, index) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1751 | bool used = false; |
| 1752 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1753 | list_for_each_entry(pgid, &ocelot->pgids, list) { |
| 1754 | if (pgid->index == index) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1755 | used = true; |
| 1756 | break; |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | if (!used) |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1761 | return ocelot_pgid_alloc(ocelot, index, mc->ports); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1762 | } |
| 1763 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1764 | return ERR_PTR(-ENOSPC); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | static void ocelot_encode_ports_to_mdb(unsigned char *addr, |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1768 | struct ocelot_multicast *mc) |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1769 | { |
Vladimir Oltean | ebbd860 | 2020-10-29 04:27:35 +0200 | [diff] [blame] | 1770 | ether_addr_copy(addr, mc->addr); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1771 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1772 | if (mc->entry_type == ENTRYTYPE_MACv4) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1773 | addr[0] = 0; |
| 1774 | addr[1] = mc->ports >> 8; |
| 1775 | addr[2] = mc->ports & 0xff; |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1776 | } else if (mc->entry_type == ENTRYTYPE_MACv6) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1777 | addr[0] = mc->ports >> 8; |
| 1778 | addr[1] = mc->ports & 0xff; |
| 1779 | } |
| 1780 | } |
| 1781 | |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1782 | int ocelot_port_mdb_add(struct ocelot *ocelot, int port, |
| 1783 | const struct switchdev_obj_port_mdb *mdb) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1784 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1785 | unsigned char addr[ETH_ALEN]; |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1786 | struct ocelot_multicast *mc; |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1787 | struct ocelot_pgid *pgid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1788 | u16 vid = mdb->vid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1789 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 1790 | if (port == ocelot->npi) |
| 1791 | port = ocelot->num_phys_ports; |
| 1792 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1793 | mc = ocelot_multicast_get(ocelot, mdb->addr, vid); |
| 1794 | if (!mc) { |
Vladimir Oltean | 728e69a | 2020-10-29 04:27:36 +0200 | [diff] [blame] | 1795 | /* New entry */ |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1796 | mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); |
| 1797 | if (!mc) |
| 1798 | return -ENOMEM; |
| 1799 | |
| 1800 | mc->entry_type = ocelot_classify_mdb(mdb->addr); |
| 1801 | ether_addr_copy(mc->addr, mdb->addr); |
| 1802 | mc->vid = vid; |
| 1803 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1804 | list_add_tail(&mc->list, &ocelot->multicast); |
Vladimir Oltean | 728e69a | 2020-10-29 04:27:36 +0200 | [diff] [blame] | 1805 | } else { |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1806 | /* Existing entry. Clean up the current port mask from |
| 1807 | * hardware now, because we'll be modifying it. |
| 1808 | */ |
| 1809 | ocelot_pgid_free(ocelot, mc->pgid); |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1810 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1811 | ocelot_mact_forget(ocelot, addr, vid); |
| 1812 | } |
| 1813 | |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1814 | mc->ports |= BIT(port); |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1815 | |
| 1816 | pgid = ocelot_mdb_get_pgid(ocelot, mc); |
| 1817 | if (IS_ERR(pgid)) { |
| 1818 | dev_err(ocelot->dev, |
| 1819 | "Cannot allocate PGID for mdb %pM vid %d\n", |
| 1820 | mc->addr, mc->vid); |
| 1821 | devm_kfree(ocelot->dev, mc); |
| 1822 | return PTR_ERR(pgid); |
| 1823 | } |
| 1824 | mc->pgid = pgid; |
| 1825 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1826 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1827 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1828 | if (mc->entry_type != ENTRYTYPE_MACv4 && |
| 1829 | mc->entry_type != ENTRYTYPE_MACv6) |
| 1830 | ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, |
| 1831 | pgid->index); |
| 1832 | |
| 1833 | return ocelot_mact_learn(ocelot, pgid->index, addr, vid, |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1834 | mc->entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1835 | } |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1836 | EXPORT_SYMBOL(ocelot_port_mdb_add); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1837 | |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1838 | int ocelot_port_mdb_del(struct ocelot *ocelot, int port, |
| 1839 | const struct switchdev_obj_port_mdb *mdb) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1840 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1841 | unsigned char addr[ETH_ALEN]; |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1842 | struct ocelot_multicast *mc; |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1843 | struct ocelot_pgid *pgid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1844 | u16 vid = mdb->vid; |
| 1845 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 1846 | if (port == ocelot->npi) |
| 1847 | port = ocelot->num_phys_ports; |
| 1848 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1849 | mc = ocelot_multicast_get(ocelot, mdb->addr, vid); |
| 1850 | if (!mc) |
| 1851 | return -ENOENT; |
| 1852 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1853 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1854 | ocelot_mact_forget(ocelot, addr, vid); |
| 1855 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1856 | ocelot_pgid_free(ocelot, mc->pgid); |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1857 | mc->ports &= ~BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1858 | if (!mc->ports) { |
| 1859 | list_del(&mc->list); |
| 1860 | devm_kfree(ocelot->dev, mc); |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1864 | /* We have a PGID with fewer ports now */ |
| 1865 | pgid = ocelot_mdb_get_pgid(ocelot, mc); |
| 1866 | if (IS_ERR(pgid)) |
| 1867 | return PTR_ERR(pgid); |
| 1868 | mc->pgid = pgid; |
| 1869 | |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1870 | ocelot_encode_ports_to_mdb(addr, mc); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1871 | |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 1872 | if (mc->entry_type != ENTRYTYPE_MACv4 && |
| 1873 | mc->entry_type != ENTRYTYPE_MACv6) |
| 1874 | ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID, |
| 1875 | pgid->index); |
| 1876 | |
| 1877 | return ocelot_mact_learn(ocelot, pgid->index, addr, vid, |
Vladimir Oltean | bb8d53f | 2020-10-29 04:27:37 +0200 | [diff] [blame] | 1878 | mc->entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1879 | } |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1880 | EXPORT_SYMBOL(ocelot_port_mdb_del); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1881 | |
Vladimir Oltean | e4bd44e | 2021-03-23 01:51:52 +0200 | [diff] [blame] | 1882 | void ocelot_port_bridge_join(struct ocelot *ocelot, int port, |
| 1883 | struct net_device *bridge) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1884 | { |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1885 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1886 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1887 | ocelot_port->bridge = bridge; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1888 | |
Vladimir Oltean | e4bd44e | 2021-03-23 01:51:52 +0200 | [diff] [blame] | 1889 | ocelot_apply_bridge_fwd_mask(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1890 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1891 | EXPORT_SYMBOL(ocelot_port_bridge_join); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1892 | |
Vladimir Oltean | e4bd44e | 2021-03-23 01:51:52 +0200 | [diff] [blame] | 1893 | void ocelot_port_bridge_leave(struct ocelot *ocelot, int port, |
| 1894 | struct net_device *bridge) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1895 | { |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1896 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 2e554a7 | 2020-10-03 01:06:46 +0300 | [diff] [blame] | 1897 | |
Vladimir Oltean | df291e5 | 2021-03-19 01:36:36 +0200 | [diff] [blame] | 1898 | ocelot_port->bridge = NULL; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 1899 | |
Vladimir Oltean | d400442 | 2021-10-20 20:58:52 +0300 | [diff] [blame] | 1900 | ocelot_port_set_pvid(ocelot, port, NULL); |
Vladimir Oltean | 0da1a1c | 2021-10-20 20:58:50 +0300 | [diff] [blame] | 1901 | ocelot_port_manage_port_tag(ocelot, port); |
Vladimir Oltean | e4bd44e | 2021-03-23 01:51:52 +0200 | [diff] [blame] | 1902 | ocelot_apply_bridge_fwd_mask(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1903 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1904 | EXPORT_SYMBOL(ocelot_port_bridge_leave); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1905 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1906 | static void ocelot_set_aggr_pgids(struct ocelot *ocelot) |
| 1907 | { |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1908 | unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1909 | int i, port, lag; |
| 1910 | |
| 1911 | /* Reset destination and aggregation PGIDS */ |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1912 | for_each_unicast_dest_pgid(ocelot, port) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1913 | ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); |
| 1914 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1915 | for_each_aggr_pgid(ocelot, i) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1916 | ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), |
| 1917 | ANA_PGID_PGID, i); |
| 1918 | |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1919 | /* The visited ports bitmask holds the list of ports offloading any |
| 1920 | * bonding interface. Initially we mark all these ports as unvisited, |
| 1921 | * then every time we visit a port in this bitmask, we know that it is |
| 1922 | * the lowest numbered port, i.e. the one whose logical ID == physical |
| 1923 | * port ID == LAG ID. So we mark as visited all further ports in the |
| 1924 | * bitmask that are offloading the same bonding interface. This way, |
| 1925 | * we set up the aggregation PGIDs only once per bonding interface. |
| 1926 | */ |
| 1927 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1928 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1929 | |
| 1930 | if (!ocelot_port || !ocelot_port->bond) |
| 1931 | continue; |
| 1932 | |
| 1933 | visited &= ~BIT(port); |
| 1934 | } |
| 1935 | |
| 1936 | /* Now, set PGIDs for each active LAG */ |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1937 | for (lag = 0; lag < ocelot->num_phys_ports; lag++) { |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1938 | struct net_device *bond = ocelot->ports[lag]->bond; |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1939 | int num_active_ports = 0; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1940 | unsigned long bond_mask; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1941 | u8 aggr_idx[16]; |
| 1942 | |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1943 | if (!bond || (visited & BIT(lag))) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1944 | continue; |
| 1945 | |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1946 | bond_mask = ocelot_get_bond_mask(ocelot, bond, true); |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1947 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1948 | for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { |
| 1949 | // Destination mask |
| 1950 | ocelot_write_rix(ocelot, bond_mask, |
| 1951 | ANA_PGID_PGID, port); |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1952 | aggr_idx[num_active_ports++] = port; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1953 | } |
| 1954 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1955 | for_each_aggr_pgid(ocelot, i) { |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1956 | u32 ac; |
| 1957 | |
| 1958 | ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); |
| 1959 | ac &= ~bond_mask; |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 1960 | /* Don't do division by zero if there was no active |
| 1961 | * port. Just make all aggregation codes zero. |
| 1962 | */ |
| 1963 | if (num_active_ports) |
| 1964 | ac |= BIT(aggr_idx[i % num_active_ports]); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1965 | ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); |
| 1966 | } |
Vladimir Oltean | 528d3f1 | 2021-02-06 00:02:17 +0200 | [diff] [blame] | 1967 | |
| 1968 | /* Mark all ports in the same LAG as visited to avoid applying |
| 1969 | * the same config again. |
| 1970 | */ |
| 1971 | for (port = lag; port < ocelot->num_phys_ports; port++) { |
| 1972 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1973 | |
| 1974 | if (!ocelot_port) |
| 1975 | continue; |
| 1976 | |
| 1977 | if (ocelot_port->bond == bond) |
| 1978 | visited |= BIT(port); |
| 1979 | } |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1983 | /* When offloading a bonding interface, the switch ports configured under the |
| 1984 | * same bond must have the same logical port ID, equal to the physical port ID |
| 1985 | * of the lowest numbered physical port in that bond. Otherwise, in standalone/ |
| 1986 | * bridged mode, each port has a logical port ID equal to its physical port ID. |
| 1987 | */ |
| 1988 | static void ocelot_setup_logical_port_ids(struct ocelot *ocelot) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1989 | { |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1990 | int port; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1991 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1992 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1993 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1994 | struct net_device *bond; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1995 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1996 | if (!ocelot_port) |
| 1997 | continue; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1998 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 1999 | bond = ocelot_port->bond; |
| 2000 | if (bond) { |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 2001 | int lag = __ffs(ocelot_get_bond_mask(ocelot, bond, |
| 2002 | false)); |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2003 | |
| 2004 | ocelot_rmw_gix(ocelot, |
| 2005 | ANA_PORT_PORT_CFG_PORTID_VAL(lag), |
| 2006 | ANA_PORT_PORT_CFG_PORTID_VAL_M, |
| 2007 | ANA_PORT_PORT_CFG, port); |
| 2008 | } else { |
| 2009 | ocelot_rmw_gix(ocelot, |
| 2010 | ANA_PORT_PORT_CFG_PORTID_VAL(port), |
| 2011 | ANA_PORT_PORT_CFG_PORTID_VAL_M, |
| 2012 | ANA_PORT_PORT_CFG, port); |
| 2013 | } |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 2017 | int ocelot_port_lag_join(struct ocelot *ocelot, int port, |
Vladimir Oltean | 583cbbe | 2021-02-06 00:02:12 +0200 | [diff] [blame] | 2018 | struct net_device *bond, |
| 2019 | struct netdev_lag_upper_info *info) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2020 | { |
Vladimir Oltean | 583cbbe | 2021-02-06 00:02:12 +0200 | [diff] [blame] | 2021 | if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) |
| 2022 | return -EOPNOTSUPP; |
| 2023 | |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 2024 | ocelot->ports[port]->bond = bond; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2025 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2026 | ocelot_setup_logical_port_ids(ocelot); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 2027 | ocelot_apply_bridge_fwd_mask(ocelot); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2028 | ocelot_set_aggr_pgids(ocelot); |
| 2029 | |
| 2030 | return 0; |
| 2031 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 2032 | EXPORT_SYMBOL(ocelot_port_lag_join); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2033 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 2034 | void ocelot_port_lag_leave(struct ocelot *ocelot, int port, |
| 2035 | struct net_device *bond) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2036 | { |
Vladimir Oltean | b80af65 | 2021-02-06 00:02:14 +0200 | [diff] [blame] | 2037 | ocelot->ports[port]->bond = NULL; |
| 2038 | |
Vladimir Oltean | 2527f2e | 2021-02-06 00:02:16 +0200 | [diff] [blame] | 2039 | ocelot_setup_logical_port_ids(ocelot); |
Vladimir Oltean | 9b52125 | 2021-01-29 03:00:02 +0200 | [diff] [blame] | 2040 | ocelot_apply_bridge_fwd_mask(ocelot); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 2041 | ocelot_set_aggr_pgids(ocelot); |
| 2042 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 2043 | EXPORT_SYMBOL(ocelot_port_lag_leave); |
Petr Machata | 0e332c8 | 2018-11-22 23:30:11 +0000 | [diff] [blame] | 2044 | |
Vladimir Oltean | 23ca3b7 | 2021-02-06 00:02:19 +0200 | [diff] [blame] | 2045 | void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active) |
| 2046 | { |
| 2047 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2048 | |
| 2049 | ocelot_port->lag_tx_active = lag_tx_active; |
| 2050 | |
| 2051 | /* Rebalance the LAGs */ |
| 2052 | ocelot_set_aggr_pgids(ocelot); |
| 2053 | } |
| 2054 | EXPORT_SYMBOL(ocelot_port_lag_change); |
| 2055 | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2056 | /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. |
| 2057 | * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG. |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2058 | * In the special case that it's the NPI port that we're configuring, the |
| 2059 | * length of the tag and optional prefix needs to be accounted for privately, |
| 2060 | * in order to be able to sustain communication at the requested @sdu. |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2061 | */ |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2062 | void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu) |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2063 | { |
| 2064 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2065 | int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 2066 | int pause_start, pause_stop; |
Vladimir Oltean | 601e984 | 2020-10-05 12:09:11 +0300 | [diff] [blame] | 2067 | int atop, atop_tot; |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2068 | |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2069 | if (port == ocelot->npi) { |
| 2070 | maxlen += OCELOT_TAG_LEN; |
| 2071 | |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2072 | if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2073 | maxlen += OCELOT_SHORT_PREFIX_LEN; |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2074 | else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2075 | maxlen += OCELOT_LONG_PREFIX_LEN; |
| 2076 | } |
| 2077 | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2078 | ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2079 | |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 2080 | /* Set Pause watermark hysteresis */ |
| 2081 | pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; |
| 2082 | pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; |
Maxim Kochetkov | 541132f | 2020-07-13 19:57:07 +0300 | [diff] [blame] | 2083 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, |
| 2084 | pause_start); |
| 2085 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, |
| 2086 | pause_stop); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2087 | |
Vladimir Oltean | 601e984 | 2020-10-05 12:09:11 +0300 | [diff] [blame] | 2088 | /* Tail dropping watermarks */ |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 2089 | atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) / |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2090 | OCELOT_BUFFER_CELL_SZ; |
Vladimir Oltean | 601e984 | 2020-10-05 12:09:11 +0300 | [diff] [blame] | 2091 | atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ; |
| 2092 | ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port); |
| 2093 | ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2094 | } |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2095 | EXPORT_SYMBOL(ocelot_port_set_maxlen); |
| 2096 | |
| 2097 | int ocelot_get_max_mtu(struct ocelot *ocelot, int port) |
| 2098 | { |
| 2099 | int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; |
| 2100 | |
| 2101 | if (port == ocelot->npi) { |
| 2102 | max_mtu -= OCELOT_TAG_LEN; |
| 2103 | |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2104 | if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2105 | max_mtu -= OCELOT_SHORT_PREFIX_LEN; |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2106 | else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG) |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 2107 | max_mtu -= OCELOT_LONG_PREFIX_LEN; |
| 2108 | } |
| 2109 | |
| 2110 | return max_mtu; |
| 2111 | } |
| 2112 | EXPORT_SYMBOL(ocelot_get_max_mtu); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2113 | |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame] | 2114 | static void ocelot_port_set_learning(struct ocelot *ocelot, int port, |
| 2115 | bool enabled) |
| 2116 | { |
| 2117 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2118 | u32 val = 0; |
| 2119 | |
| 2120 | if (enabled) |
| 2121 | val = ANA_PORT_PORT_CFG_LEARN_ENA; |
| 2122 | |
| 2123 | ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA, |
| 2124 | ANA_PORT_PORT_CFG, port); |
| 2125 | |
| 2126 | ocelot_port->learn_ena = enabled; |
| 2127 | } |
| 2128 | |
| 2129 | static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port, |
| 2130 | bool enabled) |
| 2131 | { |
| 2132 | u32 val = 0; |
| 2133 | |
| 2134 | if (enabled) |
| 2135 | val = BIT(port); |
| 2136 | |
| 2137 | ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC); |
| 2138 | } |
| 2139 | |
| 2140 | static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port, |
| 2141 | bool enabled) |
| 2142 | { |
| 2143 | u32 val = 0; |
| 2144 | |
| 2145 | if (enabled) |
| 2146 | val = BIT(port); |
| 2147 | |
| 2148 | ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC); |
| 2149 | } |
| 2150 | |
| 2151 | static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port, |
| 2152 | bool enabled) |
| 2153 | { |
| 2154 | u32 val = 0; |
| 2155 | |
| 2156 | if (enabled) |
| 2157 | val = BIT(port); |
| 2158 | |
| 2159 | ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC); |
| 2160 | } |
| 2161 | |
| 2162 | int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port, |
| 2163 | struct switchdev_brport_flags flags) |
| 2164 | { |
| 2165 | if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | |
| 2166 | BR_BCAST_FLOOD)) |
| 2167 | return -EINVAL; |
| 2168 | |
| 2169 | return 0; |
| 2170 | } |
| 2171 | EXPORT_SYMBOL(ocelot_port_pre_bridge_flags); |
| 2172 | |
| 2173 | void ocelot_port_bridge_flags(struct ocelot *ocelot, int port, |
| 2174 | struct switchdev_brport_flags flags) |
| 2175 | { |
| 2176 | if (flags.mask & BR_LEARNING) |
| 2177 | ocelot_port_set_learning(ocelot, port, |
| 2178 | !!(flags.val & BR_LEARNING)); |
| 2179 | |
| 2180 | if (flags.mask & BR_FLOOD) |
| 2181 | ocelot_port_set_ucast_flood(ocelot, port, |
| 2182 | !!(flags.val & BR_FLOOD)); |
| 2183 | |
| 2184 | if (flags.mask & BR_MCAST_FLOOD) |
| 2185 | ocelot_port_set_mcast_flood(ocelot, port, |
| 2186 | !!(flags.val & BR_MCAST_FLOOD)); |
| 2187 | |
| 2188 | if (flags.mask & BR_BCAST_FLOOD) |
| 2189 | ocelot_port_set_bcast_flood(ocelot, port, |
| 2190 | !!(flags.val & BR_BCAST_FLOOD)); |
| 2191 | } |
| 2192 | EXPORT_SYMBOL(ocelot_port_bridge_flags); |
| 2193 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 2194 | void ocelot_init_port(struct ocelot *ocelot, int port) |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 2195 | { |
| 2196 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2197 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 2198 | skb_queue_head_init(&ocelot_port->tx_skbs); |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2199 | |
| 2200 | /* Basic L2 initialization */ |
| 2201 | |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 2202 | /* Set MAC IFG Gaps |
| 2203 | * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 |
| 2204 | * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 |
| 2205 | */ |
| 2206 | ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), |
| 2207 | DEV_MAC_IFG_CFG); |
| 2208 | |
| 2209 | /* Load seed (0) and set MAC HDX late collision */ |
| 2210 | ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | |
| 2211 | DEV_MAC_HDX_CFG_SEED_LOAD, |
| 2212 | DEV_MAC_HDX_CFG); |
| 2213 | mdelay(1); |
| 2214 | ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), |
| 2215 | DEV_MAC_HDX_CFG); |
| 2216 | |
| 2217 | /* Set Max Length and maximum tags allowed */ |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2218 | ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 2219 | ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | |
| 2220 | DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 2221 | DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 2222 | DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, |
| 2223 | DEV_MAC_TAGS_CFG); |
| 2224 | |
| 2225 | /* Set SMAC of Pause frame (00:00:00:00:00:00) */ |
| 2226 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); |
| 2227 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); |
| 2228 | |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 2229 | /* Enable transmission of pause frames */ |
Maxim Kochetkov | 541132f | 2020-07-13 19:57:07 +0300 | [diff] [blame] | 2230 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 2231 | |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2232 | /* Drop frames with multicast source address */ |
| 2233 | ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, |
| 2234 | ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, |
| 2235 | ANA_PORT_DROP_CFG, port); |
| 2236 | |
| 2237 | /* Set default VLAN and tag type to 8021Q. */ |
| 2238 | ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), |
| 2239 | REW_PORT_VLAN_CFG_PORT_TPID_M, |
| 2240 | REW_PORT_VLAN_CFG, port); |
| 2241 | |
Vladimir Oltean | 421741e | 2021-02-12 17:15:59 +0200 | [diff] [blame] | 2242 | /* Disable source address learning for standalone mode */ |
| 2243 | ocelot_port_set_learning(ocelot, port, false); |
| 2244 | |
Vladimir Oltean | 46efe4e | 2021-08-15 04:47:47 +0300 | [diff] [blame] | 2245 | /* Set the port's initial logical port ID value, enable receiving |
| 2246 | * frames on it, and configure the MAC address learning type to |
| 2247 | * automatic. |
| 2248 | */ |
| 2249 | ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | |
| 2250 | ANA_PORT_PORT_CFG_RECV_ENA | |
| 2251 | ANA_PORT_PORT_CFG_PORTID_VAL(port), |
| 2252 | ANA_PORT_PORT_CFG, port); |
| 2253 | |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2254 | /* Enable vcap lookups */ |
| 2255 | ocelot_vcap_enable(ocelot, port); |
| 2256 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 2257 | EXPORT_SYMBOL(ocelot_init_port); |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 2258 | |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 2259 | /* Configure and enable the CPU port module, which is a set of queues |
| 2260 | * accessible through register MMIO, frame DMA or Ethernet (in case |
| 2261 | * NPI mode is used). |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2262 | */ |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 2263 | static void ocelot_cpu_port_init(struct ocelot *ocelot) |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2264 | { |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2265 | int cpu = ocelot->num_phys_ports; |
| 2266 | |
| 2267 | /* The unicast destination PGID for the CPU port module is unused */ |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2268 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2269 | /* Instead set up a multicast destination PGID for traffic copied to |
| 2270 | * the CPU. Whitelisted MAC addresses like the port netdevice MAC |
| 2271 | * addresses will be copied to the CPU via this PGID. |
| 2272 | */ |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2273 | ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); |
| 2274 | ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | |
| 2275 | ANA_PORT_PORT_CFG_PORTID_VAL(cpu), |
| 2276 | ANA_PORT_PORT_CFG, cpu); |
| 2277 | |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2278 | /* Enable CPU port module */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 2279 | ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 2280 | /* CPU port Injection/Extraction configuration */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 2281 | ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2282 | OCELOT_TAG_PREFIX_NONE); |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 2283 | ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, |
Vladimir Oltean | cacea62 | 2021-01-29 03:00:03 +0200 | [diff] [blame] | 2284 | OCELOT_TAG_PREFIX_NONE); |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2285 | |
| 2286 | /* Configure the CPU port to be VLAN aware */ |
Vladimir Oltean | bfbab31 | 2021-10-20 20:58:51 +0300 | [diff] [blame] | 2287 | ocelot_write_gix(ocelot, |
| 2288 | ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_VLAN_UNAWARE_PVID) | |
| 2289 | ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 2290 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2291 | ANA_PORT_VLAN_CFG, cpu); |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2292 | } |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2293 | |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 2294 | static void ocelot_detect_features(struct ocelot *ocelot) |
| 2295 | { |
| 2296 | int mmgt, eq_ctrl; |
| 2297 | |
| 2298 | /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds |
| 2299 | * the number of 240-byte free memory words (aka 4-cell chunks) and not |
| 2300 | * 192 bytes as the documentation incorrectly says. |
| 2301 | */ |
| 2302 | mmgt = ocelot_read(ocelot, SYS_MMGT); |
| 2303 | ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt); |
| 2304 | |
| 2305 | eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL); |
| 2306 | ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl); |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 2307 | } |
| 2308 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2309 | int ocelot_init(struct ocelot *ocelot) |
| 2310 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2311 | char queue_name[32]; |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 2312 | int i, ret; |
| 2313 | u32 port; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2314 | |
Vladimir Oltean | 3a77b59 | 2019-11-14 17:03:26 +0200 | [diff] [blame] | 2315 | if (ocelot->ops->reset) { |
| 2316 | ret = ocelot->ops->reset(ocelot); |
| 2317 | if (ret) { |
| 2318 | dev_err(ocelot->dev, "Switch reset failed\n"); |
| 2319 | return ret; |
| 2320 | } |
| 2321 | } |
| 2322 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2323 | ocelot->stats = devm_kcalloc(ocelot->dev, |
| 2324 | ocelot->num_phys_ports * ocelot->num_stats, |
| 2325 | sizeof(u64), GFP_KERNEL); |
| 2326 | if (!ocelot->stats) |
| 2327 | return -ENOMEM; |
| 2328 | |
| 2329 | mutex_init(&ocelot->stats_lock); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 2330 | mutex_init(&ocelot->ptp_lock); |
Vladimir Oltean | 2468346 | 2021-10-24 20:17:51 +0300 | [diff] [blame] | 2331 | mutex_init(&ocelot->mact_lock); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 2332 | spin_lock_init(&ocelot->ptp_clock_lock); |
Vladimir Oltean | 52849bc | 2021-10-12 14:40:36 +0300 | [diff] [blame] | 2333 | spin_lock_init(&ocelot->ts_id_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2334 | snprintf(queue_name, sizeof(queue_name), "%s-stats", |
| 2335 | dev_name(ocelot->dev)); |
| 2336 | ocelot->stats_queue = create_singlethread_workqueue(queue_name); |
| 2337 | if (!ocelot->stats_queue) |
| 2338 | return -ENOMEM; |
| 2339 | |
Vladimir Oltean | ca0b272 | 2020-12-12 21:16:12 +0200 | [diff] [blame] | 2340 | ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0); |
| 2341 | if (!ocelot->owq) { |
| 2342 | destroy_workqueue(ocelot->stats_queue); |
| 2343 | return -ENOMEM; |
| 2344 | } |
| 2345 | |
Claudiu Manoil | 2b120dd | 2019-11-09 15:02:58 +0200 | [diff] [blame] | 2346 | INIT_LIST_HEAD(&ocelot->multicast); |
Vladimir Oltean | e5d1f89 | 2020-10-29 04:27:38 +0200 | [diff] [blame] | 2347 | INIT_LIST_HEAD(&ocelot->pgids); |
Vladimir Oltean | 90e0aa8 | 2021-10-20 20:58:49 +0300 | [diff] [blame] | 2348 | INIT_LIST_HEAD(&ocelot->vlans); |
Vladimir Oltean | f6fe01d | 2021-01-15 04:11:11 +0200 | [diff] [blame] | 2349 | ocelot_detect_features(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2350 | ocelot_mact_init(ocelot); |
| 2351 | ocelot_vlan_init(ocelot); |
Vladimir Oltean | aae4e50 | 2020-06-20 18:43:46 +0300 | [diff] [blame] | 2352 | ocelot_vcap_init(ocelot); |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 2353 | ocelot_cpu_port_init(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2354 | |
Xiaoliang Yang | 23e2c50 | 2021-11-18 18:11:59 +0800 | [diff] [blame^] | 2355 | if (ocelot->ops->psfp_init) |
| 2356 | ocelot->ops->psfp_init(ocelot); |
| 2357 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2358 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 2359 | /* Clear all counters (5 groups) */ |
| 2360 | ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | |
| 2361 | SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), |
| 2362 | SYS_STAT_CFG); |
| 2363 | } |
| 2364 | |
| 2365 | /* Only use S-Tag */ |
| 2366 | ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); |
| 2367 | |
| 2368 | /* Aggregation mode */ |
| 2369 | ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | |
| 2370 | ANA_AGGR_CFG_AC_DMAC_ENA | |
| 2371 | ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | |
Vladimir Oltean | f79c20c | 2021-02-06 00:02:13 +0200 | [diff] [blame] | 2372 | ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA | |
| 2373 | ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA | |
| 2374 | ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA, |
| 2375 | ANA_AGGR_CFG); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2376 | |
| 2377 | /* Set MAC age time to default value. The entry is aged after |
| 2378 | * 2*AGE_PERIOD |
| 2379 | */ |
| 2380 | ocelot_write(ocelot, |
| 2381 | ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), |
| 2382 | ANA_AUTOAGE); |
| 2383 | |
| 2384 | /* Disable learning for frames discarded by VLAN ingress filtering */ |
| 2385 | regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); |
| 2386 | |
| 2387 | /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ |
| 2388 | ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | |
| 2389 | SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); |
| 2390 | |
| 2391 | /* Setup flooding PGIDs */ |
Vladimir Oltean | edd2410 | 2020-12-04 19:54:16 +0200 | [diff] [blame] | 2392 | for (i = 0; i < ocelot->num_flooding_pgids; i++) |
| 2393 | ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | |
Vladimir Oltean | b360d94 | 2021-02-12 17:15:58 +0200 | [diff] [blame] | 2394 | ANA_FLOODING_FLD_BROADCAST(PGID_BC) | |
Vladimir Oltean | edd2410 | 2020-12-04 19:54:16 +0200 | [diff] [blame] | 2395 | ANA_FLOODING_FLD_UNICAST(PGID_UC), |
| 2396 | ANA_FLOODING, i); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2397 | ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | |
| 2398 | ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | |
| 2399 | ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | |
| 2400 | ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), |
| 2401 | ANA_FLOODING_IPMC); |
| 2402 | |
| 2403 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 2404 | /* Transmit the frame to the local port. */ |
| 2405 | ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); |
| 2406 | /* Do not forward BPDU frames to the front ports. */ |
| 2407 | ocelot_write_gix(ocelot, |
| 2408 | ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), |
| 2409 | ANA_PORT_CPU_FWD_BPDU_CFG, |
| 2410 | port); |
| 2411 | /* Ensure bridging is disabled */ |
| 2412 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); |
| 2413 | } |
| 2414 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 2415 | for_each_nonreserved_multicast_dest_pgid(ocelot, i) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2416 | u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); |
| 2417 | |
| 2418 | ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); |
| 2419 | } |
Horatiu Vultur | ebb1bb4 | 2021-03-16 21:10:17 +0100 | [diff] [blame] | 2420 | |
| 2421 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE); |
| 2422 | |
Vladimir Oltean | b360d94 | 2021-02-12 17:15:58 +0200 | [diff] [blame] | 2423 | /* Allow broadcast and unknown L2 multicast to the CPU. */ |
| 2424 | ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 2425 | ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 2426 | ANA_PGID_PGID, PGID_MC); |
| 2427 | ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 2428 | ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)), |
| 2429 | ANA_PGID_PGID, PGID_BC); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2430 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); |
| 2431 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); |
| 2432 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2433 | /* Allow manual injection via DEVCPU_QS registers, and byte swap these |
| 2434 | * registers endianness. |
| 2435 | */ |
| 2436 | ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | |
| 2437 | QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); |
| 2438 | ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | |
| 2439 | QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); |
| 2440 | ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | |
| 2441 | ANA_CPUQ_CFG_CPUQ_LRN(2) | |
| 2442 | ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | |
| 2443 | ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | |
| 2444 | ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | |
| 2445 | ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | |
| 2446 | ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | |
| 2447 | ANA_CPUQ_CFG_CPUQ_IGMP(6) | |
| 2448 | ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); |
| 2449 | for (i = 0; i < 16; i++) |
| 2450 | ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | |
| 2451 | ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), |
| 2452 | ANA_CPUQ_8021_CFG, i); |
| 2453 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 2454 | INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2455 | queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, |
| 2456 | OCELOT_STATS_CHECK_DELAY); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 2457 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2458 | return 0; |
| 2459 | } |
| 2460 | EXPORT_SYMBOL(ocelot_init); |
| 2461 | |
| 2462 | void ocelot_deinit(struct ocelot *ocelot) |
| 2463 | { |
Claudiu Manoil | c5d1396 | 2019-07-25 16:33:18 +0300 | [diff] [blame] | 2464 | cancel_delayed_work(&ocelot->stats_work); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2465 | destroy_workqueue(ocelot->stats_queue); |
Vladimir Oltean | ca0b272 | 2020-12-12 21:16:12 +0200 | [diff] [blame] | 2466 | destroy_workqueue(ocelot->owq); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2467 | mutex_destroy(&ocelot->stats_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2468 | } |
| 2469 | EXPORT_SYMBOL(ocelot_deinit); |
| 2470 | |
Vladimir Oltean | e5fb512 | 2020-09-18 04:07:30 +0300 | [diff] [blame] | 2471 | void ocelot_deinit_port(struct ocelot *ocelot, int port) |
| 2472 | { |
| 2473 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 2474 | |
| 2475 | skb_queue_purge(&ocelot_port->tx_skbs); |
| 2476 | } |
| 2477 | EXPORT_SYMBOL(ocelot_deinit_port); |
| 2478 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 2479 | MODULE_LICENSE("Dual MIT/GPL"); |