1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Microsemi Ocelot Switch TC driver
4 * Copyright (c) 2019 Microsemi Corporation
8 #include "ocelot_police.h"
9 #include "ocelot_ace.h"
10 #include <net/pkt_cls.h>
12 static int ocelot_setup_tc_cls_matchall(struct ocelot_port *port,
13 struct tc_cls_matchall_offload *f,
16 struct netlink_ext_ack *extack = f->common.extack;
17 struct ocelot_policer pol = { 0 };
18 struct flow_action_entry *action;
21 netdev_dbg(port->dev, "%s: port %u command %d cookie %lu\n",
22 __func__, port->chip_port, f->command, f->cookie);
25 NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
30 case TC_CLSMATCHALL_REPLACE:
31 if (!flow_offload_has_one_action(&f->rule->action)) {
32 NL_SET_ERR_MSG_MOD(extack,
33 "Only one action is supported");
37 if (port->tc.block_shared) {
38 NL_SET_ERR_MSG_MOD(extack,
39 "Rate limit is not supported on shared blocks");
43 action = &f->rule->action.entries[0];
45 if (action->id != FLOW_ACTION_POLICE) {
46 NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
50 if (port->tc.police_id && port->tc.police_id != f->cookie) {
51 NL_SET_ERR_MSG_MOD(extack,
52 "Only one policer per port is supported\n");
56 pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
57 pol.burst = (u32)div_u64(action->police.rate_bytes_ps *
58 PSCHED_NS2TICKS(action->police.burst),
59 PSCHED_TICKS_PER_SEC);
61 err = ocelot_port_policer_add(port, &pol);
63 NL_SET_ERR_MSG_MOD(extack, "Could not add policer\n");
67 port->tc.police_id = f->cookie;
68 port->tc.offload_cnt++;
70 case TC_CLSMATCHALL_DESTROY:
71 if (port->tc.police_id != f->cookie)
74 err = ocelot_port_policer_del(port);
76 NL_SET_ERR_MSG_MOD(extack,
77 "Could not delete policer\n");
80 port->tc.police_id = 0;
81 port->tc.offload_cnt--;
83 case TC_CLSMATCHALL_STATS: /* fall through */
89 static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
91 void *cb_priv, bool ingress)
93 struct ocelot_port *port = cb_priv;
95 if (!tc_cls_can_offload_and_chain0(port->dev, type_data))
99 case TC_SETUP_CLSMATCHALL:
100 netdev_dbg(port->dev, "tc_block_cb: TC_SETUP_CLSMATCHALL %s\n",
101 ingress ? "ingress" : "egress");
103 return ocelot_setup_tc_cls_matchall(port, type_data, ingress);
104 case TC_SETUP_CLSFLOWER:
107 netdev_dbg(port->dev, "tc_block_cb: type %d %s\n",
109 ingress ? "ingress" : "egress");
115 static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
119 return ocelot_setup_tc_block_cb(type, type_data,
123 static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
127 return ocelot_setup_tc_block_cb(type, type_data,
131 static LIST_HEAD(ocelot_block_cb_list);
133 static int ocelot_setup_tc_block(struct ocelot_port *port,
134 struct flow_block_offload *f)
136 struct flow_block_cb *block_cb;
140 netdev_dbg(port->dev, "tc_block command %d, binder_type %d\n",
141 f->command, f->binder_type);
143 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
144 cb = ocelot_setup_tc_block_cb_ig;
145 port->tc.block_shared = f->block_shared;
146 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
147 cb = ocelot_setup_tc_block_cb_eg;
152 f->driver_block_list = &ocelot_block_cb_list;
154 switch (f->command) {
155 case FLOW_BLOCK_BIND:
156 if (flow_block_cb_is_busy(cb, port, &ocelot_block_cb_list))
159 block_cb = flow_block_cb_alloc(f->net, cb, port, port, NULL);
160 if (IS_ERR(block_cb))
161 return PTR_ERR(block_cb);
163 err = ocelot_setup_tc_block_flower_bind(port, f);
165 flow_block_cb_free(block_cb);
168 flow_block_cb_add(block_cb, f);
169 list_add_tail(&block_cb->driver_list, f->driver_block_list);
171 case FLOW_BLOCK_UNBIND:
172 block_cb = flow_block_cb_lookup(f, cb, port);
176 ocelot_setup_tc_block_flower_unbind(port, f);
177 flow_block_cb_remove(block_cb, f);
178 list_del(&block_cb->driver_list);
185 int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
188 struct ocelot_port *port = netdev_priv(dev);
192 return ocelot_setup_tc_block(port, type_data);