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