1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2020, NXP Semiconductors
5 #include "sja1105_vl.h"
7 struct sja1105_rule *sja1105_rule_find(struct sja1105_private *priv,
10 struct sja1105_rule *rule;
12 list_for_each_entry(rule, &priv->flow_block.rules, list)
13 if (rule->cookie == cookie)
19 static int sja1105_find_free_l2_policer(struct sja1105_private *priv)
23 for (i = 0; i < SJA1105_NUM_L2_POLICERS; i++)
24 if (!priv->flow_block.l2_policer_used[i])
30 static int sja1105_setup_bcast_policer(struct sja1105_private *priv,
31 struct netlink_ext_ack *extack,
32 unsigned long cookie, int port,
33 u64 rate_bytes_per_sec,
36 struct sja1105_rule *rule = sja1105_rule_find(priv, cookie);
37 struct sja1105_l2_policing_entry *policing;
38 bool new_rule = false;
43 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
47 rule->cookie = cookie;
48 rule->type = SJA1105_RULE_BCAST_POLICER;
49 rule->bcast_pol.sharindx = sja1105_find_free_l2_policer(priv);
50 rule->key.type = SJA1105_KEY_BCAST;
54 if (rule->bcast_pol.sharindx == -1) {
55 NL_SET_ERR_MSG_MOD(extack, "No more L2 policers free");
60 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
62 if (policing[(SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port].sharindx != port) {
63 NL_SET_ERR_MSG_MOD(extack,
64 "Port already has a broadcast policer");
69 rule->port_mask |= BIT(port);
71 /* Make the broadcast policers of all ports attached to this block
72 * point to the newly allocated policer
74 for_each_set_bit(p, &rule->port_mask, SJA1105_NUM_PORTS) {
75 int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + p;
77 policing[bcast].sharindx = rule->bcast_pol.sharindx;
80 policing[rule->bcast_pol.sharindx].rate = div_u64(rate_bytes_per_sec *
82 policing[rule->bcast_pol.sharindx].smax = burst;
84 /* TODO: support per-flow MTU */
85 policing[rule->bcast_pol.sharindx].maxlen = VLAN_ETH_FRAME_LEN +
88 rc = sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
91 if (rc == 0 && new_rule) {
92 priv->flow_block.l2_policer_used[rule->bcast_pol.sharindx] = true;
93 list_add(&rule->list, &priv->flow_block.rules);
94 } else if (new_rule) {
101 static int sja1105_setup_tc_policer(struct sja1105_private *priv,
102 struct netlink_ext_ack *extack,
103 unsigned long cookie, int port, int tc,
104 u64 rate_bytes_per_sec,
107 struct sja1105_rule *rule = sja1105_rule_find(priv, cookie);
108 struct sja1105_l2_policing_entry *policing;
109 bool new_rule = false;
114 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
118 rule->cookie = cookie;
119 rule->type = SJA1105_RULE_TC_POLICER;
120 rule->tc_pol.sharindx = sja1105_find_free_l2_policer(priv);
121 rule->key.type = SJA1105_KEY_TC;
122 rule->key.tc.pcp = tc;
126 if (rule->tc_pol.sharindx == -1) {
127 NL_SET_ERR_MSG_MOD(extack, "No more L2 policers free");
132 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
134 if (policing[(port * SJA1105_NUM_TC) + tc].sharindx != port) {
135 NL_SET_ERR_MSG_MOD(extack,
136 "Port-TC pair already has an L2 policer");
141 rule->port_mask |= BIT(port);
143 /* Make the policers for traffic class @tc of all ports attached to
144 * this block point to the newly allocated policer
146 for_each_set_bit(p, &rule->port_mask, SJA1105_NUM_PORTS) {
147 int index = (p * SJA1105_NUM_TC) + tc;
149 policing[index].sharindx = rule->tc_pol.sharindx;
152 policing[rule->tc_pol.sharindx].rate = div_u64(rate_bytes_per_sec *
154 policing[rule->tc_pol.sharindx].smax = burst;
156 /* TODO: support per-flow MTU */
157 policing[rule->tc_pol.sharindx].maxlen = VLAN_ETH_FRAME_LEN +
160 rc = sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
163 if (rc == 0 && new_rule) {
164 priv->flow_block.l2_policer_used[rule->tc_pol.sharindx] = true;
165 list_add(&rule->list, &priv->flow_block.rules);
166 } else if (new_rule) {
173 static int sja1105_flower_policer(struct sja1105_private *priv, int port,
174 struct netlink_ext_ack *extack,
175 unsigned long cookie,
176 struct sja1105_key *key,
177 u64 rate_bytes_per_sec,
181 case SJA1105_KEY_BCAST:
182 return sja1105_setup_bcast_policer(priv, extack, cookie, port,
183 rate_bytes_per_sec, burst);
185 return sja1105_setup_tc_policer(priv, extack, cookie, port,
186 key->tc.pcp, rate_bytes_per_sec,
189 NL_SET_ERR_MSG_MOD(extack, "Unknown keys for policing");
194 static int sja1105_flower_parse_key(struct sja1105_private *priv,
195 struct netlink_ext_ack *extack,
196 struct flow_cls_offload *cls,
197 struct sja1105_key *key)
199 struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
200 struct flow_dissector *dissector = rule->match.dissector;
201 bool is_bcast_dmac = false;
206 if (dissector->used_keys &
207 ~(BIT(FLOW_DISSECTOR_KEY_BASIC) |
208 BIT(FLOW_DISSECTOR_KEY_CONTROL) |
209 BIT(FLOW_DISSECTOR_KEY_VLAN) |
210 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS))) {
211 NL_SET_ERR_MSG_MOD(extack,
212 "Unsupported keys used");
216 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
217 struct flow_match_basic match;
219 flow_rule_match_basic(rule, &match);
220 if (match.key->n_proto) {
221 NL_SET_ERR_MSG_MOD(extack,
222 "Matching on protocol not supported");
227 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
228 u8 bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
229 u8 null[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
230 struct flow_match_eth_addrs match;
232 flow_rule_match_eth_addrs(rule, &match);
234 if (!ether_addr_equal_masked(match.key->src, null,
236 NL_SET_ERR_MSG_MOD(extack,
237 "Matching on source MAC not supported");
241 if (!ether_addr_equal(match.mask->dst, bcast)) {
242 NL_SET_ERR_MSG_MOD(extack,
243 "Masked matching on MAC not supported");
247 dmac = ether_addr_to_u64(match.key->dst);
248 is_bcast_dmac = ether_addr_equal(match.key->dst, bcast);
251 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
252 struct flow_match_vlan match;
254 flow_rule_match_vlan(rule, &match);
256 if (match.mask->vlan_id &&
257 match.mask->vlan_id != VLAN_VID_MASK) {
258 NL_SET_ERR_MSG_MOD(extack,
259 "Masked matching on VID is not supported");
263 if (match.mask->vlan_priority &&
264 match.mask->vlan_priority != 0x7) {
265 NL_SET_ERR_MSG_MOD(extack,
266 "Masked matching on PCP is not supported");
270 if (match.mask->vlan_id)
271 vid = match.key->vlan_id;
272 if (match.mask->vlan_priority)
273 pcp = match.key->vlan_priority;
276 if (is_bcast_dmac && vid == U16_MAX && pcp == U16_MAX) {
277 key->type = SJA1105_KEY_BCAST;
280 if (dmac == U64_MAX && vid == U16_MAX && pcp != U16_MAX) {
281 key->type = SJA1105_KEY_TC;
285 if (dmac != U64_MAX && vid != U16_MAX && pcp != U16_MAX) {
286 key->type = SJA1105_KEY_VLAN_AWARE_VL;
292 if (dmac != U64_MAX) {
293 key->type = SJA1105_KEY_VLAN_UNAWARE_VL;
298 NL_SET_ERR_MSG_MOD(extack, "Not matching on any known key");
302 int sja1105_cls_flower_add(struct dsa_switch *ds, int port,
303 struct flow_cls_offload *cls, bool ingress)
305 struct flow_rule *rule = flow_cls_offload_flow_rule(cls);
306 struct netlink_ext_ack *extack = cls->common.extack;
307 struct sja1105_private *priv = ds->priv;
308 const struct flow_action_entry *act;
309 unsigned long cookie = cls->cookie;
310 bool routing_rule = false;
311 struct sja1105_key key;
312 bool gate_rule = false;
313 bool vl_rule = false;
316 rc = sja1105_flower_parse_key(priv, extack, cls, &key);
322 flow_action_for_each(i, act, &rule->action) {
324 case FLOW_ACTION_POLICE:
325 rc = sja1105_flower_policer(priv, port, extack, cookie,
327 act->police.rate_bytes_ps,
332 case FLOW_ACTION_TRAP: {
333 int cpu = dsa_upstream_port(ds, port);
338 rc = sja1105_vl_redirect(priv, port, extack, cookie,
339 &key, BIT(cpu), true);
344 case FLOW_ACTION_REDIRECT: {
345 struct dsa_port *to_dp;
347 to_dp = dsa_port_from_netdev(act->dev);
349 NL_SET_ERR_MSG_MOD(extack,
350 "Destination not a switch port");
357 rc = sja1105_vl_redirect(priv, port, extack, cookie,
358 &key, BIT(to_dp->index), true);
363 case FLOW_ACTION_DROP:
366 rc = sja1105_vl_redirect(priv, port, extack, cookie,
371 case FLOW_ACTION_GATE:
375 rc = sja1105_vl_gate(priv, port, extack, cookie,
376 &key, act->gate.index,
380 act->gate.cycletimeext,
381 act->gate.num_entries,
387 NL_SET_ERR_MSG_MOD(extack,
388 "Action not supported");
394 if (vl_rule && !rc) {
395 /* Delay scheduling configuration until DESTPORTS has been
396 * populated by all other actions.
400 NL_SET_ERR_MSG_MOD(extack,
401 "Can only offload gate action together with redirect or trap");
404 rc = sja1105_init_scheduling(priv);
409 rc = sja1105_static_config_reload(priv, SJA1105_VIRTUAL_LINKS);
416 int sja1105_cls_flower_del(struct dsa_switch *ds, int port,
417 struct flow_cls_offload *cls, bool ingress)
419 struct sja1105_private *priv = ds->priv;
420 struct sja1105_rule *rule = sja1105_rule_find(priv, cls->cookie);
421 struct sja1105_l2_policing_entry *policing;
427 if (rule->type == SJA1105_RULE_VL)
428 return sja1105_vl_delete(priv, port, rule, cls->common.extack);
430 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries;
432 if (rule->type == SJA1105_RULE_BCAST_POLICER) {
433 int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + port;
435 old_sharindx = policing[bcast].sharindx;
436 policing[bcast].sharindx = port;
437 } else if (rule->type == SJA1105_RULE_TC_POLICER) {
438 int index = (port * SJA1105_NUM_TC) + rule->key.tc.pcp;
440 old_sharindx = policing[index].sharindx;
441 policing[index].sharindx = port;
446 rule->port_mask &= ~BIT(port);
447 if (!rule->port_mask) {
448 priv->flow_block.l2_policer_used[old_sharindx] = false;
449 list_del(&rule->list);
453 return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING);
456 int sja1105_cls_flower_stats(struct dsa_switch *ds, int port,
457 struct flow_cls_offload *cls, bool ingress)
459 struct sja1105_private *priv = ds->priv;
460 struct sja1105_rule *rule = sja1105_rule_find(priv, cls->cookie);
466 if (rule->type != SJA1105_RULE_VL)
469 rc = sja1105_vl_stats(priv, port, rule, &cls->stats,
477 void sja1105_flower_setup(struct dsa_switch *ds)
479 struct sja1105_private *priv = ds->priv;
482 INIT_LIST_HEAD(&priv->flow_block.rules);
484 for (port = 0; port < SJA1105_NUM_PORTS; port++)
485 priv->flow_block.l2_policer_used[port] = true;
488 void sja1105_flower_teardown(struct dsa_switch *ds)
490 struct sja1105_private *priv = ds->priv;
491 struct sja1105_rule *rule;
492 struct list_head *pos, *n;
494 list_for_each_safe(pos, n, &priv->flow_block.rules) {
495 rule = list_entry(pos, struct sja1105_rule, list);
496 list_del(&rule->list);