blob: 5b9cde6d3e3834ed33915340fbf33b47e118cb08 [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
Horatiu Vulturb5962292019-05-31 09:16:56 +0200135static void ocelot_vcap_enable(struct ocelot *ocelot, struct ocelot_port *port)
136{
137 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
138 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
139 ANA_PORT_VCAP_S2_CFG, port->chip_port);
140}
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
172static void ocelot_vlan_mode(struct ocelot_port *port,
173 netdev_features_t features)
174{
175 struct ocelot *ocelot = port->ocelot;
176 u8 p = port->chip_port;
177 u32 val;
178
179 /* Filtering */
180 val = ocelot_read(ocelot, ANA_VLANMASK);
181 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
182 val |= BIT(p);
183 else
184 val &= ~BIT(p);
185 ocelot_write(ocelot, val, ANA_VLANMASK);
186}
187
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200188static void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
189 bool vlan_aware)
Antoine Tenart71425292018-06-26 14:28:49 +0200190{
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200191 struct ocelot_port *ocelot_port = ocelot->ports[port];
Antoine Tenart71425292018-06-26 14:28:49 +0200192 u32 val;
193
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200194 if (vlan_aware)
195 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
196 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
197 else
198 val = 0;
Antoine Tenart71425292018-06-26 14:28:49 +0200199 ocelot_rmw_gix(ocelot, val,
Antoine Tenart71425292018-06-26 14:28:49 +0200200 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
201 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200202 ANA_PORT_VLAN_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200203
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200204 if (vlan_aware && !ocelot_port->vid)
Antoine Tenart71425292018-06-26 14:28:49 +0200205 /* If port is vlan-aware and tagged, drop untagged and priority
206 * tagged frames.
207 */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200208 val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
209 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
210 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
211 else
212 val = 0;
213 ocelot_rmw_gix(ocelot, val,
214 ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
Antoine Tenart71425292018-06-26 14:28:49 +0200215 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200216 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
217 ANA_PORT_DROP_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200218
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200219 if (vlan_aware) {
220 if (ocelot_port->vid)
Antoine Tenart71425292018-06-26 14:28:49 +0200221 /* Tag all frames except when VID == DEFAULT_VLAN */
222 val |= REW_TAG_CFG_TAG_CFG(1);
223 else
224 /* Tag all frames */
225 val |= REW_TAG_CFG_TAG_CFG(3);
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200226 } else {
227 /* Port tagging disabled. */
228 val = REW_TAG_CFG_TAG_CFG(0);
Antoine Tenart71425292018-06-26 14:28:49 +0200229 }
230 ocelot_rmw_gix(ocelot, val,
Antoine Tenart71425292018-06-26 14:28:49 +0200231 REW_TAG_CFG_TAG_CFG_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200232 REW_TAG_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200233
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200234 ocelot_port->vlan_aware = vlan_aware;
235}
236
237static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
238 u16 vid)
239{
240 struct ocelot_port *ocelot_port = ocelot->ports[port];
241
242 if (ocelot_port->vid != vid) {
243 /* Always permit deleting the native VLAN (vid = 0) */
244 if (ocelot_port->vid && vid) {
245 dev_err(ocelot->dev,
246 "Port already has a native VLAN: %d\n",
247 ocelot_port->vid);
248 return -EBUSY;
249 }
250 ocelot_port->vid = vid;
251 }
252
253 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
Antoine Tenart71425292018-06-26 14:28:49 +0200254 REW_PORT_VLAN_CFG_PORT_VID_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200255 REW_PORT_VLAN_CFG, port);
256
257 return 0;
258}
259
260/* 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 Oltean98559342019-11-09 15:02:48 +0200273static int 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}
297
Vladimir Oltean98559342019-11-09 15:02:48 +0200298static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
299 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200300{
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200301 struct ocelot_port *ocelot_port = netdev_priv(dev);
302 struct ocelot *ocelot = ocelot_port->ocelot;
303 int port = ocelot_port->chip_port;
Antoine Tenart71425292018-06-26 14:28:49 +0200304 int ret;
305
Vladimir Oltean98559342019-11-09 15:02:48 +0200306 ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
307 if (ret)
308 return ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200309
Vladimir Oltean98559342019-11-09 15:02:48 +0200310 /* Add the port MAC address to with the right VLAN information */
311 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
312 ENTRYTYPE_LOCKED);
313
314 return 0;
315}
316
317static int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
318{
319 struct ocelot_port *ocelot_port = ocelot->ports[port];
320 int ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200321
322 /* Stop the port from being a member of the vlan */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200323 ocelot->vlan_mask[vid] &= ~BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200324 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
325 if (ret)
326 return ret;
327
328 /* Ingress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200329 if (ocelot_port->pvid == vid)
330 ocelot_port_set_pvid(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200331
332 /* Egress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200333 if (ocelot_port->vid == vid)
334 ocelot_port_set_native_vlan(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200335
336 return 0;
337}
338
Vladimir Oltean98559342019-11-09 15:02:48 +0200339static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
340{
341 struct ocelot_port *ocelot_port = netdev_priv(dev);
342 struct ocelot *ocelot = ocelot_port->ocelot;
343 int port = ocelot_port->chip_port;
344 int ret;
345
346 /* 8021q removes VID 0 on module unload for all interfaces
347 * with VLAN filtering feature. We need to keep it to receive
348 * untagged traffic.
349 */
350 if (vid == 0)
351 return 0;
352
353 ret = ocelot_vlan_del(ocelot, port, vid);
354 if (ret)
355 return ret;
356
357 /* Del the port MAC address to with the right VLAN information */
358 ocelot_mact_forget(ocelot, dev->dev_addr, vid);
359
360 return 0;
361}
362
Alexandre Bellonia556c762018-05-14 22:04:57 +0200363static void ocelot_vlan_init(struct ocelot *ocelot)
364{
Antoine Tenart71425292018-06-26 14:28:49 +0200365 u16 port, vid;
366
Alexandre Bellonia556c762018-05-14 22:04:57 +0200367 /* Clear VLAN table, by default all ports are members of all VLANs */
368 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
369 ANA_TABLES_VLANACCESS);
370 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200371
372 /* Configure the port VLAN memberships */
373 for (vid = 1; vid < VLAN_N_VID; vid++) {
374 ocelot->vlan_mask[vid] = 0;
375 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
376 }
377
378 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
379 * traffic. It is added automatically if 8021q module is loaded, but
380 * we can't rely on it since module may be not loaded.
381 */
382 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
383 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
384
385 /* Configure the CPU port to be VLAN aware */
386 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
387 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
388 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
389 ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
390
391 /* Set vlan ingress filter mask to all ports but the CPU port by
392 * default.
393 */
394 ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
395
396 for (port = 0; port < ocelot->num_phys_ports; port++) {
397 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
398 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
399 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200400}
401
402/* Watermark encode
403 * Bit 8: Unit; 0:1, 1:16
404 * Bit 7-0: Value to be multiplied with unit
405 */
406static u16 ocelot_wm_enc(u16 value)
407{
408 if (value >= BIT(8))
409 return BIT(8) | (value / 16);
410
411 return value;
412}
413
414static void ocelot_port_adjust_link(struct net_device *dev)
415{
416 struct ocelot_port *port = netdev_priv(dev);
417 struct ocelot *ocelot = port->ocelot;
418 u8 p = port->chip_port;
419 int speed, atop_wm, mode = 0;
420
421 switch (dev->phydev->speed) {
422 case SPEED_10:
423 speed = OCELOT_SPEED_10;
424 break;
425 case SPEED_100:
426 speed = OCELOT_SPEED_100;
427 break;
428 case SPEED_1000:
429 speed = OCELOT_SPEED_1000;
430 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
431 break;
432 case SPEED_2500:
433 speed = OCELOT_SPEED_2500;
434 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
435 break;
436 default:
437 netdev_err(dev, "Unsupported PHY speed: %d\n",
438 dev->phydev->speed);
439 return;
440 }
441
442 phy_print_status(dev->phydev);
443
444 if (!dev->phydev->link)
445 return;
446
447 /* Only full duplex supported for now */
448 ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
449 mode, DEV_MAC_MODE_CFG);
450
451 /* Set MAC IFG Gaps
452 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
453 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
454 */
455 ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
456
457 /* Load seed (0) and set MAC HDX late collision */
458 ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
459 DEV_MAC_HDX_CFG_SEED_LOAD,
460 DEV_MAC_HDX_CFG);
461 mdelay(1);
462 ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
463 DEV_MAC_HDX_CFG);
464
465 /* Disable HDX fast control */
466 ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
467
468 /* SGMII only for now */
469 ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
470 ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
471
472 /* Enable PCS */
473 ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
474
475 /* No aneg on SGMII */
476 ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
477
478 /* No loopback */
479 ocelot_port_writel(port, 0, PCS1G_LB_CFG);
480
481 /* Set Max Length and maximum tags allowed */
482 ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
483 ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
484 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
485 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
486 DEV_MAC_TAGS_CFG);
487
488 /* Enable MAC module */
489 ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
490 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
491
492 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
493 * reset */
494 ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
495 DEV_CLOCK_CFG);
496
497 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
498 ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
499 ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
500
501 /* No PFC */
502 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
503 ANA_PFC_PFC_CFG, p);
504
505 /* Set Pause WM hysteresis
506 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
507 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
508 */
509 ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
510 SYS_PAUSE_CFG_PAUSE_STOP(101) |
511 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
512
513 /* Core: Enable port for frame transfer */
514 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
515 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
516 QSYS_SWITCH_PORT_MODE_PORT_ENA,
517 QSYS_SWITCH_PORT_MODE, p);
518
519 /* Flow control */
520 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
521 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
522 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
523 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
524 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
525 SYS_MAC_FC_CFG, p);
526 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
527
528 /* Tail dropping watermark */
529 atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
530 ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
531 SYS_ATOP, p);
532 ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
533}
534
535static int ocelot_port_open(struct net_device *dev)
536{
537 struct ocelot_port *port = netdev_priv(dev);
538 struct ocelot *ocelot = port->ocelot;
539 int err;
540
541 /* Enable receiving frames on the port, and activate auto-learning of
542 * MAC addresses.
543 */
544 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
545 ANA_PORT_PORT_CFG_RECV_ENA |
546 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
547 ANA_PORT_PORT_CFG, port->chip_port);
548
Quentin Schulz71e32a202018-10-04 14:22:08 +0200549 if (port->serdes) {
Grygorii Strashkoc8fe6d72018-11-19 19:24:22 -0600550 err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET,
551 port->phy_mode);
Quentin Schulz71e32a202018-10-04 14:22:08 +0200552 if (err) {
553 netdev_err(dev, "Could not set mode of SerDes\n");
554 return err;
555 }
556 }
557
Alexandre Bellonia556c762018-05-14 22:04:57 +0200558 err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
Quentin Schulz71e32a202018-10-04 14:22:08 +0200559 port->phy_mode);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200560 if (err) {
561 netdev_err(dev, "Could not attach to PHY\n");
562 return err;
563 }
564
565 dev->phydev = port->phy;
566
567 phy_attached_info(port->phy);
568 phy_start(port->phy);
569 return 0;
570}
571
572static int ocelot_port_stop(struct net_device *dev)
573{
574 struct ocelot_port *port = netdev_priv(dev);
575
576 phy_disconnect(port->phy);
577
578 dev->phydev = NULL;
579
580 ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
581 ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
582 QSYS_SWITCH_PORT_MODE, port->chip_port);
583 return 0;
584}
585
586/* Generate the IFH for frame injection
587 *
588 * The IFH is a 128bit-value
589 * bit 127: bypass the analyzer processing
590 * bit 56-67: destination mask
591 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
592 * bit 20-27: cpu extraction queue mask
593 * bit 16: tag type 0: C-tag, 1: S-tag
594 * bit 0-11: VID
595 */
596static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
597{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200598 ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
Antoine Tenart08d02362018-06-20 10:50:46 +0200599 ifh[1] = (0xf00 & info->port) >> 8;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200600 ifh[2] = (0xff & info->port) << 24;
Antoine Tenart08d02362018-06-20 10:50:46 +0200601 ifh[3] = (info->tag_type << 16) | info->vid;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200602
603 return 0;
604}
605
606static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
607{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200608 struct skb_shared_info *shinfo = skb_shinfo(skb);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200609 struct ocelot_port *port = netdev_priv(dev);
610 struct ocelot *ocelot = port->ocelot;
611 u32 val, ifh[IFH_LEN];
612 struct frame_info info = {};
613 u8 grp = 0; /* Send everything on CPU group 0 */
614 unsigned int i, count, last;
615
616 val = ocelot_read(ocelot, QS_INJ_STATUS);
617 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
618 (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
619 return NETDEV_TX_BUSY;
620
621 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
622 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
623
624 info.port = BIT(port->chip_port);
Antoine Tenart08d02362018-06-20 10:50:46 +0200625 info.tag_type = IFH_TAG_TYPE_C;
626 info.vid = skb_vlan_tag_get(skb);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200627
628 /* Check if timestamping is needed */
629 if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
630 info.rew_op = port->ptp_cmd;
631 if (port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
632 info.rew_op |= (port->ts_id % 4) << 3;
633 }
634
Alexandre Bellonia556c762018-05-14 22:04:57 +0200635 ocelot_gen_ifh(ifh, &info);
636
637 for (i = 0; i < IFH_LEN; i++)
Antoine Tenartc2cd6502018-06-22 11:50:52 +0200638 ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
639 QS_INJ_WR, grp);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200640
641 count = (skb->len + 3) / 4;
642 last = skb->len % 4;
643 for (i = 0; i < count; i++) {
644 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
645 }
646
647 /* Add padding */
648 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
649 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
650 i++;
651 }
652
653 /* Indicate EOF and valid bytes in last word */
654 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
655 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
656 QS_INJ_CTRL_EOF,
657 QS_INJ_CTRL, grp);
658
659 /* Add dummy CRC */
660 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
661 skb_tx_timestamp(skb);
662
663 dev->stats.tx_packets++;
664 dev->stats.tx_bytes += skb->len;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200665
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200666 if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
667 port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
668 struct ocelot_skb *oskb =
669 kzalloc(sizeof(struct ocelot_skb), GFP_ATOMIC);
670
671 if (unlikely(!oskb))
672 goto out;
673
674 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
675
676 oskb->skb = skb;
677 oskb->id = port->ts_id % 4;
678 port->ts_id++;
679
680 list_add_tail(&oskb->head, &port->skbs);
681
682 return NETDEV_TX_OK;
683 }
684
685out:
686 dev_kfree_skb_any(skb);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200687 return NETDEV_TX_OK;
688}
689
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200690void ocelot_get_hwtimestamp(struct ocelot *ocelot, struct timespec64 *ts)
691{
692 unsigned long flags;
693 u32 val;
694
695 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
696
697 /* Read current PTP time to get seconds */
698 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
699
700 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
701 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
702 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
703 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
704
705 /* Read packet HW timestamp from FIFO */
706 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
707 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
708
709 /* Sec has incremented since the ts was registered */
710 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
711 ts->tv_sec--;
712
713 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
714}
715EXPORT_SYMBOL(ocelot_get_hwtimestamp);
716
Claudiu Manoil40a15782019-05-21 19:52:55 +0300717static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200718{
Claudiu Manoil40a15782019-05-21 19:52:55 +0300719 struct ocelot_port *port = netdev_priv(dev);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200720
Claudiu Manoil40a15782019-05-21 19:52:55 +0300721 return ocelot_mact_forget(port->ocelot, addr, port->pvid);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200722}
723
Claudiu Manoil40a15782019-05-21 19:52:55 +0300724static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200725{
Claudiu Manoil40a15782019-05-21 19:52:55 +0300726 struct ocelot_port *port = netdev_priv(dev);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200727
Claudiu Manoil40a15782019-05-21 19:52:55 +0300728 return ocelot_mact_learn(port->ocelot, PGID_CPU, addr, port->pvid,
729 ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200730}
731
732static void ocelot_set_rx_mode(struct net_device *dev)
733{
734 struct ocelot_port *port = netdev_priv(dev);
735 struct ocelot *ocelot = port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200736 int i;
737 u32 val;
738
739 /* This doesn't handle promiscuous mode because the bridge core is
740 * setting IFF_PROMISC on all slave interfaces and all frames would be
741 * forwarded to the CPU port.
742 */
743 val = GENMASK(ocelot->num_phys_ports - 1, 0);
744 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
745 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
746
Claudiu Manoil40a15782019-05-21 19:52:55 +0300747 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200748}
749
750static int ocelot_port_get_phys_port_name(struct net_device *dev,
751 char *buf, size_t len)
752{
753 struct ocelot_port *port = netdev_priv(dev);
754 int ret;
755
756 ret = snprintf(buf, len, "p%d", port->chip_port);
757 if (ret >= len)
758 return -EINVAL;
759
760 return 0;
761}
762
763static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
764{
765 struct ocelot_port *port = netdev_priv(dev);
766 struct ocelot *ocelot = port->ocelot;
767 const struct sockaddr *addr = p;
768
769 /* Learn the new net device MAC address in the mac table. */
770 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
771 ENTRYTYPE_LOCKED);
772 /* Then forget the previous one. */
773 ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
774
775 ether_addr_copy(dev->dev_addr, addr->sa_data);
776 return 0;
777}
778
779static void ocelot_get_stats64(struct net_device *dev,
780 struct rtnl_link_stats64 *stats)
781{
782 struct ocelot_port *port = netdev_priv(dev);
783 struct ocelot *ocelot = port->ocelot;
784
785 /* Configure the port to read the stats from */
786 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
787 SYS_STAT_CFG);
788
789 /* Get Rx stats */
790 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
791 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
792 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
793 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
794 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
795 ocelot_read(ocelot, SYS_COUNT_RX_64) +
796 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
797 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
798 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
799 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
800 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
801 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
802 stats->rx_dropped = dev->stats.rx_dropped;
803
804 /* Get Tx stats */
805 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
806 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
807 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
808 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
809 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
810 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
811 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
812 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
813 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
814 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
815}
816
817static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
818 struct net_device *dev, const unsigned char *addr,
Petr Machata87b09842019-01-16 23:06:50 +0000819 u16 vid, u16 flags,
820 struct netlink_ext_ack *extack)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200821{
822 struct ocelot_port *port = netdev_priv(dev);
823 struct ocelot *ocelot = port->ocelot;
824
Antoine Tenart71425292018-06-26 14:28:49 +0200825 if (!vid) {
826 if (!port->vlan_aware)
827 /* If the bridge is not VLAN aware and no VID was
828 * provided, set it to pvid to ensure the MAC entry
829 * matches incoming untagged packets
830 */
831 vid = port->pvid;
832 else
833 /* If the bridge is VLAN aware a VID must be provided as
834 * otherwise the learnt entry wouldn't match any frame.
835 */
836 return -EINVAL;
837 }
838
Alexandre Bellonia556c762018-05-14 22:04:57 +0200839 return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
Allan W. Nielsen8fd1a4a2018-12-20 09:37:17 +0100840 ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200841}
842
843static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
844 struct net_device *dev,
845 const unsigned char *addr, u16 vid)
846{
847 struct ocelot_port *port = netdev_priv(dev);
848 struct ocelot *ocelot = port->ocelot;
849
850 return ocelot_mact_forget(ocelot, addr, vid);
851}
852
853struct ocelot_dump_ctx {
854 struct net_device *dev;
855 struct sk_buff *skb;
856 struct netlink_callback *cb;
857 int idx;
858};
859
860static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
861 struct ocelot_dump_ctx *dump)
862{
863 u32 portid = NETLINK_CB(dump->cb->skb).portid;
864 u32 seq = dump->cb->nlh->nlmsg_seq;
865 struct nlmsghdr *nlh;
866 struct ndmsg *ndm;
867
868 if (dump->idx < dump->cb->args[2])
869 goto skip;
870
871 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
872 sizeof(*ndm), NLM_F_MULTI);
873 if (!nlh)
874 return -EMSGSIZE;
875
876 ndm = nlmsg_data(nlh);
877 ndm->ndm_family = AF_BRIDGE;
878 ndm->ndm_pad1 = 0;
879 ndm->ndm_pad2 = 0;
880 ndm->ndm_flags = NTF_SELF;
881 ndm->ndm_type = 0;
882 ndm->ndm_ifindex = dump->dev->ifindex;
883 ndm->ndm_state = NUD_REACHABLE;
884
885 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
886 goto nla_put_failure;
887
888 if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
889 goto nla_put_failure;
890
891 nlmsg_end(dump->skb, nlh);
892
893skip:
894 dump->idx++;
895 return 0;
896
897nla_put_failure:
898 nlmsg_cancel(dump->skb, nlh);
899 return -EMSGSIZE;
900}
901
902static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
903 struct ocelot_mact_entry *entry)
904{
905 struct ocelot *ocelot = port->ocelot;
906 char mac[ETH_ALEN];
907 u32 val, dst, macl, mach;
908
909 /* Set row and column to read from */
910 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
911 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
912
913 /* Issue a read command */
914 ocelot_write(ocelot,
915 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
916 ANA_TABLES_MACACCESS);
917
918 if (ocelot_mact_wait_for_completion(ocelot))
919 return -ETIMEDOUT;
920
921 /* Read the entry flags */
922 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
923 if (!(val & ANA_TABLES_MACACCESS_VALID))
924 return -EINVAL;
925
926 /* If the entry read has another port configured as its destination,
927 * do not report it.
928 */
929 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
930 if (dst != port->chip_port)
931 return -EINVAL;
932
933 /* Get the entry's MAC address and VLAN id */
934 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
935 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
936
937 mac[0] = (mach >> 8) & 0xff;
938 mac[1] = (mach >> 0) & 0xff;
939 mac[2] = (macl >> 24) & 0xff;
940 mac[3] = (macl >> 16) & 0xff;
941 mac[4] = (macl >> 8) & 0xff;
942 mac[5] = (macl >> 0) & 0xff;
943
944 entry->vid = (mach >> 16) & 0xfff;
945 ether_addr_copy(entry->mac, mac);
946
947 return 0;
948}
949
950static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
951 struct net_device *dev,
952 struct net_device *filter_dev, int *idx)
953{
954 struct ocelot_port *port = netdev_priv(dev);
955 int i, j, ret = 0;
956 struct ocelot_dump_ctx dump = {
957 .dev = dev,
958 .skb = skb,
959 .cb = cb,
960 .idx = *idx,
961 };
962
963 struct ocelot_mact_entry entry;
964
965 /* Loop through all the mac tables entries. There are 1024 rows of 4
966 * entries.
967 */
968 for (i = 0; i < 1024; i++) {
969 for (j = 0; j < 4; j++) {
970 ret = ocelot_mact_read(port, i, j, &entry);
971 /* If the entry is invalid (wrong port, invalid...),
972 * skip it.
973 */
974 if (ret == -EINVAL)
975 continue;
976 else if (ret)
977 goto end;
978
979 ret = ocelot_fdb_do_dump(&entry, &dump);
980 if (ret)
981 goto end;
982 }
983 }
984
985end:
986 *idx = dump.idx;
987 return ret;
988}
989
Antoine Tenart71425292018-06-26 14:28:49 +0200990static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
991 u16 vid)
992{
Vladimir Oltean1c44ce52019-10-26 21:04:26 +0300993 return ocelot_vlan_vid_add(dev, vid, false, false);
Antoine Tenart71425292018-06-26 14:28:49 +0200994}
995
996static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
997 u16 vid)
998{
999 return ocelot_vlan_vid_del(dev, vid);
1000}
1001
1002static int ocelot_set_features(struct net_device *dev,
1003 netdev_features_t features)
1004{
1005 struct ocelot_port *port = netdev_priv(dev);
1006 netdev_features_t changed = dev->features ^ features;
1007
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001008 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
1009 port->tc.offload_cnt) {
1010 netdev_err(dev,
1011 "Cannot disable HW TC offload while offloads active\n");
1012 return -EBUSY;
1013 }
1014
Antoine Tenart71425292018-06-26 14:28:49 +02001015 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
1016 ocelot_vlan_mode(port, features);
1017
1018 return 0;
1019}
1020
Florian Fainelli751302c2019-02-06 09:45:40 -08001021static int ocelot_get_port_parent_id(struct net_device *dev,
1022 struct netdev_phys_item_id *ppid)
1023{
1024 struct ocelot_port *ocelot_port = netdev_priv(dev);
1025 struct ocelot *ocelot = ocelot_port->ocelot;
1026
1027 ppid->id_len = sizeof(ocelot->base_mac);
1028 memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
1029
1030 return 0;
1031}
1032
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001033static int ocelot_hwstamp_get(struct ocelot_port *port, struct ifreq *ifr)
1034{
1035 struct ocelot *ocelot = port->ocelot;
1036
1037 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1038 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1039}
1040
1041static int ocelot_hwstamp_set(struct ocelot_port *port, struct ifreq *ifr)
1042{
1043 struct ocelot *ocelot = port->ocelot;
1044 struct hwtstamp_config cfg;
1045
1046 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1047 return -EFAULT;
1048
1049 /* reserved for future extensions */
1050 if (cfg.flags)
1051 return -EINVAL;
1052
1053 /* Tx type sanity check */
1054 switch (cfg.tx_type) {
1055 case HWTSTAMP_TX_ON:
1056 port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
1057 break;
1058 case HWTSTAMP_TX_ONESTEP_SYNC:
1059 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1060 * need to update the origin time.
1061 */
1062 port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
1063 break;
1064 case HWTSTAMP_TX_OFF:
1065 port->ptp_cmd = 0;
1066 break;
1067 default:
1068 return -ERANGE;
1069 }
1070
1071 mutex_lock(&ocelot->ptp_lock);
1072
1073 switch (cfg.rx_filter) {
1074 case HWTSTAMP_FILTER_NONE:
1075 break;
1076 case HWTSTAMP_FILTER_ALL:
1077 case HWTSTAMP_FILTER_SOME:
1078 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1079 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1080 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1081 case HWTSTAMP_FILTER_NTP_ALL:
1082 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1083 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1084 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1085 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1086 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1087 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1088 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1089 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1090 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1091 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1092 break;
1093 default:
1094 mutex_unlock(&ocelot->ptp_lock);
1095 return -ERANGE;
1096 }
1097
1098 /* Commit back the result & save it */
1099 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1100 mutex_unlock(&ocelot->ptp_lock);
1101
1102 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1103}
1104
1105static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1106{
1107 struct ocelot_port *port = netdev_priv(dev);
1108 struct ocelot *ocelot = port->ocelot;
1109
1110 /* The function is only used for PTP operations for now */
1111 if (!ocelot->ptp)
1112 return -EOPNOTSUPP;
1113
1114 switch (cmd) {
1115 case SIOCSHWTSTAMP:
1116 return ocelot_hwstamp_set(port, ifr);
1117 case SIOCGHWTSTAMP:
1118 return ocelot_hwstamp_get(port, ifr);
1119 default:
1120 return -EOPNOTSUPP;
1121 }
1122}
1123
Alexandre Bellonia556c762018-05-14 22:04:57 +02001124static const struct net_device_ops ocelot_port_netdev_ops = {
1125 .ndo_open = ocelot_port_open,
1126 .ndo_stop = ocelot_port_stop,
1127 .ndo_start_xmit = ocelot_port_xmit,
1128 .ndo_set_rx_mode = ocelot_set_rx_mode,
1129 .ndo_get_phys_port_name = ocelot_port_get_phys_port_name,
1130 .ndo_set_mac_address = ocelot_port_set_mac_address,
1131 .ndo_get_stats64 = ocelot_get_stats64,
1132 .ndo_fdb_add = ocelot_fdb_add,
1133 .ndo_fdb_del = ocelot_fdb_del,
1134 .ndo_fdb_dump = ocelot_fdb_dump,
Antoine Tenart71425292018-06-26 14:28:49 +02001135 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
1136 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
1137 .ndo_set_features = ocelot_set_features,
Florian Fainelli751302c2019-02-06 09:45:40 -08001138 .ndo_get_port_parent_id = ocelot_get_port_parent_id,
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001139 .ndo_setup_tc = ocelot_setup_tc,
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001140 .ndo_do_ioctl = ocelot_ioctl,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001141};
1142
1143static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
1144{
1145 struct ocelot_port *port = netdev_priv(netdev);
1146 struct ocelot *ocelot = port->ocelot;
1147 int i;
1148
1149 if (sset != ETH_SS_STATS)
1150 return;
1151
1152 for (i = 0; i < ocelot->num_stats; i++)
1153 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1154 ETH_GSTRING_LEN);
1155}
1156
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001157static void ocelot_update_stats(struct ocelot *ocelot)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001158{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001159 int i, j;
1160
1161 mutex_lock(&ocelot->stats_lock);
1162
1163 for (i = 0; i < ocelot->num_phys_ports; i++) {
1164 /* Configure the port to read the stats from */
1165 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1166
1167 for (j = 0; j < ocelot->num_stats; j++) {
1168 u32 val;
1169 unsigned int idx = i * ocelot->num_stats + j;
1170
1171 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1172 ocelot->stats_layout[j].offset);
1173
1174 if (val < (ocelot->stats[idx] & U32_MAX))
1175 ocelot->stats[idx] += (u64)1 << 32;
1176
1177 ocelot->stats[idx] = (ocelot->stats[idx] &
1178 ~(u64)U32_MAX) + val;
1179 }
1180 }
1181
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001182 mutex_unlock(&ocelot->stats_lock);
1183}
1184
1185static void ocelot_check_stats_work(struct work_struct *work)
1186{
1187 struct delayed_work *del_work = to_delayed_work(work);
1188 struct ocelot *ocelot = container_of(del_work, struct ocelot,
1189 stats_work);
1190
1191 ocelot_update_stats(ocelot);
1192
Alexandre Bellonia556c762018-05-14 22:04:57 +02001193 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1194 OCELOT_STATS_CHECK_DELAY);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001195}
1196
1197static void ocelot_get_ethtool_stats(struct net_device *dev,
1198 struct ethtool_stats *stats, u64 *data)
1199{
1200 struct ocelot_port *port = netdev_priv(dev);
1201 struct ocelot *ocelot = port->ocelot;
1202 int i;
1203
1204 /* check and update now */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001205 ocelot_update_stats(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001206
1207 /* Copy all counters */
1208 for (i = 0; i < ocelot->num_stats; i++)
1209 *data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
1210}
1211
1212static int ocelot_get_sset_count(struct net_device *dev, int sset)
1213{
1214 struct ocelot_port *port = netdev_priv(dev);
1215 struct ocelot *ocelot = port->ocelot;
1216
1217 if (sset != ETH_SS_STATS)
1218 return -EOPNOTSUPP;
1219 return ocelot->num_stats;
1220}
1221
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001222static int ocelot_get_ts_info(struct net_device *dev,
1223 struct ethtool_ts_info *info)
1224{
1225 struct ocelot_port *ocelot_port = netdev_priv(dev);
1226 struct ocelot *ocelot = ocelot_port->ocelot;
1227
1228 if (!ocelot->ptp)
1229 return ethtool_op_get_ts_info(dev, info);
1230
1231 info->phc_index = ocelot->ptp_clock ?
1232 ptp_clock_index(ocelot->ptp_clock) : -1;
1233 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1234 SOF_TIMESTAMPING_RX_SOFTWARE |
1235 SOF_TIMESTAMPING_SOFTWARE |
1236 SOF_TIMESTAMPING_TX_HARDWARE |
1237 SOF_TIMESTAMPING_RX_HARDWARE |
1238 SOF_TIMESTAMPING_RAW_HARDWARE;
1239 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1240 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
1241 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
1242
1243 return 0;
1244}
1245
Alexandre Bellonia556c762018-05-14 22:04:57 +02001246static const struct ethtool_ops ocelot_ethtool_ops = {
1247 .get_strings = ocelot_get_strings,
1248 .get_ethtool_stats = ocelot_get_ethtool_stats,
1249 .get_sset_count = ocelot_get_sset_count,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001250 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1251 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001252 .get_ts_info = ocelot_get_ts_info,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001253};
1254
Alexandre Bellonia556c762018-05-14 22:04:57 +02001255static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
1256 struct switchdev_trans *trans,
1257 u8 state)
1258{
1259 struct ocelot *ocelot = ocelot_port->ocelot;
1260 u32 port_cfg;
1261 int port, i;
1262
1263 if (switchdev_trans_ph_prepare(trans))
1264 return 0;
1265
1266 if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
1267 return 0;
1268
1269 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
1270 ocelot_port->chip_port);
1271
1272 switch (state) {
1273 case BR_STATE_FORWARDING:
1274 ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
1275 /* Fallthrough */
1276 case BR_STATE_LEARNING:
1277 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1278 break;
1279
1280 default:
1281 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1282 ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
1283 break;
1284 }
1285
1286 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
1287 ocelot_port->chip_port);
1288
1289 /* Apply FWD mask. The loop is needed to add/remove the current port as
1290 * a source for the other ports.
1291 */
1292 for (port = 0; port < ocelot->num_phys_ports; port++) {
1293 if (ocelot->bridge_fwd_mask & BIT(port)) {
1294 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
1295
1296 for (i = 0; i < ocelot->num_phys_ports; i++) {
1297 unsigned long bond_mask = ocelot->lags[i];
1298
1299 if (!bond_mask)
1300 continue;
1301
1302 if (bond_mask & BIT(port)) {
1303 mask &= ~bond_mask;
1304 break;
1305 }
1306 }
1307
1308 ocelot_write_rix(ocelot,
1309 BIT(ocelot->num_phys_ports) | mask,
1310 ANA_PGID_PGID, PGID_SRC + port);
1311 } else {
1312 /* Only the CPU port, this is compatible with link
1313 * aggregation.
1314 */
1315 ocelot_write_rix(ocelot,
1316 BIT(ocelot->num_phys_ports),
1317 ANA_PGID_PGID, PGID_SRC + port);
1318 }
1319 }
1320
1321 return 0;
1322}
1323
1324static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
1325 unsigned long ageing_clock_t)
1326{
1327 struct ocelot *ocelot = ocelot_port->ocelot;
1328 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1329 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1330
1331 ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
1332 ANA_AUTOAGE);
1333}
1334
1335static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
1336{
1337 struct ocelot *ocelot = port->ocelot;
1338 u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
1339 port->chip_port);
1340
1341 if (mc)
1342 val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1343 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1344 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1345 else
1346 val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1347 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1348 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
1349
1350 ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
1351}
1352
1353static int ocelot_port_attr_set(struct net_device *dev,
1354 const struct switchdev_attr *attr,
1355 struct switchdev_trans *trans)
1356{
1357 struct ocelot_port *ocelot_port = netdev_priv(dev);
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001358 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001359 int err = 0;
1360
1361 switch (attr->id) {
1362 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1363 ocelot_port_attr_stp_state_set(ocelot_port, trans,
1364 attr->u.stp_state);
1365 break;
1366 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1367 ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
1368 break;
Antoine Tenart71425292018-06-26 14:28:49 +02001369 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001370 ocelot_port_vlan_filtering(ocelot, ocelot_port->chip_port,
1371 attr->u.vlan_filtering);
Antoine Tenart71425292018-06-26 14:28:49 +02001372 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001373 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1374 ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
1375 break;
1376 default:
1377 err = -EOPNOTSUPP;
1378 break;
1379 }
1380
1381 return err;
1382}
1383
Antoine Tenart71425292018-06-26 14:28:49 +02001384static int ocelot_port_obj_add_vlan(struct net_device *dev,
1385 const struct switchdev_obj_port_vlan *vlan,
1386 struct switchdev_trans *trans)
1387{
1388 int ret;
1389 u16 vid;
1390
1391 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1392 ret = ocelot_vlan_vid_add(dev, vid,
1393 vlan->flags & BRIDGE_VLAN_INFO_PVID,
1394 vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
1395 if (ret)
1396 return ret;
1397 }
1398
1399 return 0;
1400}
1401
1402static int ocelot_port_vlan_del_vlan(struct net_device *dev,
1403 const struct switchdev_obj_port_vlan *vlan)
1404{
1405 int ret;
1406 u16 vid;
1407
1408 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1409 ret = ocelot_vlan_vid_del(dev, vid);
1410
1411 if (ret)
1412 return ret;
1413 }
1414
1415 return 0;
1416}
1417
Alexandre Bellonia556c762018-05-14 22:04:57 +02001418static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1419 const unsigned char *addr,
1420 u16 vid)
1421{
1422 struct ocelot_multicast *mc;
1423
1424 list_for_each_entry(mc, &ocelot->multicast, list) {
1425 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1426 return mc;
1427 }
1428
1429 return NULL;
1430}
1431
1432static int ocelot_port_obj_add_mdb(struct net_device *dev,
1433 const struct switchdev_obj_port_mdb *mdb,
1434 struct switchdev_trans *trans)
1435{
1436 struct ocelot_port *port = netdev_priv(dev);
1437 struct ocelot *ocelot = port->ocelot;
1438 struct ocelot_multicast *mc;
1439 unsigned char addr[ETH_ALEN];
1440 u16 vid = mdb->vid;
1441 bool new = false;
1442
1443 if (!vid)
Antoine Tenart71425292018-06-26 14:28:49 +02001444 vid = port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001445
1446 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1447 if (!mc) {
1448 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1449 if (!mc)
1450 return -ENOMEM;
1451
1452 memcpy(mc->addr, mdb->addr, ETH_ALEN);
1453 mc->vid = vid;
1454
1455 list_add_tail(&mc->list, &ocelot->multicast);
1456 new = true;
1457 }
1458
1459 memcpy(addr, mc->addr, ETH_ALEN);
1460 addr[0] = 0;
1461
1462 if (!new) {
1463 addr[2] = mc->ports << 0;
1464 addr[1] = mc->ports << 8;
1465 ocelot_mact_forget(ocelot, addr, vid);
1466 }
1467
1468 mc->ports |= BIT(port->chip_port);
1469 addr[2] = mc->ports << 0;
1470 addr[1] = mc->ports << 8;
1471
1472 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1473}
1474
1475static int ocelot_port_obj_del_mdb(struct net_device *dev,
1476 const struct switchdev_obj_port_mdb *mdb)
1477{
1478 struct ocelot_port *port = netdev_priv(dev);
1479 struct ocelot *ocelot = port->ocelot;
1480 struct ocelot_multicast *mc;
1481 unsigned char addr[ETH_ALEN];
1482 u16 vid = mdb->vid;
1483
1484 if (!vid)
Antoine Tenart71425292018-06-26 14:28:49 +02001485 vid = port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001486
1487 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1488 if (!mc)
1489 return -ENOENT;
1490
1491 memcpy(addr, mc->addr, ETH_ALEN);
1492 addr[2] = mc->ports << 0;
1493 addr[1] = mc->ports << 8;
1494 addr[0] = 0;
1495 ocelot_mact_forget(ocelot, addr, vid);
1496
1497 mc->ports &= ~BIT(port->chip_port);
1498 if (!mc->ports) {
1499 list_del(&mc->list);
1500 devm_kfree(ocelot->dev, mc);
1501 return 0;
1502 }
1503
1504 addr[2] = mc->ports << 0;
1505 addr[1] = mc->ports << 8;
1506
1507 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1508}
1509
1510static int ocelot_port_obj_add(struct net_device *dev,
1511 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +00001512 struct switchdev_trans *trans,
1513 struct netlink_ext_ack *extack)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001514{
1515 int ret = 0;
1516
1517 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001518 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1519 ret = ocelot_port_obj_add_vlan(dev,
1520 SWITCHDEV_OBJ_PORT_VLAN(obj),
1521 trans);
1522 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001523 case SWITCHDEV_OBJ_ID_PORT_MDB:
1524 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1525 trans);
1526 break;
1527 default:
1528 return -EOPNOTSUPP;
1529 }
1530
1531 return ret;
1532}
1533
1534static int ocelot_port_obj_del(struct net_device *dev,
1535 const struct switchdev_obj *obj)
1536{
1537 int ret = 0;
1538
1539 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001540 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1541 ret = ocelot_port_vlan_del_vlan(dev,
1542 SWITCHDEV_OBJ_PORT_VLAN(obj));
1543 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001544 case SWITCHDEV_OBJ_ID_PORT_MDB:
1545 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1546 break;
1547 default:
1548 return -EOPNOTSUPP;
1549 }
1550
1551 return ret;
1552}
1553
Alexandre Bellonia556c762018-05-14 22:04:57 +02001554static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1555 struct net_device *bridge)
1556{
1557 struct ocelot *ocelot = ocelot_port->ocelot;
1558
1559 if (!ocelot->bridge_mask) {
1560 ocelot->hw_bridge_dev = bridge;
1561 } else {
1562 if (ocelot->hw_bridge_dev != bridge)
1563 /* This is adding the port to a second bridge, this is
1564 * unsupported */
1565 return -ENODEV;
1566 }
1567
1568 ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1569
1570 return 0;
1571}
1572
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001573static int ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1574 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001575{
1576 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001577 int port = ocelot_port->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001578
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001579 ocelot->bridge_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001580
1581 if (!ocelot->bridge_mask)
1582 ocelot->hw_bridge_dev = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02001583
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001584 ocelot_port_vlan_filtering(ocelot, port, 0);
1585 ocelot_port_set_pvid(ocelot, port, 0);
1586 return ocelot_port_set_native_vlan(ocelot, port, 0);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001587}
1588
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001589static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1590{
1591 int i, port, lag;
1592
1593 /* Reset destination and aggregation PGIDS */
1594 for (port = 0; port < ocelot->num_phys_ports; port++)
1595 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1596
1597 for (i = PGID_AGGR; i < PGID_SRC; i++)
1598 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1599 ANA_PGID_PGID, i);
1600
1601 /* Now, set PGIDs for each LAG */
1602 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1603 unsigned long bond_mask;
1604 int aggr_count = 0;
1605 u8 aggr_idx[16];
1606
1607 bond_mask = ocelot->lags[lag];
1608 if (!bond_mask)
1609 continue;
1610
1611 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1612 // Destination mask
1613 ocelot_write_rix(ocelot, bond_mask,
1614 ANA_PGID_PGID, port);
1615 aggr_idx[aggr_count] = port;
1616 aggr_count++;
1617 }
1618
1619 for (i = PGID_AGGR; i < PGID_SRC; i++) {
1620 u32 ac;
1621
1622 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1623 ac &= ~bond_mask;
1624 ac |= BIT(aggr_idx[i % aggr_count]);
1625 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1626 }
1627 }
1628}
1629
1630static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1631{
1632 unsigned long bond_mask = ocelot->lags[lag];
1633 unsigned int p;
1634
1635 for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1636 u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1637
1638 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1639
1640 /* Use lag port as logical port for port i */
1641 ocelot_write_gix(ocelot, port_cfg |
1642 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1643 ANA_PORT_PORT_CFG, p);
1644 }
1645}
1646
1647static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
1648 struct net_device *bond)
1649{
1650 struct ocelot *ocelot = ocelot_port->ocelot;
1651 int p = ocelot_port->chip_port;
1652 int lag, lp;
1653 struct net_device *ndev;
1654 u32 bond_mask = 0;
1655
1656 rcu_read_lock();
1657 for_each_netdev_in_bond_rcu(bond, ndev) {
1658 struct ocelot_port *port = netdev_priv(ndev);
1659
1660 bond_mask |= BIT(port->chip_port);
1661 }
1662 rcu_read_unlock();
1663
1664 lp = __ffs(bond_mask);
1665
1666 /* If the new port is the lowest one, use it as the logical port from
1667 * now on
1668 */
1669 if (p == lp) {
1670 lag = p;
1671 ocelot->lags[p] = bond_mask;
1672 bond_mask &= ~BIT(p);
1673 if (bond_mask) {
1674 lp = __ffs(bond_mask);
1675 ocelot->lags[lp] = 0;
1676 }
1677 } else {
1678 lag = lp;
1679 ocelot->lags[lp] |= BIT(p);
1680 }
1681
1682 ocelot_setup_lag(ocelot, lag);
1683 ocelot_set_aggr_pgids(ocelot);
1684
1685 return 0;
1686}
1687
1688static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
1689 struct net_device *bond)
1690{
1691 struct ocelot *ocelot = ocelot_port->ocelot;
1692 int p = ocelot_port->chip_port;
1693 u32 port_cfg;
1694 int i;
1695
1696 /* Remove port from any lag */
1697 for (i = 0; i < ocelot->num_phys_ports; i++)
1698 ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
1699
1700 /* if it was the logical port of the lag, move the lag config to the
1701 * next port
1702 */
1703 if (ocelot->lags[p]) {
1704 int n = __ffs(ocelot->lags[p]);
1705
1706 ocelot->lags[n] = ocelot->lags[p];
1707 ocelot->lags[p] = 0;
1708
1709 ocelot_setup_lag(ocelot, n);
1710 }
1711
1712 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1713 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1714 ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
1715 ANA_PORT_PORT_CFG, p);
1716
1717 ocelot_set_aggr_pgids(ocelot);
1718}
1719
Alexandre Bellonia556c762018-05-14 22:04:57 +02001720/* Checks if the net_device instance given to us originate from our driver. */
1721static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1722{
1723 return dev->netdev_ops == &ocelot_port_netdev_ops;
1724}
1725
1726static int ocelot_netdevice_port_event(struct net_device *dev,
1727 unsigned long event,
1728 struct netdev_notifier_changeupper_info *info)
1729{
1730 struct ocelot_port *ocelot_port = netdev_priv(dev);
1731 int err = 0;
1732
Alexandre Bellonia556c762018-05-14 22:04:57 +02001733 switch (event) {
1734 case NETDEV_CHANGEUPPER:
1735 if (netif_is_bridge_master(info->upper_dev)) {
1736 if (info->linking)
1737 err = ocelot_port_bridge_join(ocelot_port,
1738 info->upper_dev);
1739 else
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001740 err = ocelot_port_bridge_leave(ocelot_port,
1741 info->upper_dev);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001742 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001743 if (netif_is_lag_master(info->upper_dev)) {
1744 if (info->linking)
1745 err = ocelot_port_lag_join(ocelot_port,
1746 info->upper_dev);
1747 else
1748 ocelot_port_lag_leave(ocelot_port,
1749 info->upper_dev);
1750 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001751 break;
1752 default:
1753 break;
1754 }
1755
1756 return err;
1757}
1758
1759static int ocelot_netdevice_event(struct notifier_block *unused,
1760 unsigned long event, void *ptr)
1761{
1762 struct netdev_notifier_changeupper_info *info = ptr;
1763 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Geert Uytterhoeven2ac0e152018-06-07 15:10:30 +02001764 int ret = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001765
Claudiu Manoil7afb3e52019-11-05 23:50:13 +02001766 if (!ocelot_netdevice_dev_check(dev))
1767 return 0;
1768
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001769 if (event == NETDEV_PRECHANGEUPPER &&
1770 netif_is_lag_master(info->upper_dev)) {
1771 struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1772 struct netlink_ext_ack *extack;
1773
Claudiu Manoil3b3eed82019-11-05 23:50:14 +02001774 if (lag_upper_info &&
1775 lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001776 extack = netdev_notifier_info_to_extack(&info->info);
1777 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1778
1779 ret = -EINVAL;
1780 goto notify;
1781 }
1782 }
1783
Alexandre Bellonia556c762018-05-14 22:04:57 +02001784 if (netif_is_lag_master(dev)) {
1785 struct net_device *slave;
1786 struct list_head *iter;
1787
1788 netdev_for_each_lower_dev(dev, slave, iter) {
1789 ret = ocelot_netdevice_port_event(slave, event, info);
1790 if (ret)
1791 goto notify;
1792 }
1793 } else {
1794 ret = ocelot_netdevice_port_event(dev, event, info);
1795 }
1796
1797notify:
1798 return notifier_from_errno(ret);
1799}
1800
1801struct notifier_block ocelot_netdevice_nb __read_mostly = {
1802 .notifier_call = ocelot_netdevice_event,
1803};
1804EXPORT_SYMBOL(ocelot_netdevice_nb);
1805
Florian Fainelli56da64b2019-02-27 11:44:29 -08001806static int ocelot_switchdev_event(struct notifier_block *unused,
1807 unsigned long event, void *ptr)
1808{
1809 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1810 int err;
1811
1812 switch (event) {
1813 case SWITCHDEV_PORT_ATTR_SET:
1814 err = switchdev_handle_port_attr_set(dev, ptr,
1815 ocelot_netdevice_dev_check,
1816 ocelot_port_attr_set);
1817 return notifier_from_errno(err);
1818 }
1819
1820 return NOTIFY_DONE;
1821}
1822
1823struct notifier_block ocelot_switchdev_nb __read_mostly = {
1824 .notifier_call = ocelot_switchdev_event,
1825};
1826EXPORT_SYMBOL(ocelot_switchdev_nb);
1827
Petr Machata0e332c82018-11-22 23:30:11 +00001828static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1829 unsigned long event, void *ptr)
1830{
1831 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1832 int err;
1833
1834 switch (event) {
1835 /* Blocking events. */
1836 case SWITCHDEV_PORT_OBJ_ADD:
1837 err = switchdev_handle_port_obj_add(dev, ptr,
1838 ocelot_netdevice_dev_check,
1839 ocelot_port_obj_add);
1840 return notifier_from_errno(err);
1841 case SWITCHDEV_PORT_OBJ_DEL:
1842 err = switchdev_handle_port_obj_del(dev, ptr,
1843 ocelot_netdevice_dev_check,
1844 ocelot_port_obj_del);
1845 return notifier_from_errno(err);
Florian Fainelli56da64b2019-02-27 11:44:29 -08001846 case SWITCHDEV_PORT_ATTR_SET:
1847 err = switchdev_handle_port_attr_set(dev, ptr,
1848 ocelot_netdevice_dev_check,
1849 ocelot_port_attr_set);
1850 return notifier_from_errno(err);
Petr Machata0e332c82018-11-22 23:30:11 +00001851 }
1852
1853 return NOTIFY_DONE;
1854}
1855
1856struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1857 .notifier_call = ocelot_switchdev_blocking_event,
1858};
1859EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
1860
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001861int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
1862{
1863 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1864 unsigned long flags;
1865 time64_t s;
1866 u32 val;
1867 s64 ns;
1868
1869 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
1870
1871 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1872 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1873 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
1874 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1875
1876 s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
1877 s <<= 32;
1878 s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
1879 ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
1880
1881 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
1882
1883 /* Deal with negative values */
1884 if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
1885 s--;
1886 ns &= 0xf;
1887 ns += 999999984;
1888 }
1889
1890 set_normalized_timespec64(ts, s, ns);
1891 return 0;
1892}
1893EXPORT_SYMBOL(ocelot_ptp_gettime64);
1894
1895static int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
1896 const struct timespec64 *ts)
1897{
1898 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1899 unsigned long flags;
1900 u32 val;
1901
1902 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
1903
1904 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1905 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1906 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
1907
1908 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1909
1910 ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
1911 TOD_ACC_PIN);
1912 ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
1913 TOD_ACC_PIN);
1914 ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
1915
1916 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1917 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1918 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
1919
1920 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1921
1922 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
1923 return 0;
1924}
1925
1926static int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1927{
1928 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
1929 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1930 unsigned long flags;
1931 u32 val;
1932
1933 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
1934
1935 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1936 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1937 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
1938
1939 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1940
1941 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
1942 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
1943 ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
1944
1945 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1946 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1947 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
1948
1949 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1950
1951 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
1952 } else {
1953 /* Fall back using ocelot_ptp_settime64 which is not exact. */
1954 struct timespec64 ts;
1955 u64 now;
1956
1957 ocelot_ptp_gettime64(ptp, &ts);
1958
1959 now = ktime_to_ns(timespec64_to_ktime(ts));
1960 ts = ns_to_timespec64(now + delta);
1961
1962 ocelot_ptp_settime64(ptp, &ts);
1963 }
1964 return 0;
1965}
1966
1967static int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
1968{
1969 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1970 u32 unit = 0, direction = 0;
1971 unsigned long flags;
1972 u64 adj = 0;
1973
1974 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
1975
1976 if (!scaled_ppm)
1977 goto disable_adj;
1978
1979 if (scaled_ppm < 0) {
1980 direction = PTP_CFG_CLK_ADJ_CFG_DIR;
1981 scaled_ppm = -scaled_ppm;
1982 }
1983
1984 adj = PSEC_PER_SEC << 16;
1985 do_div(adj, scaled_ppm);
1986 do_div(adj, 1000);
1987
1988 /* If the adjustment value is too large, use ns instead */
1989 if (adj >= (1L << 30)) {
1990 unit = PTP_CFG_CLK_ADJ_FREQ_NS;
1991 do_div(adj, 1000);
1992 }
1993
1994 /* Still too big */
1995 if (adj >= (1L << 30))
1996 goto disable_adj;
1997
1998 ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
1999 ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
2000 PTP_CLK_CFG_ADJ_CFG);
2001
2002 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2003 return 0;
2004
2005disable_adj:
2006 ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
2007
2008 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2009 return 0;
2010}
2011
2012static struct ptp_clock_info ocelot_ptp_clock_info = {
2013 .owner = THIS_MODULE,
2014 .name = "ocelot ptp",
2015 .max_adj = 0x7fffffff,
2016 .n_alarm = 0,
2017 .n_ext_ts = 0,
2018 .n_per_out = 0,
2019 .n_pins = 0,
2020 .pps = 0,
2021 .gettime64 = ocelot_ptp_gettime64,
2022 .settime64 = ocelot_ptp_settime64,
2023 .adjtime = ocelot_ptp_adjtime,
2024 .adjfine = ocelot_ptp_adjfine,
2025};
2026
2027static int ocelot_init_timestamp(struct ocelot *ocelot)
2028{
2029 ocelot->ptp_info = ocelot_ptp_clock_info;
2030 ocelot->ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
2031 if (IS_ERR(ocelot->ptp_clock))
2032 return PTR_ERR(ocelot->ptp_clock);
2033 /* Check if PHC support is missing at the configuration level */
2034 if (!ocelot->ptp_clock)
2035 return 0;
2036
2037 ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
2038 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
2039 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
2040
2041 ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
2042
2043 /* There is no device reconfiguration, PTP Rx stamping is always
2044 * enabled.
2045 */
2046 ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
2047
2048 return 0;
2049}
2050
Alexandre Bellonia556c762018-05-14 22:04:57 +02002051int ocelot_probe_port(struct ocelot *ocelot, u8 port,
2052 void __iomem *regs,
2053 struct phy_device *phy)
2054{
2055 struct ocelot_port *ocelot_port;
2056 struct net_device *dev;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02002057 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002058 int err;
2059
2060 dev = alloc_etherdev(sizeof(struct ocelot_port));
2061 if (!dev)
2062 return -ENOMEM;
2063 SET_NETDEV_DEV(dev, ocelot->dev);
2064 ocelot_port = netdev_priv(dev);
2065 ocelot_port->dev = dev;
2066 ocelot_port->ocelot = ocelot;
2067 ocelot_port->regs = regs;
2068 ocelot_port->chip_port = port;
2069 ocelot_port->phy = phy;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002070 ocelot->ports[port] = ocelot_port;
2071
2072 dev->netdev_ops = &ocelot_port_netdev_ops;
2073 dev->ethtool_ops = &ocelot_ethtool_ops;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002074
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02002075 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
2076 NETIF_F_HW_TC;
2077 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
Antoine Tenart71425292018-06-26 14:28:49 +02002078
Alexandre Bellonia556c762018-05-14 22:04:57 +02002079 memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2080 dev->dev_addr[ETH_ALEN - 1] += port;
2081 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2082 ENTRYTYPE_LOCKED);
2083
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002084 INIT_LIST_HEAD(&ocelot_port->skbs);
2085
Alexandre Bellonia556c762018-05-14 22:04:57 +02002086 err = register_netdev(dev);
2087 if (err) {
2088 dev_err(ocelot->dev, "register_netdev failed\n");
2089 goto err_register_netdev;
2090 }
2091
Antoine Tenart71425292018-06-26 14:28:49 +02002092 /* Basic L2 initialization */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02002093
2094 /* Drop frames with multicast source address */
2095 val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
2096 ocelot_rmw_gix(ocelot, val, val, ANA_PORT_DROP_CFG, port);
2097
2098 /* Set default VLAN and tag type to 8021Q. */
2099 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2100 REW_PORT_VLAN_CFG_PORT_TPID_M,
2101 REW_PORT_VLAN_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +02002102
Horatiu Vulturb5962292019-05-31 09:16:56 +02002103 /* Enable vcap lookups */
2104 ocelot_vcap_enable(ocelot, ocelot_port);
2105
Alexandre Bellonia556c762018-05-14 22:04:57 +02002106 return 0;
2107
2108err_register_netdev:
2109 free_netdev(dev);
2110 return err;
2111}
2112EXPORT_SYMBOL(ocelot_probe_port);
2113
2114int ocelot_init(struct ocelot *ocelot)
2115{
2116 u32 port;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002117 int i, ret, cpu = ocelot->num_phys_ports;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002118 char queue_name[32];
2119
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002120 ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2121 sizeof(u32), GFP_KERNEL);
2122 if (!ocelot->lags)
2123 return -ENOMEM;
2124
Alexandre Bellonia556c762018-05-14 22:04:57 +02002125 ocelot->stats = devm_kcalloc(ocelot->dev,
2126 ocelot->num_phys_ports * ocelot->num_stats,
2127 sizeof(u64), GFP_KERNEL);
2128 if (!ocelot->stats)
2129 return -ENOMEM;
2130
2131 mutex_init(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002132 mutex_init(&ocelot->ptp_lock);
2133 spin_lock_init(&ocelot->ptp_clock_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002134 snprintf(queue_name, sizeof(queue_name), "%s-stats",
2135 dev_name(ocelot->dev));
2136 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2137 if (!ocelot->stats_queue)
2138 return -ENOMEM;
2139
2140 ocelot_mact_init(ocelot);
2141 ocelot_vlan_init(ocelot);
Horatiu Vulturb5962292019-05-31 09:16:56 +02002142 ocelot_ace_init(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002143
2144 for (port = 0; port < ocelot->num_phys_ports; port++) {
2145 /* Clear all counters (5 groups) */
2146 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2147 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2148 SYS_STAT_CFG);
2149 }
2150
2151 /* Only use S-Tag */
2152 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2153
2154 /* Aggregation mode */
2155 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2156 ANA_AGGR_CFG_AC_DMAC_ENA |
2157 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2158 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2159
2160 /* Set MAC age time to default value. The entry is aged after
2161 * 2*AGE_PERIOD
2162 */
2163 ocelot_write(ocelot,
2164 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2165 ANA_AUTOAGE);
2166
2167 /* Disable learning for frames discarded by VLAN ingress filtering */
2168 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2169
2170 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2171 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2172 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2173
2174 /* Setup flooding PGIDs */
2175 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2176 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2177 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2178 ANA_FLOODING, 0);
2179 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2180 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2181 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2182 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2183 ANA_FLOODING_IPMC);
2184
2185 for (port = 0; port < ocelot->num_phys_ports; port++) {
2186 /* Transmit the frame to the local port. */
2187 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2188 /* Do not forward BPDU frames to the front ports. */
2189 ocelot_write_gix(ocelot,
2190 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2191 ANA_PORT_CPU_FWD_BPDU_CFG,
2192 port);
2193 /* Ensure bridging is disabled */
2194 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2195 }
2196
2197 /* Configure and enable the CPU port. */
2198 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2199 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2200 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2201 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2202 ANA_PORT_PORT_CFG, cpu);
2203
2204 /* Allow broadcast MAC frames. */
2205 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2206 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2207
2208 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2209 }
2210 ocelot_write_rix(ocelot,
2211 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2212 ANA_PGID_PGID, PGID_MC);
2213 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2214 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2215
2216 /* CPU port Injection/Extraction configuration */
2217 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2218 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2219 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2220 QSYS_SWITCH_PORT_MODE, cpu);
2221 ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
2222 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
2223 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2224 * registers endianness.
2225 */
2226 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2227 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2228 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2229 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2230 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2231 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2232 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2233 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2234 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2235 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2236 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2237 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2238 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2239 for (i = 0; i < 16; i++)
2240 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2241 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2242 ANA_CPUQ_8021_CFG, i);
2243
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03002244 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002245 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2246 OCELOT_STATS_CHECK_DELAY);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002247
2248 if (ocelot->ptp) {
2249 ret = ocelot_init_timestamp(ocelot);
2250 if (ret) {
2251 dev_err(ocelot->dev,
2252 "Timestamp initialization failed\n");
2253 return ret;
2254 }
2255 }
2256
Alexandre Bellonia556c762018-05-14 22:04:57 +02002257 return 0;
2258}
2259EXPORT_SYMBOL(ocelot_init);
2260
2261void ocelot_deinit(struct ocelot *ocelot)
2262{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002263 struct list_head *pos, *tmp;
2264 struct ocelot_port *port;
2265 struct ocelot_skb *entry;
2266 int i;
2267
Claudiu Manoilc5d13962019-07-25 16:33:18 +03002268 cancel_delayed_work(&ocelot->stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002269 destroy_workqueue(ocelot->stats_queue);
2270 mutex_destroy(&ocelot->stats_lock);
Horatiu Vulturb5962292019-05-31 09:16:56 +02002271 ocelot_ace_deinit();
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002272
2273 for (i = 0; i < ocelot->num_phys_ports; i++) {
2274 port = ocelot->ports[i];
2275
2276 list_for_each_safe(pos, tmp, &port->skbs) {
2277 entry = list_entry(pos, struct ocelot_skb, head);
2278
2279 list_del(pos);
2280 dev_kfree_skb_any(entry->skb);
2281 kfree(entry);
2282 }
2283 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02002284}
2285EXPORT_SYMBOL(ocelot_deinit);
2286
2287MODULE_LICENSE("Dual MIT/GPL");