1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi Ocelot Switch driver
5 * Copyright (c) 2017 Microsemi Corporation
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>
21 #include <net/netevent.h>
22 #include <net/rtnetlink.h>
23 #include <net/switchdev.h>
26 #include "ocelot_ace.h"
28 #define TABLE_UPDATE_SLEEP_US 10
29 #define TABLE_UPDATE_TIMEOUT_US 100000
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.
37 enum macaccess_entry_type
{
44 struct ocelot_mact_entry
{
47 enum macaccess_entry_type type
;
50 static inline u32
ocelot_mact_read_macaccess(struct ocelot
*ocelot
)
52 return ocelot_read(ocelot
, ANA_TABLES_MACACCESS
);
55 static inline int ocelot_mact_wait_for_completion(struct ocelot
*ocelot
)
59 return readx_poll_timeout(ocelot_mact_read_macaccess
,
61 (val
& ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M
) ==
63 TABLE_UPDATE_SLEEP_US
, TABLE_UPDATE_TIMEOUT_US
);
66 static void ocelot_mact_select(struct ocelot
*ocelot
,
67 const unsigned char mac
[ETH_ALEN
],
70 u32 macl
= 0, mach
= 0;
72 /* Set the MAC address to handle and the vlan associated in a format
73 * understood by the hardware.
83 ocelot_write(ocelot
, macl
, ANA_TABLES_MACLDATA
);
84 ocelot_write(ocelot
, mach
, ANA_TABLES_MACHDATA
);
88 static int ocelot_mact_learn(struct ocelot
*ocelot
, int port
,
89 const unsigned char mac
[ETH_ALEN
],
91 enum macaccess_entry_type type
)
93 ocelot_mact_select(ocelot
, mac
, vid
);
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
);
102 return ocelot_mact_wait_for_completion(ocelot
);
105 static int ocelot_mact_forget(struct ocelot
*ocelot
,
106 const unsigned char mac
[ETH_ALEN
],
109 ocelot_mact_select(ocelot
, mac
, vid
);
111 /* Issue a forget command */
113 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET
),
114 ANA_TABLES_MACACCESS
);
116 return ocelot_mact_wait_for_completion(ocelot
);
119 static void ocelot_mact_init(struct ocelot
*ocelot
)
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.
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
,
131 /* Clear the MAC table */
132 ocelot_write(ocelot
, MACACCESS_CMD_INIT
, ANA_TABLES_MACACCESS
);
135 static void ocelot_vcap_enable(struct ocelot
*ocelot
, int port
)
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
);
142 static inline u32
ocelot_vlant_read_vlanaccess(struct ocelot
*ocelot
)
144 return ocelot_read(ocelot
, ANA_TABLES_VLANACCESS
);
147 static inline int ocelot_vlant_wait_for_completion(struct ocelot
*ocelot
)
151 return readx_poll_timeout(ocelot_vlant_read_vlanaccess
,
154 (val
& ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M
) ==
155 ANA_TABLES_VLANACCESS_CMD_IDLE
,
156 TABLE_UPDATE_SLEEP_US
, TABLE_UPDATE_TIMEOUT_US
);
159 static int ocelot_vlant_set_mask(struct ocelot
*ocelot
, u16 vid
, u32 mask
)
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
);
169 return ocelot_vlant_wait_for_completion(ocelot
);
172 static void ocelot_vlan_mode(struct ocelot
*ocelot
, int port
,
173 netdev_features_t features
)
178 val
= ocelot_read(ocelot
, ANA_VLANMASK
);
179 if (features
& NETIF_F_HW_VLAN_CTAG_FILTER
)
183 ocelot_write(ocelot
, val
, ANA_VLANMASK
);
186 void ocelot_port_vlan_filtering(struct ocelot
*ocelot
, int port
,
189 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
193 val
= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
194 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
197 ocelot_rmw_gix(ocelot
, val
,
198 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
199 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M
,
200 ANA_PORT_VLAN_CFG
, port
);
202 if (vlan_aware
&& !ocelot_port
->vid
)
203 /* If port is vlan-aware and tagged, drop untagged and priority
206 val
= ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA
|
207 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA
|
208 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA
;
211 ocelot_rmw_gix(ocelot
, val
,
212 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 ANA_PORT_DROP_CFG
, port
);
218 if (ocelot_port
->vid
)
219 /* Tag all frames except when VID == DEFAULT_VLAN */
220 val
|= REW_TAG_CFG_TAG_CFG(1);
223 val
|= REW_TAG_CFG_TAG_CFG(3);
225 /* Port tagging disabled. */
226 val
= REW_TAG_CFG_TAG_CFG(0);
228 ocelot_rmw_gix(ocelot
, val
,
229 REW_TAG_CFG_TAG_CFG_M
,
232 EXPORT_SYMBOL(ocelot_port_vlan_filtering
);
234 static int ocelot_port_set_native_vlan(struct ocelot
*ocelot
, int port
,
237 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
239 if (ocelot_port
->vid
!= vid
) {
240 /* Always permit deleting the native VLAN (vid = 0) */
241 if (ocelot_port
->vid
&& vid
) {
243 "Port already has a native VLAN: %d\n",
247 ocelot_port
->vid
= vid
;
250 ocelot_rmw_gix(ocelot
, REW_PORT_VLAN_CFG_PORT_VID(vid
),
251 REW_PORT_VLAN_CFG_PORT_VID_M
,
252 REW_PORT_VLAN_CFG
, port
);
257 /* Default vlan to clasify for untagged frames (may be zero) */
258 static void ocelot_port_set_pvid(struct ocelot
*ocelot
, int port
, u16 pvid
)
260 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
262 ocelot_rmw_gix(ocelot
,
263 ANA_PORT_VLAN_CFG_VLAN_VID(pvid
),
264 ANA_PORT_VLAN_CFG_VLAN_VID_M
,
265 ANA_PORT_VLAN_CFG
, port
);
267 ocelot_port
->pvid
= pvid
;
270 int ocelot_vlan_add(struct ocelot
*ocelot
, int port
, u16 vid
, bool pvid
,
275 /* Make the port a member of the VLAN */
276 ocelot
->vlan_mask
[vid
] |= BIT(port
);
277 ret
= ocelot_vlant_set_mask(ocelot
, vid
, ocelot
->vlan_mask
[vid
]);
281 /* Default ingress vlan classification */
283 ocelot_port_set_pvid(ocelot
, port
, vid
);
285 /* Untagged egress vlan clasification */
287 ret
= ocelot_port_set_native_vlan(ocelot
, port
, vid
);
294 EXPORT_SYMBOL(ocelot_vlan_add
);
296 static int ocelot_vlan_vid_add(struct net_device
*dev
, u16 vid
, bool pvid
,
299 struct ocelot_port_private
*priv
= netdev_priv(dev
);
300 struct ocelot_port
*ocelot_port
= &priv
->port
;
301 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
302 int port
= priv
->chip_port
;
305 ret
= ocelot_vlan_add(ocelot
, port
, vid
, pvid
, untagged
);
309 /* Add the port MAC address to with the right VLAN information */
310 ocelot_mact_learn(ocelot
, PGID_CPU
, dev
->dev_addr
, vid
,
316 int ocelot_vlan_del(struct ocelot
*ocelot
, int port
, u16 vid
)
318 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
321 /* Stop the port from being a member of the vlan */
322 ocelot
->vlan_mask
[vid
] &= ~BIT(port
);
323 ret
= ocelot_vlant_set_mask(ocelot
, vid
, ocelot
->vlan_mask
[vid
]);
328 if (ocelot_port
->pvid
== vid
)
329 ocelot_port_set_pvid(ocelot
, port
, 0);
332 if (ocelot_port
->vid
== vid
)
333 ocelot_port_set_native_vlan(ocelot
, port
, 0);
337 EXPORT_SYMBOL(ocelot_vlan_del
);
339 static int ocelot_vlan_vid_del(struct net_device
*dev
, u16 vid
)
341 struct ocelot_port_private
*priv
= netdev_priv(dev
);
342 struct ocelot
*ocelot
= priv
->port
.ocelot
;
343 int port
= priv
->chip_port
;
346 /* 8021q removes VID 0 on module unload for all interfaces
347 * with VLAN filtering feature. We need to keep it to receive
353 ret
= ocelot_vlan_del(ocelot
, port
, vid
);
357 /* Del the port MAC address to with the right VLAN information */
358 ocelot_mact_forget(ocelot
, dev
->dev_addr
, vid
);
363 static void ocelot_vlan_init(struct ocelot
*ocelot
)
367 /* Clear VLAN table, by default all ports are members of all VLANs */
368 ocelot_write(ocelot
, ANA_TABLES_VLANACCESS_CMD_INIT
,
369 ANA_TABLES_VLANACCESS
);
370 ocelot_vlant_wait_for_completion(ocelot
);
372 /* Configure the port VLAN memberships */
373 for (vid
= 1; vid
< VLAN_N_VID
; vid
++) {
374 ocelot
->vlan_mask
[vid
] = 0;
375 ocelot_vlant_set_mask(ocelot
, vid
, ocelot
->vlan_mask
[vid
]);
378 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
379 * traffic. It is added automatically if 8021q module is loaded, but
380 * we can't rely on it since module may be not loaded.
382 ocelot
->vlan_mask
[0] = GENMASK(ocelot
->num_phys_ports
- 1, 0);
383 ocelot_vlant_set_mask(ocelot
, 0, ocelot
->vlan_mask
[0]);
385 /* Set vlan ingress filter mask to all ports but the CPU port by
388 ocelot_write(ocelot
, GENMASK(ocelot
->num_phys_ports
- 1, 0),
391 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
392 ocelot_write_gix(ocelot
, 0, REW_PORT_VLAN_CFG
, port
);
393 ocelot_write_gix(ocelot
, 0, REW_TAG_CFG
, port
);
398 * Bit 8: Unit; 0:1, 1:16
399 * Bit 7-0: Value to be multiplied with unit
401 static u16
ocelot_wm_enc(u16 value
)
404 return BIT(8) | (value
/ 16);
409 void ocelot_adjust_link(struct ocelot
*ocelot
, int port
,
410 struct phy_device
*phydev
)
412 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
415 switch (phydev
->speed
) {
417 speed
= OCELOT_SPEED_10
;
420 speed
= OCELOT_SPEED_100
;
423 speed
= OCELOT_SPEED_1000
;
424 mode
= DEV_MAC_MODE_CFG_GIGA_MODE_ENA
;
427 speed
= OCELOT_SPEED_2500
;
428 mode
= DEV_MAC_MODE_CFG_GIGA_MODE_ENA
;
431 dev_err(ocelot
->dev
, "Unsupported PHY speed on port %d: %d\n",
432 port
, phydev
->speed
);
436 phy_print_status(phydev
);
441 /* Only full duplex supported for now */
442 ocelot_port_writel(ocelot_port
, DEV_MAC_MODE_CFG_FDX_ENA
|
443 mode
, DEV_MAC_MODE_CFG
);
445 if (ocelot
->ops
->pcs_init
)
446 ocelot
->ops
->pcs_init(ocelot
, port
);
448 /* Enable MAC module */
449 ocelot_port_writel(ocelot_port
, DEV_MAC_ENA_CFG_RX_ENA
|
450 DEV_MAC_ENA_CFG_TX_ENA
, DEV_MAC_ENA_CFG
);
452 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
454 ocelot_port_writel(ocelot_port
, DEV_CLOCK_CFG_LINK_SPEED(speed
),
458 ocelot_write_gix(ocelot
, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed
),
459 ANA_PFC_PFC_CFG
, port
);
461 /* Core: Enable port for frame transfer */
462 ocelot_write_rix(ocelot
, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE
|
463 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
464 QSYS_SWITCH_PORT_MODE_PORT_ENA
,
465 QSYS_SWITCH_PORT_MODE
, port
);
468 ocelot_write_rix(ocelot
, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
469 SYS_MAC_FC_CFG_RX_FC_ENA
| SYS_MAC_FC_CFG_TX_FC_ENA
|
470 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA
|
471 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
472 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed
),
473 SYS_MAC_FC_CFG
, port
);
474 ocelot_write_rix(ocelot
, 0, ANA_POL_FLOWC
, port
);
476 EXPORT_SYMBOL(ocelot_adjust_link
);
478 static void ocelot_port_adjust_link(struct net_device
*dev
)
480 struct ocelot_port_private
*priv
= netdev_priv(dev
);
481 struct ocelot
*ocelot
= priv
->port
.ocelot
;
482 int port
= priv
->chip_port
;
484 ocelot_adjust_link(ocelot
, port
, dev
->phydev
);
487 void ocelot_port_enable(struct ocelot
*ocelot
, int port
,
488 struct phy_device
*phy
)
490 /* Enable receiving frames on the port, and activate auto-learning of
493 ocelot_write_gix(ocelot
, ANA_PORT_PORT_CFG_LEARNAUTO
|
494 ANA_PORT_PORT_CFG_RECV_ENA
|
495 ANA_PORT_PORT_CFG_PORTID_VAL(port
),
496 ANA_PORT_PORT_CFG
, port
);
498 EXPORT_SYMBOL(ocelot_port_enable
);
500 static int ocelot_port_open(struct net_device
*dev
)
502 struct ocelot_port_private
*priv
= netdev_priv(dev
);
503 struct ocelot_port
*ocelot_port
= &priv
->port
;
504 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
505 int port
= priv
->chip_port
;
509 err
= phy_set_mode_ext(priv
->serdes
, PHY_MODE_ETHERNET
,
510 ocelot_port
->phy_mode
);
512 netdev_err(dev
, "Could not set mode of SerDes\n");
517 err
= phy_connect_direct(dev
, priv
->phy
, &ocelot_port_adjust_link
,
518 ocelot_port
->phy_mode
);
520 netdev_err(dev
, "Could not attach to PHY\n");
524 dev
->phydev
= priv
->phy
;
526 phy_attached_info(priv
->phy
);
527 phy_start(priv
->phy
);
529 ocelot_port_enable(ocelot
, port
, priv
->phy
);
534 void ocelot_port_disable(struct ocelot
*ocelot
, int port
)
536 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
538 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_ENA_CFG
);
539 ocelot_rmw_rix(ocelot
, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA
,
540 QSYS_SWITCH_PORT_MODE
, port
);
542 EXPORT_SYMBOL(ocelot_port_disable
);
544 static int ocelot_port_stop(struct net_device
*dev
)
546 struct ocelot_port_private
*priv
= netdev_priv(dev
);
547 struct ocelot
*ocelot
= priv
->port
.ocelot
;
548 int port
= priv
->chip_port
;
550 phy_disconnect(priv
->phy
);
554 ocelot_port_disable(ocelot
, port
);
559 /* Generate the IFH for frame injection
561 * The IFH is a 128bit-value
562 * bit 127: bypass the analyzer processing
563 * bit 56-67: destination mask
564 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
565 * bit 20-27: cpu extraction queue mask
566 * bit 16: tag type 0: C-tag, 1: S-tag
569 static int ocelot_gen_ifh(u32
*ifh
, struct frame_info
*info
)
571 ifh
[0] = IFH_INJ_BYPASS
| ((0x1ff & info
->rew_op
) << 21);
572 ifh
[1] = (0xf00 & info
->port
) >> 8;
573 ifh
[2] = (0xff & info
->port
) << 24;
574 ifh
[3] = (info
->tag_type
<< 16) | info
->vid
;
579 int ocelot_port_add_txtstamp_skb(struct ocelot_port
*ocelot_port
,
582 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
583 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
585 if (ocelot
->ptp
&& shinfo
->tx_flags
& SKBTX_HW_TSTAMP
&&
586 ocelot_port
->ptp_cmd
== IFH_REW_OP_TWO_STEP_PTP
) {
587 shinfo
->tx_flags
|= SKBTX_IN_PROGRESS
;
588 /* Store timestamp ID in cb[0] of sk_buff */
589 skb
->cb
[0] = ocelot_port
->ts_id
% 4;
590 skb_queue_tail(&ocelot_port
->tx_skbs
, skb
);
595 EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb
);
597 static int ocelot_port_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
599 struct ocelot_port_private
*priv
= netdev_priv(dev
);
600 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
601 struct ocelot_port
*ocelot_port
= &priv
->port
;
602 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
603 u32 val
, ifh
[OCELOT_TAG_LEN
/ 4];
604 struct frame_info info
= {};
605 u8 grp
= 0; /* Send everything on CPU group 0 */
606 unsigned int i
, count
, last
;
607 int port
= priv
->chip_port
;
609 val
= ocelot_read(ocelot
, QS_INJ_STATUS
);
610 if (!(val
& QS_INJ_STATUS_FIFO_RDY(BIT(grp
))) ||
611 (val
& QS_INJ_STATUS_WMARK_REACHED(BIT(grp
))))
612 return NETDEV_TX_BUSY
;
614 ocelot_write_rix(ocelot
, QS_INJ_CTRL_GAP_SIZE(1) |
615 QS_INJ_CTRL_SOF
, QS_INJ_CTRL
, grp
);
617 info
.port
= BIT(port
);
618 info
.tag_type
= IFH_TAG_TYPE_C
;
619 info
.vid
= skb_vlan_tag_get(skb
);
621 /* Check if timestamping is needed */
622 if (ocelot
->ptp
&& shinfo
->tx_flags
& SKBTX_HW_TSTAMP
) {
623 info
.rew_op
= ocelot_port
->ptp_cmd
;
624 if (ocelot_port
->ptp_cmd
== IFH_REW_OP_TWO_STEP_PTP
)
625 info
.rew_op
|= (ocelot_port
->ts_id
% 4) << 3;
628 ocelot_gen_ifh(ifh
, &info
);
630 for (i
= 0; i
< OCELOT_TAG_LEN
/ 4; i
++)
631 ocelot_write_rix(ocelot
, (__force u32
)cpu_to_be32(ifh
[i
]),
634 count
= (skb
->len
+ 3) / 4;
636 for (i
= 0; i
< count
; i
++) {
637 ocelot_write_rix(ocelot
, ((u32
*)skb
->data
)[i
], QS_INJ_WR
, grp
);
641 while (i
< (OCELOT_BUFFER_CELL_SZ
/ 4)) {
642 ocelot_write_rix(ocelot
, 0, QS_INJ_WR
, grp
);
646 /* Indicate EOF and valid bytes in last word */
647 ocelot_write_rix(ocelot
, QS_INJ_CTRL_GAP_SIZE(1) |
648 QS_INJ_CTRL_VLD_BYTES(skb
->len
< OCELOT_BUFFER_CELL_SZ
? 0 : last
) |
653 ocelot_write_rix(ocelot
, 0, QS_INJ_WR
, grp
);
654 skb_tx_timestamp(skb
);
656 dev
->stats
.tx_packets
++;
657 dev
->stats
.tx_bytes
+= skb
->len
;
659 if (!ocelot_port_add_txtstamp_skb(ocelot_port
, skb
)) {
660 ocelot_port
->ts_id
++;
664 dev_kfree_skb_any(skb
);
668 static void ocelot_get_hwtimestamp(struct ocelot
*ocelot
,
669 struct timespec64
*ts
)
674 spin_lock_irqsave(&ocelot
->ptp_clock_lock
, flags
);
676 /* Read current PTP time to get seconds */
677 val
= ocelot_read_rix(ocelot
, PTP_PIN_CFG
, TOD_ACC_PIN
);
679 val
&= ~(PTP_PIN_CFG_SYNC
| PTP_PIN_CFG_ACTION_MASK
| PTP_PIN_CFG_DOM
);
680 val
|= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE
);
681 ocelot_write_rix(ocelot
, val
, PTP_PIN_CFG
, TOD_ACC_PIN
);
682 ts
->tv_sec
= ocelot_read_rix(ocelot
, PTP_PIN_TOD_SEC_LSB
, TOD_ACC_PIN
);
684 /* Read packet HW timestamp from FIFO */
685 val
= ocelot_read(ocelot
, SYS_PTP_TXSTAMP
);
686 ts
->tv_nsec
= SYS_PTP_TXSTAMP_PTP_TXSTAMP(val
);
688 /* Sec has incremented since the ts was registered */
689 if ((ts
->tv_sec
& 0x1) != !!(val
& SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC
))
692 spin_unlock_irqrestore(&ocelot
->ptp_clock_lock
, flags
);
695 void ocelot_get_txtstamp(struct ocelot
*ocelot
)
697 int budget
= OCELOT_PTP_QUEUE_SZ
;
700 struct sk_buff
*skb
, *skb_tmp
, *skb_match
= NULL
;
701 struct skb_shared_hwtstamps shhwtstamps
;
702 struct ocelot_port
*port
;
703 struct timespec64 ts
;
707 val
= ocelot_read(ocelot
, SYS_PTP_STATUS
);
709 /* Check if a timestamp can be retrieved */
710 if (!(val
& SYS_PTP_STATUS_PTP_MESS_VLD
))
713 WARN_ON(val
& SYS_PTP_STATUS_PTP_OVFL
);
715 /* Retrieve the ts ID and Tx port */
716 id
= SYS_PTP_STATUS_PTP_MESS_ID_X(val
);
717 txport
= SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val
);
719 /* Retrieve its associated skb */
720 port
= ocelot
->ports
[txport
];
722 spin_lock_irqsave(&port
->tx_skbs
.lock
, flags
);
724 skb_queue_walk_safe(&port
->tx_skbs
, skb
, skb_tmp
) {
725 if (skb
->cb
[0] != id
)
727 __skb_unlink(skb
, &port
->tx_skbs
);
732 spin_unlock_irqrestore(&port
->tx_skbs
.lock
, flags
);
735 ocelot_write(ocelot
, SYS_PTP_NXT_PTP_NXT
, SYS_PTP_NXT
);
737 if (unlikely(!skb_match
))
740 /* Get the h/w timestamp */
741 ocelot_get_hwtimestamp(ocelot
, &ts
);
743 /* Set the timestamp into the skb */
744 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
745 shhwtstamps
.hwtstamp
= ktime_set(ts
.tv_sec
, ts
.tv_nsec
);
746 skb_tstamp_tx(skb_match
, &shhwtstamps
);
748 dev_kfree_skb_any(skb_match
);
751 EXPORT_SYMBOL(ocelot_get_txtstamp
);
753 static int ocelot_mc_unsync(struct net_device
*dev
, const unsigned char *addr
)
755 struct ocelot_port_private
*priv
= netdev_priv(dev
);
756 struct ocelot_port
*ocelot_port
= &priv
->port
;
757 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
759 return ocelot_mact_forget(ocelot
, addr
, ocelot_port
->pvid
);
762 static int ocelot_mc_sync(struct net_device
*dev
, const unsigned char *addr
)
764 struct ocelot_port_private
*priv
= netdev_priv(dev
);
765 struct ocelot_port
*ocelot_port
= &priv
->port
;
766 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
768 return ocelot_mact_learn(ocelot
, PGID_CPU
, addr
, ocelot_port
->pvid
,
772 static void ocelot_set_rx_mode(struct net_device
*dev
)
774 struct ocelot_port_private
*priv
= netdev_priv(dev
);
775 struct ocelot
*ocelot
= priv
->port
.ocelot
;
779 /* This doesn't handle promiscuous mode because the bridge core is
780 * setting IFF_PROMISC on all slave interfaces and all frames would be
781 * forwarded to the CPU port.
783 val
= GENMASK(ocelot
->num_phys_ports
- 1, 0);
784 for (i
= ocelot
->num_phys_ports
+ 1; i
< PGID_CPU
; i
++)
785 ocelot_write_rix(ocelot
, val
, ANA_PGID_PGID
, i
);
787 __dev_mc_sync(dev
, ocelot_mc_sync
, ocelot_mc_unsync
);
790 static int ocelot_port_get_phys_port_name(struct net_device
*dev
,
791 char *buf
, size_t len
)
793 struct ocelot_port_private
*priv
= netdev_priv(dev
);
794 int port
= priv
->chip_port
;
797 ret
= snprintf(buf
, len
, "p%d", port
);
804 static int ocelot_port_set_mac_address(struct net_device
*dev
, void *p
)
806 struct ocelot_port_private
*priv
= netdev_priv(dev
);
807 struct ocelot_port
*ocelot_port
= &priv
->port
;
808 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
809 const struct sockaddr
*addr
= p
;
811 /* Learn the new net device MAC address in the mac table. */
812 ocelot_mact_learn(ocelot
, PGID_CPU
, addr
->sa_data
, ocelot_port
->pvid
,
814 /* Then forget the previous one. */
815 ocelot_mact_forget(ocelot
, dev
->dev_addr
, ocelot_port
->pvid
);
817 ether_addr_copy(dev
->dev_addr
, addr
->sa_data
);
821 static void ocelot_get_stats64(struct net_device
*dev
,
822 struct rtnl_link_stats64
*stats
)
824 struct ocelot_port_private
*priv
= netdev_priv(dev
);
825 struct ocelot
*ocelot
= priv
->port
.ocelot
;
826 int port
= priv
->chip_port
;
828 /* Configure the port to read the stats from */
829 ocelot_write(ocelot
, SYS_STAT_CFG_STAT_VIEW(port
),
833 stats
->rx_bytes
= ocelot_read(ocelot
, SYS_COUNT_RX_OCTETS
);
834 stats
->rx_packets
= ocelot_read(ocelot
, SYS_COUNT_RX_SHORTS
) +
835 ocelot_read(ocelot
, SYS_COUNT_RX_FRAGMENTS
) +
836 ocelot_read(ocelot
, SYS_COUNT_RX_JABBERS
) +
837 ocelot_read(ocelot
, SYS_COUNT_RX_LONGS
) +
838 ocelot_read(ocelot
, SYS_COUNT_RX_64
) +
839 ocelot_read(ocelot
, SYS_COUNT_RX_65_127
) +
840 ocelot_read(ocelot
, SYS_COUNT_RX_128_255
) +
841 ocelot_read(ocelot
, SYS_COUNT_RX_256_1023
) +
842 ocelot_read(ocelot
, SYS_COUNT_RX_1024_1526
) +
843 ocelot_read(ocelot
, SYS_COUNT_RX_1527_MAX
);
844 stats
->multicast
= ocelot_read(ocelot
, SYS_COUNT_RX_MULTICAST
);
845 stats
->rx_dropped
= dev
->stats
.rx_dropped
;
848 stats
->tx_bytes
= ocelot_read(ocelot
, SYS_COUNT_TX_OCTETS
);
849 stats
->tx_packets
= ocelot_read(ocelot
, SYS_COUNT_TX_64
) +
850 ocelot_read(ocelot
, SYS_COUNT_TX_65_127
) +
851 ocelot_read(ocelot
, SYS_COUNT_TX_128_511
) +
852 ocelot_read(ocelot
, SYS_COUNT_TX_512_1023
) +
853 ocelot_read(ocelot
, SYS_COUNT_TX_1024_1526
) +
854 ocelot_read(ocelot
, SYS_COUNT_TX_1527_MAX
);
855 stats
->tx_dropped
= ocelot_read(ocelot
, SYS_COUNT_TX_DROPS
) +
856 ocelot_read(ocelot
, SYS_COUNT_TX_AGING
);
857 stats
->collisions
= ocelot_read(ocelot
, SYS_COUNT_TX_COLLISION
);
860 int ocelot_fdb_add(struct ocelot
*ocelot
, int port
,
861 const unsigned char *addr
, u16 vid
, bool vlan_aware
)
863 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
867 /* If the bridge is not VLAN aware and no VID was
868 * provided, set it to pvid to ensure the MAC entry
869 * matches incoming untagged packets
871 vid
= ocelot_port
->pvid
;
873 /* If the bridge is VLAN aware a VID must be provided as
874 * otherwise the learnt entry wouldn't match any frame.
879 return ocelot_mact_learn(ocelot
, port
, addr
, vid
, ENTRYTYPE_LOCKED
);
881 EXPORT_SYMBOL(ocelot_fdb_add
);
883 static int ocelot_port_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
884 struct net_device
*dev
,
885 const unsigned char *addr
,
887 struct netlink_ext_ack
*extack
)
889 struct ocelot_port_private
*priv
= netdev_priv(dev
);
890 struct ocelot
*ocelot
= priv
->port
.ocelot
;
891 int port
= priv
->chip_port
;
893 return ocelot_fdb_add(ocelot
, port
, addr
, vid
, priv
->vlan_aware
);
896 int ocelot_fdb_del(struct ocelot
*ocelot
, int port
,
897 const unsigned char *addr
, u16 vid
)
899 return ocelot_mact_forget(ocelot
, addr
, vid
);
901 EXPORT_SYMBOL(ocelot_fdb_del
);
903 static int ocelot_port_fdb_del(struct ndmsg
*ndm
, struct nlattr
*tb
[],
904 struct net_device
*dev
,
905 const unsigned char *addr
, u16 vid
)
907 struct ocelot_port_private
*priv
= netdev_priv(dev
);
908 struct ocelot
*ocelot
= priv
->port
.ocelot
;
909 int port
= priv
->chip_port
;
911 return ocelot_fdb_del(ocelot
, port
, addr
, vid
);
914 struct ocelot_dump_ctx
{
915 struct net_device
*dev
;
917 struct netlink_callback
*cb
;
921 static int ocelot_port_fdb_do_dump(const unsigned char *addr
, u16 vid
,
922 bool is_static
, void *data
)
924 struct ocelot_dump_ctx
*dump
= data
;
925 u32 portid
= NETLINK_CB(dump
->cb
->skb
).portid
;
926 u32 seq
= dump
->cb
->nlh
->nlmsg_seq
;
927 struct nlmsghdr
*nlh
;
930 if (dump
->idx
< dump
->cb
->args
[2])
933 nlh
= nlmsg_put(dump
->skb
, portid
, seq
, RTM_NEWNEIGH
,
934 sizeof(*ndm
), NLM_F_MULTI
);
938 ndm
= nlmsg_data(nlh
);
939 ndm
->ndm_family
= AF_BRIDGE
;
942 ndm
->ndm_flags
= NTF_SELF
;
944 ndm
->ndm_ifindex
= dump
->dev
->ifindex
;
945 ndm
->ndm_state
= is_static
? NUD_NOARP
: NUD_REACHABLE
;
947 if (nla_put(dump
->skb
, NDA_LLADDR
, ETH_ALEN
, addr
))
948 goto nla_put_failure
;
950 if (vid
&& nla_put_u16(dump
->skb
, NDA_VLAN
, vid
))
951 goto nla_put_failure
;
953 nlmsg_end(dump
->skb
, nlh
);
960 nlmsg_cancel(dump
->skb
, nlh
);
964 static int ocelot_mact_read(struct ocelot
*ocelot
, int port
, int row
, int col
,
965 struct ocelot_mact_entry
*entry
)
967 u32 val
, dst
, macl
, mach
;
970 /* Set row and column to read from */
971 ocelot_field_write(ocelot
, ANA_TABLES_MACTINDX_M_INDEX
, row
);
972 ocelot_field_write(ocelot
, ANA_TABLES_MACTINDX_BUCKET
, col
);
974 /* Issue a read command */
976 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ
),
977 ANA_TABLES_MACACCESS
);
979 if (ocelot_mact_wait_for_completion(ocelot
))
982 /* Read the entry flags */
983 val
= ocelot_read(ocelot
, ANA_TABLES_MACACCESS
);
984 if (!(val
& ANA_TABLES_MACACCESS_VALID
))
987 /* If the entry read has another port configured as its destination,
990 dst
= (val
& ANA_TABLES_MACACCESS_DEST_IDX_M
) >> 3;
994 /* Get the entry's MAC address and VLAN id */
995 macl
= ocelot_read(ocelot
, ANA_TABLES_MACLDATA
);
996 mach
= ocelot_read(ocelot
, ANA_TABLES_MACHDATA
);
998 mac
[0] = (mach
>> 8) & 0xff;
999 mac
[1] = (mach
>> 0) & 0xff;
1000 mac
[2] = (macl
>> 24) & 0xff;
1001 mac
[3] = (macl
>> 16) & 0xff;
1002 mac
[4] = (macl
>> 8) & 0xff;
1003 mac
[5] = (macl
>> 0) & 0xff;
1005 entry
->vid
= (mach
>> 16) & 0xfff;
1006 ether_addr_copy(entry
->mac
, mac
);
1011 int ocelot_fdb_dump(struct ocelot
*ocelot
, int port
,
1012 dsa_fdb_dump_cb_t
*cb
, void *data
)
1016 /* Loop through all the mac tables entries. There are 1024 rows of 4
1019 for (i
= 0; i
< 1024; i
++) {
1020 for (j
= 0; j
< 4; j
++) {
1021 struct ocelot_mact_entry entry
;
1025 ret
= ocelot_mact_read(ocelot
, port
, i
, j
, &entry
);
1026 /* If the entry is invalid (wrong port, invalid...),
1034 is_static
= (entry
.type
== ENTRYTYPE_LOCKED
);
1036 ret
= cb(entry
.mac
, entry
.vid
, is_static
, data
);
1044 EXPORT_SYMBOL(ocelot_fdb_dump
);
1046 static int ocelot_port_fdb_dump(struct sk_buff
*skb
,
1047 struct netlink_callback
*cb
,
1048 struct net_device
*dev
,
1049 struct net_device
*filter_dev
, int *idx
)
1051 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1052 struct ocelot
*ocelot
= priv
->port
.ocelot
;
1053 struct ocelot_dump_ctx dump
= {
1059 int port
= priv
->chip_port
;
1062 ret
= ocelot_fdb_dump(ocelot
, port
, ocelot_port_fdb_do_dump
, &dump
);
1069 static int ocelot_vlan_rx_add_vid(struct net_device
*dev
, __be16 proto
,
1072 return ocelot_vlan_vid_add(dev
, vid
, false, false);
1075 static int ocelot_vlan_rx_kill_vid(struct net_device
*dev
, __be16 proto
,
1078 return ocelot_vlan_vid_del(dev
, vid
);
1081 static int ocelot_set_features(struct net_device
*dev
,
1082 netdev_features_t features
)
1084 netdev_features_t changed
= dev
->features
^ features
;
1085 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1086 struct ocelot
*ocelot
= priv
->port
.ocelot
;
1087 int port
= priv
->chip_port
;
1089 if ((dev
->features
& NETIF_F_HW_TC
) > (features
& NETIF_F_HW_TC
) &&
1090 priv
->tc
.offload_cnt
) {
1092 "Cannot disable HW TC offload while offloads active\n");
1096 if (changed
& NETIF_F_HW_VLAN_CTAG_FILTER
)
1097 ocelot_vlan_mode(ocelot
, port
, features
);
1102 static int ocelot_get_port_parent_id(struct net_device
*dev
,
1103 struct netdev_phys_item_id
*ppid
)
1105 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1106 struct ocelot
*ocelot
= priv
->port
.ocelot
;
1108 ppid
->id_len
= sizeof(ocelot
->base_mac
);
1109 memcpy(&ppid
->id
, &ocelot
->base_mac
, ppid
->id_len
);
1114 int ocelot_hwstamp_get(struct ocelot
*ocelot
, int port
, struct ifreq
*ifr
)
1116 return copy_to_user(ifr
->ifr_data
, &ocelot
->hwtstamp_config
,
1117 sizeof(ocelot
->hwtstamp_config
)) ? -EFAULT
: 0;
1119 EXPORT_SYMBOL(ocelot_hwstamp_get
);
1121 int ocelot_hwstamp_set(struct ocelot
*ocelot
, int port
, struct ifreq
*ifr
)
1123 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
1124 struct hwtstamp_config cfg
;
1126 if (copy_from_user(&cfg
, ifr
->ifr_data
, sizeof(cfg
)))
1129 /* reserved for future extensions */
1133 /* Tx type sanity check */
1134 switch (cfg
.tx_type
) {
1135 case HWTSTAMP_TX_ON
:
1136 ocelot_port
->ptp_cmd
= IFH_REW_OP_TWO_STEP_PTP
;
1138 case HWTSTAMP_TX_ONESTEP_SYNC
:
1139 /* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1140 * need to update the origin time.
1142 ocelot_port
->ptp_cmd
= IFH_REW_OP_ORIGIN_PTP
;
1144 case HWTSTAMP_TX_OFF
:
1145 ocelot_port
->ptp_cmd
= 0;
1151 mutex_lock(&ocelot
->ptp_lock
);
1153 switch (cfg
.rx_filter
) {
1154 case HWTSTAMP_FILTER_NONE
:
1156 case HWTSTAMP_FILTER_ALL
:
1157 case HWTSTAMP_FILTER_SOME
:
1158 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
1159 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
1160 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
1161 case HWTSTAMP_FILTER_NTP_ALL
:
1162 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
1163 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
1164 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
1165 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1166 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
1167 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
1168 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
1169 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
1170 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
1171 cfg
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
1174 mutex_unlock(&ocelot
->ptp_lock
);
1178 /* Commit back the result & save it */
1179 memcpy(&ocelot
->hwtstamp_config
, &cfg
, sizeof(cfg
));
1180 mutex_unlock(&ocelot
->ptp_lock
);
1182 return copy_to_user(ifr
->ifr_data
, &cfg
, sizeof(cfg
)) ? -EFAULT
: 0;
1184 EXPORT_SYMBOL(ocelot_hwstamp_set
);
1186 static int ocelot_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1188 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1189 struct ocelot
*ocelot
= priv
->port
.ocelot
;
1190 int port
= priv
->chip_port
;
1192 /* The function is only used for PTP operations for now */
1198 return ocelot_hwstamp_set(ocelot
, port
, ifr
);
1200 return ocelot_hwstamp_get(ocelot
, port
, ifr
);
1206 static const struct net_device_ops ocelot_port_netdev_ops
= {
1207 .ndo_open
= ocelot_port_open
,
1208 .ndo_stop
= ocelot_port_stop
,
1209 .ndo_start_xmit
= ocelot_port_xmit
,
1210 .ndo_set_rx_mode
= ocelot_set_rx_mode
,
1211 .ndo_get_phys_port_name
= ocelot_port_get_phys_port_name
,
1212 .ndo_set_mac_address
= ocelot_port_set_mac_address
,
1213 .ndo_get_stats64
= ocelot_get_stats64
,
1214 .ndo_fdb_add
= ocelot_port_fdb_add
,
1215 .ndo_fdb_del
= ocelot_port_fdb_del
,
1216 .ndo_fdb_dump
= ocelot_port_fdb_dump
,
1217 .ndo_vlan_rx_add_vid
= ocelot_vlan_rx_add_vid
,
1218 .ndo_vlan_rx_kill_vid
= ocelot_vlan_rx_kill_vid
,
1219 .ndo_set_features
= ocelot_set_features
,
1220 .ndo_get_port_parent_id
= ocelot_get_port_parent_id
,
1221 .ndo_setup_tc
= ocelot_setup_tc
,
1222 .ndo_do_ioctl
= ocelot_ioctl
,
1225 void ocelot_get_strings(struct ocelot
*ocelot
, int port
, u32 sset
, u8
*data
)
1229 if (sset
!= ETH_SS_STATS
)
1232 for (i
= 0; i
< ocelot
->num_stats
; i
++)
1233 memcpy(data
+ i
* ETH_GSTRING_LEN
, ocelot
->stats_layout
[i
].name
,
1236 EXPORT_SYMBOL(ocelot_get_strings
);
1238 static void ocelot_port_get_strings(struct net_device
*netdev
, u32 sset
,
1241 struct ocelot_port_private
*priv
= netdev_priv(netdev
);
1242 struct ocelot
*ocelot
= priv
->port
.ocelot
;
1243 int port
= priv
->chip_port
;
1245 ocelot_get_strings(ocelot
, port
, sset
, data
);
1248 static void ocelot_update_stats(struct ocelot
*ocelot
)
1252 mutex_lock(&ocelot
->stats_lock
);
1254 for (i
= 0; i
< ocelot
->num_phys_ports
; i
++) {
1255 /* Configure the port to read the stats from */
1256 ocelot_write(ocelot
, SYS_STAT_CFG_STAT_VIEW(i
), SYS_STAT_CFG
);
1258 for (j
= 0; j
< ocelot
->num_stats
; j
++) {
1260 unsigned int idx
= i
* ocelot
->num_stats
+ j
;
1262 val
= ocelot_read_rix(ocelot
, SYS_COUNT_RX_OCTETS
,
1263 ocelot
->stats_layout
[j
].offset
);
1265 if (val
< (ocelot
->stats
[idx
] & U32_MAX
))
1266 ocelot
->stats
[idx
] += (u64
)1 << 32;
1268 ocelot
->stats
[idx
] = (ocelot
->stats
[idx
] &
1269 ~(u64
)U32_MAX
) + val
;
1273 mutex_unlock(&ocelot
->stats_lock
);
1276 static void ocelot_check_stats_work(struct work_struct
*work
)
1278 struct delayed_work
*del_work
= to_delayed_work(work
);
1279 struct ocelot
*ocelot
= container_of(del_work
, struct ocelot
,
1282 ocelot_update_stats(ocelot
);
1284 queue_delayed_work(ocelot
->stats_queue
, &ocelot
->stats_work
,
1285 OCELOT_STATS_CHECK_DELAY
);
1288 void ocelot_get_ethtool_stats(struct ocelot
*ocelot
, int port
, u64
*data
)
1292 /* check and update now */
1293 ocelot_update_stats(ocelot
);
1295 /* Copy all counters */
1296 for (i
= 0; i
< ocelot
->num_stats
; i
++)
1297 *data
++ = ocelot
->stats
[port
* ocelot
->num_stats
+ i
];
1299 EXPORT_SYMBOL(ocelot_get_ethtool_stats
);
1301 static void ocelot_port_get_ethtool_stats(struct net_device
*dev
,
1302 struct ethtool_stats
*stats
,
1305 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1306 struct ocelot
*ocelot
= priv
->port
.ocelot
;
1307 int port
= priv
->chip_port
;
1309 ocelot_get_ethtool_stats(ocelot
, port
, data
);
1312 int ocelot_get_sset_count(struct ocelot
*ocelot
, int port
, int sset
)
1314 if (sset
!= ETH_SS_STATS
)
1317 return ocelot
->num_stats
;
1319 EXPORT_SYMBOL(ocelot_get_sset_count
);
1321 static int ocelot_port_get_sset_count(struct net_device
*dev
, int sset
)
1323 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1324 struct ocelot
*ocelot
= priv
->port
.ocelot
;
1325 int port
= priv
->chip_port
;
1327 return ocelot_get_sset_count(ocelot
, port
, sset
);
1330 int ocelot_get_ts_info(struct ocelot
*ocelot
, int port
,
1331 struct ethtool_ts_info
*info
)
1333 info
->phc_index
= ocelot
->ptp_clock
?
1334 ptp_clock_index(ocelot
->ptp_clock
) : -1;
1335 info
->so_timestamping
|= SOF_TIMESTAMPING_TX_SOFTWARE
|
1336 SOF_TIMESTAMPING_RX_SOFTWARE
|
1337 SOF_TIMESTAMPING_SOFTWARE
|
1338 SOF_TIMESTAMPING_TX_HARDWARE
|
1339 SOF_TIMESTAMPING_RX_HARDWARE
|
1340 SOF_TIMESTAMPING_RAW_HARDWARE
;
1341 info
->tx_types
= BIT(HWTSTAMP_TX_OFF
) | BIT(HWTSTAMP_TX_ON
) |
1342 BIT(HWTSTAMP_TX_ONESTEP_SYNC
);
1343 info
->rx_filters
= BIT(HWTSTAMP_FILTER_NONE
) | BIT(HWTSTAMP_FILTER_ALL
);
1347 EXPORT_SYMBOL(ocelot_get_ts_info
);
1349 static int ocelot_port_get_ts_info(struct net_device
*dev
,
1350 struct ethtool_ts_info
*info
)
1352 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1353 struct ocelot
*ocelot
= priv
->port
.ocelot
;
1354 int port
= priv
->chip_port
;
1357 return ethtool_op_get_ts_info(dev
, info
);
1359 return ocelot_get_ts_info(ocelot
, port
, info
);
1362 static const struct ethtool_ops ocelot_ethtool_ops
= {
1363 .get_strings
= ocelot_port_get_strings
,
1364 .get_ethtool_stats
= ocelot_port_get_ethtool_stats
,
1365 .get_sset_count
= ocelot_port_get_sset_count
,
1366 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
1367 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
1368 .get_ts_info
= ocelot_port_get_ts_info
,
1371 void ocelot_bridge_stp_state_set(struct ocelot
*ocelot
, int port
, u8 state
)
1376 if (!(BIT(port
) & ocelot
->bridge_mask
))
1379 port_cfg
= ocelot_read_gix(ocelot
, ANA_PORT_PORT_CFG
, port
);
1382 case BR_STATE_FORWARDING
:
1383 ocelot
->bridge_fwd_mask
|= BIT(port
);
1385 case BR_STATE_LEARNING
:
1386 port_cfg
|= ANA_PORT_PORT_CFG_LEARN_ENA
;
1390 port_cfg
&= ~ANA_PORT_PORT_CFG_LEARN_ENA
;
1391 ocelot
->bridge_fwd_mask
&= ~BIT(port
);
1395 ocelot_write_gix(ocelot
, port_cfg
, ANA_PORT_PORT_CFG
, port
);
1397 /* Apply FWD mask. The loop is needed to add/remove the current port as
1398 * a source for the other ports.
1400 for (p
= 0; p
< ocelot
->num_phys_ports
; p
++) {
1401 if (p
== ocelot
->cpu
|| (ocelot
->bridge_fwd_mask
& BIT(p
))) {
1402 unsigned long mask
= ocelot
->bridge_fwd_mask
& ~BIT(p
);
1404 for (i
= 0; i
< ocelot
->num_phys_ports
; i
++) {
1405 unsigned long bond_mask
= ocelot
->lags
[i
];
1410 if (bond_mask
& BIT(p
)) {
1416 /* Avoid the NPI port from looping back to itself */
1417 if (p
!= ocelot
->cpu
)
1418 mask
|= BIT(ocelot
->cpu
);
1420 ocelot_write_rix(ocelot
, mask
,
1421 ANA_PGID_PGID
, PGID_SRC
+ p
);
1423 /* Only the CPU port, this is compatible with link
1426 ocelot_write_rix(ocelot
,
1428 ANA_PGID_PGID
, PGID_SRC
+ p
);
1432 EXPORT_SYMBOL(ocelot_bridge_stp_state_set
);
1434 static void ocelot_port_attr_stp_state_set(struct ocelot
*ocelot
, int port
,
1435 struct switchdev_trans
*trans
,
1438 if (switchdev_trans_ph_prepare(trans
))
1441 ocelot_bridge_stp_state_set(ocelot
, port
, state
);
1444 void ocelot_set_ageing_time(struct ocelot
*ocelot
, unsigned int msecs
)
1446 ocelot_write(ocelot
, ANA_AUTOAGE_AGE_PERIOD(msecs
/ 2),
1449 EXPORT_SYMBOL(ocelot_set_ageing_time
);
1451 static void ocelot_port_attr_ageing_set(struct ocelot
*ocelot
, int port
,
1452 unsigned long ageing_clock_t
)
1454 unsigned long ageing_jiffies
= clock_t_to_jiffies(ageing_clock_t
);
1455 u32 ageing_time
= jiffies_to_msecs(ageing_jiffies
) / 1000;
1457 ocelot_set_ageing_time(ocelot
, ageing_time
);
1460 static void ocelot_port_attr_mc_set(struct ocelot
*ocelot
, int port
, bool mc
)
1462 u32 cpu_fwd_mcast
= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA
|
1463 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA
|
1464 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA
;
1468 val
= cpu_fwd_mcast
;
1470 ocelot_rmw_gix(ocelot
, val
, cpu_fwd_mcast
,
1471 ANA_PORT_CPU_FWD_CFG
, port
);
1474 static int ocelot_port_attr_set(struct net_device
*dev
,
1475 const struct switchdev_attr
*attr
,
1476 struct switchdev_trans
*trans
)
1478 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1479 struct ocelot
*ocelot
= priv
->port
.ocelot
;
1480 int port
= priv
->chip_port
;
1484 case SWITCHDEV_ATTR_ID_PORT_STP_STATE
:
1485 ocelot_port_attr_stp_state_set(ocelot
, port
, trans
,
1488 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
:
1489 ocelot_port_attr_ageing_set(ocelot
, port
, attr
->u
.ageing_time
);
1491 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
:
1492 priv
->vlan_aware
= attr
->u
.vlan_filtering
;
1493 ocelot_port_vlan_filtering(ocelot
, port
, priv
->vlan_aware
);
1495 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED
:
1496 ocelot_port_attr_mc_set(ocelot
, port
, !attr
->u
.mc_disabled
);
1506 static int ocelot_port_obj_add_vlan(struct net_device
*dev
,
1507 const struct switchdev_obj_port_vlan
*vlan
,
1508 struct switchdev_trans
*trans
)
1513 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++) {
1514 ret
= ocelot_vlan_vid_add(dev
, vid
,
1515 vlan
->flags
& BRIDGE_VLAN_INFO_PVID
,
1516 vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
);
1524 static int ocelot_port_vlan_del_vlan(struct net_device
*dev
,
1525 const struct switchdev_obj_port_vlan
*vlan
)
1530 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++) {
1531 ret
= ocelot_vlan_vid_del(dev
, vid
);
1540 static struct ocelot_multicast
*ocelot_multicast_get(struct ocelot
*ocelot
,
1541 const unsigned char *addr
,
1544 struct ocelot_multicast
*mc
;
1546 list_for_each_entry(mc
, &ocelot
->multicast
, list
) {
1547 if (ether_addr_equal(mc
->addr
, addr
) && mc
->vid
== vid
)
1554 static int ocelot_port_obj_add_mdb(struct net_device
*dev
,
1555 const struct switchdev_obj_port_mdb
*mdb
,
1556 struct switchdev_trans
*trans
)
1558 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1559 struct ocelot_port
*ocelot_port
= &priv
->port
;
1560 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
1561 unsigned char addr
[ETH_ALEN
];
1562 struct ocelot_multicast
*mc
;
1563 int port
= priv
->chip_port
;
1568 vid
= ocelot_port
->pvid
;
1570 mc
= ocelot_multicast_get(ocelot
, mdb
->addr
, vid
);
1572 mc
= devm_kzalloc(ocelot
->dev
, sizeof(*mc
), GFP_KERNEL
);
1576 memcpy(mc
->addr
, mdb
->addr
, ETH_ALEN
);
1579 list_add_tail(&mc
->list
, &ocelot
->multicast
);
1583 memcpy(addr
, mc
->addr
, ETH_ALEN
);
1587 addr
[2] = mc
->ports
<< 0;
1588 addr
[1] = mc
->ports
<< 8;
1589 ocelot_mact_forget(ocelot
, addr
, vid
);
1592 mc
->ports
|= BIT(port
);
1593 addr
[2] = mc
->ports
<< 0;
1594 addr
[1] = mc
->ports
<< 8;
1596 return ocelot_mact_learn(ocelot
, 0, addr
, vid
, ENTRYTYPE_MACv4
);
1599 static int ocelot_port_obj_del_mdb(struct net_device
*dev
,
1600 const struct switchdev_obj_port_mdb
*mdb
)
1602 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1603 struct ocelot_port
*ocelot_port
= &priv
->port
;
1604 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
1605 unsigned char addr
[ETH_ALEN
];
1606 struct ocelot_multicast
*mc
;
1607 int port
= priv
->chip_port
;
1611 vid
= ocelot_port
->pvid
;
1613 mc
= ocelot_multicast_get(ocelot
, mdb
->addr
, vid
);
1617 memcpy(addr
, mc
->addr
, ETH_ALEN
);
1618 addr
[2] = mc
->ports
<< 0;
1619 addr
[1] = mc
->ports
<< 8;
1621 ocelot_mact_forget(ocelot
, addr
, vid
);
1623 mc
->ports
&= ~BIT(port
);
1625 list_del(&mc
->list
);
1626 devm_kfree(ocelot
->dev
, mc
);
1630 addr
[2] = mc
->ports
<< 0;
1631 addr
[1] = mc
->ports
<< 8;
1633 return ocelot_mact_learn(ocelot
, 0, addr
, vid
, ENTRYTYPE_MACv4
);
1636 static int ocelot_port_obj_add(struct net_device
*dev
,
1637 const struct switchdev_obj
*obj
,
1638 struct switchdev_trans
*trans
,
1639 struct netlink_ext_ack
*extack
)
1644 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
1645 ret
= ocelot_port_obj_add_vlan(dev
,
1646 SWITCHDEV_OBJ_PORT_VLAN(obj
),
1649 case SWITCHDEV_OBJ_ID_PORT_MDB
:
1650 ret
= ocelot_port_obj_add_mdb(dev
, SWITCHDEV_OBJ_PORT_MDB(obj
),
1660 static int ocelot_port_obj_del(struct net_device
*dev
,
1661 const struct switchdev_obj
*obj
)
1666 case SWITCHDEV_OBJ_ID_PORT_VLAN
:
1667 ret
= ocelot_port_vlan_del_vlan(dev
,
1668 SWITCHDEV_OBJ_PORT_VLAN(obj
));
1670 case SWITCHDEV_OBJ_ID_PORT_MDB
:
1671 ret
= ocelot_port_obj_del_mdb(dev
, SWITCHDEV_OBJ_PORT_MDB(obj
));
1680 int ocelot_port_bridge_join(struct ocelot
*ocelot
, int port
,
1681 struct net_device
*bridge
)
1683 if (!ocelot
->bridge_mask
) {
1684 ocelot
->hw_bridge_dev
= bridge
;
1686 if (ocelot
->hw_bridge_dev
!= bridge
)
1687 /* This is adding the port to a second bridge, this is
1692 ocelot
->bridge_mask
|= BIT(port
);
1696 EXPORT_SYMBOL(ocelot_port_bridge_join
);
1698 int ocelot_port_bridge_leave(struct ocelot
*ocelot
, int port
,
1699 struct net_device
*bridge
)
1701 ocelot
->bridge_mask
&= ~BIT(port
);
1703 if (!ocelot
->bridge_mask
)
1704 ocelot
->hw_bridge_dev
= NULL
;
1706 ocelot_port_vlan_filtering(ocelot
, port
, 0);
1707 ocelot_port_set_pvid(ocelot
, port
, 0);
1708 return ocelot_port_set_native_vlan(ocelot
, port
, 0);
1710 EXPORT_SYMBOL(ocelot_port_bridge_leave
);
1712 static void ocelot_set_aggr_pgids(struct ocelot
*ocelot
)
1716 /* Reset destination and aggregation PGIDS */
1717 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++)
1718 ocelot_write_rix(ocelot
, BIT(port
), ANA_PGID_PGID
, port
);
1720 for (i
= PGID_AGGR
; i
< PGID_SRC
; i
++)
1721 ocelot_write_rix(ocelot
, GENMASK(ocelot
->num_phys_ports
- 1, 0),
1724 /* Now, set PGIDs for each LAG */
1725 for (lag
= 0; lag
< ocelot
->num_phys_ports
; lag
++) {
1726 unsigned long bond_mask
;
1730 bond_mask
= ocelot
->lags
[lag
];
1734 for_each_set_bit(port
, &bond_mask
, ocelot
->num_phys_ports
) {
1736 ocelot_write_rix(ocelot
, bond_mask
,
1737 ANA_PGID_PGID
, port
);
1738 aggr_idx
[aggr_count
] = port
;
1742 for (i
= PGID_AGGR
; i
< PGID_SRC
; i
++) {
1745 ac
= ocelot_read_rix(ocelot
, ANA_PGID_PGID
, i
);
1747 ac
|= BIT(aggr_idx
[i
% aggr_count
]);
1748 ocelot_write_rix(ocelot
, ac
, ANA_PGID_PGID
, i
);
1753 static void ocelot_setup_lag(struct ocelot
*ocelot
, int lag
)
1755 unsigned long bond_mask
= ocelot
->lags
[lag
];
1758 for_each_set_bit(p
, &bond_mask
, ocelot
->num_phys_ports
) {
1759 u32 port_cfg
= ocelot_read_gix(ocelot
, ANA_PORT_PORT_CFG
, p
);
1761 port_cfg
&= ~ANA_PORT_PORT_CFG_PORTID_VAL_M
;
1763 /* Use lag port as logical port for port i */
1764 ocelot_write_gix(ocelot
, port_cfg
|
1765 ANA_PORT_PORT_CFG_PORTID_VAL(lag
),
1766 ANA_PORT_PORT_CFG
, p
);
1770 static int ocelot_port_lag_join(struct ocelot
*ocelot
, int port
,
1771 struct net_device
*bond
)
1773 struct net_device
*ndev
;
1778 for_each_netdev_in_bond_rcu(bond
, ndev
) {
1779 struct ocelot_port_private
*priv
= netdev_priv(ndev
);
1781 bond_mask
|= BIT(priv
->chip_port
);
1785 lp
= __ffs(bond_mask
);
1787 /* If the new port is the lowest one, use it as the logical port from
1792 ocelot
->lags
[port
] = bond_mask
;
1793 bond_mask
&= ~BIT(port
);
1795 lp
= __ffs(bond_mask
);
1796 ocelot
->lags
[lp
] = 0;
1800 ocelot
->lags
[lp
] |= BIT(port
);
1803 ocelot_setup_lag(ocelot
, lag
);
1804 ocelot_set_aggr_pgids(ocelot
);
1809 static void ocelot_port_lag_leave(struct ocelot
*ocelot
, int port
,
1810 struct net_device
*bond
)
1815 /* Remove port from any lag */
1816 for (i
= 0; i
< ocelot
->num_phys_ports
; i
++)
1817 ocelot
->lags
[i
] &= ~BIT(port
);
1819 /* if it was the logical port of the lag, move the lag config to the
1822 if (ocelot
->lags
[port
]) {
1823 int n
= __ffs(ocelot
->lags
[port
]);
1825 ocelot
->lags
[n
] = ocelot
->lags
[port
];
1826 ocelot
->lags
[port
] = 0;
1828 ocelot_setup_lag(ocelot
, n
);
1831 port_cfg
= ocelot_read_gix(ocelot
, ANA_PORT_PORT_CFG
, port
);
1832 port_cfg
&= ~ANA_PORT_PORT_CFG_PORTID_VAL_M
;
1833 ocelot_write_gix(ocelot
, port_cfg
| ANA_PORT_PORT_CFG_PORTID_VAL(port
),
1834 ANA_PORT_PORT_CFG
, port
);
1836 ocelot_set_aggr_pgids(ocelot
);
1839 /* Checks if the net_device instance given to us originate from our driver. */
1840 static bool ocelot_netdevice_dev_check(const struct net_device
*dev
)
1842 return dev
->netdev_ops
== &ocelot_port_netdev_ops
;
1845 static int ocelot_netdevice_port_event(struct net_device
*dev
,
1846 unsigned long event
,
1847 struct netdev_notifier_changeupper_info
*info
)
1849 struct ocelot_port_private
*priv
= netdev_priv(dev
);
1850 struct ocelot_port
*ocelot_port
= &priv
->port
;
1851 struct ocelot
*ocelot
= ocelot_port
->ocelot
;
1852 int port
= priv
->chip_port
;
1856 case NETDEV_CHANGEUPPER
:
1857 if (netif_is_bridge_master(info
->upper_dev
)) {
1858 if (info
->linking
) {
1859 err
= ocelot_port_bridge_join(ocelot
, port
,
1862 err
= ocelot_port_bridge_leave(ocelot
, port
,
1864 priv
->vlan_aware
= false;
1867 if (netif_is_lag_master(info
->upper_dev
)) {
1869 err
= ocelot_port_lag_join(ocelot
, port
,
1872 ocelot_port_lag_leave(ocelot
, port
,
1883 static int ocelot_netdevice_event(struct notifier_block
*unused
,
1884 unsigned long event
, void *ptr
)
1886 struct netdev_notifier_changeupper_info
*info
= ptr
;
1887 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1890 if (!ocelot_netdevice_dev_check(dev
))
1893 if (event
== NETDEV_PRECHANGEUPPER
&&
1894 netif_is_lag_master(info
->upper_dev
)) {
1895 struct netdev_lag_upper_info
*lag_upper_info
= info
->upper_info
;
1896 struct netlink_ext_ack
*extack
;
1898 if (lag_upper_info
&&
1899 lag_upper_info
->tx_type
!= NETDEV_LAG_TX_TYPE_HASH
) {
1900 extack
= netdev_notifier_info_to_extack(&info
->info
);
1901 NL_SET_ERR_MSG_MOD(extack
, "LAG device using unsupported Tx type");
1908 if (netif_is_lag_master(dev
)) {
1909 struct net_device
*slave
;
1910 struct list_head
*iter
;
1912 netdev_for_each_lower_dev(dev
, slave
, iter
) {
1913 ret
= ocelot_netdevice_port_event(slave
, event
, info
);
1918 ret
= ocelot_netdevice_port_event(dev
, event
, info
);
1922 return notifier_from_errno(ret
);
1925 struct notifier_block ocelot_netdevice_nb __read_mostly
= {
1926 .notifier_call
= ocelot_netdevice_event
,
1928 EXPORT_SYMBOL(ocelot_netdevice_nb
);
1930 static int ocelot_switchdev_event(struct notifier_block
*unused
,
1931 unsigned long event
, void *ptr
)
1933 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
1937 case SWITCHDEV_PORT_ATTR_SET
:
1938 err
= switchdev_handle_port_attr_set(dev
, ptr
,
1939 ocelot_netdevice_dev_check
,
1940 ocelot_port_attr_set
);
1941 return notifier_from_errno(err
);
1947 struct notifier_block ocelot_switchdev_nb __read_mostly
= {
1948 .notifier_call
= ocelot_switchdev_event
,
1950 EXPORT_SYMBOL(ocelot_switchdev_nb
);
1952 static int ocelot_switchdev_blocking_event(struct notifier_block
*unused
,
1953 unsigned long event
, void *ptr
)
1955 struct net_device
*dev
= switchdev_notifier_info_to_dev(ptr
);
1959 /* Blocking events. */
1960 case SWITCHDEV_PORT_OBJ_ADD
:
1961 err
= switchdev_handle_port_obj_add(dev
, ptr
,
1962 ocelot_netdevice_dev_check
,
1963 ocelot_port_obj_add
);
1964 return notifier_from_errno(err
);
1965 case SWITCHDEV_PORT_OBJ_DEL
:
1966 err
= switchdev_handle_port_obj_del(dev
, ptr
,
1967 ocelot_netdevice_dev_check
,
1968 ocelot_port_obj_del
);
1969 return notifier_from_errno(err
);
1970 case SWITCHDEV_PORT_ATTR_SET
:
1971 err
= switchdev_handle_port_attr_set(dev
, ptr
,
1972 ocelot_netdevice_dev_check
,
1973 ocelot_port_attr_set
);
1974 return notifier_from_errno(err
);
1980 struct notifier_block ocelot_switchdev_blocking_nb __read_mostly
= {
1981 .notifier_call
= ocelot_switchdev_blocking_event
,
1983 EXPORT_SYMBOL(ocelot_switchdev_blocking_nb
);
1985 int ocelot_ptp_gettime64(struct ptp_clock_info
*ptp
, struct timespec64
*ts
)
1987 struct ocelot
*ocelot
= container_of(ptp
, struct ocelot
, ptp_info
);
1988 unsigned long flags
;
1993 spin_lock_irqsave(&ocelot
->ptp_clock_lock
, flags
);
1995 val
= ocelot_read_rix(ocelot
, PTP_PIN_CFG
, TOD_ACC_PIN
);
1996 val
&= ~(PTP_PIN_CFG_SYNC
| PTP_PIN_CFG_ACTION_MASK
| PTP_PIN_CFG_DOM
);
1997 val
|= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE
);
1998 ocelot_write_rix(ocelot
, val
, PTP_PIN_CFG
, TOD_ACC_PIN
);
2000 s
= ocelot_read_rix(ocelot
, PTP_PIN_TOD_SEC_MSB
, TOD_ACC_PIN
) & 0xffff;
2002 s
+= ocelot_read_rix(ocelot
, PTP_PIN_TOD_SEC_LSB
, TOD_ACC_PIN
);
2003 ns
= ocelot_read_rix(ocelot
, PTP_PIN_TOD_NSEC
, TOD_ACC_PIN
);
2005 spin_unlock_irqrestore(&ocelot
->ptp_clock_lock
, flags
);
2007 /* Deal with negative values */
2008 if (ns
>= 0x3ffffff0 && ns
<= 0x3fffffff) {
2014 set_normalized_timespec64(ts
, s
, ns
);
2017 EXPORT_SYMBOL(ocelot_ptp_gettime64
);
2019 static int ocelot_ptp_settime64(struct ptp_clock_info
*ptp
,
2020 const struct timespec64
*ts
)
2022 struct ocelot
*ocelot
= container_of(ptp
, struct ocelot
, ptp_info
);
2023 unsigned long flags
;
2026 spin_lock_irqsave(&ocelot
->ptp_clock_lock
, flags
);
2028 val
= ocelot_read_rix(ocelot
, PTP_PIN_CFG
, TOD_ACC_PIN
);
2029 val
&= ~(PTP_PIN_CFG_SYNC
| PTP_PIN_CFG_ACTION_MASK
| PTP_PIN_CFG_DOM
);
2030 val
|= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE
);
2032 ocelot_write_rix(ocelot
, val
, PTP_PIN_CFG
, TOD_ACC_PIN
);
2034 ocelot_write_rix(ocelot
, lower_32_bits(ts
->tv_sec
), PTP_PIN_TOD_SEC_LSB
,
2036 ocelot_write_rix(ocelot
, upper_32_bits(ts
->tv_sec
), PTP_PIN_TOD_SEC_MSB
,
2038 ocelot_write_rix(ocelot
, ts
->tv_nsec
, PTP_PIN_TOD_NSEC
, TOD_ACC_PIN
);
2040 val
= ocelot_read_rix(ocelot
, PTP_PIN_CFG
, TOD_ACC_PIN
);
2041 val
&= ~(PTP_PIN_CFG_SYNC
| PTP_PIN_CFG_ACTION_MASK
| PTP_PIN_CFG_DOM
);
2042 val
|= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD
);
2044 ocelot_write_rix(ocelot
, val
, PTP_PIN_CFG
, TOD_ACC_PIN
);
2046 spin_unlock_irqrestore(&ocelot
->ptp_clock_lock
, flags
);
2050 static int ocelot_ptp_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
2052 if (delta
> -(NSEC_PER_SEC
/ 2) && delta
< (NSEC_PER_SEC
/ 2)) {
2053 struct ocelot
*ocelot
= container_of(ptp
, struct ocelot
, ptp_info
);
2054 unsigned long flags
;
2057 spin_lock_irqsave(&ocelot
->ptp_clock_lock
, flags
);
2059 val
= ocelot_read_rix(ocelot
, PTP_PIN_CFG
, TOD_ACC_PIN
);
2060 val
&= ~(PTP_PIN_CFG_SYNC
| PTP_PIN_CFG_ACTION_MASK
| PTP_PIN_CFG_DOM
);
2061 val
|= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE
);
2063 ocelot_write_rix(ocelot
, val
, PTP_PIN_CFG
, TOD_ACC_PIN
);
2065 ocelot_write_rix(ocelot
, 0, PTP_PIN_TOD_SEC_LSB
, TOD_ACC_PIN
);
2066 ocelot_write_rix(ocelot
, 0, PTP_PIN_TOD_SEC_MSB
, TOD_ACC_PIN
);
2067 ocelot_write_rix(ocelot
, delta
, PTP_PIN_TOD_NSEC
, TOD_ACC_PIN
);
2069 val
= ocelot_read_rix(ocelot
, PTP_PIN_CFG
, TOD_ACC_PIN
);
2070 val
&= ~(PTP_PIN_CFG_SYNC
| PTP_PIN_CFG_ACTION_MASK
| PTP_PIN_CFG_DOM
);
2071 val
|= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA
);
2073 ocelot_write_rix(ocelot
, val
, PTP_PIN_CFG
, TOD_ACC_PIN
);
2075 spin_unlock_irqrestore(&ocelot
->ptp_clock_lock
, flags
);
2077 /* Fall back using ocelot_ptp_settime64 which is not exact. */
2078 struct timespec64 ts
;
2081 ocelot_ptp_gettime64(ptp
, &ts
);
2083 now
= ktime_to_ns(timespec64_to_ktime(ts
));
2084 ts
= ns_to_timespec64(now
+ delta
);
2086 ocelot_ptp_settime64(ptp
, &ts
);
2091 static int ocelot_ptp_adjfine(struct ptp_clock_info
*ptp
, long scaled_ppm
)
2093 struct ocelot
*ocelot
= container_of(ptp
, struct ocelot
, ptp_info
);
2094 u32 unit
= 0, direction
= 0;
2095 unsigned long flags
;
2098 spin_lock_irqsave(&ocelot
->ptp_clock_lock
, flags
);
2103 if (scaled_ppm
< 0) {
2104 direction
= PTP_CFG_CLK_ADJ_CFG_DIR
;
2105 scaled_ppm
= -scaled_ppm
;
2108 adj
= PSEC_PER_SEC
<< 16;
2109 do_div(adj
, scaled_ppm
);
2112 /* If the adjustment value is too large, use ns instead */
2113 if (adj
>= (1L << 30)) {
2114 unit
= PTP_CFG_CLK_ADJ_FREQ_NS
;
2119 if (adj
>= (1L << 30))
2122 ocelot_write(ocelot
, unit
| adj
, PTP_CLK_CFG_ADJ_FREQ
);
2123 ocelot_write(ocelot
, PTP_CFG_CLK_ADJ_CFG_ENA
| direction
,
2124 PTP_CLK_CFG_ADJ_CFG
);
2126 spin_unlock_irqrestore(&ocelot
->ptp_clock_lock
, flags
);
2130 ocelot_write(ocelot
, 0, PTP_CLK_CFG_ADJ_CFG
);
2132 spin_unlock_irqrestore(&ocelot
->ptp_clock_lock
, flags
);
2136 static struct ptp_clock_info ocelot_ptp_clock_info
= {
2137 .owner
= THIS_MODULE
,
2138 .name
= "ocelot ptp",
2139 .max_adj
= 0x7fffffff,
2145 .gettime64
= ocelot_ptp_gettime64
,
2146 .settime64
= ocelot_ptp_settime64
,
2147 .adjtime
= ocelot_ptp_adjtime
,
2148 .adjfine
= ocelot_ptp_adjfine
,
2151 static int ocelot_init_timestamp(struct ocelot
*ocelot
)
2153 struct ptp_clock
*ptp_clock
;
2155 ocelot
->ptp_info
= ocelot_ptp_clock_info
;
2156 ptp_clock
= ptp_clock_register(&ocelot
->ptp_info
, ocelot
->dev
);
2157 if (IS_ERR(ptp_clock
))
2158 return PTR_ERR(ptp_clock
);
2159 /* Check if PHC support is missing at the configuration level */
2163 ocelot
->ptp_clock
= ptp_clock
;
2165 ocelot_write(ocelot
, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG
);
2166 ocelot_write(ocelot
, 0xffffffff, ANA_TABLES_PTP_ID_LOW
);
2167 ocelot_write(ocelot
, 0xffffffff, ANA_TABLES_PTP_ID_HIGH
);
2169 ocelot_write(ocelot
, PTP_CFG_MISC_PTP_EN
, PTP_CFG_MISC
);
2171 /* There is no device reconfiguration, PTP Rx stamping is always
2174 ocelot
->hwtstamp_config
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
2179 static void ocelot_port_set_mtu(struct ocelot
*ocelot
, int port
, size_t mtu
)
2181 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
2184 ocelot_port_writel(ocelot_port
, mtu
, DEV_MAC_MAXLEN_CFG
);
2186 /* Set Pause WM hysteresis
2187 * 152 = 6 * mtu / OCELOT_BUFFER_CELL_SZ
2188 * 101 = 4 * mtu / OCELOT_BUFFER_CELL_SZ
2190 ocelot_write_rix(ocelot
, SYS_PAUSE_CFG_PAUSE_ENA
|
2191 SYS_PAUSE_CFG_PAUSE_STOP(101) |
2192 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG
, port
);
2194 /* Tail dropping watermark */
2195 atop_wm
= (ocelot
->shared_queue_sz
- 9 * mtu
) / OCELOT_BUFFER_CELL_SZ
;
2196 ocelot_write_rix(ocelot
, ocelot_wm_enc(9 * mtu
),
2198 ocelot_write(ocelot
, ocelot_wm_enc(atop_wm
), SYS_ATOP_TOT_CFG
);
2201 void ocelot_init_port(struct ocelot
*ocelot
, int port
)
2203 struct ocelot_port
*ocelot_port
= ocelot
->ports
[port
];
2205 skb_queue_head_init(&ocelot_port
->tx_skbs
);
2207 /* Basic L2 initialization */
2210 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
2211 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
2213 ocelot_port_writel(ocelot_port
, DEV_MAC_IFG_CFG_TX_IFG(5),
2216 /* Load seed (0) and set MAC HDX late collision */
2217 ocelot_port_writel(ocelot_port
, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
2218 DEV_MAC_HDX_CFG_SEED_LOAD
,
2221 ocelot_port_writel(ocelot_port
, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
2224 /* Set Max Length and maximum tags allowed */
2225 ocelot_port_set_mtu(ocelot
, port
, VLAN_ETH_FRAME_LEN
);
2226 ocelot_port_writel(ocelot_port
, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD
) |
2227 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA
|
2228 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA
,
2231 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
2232 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_FC_MAC_HIGH_CFG
);
2233 ocelot_port_writel(ocelot_port
, 0, DEV_MAC_FC_MAC_LOW_CFG
);
2235 /* Drop frames with multicast source address */
2236 ocelot_rmw_gix(ocelot
, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA
,
2237 ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA
,
2238 ANA_PORT_DROP_CFG
, port
);
2240 /* Set default VLAN and tag type to 8021Q. */
2241 ocelot_rmw_gix(ocelot
, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q
),
2242 REW_PORT_VLAN_CFG_PORT_TPID_M
,
2243 REW_PORT_VLAN_CFG
, port
);
2245 /* Enable vcap lookups */
2246 ocelot_vcap_enable(ocelot
, port
);
2248 EXPORT_SYMBOL(ocelot_init_port
);
2250 int ocelot_probe_port(struct ocelot
*ocelot
, u8 port
,
2252 struct phy_device
*phy
)
2254 struct ocelot_port_private
*priv
;
2255 struct ocelot_port
*ocelot_port
;
2256 struct net_device
*dev
;
2259 dev
= alloc_etherdev(sizeof(struct ocelot_port_private
));
2262 SET_NETDEV_DEV(dev
, ocelot
->dev
);
2263 priv
= netdev_priv(dev
);
2266 priv
->chip_port
= port
;
2267 ocelot_port
= &priv
->port
;
2268 ocelot_port
->ocelot
= ocelot
;
2269 ocelot_port
->regs
= regs
;
2270 ocelot
->ports
[port
] = ocelot_port
;
2272 dev
->netdev_ops
= &ocelot_port_netdev_ops
;
2273 dev
->ethtool_ops
= &ocelot_ethtool_ops
;
2275 dev
->hw_features
|= NETIF_F_HW_VLAN_CTAG_FILTER
| NETIF_F_RXFCS
|
2277 dev
->features
|= NETIF_F_HW_VLAN_CTAG_FILTER
| NETIF_F_HW_TC
;
2279 memcpy(dev
->dev_addr
, ocelot
->base_mac
, ETH_ALEN
);
2280 dev
->dev_addr
[ETH_ALEN
- 1] += port
;
2281 ocelot_mact_learn(ocelot
, PGID_CPU
, dev
->dev_addr
, ocelot_port
->pvid
,
2284 ocelot_init_port(ocelot
, port
);
2286 err
= register_netdev(dev
);
2288 dev_err(ocelot
->dev
, "register_netdev failed\n");
2294 EXPORT_SYMBOL(ocelot_probe_port
);
2296 void ocelot_set_cpu_port(struct ocelot
*ocelot
, int cpu
,
2297 enum ocelot_tag_prefix injection
,
2298 enum ocelot_tag_prefix extraction
)
2300 /* Configure and enable the CPU port. */
2301 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, cpu
);
2302 ocelot_write_rix(ocelot
, BIT(cpu
), ANA_PGID_PGID
, PGID_CPU
);
2303 ocelot_write_gix(ocelot
, ANA_PORT_PORT_CFG_RECV_ENA
|
2304 ANA_PORT_PORT_CFG_PORTID_VAL(cpu
),
2305 ANA_PORT_PORT_CFG
, cpu
);
2307 /* If the CPU port is a physical port, set up the port in Node
2308 * Processor Interface (NPI) mode. This is the mode through which
2309 * frames can be injected from and extracted to an external CPU.
2310 * Only one port can be an NPI at the same time.
2312 if (cpu
< ocelot
->num_phys_ports
) {
2313 int mtu
= VLAN_ETH_FRAME_LEN
+ OCELOT_TAG_LEN
;
2315 ocelot_write(ocelot
, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M
|
2316 QSYS_EXT_CPU_CFG_EXT_CPU_PORT(cpu
),
2319 if (injection
== OCELOT_TAG_PREFIX_SHORT
)
2320 mtu
+= OCELOT_SHORT_PREFIX_LEN
;
2321 else if (injection
== OCELOT_TAG_PREFIX_LONG
)
2322 mtu
+= OCELOT_LONG_PREFIX_LEN
;
2324 ocelot_port_set_mtu(ocelot
, cpu
, mtu
);
2327 /* CPU port Injection/Extraction configuration */
2328 ocelot_write_rix(ocelot
, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE
|
2329 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2330 QSYS_SWITCH_PORT_MODE_PORT_ENA
,
2331 QSYS_SWITCH_PORT_MODE
, cpu
);
2332 ocelot_write_rix(ocelot
, SYS_PORT_MODE_INCL_XTR_HDR(extraction
) |
2333 SYS_PORT_MODE_INCL_INJ_HDR(injection
),
2334 SYS_PORT_MODE
, cpu
);
2336 /* Configure the CPU port to be VLAN aware */
2337 ocelot_write_gix(ocelot
, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
2338 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA
|
2339 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
2340 ANA_PORT_VLAN_CFG
, cpu
);
2344 EXPORT_SYMBOL(ocelot_set_cpu_port
);
2346 int ocelot_init(struct ocelot
*ocelot
)
2348 char queue_name
[32];
2352 if (ocelot
->ops
->reset
) {
2353 ret
= ocelot
->ops
->reset(ocelot
);
2355 dev_err(ocelot
->dev
, "Switch reset failed\n");
2360 ocelot
->lags
= devm_kcalloc(ocelot
->dev
, ocelot
->num_phys_ports
,
2361 sizeof(u32
), GFP_KERNEL
);
2365 ocelot
->stats
= devm_kcalloc(ocelot
->dev
,
2366 ocelot
->num_phys_ports
* ocelot
->num_stats
,
2367 sizeof(u64
), GFP_KERNEL
);
2371 mutex_init(&ocelot
->stats_lock
);
2372 mutex_init(&ocelot
->ptp_lock
);
2373 spin_lock_init(&ocelot
->ptp_clock_lock
);
2374 snprintf(queue_name
, sizeof(queue_name
), "%s-stats",
2375 dev_name(ocelot
->dev
));
2376 ocelot
->stats_queue
= create_singlethread_workqueue(queue_name
);
2377 if (!ocelot
->stats_queue
)
2380 INIT_LIST_HEAD(&ocelot
->multicast
);
2381 ocelot_mact_init(ocelot
);
2382 ocelot_vlan_init(ocelot
);
2383 ocelot_ace_init(ocelot
);
2385 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
2386 /* Clear all counters (5 groups) */
2387 ocelot_write(ocelot
, SYS_STAT_CFG_STAT_VIEW(port
) |
2388 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2392 /* Only use S-Tag */
2393 ocelot_write(ocelot
, ETH_P_8021AD
, SYS_VLAN_ETYPE_CFG
);
2395 /* Aggregation mode */
2396 ocelot_write(ocelot
, ANA_AGGR_CFG_AC_SMAC_ENA
|
2397 ANA_AGGR_CFG_AC_DMAC_ENA
|
2398 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA
|
2399 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA
, ANA_AGGR_CFG
);
2401 /* Set MAC age time to default value. The entry is aged after
2404 ocelot_write(ocelot
,
2405 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME
/ 2 / HZ
),
2408 /* Disable learning for frames discarded by VLAN ingress filtering */
2409 regmap_field_write(ocelot
->regfields
[ANA_ADVLEARN_VLAN_CHK
], 1);
2411 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2412 ocelot_write(ocelot
, SYS_FRM_AGING_AGE_TX_ENA
|
2413 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING
);
2415 /* Setup flooding PGIDs */
2416 ocelot_write_rix(ocelot
, ANA_FLOODING_FLD_MULTICAST(PGID_MC
) |
2417 ANA_FLOODING_FLD_BROADCAST(PGID_MC
) |
2418 ANA_FLOODING_FLD_UNICAST(PGID_UC
),
2420 ocelot_write(ocelot
, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6
) |
2421 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC
) |
2422 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4
) |
2423 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC
),
2426 for (port
= 0; port
< ocelot
->num_phys_ports
; port
++) {
2427 /* Transmit the frame to the local port. */
2428 ocelot_write_rix(ocelot
, BIT(port
), ANA_PGID_PGID
, port
);
2429 /* Do not forward BPDU frames to the front ports. */
2430 ocelot_write_gix(ocelot
,
2431 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2432 ANA_PORT_CPU_FWD_BPDU_CFG
,
2434 /* Ensure bridging is disabled */
2435 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_SRC
+ port
);
2438 /* Allow broadcast MAC frames. */
2439 for (i
= ocelot
->num_phys_ports
+ 1; i
< PGID_CPU
; i
++) {
2440 u32 val
= ANA_PGID_PGID_PGID(GENMASK(ocelot
->num_phys_ports
- 1, 0));
2442 ocelot_write_rix(ocelot
, val
, ANA_PGID_PGID
, i
);
2444 ocelot_write_rix(ocelot
,
2445 ANA_PGID_PGID_PGID(GENMASK(ocelot
->num_phys_ports
, 0)),
2446 ANA_PGID_PGID
, PGID_MC
);
2447 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_MCIPV4
);
2448 ocelot_write_rix(ocelot
, 0, ANA_PGID_PGID
, PGID_MCIPV6
);
2450 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
2451 * registers endianness.
2453 ocelot_write_rix(ocelot
, QS_INJ_GRP_CFG_BYTE_SWAP
|
2454 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG
, 0);
2455 ocelot_write_rix(ocelot
, QS_XTR_GRP_CFG_BYTE_SWAP
|
2456 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG
, 0);
2457 ocelot_write(ocelot
, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2458 ANA_CPUQ_CFG_CPUQ_LRN(2) |
2459 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2460 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2461 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2462 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2463 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2464 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2465 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG
);
2466 for (i
= 0; i
< 16; i
++)
2467 ocelot_write_rix(ocelot
, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2468 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2469 ANA_CPUQ_8021_CFG
, i
);
2471 INIT_DELAYED_WORK(&ocelot
->stats_work
, ocelot_check_stats_work
);
2472 queue_delayed_work(ocelot
->stats_queue
, &ocelot
->stats_work
,
2473 OCELOT_STATS_CHECK_DELAY
);
2476 ret
= ocelot_init_timestamp(ocelot
);
2478 dev_err(ocelot
->dev
,
2479 "Timestamp initialization failed\n");
2486 EXPORT_SYMBOL(ocelot_init
);
2488 void ocelot_deinit(struct ocelot
*ocelot
)
2490 struct ocelot_port
*port
;
2493 cancel_delayed_work(&ocelot
->stats_work
);
2494 destroy_workqueue(ocelot
->stats_queue
);
2495 mutex_destroy(&ocelot
->stats_lock
);
2496 ocelot_ace_deinit();
2497 if (ocelot
->ptp_clock
)
2498 ptp_clock_unregister(ocelot
->ptp_clock
);
2500 for (i
= 0; i
< ocelot
->num_phys_ports
; i
++) {
2501 port
= ocelot
->ports
[i
];
2502 skb_queue_purge(&port
->tx_skbs
);
2505 EXPORT_SYMBOL(ocelot_deinit
);
2507 MODULE_LICENSE("Dual MIT/GPL");