5252#include "spectrum.h"
5353#include "core.h"
5454#include "reg.h"
55+ #include "spectrum_cnt.h"
56+ #include "spectrum_dpipe.h"
57+ #include "spectrum_router.h"
5558
5659struct mlxsw_sp_rif {
5760 struct list_head nexthop_list ;
@@ -62,8 +65,157 @@ struct mlxsw_sp_rif {
6265 int mtu ;
6366 u16 rif_index ;
6467 u16 vr_id ;
68+ unsigned int counter_ingress ;
69+ bool counter_ingress_valid ;
70+ unsigned int counter_egress ;
71+ bool counter_egress_valid ;
6572};
6673
74+ static unsigned int *
75+ mlxsw_sp_rif_p_counter_get (struct mlxsw_sp_rif * rif ,
76+ enum mlxsw_sp_rif_counter_dir dir )
77+ {
78+ switch (dir ) {
79+ case MLXSW_SP_RIF_COUNTER_EGRESS :
80+ return & rif -> counter_egress ;
81+ case MLXSW_SP_RIF_COUNTER_INGRESS :
82+ return & rif -> counter_ingress ;
83+ }
84+ return NULL ;
85+ }
86+
87+ static bool
88+ mlxsw_sp_rif_counter_valid_get (struct mlxsw_sp_rif * rif ,
89+ enum mlxsw_sp_rif_counter_dir dir )
90+ {
91+ switch (dir ) {
92+ case MLXSW_SP_RIF_COUNTER_EGRESS :
93+ return rif -> counter_egress_valid ;
94+ case MLXSW_SP_RIF_COUNTER_INGRESS :
95+ return rif -> counter_ingress_valid ;
96+ }
97+ return false;
98+ }
99+
100+ static void
101+ mlxsw_sp_rif_counter_valid_set (struct mlxsw_sp_rif * rif ,
102+ enum mlxsw_sp_rif_counter_dir dir ,
103+ bool valid )
104+ {
105+ switch (dir ) {
106+ case MLXSW_SP_RIF_COUNTER_EGRESS :
107+ rif -> counter_egress_valid = valid ;
108+ break ;
109+ case MLXSW_SP_RIF_COUNTER_INGRESS :
110+ rif -> counter_ingress_valid = valid ;
111+ break ;
112+ }
113+ }
114+
115+ static int mlxsw_sp_rif_counter_edit (struct mlxsw_sp * mlxsw_sp , u16 rif_index ,
116+ unsigned int counter_index , bool enable ,
117+ enum mlxsw_sp_rif_counter_dir dir )
118+ {
119+ char ritr_pl [MLXSW_REG_RITR_LEN ];
120+ bool is_egress = false;
121+ int err ;
122+
123+ if (dir == MLXSW_SP_RIF_COUNTER_EGRESS )
124+ is_egress = true;
125+ mlxsw_reg_ritr_rif_pack (ritr_pl , rif_index );
126+ err = mlxsw_reg_query (mlxsw_sp -> core , MLXSW_REG (ritr ), ritr_pl );
127+ if (err )
128+ return err ;
129+
130+ mlxsw_reg_ritr_counter_pack (ritr_pl , counter_index , enable ,
131+ is_egress );
132+ return mlxsw_reg_write (mlxsw_sp -> core , MLXSW_REG (ritr ), ritr_pl );
133+ }
134+
135+ int mlxsw_sp_rif_counter_value_get (struct mlxsw_sp * mlxsw_sp ,
136+ struct mlxsw_sp_rif * rif ,
137+ enum mlxsw_sp_rif_counter_dir dir , u64 * cnt )
138+ {
139+ char ricnt_pl [MLXSW_REG_RICNT_LEN ];
140+ unsigned int * p_counter_index ;
141+ bool valid ;
142+ int err ;
143+
144+ valid = mlxsw_sp_rif_counter_valid_get (rif , dir );
145+ if (!valid )
146+ return - EINVAL ;
147+
148+ p_counter_index = mlxsw_sp_rif_p_counter_get (rif , dir );
149+ if (!p_counter_index )
150+ return - EINVAL ;
151+ mlxsw_reg_ricnt_pack (ricnt_pl , * p_counter_index ,
152+ MLXSW_REG_RICNT_OPCODE_NOP );
153+ err = mlxsw_reg_query (mlxsw_sp -> core , MLXSW_REG (ricnt ), ricnt_pl );
154+ if (err )
155+ return err ;
156+ * cnt = mlxsw_reg_ricnt_good_unicast_packets_get (ricnt_pl );
157+ return 0 ;
158+ }
159+
160+ static int mlxsw_sp_rif_counter_clear (struct mlxsw_sp * mlxsw_sp ,
161+ unsigned int counter_index )
162+ {
163+ char ricnt_pl [MLXSW_REG_RICNT_LEN ];
164+
165+ mlxsw_reg_ricnt_pack (ricnt_pl , counter_index ,
166+ MLXSW_REG_RICNT_OPCODE_CLEAR );
167+ return mlxsw_reg_write (mlxsw_sp -> core , MLXSW_REG (ricnt ), ricnt_pl );
168+ }
169+
170+ int mlxsw_sp_rif_counter_alloc (struct mlxsw_sp * mlxsw_sp ,
171+ struct mlxsw_sp_rif * rif ,
172+ enum mlxsw_sp_rif_counter_dir dir )
173+ {
174+ unsigned int * p_counter_index ;
175+ int err ;
176+
177+ p_counter_index = mlxsw_sp_rif_p_counter_get (rif , dir );
178+ if (!p_counter_index )
179+ return - EINVAL ;
180+ err = mlxsw_sp_counter_alloc (mlxsw_sp , MLXSW_SP_COUNTER_SUB_POOL_RIF ,
181+ p_counter_index );
182+ if (err )
183+ return err ;
184+
185+ err = mlxsw_sp_rif_counter_clear (mlxsw_sp , * p_counter_index );
186+ if (err )
187+ goto err_counter_clear ;
188+
189+ err = mlxsw_sp_rif_counter_edit (mlxsw_sp , rif -> rif_index ,
190+ * p_counter_index , true, dir );
191+ if (err )
192+ goto err_counter_edit ;
193+ mlxsw_sp_rif_counter_valid_set (rif , dir , true);
194+ return 0 ;
195+
196+ err_counter_edit :
197+ err_counter_clear :
198+ mlxsw_sp_counter_free (mlxsw_sp , MLXSW_SP_COUNTER_SUB_POOL_RIF ,
199+ * p_counter_index );
200+ return err ;
201+ }
202+
203+ void mlxsw_sp_rif_counter_free (struct mlxsw_sp * mlxsw_sp ,
204+ struct mlxsw_sp_rif * rif ,
205+ enum mlxsw_sp_rif_counter_dir dir )
206+ {
207+ unsigned int * p_counter_index ;
208+
209+ p_counter_index = mlxsw_sp_rif_p_counter_get (rif , dir );
210+ if (WARN_ON (!p_counter_index ))
211+ return ;
212+ mlxsw_sp_rif_counter_edit (mlxsw_sp , rif -> rif_index ,
213+ * p_counter_index , false, dir );
214+ mlxsw_sp_counter_free (mlxsw_sp , MLXSW_SP_COUNTER_SUB_POOL_RIF ,
215+ * p_counter_index );
216+ mlxsw_sp_rif_counter_valid_set (rif , dir , false);
217+ }
218+
67219static struct mlxsw_sp_rif *
68220mlxsw_sp_rif_find_by_dev (const struct mlxsw_sp * mlxsw_sp ,
69221 const struct net_device * dev );
@@ -2822,6 +2974,15 @@ mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port *mlxsw_sp_vport,
28222974 goto err_rif_alloc ;
28232975 }
28242976
2977+ if (devlink_dpipe_table_counter_enabled (priv_to_devlink (mlxsw_sp -> core ),
2978+ MLXSW_SP_DPIPE_TABLE_NAME_ERIF )) {
2979+ err = mlxsw_sp_rif_counter_alloc (mlxsw_sp , rif ,
2980+ MLXSW_SP_RIF_COUNTER_EGRESS );
2981+ if (err )
2982+ netdev_dbg (mlxsw_sp_vport -> dev ,
2983+ "Counter alloc Failed err=%d\n" , err );
2984+ }
2985+
28252986 f -> rif = rif ;
28262987 mlxsw_sp -> rifs [rif_index ] = rif ;
28272988 vr -> rif_count ++ ;
@@ -2852,6 +3013,9 @@ static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port *mlxsw_sp_vport,
28523013
28533014 mlxsw_sp_router_rif_gone_sync (mlxsw_sp , rif );
28543015
3016+ mlxsw_sp_rif_counter_free (mlxsw_sp , rif , MLXSW_SP_RIF_COUNTER_EGRESS );
3017+ mlxsw_sp_rif_counter_free (mlxsw_sp , rif , MLXSW_SP_RIF_COUNTER_INGRESS );
3018+
28553019 vr -> rif_count -- ;
28563020 mlxsw_sp -> rifs [rif_index ] = NULL ;
28573021 f -> rif = NULL ;
0 commit comments