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