Skip to content

Commit 5390334

Browse files
HoratiuVulturdavem330
authored andcommitted
net: lan966x: Add port police support using tc-matchall
Add support for port police. It is possible to police only on the ingress side. To be able to add police support also it was required to add tc-matchall classifier offload support. Signed-off-by: Horatiu Vultur <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 95698ff commit 5390334

File tree

6 files changed

+468
-1
lines changed

6 files changed

+468
-1
lines changed

drivers/net/ethernet/microchip/lan966x/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ lan966x-switch-objs := lan966x_main.o lan966x_phylink.o lan966x_port.o \
1010
lan966x_vlan.o lan966x_fdb.o lan966x_mdb.o \
1111
lan966x_ptp.o lan966x_fdma.o lan966x_lag.o \
1212
lan966x_tc.o lan966x_mqprio.o lan966x_taprio.o \
13-
lan966x_tbf.o lan966x_cbs.o lan966x_ets.o
13+
lan966x_tbf.o lan966x_cbs.o lan966x_ets.o \
14+
lan966x_tc_matchall.o lan966x_police.o

drivers/net/ethernet/microchip/lan966x/lan966x_main.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ struct lan966x_port_config {
276276
bool autoneg;
277277
};
278278

279+
struct lan966x_port_tc {
280+
bool ingress_shared_block;
281+
unsigned long police_id;
282+
struct flow_stats police_stat;
283+
};
284+
279285
struct lan966x_port {
280286
struct net_device *dev;
281287
struct lan966x *lan966x;
@@ -302,6 +308,8 @@ struct lan966x_port {
302308
struct net_device *bond;
303309
bool lag_tx_active;
304310
enum netdev_lag_hash hash_type;
311+
312+
struct lan966x_port_tc tc;
305313
};
306314

307315
extern const struct phylink_mac_ops lan966x_phylink_mac_ops;
@@ -481,6 +489,22 @@ int lan966x_ets_add(struct lan966x_port *port,
481489
int lan966x_ets_del(struct lan966x_port *port,
482490
struct tc_ets_qopt_offload *qopt);
483491

492+
int lan966x_tc_matchall(struct lan966x_port *port,
493+
struct tc_cls_matchall_offload *f,
494+
bool ingress);
495+
496+
int lan966x_police_port_add(struct lan966x_port *port,
497+
struct flow_action *action,
498+
struct flow_action_entry *act,
499+
unsigned long police_id,
500+
bool ingress,
501+
struct netlink_ext_ack *extack);
502+
int lan966x_police_port_del(struct lan966x_port *port,
503+
unsigned long police_id,
504+
struct netlink_ext_ack *extack);
505+
void lan966x_police_port_stats(struct lan966x_port *port,
506+
struct flow_stats *stats);
507+
484508
static inline void __iomem *lan_addr(void __iomem *base[],
485509
int id, int tinst, int tcnt,
486510
int gbase, int ginst,
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
3+
#include "lan966x_main.h"
4+
5+
/* 0-8 : 9 port policers */
6+
#define POL_IDX_PORT 0
7+
8+
/* Policer order: Serial (QoS -> Port -> VCAP) */
9+
#define POL_ORDER 0x1d3
10+
11+
struct lan966x_tc_policer {
12+
/* kilobit per second */
13+
u32 rate;
14+
/* bytes */
15+
u32 burst;
16+
};
17+
18+
static int lan966x_police_add(struct lan966x_port *port,
19+
struct lan966x_tc_policer *pol,
20+
u16 pol_idx)
21+
{
22+
struct lan966x *lan966x = port->lan966x;
23+
24+
/* Rate unit is 33 1/3 kpps */
25+
pol->rate = DIV_ROUND_UP(pol->rate * 3, 100);
26+
/* Avoid zero burst size */
27+
pol->burst = pol->burst ?: 1;
28+
/* Unit is 4kB */
29+
pol->burst = DIV_ROUND_UP(pol->burst, 4096);
30+
31+
if (pol->rate > GENMASK(15, 0) ||
32+
pol->burst > GENMASK(6, 0))
33+
return -EINVAL;
34+
35+
lan_wr(ANA_POL_MODE_DROP_ON_YELLOW_ENA_SET(0) |
36+
ANA_POL_MODE_MARK_ALL_FRMS_RED_ENA_SET(0) |
37+
ANA_POL_MODE_IPG_SIZE_SET(20) |
38+
ANA_POL_MODE_FRM_MODE_SET(1) |
39+
ANA_POL_MODE_OVERSHOOT_ENA_SET(1),
40+
lan966x, ANA_POL_MODE(pol_idx));
41+
42+
lan_wr(ANA_POL_PIR_STATE_PIR_LVL_SET(0),
43+
lan966x, ANA_POL_PIR_STATE(pol_idx));
44+
45+
lan_wr(ANA_POL_PIR_CFG_PIR_RATE_SET(pol->rate) |
46+
ANA_POL_PIR_CFG_PIR_BURST_SET(pol->burst),
47+
lan966x, ANA_POL_PIR_CFG(pol_idx));
48+
49+
return 0;
50+
}
51+
52+
static int lan966x_police_del(struct lan966x_port *port,
53+
u16 pol_idx)
54+
{
55+
struct lan966x *lan966x = port->lan966x;
56+
57+
lan_wr(ANA_POL_MODE_DROP_ON_YELLOW_ENA_SET(0) |
58+
ANA_POL_MODE_MARK_ALL_FRMS_RED_ENA_SET(0) |
59+
ANA_POL_MODE_IPG_SIZE_SET(20) |
60+
ANA_POL_MODE_FRM_MODE_SET(2) |
61+
ANA_POL_MODE_OVERSHOOT_ENA_SET(1),
62+
lan966x, ANA_POL_MODE(pol_idx));
63+
64+
lan_wr(ANA_POL_PIR_STATE_PIR_LVL_SET(0),
65+
lan966x, ANA_POL_PIR_STATE(pol_idx));
66+
67+
lan_wr(ANA_POL_PIR_CFG_PIR_RATE_SET(GENMASK(14, 0)) |
68+
ANA_POL_PIR_CFG_PIR_BURST_SET(0),
69+
lan966x, ANA_POL_PIR_CFG(pol_idx));
70+
71+
return 0;
72+
}
73+
74+
static int lan966x_police_validate(struct lan966x_port *port,
75+
const struct flow_action *action,
76+
const struct flow_action_entry *act,
77+
unsigned long police_id,
78+
bool ingress,
79+
struct netlink_ext_ack *extack)
80+
{
81+
if (act->police.exceed.act_id != FLOW_ACTION_DROP) {
82+
NL_SET_ERR_MSG_MOD(extack,
83+
"Offload not supported when exceed action is not drop");
84+
return -EOPNOTSUPP;
85+
}
86+
87+
if (act->police.notexceed.act_id != FLOW_ACTION_PIPE &&
88+
act->police.notexceed.act_id != FLOW_ACTION_ACCEPT) {
89+
NL_SET_ERR_MSG_MOD(extack,
90+
"Offload not supported when conform action is not pipe or ok");
91+
return -EOPNOTSUPP;
92+
}
93+
94+
if (act->police.notexceed.act_id == FLOW_ACTION_ACCEPT &&
95+
!flow_action_is_last_entry(action, act)) {
96+
NL_SET_ERR_MSG_MOD(extack,
97+
"Offload not supported when conform action is ok, but action is not last");
98+
return -EOPNOTSUPP;
99+
}
100+
101+
if (act->police.peakrate_bytes_ps ||
102+
act->police.avrate || act->police.overhead) {
103+
NL_SET_ERR_MSG_MOD(extack,
104+
"Offload not supported when peakrate/avrate/overhead is configured");
105+
return -EOPNOTSUPP;
106+
}
107+
108+
if (act->police.rate_pkt_ps) {
109+
NL_SET_ERR_MSG_MOD(extack,
110+
"QoS offload not support packets per second");
111+
return -EOPNOTSUPP;
112+
}
113+
114+
if (!ingress) {
115+
NL_SET_ERR_MSG_MOD(extack,
116+
"Policer is not supported on egress");
117+
return -EOPNOTSUPP;
118+
}
119+
120+
if (port->tc.ingress_shared_block) {
121+
NL_SET_ERR_MSG_MOD(extack,
122+
"Policer is not supported on shared ingress blocks");
123+
return -EOPNOTSUPP;
124+
}
125+
126+
if (port->tc.police_id && port->tc.police_id != police_id) {
127+
NL_SET_ERR_MSG_MOD(extack,
128+
"Only one policer per port is supported");
129+
return -EEXIST;
130+
}
131+
132+
return 0;
133+
}
134+
135+
int lan966x_police_port_add(struct lan966x_port *port,
136+
struct flow_action *action,
137+
struct flow_action_entry *act,
138+
unsigned long police_id,
139+
bool ingress,
140+
struct netlink_ext_ack *extack)
141+
{
142+
struct lan966x *lan966x = port->lan966x;
143+
struct rtnl_link_stats64 new_stats;
144+
struct lan966x_tc_policer pol;
145+
struct flow_stats *old_stats;
146+
int err;
147+
148+
err = lan966x_police_validate(port, action, act, police_id, ingress,
149+
extack);
150+
if (err)
151+
return err;
152+
153+
memset(&pol, 0, sizeof(pol));
154+
155+
pol.rate = div_u64(act->police.rate_bytes_ps, 1000) * 8;
156+
pol.burst = act->police.burst;
157+
158+
err = lan966x_police_add(port, &pol, POL_IDX_PORT + port->chip_port);
159+
if (err) {
160+
NL_SET_ERR_MSG_MOD(extack,
161+
"Failed to add policer to port");
162+
return err;
163+
}
164+
165+
lan_rmw(ANA_POL_CFG_PORT_POL_ENA_SET(1) |
166+
ANA_POL_CFG_POL_ORDER_SET(POL_ORDER),
167+
ANA_POL_CFG_PORT_POL_ENA |
168+
ANA_POL_CFG_POL_ORDER,
169+
lan966x, ANA_POL_CFG(port->chip_port));
170+
171+
port->tc.police_id = police_id;
172+
173+
/* Setup initial stats */
174+
old_stats = &port->tc.police_stat;
175+
lan966x_stats_get(port->dev, &new_stats);
176+
old_stats->bytes = new_stats.rx_bytes;
177+
old_stats->pkts = new_stats.rx_packets;
178+
old_stats->drops = new_stats.rx_dropped;
179+
old_stats->lastused = jiffies;
180+
181+
return 0;
182+
}
183+
184+
int lan966x_police_port_del(struct lan966x_port *port,
185+
unsigned long police_id,
186+
struct netlink_ext_ack *extack)
187+
{
188+
struct lan966x *lan966x = port->lan966x;
189+
int err;
190+
191+
if (port->tc.police_id != police_id) {
192+
NL_SET_ERR_MSG_MOD(extack,
193+
"Invalid policer id");
194+
return -EINVAL;
195+
}
196+
197+
err = lan966x_police_del(port, port->tc.police_id);
198+
if (err) {
199+
NL_SET_ERR_MSG_MOD(extack,
200+
"Failed to add policer to port");
201+
return err;
202+
}
203+
204+
lan_rmw(ANA_POL_CFG_PORT_POL_ENA_SET(0) |
205+
ANA_POL_CFG_POL_ORDER_SET(POL_ORDER),
206+
ANA_POL_CFG_PORT_POL_ENA |
207+
ANA_POL_CFG_POL_ORDER,
208+
lan966x, ANA_POL_CFG(port->chip_port));
209+
210+
port->tc.police_id = 0;
211+
212+
return 0;
213+
}
214+
215+
void lan966x_police_port_stats(struct lan966x_port *port,
216+
struct flow_stats *stats)
217+
{
218+
struct rtnl_link_stats64 new_stats;
219+
struct flow_stats *old_stats;
220+
221+
old_stats = &port->tc.police_stat;
222+
lan966x_stats_get(port->dev, &new_stats);
223+
224+
flow_stats_update(stats,
225+
new_stats.rx_bytes - old_stats->bytes,
226+
new_stats.rx_packets - old_stats->pkts,
227+
new_stats.rx_dropped - old_stats->drops,
228+
old_stats->lastused,
229+
FLOW_ACTION_HW_STATS_IMMEDIATE);
230+
231+
old_stats->bytes = new_stats.rx_bytes;
232+
old_stats->pkts = new_stats.rx_packets;
233+
old_stats->drops = new_stats.rx_dropped;
234+
old_stats->lastused = jiffies;
235+
}

drivers/net/ethernet/microchip/lan966x/lan966x_regs.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,21 @@ enum lan966x_target {
354354
#define ANA_PORT_CFG_PORTID_VAL_GET(x)\
355355
FIELD_GET(ANA_PORT_CFG_PORTID_VAL, x)
356356

357+
/* ANA:PORT:POL_CFG */
358+
#define ANA_POL_CFG(g) __REG(TARGET_ANA, 0, 1, 28672, g, 9, 128, 116, 0, 1, 4)
359+
360+
#define ANA_POL_CFG_PORT_POL_ENA BIT(17)
361+
#define ANA_POL_CFG_PORT_POL_ENA_SET(x)\
362+
FIELD_PREP(ANA_POL_CFG_PORT_POL_ENA, x)
363+
#define ANA_POL_CFG_PORT_POL_ENA_GET(x)\
364+
FIELD_GET(ANA_POL_CFG_PORT_POL_ENA, x)
365+
366+
#define ANA_POL_CFG_POL_ORDER GENMASK(8, 0)
367+
#define ANA_POL_CFG_POL_ORDER_SET(x)\
368+
FIELD_PREP(ANA_POL_CFG_POL_ORDER, x)
369+
#define ANA_POL_CFG_POL_ORDER_GET(x)\
370+
FIELD_GET(ANA_POL_CFG_POL_ORDER, x)
371+
357372
/* ANA:PFC:PFC_CFG */
358373
#define ANA_PFC_CFG(g) __REG(TARGET_ANA, 0, 1, 30720, g, 8, 64, 0, 0, 1, 4)
359374

@@ -408,6 +423,63 @@ enum lan966x_target {
408423
#define ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA_GET(x)\
409424
FIELD_GET(ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, x)
410425

426+
/* ANA:POL:POL_PIR_CFG */
427+
#define ANA_POL_PIR_CFG(g) __REG(TARGET_ANA, 0, 1, 16384, g, 345, 32, 0, 0, 1, 4)
428+
429+
#define ANA_POL_PIR_CFG_PIR_RATE GENMASK(20, 6)
430+
#define ANA_POL_PIR_CFG_PIR_RATE_SET(x)\
431+
FIELD_PREP(ANA_POL_PIR_CFG_PIR_RATE, x)
432+
#define ANA_POL_PIR_CFG_PIR_RATE_GET(x)\
433+
FIELD_GET(ANA_POL_PIR_CFG_PIR_RATE, x)
434+
435+
#define ANA_POL_PIR_CFG_PIR_BURST GENMASK(5, 0)
436+
#define ANA_POL_PIR_CFG_PIR_BURST_SET(x)\
437+
FIELD_PREP(ANA_POL_PIR_CFG_PIR_BURST, x)
438+
#define ANA_POL_PIR_CFG_PIR_BURST_GET(x)\
439+
FIELD_GET(ANA_POL_PIR_CFG_PIR_BURST, x)
440+
441+
/* ANA:POL:POL_MODE_CFG */
442+
#define ANA_POL_MODE(g) __REG(TARGET_ANA, 0, 1, 16384, g, 345, 32, 8, 0, 1, 4)
443+
444+
#define ANA_POL_MODE_DROP_ON_YELLOW_ENA BIT(11)
445+
#define ANA_POL_MODE_DROP_ON_YELLOW_ENA_SET(x)\
446+
FIELD_PREP(ANA_POL_MODE_DROP_ON_YELLOW_ENA, x)
447+
#define ANA_POL_MODE_DROP_ON_YELLOW_ENA_GET(x)\
448+
FIELD_GET(ANA_POL_MODE_DROP_ON_YELLOW_ENA, x)
449+
450+
#define ANA_POL_MODE_MARK_ALL_FRMS_RED_ENA BIT(10)
451+
#define ANA_POL_MODE_MARK_ALL_FRMS_RED_ENA_SET(x)\
452+
FIELD_PREP(ANA_POL_MODE_MARK_ALL_FRMS_RED_ENA, x)
453+
#define ANA_POL_MODE_MARK_ALL_FRMS_RED_ENA_GET(x)\
454+
FIELD_GET(ANA_POL_MODE_MARK_ALL_FRMS_RED_ENA, x)
455+
456+
#define ANA_POL_MODE_IPG_SIZE GENMASK(9, 5)
457+
#define ANA_POL_MODE_IPG_SIZE_SET(x)\
458+
FIELD_PREP(ANA_POL_MODE_IPG_SIZE, x)
459+
#define ANA_POL_MODE_IPG_SIZE_GET(x)\
460+
FIELD_GET(ANA_POL_MODE_IPG_SIZE, x)
461+
462+
#define ANA_POL_MODE_FRM_MODE GENMASK(4, 3)
463+
#define ANA_POL_MODE_FRM_MODE_SET(x)\
464+
FIELD_PREP(ANA_POL_MODE_FRM_MODE, x)
465+
#define ANA_POL_MODE_FRM_MODE_GET(x)\
466+
FIELD_GET(ANA_POL_MODE_FRM_MODE, x)
467+
468+
#define ANA_POL_MODE_OVERSHOOT_ENA BIT(0)
469+
#define ANA_POL_MODE_OVERSHOOT_ENA_SET(x)\
470+
FIELD_PREP(ANA_POL_MODE_OVERSHOOT_ENA, x)
471+
#define ANA_POL_MODE_OVERSHOOT_ENA_GET(x)\
472+
FIELD_GET(ANA_POL_MODE_OVERSHOOT_ENA, x)
473+
474+
/* ANA:POL:POL_PIR_STATE */
475+
#define ANA_POL_PIR_STATE(g) __REG(TARGET_ANA, 0, 1, 16384, g, 345, 32, 12, 0, 1, 4)
476+
477+
#define ANA_POL_PIR_STATE_PIR_LVL GENMASK(21, 0)
478+
#define ANA_POL_PIR_STATE_PIR_LVL_SET(x)\
479+
FIELD_PREP(ANA_POL_PIR_STATE_PIR_LVL, x)
480+
#define ANA_POL_PIR_STATE_PIR_LVL_GET(x)\
481+
FIELD_GET(ANA_POL_PIR_STATE_PIR_LVL, x)
482+
411483
/* CHIP_TOP:CUPHY_CFG:CUPHY_PORT_CFG */
412484
#define CHIP_TOP_CUPHY_PORT_CFG(r) __REG(TARGET_CHIP_TOP, 0, 1, 16, 0, 1, 20, 8, r, 2, 4)
413485

0 commit comments

Comments
 (0)