1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
3
4 #include <linux/if_bridge.h>
5 #include <linux/list.h>
6 #include <linux/mutex.h>
7 #include <linux/refcount.h>
8 #include <linux/rtnetlink.h>
9 #include <linux/workqueue.h>
10 #include <net/arp.h>
11 #include <net/gre.h>
12 #include <net/lag.h>
13 #include <net/ndisc.h>
14 #include <net/ip6_tunnel.h>
15
16 #include "spectrum.h"
17 #include "spectrum_ipip.h"
18 #include "spectrum_span.h"
19 #include "spectrum_switchdev.h"
20
21 struct mlxsw_sp_span {
22 struct work_struct work;
23 struct mlxsw_sp *mlxsw_sp;
24 const struct mlxsw_sp_span_trigger_ops **span_trigger_ops_arr;
25 const struct mlxsw_sp_span_entry_ops **span_entry_ops_arr;
26 size_t span_entry_ops_arr_size;
27 struct list_head analyzed_ports_list;
28 struct mutex analyzed_ports_lock; /* Protects analyzed_ports_list */
29 struct list_head trigger_entries_list;
30 u16 policer_id_base;
31 refcount_t policer_id_base_ref_count;
32 atomic_t active_entries_count;
33 int entries_count;
34 struct mlxsw_sp_span_entry entries[];
35 };
36
37 struct mlxsw_sp_span_analyzed_port {
38 struct list_head list; /* Member of analyzed_ports_list */
39 refcount_t ref_count;
40 u8 local_port;
41 bool ingress;
42 };
43
44 struct mlxsw_sp_span_trigger_entry {
45 struct list_head list; /* Member of trigger_entries_list */
46 struct mlxsw_sp_span *span;
47 const struct mlxsw_sp_span_trigger_ops *ops;
48 refcount_t ref_count;
49 u8 local_port;
50 enum mlxsw_sp_span_trigger trigger;
51 struct mlxsw_sp_span_trigger_parms parms;
52 };
53
54 enum mlxsw_sp_span_trigger_type {
55 MLXSW_SP_SPAN_TRIGGER_TYPE_PORT,
56 MLXSW_SP_SPAN_TRIGGER_TYPE_GLOBAL,
57 };
58
59 struct mlxsw_sp_span_trigger_ops {
60 int (*bind)(struct mlxsw_sp_span_trigger_entry *trigger_entry);
61 void (*unbind)(struct mlxsw_sp_span_trigger_entry *trigger_entry);
62 bool (*matches)(struct mlxsw_sp_span_trigger_entry *trigger_entry,
63 enum mlxsw_sp_span_trigger trigger,
64 struct mlxsw_sp_port *mlxsw_sp_port);
65 int (*enable)(struct mlxsw_sp_span_trigger_entry *trigger_entry,
66 struct mlxsw_sp_port *mlxsw_sp_port, u8 tc);
67 void (*disable)(struct mlxsw_sp_span_trigger_entry *trigger_entry,
68 struct mlxsw_sp_port *mlxsw_sp_port, u8 tc);
69 };
70
71 static void mlxsw_sp_span_respin_work(struct work_struct *work);
72
mlxsw_sp_span_occ_get(void * priv)73 static u64 mlxsw_sp_span_occ_get(void *priv)
74 {
75 const struct mlxsw_sp *mlxsw_sp = priv;
76
77 return atomic_read(&mlxsw_sp->span->active_entries_count);
78 }
79
mlxsw_sp_span_init(struct mlxsw_sp * mlxsw_sp)80 int mlxsw_sp_span_init(struct mlxsw_sp *mlxsw_sp)
81 {
82 struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
83 struct mlxsw_sp_span *span;
84 int i, entries_count, err;
85
86 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_SPAN))
87 return -EIO;
88
89 entries_count = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_SPAN);
90 span = kzalloc(struct_size(span, entries, entries_count), GFP_KERNEL);
91 if (!span)
92 return -ENOMEM;
93 refcount_set(&span->policer_id_base_ref_count, 0);
94 span->entries_count = entries_count;
95 atomic_set(&span->active_entries_count, 0);
96 mutex_init(&span->analyzed_ports_lock);
97 INIT_LIST_HEAD(&span->analyzed_ports_list);
98 INIT_LIST_HEAD(&span->trigger_entries_list);
99 span->mlxsw_sp = mlxsw_sp;
100 mlxsw_sp->span = span;
101
102 for (i = 0; i < mlxsw_sp->span->entries_count; i++)
103 mlxsw_sp->span->entries[i].id = i;
104
105 err = mlxsw_sp->span_ops->init(mlxsw_sp);
106 if (err)
107 goto err_init;
108
109 devlink_resource_occ_get_register(devlink, MLXSW_SP_RESOURCE_SPAN,
110 mlxsw_sp_span_occ_get, mlxsw_sp);
111 INIT_WORK(&span->work, mlxsw_sp_span_respin_work);
112
113 return 0;
114
115 err_init:
116 mutex_destroy(&mlxsw_sp->span->analyzed_ports_lock);
117 kfree(mlxsw_sp->span);
118 return err;
119 }
120
mlxsw_sp_span_fini(struct mlxsw_sp * mlxsw_sp)121 void mlxsw_sp_span_fini(struct mlxsw_sp *mlxsw_sp)
122 {
123 struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
124
125 cancel_work_sync(&mlxsw_sp->span->work);
126 devlink_resource_occ_get_unregister(devlink, MLXSW_SP_RESOURCE_SPAN);
127
128 WARN_ON_ONCE(!list_empty(&mlxsw_sp->span->trigger_entries_list));
129 WARN_ON_ONCE(!list_empty(&mlxsw_sp->span->analyzed_ports_list));
130 mutex_destroy(&mlxsw_sp->span->analyzed_ports_lock);
131 kfree(mlxsw_sp->span);
132 }
133
mlxsw_sp1_span_cpu_can_handle(const struct net_device * dev)134 static bool mlxsw_sp1_span_cpu_can_handle(const struct net_device *dev)
135 {
136 return !dev;
137 }
138
mlxsw_sp1_span_entry_cpu_parms(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,struct mlxsw_sp_span_parms * sparmsp)139 static int mlxsw_sp1_span_entry_cpu_parms(struct mlxsw_sp *mlxsw_sp,
140 const struct net_device *to_dev,
141 struct mlxsw_sp_span_parms *sparmsp)
142 {
143 return -EOPNOTSUPP;
144 }
145
146 static int
mlxsw_sp1_span_entry_cpu_configure(struct mlxsw_sp_span_entry * span_entry,struct mlxsw_sp_span_parms sparms)147 mlxsw_sp1_span_entry_cpu_configure(struct mlxsw_sp_span_entry *span_entry,
148 struct mlxsw_sp_span_parms sparms)
149 {
150 return -EOPNOTSUPP;
151 }
152
153 static void
mlxsw_sp1_span_entry_cpu_deconfigure(struct mlxsw_sp_span_entry * span_entry)154 mlxsw_sp1_span_entry_cpu_deconfigure(struct mlxsw_sp_span_entry *span_entry)
155 {
156 }
157
158 static const
159 struct mlxsw_sp_span_entry_ops mlxsw_sp1_span_entry_ops_cpu = {
160 .can_handle = mlxsw_sp1_span_cpu_can_handle,
161 .parms_set = mlxsw_sp1_span_entry_cpu_parms,
162 .configure = mlxsw_sp1_span_entry_cpu_configure,
163 .deconfigure = mlxsw_sp1_span_entry_cpu_deconfigure,
164 };
165
166 static int
mlxsw_sp_span_entry_phys_parms(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,struct mlxsw_sp_span_parms * sparmsp)167 mlxsw_sp_span_entry_phys_parms(struct mlxsw_sp *mlxsw_sp,
168 const struct net_device *to_dev,
169 struct mlxsw_sp_span_parms *sparmsp)
170 {
171 sparmsp->dest_port = netdev_priv(to_dev);
172 return 0;
173 }
174
175 static int
mlxsw_sp_span_entry_phys_configure(struct mlxsw_sp_span_entry * span_entry,struct mlxsw_sp_span_parms sparms)176 mlxsw_sp_span_entry_phys_configure(struct mlxsw_sp_span_entry *span_entry,
177 struct mlxsw_sp_span_parms sparms)
178 {
179 struct mlxsw_sp_port *dest_port = sparms.dest_port;
180 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp;
181 u8 local_port = dest_port->local_port;
182 char mpat_pl[MLXSW_REG_MPAT_LEN];
183 int pa_id = span_entry->id;
184
185 /* Create a new port analayzer entry for local_port. */
186 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, true,
187 MLXSW_REG_MPAT_SPAN_TYPE_LOCAL_ETH);
188 mlxsw_reg_mpat_pide_set(mpat_pl, sparms.policer_enable);
189 mlxsw_reg_mpat_pid_set(mpat_pl, sparms.policer_id);
190
191 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
192 }
193
194 static void
mlxsw_sp_span_entry_deconfigure_common(struct mlxsw_sp_span_entry * span_entry,enum mlxsw_reg_mpat_span_type span_type)195 mlxsw_sp_span_entry_deconfigure_common(struct mlxsw_sp_span_entry *span_entry,
196 enum mlxsw_reg_mpat_span_type span_type)
197 {
198 struct mlxsw_sp_port *dest_port = span_entry->parms.dest_port;
199 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp;
200 u8 local_port = dest_port->local_port;
201 char mpat_pl[MLXSW_REG_MPAT_LEN];
202 int pa_id = span_entry->id;
203
204 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, false, span_type);
205 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
206 }
207
208 static void
mlxsw_sp_span_entry_phys_deconfigure(struct mlxsw_sp_span_entry * span_entry)209 mlxsw_sp_span_entry_phys_deconfigure(struct mlxsw_sp_span_entry *span_entry)
210 {
211 mlxsw_sp_span_entry_deconfigure_common(span_entry,
212 MLXSW_REG_MPAT_SPAN_TYPE_LOCAL_ETH);
213 }
214
215 static const
216 struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_phys = {
217 .can_handle = mlxsw_sp_port_dev_check,
218 .parms_set = mlxsw_sp_span_entry_phys_parms,
219 .configure = mlxsw_sp_span_entry_phys_configure,
220 .deconfigure = mlxsw_sp_span_entry_phys_deconfigure,
221 };
222
mlxsw_sp_span_dmac(struct neigh_table * tbl,const void * pkey,struct net_device * dev,unsigned char dmac[ETH_ALEN])223 static int mlxsw_sp_span_dmac(struct neigh_table *tbl,
224 const void *pkey,
225 struct net_device *dev,
226 unsigned char dmac[ETH_ALEN])
227 {
228 struct neighbour *neigh = neigh_lookup(tbl, pkey, dev);
229 int err = 0;
230
231 if (!neigh) {
232 neigh = neigh_create(tbl, pkey, dev);
233 if (IS_ERR(neigh))
234 return PTR_ERR(neigh);
235 }
236
237 neigh_event_send(neigh, NULL);
238
239 read_lock_bh(&neigh->lock);
240 if ((neigh->nud_state & NUD_VALID) && !neigh->dead)
241 memcpy(dmac, neigh->ha, ETH_ALEN);
242 else
243 err = -ENOENT;
244 read_unlock_bh(&neigh->lock);
245
246 neigh_release(neigh);
247 return err;
248 }
249
250 static int
mlxsw_sp_span_entry_unoffloadable(struct mlxsw_sp_span_parms * sparmsp)251 mlxsw_sp_span_entry_unoffloadable(struct mlxsw_sp_span_parms *sparmsp)
252 {
253 sparmsp->dest_port = NULL;
254 return 0;
255 }
256
257 static struct net_device *
mlxsw_sp_span_entry_bridge_8021q(const struct net_device * br_dev,unsigned char * dmac,u16 * p_vid)258 mlxsw_sp_span_entry_bridge_8021q(const struct net_device *br_dev,
259 unsigned char *dmac,
260 u16 *p_vid)
261 {
262 struct bridge_vlan_info vinfo;
263 struct net_device *edev;
264 u16 vid = *p_vid;
265
266 if (!vid && WARN_ON(br_vlan_get_pvid(br_dev, &vid)))
267 return NULL;
268 if (!vid ||
269 br_vlan_get_info(br_dev, vid, &vinfo) ||
270 !(vinfo.flags & BRIDGE_VLAN_INFO_BRENTRY))
271 return NULL;
272
273 edev = br_fdb_find_port(br_dev, dmac, vid);
274 if (!edev)
275 return NULL;
276
277 if (br_vlan_get_info(edev, vid, &vinfo))
278 return NULL;
279 if (vinfo.flags & BRIDGE_VLAN_INFO_UNTAGGED)
280 *p_vid = 0;
281 else
282 *p_vid = vid;
283 return edev;
284 }
285
286 static struct net_device *
mlxsw_sp_span_entry_bridge_8021d(const struct net_device * br_dev,unsigned char * dmac)287 mlxsw_sp_span_entry_bridge_8021d(const struct net_device *br_dev,
288 unsigned char *dmac)
289 {
290 return br_fdb_find_port(br_dev, dmac, 0);
291 }
292
293 static struct net_device *
mlxsw_sp_span_entry_bridge(const struct net_device * br_dev,unsigned char dmac[ETH_ALEN],u16 * p_vid)294 mlxsw_sp_span_entry_bridge(const struct net_device *br_dev,
295 unsigned char dmac[ETH_ALEN],
296 u16 *p_vid)
297 {
298 struct mlxsw_sp_bridge_port *bridge_port;
299 enum mlxsw_reg_spms_state spms_state;
300 struct net_device *dev = NULL;
301 struct mlxsw_sp_port *port;
302 u8 stp_state;
303
304 if (br_vlan_enabled(br_dev))
305 dev = mlxsw_sp_span_entry_bridge_8021q(br_dev, dmac, p_vid);
306 else if (!*p_vid)
307 dev = mlxsw_sp_span_entry_bridge_8021d(br_dev, dmac);
308 if (!dev)
309 return NULL;
310
311 port = mlxsw_sp_port_dev_lower_find(dev);
312 if (!port)
313 return NULL;
314
315 bridge_port = mlxsw_sp_bridge_port_find(port->mlxsw_sp->bridge, dev);
316 if (!bridge_port)
317 return NULL;
318
319 stp_state = mlxsw_sp_bridge_port_stp_state(bridge_port);
320 spms_state = mlxsw_sp_stp_spms_state(stp_state);
321 if (spms_state != MLXSW_REG_SPMS_STATE_FORWARDING)
322 return NULL;
323
324 return dev;
325 }
326
327 static struct net_device *
mlxsw_sp_span_entry_vlan(const struct net_device * vlan_dev,u16 * p_vid)328 mlxsw_sp_span_entry_vlan(const struct net_device *vlan_dev,
329 u16 *p_vid)
330 {
331 *p_vid = vlan_dev_vlan_id(vlan_dev);
332 return vlan_dev_real_dev(vlan_dev);
333 }
334
335 static struct net_device *
mlxsw_sp_span_entry_lag(struct net_device * lag_dev)336 mlxsw_sp_span_entry_lag(struct net_device *lag_dev)
337 {
338 struct net_device *dev;
339 struct list_head *iter;
340
341 netdev_for_each_lower_dev(lag_dev, dev, iter)
342 if (netif_carrier_ok(dev) &&
343 net_lag_port_dev_txable(dev) &&
344 mlxsw_sp_port_dev_check(dev))
345 return dev;
346
347 return NULL;
348 }
349
350 static __maybe_unused int
mlxsw_sp_span_entry_tunnel_parms_common(struct net_device * edev,union mlxsw_sp_l3addr saddr,union mlxsw_sp_l3addr daddr,union mlxsw_sp_l3addr gw,__u8 ttl,struct neigh_table * tbl,struct mlxsw_sp_span_parms * sparmsp)351 mlxsw_sp_span_entry_tunnel_parms_common(struct net_device *edev,
352 union mlxsw_sp_l3addr saddr,
353 union mlxsw_sp_l3addr daddr,
354 union mlxsw_sp_l3addr gw,
355 __u8 ttl,
356 struct neigh_table *tbl,
357 struct mlxsw_sp_span_parms *sparmsp)
358 {
359 unsigned char dmac[ETH_ALEN];
360 u16 vid = 0;
361
362 if (mlxsw_sp_l3addr_is_zero(gw))
363 gw = daddr;
364
365 if (!edev || mlxsw_sp_span_dmac(tbl, &gw, edev, dmac))
366 goto unoffloadable;
367
368 if (is_vlan_dev(edev))
369 edev = mlxsw_sp_span_entry_vlan(edev, &vid);
370
371 if (netif_is_bridge_master(edev)) {
372 edev = mlxsw_sp_span_entry_bridge(edev, dmac, &vid);
373 if (!edev)
374 goto unoffloadable;
375 }
376
377 if (is_vlan_dev(edev)) {
378 if (vid || !(edev->flags & IFF_UP))
379 goto unoffloadable;
380 edev = mlxsw_sp_span_entry_vlan(edev, &vid);
381 }
382
383 if (netif_is_lag_master(edev)) {
384 if (!(edev->flags & IFF_UP))
385 goto unoffloadable;
386 edev = mlxsw_sp_span_entry_lag(edev);
387 if (!edev)
388 goto unoffloadable;
389 }
390
391 if (!mlxsw_sp_port_dev_check(edev))
392 goto unoffloadable;
393
394 sparmsp->dest_port = netdev_priv(edev);
395 sparmsp->ttl = ttl;
396 memcpy(sparmsp->dmac, dmac, ETH_ALEN);
397 memcpy(sparmsp->smac, edev->dev_addr, ETH_ALEN);
398 sparmsp->saddr = saddr;
399 sparmsp->daddr = daddr;
400 sparmsp->vid = vid;
401 return 0;
402
403 unoffloadable:
404 return mlxsw_sp_span_entry_unoffloadable(sparmsp);
405 }
406
407 #if IS_ENABLED(CONFIG_NET_IPGRE)
408 static struct net_device *
mlxsw_sp_span_gretap4_route(const struct net_device * to_dev,__be32 * saddrp,__be32 * daddrp)409 mlxsw_sp_span_gretap4_route(const struct net_device *to_dev,
410 __be32 *saddrp, __be32 *daddrp)
411 {
412 struct ip_tunnel *tun = netdev_priv(to_dev);
413 struct net_device *dev = NULL;
414 struct ip_tunnel_parm parms;
415 struct rtable *rt = NULL;
416 struct flowi4 fl4;
417
418 /* We assume "dev" stays valid after rt is put. */
419 ASSERT_RTNL();
420
421 parms = mlxsw_sp_ipip_netdev_parms4(to_dev);
422 ip_tunnel_init_flow(&fl4, parms.iph.protocol, *daddrp, *saddrp,
423 0, 0, parms.link, tun->fwmark, 0);
424
425 rt = ip_route_output_key(tun->net, &fl4);
426 if (IS_ERR(rt))
427 return NULL;
428
429 if (rt->rt_type != RTN_UNICAST)
430 goto out;
431
432 dev = rt->dst.dev;
433 *saddrp = fl4.saddr;
434 if (rt->rt_gw_family == AF_INET)
435 *daddrp = rt->rt_gw4;
436 /* can not offload if route has an IPv6 gateway */
437 else if (rt->rt_gw_family == AF_INET6)
438 dev = NULL;
439
440 out:
441 ip_rt_put(rt);
442 return dev;
443 }
444
445 static int
mlxsw_sp_span_entry_gretap4_parms(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,struct mlxsw_sp_span_parms * sparmsp)446 mlxsw_sp_span_entry_gretap4_parms(struct mlxsw_sp *mlxsw_sp,
447 const struct net_device *to_dev,
448 struct mlxsw_sp_span_parms *sparmsp)
449 {
450 struct ip_tunnel_parm tparm = mlxsw_sp_ipip_netdev_parms4(to_dev);
451 union mlxsw_sp_l3addr saddr = { .addr4 = tparm.iph.saddr };
452 union mlxsw_sp_l3addr daddr = { .addr4 = tparm.iph.daddr };
453 bool inherit_tos = tparm.iph.tos & 0x1;
454 bool inherit_ttl = !tparm.iph.ttl;
455 union mlxsw_sp_l3addr gw = daddr;
456 struct net_device *l3edev;
457
458 if (!(to_dev->flags & IFF_UP) ||
459 /* Reject tunnels with GRE keys, checksums, etc. */
460 tparm.i_flags || tparm.o_flags ||
461 /* Require a fixed TTL and a TOS copied from the mirrored packet. */
462 inherit_ttl || !inherit_tos ||
463 /* A destination address may not be "any". */
464 mlxsw_sp_l3addr_is_zero(daddr))
465 return mlxsw_sp_span_entry_unoffloadable(sparmsp);
466
467 l3edev = mlxsw_sp_span_gretap4_route(to_dev, &saddr.addr4, &gw.addr4);
468 return mlxsw_sp_span_entry_tunnel_parms_common(l3edev, saddr, daddr, gw,
469 tparm.iph.ttl,
470 &arp_tbl, sparmsp);
471 }
472
473 static int
mlxsw_sp_span_entry_gretap4_configure(struct mlxsw_sp_span_entry * span_entry,struct mlxsw_sp_span_parms sparms)474 mlxsw_sp_span_entry_gretap4_configure(struct mlxsw_sp_span_entry *span_entry,
475 struct mlxsw_sp_span_parms sparms)
476 {
477 struct mlxsw_sp_port *dest_port = sparms.dest_port;
478 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp;
479 u8 local_port = dest_port->local_port;
480 char mpat_pl[MLXSW_REG_MPAT_LEN];
481 int pa_id = span_entry->id;
482
483 /* Create a new port analayzer entry for local_port. */
484 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, true,
485 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH_L3);
486 mlxsw_reg_mpat_pide_set(mpat_pl, sparms.policer_enable);
487 mlxsw_reg_mpat_pid_set(mpat_pl, sparms.policer_id);
488 mlxsw_reg_mpat_eth_rspan_pack(mpat_pl, sparms.vid);
489 mlxsw_reg_mpat_eth_rspan_l2_pack(mpat_pl,
490 MLXSW_REG_MPAT_ETH_RSPAN_VERSION_NO_HEADER,
491 sparms.dmac, !!sparms.vid);
492 mlxsw_reg_mpat_eth_rspan_l3_ipv4_pack(mpat_pl,
493 sparms.ttl, sparms.smac,
494 be32_to_cpu(sparms.saddr.addr4),
495 be32_to_cpu(sparms.daddr.addr4));
496
497 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
498 }
499
500 static void
mlxsw_sp_span_entry_gretap4_deconfigure(struct mlxsw_sp_span_entry * span_entry)501 mlxsw_sp_span_entry_gretap4_deconfigure(struct mlxsw_sp_span_entry *span_entry)
502 {
503 mlxsw_sp_span_entry_deconfigure_common(span_entry,
504 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH_L3);
505 }
506
507 static const struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_gretap4 = {
508 .can_handle = netif_is_gretap,
509 .parms_set = mlxsw_sp_span_entry_gretap4_parms,
510 .configure = mlxsw_sp_span_entry_gretap4_configure,
511 .deconfigure = mlxsw_sp_span_entry_gretap4_deconfigure,
512 };
513 #endif
514
515 #if IS_ENABLED(CONFIG_IPV6_GRE)
516 static struct net_device *
mlxsw_sp_span_gretap6_route(const struct net_device * to_dev,struct in6_addr * saddrp,struct in6_addr * daddrp)517 mlxsw_sp_span_gretap6_route(const struct net_device *to_dev,
518 struct in6_addr *saddrp,
519 struct in6_addr *daddrp)
520 {
521 struct ip6_tnl *t = netdev_priv(to_dev);
522 struct flowi6 fl6 = t->fl.u.ip6;
523 struct net_device *dev = NULL;
524 struct dst_entry *dst;
525 struct rt6_info *rt6;
526
527 /* We assume "dev" stays valid after dst is released. */
528 ASSERT_RTNL();
529
530 fl6.flowi6_mark = t->parms.fwmark;
531 if (!ip6_tnl_xmit_ctl(t, &fl6.saddr, &fl6.daddr))
532 return NULL;
533
534 dst = ip6_route_output(t->net, NULL, &fl6);
535 if (!dst || dst->error)
536 goto out;
537
538 rt6 = container_of(dst, struct rt6_info, dst);
539
540 dev = dst->dev;
541 *saddrp = fl6.saddr;
542 *daddrp = rt6->rt6i_gateway;
543
544 out:
545 dst_release(dst);
546 return dev;
547 }
548
549 static int
mlxsw_sp_span_entry_gretap6_parms(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,struct mlxsw_sp_span_parms * sparmsp)550 mlxsw_sp_span_entry_gretap6_parms(struct mlxsw_sp *mlxsw_sp,
551 const struct net_device *to_dev,
552 struct mlxsw_sp_span_parms *sparmsp)
553 {
554 struct __ip6_tnl_parm tparm = mlxsw_sp_ipip_netdev_parms6(to_dev);
555 bool inherit_tos = tparm.flags & IP6_TNL_F_USE_ORIG_TCLASS;
556 union mlxsw_sp_l3addr saddr = { .addr6 = tparm.laddr };
557 union mlxsw_sp_l3addr daddr = { .addr6 = tparm.raddr };
558 bool inherit_ttl = !tparm.hop_limit;
559 union mlxsw_sp_l3addr gw = daddr;
560 struct net_device *l3edev;
561
562 if (!(to_dev->flags & IFF_UP) ||
563 /* Reject tunnels with GRE keys, checksums, etc. */
564 tparm.i_flags || tparm.o_flags ||
565 /* Require a fixed TTL and a TOS copied from the mirrored packet. */
566 inherit_ttl || !inherit_tos ||
567 /* A destination address may not be "any". */
568 mlxsw_sp_l3addr_is_zero(daddr))
569 return mlxsw_sp_span_entry_unoffloadable(sparmsp);
570
571 l3edev = mlxsw_sp_span_gretap6_route(to_dev, &saddr.addr6, &gw.addr6);
572 return mlxsw_sp_span_entry_tunnel_parms_common(l3edev, saddr, daddr, gw,
573 tparm.hop_limit,
574 &nd_tbl, sparmsp);
575 }
576
577 static int
mlxsw_sp_span_entry_gretap6_configure(struct mlxsw_sp_span_entry * span_entry,struct mlxsw_sp_span_parms sparms)578 mlxsw_sp_span_entry_gretap6_configure(struct mlxsw_sp_span_entry *span_entry,
579 struct mlxsw_sp_span_parms sparms)
580 {
581 struct mlxsw_sp_port *dest_port = sparms.dest_port;
582 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp;
583 u8 local_port = dest_port->local_port;
584 char mpat_pl[MLXSW_REG_MPAT_LEN];
585 int pa_id = span_entry->id;
586
587 /* Create a new port analayzer entry for local_port. */
588 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, true,
589 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH_L3);
590 mlxsw_reg_mpat_pide_set(mpat_pl, sparms.policer_enable);
591 mlxsw_reg_mpat_pid_set(mpat_pl, sparms.policer_id);
592 mlxsw_reg_mpat_eth_rspan_pack(mpat_pl, sparms.vid);
593 mlxsw_reg_mpat_eth_rspan_l2_pack(mpat_pl,
594 MLXSW_REG_MPAT_ETH_RSPAN_VERSION_NO_HEADER,
595 sparms.dmac, !!sparms.vid);
596 mlxsw_reg_mpat_eth_rspan_l3_ipv6_pack(mpat_pl, sparms.ttl, sparms.smac,
597 sparms.saddr.addr6,
598 sparms.daddr.addr6);
599
600 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
601 }
602
603 static void
mlxsw_sp_span_entry_gretap6_deconfigure(struct mlxsw_sp_span_entry * span_entry)604 mlxsw_sp_span_entry_gretap6_deconfigure(struct mlxsw_sp_span_entry *span_entry)
605 {
606 mlxsw_sp_span_entry_deconfigure_common(span_entry,
607 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH_L3);
608 }
609
610 static const
611 struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_gretap6 = {
612 .can_handle = netif_is_ip6gretap,
613 .parms_set = mlxsw_sp_span_entry_gretap6_parms,
614 .configure = mlxsw_sp_span_entry_gretap6_configure,
615 .deconfigure = mlxsw_sp_span_entry_gretap6_deconfigure,
616 };
617 #endif
618
619 static bool
mlxsw_sp_span_vlan_can_handle(const struct net_device * dev)620 mlxsw_sp_span_vlan_can_handle(const struct net_device *dev)
621 {
622 return is_vlan_dev(dev) &&
623 mlxsw_sp_port_dev_check(vlan_dev_real_dev(dev));
624 }
625
626 static int
mlxsw_sp_span_entry_vlan_parms(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,struct mlxsw_sp_span_parms * sparmsp)627 mlxsw_sp_span_entry_vlan_parms(struct mlxsw_sp *mlxsw_sp,
628 const struct net_device *to_dev,
629 struct mlxsw_sp_span_parms *sparmsp)
630 {
631 struct net_device *real_dev;
632 u16 vid;
633
634 if (!(to_dev->flags & IFF_UP))
635 return mlxsw_sp_span_entry_unoffloadable(sparmsp);
636
637 real_dev = mlxsw_sp_span_entry_vlan(to_dev, &vid);
638 sparmsp->dest_port = netdev_priv(real_dev);
639 sparmsp->vid = vid;
640 return 0;
641 }
642
643 static int
mlxsw_sp_span_entry_vlan_configure(struct mlxsw_sp_span_entry * span_entry,struct mlxsw_sp_span_parms sparms)644 mlxsw_sp_span_entry_vlan_configure(struct mlxsw_sp_span_entry *span_entry,
645 struct mlxsw_sp_span_parms sparms)
646 {
647 struct mlxsw_sp_port *dest_port = sparms.dest_port;
648 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp;
649 u8 local_port = dest_port->local_port;
650 char mpat_pl[MLXSW_REG_MPAT_LEN];
651 int pa_id = span_entry->id;
652
653 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, true,
654 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH);
655 mlxsw_reg_mpat_pide_set(mpat_pl, sparms.policer_enable);
656 mlxsw_reg_mpat_pid_set(mpat_pl, sparms.policer_id);
657 mlxsw_reg_mpat_eth_rspan_pack(mpat_pl, sparms.vid);
658
659 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
660 }
661
662 static void
mlxsw_sp_span_entry_vlan_deconfigure(struct mlxsw_sp_span_entry * span_entry)663 mlxsw_sp_span_entry_vlan_deconfigure(struct mlxsw_sp_span_entry *span_entry)
664 {
665 mlxsw_sp_span_entry_deconfigure_common(span_entry,
666 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH);
667 }
668
669 static const
670 struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_vlan = {
671 .can_handle = mlxsw_sp_span_vlan_can_handle,
672 .parms_set = mlxsw_sp_span_entry_vlan_parms,
673 .configure = mlxsw_sp_span_entry_vlan_configure,
674 .deconfigure = mlxsw_sp_span_entry_vlan_deconfigure,
675 };
676
677 static const
678 struct mlxsw_sp_span_entry_ops *mlxsw_sp1_span_entry_ops_arr[] = {
679 &mlxsw_sp1_span_entry_ops_cpu,
680 &mlxsw_sp_span_entry_ops_phys,
681 #if IS_ENABLED(CONFIG_NET_IPGRE)
682 &mlxsw_sp_span_entry_ops_gretap4,
683 #endif
684 #if IS_ENABLED(CONFIG_IPV6_GRE)
685 &mlxsw_sp_span_entry_ops_gretap6,
686 #endif
687 &mlxsw_sp_span_entry_ops_vlan,
688 };
689
mlxsw_sp2_span_cpu_can_handle(const struct net_device * dev)690 static bool mlxsw_sp2_span_cpu_can_handle(const struct net_device *dev)
691 {
692 return !dev;
693 }
694
mlxsw_sp2_span_entry_cpu_parms(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,struct mlxsw_sp_span_parms * sparmsp)695 static int mlxsw_sp2_span_entry_cpu_parms(struct mlxsw_sp *mlxsw_sp,
696 const struct net_device *to_dev,
697 struct mlxsw_sp_span_parms *sparmsp)
698 {
699 sparmsp->dest_port = mlxsw_sp->ports[MLXSW_PORT_CPU_PORT];
700 return 0;
701 }
702
703 static int
mlxsw_sp2_span_entry_cpu_configure(struct mlxsw_sp_span_entry * span_entry,struct mlxsw_sp_span_parms sparms)704 mlxsw_sp2_span_entry_cpu_configure(struct mlxsw_sp_span_entry *span_entry,
705 struct mlxsw_sp_span_parms sparms)
706 {
707 /* Mirroring to the CPU port is like mirroring to any other physical
708 * port. Its local port is used instead of that of the physical port.
709 */
710 return mlxsw_sp_span_entry_phys_configure(span_entry, sparms);
711 }
712
713 static void
mlxsw_sp2_span_entry_cpu_deconfigure(struct mlxsw_sp_span_entry * span_entry)714 mlxsw_sp2_span_entry_cpu_deconfigure(struct mlxsw_sp_span_entry *span_entry)
715 {
716 enum mlxsw_reg_mpat_span_type span_type;
717
718 span_type = MLXSW_REG_MPAT_SPAN_TYPE_LOCAL_ETH;
719 mlxsw_sp_span_entry_deconfigure_common(span_entry, span_type);
720 }
721
722 static const
723 struct mlxsw_sp_span_entry_ops mlxsw_sp2_span_entry_ops_cpu = {
724 .can_handle = mlxsw_sp2_span_cpu_can_handle,
725 .parms_set = mlxsw_sp2_span_entry_cpu_parms,
726 .configure = mlxsw_sp2_span_entry_cpu_configure,
727 .deconfigure = mlxsw_sp2_span_entry_cpu_deconfigure,
728 };
729
730 static const
731 struct mlxsw_sp_span_entry_ops *mlxsw_sp2_span_entry_ops_arr[] = {
732 &mlxsw_sp2_span_entry_ops_cpu,
733 &mlxsw_sp_span_entry_ops_phys,
734 #if IS_ENABLED(CONFIG_NET_IPGRE)
735 &mlxsw_sp_span_entry_ops_gretap4,
736 #endif
737 #if IS_ENABLED(CONFIG_IPV6_GRE)
738 &mlxsw_sp_span_entry_ops_gretap6,
739 #endif
740 &mlxsw_sp_span_entry_ops_vlan,
741 };
742
743 static int
mlxsw_sp_span_entry_nop_parms(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,struct mlxsw_sp_span_parms * sparmsp)744 mlxsw_sp_span_entry_nop_parms(struct mlxsw_sp *mlxsw_sp,
745 const struct net_device *to_dev,
746 struct mlxsw_sp_span_parms *sparmsp)
747 {
748 return mlxsw_sp_span_entry_unoffloadable(sparmsp);
749 }
750
751 static int
mlxsw_sp_span_entry_nop_configure(struct mlxsw_sp_span_entry * span_entry,struct mlxsw_sp_span_parms sparms)752 mlxsw_sp_span_entry_nop_configure(struct mlxsw_sp_span_entry *span_entry,
753 struct mlxsw_sp_span_parms sparms)
754 {
755 return 0;
756 }
757
758 static void
mlxsw_sp_span_entry_nop_deconfigure(struct mlxsw_sp_span_entry * span_entry)759 mlxsw_sp_span_entry_nop_deconfigure(struct mlxsw_sp_span_entry *span_entry)
760 {
761 }
762
763 static const struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_nop = {
764 .parms_set = mlxsw_sp_span_entry_nop_parms,
765 .configure = mlxsw_sp_span_entry_nop_configure,
766 .deconfigure = mlxsw_sp_span_entry_nop_deconfigure,
767 };
768
769 static void
mlxsw_sp_span_entry_configure(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_span_entry * span_entry,struct mlxsw_sp_span_parms sparms)770 mlxsw_sp_span_entry_configure(struct mlxsw_sp *mlxsw_sp,
771 struct mlxsw_sp_span_entry *span_entry,
772 struct mlxsw_sp_span_parms sparms)
773 {
774 int err;
775
776 if (!sparms.dest_port)
777 goto set_parms;
778
779 if (sparms.dest_port->mlxsw_sp != mlxsw_sp) {
780 dev_err(mlxsw_sp->bus_info->dev,
781 "Cannot mirror to a port which belongs to a different mlxsw instance\n");
782 sparms.dest_port = NULL;
783 goto set_parms;
784 }
785
786 err = span_entry->ops->configure(span_entry, sparms);
787 if (err) {
788 dev_err(mlxsw_sp->bus_info->dev, "Failed to offload mirror\n");
789 sparms.dest_port = NULL;
790 goto set_parms;
791 }
792
793 set_parms:
794 span_entry->parms = sparms;
795 }
796
797 static void
mlxsw_sp_span_entry_deconfigure(struct mlxsw_sp_span_entry * span_entry)798 mlxsw_sp_span_entry_deconfigure(struct mlxsw_sp_span_entry *span_entry)
799 {
800 if (span_entry->parms.dest_port)
801 span_entry->ops->deconfigure(span_entry);
802 }
803
mlxsw_sp_span_policer_id_base_set(struct mlxsw_sp_span * span,u16 policer_id)804 static int mlxsw_sp_span_policer_id_base_set(struct mlxsw_sp_span *span,
805 u16 policer_id)
806 {
807 struct mlxsw_sp *mlxsw_sp = span->mlxsw_sp;
808 u16 policer_id_base;
809 int err;
810
811 /* Policers set on SPAN agents must be in the range of
812 * `policer_id_base .. policer_id_base + max_span_agents - 1`. If the
813 * base is set and the new policer is not within the range, then we
814 * must error out.
815 */
816 if (refcount_read(&span->policer_id_base_ref_count)) {
817 if (policer_id < span->policer_id_base ||
818 policer_id >= span->policer_id_base + span->entries_count)
819 return -EINVAL;
820
821 refcount_inc(&span->policer_id_base_ref_count);
822 return 0;
823 }
824
825 /* Base must be even. */
826 policer_id_base = policer_id % 2 == 0 ? policer_id : policer_id - 1;
827 err = mlxsw_sp->span_ops->policer_id_base_set(mlxsw_sp,
828 policer_id_base);
829 if (err)
830 return err;
831
832 span->policer_id_base = policer_id_base;
833 refcount_set(&span->policer_id_base_ref_count, 1);
834
835 return 0;
836 }
837
mlxsw_sp_span_policer_id_base_unset(struct mlxsw_sp_span * span)838 static void mlxsw_sp_span_policer_id_base_unset(struct mlxsw_sp_span *span)
839 {
840 if (refcount_dec_and_test(&span->policer_id_base_ref_count))
841 span->policer_id_base = 0;
842 }
843
844 static struct mlxsw_sp_span_entry *
mlxsw_sp_span_entry_create(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,const struct mlxsw_sp_span_entry_ops * ops,struct mlxsw_sp_span_parms sparms)845 mlxsw_sp_span_entry_create(struct mlxsw_sp *mlxsw_sp,
846 const struct net_device *to_dev,
847 const struct mlxsw_sp_span_entry_ops *ops,
848 struct mlxsw_sp_span_parms sparms)
849 {
850 struct mlxsw_sp_span_entry *span_entry = NULL;
851 int i;
852
853 /* find a free entry to use */
854 for (i = 0; i < mlxsw_sp->span->entries_count; i++) {
855 if (!refcount_read(&mlxsw_sp->span->entries[i].ref_count)) {
856 span_entry = &mlxsw_sp->span->entries[i];
857 break;
858 }
859 }
860 if (!span_entry)
861 return NULL;
862
863 if (sparms.policer_enable) {
864 int err;
865
866 err = mlxsw_sp_span_policer_id_base_set(mlxsw_sp->span,
867 sparms.policer_id);
868 if (err)
869 return NULL;
870 }
871
872 atomic_inc(&mlxsw_sp->span->active_entries_count);
873 span_entry->ops = ops;
874 refcount_set(&span_entry->ref_count, 1);
875 span_entry->to_dev = to_dev;
876 mlxsw_sp_span_entry_configure(mlxsw_sp, span_entry, sparms);
877
878 return span_entry;
879 }
880
mlxsw_sp_span_entry_destroy(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_span_entry * span_entry)881 static void mlxsw_sp_span_entry_destroy(struct mlxsw_sp *mlxsw_sp,
882 struct mlxsw_sp_span_entry *span_entry)
883 {
884 mlxsw_sp_span_entry_deconfigure(span_entry);
885 atomic_dec(&mlxsw_sp->span->active_entries_count);
886 if (span_entry->parms.policer_enable)
887 mlxsw_sp_span_policer_id_base_unset(mlxsw_sp->span);
888 }
889
890 struct mlxsw_sp_span_entry *
mlxsw_sp_span_entry_find_by_port(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev)891 mlxsw_sp_span_entry_find_by_port(struct mlxsw_sp *mlxsw_sp,
892 const struct net_device *to_dev)
893 {
894 int i;
895
896 for (i = 0; i < mlxsw_sp->span->entries_count; i++) {
897 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span->entries[i];
898
899 if (refcount_read(&curr->ref_count) && curr->to_dev == to_dev)
900 return curr;
901 }
902 return NULL;
903 }
904
mlxsw_sp_span_entry_invalidate(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_span_entry * span_entry)905 void mlxsw_sp_span_entry_invalidate(struct mlxsw_sp *mlxsw_sp,
906 struct mlxsw_sp_span_entry *span_entry)
907 {
908 mlxsw_sp_span_entry_deconfigure(span_entry);
909 span_entry->ops = &mlxsw_sp_span_entry_ops_nop;
910 }
911
912 static struct mlxsw_sp_span_entry *
mlxsw_sp_span_entry_find_by_id(struct mlxsw_sp * mlxsw_sp,int span_id)913 mlxsw_sp_span_entry_find_by_id(struct mlxsw_sp *mlxsw_sp, int span_id)
914 {
915 int i;
916
917 for (i = 0; i < mlxsw_sp->span->entries_count; i++) {
918 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span->entries[i];
919
920 if (refcount_read(&curr->ref_count) && curr->id == span_id)
921 return curr;
922 }
923 return NULL;
924 }
925
926 static struct mlxsw_sp_span_entry *
mlxsw_sp_span_entry_find_by_parms(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,const struct mlxsw_sp_span_parms * sparms)927 mlxsw_sp_span_entry_find_by_parms(struct mlxsw_sp *mlxsw_sp,
928 const struct net_device *to_dev,
929 const struct mlxsw_sp_span_parms *sparms)
930 {
931 int i;
932
933 for (i = 0; i < mlxsw_sp->span->entries_count; i++) {
934 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span->entries[i];
935
936 if (refcount_read(&curr->ref_count) && curr->to_dev == to_dev &&
937 curr->parms.policer_enable == sparms->policer_enable &&
938 curr->parms.policer_id == sparms->policer_id)
939 return curr;
940 }
941 return NULL;
942 }
943
944 static struct mlxsw_sp_span_entry *
mlxsw_sp_span_entry_get(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev,const struct mlxsw_sp_span_entry_ops * ops,struct mlxsw_sp_span_parms sparms)945 mlxsw_sp_span_entry_get(struct mlxsw_sp *mlxsw_sp,
946 const struct net_device *to_dev,
947 const struct mlxsw_sp_span_entry_ops *ops,
948 struct mlxsw_sp_span_parms sparms)
949 {
950 struct mlxsw_sp_span_entry *span_entry;
951
952 span_entry = mlxsw_sp_span_entry_find_by_parms(mlxsw_sp, to_dev,
953 &sparms);
954 if (span_entry) {
955 /* Already exists, just take a reference */
956 refcount_inc(&span_entry->ref_count);
957 return span_entry;
958 }
959
960 return mlxsw_sp_span_entry_create(mlxsw_sp, to_dev, ops, sparms);
961 }
962
mlxsw_sp_span_entry_put(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_span_entry * span_entry)963 static int mlxsw_sp_span_entry_put(struct mlxsw_sp *mlxsw_sp,
964 struct mlxsw_sp_span_entry *span_entry)
965 {
966 if (refcount_dec_and_test(&span_entry->ref_count))
967 mlxsw_sp_span_entry_destroy(mlxsw_sp, span_entry);
968 return 0;
969 }
970
mlxsw_sp_span_port_buffer_update(struct mlxsw_sp_port * mlxsw_sp_port,bool enable)971 static int mlxsw_sp_span_port_buffer_update(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
972 {
973 struct mlxsw_sp_hdroom hdroom;
974
975 hdroom = *mlxsw_sp_port->hdroom;
976 hdroom.int_buf.enable = enable;
977 mlxsw_sp_hdroom_bufs_reset_sizes(mlxsw_sp_port, &hdroom);
978
979 return mlxsw_sp_hdroom_configure(mlxsw_sp_port, &hdroom);
980 }
981
982 static int
mlxsw_sp_span_port_buffer_enable(struct mlxsw_sp_port * mlxsw_sp_port)983 mlxsw_sp_span_port_buffer_enable(struct mlxsw_sp_port *mlxsw_sp_port)
984 {
985 return mlxsw_sp_span_port_buffer_update(mlxsw_sp_port, true);
986 }
987
mlxsw_sp_span_port_buffer_disable(struct mlxsw_sp_port * mlxsw_sp_port)988 static void mlxsw_sp_span_port_buffer_disable(struct mlxsw_sp_port *mlxsw_sp_port)
989 {
990 mlxsw_sp_span_port_buffer_update(mlxsw_sp_port, false);
991 }
992
993 static struct mlxsw_sp_span_analyzed_port *
mlxsw_sp_span_analyzed_port_find(struct mlxsw_sp_span * span,u8 local_port,bool ingress)994 mlxsw_sp_span_analyzed_port_find(struct mlxsw_sp_span *span, u8 local_port,
995 bool ingress)
996 {
997 struct mlxsw_sp_span_analyzed_port *analyzed_port;
998
999 list_for_each_entry(analyzed_port, &span->analyzed_ports_list, list) {
1000 if (analyzed_port->local_port == local_port &&
1001 analyzed_port->ingress == ingress)
1002 return analyzed_port;
1003 }
1004
1005 return NULL;
1006 }
1007
1008 static const struct mlxsw_sp_span_entry_ops *
mlxsw_sp_span_entry_ops(struct mlxsw_sp * mlxsw_sp,const struct net_device * to_dev)1009 mlxsw_sp_span_entry_ops(struct mlxsw_sp *mlxsw_sp,
1010 const struct net_device *to_dev)
1011 {
1012 struct mlxsw_sp_span *span = mlxsw_sp->span;
1013 size_t i;
1014
1015 for (i = 0; i < span->span_entry_ops_arr_size; ++i)
1016 if (span->span_entry_ops_arr[i]->can_handle(to_dev))
1017 return span->span_entry_ops_arr[i];
1018
1019 return NULL;
1020 }
1021
mlxsw_sp_span_respin_work(struct work_struct * work)1022 static void mlxsw_sp_span_respin_work(struct work_struct *work)
1023 {
1024 struct mlxsw_sp_span *span;
1025 struct mlxsw_sp *mlxsw_sp;
1026 int i, err;
1027
1028 span = container_of(work, struct mlxsw_sp_span, work);
1029 mlxsw_sp = span->mlxsw_sp;
1030
1031 rtnl_lock();
1032 for (i = 0; i < mlxsw_sp->span->entries_count; i++) {
1033 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span->entries[i];
1034 struct mlxsw_sp_span_parms sparms = {NULL};
1035
1036 if (!refcount_read(&curr->ref_count))
1037 continue;
1038
1039 err = curr->ops->parms_set(mlxsw_sp, curr->to_dev, &sparms);
1040 if (err)
1041 continue;
1042
1043 if (memcmp(&sparms, &curr->parms, sizeof(sparms))) {
1044 mlxsw_sp_span_entry_deconfigure(curr);
1045 mlxsw_sp_span_entry_configure(mlxsw_sp, curr, sparms);
1046 }
1047 }
1048 rtnl_unlock();
1049 }
1050
mlxsw_sp_span_respin(struct mlxsw_sp * mlxsw_sp)1051 void mlxsw_sp_span_respin(struct mlxsw_sp *mlxsw_sp)
1052 {
1053 if (atomic_read(&mlxsw_sp->span->active_entries_count) == 0)
1054 return;
1055 mlxsw_core_schedule_work(&mlxsw_sp->span->work);
1056 }
1057
mlxsw_sp_span_agent_get(struct mlxsw_sp * mlxsw_sp,int * p_span_id,const struct mlxsw_sp_span_agent_parms * parms)1058 int mlxsw_sp_span_agent_get(struct mlxsw_sp *mlxsw_sp, int *p_span_id,
1059 const struct mlxsw_sp_span_agent_parms *parms)
1060 {
1061 const struct net_device *to_dev = parms->to_dev;
1062 const struct mlxsw_sp_span_entry_ops *ops;
1063 struct mlxsw_sp_span_entry *span_entry;
1064 struct mlxsw_sp_span_parms sparms;
1065 int err;
1066
1067 ASSERT_RTNL();
1068
1069 ops = mlxsw_sp_span_entry_ops(mlxsw_sp, to_dev);
1070 if (!ops) {
1071 dev_err(mlxsw_sp->bus_info->dev, "Cannot mirror to requested destination\n");
1072 return -EOPNOTSUPP;
1073 }
1074
1075 memset(&sparms, 0, sizeof(sparms));
1076 err = ops->parms_set(mlxsw_sp, to_dev, &sparms);
1077 if (err)
1078 return err;
1079
1080 sparms.policer_id = parms->policer_id;
1081 sparms.policer_enable = parms->policer_enable;
1082 span_entry = mlxsw_sp_span_entry_get(mlxsw_sp, to_dev, ops, sparms);
1083 if (!span_entry)
1084 return -ENOBUFS;
1085
1086 *p_span_id = span_entry->id;
1087
1088 return 0;
1089 }
1090
mlxsw_sp_span_agent_put(struct mlxsw_sp * mlxsw_sp,int span_id)1091 void mlxsw_sp_span_agent_put(struct mlxsw_sp *mlxsw_sp, int span_id)
1092 {
1093 struct mlxsw_sp_span_entry *span_entry;
1094
1095 ASSERT_RTNL();
1096
1097 span_entry = mlxsw_sp_span_entry_find_by_id(mlxsw_sp, span_id);
1098 if (WARN_ON_ONCE(!span_entry))
1099 return;
1100
1101 mlxsw_sp_span_entry_put(mlxsw_sp, span_entry);
1102 }
1103
1104 static struct mlxsw_sp_span_analyzed_port *
mlxsw_sp_span_analyzed_port_create(struct mlxsw_sp_span * span,struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)1105 mlxsw_sp_span_analyzed_port_create(struct mlxsw_sp_span *span,
1106 struct mlxsw_sp_port *mlxsw_sp_port,
1107 bool ingress)
1108 {
1109 struct mlxsw_sp_span_analyzed_port *analyzed_port;
1110 int err;
1111
1112 analyzed_port = kzalloc(sizeof(*analyzed_port), GFP_KERNEL);
1113 if (!analyzed_port)
1114 return ERR_PTR(-ENOMEM);
1115
1116 refcount_set(&analyzed_port->ref_count, 1);
1117 analyzed_port->local_port = mlxsw_sp_port->local_port;
1118 analyzed_port->ingress = ingress;
1119 list_add_tail(&analyzed_port->list, &span->analyzed_ports_list);
1120
1121 /* An egress mirror buffer should be allocated on the egress port which
1122 * does the mirroring.
1123 */
1124 if (!ingress) {
1125 err = mlxsw_sp_span_port_buffer_enable(mlxsw_sp_port);
1126 if (err)
1127 goto err_buffer_update;
1128 }
1129
1130 return analyzed_port;
1131
1132 err_buffer_update:
1133 list_del(&analyzed_port->list);
1134 kfree(analyzed_port);
1135 return ERR_PTR(err);
1136 }
1137
1138 static void
mlxsw_sp_span_analyzed_port_destroy(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_span_analyzed_port * analyzed_port)1139 mlxsw_sp_span_analyzed_port_destroy(struct mlxsw_sp_port *mlxsw_sp_port,
1140 struct mlxsw_sp_span_analyzed_port *
1141 analyzed_port)
1142 {
1143 /* Remove egress mirror buffer now that port is no longer analyzed
1144 * at egress.
1145 */
1146 if (!analyzed_port->ingress)
1147 mlxsw_sp_span_port_buffer_disable(mlxsw_sp_port);
1148
1149 list_del(&analyzed_port->list);
1150 kfree(analyzed_port);
1151 }
1152
mlxsw_sp_span_analyzed_port_get(struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)1153 int mlxsw_sp_span_analyzed_port_get(struct mlxsw_sp_port *mlxsw_sp_port,
1154 bool ingress)
1155 {
1156 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1157 struct mlxsw_sp_span_analyzed_port *analyzed_port;
1158 u8 local_port = mlxsw_sp_port->local_port;
1159 int err = 0;
1160
1161 mutex_lock(&mlxsw_sp->span->analyzed_ports_lock);
1162
1163 analyzed_port = mlxsw_sp_span_analyzed_port_find(mlxsw_sp->span,
1164 local_port, ingress);
1165 if (analyzed_port) {
1166 refcount_inc(&analyzed_port->ref_count);
1167 goto out_unlock;
1168 }
1169
1170 analyzed_port = mlxsw_sp_span_analyzed_port_create(mlxsw_sp->span,
1171 mlxsw_sp_port,
1172 ingress);
1173 if (IS_ERR(analyzed_port))
1174 err = PTR_ERR(analyzed_port);
1175
1176 out_unlock:
1177 mutex_unlock(&mlxsw_sp->span->analyzed_ports_lock);
1178 return err;
1179 }
1180
mlxsw_sp_span_analyzed_port_put(struct mlxsw_sp_port * mlxsw_sp_port,bool ingress)1181 void mlxsw_sp_span_analyzed_port_put(struct mlxsw_sp_port *mlxsw_sp_port,
1182 bool ingress)
1183 {
1184 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1185 struct mlxsw_sp_span_analyzed_port *analyzed_port;
1186 u8 local_port = mlxsw_sp_port->local_port;
1187
1188 mutex_lock(&mlxsw_sp->span->analyzed_ports_lock);
1189
1190 analyzed_port = mlxsw_sp_span_analyzed_port_find(mlxsw_sp->span,
1191 local_port, ingress);
1192 if (WARN_ON_ONCE(!analyzed_port))
1193 goto out_unlock;
1194
1195 if (!refcount_dec_and_test(&analyzed_port->ref_count))
1196 goto out_unlock;
1197
1198 mlxsw_sp_span_analyzed_port_destroy(mlxsw_sp_port, analyzed_port);
1199
1200 out_unlock:
1201 mutex_unlock(&mlxsw_sp->span->analyzed_ports_lock);
1202 }
1203
1204 static int
__mlxsw_sp_span_trigger_port_bind(struct mlxsw_sp_span * span,struct mlxsw_sp_span_trigger_entry * trigger_entry,bool enable)1205 __mlxsw_sp_span_trigger_port_bind(struct mlxsw_sp_span *span,
1206 struct mlxsw_sp_span_trigger_entry *
1207 trigger_entry, bool enable)
1208 {
1209 char mpar_pl[MLXSW_REG_MPAR_LEN];
1210 enum mlxsw_reg_mpar_i_e i_e;
1211
1212 switch (trigger_entry->trigger) {
1213 case MLXSW_SP_SPAN_TRIGGER_INGRESS:
1214 i_e = MLXSW_REG_MPAR_TYPE_INGRESS;
1215 break;
1216 case MLXSW_SP_SPAN_TRIGGER_EGRESS:
1217 i_e = MLXSW_REG_MPAR_TYPE_EGRESS;
1218 break;
1219 default:
1220 WARN_ON_ONCE(1);
1221 return -EINVAL;
1222 }
1223
1224 mlxsw_reg_mpar_pack(mpar_pl, trigger_entry->local_port, i_e, enable,
1225 trigger_entry->parms.span_id);
1226 return mlxsw_reg_write(span->mlxsw_sp->core, MLXSW_REG(mpar), mpar_pl);
1227 }
1228
1229 static int
mlxsw_sp_span_trigger_port_bind(struct mlxsw_sp_span_trigger_entry * trigger_entry)1230 mlxsw_sp_span_trigger_port_bind(struct mlxsw_sp_span_trigger_entry *
1231 trigger_entry)
1232 {
1233 return __mlxsw_sp_span_trigger_port_bind(trigger_entry->span,
1234 trigger_entry, true);
1235 }
1236
1237 static void
mlxsw_sp_span_trigger_port_unbind(struct mlxsw_sp_span_trigger_entry * trigger_entry)1238 mlxsw_sp_span_trigger_port_unbind(struct mlxsw_sp_span_trigger_entry *
1239 trigger_entry)
1240 {
1241 __mlxsw_sp_span_trigger_port_bind(trigger_entry->span, trigger_entry,
1242 false);
1243 }
1244
1245 static bool
mlxsw_sp_span_trigger_port_matches(struct mlxsw_sp_span_trigger_entry * trigger_entry,enum mlxsw_sp_span_trigger trigger,struct mlxsw_sp_port * mlxsw_sp_port)1246 mlxsw_sp_span_trigger_port_matches(struct mlxsw_sp_span_trigger_entry *
1247 trigger_entry,
1248 enum mlxsw_sp_span_trigger trigger,
1249 struct mlxsw_sp_port *mlxsw_sp_port)
1250 {
1251 return trigger_entry->trigger == trigger &&
1252 trigger_entry->local_port == mlxsw_sp_port->local_port;
1253 }
1254
1255 static int
mlxsw_sp_span_trigger_port_enable(struct mlxsw_sp_span_trigger_entry * trigger_entry,struct mlxsw_sp_port * mlxsw_sp_port,u8 tc)1256 mlxsw_sp_span_trigger_port_enable(struct mlxsw_sp_span_trigger_entry *
1257 trigger_entry,
1258 struct mlxsw_sp_port *mlxsw_sp_port, u8 tc)
1259 {
1260 /* Port trigger are enabled during binding. */
1261 return 0;
1262 }
1263
1264 static void
mlxsw_sp_span_trigger_port_disable(struct mlxsw_sp_span_trigger_entry * trigger_entry,struct mlxsw_sp_port * mlxsw_sp_port,u8 tc)1265 mlxsw_sp_span_trigger_port_disable(struct mlxsw_sp_span_trigger_entry *
1266 trigger_entry,
1267 struct mlxsw_sp_port *mlxsw_sp_port, u8 tc)
1268 {
1269 }
1270
1271 static const struct mlxsw_sp_span_trigger_ops
1272 mlxsw_sp_span_trigger_port_ops = {
1273 .bind = mlxsw_sp_span_trigger_port_bind,
1274 .unbind = mlxsw_sp_span_trigger_port_unbind,
1275 .matches = mlxsw_sp_span_trigger_port_matches,
1276 .enable = mlxsw_sp_span_trigger_port_enable,
1277 .disable = mlxsw_sp_span_trigger_port_disable,
1278 };
1279
1280 static int
mlxsw_sp1_span_trigger_global_bind(struct mlxsw_sp_span_trigger_entry * trigger_entry)1281 mlxsw_sp1_span_trigger_global_bind(struct mlxsw_sp_span_trigger_entry *
1282 trigger_entry)
1283 {
1284 return -EOPNOTSUPP;
1285 }
1286
1287 static void
mlxsw_sp1_span_trigger_global_unbind(struct mlxsw_sp_span_trigger_entry * trigger_entry)1288 mlxsw_sp1_span_trigger_global_unbind(struct mlxsw_sp_span_trigger_entry *
1289 trigger_entry)
1290 {
1291 }
1292
1293 static bool
mlxsw_sp1_span_trigger_global_matches(struct mlxsw_sp_span_trigger_entry * trigger_entry,enum mlxsw_sp_span_trigger trigger,struct mlxsw_sp_port * mlxsw_sp_port)1294 mlxsw_sp1_span_trigger_global_matches(struct mlxsw_sp_span_trigger_entry *
1295 trigger_entry,
1296 enum mlxsw_sp_span_trigger trigger,
1297 struct mlxsw_sp_port *mlxsw_sp_port)
1298 {
1299 WARN_ON_ONCE(1);
1300 return false;
1301 }
1302
1303 static int
mlxsw_sp1_span_trigger_global_enable(struct mlxsw_sp_span_trigger_entry * trigger_entry,struct mlxsw_sp_port * mlxsw_sp_port,u8 tc)1304 mlxsw_sp1_span_trigger_global_enable(struct mlxsw_sp_span_trigger_entry *
1305 trigger_entry,
1306 struct mlxsw_sp_port *mlxsw_sp_port,
1307 u8 tc)
1308 {
1309 return -EOPNOTSUPP;
1310 }
1311
1312 static void
mlxsw_sp1_span_trigger_global_disable(struct mlxsw_sp_span_trigger_entry * trigger_entry,struct mlxsw_sp_port * mlxsw_sp_port,u8 tc)1313 mlxsw_sp1_span_trigger_global_disable(struct mlxsw_sp_span_trigger_entry *
1314 trigger_entry,
1315 struct mlxsw_sp_port *mlxsw_sp_port,
1316 u8 tc)
1317 {
1318 }
1319
1320 static const struct mlxsw_sp_span_trigger_ops
1321 mlxsw_sp1_span_trigger_global_ops = {
1322 .bind = mlxsw_sp1_span_trigger_global_bind,
1323 .unbind = mlxsw_sp1_span_trigger_global_unbind,
1324 .matches = mlxsw_sp1_span_trigger_global_matches,
1325 .enable = mlxsw_sp1_span_trigger_global_enable,
1326 .disable = mlxsw_sp1_span_trigger_global_disable,
1327 };
1328
1329 static const struct mlxsw_sp_span_trigger_ops *
1330 mlxsw_sp1_span_trigger_ops_arr[] = {
1331 [MLXSW_SP_SPAN_TRIGGER_TYPE_PORT] = &mlxsw_sp_span_trigger_port_ops,
1332 [MLXSW_SP_SPAN_TRIGGER_TYPE_GLOBAL] =
1333 &mlxsw_sp1_span_trigger_global_ops,
1334 };
1335
1336 static int
mlxsw_sp2_span_trigger_global_bind(struct mlxsw_sp_span_trigger_entry * trigger_entry)1337 mlxsw_sp2_span_trigger_global_bind(struct mlxsw_sp_span_trigger_entry *
1338 trigger_entry)
1339 {
1340 struct mlxsw_sp *mlxsw_sp = trigger_entry->span->mlxsw_sp;
1341 enum mlxsw_reg_mpagr_trigger trigger;
1342 char mpagr_pl[MLXSW_REG_MPAGR_LEN];
1343
1344 switch (trigger_entry->trigger) {
1345 case MLXSW_SP_SPAN_TRIGGER_TAIL_DROP:
1346 trigger = MLXSW_REG_MPAGR_TRIGGER_INGRESS_SHARED_BUFFER;
1347 break;
1348 case MLXSW_SP_SPAN_TRIGGER_EARLY_DROP:
1349 trigger = MLXSW_REG_MPAGR_TRIGGER_INGRESS_WRED;
1350 break;
1351 case MLXSW_SP_SPAN_TRIGGER_ECN:
1352 trigger = MLXSW_REG_MPAGR_TRIGGER_EGRESS_ECN;
1353 break;
1354 default:
1355 WARN_ON_ONCE(1);
1356 return -EINVAL;
1357 }
1358
1359 mlxsw_reg_mpagr_pack(mpagr_pl, trigger, trigger_entry->parms.span_id,
1360 1);
1361 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpagr), mpagr_pl);
1362 }
1363
1364 static void
mlxsw_sp2_span_trigger_global_unbind(struct mlxsw_sp_span_trigger_entry * trigger_entry)1365 mlxsw_sp2_span_trigger_global_unbind(struct mlxsw_sp_span_trigger_entry *
1366 trigger_entry)
1367 {
1368 /* There is no unbinding for global triggers. The trigger should be
1369 * disabled on all ports by now.
1370 */
1371 }
1372
1373 static bool
mlxsw_sp2_span_trigger_global_matches(struct mlxsw_sp_span_trigger_entry * trigger_entry,enum mlxsw_sp_span_trigger trigger,struct mlxsw_sp_port * mlxsw_sp_port)1374 mlxsw_sp2_span_trigger_global_matches(struct mlxsw_sp_span_trigger_entry *
1375 trigger_entry,
1376 enum mlxsw_sp_span_trigger trigger,
1377 struct mlxsw_sp_port *mlxsw_sp_port)
1378 {
1379 return trigger_entry->trigger == trigger;
1380 }
1381
1382 static int
__mlxsw_sp2_span_trigger_global_enable(struct mlxsw_sp_span_trigger_entry * trigger_entry,struct mlxsw_sp_port * mlxsw_sp_port,u8 tc,bool enable)1383 __mlxsw_sp2_span_trigger_global_enable(struct mlxsw_sp_span_trigger_entry *
1384 trigger_entry,
1385 struct mlxsw_sp_port *mlxsw_sp_port,
1386 u8 tc, bool enable)
1387 {
1388 struct mlxsw_sp *mlxsw_sp = trigger_entry->span->mlxsw_sp;
1389 char momte_pl[MLXSW_REG_MOMTE_LEN];
1390 enum mlxsw_reg_momte_type type;
1391 int err;
1392
1393 switch (trigger_entry->trigger) {
1394 case MLXSW_SP_SPAN_TRIGGER_TAIL_DROP:
1395 type = MLXSW_REG_MOMTE_TYPE_SHARED_BUFFER_TCLASS;
1396 break;
1397 case MLXSW_SP_SPAN_TRIGGER_EARLY_DROP:
1398 type = MLXSW_REG_MOMTE_TYPE_WRED;
1399 break;
1400 case MLXSW_SP_SPAN_TRIGGER_ECN:
1401 type = MLXSW_REG_MOMTE_TYPE_ECN;
1402 break;
1403 default:
1404 WARN_ON_ONCE(1);
1405 return -EINVAL;
1406 }
1407
1408 /* Query existing configuration in order to only change the state of
1409 * the specified traffic class.
1410 */
1411 mlxsw_reg_momte_pack(momte_pl, mlxsw_sp_port->local_port, type);
1412 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(momte), momte_pl);
1413 if (err)
1414 return err;
1415
1416 mlxsw_reg_momte_tclass_en_set(momte_pl, tc, enable);
1417 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(momte), momte_pl);
1418 }
1419
1420 static int
mlxsw_sp2_span_trigger_global_enable(struct mlxsw_sp_span_trigger_entry * trigger_entry,struct mlxsw_sp_port * mlxsw_sp_port,u8 tc)1421 mlxsw_sp2_span_trigger_global_enable(struct mlxsw_sp_span_trigger_entry *
1422 trigger_entry,
1423 struct mlxsw_sp_port *mlxsw_sp_port,
1424 u8 tc)
1425 {
1426 return __mlxsw_sp2_span_trigger_global_enable(trigger_entry,
1427 mlxsw_sp_port, tc, true);
1428 }
1429
1430 static void
mlxsw_sp2_span_trigger_global_disable(struct mlxsw_sp_span_trigger_entry * trigger_entry,struct mlxsw_sp_port * mlxsw_sp_port,u8 tc)1431 mlxsw_sp2_span_trigger_global_disable(struct mlxsw_sp_span_trigger_entry *
1432 trigger_entry,
1433 struct mlxsw_sp_port *mlxsw_sp_port,
1434 u8 tc)
1435 {
1436 __mlxsw_sp2_span_trigger_global_enable(trigger_entry, mlxsw_sp_port, tc,
1437 false);
1438 }
1439
1440 static const struct mlxsw_sp_span_trigger_ops
1441 mlxsw_sp2_span_trigger_global_ops = {
1442 .bind = mlxsw_sp2_span_trigger_global_bind,
1443 .unbind = mlxsw_sp2_span_trigger_global_unbind,
1444 .matches = mlxsw_sp2_span_trigger_global_matches,
1445 .enable = mlxsw_sp2_span_trigger_global_enable,
1446 .disable = mlxsw_sp2_span_trigger_global_disable,
1447 };
1448
1449 static const struct mlxsw_sp_span_trigger_ops *
1450 mlxsw_sp2_span_trigger_ops_arr[] = {
1451 [MLXSW_SP_SPAN_TRIGGER_TYPE_PORT] = &mlxsw_sp_span_trigger_port_ops,
1452 [MLXSW_SP_SPAN_TRIGGER_TYPE_GLOBAL] =
1453 &mlxsw_sp2_span_trigger_global_ops,
1454 };
1455
1456 static void
mlxsw_sp_span_trigger_ops_set(struct mlxsw_sp_span_trigger_entry * trigger_entry)1457 mlxsw_sp_span_trigger_ops_set(struct mlxsw_sp_span_trigger_entry *trigger_entry)
1458 {
1459 struct mlxsw_sp_span *span = trigger_entry->span;
1460 enum mlxsw_sp_span_trigger_type type;
1461
1462 switch (trigger_entry->trigger) {
1463 case MLXSW_SP_SPAN_TRIGGER_INGRESS:
1464 case MLXSW_SP_SPAN_TRIGGER_EGRESS:
1465 type = MLXSW_SP_SPAN_TRIGGER_TYPE_PORT;
1466 break;
1467 case MLXSW_SP_SPAN_TRIGGER_TAIL_DROP:
1468 case MLXSW_SP_SPAN_TRIGGER_EARLY_DROP:
1469 case MLXSW_SP_SPAN_TRIGGER_ECN:
1470 type = MLXSW_SP_SPAN_TRIGGER_TYPE_GLOBAL;
1471 break;
1472 default:
1473 WARN_ON_ONCE(1);
1474 return;
1475 }
1476
1477 trigger_entry->ops = span->span_trigger_ops_arr[type];
1478 }
1479
1480 static struct mlxsw_sp_span_trigger_entry *
mlxsw_sp_span_trigger_entry_create(struct mlxsw_sp_span * span,enum mlxsw_sp_span_trigger trigger,struct mlxsw_sp_port * mlxsw_sp_port,const struct mlxsw_sp_span_trigger_parms * parms)1481 mlxsw_sp_span_trigger_entry_create(struct mlxsw_sp_span *span,
1482 enum mlxsw_sp_span_trigger trigger,
1483 struct mlxsw_sp_port *mlxsw_sp_port,
1484 const struct mlxsw_sp_span_trigger_parms
1485 *parms)
1486 {
1487 struct mlxsw_sp_span_trigger_entry *trigger_entry;
1488 int err;
1489
1490 trigger_entry = kzalloc(sizeof(*trigger_entry), GFP_KERNEL);
1491 if (!trigger_entry)
1492 return ERR_PTR(-ENOMEM);
1493
1494 refcount_set(&trigger_entry->ref_count, 1);
1495 trigger_entry->local_port = mlxsw_sp_port ? mlxsw_sp_port->local_port :
1496 0;
1497 trigger_entry->trigger = trigger;
1498 memcpy(&trigger_entry->parms, parms, sizeof(trigger_entry->parms));
1499 trigger_entry->span = span;
1500 mlxsw_sp_span_trigger_ops_set(trigger_entry);
1501 list_add_tail(&trigger_entry->list, &span->trigger_entries_list);
1502
1503 err = trigger_entry->ops->bind(trigger_entry);
1504 if (err)
1505 goto err_trigger_entry_bind;
1506
1507 return trigger_entry;
1508
1509 err_trigger_entry_bind:
1510 list_del(&trigger_entry->list);
1511 kfree(trigger_entry);
1512 return ERR_PTR(err);
1513 }
1514
1515 static void
mlxsw_sp_span_trigger_entry_destroy(struct mlxsw_sp_span * span,struct mlxsw_sp_span_trigger_entry * trigger_entry)1516 mlxsw_sp_span_trigger_entry_destroy(struct mlxsw_sp_span *span,
1517 struct mlxsw_sp_span_trigger_entry *
1518 trigger_entry)
1519 {
1520 trigger_entry->ops->unbind(trigger_entry);
1521 list_del(&trigger_entry->list);
1522 kfree(trigger_entry);
1523 }
1524
1525 static struct mlxsw_sp_span_trigger_entry *
mlxsw_sp_span_trigger_entry_find(struct mlxsw_sp_span * span,enum mlxsw_sp_span_trigger trigger,struct mlxsw_sp_port * mlxsw_sp_port)1526 mlxsw_sp_span_trigger_entry_find(struct mlxsw_sp_span *span,
1527 enum mlxsw_sp_span_trigger trigger,
1528 struct mlxsw_sp_port *mlxsw_sp_port)
1529 {
1530 struct mlxsw_sp_span_trigger_entry *trigger_entry;
1531
1532 list_for_each_entry(trigger_entry, &span->trigger_entries_list, list) {
1533 if (trigger_entry->ops->matches(trigger_entry, trigger,
1534 mlxsw_sp_port))
1535 return trigger_entry;
1536 }
1537
1538 return NULL;
1539 }
1540
mlxsw_sp_span_agent_bind(struct mlxsw_sp * mlxsw_sp,enum mlxsw_sp_span_trigger trigger,struct mlxsw_sp_port * mlxsw_sp_port,const struct mlxsw_sp_span_trigger_parms * parms)1541 int mlxsw_sp_span_agent_bind(struct mlxsw_sp *mlxsw_sp,
1542 enum mlxsw_sp_span_trigger trigger,
1543 struct mlxsw_sp_port *mlxsw_sp_port,
1544 const struct mlxsw_sp_span_trigger_parms *parms)
1545 {
1546 struct mlxsw_sp_span_trigger_entry *trigger_entry;
1547 int err = 0;
1548
1549 ASSERT_RTNL();
1550
1551 if (!mlxsw_sp_span_entry_find_by_id(mlxsw_sp, parms->span_id))
1552 return -EINVAL;
1553
1554 trigger_entry = mlxsw_sp_span_trigger_entry_find(mlxsw_sp->span,
1555 trigger,
1556 mlxsw_sp_port);
1557 if (trigger_entry) {
1558 if (trigger_entry->parms.span_id != parms->span_id)
1559 return -EINVAL;
1560 refcount_inc(&trigger_entry->ref_count);
1561 goto out;
1562 }
1563
1564 trigger_entry = mlxsw_sp_span_trigger_entry_create(mlxsw_sp->span,
1565 trigger,
1566 mlxsw_sp_port,
1567 parms);
1568 if (IS_ERR(trigger_entry))
1569 err = PTR_ERR(trigger_entry);
1570
1571 out:
1572 return err;
1573 }
1574
mlxsw_sp_span_agent_unbind(struct mlxsw_sp * mlxsw_sp,enum mlxsw_sp_span_trigger trigger,struct mlxsw_sp_port * mlxsw_sp_port,const struct mlxsw_sp_span_trigger_parms * parms)1575 void mlxsw_sp_span_agent_unbind(struct mlxsw_sp *mlxsw_sp,
1576 enum mlxsw_sp_span_trigger trigger,
1577 struct mlxsw_sp_port *mlxsw_sp_port,
1578 const struct mlxsw_sp_span_trigger_parms *parms)
1579 {
1580 struct mlxsw_sp_span_trigger_entry *trigger_entry;
1581
1582 ASSERT_RTNL();
1583
1584 if (WARN_ON_ONCE(!mlxsw_sp_span_entry_find_by_id(mlxsw_sp,
1585 parms->span_id)))
1586 return;
1587
1588 trigger_entry = mlxsw_sp_span_trigger_entry_find(mlxsw_sp->span,
1589 trigger,
1590 mlxsw_sp_port);
1591 if (WARN_ON_ONCE(!trigger_entry))
1592 return;
1593
1594 if (!refcount_dec_and_test(&trigger_entry->ref_count))
1595 return;
1596
1597 mlxsw_sp_span_trigger_entry_destroy(mlxsw_sp->span, trigger_entry);
1598 }
1599
mlxsw_sp_span_trigger_enable(struct mlxsw_sp_port * mlxsw_sp_port,enum mlxsw_sp_span_trigger trigger,u8 tc)1600 int mlxsw_sp_span_trigger_enable(struct mlxsw_sp_port *mlxsw_sp_port,
1601 enum mlxsw_sp_span_trigger trigger, u8 tc)
1602 {
1603 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1604 struct mlxsw_sp_span_trigger_entry *trigger_entry;
1605
1606 ASSERT_RTNL();
1607
1608 trigger_entry = mlxsw_sp_span_trigger_entry_find(mlxsw_sp->span,
1609 trigger,
1610 mlxsw_sp_port);
1611 if (WARN_ON_ONCE(!trigger_entry))
1612 return -EINVAL;
1613
1614 return trigger_entry->ops->enable(trigger_entry, mlxsw_sp_port, tc);
1615 }
1616
mlxsw_sp_span_trigger_disable(struct mlxsw_sp_port * mlxsw_sp_port,enum mlxsw_sp_span_trigger trigger,u8 tc)1617 void mlxsw_sp_span_trigger_disable(struct mlxsw_sp_port *mlxsw_sp_port,
1618 enum mlxsw_sp_span_trigger trigger, u8 tc)
1619 {
1620 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1621 struct mlxsw_sp_span_trigger_entry *trigger_entry;
1622
1623 ASSERT_RTNL();
1624
1625 trigger_entry = mlxsw_sp_span_trigger_entry_find(mlxsw_sp->span,
1626 trigger,
1627 mlxsw_sp_port);
1628 if (WARN_ON_ONCE(!trigger_entry))
1629 return;
1630
1631 return trigger_entry->ops->disable(trigger_entry, mlxsw_sp_port, tc);
1632 }
1633
mlxsw_sp1_span_init(struct mlxsw_sp * mlxsw_sp)1634 static int mlxsw_sp1_span_init(struct mlxsw_sp *mlxsw_sp)
1635 {
1636 size_t arr_size = ARRAY_SIZE(mlxsw_sp1_span_entry_ops_arr);
1637
1638 /* Must be first to avoid NULL pointer dereference by subsequent
1639 * can_handle() callbacks.
1640 */
1641 if (WARN_ON(mlxsw_sp1_span_entry_ops_arr[0] !=
1642 &mlxsw_sp1_span_entry_ops_cpu))
1643 return -EINVAL;
1644
1645 mlxsw_sp->span->span_trigger_ops_arr = mlxsw_sp1_span_trigger_ops_arr;
1646 mlxsw_sp->span->span_entry_ops_arr = mlxsw_sp1_span_entry_ops_arr;
1647 mlxsw_sp->span->span_entry_ops_arr_size = arr_size;
1648
1649 return 0;
1650 }
1651
mlxsw_sp1_span_policer_id_base_set(struct mlxsw_sp * mlxsw_sp,u16 policer_id_base)1652 static int mlxsw_sp1_span_policer_id_base_set(struct mlxsw_sp *mlxsw_sp,
1653 u16 policer_id_base)
1654 {
1655 return -EOPNOTSUPP;
1656 }
1657
1658 const struct mlxsw_sp_span_ops mlxsw_sp1_span_ops = {
1659 .init = mlxsw_sp1_span_init,
1660 .policer_id_base_set = mlxsw_sp1_span_policer_id_base_set,
1661 };
1662
mlxsw_sp2_span_init(struct mlxsw_sp * mlxsw_sp)1663 static int mlxsw_sp2_span_init(struct mlxsw_sp *mlxsw_sp)
1664 {
1665 size_t arr_size = ARRAY_SIZE(mlxsw_sp2_span_entry_ops_arr);
1666
1667 /* Must be first to avoid NULL pointer dereference by subsequent
1668 * can_handle() callbacks.
1669 */
1670 if (WARN_ON(mlxsw_sp2_span_entry_ops_arr[0] !=
1671 &mlxsw_sp2_span_entry_ops_cpu))
1672 return -EINVAL;
1673
1674 mlxsw_sp->span->span_trigger_ops_arr = mlxsw_sp2_span_trigger_ops_arr;
1675 mlxsw_sp->span->span_entry_ops_arr = mlxsw_sp2_span_entry_ops_arr;
1676 mlxsw_sp->span->span_entry_ops_arr_size = arr_size;
1677
1678 return 0;
1679 }
1680
1681 #define MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR 38
1682 #define MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR 50
1683
mlxsw_sp2_span_policer_id_base_set(struct mlxsw_sp * mlxsw_sp,u16 policer_id_base)1684 static int mlxsw_sp2_span_policer_id_base_set(struct mlxsw_sp *mlxsw_sp,
1685 u16 policer_id_base)
1686 {
1687 char mogcr_pl[MLXSW_REG_MOGCR_LEN];
1688 int err;
1689
1690 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mogcr), mogcr_pl);
1691 if (err)
1692 return err;
1693
1694 mlxsw_reg_mogcr_mirroring_pid_base_set(mogcr_pl, policer_id_base);
1695 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mogcr), mogcr_pl);
1696 }
1697
1698 const struct mlxsw_sp_span_ops mlxsw_sp2_span_ops = {
1699 .init = mlxsw_sp2_span_init,
1700 .policer_id_base_set = mlxsw_sp2_span_policer_id_base_set,
1701 };
1702
1703 const struct mlxsw_sp_span_ops mlxsw_sp3_span_ops = {
1704 .init = mlxsw_sp2_span_init,
1705 .policer_id_base_set = mlxsw_sp2_span_policer_id_base_set,
1706 };
1707