1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2020 Marvell International Ltd. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/list.h>
8 #include "prestera_hw.h"
9 #include "prestera_acl.h"
10 #include "prestera_flow.h"
11 #include "prestera_span.h"
13 struct prestera_span_entry {
14 struct list_head list;
15 struct prestera_port *port;
20 struct prestera_span {
21 struct prestera_switch *sw;
22 struct list_head entries;
25 static struct prestera_span_entry *
26 prestera_span_entry_create(struct prestera_port *port, u8 span_id)
28 struct prestera_span_entry *entry;
30 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
32 return ERR_PTR(-ENOMEM);
34 refcount_set(&entry->ref_count, 1);
37 list_add_tail(&entry->list, &port->sw->span->entries);
42 static void prestera_span_entry_del(struct prestera_span_entry *entry)
44 list_del(&entry->list);
48 static struct prestera_span_entry *
49 prestera_span_entry_find_by_id(struct prestera_span *span, u8 span_id)
51 struct prestera_span_entry *entry;
53 list_for_each_entry(entry, &span->entries, list) {
54 if (entry->id == span_id)
61 static struct prestera_span_entry *
62 prestera_span_entry_find_by_port(struct prestera_span *span,
63 struct prestera_port *port)
65 struct prestera_span_entry *entry;
67 list_for_each_entry(entry, &span->entries, list) {
68 if (entry->port == port)
75 static int prestera_span_get(struct prestera_port *port, u8 *span_id)
78 struct prestera_switch *sw = port->sw;
79 struct prestera_span_entry *entry;
82 entry = prestera_span_entry_find_by_port(sw->span, port);
84 refcount_inc(&entry->ref_count);
89 err = prestera_hw_span_get(port, &new_span_id);
93 entry = prestera_span_entry_create(port, new_span_id);
95 prestera_hw_span_release(sw, new_span_id);
96 return PTR_ERR(entry);
99 *span_id = new_span_id;
103 static int prestera_span_put(struct prestera_switch *sw, u8 span_id)
105 struct prestera_span_entry *entry;
108 entry = prestera_span_entry_find_by_id(sw->span, span_id);
112 if (!refcount_dec_and_test(&entry->ref_count))
115 err = prestera_hw_span_release(sw, span_id);
119 prestera_span_entry_del(entry);
123 static int prestera_span_rule_add(struct prestera_flow_block_binding *binding,
124 struct prestera_port *to_port)
126 struct prestera_switch *sw = binding->port->sw;
130 if (binding->span_id != PRESTERA_SPAN_INVALID_ID)
131 /* port already in mirroring */
134 err = prestera_span_get(to_port, &span_id);
138 err = prestera_hw_span_bind(binding->port, span_id);
140 prestera_span_put(sw, span_id);
144 binding->span_id = span_id;
148 static int prestera_span_rule_del(struct prestera_flow_block_binding *binding)
152 err = prestera_hw_span_unbind(binding->port);
156 err = prestera_span_put(binding->port->sw, binding->span_id);
160 binding->span_id = PRESTERA_SPAN_INVALID_ID;
164 int prestera_span_replace(struct prestera_flow_block *block,
165 struct tc_cls_matchall_offload *f)
167 struct prestera_flow_block_binding *binding;
168 __be16 protocol = f->common.protocol;
169 struct flow_action_entry *act;
170 struct prestera_port *port;
173 if (!flow_offload_has_one_action(&f->rule->action)) {
174 NL_SET_ERR_MSG(f->common.extack,
175 "Only singular actions are supported");
179 act = &f->rule->action.entries[0];
181 if (!prestera_netdev_check(act->dev)) {
182 NL_SET_ERR_MSG(f->common.extack,
183 "Only Marvell Prestera port is supported");
186 if (!tc_cls_can_offload_and_chain0(act->dev, &f->common))
188 if (act->id != FLOW_ACTION_MIRRED)
190 if (protocol != htons(ETH_P_ALL))
193 port = netdev_priv(act->dev);
195 list_for_each_entry(binding, &block->binding_list, list) {
196 err = prestera_span_rule_add(binding, port);
204 list_for_each_entry_continue_reverse(binding,
205 &block->binding_list, list)
206 prestera_span_rule_del(binding);
210 void prestera_span_destroy(struct prestera_flow_block *block)
212 struct prestera_flow_block_binding *binding;
214 list_for_each_entry(binding, &block->binding_list, list)
215 prestera_span_rule_del(binding);
218 int prestera_span_init(struct prestera_switch *sw)
220 struct prestera_span *span;
222 span = kzalloc(sizeof(*span), GFP_KERNEL);
226 INIT_LIST_HEAD(&span->entries);
234 void prestera_span_fini(struct prestera_switch *sw)
236 struct prestera_span *span = sw->span;
238 WARN_ON(!list_empty(&span->entries));