Skip to content

Commit 4e50d72

Browse files
Danielmachondavem330
authored andcommitted
net: sparx5: add port mirroring implementation
The hardware supports three independent mirroring probes. Each probe can be configured to mirror rx or tx traffic (direction). Using tc matchall, it is now possible to add a source port and a monitor port to a mirror probe. Depending on the mirror direction, rx or tx traffic from a source port will be mirrored to the monitor port. A single source port can be a member of multiple mirror probes. Signed-off-by: Daniel Machon <daniel.machon@microchip.com> Reviewed-by: Steen Hegelund <Steen.Hegelund@microchip.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1ede4ac commit 4e50d72

File tree

3 files changed

+212
-1
lines changed

3 files changed

+212
-1
lines changed

drivers/net/ethernet/microchip/sparx5/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ sparx5-switch-y := sparx5_main.o sparx5_packet.o \
1010
sparx5_switchdev.o sparx5_calendar.o sparx5_ethtool.o sparx5_fdma.o \
1111
sparx5_ptp.o sparx5_pgid.o sparx5_tc.o sparx5_qos.o \
1212
sparx5_vcap_impl.o sparx5_vcap_ag_api.o sparx5_tc_flower.o \
13-
sparx5_tc_matchall.o sparx5_pool.o sparx5_sdlb.o sparx5_police.o sparx5_psfp.o
13+
sparx5_tc_matchall.o sparx5_pool.o sparx5_sdlb.o sparx5_police.o \
14+
sparx5_psfp.o sparx5_mirror.o
1415

1516
sparx5-switch-$(CONFIG_SPARX5_DCB) += sparx5_dcb.o
1617
sparx5-switch-$(CONFIG_DEBUG_FS) += sparx5_vcap_debugfs.o

drivers/net/ethernet/microchip/sparx5/sparx5_main.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,20 @@ struct sparx5_mdb_entry {
228228
u16 pgid_idx;
229229
};
230230

231+
struct sparx5_mall_mirror_entry {
232+
u32 idx;
233+
struct sparx5_port *port;
234+
};
235+
231236
struct sparx5_mall_entry {
232237
struct list_head list;
233238
struct sparx5_port *port;
234239
unsigned long cookie;
235240
enum flow_action_id type;
236241
bool ingress;
242+
union {
243+
struct sparx5_mall_mirror_entry mirror;
244+
};
237245
};
238246

239247
#define SPARX5_PTP_TIMEOUT msecs_to_jiffies(10)
@@ -551,6 +559,10 @@ void sparx5_psfp_init(struct sparx5 *sparx5);
551559
void sparx5_new_base_time(struct sparx5 *sparx5, const u32 cycle_time,
552560
const ktime_t org_base_time, ktime_t *new_base_time);
553561

562+
/* sparx5_mirror.c */
563+
int sparx5_mirror_add(struct sparx5_mall_entry *entry);
564+
void sparx5_mirror_del(struct sparx5_mall_entry *entry);
565+
554566
/* Clock period in picoseconds */
555567
static inline u32 sparx5_clk_period(enum sparx5_core_clockfreq cclock)
556568
{
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/* Microchip Sparx5 Switch driver
3+
*
4+
* Copyright (c) 2024 Microchip Technology Inc. and its subsidiaries.
5+
*/
6+
7+
#include "sparx5_main.h"
8+
#include "sparx5_main_regs.h"
9+
#include "sparx5_tc.h"
10+
11+
#define SPX5_MIRROR_PROBE_MAX 3
12+
#define SPX5_MIRROR_DISABLED 0
13+
#define SPX5_MIRROR_EGRESS 1
14+
#define SPX5_MIRROR_INGRESS 2
15+
#define SPX5_MIRROR_MONITOR_PORT_DEFAULT 65
16+
#define SPX5_QFWD_MP_OFFSET 9 /* Mirror port offset in the QFWD register */
17+
18+
/* Convert from bool ingress/egress to mirror direction */
19+
static u32 sparx5_mirror_to_dir(bool ingress)
20+
{
21+
return ingress ? SPX5_MIRROR_INGRESS : SPX5_MIRROR_EGRESS;
22+
}
23+
24+
/* Get ports belonging to this mirror */
25+
static u64 sparx5_mirror_port_get(struct sparx5 *sparx5, u32 idx)
26+
{
27+
return (u64)spx5_rd(sparx5, ANA_AC_PROBE_PORT_CFG1(idx)) << 32 |
28+
spx5_rd(sparx5, ANA_AC_PROBE_PORT_CFG(idx));
29+
}
30+
31+
/* Add port to mirror (only front ports) */
32+
static void sparx5_mirror_port_add(struct sparx5 *sparx5, u32 idx, u32 portno)
33+
{
34+
u32 val, reg = portno;
35+
36+
reg = portno / BITS_PER_BYTE;
37+
val = BIT(portno % BITS_PER_BYTE);
38+
39+
if (reg == 0)
40+
return spx5_rmw(val, val, sparx5, ANA_AC_PROBE_PORT_CFG(idx));
41+
else
42+
return spx5_rmw(val, val, sparx5, ANA_AC_PROBE_PORT_CFG1(idx));
43+
}
44+
45+
/* Delete port from mirror (only front ports) */
46+
static void sparx5_mirror_port_del(struct sparx5 *sparx5, u32 idx, u32 portno)
47+
{
48+
u32 val, reg = portno;
49+
50+
reg = portno / BITS_PER_BYTE;
51+
val = BIT(portno % BITS_PER_BYTE);
52+
53+
if (reg == 0)
54+
return spx5_rmw(0, val, sparx5, ANA_AC_PROBE_PORT_CFG(idx));
55+
else
56+
return spx5_rmw(0, val, sparx5, ANA_AC_PROBE_PORT_CFG1(idx));
57+
}
58+
59+
/* Check if mirror contains port */
60+
static bool sparx5_mirror_contains(struct sparx5 *sparx5, u32 idx, u32 portno)
61+
{
62+
return (sparx5_mirror_port_get(sparx5, idx) & BIT_ULL(portno)) != 0;
63+
}
64+
65+
/* Check if mirror is empty */
66+
static bool sparx5_mirror_is_empty(struct sparx5 *sparx5, u32 idx)
67+
{
68+
return sparx5_mirror_port_get(sparx5, idx) == 0;
69+
}
70+
71+
/* Get direction of mirror */
72+
static u32 sparx5_mirror_dir_get(struct sparx5 *sparx5, u32 idx)
73+
{
74+
u32 val = spx5_rd(sparx5, ANA_AC_PROBE_CFG(idx));
75+
76+
return ANA_AC_PROBE_CFG_PROBE_DIRECTION_GET(val);
77+
}
78+
79+
/* Set direction of mirror */
80+
static void sparx5_mirror_dir_set(struct sparx5 *sparx5, u32 idx, u32 dir)
81+
{
82+
spx5_rmw(ANA_AC_PROBE_CFG_PROBE_DIRECTION_SET(dir),
83+
ANA_AC_PROBE_CFG_PROBE_DIRECTION, sparx5,
84+
ANA_AC_PROBE_CFG(idx));
85+
}
86+
87+
/* Set the monitor port for this mirror */
88+
static void sparx5_mirror_monitor_set(struct sparx5 *sparx5, u32 idx,
89+
u32 portno)
90+
{
91+
spx5_rmw(QFWD_FRAME_COPY_CFG_FRMC_PORT_VAL_SET(portno),
92+
QFWD_FRAME_COPY_CFG_FRMC_PORT_VAL, sparx5,
93+
QFWD_FRAME_COPY_CFG(idx + SPX5_QFWD_MP_OFFSET));
94+
}
95+
96+
/* Get the monitor port of this mirror */
97+
static u32 sparx5_mirror_monitor_get(struct sparx5 *sparx5, u32 idx)
98+
{
99+
u32 val = spx5_rd(sparx5,
100+
QFWD_FRAME_COPY_CFG(idx + SPX5_QFWD_MP_OFFSET));
101+
102+
return QFWD_FRAME_COPY_CFG_FRMC_PORT_VAL_GET(val);
103+
}
104+
105+
/* Check if port is the monitor port of this mirror */
106+
static bool sparx5_mirror_has_monitor(struct sparx5 *sparx5, u32 idx,
107+
u32 portno)
108+
{
109+
return sparx5_mirror_monitor_get(sparx5, idx) == portno;
110+
}
111+
112+
/* Get a suitable mirror for this port */
113+
static int sparx5_mirror_get(struct sparx5_port *sport,
114+
struct sparx5_port *mport, u32 dir, u32 *idx)
115+
{
116+
struct sparx5 *sparx5 = sport->sparx5;
117+
u32 i;
118+
119+
/* Check if this port is already used as a monitor port */
120+
for (i = 0; i < SPX5_MIRROR_PROBE_MAX; i++)
121+
if (sparx5_mirror_has_monitor(sparx5, i, sport->portno))
122+
return -EINVAL;
123+
124+
/* Check if existing mirror can be reused
125+
* (same direction and monitor port).
126+
*/
127+
for (i = 0; i < SPX5_MIRROR_PROBE_MAX; i++) {
128+
if (sparx5_mirror_dir_get(sparx5, i) == dir &&
129+
sparx5_mirror_has_monitor(sparx5, i, mport->portno)) {
130+
*idx = i;
131+
return 0;
132+
}
133+
}
134+
135+
/* Return free mirror */
136+
for (i = 0; i < SPX5_MIRROR_PROBE_MAX; i++) {
137+
if (sparx5_mirror_is_empty(sparx5, i)) {
138+
*idx = i;
139+
return 0;
140+
}
141+
}
142+
143+
return -ENOENT;
144+
}
145+
146+
int sparx5_mirror_add(struct sparx5_mall_entry *entry)
147+
{
148+
u32 mirror_idx, dir = sparx5_mirror_to_dir(entry->ingress);
149+
struct sparx5_port *sport, *mport;
150+
struct sparx5 *sparx5;
151+
int err;
152+
153+
/* Source port */
154+
sport = entry->port;
155+
/* monitor port */
156+
mport = entry->mirror.port;
157+
sparx5 = sport->sparx5;
158+
159+
if (sport->portno == mport->portno)
160+
return -EINVAL;
161+
162+
err = sparx5_mirror_get(sport, mport, dir, &mirror_idx);
163+
if (err)
164+
return err;
165+
166+
if (sparx5_mirror_contains(sparx5, mirror_idx, sport->portno))
167+
return -EEXIST;
168+
169+
/* Add port to mirror */
170+
sparx5_mirror_port_add(sparx5, mirror_idx, sport->portno);
171+
172+
/* Set direction of mirror */
173+
sparx5_mirror_dir_set(sparx5, mirror_idx, dir);
174+
175+
/* Set monitor port for mirror */
176+
sparx5_mirror_monitor_set(sparx5, mirror_idx, mport->portno);
177+
178+
entry->mirror.idx = mirror_idx;
179+
180+
return 0;
181+
}
182+
183+
void sparx5_mirror_del(struct sparx5_mall_entry *entry)
184+
{
185+
struct sparx5_port *port = entry->port;
186+
struct sparx5 *sparx5 = port->sparx5;
187+
u32 mirror_idx = entry->mirror.idx;
188+
189+
sparx5_mirror_port_del(sparx5, mirror_idx, port->portno);
190+
if (!sparx5_mirror_is_empty(sparx5, mirror_idx))
191+
return;
192+
193+
sparx5_mirror_dir_set(sparx5, mirror_idx, SPX5_MIRROR_DISABLED);
194+
195+
sparx5_mirror_monitor_set(sparx5,
196+
mirror_idx,
197+
SPX5_MIRROR_MONITOR_PORT_DEFAULT);
198+
}

0 commit comments

Comments
 (0)