blob: 7302724a92617db0b28163ac8e0ca037fc931ffc [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 */
7#include <linux/etherdevice.h>
8#include <linux/ethtool.h>
9#include <linux/if_bridge.h>
10#include <linux/if_ether.h>
11#include <linux/if_vlan.h>
12#include <linux/interrupt.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/netdevice.h>
16#include <linux/phy.h>
Antoine Tenart4e3b0462019-08-12 16:45:37 +020017#include <linux/ptp_clock_kernel.h>
Alexandre Bellonia556c762018-05-14 22:04:57 +020018#include <linux/skbuff.h>
Steen Hegelund639c1b22018-12-20 14:16:31 +010019#include <linux/iopoll.h>
Alexandre Bellonia556c762018-05-14 22:04:57 +020020#include <net/arp.h>
21#include <net/netevent.h>
22#include <net/rtnetlink.h>
23#include <net/switchdev.h>
24
25#include "ocelot.h"
Horatiu Vulturb5962292019-05-31 09:16:56 +020026#include "ocelot_ace.h"
Alexandre Bellonia556c762018-05-14 22:04:57 +020027
Steen Hegelund639c1b22018-12-20 14:16:31 +010028#define TABLE_UPDATE_SLEEP_US 10
29#define TABLE_UPDATE_TIMEOUT_US 100000
30
Alexandre Bellonia556c762018-05-14 22:04:57 +020031/* MAC table entry types.
32 * ENTRYTYPE_NORMAL is subject to aging.
33 * ENTRYTYPE_LOCKED is not subject to aging.
34 * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
35 * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
36 */
37enum macaccess_entry_type {
38 ENTRYTYPE_NORMAL = 0,
39 ENTRYTYPE_LOCKED,
40 ENTRYTYPE_MACv4,
41 ENTRYTYPE_MACv6,
42};
43
44struct ocelot_mact_entry {
45 u8 mac[ETH_ALEN];
46 u16 vid;
47 enum macaccess_entry_type type;
48};
49
Steen Hegelund639c1b22018-12-20 14:16:31 +010050static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
51{
52 return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
53}
54
Alexandre Bellonia556c762018-05-14 22:04:57 +020055static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
56{
Steen Hegelund639c1b22018-12-20 14:16:31 +010057 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +020058
Steen Hegelund639c1b22018-12-20 14:16:31 +010059 return readx_poll_timeout(ocelot_mact_read_macaccess,
60 ocelot, val,
61 (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
62 MACACCESS_CMD_IDLE,
63 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +020064}
65
66static void ocelot_mact_select(struct ocelot *ocelot,
67 const unsigned char mac[ETH_ALEN],
68 unsigned int vid)
69{
70 u32 macl = 0, mach = 0;
71
72 /* Set the MAC address to handle and the vlan associated in a format
73 * understood by the hardware.
74 */
75 mach |= vid << 16;
76 mach |= mac[0] << 8;
77 mach |= mac[1] << 0;
78 macl |= mac[2] << 24;
79 macl |= mac[3] << 16;
80 macl |= mac[4] << 8;
81 macl |= mac[5] << 0;
82
83 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
84 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
85
86}
87
88static int ocelot_mact_learn(struct ocelot *ocelot, int port,
89 const unsigned char mac[ETH_ALEN],
90 unsigned int vid,
91 enum macaccess_entry_type type)
92{
93 ocelot_mact_select(ocelot, mac, vid);
94
95 /* Issue a write command */
96 ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
97 ANA_TABLES_MACACCESS_DEST_IDX(port) |
98 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
99 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
100 ANA_TABLES_MACACCESS);
101
102 return ocelot_mact_wait_for_completion(ocelot);
103}
104
105static int ocelot_mact_forget(struct ocelot *ocelot,
106 const unsigned char mac[ETH_ALEN],
107 unsigned int vid)
108{
109 ocelot_mact_select(ocelot, mac, vid);
110
111 /* Issue a forget command */
112 ocelot_write(ocelot,
113 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
114 ANA_TABLES_MACACCESS);
115
116 return ocelot_mact_wait_for_completion(ocelot);
117}
118
119static void ocelot_mact_init(struct ocelot *ocelot)
120{
121 /* Configure the learning mode entries attributes:
122 * - Do not copy the frame to the CPU extraction queues.
123 * - Use the vlan and mac_cpoy for dmac lookup.
124 */
125 ocelot_rmw(ocelot, 0,
126 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
127 | ANA_AGENCTRL_LEARN_FWD_KILL
128 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
129 ANA_AGENCTRL);
130
131 /* Clear the MAC table */
132 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
133}
134
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200135static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
Horatiu Vulturb5962292019-05-31 09:16:56 +0200136{
137 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
138 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200139 ANA_PORT_VCAP_S2_CFG, port);
Horatiu Vulturb5962292019-05-31 09:16:56 +0200140}
141
Steen Hegelund639c1b22018-12-20 14:16:31 +0100142static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
143{
144 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
145}
146
Alexandre Bellonia556c762018-05-14 22:04:57 +0200147static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
148{
Steen Hegelund639c1b22018-12-20 14:16:31 +0100149 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200150
Steen Hegelund639c1b22018-12-20 14:16:31 +0100151 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
152 ocelot,
153 val,
154 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
155 ANA_TABLES_VLANACCESS_CMD_IDLE,
156 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200157}
158
Antoine Tenart71425292018-06-26 14:28:49 +0200159static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
160{
161 /* Select the VID to configure */
162 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
163 ANA_TABLES_VLANTIDX);
164 /* Set the vlan port members mask and issue a write command */
165 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
166 ANA_TABLES_VLANACCESS_CMD_WRITE,
167 ANA_TABLES_VLANACCESS);
168
169 return ocelot_vlant_wait_for_completion(ocelot);
170}
171
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200172static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
Antoine Tenart71425292018-06-26 14:28:49 +0200173 netdev_features_t features)
174{
Antoine Tenart71425292018-06-26 14:28:49 +0200175 u32 val;
176
177 /* Filtering */
178 val = ocelot_read(ocelot, ANA_VLANMASK);
179 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200180 val |= BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200181 else
Vladimir Olteanf270dbf2019-11-09 15:02:52 +0200182 val &= ~BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200183 ocelot_write(ocelot, val, ANA_VLANMASK);
184}
185
Vladimir Oltean5e256362019-11-14 17:03:27 +0200186void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
187 bool vlan_aware)
Antoine Tenart71425292018-06-26 14:28:49 +0200188{
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200189 struct ocelot_port *ocelot_port = ocelot->ports[port];
Antoine Tenart71425292018-06-26 14:28:49 +0200190 u32 val;
191
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200192 if (vlan_aware)
193 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
194 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
195 else
196 val = 0;
Antoine Tenart71425292018-06-26 14:28:49 +0200197 ocelot_rmw_gix(ocelot, val,
Antoine Tenart71425292018-06-26 14:28:49 +0200198 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
199 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200200 ANA_PORT_VLAN_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200201
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200202 if (vlan_aware && !ocelot_port->vid)
Antoine Tenart71425292018-06-26 14:28:49 +0200203 /* If port is vlan-aware and tagged, drop untagged and priority
204 * tagged frames.
205 */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200206 val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
207 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
208 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
209 else
210 val = 0;
211 ocelot_rmw_gix(ocelot, val,
212 ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
Antoine Tenart71425292018-06-26 14:28:49 +0200213 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200214 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
215 ANA_PORT_DROP_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200216
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200217 if (vlan_aware) {
218 if (ocelot_port->vid)
Antoine Tenart71425292018-06-26 14:28:49 +0200219 /* Tag all frames except when VID == DEFAULT_VLAN */
220 val |= REW_TAG_CFG_TAG_CFG(1);
221 else
222 /* Tag all frames */
223 val |= REW_TAG_CFG_TAG_CFG(3);
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200224 } else {
225 /* Port tagging disabled. */
226 val = REW_TAG_CFG_TAG_CFG(0);
Antoine Tenart71425292018-06-26 14:28:49 +0200227 }
228 ocelot_rmw_gix(ocelot, val,
Antoine Tenart71425292018-06-26 14:28:49 +0200229 REW_TAG_CFG_TAG_CFG_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200230 REW_TAG_CFG, port);
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200231}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200232EXPORT_SYMBOL(ocelot_port_vlan_filtering);
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200233
234static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
235 u16 vid)
236{
237 struct ocelot_port *ocelot_port = ocelot->ports[port];
238
239 if (ocelot_port->vid != vid) {
240 /* Always permit deleting the native VLAN (vid = 0) */
241 if (ocelot_port->vid && vid) {
242 dev_err(ocelot->dev,
243 "Port already has a native VLAN: %d\n",
244 ocelot_port->vid);
245 return -EBUSY;
246 }
247 ocelot_port->vid = vid;
248 }
249
250 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
Antoine Tenart71425292018-06-26 14:28:49 +0200251 REW_PORT_VLAN_CFG_PORT_VID_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200252 REW_PORT_VLAN_CFG, port);
253
254 return 0;
255}
256
257/* Default vlan to clasify for untagged frames (may be zero) */
258static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
259{
260 struct ocelot_port *ocelot_port = ocelot->ports[port];
261
262 ocelot_rmw_gix(ocelot,
263 ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
264 ANA_PORT_VLAN_CFG_VLAN_VID_M,
265 ANA_PORT_VLAN_CFG, port);
266
267 ocelot_port->pvid = pvid;
Antoine Tenart71425292018-06-26 14:28:49 +0200268}
269
Vladimir Oltean5e256362019-11-14 17:03:27 +0200270int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
271 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200272{
Antoine Tenart71425292018-06-26 14:28:49 +0200273 int ret;
274
Antoine Tenart71425292018-06-26 14:28:49 +0200275 /* Make the port a member of the VLAN */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200276 ocelot->vlan_mask[vid] |= BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200277 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
278 if (ret)
279 return ret;
280
281 /* Default ingress vlan classification */
282 if (pvid)
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200283 ocelot_port_set_pvid(ocelot, port, vid);
Antoine Tenart71425292018-06-26 14:28:49 +0200284
285 /* Untagged egress vlan clasification */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200286 if (untagged) {
287 ret = ocelot_port_set_native_vlan(ocelot, port, vid);
288 if (ret)
289 return ret;
Vladimir Olteanb9cd75e2019-10-26 21:04:27 +0300290 }
Antoine Tenart71425292018-06-26 14:28:49 +0200291
Antoine Tenart71425292018-06-26 14:28:49 +0200292 return 0;
293}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200294EXPORT_SYMBOL(ocelot_vlan_add);
Antoine Tenart71425292018-06-26 14:28:49 +0200295
Vladimir Oltean98559342019-11-09 15:02:48 +0200296static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
297 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200298{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200299 struct ocelot_port_private *priv = netdev_priv(dev);
300 struct ocelot_port *ocelot_port = &priv->port;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200301 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200302 int port = priv->chip_port;
Antoine Tenart71425292018-06-26 14:28:49 +0200303 int ret;
304
Vladimir Oltean98559342019-11-09 15:02:48 +0200305 ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
306 if (ret)
307 return ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200308
Vladimir Oltean98559342019-11-09 15:02:48 +0200309 /* Add the port MAC address to with the right VLAN information */
310 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
311 ENTRYTYPE_LOCKED);
312
313 return 0;
314}
315
Vladimir Oltean5e256362019-11-14 17:03:27 +0200316int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
Vladimir Oltean98559342019-11-09 15:02:48 +0200317{
318 struct ocelot_port *ocelot_port = ocelot->ports[port];
319 int ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200320
321 /* Stop the port from being a member of the vlan */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200322 ocelot->vlan_mask[vid] &= ~BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200323 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
324 if (ret)
325 return ret;
326
327 /* Ingress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200328 if (ocelot_port->pvid == vid)
329 ocelot_port_set_pvid(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200330
331 /* Egress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200332 if (ocelot_port->vid == vid)
333 ocelot_port_set_native_vlan(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200334
335 return 0;
336}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200337EXPORT_SYMBOL(ocelot_vlan_del);
Antoine Tenart71425292018-06-26 14:28:49 +0200338
Vladimir Oltean98559342019-11-09 15:02:48 +0200339static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
340{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200341 struct ocelot_port_private *priv = netdev_priv(dev);
342 struct ocelot *ocelot = priv->port.ocelot;
343 int port = priv->chip_port;
Vladimir Oltean98559342019-11-09 15:02:48 +0200344 int ret;
345
346 /* 8021q removes VID 0 on module unload for all interfaces
347 * with VLAN filtering feature. We need to keep it to receive
348 * untagged traffic.
349 */
350 if (vid == 0)
351 return 0;
352
353 ret = ocelot_vlan_del(ocelot, port, vid);
354 if (ret)
355 return ret;
356
357 /* Del the port MAC address to with the right VLAN information */
358 ocelot_mact_forget(ocelot, dev->dev_addr, vid);
359
360 return 0;
361}
362
Alexandre Bellonia556c762018-05-14 22:04:57 +0200363static void ocelot_vlan_init(struct ocelot *ocelot)
364{
Antoine Tenart71425292018-06-26 14:28:49 +0200365 u16 port, vid;
366
Alexandre Bellonia556c762018-05-14 22:04:57 +0200367 /* Clear VLAN table, by default all ports are members of all VLANs */
368 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
369 ANA_TABLES_VLANACCESS);
370 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200371
372 /* Configure the port VLAN memberships */
373 for (vid = 1; vid < VLAN_N_VID; vid++) {
374 ocelot->vlan_mask[vid] = 0;
375 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
376 }
377
378 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
379 * traffic. It is added automatically if 8021q module is loaded, but
380 * we can't rely on it since module may be not loaded.
381 */
382 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
383 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
384
Antoine Tenart71425292018-06-26 14:28:49 +0200385 /* Set vlan ingress filter mask to all ports but the CPU port by
386 * default.
387 */
Vladimir Oltean714d0ff2019-11-09 15:02:55 +0200388 ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
389 ANA_VLANMASK);
Antoine Tenart71425292018-06-26 14:28:49 +0200390
391 for (port = 0; port < ocelot->num_phys_ports; port++) {
392 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
393 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
394 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200395}
396
397/* Watermark encode
398 * Bit 8: Unit; 0:1, 1:16
399 * Bit 7-0: Value to be multiplied with unit
400 */
401static u16 ocelot_wm_enc(u16 value)
402{
403 if (value >= BIT(8))
404 return BIT(8) | (value / 16);
405
406 return value;
407}
408
Vladimir Oltean5e256362019-11-14 17:03:27 +0200409void ocelot_adjust_link(struct ocelot *ocelot, int port,
410 struct phy_device *phydev)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200411{
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200412 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +0200413 int speed, mode = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200414
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200415 switch (phydev->speed) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200416 case SPEED_10:
417 speed = OCELOT_SPEED_10;
418 break;
419 case SPEED_100:
420 speed = OCELOT_SPEED_100;
421 break;
422 case SPEED_1000:
423 speed = OCELOT_SPEED_1000;
424 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
425 break;
426 case SPEED_2500:
427 speed = OCELOT_SPEED_2500;
428 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
429 break;
430 default:
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200431 dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
432 port, phydev->speed);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200433 return;
434 }
435
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200436 phy_print_status(phydev);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200437
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200438 if (!phydev->link)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200439 return;
440
441 /* Only full duplex supported for now */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200442 ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200443 mode, DEV_MAC_MODE_CFG);
444
Claudiu Manoildc3de2a2019-11-14 17:03:21 +0200445 if (ocelot->ops->pcs_init)
446 ocelot->ops->pcs_init(ocelot, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200447
Alexandre Bellonia556c762018-05-14 22:04:57 +0200448 /* Enable MAC module */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200449 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200450 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
451
452 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
453 * reset */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200454 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
Alexandre Bellonia556c762018-05-14 22:04:57 +0200455 DEV_CLOCK_CFG);
456
Alexandre Bellonia556c762018-05-14 22:04:57 +0200457 /* No PFC */
458 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200459 ANA_PFC_PFC_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200460
Alexandre Bellonia556c762018-05-14 22:04:57 +0200461 /* Core: Enable port for frame transfer */
462 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
463 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
464 QSYS_SWITCH_PORT_MODE_PORT_ENA,
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200465 QSYS_SWITCH_PORT_MODE, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200466
467 /* Flow control */
468 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
469 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
470 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
471 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
472 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200473 SYS_MAC_FC_CFG, port);
474 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200475}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200476EXPORT_SYMBOL(ocelot_adjust_link);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200477
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200478static void ocelot_port_adjust_link(struct net_device *dev)
479{
480 struct ocelot_port_private *priv = netdev_priv(dev);
481 struct ocelot *ocelot = priv->port.ocelot;
482 int port = priv->chip_port;
483
484 ocelot_adjust_link(ocelot, port, dev->phydev);
485}
486
Vladimir Oltean5e256362019-11-14 17:03:27 +0200487void ocelot_port_enable(struct ocelot *ocelot, int port,
488 struct phy_device *phy)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200489{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200490 /* Enable receiving frames on the port, and activate auto-learning of
491 * MAC addresses.
492 */
493 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
494 ANA_PORT_PORT_CFG_RECV_ENA |
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200495 ANA_PORT_PORT_CFG_PORTID_VAL(port),
496 ANA_PORT_PORT_CFG, port);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200497}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200498EXPORT_SYMBOL(ocelot_port_enable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200499
500static int ocelot_port_open(struct net_device *dev)
501{
502 struct ocelot_port_private *priv = netdev_priv(dev);
503 struct ocelot *ocelot = priv->port.ocelot;
504 int port = priv->chip_port;
505 int err;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200506
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200507 if (priv->serdes) {
508 err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
509 priv->phy_mode);
Quentin Schulz71e32a202018-10-04 14:22:08 +0200510 if (err) {
511 netdev_err(dev, "Could not set mode of SerDes\n");
512 return err;
513 }
514 }
515
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200516 err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
517 priv->phy_mode);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200518 if (err) {
519 netdev_err(dev, "Could not attach to PHY\n");
520 return err;
521 }
522
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200523 dev->phydev = priv->phy;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200524
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200525 phy_attached_info(priv->phy);
526 phy_start(priv->phy);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200527
528 ocelot_port_enable(ocelot, port, priv->phy);
529
Alexandre Bellonia556c762018-05-14 22:04:57 +0200530 return 0;
531}
532
Vladimir Oltean5e256362019-11-14 17:03:27 +0200533void ocelot_port_disable(struct ocelot *ocelot, int port)
Vladimir Oltean889b8952019-11-09 15:02:57 +0200534{
535 struct ocelot_port *ocelot_port = ocelot->ports[port];
536
537 ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
538 ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
539 QSYS_SWITCH_PORT_MODE, port);
540}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200541EXPORT_SYMBOL(ocelot_port_disable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200542
Alexandre Bellonia556c762018-05-14 22:04:57 +0200543static int ocelot_port_stop(struct net_device *dev)
544{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200545 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200546 struct ocelot *ocelot = priv->port.ocelot;
547 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200548
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200549 phy_disconnect(priv->phy);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200550
551 dev->phydev = NULL;
552
Vladimir Oltean889b8952019-11-09 15:02:57 +0200553 ocelot_port_disable(ocelot, port);
554
Alexandre Bellonia556c762018-05-14 22:04:57 +0200555 return 0;
556}
557
558/* Generate the IFH for frame injection
559 *
560 * The IFH is a 128bit-value
561 * bit 127: bypass the analyzer processing
562 * bit 56-67: destination mask
563 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
564 * bit 20-27: cpu extraction queue mask
565 * bit 16: tag type 0: C-tag, 1: S-tag
566 * bit 0-11: VID
567 */
568static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
569{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200570 ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
Antoine Tenart08d02362018-06-20 10:50:46 +0200571 ifh[1] = (0xf00 & info->port) >> 8;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200572 ifh[2] = (0xff & info->port) << 24;
Antoine Tenart08d02362018-06-20 10:50:46 +0200573 ifh[3] = (info->tag_type << 16) | info->vid;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200574
575 return 0;
576}
577
578static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
579{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200580 struct ocelot_port_private *priv = netdev_priv(dev);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200581 struct skb_shared_info *shinfo = skb_shinfo(skb);
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200582 struct ocelot_port *ocelot_port = &priv->port;
583 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Olteanf24711f2019-11-14 17:03:24 +0200584 u32 val, ifh[OCELOT_TAG_LEN / 4];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200585 struct frame_info info = {};
586 u8 grp = 0; /* Send everything on CPU group 0 */
587 unsigned int i, count, last;
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200588 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200589
590 val = ocelot_read(ocelot, QS_INJ_STATUS);
591 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
592 (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
593 return NETDEV_TX_BUSY;
594
595 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
596 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
597
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200598 info.port = BIT(port);
Antoine Tenart08d02362018-06-20 10:50:46 +0200599 info.tag_type = IFH_TAG_TYPE_C;
600 info.vid = skb_vlan_tag_get(skb);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200601
602 /* Check if timestamping is needed */
603 if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200604 info.rew_op = ocelot_port->ptp_cmd;
605 if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
606 info.rew_op |= (ocelot_port->ts_id % 4) << 3;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200607 }
608
Alexandre Bellonia556c762018-05-14 22:04:57 +0200609 ocelot_gen_ifh(ifh, &info);
610
Vladimir Olteanf24711f2019-11-14 17:03:24 +0200611 for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
Antoine Tenartc2cd6502018-06-22 11:50:52 +0200612 ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
613 QS_INJ_WR, grp);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200614
615 count = (skb->len + 3) / 4;
616 last = skb->len % 4;
617 for (i = 0; i < count; i++) {
618 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
619 }
620
621 /* Add padding */
622 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
623 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
624 i++;
625 }
626
627 /* Indicate EOF and valid bytes in last word */
628 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
629 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
630 QS_INJ_CTRL_EOF,
631 QS_INJ_CTRL, grp);
632
633 /* Add dummy CRC */
634 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
635 skb_tx_timestamp(skb);
636
637 dev->stats.tx_packets++;
638 dev->stats.tx_bytes += skb->len;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200639
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200640 if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200641 ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200642 struct ocelot_skb *oskb =
643 kzalloc(sizeof(struct ocelot_skb), GFP_ATOMIC);
644
645 if (unlikely(!oskb))
646 goto out;
647
648 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
649
650 oskb->skb = skb;
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200651 oskb->id = ocelot_port->ts_id % 4;
652 ocelot_port->ts_id++;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200653
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200654 list_add_tail(&oskb->head, &ocelot_port->skbs);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200655
656 return NETDEV_TX_OK;
657 }
658
659out:
660 dev_kfree_skb_any(skb);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200661 return NETDEV_TX_OK;
662}
663
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200664void ocelot_get_hwtimestamp(struct ocelot *ocelot, struct timespec64 *ts)
665{
666 unsigned long flags;
667 u32 val;
668
669 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
670
671 /* Read current PTP time to get seconds */
672 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
673
674 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
675 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
676 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
677 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
678
679 /* Read packet HW timestamp from FIFO */
680 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
681 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
682
683 /* Sec has incremented since the ts was registered */
684 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
685 ts->tv_sec--;
686
687 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
688}
689EXPORT_SYMBOL(ocelot_get_hwtimestamp);
690
Claudiu Manoil40a15782019-05-21 19:52:55 +0300691static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200692{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200693 struct ocelot_port_private *priv = netdev_priv(dev);
694 struct ocelot_port *ocelot_port = &priv->port;
695 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200696
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200697 return ocelot_mact_forget(ocelot, addr, ocelot_port->pvid);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200698}
699
Claudiu Manoil40a15782019-05-21 19:52:55 +0300700static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200701{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200702 struct ocelot_port_private *priv = netdev_priv(dev);
703 struct ocelot_port *ocelot_port = &priv->port;
704 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200705
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200706 return ocelot_mact_learn(ocelot, PGID_CPU, addr, ocelot_port->pvid,
Claudiu Manoil40a15782019-05-21 19:52:55 +0300707 ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200708}
709
710static void ocelot_set_rx_mode(struct net_device *dev)
711{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200712 struct ocelot_port_private *priv = netdev_priv(dev);
713 struct ocelot *ocelot = priv->port.ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200714 u32 val;
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200715 int i;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200716
717 /* This doesn't handle promiscuous mode because the bridge core is
718 * setting IFF_PROMISC on all slave interfaces and all frames would be
719 * forwarded to the CPU port.
720 */
721 val = GENMASK(ocelot->num_phys_ports - 1, 0);
722 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
723 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
724
Claudiu Manoil40a15782019-05-21 19:52:55 +0300725 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200726}
727
728static int ocelot_port_get_phys_port_name(struct net_device *dev,
729 char *buf, size_t len)
730{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200731 struct ocelot_port_private *priv = netdev_priv(dev);
732 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200733 int ret;
734
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200735 ret = snprintf(buf, len, "p%d", port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200736 if (ret >= len)
737 return -EINVAL;
738
739 return 0;
740}
741
742static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
743{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200744 struct ocelot_port_private *priv = netdev_priv(dev);
745 struct ocelot_port *ocelot_port = &priv->port;
746 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200747 const struct sockaddr *addr = p;
748
749 /* Learn the new net device MAC address in the mac table. */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200750 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, ocelot_port->pvid,
Alexandre Bellonia556c762018-05-14 22:04:57 +0200751 ENTRYTYPE_LOCKED);
752 /* Then forget the previous one. */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200753 ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200754
755 ether_addr_copy(dev->dev_addr, addr->sa_data);
756 return 0;
757}
758
759static void ocelot_get_stats64(struct net_device *dev,
760 struct rtnl_link_stats64 *stats)
761{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200762 struct ocelot_port_private *priv = netdev_priv(dev);
763 struct ocelot *ocelot = priv->port.ocelot;
764 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200765
766 /* Configure the port to read the stats from */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200767 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
Alexandre Bellonia556c762018-05-14 22:04:57 +0200768 SYS_STAT_CFG);
769
770 /* Get Rx stats */
771 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
772 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
773 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
774 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
775 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
776 ocelot_read(ocelot, SYS_COUNT_RX_64) +
777 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
778 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
779 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
780 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
781 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
782 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
783 stats->rx_dropped = dev->stats.rx_dropped;
784
785 /* Get Tx stats */
786 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
787 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
788 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
789 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
790 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
791 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
792 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
793 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
794 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
795 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
796}
797
Vladimir Oltean5e256362019-11-14 17:03:27 +0200798int ocelot_fdb_add(struct ocelot *ocelot, int port,
799 const unsigned char *addr, u16 vid, bool vlan_aware)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200800{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200801 struct ocelot_port *ocelot_port = ocelot->ports[port];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200802
Antoine Tenart71425292018-06-26 14:28:49 +0200803 if (!vid) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200804 if (!vlan_aware)
Antoine Tenart71425292018-06-26 14:28:49 +0200805 /* If the bridge is not VLAN aware and no VID was
806 * provided, set it to pvid to ensure the MAC entry
807 * matches incoming untagged packets
808 */
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200809 vid = ocelot_port->pvid;
Antoine Tenart71425292018-06-26 14:28:49 +0200810 else
811 /* If the bridge is VLAN aware a VID must be provided as
812 * otherwise the learnt entry wouldn't match any frame.
813 */
814 return -EINVAL;
815 }
816
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200817 return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200818}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200819EXPORT_SYMBOL(ocelot_fdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200820
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200821static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
822 struct net_device *dev,
823 const unsigned char *addr,
824 u16 vid, u16 flags,
825 struct netlink_ext_ack *extack)
826{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200827 struct ocelot_port_private *priv = netdev_priv(dev);
828 struct ocelot *ocelot = priv->port.ocelot;
829 int port = priv->chip_port;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200830
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200831 return ocelot_fdb_add(ocelot, port, addr, vid, priv->vlan_aware);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200832}
833
Vladimir Oltean5e256362019-11-14 17:03:27 +0200834int ocelot_fdb_del(struct ocelot *ocelot, int port,
835 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200836{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200837 return ocelot_mact_forget(ocelot, addr, vid);
838}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200839EXPORT_SYMBOL(ocelot_fdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200840
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200841static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
842 struct net_device *dev,
843 const unsigned char *addr, u16 vid)
844{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200845 struct ocelot_port_private *priv = netdev_priv(dev);
846 struct ocelot *ocelot = priv->port.ocelot;
847 int port = priv->chip_port;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200848
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200849 return ocelot_fdb_del(ocelot, port, addr, vid);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200850}
851
Alexandre Bellonia556c762018-05-14 22:04:57 +0200852struct ocelot_dump_ctx {
853 struct net_device *dev;
854 struct sk_buff *skb;
855 struct netlink_callback *cb;
856 int idx;
857};
858
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200859static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
860 bool is_static, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200861{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200862 struct ocelot_dump_ctx *dump = data;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200863 u32 portid = NETLINK_CB(dump->cb->skb).portid;
864 u32 seq = dump->cb->nlh->nlmsg_seq;
865 struct nlmsghdr *nlh;
866 struct ndmsg *ndm;
867
868 if (dump->idx < dump->cb->args[2])
869 goto skip;
870
871 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
872 sizeof(*ndm), NLM_F_MULTI);
873 if (!nlh)
874 return -EMSGSIZE;
875
876 ndm = nlmsg_data(nlh);
877 ndm->ndm_family = AF_BRIDGE;
878 ndm->ndm_pad1 = 0;
879 ndm->ndm_pad2 = 0;
880 ndm->ndm_flags = NTF_SELF;
881 ndm->ndm_type = 0;
882 ndm->ndm_ifindex = dump->dev->ifindex;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200883 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200884
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200885 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200886 goto nla_put_failure;
887
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200888 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200889 goto nla_put_failure;
890
891 nlmsg_end(dump->skb, nlh);
892
893skip:
894 dump->idx++;
895 return 0;
896
897nla_put_failure:
898 nlmsg_cancel(dump->skb, nlh);
899 return -EMSGSIZE;
900}
901
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200902static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
903 struct ocelot_mact_entry *entry)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200904{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200905 u32 val, dst, macl, mach;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200906 char mac[ETH_ALEN];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200907
908 /* Set row and column to read from */
909 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
910 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
911
912 /* Issue a read command */
913 ocelot_write(ocelot,
914 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
915 ANA_TABLES_MACACCESS);
916
917 if (ocelot_mact_wait_for_completion(ocelot))
918 return -ETIMEDOUT;
919
920 /* Read the entry flags */
921 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
922 if (!(val & ANA_TABLES_MACACCESS_VALID))
923 return -EINVAL;
924
925 /* If the entry read has another port configured as its destination,
926 * do not report it.
927 */
928 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200929 if (dst != port)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200930 return -EINVAL;
931
932 /* Get the entry's MAC address and VLAN id */
933 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
934 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
935
936 mac[0] = (mach >> 8) & 0xff;
937 mac[1] = (mach >> 0) & 0xff;
938 mac[2] = (macl >> 24) & 0xff;
939 mac[3] = (macl >> 16) & 0xff;
940 mac[4] = (macl >> 8) & 0xff;
941 mac[5] = (macl >> 0) & 0xff;
942
943 entry->vid = (mach >> 16) & 0xfff;
944 ether_addr_copy(entry->mac, mac);
945
946 return 0;
947}
948
Vladimir Oltean5e256362019-11-14 17:03:27 +0200949int ocelot_fdb_dump(struct ocelot *ocelot, int port,
950 dsa_fdb_dump_cb_t *cb, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200951{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200952 int i, j;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200953
954 /* Loop through all the mac tables entries. There are 1024 rows of 4
955 * entries.
956 */
957 for (i = 0; i < 1024; i++) {
958 for (j = 0; j < 4; j++) {
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200959 struct ocelot_mact_entry entry;
960 bool is_static;
961 int ret;
962
963 ret = ocelot_mact_read(ocelot, port, i, j, &entry);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200964 /* If the entry is invalid (wrong port, invalid...),
965 * skip it.
966 */
967 if (ret == -EINVAL)
968 continue;
969 else if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200970 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200971
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200972 is_static = (entry.type == ENTRYTYPE_LOCKED);
973
974 ret = cb(entry.mac, entry.vid, is_static, data);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200975 if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200976 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200977 }
978 }
979
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200980 return 0;
981}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200982EXPORT_SYMBOL(ocelot_fdb_dump);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200983
984static int ocelot_port_fdb_dump(struct sk_buff *skb,
985 struct netlink_callback *cb,
986 struct net_device *dev,
987 struct net_device *filter_dev, int *idx)
988{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200989 struct ocelot_port_private *priv = netdev_priv(dev);
990 struct ocelot *ocelot = priv->port.ocelot;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200991 struct ocelot_dump_ctx dump = {
992 .dev = dev,
993 .skb = skb,
994 .cb = cb,
995 .idx = *idx,
996 };
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200997 int port = priv->chip_port;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200998 int ret;
999
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001000 ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001001
Alexandre Bellonia556c762018-05-14 22:04:57 +02001002 *idx = dump.idx;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001003
Alexandre Bellonia556c762018-05-14 22:04:57 +02001004 return ret;
1005}
1006
Antoine Tenart71425292018-06-26 14:28:49 +02001007static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1008 u16 vid)
1009{
Vladimir Oltean1c44ce52019-10-26 21:04:26 +03001010 return ocelot_vlan_vid_add(dev, vid, false, false);
Antoine Tenart71425292018-06-26 14:28:49 +02001011}
1012
1013static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1014 u16 vid)
1015{
1016 return ocelot_vlan_vid_del(dev, vid);
1017}
1018
1019static int ocelot_set_features(struct net_device *dev,
1020 netdev_features_t features)
1021{
Antoine Tenart71425292018-06-26 14:28:49 +02001022 netdev_features_t changed = dev->features ^ features;
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001023 struct ocelot_port_private *priv = netdev_priv(dev);
1024 struct ocelot *ocelot = priv->port.ocelot;
1025 int port = priv->chip_port;
Antoine Tenart71425292018-06-26 14:28:49 +02001026
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001027 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001028 priv->tc.offload_cnt) {
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001029 netdev_err(dev,
1030 "Cannot disable HW TC offload while offloads active\n");
1031 return -EBUSY;
1032 }
1033
Antoine Tenart71425292018-06-26 14:28:49 +02001034 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001035 ocelot_vlan_mode(ocelot, port, features);
Antoine Tenart71425292018-06-26 14:28:49 +02001036
1037 return 0;
1038}
1039
Florian Fainelli751302c2019-02-06 09:45:40 -08001040static int ocelot_get_port_parent_id(struct net_device *dev,
1041 struct netdev_phys_item_id *ppid)
1042{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001043 struct ocelot_port_private *priv = netdev_priv(dev);
1044 struct ocelot *ocelot = priv->port.ocelot;
Florian Fainelli751302c2019-02-06 09:45:40 -08001045
1046 ppid->id_len = sizeof(ocelot->base_mac);
1047 memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
1048
1049 return 0;
1050}
1051
Yangbo Luf1459222019-11-20 16:23:14 +08001052int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001053{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001054 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1055 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1056}
Yangbo Luf1459222019-11-20 16:23:14 +08001057EXPORT_SYMBOL(ocelot_hwstamp_get);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001058
Yangbo Luf1459222019-11-20 16:23:14 +08001059int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001060{
Vladimir Oltean306fd442019-11-09 15:02:50 +02001061 struct ocelot_port *ocelot_port = ocelot->ports[port];
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001062 struct hwtstamp_config cfg;
1063
1064 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1065 return -EFAULT;
1066
1067 /* reserved for future extensions */
1068 if (cfg.flags)
1069 return -EINVAL;
1070
1071 /* Tx type sanity check */
1072 switch (cfg.tx_type) {
1073 case HWTSTAMP_TX_ON:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001074 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001075 break;
1076 case HWTSTAMP_TX_ONESTEP_SYNC:
1077 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1078 * need to update the origin time.
1079 */
Vladimir Oltean306fd442019-11-09 15:02:50 +02001080 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001081 break;
1082 case HWTSTAMP_TX_OFF:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001083 ocelot_port->ptp_cmd = 0;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001084 break;
1085 default:
1086 return -ERANGE;
1087 }
1088
1089 mutex_lock(&ocelot->ptp_lock);
1090
1091 switch (cfg.rx_filter) {
1092 case HWTSTAMP_FILTER_NONE:
1093 break;
1094 case HWTSTAMP_FILTER_ALL:
1095 case HWTSTAMP_FILTER_SOME:
1096 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1097 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1098 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1099 case HWTSTAMP_FILTER_NTP_ALL:
1100 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1101 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1102 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1103 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1104 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1105 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1106 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1107 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1108 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1109 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1110 break;
1111 default:
1112 mutex_unlock(&ocelot->ptp_lock);
1113 return -ERANGE;
1114 }
1115
1116 /* Commit back the result & save it */
1117 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1118 mutex_unlock(&ocelot->ptp_lock);
1119
1120 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1121}
Yangbo Luf1459222019-11-20 16:23:14 +08001122EXPORT_SYMBOL(ocelot_hwstamp_set);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001123
1124static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1125{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001126 struct ocelot_port_private *priv = netdev_priv(dev);
1127 struct ocelot *ocelot = priv->port.ocelot;
1128 int port = priv->chip_port;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001129
1130 /* The function is only used for PTP operations for now */
1131 if (!ocelot->ptp)
1132 return -EOPNOTSUPP;
1133
1134 switch (cmd) {
1135 case SIOCSHWTSTAMP:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001136 return ocelot_hwstamp_set(ocelot, port, ifr);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001137 case SIOCGHWTSTAMP:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001138 return ocelot_hwstamp_get(ocelot, port, ifr);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001139 default:
1140 return -EOPNOTSUPP;
1141 }
1142}
1143
Alexandre Bellonia556c762018-05-14 22:04:57 +02001144static const struct net_device_ops ocelot_port_netdev_ops = {
1145 .ndo_open = ocelot_port_open,
1146 .ndo_stop = ocelot_port_stop,
1147 .ndo_start_xmit = ocelot_port_xmit,
1148 .ndo_set_rx_mode = ocelot_set_rx_mode,
1149 .ndo_get_phys_port_name = ocelot_port_get_phys_port_name,
1150 .ndo_set_mac_address = ocelot_port_set_mac_address,
1151 .ndo_get_stats64 = ocelot_get_stats64,
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001152 .ndo_fdb_add = ocelot_port_fdb_add,
1153 .ndo_fdb_del = ocelot_port_fdb_del,
1154 .ndo_fdb_dump = ocelot_port_fdb_dump,
Antoine Tenart71425292018-06-26 14:28:49 +02001155 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
1156 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
1157 .ndo_set_features = ocelot_set_features,
Florian Fainelli751302c2019-02-06 09:45:40 -08001158 .ndo_get_port_parent_id = ocelot_get_port_parent_id,
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001159 .ndo_setup_tc = ocelot_setup_tc,
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001160 .ndo_do_ioctl = ocelot_ioctl,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001161};
1162
Vladimir Oltean5e256362019-11-14 17:03:27 +02001163void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001164{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001165 int i;
1166
1167 if (sset != ETH_SS_STATS)
1168 return;
1169
1170 for (i = 0; i < ocelot->num_stats; i++)
1171 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1172 ETH_GSTRING_LEN);
1173}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001174EXPORT_SYMBOL(ocelot_get_strings);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001175
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001176static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
1177 u8 *data)
1178{
1179 struct ocelot_port_private *priv = netdev_priv(netdev);
1180 struct ocelot *ocelot = priv->port.ocelot;
1181 int port = priv->chip_port;
1182
1183 ocelot_get_strings(ocelot, port, sset, data);
1184}
1185
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001186static void ocelot_update_stats(struct ocelot *ocelot)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001187{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001188 int i, j;
1189
1190 mutex_lock(&ocelot->stats_lock);
1191
1192 for (i = 0; i < ocelot->num_phys_ports; i++) {
1193 /* Configure the port to read the stats from */
1194 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1195
1196 for (j = 0; j < ocelot->num_stats; j++) {
1197 u32 val;
1198 unsigned int idx = i * ocelot->num_stats + j;
1199
1200 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1201 ocelot->stats_layout[j].offset);
1202
1203 if (val < (ocelot->stats[idx] & U32_MAX))
1204 ocelot->stats[idx] += (u64)1 << 32;
1205
1206 ocelot->stats[idx] = (ocelot->stats[idx] &
1207 ~(u64)U32_MAX) + val;
1208 }
1209 }
1210
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001211 mutex_unlock(&ocelot->stats_lock);
1212}
1213
1214static void ocelot_check_stats_work(struct work_struct *work)
1215{
1216 struct delayed_work *del_work = to_delayed_work(work);
1217 struct ocelot *ocelot = container_of(del_work, struct ocelot,
1218 stats_work);
1219
1220 ocelot_update_stats(ocelot);
1221
Alexandre Bellonia556c762018-05-14 22:04:57 +02001222 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1223 OCELOT_STATS_CHECK_DELAY);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001224}
1225
Vladimir Oltean5e256362019-11-14 17:03:27 +02001226void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001227{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001228 int i;
1229
1230 /* check and update now */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001231 ocelot_update_stats(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001232
1233 /* Copy all counters */
1234 for (i = 0; i < ocelot->num_stats; i++)
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001235 *data++ = ocelot->stats[port * ocelot->num_stats + i];
Alexandre Bellonia556c762018-05-14 22:04:57 +02001236}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001237EXPORT_SYMBOL(ocelot_get_ethtool_stats);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001238
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001239static void ocelot_port_get_ethtool_stats(struct net_device *dev,
1240 struct ethtool_stats *stats,
1241 u64 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001242{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001243 struct ocelot_port_private *priv = netdev_priv(dev);
1244 struct ocelot *ocelot = priv->port.ocelot;
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001245 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001246
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001247 ocelot_get_ethtool_stats(ocelot, port, data);
1248}
1249
Vladimir Oltean5e256362019-11-14 17:03:27 +02001250int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001251{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001252 if (sset != ETH_SS_STATS)
1253 return -EOPNOTSUPP;
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001254
Alexandre Bellonia556c762018-05-14 22:04:57 +02001255 return ocelot->num_stats;
1256}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001257EXPORT_SYMBOL(ocelot_get_sset_count);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001258
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001259static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001260{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001261 struct ocelot_port_private *priv = netdev_priv(dev);
1262 struct ocelot *ocelot = priv->port.ocelot;
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001263 int port = priv->chip_port;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001264
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001265 return ocelot_get_sset_count(ocelot, port, sset);
1266}
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001267
Vladimir Oltean5e256362019-11-14 17:03:27 +02001268int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1269 struct ethtool_ts_info *info)
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001270{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001271 info->phc_index = ocelot->ptp_clock ?
1272 ptp_clock_index(ocelot->ptp_clock) : -1;
1273 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1274 SOF_TIMESTAMPING_RX_SOFTWARE |
1275 SOF_TIMESTAMPING_SOFTWARE |
1276 SOF_TIMESTAMPING_TX_HARDWARE |
1277 SOF_TIMESTAMPING_RX_HARDWARE |
1278 SOF_TIMESTAMPING_RAW_HARDWARE;
1279 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1280 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
1281 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
1282
1283 return 0;
1284}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001285EXPORT_SYMBOL(ocelot_get_ts_info);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001286
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001287static int ocelot_port_get_ts_info(struct net_device *dev,
1288 struct ethtool_ts_info *info)
1289{
1290 struct ocelot_port_private *priv = netdev_priv(dev);
1291 struct ocelot *ocelot = priv->port.ocelot;
1292 int port = priv->chip_port;
1293
1294 if (!ocelot->ptp)
1295 return ethtool_op_get_ts_info(dev, info);
1296
1297 return ocelot_get_ts_info(ocelot, port, info);
1298}
1299
Alexandre Bellonia556c762018-05-14 22:04:57 +02001300static const struct ethtool_ops ocelot_ethtool_ops = {
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001301 .get_strings = ocelot_port_get_strings,
1302 .get_ethtool_stats = ocelot_port_get_ethtool_stats,
1303 .get_sset_count = ocelot_port_get_sset_count,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001304 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1305 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001306 .get_ts_info = ocelot_port_get_ts_info,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001307};
1308
Vladimir Oltean5e256362019-11-14 17:03:27 +02001309void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001310{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001311 u32 port_cfg;
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001312 int p, i;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001313
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001314 if (!(BIT(port) & ocelot->bridge_mask))
1315 return;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001316
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001317 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001318
1319 switch (state) {
1320 case BR_STATE_FORWARDING:
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001321 ocelot->bridge_fwd_mask |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001322 /* Fallthrough */
1323 case BR_STATE_LEARNING:
1324 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1325 break;
1326
1327 default:
1328 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001329 ocelot->bridge_fwd_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001330 break;
1331 }
1332
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001333 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001334
1335 /* Apply FWD mask. The loop is needed to add/remove the current port as
1336 * a source for the other ports.
1337 */
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001338 for (p = 0; p < ocelot->num_phys_ports; p++) {
Vladimir Olteanc9d22032019-11-09 15:03:01 +02001339 if (p == ocelot->cpu || (ocelot->bridge_fwd_mask & BIT(p))) {
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001340 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001341
1342 for (i = 0; i < ocelot->num_phys_ports; i++) {
1343 unsigned long bond_mask = ocelot->lags[i];
1344
1345 if (!bond_mask)
1346 continue;
1347
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001348 if (bond_mask & BIT(p)) {
Alexandre Bellonia556c762018-05-14 22:04:57 +02001349 mask &= ~bond_mask;
1350 break;
1351 }
1352 }
1353
Vladimir Olteanc9d22032019-11-09 15:03:01 +02001354 /* Avoid the NPI port from looping back to itself */
1355 if (p != ocelot->cpu)
1356 mask |= BIT(ocelot->cpu);
1357
1358 ocelot_write_rix(ocelot, mask,
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001359 ANA_PGID_PGID, PGID_SRC + p);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001360 } else {
1361 /* Only the CPU port, this is compatible with link
1362 * aggregation.
1363 */
1364 ocelot_write_rix(ocelot,
Vladimir Olteanc9d22032019-11-09 15:03:01 +02001365 BIT(ocelot->cpu),
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001366 ANA_PGID_PGID, PGID_SRC + p);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001367 }
1368 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001369}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001370EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001371
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001372static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
1373 struct switchdev_trans *trans,
1374 u8 state)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001375{
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001376 if (switchdev_trans_ph_prepare(trans))
1377 return;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001378
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001379 ocelot_bridge_stp_state_set(ocelot, port, state);
1380}
1381
Vladimir Oltean5e256362019-11-14 17:03:27 +02001382void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001383{
1384 ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(msecs / 2),
Alexandre Bellonia556c762018-05-14 22:04:57 +02001385 ANA_AUTOAGE);
1386}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001387EXPORT_SYMBOL(ocelot_set_ageing_time);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001388
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001389static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
1390 unsigned long ageing_clock_t)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001391{
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001392 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1393 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1394
1395 ocelot_set_ageing_time(ocelot, ageing_time);
1396}
1397
1398static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
1399{
1400 u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1401 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1402 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1403 u32 val = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001404
1405 if (mc)
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001406 val = cpu_fwd_mcast;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001407
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001408 ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
1409 ANA_PORT_CPU_FWD_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001410}
1411
1412static int ocelot_port_attr_set(struct net_device *dev,
1413 const struct switchdev_attr *attr,
1414 struct switchdev_trans *trans)
1415{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001416 struct ocelot_port_private *priv = netdev_priv(dev);
1417 struct ocelot *ocelot = priv->port.ocelot;
1418 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001419 int err = 0;
1420
1421 switch (attr->id) {
1422 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001423 ocelot_port_attr_stp_state_set(ocelot, port, trans,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001424 attr->u.stp_state);
1425 break;
1426 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001427 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001428 break;
Antoine Tenart71425292018-06-26 14:28:49 +02001429 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001430 priv->vlan_aware = attr->u.vlan_filtering;
1431 ocelot_port_vlan_filtering(ocelot, port, priv->vlan_aware);
Antoine Tenart71425292018-06-26 14:28:49 +02001432 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001433 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001434 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001435 break;
1436 default:
1437 err = -EOPNOTSUPP;
1438 break;
1439 }
1440
1441 return err;
1442}
1443
Antoine Tenart71425292018-06-26 14:28:49 +02001444static int ocelot_port_obj_add_vlan(struct net_device *dev,
1445 const struct switchdev_obj_port_vlan *vlan,
1446 struct switchdev_trans *trans)
1447{
1448 int ret;
1449 u16 vid;
1450
1451 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1452 ret = ocelot_vlan_vid_add(dev, vid,
1453 vlan->flags & BRIDGE_VLAN_INFO_PVID,
1454 vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
1455 if (ret)
1456 return ret;
1457 }
1458
1459 return 0;
1460}
1461
1462static int ocelot_port_vlan_del_vlan(struct net_device *dev,
1463 const struct switchdev_obj_port_vlan *vlan)
1464{
1465 int ret;
1466 u16 vid;
1467
1468 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1469 ret = ocelot_vlan_vid_del(dev, vid);
1470
1471 if (ret)
1472 return ret;
1473 }
1474
1475 return 0;
1476}
1477
Alexandre Bellonia556c762018-05-14 22:04:57 +02001478static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1479 const unsigned char *addr,
1480 u16 vid)
1481{
1482 struct ocelot_multicast *mc;
1483
1484 list_for_each_entry(mc, &ocelot->multicast, list) {
1485 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1486 return mc;
1487 }
1488
1489 return NULL;
1490}
1491
1492static int ocelot_port_obj_add_mdb(struct net_device *dev,
1493 const struct switchdev_obj_port_mdb *mdb,
1494 struct switchdev_trans *trans)
1495{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001496 struct ocelot_port_private *priv = netdev_priv(dev);
1497 struct ocelot_port *ocelot_port = &priv->port;
1498 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001499 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001500 struct ocelot_multicast *mc;
1501 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001502 u16 vid = mdb->vid;
1503 bool new = false;
1504
1505 if (!vid)
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001506 vid = ocelot_port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001507
1508 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1509 if (!mc) {
1510 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1511 if (!mc)
1512 return -ENOMEM;
1513
1514 memcpy(mc->addr, mdb->addr, ETH_ALEN);
1515 mc->vid = vid;
1516
1517 list_add_tail(&mc->list, &ocelot->multicast);
1518 new = true;
1519 }
1520
1521 memcpy(addr, mc->addr, ETH_ALEN);
1522 addr[0] = 0;
1523
1524 if (!new) {
1525 addr[2] = mc->ports << 0;
1526 addr[1] = mc->ports << 8;
1527 ocelot_mact_forget(ocelot, addr, vid);
1528 }
1529
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001530 mc->ports |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001531 addr[2] = mc->ports << 0;
1532 addr[1] = mc->ports << 8;
1533
1534 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1535}
1536
1537static int ocelot_port_obj_del_mdb(struct net_device *dev,
1538 const struct switchdev_obj_port_mdb *mdb)
1539{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001540 struct ocelot_port_private *priv = netdev_priv(dev);
1541 struct ocelot_port *ocelot_port = &priv->port;
1542 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001543 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001544 struct ocelot_multicast *mc;
1545 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001546 u16 vid = mdb->vid;
1547
1548 if (!vid)
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001549 vid = ocelot_port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001550
1551 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1552 if (!mc)
1553 return -ENOENT;
1554
1555 memcpy(addr, mc->addr, ETH_ALEN);
1556 addr[2] = mc->ports << 0;
1557 addr[1] = mc->ports << 8;
1558 addr[0] = 0;
1559 ocelot_mact_forget(ocelot, addr, vid);
1560
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001561 mc->ports &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001562 if (!mc->ports) {
1563 list_del(&mc->list);
1564 devm_kfree(ocelot->dev, mc);
1565 return 0;
1566 }
1567
1568 addr[2] = mc->ports << 0;
1569 addr[1] = mc->ports << 8;
1570
1571 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1572}
1573
1574static int ocelot_port_obj_add(struct net_device *dev,
1575 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +00001576 struct switchdev_trans *trans,
1577 struct netlink_ext_ack *extack)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001578{
1579 int ret = 0;
1580
1581 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001582 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1583 ret = ocelot_port_obj_add_vlan(dev,
1584 SWITCHDEV_OBJ_PORT_VLAN(obj),
1585 trans);
1586 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001587 case SWITCHDEV_OBJ_ID_PORT_MDB:
1588 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1589 trans);
1590 break;
1591 default:
1592 return -EOPNOTSUPP;
1593 }
1594
1595 return ret;
1596}
1597
1598static int ocelot_port_obj_del(struct net_device *dev,
1599 const struct switchdev_obj *obj)
1600{
1601 int ret = 0;
1602
1603 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001604 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1605 ret = ocelot_port_vlan_del_vlan(dev,
1606 SWITCHDEV_OBJ_PORT_VLAN(obj));
1607 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001608 case SWITCHDEV_OBJ_ID_PORT_MDB:
1609 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1610 break;
1611 default:
1612 return -EOPNOTSUPP;
1613 }
1614
1615 return ret;
1616}
1617
Vladimir Oltean5e256362019-11-14 17:03:27 +02001618int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1619 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001620{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001621 if (!ocelot->bridge_mask) {
1622 ocelot->hw_bridge_dev = bridge;
1623 } else {
1624 if (ocelot->hw_bridge_dev != bridge)
1625 /* This is adding the port to a second bridge, this is
1626 * unsupported */
1627 return -ENODEV;
1628 }
1629
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001630 ocelot->bridge_mask |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001631
1632 return 0;
1633}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001634EXPORT_SYMBOL(ocelot_port_bridge_join);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001635
Vladimir Oltean5e256362019-11-14 17:03:27 +02001636int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1637 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001638{
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001639 ocelot->bridge_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001640
1641 if (!ocelot->bridge_mask)
1642 ocelot->hw_bridge_dev = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02001643
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001644 ocelot_port_vlan_filtering(ocelot, port, 0);
1645 ocelot_port_set_pvid(ocelot, port, 0);
1646 return ocelot_port_set_native_vlan(ocelot, port, 0);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001647}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001648EXPORT_SYMBOL(ocelot_port_bridge_leave);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001649
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001650static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1651{
1652 int i, port, lag;
1653
1654 /* Reset destination and aggregation PGIDS */
1655 for (port = 0; port < ocelot->num_phys_ports; port++)
1656 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1657
1658 for (i = PGID_AGGR; i < PGID_SRC; i++)
1659 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1660 ANA_PGID_PGID, i);
1661
1662 /* Now, set PGIDs for each LAG */
1663 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1664 unsigned long bond_mask;
1665 int aggr_count = 0;
1666 u8 aggr_idx[16];
1667
1668 bond_mask = ocelot->lags[lag];
1669 if (!bond_mask)
1670 continue;
1671
1672 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1673 // Destination mask
1674 ocelot_write_rix(ocelot, bond_mask,
1675 ANA_PGID_PGID, port);
1676 aggr_idx[aggr_count] = port;
1677 aggr_count++;
1678 }
1679
1680 for (i = PGID_AGGR; i < PGID_SRC; i++) {
1681 u32 ac;
1682
1683 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1684 ac &= ~bond_mask;
1685 ac |= BIT(aggr_idx[i % aggr_count]);
1686 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1687 }
1688 }
1689}
1690
1691static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1692{
1693 unsigned long bond_mask = ocelot->lags[lag];
1694 unsigned int p;
1695
1696 for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1697 u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1698
1699 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1700
1701 /* Use lag port as logical port for port i */
1702 ocelot_write_gix(ocelot, port_cfg |
1703 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1704 ANA_PORT_PORT_CFG, p);
1705 }
1706}
1707
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001708static int ocelot_port_lag_join(struct ocelot *ocelot, int port,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001709 struct net_device *bond)
1710{
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001711 struct net_device *ndev;
1712 u32 bond_mask = 0;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001713 int lag, lp;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001714
1715 rcu_read_lock();
1716 for_each_netdev_in_bond_rcu(bond, ndev) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001717 struct ocelot_port_private *priv = netdev_priv(ndev);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001718
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001719 bond_mask |= BIT(priv->chip_port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001720 }
1721 rcu_read_unlock();
1722
1723 lp = __ffs(bond_mask);
1724
1725 /* If the new port is the lowest one, use it as the logical port from
1726 * now on
1727 */
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001728 if (port == lp) {
1729 lag = port;
1730 ocelot->lags[port] = bond_mask;
1731 bond_mask &= ~BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001732 if (bond_mask) {
1733 lp = __ffs(bond_mask);
1734 ocelot->lags[lp] = 0;
1735 }
1736 } else {
1737 lag = lp;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001738 ocelot->lags[lp] |= BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001739 }
1740
1741 ocelot_setup_lag(ocelot, lag);
1742 ocelot_set_aggr_pgids(ocelot);
1743
1744 return 0;
1745}
1746
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001747static void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001748 struct net_device *bond)
1749{
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001750 u32 port_cfg;
1751 int i;
1752
1753 /* Remove port from any lag */
1754 for (i = 0; i < ocelot->num_phys_ports; i++)
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001755 ocelot->lags[i] &= ~BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001756
1757 /* if it was the logical port of the lag, move the lag config to the
1758 * next port
1759 */
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001760 if (ocelot->lags[port]) {
1761 int n = __ffs(ocelot->lags[port]);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001762
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001763 ocelot->lags[n] = ocelot->lags[port];
1764 ocelot->lags[port] = 0;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001765
1766 ocelot_setup_lag(ocelot, n);
1767 }
1768
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001769 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001770 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001771 ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1772 ANA_PORT_PORT_CFG, port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001773
1774 ocelot_set_aggr_pgids(ocelot);
1775}
1776
Alexandre Bellonia556c762018-05-14 22:04:57 +02001777/* Checks if the net_device instance given to us originate from our driver. */
1778static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1779{
1780 return dev->netdev_ops == &ocelot_port_netdev_ops;
1781}
1782
1783static int ocelot_netdevice_port_event(struct net_device *dev,
1784 unsigned long event,
1785 struct netdev_notifier_changeupper_info *info)
1786{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001787 struct ocelot_port_private *priv = netdev_priv(dev);
1788 struct ocelot_port *ocelot_port = &priv->port;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001789 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001790 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001791 int err = 0;
1792
Alexandre Bellonia556c762018-05-14 22:04:57 +02001793 switch (event) {
1794 case NETDEV_CHANGEUPPER:
1795 if (netif_is_bridge_master(info->upper_dev)) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001796 if (info->linking) {
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001797 err = ocelot_port_bridge_join(ocelot, port,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001798 info->upper_dev);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001799 } else {
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001800 err = ocelot_port_bridge_leave(ocelot, port,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001801 info->upper_dev);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001802 priv->vlan_aware = false;
1803 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001804 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001805 if (netif_is_lag_master(info->upper_dev)) {
1806 if (info->linking)
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001807 err = ocelot_port_lag_join(ocelot, port,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001808 info->upper_dev);
1809 else
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001810 ocelot_port_lag_leave(ocelot, port,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001811 info->upper_dev);
1812 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001813 break;
1814 default:
1815 break;
1816 }
1817
1818 return err;
1819}
1820
1821static int ocelot_netdevice_event(struct notifier_block *unused,
1822 unsigned long event, void *ptr)
1823{
1824 struct netdev_notifier_changeupper_info *info = ptr;
1825 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Geert Uytterhoeven2ac0e152018-06-07 15:10:30 +02001826 int ret = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001827
Claudiu Manoil7afb3e52019-11-05 23:50:13 +02001828 if (!ocelot_netdevice_dev_check(dev))
1829 return 0;
1830
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001831 if (event == NETDEV_PRECHANGEUPPER &&
1832 netif_is_lag_master(info->upper_dev)) {
1833 struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1834 struct netlink_ext_ack *extack;
1835
Claudiu Manoil3b3eed82019-11-05 23:50:14 +02001836 if (lag_upper_info &&
1837 lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001838 extack = netdev_notifier_info_to_extack(&info->info);
1839 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1840
1841 ret = -EINVAL;
1842 goto notify;
1843 }
1844 }
1845
Alexandre Bellonia556c762018-05-14 22:04:57 +02001846 if (netif_is_lag_master(dev)) {
1847 struct net_device *slave;
1848 struct list_head *iter;
1849
1850 netdev_for_each_lower_dev(dev, slave, iter) {
1851 ret = ocelot_netdevice_port_event(slave, event, info);
1852 if (ret)
1853 goto notify;
1854 }
1855 } else {
1856 ret = ocelot_netdevice_port_event(dev, event, info);
1857 }
1858
1859notify:
1860 return notifier_from_errno(ret);
1861}
1862
1863struct notifier_block ocelot_netdevice_nb __read_mostly = {
1864 .notifier_call = ocelot_netdevice_event,
1865};
1866EXPORT_SYMBOL(ocelot_netdevice_nb);
1867
Florian Fainelli56da64b2019-02-27 11:44:29 -08001868static int ocelot_switchdev_event(struct notifier_block *unused,
1869 unsigned long event, void *ptr)
1870{
1871 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1872 int err;
1873
1874 switch (event) {
1875 case SWITCHDEV_PORT_ATTR_SET:
1876 err = switchdev_handle_port_attr_set(dev, ptr,
1877 ocelot_netdevice_dev_check,
1878 ocelot_port_attr_set);
1879 return notifier_from_errno(err);
1880 }
1881
1882 return NOTIFY_DONE;
1883}
1884
1885struct notifier_block ocelot_switchdev_nb __read_mostly = {
1886 .notifier_call = ocelot_switchdev_event,
1887};
1888EXPORT_SYMBOL(ocelot_switchdev_nb);
1889
Petr Machata0e332c82018-11-22 23:30:11 +00001890static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1891 unsigned long event, void *ptr)
1892{
1893 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1894 int err;
1895
1896 switch (event) {
1897 /* Blocking events. */
1898 case SWITCHDEV_PORT_OBJ_ADD:
1899 err = switchdev_handle_port_obj_add(dev, ptr,
1900 ocelot_netdevice_dev_check,
1901 ocelot_port_obj_add);
1902 return notifier_from_errno(err);
1903 case SWITCHDEV_PORT_OBJ_DEL:
1904 err = switchdev_handle_port_obj_del(dev, ptr,
1905 ocelot_netdevice_dev_check,
1906 ocelot_port_obj_del);
1907 return notifier_from_errno(err);
Florian Fainelli56da64b2019-02-27 11:44:29 -08001908 case SWITCHDEV_PORT_ATTR_SET:
1909 err = switchdev_handle_port_attr_set(dev, ptr,
1910 ocelot_netdevice_dev_check,
1911 ocelot_port_attr_set);
1912 return notifier_from_errno(err);
Petr Machata0e332c82018-11-22 23:30:11 +00001913 }
1914
1915 return NOTIFY_DONE;
1916}
1917
1918struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1919 .notifier_call = ocelot_switchdev_blocking_event,
1920};
1921EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
1922
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001923int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
1924{
1925 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1926 unsigned long flags;
1927 time64_t s;
1928 u32 val;
1929 s64 ns;
1930
1931 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
1932
1933 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1934 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1935 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
1936 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1937
1938 s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
1939 s <<= 32;
1940 s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
1941 ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
1942
1943 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
1944
1945 /* Deal with negative values */
1946 if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
1947 s--;
1948 ns &= 0xf;
1949 ns += 999999984;
1950 }
1951
1952 set_normalized_timespec64(ts, s, ns);
1953 return 0;
1954}
1955EXPORT_SYMBOL(ocelot_ptp_gettime64);
1956
1957static int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
1958 const struct timespec64 *ts)
1959{
1960 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1961 unsigned long flags;
1962 u32 val;
1963
1964 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
1965
1966 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1967 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1968 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
1969
1970 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1971
1972 ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
1973 TOD_ACC_PIN);
1974 ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
1975 TOD_ACC_PIN);
1976 ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
1977
1978 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1979 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1980 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
1981
1982 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1983
1984 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
1985 return 0;
1986}
1987
1988static int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1989{
1990 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
1991 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1992 unsigned long flags;
1993 u32 val;
1994
1995 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
1996
1997 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1998 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1999 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
2000
2001 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
2002
2003 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
2004 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
2005 ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
2006
2007 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
2008 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
2009 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
2010
2011 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
2012
2013 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2014 } else {
2015 /* Fall back using ocelot_ptp_settime64 which is not exact. */
2016 struct timespec64 ts;
2017 u64 now;
2018
2019 ocelot_ptp_gettime64(ptp, &ts);
2020
2021 now = ktime_to_ns(timespec64_to_ktime(ts));
2022 ts = ns_to_timespec64(now + delta);
2023
2024 ocelot_ptp_settime64(ptp, &ts);
2025 }
2026 return 0;
2027}
2028
2029static int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
2030{
2031 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
2032 u32 unit = 0, direction = 0;
2033 unsigned long flags;
2034 u64 adj = 0;
2035
2036 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
2037
2038 if (!scaled_ppm)
2039 goto disable_adj;
2040
2041 if (scaled_ppm < 0) {
2042 direction = PTP_CFG_CLK_ADJ_CFG_DIR;
2043 scaled_ppm = -scaled_ppm;
2044 }
2045
2046 adj = PSEC_PER_SEC << 16;
2047 do_div(adj, scaled_ppm);
2048 do_div(adj, 1000);
2049
2050 /* If the adjustment value is too large, use ns instead */
2051 if (adj >= (1L << 30)) {
2052 unit = PTP_CFG_CLK_ADJ_FREQ_NS;
2053 do_div(adj, 1000);
2054 }
2055
2056 /* Still too big */
2057 if (adj >= (1L << 30))
2058 goto disable_adj;
2059
2060 ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
2061 ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
2062 PTP_CLK_CFG_ADJ_CFG);
2063
2064 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2065 return 0;
2066
2067disable_adj:
2068 ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
2069
2070 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2071 return 0;
2072}
2073
2074static struct ptp_clock_info ocelot_ptp_clock_info = {
2075 .owner = THIS_MODULE,
2076 .name = "ocelot ptp",
2077 .max_adj = 0x7fffffff,
2078 .n_alarm = 0,
2079 .n_ext_ts = 0,
2080 .n_per_out = 0,
2081 .n_pins = 0,
2082 .pps = 0,
2083 .gettime64 = ocelot_ptp_gettime64,
2084 .settime64 = ocelot_ptp_settime64,
2085 .adjtime = ocelot_ptp_adjtime,
2086 .adjfine = ocelot_ptp_adjfine,
2087};
2088
2089static int ocelot_init_timestamp(struct ocelot *ocelot)
2090{
2091 ocelot->ptp_info = ocelot_ptp_clock_info;
2092 ocelot->ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
2093 if (IS_ERR(ocelot->ptp_clock))
2094 return PTR_ERR(ocelot->ptp_clock);
2095 /* Check if PHC support is missing at the configuration level */
2096 if (!ocelot->ptp_clock)
2097 return 0;
2098
2099 ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
2100 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
2101 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
2102
2103 ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
2104
2105 /* There is no device reconfiguration, PTP Rx stamping is always
2106 * enabled.
2107 */
2108 ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
2109
2110 return 0;
2111}
2112
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002113static void ocelot_port_set_mtu(struct ocelot *ocelot, int port, size_t mtu)
Vladimir Oltean31350d72019-11-09 15:02:56 +02002114{
2115 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002116 int atop_wm;
Vladimir Oltean31350d72019-11-09 15:02:56 +02002117
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002118 ocelot_port_writel(ocelot_port, mtu, DEV_MAC_MAXLEN_CFG);
2119
2120 /* Set Pause WM hysteresis
2121 * 152 = 6 * mtu / OCELOT_BUFFER_CELL_SZ
2122 * 101 = 4 * mtu / OCELOT_BUFFER_CELL_SZ
2123 */
2124 ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
2125 SYS_PAUSE_CFG_PAUSE_STOP(101) |
2126 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, port);
2127
2128 /* Tail dropping watermark */
2129 atop_wm = (ocelot->shared_queue_sz - 9 * mtu) / OCELOT_BUFFER_CELL_SZ;
2130 ocelot_write_rix(ocelot, ocelot_wm_enc(9 * mtu),
2131 SYS_ATOP, port);
2132 ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
2133}
2134
Vladimir Oltean5e256362019-11-14 17:03:27 +02002135void ocelot_init_port(struct ocelot *ocelot, int port)
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002136{
2137 struct ocelot_port *ocelot_port = ocelot->ports[port];
2138
Vladimir Oltean31350d72019-11-09 15:02:56 +02002139 INIT_LIST_HEAD(&ocelot_port->skbs);
2140
2141 /* Basic L2 initialization */
2142
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002143 /* Set MAC IFG Gaps
2144 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
2145 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
2146 */
2147 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
2148 DEV_MAC_IFG_CFG);
2149
2150 /* Load seed (0) and set MAC HDX late collision */
2151 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
2152 DEV_MAC_HDX_CFG_SEED_LOAD,
2153 DEV_MAC_HDX_CFG);
2154 mdelay(1);
2155 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
2156 DEV_MAC_HDX_CFG);
2157
2158 /* Set Max Length and maximum tags allowed */
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002159 ocelot_port_set_mtu(ocelot, port, VLAN_ETH_FRAME_LEN);
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002160 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
2161 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2162 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
2163 DEV_MAC_TAGS_CFG);
2164
2165 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
2166 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
2167 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
2168
Vladimir Oltean31350d72019-11-09 15:02:56 +02002169 /* Drop frames with multicast source address */
2170 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2171 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2172 ANA_PORT_DROP_CFG, port);
2173
2174 /* Set default VLAN and tag type to 8021Q. */
2175 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2176 REW_PORT_VLAN_CFG_PORT_TPID_M,
2177 REW_PORT_VLAN_CFG, port);
2178
2179 /* Enable vcap lookups */
2180 ocelot_vcap_enable(ocelot, port);
2181}
Vladimir Oltean5e256362019-11-14 17:03:27 +02002182EXPORT_SYMBOL(ocelot_init_port);
Vladimir Oltean31350d72019-11-09 15:02:56 +02002183
Alexandre Bellonia556c762018-05-14 22:04:57 +02002184int ocelot_probe_port(struct ocelot *ocelot, u8 port,
2185 void __iomem *regs,
2186 struct phy_device *phy)
2187{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002188 struct ocelot_port_private *priv;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002189 struct ocelot_port *ocelot_port;
2190 struct net_device *dev;
2191 int err;
2192
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002193 dev = alloc_etherdev(sizeof(struct ocelot_port_private));
Alexandre Bellonia556c762018-05-14 22:04:57 +02002194 if (!dev)
2195 return -ENOMEM;
2196 SET_NETDEV_DEV(dev, ocelot->dev);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002197 priv = netdev_priv(dev);
2198 priv->dev = dev;
2199 priv->phy = phy;
2200 priv->chip_port = port;
2201 ocelot_port = &priv->port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002202 ocelot_port->ocelot = ocelot;
2203 ocelot_port->regs = regs;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002204 ocelot->ports[port] = ocelot_port;
2205
2206 dev->netdev_ops = &ocelot_port_netdev_ops;
2207 dev->ethtool_ops = &ocelot_ethtool_ops;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002208
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02002209 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
2210 NETIF_F_HW_TC;
2211 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
Antoine Tenart71425292018-06-26 14:28:49 +02002212
Alexandre Bellonia556c762018-05-14 22:04:57 +02002213 memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2214 dev->dev_addr[ETH_ALEN - 1] += port;
2215 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2216 ENTRYTYPE_LOCKED);
2217
Vladimir Oltean31350d72019-11-09 15:02:56 +02002218 ocelot_init_port(ocelot, port);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002219
Alexandre Bellonia556c762018-05-14 22:04:57 +02002220 err = register_netdev(dev);
2221 if (err) {
2222 dev_err(ocelot->dev, "register_netdev failed\n");
Vladimir Oltean31350d72019-11-09 15:02:56 +02002223 free_netdev(dev);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002224 }
2225
Alexandre Bellonia556c762018-05-14 22:04:57 +02002226 return err;
2227}
2228EXPORT_SYMBOL(ocelot_probe_port);
2229
Vladimir Oltean21468192019-11-09 15:03:00 +02002230void ocelot_set_cpu_port(struct ocelot *ocelot, int cpu,
2231 enum ocelot_tag_prefix injection,
2232 enum ocelot_tag_prefix extraction)
2233{
2234 /* Configure and enable the CPU port. */
2235 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2236 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2237 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2238 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2239 ANA_PORT_PORT_CFG, cpu);
2240
2241 /* If the CPU port is a physical port, set up the port in Node
2242 * Processor Interface (NPI) mode. This is the mode through which
2243 * frames can be injected from and extracted to an external CPU.
2244 * Only one port can be an NPI at the same time.
2245 */
2246 if (cpu < ocelot->num_phys_ports) {
Vladimir Olteanba551bc2019-11-14 17:03:25 +02002247 int mtu = VLAN_ETH_FRAME_LEN + OCELOT_TAG_LEN;
2248
Vladimir Oltean21468192019-11-09 15:03:00 +02002249 ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
2250 QSYS_EXT_CPU_CFG_EXT_CPU_PORT(cpu),
2251 QSYS_EXT_CPU_CFG);
Vladimir Olteanba551bc2019-11-14 17:03:25 +02002252
2253 if (injection == OCELOT_TAG_PREFIX_SHORT)
2254 mtu += OCELOT_SHORT_PREFIX_LEN;
2255 else if (injection == OCELOT_TAG_PREFIX_LONG)
2256 mtu += OCELOT_LONG_PREFIX_LEN;
2257
2258 ocelot_port_set_mtu(ocelot, cpu, mtu);
Vladimir Oltean21468192019-11-09 15:03:00 +02002259 }
2260
2261 /* CPU port Injection/Extraction configuration */
2262 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2263 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2264 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2265 QSYS_SWITCH_PORT_MODE, cpu);
2266 ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
2267 SYS_PORT_MODE_INCL_INJ_HDR(injection),
2268 SYS_PORT_MODE, cpu);
2269
2270 /* Configure the CPU port to be VLAN aware */
2271 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
2272 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2273 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
2274 ANA_PORT_VLAN_CFG, cpu);
2275
2276 ocelot->cpu = cpu;
2277}
2278EXPORT_SYMBOL(ocelot_set_cpu_port);
2279
Alexandre Bellonia556c762018-05-14 22:04:57 +02002280int ocelot_init(struct ocelot *ocelot)
2281{
Alexandre Bellonia556c762018-05-14 22:04:57 +02002282 char queue_name[32];
Vladimir Oltean21468192019-11-09 15:03:00 +02002283 int i, ret;
2284 u32 port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002285
Vladimir Oltean3a77b592019-11-14 17:03:26 +02002286 if (ocelot->ops->reset) {
2287 ret = ocelot->ops->reset(ocelot);
2288 if (ret) {
2289 dev_err(ocelot->dev, "Switch reset failed\n");
2290 return ret;
2291 }
2292 }
2293
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002294 ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2295 sizeof(u32), GFP_KERNEL);
2296 if (!ocelot->lags)
2297 return -ENOMEM;
2298
Alexandre Bellonia556c762018-05-14 22:04:57 +02002299 ocelot->stats = devm_kcalloc(ocelot->dev,
2300 ocelot->num_phys_ports * ocelot->num_stats,
2301 sizeof(u64), GFP_KERNEL);
2302 if (!ocelot->stats)
2303 return -ENOMEM;
2304
2305 mutex_init(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002306 mutex_init(&ocelot->ptp_lock);
2307 spin_lock_init(&ocelot->ptp_clock_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002308 snprintf(queue_name, sizeof(queue_name), "%s-stats",
2309 dev_name(ocelot->dev));
2310 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2311 if (!ocelot->stats_queue)
2312 return -ENOMEM;
2313
Claudiu Manoil2b120dd2019-11-09 15:02:58 +02002314 INIT_LIST_HEAD(&ocelot->multicast);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002315 ocelot_mact_init(ocelot);
2316 ocelot_vlan_init(ocelot);
Horatiu Vulturb5962292019-05-31 09:16:56 +02002317 ocelot_ace_init(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002318
2319 for (port = 0; port < ocelot->num_phys_ports; port++) {
2320 /* Clear all counters (5 groups) */
2321 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2322 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2323 SYS_STAT_CFG);
2324 }
2325
2326 /* Only use S-Tag */
2327 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2328
2329 /* Aggregation mode */
2330 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2331 ANA_AGGR_CFG_AC_DMAC_ENA |
2332 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2333 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2334
2335 /* Set MAC age time to default value. The entry is aged after
2336 * 2*AGE_PERIOD
2337 */
2338 ocelot_write(ocelot,
2339 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2340 ANA_AUTOAGE);
2341
2342 /* Disable learning for frames discarded by VLAN ingress filtering */
2343 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2344
2345 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2346 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2347 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2348
2349 /* Setup flooding PGIDs */
2350 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2351 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2352 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2353 ANA_FLOODING, 0);
2354 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2355 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2356 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2357 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2358 ANA_FLOODING_IPMC);
2359
2360 for (port = 0; port < ocelot->num_phys_ports; port++) {
2361 /* Transmit the frame to the local port. */
2362 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2363 /* Do not forward BPDU frames to the front ports. */
2364 ocelot_write_gix(ocelot,
2365 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2366 ANA_PORT_CPU_FWD_BPDU_CFG,
2367 port);
2368 /* Ensure bridging is disabled */
2369 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2370 }
2371
Alexandre Bellonia556c762018-05-14 22:04:57 +02002372 /* Allow broadcast MAC frames. */
2373 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2374 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2375
2376 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2377 }
2378 ocelot_write_rix(ocelot,
2379 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2380 ANA_PGID_PGID, PGID_MC);
2381 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2382 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2383
Alexandre Bellonia556c762018-05-14 22:04:57 +02002384 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2385 * registers endianness.
2386 */
2387 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2388 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2389 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2390 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2391 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2392 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2393 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2394 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2395 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2396 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2397 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2398 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2399 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2400 for (i = 0; i < 16; i++)
2401 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2402 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2403 ANA_CPUQ_8021_CFG, i);
2404
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03002405 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002406 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2407 OCELOT_STATS_CHECK_DELAY);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002408
2409 if (ocelot->ptp) {
2410 ret = ocelot_init_timestamp(ocelot);
2411 if (ret) {
2412 dev_err(ocelot->dev,
2413 "Timestamp initialization failed\n");
2414 return ret;
2415 }
2416 }
2417
Alexandre Bellonia556c762018-05-14 22:04:57 +02002418 return 0;
2419}
2420EXPORT_SYMBOL(ocelot_init);
2421
2422void ocelot_deinit(struct ocelot *ocelot)
2423{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002424 struct list_head *pos, *tmp;
2425 struct ocelot_port *port;
2426 struct ocelot_skb *entry;
2427 int i;
2428
Claudiu Manoilc5d13962019-07-25 16:33:18 +03002429 cancel_delayed_work(&ocelot->stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002430 destroy_workqueue(ocelot->stats_queue);
2431 mutex_destroy(&ocelot->stats_lock);
Horatiu Vulturb5962292019-05-31 09:16:56 +02002432 ocelot_ace_deinit();
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002433
2434 for (i = 0; i < ocelot->num_phys_ports; i++) {
2435 port = ocelot->ports[i];
2436
2437 list_for_each_safe(pos, tmp, &port->skbs) {
2438 entry = list_entry(pos, struct ocelot_skb, head);
2439
2440 list_del(pos);
2441 dev_kfree_skb_any(entry->skb);
2442 kfree(entry);
2443 }
2444 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02002445}
2446EXPORT_SYMBOL(ocelot_deinit);
2447
2448MODULE_LICENSE("Dual MIT/GPL");