blob: 3af8e607c8a9f2ed82b35bdf4c90bff4550cb96f [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{
63 ocelot_mact_select(ocelot, mac, vid);
64
65 /* Issue a write command */
66 ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
67 ANA_TABLES_MACACCESS_DEST_IDX(port) |
68 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
69 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
70 ANA_TABLES_MACACCESS);
71
72 return ocelot_mact_wait_for_completion(ocelot);
73}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030074EXPORT_SYMBOL(ocelot_mact_learn);
Alexandre Bellonia556c762018-05-14 22:04:57 +020075
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030076int ocelot_mact_forget(struct ocelot *ocelot,
77 const unsigned char mac[ETH_ALEN], unsigned int vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +020078{
79 ocelot_mact_select(ocelot, mac, vid);
80
81 /* Issue a forget command */
82 ocelot_write(ocelot,
83 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
84 ANA_TABLES_MACACCESS);
85
86 return ocelot_mact_wait_for_completion(ocelot);
87}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030088EXPORT_SYMBOL(ocelot_mact_forget);
Alexandre Bellonia556c762018-05-14 22:04:57 +020089
90static void ocelot_mact_init(struct ocelot *ocelot)
91{
92 /* Configure the learning mode entries attributes:
93 * - Do not copy the frame to the CPU extraction queues.
94 * - Use the vlan and mac_cpoy for dmac lookup.
95 */
96 ocelot_rmw(ocelot, 0,
97 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
98 | ANA_AGENCTRL_LEARN_FWD_KILL
99 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
100 ANA_AGENCTRL);
101
102 /* Clear the MAC table */
103 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
104}
105
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200106static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
Horatiu Vulturb5962292019-05-31 09:16:56 +0200107{
108 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
109 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200110 ANA_PORT_VCAP_S2_CFG, port);
Xiaoliang Yang75944fd2020-10-02 15:02:23 +0300111
112 ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
113 ANA_PORT_VCAP_CFG, port);
Xiaoliang Yang2f17c052020-10-02 15:02:24 +0300114
115 ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
116 REW_PORT_CFG_ES0_EN,
117 REW_PORT_CFG, port);
Horatiu Vulturb5962292019-05-31 09:16:56 +0200118}
119
Steen Hegelund639c1b22018-12-20 14:16:31 +0100120static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
121{
122 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
123}
124
Alexandre Bellonia556c762018-05-14 22:04:57 +0200125static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
126{
Steen Hegelund639c1b22018-12-20 14:16:31 +0100127 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200128
Steen Hegelund639c1b22018-12-20 14:16:31 +0100129 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
130 ocelot,
131 val,
132 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
133 ANA_TABLES_VLANACCESS_CMD_IDLE,
134 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200135}
136
Antoine Tenart71425292018-06-26 14:28:49 +0200137static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
138{
139 /* Select the VID to configure */
140 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
141 ANA_TABLES_VLANTIDX);
142 /* Set the vlan port members mask and issue a write command */
143 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
144 ANA_TABLES_VLANACCESS_CMD_WRITE,
145 ANA_TABLES_VLANACCESS);
146
147 return ocelot_vlant_wait_for_completion(ocelot);
148}
149
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200150static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
151 struct ocelot_vlan native_vlan)
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200152{
153 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300154 u32 val = 0;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200155
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200156 ocelot_port->native_vlan = native_vlan;
157
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200158 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
Antoine Tenart71425292018-06-26 14:28:49 +0200159 REW_PORT_VLAN_CFG_PORT_VID_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200160 REW_PORT_VLAN_CFG, port);
161
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300162 if (ocelot_port->vlan_aware) {
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200163 if (native_vlan.valid)
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300164 /* Tag all frames except when VID == DEFAULT_VLAN */
165 val = REW_TAG_CFG_TAG_CFG(1);
166 else
167 /* Tag all frames */
168 val = REW_TAG_CFG_TAG_CFG(3);
169 } else {
170 /* Port tagging disabled. */
171 val = REW_TAG_CFG_TAG_CFG(0);
172 }
173 ocelot_rmw_gix(ocelot, val,
174 REW_TAG_CFG_TAG_CFG_M,
175 REW_TAG_CFG, port);
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200176}
177
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200178/* Default vlan to clasify for untagged frames (may be zero) */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200179static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
180 struct ocelot_vlan pvid_vlan)
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200181{
182 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200183 u32 val = 0;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200184
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200185 ocelot_port->pvid_vlan = pvid_vlan;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200186
187 if (!ocelot_port->vlan_aware)
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200188 pvid_vlan.vid = 0;
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200189
190 ocelot_rmw_gix(ocelot,
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200191 ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200192 ANA_PORT_VLAN_CFG_VLAN_VID_M,
193 ANA_PORT_VLAN_CFG, port);
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200194
195 /* If there's no pvid, we should drop not only untagged traffic (which
196 * happens automatically), but also 802.1p traffic which gets
197 * classified to VLAN 0, but that is always in our RX filter, so it
198 * would get accepted were it not for this setting.
199 */
200 if (!pvid_vlan.valid && ocelot_port->vlan_aware)
201 val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
202 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
203
204 ocelot_rmw_gix(ocelot, val,
205 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
206 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
207 ANA_PORT_DROP_CFG, port);
Vladimir Oltean75e5a552020-10-31 12:29:10 +0200208}
209
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300210int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200211 bool vlan_aware)
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300212{
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200213 struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300214 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200215 struct ocelot_vcap_filter *filter;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300216 u32 val;
217
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200218 list_for_each_entry(filter, &block->rules, list) {
219 if (filter->ingress_port_mask & BIT(port) &&
220 filter->action.vid_replace_ena) {
221 dev_err(ocelot->dev,
222 "Cannot change VLAN state with vlan modify rules active\n");
223 return -EBUSY;
Vladimir Oltean70edfae2020-10-08 14:56:58 +0300224 }
Vladimir Oltean70edfae2020-10-08 14:56:58 +0300225 }
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300226
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300227 ocelot_port->vlan_aware = vlan_aware;
228
229 if (vlan_aware)
230 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
231 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
232 else
233 val = 0;
234 ocelot_rmw_gix(ocelot, val,
235 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
236 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
237 ANA_PORT_VLAN_CFG, port);
238
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200239 ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
240 ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
Vladimir Oltean2e554a72020-10-03 01:06:46 +0300241
242 return 0;
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300243}
244EXPORT_SYMBOL(ocelot_port_vlan_filtering);
245
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200246int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
247 bool untagged)
248{
249 struct ocelot_port *ocelot_port = ocelot->ports[port];
250
251 /* Deny changing the native VLAN, but always permit deleting it */
252 if (untagged && ocelot_port->native_vlan.vid != vid &&
253 ocelot_port->native_vlan.valid) {
254 dev_err(ocelot->dev,
255 "Port already has a native VLAN: %d\n",
256 ocelot_port->native_vlan.vid);
257 return -EBUSY;
258 }
259
260 return 0;
261}
262EXPORT_SYMBOL(ocelot_vlan_prepare);
263
Vladimir Oltean5e256362019-11-14 17:03:27 +0200264int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
265 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200266{
Antoine Tenart71425292018-06-26 14:28:49 +0200267 int ret;
268
Antoine Tenart71425292018-06-26 14:28:49 +0200269 /* Make the port a member of the VLAN */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200270 ocelot->vlan_mask[vid] |= BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200271 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
272 if (ret)
273 return ret;
274
275 /* Default ingress vlan classification */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200276 if (pvid) {
277 struct ocelot_vlan pvid_vlan;
278
279 pvid_vlan.vid = vid;
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200280 pvid_vlan.valid = true;
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200281 ocelot_port_set_pvid(ocelot, port, pvid_vlan);
282 }
Antoine Tenart71425292018-06-26 14:28:49 +0200283
284 /* Untagged egress vlan clasification */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200285 if (untagged) {
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200286 struct ocelot_vlan native_vlan;
287
288 native_vlan.vid = vid;
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200289 native_vlan.valid = true;
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200290 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
Vladimir Olteanb9cd75e2019-10-26 21:04:27 +0300291 }
Antoine Tenart71425292018-06-26 14:28:49 +0200292
Antoine Tenart71425292018-06-26 14:28:49 +0200293 return 0;
294}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200295EXPORT_SYMBOL(ocelot_vlan_add);
Antoine Tenart71425292018-06-26 14:28:49 +0200296
Vladimir Oltean5e256362019-11-14 17:03:27 +0200297int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
Vladimir Oltean98559342019-11-09 15:02:48 +0200298{
299 struct ocelot_port *ocelot_port = ocelot->ports[port];
300 int ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200301
302 /* Stop the port from being a member of the vlan */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200303 ocelot->vlan_mask[vid] &= ~BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200304 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
305 if (ret)
306 return ret;
307
Vladimir Olteanbe0576f2020-10-31 12:29:14 +0200308 /* Ingress */
309 if (ocelot_port->pvid_vlan.vid == vid) {
310 struct ocelot_vlan pvid_vlan = {0};
311
312 ocelot_port_set_pvid(ocelot, port, pvid_vlan);
313 }
314
Antoine Tenart71425292018-06-26 14:28:49 +0200315 /* Egress */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200316 if (ocelot_port->native_vlan.vid == vid) {
Vladimir Olteane2b2e832020-10-31 12:29:13 +0200317 struct ocelot_vlan native_vlan = {0};
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200318
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200319 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
320 }
Antoine Tenart71425292018-06-26 14:28:49 +0200321
322 return 0;
323}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200324EXPORT_SYMBOL(ocelot_vlan_del);
Antoine Tenart71425292018-06-26 14:28:49 +0200325
Alexandre Bellonia556c762018-05-14 22:04:57 +0200326static void ocelot_vlan_init(struct ocelot *ocelot)
327{
Antoine Tenart71425292018-06-26 14:28:49 +0200328 u16 port, vid;
329
Alexandre Bellonia556c762018-05-14 22:04:57 +0200330 /* Clear VLAN table, by default all ports are members of all VLANs */
331 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
332 ANA_TABLES_VLANACCESS);
333 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200334
335 /* Configure the port VLAN memberships */
336 for (vid = 1; vid < VLAN_N_VID; vid++) {
337 ocelot->vlan_mask[vid] = 0;
338 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
339 }
340
341 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
342 * traffic. It is added automatically if 8021q module is loaded, but
343 * we can't rely on it since module may be not loaded.
344 */
345 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
346 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
347
Antoine Tenart71425292018-06-26 14:28:49 +0200348 /* Set vlan ingress filter mask to all ports but the CPU port by
349 * default.
350 */
Vladimir Oltean714d0ff2019-11-09 15:02:55 +0200351 ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
352 ANA_VLANMASK);
Antoine Tenart71425292018-06-26 14:28:49 +0200353
354 for (port = 0; port < ocelot->num_phys_ports; port++) {
355 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
356 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
357 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200358}
359
Vladimir Oltean5e256362019-11-14 17:03:27 +0200360void ocelot_adjust_link(struct ocelot *ocelot, int port,
361 struct phy_device *phydev)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200362{
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200363 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +0200364 int speed, mode = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200365
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200366 switch (phydev->speed) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200367 case SPEED_10:
368 speed = OCELOT_SPEED_10;
369 break;
370 case SPEED_100:
371 speed = OCELOT_SPEED_100;
372 break;
373 case SPEED_1000:
374 speed = OCELOT_SPEED_1000;
375 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
376 break;
377 case SPEED_2500:
378 speed = OCELOT_SPEED_2500;
379 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
380 break;
381 default:
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200382 dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
383 port, phydev->speed);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200384 return;
385 }
386
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200387 phy_print_status(phydev);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200388
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200389 if (!phydev->link)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200390 return;
391
392 /* Only full duplex supported for now */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200393 ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200394 mode, DEV_MAC_MODE_CFG);
395
Vladimir Oltean1ba8f652020-02-29 16:31:11 +0200396 /* Disable HDX fast control */
397 ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
398 DEV_PORT_MISC);
399
400 /* SGMII only for now */
401 ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
402 PCS1G_MODE_CFG);
403 ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
404
405 /* Enable PCS */
406 ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
407
408 /* No aneg on SGMII */
409 ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
410
411 /* No loopback */
412 ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200413
Alexandre Bellonia556c762018-05-14 22:04:57 +0200414 /* Enable MAC module */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200415 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200416 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
417
418 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
419 * reset */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200420 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
Alexandre Bellonia556c762018-05-14 22:04:57 +0200421 DEV_CLOCK_CFG);
422
Alexandre Bellonia556c762018-05-14 22:04:57 +0200423 /* No PFC */
424 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200425 ANA_PFC_PFC_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200426
Alexandre Bellonia556c762018-05-14 22:04:57 +0200427 /* Core: Enable port for frame transfer */
Vladimir Oltean886e1382020-07-13 19:57:03 +0300428 ocelot_fields_write(ocelot, port,
429 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200430
431 /* Flow control */
432 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
433 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
434 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
435 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
436 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200437 SYS_MAC_FC_CFG, port);
438 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200439}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200440EXPORT_SYMBOL(ocelot_adjust_link);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200441
Vladimir Oltean5e256362019-11-14 17:03:27 +0200442void ocelot_port_enable(struct ocelot *ocelot, int port,
443 struct phy_device *phy)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200444{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200445 /* Enable receiving frames on the port, and activate auto-learning of
446 * MAC addresses.
447 */
448 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
449 ANA_PORT_PORT_CFG_RECV_ENA |
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200450 ANA_PORT_PORT_CFG_PORTID_VAL(port),
451 ANA_PORT_PORT_CFG, port);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200452}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200453EXPORT_SYMBOL(ocelot_port_enable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200454
Vladimir Oltean5e256362019-11-14 17:03:27 +0200455void ocelot_port_disable(struct ocelot *ocelot, int port)
Vladimir Oltean889b8952019-11-09 15:02:57 +0200456{
457 struct ocelot_port *ocelot_port = ocelot->ports[port];
458
459 ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
Vladimir Oltean886e1382020-07-13 19:57:03 +0300460 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200461}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200462EXPORT_SYMBOL(ocelot_port_disable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200463
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300464void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
465 struct sk_buff *clone)
Yangbo Lu400928b2019-11-20 16:23:16 +0800466{
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300467 struct ocelot_port *ocelot_port = ocelot->ports[port];
Yangbo Lu400928b2019-11-20 16:23:16 +0800468
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300469 spin_lock(&ocelot_port->ts_id_lock);
Vladimir Oltean65652432020-09-18 04:07:24 +0300470
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300471 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
472 /* Store timestamp ID in cb[0] of sk_buff */
473 clone->cb[0] = ocelot_port->ts_id;
474 ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
475 skb_queue_tail(&ocelot_port->tx_skbs, clone);
Vladimir Oltean65652432020-09-18 04:07:24 +0300476
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300477 spin_unlock(&ocelot_port->ts_id_lock);
Yangbo Lu400928b2019-11-20 16:23:16 +0800478}
479EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
480
Yangbo Lue23a7b32019-11-20 16:23:15 +0800481static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
482 struct timespec64 *ts)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200483{
484 unsigned long flags;
485 u32 val;
486
487 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
488
489 /* Read current PTP time to get seconds */
490 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
491
492 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
493 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
494 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
495 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
496
497 /* Read packet HW timestamp from FIFO */
498 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
499 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
500
501 /* Sec has incremented since the ts was registered */
502 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
503 ts->tv_sec--;
504
505 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
506}
Yangbo Lue23a7b32019-11-20 16:23:15 +0800507
508void ocelot_get_txtstamp(struct ocelot *ocelot)
509{
510 int budget = OCELOT_PTP_QUEUE_SZ;
511
512 while (budget--) {
Yangbo Lub049da12019-11-27 15:27:57 +0800513 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800514 struct skb_shared_hwtstamps shhwtstamps;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800515 struct ocelot_port *port;
516 struct timespec64 ts;
Yangbo Lub049da12019-11-27 15:27:57 +0800517 unsigned long flags;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800518 u32 val, id, txport;
519
520 val = ocelot_read(ocelot, SYS_PTP_STATUS);
521
522 /* Check if a timestamp can be retrieved */
523 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
524 break;
525
526 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
527
528 /* Retrieve the ts ID and Tx port */
529 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
530 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
531
532 /* Retrieve its associated skb */
533 port = ocelot->ports[txport];
534
Yangbo Lub049da12019-11-27 15:27:57 +0800535 spin_lock_irqsave(&port->tx_skbs.lock, flags);
536
537 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
538 if (skb->cb[0] != id)
Yangbo Lue23a7b32019-11-20 16:23:15 +0800539 continue;
Yangbo Lub049da12019-11-27 15:27:57 +0800540 __skb_unlink(skb, &port->tx_skbs);
541 skb_match = skb;
Yangbo Lufc62c092019-11-27 15:27:56 +0800542 break;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800543 }
544
Yangbo Lub049da12019-11-27 15:27:57 +0800545 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
546
laurent brando5fd82202020-07-27 18:26:14 +0800547 /* Get the h/w timestamp */
548 ocelot_get_hwtimestamp(ocelot, &ts);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800549
Yangbo Lub049da12019-11-27 15:27:57 +0800550 if (unlikely(!skb_match))
Yangbo Lue23a7b32019-11-20 16:23:15 +0800551 continue;
552
Yangbo Lue23a7b32019-11-20 16:23:15 +0800553 /* Set the timestamp into the skb */
554 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
555 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300556 skb_complete_tx_timestamp(skb_match, &shhwtstamps);
laurent brando5fd82202020-07-27 18:26:14 +0800557
558 /* Next ts */
559 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800560 }
561}
562EXPORT_SYMBOL(ocelot_get_txtstamp);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200563
Vladimir Oltean5e256362019-11-14 17:03:27 +0200564int ocelot_fdb_add(struct ocelot *ocelot, int port,
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300565 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200566{
Vladimir Oltean471beb12020-06-21 14:46:00 +0300567 int pgid = port;
568
569 if (port == ocelot->npi)
570 pgid = PGID_CPU;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200571
Vladimir Oltean471beb12020-06-21 14:46:00 +0300572 return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200573}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200574EXPORT_SYMBOL(ocelot_fdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200575
Vladimir Oltean5e256362019-11-14 17:03:27 +0200576int ocelot_fdb_del(struct ocelot *ocelot, int port,
577 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200578{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200579 return ocelot_mact_forget(ocelot, addr, vid);
580}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200581EXPORT_SYMBOL(ocelot_fdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200582
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300583int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
584 bool is_static, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200585{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200586 struct ocelot_dump_ctx *dump = data;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200587 u32 portid = NETLINK_CB(dump->cb->skb).portid;
588 u32 seq = dump->cb->nlh->nlmsg_seq;
589 struct nlmsghdr *nlh;
590 struct ndmsg *ndm;
591
592 if (dump->idx < dump->cb->args[2])
593 goto skip;
594
595 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
596 sizeof(*ndm), NLM_F_MULTI);
597 if (!nlh)
598 return -EMSGSIZE;
599
600 ndm = nlmsg_data(nlh);
601 ndm->ndm_family = AF_BRIDGE;
602 ndm->ndm_pad1 = 0;
603 ndm->ndm_pad2 = 0;
604 ndm->ndm_flags = NTF_SELF;
605 ndm->ndm_type = 0;
606 ndm->ndm_ifindex = dump->dev->ifindex;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200607 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200608
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200609 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200610 goto nla_put_failure;
611
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200612 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200613 goto nla_put_failure;
614
615 nlmsg_end(dump->skb, nlh);
616
617skip:
618 dump->idx++;
619 return 0;
620
621nla_put_failure:
622 nlmsg_cancel(dump->skb, nlh);
623 return -EMSGSIZE;
624}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300625EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200626
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200627static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
628 struct ocelot_mact_entry *entry)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200629{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200630 u32 val, dst, macl, mach;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200631 char mac[ETH_ALEN];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200632
633 /* Set row and column to read from */
634 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
635 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
636
637 /* Issue a read command */
638 ocelot_write(ocelot,
639 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
640 ANA_TABLES_MACACCESS);
641
642 if (ocelot_mact_wait_for_completion(ocelot))
643 return -ETIMEDOUT;
644
645 /* Read the entry flags */
646 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
647 if (!(val & ANA_TABLES_MACACCESS_VALID))
648 return -EINVAL;
649
650 /* If the entry read has another port configured as its destination,
651 * do not report it.
652 */
653 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200654 if (dst != port)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200655 return -EINVAL;
656
657 /* Get the entry's MAC address and VLAN id */
658 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
659 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
660
661 mac[0] = (mach >> 8) & 0xff;
662 mac[1] = (mach >> 0) & 0xff;
663 mac[2] = (macl >> 24) & 0xff;
664 mac[3] = (macl >> 16) & 0xff;
665 mac[4] = (macl >> 8) & 0xff;
666 mac[5] = (macl >> 0) & 0xff;
667
668 entry->vid = (mach >> 16) & 0xfff;
669 ether_addr_copy(entry->mac, mac);
670
671 return 0;
672}
673
Vladimir Oltean5e256362019-11-14 17:03:27 +0200674int ocelot_fdb_dump(struct ocelot *ocelot, int port,
675 dsa_fdb_dump_cb_t *cb, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200676{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200677 int i, j;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200678
Vladimir Oltean21ce7f32020-05-04 01:20:26 +0300679 /* Loop through all the mac tables entries. */
680 for (i = 0; i < ocelot->num_mact_rows; i++) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200681 for (j = 0; j < 4; j++) {
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200682 struct ocelot_mact_entry entry;
683 bool is_static;
684 int ret;
685
686 ret = ocelot_mact_read(ocelot, port, i, j, &entry);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200687 /* If the entry is invalid (wrong port, invalid...),
688 * skip it.
689 */
690 if (ret == -EINVAL)
691 continue;
692 else if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200693 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200694
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200695 is_static = (entry.type == ENTRYTYPE_LOCKED);
696
697 ret = cb(entry.mac, entry.vid, is_static, data);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200698 if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200699 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200700 }
701 }
702
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200703 return 0;
704}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200705EXPORT_SYMBOL(ocelot_fdb_dump);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200706
Yangbo Luf1459222019-11-20 16:23:14 +0800707int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200708{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200709 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
710 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
711}
Yangbo Luf1459222019-11-20 16:23:14 +0800712EXPORT_SYMBOL(ocelot_hwstamp_get);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200713
Yangbo Luf1459222019-11-20 16:23:14 +0800714int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200715{
Vladimir Oltean306fd442019-11-09 15:02:50 +0200716 struct ocelot_port *ocelot_port = ocelot->ports[port];
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200717 struct hwtstamp_config cfg;
718
719 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
720 return -EFAULT;
721
722 /* reserved for future extensions */
723 if (cfg.flags)
724 return -EINVAL;
725
726 /* Tx type sanity check */
727 switch (cfg.tx_type) {
728 case HWTSTAMP_TX_ON:
Vladimir Oltean306fd442019-11-09 15:02:50 +0200729 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200730 break;
731 case HWTSTAMP_TX_ONESTEP_SYNC:
732 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
733 * need to update the origin time.
734 */
Vladimir Oltean306fd442019-11-09 15:02:50 +0200735 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200736 break;
737 case HWTSTAMP_TX_OFF:
Vladimir Oltean306fd442019-11-09 15:02:50 +0200738 ocelot_port->ptp_cmd = 0;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200739 break;
740 default:
741 return -ERANGE;
742 }
743
744 mutex_lock(&ocelot->ptp_lock);
745
746 switch (cfg.rx_filter) {
747 case HWTSTAMP_FILTER_NONE:
748 break;
749 case HWTSTAMP_FILTER_ALL:
750 case HWTSTAMP_FILTER_SOME:
751 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
752 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
753 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
754 case HWTSTAMP_FILTER_NTP_ALL:
755 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
756 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
757 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
758 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
759 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
760 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
761 case HWTSTAMP_FILTER_PTP_V2_EVENT:
762 case HWTSTAMP_FILTER_PTP_V2_SYNC:
763 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
764 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
765 break;
766 default:
767 mutex_unlock(&ocelot->ptp_lock);
768 return -ERANGE;
769 }
770
771 /* Commit back the result & save it */
772 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
773 mutex_unlock(&ocelot->ptp_lock);
774
775 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
776}
Yangbo Luf1459222019-11-20 16:23:14 +0800777EXPORT_SYMBOL(ocelot_hwstamp_set);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200778
Vladimir Oltean5e256362019-11-14 17:03:27 +0200779void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200780{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200781 int i;
782
783 if (sset != ETH_SS_STATS)
784 return;
785
786 for (i = 0; i < ocelot->num_stats; i++)
787 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
788 ETH_GSTRING_LEN);
789}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200790EXPORT_SYMBOL(ocelot_get_strings);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200791
Claudiu Manoil1e1caa92019-04-16 17:51:59 +0300792static void ocelot_update_stats(struct ocelot *ocelot)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200793{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200794 int i, j;
795
796 mutex_lock(&ocelot->stats_lock);
797
798 for (i = 0; i < ocelot->num_phys_ports; i++) {
799 /* Configure the port to read the stats from */
800 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
801
802 for (j = 0; j < ocelot->num_stats; j++) {
803 u32 val;
804 unsigned int idx = i * ocelot->num_stats + j;
805
806 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
807 ocelot->stats_layout[j].offset);
808
809 if (val < (ocelot->stats[idx] & U32_MAX))
810 ocelot->stats[idx] += (u64)1 << 32;
811
812 ocelot->stats[idx] = (ocelot->stats[idx] &
813 ~(u64)U32_MAX) + val;
814 }
815 }
816
Claudiu Manoil1e1caa92019-04-16 17:51:59 +0300817 mutex_unlock(&ocelot->stats_lock);
818}
819
820static void ocelot_check_stats_work(struct work_struct *work)
821{
822 struct delayed_work *del_work = to_delayed_work(work);
823 struct ocelot *ocelot = container_of(del_work, struct ocelot,
824 stats_work);
825
826 ocelot_update_stats(ocelot);
827
Alexandre Bellonia556c762018-05-14 22:04:57 +0200828 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
829 OCELOT_STATS_CHECK_DELAY);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200830}
831
Vladimir Oltean5e256362019-11-14 17:03:27 +0200832void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200833{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200834 int i;
835
836 /* check and update now */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +0300837 ocelot_update_stats(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200838
839 /* Copy all counters */
840 for (i = 0; i < ocelot->num_stats; i++)
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200841 *data++ = ocelot->stats[port * ocelot->num_stats + i];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200842}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200843EXPORT_SYMBOL(ocelot_get_ethtool_stats);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200844
Vladimir Oltean5e256362019-11-14 17:03:27 +0200845int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
Vladimir Olteanc7282d32019-11-09 15:02:54 +0200846{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200847 if (sset != ETH_SS_STATS)
848 return -EOPNOTSUPP;
Vladimir Olteanc7282d32019-11-09 15:02:54 +0200849
Alexandre Bellonia556c762018-05-14 22:04:57 +0200850 return ocelot->num_stats;
851}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200852EXPORT_SYMBOL(ocelot_get_sset_count);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200853
Vladimir Oltean5e256362019-11-14 17:03:27 +0200854int ocelot_get_ts_info(struct ocelot *ocelot, int port,
855 struct ethtool_ts_info *info)
Vladimir Olteanc7282d32019-11-09 15:02:54 +0200856{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200857 info->phc_index = ocelot->ptp_clock ?
858 ptp_clock_index(ocelot->ptp_clock) : -1;
Yangbo Lud2b09a82020-04-20 10:46:46 +0800859 if (info->phc_index == -1) {
860 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
861 SOF_TIMESTAMPING_RX_SOFTWARE |
862 SOF_TIMESTAMPING_SOFTWARE;
863 return 0;
864 }
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200865 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
866 SOF_TIMESTAMPING_RX_SOFTWARE |
867 SOF_TIMESTAMPING_SOFTWARE |
868 SOF_TIMESTAMPING_TX_HARDWARE |
869 SOF_TIMESTAMPING_RX_HARDWARE |
870 SOF_TIMESTAMPING_RAW_HARDWARE;
871 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
872 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
873 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
874
875 return 0;
876}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200877EXPORT_SYMBOL(ocelot_get_ts_info);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200878
Vladimir Oltean5e256362019-11-14 17:03:27 +0200879void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200880{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200881 u32 port_cfg;
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200882 int p, i;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200883
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200884 if (!(BIT(port) & ocelot->bridge_mask))
885 return;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200886
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200887 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200888
889 switch (state) {
890 case BR_STATE_FORWARDING:
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200891 ocelot->bridge_fwd_mask |= BIT(port);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500892 fallthrough;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200893 case BR_STATE_LEARNING:
894 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
895 break;
896
897 default:
898 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200899 ocelot->bridge_fwd_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200900 break;
901 }
902
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200903 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200904
905 /* Apply FWD mask. The loop is needed to add/remove the current port as
906 * a source for the other ports.
907 */
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200908 for (p = 0; p < ocelot->num_phys_ports; p++) {
Vladimir Oltean69df5782020-02-29 16:50:02 +0200909 if (ocelot->bridge_fwd_mask & BIT(p)) {
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200910 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200911
912 for (i = 0; i < ocelot->num_phys_ports; i++) {
913 unsigned long bond_mask = ocelot->lags[i];
914
915 if (!bond_mask)
916 continue;
917
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200918 if (bond_mask & BIT(p)) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200919 mask &= ~bond_mask;
920 break;
921 }
922 }
923
Vladimir Olteanc9d22032019-11-09 15:03:01 +0200924 ocelot_write_rix(ocelot, mask,
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200925 ANA_PGID_PGID, PGID_SRC + p);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200926 } else {
Vladimir Oltean69df5782020-02-29 16:50:02 +0200927 ocelot_write_rix(ocelot, 0,
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200928 ANA_PGID_PGID, PGID_SRC + p);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200929 }
930 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200931}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200932EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200933
Vladimir Oltean5e256362019-11-14 17:03:27 +0200934void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
Vladimir Oltean4bda1412019-11-09 15:02:51 +0200935{
Vladimir Olteanc0d7ecc2020-05-04 01:20:27 +0300936 unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
937
938 /* Setting AGE_PERIOD to zero effectively disables automatic aging,
939 * which is clearly not what our intention is. So avoid that.
940 */
941 if (!age_period)
942 age_period = 1;
943
944 ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200945}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200946EXPORT_SYMBOL(ocelot_set_ageing_time);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200947
Alexandre Bellonia556c762018-05-14 22:04:57 +0200948static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
949 const unsigned char *addr,
950 u16 vid)
951{
952 struct ocelot_multicast *mc;
953
954 list_for_each_entry(mc, &ocelot->multicast, list) {
955 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
956 return mc;
957 }
958
959 return NULL;
960}
961
Vladimir Oltean9403c152020-06-21 14:46:03 +0300962static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
963{
964 if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
965 return ENTRYTYPE_MACv4;
966 if (addr[0] == 0x33 && addr[1] == 0x33)
967 return ENTRYTYPE_MACv6;
Vladimir Oltean7c313142020-10-29 04:27:34 +0200968 return ENTRYTYPE_LOCKED;
Vladimir Oltean9403c152020-06-21 14:46:03 +0300969}
970
Vladimir Olteane5d1f892020-10-29 04:27:38 +0200971static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
972 unsigned long ports)
Vladimir Oltean9403c152020-06-21 14:46:03 +0300973{
Vladimir Olteane5d1f892020-10-29 04:27:38 +0200974 struct ocelot_pgid *pgid;
975
976 pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
977 if (!pgid)
978 return ERR_PTR(-ENOMEM);
979
980 pgid->ports = ports;
981 pgid->index = index;
982 refcount_set(&pgid->refcount, 1);
983 list_add_tail(&pgid->list, &ocelot->pgids);
984
985 return pgid;
986}
987
988static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
989{
990 if (!refcount_dec_and_test(&pgid->refcount))
991 return;
992
993 list_del(&pgid->list);
994 kfree(pgid);
995}
996
997static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
998 const struct ocelot_multicast *mc)
999{
1000 struct ocelot_pgid *pgid;
1001 int index;
Vladimir Oltean9403c152020-06-21 14:46:03 +03001002
1003 /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1004 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1005 * destination mask table (PGID), the destination set is programmed as
1006 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1007 */
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001008 if (mc->entry_type == ENTRYTYPE_MACv4 ||
1009 mc->entry_type == ENTRYTYPE_MACv6)
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001010 return ocelot_pgid_alloc(ocelot, 0, mc->ports);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001011
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001012 list_for_each_entry(pgid, &ocelot->pgids, list) {
1013 /* When searching for a nonreserved multicast PGID, ignore the
1014 * dummy PGID of zero that we have for MACv4/MACv6 entries
1015 */
1016 if (pgid->index && pgid->ports == mc->ports) {
1017 refcount_inc(&pgid->refcount);
1018 return pgid;
1019 }
1020 }
1021
1022 /* Search for a free index in the nonreserved multicast PGID area */
1023 for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001024 bool used = false;
1025
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001026 list_for_each_entry(pgid, &ocelot->pgids, list) {
1027 if (pgid->index == index) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001028 used = true;
1029 break;
1030 }
1031 }
1032
1033 if (!used)
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001034 return ocelot_pgid_alloc(ocelot, index, mc->ports);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001035 }
1036
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001037 return ERR_PTR(-ENOSPC);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001038}
1039
1040static void ocelot_encode_ports_to_mdb(unsigned char *addr,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001041 struct ocelot_multicast *mc)
Vladimir Oltean9403c152020-06-21 14:46:03 +03001042{
Vladimir Olteanebbd8602020-10-29 04:27:35 +02001043 ether_addr_copy(addr, mc->addr);
Vladimir Oltean9403c152020-06-21 14:46:03 +03001044
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001045 if (mc->entry_type == ENTRYTYPE_MACv4) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001046 addr[0] = 0;
1047 addr[1] = mc->ports >> 8;
1048 addr[2] = mc->ports & 0xff;
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001049 } else if (mc->entry_type == ENTRYTYPE_MACv6) {
Vladimir Oltean9403c152020-06-21 14:46:03 +03001050 addr[0] = mc->ports >> 8;
1051 addr[1] = mc->ports & 0xff;
1052 }
1053}
1054
Vladimir Oltean209edf92020-06-21 14:46:01 +03001055int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1056 const struct switchdev_obj_port_mdb *mdb)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001057{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001058 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001059 struct ocelot_multicast *mc;
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001060 struct ocelot_pgid *pgid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001061 u16 vid = mdb->vid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001062
Vladimir Oltean471beb12020-06-21 14:46:00 +03001063 if (port == ocelot->npi)
1064 port = ocelot->num_phys_ports;
1065
Alexandre Bellonia556c762018-05-14 22:04:57 +02001066 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1067 if (!mc) {
Vladimir Oltean728e69a2020-10-29 04:27:36 +02001068 /* New entry */
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001069 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1070 if (!mc)
1071 return -ENOMEM;
1072
1073 mc->entry_type = ocelot_classify_mdb(mdb->addr);
1074 ether_addr_copy(mc->addr, mdb->addr);
1075 mc->vid = vid;
1076
Alexandre Bellonia556c762018-05-14 22:04:57 +02001077 list_add_tail(&mc->list, &ocelot->multicast);
Vladimir Oltean728e69a2020-10-29 04:27:36 +02001078 } else {
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001079 /* Existing entry. Clean up the current port mask from
1080 * hardware now, because we'll be modifying it.
1081 */
1082 ocelot_pgid_free(ocelot, mc->pgid);
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001083 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001084 ocelot_mact_forget(ocelot, addr, vid);
1085 }
1086
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001087 mc->ports |= BIT(port);
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001088
1089 pgid = ocelot_mdb_get_pgid(ocelot, mc);
1090 if (IS_ERR(pgid)) {
1091 dev_err(ocelot->dev,
1092 "Cannot allocate PGID for mdb %pM vid %d\n",
1093 mc->addr, mc->vid);
1094 devm_kfree(ocelot->dev, mc);
1095 return PTR_ERR(pgid);
1096 }
1097 mc->pgid = pgid;
1098
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001099 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001100
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001101 if (mc->entry_type != ENTRYTYPE_MACv4 &&
1102 mc->entry_type != ENTRYTYPE_MACv6)
1103 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1104 pgid->index);
1105
1106 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001107 mc->entry_type);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001108}
Vladimir Oltean209edf92020-06-21 14:46:01 +03001109EXPORT_SYMBOL(ocelot_port_mdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001110
Vladimir Oltean209edf92020-06-21 14:46:01 +03001111int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1112 const struct switchdev_obj_port_mdb *mdb)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001113{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001114 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001115 struct ocelot_multicast *mc;
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001116 struct ocelot_pgid *pgid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001117 u16 vid = mdb->vid;
1118
Vladimir Oltean471beb12020-06-21 14:46:00 +03001119 if (port == ocelot->npi)
1120 port = ocelot->num_phys_ports;
1121
Alexandre Bellonia556c762018-05-14 22:04:57 +02001122 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1123 if (!mc)
1124 return -ENOENT;
1125
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001126 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001127 ocelot_mact_forget(ocelot, addr, vid);
1128
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001129 ocelot_pgid_free(ocelot, mc->pgid);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001130 mc->ports &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001131 if (!mc->ports) {
1132 list_del(&mc->list);
1133 devm_kfree(ocelot->dev, mc);
1134 return 0;
1135 }
1136
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001137 /* We have a PGID with fewer ports now */
1138 pgid = ocelot_mdb_get_pgid(ocelot, mc);
1139 if (IS_ERR(pgid))
1140 return PTR_ERR(pgid);
1141 mc->pgid = pgid;
1142
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001143 ocelot_encode_ports_to_mdb(addr, mc);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001144
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001145 if (mc->entry_type != ENTRYTYPE_MACv4 &&
1146 mc->entry_type != ENTRYTYPE_MACv6)
1147 ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1148 pgid->index);
1149
1150 return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
Vladimir Olteanbb8d53f2020-10-29 04:27:37 +02001151 mc->entry_type);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001152}
Vladimir Oltean209edf92020-06-21 14:46:01 +03001153EXPORT_SYMBOL(ocelot_port_mdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001154
Vladimir Oltean5e256362019-11-14 17:03:27 +02001155int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1156 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001157{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001158 if (!ocelot->bridge_mask) {
1159 ocelot->hw_bridge_dev = bridge;
1160 } else {
1161 if (ocelot->hw_bridge_dev != bridge)
1162 /* This is adding the port to a second bridge, this is
1163 * unsupported */
1164 return -ENODEV;
1165 }
1166
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001167 ocelot->bridge_mask |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001168
1169 return 0;
1170}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001171EXPORT_SYMBOL(ocelot_port_bridge_join);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001172
Vladimir Oltean5e256362019-11-14 17:03:27 +02001173int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1174 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001175{
Vladimir Olteanc3e58a752020-10-31 12:29:12 +02001176 struct ocelot_vlan pvid = {0}, native_vlan = {0};
Vladimir Oltean2e554a72020-10-03 01:06:46 +03001177 int ret;
1178
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001179 ocelot->bridge_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001180
1181 if (!ocelot->bridge_mask)
1182 ocelot->hw_bridge_dev = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02001183
Vladimir Olteanbae33f22021-01-09 02:01:50 +02001184 ret = ocelot_port_vlan_filtering(ocelot, port, false);
Vladimir Oltean2e554a72020-10-03 01:06:46 +03001185 if (ret)
1186 return ret;
1187
Vladimir Olteanc3e58a752020-10-31 12:29:12 +02001188 ocelot_port_set_pvid(ocelot, port, pvid);
Vladimir Oltean2f0402f2020-10-31 12:29:15 +02001189 ocelot_port_set_native_vlan(ocelot, port, native_vlan);
1190
1191 return 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001192}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001193EXPORT_SYMBOL(ocelot_port_bridge_leave);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001194
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001195static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1196{
1197 int i, port, lag;
1198
1199 /* Reset destination and aggregation PGIDS */
Vladimir Oltean96b029b2020-06-21 14:46:02 +03001200 for_each_unicast_dest_pgid(ocelot, port)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001201 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1202
Vladimir Oltean96b029b2020-06-21 14:46:02 +03001203 for_each_aggr_pgid(ocelot, i)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001204 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1205 ANA_PGID_PGID, i);
1206
1207 /* Now, set PGIDs for each LAG */
1208 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1209 unsigned long bond_mask;
1210 int aggr_count = 0;
1211 u8 aggr_idx[16];
1212
1213 bond_mask = ocelot->lags[lag];
1214 if (!bond_mask)
1215 continue;
1216
1217 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1218 // Destination mask
1219 ocelot_write_rix(ocelot, bond_mask,
1220 ANA_PGID_PGID, port);
1221 aggr_idx[aggr_count] = port;
1222 aggr_count++;
1223 }
1224
Vladimir Oltean96b029b2020-06-21 14:46:02 +03001225 for_each_aggr_pgid(ocelot, i) {
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001226 u32 ac;
1227
1228 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1229 ac &= ~bond_mask;
1230 ac |= BIT(aggr_idx[i % aggr_count]);
1231 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1232 }
1233 }
1234}
1235
1236static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1237{
1238 unsigned long bond_mask = ocelot->lags[lag];
1239 unsigned int p;
1240
1241 for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1242 u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1243
1244 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1245
1246 /* Use lag port as logical port for port i */
1247 ocelot_write_gix(ocelot, port_cfg |
1248 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1249 ANA_PORT_PORT_CFG, p);
1250 }
1251}
1252
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001253int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1254 struct net_device *bond)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001255{
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001256 struct net_device *ndev;
1257 u32 bond_mask = 0;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001258 int lag, lp;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001259
1260 rcu_read_lock();
1261 for_each_netdev_in_bond_rcu(bond, ndev) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001262 struct ocelot_port_private *priv = netdev_priv(ndev);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001263
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001264 bond_mask |= BIT(priv->chip_port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001265 }
1266 rcu_read_unlock();
1267
1268 lp = __ffs(bond_mask);
1269
1270 /* If the new port is the lowest one, use it as the logical port from
1271 * now on
1272 */
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001273 if (port == lp) {
1274 lag = port;
1275 ocelot->lags[port] = bond_mask;
1276 bond_mask &= ~BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001277 if (bond_mask) {
1278 lp = __ffs(bond_mask);
1279 ocelot->lags[lp] = 0;
1280 }
1281 } else {
1282 lag = lp;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001283 ocelot->lags[lp] |= BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001284 }
1285
1286 ocelot_setup_lag(ocelot, lag);
1287 ocelot_set_aggr_pgids(ocelot);
1288
1289 return 0;
1290}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001291EXPORT_SYMBOL(ocelot_port_lag_join);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001292
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001293void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1294 struct net_device *bond)
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001295{
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001296 u32 port_cfg;
1297 int i;
1298
1299 /* Remove port from any lag */
1300 for (i = 0; i < ocelot->num_phys_ports; i++)
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001301 ocelot->lags[i] &= ~BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001302
1303 /* if it was the logical port of the lag, move the lag config to the
1304 * next port
1305 */
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001306 if (ocelot->lags[port]) {
1307 int n = __ffs(ocelot->lags[port]);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001308
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001309 ocelot->lags[n] = ocelot->lags[port];
1310 ocelot->lags[port] = 0;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001311
1312 ocelot_setup_lag(ocelot, n);
1313 }
1314
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001315 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001316 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001317 ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1318 ANA_PORT_PORT_CFG, port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001319
1320 ocelot_set_aggr_pgids(ocelot);
1321}
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001322EXPORT_SYMBOL(ocelot_port_lag_leave);
Petr Machata0e332c82018-11-22 23:30:11 +00001323
Vladimir Olteana8015de2020-03-10 03:28:18 +02001324/* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1325 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001326 * In the special case that it's the NPI port that we're configuring, the
1327 * length of the tag and optional prefix needs to be accounted for privately,
1328 * in order to be able to sustain communication at the requested @sdu.
Vladimir Olteana8015de2020-03-10 03:28:18 +02001329 */
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001330void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
Vladimir Oltean31350d72019-11-09 15:02:56 +02001331{
1332 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteana8015de2020-03-10 03:28:18 +02001333 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
Vladimir Olteane8e6e732020-07-13 19:57:05 +03001334 int pause_start, pause_stop;
Vladimir Oltean601e9842020-10-05 12:09:11 +03001335 int atop, atop_tot;
Vladimir Oltean31350d72019-11-09 15:02:56 +02001336
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001337 if (port == ocelot->npi) {
1338 maxlen += OCELOT_TAG_LEN;
1339
1340 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1341 maxlen += OCELOT_SHORT_PREFIX_LEN;
1342 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
1343 maxlen += OCELOT_LONG_PREFIX_LEN;
1344 }
1345
Vladimir Olteana8015de2020-03-10 03:28:18 +02001346 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001347
Vladimir Olteane8e6e732020-07-13 19:57:05 +03001348 /* Set Pause watermark hysteresis */
1349 pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1350 pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
Maxim Kochetkov541132f2020-07-13 19:57:07 +03001351 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1352 pause_start);
1353 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1354 pause_stop);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001355
Vladimir Oltean601e9842020-10-05 12:09:11 +03001356 /* Tail dropping watermarks */
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02001357 atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
Vladimir Olteana8015de2020-03-10 03:28:18 +02001358 OCELOT_BUFFER_CELL_SZ;
Vladimir Oltean601e9842020-10-05 12:09:11 +03001359 atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1360 ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1361 ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001362}
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02001363EXPORT_SYMBOL(ocelot_port_set_maxlen);
1364
1365int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
1366{
1367 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
1368
1369 if (port == ocelot->npi) {
1370 max_mtu -= OCELOT_TAG_LEN;
1371
1372 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1373 max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1374 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
1375 max_mtu -= OCELOT_LONG_PREFIX_LEN;
1376 }
1377
1378 return max_mtu;
1379}
1380EXPORT_SYMBOL(ocelot_get_max_mtu);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001381
Vladimir Oltean5e256362019-11-14 17:03:27 +02001382void ocelot_init_port(struct ocelot *ocelot, int port)
Vladimir Olteanfa914e92019-11-14 17:03:23 +02001383{
1384 struct ocelot_port *ocelot_port = ocelot->ports[port];
1385
Yangbo Lub049da12019-11-27 15:27:57 +08001386 skb_queue_head_init(&ocelot_port->tx_skbs);
Vladimir Oltean65652432020-09-18 04:07:24 +03001387 spin_lock_init(&ocelot_port->ts_id_lock);
Vladimir Oltean31350d72019-11-09 15:02:56 +02001388
1389 /* Basic L2 initialization */
1390
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001391 /* Set MAC IFG Gaps
1392 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1393 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1394 */
1395 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
1396 DEV_MAC_IFG_CFG);
1397
1398 /* Load seed (0) and set MAC HDX late collision */
1399 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1400 DEV_MAC_HDX_CFG_SEED_LOAD,
1401 DEV_MAC_HDX_CFG);
1402 mdelay(1);
1403 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
1404 DEV_MAC_HDX_CFG);
1405
1406 /* Set Max Length and maximum tags allowed */
Vladimir Olteana8015de2020-03-10 03:28:18 +02001407 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001408 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
1409 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
Vladimir Olteana8015de2020-03-10 03:28:18 +02001410 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02001411 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
1412 DEV_MAC_TAGS_CFG);
1413
1414 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
1415 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
1416 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
1417
Vladimir Olteane8e6e732020-07-13 19:57:05 +03001418 /* Enable transmission of pause frames */
Maxim Kochetkov541132f2020-07-13 19:57:07 +03001419 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
Vladimir Olteane8e6e732020-07-13 19:57:05 +03001420
Vladimir Oltean31350d72019-11-09 15:02:56 +02001421 /* Drop frames with multicast source address */
1422 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1423 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1424 ANA_PORT_DROP_CFG, port);
1425
1426 /* Set default VLAN and tag type to 8021Q. */
1427 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
1428 REW_PORT_VLAN_CFG_PORT_TPID_M,
1429 REW_PORT_VLAN_CFG, port);
1430
1431 /* Enable vcap lookups */
1432 ocelot_vcap_enable(ocelot, port);
1433}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001434EXPORT_SYMBOL(ocelot_init_port);
Vladimir Oltean31350d72019-11-09 15:02:56 +02001435
Vladimir Oltean2d44b092020-09-26 22:32:01 +03001436/* Configure and enable the CPU port module, which is a set of queues
1437 * accessible through register MMIO, frame DMA or Ethernet (in case
1438 * NPI mode is used).
Vladimir Oltean69df5782020-02-29 16:50:02 +02001439 */
Vladimir Oltean2d44b092020-09-26 22:32:01 +03001440static void ocelot_cpu_port_init(struct ocelot *ocelot)
Vladimir Oltean21468192019-11-09 15:03:00 +02001441{
Vladimir Oltean69df5782020-02-29 16:50:02 +02001442 int cpu = ocelot->num_phys_ports;
1443
1444 /* The unicast destination PGID for the CPU port module is unused */
Vladimir Oltean21468192019-11-09 15:03:00 +02001445 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
Vladimir Oltean69df5782020-02-29 16:50:02 +02001446 /* Instead set up a multicast destination PGID for traffic copied to
1447 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
1448 * addresses will be copied to the CPU via this PGID.
1449 */
Vladimir Oltean21468192019-11-09 15:03:00 +02001450 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1451 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1452 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1453 ANA_PORT_PORT_CFG, cpu);
1454
Vladimir Oltean69df5782020-02-29 16:50:02 +02001455 /* Enable CPU port module */
Vladimir Oltean886e1382020-07-13 19:57:03 +03001456 ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
Vladimir Oltean69df5782020-02-29 16:50:02 +02001457 /* CPU port Injection/Extraction configuration */
Vladimir Oltean886e1382020-07-13 19:57:03 +03001458 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
Vladimir Oltean2d44b092020-09-26 22:32:01 +03001459 ocelot->xtr_prefix);
Vladimir Oltean886e1382020-07-13 19:57:03 +03001460 ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
Vladimir Oltean2d44b092020-09-26 22:32:01 +03001461 ocelot->inj_prefix);
Vladimir Oltean21468192019-11-09 15:03:00 +02001462
1463 /* Configure the CPU port to be VLAN aware */
1464 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
1465 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1466 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
1467 ANA_PORT_VLAN_CFG, cpu);
Vladimir Oltean21468192019-11-09 15:03:00 +02001468}
Vladimir Oltean21468192019-11-09 15:03:00 +02001469
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02001470static void ocelot_detect_features(struct ocelot *ocelot)
1471{
1472 int mmgt, eq_ctrl;
1473
1474 /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
1475 * the number of 240-byte free memory words (aka 4-cell chunks) and not
1476 * 192 bytes as the documentation incorrectly says.
1477 */
1478 mmgt = ocelot_read(ocelot, SYS_MMGT);
1479 ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
1480
1481 eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
1482 ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
1483
1484 dev_info(ocelot->dev,
1485 "Detected %d bytes of packet buffer and %d frame references\n",
1486 ocelot->packet_buffer_size, ocelot->num_frame_refs);
1487}
1488
Alexandre Bellonia556c762018-05-14 22:04:57 +02001489int ocelot_init(struct ocelot *ocelot)
1490{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001491 char queue_name[32];
Vladimir Oltean21468192019-11-09 15:03:00 +02001492 int i, ret;
1493 u32 port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001494
Vladimir Oltean3a77b592019-11-14 17:03:26 +02001495 if (ocelot->ops->reset) {
1496 ret = ocelot->ops->reset(ocelot);
1497 if (ret) {
1498 dev_err(ocelot->dev, "Switch reset failed\n");
1499 return ret;
1500 }
1501 }
1502
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001503 ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1504 sizeof(u32), GFP_KERNEL);
1505 if (!ocelot->lags)
1506 return -ENOMEM;
1507
Alexandre Bellonia556c762018-05-14 22:04:57 +02001508 ocelot->stats = devm_kcalloc(ocelot->dev,
1509 ocelot->num_phys_ports * ocelot->num_stats,
1510 sizeof(u64), GFP_KERNEL);
1511 if (!ocelot->stats)
1512 return -ENOMEM;
1513
1514 mutex_init(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001515 mutex_init(&ocelot->ptp_lock);
1516 spin_lock_init(&ocelot->ptp_clock_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001517 snprintf(queue_name, sizeof(queue_name), "%s-stats",
1518 dev_name(ocelot->dev));
1519 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1520 if (!ocelot->stats_queue)
1521 return -ENOMEM;
1522
Vladimir Olteanca0b2722020-12-12 21:16:12 +02001523 ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
1524 if (!ocelot->owq) {
1525 destroy_workqueue(ocelot->stats_queue);
1526 return -ENOMEM;
1527 }
1528
Claudiu Manoil2b120dd2019-11-09 15:02:58 +02001529 INIT_LIST_HEAD(&ocelot->multicast);
Vladimir Olteane5d1f892020-10-29 04:27:38 +02001530 INIT_LIST_HEAD(&ocelot->pgids);
Vladimir Olteanf6fe01d2021-01-15 04:11:11 +02001531 ocelot_detect_features(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001532 ocelot_mact_init(ocelot);
1533 ocelot_vlan_init(ocelot);
Vladimir Olteanaae4e502020-06-20 18:43:46 +03001534 ocelot_vcap_init(ocelot);
Vladimir Oltean2d44b092020-09-26 22:32:01 +03001535 ocelot_cpu_port_init(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001536
1537 for (port = 0; port < ocelot->num_phys_ports; port++) {
1538 /* Clear all counters (5 groups) */
1539 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1540 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1541 SYS_STAT_CFG);
1542 }
1543
1544 /* Only use S-Tag */
1545 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1546
1547 /* Aggregation mode */
1548 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1549 ANA_AGGR_CFG_AC_DMAC_ENA |
1550 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1551 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1552
1553 /* Set MAC age time to default value. The entry is aged after
1554 * 2*AGE_PERIOD
1555 */
1556 ocelot_write(ocelot,
1557 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1558 ANA_AUTOAGE);
1559
1560 /* Disable learning for frames discarded by VLAN ingress filtering */
1561 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1562
1563 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1564 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1565 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1566
1567 /* Setup flooding PGIDs */
Vladimir Olteanedd24102020-12-04 19:54:16 +02001568 for (i = 0; i < ocelot->num_flooding_pgids; i++)
1569 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1570 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1571 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1572 ANA_FLOODING, i);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001573 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1574 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1575 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1576 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1577 ANA_FLOODING_IPMC);
1578
1579 for (port = 0; port < ocelot->num_phys_ports; port++) {
1580 /* Transmit the frame to the local port. */
1581 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1582 /* Do not forward BPDU frames to the front ports. */
1583 ocelot_write_gix(ocelot,
1584 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1585 ANA_PORT_CPU_FWD_BPDU_CFG,
1586 port);
1587 /* Ensure bridging is disabled */
1588 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1589 }
1590
Alexandre Bellonia556c762018-05-14 22:04:57 +02001591 /* Allow broadcast MAC frames. */
Vladimir Oltean96b029b2020-06-21 14:46:02 +03001592 for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
Alexandre Bellonia556c762018-05-14 22:04:57 +02001593 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1594
1595 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1596 }
1597 ocelot_write_rix(ocelot,
1598 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1599 ANA_PGID_PGID, PGID_MC);
1600 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1601 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1602
Alexandre Bellonia556c762018-05-14 22:04:57 +02001603 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
1604 * registers endianness.
1605 */
1606 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1607 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1608 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1609 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1610 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1611 ANA_CPUQ_CFG_CPUQ_LRN(2) |
1612 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1613 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1614 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1615 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1616 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1617 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1618 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1619 for (i = 0; i < 16; i++)
1620 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1621 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1622 ANA_CPUQ_8021_CFG, i);
1623
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001624 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001625 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1626 OCELOT_STATS_CHECK_DELAY);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001627
Alexandre Bellonia556c762018-05-14 22:04:57 +02001628 return 0;
1629}
1630EXPORT_SYMBOL(ocelot_init);
1631
1632void ocelot_deinit(struct ocelot *ocelot)
1633{
Claudiu Manoilc5d13962019-07-25 16:33:18 +03001634 cancel_delayed_work(&ocelot->stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001635 destroy_workqueue(ocelot->stats_queue);
Vladimir Olteanca0b2722020-12-12 21:16:12 +02001636 destroy_workqueue(ocelot->owq);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001637 mutex_destroy(&ocelot->stats_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001638}
1639EXPORT_SYMBOL(ocelot_deinit);
1640
Vladimir Olteane5fb5122020-09-18 04:07:30 +03001641void ocelot_deinit_port(struct ocelot *ocelot, int port)
1642{
1643 struct ocelot_port *ocelot_port = ocelot->ports[port];
1644
1645 skb_queue_purge(&ocelot_port->tx_skbs);
1646}
1647EXPORT_SYMBOL(ocelot_deinit_port);
1648
Alexandre Bellonia556c762018-05-14 22:04:57 +02001649MODULE_LICENSE("Dual MIT/GPL");