blob: 5c2b5a2e860866190b6740ab00fabd9fb79002c9 [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>
Alexandre Bellonia556c762018-05-14 22:04:57 +02008#include "ocelot.h"
Horatiu Vulturb5962292019-05-31 09:16:56 +02009#include "ocelot_ace.h"
Alexandre Bellonia556c762018-05-14 22:04:57 +020010
Steen Hegelund639c1b22018-12-20 14:16:31 +010011#define TABLE_UPDATE_SLEEP_US 10
12#define TABLE_UPDATE_TIMEOUT_US 100000
13
Alexandre Bellonia556c762018-05-14 22:04:57 +020014struct ocelot_mact_entry {
15 u8 mac[ETH_ALEN];
16 u16 vid;
17 enum macaccess_entry_type type;
18};
19
Steen Hegelund639c1b22018-12-20 14:16:31 +010020static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
21{
22 return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
23}
24
Alexandre Bellonia556c762018-05-14 22:04:57 +020025static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
26{
Steen Hegelund639c1b22018-12-20 14:16:31 +010027 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +020028
Steen Hegelund639c1b22018-12-20 14:16:31 +010029 return readx_poll_timeout(ocelot_mact_read_macaccess,
30 ocelot, val,
31 (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
32 MACACCESS_CMD_IDLE,
33 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +020034}
35
36static void ocelot_mact_select(struct ocelot *ocelot,
37 const unsigned char mac[ETH_ALEN],
38 unsigned int vid)
39{
40 u32 macl = 0, mach = 0;
41
42 /* Set the MAC address to handle and the vlan associated in a format
43 * understood by the hardware.
44 */
45 mach |= vid << 16;
46 mach |= mac[0] << 8;
47 mach |= mac[1] << 0;
48 macl |= mac[2] << 24;
49 macl |= mac[3] << 16;
50 macl |= mac[4] << 8;
51 macl |= mac[5] << 0;
52
53 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
54 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
55
56}
57
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030058int ocelot_mact_learn(struct ocelot *ocelot, int port,
59 const unsigned char mac[ETH_ALEN],
60 unsigned int vid, enum macaccess_entry_type type)
Alexandre Bellonia556c762018-05-14 22:04:57 +020061{
62 ocelot_mact_select(ocelot, mac, vid);
63
64 /* Issue a write command */
65 ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
66 ANA_TABLES_MACACCESS_DEST_IDX(port) |
67 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
68 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
69 ANA_TABLES_MACACCESS);
70
71 return ocelot_mact_wait_for_completion(ocelot);
72}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030073EXPORT_SYMBOL(ocelot_mact_learn);
Alexandre Bellonia556c762018-05-14 22:04:57 +020074
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030075int ocelot_mact_forget(struct ocelot *ocelot,
76 const unsigned char mac[ETH_ALEN], unsigned int vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +020077{
78 ocelot_mact_select(ocelot, mac, vid);
79
80 /* Issue a forget command */
81 ocelot_write(ocelot,
82 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
83 ANA_TABLES_MACACCESS);
84
85 return ocelot_mact_wait_for_completion(ocelot);
86}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030087EXPORT_SYMBOL(ocelot_mact_forget);
Alexandre Bellonia556c762018-05-14 22:04:57 +020088
89static void ocelot_mact_init(struct ocelot *ocelot)
90{
91 /* Configure the learning mode entries attributes:
92 * - Do not copy the frame to the CPU extraction queues.
93 * - Use the vlan and mac_cpoy for dmac lookup.
94 */
95 ocelot_rmw(ocelot, 0,
96 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
97 | ANA_AGENCTRL_LEARN_FWD_KILL
98 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
99 ANA_AGENCTRL);
100
101 /* Clear the MAC table */
102 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
103}
104
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200105static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
Horatiu Vulturb5962292019-05-31 09:16:56 +0200106{
107 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
108 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200109 ANA_PORT_VCAP_S2_CFG, port);
Horatiu Vulturb5962292019-05-31 09:16:56 +0200110}
111
Steen Hegelund639c1b22018-12-20 14:16:31 +0100112static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
113{
114 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
115}
116
Alexandre Bellonia556c762018-05-14 22:04:57 +0200117static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
118{
Steen Hegelund639c1b22018-12-20 14:16:31 +0100119 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200120
Steen Hegelund639c1b22018-12-20 14:16:31 +0100121 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
122 ocelot,
123 val,
124 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
125 ANA_TABLES_VLANACCESS_CMD_IDLE,
126 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200127}
128
Antoine Tenart71425292018-06-26 14:28:49 +0200129static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
130{
131 /* Select the VID to configure */
132 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
133 ANA_TABLES_VLANTIDX);
134 /* Set the vlan port members mask and issue a write command */
135 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
136 ANA_TABLES_VLANACCESS_CMD_WRITE,
137 ANA_TABLES_VLANACCESS);
138
139 return ocelot_vlant_wait_for_completion(ocelot);
140}
141
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200142static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
143 u16 vid)
144{
145 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300146 u32 val = 0;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200147
148 if (ocelot_port->vid != vid) {
149 /* Always permit deleting the native VLAN (vid = 0) */
150 if (ocelot_port->vid && vid) {
151 dev_err(ocelot->dev,
152 "Port already has a native VLAN: %d\n",
153 ocelot_port->vid);
154 return -EBUSY;
155 }
156 ocelot_port->vid = vid;
157 }
158
159 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
Antoine Tenart71425292018-06-26 14:28:49 +0200160 REW_PORT_VLAN_CFG_PORT_VID_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200161 REW_PORT_VLAN_CFG, port);
162
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300163 if (ocelot_port->vlan_aware && !ocelot_port->vid)
164 /* If port is vlan-aware and tagged, drop untagged and priority
165 * tagged frames.
166 */
167 val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
168 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
169 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
170 ocelot_rmw_gix(ocelot, val,
171 ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
172 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
173 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
174 ANA_PORT_DROP_CFG, port);
175
176 if (ocelot_port->vlan_aware) {
177 if (ocelot_port->vid)
178 /* Tag all frames except when VID == DEFAULT_VLAN */
179 val = REW_TAG_CFG_TAG_CFG(1);
180 else
181 /* Tag all frames */
182 val = REW_TAG_CFG_TAG_CFG(3);
183 } else {
184 /* Port tagging disabled. */
185 val = REW_TAG_CFG_TAG_CFG(0);
186 }
187 ocelot_rmw_gix(ocelot, val,
188 REW_TAG_CFG_TAG_CFG_M,
189 REW_TAG_CFG, port);
190
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200191 return 0;
192}
193
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300194void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
195 bool vlan_aware)
196{
197 struct ocelot_port *ocelot_port = ocelot->ports[port];
198 u32 val;
199
200 ocelot_port->vlan_aware = vlan_aware;
201
202 if (vlan_aware)
203 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
204 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
205 else
206 val = 0;
207 ocelot_rmw_gix(ocelot, val,
208 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
209 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
210 ANA_PORT_VLAN_CFG, port);
211
212 ocelot_port_set_native_vlan(ocelot, port, ocelot_port->vid);
213}
214EXPORT_SYMBOL(ocelot_port_vlan_filtering);
215
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200216/* Default vlan to clasify for untagged frames (may be zero) */
217static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
218{
219 struct ocelot_port *ocelot_port = ocelot->ports[port];
220
221 ocelot_rmw_gix(ocelot,
222 ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
223 ANA_PORT_VLAN_CFG_VLAN_VID_M,
224 ANA_PORT_VLAN_CFG, port);
225
226 ocelot_port->pvid = pvid;
Antoine Tenart71425292018-06-26 14:28:49 +0200227}
228
Vladimir Oltean5e256362019-11-14 17:03:27 +0200229int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
230 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200231{
Antoine Tenart71425292018-06-26 14:28:49 +0200232 int ret;
233
Antoine Tenart71425292018-06-26 14:28:49 +0200234 /* Make the port a member of the VLAN */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200235 ocelot->vlan_mask[vid] |= BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200236 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
237 if (ret)
238 return ret;
239
240 /* Default ingress vlan classification */
241 if (pvid)
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200242 ocelot_port_set_pvid(ocelot, port, vid);
Antoine Tenart71425292018-06-26 14:28:49 +0200243
244 /* Untagged egress vlan clasification */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200245 if (untagged) {
246 ret = ocelot_port_set_native_vlan(ocelot, port, vid);
247 if (ret)
248 return ret;
Vladimir Olteanb9cd75e2019-10-26 21:04:27 +0300249 }
Antoine Tenart71425292018-06-26 14:28:49 +0200250
Antoine Tenart71425292018-06-26 14:28:49 +0200251 return 0;
252}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200253EXPORT_SYMBOL(ocelot_vlan_add);
Antoine Tenart71425292018-06-26 14:28:49 +0200254
Vladimir Oltean5e256362019-11-14 17:03:27 +0200255int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
Vladimir Oltean98559342019-11-09 15:02:48 +0200256{
257 struct ocelot_port *ocelot_port = ocelot->ports[port];
258 int ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200259
260 /* Stop the port from being a member of the vlan */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200261 ocelot->vlan_mask[vid] &= ~BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200262 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
263 if (ret)
264 return ret;
265
266 /* Ingress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200267 if (ocelot_port->pvid == vid)
268 ocelot_port_set_pvid(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200269
270 /* Egress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200271 if (ocelot_port->vid == vid)
272 ocelot_port_set_native_vlan(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200273
274 return 0;
275}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200276EXPORT_SYMBOL(ocelot_vlan_del);
Antoine Tenart71425292018-06-26 14:28:49 +0200277
Alexandre Bellonia556c762018-05-14 22:04:57 +0200278static void ocelot_vlan_init(struct ocelot *ocelot)
279{
Antoine Tenart71425292018-06-26 14:28:49 +0200280 u16 port, vid;
281
Alexandre Bellonia556c762018-05-14 22:04:57 +0200282 /* Clear VLAN table, by default all ports are members of all VLANs */
283 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
284 ANA_TABLES_VLANACCESS);
285 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200286
287 /* Configure the port VLAN memberships */
288 for (vid = 1; vid < VLAN_N_VID; vid++) {
289 ocelot->vlan_mask[vid] = 0;
290 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
291 }
292
293 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
294 * traffic. It is added automatically if 8021q module is loaded, but
295 * we can't rely on it since module may be not loaded.
296 */
297 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
298 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
299
Antoine Tenart71425292018-06-26 14:28:49 +0200300 /* Set vlan ingress filter mask to all ports but the CPU port by
301 * default.
302 */
Vladimir Oltean714d0ff2019-11-09 15:02:55 +0200303 ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
304 ANA_VLANMASK);
Antoine Tenart71425292018-06-26 14:28:49 +0200305
306 for (port = 0; port < ocelot->num_phys_ports; port++) {
307 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
308 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
309 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200310}
311
312/* Watermark encode
313 * Bit 8: Unit; 0:1, 1:16
314 * Bit 7-0: Value to be multiplied with unit
315 */
316static u16 ocelot_wm_enc(u16 value)
317{
318 if (value >= BIT(8))
319 return BIT(8) | (value / 16);
320
321 return value;
322}
323
Vladimir Oltean5e256362019-11-14 17:03:27 +0200324void ocelot_adjust_link(struct ocelot *ocelot, int port,
325 struct phy_device *phydev)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200326{
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200327 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +0200328 int speed, mode = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200329
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200330 switch (phydev->speed) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200331 case SPEED_10:
332 speed = OCELOT_SPEED_10;
333 break;
334 case SPEED_100:
335 speed = OCELOT_SPEED_100;
336 break;
337 case SPEED_1000:
338 speed = OCELOT_SPEED_1000;
339 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
340 break;
341 case SPEED_2500:
342 speed = OCELOT_SPEED_2500;
343 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
344 break;
345 default:
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200346 dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
347 port, phydev->speed);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200348 return;
349 }
350
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200351 phy_print_status(phydev);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200352
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200353 if (!phydev->link)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200354 return;
355
356 /* Only full duplex supported for now */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200357 ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200358 mode, DEV_MAC_MODE_CFG);
359
Vladimir Oltean1ba8f652020-02-29 16:31:11 +0200360 /* Disable HDX fast control */
361 ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
362 DEV_PORT_MISC);
363
364 /* SGMII only for now */
365 ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
366 PCS1G_MODE_CFG);
367 ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
368
369 /* Enable PCS */
370 ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
371
372 /* No aneg on SGMII */
373 ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
374
375 /* No loopback */
376 ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200377
Alexandre Bellonia556c762018-05-14 22:04:57 +0200378 /* Enable MAC module */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200379 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200380 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
381
382 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
383 * reset */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200384 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
Alexandre Bellonia556c762018-05-14 22:04:57 +0200385 DEV_CLOCK_CFG);
386
Alexandre Bellonia556c762018-05-14 22:04:57 +0200387 /* No PFC */
388 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200389 ANA_PFC_PFC_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200390
Alexandre Bellonia556c762018-05-14 22:04:57 +0200391 /* Core: Enable port for frame transfer */
392 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
393 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
394 QSYS_SWITCH_PORT_MODE_PORT_ENA,
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200395 QSYS_SWITCH_PORT_MODE, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200396
397 /* Flow control */
398 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
399 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
400 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
401 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
402 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200403 SYS_MAC_FC_CFG, port);
404 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200405}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200406EXPORT_SYMBOL(ocelot_adjust_link);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200407
Vladimir Oltean5e256362019-11-14 17:03:27 +0200408void ocelot_port_enable(struct ocelot *ocelot, int port,
409 struct phy_device *phy)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200410{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200411 /* Enable receiving frames on the port, and activate auto-learning of
412 * MAC addresses.
413 */
414 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
415 ANA_PORT_PORT_CFG_RECV_ENA |
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200416 ANA_PORT_PORT_CFG_PORTID_VAL(port),
417 ANA_PORT_PORT_CFG, port);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200418}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200419EXPORT_SYMBOL(ocelot_port_enable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200420
Vladimir Oltean5e256362019-11-14 17:03:27 +0200421void ocelot_port_disable(struct ocelot *ocelot, int port)
Vladimir Oltean889b8952019-11-09 15:02:57 +0200422{
423 struct ocelot_port *ocelot_port = ocelot->ports[port];
424
425 ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
426 ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
427 QSYS_SWITCH_PORT_MODE, port);
428}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200429EXPORT_SYMBOL(ocelot_port_disable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200430
Yangbo Lu400928b2019-11-20 16:23:16 +0800431int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
432 struct sk_buff *skb)
433{
434 struct skb_shared_info *shinfo = skb_shinfo(skb);
435 struct ocelot *ocelot = ocelot_port->ocelot;
436
437 if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
438 ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
Yangbo Lu400928b2019-11-20 16:23:16 +0800439 shinfo->tx_flags |= SKBTX_IN_PROGRESS;
Yangbo Lub049da12019-11-27 15:27:57 +0800440 /* Store timestamp ID in cb[0] of sk_buff */
441 skb->cb[0] = ocelot_port->ts_id % 4;
442 skb_queue_tail(&ocelot_port->tx_skbs, skb);
Yangbo Lu400928b2019-11-20 16:23:16 +0800443 return 0;
444 }
445 return -ENODATA;
446}
447EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
448
Yangbo Lue23a7b32019-11-20 16:23:15 +0800449static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
450 struct timespec64 *ts)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200451{
452 unsigned long flags;
453 u32 val;
454
455 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
456
457 /* Read current PTP time to get seconds */
458 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
459
460 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
461 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
462 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
463 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
464
465 /* Read packet HW timestamp from FIFO */
466 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
467 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
468
469 /* Sec has incremented since the ts was registered */
470 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
471 ts->tv_sec--;
472
473 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
474}
Yangbo Lue23a7b32019-11-20 16:23:15 +0800475
476void ocelot_get_txtstamp(struct ocelot *ocelot)
477{
478 int budget = OCELOT_PTP_QUEUE_SZ;
479
480 while (budget--) {
Yangbo Lub049da12019-11-27 15:27:57 +0800481 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800482 struct skb_shared_hwtstamps shhwtstamps;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800483 struct ocelot_port *port;
484 struct timespec64 ts;
Yangbo Lub049da12019-11-27 15:27:57 +0800485 unsigned long flags;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800486 u32 val, id, txport;
487
488 val = ocelot_read(ocelot, SYS_PTP_STATUS);
489
490 /* Check if a timestamp can be retrieved */
491 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
492 break;
493
494 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
495
496 /* Retrieve the ts ID and Tx port */
497 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
498 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
499
500 /* Retrieve its associated skb */
501 port = ocelot->ports[txport];
502
Yangbo Lub049da12019-11-27 15:27:57 +0800503 spin_lock_irqsave(&port->tx_skbs.lock, flags);
504
505 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
506 if (skb->cb[0] != id)
Yangbo Lue23a7b32019-11-20 16:23:15 +0800507 continue;
Yangbo Lub049da12019-11-27 15:27:57 +0800508 __skb_unlink(skb, &port->tx_skbs);
509 skb_match = skb;
Yangbo Lufc62c092019-11-27 15:27:56 +0800510 break;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800511 }
512
Yangbo Lub049da12019-11-27 15:27:57 +0800513 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
514
Yangbo Lue23a7b32019-11-20 16:23:15 +0800515 /* Next ts */
516 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
517
Yangbo Lub049da12019-11-27 15:27:57 +0800518 if (unlikely(!skb_match))
Yangbo Lue23a7b32019-11-20 16:23:15 +0800519 continue;
520
521 /* Get the h/w timestamp */
522 ocelot_get_hwtimestamp(ocelot, &ts);
523
524 /* Set the timestamp into the skb */
525 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
526 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
Yangbo Lub049da12019-11-27 15:27:57 +0800527 skb_tstamp_tx(skb_match, &shhwtstamps);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800528
Yangbo Lub049da12019-11-27 15:27:57 +0800529 dev_kfree_skb_any(skb_match);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800530 }
531}
532EXPORT_SYMBOL(ocelot_get_txtstamp);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200533
Vladimir Oltean5e256362019-11-14 17:03:27 +0200534int ocelot_fdb_add(struct ocelot *ocelot, int port,
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300535 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200536{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200537 struct ocelot_port *ocelot_port = ocelot->ports[port];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200538
Antoine Tenart71425292018-06-26 14:28:49 +0200539 if (!vid) {
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300540 if (!ocelot_port->vlan_aware)
Antoine Tenart71425292018-06-26 14:28:49 +0200541 /* If the bridge is not VLAN aware and no VID was
542 * provided, set it to pvid to ensure the MAC entry
543 * matches incoming untagged packets
544 */
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200545 vid = ocelot_port->pvid;
Antoine Tenart71425292018-06-26 14:28:49 +0200546 else
547 /* If the bridge is VLAN aware a VID must be provided as
548 * otherwise the learnt entry wouldn't match any frame.
549 */
550 return -EINVAL;
551 }
552
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200553 return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200554}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200555EXPORT_SYMBOL(ocelot_fdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200556
Vladimir Oltean5e256362019-11-14 17:03:27 +0200557int ocelot_fdb_del(struct ocelot *ocelot, int port,
558 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200559{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200560 return ocelot_mact_forget(ocelot, addr, vid);
561}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200562EXPORT_SYMBOL(ocelot_fdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200563
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300564int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
565 bool is_static, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200566{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200567 struct ocelot_dump_ctx *dump = data;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200568 u32 portid = NETLINK_CB(dump->cb->skb).portid;
569 u32 seq = dump->cb->nlh->nlmsg_seq;
570 struct nlmsghdr *nlh;
571 struct ndmsg *ndm;
572
573 if (dump->idx < dump->cb->args[2])
574 goto skip;
575
576 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
577 sizeof(*ndm), NLM_F_MULTI);
578 if (!nlh)
579 return -EMSGSIZE;
580
581 ndm = nlmsg_data(nlh);
582 ndm->ndm_family = AF_BRIDGE;
583 ndm->ndm_pad1 = 0;
584 ndm->ndm_pad2 = 0;
585 ndm->ndm_flags = NTF_SELF;
586 ndm->ndm_type = 0;
587 ndm->ndm_ifindex = dump->dev->ifindex;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200588 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200589
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200590 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200591 goto nla_put_failure;
592
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200593 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200594 goto nla_put_failure;
595
596 nlmsg_end(dump->skb, nlh);
597
598skip:
599 dump->idx++;
600 return 0;
601
602nla_put_failure:
603 nlmsg_cancel(dump->skb, nlh);
604 return -EMSGSIZE;
605}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300606EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200607
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200608static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
609 struct ocelot_mact_entry *entry)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200610{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200611 u32 val, dst, macl, mach;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200612 char mac[ETH_ALEN];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200613
614 /* Set row and column to read from */
615 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
616 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
617
618 /* Issue a read command */
619 ocelot_write(ocelot,
620 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
621 ANA_TABLES_MACACCESS);
622
623 if (ocelot_mact_wait_for_completion(ocelot))
624 return -ETIMEDOUT;
625
626 /* Read the entry flags */
627 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
628 if (!(val & ANA_TABLES_MACACCESS_VALID))
629 return -EINVAL;
630
631 /* If the entry read has another port configured as its destination,
632 * do not report it.
633 */
634 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200635 if (dst != port)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200636 return -EINVAL;
637
638 /* Get the entry's MAC address and VLAN id */
639 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
640 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
641
642 mac[0] = (mach >> 8) & 0xff;
643 mac[1] = (mach >> 0) & 0xff;
644 mac[2] = (macl >> 24) & 0xff;
645 mac[3] = (macl >> 16) & 0xff;
646 mac[4] = (macl >> 8) & 0xff;
647 mac[5] = (macl >> 0) & 0xff;
648
649 entry->vid = (mach >> 16) & 0xfff;
650 ether_addr_copy(entry->mac, mac);
651
652 return 0;
653}
654
Vladimir Oltean5e256362019-11-14 17:03:27 +0200655int ocelot_fdb_dump(struct ocelot *ocelot, int port,
656 dsa_fdb_dump_cb_t *cb, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200657{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200658 int i, j;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200659
Vladimir Oltean21ce7f32020-05-04 01:20:26 +0300660 /* Loop through all the mac tables entries. */
661 for (i = 0; i < ocelot->num_mact_rows; i++) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200662 for (j = 0; j < 4; j++) {
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200663 struct ocelot_mact_entry entry;
664 bool is_static;
665 int ret;
666
667 ret = ocelot_mact_read(ocelot, port, i, j, &entry);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200668 /* If the entry is invalid (wrong port, invalid...),
669 * skip it.
670 */
671 if (ret == -EINVAL)
672 continue;
673 else if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200674 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200675
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200676 is_static = (entry.type == ENTRYTYPE_LOCKED);
677
678 ret = cb(entry.mac, entry.vid, is_static, data);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200679 if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200680 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200681 }
682 }
683
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200684 return 0;
685}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200686EXPORT_SYMBOL(ocelot_fdb_dump);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200687
Yangbo Luf1459222019-11-20 16:23:14 +0800688int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200689{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200690 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
691 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
692}
Yangbo Luf1459222019-11-20 16:23:14 +0800693EXPORT_SYMBOL(ocelot_hwstamp_get);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200694
Yangbo Luf1459222019-11-20 16:23:14 +0800695int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200696{
Vladimir Oltean306fd442019-11-09 15:02:50 +0200697 struct ocelot_port *ocelot_port = ocelot->ports[port];
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200698 struct hwtstamp_config cfg;
699
700 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
701 return -EFAULT;
702
703 /* reserved for future extensions */
704 if (cfg.flags)
705 return -EINVAL;
706
707 /* Tx type sanity check */
708 switch (cfg.tx_type) {
709 case HWTSTAMP_TX_ON:
Vladimir Oltean306fd442019-11-09 15:02:50 +0200710 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200711 break;
712 case HWTSTAMP_TX_ONESTEP_SYNC:
713 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
714 * need to update the origin time.
715 */
Vladimir Oltean306fd442019-11-09 15:02:50 +0200716 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200717 break;
718 case HWTSTAMP_TX_OFF:
Vladimir Oltean306fd442019-11-09 15:02:50 +0200719 ocelot_port->ptp_cmd = 0;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200720 break;
721 default:
722 return -ERANGE;
723 }
724
725 mutex_lock(&ocelot->ptp_lock);
726
727 switch (cfg.rx_filter) {
728 case HWTSTAMP_FILTER_NONE:
729 break;
730 case HWTSTAMP_FILTER_ALL:
731 case HWTSTAMP_FILTER_SOME:
732 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
733 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
734 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
735 case HWTSTAMP_FILTER_NTP_ALL:
736 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
737 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
738 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
739 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
740 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
741 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
742 case HWTSTAMP_FILTER_PTP_V2_EVENT:
743 case HWTSTAMP_FILTER_PTP_V2_SYNC:
744 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
745 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
746 break;
747 default:
748 mutex_unlock(&ocelot->ptp_lock);
749 return -ERANGE;
750 }
751
752 /* Commit back the result & save it */
753 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
754 mutex_unlock(&ocelot->ptp_lock);
755
756 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
757}
Yangbo Luf1459222019-11-20 16:23:14 +0800758EXPORT_SYMBOL(ocelot_hwstamp_set);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200759
Vladimir Oltean5e256362019-11-14 17:03:27 +0200760void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200761{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200762 int i;
763
764 if (sset != ETH_SS_STATS)
765 return;
766
767 for (i = 0; i < ocelot->num_stats; i++)
768 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
769 ETH_GSTRING_LEN);
770}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200771EXPORT_SYMBOL(ocelot_get_strings);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200772
Claudiu Manoil1e1caa92019-04-16 17:51:59 +0300773static void ocelot_update_stats(struct ocelot *ocelot)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200774{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200775 int i, j;
776
777 mutex_lock(&ocelot->stats_lock);
778
779 for (i = 0; i < ocelot->num_phys_ports; i++) {
780 /* Configure the port to read the stats from */
781 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
782
783 for (j = 0; j < ocelot->num_stats; j++) {
784 u32 val;
785 unsigned int idx = i * ocelot->num_stats + j;
786
787 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
788 ocelot->stats_layout[j].offset);
789
790 if (val < (ocelot->stats[idx] & U32_MAX))
791 ocelot->stats[idx] += (u64)1 << 32;
792
793 ocelot->stats[idx] = (ocelot->stats[idx] &
794 ~(u64)U32_MAX) + val;
795 }
796 }
797
Claudiu Manoil1e1caa92019-04-16 17:51:59 +0300798 mutex_unlock(&ocelot->stats_lock);
799}
800
801static void ocelot_check_stats_work(struct work_struct *work)
802{
803 struct delayed_work *del_work = to_delayed_work(work);
804 struct ocelot *ocelot = container_of(del_work, struct ocelot,
805 stats_work);
806
807 ocelot_update_stats(ocelot);
808
Alexandre Bellonia556c762018-05-14 22:04:57 +0200809 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
810 OCELOT_STATS_CHECK_DELAY);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200811}
812
Vladimir Oltean5e256362019-11-14 17:03:27 +0200813void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200814{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200815 int i;
816
817 /* check and update now */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +0300818 ocelot_update_stats(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200819
820 /* Copy all counters */
821 for (i = 0; i < ocelot->num_stats; i++)
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200822 *data++ = ocelot->stats[port * ocelot->num_stats + i];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200823}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200824EXPORT_SYMBOL(ocelot_get_ethtool_stats);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200825
Vladimir Oltean5e256362019-11-14 17:03:27 +0200826int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
Vladimir Olteanc7282d32019-11-09 15:02:54 +0200827{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200828 if (sset != ETH_SS_STATS)
829 return -EOPNOTSUPP;
Vladimir Olteanc7282d32019-11-09 15:02:54 +0200830
Alexandre Bellonia556c762018-05-14 22:04:57 +0200831 return ocelot->num_stats;
832}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200833EXPORT_SYMBOL(ocelot_get_sset_count);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200834
Vladimir Oltean5e256362019-11-14 17:03:27 +0200835int ocelot_get_ts_info(struct ocelot *ocelot, int port,
836 struct ethtool_ts_info *info)
Vladimir Olteanc7282d32019-11-09 15:02:54 +0200837{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200838 info->phc_index = ocelot->ptp_clock ?
839 ptp_clock_index(ocelot->ptp_clock) : -1;
Yangbo Lud2b09a82020-04-20 10:46:46 +0800840 if (info->phc_index == -1) {
841 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
842 SOF_TIMESTAMPING_RX_SOFTWARE |
843 SOF_TIMESTAMPING_SOFTWARE;
844 return 0;
845 }
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200846 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
847 SOF_TIMESTAMPING_RX_SOFTWARE |
848 SOF_TIMESTAMPING_SOFTWARE |
849 SOF_TIMESTAMPING_TX_HARDWARE |
850 SOF_TIMESTAMPING_RX_HARDWARE |
851 SOF_TIMESTAMPING_RAW_HARDWARE;
852 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
853 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
854 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
855
856 return 0;
857}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200858EXPORT_SYMBOL(ocelot_get_ts_info);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200859
Vladimir Oltean5e256362019-11-14 17:03:27 +0200860void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200861{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200862 u32 port_cfg;
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200863 int p, i;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200864
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200865 if (!(BIT(port) & ocelot->bridge_mask))
866 return;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200867
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200868 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200869
870 switch (state) {
871 case BR_STATE_FORWARDING:
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200872 ocelot->bridge_fwd_mask |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200873 /* Fallthrough */
874 case BR_STATE_LEARNING:
875 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
876 break;
877
878 default:
879 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200880 ocelot->bridge_fwd_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200881 break;
882 }
883
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200884 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200885
886 /* Apply FWD mask. The loop is needed to add/remove the current port as
887 * a source for the other ports.
888 */
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200889 for (p = 0; p < ocelot->num_phys_ports; p++) {
Vladimir Oltean69df5782020-02-29 16:50:02 +0200890 if (ocelot->bridge_fwd_mask & BIT(p)) {
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200891 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200892
893 for (i = 0; i < ocelot->num_phys_ports; i++) {
894 unsigned long bond_mask = ocelot->lags[i];
895
896 if (!bond_mask)
897 continue;
898
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200899 if (bond_mask & BIT(p)) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200900 mask &= ~bond_mask;
901 break;
902 }
903 }
904
Vladimir Olteanc9d22032019-11-09 15:03:01 +0200905 ocelot_write_rix(ocelot, mask,
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200906 ANA_PGID_PGID, PGID_SRC + p);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200907 } else {
Vladimir Oltean69df5782020-02-29 16:50:02 +0200908 ocelot_write_rix(ocelot, 0,
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200909 ANA_PGID_PGID, PGID_SRC + p);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200910 }
911 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200912}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200913EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200914
Vladimir Oltean5e256362019-11-14 17:03:27 +0200915void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200916{
Vladimir Olteanc0d7ecc2020-05-04 01:20:27 +0300917 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
918
919 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
920 * which is clearly not what our intention is. So avoid that.
921 */
922 if (!age_period)
923 age_period = 1;
924
925 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200926}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200927EXPORT_SYMBOL(ocelot_set_ageing_time);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200928
Alexandre Bellonia556c762018-05-14 22:04:57 +0200929static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
930 const unsigned char *addr,
931 u16 vid)
932{
933 struct ocelot_multicast *mc;
934
935 list_for_each_entry(mc, &ocelot->multicast, list) {
936 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
937 return mc;
938 }
939
940 return NULL;
941}
942
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300943int ocelot_port_obj_add_mdb(struct net_device *dev,
944 const struct switchdev_obj_port_mdb *mdb,
945 struct switchdev_trans *trans)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200946{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200947 struct ocelot_port_private *priv = netdev_priv(dev);
948 struct ocelot_port *ocelot_port = &priv->port;
949 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200950 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200951 struct ocelot_multicast *mc;
952 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200953 u16 vid = mdb->vid;
954 bool new = false;
955
956 if (!vid)
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200957 vid = ocelot_port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200958
959 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
960 if (!mc) {
961 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
962 if (!mc)
963 return -ENOMEM;
964
965 memcpy(mc->addr, mdb->addr, ETH_ALEN);
966 mc->vid = vid;
967
968 list_add_tail(&mc->list, &ocelot->multicast);
969 new = true;
970 }
971
972 memcpy(addr, mc->addr, ETH_ALEN);
973 addr[0] = 0;
974
975 if (!new) {
976 addr[2] = mc->ports << 0;
977 addr[1] = mc->ports << 8;
978 ocelot_mact_forget(ocelot, addr, vid);
979 }
980
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200981 mc->ports |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200982 addr[2] = mc->ports << 0;
983 addr[1] = mc->ports << 8;
984
985 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
986}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300987EXPORT_SYMBOL(ocelot_port_obj_add_mdb);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200988
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300989int ocelot_port_obj_del_mdb(struct net_device *dev,
990 const struct switchdev_obj_port_mdb *mdb)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200991{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200992 struct ocelot_port_private *priv = netdev_priv(dev);
993 struct ocelot_port *ocelot_port = &priv->port;
994 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200995 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200996 struct ocelot_multicast *mc;
997 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200998 u16 vid = mdb->vid;
999
1000 if (!vid)
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001001 vid = ocelot_port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001002
1003 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1004 if (!mc)
1005 return -ENOENT;
1006
1007 memcpy(addr, mc->addr, ETH_ALEN);
1008 addr[2] = mc->ports << 0;
1009 addr[1] = mc->ports << 8;
1010 addr[0] = 0;
1011 ocelot_mact_forget(ocelot, addr, vid);
1012
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001013 mc->ports &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001014 if (!mc->ports) {
1015 list_del(&mc->list);
1016 devm_kfree(ocelot->dev, mc);
1017 return 0;
1018 }
1019
1020 addr[2] = mc->ports << 0;
1021 addr[1] = mc->ports << 8;
1022
1023 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1024}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001025EXPORT_SYMBOL(ocelot_port_obj_del_mdb);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001026
Vladimir Oltean5e256362019-11-14 17:03:27 +02001027int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1028 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001029{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001030 if (!ocelot->bridge_mask) {
1031 ocelot->hw_bridge_dev = bridge;
1032 } else {
1033 if (ocelot->hw_bridge_dev != bridge)
1034 /* This is adding the port to a second bridge, this is
1035 * unsupported */
1036 return -ENODEV;
1037 }
1038
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001039 ocelot->bridge_mask |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001040
1041 return 0;
1042}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001043EXPORT_SYMBOL(ocelot_port_bridge_join);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001044
Vladimir Oltean5e256362019-11-14 17:03:27 +02001045int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1046 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001047{
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001048 ocelot->bridge_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001049
1050 if (!ocelot->bridge_mask)
1051 ocelot->hw_bridge_dev = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02001052
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001053 ocelot_port_vlan_filtering(ocelot, port, 0);
1054 ocelot_port_set_pvid(ocelot, port, 0);
1055 return ocelot_port_set_native_vlan(ocelot, port, 0);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001056}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001057EXPORT_SYMBOL(ocelot_port_bridge_leave);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001058
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001059static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1060{
1061 int i, port, lag;
1062
1063 /* Reset destination and aggregation PGIDS */
1064 for (port = 0; port < ocelot->num_phys_ports; port++)
1065 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1066
1067 for (i = PGID_AGGR; i < PGID_SRC; i++)
1068 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1069 ANA_PGID_PGID, i);
1070
1071 /* Now, set PGIDs for each LAG */
1072 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1073 unsigned long bond_mask;
1074 int aggr_count = 0;
1075 u8 aggr_idx[16];
1076
1077 bond_mask = ocelot->lags[lag];
1078 if (!bond_mask)
1079 continue;
1080
1081 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1082 // Destination mask
1083 ocelot_write_rix(ocelot, bond_mask,
1084 ANA_PGID_PGID, port);
1085 aggr_idx[aggr_count] = port;
1086 aggr_count++;
1087 }
1088
1089 for (i = PGID_AGGR; i < PGID_SRC; i++) {
1090 u32 ac;
1091
1092 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1093 ac &= ~bond_mask;
1094 ac |= BIT(aggr_idx[i % aggr_count]);
1095 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1096 }
1097 }
1098}
1099
1100static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1101{
1102 unsigned long bond_mask = ocelot->lags[lag];
1103 unsigned int p;
1104
1105 for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1106 u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1107
1108 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1109
1110 /* Use lag port as logical port for port i */
1111 ocelot_write_gix(ocelot, port_cfg |
1112 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1113 ANA_PORT_PORT_CFG, p);
1114 }
1115}
1116
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001117int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1118 struct net_device *bond)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001119{
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001120 struct net_device *ndev;
1121 u32 bond_mask = 0;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001122 int lag, lp;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001123
1124 rcu_read_lock();
1125 for_each_netdev_in_bond_rcu(bond, ndev) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001126 struct ocelot_port_private *priv = netdev_priv(ndev);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001127
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001128 bond_mask |= BIT(priv->chip_port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001129 }
1130 rcu_read_unlock();
1131
1132 lp = __ffs(bond_mask);
1133
1134 /* If the new port is the lowest one, use it as the logical port from
1135 * now on
1136 */
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001137 if (port == lp) {
1138 lag = port;
1139 ocelot->lags[port] = bond_mask;
1140 bond_mask &= ~BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001141 if (bond_mask) {
1142 lp = __ffs(bond_mask);
1143 ocelot->lags[lp] = 0;
1144 }
1145 } else {
1146 lag = lp;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001147 ocelot->lags[lp] |= BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001148 }
1149
1150 ocelot_setup_lag(ocelot, lag);
1151 ocelot_set_aggr_pgids(ocelot);
1152
1153 return 0;
1154}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001155EXPORT_SYMBOL(ocelot_port_lag_join);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001156
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001157void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1158 struct net_device *bond)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001159{
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001160 u32 port_cfg;
1161 int i;
1162
1163 /* Remove port from any lag */
1164 for (i = 0; i < ocelot->num_phys_ports; i++)
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001165 ocelot->lags[i] &= ~BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001166
1167 /* if it was the logical port of the lag, move the lag config to the
1168 * next port
1169 */
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001170 if (ocelot->lags[port]) {
1171 int n = __ffs(ocelot->lags[port]);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001172
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001173 ocelot->lags[n] = ocelot->lags[port];
1174 ocelot->lags[port] = 0;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001175
1176 ocelot_setup_lag(ocelot, n);
1177 }
1178
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001179 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001180 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001181 ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1182 ANA_PORT_PORT_CFG, port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001183
1184 ocelot_set_aggr_pgids(ocelot);
1185}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001186EXPORT_SYMBOL(ocelot_port_lag_leave);
Petr Machata0e332c82018-11-22 23:30:11 +00001187
Vladimir Olteana8015de2020-03-10 03:28:18 +02001188/* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1189 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001190 * In the special case that it's the NPI port that we're configuring, the
1191 * length of the tag and optional prefix needs to be accounted for privately,
1192 * in order to be able to sustain communication at the requested @sdu.
Vladimir Olteana8015de2020-03-10 03:28:18 +02001193 */
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001194void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
Vladimir Oltean31350d72019-11-09 15:02:56 +02001195{
1196 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteana8015de2020-03-10 03:28:18 +02001197 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001198 int atop_wm;
Vladimir Oltean31350d72019-11-09 15:02:56 +02001199
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001200 if (port == ocelot->npi) {
1201 maxlen += OCELOT_TAG_LEN;
1202
1203 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1204 maxlen += OCELOT_SHORT_PREFIX_LEN;
1205 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
1206 maxlen += OCELOT_LONG_PREFIX_LEN;
1207 }
1208
Vladimir Olteana8015de2020-03-10 03:28:18 +02001209 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001210
1211 /* Set Pause WM hysteresis
Vladimir Olteana8015de2020-03-10 03:28:18 +02001212 * 152 = 6 * maxlen / OCELOT_BUFFER_CELL_SZ
1213 * 101 = 4 * maxlen / OCELOT_BUFFER_CELL_SZ
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001214 */
1215 ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
1216 SYS_PAUSE_CFG_PAUSE_STOP(101) |
1217 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, port);
1218
1219 /* Tail dropping watermark */
Vladimir Olteana8015de2020-03-10 03:28:18 +02001220 atop_wm = (ocelot->shared_queue_sz - 9 * maxlen) /
1221 OCELOT_BUFFER_CELL_SZ;
1222 ocelot_write_rix(ocelot, ocelot_wm_enc(9 * maxlen),
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001223 SYS_ATOP, port);
1224 ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
1225}
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001226EXPORT_SYMBOL(ocelot_port_set_maxlen);
1227
1228int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
1229{
1230 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
1231
1232 if (port == ocelot->npi) {
1233 max_mtu -= OCELOT_TAG_LEN;
1234
1235 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1236 max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1237 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
1238 max_mtu -= OCELOT_LONG_PREFIX_LEN;
1239 }
1240
1241 return max_mtu;
1242}
1243EXPORT_SYMBOL(ocelot_get_max_mtu);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001244
Vladimir Oltean5e256362019-11-14 17:03:27 +02001245void ocelot_init_port(struct ocelot *ocelot, int port)
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001246{
1247 struct ocelot_port *ocelot_port = ocelot->ports[port];
1248
Yangbo Lub049da12019-11-27 15:27:57 +08001249 skb_queue_head_init(&ocelot_port->tx_skbs);
Vladimir Oltean31350d72019-11-09 15:02:56 +02001250
1251 /* Basic L2 initialization */
1252
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001253 /* Set MAC IFG Gaps
1254 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1255 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1256 */
1257 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
1258 DEV_MAC_IFG_CFG);
1259
1260 /* Load seed (0) and set MAC HDX late collision */
1261 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1262 DEV_MAC_HDX_CFG_SEED_LOAD,
1263 DEV_MAC_HDX_CFG);
1264 mdelay(1);
1265 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
1266 DEV_MAC_HDX_CFG);
1267
1268 /* Set Max Length and maximum tags allowed */
Vladimir Olteana8015de2020-03-10 03:28:18 +02001269 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001270 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
1271 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
Vladimir Olteana8015de2020-03-10 03:28:18 +02001272 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001273 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
1274 DEV_MAC_TAGS_CFG);
1275
1276 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
1277 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
1278 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
1279
Vladimir Oltean31350d72019-11-09 15:02:56 +02001280 /* Drop frames with multicast source address */
1281 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1282 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1283 ANA_PORT_DROP_CFG, port);
1284
1285 /* Set default VLAN and tag type to 8021Q. */
1286 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
1287 REW_PORT_VLAN_CFG_PORT_TPID_M,
1288 REW_PORT_VLAN_CFG, port);
1289
1290 /* Enable vcap lookups */
1291 ocelot_vcap_enable(ocelot, port);
1292}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001293EXPORT_SYMBOL(ocelot_init_port);
Vladimir Oltean31350d72019-11-09 15:02:56 +02001294
Vladimir Oltean69df5782020-02-29 16:50:02 +02001295/* Configure and enable the CPU port module, which is a set of queues.
1296 * If @npi contains a valid port index, the CPU port module is connected
1297 * to the Node Processor Interface (NPI). This is the mode through which
1298 * frames can be injected from and extracted to an external CPU,
1299 * over Ethernet.
1300 */
1301void ocelot_configure_cpu(struct ocelot *ocelot, int npi,
1302 enum ocelot_tag_prefix injection,
1303 enum ocelot_tag_prefix extraction)
Vladimir Oltean21468192019-11-09 15:03:00 +02001304{
Vladimir Oltean69df5782020-02-29 16:50:02 +02001305 int cpu = ocelot->num_phys_ports;
1306
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001307 ocelot->npi = npi;
1308 ocelot->inj_prefix = injection;
1309 ocelot->xtr_prefix = extraction;
1310
Vladimir Oltean69df5782020-02-29 16:50:02 +02001311 /* The unicast destination PGID for the CPU port module is unused */
Vladimir Oltean21468192019-11-09 15:03:00 +02001312 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
Vladimir Oltean69df5782020-02-29 16:50:02 +02001313 /* Instead set up a multicast destination PGID for traffic copied to
1314 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
1315 * addresses will be copied to the CPU via this PGID.
1316 */
Vladimir Oltean21468192019-11-09 15:03:00 +02001317 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1318 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1319 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1320 ANA_PORT_PORT_CFG, cpu);
1321
Vladimir Oltean69df5782020-02-29 16:50:02 +02001322 if (npi >= 0 && npi < ocelot->num_phys_ports) {
Vladimir Oltean21468192019-11-09 15:03:00 +02001323 ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
Vladimir Oltean69df5782020-02-29 16:50:02 +02001324 QSYS_EXT_CPU_CFG_EXT_CPU_PORT(npi),
Vladimir Oltean21468192019-11-09 15:03:00 +02001325 QSYS_EXT_CPU_CFG);
Vladimir Olteanba551bc2019-11-14 17:03:25 +02001326
Vladimir Oltean69df5782020-02-29 16:50:02 +02001327 /* Enable NPI port */
1328 ocelot_write_rix(ocelot,
1329 QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
1330 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
1331 QSYS_SWITCH_PORT_MODE_PORT_ENA,
1332 QSYS_SWITCH_PORT_MODE, npi);
1333 /* NPI port Injection/Extraction configuration */
1334 ocelot_write_rix(ocelot,
1335 SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
1336 SYS_PORT_MODE_INCL_INJ_HDR(injection),
1337 SYS_PORT_MODE, npi);
Vladimir Oltean21468192019-11-09 15:03:00 +02001338 }
1339
Vladimir Oltean69df5782020-02-29 16:50:02 +02001340 /* Enable CPU port module */
Vladimir Oltean21468192019-11-09 15:03:00 +02001341 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
1342 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
1343 QSYS_SWITCH_PORT_MODE_PORT_ENA,
1344 QSYS_SWITCH_PORT_MODE, cpu);
Vladimir Oltean69df5782020-02-29 16:50:02 +02001345 /* CPU port Injection/Extraction configuration */
Vladimir Oltean21468192019-11-09 15:03:00 +02001346 ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
1347 SYS_PORT_MODE_INCL_INJ_HDR(injection),
1348 SYS_PORT_MODE, cpu);
1349
1350 /* Configure the CPU port to be VLAN aware */
1351 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
1352 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1353 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
1354 ANA_PORT_VLAN_CFG, cpu);
Vladimir Oltean21468192019-11-09 15:03:00 +02001355}
Vladimir Oltean69df5782020-02-29 16:50:02 +02001356EXPORT_SYMBOL(ocelot_configure_cpu);
Vladimir Oltean21468192019-11-09 15:03:00 +02001357
Alexandre Bellonia556c762018-05-14 22:04:57 +02001358int ocelot_init(struct ocelot *ocelot)
1359{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001360 char queue_name[32];
Vladimir Oltean21468192019-11-09 15:03:00 +02001361 int i, ret;
1362 u32 port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001363
Vladimir Oltean3a77b592019-11-14 17:03:26 +02001364 if (ocelot->ops->reset) {
1365 ret = ocelot->ops->reset(ocelot);
1366 if (ret) {
1367 dev_err(ocelot->dev, "Switch reset failed\n");
1368 return ret;
1369 }
1370 }
1371
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001372 ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1373 sizeof(u32), GFP_KERNEL);
1374 if (!ocelot->lags)
1375 return -ENOMEM;
1376
Alexandre Bellonia556c762018-05-14 22:04:57 +02001377 ocelot->stats = devm_kcalloc(ocelot->dev,
1378 ocelot->num_phys_ports * ocelot->num_stats,
1379 sizeof(u64), GFP_KERNEL);
1380 if (!ocelot->stats)
1381 return -ENOMEM;
1382
1383 mutex_init(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001384 mutex_init(&ocelot->ptp_lock);
1385 spin_lock_init(&ocelot->ptp_clock_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001386 snprintf(queue_name, sizeof(queue_name), "%s-stats",
1387 dev_name(ocelot->dev));
1388 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1389 if (!ocelot->stats_queue)
1390 return -ENOMEM;
1391
Claudiu Manoil2b120dd2019-11-09 15:02:58 +02001392 INIT_LIST_HEAD(&ocelot->multicast);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001393 ocelot_mact_init(ocelot);
1394 ocelot_vlan_init(ocelot);
Horatiu Vulturb5962292019-05-31 09:16:56 +02001395 ocelot_ace_init(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001396
1397 for (port = 0; port < ocelot->num_phys_ports; port++) {
1398 /* Clear all counters (5 groups) */
1399 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1400 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1401 SYS_STAT_CFG);
1402 }
1403
1404 /* Only use S-Tag */
1405 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1406
1407 /* Aggregation mode */
1408 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1409 ANA_AGGR_CFG_AC_DMAC_ENA |
1410 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1411 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1412
1413 /* Set MAC age time to default value. The entry is aged after
1414 * 2*AGE_PERIOD
1415 */
1416 ocelot_write(ocelot,
1417 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1418 ANA_AUTOAGE);
1419
1420 /* Disable learning for frames discarded by VLAN ingress filtering */
1421 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1422
1423 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1424 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1425 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1426
1427 /* Setup flooding PGIDs */
1428 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1429 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1430 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1431 ANA_FLOODING, 0);
1432 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1433 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1434 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1435 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1436 ANA_FLOODING_IPMC);
1437
1438 for (port = 0; port < ocelot->num_phys_ports; port++) {
1439 /* Transmit the frame to the local port. */
1440 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1441 /* Do not forward BPDU frames to the front ports. */
1442 ocelot_write_gix(ocelot,
1443 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1444 ANA_PORT_CPU_FWD_BPDU_CFG,
1445 port);
1446 /* Ensure bridging is disabled */
1447 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1448 }
1449
Alexandre Bellonia556c762018-05-14 22:04:57 +02001450 /* Allow broadcast MAC frames. */
1451 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
1452 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1453
1454 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1455 }
1456 ocelot_write_rix(ocelot,
1457 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1458 ANA_PGID_PGID, PGID_MC);
1459 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1460 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1461
Alexandre Bellonia556c762018-05-14 22:04:57 +02001462 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
1463 * registers endianness.
1464 */
1465 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1466 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1467 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1468 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1469 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1470 ANA_CPUQ_CFG_CPUQ_LRN(2) |
1471 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1472 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1473 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1474 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1475 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1476 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1477 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1478 for (i = 0; i < 16; i++)
1479 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1480 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1481 ANA_CPUQ_8021_CFG, i);
1482
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001483 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001484 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1485 OCELOT_STATS_CHECK_DELAY);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001486
Alexandre Bellonia556c762018-05-14 22:04:57 +02001487 return 0;
1488}
1489EXPORT_SYMBOL(ocelot_init);
1490
1491void ocelot_deinit(struct ocelot *ocelot)
1492{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001493 struct ocelot_port *port;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001494 int i;
1495
Claudiu Manoilc5d13962019-07-25 16:33:18 +03001496 cancel_delayed_work(&ocelot->stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001497 destroy_workqueue(ocelot->stats_queue);
1498 mutex_destroy(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001499
1500 for (i = 0; i < ocelot->num_phys_ports; i++) {
1501 port = ocelot->ports[i];
Yangbo Lub049da12019-11-27 15:27:57 +08001502 skb_queue_purge(&port->tx_skbs);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001503 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001504}
1505EXPORT_SYMBOL(ocelot_deinit);
1506
1507MODULE_LICENSE("Dual MIT/GPL");