blob: 380a5a661702883d527e5c24918cb21ca2eb3b2f [file] [log] [blame]
Alexandre Bellonia556c762018-05-14 22:04:57 +02001// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/*
3 * Microsemi Ocelot Switch driver
4 *
5 * Copyright (c) 2017 Microsemi Corporation
6 */
Alexandre Bellonia556c762018-05-14 22:04:57 +02007#include <linux/if_bridge.h>
Vladimir Oltean20968052020-09-30 01:27:26 +03008#include <soc/mscc/ocelot_vcap.h>
Alexandre Bellonia556c762018-05-14 22:04:57 +02009#include "ocelot.h"
Vladimir Oltean3c836542020-06-20 18:43:45 +030010#include "ocelot_vcap.h"
Alexandre Bellonia556c762018-05-14 22:04:57 +020011
Steen Hegelund639c1b22018-12-20 14:16:31 +010012#define TABLE_UPDATE_SLEEP_US 10
13#define TABLE_UPDATE_TIMEOUT_US 100000
14
Alexandre Bellonia556c762018-05-14 22:04:57 +020015struct ocelot_mact_entry {
16 u8 mac[ETH_ALEN];
17 u16 vid;
18 enum macaccess_entry_type type;
19};
20
Steen Hegelund639c1b22018-12-20 14:16:31 +010021static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
22{
23 return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
24}
25
Alexandre Bellonia556c762018-05-14 22:04:57 +020026static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
27{
Steen Hegelund639c1b22018-12-20 14:16:31 +010028 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +020029
Steen Hegelund639c1b22018-12-20 14:16:31 +010030 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 Bellonia556c762018-05-14 22:04:57 +020035}
36
37static 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 Oltean9c90eea2020-06-20 18:43:44 +030059int 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 Bellonia556c762018-05-14 22:04:57 +020062{
Alban Bedel584b7cf2021-01-19 15:06:38 +010063 u32 cmd = ANA_TABLES_MACACCESS_VALID |
64 ANA_TABLES_MACACCESS_DEST_IDX(port) |
65 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
66 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
67 unsigned int mc_ports;
68
69 /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
70 if (type == ENTRYTYPE_MACv4)
71 mc_ports = (mac[1] << 8) | mac[2];
72 else if (type == ENTRYTYPE_MACv6)
73 mc_ports = (mac[0] << 8) | mac[1];
74 else
75 mc_ports = 0;
76
77 if (mc_ports & BIT(ocelot->num_phys_ports))
78 cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
79
Alexandre Bellonia556c762018-05-14 22:04:57 +020080 ocelot_mact_select(ocelot, mac, vid);
81
82 /* Issue a write command */
Alban Bedel584b7cf2021-01-19 15:06:38 +010083 ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
Alexandre Bellonia556c762018-05-14 22:04:57 +020084
85 return ocelot_mact_wait_for_completion(ocelot);
86}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030087EXPORT_SYMBOL(ocelot_mact_learn);
Alexandre Bellonia556c762018-05-14 22:04:57 +020088
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030089int ocelot_mact_forget(struct ocelot *ocelot,
90 const unsigned char mac[ETH_ALEN], unsigned int vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +020091{
92 ocelot_mact_select(ocelot, mac, vid);
93
94 /* Issue a forget command */
95 ocelot_write(ocelot,
96 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
97 ANA_TABLES_MACACCESS);
98
99 return ocelot_mact_wait_for_completion(ocelot);
100}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300101EXPORT_SYMBOL(ocelot_mact_forget);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200102
103static void ocelot_mact_init(struct ocelot *ocelot)
104{
105 /* Configure the learning mode entries attributes:
106 * - Do not copy the frame to the CPU extraction queues.
107 * - Use the vlan and mac_cpoy for dmac lookup.
108 */
109 ocelot_rmw(ocelot, 0,
110 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
111 | ANA_AGENCTRL_LEARN_FWD_KILL
112 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
113 ANA_AGENCTRL);
114
115 /* Clear the MAC table */
116 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
117}
118
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200119static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
Horatiu Vulturb5962292019-05-31 09:16:56 +0200120{
121 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
122 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200123 ANA_PORT_VCAP_S2_CFG, port);
Xiaoliang Yang75944fd2020-10-02 15:02:23 +0300124
125 ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
126 ANA_PORT_VCAP_CFG, port);
Xiaoliang Yang2f17c052020-10-02 15:02:24 +0300127
128 ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
129 REW_PORT_CFG_ES0_EN,
130 REW_PORT_CFG, port);
Horatiu Vulturb5962292019-05-31 09:16:56 +0200131}
132
Steen Hegelund639c1b22018-12-20 14:16:31 +0100133static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
134{
135 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
136}
137
Alexandre Bellonia556c762018-05-14 22:04:57 +0200138static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
139{
Steen Hegelund639c1b22018-12-20 14:16:31 +0100140 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200141
Steen Hegelund639c1b22018-12-20 14:16:31 +0100142 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
143 ocelot,
144 val,
145 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
146 ANA_TABLES_VLANACCESS_CMD_IDLE,
147 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200148}
149
Antoine Tenart71425292018-06-26 14:28:49 +0200150static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
151{
152 /* Select the VID to configure */
153 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
154 ANA_TABLES_VLANTIDX);
155 /* Set the vlan port members mask and issue a write command */
156 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
157 ANA_TABLES_VLANACCESS_CMD_WRITE,
158 ANA_TABLES_VLANACCESS);
159
160 return ocelot_vlant_wait_for_completion(ocelot);
161}
162
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200163static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
164 struct ocelot_vlan native_vlan)
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200165{
166 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300167 u32 val = 0;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200168
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200169 ocelot_port->native_vlan = native_vlan;
170
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200171 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
Antoine Tenart71425292018-06-26 14:28:49 +0200172 REW_PORT_VLAN_CFG_PORT_VID_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200173 REW_PORT_VLAN_CFG, port);
174
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300175 if (ocelot_port->vlan_aware) {
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200176 if (native_vlan.valid)
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300177 /* Tag all frames except when VID == DEFAULT_VLAN */
178 val = REW_TAG_CFG_TAG_CFG(1);
179 else
180 /* Tag all frames */
181 val = REW_TAG_CFG_TAG_CFG(3);
182 } else {
183 /* Port tagging disabled. */
184 val = REW_TAG_CFG_TAG_CFG(0);
185 }
186 ocelot_rmw_gix(ocelot, val,
187 REW_TAG_CFG_TAG_CFG_M,
188 REW_TAG_CFG, port);
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200189}
190
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200191/* Default vlan to clasify for untagged frames (may be zero) */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200192static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
193 struct ocelot_vlan pvid_vlan)
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200194{
195 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200196 u32 val = 0;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200197
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200198 ocelot_port->pvid_vlan = pvid_vlan;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200199
200 if (!ocelot_port->vlan_aware)
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200201 pvid_vlan.vid = 0;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200202
203 ocelot_rmw_gix(ocelot,
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200204 ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200205 ANA_PORT_VLAN_CFG_VLAN_VID_M,
206 ANA_PORT_VLAN_CFG, port);
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200207
208 /* If there's no pvid, we should drop not only untagged traffic (which
209 * happens automatically), but also 802.1p traffic which gets
210 * classified to VLAN 0, but that is always in our RX filter, so it
211 * would get accepted were it not for this setting.
212 */
213 if (!pvid_vlan.valid && ocelot_port->vlan_aware)
214 val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
215 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
216
217 ocelot_rmw_gix(ocelot, val,
218 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
219 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
220 ANA_PORT_DROP_CFG, port);
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200221}
222
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300223int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200224 bool vlan_aware)
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300225{
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200226 struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300227 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200228 struct ocelot_vcap_filter *filter;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300229 u32 val;
230
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200231 list_for_each_entry(filter, &block->rules, list) {
232 if (filter->ingress_port_mask & BIT(port) &&
233 filter->action.vid_replace_ena) {
234 dev_err(ocelot->dev,
235 "Cannot change VLAN state with vlan modify rules active\n");
236 return -EBUSY;
Vladimir Oltean70edfae2020-10-08 14:56:58 +0300237 }
Vladimir Oltean70edfae2020-10-08 14:56:58 +0300238 }
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300239
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300240 ocelot_port->vlan_aware = vlan_aware;
241
242 if (vlan_aware)
243 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
244 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
245 else
246 val = 0;
247 ocelot_rmw_gix(ocelot, val,
248 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
249 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
250 ANA_PORT_VLAN_CFG, port);
251
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200252 ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
253 ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300254
255 return 0;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300256}
257EXPORT_SYMBOL(ocelot_port_vlan_filtering);
258
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200259int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
260 bool untagged)
261{
262 struct ocelot_port *ocelot_port = ocelot->ports[port];
263
264 /* Deny changing the native VLAN, but always permit deleting it */
265 if (untagged && ocelot_port->native_vlan.vid != vid &&
266 ocelot_port->native_vlan.valid) {
267 dev_err(ocelot->dev,
268 "Port already has a native VLAN: %d\n",
269 ocelot_port->native_vlan.vid);
270 return -EBUSY;
271 }
272
273 return 0;
274}
275EXPORT_SYMBOL(ocelot_vlan_prepare);
276
Vladimir Oltean5e256362019-11-14 17:03:27 +0200277int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
278 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200279{
Antoine Tenart71425292018-06-26 14:28:49 +0200280 int ret;
281
Antoine Tenart71425292018-06-26 14:28:49 +0200282 /* Make the port a member of the VLAN */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200283 ocelot->vlan_mask[vid] |= BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200284 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
285 if (ret)
286 return ret;
287
288 /* Default ingress vlan classification */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200289 if (pvid) {
290 struct ocelot_vlan pvid_vlan;
291
292 pvid_vlan.vid = vid;
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200293 pvid_vlan.valid = true;
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200294 ocelot_port_set_pvid(ocelot, port, pvid_vlan);
295 }
Antoine Tenart71425292018-06-26 14:28:49 +0200296
297 /* Untagged egress vlan clasification */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200298 if (untagged) {
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200299 struct ocelot_vlan native_vlan;
300
301 native_vlan.vid = vid;
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200302 native_vlan.valid = true;
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200303 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
Vladimir Olteanb9cd75e2019-10-26 21:04:27 +0300304 }
Antoine Tenart71425292018-06-26 14:28:49 +0200305
Antoine Tenart71425292018-06-26 14:28:49 +0200306 return 0;
307}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200308EXPORT_SYMBOL(ocelot_vlan_add);
Antoine Tenart71425292018-06-26 14:28:49 +0200309
Vladimir Oltean5e256362019-11-14 17:03:27 +0200310int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
Vladimir Oltean98559342019-11-09 15:02:48 +0200311{
312 struct ocelot_port *ocelot_port = ocelot->ports[port];
313 int ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200314
315 /* Stop the port from being a member of the vlan */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200316 ocelot->vlan_mask[vid] &= ~BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200317 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
318 if (ret)
319 return ret;
320
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200321 /* Ingress */
322 if (ocelot_port->pvid_vlan.vid == vid) {
323 struct ocelot_vlan pvid_vlan = {0};
324
325 ocelot_port_set_pvid(ocelot, port, pvid_vlan);
326 }
327
Antoine Tenart71425292018-06-26 14:28:49 +0200328 /* Egress */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200329 if (ocelot_port->native_vlan.vid == vid) {
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200330 struct ocelot_vlan native_vlan = {0};
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200331
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200332 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
333 }
Antoine Tenart71425292018-06-26 14:28:49 +0200334
335 return 0;
336}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200337EXPORT_SYMBOL(ocelot_vlan_del);
Antoine Tenart71425292018-06-26 14:28:49 +0200338
Alexandre Bellonia556c762018-05-14 22:04:57 +0200339static void ocelot_vlan_init(struct ocelot *ocelot)
340{
Antoine Tenart71425292018-06-26 14:28:49 +0200341 u16 port, vid;
342
Alexandre Bellonia556c762018-05-14 22:04:57 +0200343 /* Clear VLAN table, by default all ports are members of all VLANs */
344 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
345 ANA_TABLES_VLANACCESS);
346 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200347
348 /* Configure the port VLAN memberships */
349 for (vid = 1; vid < VLAN_N_VID; vid++) {
350 ocelot->vlan_mask[vid] = 0;
351 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
352 }
353
354 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
355 * traffic. It is added automatically if 8021q module is loaded, but
356 * we can't rely on it since module may be not loaded.
357 */
358 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
359 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
360
Antoine Tenart71425292018-06-26 14:28:49 +0200361 /* Set vlan ingress filter mask to all ports but the CPU port by
362 * default.
363 */
Vladimir Oltean714d0ff2019-11-09 15:02:55 +0200364 ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
365 ANA_VLANMASK);
Antoine Tenart71425292018-06-26 14:28:49 +0200366
367 for (port = 0; port < ocelot->num_phys_ports; port++) {
368 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
369 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
370 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200371}
372
Vladimir Oltean5e256362019-11-14 17:03:27 +0200373void ocelot_adjust_link(struct ocelot *ocelot, int port,
374 struct phy_device *phydev)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200375{
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200376 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +0200377 int speed, mode = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200378
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200379 switch (phydev->speed) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200380 case SPEED_10:
381 speed = OCELOT_SPEED_10;
382 break;
383 case SPEED_100:
384 speed = OCELOT_SPEED_100;
385 break;
386 case SPEED_1000:
387 speed = OCELOT_SPEED_1000;
388 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
389 break;
390 case SPEED_2500:
391 speed = OCELOT_SPEED_2500;
392 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
393 break;
394 default:
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200395 dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
396 port, phydev->speed);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200397 return;
398 }
399
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200400 phy_print_status(phydev);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200401
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200402 if (!phydev->link)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200403 return;
404
405 /* Only full duplex supported for now */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200406 ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200407 mode, DEV_MAC_MODE_CFG);
408
Vladimir Oltean1ba8f652020-02-29 16:31:11 +0200409 /* Disable HDX fast control */
410 ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
411 DEV_PORT_MISC);
412
413 /* SGMII only for now */
414 ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
415 PCS1G_MODE_CFG);
416 ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
417
418 /* Enable PCS */
419 ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
420
421 /* No aneg on SGMII */
422 ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
423
424 /* No loopback */
425 ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200426
Alexandre Bellonia556c762018-05-14 22:04:57 +0200427 /* Enable MAC module */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200428 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200429 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
430
431 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
432 * reset */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200433 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
Alexandre Bellonia556c762018-05-14 22:04:57 +0200434 DEV_CLOCK_CFG);
435
Alexandre Bellonia556c762018-05-14 22:04:57 +0200436 /* No PFC */
437 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200438 ANA_PFC_PFC_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200439
Alexandre Bellonia556c762018-05-14 22:04:57 +0200440 /* Core: Enable port for frame transfer */
Vladimir Oltean886e1382020-07-13 19:57:03 +0300441 ocelot_fields_write(ocelot, port,
442 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200443
444 /* Flow control */
445 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
446 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
447 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
448 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
449 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200450 SYS_MAC_FC_CFG, port);
451 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200452}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200453EXPORT_SYMBOL(ocelot_adjust_link);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200454
Vladimir Oltean5e256362019-11-14 17:03:27 +0200455void ocelot_port_enable(struct ocelot *ocelot, int port,
456 struct phy_device *phy)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200457{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200458 /* Enable receiving frames on the port, and activate auto-learning of
459 * MAC addresses.
460 */
461 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
462 ANA_PORT_PORT_CFG_RECV_ENA |
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200463 ANA_PORT_PORT_CFG_PORTID_VAL(port),
464 ANA_PORT_PORT_CFG, port);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200465}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200466EXPORT_SYMBOL(ocelot_port_enable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200467
Vladimir Oltean5e256362019-11-14 17:03:27 +0200468void ocelot_port_disable(struct ocelot *ocelot, int port)
Vladimir Oltean889b8952019-11-09 15:02:57 +0200469{
470 struct ocelot_port *ocelot_port = ocelot->ports[port];
471
472 ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
Vladimir Oltean886e1382020-07-13 19:57:03 +0300473 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200474}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200475EXPORT_SYMBOL(ocelot_port_disable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200476
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300477void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
478 struct sk_buff *clone)
Yangbo Lu400928b2019-11-20 16:23:16 +0800479{
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300480 struct ocelot_port *ocelot_port = ocelot->ports[port];
Yangbo Lu400928b2019-11-20 16:23:16 +0800481
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300482 spin_lock(&ocelot_port->ts_id_lock);
Vladimir Oltean65652432020-09-18 04:07:24 +0300483
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300484 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
485 /* Store timestamp ID in cb[0] of sk_buff */
486 clone->cb[0] = ocelot_port->ts_id;
487 ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
488 skb_queue_tail(&ocelot_port->tx_skbs, clone);
Vladimir Oltean65652432020-09-18 04:07:24 +0300489
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300490 spin_unlock(&ocelot_port->ts_id_lock);
Yangbo Lu400928b2019-11-20 16:23:16 +0800491}
492EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
493
Yangbo Lue23a7b32019-11-20 16:23:15 +0800494static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
495 struct timespec64 *ts)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200496{
497 unsigned long flags;
498 u32 val;
499
500 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
501
502 /* Read current PTP time to get seconds */
503 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
504
505 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
506 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
507 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
508 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
509
510 /* Read packet HW timestamp from FIFO */
511 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
512 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
513
514 /* Sec has incremented since the ts was registered */
515 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
516 ts->tv_sec--;
517
518 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
519}
Yangbo Lue23a7b32019-11-20 16:23:15 +0800520
521void ocelot_get_txtstamp(struct ocelot *ocelot)
522{
523 int budget = OCELOT_PTP_QUEUE_SZ;
524
525 while (budget--) {
Yangbo Lub049da12019-11-27 15:27:57 +0800526 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800527 struct skb_shared_hwtstamps shhwtstamps;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800528 struct ocelot_port *port;
529 struct timespec64 ts;
Yangbo Lub049da12019-11-27 15:27:57 +0800530 unsigned long flags;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800531 u32 val, id, txport;
532
533 val = ocelot_read(ocelot, SYS_PTP_STATUS);
534
535 /* Check if a timestamp can be retrieved */
536 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
537 break;
538
539 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
540
541 /* Retrieve the ts ID and Tx port */
542 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
543 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
544
545 /* Retrieve its associated skb */
546 port = ocelot->ports[txport];
547
Yangbo Lub049da12019-11-27 15:27:57 +0800548 spin_lock_irqsave(&port->tx_skbs.lock, flags);
549
550 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
551 if (skb->cb[0] != id)
Yangbo Lue23a7b32019-11-20 16:23:15 +0800552 continue;
Yangbo Lub049da12019-11-27 15:27:57 +0800553 __skb_unlink(skb, &port->tx_skbs);
554 skb_match = skb;
Yangbo Lufc62c092019-11-27 15:27:56 +0800555 break;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800556 }
557
Yangbo Lub049da12019-11-27 15:27:57 +0800558 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
559
laurent brando5fd82202020-07-27 18:26:14 +0800560 /* Get the h/w timestamp */
561 ocelot_get_hwtimestamp(ocelot, &ts);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800562
Yangbo Lub049da12019-11-27 15:27:57 +0800563 if (unlikely(!skb_match))
Yangbo Lue23a7b32019-11-20 16:23:15 +0800564 continue;
565
Yangbo Lue23a7b32019-11-20 16:23:15 +0800566 /* Set the timestamp into the skb */
567 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
568 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300569 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
laurent brando5fd82202020-07-27 18:26:14 +0800570
571 /* Next ts */
572 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800573 }
574}
575EXPORT_SYMBOL(ocelot_get_txtstamp);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200576
Vladimir Oltean5e256362019-11-14 17:03:27 +0200577int ocelot_fdb_add(struct ocelot *ocelot, int port,
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300578 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200579{
Vladimir Oltean471beb12020-06-21 14:46:00 +0300580 int pgid = port;
581
582 if (port == ocelot->npi)
583 pgid = PGID_CPU;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200584
Vladimir Oltean471beb12020-06-21 14:46:00 +0300585 return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200586}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200587EXPORT_SYMBOL(ocelot_fdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200588
Vladimir Oltean5e256362019-11-14 17:03:27 +0200589int ocelot_fdb_del(struct ocelot *ocelot, int port,
590 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200591{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200592 return ocelot_mact_forget(ocelot, addr, vid);
593}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200594EXPORT_SYMBOL(ocelot_fdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200595
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300596int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
597 bool is_static, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200598{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200599 struct ocelot_dump_ctx *dump = data;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200600 u32 portid = NETLINK_CB(dump->cb->skb).portid;
601 u32 seq = dump->cb->nlh->nlmsg_seq;
602 struct nlmsghdr *nlh;
603 struct ndmsg *ndm;
604
605 if (dump->idx < dump->cb->args[2])
606 goto skip;
607
608 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
609 sizeof(*ndm), NLM_F_MULTI);
610 if (!nlh)
611 return -EMSGSIZE;
612
613 ndm = nlmsg_data(nlh);
614 ndm->ndm_family = AF_BRIDGE;
615 ndm->ndm_pad1 = 0;
616 ndm->ndm_pad2 = 0;
617 ndm->ndm_flags = NTF_SELF;
618 ndm->ndm_type = 0;
619 ndm->ndm_ifindex = dump->dev->ifindex;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200620 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200621
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200622 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200623 goto nla_put_failure;
624
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200625 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200626 goto nla_put_failure;
627
628 nlmsg_end(dump->skb, nlh);
629
630skip:
631 dump->idx++;
632 return 0;
633
634nla_put_failure:
635 nlmsg_cancel(dump->skb, nlh);
636 return -EMSGSIZE;
637}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300638EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200639
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200640static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
641 struct ocelot_mact_entry *entry)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200642{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200643 u32 val, dst, macl, mach;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200644 char mac[ETH_ALEN];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200645
646 /* Set row and column to read from */
647 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
648 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
649
650 /* Issue a read command */
651 ocelot_write(ocelot,
652 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
653 ANA_TABLES_MACACCESS);
654
655 if (ocelot_mact_wait_for_completion(ocelot))
656 return -ETIMEDOUT;
657
658 /* Read the entry flags */
659 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
660 if (!(val & ANA_TABLES_MACACCESS_VALID))
661 return -EINVAL;
662
663 /* If the entry read has another port configured as its destination,
664 * do not report it.
665 */
666 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200667 if (dst != port)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200668 return -EINVAL;
669
670 /* Get the entry's MAC address and VLAN id */
671 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
672 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
673
674 mac[0] = (mach >> 8) & 0xff;
675 mac[1] = (mach >> 0) & 0xff;
676 mac[2] = (macl >> 24) & 0xff;
677 mac[3] = (macl >> 16) & 0xff;
678 mac[4] = (macl >> 8) & 0xff;
679 mac[5] = (macl >> 0) & 0xff;
680
681 entry->vid = (mach >> 16) & 0xfff;
682 ether_addr_copy(entry->mac, mac);
683
684 return 0;
685}
686
Vladimir Oltean5e256362019-11-14 17:03:27 +0200687int ocelot_fdb_dump(struct ocelot *ocelot, int port,
688 dsa_fdb_dump_cb_t *cb, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200689{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200690 int i, j;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200691
Vladimir Oltean21ce7f32020-05-04 01:20:26 +0300692 /* Loop through all the mac tables entries. */
693 for (i = 0; i < ocelot->num_mact_rows; i++) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200694 for (j = 0; j < 4; j++) {
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200695 struct ocelot_mact_entry entry;
696 bool is_static;
697 int ret;
698
699 ret = ocelot_mact_read(ocelot, port, i, j, &entry);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200700 /* If the entry is invalid (wrong port, invalid...),
701 * skip it.
702 */
703 if (ret == -EINVAL)
704 continue;
705 else if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200706 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200707
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200708 is_static = (entry.type == ENTRYTYPE_LOCKED);
709
710 ret = cb(entry.mac, entry.vid, is_static, data);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200711 if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200712 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200713 }
714 }
715
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200716 return 0;
717}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200718EXPORT_SYMBOL(ocelot_fdb_dump);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200719
Yangbo Luf1459222019-11-20 16:23:14 +0800720int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200721{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200722 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
723 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
724}
Yangbo Luf1459222019-11-20 16:23:14 +0800725EXPORT_SYMBOL(ocelot_hwstamp_get);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200726
Yangbo Luf1459222019-11-20 16:23:14 +0800727int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200728{
Vladimir Oltean306fd442019-11-09 15:02:50 +0200729 struct ocelot_port *ocelot_port = ocelot->ports[port];
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200730 struct hwtstamp_config cfg;
731
732 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
733 return -EFAULT;
734
735 /* reserved for future extensions */
736 if (cfg.flags)
737 return -EINVAL;
738
739 /* Tx type sanity check */
740 switch (cfg.tx_type) {
741 case HWTSTAMP_TX_ON:
Vladimir Oltean306fd442019-11-09 15:02:50 +0200742 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200743 break;
744 case HWTSTAMP_TX_ONESTEP_SYNC:
745 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
746 * need to update the origin time.
747 */
Vladimir Oltean306fd442019-11-09 15:02:50 +0200748 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200749 break;
750 case HWTSTAMP_TX_OFF:
Vladimir Oltean306fd442019-11-09 15:02:50 +0200751 ocelot_port->ptp_cmd = 0;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200752 break;
753 default:
754 return -ERANGE;
755 }
756
757 mutex_lock(&ocelot->ptp_lock);
758
759 switch (cfg.rx_filter) {
760 case HWTSTAMP_FILTER_NONE:
761 break;
762 case HWTSTAMP_FILTER_ALL:
763 case HWTSTAMP_FILTER_SOME:
764 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
765 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
766 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
767 case HWTSTAMP_FILTER_NTP_ALL:
768 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
769 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
770 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
771 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
772 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
773 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
774 case HWTSTAMP_FILTER_PTP_V2_EVENT:
775 case HWTSTAMP_FILTER_PTP_V2_SYNC:
776 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
777 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
778 break;
779 default:
780 mutex_unlock(&ocelot->ptp_lock);
781 return -ERANGE;
782 }
783
784 /* Commit back the result & save it */
785 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
786 mutex_unlock(&ocelot->ptp_lock);
787
788 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
789}
Yangbo Luf1459222019-11-20 16:23:14 +0800790EXPORT_SYMBOL(ocelot_hwstamp_set);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200791
Vladimir Oltean5e256362019-11-14 17:03:27 +0200792void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200793{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200794 int i;
795
796 if (sset != ETH_SS_STATS)
797 return;
798
799 for (i = 0; i < ocelot->num_stats; i++)
800 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
801 ETH_GSTRING_LEN);
802}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200803EXPORT_SYMBOL(ocelot_get_strings);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200804
Claudiu Manoil1e1caa92019-04-16 17:51:59 +0300805static void ocelot_update_stats(struct ocelot *ocelot)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200806{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200807 int i, j;
808
809 mutex_lock(&ocelot->stats_lock);
810
811 for (i = 0; i < ocelot->num_phys_ports; i++) {
812 /* Configure the port to read the stats from */
813 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
814
815 for (j = 0; j < ocelot->num_stats; j++) {
816 u32 val;
817 unsigned int idx = i * ocelot->num_stats + j;
818
819 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
820 ocelot->stats_layout[j].offset);
821
822 if (val < (ocelot->stats[idx] & U32_MAX))
823 ocelot->stats[idx] += (u64)1 << 32;
824
825 ocelot->stats[idx] = (ocelot->stats[idx] &
826 ~(u64)U32_MAX) + val;
827 }
828 }
829
Claudiu Manoil1e1caa92019-04-16 17:51:59 +0300830 mutex_unlock(&ocelot->stats_lock);
831}
832
833static void ocelot_check_stats_work(struct work_struct *work)
834{
835 struct delayed_work *del_work = to_delayed_work(work);
836 struct ocelot *ocelot = container_of(del_work, struct ocelot,
837 stats_work);
838
839 ocelot_update_stats(ocelot);
840
Alexandre Bellonia556c762018-05-14 22:04:57 +0200841 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
842 OCELOT_STATS_CHECK_DELAY);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200843}
844
Vladimir Oltean5e256362019-11-14 17:03:27 +0200845void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200846{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200847 int i;
848
849 /* check and update now */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +0300850 ocelot_update_stats(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200851
852 /* Copy all counters */
853 for (i = 0; i < ocelot->num_stats; i++)
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200854 *data++ = ocelot->stats[port * ocelot->num_stats + i];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200855}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200856EXPORT_SYMBOL(ocelot_get_ethtool_stats);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200857
Vladimir Oltean5e256362019-11-14 17:03:27 +0200858int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
Vladimir Olteanc7282d32019-11-09 15:02:54 +0200859{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200860 if (sset != ETH_SS_STATS)
861 return -EOPNOTSUPP;
Vladimir Olteanc7282d32019-11-09 15:02:54 +0200862
Alexandre Bellonia556c762018-05-14 22:04:57 +0200863 return ocelot->num_stats;
864}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200865EXPORT_SYMBOL(ocelot_get_sset_count);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200866
Vladimir Oltean5e256362019-11-14 17:03:27 +0200867int ocelot_get_ts_info(struct ocelot *ocelot, int port,
868 struct ethtool_ts_info *info)
Vladimir Olteanc7282d32019-11-09 15:02:54 +0200869{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200870 info->phc_index = ocelot->ptp_clock ?
871 ptp_clock_index(ocelot->ptp_clock) : -1;
Yangbo Lud2b09a82020-04-20 10:46:46 +0800872 if (info->phc_index == -1) {
873 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
874 SOF_TIMESTAMPING_RX_SOFTWARE |
875 SOF_TIMESTAMPING_SOFTWARE;
876 return 0;
877 }
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200878 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
879 SOF_TIMESTAMPING_RX_SOFTWARE |
880 SOF_TIMESTAMPING_SOFTWARE |
881 SOF_TIMESTAMPING_TX_HARDWARE |
882 SOF_TIMESTAMPING_RX_HARDWARE |
883 SOF_TIMESTAMPING_RAW_HARDWARE;
884 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
885 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
886 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
887
888 return 0;
889}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200890EXPORT_SYMBOL(ocelot_get_ts_info);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200891
Vladimir Olteanb80af652021-02-06 00:02:14 +0200892static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
893{
894 u32 mask = 0;
895 int port;
896
897 for (port = 0; port < ocelot->num_phys_ports; port++) {
898 struct ocelot_port *ocelot_port = ocelot->ports[port];
899
900 if (!ocelot_port)
901 continue;
902
903 if (ocelot_port->bond == bond)
904 mask |= BIT(port);
905 }
906
907 return mask;
908}
909
Vladimir Olteane21268e2021-01-29 03:00:09 +0200910static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
Vladimir Oltean9b521252021-01-29 03:00:02 +0200911{
Vladimir Olteane21268e2021-01-29 03:00:09 +0200912 u32 mask = 0;
Vladimir Oltean9b521252021-01-29 03:00:02 +0200913 int port;
914
Vladimir Olteane21268e2021-01-29 03:00:09 +0200915 for (port = 0; port < ocelot->num_phys_ports; port++) {
916 struct ocelot_port *ocelot_port = ocelot->ports[port];
917
918 if (!ocelot_port)
919 continue;
920
921 if (ocelot_port->is_dsa_8021q_cpu)
922 mask |= BIT(port);
923 }
924
925 return mask;
926}
927
928void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
929{
930 unsigned long cpu_fwd_mask;
931 int port;
932
933 /* If a DSA tag_8021q CPU exists, it needs to be included in the
934 * regular forwarding path of the front ports regardless of whether
935 * those are bridged or standalone.
936 * If DSA tag_8021q is not used, this returns 0, which is fine because
937 * the hardware-based CPU port module can be a destination for packets
938 * even if it isn't part of PGID_SRC.
939 */
940 cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
941
Vladimir Oltean9b521252021-01-29 03:00:02 +0200942 /* Apply FWD mask. The loop is needed to add/remove the current port as
943 * a source for the other ports.
944 */
945 for (port = 0; port < ocelot->num_phys_ports; port++) {
Vladimir Olteane21268e2021-01-29 03:00:09 +0200946 struct ocelot_port *ocelot_port = ocelot->ports[port];
947 unsigned long mask;
948
949 if (!ocelot_port) {
950 /* Unused ports can't send anywhere */
951 mask = 0;
952 } else if (ocelot_port->is_dsa_8021q_cpu) {
953 /* The DSA tag_8021q CPU ports need to be able to
954 * forward packets to all other ports except for
955 * themselves
956 */
957 mask = GENMASK(ocelot->num_phys_ports - 1, 0);
958 mask &= ~cpu_fwd_mask;
959 } else if (ocelot->bridge_fwd_mask & BIT(port)) {
Vladimir Oltean528d3f12021-02-06 00:02:17 +0200960 struct net_device *bond = ocelot_port->bond;
Vladimir Oltean9b521252021-01-29 03:00:02 +0200961
Vladimir Olteane21268e2021-01-29 03:00:09 +0200962 mask = ocelot->bridge_fwd_mask & ~BIT(port);
Vladimir Oltean528d3f12021-02-06 00:02:17 +0200963 if (bond)
964 mask &= ~ocelot_get_bond_mask(ocelot, bond);
Vladimir Oltean9b521252021-01-29 03:00:02 +0200965 } else {
Vladimir Olteane21268e2021-01-29 03:00:09 +0200966 /* Standalone ports forward only to DSA tag_8021q CPU
967 * ports (if those exist), or to the hardware CPU port
968 * module otherwise.
969 */
970 mask = cpu_fwd_mask;
Vladimir Oltean9b521252021-01-29 03:00:02 +0200971 }
Vladimir Olteane21268e2021-01-29 03:00:09 +0200972
973 ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
Vladimir Oltean9b521252021-01-29 03:00:02 +0200974 }
975}
Vladimir Olteane21268e2021-01-29 03:00:09 +0200976EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
Vladimir Oltean9b521252021-01-29 03:00:02 +0200977
Vladimir Oltean5e256362019-11-14 17:03:27 +0200978void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200979{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200980 u32 port_cfg;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200981
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200982 if (!(BIT(port) & ocelot->bridge_mask))
983 return;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200984
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200985 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200986
987 switch (state) {
988 case BR_STATE_FORWARDING:
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200989 ocelot->bridge_fwd_mask |= BIT(port);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500990 fallthrough;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200991 case BR_STATE_LEARNING:
992 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
993 break;
994
995 default:
996 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200997 ocelot->bridge_fwd_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200998 break;
999 }
1000
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001001 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001002
Vladimir Oltean9b521252021-01-29 03:00:02 +02001003 ocelot_apply_bridge_fwd_mask(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001004}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001005EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001006
Vladimir Oltean5e256362019-11-14 17:03:27 +02001007void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001008{
Vladimir Olteanc0d7ecc2020-05-04 01:20:27 +03001009 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1010
1011 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
1012 * which is clearly not what our intention is. So avoid that.
1013 */
1014 if (!age_period)
1015 age_period = 1;
1016
1017 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001018}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001019EXPORT_SYMBOL(ocelot_set_ageing_time);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001020
Alexandre Bellonia556c762018-05-14 22:04:57 +02001021static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1022 const unsigned char *addr,
1023 u16 vid)
1024{
1025 struct ocelot_multicast *mc;
1026
1027 list_for_each_entry(mc, &ocelot->multicast, list) {
1028 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1029 return mc;
1030 }
1031
1032 return NULL;
1033}
1034
Vladimir Oltean9403c152020-06-21 14:46:03 +03001035static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
1036{
1037 if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
1038 return ENTRYTYPE_MACv4;
1039 if (addr[0] == 0x33 && addr[1] == 0x33)
1040 return ENTRYTYPE_MACv6;
Vladimir Oltean7c313142020-10-29 04:27:34 +02001041 return ENTRYTYPE_LOCKED;
Vladimir Oltean9403c152020-06-21 14:46:03 +03001042}
1043
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001044static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1045 unsigned long ports)
Vladimir Oltean9403c152020-06-21 14:46:03 +03001046{
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001047 struct ocelot_pgid *pgid;
1048
1049 pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1050 if (!pgid)
1051 return ERR_PTR(-ENOMEM);
1052
1053 pgid->ports = ports;
1054 pgid->index = index;
1055 refcount_set(&pgid->refcount, 1);
1056 list_add_tail(&pgid->list, &ocelot->pgids);
1057
1058 return pgid;
1059}
1060
1061static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1062{
1063 if (!refcount_dec_and_test(&pgid->refcount))
1064 return;
1065
1066 list_del(&pgid->list);
1067 kfree(pgid);
1068}
1069
1070static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1071 const struct ocelot_multicast *mc)
1072{
1073 struct ocelot_pgid *pgid;
1074 int index;
Vladimir Oltean9403c152020-06-21 14:46:03 +03001075
1076 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1077 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1078 * destination mask table (PGID), the destination set is programmed as
1079 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1080 */
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001081 if (mc->entry_type == ENTRYTYPE_MACv4 ||
1082 mc->entry_type == ENTRYTYPE_MACv6)
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001083 return ocelot_pgid_alloc(ocelot, 0, mc->ports);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001084
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001085 list_for_each_entry(pgid, &ocelot->pgids, list) {
1086 /* When searching for a nonreserved multicast PGID, ignore the
1087 * dummy PGID of zero that we have for MACv4/MACv6 entries
1088 */
1089 if (pgid->index && pgid->ports == mc->ports) {
1090 refcount_inc(&pgid->refcount);
1091 return pgid;
1092 }
1093 }
1094
1095 /* Search for a free index in the nonreserved multicast PGID area */
1096 for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001097 bool used = false;
1098
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001099 list_for_each_entry(pgid, &ocelot->pgids, list) {
1100 if (pgid->index == index) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001101 used = true;
1102 break;
1103 }
1104 }
1105
1106 if (!used)
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001107 return ocelot_pgid_alloc(ocelot, index, mc->ports);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001108 }
1109
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001110 return ERR_PTR(-ENOSPC);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001111}
1112
1113static void ocelot_encode_ports_to_mdb(unsigned char *addr,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001114 struct ocelot_multicast *mc)
Vladimir Oltean9403c152020-06-21 14:46:03 +03001115{
Vladimir Olteanebbd8602020-10-29 04:27:35 +02001116 ether_addr_copy(addr, mc->addr);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001117
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001118 if (mc->entry_type == ENTRYTYPE_MACv4) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001119 addr[0] = 0;
1120 addr[1] = mc->ports >> 8;
1121 addr[2] = mc->ports & 0xff;
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001122 } else if (mc->entry_type == ENTRYTYPE_MACv6) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001123 addr[0] = mc->ports >> 8;
1124 addr[1] = mc->ports & 0xff;
1125 }
1126}
1127
Vladimir Oltean209edf92020-06-21 14:46:01 +03001128int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1129 const struct switchdev_obj_port_mdb *mdb)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001130{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001131 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001132 struct ocelot_multicast *mc;
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001133 struct ocelot_pgid *pgid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001134 u16 vid = mdb->vid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001135
Vladimir Oltean471beb12020-06-21 14:46:00 +03001136 if (port == ocelot->npi)
1137 port = ocelot->num_phys_ports;
1138
Alexandre Bellonia556c762018-05-14 22:04:57 +02001139 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1140 if (!mc) {
Vladimir Oltean728e69a2020-10-29 04:27:36 +02001141 /* New entry */
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001142 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1143 if (!mc)
1144 return -ENOMEM;
1145
1146 mc->entry_type = ocelot_classify_mdb(mdb->addr);
1147 ether_addr_copy(mc->addr, mdb->addr);
1148 mc->vid = vid;
1149
Alexandre Bellonia556c762018-05-14 22:04:57 +02001150 list_add_tail(&mc->list, &ocelot->multicast);
Vladimir Oltean728e69a2020-10-29 04:27:36 +02001151 } else {
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001152 /* Existing entry. Clean up the current port mask from
1153 * hardware now, because we'll be modifying it.
1154 */
1155 ocelot_pgid_free(ocelot, mc->pgid);
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001156 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001157 ocelot_mact_forget(ocelot, addr, vid);
1158 }
1159
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001160 mc->ports |= BIT(port);
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001161
1162 pgid = ocelot_mdb_get_pgid(ocelot, mc);
1163 if (IS_ERR(pgid)) {
1164 dev_err(ocelot->dev,
1165 "Cannot allocate PGID for mdb %pM vid %d\n",
1166 mc->addr, mc->vid);
1167 devm_kfree(ocelot->dev, mc);
1168 return PTR_ERR(pgid);
1169 }
1170 mc->pgid = pgid;
1171
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001172 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001173
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001174 if (mc->entry_type != ENTRYTYPE_MACv4 &&
1175 mc->entry_type != ENTRYTYPE_MACv6)
1176 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1177 pgid->index);
1178
1179 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001180 mc->entry_type);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001181}
Vladimir Oltean209edf92020-06-21 14:46:01 +03001182EXPORT_SYMBOL(ocelot_port_mdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001183
Vladimir Oltean209edf92020-06-21 14:46:01 +03001184int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1185 const struct switchdev_obj_port_mdb *mdb)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001186{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001187 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001188 struct ocelot_multicast *mc;
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001189 struct ocelot_pgid *pgid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001190 u16 vid = mdb->vid;
1191
Vladimir Oltean471beb12020-06-21 14:46:00 +03001192 if (port == ocelot->npi)
1193 port = ocelot->num_phys_ports;
1194
Alexandre Bellonia556c762018-05-14 22:04:57 +02001195 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1196 if (!mc)
1197 return -ENOENT;
1198
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001199 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001200 ocelot_mact_forget(ocelot, addr, vid);
1201
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001202 ocelot_pgid_free(ocelot, mc->pgid);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001203 mc->ports &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001204 if (!mc->ports) {
1205 list_del(&mc->list);
1206 devm_kfree(ocelot->dev, mc);
1207 return 0;
1208 }
1209
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001210 /* We have a PGID with fewer ports now */
1211 pgid = ocelot_mdb_get_pgid(ocelot, mc);
1212 if (IS_ERR(pgid))
1213 return PTR_ERR(pgid);
1214 mc->pgid = pgid;
1215
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001216 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001217
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001218 if (mc->entry_type != ENTRYTYPE_MACv4 &&
1219 mc->entry_type != ENTRYTYPE_MACv6)
1220 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1221 pgid->index);
1222
1223 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001224 mc->entry_type);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001225}
Vladimir Oltean209edf92020-06-21 14:46:01 +03001226EXPORT_SYMBOL(ocelot_port_mdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001227
Vladimir Oltean5e256362019-11-14 17:03:27 +02001228int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1229 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001230{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001231 if (!ocelot->bridge_mask) {
1232 ocelot->hw_bridge_dev = bridge;
1233 } else {
1234 if (ocelot->hw_bridge_dev != bridge)
1235 /* This is adding the port to a second bridge, this is
1236 * unsupported */
1237 return -ENODEV;
1238 }
1239
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001240 ocelot->bridge_mask |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001241
1242 return 0;
1243}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001244EXPORT_SYMBOL(ocelot_port_bridge_join);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001245
Vladimir Oltean5e256362019-11-14 17:03:27 +02001246int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1247 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001248{
Vladimir Olteanc3e58a752020-10-31 12:29:12 +02001249 struct ocelot_vlan pvid = {0}, native_vlan = {0};
Vladimir Oltean2e554a72020-10-03 01:06:46 +03001250 int ret;
1251
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001252 ocelot->bridge_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001253
1254 if (!ocelot->bridge_mask)
1255 ocelot->hw_bridge_dev = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02001256
Vladimir Olteanbae33f22021-01-09 02:01:50 +02001257 ret = ocelot_port_vlan_filtering(ocelot, port, false);
Vladimir Oltean2e554a72020-10-03 01:06:46 +03001258 if (ret)
1259 return ret;
1260
Vladimir Olteanc3e58a752020-10-31 12:29:12 +02001261 ocelot_port_set_pvid(ocelot, port, pvid);
Vladimir Oltean2f0402f2020-10-31 12:29:15 +02001262 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
1263
1264 return 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001265}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001266EXPORT_SYMBOL(ocelot_port_bridge_leave);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001267
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001268static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1269{
Vladimir Oltean528d3f12021-02-06 00:02:17 +02001270 unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001271 int i, port, lag;
1272
1273 /* Reset destination and aggregation PGIDS */
Vladimir Oltean96b029b2020-06-21 14:46:02 +03001274 for_each_unicast_dest_pgid(ocelot, port)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001275 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1276
Vladimir Oltean96b029b2020-06-21 14:46:02 +03001277 for_each_aggr_pgid(ocelot, i)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001278 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1279 ANA_PGID_PGID, i);
1280
Vladimir Oltean528d3f12021-02-06 00:02:17 +02001281 /* The visited ports bitmask holds the list of ports offloading any
1282 * bonding interface. Initially we mark all these ports as unvisited,
1283 * then every time we visit a port in this bitmask, we know that it is
1284 * the lowest numbered port, i.e. the one whose logical ID == physical
1285 * port ID == LAG ID. So we mark as visited all further ports in the
1286 * bitmask that are offloading the same bonding interface. This way,
1287 * we set up the aggregation PGIDs only once per bonding interface.
1288 */
1289 for (port = 0; port < ocelot->num_phys_ports; port++) {
1290 struct ocelot_port *ocelot_port = ocelot->ports[port];
1291
1292 if (!ocelot_port || !ocelot_port->bond)
1293 continue;
1294
1295 visited &= ~BIT(port);
1296 }
1297
1298 /* Now, set PGIDs for each active LAG */
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001299 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
Vladimir Oltean528d3f12021-02-06 00:02:17 +02001300 struct net_device *bond = ocelot->ports[lag]->bond;
Vladimir Oltean21357b62021-02-06 00:02:18 +02001301 int num_ports_in_lag = 0;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001302 unsigned long bond_mask;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001303 u8 aggr_idx[16];
1304
Vladimir Oltean528d3f12021-02-06 00:02:17 +02001305 if (!bond || (visited & BIT(lag)))
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001306 continue;
1307
Vladimir Oltean528d3f12021-02-06 00:02:17 +02001308 bond_mask = ocelot_get_bond_mask(ocelot, bond);
1309
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001310 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1311 // Destination mask
1312 ocelot_write_rix(ocelot, bond_mask,
1313 ANA_PGID_PGID, port);
Vladimir Oltean21357b62021-02-06 00:02:18 +02001314 aggr_idx[num_ports_in_lag++] = port;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001315 }
1316
Vladimir Oltean96b029b2020-06-21 14:46:02 +03001317 for_each_aggr_pgid(ocelot, i) {
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001318 u32 ac;
1319
1320 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1321 ac &= ~bond_mask;
Vladimir Oltean21357b62021-02-06 00:02:18 +02001322 ac |= BIT(aggr_idx[i % num_ports_in_lag]);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001323 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1324 }
Vladimir Oltean528d3f12021-02-06 00:02:17 +02001325
1326 /* Mark all ports in the same LAG as visited to avoid applying
1327 * the same config again.
1328 */
1329 for (port = lag; port < ocelot->num_phys_ports; port++) {
1330 struct ocelot_port *ocelot_port = ocelot->ports[port];
1331
1332 if (!ocelot_port)
1333 continue;
1334
1335 if (ocelot_port->bond == bond)
1336 visited |= BIT(port);
1337 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001338 }
1339}
1340
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02001341/* When offloading a bonding interface, the switch ports configured under the
1342 * same bond must have the same logical port ID, equal to the physical port ID
1343 * of the lowest numbered physical port in that bond. Otherwise, in standalone/
1344 * bridged mode, each port has a logical port ID equal to its physical port ID.
1345 */
1346static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001347{
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02001348 int port;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001349
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02001350 for (port = 0; port < ocelot->num_phys_ports; port++) {
1351 struct ocelot_port *ocelot_port = ocelot->ports[port];
1352 struct net_device *bond;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001353
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02001354 if (!ocelot_port)
1355 continue;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001356
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02001357 bond = ocelot_port->bond;
1358 if (bond) {
1359 int lag = __ffs(ocelot_get_bond_mask(ocelot, bond));
1360
1361 ocelot_rmw_gix(ocelot,
1362 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1363 ANA_PORT_PORT_CFG_PORTID_VAL_M,
1364 ANA_PORT_PORT_CFG, port);
1365 } else {
1366 ocelot_rmw_gix(ocelot,
1367 ANA_PORT_PORT_CFG_PORTID_VAL(port),
1368 ANA_PORT_PORT_CFG_PORTID_VAL_M,
1369 ANA_PORT_PORT_CFG, port);
1370 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001371 }
1372}
1373
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001374int ocelot_port_lag_join(struct ocelot *ocelot, int port,
Vladimir Oltean583cbbe2021-02-06 00:02:12 +02001375 struct net_device *bond,
1376 struct netdev_lag_upper_info *info)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001377{
Vladimir Oltean583cbbe2021-02-06 00:02:12 +02001378 if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1379 return -EOPNOTSUPP;
1380
Vladimir Olteanb80af652021-02-06 00:02:14 +02001381 ocelot->ports[port]->bond = bond;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001382
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02001383 ocelot_setup_logical_port_ids(ocelot);
Vladimir Oltean9b521252021-01-29 03:00:02 +02001384 ocelot_apply_bridge_fwd_mask(ocelot);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001385 ocelot_set_aggr_pgids(ocelot);
1386
1387 return 0;
1388}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001389EXPORT_SYMBOL(ocelot_port_lag_join);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001390
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001391void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1392 struct net_device *bond)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001393{
Vladimir Olteanb80af652021-02-06 00:02:14 +02001394 ocelot->ports[port]->bond = NULL;
1395
Vladimir Oltean2527f2e2021-02-06 00:02:16 +02001396 ocelot_setup_logical_port_ids(ocelot);
Vladimir Oltean9b521252021-01-29 03:00:02 +02001397 ocelot_apply_bridge_fwd_mask(ocelot);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001398 ocelot_set_aggr_pgids(ocelot);
1399}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001400EXPORT_SYMBOL(ocelot_port_lag_leave);
Petr Machata0e332c82018-11-22 23:30:11 +00001401
Vladimir Olteana8015de2020-03-10 03:28:18 +02001402/* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1403 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001404 * In the special case that it's the NPI port that we're configuring, the
1405 * length of the tag and optional prefix needs to be accounted for privately,
1406 * in order to be able to sustain communication at the requested @sdu.
Vladimir Olteana8015de2020-03-10 03:28:18 +02001407 */
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001408void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
Vladimir Oltean31350d72019-11-09 15:02:56 +02001409{
1410 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteana8015de2020-03-10 03:28:18 +02001411 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
Vladimir Olteane8e6e732020-07-13 19:57:05 +03001412 int pause_start, pause_stop;
Vladimir Oltean601e9842020-10-05 12:09:11 +03001413 int atop, atop_tot;
Vladimir Oltean31350d72019-11-09 15:02:56 +02001414
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001415 if (port == ocelot->npi) {
1416 maxlen += OCELOT_TAG_LEN;
1417
Vladimir Olteancacea622021-01-29 03:00:03 +02001418 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001419 maxlen += OCELOT_SHORT_PREFIX_LEN;
Vladimir Olteancacea622021-01-29 03:00:03 +02001420 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001421 maxlen += OCELOT_LONG_PREFIX_LEN;
1422 }
1423
Vladimir Olteana8015de2020-03-10 03:28:18 +02001424 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001425
Vladimir Olteane8e6e732020-07-13 19:57:05 +03001426 /* Set Pause watermark hysteresis */
1427 pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1428 pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
Maxim Kochetkov541132f2020-07-13 19:57:07 +03001429 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1430 pause_start);
1431 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1432 pause_stop);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001433
Vladimir Oltean601e9842020-10-05 12:09:11 +03001434 /* Tail dropping watermarks */
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02001435 atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
Vladimir Olteana8015de2020-03-10 03:28:18 +02001436 OCELOT_BUFFER_CELL_SZ;
Vladimir Oltean601e9842020-10-05 12:09:11 +03001437 atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1438 ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1439 ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001440}
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001441EXPORT_SYMBOL(ocelot_port_set_maxlen);
1442
1443int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
1444{
1445 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
1446
1447 if (port == ocelot->npi) {
1448 max_mtu -= OCELOT_TAG_LEN;
1449
Vladimir Olteancacea622021-01-29 03:00:03 +02001450 if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001451 max_mtu -= OCELOT_SHORT_PREFIX_LEN;
Vladimir Olteancacea622021-01-29 03:00:03 +02001452 else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001453 max_mtu -= OCELOT_LONG_PREFIX_LEN;
1454 }
1455
1456 return max_mtu;
1457}
1458EXPORT_SYMBOL(ocelot_get_max_mtu);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001459
Vladimir Oltean5e256362019-11-14 17:03:27 +02001460void ocelot_init_port(struct ocelot *ocelot, int port)
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001461{
1462 struct ocelot_port *ocelot_port = ocelot->ports[port];
1463
Yangbo Lub049da12019-11-27 15:27:57 +08001464 skb_queue_head_init(&ocelot_port->tx_skbs);
Vladimir Oltean65652432020-09-18 04:07:24 +03001465 spin_lock_init(&ocelot_port->ts_id_lock);
Vladimir Oltean31350d72019-11-09 15:02:56 +02001466
1467 /* Basic L2 initialization */
1468
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001469 /* Set MAC IFG Gaps
1470 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1471 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1472 */
1473 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
1474 DEV_MAC_IFG_CFG);
1475
1476 /* Load seed (0) and set MAC HDX late collision */
1477 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1478 DEV_MAC_HDX_CFG_SEED_LOAD,
1479 DEV_MAC_HDX_CFG);
1480 mdelay(1);
1481 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
1482 DEV_MAC_HDX_CFG);
1483
1484 /* Set Max Length and maximum tags allowed */
Vladimir Olteana8015de2020-03-10 03:28:18 +02001485 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001486 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
1487 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
Vladimir Olteana8015de2020-03-10 03:28:18 +02001488 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001489 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
1490 DEV_MAC_TAGS_CFG);
1491
1492 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
1493 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
1494 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
1495
Vladimir Olteane8e6e732020-07-13 19:57:05 +03001496 /* Enable transmission of pause frames */
Maxim Kochetkov541132f2020-07-13 19:57:07 +03001497 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
Vladimir Olteane8e6e732020-07-13 19:57:05 +03001498
Vladimir Oltean31350d72019-11-09 15:02:56 +02001499 /* Drop frames with multicast source address */
1500 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1501 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1502 ANA_PORT_DROP_CFG, port);
1503
1504 /* Set default VLAN and tag type to 8021Q. */
1505 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
1506 REW_PORT_VLAN_CFG_PORT_TPID_M,
1507 REW_PORT_VLAN_CFG, port);
1508
1509 /* Enable vcap lookups */
1510 ocelot_vcap_enable(ocelot, port);
1511}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001512EXPORT_SYMBOL(ocelot_init_port);
Vladimir Oltean31350d72019-11-09 15:02:56 +02001513
Vladimir Oltean2d44b092020-09-26 22:32:01 +03001514/* Configure and enable the CPU port module, which is a set of queues
1515 * accessible through register MMIO, frame DMA or Ethernet (in case
1516 * NPI mode is used).
Vladimir Oltean69df5782020-02-29 16:50:02 +02001517 */
Vladimir Oltean2d44b092020-09-26 22:32:01 +03001518static void ocelot_cpu_port_init(struct ocelot *ocelot)
Vladimir Oltean21468192019-11-09 15:03:00 +02001519{
Vladimir Oltean69df5782020-02-29 16:50:02 +02001520 int cpu = ocelot->num_phys_ports;
1521
1522 /* The unicast destination PGID for the CPU port module is unused */
Vladimir Oltean21468192019-11-09 15:03:00 +02001523 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
Vladimir Oltean69df5782020-02-29 16:50:02 +02001524 /* Instead set up a multicast destination PGID for traffic copied to
1525 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
1526 * addresses will be copied to the CPU via this PGID.
1527 */
Vladimir Oltean21468192019-11-09 15:03:00 +02001528 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1529 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1530 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1531 ANA_PORT_PORT_CFG, cpu);
1532
Vladimir Oltean69df5782020-02-29 16:50:02 +02001533 /* Enable CPU port module */
Vladimir Oltean886e1382020-07-13 19:57:03 +03001534 ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
Vladimir Oltean69df5782020-02-29 16:50:02 +02001535 /* CPU port Injection/Extraction configuration */
Vladimir Oltean886e1382020-07-13 19:57:03 +03001536 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
Vladimir Olteancacea622021-01-29 03:00:03 +02001537 OCELOT_TAG_PREFIX_NONE);
Vladimir Oltean886e1382020-07-13 19:57:03 +03001538 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
Vladimir Olteancacea622021-01-29 03:00:03 +02001539 OCELOT_TAG_PREFIX_NONE);
Vladimir Oltean21468192019-11-09 15:03:00 +02001540
1541 /* Configure the CPU port to be VLAN aware */
1542 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
1543 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1544 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
1545 ANA_PORT_VLAN_CFG, cpu);
Vladimir Oltean21468192019-11-09 15:03:00 +02001546}
Vladimir Oltean21468192019-11-09 15:03:00 +02001547
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02001548static void ocelot_detect_features(struct ocelot *ocelot)
1549{
1550 int mmgt, eq_ctrl;
1551
1552 /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
1553 * the number of 240-byte free memory words (aka 4-cell chunks) and not
1554 * 192 bytes as the documentation incorrectly says.
1555 */
1556 mmgt = ocelot_read(ocelot, SYS_MMGT);
1557 ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
1558
1559 eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
1560 ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02001561}
1562
Alexandre Bellonia556c762018-05-14 22:04:57 +02001563int ocelot_init(struct ocelot *ocelot)
1564{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001565 char queue_name[32];
Vladimir Oltean21468192019-11-09 15:03:00 +02001566 int i, ret;
1567 u32 port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001568
Vladimir Oltean3a77b592019-11-14 17:03:26 +02001569 if (ocelot->ops->reset) {
1570 ret = ocelot->ops->reset(ocelot);
1571 if (ret) {
1572 dev_err(ocelot->dev, "Switch reset failed\n");
1573 return ret;
1574 }
1575 }
1576
Alexandre Bellonia556c762018-05-14 22:04:57 +02001577 ocelot->stats = devm_kcalloc(ocelot->dev,
1578 ocelot->num_phys_ports * ocelot->num_stats,
1579 sizeof(u64), GFP_KERNEL);
1580 if (!ocelot->stats)
1581 return -ENOMEM;
1582
1583 mutex_init(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001584 mutex_init(&ocelot->ptp_lock);
1585 spin_lock_init(&ocelot->ptp_clock_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001586 snprintf(queue_name, sizeof(queue_name), "%s-stats",
1587 dev_name(ocelot->dev));
1588 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1589 if (!ocelot->stats_queue)
1590 return -ENOMEM;
1591
Vladimir Olteanca0b2722020-12-12 21:16:12 +02001592 ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
1593 if (!ocelot->owq) {
1594 destroy_workqueue(ocelot->stats_queue);
1595 return -ENOMEM;
1596 }
1597
Claudiu Manoil2b120dd2019-11-09 15:02:58 +02001598 INIT_LIST_HEAD(&ocelot->multicast);
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001599 INIT_LIST_HEAD(&ocelot->pgids);
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02001600 ocelot_detect_features(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001601 ocelot_mact_init(ocelot);
1602 ocelot_vlan_init(ocelot);
Vladimir Olteanaae4e502020-06-20 18:43:46 +03001603 ocelot_vcap_init(ocelot);
Vladimir Oltean2d44b092020-09-26 22:32:01 +03001604 ocelot_cpu_port_init(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001605
1606 for (port = 0; port < ocelot->num_phys_ports; port++) {
1607 /* Clear all counters (5 groups) */
1608 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1609 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1610 SYS_STAT_CFG);
1611 }
1612
1613 /* Only use S-Tag */
1614 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1615
1616 /* Aggregation mode */
1617 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1618 ANA_AGGR_CFG_AC_DMAC_ENA |
1619 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
Vladimir Olteanf79c20c2021-02-06 00:02:13 +02001620 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
1621 ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
1622 ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
1623 ANA_AGGR_CFG);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001624
1625 /* Set MAC age time to default value. The entry is aged after
1626 * 2*AGE_PERIOD
1627 */
1628 ocelot_write(ocelot,
1629 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1630 ANA_AUTOAGE);
1631
1632 /* Disable learning for frames discarded by VLAN ingress filtering */
1633 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1634
1635 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1636 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1637 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1638
1639 /* Setup flooding PGIDs */
Vladimir Olteanedd24102020-12-04 19:54:16 +02001640 for (i = 0; i < ocelot->num_flooding_pgids; i++)
1641 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1642 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1643 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1644 ANA_FLOODING, i);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001645 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1646 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1647 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1648 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1649 ANA_FLOODING_IPMC);
1650
1651 for (port = 0; port < ocelot->num_phys_ports; port++) {
1652 /* Transmit the frame to the local port. */
1653 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1654 /* Do not forward BPDU frames to the front ports. */
1655 ocelot_write_gix(ocelot,
1656 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1657 ANA_PORT_CPU_FWD_BPDU_CFG,
1658 port);
1659 /* Ensure bridging is disabled */
1660 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1661 }
1662
Alexandre Bellonia556c762018-05-14 22:04:57 +02001663 /* Allow broadcast MAC frames. */
Vladimir Oltean96b029b2020-06-21 14:46:02 +03001664 for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
Alexandre Bellonia556c762018-05-14 22:04:57 +02001665 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1666
1667 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1668 }
1669 ocelot_write_rix(ocelot,
1670 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1671 ANA_PGID_PGID, PGID_MC);
1672 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1673 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1674
Alexandre Bellonia556c762018-05-14 22:04:57 +02001675 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
1676 * registers endianness.
1677 */
1678 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1679 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1680 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1681 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1682 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1683 ANA_CPUQ_CFG_CPUQ_LRN(2) |
1684 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1685 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1686 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1687 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1688 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1689 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1690 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1691 for (i = 0; i < 16; i++)
1692 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1693 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1694 ANA_CPUQ_8021_CFG, i);
1695
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001696 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001697 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1698 OCELOT_STATS_CHECK_DELAY);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001699
Alexandre Bellonia556c762018-05-14 22:04:57 +02001700 return 0;
1701}
1702EXPORT_SYMBOL(ocelot_init);
1703
1704void ocelot_deinit(struct ocelot *ocelot)
1705{
Claudiu Manoilc5d13962019-07-25 16:33:18 +03001706 cancel_delayed_work(&ocelot->stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001707 destroy_workqueue(ocelot->stats_queue);
Vladimir Olteanca0b2722020-12-12 21:16:12 +02001708 destroy_workqueue(ocelot->owq);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001709 mutex_destroy(&ocelot->stats_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001710}
1711EXPORT_SYMBOL(ocelot_deinit);
1712
Vladimir Olteane5fb5122020-09-18 04:07:30 +03001713void ocelot_deinit_port(struct ocelot *ocelot, int port)
1714{
1715 struct ocelot_port *ocelot_port = ocelot->ports[port];
1716
1717 skb_queue_purge(&ocelot_port->tx_skbs);
1718}
1719EXPORT_SYMBOL(ocelot_deinit_port);
1720
Alexandre Bellonia556c762018-05-14 22:04:57 +02001721MODULE_LICENSE("Dual MIT/GPL");