blob: 887b3cc88354f0bf02c4b0a214fa2d41b3e0947b [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 Oltean97bb69e2019-11-09 15:02:47 +0200186static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
187 u16 vid)
188{
189 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300190 u32 val = 0;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200191
192 if (ocelot_port->vid != vid) {
193 /* Always permit deleting the native VLAN (vid = 0) */
194 if (ocelot_port->vid && vid) {
195 dev_err(ocelot->dev,
196 "Port already has a native VLAN: %d\n",
197 ocelot_port->vid);
198 return -EBUSY;
199 }
200 ocelot_port->vid = vid;
201 }
202
203 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
Antoine Tenart71425292018-06-26 14:28:49 +0200204 REW_PORT_VLAN_CFG_PORT_VID_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200205 REW_PORT_VLAN_CFG, port);
206
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300207 if (ocelot_port->vlan_aware && !ocelot_port->vid)
208 /* If port is vlan-aware and tagged, drop untagged and priority
209 * tagged frames.
210 */
211 val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
212 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
213 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
214 ocelot_rmw_gix(ocelot, val,
215 ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
216 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
217 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
218 ANA_PORT_DROP_CFG, port);
219
220 if (ocelot_port->vlan_aware) {
221 if (ocelot_port->vid)
222 /* Tag all frames except when VID == DEFAULT_VLAN */
223 val = REW_TAG_CFG_TAG_CFG(1);
224 else
225 /* Tag all frames */
226 val = REW_TAG_CFG_TAG_CFG(3);
227 } else {
228 /* Port tagging disabled. */
229 val = REW_TAG_CFG_TAG_CFG(0);
230 }
231 ocelot_rmw_gix(ocelot, val,
232 REW_TAG_CFG_TAG_CFG_M,
233 REW_TAG_CFG, port);
234
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200235 return 0;
236}
237
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300238void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
239 bool vlan_aware)
240{
241 struct ocelot_port *ocelot_port = ocelot->ports[port];
242 u32 val;
243
244 ocelot_port->vlan_aware = vlan_aware;
245
246 if (vlan_aware)
247 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
248 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
249 else
250 val = 0;
251 ocelot_rmw_gix(ocelot, val,
252 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
253 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
254 ANA_PORT_VLAN_CFG, port);
255
256 ocelot_port_set_native_vlan(ocelot, port, ocelot_port->vid);
257}
258EXPORT_SYMBOL(ocelot_port_vlan_filtering);
259
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200260/* Default vlan to clasify for untagged frames (may be zero) */
261static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
262{
263 struct ocelot_port *ocelot_port = ocelot->ports[port];
264
265 ocelot_rmw_gix(ocelot,
266 ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
267 ANA_PORT_VLAN_CFG_VLAN_VID_M,
268 ANA_PORT_VLAN_CFG, port);
269
270 ocelot_port->pvid = pvid;
Antoine Tenart71425292018-06-26 14:28:49 +0200271}
272
Vladimir Oltean5e256362019-11-14 17:03:27 +0200273int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
274 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200275{
Antoine Tenart71425292018-06-26 14:28:49 +0200276 int ret;
277
Antoine Tenart71425292018-06-26 14:28:49 +0200278 /* Make the port a member of the VLAN */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200279 ocelot->vlan_mask[vid] |= BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200280 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
281 if (ret)
282 return ret;
283
284 /* Default ingress vlan classification */
285 if (pvid)
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200286 ocelot_port_set_pvid(ocelot, port, vid);
Antoine Tenart71425292018-06-26 14:28:49 +0200287
288 /* Untagged egress vlan clasification */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200289 if (untagged) {
290 ret = ocelot_port_set_native_vlan(ocelot, port, vid);
291 if (ret)
292 return ret;
Vladimir Olteanb9cd75e2019-10-26 21:04:27 +0300293 }
Antoine Tenart71425292018-06-26 14:28:49 +0200294
Antoine Tenart71425292018-06-26 14:28:49 +0200295 return 0;
296}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200297EXPORT_SYMBOL(ocelot_vlan_add);
Antoine Tenart71425292018-06-26 14:28:49 +0200298
Vladimir Oltean98559342019-11-09 15:02:48 +0200299static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
300 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200301{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200302 struct ocelot_port_private *priv = netdev_priv(dev);
303 struct ocelot_port *ocelot_port = &priv->port;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200304 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200305 int port = priv->chip_port;
Antoine Tenart71425292018-06-26 14:28:49 +0200306 int ret;
307
Vladimir Oltean98559342019-11-09 15:02:48 +0200308 ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
309 if (ret)
310 return ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200311
Vladimir Oltean98559342019-11-09 15:02:48 +0200312 /* Add the port MAC address to with the right VLAN information */
313 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
314 ENTRYTYPE_LOCKED);
315
316 return 0;
317}
318
Vladimir Oltean5e256362019-11-14 17:03:27 +0200319int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
Vladimir Oltean98559342019-11-09 15:02:48 +0200320{
321 struct ocelot_port *ocelot_port = ocelot->ports[port];
322 int ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200323
324 /* Stop the port from being a member of the vlan */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200325 ocelot->vlan_mask[vid] &= ~BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200326 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
327 if (ret)
328 return ret;
329
330 /* Ingress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200331 if (ocelot_port->pvid == vid)
332 ocelot_port_set_pvid(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200333
334 /* Egress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200335 if (ocelot_port->vid == vid)
336 ocelot_port_set_native_vlan(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200337
338 return 0;
339}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200340EXPORT_SYMBOL(ocelot_vlan_del);
Antoine Tenart71425292018-06-26 14:28:49 +0200341
Vladimir Oltean98559342019-11-09 15:02:48 +0200342static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
343{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200344 struct ocelot_port_private *priv = netdev_priv(dev);
345 struct ocelot *ocelot = priv->port.ocelot;
346 int port = priv->chip_port;
Vladimir Oltean98559342019-11-09 15:02:48 +0200347 int ret;
348
349 /* 8021q removes VID 0 on module unload for all interfaces
350 * with VLAN filtering feature. We need to keep it to receive
351 * untagged traffic.
352 */
353 if (vid == 0)
354 return 0;
355
356 ret = ocelot_vlan_del(ocelot, port, vid);
357 if (ret)
358 return ret;
359
360 /* Del the port MAC address to with the right VLAN information */
361 ocelot_mact_forget(ocelot, dev->dev_addr, vid);
362
363 return 0;
364}
365
Alexandre Bellonia556c762018-05-14 22:04:57 +0200366static void ocelot_vlan_init(struct ocelot *ocelot)
367{
Antoine Tenart71425292018-06-26 14:28:49 +0200368 u16 port, vid;
369
Alexandre Bellonia556c762018-05-14 22:04:57 +0200370 /* Clear VLAN table, by default all ports are members of all VLANs */
371 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
372 ANA_TABLES_VLANACCESS);
373 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200374
375 /* Configure the port VLAN memberships */
376 for (vid = 1; vid < VLAN_N_VID; vid++) {
377 ocelot->vlan_mask[vid] = 0;
378 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
379 }
380
381 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
382 * traffic. It is added automatically if 8021q module is loaded, but
383 * we can't rely on it since module may be not loaded.
384 */
385 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
386 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
387
Antoine Tenart71425292018-06-26 14:28:49 +0200388 /* Set vlan ingress filter mask to all ports but the CPU port by
389 * default.
390 */
Vladimir Oltean714d0ff2019-11-09 15:02:55 +0200391 ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
392 ANA_VLANMASK);
Antoine Tenart71425292018-06-26 14:28:49 +0200393
394 for (port = 0; port < ocelot->num_phys_ports; port++) {
395 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
396 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
397 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200398}
399
400/* Watermark encode
401 * Bit 8: Unit; 0:1, 1:16
402 * Bit 7-0: Value to be multiplied with unit
403 */
404static u16 ocelot_wm_enc(u16 value)
405{
406 if (value >= BIT(8))
407 return BIT(8) | (value / 16);
408
409 return value;
410}
411
Vladimir Oltean5e256362019-11-14 17:03:27 +0200412void ocelot_adjust_link(struct ocelot *ocelot, int port,
413 struct phy_device *phydev)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200414{
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200415 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +0200416 int speed, mode = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200417
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200418 switch (phydev->speed) {
Alexandre Bellonia556c762018-05-14 22:04:57 +0200419 case SPEED_10:
420 speed = OCELOT_SPEED_10;
421 break;
422 case SPEED_100:
423 speed = OCELOT_SPEED_100;
424 break;
425 case SPEED_1000:
426 speed = OCELOT_SPEED_1000;
427 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
428 break;
429 case SPEED_2500:
430 speed = OCELOT_SPEED_2500;
431 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
432 break;
433 default:
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200434 dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
435 port, phydev->speed);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200436 return;
437 }
438
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200439 phy_print_status(phydev);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200440
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200441 if (!phydev->link)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200442 return;
443
444 /* Only full duplex supported for now */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200445 ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200446 mode, DEV_MAC_MODE_CFG);
447
Vladimir Oltean1ba8f652020-02-29 16:31:11 +0200448 /* Disable HDX fast control */
449 ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
450 DEV_PORT_MISC);
451
452 /* SGMII only for now */
453 ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
454 PCS1G_MODE_CFG);
455 ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
456
457 /* Enable PCS */
458 ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
459
460 /* No aneg on SGMII */
461 ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
462
463 /* No loopback */
464 ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200465
Alexandre Bellonia556c762018-05-14 22:04:57 +0200466 /* Enable MAC module */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200467 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
Alexandre Bellonia556c762018-05-14 22:04:57 +0200468 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
469
470 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
471 * reset */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200472 ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
Alexandre Bellonia556c762018-05-14 22:04:57 +0200473 DEV_CLOCK_CFG);
474
Alexandre Bellonia556c762018-05-14 22:04:57 +0200475 /* No PFC */
476 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200477 ANA_PFC_PFC_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200478
Alexandre Bellonia556c762018-05-14 22:04:57 +0200479 /* Core: Enable port for frame transfer */
480 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
481 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
482 QSYS_SWITCH_PORT_MODE_PORT_ENA,
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200483 QSYS_SWITCH_PORT_MODE, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200484
485 /* Flow control */
486 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
487 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
488 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
489 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
490 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200491 SYS_MAC_FC_CFG, port);
492 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200493}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200494EXPORT_SYMBOL(ocelot_adjust_link);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200495
Vladimir Oltean26f4dba2019-11-09 15:02:59 +0200496static void ocelot_port_adjust_link(struct net_device *dev)
497{
498 struct ocelot_port_private *priv = netdev_priv(dev);
499 struct ocelot *ocelot = priv->port.ocelot;
500 int port = priv->chip_port;
501
502 ocelot_adjust_link(ocelot, port, dev->phydev);
503}
504
Vladimir Oltean5e256362019-11-14 17:03:27 +0200505void ocelot_port_enable(struct ocelot *ocelot, int port,
506 struct phy_device *phy)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200507{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200508 /* Enable receiving frames on the port, and activate auto-learning of
509 * MAC addresses.
510 */
511 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
512 ANA_PORT_PORT_CFG_RECV_ENA |
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200513 ANA_PORT_PORT_CFG_PORTID_VAL(port),
514 ANA_PORT_PORT_CFG, port);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200515}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200516EXPORT_SYMBOL(ocelot_port_enable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200517
518static int ocelot_port_open(struct net_device *dev)
519{
520 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Olteanee50d072020-01-06 03:34:15 +0200521 struct ocelot_port *ocelot_port = &priv->port;
522 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean889b8952019-11-09 15:02:57 +0200523 int port = priv->chip_port;
524 int err;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200525
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200526 if (priv->serdes) {
527 err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
Vladimir Olteanee50d072020-01-06 03:34:15 +0200528 ocelot_port->phy_mode);
Quentin Schulz71e32a202018-10-04 14:22:08 +0200529 if (err) {
530 netdev_err(dev, "Could not set mode of SerDes\n");
531 return err;
532 }
533 }
534
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200535 err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
Vladimir Olteanee50d072020-01-06 03:34:15 +0200536 ocelot_port->phy_mode);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200537 if (err) {
538 netdev_err(dev, "Could not attach to PHY\n");
539 return err;
540 }
541
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200542 dev->phydev = priv->phy;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200543
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200544 phy_attached_info(priv->phy);
545 phy_start(priv->phy);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200546
547 ocelot_port_enable(ocelot, port, priv->phy);
548
Alexandre Bellonia556c762018-05-14 22:04:57 +0200549 return 0;
550}
551
Vladimir Oltean5e256362019-11-14 17:03:27 +0200552void ocelot_port_disable(struct ocelot *ocelot, int port)
Vladimir Oltean889b8952019-11-09 15:02:57 +0200553{
554 struct ocelot_port *ocelot_port = ocelot->ports[port];
555
556 ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
557 ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
558 QSYS_SWITCH_PORT_MODE, port);
559}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200560EXPORT_SYMBOL(ocelot_port_disable);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200561
Alexandre Bellonia556c762018-05-14 22:04:57 +0200562static int ocelot_port_stop(struct net_device *dev)
563{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200564 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean889b8952019-11-09 15:02:57 +0200565 struct ocelot *ocelot = priv->port.ocelot;
566 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200567
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200568 phy_disconnect(priv->phy);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200569
570 dev->phydev = NULL;
571
Vladimir Oltean889b8952019-11-09 15:02:57 +0200572 ocelot_port_disable(ocelot, port);
573
Alexandre Bellonia556c762018-05-14 22:04:57 +0200574 return 0;
575}
576
577/* Generate the IFH for frame injection
578 *
579 * The IFH is a 128bit-value
580 * bit 127: bypass the analyzer processing
581 * bit 56-67: destination mask
582 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
583 * bit 20-27: cpu extraction queue mask
584 * bit 16: tag type 0: C-tag, 1: S-tag
585 * bit 0-11: VID
586 */
587static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
588{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200589 ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
Antoine Tenart08d02362018-06-20 10:50:46 +0200590 ifh[1] = (0xf00 & info->port) >> 8;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200591 ifh[2] = (0xff & info->port) << 24;
Antoine Tenart08d02362018-06-20 10:50:46 +0200592 ifh[3] = (info->tag_type << 16) | info->vid;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200593
594 return 0;
595}
596
Yangbo Lu400928b2019-11-20 16:23:16 +0800597int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
598 struct sk_buff *skb)
599{
600 struct skb_shared_info *shinfo = skb_shinfo(skb);
601 struct ocelot *ocelot = ocelot_port->ocelot;
602
603 if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
604 ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
Yangbo Lu400928b2019-11-20 16:23:16 +0800605 shinfo->tx_flags |= SKBTX_IN_PROGRESS;
Yangbo Lub049da12019-11-27 15:27:57 +0800606 /* Store timestamp ID in cb[0] of sk_buff */
607 skb->cb[0] = ocelot_port->ts_id % 4;
608 skb_queue_tail(&ocelot_port->tx_skbs, skb);
Yangbo Lu400928b2019-11-20 16:23:16 +0800609 return 0;
610 }
611 return -ENODATA;
612}
613EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
614
Alexandre Bellonia556c762018-05-14 22:04:57 +0200615static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
616{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200617 struct ocelot_port_private *priv = netdev_priv(dev);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200618 struct skb_shared_info *shinfo = skb_shinfo(skb);
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200619 struct ocelot_port *ocelot_port = &priv->port;
620 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Olteanf24711f2019-11-14 17:03:24 +0200621 u32 val, ifh[OCELOT_TAG_LEN / 4];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200622 struct frame_info info = {};
623 u8 grp = 0; /* Send everything on CPU group 0 */
624 unsigned int i, count, last;
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200625 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200626
627 val = ocelot_read(ocelot, QS_INJ_STATUS);
628 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
629 (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
630 return NETDEV_TX_BUSY;
631
632 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
633 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
634
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200635 info.port = BIT(port);
Antoine Tenart08d02362018-06-20 10:50:46 +0200636 info.tag_type = IFH_TAG_TYPE_C;
637 info.vid = skb_vlan_tag_get(skb);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200638
639 /* Check if timestamping is needed */
640 if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200641 info.rew_op = ocelot_port->ptp_cmd;
642 if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
643 info.rew_op |= (ocelot_port->ts_id % 4) << 3;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200644 }
645
Alexandre Bellonia556c762018-05-14 22:04:57 +0200646 ocelot_gen_ifh(ifh, &info);
647
Vladimir Olteanf24711f2019-11-14 17:03:24 +0200648 for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
Antoine Tenartc2cd6502018-06-22 11:50:52 +0200649 ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
650 QS_INJ_WR, grp);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200651
652 count = (skb->len + 3) / 4;
653 last = skb->len % 4;
654 for (i = 0; i < count; i++) {
655 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
656 }
657
658 /* Add padding */
659 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
660 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
661 i++;
662 }
663
664 /* Indicate EOF and valid bytes in last word */
665 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
666 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
667 QS_INJ_CTRL_EOF,
668 QS_INJ_CTRL, grp);
669
670 /* Add dummy CRC */
671 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
672 skb_tx_timestamp(skb);
673
674 dev->stats.tx_packets++;
675 dev->stats.tx_bytes += skb->len;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200676
Yangbo Lu400928b2019-11-20 16:23:16 +0800677 if (!ocelot_port_add_txtstamp_skb(ocelot_port, skb)) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200678 ocelot_port->ts_id++;
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200679 return NETDEV_TX_OK;
680 }
681
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200682 dev_kfree_skb_any(skb);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200683 return NETDEV_TX_OK;
684}
685
Yangbo Lue23a7b32019-11-20 16:23:15 +0800686static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
687 struct timespec64 *ts)
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200688{
689 unsigned long flags;
690 u32 val;
691
692 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
693
694 /* Read current PTP time to get seconds */
695 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
696
697 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
698 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
699 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
700 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
701
702 /* Read packet HW timestamp from FIFO */
703 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
704 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
705
706 /* Sec has incremented since the ts was registered */
707 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
708 ts->tv_sec--;
709
710 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
711}
Yangbo Lue23a7b32019-11-20 16:23:15 +0800712
713void ocelot_get_txtstamp(struct ocelot *ocelot)
714{
715 int budget = OCELOT_PTP_QUEUE_SZ;
716
717 while (budget--) {
Yangbo Lub049da12019-11-27 15:27:57 +0800718 struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800719 struct skb_shared_hwtstamps shhwtstamps;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800720 struct ocelot_port *port;
721 struct timespec64 ts;
Yangbo Lub049da12019-11-27 15:27:57 +0800722 unsigned long flags;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800723 u32 val, id, txport;
724
725 val = ocelot_read(ocelot, SYS_PTP_STATUS);
726
727 /* Check if a timestamp can be retrieved */
728 if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
729 break;
730
731 WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
732
733 /* Retrieve the ts ID and Tx port */
734 id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
735 txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
736
737 /* Retrieve its associated skb */
738 port = ocelot->ports[txport];
739
Yangbo Lub049da12019-11-27 15:27:57 +0800740 spin_lock_irqsave(&port->tx_skbs.lock, flags);
741
742 skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
743 if (skb->cb[0] != id)
Yangbo Lue23a7b32019-11-20 16:23:15 +0800744 continue;
Yangbo Lub049da12019-11-27 15:27:57 +0800745 __skb_unlink(skb, &port->tx_skbs);
746 skb_match = skb;
Yangbo Lufc62c092019-11-27 15:27:56 +0800747 break;
Yangbo Lue23a7b32019-11-20 16:23:15 +0800748 }
749
Yangbo Lub049da12019-11-27 15:27:57 +0800750 spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
751
Yangbo Lue23a7b32019-11-20 16:23:15 +0800752 /* Next ts */
753 ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
754
Yangbo Lub049da12019-11-27 15:27:57 +0800755 if (unlikely(!skb_match))
Yangbo Lue23a7b32019-11-20 16:23:15 +0800756 continue;
757
758 /* Get the h/w timestamp */
759 ocelot_get_hwtimestamp(ocelot, &ts);
760
761 /* Set the timestamp into the skb */
762 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
763 shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
Yangbo Lub049da12019-11-27 15:27:57 +0800764 skb_tstamp_tx(skb_match, &shhwtstamps);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800765
Yangbo Lub049da12019-11-27 15:27:57 +0800766 dev_kfree_skb_any(skb_match);
Yangbo Lue23a7b32019-11-20 16:23:15 +0800767 }
768}
769EXPORT_SYMBOL(ocelot_get_txtstamp);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200770
Claudiu Manoil40a15782019-05-21 19:52:55 +0300771static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200772{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200773 struct ocelot_port_private *priv = netdev_priv(dev);
774 struct ocelot_port *ocelot_port = &priv->port;
775 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200776
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200777 return ocelot_mact_forget(ocelot, addr, ocelot_port->pvid);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200778}
779
Claudiu Manoil40a15782019-05-21 19:52:55 +0300780static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200781{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200782 struct ocelot_port_private *priv = netdev_priv(dev);
783 struct ocelot_port *ocelot_port = &priv->port;
784 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200785
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200786 return ocelot_mact_learn(ocelot, PGID_CPU, addr, ocelot_port->pvid,
Claudiu Manoil40a15782019-05-21 19:52:55 +0300787 ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200788}
789
790static void ocelot_set_rx_mode(struct net_device *dev)
791{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200792 struct ocelot_port_private *priv = netdev_priv(dev);
793 struct ocelot *ocelot = priv->port.ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200794 u32 val;
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200795 int i;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200796
797 /* This doesn't handle promiscuous mode because the bridge core is
798 * setting IFF_PROMISC on all slave interfaces and all frames would be
799 * forwarded to the CPU port.
800 */
801 val = GENMASK(ocelot->num_phys_ports - 1, 0);
802 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
803 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
804
Claudiu Manoil40a15782019-05-21 19:52:55 +0300805 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200806}
807
808static int ocelot_port_get_phys_port_name(struct net_device *dev,
809 char *buf, size_t len)
810{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200811 struct ocelot_port_private *priv = netdev_priv(dev);
812 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200813 int ret;
814
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200815 ret = snprintf(buf, len, "p%d", port);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200816 if (ret >= len)
817 return -EINVAL;
818
819 return 0;
820}
821
822static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
823{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200824 struct ocelot_port_private *priv = netdev_priv(dev);
825 struct ocelot_port *ocelot_port = &priv->port;
826 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200827 const struct sockaddr *addr = p;
828
829 /* Learn the new net device MAC address in the mac table. */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200830 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, ocelot_port->pvid,
Alexandre Bellonia556c762018-05-14 22:04:57 +0200831 ENTRYTYPE_LOCKED);
832 /* Then forget the previous one. */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200833 ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200834
835 ether_addr_copy(dev->dev_addr, addr->sa_data);
836 return 0;
837}
838
839static void ocelot_get_stats64(struct net_device *dev,
840 struct rtnl_link_stats64 *stats)
841{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200842 struct ocelot_port_private *priv = netdev_priv(dev);
843 struct ocelot *ocelot = priv->port.ocelot;
844 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200845
846 /* Configure the port to read the stats from */
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200847 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
Alexandre Bellonia556c762018-05-14 22:04:57 +0200848 SYS_STAT_CFG);
849
850 /* Get Rx stats */
851 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
852 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
853 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
854 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
855 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
856 ocelot_read(ocelot, SYS_COUNT_RX_64) +
857 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
858 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
859 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
860 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
861 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
862 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
863 stats->rx_dropped = dev->stats.rx_dropped;
864
865 /* Get Tx stats */
866 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
867 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
868 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
869 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
870 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
871 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
872 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
873 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
874 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
875 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
876}
877
Vladimir Oltean5e256362019-11-14 17:03:27 +0200878int ocelot_fdb_add(struct ocelot *ocelot, int port,
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300879 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200880{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200881 struct ocelot_port *ocelot_port = ocelot->ports[port];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200882
Antoine Tenart71425292018-06-26 14:28:49 +0200883 if (!vid) {
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300884 if (!ocelot_port->vlan_aware)
Antoine Tenart71425292018-06-26 14:28:49 +0200885 /* If the bridge is not VLAN aware and no VID was
886 * provided, set it to pvid to ensure the MAC entry
887 * matches incoming untagged packets
888 */
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200889 vid = ocelot_port->pvid;
Antoine Tenart71425292018-06-26 14:28:49 +0200890 else
891 /* If the bridge is VLAN aware a VID must be provided as
892 * otherwise the learnt entry wouldn't match any frame.
893 */
894 return -EINVAL;
895 }
896
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200897 return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200898}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200899EXPORT_SYMBOL(ocelot_fdb_add);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200900
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200901static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
902 struct net_device *dev,
903 const unsigned char *addr,
904 u16 vid, u16 flags,
905 struct netlink_ext_ack *extack)
906{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200907 struct ocelot_port_private *priv = netdev_priv(dev);
908 struct ocelot *ocelot = priv->port.ocelot;
909 int port = priv->chip_port;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200910
Vladimir Oltean87b0f982020-04-14 22:36:15 +0300911 return ocelot_fdb_add(ocelot, port, addr, vid);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200912}
913
Vladimir Oltean5e256362019-11-14 17:03:27 +0200914int ocelot_fdb_del(struct ocelot *ocelot, int port,
915 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200916{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200917 return ocelot_mact_forget(ocelot, addr, vid);
918}
Vladimir Oltean5e256362019-11-14 17:03:27 +0200919EXPORT_SYMBOL(ocelot_fdb_del);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200920
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200921static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
922 struct net_device *dev,
923 const unsigned char *addr, u16 vid)
924{
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200925 struct ocelot_port_private *priv = netdev_priv(dev);
926 struct ocelot *ocelot = priv->port.ocelot;
927 int port = priv->chip_port;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200928
Vladimir Oltean004d44f2019-11-09 15:02:53 +0200929 return ocelot_fdb_del(ocelot, port, addr, vid);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200930}
931
Alexandre Bellonia556c762018-05-14 22:04:57 +0200932struct ocelot_dump_ctx {
933 struct net_device *dev;
934 struct sk_buff *skb;
935 struct netlink_callback *cb;
936 int idx;
937};
938
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200939static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
940 bool is_static, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200941{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200942 struct ocelot_dump_ctx *dump = data;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200943 u32 portid = NETLINK_CB(dump->cb->skb).portid;
944 u32 seq = dump->cb->nlh->nlmsg_seq;
945 struct nlmsghdr *nlh;
946 struct ndmsg *ndm;
947
948 if (dump->idx < dump->cb->args[2])
949 goto skip;
950
951 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
952 sizeof(*ndm), NLM_F_MULTI);
953 if (!nlh)
954 return -EMSGSIZE;
955
956 ndm = nlmsg_data(nlh);
957 ndm->ndm_family = AF_BRIDGE;
958 ndm->ndm_pad1 = 0;
959 ndm->ndm_pad2 = 0;
960 ndm->ndm_flags = NTF_SELF;
961 ndm->ndm_type = 0;
962 ndm->ndm_ifindex = dump->dev->ifindex;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200963 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200964
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200965 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200966 goto nla_put_failure;
967
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200968 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200969 goto nla_put_failure;
970
971 nlmsg_end(dump->skb, nlh);
972
973skip:
974 dump->idx++;
975 return 0;
976
977nla_put_failure:
978 nlmsg_cancel(dump->skb, nlh);
979 return -EMSGSIZE;
980}
981
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200982static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
983 struct ocelot_mact_entry *entry)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200984{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200985 u32 val, dst, macl, mach;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200986 char mac[ETH_ALEN];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200987
988 /* Set row and column to read from */
989 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
990 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
991
992 /* Issue a read command */
993 ocelot_write(ocelot,
994 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
995 ANA_TABLES_MACACCESS);
996
997 if (ocelot_mact_wait_for_completion(ocelot))
998 return -ETIMEDOUT;
999
1000 /* Read the entry flags */
1001 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1002 if (!(val & ANA_TABLES_MACACCESS_VALID))
1003 return -EINVAL;
1004
1005 /* If the entry read has another port configured as its destination,
1006 * do not report it.
1007 */
1008 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001009 if (dst != port)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001010 return -EINVAL;
1011
1012 /* Get the entry's MAC address and VLAN id */
1013 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1014 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1015
1016 mac[0] = (mach >> 8) & 0xff;
1017 mac[1] = (mach >> 0) & 0xff;
1018 mac[2] = (macl >> 24) & 0xff;
1019 mac[3] = (macl >> 16) & 0xff;
1020 mac[4] = (macl >> 8) & 0xff;
1021 mac[5] = (macl >> 0) & 0xff;
1022
1023 entry->vid = (mach >> 16) & 0xfff;
1024 ether_addr_copy(entry->mac, mac);
1025
1026 return 0;
1027}
1028
Vladimir Oltean5e256362019-11-14 17:03:27 +02001029int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1030 dsa_fdb_dump_cb_t *cb, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001031{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001032 int i, j;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001033
Vladimir Oltean21ce7f32020-05-04 01:20:26 +03001034 /* Loop through all the mac tables entries. */
1035 for (i = 0; i < ocelot->num_mact_rows; i++) {
Alexandre Bellonia556c762018-05-14 22:04:57 +02001036 for (j = 0; j < 4; j++) {
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001037 struct ocelot_mact_entry entry;
1038 bool is_static;
1039 int ret;
1040
1041 ret = ocelot_mact_read(ocelot, port, i, j, &entry);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001042 /* If the entry is invalid (wrong port, invalid...),
1043 * skip it.
1044 */
1045 if (ret == -EINVAL)
1046 continue;
1047 else if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001048 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001049
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001050 is_static = (entry.type == ENTRYTYPE_LOCKED);
1051
1052 ret = cb(entry.mac, entry.vid, is_static, data);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001053 if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001054 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001055 }
1056 }
1057
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001058 return 0;
1059}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001060EXPORT_SYMBOL(ocelot_fdb_dump);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001061
1062static int ocelot_port_fdb_dump(struct sk_buff *skb,
1063 struct netlink_callback *cb,
1064 struct net_device *dev,
1065 struct net_device *filter_dev, int *idx)
1066{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001067 struct ocelot_port_private *priv = netdev_priv(dev);
1068 struct ocelot *ocelot = priv->port.ocelot;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001069 struct ocelot_dump_ctx dump = {
1070 .dev = dev,
1071 .skb = skb,
1072 .cb = cb,
1073 .idx = *idx,
1074 };
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001075 int port = priv->chip_port;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001076 int ret;
1077
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001078 ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001079
Alexandre Bellonia556c762018-05-14 22:04:57 +02001080 *idx = dump.idx;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001081
Alexandre Bellonia556c762018-05-14 22:04:57 +02001082 return ret;
1083}
1084
Antoine Tenart71425292018-06-26 14:28:49 +02001085static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1086 u16 vid)
1087{
Vladimir Oltean1c44ce52019-10-26 21:04:26 +03001088 return ocelot_vlan_vid_add(dev, vid, false, false);
Antoine Tenart71425292018-06-26 14:28:49 +02001089}
1090
1091static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1092 u16 vid)
1093{
1094 return ocelot_vlan_vid_del(dev, vid);
1095}
1096
1097static int ocelot_set_features(struct net_device *dev,
1098 netdev_features_t features)
1099{
Antoine Tenart71425292018-06-26 14:28:49 +02001100 netdev_features_t changed = dev->features ^ features;
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001101 struct ocelot_port_private *priv = netdev_priv(dev);
1102 struct ocelot *ocelot = priv->port.ocelot;
1103 int port = priv->chip_port;
Antoine Tenart71425292018-06-26 14:28:49 +02001104
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001105 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001106 priv->tc.offload_cnt) {
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001107 netdev_err(dev,
1108 "Cannot disable HW TC offload while offloads active\n");
1109 return -EBUSY;
1110 }
1111
Antoine Tenart71425292018-06-26 14:28:49 +02001112 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001113 ocelot_vlan_mode(ocelot, port, features);
Antoine Tenart71425292018-06-26 14:28:49 +02001114
1115 return 0;
1116}
1117
Florian Fainelli751302c2019-02-06 09:45:40 -08001118static int ocelot_get_port_parent_id(struct net_device *dev,
1119 struct netdev_phys_item_id *ppid)
1120{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001121 struct ocelot_port_private *priv = netdev_priv(dev);
1122 struct ocelot *ocelot = priv->port.ocelot;
Florian Fainelli751302c2019-02-06 09:45:40 -08001123
1124 ppid->id_len = sizeof(ocelot->base_mac);
1125 memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
1126
1127 return 0;
1128}
1129
Yangbo Luf1459222019-11-20 16:23:14 +08001130int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001131{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001132 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1133 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1134}
Yangbo Luf1459222019-11-20 16:23:14 +08001135EXPORT_SYMBOL(ocelot_hwstamp_get);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001136
Yangbo Luf1459222019-11-20 16:23:14 +08001137int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001138{
Vladimir Oltean306fd442019-11-09 15:02:50 +02001139 struct ocelot_port *ocelot_port = ocelot->ports[port];
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001140 struct hwtstamp_config cfg;
1141
1142 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1143 return -EFAULT;
1144
1145 /* reserved for future extensions */
1146 if (cfg.flags)
1147 return -EINVAL;
1148
1149 /* Tx type sanity check */
1150 switch (cfg.tx_type) {
1151 case HWTSTAMP_TX_ON:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001152 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001153 break;
1154 case HWTSTAMP_TX_ONESTEP_SYNC:
1155 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1156 * need to update the origin time.
1157 */
Vladimir Oltean306fd442019-11-09 15:02:50 +02001158 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001159 break;
1160 case HWTSTAMP_TX_OFF:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001161 ocelot_port->ptp_cmd = 0;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001162 break;
1163 default:
1164 return -ERANGE;
1165 }
1166
1167 mutex_lock(&ocelot->ptp_lock);
1168
1169 switch (cfg.rx_filter) {
1170 case HWTSTAMP_FILTER_NONE:
1171 break;
1172 case HWTSTAMP_FILTER_ALL:
1173 case HWTSTAMP_FILTER_SOME:
1174 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1175 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1176 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1177 case HWTSTAMP_FILTER_NTP_ALL:
1178 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1179 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1180 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1181 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1182 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1183 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1184 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1185 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1186 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1187 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1188 break;
1189 default:
1190 mutex_unlock(&ocelot->ptp_lock);
1191 return -ERANGE;
1192 }
1193
1194 /* Commit back the result & save it */
1195 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1196 mutex_unlock(&ocelot->ptp_lock);
1197
1198 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1199}
Yangbo Luf1459222019-11-20 16:23:14 +08001200EXPORT_SYMBOL(ocelot_hwstamp_set);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001201
1202static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1203{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001204 struct ocelot_port_private *priv = netdev_priv(dev);
1205 struct ocelot *ocelot = priv->port.ocelot;
1206 int port = priv->chip_port;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001207
1208 /* The function is only used for PTP operations for now */
1209 if (!ocelot->ptp)
1210 return -EOPNOTSUPP;
1211
1212 switch (cmd) {
1213 case SIOCSHWTSTAMP:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001214 return ocelot_hwstamp_set(ocelot, port, ifr);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001215 case SIOCGHWTSTAMP:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001216 return ocelot_hwstamp_get(ocelot, port, ifr);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001217 default:
1218 return -EOPNOTSUPP;
1219 }
1220}
1221
Alexandre Bellonia556c762018-05-14 22:04:57 +02001222static const struct net_device_ops ocelot_port_netdev_ops = {
1223 .ndo_open = ocelot_port_open,
1224 .ndo_stop = ocelot_port_stop,
1225 .ndo_start_xmit = ocelot_port_xmit,
1226 .ndo_set_rx_mode = ocelot_set_rx_mode,
1227 .ndo_get_phys_port_name = ocelot_port_get_phys_port_name,
1228 .ndo_set_mac_address = ocelot_port_set_mac_address,
1229 .ndo_get_stats64 = ocelot_get_stats64,
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001230 .ndo_fdb_add = ocelot_port_fdb_add,
1231 .ndo_fdb_del = ocelot_port_fdb_del,
1232 .ndo_fdb_dump = ocelot_port_fdb_dump,
Antoine Tenart71425292018-06-26 14:28:49 +02001233 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
1234 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
1235 .ndo_set_features = ocelot_set_features,
Florian Fainelli751302c2019-02-06 09:45:40 -08001236 .ndo_get_port_parent_id = ocelot_get_port_parent_id,
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001237 .ndo_setup_tc = ocelot_setup_tc,
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001238 .ndo_do_ioctl = ocelot_ioctl,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001239};
1240
Vladimir Oltean5e256362019-11-14 17:03:27 +02001241void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001242{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001243 int i;
1244
1245 if (sset != ETH_SS_STATS)
1246 return;
1247
1248 for (i = 0; i < ocelot->num_stats; i++)
1249 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1250 ETH_GSTRING_LEN);
1251}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001252EXPORT_SYMBOL(ocelot_get_strings);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001253
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001254static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
1255 u8 *data)
1256{
1257 struct ocelot_port_private *priv = netdev_priv(netdev);
1258 struct ocelot *ocelot = priv->port.ocelot;
1259 int port = priv->chip_port;
1260
1261 ocelot_get_strings(ocelot, port, sset, data);
1262}
1263
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001264static void ocelot_update_stats(struct ocelot *ocelot)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001265{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001266 int i, j;
1267
1268 mutex_lock(&ocelot->stats_lock);
1269
1270 for (i = 0; i < ocelot->num_phys_ports; i++) {
1271 /* Configure the port to read the stats from */
1272 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1273
1274 for (j = 0; j < ocelot->num_stats; j++) {
1275 u32 val;
1276 unsigned int idx = i * ocelot->num_stats + j;
1277
1278 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1279 ocelot->stats_layout[j].offset);
1280
1281 if (val < (ocelot->stats[idx] & U32_MAX))
1282 ocelot->stats[idx] += (u64)1 << 32;
1283
1284 ocelot->stats[idx] = (ocelot->stats[idx] &
1285 ~(u64)U32_MAX) + val;
1286 }
1287 }
1288
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001289 mutex_unlock(&ocelot->stats_lock);
1290}
1291
1292static void ocelot_check_stats_work(struct work_struct *work)
1293{
1294 struct delayed_work *del_work = to_delayed_work(work);
1295 struct ocelot *ocelot = container_of(del_work, struct ocelot,
1296 stats_work);
1297
1298 ocelot_update_stats(ocelot);
1299
Alexandre Bellonia556c762018-05-14 22:04:57 +02001300 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1301 OCELOT_STATS_CHECK_DELAY);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001302}
1303
Vladimir Oltean5e256362019-11-14 17:03:27 +02001304void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001305{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001306 int i;
1307
1308 /* check and update now */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001309 ocelot_update_stats(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001310
1311 /* Copy all counters */
1312 for (i = 0; i < ocelot->num_stats; i++)
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001313 *data++ = ocelot->stats[port * ocelot->num_stats + i];
Alexandre Bellonia556c762018-05-14 22:04:57 +02001314}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001315EXPORT_SYMBOL(ocelot_get_ethtool_stats);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001316
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001317static void ocelot_port_get_ethtool_stats(struct net_device *dev,
1318 struct ethtool_stats *stats,
1319 u64 *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001320{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001321 struct ocelot_port_private *priv = netdev_priv(dev);
1322 struct ocelot *ocelot = priv->port.ocelot;
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001323 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001324
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001325 ocelot_get_ethtool_stats(ocelot, port, data);
1326}
1327
Vladimir Oltean5e256362019-11-14 17:03:27 +02001328int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001329{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001330 if (sset != ETH_SS_STATS)
1331 return -EOPNOTSUPP;
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001332
Alexandre Bellonia556c762018-05-14 22:04:57 +02001333 return ocelot->num_stats;
1334}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001335EXPORT_SYMBOL(ocelot_get_sset_count);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001336
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001337static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001338{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001339 struct ocelot_port_private *priv = netdev_priv(dev);
1340 struct ocelot *ocelot = priv->port.ocelot;
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001341 int port = priv->chip_port;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001342
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001343 return ocelot_get_sset_count(ocelot, port, sset);
1344}
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001345
Vladimir Oltean5e256362019-11-14 17:03:27 +02001346int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1347 struct ethtool_ts_info *info)
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001348{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001349 info->phc_index = ocelot->ptp_clock ?
1350 ptp_clock_index(ocelot->ptp_clock) : -1;
1351 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1352 SOF_TIMESTAMPING_RX_SOFTWARE |
1353 SOF_TIMESTAMPING_SOFTWARE |
1354 SOF_TIMESTAMPING_TX_HARDWARE |
1355 SOF_TIMESTAMPING_RX_HARDWARE |
1356 SOF_TIMESTAMPING_RAW_HARDWARE;
1357 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1358 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
1359 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
1360
1361 return 0;
1362}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001363EXPORT_SYMBOL(ocelot_get_ts_info);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001364
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001365static int ocelot_port_get_ts_info(struct net_device *dev,
1366 struct ethtool_ts_info *info)
1367{
1368 struct ocelot_port_private *priv = netdev_priv(dev);
1369 struct ocelot *ocelot = priv->port.ocelot;
1370 int port = priv->chip_port;
1371
1372 if (!ocelot->ptp)
1373 return ethtool_op_get_ts_info(dev, info);
1374
1375 return ocelot_get_ts_info(ocelot, port, info);
1376}
1377
Alexandre Bellonia556c762018-05-14 22:04:57 +02001378static const struct ethtool_ops ocelot_ethtool_ops = {
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001379 .get_strings = ocelot_port_get_strings,
1380 .get_ethtool_stats = ocelot_port_get_ethtool_stats,
1381 .get_sset_count = ocelot_port_get_sset_count,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001382 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1383 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Vladimir Olteanc7282d32019-11-09 15:02:54 +02001384 .get_ts_info = ocelot_port_get_ts_info,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001385};
1386
Vladimir Oltean5e256362019-11-14 17:03:27 +02001387void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001388{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001389 u32 port_cfg;
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001390 int p, i;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001391
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001392 if (!(BIT(port) & ocelot->bridge_mask))
1393 return;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001394
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001395 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001396
1397 switch (state) {
1398 case BR_STATE_FORWARDING:
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001399 ocelot->bridge_fwd_mask |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001400 /* Fallthrough */
1401 case BR_STATE_LEARNING:
1402 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1403 break;
1404
1405 default:
1406 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001407 ocelot->bridge_fwd_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001408 break;
1409 }
1410
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001411 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001412
1413 /* Apply FWD mask. The loop is needed to add/remove the current port as
1414 * a source for the other ports.
1415 */
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001416 for (p = 0; p < ocelot->num_phys_ports; p++) {
Vladimir Oltean69df5782020-02-29 16:50:02 +02001417 if (ocelot->bridge_fwd_mask & BIT(p)) {
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001418 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001419
1420 for (i = 0; i < ocelot->num_phys_ports; i++) {
1421 unsigned long bond_mask = ocelot->lags[i];
1422
1423 if (!bond_mask)
1424 continue;
1425
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001426 if (bond_mask & BIT(p)) {
Alexandre Bellonia556c762018-05-14 22:04:57 +02001427 mask &= ~bond_mask;
1428 break;
1429 }
1430 }
1431
Vladimir Olteanc9d22032019-11-09 15:03:01 +02001432 ocelot_write_rix(ocelot, mask,
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001433 ANA_PGID_PGID, PGID_SRC + p);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001434 } else {
Vladimir Oltean69df5782020-02-29 16:50:02 +02001435 ocelot_write_rix(ocelot, 0,
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001436 ANA_PGID_PGID, PGID_SRC + p);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001437 }
1438 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001439}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001440EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001441
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001442static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
1443 struct switchdev_trans *trans,
1444 u8 state)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001445{
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001446 if (switchdev_trans_ph_prepare(trans))
1447 return;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001448
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001449 ocelot_bridge_stp_state_set(ocelot, port, state);
1450}
1451
Vladimir Oltean5e256362019-11-14 17:03:27 +02001452void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001453{
1454 ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(msecs / 2),
Alexandre Bellonia556c762018-05-14 22:04:57 +02001455 ANA_AUTOAGE);
1456}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001457EXPORT_SYMBOL(ocelot_set_ageing_time);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001458
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001459static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
1460 unsigned long ageing_clock_t)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001461{
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001462 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1463 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1464
1465 ocelot_set_ageing_time(ocelot, ageing_time);
1466}
1467
1468static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
1469{
1470 u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1471 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1472 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1473 u32 val = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001474
1475 if (mc)
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001476 val = cpu_fwd_mcast;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001477
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001478 ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
1479 ANA_PORT_CPU_FWD_CFG, port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001480}
1481
1482static int ocelot_port_attr_set(struct net_device *dev,
1483 const struct switchdev_attr *attr,
1484 struct switchdev_trans *trans)
1485{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001486 struct ocelot_port_private *priv = netdev_priv(dev);
1487 struct ocelot *ocelot = priv->port.ocelot;
1488 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001489 int err = 0;
1490
1491 switch (attr->id) {
1492 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001493 ocelot_port_attr_stp_state_set(ocelot, port, trans,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001494 attr->u.stp_state);
1495 break;
1496 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001497 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001498 break;
Antoine Tenart71425292018-06-26 14:28:49 +02001499 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vladimir Oltean87b0f982020-04-14 22:36:15 +03001500 ocelot_port_vlan_filtering(ocelot, port,
1501 attr->u.vlan_filtering);
Antoine Tenart71425292018-06-26 14:28:49 +02001502 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001503 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
Vladimir Oltean4bda1412019-11-09 15:02:51 +02001504 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001505 break;
1506 default:
1507 err = -EOPNOTSUPP;
1508 break;
1509 }
1510
1511 return err;
1512}
1513
Antoine Tenart71425292018-06-26 14:28:49 +02001514static int ocelot_port_obj_add_vlan(struct net_device *dev,
1515 const struct switchdev_obj_port_vlan *vlan,
1516 struct switchdev_trans *trans)
1517{
1518 int ret;
1519 u16 vid;
1520
1521 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1522 ret = ocelot_vlan_vid_add(dev, vid,
1523 vlan->flags & BRIDGE_VLAN_INFO_PVID,
1524 vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
1525 if (ret)
1526 return ret;
1527 }
1528
1529 return 0;
1530}
1531
1532static int ocelot_port_vlan_del_vlan(struct net_device *dev,
1533 const struct switchdev_obj_port_vlan *vlan)
1534{
1535 int ret;
1536 u16 vid;
1537
1538 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1539 ret = ocelot_vlan_vid_del(dev, vid);
1540
1541 if (ret)
1542 return ret;
1543 }
1544
1545 return 0;
1546}
1547
Alexandre Bellonia556c762018-05-14 22:04:57 +02001548static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1549 const unsigned char *addr,
1550 u16 vid)
1551{
1552 struct ocelot_multicast *mc;
1553
1554 list_for_each_entry(mc, &ocelot->multicast, list) {
1555 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1556 return mc;
1557 }
1558
1559 return NULL;
1560}
1561
1562static int ocelot_port_obj_add_mdb(struct net_device *dev,
1563 const struct switchdev_obj_port_mdb *mdb,
1564 struct switchdev_trans *trans)
1565{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001566 struct ocelot_port_private *priv = netdev_priv(dev);
1567 struct ocelot_port *ocelot_port = &priv->port;
1568 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001569 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001570 struct ocelot_multicast *mc;
1571 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001572 u16 vid = mdb->vid;
1573 bool new = false;
1574
1575 if (!vid)
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001576 vid = ocelot_port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001577
1578 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1579 if (!mc) {
1580 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1581 if (!mc)
1582 return -ENOMEM;
1583
1584 memcpy(mc->addr, mdb->addr, ETH_ALEN);
1585 mc->vid = vid;
1586
1587 list_add_tail(&mc->list, &ocelot->multicast);
1588 new = true;
1589 }
1590
1591 memcpy(addr, mc->addr, ETH_ALEN);
1592 addr[0] = 0;
1593
1594 if (!new) {
1595 addr[2] = mc->ports << 0;
1596 addr[1] = mc->ports << 8;
1597 ocelot_mact_forget(ocelot, addr, vid);
1598 }
1599
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001600 mc->ports |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001601 addr[2] = mc->ports << 0;
1602 addr[1] = mc->ports << 8;
1603
1604 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1605}
1606
1607static int ocelot_port_obj_del_mdb(struct net_device *dev,
1608 const struct switchdev_obj_port_mdb *mdb)
1609{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001610 struct ocelot_port_private *priv = netdev_priv(dev);
1611 struct ocelot_port *ocelot_port = &priv->port;
1612 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001613 unsigned char addr[ETH_ALEN];
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001614 struct ocelot_multicast *mc;
1615 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001616 u16 vid = mdb->vid;
1617
1618 if (!vid)
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001619 vid = ocelot_port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001620
1621 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1622 if (!mc)
1623 return -ENOENT;
1624
1625 memcpy(addr, mc->addr, ETH_ALEN);
1626 addr[2] = mc->ports << 0;
1627 addr[1] = mc->ports << 8;
1628 addr[0] = 0;
1629 ocelot_mact_forget(ocelot, addr, vid);
1630
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001631 mc->ports &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001632 if (!mc->ports) {
1633 list_del(&mc->list);
1634 devm_kfree(ocelot->dev, mc);
1635 return 0;
1636 }
1637
1638 addr[2] = mc->ports << 0;
1639 addr[1] = mc->ports << 8;
1640
1641 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1642}
1643
1644static int ocelot_port_obj_add(struct net_device *dev,
1645 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +00001646 struct switchdev_trans *trans,
1647 struct netlink_ext_ack *extack)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001648{
1649 int ret = 0;
1650
1651 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001652 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1653 ret = ocelot_port_obj_add_vlan(dev,
1654 SWITCHDEV_OBJ_PORT_VLAN(obj),
1655 trans);
1656 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001657 case SWITCHDEV_OBJ_ID_PORT_MDB:
1658 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1659 trans);
1660 break;
1661 default:
1662 return -EOPNOTSUPP;
1663 }
1664
1665 return ret;
1666}
1667
1668static int ocelot_port_obj_del(struct net_device *dev,
1669 const struct switchdev_obj *obj)
1670{
1671 int ret = 0;
1672
1673 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001674 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1675 ret = ocelot_port_vlan_del_vlan(dev,
1676 SWITCHDEV_OBJ_PORT_VLAN(obj));
1677 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001678 case SWITCHDEV_OBJ_ID_PORT_MDB:
1679 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1680 break;
1681 default:
1682 return -EOPNOTSUPP;
1683 }
1684
1685 return ret;
1686}
1687
Vladimir Oltean5e256362019-11-14 17:03:27 +02001688int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1689 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001690{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001691 if (!ocelot->bridge_mask) {
1692 ocelot->hw_bridge_dev = bridge;
1693 } else {
1694 if (ocelot->hw_bridge_dev != bridge)
1695 /* This is adding the port to a second bridge, this is
1696 * unsupported */
1697 return -ENODEV;
1698 }
1699
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001700 ocelot->bridge_mask |= BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001701
1702 return 0;
1703}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001704EXPORT_SYMBOL(ocelot_port_bridge_join);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001705
Vladimir Oltean5e256362019-11-14 17:03:27 +02001706int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1707 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001708{
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001709 ocelot->bridge_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001710
1711 if (!ocelot->bridge_mask)
1712 ocelot->hw_bridge_dev = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02001713
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001714 ocelot_port_vlan_filtering(ocelot, port, 0);
1715 ocelot_port_set_pvid(ocelot, port, 0);
1716 return ocelot_port_set_native_vlan(ocelot, port, 0);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001717}
Vladimir Oltean5e256362019-11-14 17:03:27 +02001718EXPORT_SYMBOL(ocelot_port_bridge_leave);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001719
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001720static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1721{
1722 int i, port, lag;
1723
1724 /* Reset destination and aggregation PGIDS */
1725 for (port = 0; port < ocelot->num_phys_ports; port++)
1726 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1727
1728 for (i = PGID_AGGR; i < PGID_SRC; i++)
1729 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1730 ANA_PGID_PGID, i);
1731
1732 /* Now, set PGIDs for each LAG */
1733 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1734 unsigned long bond_mask;
1735 int aggr_count = 0;
1736 u8 aggr_idx[16];
1737
1738 bond_mask = ocelot->lags[lag];
1739 if (!bond_mask)
1740 continue;
1741
1742 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1743 // Destination mask
1744 ocelot_write_rix(ocelot, bond_mask,
1745 ANA_PGID_PGID, port);
1746 aggr_idx[aggr_count] = port;
1747 aggr_count++;
1748 }
1749
1750 for (i = PGID_AGGR; i < PGID_SRC; i++) {
1751 u32 ac;
1752
1753 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1754 ac &= ~bond_mask;
1755 ac |= BIT(aggr_idx[i % aggr_count]);
1756 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1757 }
1758 }
1759}
1760
1761static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1762{
1763 unsigned long bond_mask = ocelot->lags[lag];
1764 unsigned int p;
1765
1766 for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1767 u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1768
1769 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1770
1771 /* Use lag port as logical port for port i */
1772 ocelot_write_gix(ocelot, port_cfg |
1773 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1774 ANA_PORT_PORT_CFG, p);
1775 }
1776}
1777
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001778static int ocelot_port_lag_join(struct ocelot *ocelot, int port,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001779 struct net_device *bond)
1780{
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001781 struct net_device *ndev;
1782 u32 bond_mask = 0;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001783 int lag, lp;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001784
1785 rcu_read_lock();
1786 for_each_netdev_in_bond_rcu(bond, ndev) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001787 struct ocelot_port_private *priv = netdev_priv(ndev);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001788
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001789 bond_mask |= BIT(priv->chip_port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001790 }
1791 rcu_read_unlock();
1792
1793 lp = __ffs(bond_mask);
1794
1795 /* If the new port is the lowest one, use it as the logical port from
1796 * now on
1797 */
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001798 if (port == lp) {
1799 lag = port;
1800 ocelot->lags[port] = bond_mask;
1801 bond_mask &= ~BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001802 if (bond_mask) {
1803 lp = __ffs(bond_mask);
1804 ocelot->lags[lp] = 0;
1805 }
1806 } else {
1807 lag = lp;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001808 ocelot->lags[lp] |= BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001809 }
1810
1811 ocelot_setup_lag(ocelot, lag);
1812 ocelot_set_aggr_pgids(ocelot);
1813
1814 return 0;
1815}
1816
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001817static void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001818 struct net_device *bond)
1819{
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001820 u32 port_cfg;
1821 int i;
1822
1823 /* Remove port from any lag */
1824 for (i = 0; i < ocelot->num_phys_ports; i++)
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001825 ocelot->lags[i] &= ~BIT(port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001826
1827 /* if it was the logical port of the lag, move the lag config to the
1828 * next port
1829 */
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001830 if (ocelot->lags[port]) {
1831 int n = __ffs(ocelot->lags[port]);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001832
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001833 ocelot->lags[n] = ocelot->lags[port];
1834 ocelot->lags[port] = 0;
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001835
1836 ocelot_setup_lag(ocelot, n);
1837 }
1838
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001839 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001840 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001841 ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1842 ANA_PORT_PORT_CFG, port);
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001843
1844 ocelot_set_aggr_pgids(ocelot);
1845}
1846
Alexandre Bellonia556c762018-05-14 22:04:57 +02001847/* Checks if the net_device instance given to us originate from our driver. */
1848static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1849{
1850 return dev->netdev_ops == &ocelot_port_netdev_ops;
1851}
1852
1853static int ocelot_netdevice_port_event(struct net_device *dev,
1854 unsigned long event,
1855 struct netdev_notifier_changeupper_info *info)
1856{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001857 struct ocelot_port_private *priv = netdev_priv(dev);
1858 struct ocelot_port *ocelot_port = &priv->port;
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001859 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001860 int port = priv->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001861 int err = 0;
1862
Alexandre Bellonia556c762018-05-14 22:04:57 +02001863 switch (event) {
1864 case NETDEV_CHANGEUPPER:
1865 if (netif_is_bridge_master(info->upper_dev)) {
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001866 if (info->linking) {
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001867 err = ocelot_port_bridge_join(ocelot, port,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001868 info->upper_dev);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001869 } else {
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001870 err = ocelot_port_bridge_leave(ocelot, port,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001871 info->upper_dev);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02001872 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001873 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001874 if (netif_is_lag_master(info->upper_dev)) {
1875 if (info->linking)
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001876 err = ocelot_port_lag_join(ocelot, port,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001877 info->upper_dev);
1878 else
Vladimir Olteanf270dbf2019-11-09 15:02:52 +02001879 ocelot_port_lag_leave(ocelot, port,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001880 info->upper_dev);
1881 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001882 break;
1883 default:
1884 break;
1885 }
1886
1887 return err;
1888}
1889
1890static int ocelot_netdevice_event(struct notifier_block *unused,
1891 unsigned long event, void *ptr)
1892{
1893 struct netdev_notifier_changeupper_info *info = ptr;
1894 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Geert Uytterhoeven2ac0e152018-06-07 15:10:30 +02001895 int ret = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001896
Claudiu Manoil7afb3e52019-11-05 23:50:13 +02001897 if (!ocelot_netdevice_dev_check(dev))
1898 return 0;
1899
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001900 if (event == NETDEV_PRECHANGEUPPER &&
1901 netif_is_lag_master(info->upper_dev)) {
1902 struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1903 struct netlink_ext_ack *extack;
1904
Claudiu Manoil3b3eed82019-11-05 23:50:14 +02001905 if (lag_upper_info &&
1906 lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001907 extack = netdev_notifier_info_to_extack(&info->info);
1908 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1909
1910 ret = -EINVAL;
1911 goto notify;
1912 }
1913 }
1914
Alexandre Bellonia556c762018-05-14 22:04:57 +02001915 if (netif_is_lag_master(dev)) {
1916 struct net_device *slave;
1917 struct list_head *iter;
1918
1919 netdev_for_each_lower_dev(dev, slave, iter) {
1920 ret = ocelot_netdevice_port_event(slave, event, info);
1921 if (ret)
1922 goto notify;
1923 }
1924 } else {
1925 ret = ocelot_netdevice_port_event(dev, event, info);
1926 }
1927
1928notify:
1929 return notifier_from_errno(ret);
1930}
1931
1932struct notifier_block ocelot_netdevice_nb __read_mostly = {
1933 .notifier_call = ocelot_netdevice_event,
1934};
1935EXPORT_SYMBOL(ocelot_netdevice_nb);
1936
Florian Fainelli56da64b2019-02-27 11:44:29 -08001937static int ocelot_switchdev_event(struct notifier_block *unused,
1938 unsigned long event, void *ptr)
1939{
1940 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1941 int err;
1942
1943 switch (event) {
1944 case SWITCHDEV_PORT_ATTR_SET:
1945 err = switchdev_handle_port_attr_set(dev, ptr,
1946 ocelot_netdevice_dev_check,
1947 ocelot_port_attr_set);
1948 return notifier_from_errno(err);
1949 }
1950
1951 return NOTIFY_DONE;
1952}
1953
1954struct notifier_block ocelot_switchdev_nb __read_mostly = {
1955 .notifier_call = ocelot_switchdev_event,
1956};
1957EXPORT_SYMBOL(ocelot_switchdev_nb);
1958
Petr Machata0e332c82018-11-22 23:30:11 +00001959static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1960 unsigned long event, void *ptr)
1961{
1962 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1963 int err;
1964
1965 switch (event) {
1966 /* Blocking events. */
1967 case SWITCHDEV_PORT_OBJ_ADD:
1968 err = switchdev_handle_port_obj_add(dev, ptr,
1969 ocelot_netdevice_dev_check,
1970 ocelot_port_obj_add);
1971 return notifier_from_errno(err);
1972 case SWITCHDEV_PORT_OBJ_DEL:
1973 err = switchdev_handle_port_obj_del(dev, ptr,
1974 ocelot_netdevice_dev_check,
1975 ocelot_port_obj_del);
1976 return notifier_from_errno(err);
Florian Fainelli56da64b2019-02-27 11:44:29 -08001977 case SWITCHDEV_PORT_ATTR_SET:
1978 err = switchdev_handle_port_attr_set(dev, ptr,
1979 ocelot_netdevice_dev_check,
1980 ocelot_port_attr_set);
1981 return notifier_from_errno(err);
Petr Machata0e332c82018-11-22 23:30:11 +00001982 }
1983
1984 return NOTIFY_DONE;
1985}
1986
1987struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1988 .notifier_call = ocelot_switchdev_blocking_event,
1989};
1990EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
1991
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001992int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
1993{
1994 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1995 unsigned long flags;
1996 time64_t s;
1997 u32 val;
1998 s64 ns;
1999
2000 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
2001
2002 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
2003 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
2004 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
2005 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
2006
2007 s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
2008 s <<= 32;
2009 s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
2010 ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
2011
2012 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2013
2014 /* Deal with negative values */
2015 if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
2016 s--;
2017 ns &= 0xf;
2018 ns += 999999984;
2019 }
2020
2021 set_normalized_timespec64(ts, s, ns);
2022 return 0;
2023}
2024EXPORT_SYMBOL(ocelot_ptp_gettime64);
2025
2026static int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
2027 const struct timespec64 *ts)
2028{
2029 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
2030 unsigned long flags;
2031 u32 val;
2032
2033 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
2034
2035 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
2036 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
2037 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
2038
2039 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
2040
2041 ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
2042 TOD_ACC_PIN);
2043 ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
2044 TOD_ACC_PIN);
2045 ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
2046
2047 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
2048 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
2049 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
2050
2051 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
2052
2053 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2054 return 0;
2055}
2056
2057static int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
2058{
2059 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
2060 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
2061 unsigned long flags;
2062 u32 val;
2063
2064 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
2065
2066 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
2067 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
2068 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
2069
2070 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
2071
2072 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
2073 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
2074 ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
2075
2076 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
2077 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
2078 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
2079
2080 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
2081
2082 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2083 } else {
2084 /* Fall back using ocelot_ptp_settime64 which is not exact. */
2085 struct timespec64 ts;
2086 u64 now;
2087
2088 ocelot_ptp_gettime64(ptp, &ts);
2089
2090 now = ktime_to_ns(timespec64_to_ktime(ts));
2091 ts = ns_to_timespec64(now + delta);
2092
2093 ocelot_ptp_settime64(ptp, &ts);
2094 }
2095 return 0;
2096}
2097
2098static int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
2099{
2100 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
2101 u32 unit = 0, direction = 0;
2102 unsigned long flags;
2103 u64 adj = 0;
2104
2105 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
2106
2107 if (!scaled_ppm)
2108 goto disable_adj;
2109
2110 if (scaled_ppm < 0) {
2111 direction = PTP_CFG_CLK_ADJ_CFG_DIR;
2112 scaled_ppm = -scaled_ppm;
2113 }
2114
2115 adj = PSEC_PER_SEC << 16;
2116 do_div(adj, scaled_ppm);
2117 do_div(adj, 1000);
2118
2119 /* If the adjustment value is too large, use ns instead */
2120 if (adj >= (1L << 30)) {
2121 unit = PTP_CFG_CLK_ADJ_FREQ_NS;
2122 do_div(adj, 1000);
2123 }
2124
2125 /* Still too big */
2126 if (adj >= (1L << 30))
2127 goto disable_adj;
2128
2129 ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
2130 ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
2131 PTP_CLK_CFG_ADJ_CFG);
2132
2133 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2134 return 0;
2135
2136disable_adj:
2137 ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
2138
2139 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2140 return 0;
2141}
2142
2143static struct ptp_clock_info ocelot_ptp_clock_info = {
2144 .owner = THIS_MODULE,
2145 .name = "ocelot ptp",
2146 .max_adj = 0x7fffffff,
2147 .n_alarm = 0,
2148 .n_ext_ts = 0,
2149 .n_per_out = 0,
2150 .n_pins = 0,
2151 .pps = 0,
2152 .gettime64 = ocelot_ptp_gettime64,
2153 .settime64 = ocelot_ptp_settime64,
2154 .adjtime = ocelot_ptp_adjtime,
2155 .adjfine = ocelot_ptp_adjfine,
2156};
2157
2158static int ocelot_init_timestamp(struct ocelot *ocelot)
2159{
Vladimir Oltean93859732019-12-03 17:45:35 +02002160 struct ptp_clock *ptp_clock;
2161
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002162 ocelot->ptp_info = ocelot_ptp_clock_info;
Vladimir Oltean93859732019-12-03 17:45:35 +02002163 ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
2164 if (IS_ERR(ptp_clock))
2165 return PTR_ERR(ptp_clock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002166 /* Check if PHC support is missing at the configuration level */
Vladimir Oltean93859732019-12-03 17:45:35 +02002167 if (!ptp_clock)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002168 return 0;
2169
Vladimir Oltean93859732019-12-03 17:45:35 +02002170 ocelot->ptp_clock = ptp_clock;
2171
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002172 ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
2173 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
2174 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
2175
2176 ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
2177
2178 /* There is no device reconfiguration, PTP Rx stamping is always
2179 * enabled.
2180 */
2181 ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
2182
2183 return 0;
2184}
2185
Vladimir Olteana8015de2020-03-10 03:28:18 +02002186/* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2187 * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002188 * In the special case that it's the NPI port that we're configuring, the
2189 * length of the tag and optional prefix needs to be accounted for privately,
2190 * in order to be able to sustain communication at the requested @sdu.
Vladimir Olteana8015de2020-03-10 03:28:18 +02002191 */
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002192void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
Vladimir Oltean31350d72019-11-09 15:02:56 +02002193{
2194 struct ocelot_port *ocelot_port = ocelot->ports[port];
Vladimir Olteana8015de2020-03-10 03:28:18 +02002195 int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002196 int atop_wm;
Vladimir Oltean31350d72019-11-09 15:02:56 +02002197
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002198 if (port == ocelot->npi) {
2199 maxlen += OCELOT_TAG_LEN;
2200
2201 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
2202 maxlen += OCELOT_SHORT_PREFIX_LEN;
2203 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
2204 maxlen += OCELOT_LONG_PREFIX_LEN;
2205 }
2206
Vladimir Olteana8015de2020-03-10 03:28:18 +02002207 ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002208
2209 /* Set Pause WM hysteresis
Vladimir Olteana8015de2020-03-10 03:28:18 +02002210 * 152 = 6 * maxlen / OCELOT_BUFFER_CELL_SZ
2211 * 101 = 4 * maxlen / OCELOT_BUFFER_CELL_SZ
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002212 */
2213 ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
2214 SYS_PAUSE_CFG_PAUSE_STOP(101) |
2215 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, port);
2216
2217 /* Tail dropping watermark */
Vladimir Olteana8015de2020-03-10 03:28:18 +02002218 atop_wm = (ocelot->shared_queue_sz - 9 * maxlen) /
2219 OCELOT_BUFFER_CELL_SZ;
2220 ocelot_write_rix(ocelot, ocelot_wm_enc(9 * maxlen),
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002221 SYS_ATOP, port);
2222 ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
2223}
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002224EXPORT_SYMBOL(ocelot_port_set_maxlen);
2225
2226int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
2227{
2228 int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
2229
2230 if (port == ocelot->npi) {
2231 max_mtu -= OCELOT_TAG_LEN;
2232
2233 if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
2234 max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2235 else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
2236 max_mtu -= OCELOT_LONG_PREFIX_LEN;
2237 }
2238
2239 return max_mtu;
2240}
2241EXPORT_SYMBOL(ocelot_get_max_mtu);
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002242
Vladimir Oltean5e256362019-11-14 17:03:27 +02002243void ocelot_init_port(struct ocelot *ocelot, int port)
Vladimir Olteanfa914e92019-11-14 17:03:23 +02002244{
2245 struct ocelot_port *ocelot_port = ocelot->ports[port];
2246
Yangbo Lub049da12019-11-27 15:27:57 +08002247 skb_queue_head_init(&ocelot_port->tx_skbs);
Vladimir Oltean31350d72019-11-09 15:02:56 +02002248
2249 /* Basic L2 initialization */
2250
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002251 /* Set MAC IFG Gaps
2252 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
2253 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
2254 */
2255 ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
2256 DEV_MAC_IFG_CFG);
2257
2258 /* Load seed (0) and set MAC HDX late collision */
2259 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
2260 DEV_MAC_HDX_CFG_SEED_LOAD,
2261 DEV_MAC_HDX_CFG);
2262 mdelay(1);
2263 ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
2264 DEV_MAC_HDX_CFG);
2265
2266 /* Set Max Length and maximum tags allowed */
Vladimir Olteana8015de2020-03-10 03:28:18 +02002267 ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002268 ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
2269 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
Vladimir Olteana8015de2020-03-10 03:28:18 +02002270 DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
Vladimir Oltean5bc9d2e2019-11-14 17:03:22 +02002271 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
2272 DEV_MAC_TAGS_CFG);
2273
2274 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
2275 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
2276 ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
2277
Vladimir Oltean31350d72019-11-09 15:02:56 +02002278 /* Drop frames with multicast source address */
2279 ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2280 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2281 ANA_PORT_DROP_CFG, port);
2282
2283 /* Set default VLAN and tag type to 8021Q. */
2284 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2285 REW_PORT_VLAN_CFG_PORT_TPID_M,
2286 REW_PORT_VLAN_CFG, port);
2287
2288 /* Enable vcap lookups */
2289 ocelot_vcap_enable(ocelot, port);
2290}
Vladimir Oltean5e256362019-11-14 17:03:27 +02002291EXPORT_SYMBOL(ocelot_init_port);
Vladimir Oltean31350d72019-11-09 15:02:56 +02002292
Alexandre Bellonia556c762018-05-14 22:04:57 +02002293int ocelot_probe_port(struct ocelot *ocelot, u8 port,
2294 void __iomem *regs,
2295 struct phy_device *phy)
2296{
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002297 struct ocelot_port_private *priv;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002298 struct ocelot_port *ocelot_port;
2299 struct net_device *dev;
2300 int err;
2301
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002302 dev = alloc_etherdev(sizeof(struct ocelot_port_private));
Alexandre Bellonia556c762018-05-14 22:04:57 +02002303 if (!dev)
2304 return -ENOMEM;
2305 SET_NETDEV_DEV(dev, ocelot->dev);
Vladimir Oltean004d44f2019-11-09 15:02:53 +02002306 priv = netdev_priv(dev);
2307 priv->dev = dev;
2308 priv->phy = phy;
2309 priv->chip_port = port;
2310 ocelot_port = &priv->port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002311 ocelot_port->ocelot = ocelot;
2312 ocelot_port->regs = regs;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002313 ocelot->ports[port] = ocelot_port;
2314
2315 dev->netdev_ops = &ocelot_port_netdev_ops;
2316 dev->ethtool_ops = &ocelot_ethtool_ops;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002317
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02002318 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
2319 NETIF_F_HW_TC;
2320 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
Antoine Tenart71425292018-06-26 14:28:49 +02002321
Alexandre Bellonia556c762018-05-14 22:04:57 +02002322 memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2323 dev->dev_addr[ETH_ALEN - 1] += port;
2324 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2325 ENTRYTYPE_LOCKED);
2326
Vladimir Oltean31350d72019-11-09 15:02:56 +02002327 ocelot_init_port(ocelot, port);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002328
Alexandre Bellonia556c762018-05-14 22:04:57 +02002329 err = register_netdev(dev);
2330 if (err) {
2331 dev_err(ocelot->dev, "register_netdev failed\n");
Vladimir Oltean31350d72019-11-09 15:02:56 +02002332 free_netdev(dev);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002333 }
2334
Alexandre Bellonia556c762018-05-14 22:04:57 +02002335 return err;
2336}
2337EXPORT_SYMBOL(ocelot_probe_port);
2338
Vladimir Oltean69df5782020-02-29 16:50:02 +02002339/* Configure and enable the CPU port module, which is a set of queues.
2340 * If @npi contains a valid port index, the CPU port module is connected
2341 * to the Node Processor Interface (NPI). This is the mode through which
2342 * frames can be injected from and extracted to an external CPU,
2343 * over Ethernet.
2344 */
2345void ocelot_configure_cpu(struct ocelot *ocelot, int npi,
2346 enum ocelot_tag_prefix injection,
2347 enum ocelot_tag_prefix extraction)
Vladimir Oltean21468192019-11-09 15:03:00 +02002348{
Vladimir Oltean69df5782020-02-29 16:50:02 +02002349 int cpu = ocelot->num_phys_ports;
2350
Vladimir Oltean0b912fc2020-03-27 21:55:47 +02002351 ocelot->npi = npi;
2352 ocelot->inj_prefix = injection;
2353 ocelot->xtr_prefix = extraction;
2354
Vladimir Oltean69df5782020-02-29 16:50:02 +02002355 /* The unicast destination PGID for the CPU port module is unused */
Vladimir Oltean21468192019-11-09 15:03:00 +02002356 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
Vladimir Oltean69df5782020-02-29 16:50:02 +02002357 /* Instead set up a multicast destination PGID for traffic copied to
2358 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
2359 * addresses will be copied to the CPU via this PGID.
2360 */
Vladimir Oltean21468192019-11-09 15:03:00 +02002361 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2362 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2363 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2364 ANA_PORT_PORT_CFG, cpu);
2365
Vladimir Oltean69df5782020-02-29 16:50:02 +02002366 if (npi >= 0 && npi < ocelot->num_phys_ports) {
Vladimir Oltean21468192019-11-09 15:03:00 +02002367 ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
Vladimir Oltean69df5782020-02-29 16:50:02 +02002368 QSYS_EXT_CPU_CFG_EXT_CPU_PORT(npi),
Vladimir Oltean21468192019-11-09 15:03:00 +02002369 QSYS_EXT_CPU_CFG);
Vladimir Olteanba551bc2019-11-14 17:03:25 +02002370
Vladimir Oltean69df5782020-02-29 16:50:02 +02002371 /* Enable NPI port */
2372 ocelot_write_rix(ocelot,
2373 QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2374 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2375 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2376 QSYS_SWITCH_PORT_MODE, npi);
2377 /* NPI port Injection/Extraction configuration */
2378 ocelot_write_rix(ocelot,
2379 SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
2380 SYS_PORT_MODE_INCL_INJ_HDR(injection),
2381 SYS_PORT_MODE, npi);
Vladimir Oltean21468192019-11-09 15:03:00 +02002382 }
2383
Vladimir Oltean69df5782020-02-29 16:50:02 +02002384 /* Enable CPU port module */
Vladimir Oltean21468192019-11-09 15:03:00 +02002385 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2386 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2387 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2388 QSYS_SWITCH_PORT_MODE, cpu);
Vladimir Oltean69df5782020-02-29 16:50:02 +02002389 /* CPU port Injection/Extraction configuration */
Vladimir Oltean21468192019-11-09 15:03:00 +02002390 ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
2391 SYS_PORT_MODE_INCL_INJ_HDR(injection),
2392 SYS_PORT_MODE, cpu);
2393
2394 /* Configure the CPU port to be VLAN aware */
2395 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
2396 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2397 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
2398 ANA_PORT_VLAN_CFG, cpu);
Vladimir Oltean21468192019-11-09 15:03:00 +02002399}
Vladimir Oltean69df5782020-02-29 16:50:02 +02002400EXPORT_SYMBOL(ocelot_configure_cpu);
Vladimir Oltean21468192019-11-09 15:03:00 +02002401
Alexandre Bellonia556c762018-05-14 22:04:57 +02002402int ocelot_init(struct ocelot *ocelot)
2403{
Alexandre Bellonia556c762018-05-14 22:04:57 +02002404 char queue_name[32];
Vladimir Oltean21468192019-11-09 15:03:00 +02002405 int i, ret;
2406 u32 port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002407
Vladimir Oltean3a77b592019-11-14 17:03:26 +02002408 if (ocelot->ops->reset) {
2409 ret = ocelot->ops->reset(ocelot);
2410 if (ret) {
2411 dev_err(ocelot->dev, "Switch reset failed\n");
2412 return ret;
2413 }
2414 }
2415
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002416 ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2417 sizeof(u32), GFP_KERNEL);
2418 if (!ocelot->lags)
2419 return -ENOMEM;
2420
Alexandre Bellonia556c762018-05-14 22:04:57 +02002421 ocelot->stats = devm_kcalloc(ocelot->dev,
2422 ocelot->num_phys_ports * ocelot->num_stats,
2423 sizeof(u64), GFP_KERNEL);
2424 if (!ocelot->stats)
2425 return -ENOMEM;
2426
2427 mutex_init(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002428 mutex_init(&ocelot->ptp_lock);
2429 spin_lock_init(&ocelot->ptp_clock_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002430 snprintf(queue_name, sizeof(queue_name), "%s-stats",
2431 dev_name(ocelot->dev));
2432 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2433 if (!ocelot->stats_queue)
2434 return -ENOMEM;
2435
Claudiu Manoil2b120dd2019-11-09 15:02:58 +02002436 INIT_LIST_HEAD(&ocelot->multicast);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002437 ocelot_mact_init(ocelot);
2438 ocelot_vlan_init(ocelot);
Horatiu Vulturb5962292019-05-31 09:16:56 +02002439 ocelot_ace_init(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002440
2441 for (port = 0; port < ocelot->num_phys_ports; port++) {
2442 /* Clear all counters (5 groups) */
2443 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2444 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2445 SYS_STAT_CFG);
2446 }
2447
2448 /* Only use S-Tag */
2449 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2450
2451 /* Aggregation mode */
2452 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2453 ANA_AGGR_CFG_AC_DMAC_ENA |
2454 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2455 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2456
2457 /* Set MAC age time to default value. The entry is aged after
2458 * 2*AGE_PERIOD
2459 */
2460 ocelot_write(ocelot,
2461 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2462 ANA_AUTOAGE);
2463
2464 /* Disable learning for frames discarded by VLAN ingress filtering */
2465 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2466
2467 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2468 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2469 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2470
2471 /* Setup flooding PGIDs */
2472 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2473 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2474 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2475 ANA_FLOODING, 0);
2476 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2477 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2478 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2479 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2480 ANA_FLOODING_IPMC);
2481
2482 for (port = 0; port < ocelot->num_phys_ports; port++) {
2483 /* Transmit the frame to the local port. */
2484 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2485 /* Do not forward BPDU frames to the front ports. */
2486 ocelot_write_gix(ocelot,
2487 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2488 ANA_PORT_CPU_FWD_BPDU_CFG,
2489 port);
2490 /* Ensure bridging is disabled */
2491 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2492 }
2493
Alexandre Bellonia556c762018-05-14 22:04:57 +02002494 /* Allow broadcast MAC frames. */
2495 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2496 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2497
2498 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2499 }
2500 ocelot_write_rix(ocelot,
2501 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2502 ANA_PGID_PGID, PGID_MC);
2503 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2504 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2505
Alexandre Bellonia556c762018-05-14 22:04:57 +02002506 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2507 * registers endianness.
2508 */
2509 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2510 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2511 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2512 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2513 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2514 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2515 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2516 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2517 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2518 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2519 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2520 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2521 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2522 for (i = 0; i < 16; i++)
2523 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2524 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2525 ANA_CPUQ_8021_CFG, i);
2526
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03002527 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002528 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2529 OCELOT_STATS_CHECK_DELAY);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002530
2531 if (ocelot->ptp) {
2532 ret = ocelot_init_timestamp(ocelot);
2533 if (ret) {
2534 dev_err(ocelot->dev,
2535 "Timestamp initialization failed\n");
2536 return ret;
2537 }
2538 }
2539
Alexandre Bellonia556c762018-05-14 22:04:57 +02002540 return 0;
2541}
2542EXPORT_SYMBOL(ocelot_init);
2543
2544void ocelot_deinit(struct ocelot *ocelot)
2545{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002546 struct ocelot_port *port;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002547 int i;
2548
Claudiu Manoilc5d13962019-07-25 16:33:18 +03002549 cancel_delayed_work(&ocelot->stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002550 destroy_workqueue(ocelot->stats_queue);
2551 mutex_destroy(&ocelot->stats_lock);
Vladimir Oltean93859732019-12-03 17:45:35 +02002552 if (ocelot->ptp_clock)
2553 ptp_clock_unregister(ocelot->ptp_clock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002554
2555 for (i = 0; i < ocelot->num_phys_ports; i++) {
2556 port = ocelot->ports[i];
Yangbo Lub049da12019-11-27 15:27:57 +08002557 skb_queue_purge(&port->tx_skbs);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002558 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02002559}
2560EXPORT_SYMBOL(ocelot_deinit);
2561
2562MODULE_LICENSE("Dual MIT/GPL");