1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
2 /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
3 
4 #ifndef _MLXSW_SPECTRUM_SPAN_H
5 #define _MLXSW_SPECTRUM_SPAN_H
6 
7 #include <linux/types.h>
8 #include <linux/if_ether.h>
9 #include <linux/refcount.h>
10 
11 #include "spectrum_router.h"
12 
13 struct mlxsw_sp;
14 struct mlxsw_sp_port;
15 
16 /* SPAN session identifiers that correspond to MLXSW_TRAP_ID_MIRROR_SESSION<i>
17  * trap identifiers. The session identifier is an attribute of the SPAN agent,
18  * which determines the trap identifier of packets that are mirrored to the
19  * CPU. Packets that are trapped to the CPU for the same logical reason (e.g.,
20  * buffer drops) should use the same session identifier.
21  */
22 enum mlxsw_sp_span_session_id {
23 	MLXSW_SP_SPAN_SESSION_ID_BUFFER,
24 	MLXSW_SP_SPAN_SESSION_ID_SAMPLING,
25 
26 	__MLXSW_SP_SPAN_SESSION_ID_MAX = 8,
27 };
28 
29 struct mlxsw_sp_span_parms {
30 	struct mlxsw_sp_port *dest_port; /* NULL for unoffloaded SPAN. */
31 	unsigned int ttl;
32 	unsigned char dmac[ETH_ALEN];
33 	unsigned char smac[ETH_ALEN];
34 	union mlxsw_sp_l3addr daddr;
35 	union mlxsw_sp_l3addr saddr;
36 	u16 vid;
37 	u16 policer_id;
38 	bool policer_enable;
39 	enum mlxsw_sp_span_session_id session_id;
40 };
41 
42 enum mlxsw_sp_span_trigger {
43 	MLXSW_SP_SPAN_TRIGGER_INGRESS,
44 	MLXSW_SP_SPAN_TRIGGER_EGRESS,
45 	MLXSW_SP_SPAN_TRIGGER_TAIL_DROP,
46 	MLXSW_SP_SPAN_TRIGGER_EARLY_DROP,
47 	MLXSW_SP_SPAN_TRIGGER_ECN,
48 };
49 
50 struct mlxsw_sp_span_trigger_parms {
51 	int span_id;
52 	u32 probability_rate;
53 };
54 
55 struct mlxsw_sp_span_agent_parms {
56 	const struct net_device *to_dev;
57 	u16 policer_id;
58 	bool policer_enable;
59 	enum mlxsw_sp_span_session_id session_id;
60 };
61 
62 struct mlxsw_sp_span_entry_ops;
63 
64 struct mlxsw_sp_span_ops {
65 	int (*init)(struct mlxsw_sp *mlxsw_sp);
66 	int (*policer_id_base_set)(struct mlxsw_sp *mlxsw_sp,
67 				   u16 policer_id_base);
68 };
69 
70 struct mlxsw_sp_span_entry {
71 	const struct net_device *to_dev;
72 	const struct mlxsw_sp_span_entry_ops *ops;
73 	struct mlxsw_sp_span_parms parms;
74 	refcount_t ref_count;
75 	int id;
76 };
77 
78 struct mlxsw_sp_span_entry_ops {
79 	bool is_static;
80 	bool (*can_handle)(const struct net_device *to_dev);
81 	int (*parms_set)(struct mlxsw_sp *mlxsw_sp,
82 			 const struct net_device *to_dev,
83 			 struct mlxsw_sp_span_parms *sparmsp);
84 	int (*configure)(struct mlxsw_sp_span_entry *span_entry,
85 			 struct mlxsw_sp_span_parms sparms);
86 	void (*deconfigure)(struct mlxsw_sp_span_entry *span_entry);
87 };
88 
89 int mlxsw_sp_span_init(struct mlxsw_sp *mlxsw_sp);
90 void mlxsw_sp_span_fini(struct mlxsw_sp *mlxsw_sp);
91 void mlxsw_sp_span_respin(struct mlxsw_sp *mlxsw_sp);
92 
93 struct mlxsw_sp_span_entry *
94 mlxsw_sp_span_entry_find_by_port(struct mlxsw_sp *mlxsw_sp,
95 				 const struct net_device *to_dev);
96 
97 void mlxsw_sp_span_entry_invalidate(struct mlxsw_sp *mlxsw_sp,
98 				    struct mlxsw_sp_span_entry *span_entry);
99 
100 int mlxsw_sp_span_port_mtu_update(struct mlxsw_sp_port *port, u16 mtu);
101 void mlxsw_sp_span_speed_update_work(struct work_struct *work);
102 
103 int mlxsw_sp_span_agent_get(struct mlxsw_sp *mlxsw_sp, int *p_span_id,
104 			    const struct mlxsw_sp_span_agent_parms *parms);
105 void mlxsw_sp_span_agent_put(struct mlxsw_sp *mlxsw_sp, int span_id);
106 int mlxsw_sp_span_analyzed_port_get(struct mlxsw_sp_port *mlxsw_sp_port,
107 				    bool ingress);
108 void mlxsw_sp_span_analyzed_port_put(struct mlxsw_sp_port *mlxsw_sp_port,
109 				     bool ingress);
110 int mlxsw_sp_span_agent_bind(struct mlxsw_sp *mlxsw_sp,
111 			     enum mlxsw_sp_span_trigger trigger,
112 			     struct mlxsw_sp_port *mlxsw_sp_port,
113 			     const struct mlxsw_sp_span_trigger_parms *parms);
114 void
115 mlxsw_sp_span_agent_unbind(struct mlxsw_sp *mlxsw_sp,
116 			   enum mlxsw_sp_span_trigger trigger,
117 			   struct mlxsw_sp_port *mlxsw_sp_port,
118 			   const struct mlxsw_sp_span_trigger_parms *parms);
119 int mlxsw_sp_span_trigger_enable(struct mlxsw_sp_port *mlxsw_sp_port,
120 				 enum mlxsw_sp_span_trigger trigger, u8 tc);
121 void mlxsw_sp_span_trigger_disable(struct mlxsw_sp_port *mlxsw_sp_port,
122 				   enum mlxsw_sp_span_trigger trigger, u8 tc);
123 
124 extern const struct mlxsw_sp_span_ops mlxsw_sp1_span_ops;
125 extern const struct mlxsw_sp_span_ops mlxsw_sp2_span_ops;
126 extern const struct mlxsw_sp_span_ops mlxsw_sp3_span_ops;
127 
128 #endif
129