blob: 8b7d46693e49f29efae0297281a05cb1d104c0f3 [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>
Vladimir Oltean531ee1a2019-11-09 15:02:49 +020024#include <net/dsa.h>
Alexandre Bellonia556c762018-05-14 22:04:57 +020025
26#include "ocelot.h"
Horatiu Vulturb5962292019-05-31 09:16:56 +020027#include "ocelot_ace.h"
Alexandre Bellonia556c762018-05-14 22:04:57 +020028
Steen Hegelund639c1b22018-12-20 14:16:31 +010029#define TABLE_UPDATE_SLEEP_US 10
30#define TABLE_UPDATE_TIMEOUT_US 100000
31
Alexandre Bellonia556c762018-05-14 22:04:57 +020032/* MAC table entry types.
33 * ENTRYTYPE_NORMAL is subject to aging.
34 * ENTRYTYPE_LOCKED is not subject to aging.
35 * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
36 * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
37 */
38enum macaccess_entry_type {
39 ENTRYTYPE_NORMAL = 0,
40 ENTRYTYPE_LOCKED,
41 ENTRYTYPE_MACv4,
42 ENTRYTYPE_MACv6,
43};
44
45struct ocelot_mact_entry {
46 u8 mac[ETH_ALEN];
47 u16 vid;
48 enum macaccess_entry_type type;
49};
50
Steen Hegelund639c1b22018-12-20 14:16:31 +010051static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
52{
53 return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
54}
55
Alexandre Bellonia556c762018-05-14 22:04:57 +020056static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
57{
Steen Hegelund639c1b22018-12-20 14:16:31 +010058 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +020059
Steen Hegelund639c1b22018-12-20 14:16:31 +010060 return readx_poll_timeout(ocelot_mact_read_macaccess,
61 ocelot, val,
62 (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
63 MACACCESS_CMD_IDLE,
64 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +020065}
66
67static void ocelot_mact_select(struct ocelot *ocelot,
68 const unsigned char mac[ETH_ALEN],
69 unsigned int vid)
70{
71 u32 macl = 0, mach = 0;
72
73 /* Set the MAC address to handle and the vlan associated in a format
74 * understood by the hardware.
75 */
76 mach |= vid << 16;
77 mach |= mac[0] << 8;
78 mach |= mac[1] << 0;
79 macl |= mac[2] << 24;
80 macl |= mac[3] << 16;
81 macl |= mac[4] << 8;
82 macl |= mac[5] << 0;
83
84 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
85 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
86
87}
88
89static int ocelot_mact_learn(struct ocelot *ocelot, int port,
90 const unsigned char mac[ETH_ALEN],
91 unsigned int vid,
92 enum macaccess_entry_type type)
93{
94 ocelot_mact_select(ocelot, mac, vid);
95
96 /* Issue a write command */
97 ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
98 ANA_TABLES_MACACCESS_DEST_IDX(port) |
99 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
100 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
101 ANA_TABLES_MACACCESS);
102
103 return ocelot_mact_wait_for_completion(ocelot);
104}
105
106static int ocelot_mact_forget(struct ocelot *ocelot,
107 const unsigned char mac[ETH_ALEN],
108 unsigned int vid)
109{
110 ocelot_mact_select(ocelot, mac, vid);
111
112 /* Issue a forget command */
113 ocelot_write(ocelot,
114 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
115 ANA_TABLES_MACACCESS);
116
117 return ocelot_mact_wait_for_completion(ocelot);
118}
119
120static void ocelot_mact_init(struct ocelot *ocelot)
121{
122 /* Configure the learning mode entries attributes:
123 * - Do not copy the frame to the CPU extraction queues.
124 * - Use the vlan and mac_cpoy for dmac lookup.
125 */
126 ocelot_rmw(ocelot, 0,
127 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
128 | ANA_AGENCTRL_LEARN_FWD_KILL
129 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
130 ANA_AGENCTRL);
131
132 /* Clear the MAC table */
133 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
134}
135
Horatiu Vulturb5962292019-05-31 09:16:56 +0200136static void ocelot_vcap_enable(struct ocelot *ocelot, struct ocelot_port *port)
137{
138 ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
139 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
140 ANA_PORT_VCAP_S2_CFG, port->chip_port);
141}
142
Steen Hegelund639c1b22018-12-20 14:16:31 +0100143static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
144{
145 return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
146}
147
Alexandre Bellonia556c762018-05-14 22:04:57 +0200148static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
149{
Steen Hegelund639c1b22018-12-20 14:16:31 +0100150 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200151
Steen Hegelund639c1b22018-12-20 14:16:31 +0100152 return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
153 ocelot,
154 val,
155 (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
156 ANA_TABLES_VLANACCESS_CMD_IDLE,
157 TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200158}
159
Antoine Tenart71425292018-06-26 14:28:49 +0200160static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
161{
162 /* Select the VID to configure */
163 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
164 ANA_TABLES_VLANTIDX);
165 /* Set the vlan port members mask and issue a write command */
166 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
167 ANA_TABLES_VLANACCESS_CMD_WRITE,
168 ANA_TABLES_VLANACCESS);
169
170 return ocelot_vlant_wait_for_completion(ocelot);
171}
172
173static void ocelot_vlan_mode(struct ocelot_port *port,
174 netdev_features_t features)
175{
176 struct ocelot *ocelot = port->ocelot;
177 u8 p = port->chip_port;
178 u32 val;
179
180 /* Filtering */
181 val = ocelot_read(ocelot, ANA_VLANMASK);
182 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
183 val |= BIT(p);
184 else
185 val &= ~BIT(p);
186 ocelot_write(ocelot, val, ANA_VLANMASK);
187}
188
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200189static void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
190 bool vlan_aware)
Antoine Tenart71425292018-06-26 14:28:49 +0200191{
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200192 struct ocelot_port *ocelot_port = ocelot->ports[port];
Antoine Tenart71425292018-06-26 14:28:49 +0200193 u32 val;
194
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200195 if (vlan_aware)
196 val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
197 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
198 else
199 val = 0;
Antoine Tenart71425292018-06-26 14:28:49 +0200200 ocelot_rmw_gix(ocelot, val,
Antoine Tenart71425292018-06-26 14:28:49 +0200201 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
202 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200203 ANA_PORT_VLAN_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200204
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200205 if (vlan_aware && !ocelot_port->vid)
Antoine Tenart71425292018-06-26 14:28:49 +0200206 /* If port is vlan-aware and tagged, drop untagged and priority
207 * tagged frames.
208 */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200209 val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
210 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
211 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
212 else
213 val = 0;
214 ocelot_rmw_gix(ocelot, val,
215 ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
Antoine Tenart71425292018-06-26 14:28:49 +0200216 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200217 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
218 ANA_PORT_DROP_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200219
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200220 if (vlan_aware) {
221 if (ocelot_port->vid)
Antoine Tenart71425292018-06-26 14:28:49 +0200222 /* 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);
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200227 } else {
228 /* Port tagging disabled. */
229 val = REW_TAG_CFG_TAG_CFG(0);
Antoine Tenart71425292018-06-26 14:28:49 +0200230 }
231 ocelot_rmw_gix(ocelot, val,
Antoine Tenart71425292018-06-26 14:28:49 +0200232 REW_TAG_CFG_TAG_CFG_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200233 REW_TAG_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +0200234
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200235 ocelot_port->vlan_aware = vlan_aware;
236}
237
238static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
239 u16 vid)
240{
241 struct ocelot_port *ocelot_port = ocelot->ports[port];
242
243 if (ocelot_port->vid != vid) {
244 /* Always permit deleting the native VLAN (vid = 0) */
245 if (ocelot_port->vid && vid) {
246 dev_err(ocelot->dev,
247 "Port already has a native VLAN: %d\n",
248 ocelot_port->vid);
249 return -EBUSY;
250 }
251 ocelot_port->vid = vid;
252 }
253
254 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
Antoine Tenart71425292018-06-26 14:28:49 +0200255 REW_PORT_VLAN_CFG_PORT_VID_M,
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200256 REW_PORT_VLAN_CFG, port);
257
258 return 0;
259}
260
261/* Default vlan to clasify for untagged frames (may be zero) */
262static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
263{
264 struct ocelot_port *ocelot_port = ocelot->ports[port];
265
266 ocelot_rmw_gix(ocelot,
267 ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
268 ANA_PORT_VLAN_CFG_VLAN_VID_M,
269 ANA_PORT_VLAN_CFG, port);
270
271 ocelot_port->pvid = pvid;
Antoine Tenart71425292018-06-26 14:28:49 +0200272}
273
Vladimir Oltean98559342019-11-09 15:02:48 +0200274static int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
275 bool untagged)
Antoine Tenart71425292018-06-26 14:28:49 +0200276{
Antoine Tenart71425292018-06-26 14:28:49 +0200277 int ret;
278
Antoine Tenart71425292018-06-26 14:28:49 +0200279 /* Make the port a member of the VLAN */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200280 ocelot->vlan_mask[vid] |= BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200281 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
282 if (ret)
283 return ret;
284
285 /* Default ingress vlan classification */
286 if (pvid)
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200287 ocelot_port_set_pvid(ocelot, port, vid);
Antoine Tenart71425292018-06-26 14:28:49 +0200288
289 /* Untagged egress vlan clasification */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200290 if (untagged) {
291 ret = ocelot_port_set_native_vlan(ocelot, port, vid);
292 if (ret)
293 return ret;
Vladimir Olteanb9cd75e2019-10-26 21:04:27 +0300294 }
Antoine Tenart71425292018-06-26 14:28:49 +0200295
Antoine Tenart71425292018-06-26 14:28:49 +0200296 return 0;
297}
298
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 Oltean97bb69e2019-11-09 15:02:47 +0200302 struct ocelot_port *ocelot_port = netdev_priv(dev);
303 struct ocelot *ocelot = ocelot_port->ocelot;
304 int port = ocelot_port->chip_port;
Antoine Tenart71425292018-06-26 14:28:49 +0200305 int ret;
306
Vladimir Oltean98559342019-11-09 15:02:48 +0200307 ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
308 if (ret)
309 return ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200310
Vladimir Oltean98559342019-11-09 15:02:48 +0200311 /* Add the port MAC address to with the right VLAN information */
312 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
313 ENTRYTYPE_LOCKED);
314
315 return 0;
316}
317
318static int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
319{
320 struct ocelot_port *ocelot_port = ocelot->ports[port];
321 int ret;
Antoine Tenart71425292018-06-26 14:28:49 +0200322
323 /* Stop the port from being a member of the vlan */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200324 ocelot->vlan_mask[vid] &= ~BIT(port);
Antoine Tenart71425292018-06-26 14:28:49 +0200325 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
326 if (ret)
327 return ret;
328
329 /* Ingress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200330 if (ocelot_port->pvid == vid)
331 ocelot_port_set_pvid(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200332
333 /* Egress */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +0200334 if (ocelot_port->vid == vid)
335 ocelot_port_set_native_vlan(ocelot, port, 0);
Antoine Tenart71425292018-06-26 14:28:49 +0200336
337 return 0;
338}
339
Vladimir Oltean98559342019-11-09 15:02:48 +0200340static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
341{
342 struct ocelot_port *ocelot_port = netdev_priv(dev);
343 struct ocelot *ocelot = ocelot_port->ocelot;
344 int port = ocelot_port->chip_port;
345 int ret;
346
347 /* 8021q removes VID 0 on module unload for all interfaces
348 * with VLAN filtering feature. We need to keep it to receive
349 * untagged traffic.
350 */
351 if (vid == 0)
352 return 0;
353
354 ret = ocelot_vlan_del(ocelot, port, vid);
355 if (ret)
356 return ret;
357
358 /* Del the port MAC address to with the right VLAN information */
359 ocelot_mact_forget(ocelot, dev->dev_addr, vid);
360
361 return 0;
362}
363
Alexandre Bellonia556c762018-05-14 22:04:57 +0200364static void ocelot_vlan_init(struct ocelot *ocelot)
365{
Antoine Tenart71425292018-06-26 14:28:49 +0200366 u16 port, vid;
367
Alexandre Bellonia556c762018-05-14 22:04:57 +0200368 /* Clear VLAN table, by default all ports are members of all VLANs */
369 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
370 ANA_TABLES_VLANACCESS);
371 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200372
373 /* Configure the port VLAN memberships */
374 for (vid = 1; vid < VLAN_N_VID; vid++) {
375 ocelot->vlan_mask[vid] = 0;
376 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
377 }
378
379 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
380 * traffic. It is added automatically if 8021q module is loaded, but
381 * we can't rely on it since module may be not loaded.
382 */
383 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
384 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
385
386 /* Configure the CPU port to be VLAN aware */
387 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
388 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
389 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
390 ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
391
392 /* Set vlan ingress filter mask to all ports but the CPU port by
393 * default.
394 */
395 ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
396
397 for (port = 0; port < ocelot->num_phys_ports; port++) {
398 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
399 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
400 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200401}
402
403/* Watermark encode
404 * Bit 8: Unit; 0:1, 1:16
405 * Bit 7-0: Value to be multiplied with unit
406 */
407static u16 ocelot_wm_enc(u16 value)
408{
409 if (value >= BIT(8))
410 return BIT(8) | (value / 16);
411
412 return value;
413}
414
415static void ocelot_port_adjust_link(struct net_device *dev)
416{
417 struct ocelot_port *port = netdev_priv(dev);
418 struct ocelot *ocelot = port->ocelot;
419 u8 p = port->chip_port;
420 int speed, atop_wm, mode = 0;
421
422 switch (dev->phydev->speed) {
423 case SPEED_10:
424 speed = OCELOT_SPEED_10;
425 break;
426 case SPEED_100:
427 speed = OCELOT_SPEED_100;
428 break;
429 case SPEED_1000:
430 speed = OCELOT_SPEED_1000;
431 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
432 break;
433 case SPEED_2500:
434 speed = OCELOT_SPEED_2500;
435 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
436 break;
437 default:
438 netdev_err(dev, "Unsupported PHY speed: %d\n",
439 dev->phydev->speed);
440 return;
441 }
442
443 phy_print_status(dev->phydev);
444
445 if (!dev->phydev->link)
446 return;
447
448 /* Only full duplex supported for now */
449 ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
450 mode, DEV_MAC_MODE_CFG);
451
452 /* Set MAC IFG Gaps
453 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
454 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
455 */
456 ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
457
458 /* Load seed (0) and set MAC HDX late collision */
459 ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
460 DEV_MAC_HDX_CFG_SEED_LOAD,
461 DEV_MAC_HDX_CFG);
462 mdelay(1);
463 ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
464 DEV_MAC_HDX_CFG);
465
466 /* Disable HDX fast control */
467 ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
468
469 /* SGMII only for now */
470 ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
471 ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
472
473 /* Enable PCS */
474 ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
475
476 /* No aneg on SGMII */
477 ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
478
479 /* No loopback */
480 ocelot_port_writel(port, 0, PCS1G_LB_CFG);
481
482 /* Set Max Length and maximum tags allowed */
483 ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
484 ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
485 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
486 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
487 DEV_MAC_TAGS_CFG);
488
489 /* Enable MAC module */
490 ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
491 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
492
493 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
494 * reset */
495 ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
496 DEV_CLOCK_CFG);
497
498 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
499 ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
500 ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
501
502 /* No PFC */
503 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
504 ANA_PFC_PFC_CFG, p);
505
506 /* Set Pause WM hysteresis
507 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
508 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
509 */
510 ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
511 SYS_PAUSE_CFG_PAUSE_STOP(101) |
512 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
513
514 /* Core: Enable port for frame transfer */
515 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
516 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
517 QSYS_SWITCH_PORT_MODE_PORT_ENA,
518 QSYS_SWITCH_PORT_MODE, p);
519
520 /* Flow control */
521 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
522 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
523 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
524 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
525 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
526 SYS_MAC_FC_CFG, p);
527 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
528
529 /* Tail dropping watermark */
530 atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
531 ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
532 SYS_ATOP, p);
533 ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
534}
535
536static int ocelot_port_open(struct net_device *dev)
537{
538 struct ocelot_port *port = netdev_priv(dev);
539 struct ocelot *ocelot = port->ocelot;
540 int err;
541
542 /* Enable receiving frames on the port, and activate auto-learning of
543 * MAC addresses.
544 */
545 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
546 ANA_PORT_PORT_CFG_RECV_ENA |
547 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
548 ANA_PORT_PORT_CFG, port->chip_port);
549
Quentin Schulz71e32a202018-10-04 14:22:08 +0200550 if (port->serdes) {
Grygorii Strashkoc8fe6d72018-11-19 19:24:22 -0600551 err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET,
552 port->phy_mode);
Quentin Schulz71e32a202018-10-04 14:22:08 +0200553 if (err) {
554 netdev_err(dev, "Could not set mode of SerDes\n");
555 return err;
556 }
557 }
558
Alexandre Bellonia556c762018-05-14 22:04:57 +0200559 err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
Quentin Schulz71e32a202018-10-04 14:22:08 +0200560 port->phy_mode);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200561 if (err) {
562 netdev_err(dev, "Could not attach to PHY\n");
563 return err;
564 }
565
566 dev->phydev = port->phy;
567
568 phy_attached_info(port->phy);
569 phy_start(port->phy);
570 return 0;
571}
572
573static int ocelot_port_stop(struct net_device *dev)
574{
575 struct ocelot_port *port = netdev_priv(dev);
576
577 phy_disconnect(port->phy);
578
579 dev->phydev = NULL;
580
581 ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
582 ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
583 QSYS_SWITCH_PORT_MODE, port->chip_port);
584 return 0;
585}
586
587/* Generate the IFH for frame injection
588 *
589 * The IFH is a 128bit-value
590 * bit 127: bypass the analyzer processing
591 * bit 56-67: destination mask
592 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
593 * bit 20-27: cpu extraction queue mask
594 * bit 16: tag type 0: C-tag, 1: S-tag
595 * bit 0-11: VID
596 */
597static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
598{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200599 ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
Antoine Tenart08d02362018-06-20 10:50:46 +0200600 ifh[1] = (0xf00 & info->port) >> 8;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200601 ifh[2] = (0xff & info->port) << 24;
Antoine Tenart08d02362018-06-20 10:50:46 +0200602 ifh[3] = (info->tag_type << 16) | info->vid;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200603
604 return 0;
605}
606
607static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
608{
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200609 struct skb_shared_info *shinfo = skb_shinfo(skb);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200610 struct ocelot_port *port = netdev_priv(dev);
611 struct ocelot *ocelot = port->ocelot;
612 u32 val, ifh[IFH_LEN];
613 struct frame_info info = {};
614 u8 grp = 0; /* Send everything on CPU group 0 */
615 unsigned int i, count, last;
616
617 val = ocelot_read(ocelot, QS_INJ_STATUS);
618 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
619 (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
620 return NETDEV_TX_BUSY;
621
622 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
623 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
624
625 info.port = BIT(port->chip_port);
Antoine Tenart08d02362018-06-20 10:50:46 +0200626 info.tag_type = IFH_TAG_TYPE_C;
627 info.vid = skb_vlan_tag_get(skb);
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200628
629 /* Check if timestamping is needed */
630 if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
631 info.rew_op = port->ptp_cmd;
632 if (port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
633 info.rew_op |= (port->ts_id % 4) << 3;
634 }
635
Alexandre Bellonia556c762018-05-14 22:04:57 +0200636 ocelot_gen_ifh(ifh, &info);
637
638 for (i = 0; i < IFH_LEN; i++)
Antoine Tenartc2cd6502018-06-22 11:50:52 +0200639 ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
640 QS_INJ_WR, grp);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200641
642 count = (skb->len + 3) / 4;
643 last = skb->len % 4;
644 for (i = 0; i < count; i++) {
645 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
646 }
647
648 /* Add padding */
649 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
650 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
651 i++;
652 }
653
654 /* Indicate EOF and valid bytes in last word */
655 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
656 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
657 QS_INJ_CTRL_EOF,
658 QS_INJ_CTRL, grp);
659
660 /* Add dummy CRC */
661 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
662 skb_tx_timestamp(skb);
663
664 dev->stats.tx_packets++;
665 dev->stats.tx_bytes += skb->len;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200666
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200667 if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
668 port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
669 struct ocelot_skb *oskb =
670 kzalloc(sizeof(struct ocelot_skb), GFP_ATOMIC);
671
672 if (unlikely(!oskb))
673 goto out;
674
675 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
676
677 oskb->skb = skb;
678 oskb->id = port->ts_id % 4;
679 port->ts_id++;
680
681 list_add_tail(&oskb->head, &port->skbs);
682
683 return NETDEV_TX_OK;
684 }
685
686out:
687 dev_kfree_skb_any(skb);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200688 return NETDEV_TX_OK;
689}
690
Antoine Tenart4e3b0462019-08-12 16:45:37 +0200691void ocelot_get_hwtimestamp(struct ocelot *ocelot, struct timespec64 *ts)
692{
693 unsigned long flags;
694 u32 val;
695
696 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
697
698 /* Read current PTP time to get seconds */
699 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
700
701 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
702 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
703 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
704 ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
705
706 /* Read packet HW timestamp from FIFO */
707 val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
708 ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
709
710 /* Sec has incremented since the ts was registered */
711 if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
712 ts->tv_sec--;
713
714 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
715}
716EXPORT_SYMBOL(ocelot_get_hwtimestamp);
717
Claudiu Manoil40a15782019-05-21 19:52:55 +0300718static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200719{
Claudiu Manoil40a15782019-05-21 19:52:55 +0300720 struct ocelot_port *port = netdev_priv(dev);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200721
Claudiu Manoil40a15782019-05-21 19:52:55 +0300722 return ocelot_mact_forget(port->ocelot, addr, port->pvid);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200723}
724
Claudiu Manoil40a15782019-05-21 19:52:55 +0300725static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200726{
Claudiu Manoil40a15782019-05-21 19:52:55 +0300727 struct ocelot_port *port = netdev_priv(dev);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200728
Claudiu Manoil40a15782019-05-21 19:52:55 +0300729 return ocelot_mact_learn(port->ocelot, PGID_CPU, addr, port->pvid,
730 ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200731}
732
733static void ocelot_set_rx_mode(struct net_device *dev)
734{
735 struct ocelot_port *port = netdev_priv(dev);
736 struct ocelot *ocelot = port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200737 int i;
738 u32 val;
739
740 /* This doesn't handle promiscuous mode because the bridge core is
741 * setting IFF_PROMISC on all slave interfaces and all frames would be
742 * forwarded to the CPU port.
743 */
744 val = GENMASK(ocelot->num_phys_ports - 1, 0);
745 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
746 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
747
Claudiu Manoil40a15782019-05-21 19:52:55 +0300748 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200749}
750
751static int ocelot_port_get_phys_port_name(struct net_device *dev,
752 char *buf, size_t len)
753{
754 struct ocelot_port *port = netdev_priv(dev);
755 int ret;
756
757 ret = snprintf(buf, len, "p%d", port->chip_port);
758 if (ret >= len)
759 return -EINVAL;
760
761 return 0;
762}
763
764static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
765{
766 struct ocelot_port *port = netdev_priv(dev);
767 struct ocelot *ocelot = port->ocelot;
768 const struct sockaddr *addr = p;
769
770 /* Learn the new net device MAC address in the mac table. */
771 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
772 ENTRYTYPE_LOCKED);
773 /* Then forget the previous one. */
774 ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
775
776 ether_addr_copy(dev->dev_addr, addr->sa_data);
777 return 0;
778}
779
780static void ocelot_get_stats64(struct net_device *dev,
781 struct rtnl_link_stats64 *stats)
782{
783 struct ocelot_port *port = netdev_priv(dev);
784 struct ocelot *ocelot = port->ocelot;
785
786 /* Configure the port to read the stats from */
787 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
788 SYS_STAT_CFG);
789
790 /* Get Rx stats */
791 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
792 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
793 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
794 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
795 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
796 ocelot_read(ocelot, SYS_COUNT_RX_64) +
797 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
798 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
799 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
800 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
801 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
802 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
803 stats->rx_dropped = dev->stats.rx_dropped;
804
805 /* Get Tx stats */
806 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
807 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
808 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
809 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
810 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
811 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
812 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
813 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
814 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
815 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
816}
817
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200818static int ocelot_fdb_add(struct ocelot *ocelot, int port,
819 const unsigned char *addr, u16 vid)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200820{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200821 struct ocelot_port *ocelot_port = ocelot->ports[port];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200822
Antoine Tenart71425292018-06-26 14:28:49 +0200823 if (!vid) {
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200824 if (!ocelot_port->vlan_aware)
Antoine Tenart71425292018-06-26 14:28:49 +0200825 /* If the bridge is not VLAN aware and no VID was
826 * provided, set it to pvid to ensure the MAC entry
827 * matches incoming untagged packets
828 */
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200829 vid = ocelot_port->pvid;
Antoine Tenart71425292018-06-26 14:28:49 +0200830 else
831 /* If the bridge is VLAN aware a VID must be provided as
832 * otherwise the learnt entry wouldn't match any frame.
833 */
834 return -EINVAL;
835 }
836
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200837 return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200838}
839
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200840static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
841 struct net_device *dev,
842 const unsigned char *addr,
843 u16 vid, u16 flags,
844 struct netlink_ext_ack *extack)
845{
846 struct ocelot_port *ocelot_port = netdev_priv(dev);
847 struct ocelot *ocelot = ocelot_port->ocelot;
848
849 return ocelot_fdb_add(ocelot, ocelot_port->chip_port, addr, vid);
850}
851
852static int ocelot_fdb_del(struct ocelot *ocelot, int port,
Alexandre Bellonia556c762018-05-14 22:04:57 +0200853 const unsigned char *addr, u16 vid)
854{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200855 return ocelot_mact_forget(ocelot, addr, vid);
856}
857
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200858static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
859 struct net_device *dev,
860 const unsigned char *addr, u16 vid)
861{
862 struct ocelot_port *ocelot_port = netdev_priv(dev);
863 struct ocelot *ocelot = ocelot_port->ocelot;
864
865 return ocelot_fdb_del(ocelot, ocelot_port->chip_port, addr, vid);
866}
867
Alexandre Bellonia556c762018-05-14 22:04:57 +0200868struct ocelot_dump_ctx {
869 struct net_device *dev;
870 struct sk_buff *skb;
871 struct netlink_callback *cb;
872 int idx;
873};
874
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200875static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
876 bool is_static, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200877{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200878 struct ocelot_dump_ctx *dump = data;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200879 u32 portid = NETLINK_CB(dump->cb->skb).portid;
880 u32 seq = dump->cb->nlh->nlmsg_seq;
881 struct nlmsghdr *nlh;
882 struct ndmsg *ndm;
883
884 if (dump->idx < dump->cb->args[2])
885 goto skip;
886
887 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
888 sizeof(*ndm), NLM_F_MULTI);
889 if (!nlh)
890 return -EMSGSIZE;
891
892 ndm = nlmsg_data(nlh);
893 ndm->ndm_family = AF_BRIDGE;
894 ndm->ndm_pad1 = 0;
895 ndm->ndm_pad2 = 0;
896 ndm->ndm_flags = NTF_SELF;
897 ndm->ndm_type = 0;
898 ndm->ndm_ifindex = dump->dev->ifindex;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200899 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200900
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200901 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200902 goto nla_put_failure;
903
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200904 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
Alexandre Bellonia556c762018-05-14 22:04:57 +0200905 goto nla_put_failure;
906
907 nlmsg_end(dump->skb, nlh);
908
909skip:
910 dump->idx++;
911 return 0;
912
913nla_put_failure:
914 nlmsg_cancel(dump->skb, nlh);
915 return -EMSGSIZE;
916}
917
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200918static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
919 struct ocelot_mact_entry *entry)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200920{
Alexandre Bellonia556c762018-05-14 22:04:57 +0200921 u32 val, dst, macl, mach;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200922 char mac[ETH_ALEN];
Alexandre Bellonia556c762018-05-14 22:04:57 +0200923
924 /* Set row and column to read from */
925 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
926 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
927
928 /* Issue a read command */
929 ocelot_write(ocelot,
930 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
931 ANA_TABLES_MACACCESS);
932
933 if (ocelot_mact_wait_for_completion(ocelot))
934 return -ETIMEDOUT;
935
936 /* Read the entry flags */
937 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
938 if (!(val & ANA_TABLES_MACACCESS_VALID))
939 return -EINVAL;
940
941 /* If the entry read has another port configured as its destination,
942 * do not report it.
943 */
944 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200945 if (dst != port)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200946 return -EINVAL;
947
948 /* Get the entry's MAC address and VLAN id */
949 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
950 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
951
952 mac[0] = (mach >> 8) & 0xff;
953 mac[1] = (mach >> 0) & 0xff;
954 mac[2] = (macl >> 24) & 0xff;
955 mac[3] = (macl >> 16) & 0xff;
956 mac[4] = (macl >> 8) & 0xff;
957 mac[5] = (macl >> 0) & 0xff;
958
959 entry->vid = (mach >> 16) & 0xfff;
960 ether_addr_copy(entry->mac, mac);
961
962 return 0;
963}
964
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200965static int ocelot_fdb_dump(struct ocelot *ocelot, int port,
966 dsa_fdb_dump_cb_t *cb, void *data)
Alexandre Bellonia556c762018-05-14 22:04:57 +0200967{
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200968 int i, j;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200969
970 /* Loop through all the mac tables entries. There are 1024 rows of 4
971 * entries.
972 */
973 for (i = 0; i < 1024; i++) {
974 for (j = 0; j < 4; j++) {
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200975 struct ocelot_mact_entry entry;
976 bool is_static;
977 int ret;
978
979 ret = ocelot_mact_read(ocelot, port, i, j, &entry);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200980 /* If the entry is invalid (wrong port, invalid...),
981 * skip it.
982 */
983 if (ret == -EINVAL)
984 continue;
985 else if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200986 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200987
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200988 is_static = (entry.type == ENTRYTYPE_LOCKED);
989
990 ret = cb(entry.mac, entry.vid, is_static, data);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200991 if (ret)
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200992 return ret;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200993 }
994 }
995
Vladimir Oltean531ee1a2019-11-09 15:02:49 +0200996 return 0;
997}
998
999static int ocelot_port_fdb_dump(struct sk_buff *skb,
1000 struct netlink_callback *cb,
1001 struct net_device *dev,
1002 struct net_device *filter_dev, int *idx)
1003{
1004 struct ocelot_port *ocelot_port = netdev_priv(dev);
1005 struct ocelot *ocelot = ocelot_port->ocelot;
1006 struct ocelot_dump_ctx dump = {
1007 .dev = dev,
1008 .skb = skb,
1009 .cb = cb,
1010 .idx = *idx,
1011 };
1012 int ret;
1013
1014 ret = ocelot_fdb_dump(ocelot, ocelot_port->chip_port,
1015 ocelot_port_fdb_do_dump, &dump);
1016
Alexandre Bellonia556c762018-05-14 22:04:57 +02001017 *idx = dump.idx;
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001018
Alexandre Bellonia556c762018-05-14 22:04:57 +02001019 return ret;
1020}
1021
Antoine Tenart71425292018-06-26 14:28:49 +02001022static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1023 u16 vid)
1024{
Vladimir Oltean1c44ce52019-10-26 21:04:26 +03001025 return ocelot_vlan_vid_add(dev, vid, false, false);
Antoine Tenart71425292018-06-26 14:28:49 +02001026}
1027
1028static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1029 u16 vid)
1030{
1031 return ocelot_vlan_vid_del(dev, vid);
1032}
1033
1034static int ocelot_set_features(struct net_device *dev,
1035 netdev_features_t features)
1036{
1037 struct ocelot_port *port = netdev_priv(dev);
1038 netdev_features_t changed = dev->features ^ features;
1039
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001040 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
1041 port->tc.offload_cnt) {
1042 netdev_err(dev,
1043 "Cannot disable HW TC offload while offloads active\n");
1044 return -EBUSY;
1045 }
1046
Antoine Tenart71425292018-06-26 14:28:49 +02001047 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
1048 ocelot_vlan_mode(port, features);
1049
1050 return 0;
1051}
1052
Florian Fainelli751302c2019-02-06 09:45:40 -08001053static int ocelot_get_port_parent_id(struct net_device *dev,
1054 struct netdev_phys_item_id *ppid)
1055{
1056 struct ocelot_port *ocelot_port = netdev_priv(dev);
1057 struct ocelot *ocelot = ocelot_port->ocelot;
1058
1059 ppid->id_len = sizeof(ocelot->base_mac);
1060 memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
1061
1062 return 0;
1063}
1064
Vladimir Oltean306fd442019-11-09 15:02:50 +02001065static int ocelot_hwstamp_get(struct ocelot *ocelot, int port,
1066 struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001067{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001068 return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1069 sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1070}
1071
Vladimir Oltean306fd442019-11-09 15:02:50 +02001072static int ocelot_hwstamp_set(struct ocelot *ocelot, int port,
1073 struct ifreq *ifr)
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001074{
Vladimir Oltean306fd442019-11-09 15:02:50 +02001075 struct ocelot_port *ocelot_port = ocelot->ports[port];
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001076 struct hwtstamp_config cfg;
1077
1078 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1079 return -EFAULT;
1080
1081 /* reserved for future extensions */
1082 if (cfg.flags)
1083 return -EINVAL;
1084
1085 /* Tx type sanity check */
1086 switch (cfg.tx_type) {
1087 case HWTSTAMP_TX_ON:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001088 ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001089 break;
1090 case HWTSTAMP_TX_ONESTEP_SYNC:
1091 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1092 * need to update the origin time.
1093 */
Vladimir Oltean306fd442019-11-09 15:02:50 +02001094 ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001095 break;
1096 case HWTSTAMP_TX_OFF:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001097 ocelot_port->ptp_cmd = 0;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001098 break;
1099 default:
1100 return -ERANGE;
1101 }
1102
1103 mutex_lock(&ocelot->ptp_lock);
1104
1105 switch (cfg.rx_filter) {
1106 case HWTSTAMP_FILTER_NONE:
1107 break;
1108 case HWTSTAMP_FILTER_ALL:
1109 case HWTSTAMP_FILTER_SOME:
1110 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1111 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1112 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1113 case HWTSTAMP_FILTER_NTP_ALL:
1114 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1115 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1116 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1117 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1118 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1119 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1120 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1121 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1122 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1123 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1124 break;
1125 default:
1126 mutex_unlock(&ocelot->ptp_lock);
1127 return -ERANGE;
1128 }
1129
1130 /* Commit back the result & save it */
1131 memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1132 mutex_unlock(&ocelot->ptp_lock);
1133
1134 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1135}
1136
1137static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1138{
Vladimir Oltean306fd442019-11-09 15:02:50 +02001139 struct ocelot_port *ocelot_port = netdev_priv(dev);
1140 struct ocelot *ocelot = ocelot_port->ocelot;
1141 int port = ocelot_port->chip_port;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001142
1143 /* The function is only used for PTP operations for now */
1144 if (!ocelot->ptp)
1145 return -EOPNOTSUPP;
1146
1147 switch (cmd) {
1148 case SIOCSHWTSTAMP:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001149 return ocelot_hwstamp_set(ocelot, port, ifr);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001150 case SIOCGHWTSTAMP:
Vladimir Oltean306fd442019-11-09 15:02:50 +02001151 return ocelot_hwstamp_get(ocelot, port, ifr);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001152 default:
1153 return -EOPNOTSUPP;
1154 }
1155}
1156
Alexandre Bellonia556c762018-05-14 22:04:57 +02001157static const struct net_device_ops ocelot_port_netdev_ops = {
1158 .ndo_open = ocelot_port_open,
1159 .ndo_stop = ocelot_port_stop,
1160 .ndo_start_xmit = ocelot_port_xmit,
1161 .ndo_set_rx_mode = ocelot_set_rx_mode,
1162 .ndo_get_phys_port_name = ocelot_port_get_phys_port_name,
1163 .ndo_set_mac_address = ocelot_port_set_mac_address,
1164 .ndo_get_stats64 = ocelot_get_stats64,
Vladimir Oltean531ee1a2019-11-09 15:02:49 +02001165 .ndo_fdb_add = ocelot_port_fdb_add,
1166 .ndo_fdb_del = ocelot_port_fdb_del,
1167 .ndo_fdb_dump = ocelot_port_fdb_dump,
Antoine Tenart71425292018-06-26 14:28:49 +02001168 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
1169 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
1170 .ndo_set_features = ocelot_set_features,
Florian Fainelli751302c2019-02-06 09:45:40 -08001171 .ndo_get_port_parent_id = ocelot_get_port_parent_id,
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02001172 .ndo_setup_tc = ocelot_setup_tc,
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001173 .ndo_do_ioctl = ocelot_ioctl,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001174};
1175
1176static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
1177{
1178 struct ocelot_port *port = netdev_priv(netdev);
1179 struct ocelot *ocelot = port->ocelot;
1180 int i;
1181
1182 if (sset != ETH_SS_STATS)
1183 return;
1184
1185 for (i = 0; i < ocelot->num_stats; i++)
1186 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1187 ETH_GSTRING_LEN);
1188}
1189
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001190static void ocelot_update_stats(struct ocelot *ocelot)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001191{
Alexandre Bellonia556c762018-05-14 22:04:57 +02001192 int i, j;
1193
1194 mutex_lock(&ocelot->stats_lock);
1195
1196 for (i = 0; i < ocelot->num_phys_ports; i++) {
1197 /* Configure the port to read the stats from */
1198 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1199
1200 for (j = 0; j < ocelot->num_stats; j++) {
1201 u32 val;
1202 unsigned int idx = i * ocelot->num_stats + j;
1203
1204 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1205 ocelot->stats_layout[j].offset);
1206
1207 if (val < (ocelot->stats[idx] & U32_MAX))
1208 ocelot->stats[idx] += (u64)1 << 32;
1209
1210 ocelot->stats[idx] = (ocelot->stats[idx] &
1211 ~(u64)U32_MAX) + val;
1212 }
1213 }
1214
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001215 mutex_unlock(&ocelot->stats_lock);
1216}
1217
1218static void ocelot_check_stats_work(struct work_struct *work)
1219{
1220 struct delayed_work *del_work = to_delayed_work(work);
1221 struct ocelot *ocelot = container_of(del_work, struct ocelot,
1222 stats_work);
1223
1224 ocelot_update_stats(ocelot);
1225
Alexandre Bellonia556c762018-05-14 22:04:57 +02001226 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1227 OCELOT_STATS_CHECK_DELAY);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001228}
1229
1230static void ocelot_get_ethtool_stats(struct net_device *dev,
1231 struct ethtool_stats *stats, u64 *data)
1232{
1233 struct ocelot_port *port = netdev_priv(dev);
1234 struct ocelot *ocelot = port->ocelot;
1235 int i;
1236
1237 /* check and update now */
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03001238 ocelot_update_stats(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001239
1240 /* Copy all counters */
1241 for (i = 0; i < ocelot->num_stats; i++)
1242 *data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
1243}
1244
1245static int ocelot_get_sset_count(struct net_device *dev, int sset)
1246{
1247 struct ocelot_port *port = netdev_priv(dev);
1248 struct ocelot *ocelot = port->ocelot;
1249
1250 if (sset != ETH_SS_STATS)
1251 return -EOPNOTSUPP;
1252 return ocelot->num_stats;
1253}
1254
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001255static int ocelot_get_ts_info(struct net_device *dev,
1256 struct ethtool_ts_info *info)
1257{
1258 struct ocelot_port *ocelot_port = netdev_priv(dev);
1259 struct ocelot *ocelot = ocelot_port->ocelot;
1260
1261 if (!ocelot->ptp)
1262 return ethtool_op_get_ts_info(dev, info);
1263
1264 info->phc_index = ocelot->ptp_clock ?
1265 ptp_clock_index(ocelot->ptp_clock) : -1;
1266 info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1267 SOF_TIMESTAMPING_RX_SOFTWARE |
1268 SOF_TIMESTAMPING_SOFTWARE |
1269 SOF_TIMESTAMPING_TX_HARDWARE |
1270 SOF_TIMESTAMPING_RX_HARDWARE |
1271 SOF_TIMESTAMPING_RAW_HARDWARE;
1272 info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1273 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
1274 info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
1275
1276 return 0;
1277}
1278
Alexandre Bellonia556c762018-05-14 22:04:57 +02001279static const struct ethtool_ops ocelot_ethtool_ops = {
1280 .get_strings = ocelot_get_strings,
1281 .get_ethtool_stats = ocelot_get_ethtool_stats,
1282 .get_sset_count = ocelot_get_sset_count,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001283 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1284 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001285 .get_ts_info = ocelot_get_ts_info,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001286};
1287
Alexandre Bellonia556c762018-05-14 22:04:57 +02001288static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
1289 struct switchdev_trans *trans,
1290 u8 state)
1291{
1292 struct ocelot *ocelot = ocelot_port->ocelot;
1293 u32 port_cfg;
1294 int port, i;
1295
1296 if (switchdev_trans_ph_prepare(trans))
1297 return 0;
1298
1299 if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
1300 return 0;
1301
1302 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
1303 ocelot_port->chip_port);
1304
1305 switch (state) {
1306 case BR_STATE_FORWARDING:
1307 ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
1308 /* Fallthrough */
1309 case BR_STATE_LEARNING:
1310 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1311 break;
1312
1313 default:
1314 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1315 ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
1316 break;
1317 }
1318
1319 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
1320 ocelot_port->chip_port);
1321
1322 /* Apply FWD mask. The loop is needed to add/remove the current port as
1323 * a source for the other ports.
1324 */
1325 for (port = 0; port < ocelot->num_phys_ports; port++) {
1326 if (ocelot->bridge_fwd_mask & BIT(port)) {
1327 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
1328
1329 for (i = 0; i < ocelot->num_phys_ports; i++) {
1330 unsigned long bond_mask = ocelot->lags[i];
1331
1332 if (!bond_mask)
1333 continue;
1334
1335 if (bond_mask & BIT(port)) {
1336 mask &= ~bond_mask;
1337 break;
1338 }
1339 }
1340
1341 ocelot_write_rix(ocelot,
1342 BIT(ocelot->num_phys_ports) | mask,
1343 ANA_PGID_PGID, PGID_SRC + port);
1344 } else {
1345 /* Only the CPU port, this is compatible with link
1346 * aggregation.
1347 */
1348 ocelot_write_rix(ocelot,
1349 BIT(ocelot->num_phys_ports),
1350 ANA_PGID_PGID, PGID_SRC + port);
1351 }
1352 }
1353
1354 return 0;
1355}
1356
1357static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
1358 unsigned long ageing_clock_t)
1359{
1360 struct ocelot *ocelot = ocelot_port->ocelot;
1361 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1362 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1363
1364 ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
1365 ANA_AUTOAGE);
1366}
1367
1368static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
1369{
1370 struct ocelot *ocelot = port->ocelot;
1371 u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
1372 port->chip_port);
1373
1374 if (mc)
1375 val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1376 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1377 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1378 else
1379 val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1380 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1381 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
1382
1383 ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
1384}
1385
1386static int ocelot_port_attr_set(struct net_device *dev,
1387 const struct switchdev_attr *attr,
1388 struct switchdev_trans *trans)
1389{
1390 struct ocelot_port *ocelot_port = netdev_priv(dev);
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001391 struct ocelot *ocelot = ocelot_port->ocelot;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001392 int err = 0;
1393
1394 switch (attr->id) {
1395 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1396 ocelot_port_attr_stp_state_set(ocelot_port, trans,
1397 attr->u.stp_state);
1398 break;
1399 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1400 ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
1401 break;
Antoine Tenart71425292018-06-26 14:28:49 +02001402 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001403 ocelot_port_vlan_filtering(ocelot, ocelot_port->chip_port,
1404 attr->u.vlan_filtering);
Antoine Tenart71425292018-06-26 14:28:49 +02001405 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001406 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1407 ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
1408 break;
1409 default:
1410 err = -EOPNOTSUPP;
1411 break;
1412 }
1413
1414 return err;
1415}
1416
Antoine Tenart71425292018-06-26 14:28:49 +02001417static int ocelot_port_obj_add_vlan(struct net_device *dev,
1418 const struct switchdev_obj_port_vlan *vlan,
1419 struct switchdev_trans *trans)
1420{
1421 int ret;
1422 u16 vid;
1423
1424 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1425 ret = ocelot_vlan_vid_add(dev, vid,
1426 vlan->flags & BRIDGE_VLAN_INFO_PVID,
1427 vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
1428 if (ret)
1429 return ret;
1430 }
1431
1432 return 0;
1433}
1434
1435static int ocelot_port_vlan_del_vlan(struct net_device *dev,
1436 const struct switchdev_obj_port_vlan *vlan)
1437{
1438 int ret;
1439 u16 vid;
1440
1441 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1442 ret = ocelot_vlan_vid_del(dev, vid);
1443
1444 if (ret)
1445 return ret;
1446 }
1447
1448 return 0;
1449}
1450
Alexandre Bellonia556c762018-05-14 22:04:57 +02001451static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1452 const unsigned char *addr,
1453 u16 vid)
1454{
1455 struct ocelot_multicast *mc;
1456
1457 list_for_each_entry(mc, &ocelot->multicast, list) {
1458 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1459 return mc;
1460 }
1461
1462 return NULL;
1463}
1464
1465static int ocelot_port_obj_add_mdb(struct net_device *dev,
1466 const struct switchdev_obj_port_mdb *mdb,
1467 struct switchdev_trans *trans)
1468{
1469 struct ocelot_port *port = netdev_priv(dev);
1470 struct ocelot *ocelot = port->ocelot;
1471 struct ocelot_multicast *mc;
1472 unsigned char addr[ETH_ALEN];
1473 u16 vid = mdb->vid;
1474 bool new = false;
1475
1476 if (!vid)
Antoine Tenart71425292018-06-26 14:28:49 +02001477 vid = port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001478
1479 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1480 if (!mc) {
1481 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1482 if (!mc)
1483 return -ENOMEM;
1484
1485 memcpy(mc->addr, mdb->addr, ETH_ALEN);
1486 mc->vid = vid;
1487
1488 list_add_tail(&mc->list, &ocelot->multicast);
1489 new = true;
1490 }
1491
1492 memcpy(addr, mc->addr, ETH_ALEN);
1493 addr[0] = 0;
1494
1495 if (!new) {
1496 addr[2] = mc->ports << 0;
1497 addr[1] = mc->ports << 8;
1498 ocelot_mact_forget(ocelot, addr, vid);
1499 }
1500
1501 mc->ports |= BIT(port->chip_port);
1502 addr[2] = mc->ports << 0;
1503 addr[1] = mc->ports << 8;
1504
1505 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1506}
1507
1508static int ocelot_port_obj_del_mdb(struct net_device *dev,
1509 const struct switchdev_obj_port_mdb *mdb)
1510{
1511 struct ocelot_port *port = netdev_priv(dev);
1512 struct ocelot *ocelot = port->ocelot;
1513 struct ocelot_multicast *mc;
1514 unsigned char addr[ETH_ALEN];
1515 u16 vid = mdb->vid;
1516
1517 if (!vid)
Antoine Tenart71425292018-06-26 14:28:49 +02001518 vid = port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001519
1520 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1521 if (!mc)
1522 return -ENOENT;
1523
1524 memcpy(addr, mc->addr, ETH_ALEN);
1525 addr[2] = mc->ports << 0;
1526 addr[1] = mc->ports << 8;
1527 addr[0] = 0;
1528 ocelot_mact_forget(ocelot, addr, vid);
1529
1530 mc->ports &= ~BIT(port->chip_port);
1531 if (!mc->ports) {
1532 list_del(&mc->list);
1533 devm_kfree(ocelot->dev, mc);
1534 return 0;
1535 }
1536
1537 addr[2] = mc->ports << 0;
1538 addr[1] = mc->ports << 8;
1539
1540 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1541}
1542
1543static int ocelot_port_obj_add(struct net_device *dev,
1544 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +00001545 struct switchdev_trans *trans,
1546 struct netlink_ext_ack *extack)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001547{
1548 int ret = 0;
1549
1550 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001551 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1552 ret = ocelot_port_obj_add_vlan(dev,
1553 SWITCHDEV_OBJ_PORT_VLAN(obj),
1554 trans);
1555 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001556 case SWITCHDEV_OBJ_ID_PORT_MDB:
1557 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1558 trans);
1559 break;
1560 default:
1561 return -EOPNOTSUPP;
1562 }
1563
1564 return ret;
1565}
1566
1567static int ocelot_port_obj_del(struct net_device *dev,
1568 const struct switchdev_obj *obj)
1569{
1570 int ret = 0;
1571
1572 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001573 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1574 ret = ocelot_port_vlan_del_vlan(dev,
1575 SWITCHDEV_OBJ_PORT_VLAN(obj));
1576 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001577 case SWITCHDEV_OBJ_ID_PORT_MDB:
1578 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1579 break;
1580 default:
1581 return -EOPNOTSUPP;
1582 }
1583
1584 return ret;
1585}
1586
Alexandre Bellonia556c762018-05-14 22:04:57 +02001587static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1588 struct net_device *bridge)
1589{
1590 struct ocelot *ocelot = ocelot_port->ocelot;
1591
1592 if (!ocelot->bridge_mask) {
1593 ocelot->hw_bridge_dev = bridge;
1594 } else {
1595 if (ocelot->hw_bridge_dev != bridge)
1596 /* This is adding the port to a second bridge, this is
1597 * unsupported */
1598 return -ENODEV;
1599 }
1600
1601 ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1602
1603 return 0;
1604}
1605
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001606static int ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1607 struct net_device *bridge)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001608{
1609 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001610 int port = ocelot_port->chip_port;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001611
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001612 ocelot->bridge_mask &= ~BIT(port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001613
1614 if (!ocelot->bridge_mask)
1615 ocelot->hw_bridge_dev = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02001616
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001617 ocelot_port_vlan_filtering(ocelot, port, 0);
1618 ocelot_port_set_pvid(ocelot, port, 0);
1619 return ocelot_port_set_native_vlan(ocelot, port, 0);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001620}
1621
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001622static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1623{
1624 int i, port, lag;
1625
1626 /* Reset destination and aggregation PGIDS */
1627 for (port = 0; port < ocelot->num_phys_ports; port++)
1628 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1629
1630 for (i = PGID_AGGR; i < PGID_SRC; i++)
1631 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1632 ANA_PGID_PGID, i);
1633
1634 /* Now, set PGIDs for each LAG */
1635 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1636 unsigned long bond_mask;
1637 int aggr_count = 0;
1638 u8 aggr_idx[16];
1639
1640 bond_mask = ocelot->lags[lag];
1641 if (!bond_mask)
1642 continue;
1643
1644 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1645 // Destination mask
1646 ocelot_write_rix(ocelot, bond_mask,
1647 ANA_PGID_PGID, port);
1648 aggr_idx[aggr_count] = port;
1649 aggr_count++;
1650 }
1651
1652 for (i = PGID_AGGR; i < PGID_SRC; i++) {
1653 u32 ac;
1654
1655 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1656 ac &= ~bond_mask;
1657 ac |= BIT(aggr_idx[i % aggr_count]);
1658 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1659 }
1660 }
1661}
1662
1663static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1664{
1665 unsigned long bond_mask = ocelot->lags[lag];
1666 unsigned int p;
1667
1668 for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1669 u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1670
1671 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1672
1673 /* Use lag port as logical port for port i */
1674 ocelot_write_gix(ocelot, port_cfg |
1675 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1676 ANA_PORT_PORT_CFG, p);
1677 }
1678}
1679
1680static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
1681 struct net_device *bond)
1682{
1683 struct ocelot *ocelot = ocelot_port->ocelot;
1684 int p = ocelot_port->chip_port;
1685 int lag, lp;
1686 struct net_device *ndev;
1687 u32 bond_mask = 0;
1688
1689 rcu_read_lock();
1690 for_each_netdev_in_bond_rcu(bond, ndev) {
1691 struct ocelot_port *port = netdev_priv(ndev);
1692
1693 bond_mask |= BIT(port->chip_port);
1694 }
1695 rcu_read_unlock();
1696
1697 lp = __ffs(bond_mask);
1698
1699 /* If the new port is the lowest one, use it as the logical port from
1700 * now on
1701 */
1702 if (p == lp) {
1703 lag = p;
1704 ocelot->lags[p] = bond_mask;
1705 bond_mask &= ~BIT(p);
1706 if (bond_mask) {
1707 lp = __ffs(bond_mask);
1708 ocelot->lags[lp] = 0;
1709 }
1710 } else {
1711 lag = lp;
1712 ocelot->lags[lp] |= BIT(p);
1713 }
1714
1715 ocelot_setup_lag(ocelot, lag);
1716 ocelot_set_aggr_pgids(ocelot);
1717
1718 return 0;
1719}
1720
1721static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
1722 struct net_device *bond)
1723{
1724 struct ocelot *ocelot = ocelot_port->ocelot;
1725 int p = ocelot_port->chip_port;
1726 u32 port_cfg;
1727 int i;
1728
1729 /* Remove port from any lag */
1730 for (i = 0; i < ocelot->num_phys_ports; i++)
1731 ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
1732
1733 /* if it was the logical port of the lag, move the lag config to the
1734 * next port
1735 */
1736 if (ocelot->lags[p]) {
1737 int n = __ffs(ocelot->lags[p]);
1738
1739 ocelot->lags[n] = ocelot->lags[p];
1740 ocelot->lags[p] = 0;
1741
1742 ocelot_setup_lag(ocelot, n);
1743 }
1744
1745 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1746 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1747 ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
1748 ANA_PORT_PORT_CFG, p);
1749
1750 ocelot_set_aggr_pgids(ocelot);
1751}
1752
Alexandre Bellonia556c762018-05-14 22:04:57 +02001753/* Checks if the net_device instance given to us originate from our driver. */
1754static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1755{
1756 return dev->netdev_ops == &ocelot_port_netdev_ops;
1757}
1758
1759static int ocelot_netdevice_port_event(struct net_device *dev,
1760 unsigned long event,
1761 struct netdev_notifier_changeupper_info *info)
1762{
1763 struct ocelot_port *ocelot_port = netdev_priv(dev);
1764 int err = 0;
1765
Alexandre Bellonia556c762018-05-14 22:04:57 +02001766 switch (event) {
1767 case NETDEV_CHANGEUPPER:
1768 if (netif_is_bridge_master(info->upper_dev)) {
1769 if (info->linking)
1770 err = ocelot_port_bridge_join(ocelot_port,
1771 info->upper_dev);
1772 else
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02001773 err = ocelot_port_bridge_leave(ocelot_port,
1774 info->upper_dev);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001775 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001776 if (netif_is_lag_master(info->upper_dev)) {
1777 if (info->linking)
1778 err = ocelot_port_lag_join(ocelot_port,
1779 info->upper_dev);
1780 else
1781 ocelot_port_lag_leave(ocelot_port,
1782 info->upper_dev);
1783 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001784 break;
1785 default:
1786 break;
1787 }
1788
1789 return err;
1790}
1791
1792static int ocelot_netdevice_event(struct notifier_block *unused,
1793 unsigned long event, void *ptr)
1794{
1795 struct netdev_notifier_changeupper_info *info = ptr;
1796 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Geert Uytterhoeven2ac0e152018-06-07 15:10:30 +02001797 int ret = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001798
Claudiu Manoil7afb3e52019-11-05 23:50:13 +02001799 if (!ocelot_netdevice_dev_check(dev))
1800 return 0;
1801
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001802 if (event == NETDEV_PRECHANGEUPPER &&
1803 netif_is_lag_master(info->upper_dev)) {
1804 struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1805 struct netlink_ext_ack *extack;
1806
Claudiu Manoil3b3eed82019-11-05 23:50:14 +02001807 if (lag_upper_info &&
1808 lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001809 extack = netdev_notifier_info_to_extack(&info->info);
1810 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1811
1812 ret = -EINVAL;
1813 goto notify;
1814 }
1815 }
1816
Alexandre Bellonia556c762018-05-14 22:04:57 +02001817 if (netif_is_lag_master(dev)) {
1818 struct net_device *slave;
1819 struct list_head *iter;
1820
1821 netdev_for_each_lower_dev(dev, slave, iter) {
1822 ret = ocelot_netdevice_port_event(slave, event, info);
1823 if (ret)
1824 goto notify;
1825 }
1826 } else {
1827 ret = ocelot_netdevice_port_event(dev, event, info);
1828 }
1829
1830notify:
1831 return notifier_from_errno(ret);
1832}
1833
1834struct notifier_block ocelot_netdevice_nb __read_mostly = {
1835 .notifier_call = ocelot_netdevice_event,
1836};
1837EXPORT_SYMBOL(ocelot_netdevice_nb);
1838
Florian Fainelli56da64b2019-02-27 11:44:29 -08001839static int ocelot_switchdev_event(struct notifier_block *unused,
1840 unsigned long event, void *ptr)
1841{
1842 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1843 int err;
1844
1845 switch (event) {
1846 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);
1851 }
1852
1853 return NOTIFY_DONE;
1854}
1855
1856struct notifier_block ocelot_switchdev_nb __read_mostly = {
1857 .notifier_call = ocelot_switchdev_event,
1858};
1859EXPORT_SYMBOL(ocelot_switchdev_nb);
1860
Petr Machata0e332c82018-11-22 23:30:11 +00001861static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1862 unsigned long event, void *ptr)
1863{
1864 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1865 int err;
1866
1867 switch (event) {
1868 /* Blocking events. */
1869 case SWITCHDEV_PORT_OBJ_ADD:
1870 err = switchdev_handle_port_obj_add(dev, ptr,
1871 ocelot_netdevice_dev_check,
1872 ocelot_port_obj_add);
1873 return notifier_from_errno(err);
1874 case SWITCHDEV_PORT_OBJ_DEL:
1875 err = switchdev_handle_port_obj_del(dev, ptr,
1876 ocelot_netdevice_dev_check,
1877 ocelot_port_obj_del);
1878 return notifier_from_errno(err);
Florian Fainelli56da64b2019-02-27 11:44:29 -08001879 case SWITCHDEV_PORT_ATTR_SET:
1880 err = switchdev_handle_port_attr_set(dev, ptr,
1881 ocelot_netdevice_dev_check,
1882 ocelot_port_attr_set);
1883 return notifier_from_errno(err);
Petr Machata0e332c82018-11-22 23:30:11 +00001884 }
1885
1886 return NOTIFY_DONE;
1887}
1888
1889struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1890 .notifier_call = ocelot_switchdev_blocking_event,
1891};
1892EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
1893
Antoine Tenart4e3b0462019-08-12 16:45:37 +02001894int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
1895{
1896 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1897 unsigned long flags;
1898 time64_t s;
1899 u32 val;
1900 s64 ns;
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_SAVE);
1907 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1908
1909 s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
1910 s <<= 32;
1911 s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
1912 ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
1913
1914 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
1915
1916 /* Deal with negative values */
1917 if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
1918 s--;
1919 ns &= 0xf;
1920 ns += 999999984;
1921 }
1922
1923 set_normalized_timespec64(ts, s, ns);
1924 return 0;
1925}
1926EXPORT_SYMBOL(ocelot_ptp_gettime64);
1927
1928static int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
1929 const struct timespec64 *ts)
1930{
1931 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1932 unsigned long flags;
1933 u32 val;
1934
1935 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
1936
1937 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1938 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1939 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
1940
1941 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1942
1943 ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
1944 TOD_ACC_PIN);
1945 ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
1946 TOD_ACC_PIN);
1947 ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
1948
1949 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1950 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1951 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
1952
1953 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1954
1955 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
1956 return 0;
1957}
1958
1959static int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
1960{
1961 if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
1962 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
1963 unsigned long flags;
1964 u32 val;
1965
1966 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
1967
1968 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1969 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1970 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
1971
1972 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1973
1974 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
1975 ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
1976 ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
1977
1978 val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
1979 val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
1980 val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
1981
1982 ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
1983
1984 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
1985 } else {
1986 /* Fall back using ocelot_ptp_settime64 which is not exact. */
1987 struct timespec64 ts;
1988 u64 now;
1989
1990 ocelot_ptp_gettime64(ptp, &ts);
1991
1992 now = ktime_to_ns(timespec64_to_ktime(ts));
1993 ts = ns_to_timespec64(now + delta);
1994
1995 ocelot_ptp_settime64(ptp, &ts);
1996 }
1997 return 0;
1998}
1999
2000static int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
2001{
2002 struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
2003 u32 unit = 0, direction = 0;
2004 unsigned long flags;
2005 u64 adj = 0;
2006
2007 spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
2008
2009 if (!scaled_ppm)
2010 goto disable_adj;
2011
2012 if (scaled_ppm < 0) {
2013 direction = PTP_CFG_CLK_ADJ_CFG_DIR;
2014 scaled_ppm = -scaled_ppm;
2015 }
2016
2017 adj = PSEC_PER_SEC << 16;
2018 do_div(adj, scaled_ppm);
2019 do_div(adj, 1000);
2020
2021 /* If the adjustment value is too large, use ns instead */
2022 if (adj >= (1L << 30)) {
2023 unit = PTP_CFG_CLK_ADJ_FREQ_NS;
2024 do_div(adj, 1000);
2025 }
2026
2027 /* Still too big */
2028 if (adj >= (1L << 30))
2029 goto disable_adj;
2030
2031 ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
2032 ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
2033 PTP_CLK_CFG_ADJ_CFG);
2034
2035 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2036 return 0;
2037
2038disable_adj:
2039 ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
2040
2041 spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
2042 return 0;
2043}
2044
2045static struct ptp_clock_info ocelot_ptp_clock_info = {
2046 .owner = THIS_MODULE,
2047 .name = "ocelot ptp",
2048 .max_adj = 0x7fffffff,
2049 .n_alarm = 0,
2050 .n_ext_ts = 0,
2051 .n_per_out = 0,
2052 .n_pins = 0,
2053 .pps = 0,
2054 .gettime64 = ocelot_ptp_gettime64,
2055 .settime64 = ocelot_ptp_settime64,
2056 .adjtime = ocelot_ptp_adjtime,
2057 .adjfine = ocelot_ptp_adjfine,
2058};
2059
2060static int ocelot_init_timestamp(struct ocelot *ocelot)
2061{
2062 ocelot->ptp_info = ocelot_ptp_clock_info;
2063 ocelot->ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
2064 if (IS_ERR(ocelot->ptp_clock))
2065 return PTR_ERR(ocelot->ptp_clock);
2066 /* Check if PHC support is missing at the configuration level */
2067 if (!ocelot->ptp_clock)
2068 return 0;
2069
2070 ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
2071 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
2072 ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
2073
2074 ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
2075
2076 /* There is no device reconfiguration, PTP Rx stamping is always
2077 * enabled.
2078 */
2079 ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
2080
2081 return 0;
2082}
2083
Alexandre Bellonia556c762018-05-14 22:04:57 +02002084int ocelot_probe_port(struct ocelot *ocelot, u8 port,
2085 void __iomem *regs,
2086 struct phy_device *phy)
2087{
2088 struct ocelot_port *ocelot_port;
2089 struct net_device *dev;
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02002090 u32 val;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002091 int err;
2092
2093 dev = alloc_etherdev(sizeof(struct ocelot_port));
2094 if (!dev)
2095 return -ENOMEM;
2096 SET_NETDEV_DEV(dev, ocelot->dev);
2097 ocelot_port = netdev_priv(dev);
2098 ocelot_port->dev = dev;
2099 ocelot_port->ocelot = ocelot;
2100 ocelot_port->regs = regs;
2101 ocelot_port->chip_port = port;
2102 ocelot_port->phy = phy;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002103 ocelot->ports[port] = ocelot_port;
2104
2105 dev->netdev_ops = &ocelot_port_netdev_ops;
2106 dev->ethtool_ops = &ocelot_ethtool_ops;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002107
Joergen Andreasen2c1d0292019-05-28 14:49:17 +02002108 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
2109 NETIF_F_HW_TC;
2110 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
Antoine Tenart71425292018-06-26 14:28:49 +02002111
Alexandre Bellonia556c762018-05-14 22:04:57 +02002112 memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2113 dev->dev_addr[ETH_ALEN - 1] += port;
2114 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2115 ENTRYTYPE_LOCKED);
2116
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002117 INIT_LIST_HEAD(&ocelot_port->skbs);
2118
Alexandre Bellonia556c762018-05-14 22:04:57 +02002119 err = register_netdev(dev);
2120 if (err) {
2121 dev_err(ocelot->dev, "register_netdev failed\n");
2122 goto err_register_netdev;
2123 }
2124
Antoine Tenart71425292018-06-26 14:28:49 +02002125 /* Basic L2 initialization */
Vladimir Oltean97bb69e2019-11-09 15:02:47 +02002126
2127 /* Drop frames with multicast source address */
2128 val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
2129 ocelot_rmw_gix(ocelot, val, val, ANA_PORT_DROP_CFG, port);
2130
2131 /* Set default VLAN and tag type to 8021Q. */
2132 ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2133 REW_PORT_VLAN_CFG_PORT_TPID_M,
2134 REW_PORT_VLAN_CFG, port);
Antoine Tenart71425292018-06-26 14:28:49 +02002135
Horatiu Vulturb5962292019-05-31 09:16:56 +02002136 /* Enable vcap lookups */
2137 ocelot_vcap_enable(ocelot, ocelot_port);
2138
Alexandre Bellonia556c762018-05-14 22:04:57 +02002139 return 0;
2140
2141err_register_netdev:
2142 free_netdev(dev);
2143 return err;
2144}
2145EXPORT_SYMBOL(ocelot_probe_port);
2146
2147int ocelot_init(struct ocelot *ocelot)
2148{
2149 u32 port;
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002150 int i, ret, cpu = ocelot->num_phys_ports;
Alexandre Bellonia556c762018-05-14 22:04:57 +02002151 char queue_name[32];
2152
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02002153 ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2154 sizeof(u32), GFP_KERNEL);
2155 if (!ocelot->lags)
2156 return -ENOMEM;
2157
Alexandre Bellonia556c762018-05-14 22:04:57 +02002158 ocelot->stats = devm_kcalloc(ocelot->dev,
2159 ocelot->num_phys_ports * ocelot->num_stats,
2160 sizeof(u64), GFP_KERNEL);
2161 if (!ocelot->stats)
2162 return -ENOMEM;
2163
2164 mutex_init(&ocelot->stats_lock);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002165 mutex_init(&ocelot->ptp_lock);
2166 spin_lock_init(&ocelot->ptp_clock_lock);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002167 snprintf(queue_name, sizeof(queue_name), "%s-stats",
2168 dev_name(ocelot->dev));
2169 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2170 if (!ocelot->stats_queue)
2171 return -ENOMEM;
2172
2173 ocelot_mact_init(ocelot);
2174 ocelot_vlan_init(ocelot);
Horatiu Vulturb5962292019-05-31 09:16:56 +02002175 ocelot_ace_init(ocelot);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002176
2177 for (port = 0; port < ocelot->num_phys_ports; port++) {
2178 /* Clear all counters (5 groups) */
2179 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2180 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2181 SYS_STAT_CFG);
2182 }
2183
2184 /* Only use S-Tag */
2185 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2186
2187 /* Aggregation mode */
2188 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2189 ANA_AGGR_CFG_AC_DMAC_ENA |
2190 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2191 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2192
2193 /* Set MAC age time to default value. The entry is aged after
2194 * 2*AGE_PERIOD
2195 */
2196 ocelot_write(ocelot,
2197 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2198 ANA_AUTOAGE);
2199
2200 /* Disable learning for frames discarded by VLAN ingress filtering */
2201 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2202
2203 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2204 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2205 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2206
2207 /* Setup flooding PGIDs */
2208 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2209 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2210 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2211 ANA_FLOODING, 0);
2212 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2213 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2214 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2215 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2216 ANA_FLOODING_IPMC);
2217
2218 for (port = 0; port < ocelot->num_phys_ports; port++) {
2219 /* Transmit the frame to the local port. */
2220 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2221 /* Do not forward BPDU frames to the front ports. */
2222 ocelot_write_gix(ocelot,
2223 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2224 ANA_PORT_CPU_FWD_BPDU_CFG,
2225 port);
2226 /* Ensure bridging is disabled */
2227 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2228 }
2229
2230 /* Configure and enable the CPU port. */
2231 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2232 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2233 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2234 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2235 ANA_PORT_PORT_CFG, cpu);
2236
2237 /* Allow broadcast MAC frames. */
2238 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2239 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2240
2241 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2242 }
2243 ocelot_write_rix(ocelot,
2244 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2245 ANA_PGID_PGID, PGID_MC);
2246 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2247 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2248
2249 /* CPU port Injection/Extraction configuration */
2250 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2251 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2252 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2253 QSYS_SWITCH_PORT_MODE, cpu);
2254 ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
2255 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
2256 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2257 * registers endianness.
2258 */
2259 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2260 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2261 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2262 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2263 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2264 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2265 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2266 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2267 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2268 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2269 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2270 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2271 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2272 for (i = 0; i < 16; i++)
2273 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2274 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2275 ANA_CPUQ_8021_CFG, i);
2276
Claudiu Manoil1e1caa92019-04-16 17:51:59 +03002277 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002278 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2279 OCELOT_STATS_CHECK_DELAY);
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002280
2281 if (ocelot->ptp) {
2282 ret = ocelot_init_timestamp(ocelot);
2283 if (ret) {
2284 dev_err(ocelot->dev,
2285 "Timestamp initialization failed\n");
2286 return ret;
2287 }
2288 }
2289
Alexandre Bellonia556c762018-05-14 22:04:57 +02002290 return 0;
2291}
2292EXPORT_SYMBOL(ocelot_init);
2293
2294void ocelot_deinit(struct ocelot *ocelot)
2295{
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002296 struct list_head *pos, *tmp;
2297 struct ocelot_port *port;
2298 struct ocelot_skb *entry;
2299 int i;
2300
Claudiu Manoilc5d13962019-07-25 16:33:18 +03002301 cancel_delayed_work(&ocelot->stats_work);
Alexandre Bellonia556c762018-05-14 22:04:57 +02002302 destroy_workqueue(ocelot->stats_queue);
2303 mutex_destroy(&ocelot->stats_lock);
Horatiu Vulturb5962292019-05-31 09:16:56 +02002304 ocelot_ace_deinit();
Antoine Tenart4e3b0462019-08-12 16:45:37 +02002305
2306 for (i = 0; i < ocelot->num_phys_ports; i++) {
2307 port = ocelot->ports[i];
2308
2309 list_for_each_safe(pos, tmp, &port->skbs) {
2310 entry = list_entry(pos, struct ocelot_skb, head);
2311
2312 list_del(pos);
2313 dev_kfree_skb_any(entry->skb);
2314 kfree(entry);
2315 }
2316 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02002317}
2318EXPORT_SYMBOL(ocelot_deinit);
2319
2320MODULE_LICENSE("Dual MIT/GPL");