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 | */ |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 7 | #include <linux/if_bridge.h> |
Vladimir Oltean | 2096805 | 2020-09-30 01:27:26 +0300 | [diff] [blame^] | 8 | #include <soc/mscc/ocelot_vcap.h> |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 9 | #include "ocelot.h" |
Vladimir Oltean | 3c83654 | 2020-06-20 18:43:45 +0300 | [diff] [blame] | 10 | #include "ocelot_vcap.h" |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 11 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 12 | #define TABLE_UPDATE_SLEEP_US 10 |
| 13 | #define TABLE_UPDATE_TIMEOUT_US 100000 |
| 14 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 15 | struct ocelot_mact_entry { |
| 16 | u8 mac[ETH_ALEN]; |
| 17 | u16 vid; |
| 18 | enum macaccess_entry_type type; |
| 19 | }; |
| 20 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 21 | static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot) |
| 22 | { |
| 23 | return ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 24 | } |
| 25 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 26 | static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot) |
| 27 | { |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 28 | u32 val; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 29 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 30 | return readx_poll_timeout(ocelot_mact_read_macaccess, |
| 31 | ocelot, val, |
| 32 | (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) == |
| 33 | MACACCESS_CMD_IDLE, |
| 34 | TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static void ocelot_mact_select(struct ocelot *ocelot, |
| 38 | const unsigned char mac[ETH_ALEN], |
| 39 | unsigned int vid) |
| 40 | { |
| 41 | u32 macl = 0, mach = 0; |
| 42 | |
| 43 | /* Set the MAC address to handle and the vlan associated in a format |
| 44 | * understood by the hardware. |
| 45 | */ |
| 46 | mach |= vid << 16; |
| 47 | mach |= mac[0] << 8; |
| 48 | mach |= mac[1] << 0; |
| 49 | macl |= mac[2] << 24; |
| 50 | macl |= mac[3] << 16; |
| 51 | macl |= mac[4] << 8; |
| 52 | macl |= mac[5] << 0; |
| 53 | |
| 54 | ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA); |
| 55 | ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA); |
| 56 | |
| 57 | } |
| 58 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 59 | int ocelot_mact_learn(struct ocelot *ocelot, int port, |
| 60 | const unsigned char mac[ETH_ALEN], |
| 61 | unsigned int vid, enum macaccess_entry_type type) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 62 | { |
| 63 | ocelot_mact_select(ocelot, mac, vid); |
| 64 | |
| 65 | /* Issue a write command */ |
| 66 | ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID | |
| 67 | ANA_TABLES_MACACCESS_DEST_IDX(port) | |
| 68 | ANA_TABLES_MACACCESS_ENTRYTYPE(type) | |
| 69 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN), |
| 70 | ANA_TABLES_MACACCESS); |
| 71 | |
| 72 | return ocelot_mact_wait_for_completion(ocelot); |
| 73 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 74 | EXPORT_SYMBOL(ocelot_mact_learn); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 75 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 76 | int ocelot_mact_forget(struct ocelot *ocelot, |
| 77 | const unsigned char mac[ETH_ALEN], unsigned int vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 78 | { |
| 79 | ocelot_mact_select(ocelot, mac, vid); |
| 80 | |
| 81 | /* Issue a forget command */ |
| 82 | ocelot_write(ocelot, |
| 83 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET), |
| 84 | ANA_TABLES_MACACCESS); |
| 85 | |
| 86 | return ocelot_mact_wait_for_completion(ocelot); |
| 87 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 88 | EXPORT_SYMBOL(ocelot_mact_forget); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 89 | |
| 90 | static void ocelot_mact_init(struct ocelot *ocelot) |
| 91 | { |
| 92 | /* Configure the learning mode entries attributes: |
| 93 | * - Do not copy the frame to the CPU extraction queues. |
| 94 | * - Use the vlan and mac_cpoy for dmac lookup. |
| 95 | */ |
| 96 | ocelot_rmw(ocelot, 0, |
| 97 | ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS |
| 98 | | ANA_AGENCTRL_LEARN_FWD_KILL |
| 99 | | ANA_AGENCTRL_LEARN_IGNORE_VLAN, |
| 100 | ANA_AGENCTRL); |
| 101 | |
| 102 | /* Clear the MAC table */ |
| 103 | ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS); |
| 104 | } |
| 105 | |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 106 | static void ocelot_vcap_enable(struct ocelot *ocelot, int port) |
Horatiu Vultur | b596229 | 2019-05-31 09:16:56 +0200 | [diff] [blame] | 107 | { |
| 108 | ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA | |
| 109 | ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa), |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 110 | ANA_PORT_VCAP_S2_CFG, port); |
Horatiu Vultur | b596229 | 2019-05-31 09:16:56 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 113 | static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot) |
| 114 | { |
| 115 | return ocelot_read(ocelot, ANA_TABLES_VLANACCESS); |
| 116 | } |
| 117 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 118 | static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot) |
| 119 | { |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 120 | u32 val; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 121 | |
Steen Hegelund | 639c1b2 | 2018-12-20 14:16:31 +0100 | [diff] [blame] | 122 | return readx_poll_timeout(ocelot_vlant_read_vlanaccess, |
| 123 | ocelot, |
| 124 | val, |
| 125 | (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) == |
| 126 | ANA_TABLES_VLANACCESS_CMD_IDLE, |
| 127 | TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 130 | static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask) |
| 131 | { |
| 132 | /* Select the VID to configure */ |
| 133 | ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid), |
| 134 | ANA_TABLES_VLANTIDX); |
| 135 | /* Set the vlan port members mask and issue a write command */ |
| 136 | ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) | |
| 137 | ANA_TABLES_VLANACCESS_CMD_WRITE, |
| 138 | ANA_TABLES_VLANACCESS); |
| 139 | |
| 140 | return ocelot_vlant_wait_for_completion(ocelot); |
| 141 | } |
| 142 | |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 143 | static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port, |
| 144 | u16 vid) |
| 145 | { |
| 146 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 147 | u32 val = 0; |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 148 | |
| 149 | if (ocelot_port->vid != vid) { |
| 150 | /* Always permit deleting the native VLAN (vid = 0) */ |
| 151 | if (ocelot_port->vid && vid) { |
| 152 | dev_err(ocelot->dev, |
| 153 | "Port already has a native VLAN: %d\n", |
| 154 | ocelot_port->vid); |
| 155 | return -EBUSY; |
| 156 | } |
| 157 | ocelot_port->vid = vid; |
| 158 | } |
| 159 | |
| 160 | ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid), |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 161 | REW_PORT_VLAN_CFG_PORT_VID_M, |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 162 | REW_PORT_VLAN_CFG, port); |
| 163 | |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 164 | if (ocelot_port->vlan_aware && !ocelot_port->vid) |
| 165 | /* If port is vlan-aware and tagged, drop untagged and priority |
| 166 | * tagged frames. |
| 167 | */ |
| 168 | val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA | |
| 169 | ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | |
| 170 | ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA; |
| 171 | ocelot_rmw_gix(ocelot, val, |
| 172 | ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA | |
| 173 | ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA | |
| 174 | ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA, |
| 175 | ANA_PORT_DROP_CFG, port); |
| 176 | |
| 177 | if (ocelot_port->vlan_aware) { |
| 178 | if (ocelot_port->vid) |
| 179 | /* Tag all frames except when VID == DEFAULT_VLAN */ |
| 180 | val = REW_TAG_CFG_TAG_CFG(1); |
| 181 | else |
| 182 | /* Tag all frames */ |
| 183 | val = REW_TAG_CFG_TAG_CFG(3); |
| 184 | } else { |
| 185 | /* Port tagging disabled. */ |
| 186 | val = REW_TAG_CFG_TAG_CFG(0); |
| 187 | } |
| 188 | ocelot_rmw_gix(ocelot, val, |
| 189 | REW_TAG_CFG_TAG_CFG_M, |
| 190 | REW_TAG_CFG, port); |
| 191 | |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 195 | void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port, |
| 196 | bool vlan_aware) |
| 197 | { |
| 198 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 199 | u32 val; |
| 200 | |
| 201 | ocelot_port->vlan_aware = vlan_aware; |
| 202 | |
| 203 | if (vlan_aware) |
| 204 | val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 205 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1); |
| 206 | else |
| 207 | val = 0; |
| 208 | ocelot_rmw_gix(ocelot, val, |
| 209 | ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 210 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M, |
| 211 | ANA_PORT_VLAN_CFG, port); |
| 212 | |
| 213 | ocelot_port_set_native_vlan(ocelot, port, ocelot_port->vid); |
| 214 | } |
| 215 | EXPORT_SYMBOL(ocelot_port_vlan_filtering); |
| 216 | |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 217 | /* Default vlan to clasify for untagged frames (may be zero) */ |
| 218 | static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid) |
| 219 | { |
| 220 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 221 | |
| 222 | ocelot_rmw_gix(ocelot, |
| 223 | ANA_PORT_VLAN_CFG_VLAN_VID(pvid), |
| 224 | ANA_PORT_VLAN_CFG_VLAN_VID_M, |
| 225 | ANA_PORT_VLAN_CFG, port); |
| 226 | |
| 227 | ocelot_port->pvid = pvid; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 230 | int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid, |
| 231 | bool untagged) |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 232 | { |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 233 | int ret; |
| 234 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 235 | /* Make the port a member of the VLAN */ |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 236 | ocelot->vlan_mask[vid] |= BIT(port); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 237 | ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); |
| 238 | if (ret) |
| 239 | return ret; |
| 240 | |
| 241 | /* Default ingress vlan classification */ |
| 242 | if (pvid) |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 243 | ocelot_port_set_pvid(ocelot, port, vid); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 244 | |
| 245 | /* Untagged egress vlan clasification */ |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 246 | if (untagged) { |
| 247 | ret = ocelot_port_set_native_vlan(ocelot, port, vid); |
| 248 | if (ret) |
| 249 | return ret; |
Vladimir Oltean | b9cd75e | 2019-10-26 21:04:27 +0300 | [diff] [blame] | 250 | } |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 251 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 252 | return 0; |
| 253 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 254 | EXPORT_SYMBOL(ocelot_vlan_add); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 255 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 256 | int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid) |
Vladimir Oltean | 9855934 | 2019-11-09 15:02:48 +0200 | [diff] [blame] | 257 | { |
| 258 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 259 | int ret; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 260 | |
| 261 | /* Stop the port from being a member of the vlan */ |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 262 | ocelot->vlan_mask[vid] &= ~BIT(port); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 263 | ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
| 267 | /* Ingress */ |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 268 | if (ocelot_port->pvid == vid) |
| 269 | ocelot_port_set_pvid(ocelot, port, 0); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 270 | |
| 271 | /* Egress */ |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 272 | if (ocelot_port->vid == vid) |
| 273 | ocelot_port_set_native_vlan(ocelot, port, 0); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 277 | EXPORT_SYMBOL(ocelot_vlan_del); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 278 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 279 | static void ocelot_vlan_init(struct ocelot *ocelot) |
| 280 | { |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 281 | u16 port, vid; |
| 282 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 283 | /* Clear VLAN table, by default all ports are members of all VLANs */ |
| 284 | ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT, |
| 285 | ANA_TABLES_VLANACCESS); |
| 286 | ocelot_vlant_wait_for_completion(ocelot); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 287 | |
| 288 | /* Configure the port VLAN memberships */ |
| 289 | for (vid = 1; vid < VLAN_N_VID; vid++) { |
| 290 | ocelot->vlan_mask[vid] = 0; |
| 291 | ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]); |
| 292 | } |
| 293 | |
| 294 | /* Because VLAN filtering is enabled, we need VID 0 to get untagged |
| 295 | * traffic. It is added automatically if 8021q module is loaded, but |
| 296 | * we can't rely on it since module may be not loaded. |
| 297 | */ |
| 298 | ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0); |
| 299 | ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]); |
| 300 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 301 | /* Set vlan ingress filter mask to all ports but the CPU port by |
| 302 | * default. |
| 303 | */ |
Vladimir Oltean | 714d0ff | 2019-11-09 15:02:55 +0200 | [diff] [blame] | 304 | ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), |
| 305 | ANA_VLANMASK); |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 306 | |
| 307 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 308 | ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port); |
| 309 | ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port); |
| 310 | } |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 313 | void ocelot_adjust_link(struct ocelot *ocelot, int port, |
| 314 | struct phy_device *phydev) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 315 | { |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 316 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 317 | int speed, mode = 0; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 318 | |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 319 | switch (phydev->speed) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 320 | case SPEED_10: |
| 321 | speed = OCELOT_SPEED_10; |
| 322 | break; |
| 323 | case SPEED_100: |
| 324 | speed = OCELOT_SPEED_100; |
| 325 | break; |
| 326 | case SPEED_1000: |
| 327 | speed = OCELOT_SPEED_1000; |
| 328 | mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; |
| 329 | break; |
| 330 | case SPEED_2500: |
| 331 | speed = OCELOT_SPEED_2500; |
| 332 | mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA; |
| 333 | break; |
| 334 | default: |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 335 | dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n", |
| 336 | port, phydev->speed); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 340 | phy_print_status(phydev); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 341 | |
Vladimir Oltean | 26f4dba | 2019-11-09 15:02:59 +0200 | [diff] [blame] | 342 | if (!phydev->link) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 343 | return; |
| 344 | |
| 345 | /* Only full duplex supported for now */ |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 346 | ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 347 | mode, DEV_MAC_MODE_CFG); |
| 348 | |
Vladimir Oltean | 1ba8f65 | 2020-02-29 16:31:11 +0200 | [diff] [blame] | 349 | /* Disable HDX fast control */ |
| 350 | ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS, |
| 351 | DEV_PORT_MISC); |
| 352 | |
| 353 | /* SGMII only for now */ |
| 354 | ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA, |
| 355 | PCS1G_MODE_CFG); |
| 356 | ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG); |
| 357 | |
| 358 | /* Enable PCS */ |
| 359 | ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG); |
| 360 | |
| 361 | /* No aneg on SGMII */ |
| 362 | ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG); |
| 363 | |
| 364 | /* No loopback */ |
| 365 | ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 366 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 367 | /* Enable MAC module */ |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 368 | ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 369 | DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); |
| 370 | |
| 371 | /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of |
| 372 | * reset */ |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 373 | ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed), |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 374 | DEV_CLOCK_CFG); |
| 375 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 376 | /* No PFC */ |
| 377 | ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed), |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 378 | ANA_PFC_PFC_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 379 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 380 | /* Core: Enable port for frame transfer */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 381 | ocelot_fields_write(ocelot, port, |
| 382 | QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 383 | |
| 384 | /* Flow control */ |
| 385 | ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | |
| 386 | SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA | |
| 387 | SYS_MAC_FC_CFG_ZERO_PAUSE_ENA | |
| 388 | SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | |
| 389 | SYS_MAC_FC_CFG_FC_LINK_SPEED(speed), |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 390 | SYS_MAC_FC_CFG, port); |
| 391 | ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 392 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 393 | EXPORT_SYMBOL(ocelot_adjust_link); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 394 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 395 | void ocelot_port_enable(struct ocelot *ocelot, int port, |
| 396 | struct phy_device *phy) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 397 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 398 | /* Enable receiving frames on the port, and activate auto-learning of |
| 399 | * MAC addresses. |
| 400 | */ |
| 401 | ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | |
| 402 | ANA_PORT_PORT_CFG_RECV_ENA | |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 403 | ANA_PORT_PORT_CFG_PORTID_VAL(port), |
| 404 | ANA_PORT_PORT_CFG, port); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 405 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 406 | EXPORT_SYMBOL(ocelot_port_enable); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 407 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 408 | void ocelot_port_disable(struct ocelot *ocelot, int port) |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 409 | { |
| 410 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 411 | |
| 412 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG); |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 413 | ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 414 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 415 | EXPORT_SYMBOL(ocelot_port_disable); |
Vladimir Oltean | 889b895 | 2019-11-09 15:02:57 +0200 | [diff] [blame] | 416 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 417 | void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, |
| 418 | struct sk_buff *clone) |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 419 | { |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 420 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 421 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 422 | spin_lock(&ocelot_port->ts_id_lock); |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 423 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 424 | skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; |
| 425 | /* Store timestamp ID in cb[0] of sk_buff */ |
| 426 | clone->cb[0] = ocelot_port->ts_id; |
| 427 | ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4; |
| 428 | skb_queue_tail(&ocelot_port->tx_skbs, clone); |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 429 | |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 430 | spin_unlock(&ocelot_port->ts_id_lock); |
Yangbo Lu | 400928b | 2019-11-20 16:23:16 +0800 | [diff] [blame] | 431 | } |
| 432 | EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb); |
| 433 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 434 | static void ocelot_get_hwtimestamp(struct ocelot *ocelot, |
| 435 | struct timespec64 *ts) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 436 | { |
| 437 | unsigned long flags; |
| 438 | u32 val; |
| 439 | |
| 440 | spin_lock_irqsave(&ocelot->ptp_clock_lock, flags); |
| 441 | |
| 442 | /* Read current PTP time to get seconds */ |
| 443 | val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN); |
| 444 | |
| 445 | val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM); |
| 446 | val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE); |
| 447 | ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN); |
| 448 | ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN); |
| 449 | |
| 450 | /* Read packet HW timestamp from FIFO */ |
| 451 | val = ocelot_read(ocelot, SYS_PTP_TXSTAMP); |
| 452 | ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val); |
| 453 | |
| 454 | /* Sec has incremented since the ts was registered */ |
| 455 | if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC)) |
| 456 | ts->tv_sec--; |
| 457 | |
| 458 | spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags); |
| 459 | } |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 460 | |
| 461 | void ocelot_get_txtstamp(struct ocelot *ocelot) |
| 462 | { |
| 463 | int budget = OCELOT_PTP_QUEUE_SZ; |
| 464 | |
| 465 | while (budget--) { |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 466 | struct sk_buff *skb, *skb_tmp, *skb_match = NULL; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 467 | struct skb_shared_hwtstamps shhwtstamps; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 468 | struct ocelot_port *port; |
| 469 | struct timespec64 ts; |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 470 | unsigned long flags; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 471 | u32 val, id, txport; |
| 472 | |
| 473 | val = ocelot_read(ocelot, SYS_PTP_STATUS); |
| 474 | |
| 475 | /* Check if a timestamp can be retrieved */ |
| 476 | if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD)) |
| 477 | break; |
| 478 | |
| 479 | WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL); |
| 480 | |
| 481 | /* Retrieve the ts ID and Tx port */ |
| 482 | id = SYS_PTP_STATUS_PTP_MESS_ID_X(val); |
| 483 | txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val); |
| 484 | |
| 485 | /* Retrieve its associated skb */ |
| 486 | port = ocelot->ports[txport]; |
| 487 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 488 | spin_lock_irqsave(&port->tx_skbs.lock, flags); |
| 489 | |
| 490 | skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) { |
| 491 | if (skb->cb[0] != id) |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 492 | continue; |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 493 | __skb_unlink(skb, &port->tx_skbs); |
| 494 | skb_match = skb; |
Yangbo Lu | fc62c09 | 2019-11-27 15:27:56 +0800 | [diff] [blame] | 495 | break; |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 496 | } |
| 497 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 498 | spin_unlock_irqrestore(&port->tx_skbs.lock, flags); |
| 499 | |
laurent brando | 5fd8220 | 2020-07-27 18:26:14 +0800 | [diff] [blame] | 500 | /* Get the h/w timestamp */ |
| 501 | ocelot_get_hwtimestamp(ocelot, &ts); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 502 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 503 | if (unlikely(!skb_match)) |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 504 | continue; |
| 505 | |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 506 | /* Set the timestamp into the skb */ |
| 507 | memset(&shhwtstamps, 0, sizeof(shhwtstamps)); |
| 508 | shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec); |
Vladimir Oltean | e2f9a8f | 2020-09-23 14:24:20 +0300 | [diff] [blame] | 509 | skb_complete_tx_timestamp(skb_match, &shhwtstamps); |
laurent brando | 5fd8220 | 2020-07-27 18:26:14 +0800 | [diff] [blame] | 510 | |
| 511 | /* Next ts */ |
| 512 | ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT); |
Yangbo Lu | e23a7b3 | 2019-11-20 16:23:15 +0800 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | EXPORT_SYMBOL(ocelot_get_txtstamp); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 516 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 517 | int ocelot_fdb_add(struct ocelot *ocelot, int port, |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 518 | const unsigned char *addr, u16 vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 519 | { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 520 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 521 | int pgid = port; |
| 522 | |
| 523 | if (port == ocelot->npi) |
| 524 | pgid = PGID_CPU; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 525 | |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 526 | if (!vid) { |
Vladimir Oltean | 87b0f98 | 2020-04-14 22:36:15 +0300 | [diff] [blame] | 527 | if (!ocelot_port->vlan_aware) |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 528 | /* If the bridge is not VLAN aware and no VID was |
| 529 | * provided, set it to pvid to ensure the MAC entry |
| 530 | * matches incoming untagged packets |
| 531 | */ |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 532 | vid = ocelot_port->pvid; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 533 | else |
| 534 | /* If the bridge is VLAN aware a VID must be provided as |
| 535 | * otherwise the learnt entry wouldn't match any frame. |
| 536 | */ |
| 537 | return -EINVAL; |
| 538 | } |
| 539 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 540 | return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 541 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 542 | EXPORT_SYMBOL(ocelot_fdb_add); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 543 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 544 | int ocelot_fdb_del(struct ocelot *ocelot, int port, |
| 545 | const unsigned char *addr, u16 vid) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 546 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 547 | return ocelot_mact_forget(ocelot, addr, vid); |
| 548 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 549 | EXPORT_SYMBOL(ocelot_fdb_del); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 550 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 551 | int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid, |
| 552 | bool is_static, void *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 553 | { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 554 | struct ocelot_dump_ctx *dump = data; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 555 | u32 portid = NETLINK_CB(dump->cb->skb).portid; |
| 556 | u32 seq = dump->cb->nlh->nlmsg_seq; |
| 557 | struct nlmsghdr *nlh; |
| 558 | struct ndmsg *ndm; |
| 559 | |
| 560 | if (dump->idx < dump->cb->args[2]) |
| 561 | goto skip; |
| 562 | |
| 563 | nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, |
| 564 | sizeof(*ndm), NLM_F_MULTI); |
| 565 | if (!nlh) |
| 566 | return -EMSGSIZE; |
| 567 | |
| 568 | ndm = nlmsg_data(nlh); |
| 569 | ndm->ndm_family = AF_BRIDGE; |
| 570 | ndm->ndm_pad1 = 0; |
| 571 | ndm->ndm_pad2 = 0; |
| 572 | ndm->ndm_flags = NTF_SELF; |
| 573 | ndm->ndm_type = 0; |
| 574 | ndm->ndm_ifindex = dump->dev->ifindex; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 575 | ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 576 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 577 | if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr)) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 578 | goto nla_put_failure; |
| 579 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 580 | if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid)) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 581 | goto nla_put_failure; |
| 582 | |
| 583 | nlmsg_end(dump->skb, nlh); |
| 584 | |
| 585 | skip: |
| 586 | dump->idx++; |
| 587 | return 0; |
| 588 | |
| 589 | nla_put_failure: |
| 590 | nlmsg_cancel(dump->skb, nlh); |
| 591 | return -EMSGSIZE; |
| 592 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 593 | EXPORT_SYMBOL(ocelot_port_fdb_do_dump); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 594 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 595 | static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col, |
| 596 | struct ocelot_mact_entry *entry) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 597 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 598 | u32 val, dst, macl, mach; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 599 | char mac[ETH_ALEN]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 600 | |
| 601 | /* Set row and column to read from */ |
| 602 | ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row); |
| 603 | ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col); |
| 604 | |
| 605 | /* Issue a read command */ |
| 606 | ocelot_write(ocelot, |
| 607 | ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ), |
| 608 | ANA_TABLES_MACACCESS); |
| 609 | |
| 610 | if (ocelot_mact_wait_for_completion(ocelot)) |
| 611 | return -ETIMEDOUT; |
| 612 | |
| 613 | /* Read the entry flags */ |
| 614 | val = ocelot_read(ocelot, ANA_TABLES_MACACCESS); |
| 615 | if (!(val & ANA_TABLES_MACACCESS_VALID)) |
| 616 | return -EINVAL; |
| 617 | |
| 618 | /* If the entry read has another port configured as its destination, |
| 619 | * do not report it. |
| 620 | */ |
| 621 | dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3; |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 622 | if (dst != port) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 623 | return -EINVAL; |
| 624 | |
| 625 | /* Get the entry's MAC address and VLAN id */ |
| 626 | macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA); |
| 627 | mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA); |
| 628 | |
| 629 | mac[0] = (mach >> 8) & 0xff; |
| 630 | mac[1] = (mach >> 0) & 0xff; |
| 631 | mac[2] = (macl >> 24) & 0xff; |
| 632 | mac[3] = (macl >> 16) & 0xff; |
| 633 | mac[4] = (macl >> 8) & 0xff; |
| 634 | mac[5] = (macl >> 0) & 0xff; |
| 635 | |
| 636 | entry->vid = (mach >> 16) & 0xfff; |
| 637 | ether_addr_copy(entry->mac, mac); |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 642 | int ocelot_fdb_dump(struct ocelot *ocelot, int port, |
| 643 | dsa_fdb_dump_cb_t *cb, void *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 644 | { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 645 | int i, j; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 646 | |
Vladimir Oltean | 21ce7f3 | 2020-05-04 01:20:26 +0300 | [diff] [blame] | 647 | /* Loop through all the mac tables entries. */ |
| 648 | for (i = 0; i < ocelot->num_mact_rows; i++) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 649 | for (j = 0; j < 4; j++) { |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 650 | struct ocelot_mact_entry entry; |
| 651 | bool is_static; |
| 652 | int ret; |
| 653 | |
| 654 | ret = ocelot_mact_read(ocelot, port, i, j, &entry); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 655 | /* If the entry is invalid (wrong port, invalid...), |
| 656 | * skip it. |
| 657 | */ |
| 658 | if (ret == -EINVAL) |
| 659 | continue; |
| 660 | else if (ret) |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 661 | return ret; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 662 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 663 | is_static = (entry.type == ENTRYTYPE_LOCKED); |
| 664 | |
| 665 | ret = cb(entry.mac, entry.vid, is_static, data); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 666 | if (ret) |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 667 | return ret; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 671 | return 0; |
| 672 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 673 | EXPORT_SYMBOL(ocelot_fdb_dump); |
Vladimir Oltean | 531ee1a | 2019-11-09 15:02:49 +0200 | [diff] [blame] | 674 | |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 675 | int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 676 | { |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 677 | return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config, |
| 678 | sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0; |
| 679 | } |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 680 | EXPORT_SYMBOL(ocelot_hwstamp_get); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 681 | |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 682 | int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr) |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 683 | { |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 684 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 685 | struct hwtstamp_config cfg; |
| 686 | |
| 687 | if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) |
| 688 | return -EFAULT; |
| 689 | |
| 690 | /* reserved for future extensions */ |
| 691 | if (cfg.flags) |
| 692 | return -EINVAL; |
| 693 | |
| 694 | /* Tx type sanity check */ |
| 695 | switch (cfg.tx_type) { |
| 696 | case HWTSTAMP_TX_ON: |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 697 | ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 698 | break; |
| 699 | case HWTSTAMP_TX_ONESTEP_SYNC: |
| 700 | /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we |
| 701 | * need to update the origin time. |
| 702 | */ |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 703 | ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 704 | break; |
| 705 | case HWTSTAMP_TX_OFF: |
Vladimir Oltean | 306fd44 | 2019-11-09 15:02:50 +0200 | [diff] [blame] | 706 | ocelot_port->ptp_cmd = 0; |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 707 | break; |
| 708 | default: |
| 709 | return -ERANGE; |
| 710 | } |
| 711 | |
| 712 | mutex_lock(&ocelot->ptp_lock); |
| 713 | |
| 714 | switch (cfg.rx_filter) { |
| 715 | case HWTSTAMP_FILTER_NONE: |
| 716 | break; |
| 717 | case HWTSTAMP_FILTER_ALL: |
| 718 | case HWTSTAMP_FILTER_SOME: |
| 719 | case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: |
| 720 | case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: |
| 721 | case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: |
| 722 | case HWTSTAMP_FILTER_NTP_ALL: |
| 723 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: |
| 724 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: |
| 725 | case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: |
| 726 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: |
| 727 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: |
| 728 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: |
| 729 | case HWTSTAMP_FILTER_PTP_V2_EVENT: |
| 730 | case HWTSTAMP_FILTER_PTP_V2_SYNC: |
| 731 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: |
| 732 | cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; |
| 733 | break; |
| 734 | default: |
| 735 | mutex_unlock(&ocelot->ptp_lock); |
| 736 | return -ERANGE; |
| 737 | } |
| 738 | |
| 739 | /* Commit back the result & save it */ |
| 740 | memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg)); |
| 741 | mutex_unlock(&ocelot->ptp_lock); |
| 742 | |
| 743 | return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; |
| 744 | } |
Yangbo Lu | f145922 | 2019-11-20 16:23:14 +0800 | [diff] [blame] | 745 | EXPORT_SYMBOL(ocelot_hwstamp_set); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 746 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 747 | 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] | 748 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 749 | int i; |
| 750 | |
| 751 | if (sset != ETH_SS_STATS) |
| 752 | return; |
| 753 | |
| 754 | for (i = 0; i < ocelot->num_stats; i++) |
| 755 | memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name, |
| 756 | ETH_GSTRING_LEN); |
| 757 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 758 | EXPORT_SYMBOL(ocelot_get_strings); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 759 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 760 | static void ocelot_update_stats(struct ocelot *ocelot) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 761 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 762 | int i, j; |
| 763 | |
| 764 | mutex_lock(&ocelot->stats_lock); |
| 765 | |
| 766 | for (i = 0; i < ocelot->num_phys_ports; i++) { |
| 767 | /* Configure the port to read the stats from */ |
| 768 | ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG); |
| 769 | |
| 770 | for (j = 0; j < ocelot->num_stats; j++) { |
| 771 | u32 val; |
| 772 | unsigned int idx = i * ocelot->num_stats + j; |
| 773 | |
| 774 | val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS, |
| 775 | ocelot->stats_layout[j].offset); |
| 776 | |
| 777 | if (val < (ocelot->stats[idx] & U32_MAX)) |
| 778 | ocelot->stats[idx] += (u64)1 << 32; |
| 779 | |
| 780 | ocelot->stats[idx] = (ocelot->stats[idx] & |
| 781 | ~(u64)U32_MAX) + val; |
| 782 | } |
| 783 | } |
| 784 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 785 | mutex_unlock(&ocelot->stats_lock); |
| 786 | } |
| 787 | |
| 788 | static void ocelot_check_stats_work(struct work_struct *work) |
| 789 | { |
| 790 | struct delayed_work *del_work = to_delayed_work(work); |
| 791 | struct ocelot *ocelot = container_of(del_work, struct ocelot, |
| 792 | stats_work); |
| 793 | |
| 794 | ocelot_update_stats(ocelot); |
| 795 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 796 | queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, |
| 797 | OCELOT_STATS_CHECK_DELAY); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 800 | void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 801 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 802 | int i; |
| 803 | |
| 804 | /* check and update now */ |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 805 | ocelot_update_stats(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 806 | |
| 807 | /* Copy all counters */ |
| 808 | for (i = 0; i < ocelot->num_stats; i++) |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 809 | *data++ = ocelot->stats[port * ocelot->num_stats + i]; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 810 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 811 | EXPORT_SYMBOL(ocelot_get_ethtool_stats); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 812 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 813 | int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset) |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 814 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 815 | if (sset != ETH_SS_STATS) |
| 816 | return -EOPNOTSUPP; |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 817 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 818 | return ocelot->num_stats; |
| 819 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 820 | EXPORT_SYMBOL(ocelot_get_sset_count); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 821 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 822 | int ocelot_get_ts_info(struct ocelot *ocelot, int port, |
| 823 | struct ethtool_ts_info *info) |
Vladimir Oltean | c7282d3 | 2019-11-09 15:02:54 +0200 | [diff] [blame] | 824 | { |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 825 | info->phc_index = ocelot->ptp_clock ? |
| 826 | ptp_clock_index(ocelot->ptp_clock) : -1; |
Yangbo Lu | d2b09a8 | 2020-04-20 10:46:46 +0800 | [diff] [blame] | 827 | if (info->phc_index == -1) { |
| 828 | info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | |
| 829 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 830 | SOF_TIMESTAMPING_SOFTWARE; |
| 831 | return 0; |
| 832 | } |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 833 | info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE | |
| 834 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 835 | SOF_TIMESTAMPING_SOFTWARE | |
| 836 | SOF_TIMESTAMPING_TX_HARDWARE | |
| 837 | SOF_TIMESTAMPING_RX_HARDWARE | |
| 838 | SOF_TIMESTAMPING_RAW_HARDWARE; |
| 839 | info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) | |
| 840 | BIT(HWTSTAMP_TX_ONESTEP_SYNC); |
| 841 | info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL); |
| 842 | |
| 843 | return 0; |
| 844 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 845 | EXPORT_SYMBOL(ocelot_get_ts_info); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 846 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 847 | 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] | 848 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 849 | u32 port_cfg; |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 850 | int p, i; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 851 | |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 852 | if (!(BIT(port) & ocelot->bridge_mask)) |
| 853 | return; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 854 | |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 855 | port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 856 | |
| 857 | switch (state) { |
| 858 | case BR_STATE_FORWARDING: |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 859 | ocelot->bridge_fwd_mask |= BIT(port); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 860 | fallthrough; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 861 | case BR_STATE_LEARNING: |
| 862 | port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA; |
| 863 | break; |
| 864 | |
| 865 | default: |
| 866 | port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA; |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 867 | ocelot->bridge_fwd_mask &= ~BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 868 | break; |
| 869 | } |
| 870 | |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 871 | ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 872 | |
| 873 | /* Apply FWD mask. The loop is needed to add/remove the current port as |
| 874 | * a source for the other ports. |
| 875 | */ |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 876 | for (p = 0; p < ocelot->num_phys_ports; p++) { |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 877 | if (ocelot->bridge_fwd_mask & BIT(p)) { |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 878 | unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 879 | |
| 880 | for (i = 0; i < ocelot->num_phys_ports; i++) { |
| 881 | unsigned long bond_mask = ocelot->lags[i]; |
| 882 | |
| 883 | if (!bond_mask) |
| 884 | continue; |
| 885 | |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 886 | if (bond_mask & BIT(p)) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 887 | mask &= ~bond_mask; |
| 888 | break; |
| 889 | } |
| 890 | } |
| 891 | |
Vladimir Oltean | c9d2203 | 2019-11-09 15:03:01 +0200 | [diff] [blame] | 892 | ocelot_write_rix(ocelot, mask, |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 893 | ANA_PGID_PGID, PGID_SRC + p); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 894 | } else { |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 895 | ocelot_write_rix(ocelot, 0, |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 896 | ANA_PGID_PGID, PGID_SRC + p); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 897 | } |
| 898 | } |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 899 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 900 | EXPORT_SYMBOL(ocelot_bridge_stp_state_set); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 901 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 902 | void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs) |
Vladimir Oltean | 4bda141 | 2019-11-09 15:02:51 +0200 | [diff] [blame] | 903 | { |
Vladimir Oltean | c0d7ecc | 2020-05-04 01:20:27 +0300 | [diff] [blame] | 904 | unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000); |
| 905 | |
| 906 | /* Setting AGE_PERIOD to zero effectively disables automatic aging, |
| 907 | * which is clearly not what our intention is. So avoid that. |
| 908 | */ |
| 909 | if (!age_period) |
| 910 | age_period = 1; |
| 911 | |
| 912 | ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 913 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 914 | EXPORT_SYMBOL(ocelot_set_ageing_time); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 915 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 916 | static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot, |
| 917 | const unsigned char *addr, |
| 918 | u16 vid) |
| 919 | { |
| 920 | struct ocelot_multicast *mc; |
| 921 | |
| 922 | list_for_each_entry(mc, &ocelot->multicast, list) { |
| 923 | if (ether_addr_equal(mc->addr, addr) && mc->vid == vid) |
| 924 | return mc; |
| 925 | } |
| 926 | |
| 927 | return NULL; |
| 928 | } |
| 929 | |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 930 | static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr) |
| 931 | { |
| 932 | if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e) |
| 933 | return ENTRYTYPE_MACv4; |
| 934 | if (addr[0] == 0x33 && addr[1] == 0x33) |
| 935 | return ENTRYTYPE_MACv6; |
| 936 | return ENTRYTYPE_NORMAL; |
| 937 | } |
| 938 | |
| 939 | static int ocelot_mdb_get_pgid(struct ocelot *ocelot, |
| 940 | enum macaccess_entry_type entry_type) |
| 941 | { |
| 942 | int pgid; |
| 943 | |
| 944 | /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and |
| 945 | * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the |
| 946 | * destination mask table (PGID), the destination set is programmed as |
| 947 | * part of the entry MAC address.", and the DEST_IDX is set to 0. |
| 948 | */ |
| 949 | if (entry_type == ENTRYTYPE_MACv4 || |
| 950 | entry_type == ENTRYTYPE_MACv6) |
| 951 | return 0; |
| 952 | |
| 953 | for_each_nonreserved_multicast_dest_pgid(ocelot, pgid) { |
| 954 | struct ocelot_multicast *mc; |
| 955 | bool used = false; |
| 956 | |
| 957 | list_for_each_entry(mc, &ocelot->multicast, list) { |
| 958 | if (mc->pgid == pgid) { |
| 959 | used = true; |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | if (!used) |
| 965 | return pgid; |
| 966 | } |
| 967 | |
| 968 | return -1; |
| 969 | } |
| 970 | |
| 971 | static void ocelot_encode_ports_to_mdb(unsigned char *addr, |
| 972 | struct ocelot_multicast *mc, |
| 973 | enum macaccess_entry_type entry_type) |
| 974 | { |
| 975 | memcpy(addr, mc->addr, ETH_ALEN); |
| 976 | |
| 977 | if (entry_type == ENTRYTYPE_MACv4) { |
| 978 | addr[0] = 0; |
| 979 | addr[1] = mc->ports >> 8; |
| 980 | addr[2] = mc->ports & 0xff; |
| 981 | } else if (entry_type == ENTRYTYPE_MACv6) { |
| 982 | addr[0] = mc->ports >> 8; |
| 983 | addr[1] = mc->ports & 0xff; |
| 984 | } |
| 985 | } |
| 986 | |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 987 | int ocelot_port_mdb_add(struct ocelot *ocelot, int port, |
| 988 | const struct switchdev_obj_port_mdb *mdb) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 989 | { |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 990 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 991 | enum macaccess_entry_type entry_type; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 992 | unsigned char addr[ETH_ALEN]; |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 993 | struct ocelot_multicast *mc; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 994 | u16 vid = mdb->vid; |
| 995 | bool new = false; |
| 996 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 997 | if (port == ocelot->npi) |
| 998 | port = ocelot->num_phys_ports; |
| 999 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1000 | if (!vid) |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1001 | vid = ocelot_port->pvid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1002 | |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1003 | entry_type = ocelot_classify_mdb(mdb->addr); |
| 1004 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1005 | mc = ocelot_multicast_get(ocelot, mdb->addr, vid); |
| 1006 | if (!mc) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1007 | int pgid = ocelot_mdb_get_pgid(ocelot, entry_type); |
| 1008 | |
| 1009 | if (pgid < 0) { |
| 1010 | dev_err(ocelot->dev, |
| 1011 | "No more PGIDs available for mdb %pM vid %d\n", |
| 1012 | mdb->addr, vid); |
| 1013 | return -ENOSPC; |
| 1014 | } |
| 1015 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1016 | mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL); |
| 1017 | if (!mc) |
| 1018 | return -ENOMEM; |
| 1019 | |
| 1020 | memcpy(mc->addr, mdb->addr, ETH_ALEN); |
| 1021 | mc->vid = vid; |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1022 | mc->pgid = pgid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1023 | |
| 1024 | list_add_tail(&mc->list, &ocelot->multicast); |
| 1025 | new = true; |
| 1026 | } |
| 1027 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1028 | if (!new) { |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1029 | ocelot_encode_ports_to_mdb(addr, mc, entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1030 | ocelot_mact_forget(ocelot, addr, vid); |
| 1031 | } |
| 1032 | |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1033 | mc->ports |= BIT(port); |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1034 | ocelot_encode_ports_to_mdb(addr, mc, entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1035 | |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1036 | return ocelot_mact_learn(ocelot, mc->pgid, addr, vid, entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1037 | } |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1038 | EXPORT_SYMBOL(ocelot_port_mdb_add); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1039 | |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1040 | int ocelot_port_mdb_del(struct ocelot *ocelot, int port, |
| 1041 | const struct switchdev_obj_port_mdb *mdb) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1042 | { |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1043 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1044 | enum macaccess_entry_type entry_type; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1045 | unsigned char addr[ETH_ALEN]; |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1046 | struct ocelot_multicast *mc; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1047 | u16 vid = mdb->vid; |
| 1048 | |
Vladimir Oltean | 471beb1 | 2020-06-21 14:46:00 +0300 | [diff] [blame] | 1049 | if (port == ocelot->npi) |
| 1050 | port = ocelot->num_phys_ports; |
| 1051 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1052 | if (!vid) |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1053 | vid = ocelot_port->pvid; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1054 | |
| 1055 | mc = ocelot_multicast_get(ocelot, mdb->addr, vid); |
| 1056 | if (!mc) |
| 1057 | return -ENOENT; |
| 1058 | |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1059 | entry_type = ocelot_classify_mdb(mdb->addr); |
| 1060 | |
| 1061 | ocelot_encode_ports_to_mdb(addr, mc, entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1062 | ocelot_mact_forget(ocelot, addr, vid); |
| 1063 | |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1064 | mc->ports &= ~BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1065 | if (!mc->ports) { |
| 1066 | list_del(&mc->list); |
| 1067 | devm_kfree(ocelot->dev, mc); |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1071 | ocelot_encode_ports_to_mdb(addr, mc, entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1072 | |
Vladimir Oltean | 9403c15 | 2020-06-21 14:46:03 +0300 | [diff] [blame] | 1073 | return ocelot_mact_learn(ocelot, mc->pgid, addr, vid, entry_type); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1074 | } |
Vladimir Oltean | 209edf9 | 2020-06-21 14:46:01 +0300 | [diff] [blame] | 1075 | EXPORT_SYMBOL(ocelot_port_mdb_del); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1076 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1077 | int ocelot_port_bridge_join(struct ocelot *ocelot, int port, |
| 1078 | struct net_device *bridge) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1079 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1080 | if (!ocelot->bridge_mask) { |
| 1081 | ocelot->hw_bridge_dev = bridge; |
| 1082 | } else { |
| 1083 | if (ocelot->hw_bridge_dev != bridge) |
| 1084 | /* This is adding the port to a second bridge, this is |
| 1085 | * unsupported */ |
| 1086 | return -ENODEV; |
| 1087 | } |
| 1088 | |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1089 | ocelot->bridge_mask |= BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1090 | |
| 1091 | return 0; |
| 1092 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1093 | EXPORT_SYMBOL(ocelot_port_bridge_join); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1094 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1095 | int ocelot_port_bridge_leave(struct ocelot *ocelot, int port, |
| 1096 | struct net_device *bridge) |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1097 | { |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 1098 | ocelot->bridge_mask &= ~BIT(port); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1099 | |
| 1100 | if (!ocelot->bridge_mask) |
| 1101 | ocelot->hw_bridge_dev = NULL; |
Antoine Tenart | 7142529 | 2018-06-26 14:28:49 +0200 | [diff] [blame] | 1102 | |
Vladimir Oltean | 97bb69e | 2019-11-09 15:02:47 +0200 | [diff] [blame] | 1103 | ocelot_port_vlan_filtering(ocelot, port, 0); |
| 1104 | ocelot_port_set_pvid(ocelot, port, 0); |
| 1105 | return ocelot_port_set_native_vlan(ocelot, port, 0); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1106 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1107 | EXPORT_SYMBOL(ocelot_port_bridge_leave); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1108 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1109 | static void ocelot_set_aggr_pgids(struct ocelot *ocelot) |
| 1110 | { |
| 1111 | int i, port, lag; |
| 1112 | |
| 1113 | /* Reset destination and aggregation PGIDS */ |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1114 | for_each_unicast_dest_pgid(ocelot, port) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1115 | ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); |
| 1116 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1117 | for_each_aggr_pgid(ocelot, i) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1118 | ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0), |
| 1119 | ANA_PGID_PGID, i); |
| 1120 | |
| 1121 | /* Now, set PGIDs for each LAG */ |
| 1122 | for (lag = 0; lag < ocelot->num_phys_ports; lag++) { |
| 1123 | unsigned long bond_mask; |
| 1124 | int aggr_count = 0; |
| 1125 | u8 aggr_idx[16]; |
| 1126 | |
| 1127 | bond_mask = ocelot->lags[lag]; |
| 1128 | if (!bond_mask) |
| 1129 | continue; |
| 1130 | |
| 1131 | for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) { |
| 1132 | // Destination mask |
| 1133 | ocelot_write_rix(ocelot, bond_mask, |
| 1134 | ANA_PGID_PGID, port); |
| 1135 | aggr_idx[aggr_count] = port; |
| 1136 | aggr_count++; |
| 1137 | } |
| 1138 | |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1139 | for_each_aggr_pgid(ocelot, i) { |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1140 | u32 ac; |
| 1141 | |
| 1142 | ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i); |
| 1143 | ac &= ~bond_mask; |
| 1144 | ac |= BIT(aggr_idx[i % aggr_count]); |
| 1145 | ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i); |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | static void ocelot_setup_lag(struct ocelot *ocelot, int lag) |
| 1151 | { |
| 1152 | unsigned long bond_mask = ocelot->lags[lag]; |
| 1153 | unsigned int p; |
| 1154 | |
| 1155 | for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) { |
| 1156 | u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p); |
| 1157 | |
| 1158 | port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M; |
| 1159 | |
| 1160 | /* Use lag port as logical port for port i */ |
| 1161 | ocelot_write_gix(ocelot, port_cfg | |
| 1162 | ANA_PORT_PORT_CFG_PORTID_VAL(lag), |
| 1163 | ANA_PORT_PORT_CFG, p); |
| 1164 | } |
| 1165 | } |
| 1166 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1167 | int ocelot_port_lag_join(struct ocelot *ocelot, int port, |
| 1168 | struct net_device *bond) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1169 | { |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1170 | struct net_device *ndev; |
| 1171 | u32 bond_mask = 0; |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1172 | int lag, lp; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1173 | |
| 1174 | rcu_read_lock(); |
| 1175 | for_each_netdev_in_bond_rcu(bond, ndev) { |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1176 | struct ocelot_port_private *priv = netdev_priv(ndev); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1177 | |
Vladimir Oltean | 004d44f | 2019-11-09 15:02:53 +0200 | [diff] [blame] | 1178 | bond_mask |= BIT(priv->chip_port); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1179 | } |
| 1180 | rcu_read_unlock(); |
| 1181 | |
| 1182 | lp = __ffs(bond_mask); |
| 1183 | |
| 1184 | /* If the new port is the lowest one, use it as the logical port from |
| 1185 | * now on |
| 1186 | */ |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1187 | if (port == lp) { |
| 1188 | lag = port; |
| 1189 | ocelot->lags[port] = bond_mask; |
| 1190 | bond_mask &= ~BIT(port); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1191 | if (bond_mask) { |
| 1192 | lp = __ffs(bond_mask); |
| 1193 | ocelot->lags[lp] = 0; |
| 1194 | } |
| 1195 | } else { |
| 1196 | lag = lp; |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1197 | ocelot->lags[lp] |= BIT(port); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | ocelot_setup_lag(ocelot, lag); |
| 1201 | ocelot_set_aggr_pgids(ocelot); |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1205 | EXPORT_SYMBOL(ocelot_port_lag_join); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1206 | |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1207 | void ocelot_port_lag_leave(struct ocelot *ocelot, int port, |
| 1208 | struct net_device *bond) |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1209 | { |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1210 | u32 port_cfg; |
| 1211 | int i; |
| 1212 | |
| 1213 | /* Remove port from any lag */ |
| 1214 | for (i = 0; i < ocelot->num_phys_ports; i++) |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1215 | ocelot->lags[i] &= ~BIT(port); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1216 | |
| 1217 | /* if it was the logical port of the lag, move the lag config to the |
| 1218 | * next port |
| 1219 | */ |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1220 | if (ocelot->lags[port]) { |
| 1221 | int n = __ffs(ocelot->lags[port]); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1222 | |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1223 | ocelot->lags[n] = ocelot->lags[port]; |
| 1224 | ocelot->lags[port] = 0; |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1225 | |
| 1226 | ocelot_setup_lag(ocelot, n); |
| 1227 | } |
| 1228 | |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1229 | port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1230 | port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M; |
Vladimir Oltean | f270dbf | 2019-11-09 15:02:52 +0200 | [diff] [blame] | 1231 | ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port), |
| 1232 | ANA_PORT_PORT_CFG, port); |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1233 | |
| 1234 | ocelot_set_aggr_pgids(ocelot); |
| 1235 | } |
Vladimir Oltean | 9c90eea | 2020-06-20 18:43:44 +0300 | [diff] [blame] | 1236 | EXPORT_SYMBOL(ocelot_port_lag_leave); |
Petr Machata | 0e332c8 | 2018-11-22 23:30:11 +0000 | [diff] [blame] | 1237 | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1238 | /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu. |
| 1239 | * 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] | 1240 | * In the special case that it's the NPI port that we're configuring, the |
| 1241 | * length of the tag and optional prefix needs to be accounted for privately, |
| 1242 | * in order to be able to sustain communication at the requested @sdu. |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1243 | */ |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1244 | 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] | 1245 | { |
| 1246 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1247 | int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN; |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 1248 | int pause_start, pause_stop; |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 1249 | int atop_wm; |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 1250 | |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1251 | if (port == ocelot->npi) { |
| 1252 | maxlen += OCELOT_TAG_LEN; |
| 1253 | |
| 1254 | if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT) |
| 1255 | maxlen += OCELOT_SHORT_PREFIX_LEN; |
| 1256 | else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG) |
| 1257 | maxlen += OCELOT_LONG_PREFIX_LEN; |
| 1258 | } |
| 1259 | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1260 | ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1261 | |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 1262 | /* Set Pause watermark hysteresis */ |
| 1263 | pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ; |
| 1264 | pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ; |
Maxim Kochetkov | 541132f | 2020-07-13 19:57:07 +0300 | [diff] [blame] | 1265 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START, |
| 1266 | pause_start); |
| 1267 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP, |
| 1268 | pause_stop); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1269 | |
| 1270 | /* Tail dropping watermark */ |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1271 | atop_wm = (ocelot->shared_queue_sz - 9 * maxlen) / |
| 1272 | OCELOT_BUFFER_CELL_SZ; |
Maxim Kochetkov | aa92d83 | 2020-07-13 19:57:08 +0300 | [diff] [blame] | 1273 | ocelot_write_rix(ocelot, ocelot->ops->wm_enc(9 * maxlen), |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1274 | SYS_ATOP, port); |
Maxim Kochetkov | aa92d83 | 2020-07-13 19:57:08 +0300 | [diff] [blame] | 1275 | ocelot_write(ocelot, ocelot->ops->wm_enc(atop_wm), SYS_ATOP_TOT_CFG); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1276 | } |
Vladimir Oltean | 0b912fc | 2020-03-27 21:55:47 +0200 | [diff] [blame] | 1277 | EXPORT_SYMBOL(ocelot_port_set_maxlen); |
| 1278 | |
| 1279 | int ocelot_get_max_mtu(struct ocelot *ocelot, int port) |
| 1280 | { |
| 1281 | int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN; |
| 1282 | |
| 1283 | if (port == ocelot->npi) { |
| 1284 | max_mtu -= OCELOT_TAG_LEN; |
| 1285 | |
| 1286 | if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT) |
| 1287 | max_mtu -= OCELOT_SHORT_PREFIX_LEN; |
| 1288 | else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG) |
| 1289 | max_mtu -= OCELOT_LONG_PREFIX_LEN; |
| 1290 | } |
| 1291 | |
| 1292 | return max_mtu; |
| 1293 | } |
| 1294 | EXPORT_SYMBOL(ocelot_get_max_mtu); |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1295 | |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1296 | void ocelot_init_port(struct ocelot *ocelot, int port) |
Vladimir Oltean | fa914e9 | 2019-11-14 17:03:23 +0200 | [diff] [blame] | 1297 | { |
| 1298 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1299 | |
Yangbo Lu | b049da1 | 2019-11-27 15:27:57 +0800 | [diff] [blame] | 1300 | skb_queue_head_init(&ocelot_port->tx_skbs); |
Vladimir Oltean | 6565243 | 2020-09-18 04:07:24 +0300 | [diff] [blame] | 1301 | spin_lock_init(&ocelot_port->ts_id_lock); |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 1302 | |
| 1303 | /* Basic L2 initialization */ |
| 1304 | |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 1305 | /* Set MAC IFG Gaps |
| 1306 | * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0 |
| 1307 | * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5 |
| 1308 | */ |
| 1309 | ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5), |
| 1310 | DEV_MAC_IFG_CFG); |
| 1311 | |
| 1312 | /* Load seed (0) and set MAC HDX late collision */ |
| 1313 | ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) | |
| 1314 | DEV_MAC_HDX_CFG_SEED_LOAD, |
| 1315 | DEV_MAC_HDX_CFG); |
| 1316 | mdelay(1); |
| 1317 | ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67), |
| 1318 | DEV_MAC_HDX_CFG); |
| 1319 | |
| 1320 | /* Set Max Length and maximum tags allowed */ |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1321 | ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN); |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 1322 | ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) | |
| 1323 | DEV_MAC_TAGS_CFG_VLAN_AWR_ENA | |
Vladimir Oltean | a8015de | 2020-03-10 03:28:18 +0200 | [diff] [blame] | 1324 | DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA | |
Vladimir Oltean | 5bc9d2e | 2019-11-14 17:03:22 +0200 | [diff] [blame] | 1325 | DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA, |
| 1326 | DEV_MAC_TAGS_CFG); |
| 1327 | |
| 1328 | /* Set SMAC of Pause frame (00:00:00:00:00:00) */ |
| 1329 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG); |
| 1330 | ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG); |
| 1331 | |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 1332 | /* Enable transmission of pause frames */ |
Maxim Kochetkov | 541132f | 2020-07-13 19:57:07 +0300 | [diff] [blame] | 1333 | ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); |
Vladimir Oltean | e8e6e73 | 2020-07-13 19:57:05 +0300 | [diff] [blame] | 1334 | |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 1335 | /* Drop frames with multicast source address */ |
| 1336 | ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, |
| 1337 | ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA, |
| 1338 | ANA_PORT_DROP_CFG, port); |
| 1339 | |
| 1340 | /* Set default VLAN and tag type to 8021Q. */ |
| 1341 | ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q), |
| 1342 | REW_PORT_VLAN_CFG_PORT_TPID_M, |
| 1343 | REW_PORT_VLAN_CFG, port); |
| 1344 | |
| 1345 | /* Enable vcap lookups */ |
| 1346 | ocelot_vcap_enable(ocelot, port); |
| 1347 | } |
Vladimir Oltean | 5e25636 | 2019-11-14 17:03:27 +0200 | [diff] [blame] | 1348 | EXPORT_SYMBOL(ocelot_init_port); |
Vladimir Oltean | 31350d7 | 2019-11-09 15:02:56 +0200 | [diff] [blame] | 1349 | |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 1350 | /* Configure and enable the CPU port module, which is a set of queues |
| 1351 | * accessible through register MMIO, frame DMA or Ethernet (in case |
| 1352 | * NPI mode is used). |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1353 | */ |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 1354 | static void ocelot_cpu_port_init(struct ocelot *ocelot) |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1355 | { |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1356 | int cpu = ocelot->num_phys_ports; |
| 1357 | |
| 1358 | /* The unicast destination PGID for the CPU port module is unused */ |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1359 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu); |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1360 | /* Instead set up a multicast destination PGID for traffic copied to |
| 1361 | * the CPU. Whitelisted MAC addresses like the port netdevice MAC |
| 1362 | * addresses will be copied to the CPU via this PGID. |
| 1363 | */ |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1364 | ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU); |
| 1365 | ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA | |
| 1366 | ANA_PORT_PORT_CFG_PORTID_VAL(cpu), |
| 1367 | ANA_PORT_PORT_CFG, cpu); |
| 1368 | |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1369 | /* Enable CPU port module */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 1370 | ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); |
Vladimir Oltean | 69df578 | 2020-02-29 16:50:02 +0200 | [diff] [blame] | 1371 | /* CPU port Injection/Extraction configuration */ |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 1372 | ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR, |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 1373 | ocelot->xtr_prefix); |
Vladimir Oltean | 886e138 | 2020-07-13 19:57:03 +0300 | [diff] [blame] | 1374 | ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR, |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 1375 | ocelot->inj_prefix); |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1376 | |
| 1377 | /* Configure the CPU port to be VLAN aware */ |
| 1378 | ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) | |
| 1379 | ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA | |
| 1380 | ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1), |
| 1381 | ANA_PORT_VLAN_CFG, cpu); |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1382 | } |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1383 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1384 | int ocelot_init(struct ocelot *ocelot) |
| 1385 | { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1386 | char queue_name[32]; |
Vladimir Oltean | 2146819 | 2019-11-09 15:03:00 +0200 | [diff] [blame] | 1387 | int i, ret; |
| 1388 | u32 port; |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1389 | |
Vladimir Oltean | 3a77b59 | 2019-11-14 17:03:26 +0200 | [diff] [blame] | 1390 | if (ocelot->ops->reset) { |
| 1391 | ret = ocelot->ops->reset(ocelot); |
| 1392 | if (ret) { |
| 1393 | dev_err(ocelot->dev, "Switch reset failed\n"); |
| 1394 | return ret; |
| 1395 | } |
| 1396 | } |
| 1397 | |
Alexandre Belloni | dc96ee3 | 2018-06-26 14:28:48 +0200 | [diff] [blame] | 1398 | ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports, |
| 1399 | sizeof(u32), GFP_KERNEL); |
| 1400 | if (!ocelot->lags) |
| 1401 | return -ENOMEM; |
| 1402 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1403 | ocelot->stats = devm_kcalloc(ocelot->dev, |
| 1404 | ocelot->num_phys_ports * ocelot->num_stats, |
| 1405 | sizeof(u64), GFP_KERNEL); |
| 1406 | if (!ocelot->stats) |
| 1407 | return -ENOMEM; |
| 1408 | |
| 1409 | mutex_init(&ocelot->stats_lock); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1410 | mutex_init(&ocelot->ptp_lock); |
| 1411 | spin_lock_init(&ocelot->ptp_clock_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1412 | snprintf(queue_name, sizeof(queue_name), "%s-stats", |
| 1413 | dev_name(ocelot->dev)); |
| 1414 | ocelot->stats_queue = create_singlethread_workqueue(queue_name); |
| 1415 | if (!ocelot->stats_queue) |
| 1416 | return -ENOMEM; |
| 1417 | |
Claudiu Manoil | 2b120dd | 2019-11-09 15:02:58 +0200 | [diff] [blame] | 1418 | INIT_LIST_HEAD(&ocelot->multicast); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1419 | ocelot_mact_init(ocelot); |
| 1420 | ocelot_vlan_init(ocelot); |
Vladimir Oltean | aae4e50 | 2020-06-20 18:43:46 +0300 | [diff] [blame] | 1421 | ocelot_vcap_init(ocelot); |
Vladimir Oltean | 2d44b09 | 2020-09-26 22:32:01 +0300 | [diff] [blame] | 1422 | ocelot_cpu_port_init(ocelot); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1423 | |
| 1424 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1425 | /* Clear all counters (5 groups) */ |
| 1426 | ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) | |
| 1427 | SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f), |
| 1428 | SYS_STAT_CFG); |
| 1429 | } |
| 1430 | |
| 1431 | /* Only use S-Tag */ |
| 1432 | ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG); |
| 1433 | |
| 1434 | /* Aggregation mode */ |
| 1435 | ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA | |
| 1436 | ANA_AGGR_CFG_AC_DMAC_ENA | |
| 1437 | ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA | |
| 1438 | ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG); |
| 1439 | |
| 1440 | /* Set MAC age time to default value. The entry is aged after |
| 1441 | * 2*AGE_PERIOD |
| 1442 | */ |
| 1443 | ocelot_write(ocelot, |
| 1444 | ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ), |
| 1445 | ANA_AUTOAGE); |
| 1446 | |
| 1447 | /* Disable learning for frames discarded by VLAN ingress filtering */ |
| 1448 | regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1); |
| 1449 | |
| 1450 | /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */ |
| 1451 | ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA | |
| 1452 | SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING); |
| 1453 | |
| 1454 | /* Setup flooding PGIDs */ |
| 1455 | ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | |
| 1456 | ANA_FLOODING_FLD_BROADCAST(PGID_MC) | |
| 1457 | ANA_FLOODING_FLD_UNICAST(PGID_UC), |
| 1458 | ANA_FLOODING, 0); |
| 1459 | ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) | |
| 1460 | ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) | |
| 1461 | ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) | |
| 1462 | ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC), |
| 1463 | ANA_FLOODING_IPMC); |
| 1464 | |
| 1465 | for (port = 0; port < ocelot->num_phys_ports; port++) { |
| 1466 | /* Transmit the frame to the local port. */ |
| 1467 | ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port); |
| 1468 | /* Do not forward BPDU frames to the front ports. */ |
| 1469 | ocelot_write_gix(ocelot, |
| 1470 | ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), |
| 1471 | ANA_PORT_CPU_FWD_BPDU_CFG, |
| 1472 | port); |
| 1473 | /* Ensure bridging is disabled */ |
| 1474 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port); |
| 1475 | } |
| 1476 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1477 | /* Allow broadcast MAC frames. */ |
Vladimir Oltean | 96b029b | 2020-06-21 14:46:02 +0300 | [diff] [blame] | 1478 | for_each_nonreserved_multicast_dest_pgid(ocelot, i) { |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1479 | u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0)); |
| 1480 | |
| 1481 | ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); |
| 1482 | } |
| 1483 | ocelot_write_rix(ocelot, |
| 1484 | ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)), |
| 1485 | ANA_PGID_PGID, PGID_MC); |
| 1486 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4); |
| 1487 | ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6); |
| 1488 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1489 | /* Allow manual injection via DEVCPU_QS registers, and byte swap these |
| 1490 | * registers endianness. |
| 1491 | */ |
| 1492 | ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP | |
| 1493 | QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0); |
| 1494 | ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP | |
| 1495 | QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0); |
| 1496 | ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) | |
| 1497 | ANA_CPUQ_CFG_CPUQ_LRN(2) | |
| 1498 | ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) | |
| 1499 | ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) | |
| 1500 | ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) | |
| 1501 | ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) | |
| 1502 | ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) | |
| 1503 | ANA_CPUQ_CFG_CPUQ_IGMP(6) | |
| 1504 | ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG); |
| 1505 | for (i = 0; i < 16; i++) |
| 1506 | ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) | |
| 1507 | ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6), |
| 1508 | ANA_CPUQ_8021_CFG, i); |
| 1509 | |
Claudiu Manoil | 1e1caa9 | 2019-04-16 17:51:59 +0300 | [diff] [blame] | 1510 | INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1511 | queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work, |
| 1512 | OCELOT_STATS_CHECK_DELAY); |
Antoine Tenart | 4e3b046 | 2019-08-12 16:45:37 +0200 | [diff] [blame] | 1513 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1514 | return 0; |
| 1515 | } |
| 1516 | EXPORT_SYMBOL(ocelot_init); |
| 1517 | |
| 1518 | void ocelot_deinit(struct ocelot *ocelot) |
| 1519 | { |
Claudiu Manoil | c5d1396 | 2019-07-25 16:33:18 +0300 | [diff] [blame] | 1520 | cancel_delayed_work(&ocelot->stats_work); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1521 | destroy_workqueue(ocelot->stats_queue); |
| 1522 | mutex_destroy(&ocelot->stats_lock); |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1523 | } |
| 1524 | EXPORT_SYMBOL(ocelot_deinit); |
| 1525 | |
Vladimir Oltean | e5fb512 | 2020-09-18 04:07:30 +0300 | [diff] [blame] | 1526 | void ocelot_deinit_port(struct ocelot *ocelot, int port) |
| 1527 | { |
| 1528 | struct ocelot_port *ocelot_port = ocelot->ports[port]; |
| 1529 | |
| 1530 | skb_queue_purge(&ocelot_port->tx_skbs); |
| 1531 | } |
| 1532 | EXPORT_SYMBOL(ocelot_deinit_port); |
| 1533 | |
Alexandre Belloni | a556c76 | 2018-05-14 22:04:57 +0200 | [diff] [blame] | 1534 | MODULE_LICENSE("Dual MIT/GPL"); |