1 // SPDX-License-Identifier: GPL-2.0+
3 #include "lan966x_main.h"
5 /* 0-8 : 9 port policers */
8 /* Policer order: Serial (QoS -> Port -> VCAP) */
9 #define POL_ORDER 0x1d3
11 struct lan966x_tc_policer {
12 /* kilobit per second */
18 static int lan966x_police_add(struct lan966x_port *port,
19 struct lan966x_tc_policer *pol,
22 struct lan966x *lan966x = port->lan966x;
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;
29 pol->burst = DIV_ROUND_UP(pol->burst, 4096);
31 if (pol->rate > GENMASK(15, 0) ||
32 pol->burst > GENMASK(6, 0))
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));
42 lan_wr(ANA_POL_PIR_STATE_PIR_LVL_SET(0),
43 lan966x, ANA_POL_PIR_STATE(pol_idx));
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));
52 static int lan966x_police_del(struct lan966x_port *port,
55 struct lan966x *lan966x = port->lan966x;
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));
64 lan_wr(ANA_POL_PIR_STATE_PIR_LVL_SET(0),
65 lan966x, ANA_POL_PIR_STATE(pol_idx));
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));
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,
79 struct netlink_ext_ack *extack)
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");
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");
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");
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");
108 if (act->police.rate_pkt_ps) {
109 NL_SET_ERR_MSG_MOD(extack,
110 "QoS offload not support packets per second");
115 NL_SET_ERR_MSG_MOD(extack,
116 "Policer is not supported on egress");
120 if (port->tc.ingress_shared_block) {
121 NL_SET_ERR_MSG_MOD(extack,
122 "Policer is not supported on shared ingress blocks");
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");
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,
140 struct netlink_ext_ack *extack)
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;
148 err = lan966x_police_validate(port, action, act, police_id, ingress,
153 memset(&pol, 0, sizeof(pol));
155 pol.rate = div_u64(act->police.rate_bytes_ps, 1000) * 8;
156 pol.burst = act->police.burst;
158 err = lan966x_police_add(port, &pol, POL_IDX_PORT + port->chip_port);
160 NL_SET_ERR_MSG_MOD(extack,
161 "Failed to add policer to port");
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));
171 port->tc.police_id = police_id;
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;
184 int lan966x_police_port_del(struct lan966x_port *port,
185 unsigned long police_id,
186 struct netlink_ext_ack *extack)
188 struct lan966x *lan966x = port->lan966x;
191 if (port->tc.police_id != police_id) {
192 NL_SET_ERR_MSG_MOD(extack,
193 "Invalid policer id");
197 err = lan966x_police_del(port, port->tc.police_id);
199 NL_SET_ERR_MSG_MOD(extack,
200 "Failed to add policer to port");
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));
210 port->tc.police_id = 0;
215 void lan966x_police_port_stats(struct lan966x_port *port,
216 struct flow_stats *stats)
218 struct rtnl_link_stats64 new_stats;
219 struct flow_stats *old_stats;
221 old_stats = &port->tc.police_stat;
222 lan966x_stats_get(port->dev, &new_stats);
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,
229 FLOW_ACTION_HW_STATS_IMMEDIATE);
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;