xref: /linux/drivers/net/ethernet/microsoft/mana/mana_ethtool.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2021, Microsoft Corporation. */
3 
4 #include <linux/inetdevice.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ethtool.h>
7 
8 #include <net/mana/mana.h>
9 
10 struct mana_stats_desc {
11 	char name[ETH_GSTRING_LEN];
12 	u16 offset;
13 };
14 
15 static const struct mana_stats_desc mana_eth_stats[] = {
16 	{"stop_queue", offsetof(struct mana_ethtool_stats, stop_queue)},
17 	{"wake_queue", offsetof(struct mana_ethtool_stats, wake_queue)},
18 	{"tx_cq_err", offsetof(struct mana_ethtool_stats, tx_cqe_err)},
19 	{"tx_cqe_unknown_type", offsetof(struct mana_ethtool_stats,
20 					tx_cqe_unknown_type)},
21 	{"tx_linear_pkt_cnt", offsetof(struct mana_ethtool_stats,
22 				       tx_linear_pkt_cnt)},
23 	{"rx_cqe_unknown_type", offsetof(struct mana_ethtool_stats,
24 					rx_cqe_unknown_type)},
25 };
26 
27 static const struct mana_stats_desc mana_hc_stats[] = {
28 	{"hc_rx_discards_no_wqe", offsetof(struct mana_ethtool_hc_stats,
29 					   hc_rx_discards_no_wqe)},
30 	{"hc_rx_err_vport_disabled", offsetof(struct mana_ethtool_hc_stats,
31 					      hc_rx_err_vport_disabled)},
32 	{"hc_rx_bytes", offsetof(struct mana_ethtool_hc_stats, hc_rx_bytes)},
33 	{"hc_rx_ucast_pkts", offsetof(struct mana_ethtool_hc_stats,
34 				      hc_rx_ucast_pkts)},
35 	{"hc_rx_ucast_bytes", offsetof(struct mana_ethtool_hc_stats,
36 				       hc_rx_ucast_bytes)},
37 	{"hc_rx_bcast_pkts", offsetof(struct mana_ethtool_hc_stats,
38 				      hc_rx_bcast_pkts)},
39 	{"hc_rx_bcast_bytes", offsetof(struct mana_ethtool_hc_stats,
40 				       hc_rx_bcast_bytes)},
41 	{"hc_rx_mcast_pkts", offsetof(struct mana_ethtool_hc_stats,
42 				      hc_rx_mcast_pkts)},
43 	{"hc_rx_mcast_bytes", offsetof(struct mana_ethtool_hc_stats,
44 				       hc_rx_mcast_bytes)},
45 	{"hc_tx_err_gf_disabled", offsetof(struct mana_ethtool_hc_stats,
46 					   hc_tx_err_gf_disabled)},
47 	{"hc_tx_err_vport_disabled", offsetof(struct mana_ethtool_hc_stats,
48 					      hc_tx_err_vport_disabled)},
49 	{"hc_tx_err_inval_vportoffset_pkt",
50 	 offsetof(struct mana_ethtool_hc_stats,
51 		  hc_tx_err_inval_vportoffset_pkt)},
52 	{"hc_tx_err_vlan_enforcement", offsetof(struct mana_ethtool_hc_stats,
53 						hc_tx_err_vlan_enforcement)},
54 	{"hc_tx_err_eth_type_enforcement",
55 	 offsetof(struct mana_ethtool_hc_stats, hc_tx_err_eth_type_enforcement)},
56 	{"hc_tx_err_sa_enforcement", offsetof(struct mana_ethtool_hc_stats,
57 					      hc_tx_err_sa_enforcement)},
58 	{"hc_tx_err_sqpdid_enforcement",
59 	 offsetof(struct mana_ethtool_hc_stats, hc_tx_err_sqpdid_enforcement)},
60 	{"hc_tx_err_cqpdid_enforcement",
61 	 offsetof(struct mana_ethtool_hc_stats, hc_tx_err_cqpdid_enforcement)},
62 	{"hc_tx_err_mtu_violation", offsetof(struct mana_ethtool_hc_stats,
63 					     hc_tx_err_mtu_violation)},
64 	{"hc_tx_err_inval_oob", offsetof(struct mana_ethtool_hc_stats,
65 					 hc_tx_err_inval_oob)},
66 	{"hc_tx_err_gdma", offsetof(struct mana_ethtool_hc_stats,
67 				    hc_tx_err_gdma)},
68 	{"hc_tx_bytes", offsetof(struct mana_ethtool_hc_stats, hc_tx_bytes)},
69 	{"hc_tx_ucast_pkts", offsetof(struct mana_ethtool_hc_stats,
70 					hc_tx_ucast_pkts)},
71 	{"hc_tx_ucast_bytes", offsetof(struct mana_ethtool_hc_stats,
72 					hc_tx_ucast_bytes)},
73 	{"hc_tx_bcast_pkts", offsetof(struct mana_ethtool_hc_stats,
74 					hc_tx_bcast_pkts)},
75 	{"hc_tx_bcast_bytes", offsetof(struct mana_ethtool_hc_stats,
76 					hc_tx_bcast_bytes)},
77 	{"hc_tx_mcast_pkts", offsetof(struct mana_ethtool_hc_stats,
78 					hc_tx_mcast_pkts)},
79 	{"hc_tx_mcast_bytes", offsetof(struct mana_ethtool_hc_stats,
80 					hc_tx_mcast_bytes)},
81 };
82 
83 static const struct mana_stats_desc mana_phy_stats[] = {
84 	{ "hc_rx_pkt_drop_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_drop_phy) },
85 	{ "hc_tx_pkt_drop_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_drop_phy) },
86 	{ "hc_tc0_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc0_phy) },
87 	{ "hc_tc0_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc0_phy) },
88 	{ "hc_tc0_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc0_phy) },
89 	{ "hc_tc0_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc0_phy) },
90 	{ "hc_tc1_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc1_phy) },
91 	{ "hc_tc1_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc1_phy) },
92 	{ "hc_tc1_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc1_phy) },
93 	{ "hc_tc1_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc1_phy) },
94 	{ "hc_tc2_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc2_phy) },
95 	{ "hc_tc2_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc2_phy) },
96 	{ "hc_tc2_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc2_phy) },
97 	{ "hc_tc2_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc2_phy) },
98 	{ "hc_tc3_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc3_phy) },
99 	{ "hc_tc3_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc3_phy) },
100 	{ "hc_tc3_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc3_phy) },
101 	{ "hc_tc3_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc3_phy) },
102 	{ "hc_tc4_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc4_phy) },
103 	{ "hc_tc4_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc4_phy) },
104 	{ "hc_tc4_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc4_phy) },
105 	{ "hc_tc4_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc4_phy) },
106 	{ "hc_tc5_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc5_phy) },
107 	{ "hc_tc5_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc5_phy) },
108 	{ "hc_tc5_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc5_phy) },
109 	{ "hc_tc5_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc5_phy) },
110 	{ "hc_tc6_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc6_phy) },
111 	{ "hc_tc6_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc6_phy) },
112 	{ "hc_tc6_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc6_phy) },
113 	{ "hc_tc6_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc6_phy) },
114 	{ "hc_tc7_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc7_phy) },
115 	{ "hc_tc7_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc7_phy) },
116 	{ "hc_tc7_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc7_phy) },
117 	{ "hc_tc7_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc7_phy) },
118 	{ "hc_tc0_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc0_phy) },
119 	{ "hc_tc0_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc0_phy) },
120 	{ "hc_tc1_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc1_phy) },
121 	{ "hc_tc1_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc1_phy) },
122 	{ "hc_tc2_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc2_phy) },
123 	{ "hc_tc2_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc2_phy) },
124 	{ "hc_tc3_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc3_phy) },
125 	{ "hc_tc3_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc3_phy) },
126 	{ "hc_tc4_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc4_phy) },
127 	{ "hc_tc4_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc4_phy) },
128 	{ "hc_tc5_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc5_phy) },
129 	{ "hc_tc5_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc5_phy) },
130 	{ "hc_tc6_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc6_phy) },
131 	{ "hc_tc6_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc6_phy) },
132 	{ "hc_tc7_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc7_phy) },
133 	{ "hc_tc7_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc7_phy) },
134 };
135 
136 static const char mana_priv_flags[MANA_PRIV_FLAG_MAX][ETH_GSTRING_LEN] = {
137 	[MANA_PRIV_FLAG_USE_FULL_PAGE_RXBUF] = "full-page-rx"
138 };
139 
140 static int mana_get_sset_count(struct net_device *ndev, int stringset)
141 {
142 	struct mana_port_context *apc = netdev_priv(ndev);
143 	unsigned int num_queues = apc->num_queues;
144 
145 	switch (stringset) {
146 	case ETH_SS_STATS:
147 		return ARRAY_SIZE(mana_eth_stats) +
148 		       ARRAY_SIZE(mana_phy_stats) +
149 		       ARRAY_SIZE(mana_hc_stats)  +
150 		       num_queues * (MANA_STATS_RX_COUNT + MANA_STATS_TX_COUNT +
151 				       MANA_STATS_EQ_COUNT);
152 
153 	case ETH_SS_PRIV_FLAGS:
154 		return MANA_PRIV_FLAG_MAX;
155 
156 	default:
157 		return -EINVAL;
158 	}
159 }
160 
161 static void mana_get_strings_stats(struct mana_port_context *apc, u8 **data)
162 {
163 	unsigned int num_queues = apc->num_queues;
164 	int i, j;
165 
166 	for (i = 0; i < ARRAY_SIZE(mana_eth_stats); i++)
167 		ethtool_puts(data, mana_eth_stats[i].name);
168 
169 	for (i = 0; i < ARRAY_SIZE(mana_hc_stats); i++)
170 		ethtool_puts(data, mana_hc_stats[i].name);
171 
172 	for (i = 0; i < ARRAY_SIZE(mana_phy_stats); i++)
173 		ethtool_puts(data, mana_phy_stats[i].name);
174 
175 	for (i = 0; i < num_queues; i++) {
176 		ethtool_sprintf(data, "rx_%d_packets", i);
177 		ethtool_sprintf(data, "rx_%d_bytes", i);
178 		ethtool_sprintf(data, "rx_%d_xdp_drop", i);
179 		ethtool_sprintf(data, "rx_%d_xdp_tx", i);
180 		ethtool_sprintf(data, "rx_%d_xdp_redirect", i);
181 		ethtool_sprintf(data, "rx_%d_pkt_len0_err", i);
182 		for (j = 0; j < MANA_CQE_COAL_PKTS_8 - 1; j++)
183 			ethtool_sprintf(data,
184 					"rx_%d_coalesced_cqe_%d",
185 					i,
186 					j + 2);
187 	}
188 
189 	for (i = 0; i < num_queues; i++) {
190 		ethtool_sprintf(data, "tx_%d_packets", i);
191 		ethtool_sprintf(data, "tx_%d_bytes", i);
192 		ethtool_sprintf(data, "tx_%d_xdp_xmit", i);
193 		ethtool_sprintf(data, "tx_%d_tso_packets", i);
194 		ethtool_sprintf(data, "tx_%d_tso_bytes", i);
195 		ethtool_sprintf(data, "tx_%d_tso_inner_packets", i);
196 		ethtool_sprintf(data, "tx_%d_tso_inner_bytes", i);
197 		ethtool_sprintf(data, "tx_%d_long_pkt_fmt", i);
198 		ethtool_sprintf(data, "tx_%d_short_pkt_fmt", i);
199 		ethtool_sprintf(data, "tx_%d_csum_partial", i);
200 		ethtool_sprintf(data, "tx_%d_mana_map_err", i);
201 	}
202 
203 	for (i = 0; i < num_queues; i++)
204 		ethtool_sprintf(data, "eq_%d_throttle_events", i);
205 }
206 
207 static void mana_get_strings_priv_flags(u8 **data)
208 {
209 	int i;
210 
211 	for (i = 0; i < MANA_PRIV_FLAG_MAX; i++)
212 		ethtool_puts(data, mana_priv_flags[i]);
213 }
214 
215 static void mana_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
216 {
217 	struct mana_port_context *apc = netdev_priv(ndev);
218 
219 	switch (stringset) {
220 	case ETH_SS_STATS:
221 		mana_get_strings_stats(apc, &data);
222 		break;
223 	case ETH_SS_PRIV_FLAGS:
224 		mana_get_strings_priv_flags(&data);
225 		break;
226 	default:
227 		break;
228 	}
229 }
230 
231 static void mana_get_ethtool_stats(struct net_device *ndev,
232 				   struct ethtool_stats *e_stats, u64 *data)
233 {
234 	struct mana_port_context *apc = netdev_priv(ndev);
235 	unsigned int num_queues = apc->num_queues;
236 	void *eth_stats = &apc->eth_stats;
237 	void *hc_stats = &apc->ac->hc_stats;
238 	void *phy_stats = &apc->phy_stats;
239 	struct mana_stats_rx *rx_stats;
240 	struct mana_stats_tx *tx_stats;
241 	struct gdma_eq_stats *eq_stats;
242 	unsigned int start;
243 	u64 packets, bytes;
244 	u64 xdp_redirect;
245 	u64 xdp_xmit;
246 	u64 xdp_drop;
247 	u64 xdp_tx;
248 	u64 pkt_len0_err;
249 	u64 coalesced_cqe[MANA_CQE_COAL_PKTS_8 - 1];
250 	u64 tso_packets;
251 	u64 tso_bytes;
252 	u64 tso_inner_packets;
253 	u64 tso_inner_bytes;
254 	u64 long_pkt_fmt;
255 	u64 short_pkt_fmt;
256 	u64 csum_partial;
257 	u64 mana_map_err;
258 	int q, i = 0, j;
259 	u64 throttled;
260 
261 	if (!apc->port_is_up)
262 		return;
263 
264 	/* We call this mana function to get the phy stats from GDMA and includes
265 	 * aggregate tx/rx drop counters, Per-TC(Traffic Channel) tx/rx and pause
266 	 * counters.
267 	 */
268 	mana_query_phy_stats(apc);
269 
270 	for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++)
271 		data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset);
272 
273 	for (q = 0; q < ARRAY_SIZE(mana_hc_stats); q++)
274 		data[i++] = *(u64 *)(hc_stats + mana_hc_stats[q].offset);
275 
276 	for (q = 0; q < ARRAY_SIZE(mana_phy_stats); q++)
277 		data[i++] = *(u64 *)(phy_stats + mana_phy_stats[q].offset);
278 
279 	for (q = 0; q < num_queues; q++) {
280 		rx_stats = &apc->rxqs[q]->stats;
281 
282 		do {
283 			start = u64_stats_fetch_begin(&rx_stats->syncp);
284 			packets = rx_stats->packets;
285 			bytes = rx_stats->bytes;
286 			xdp_drop = rx_stats->xdp_drop;
287 			xdp_tx = rx_stats->xdp_tx;
288 			xdp_redirect = rx_stats->xdp_redirect;
289 			pkt_len0_err = rx_stats->pkt_len0_err;
290 			for (j = 0; j < MANA_CQE_COAL_PKTS_8 - 1; j++)
291 				coalesced_cqe[j] = rx_stats->coalesced_cqe[j];
292 		} while (u64_stats_fetch_retry(&rx_stats->syncp, start));
293 
294 		data[i++] = packets;
295 		data[i++] = bytes;
296 		data[i++] = xdp_drop;
297 		data[i++] = xdp_tx;
298 		data[i++] = xdp_redirect;
299 		data[i++] = pkt_len0_err;
300 		for (j = 0; j < MANA_CQE_COAL_PKTS_8 - 1; j++)
301 			data[i++] = coalesced_cqe[j];
302 	}
303 
304 	for (q = 0; q < num_queues; q++) {
305 		tx_stats = &apc->tx_qp[q]->txq.stats;
306 
307 		do {
308 			start = u64_stats_fetch_begin(&tx_stats->syncp);
309 			packets = tx_stats->packets;
310 			bytes = tx_stats->bytes;
311 			xdp_xmit = tx_stats->xdp_xmit;
312 			tso_packets = tx_stats->tso_packets;
313 			tso_bytes = tx_stats->tso_bytes;
314 			tso_inner_packets = tx_stats->tso_inner_packets;
315 			tso_inner_bytes = tx_stats->tso_inner_bytes;
316 			long_pkt_fmt = tx_stats->long_pkt_fmt;
317 			short_pkt_fmt = tx_stats->short_pkt_fmt;
318 			csum_partial = tx_stats->csum_partial;
319 			mana_map_err = tx_stats->mana_map_err;
320 		} while (u64_stats_fetch_retry(&tx_stats->syncp, start));
321 
322 		data[i++] = packets;
323 		data[i++] = bytes;
324 		data[i++] = xdp_xmit;
325 		data[i++] = tso_packets;
326 		data[i++] = tso_bytes;
327 		data[i++] = tso_inner_packets;
328 		data[i++] = tso_inner_bytes;
329 		data[i++] = long_pkt_fmt;
330 		data[i++] = short_pkt_fmt;
331 		data[i++] = csum_partial;
332 		data[i++] = mana_map_err;
333 	}
334 
335 	for (q = 0; q < num_queues; q++) {
336 		eq_stats = &apc->eqs[q].eq->eq.stats;
337 
338 		do {
339 			start = u64_stats_fetch_begin(&eq_stats->syncp);
340 			throttled = u64_stats_read(&eq_stats->throttle_count);
341 		} while (u64_stats_fetch_retry(&eq_stats->syncp, start));
342 
343 		data[i++] = throttled;
344 	}
345 }
346 
347 static u32 mana_get_rx_ring_count(struct net_device *ndev)
348 {
349 	struct mana_port_context *apc = netdev_priv(ndev);
350 
351 	return apc->num_queues;
352 }
353 
354 static u32 mana_get_rxfh_key_size(struct net_device *ndev)
355 {
356 	return MANA_HASH_KEY_SIZE;
357 }
358 
359 static u32 mana_rss_indir_size(struct net_device *ndev)
360 {
361 	struct mana_port_context *apc = netdev_priv(ndev);
362 
363 	return apc->indir_table_sz;
364 }
365 
366 static int mana_get_rxfh(struct net_device *ndev,
367 			 struct ethtool_rxfh_param *rxfh)
368 {
369 	struct mana_port_context *apc = netdev_priv(ndev);
370 	int i;
371 
372 	rxfh->hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
373 
374 	if (rxfh->indir) {
375 		for (i = 0; i < apc->indir_table_sz; i++)
376 			rxfh->indir[i] = apc->indir_table[i];
377 	}
378 
379 	if (rxfh->key)
380 		memcpy(rxfh->key, apc->hashkey, MANA_HASH_KEY_SIZE);
381 
382 	return 0;
383 }
384 
385 static int mana_set_rxfh(struct net_device *ndev,
386 			 struct ethtool_rxfh_param *rxfh,
387 			 struct netlink_ext_ack *extack)
388 {
389 	struct mana_port_context *apc = netdev_priv(ndev);
390 	bool update_hash = false, update_table = false;
391 	u8 save_key[MANA_HASH_KEY_SIZE];
392 	u32 *save_table;
393 	int i, err;
394 
395 	if (!apc->port_is_up)
396 		return -EOPNOTSUPP;
397 
398 	if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
399 	    rxfh->hfunc != ETH_RSS_HASH_TOP)
400 		return -EOPNOTSUPP;
401 
402 	save_table = kzalloc_objs(u32, apc->indir_table_sz);
403 	if (!save_table)
404 		return -ENOMEM;
405 
406 	if (rxfh->indir) {
407 		for (i = 0; i < apc->indir_table_sz; i++)
408 			if (rxfh->indir[i] >= apc->num_queues) {
409 				err = -EINVAL;
410 				goto cleanup;
411 			}
412 
413 		update_table = true;
414 		for (i = 0; i < apc->indir_table_sz; i++) {
415 			save_table[i] = apc->indir_table[i];
416 			apc->indir_table[i] = rxfh->indir[i];
417 		}
418 	}
419 
420 	if (rxfh->key) {
421 		update_hash = true;
422 		memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE);
423 		memcpy(apc->hashkey, rxfh->key, MANA_HASH_KEY_SIZE);
424 	}
425 
426 	err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
427 
428 	if (err) { /* recover to original values */
429 		if (update_table) {
430 			for (i = 0; i < apc->indir_table_sz; i++)
431 				apc->indir_table[i] = save_table[i];
432 		}
433 
434 		if (update_hash)
435 			memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE);
436 
437 		mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
438 	}
439 
440 cleanup:
441 	kfree(save_table);
442 
443 	return err;
444 }
445 
446 static void mana_get_channels(struct net_device *ndev,
447 			      struct ethtool_channels *channel)
448 {
449 	struct mana_port_context *apc = netdev_priv(ndev);
450 
451 	channel->max_combined = apc->max_queues;
452 	channel->combined_count = apc->num_queues;
453 }
454 
455 #define MANA_RX_CQE_NSEC_DEF 2048
456 static int mana_get_coalesce(struct net_device *ndev,
457 			     struct ethtool_coalesce *ec,
458 			     struct kernel_ethtool_coalesce *kernel_coal,
459 			     struct netlink_ext_ack *extack)
460 {
461 	struct mana_port_context *apc = netdev_priv(ndev);
462 
463 	kernel_coal->rx_cqe_frames =
464 		apc->cqe8_coalescing_enable ? MANA_CQE_COAL_PKTS_8 :
465 		apc->cqe_coalescing_enable ? MANA_RXCOMP_OOB_NUM_PPI : 1;
466 
467 	kernel_coal->rx_cqe_nsecs = apc->cqe_coalescing_timeout_ns;
468 
469 	/* Return the default timeout value for old FW not providing
470 	 * this value.
471 	 */
472 	if (apc->port_is_up && apc->cqe_coalescing_enable &&
473 	    !kernel_coal->rx_cqe_nsecs)
474 		kernel_coal->rx_cqe_nsecs = MANA_RX_CQE_NSEC_DEF;
475 
476 	ec->rx_coalesce_usecs = apc->intr_modr_rx_usec;
477 	ec->rx_max_coalesced_frames = apc->intr_modr_rx_comp;
478 
479 	ec->tx_coalesce_usecs = apc->intr_modr_tx_usec;
480 	ec->tx_max_coalesced_frames = apc->intr_modr_tx_comp;
481 
482 	ec->use_adaptive_rx_coalesce = apc->rx_dim_enabled;
483 	ec->use_adaptive_tx_coalesce = apc->tx_dim_enabled;
484 
485 	return 0;
486 }
487 
488 static int mana_set_coalesce(struct net_device *ndev,
489 			     struct ethtool_coalesce *ec,
490 			     struct kernel_ethtool_coalesce *kernel_coal,
491 			     struct netlink_ext_ack *extack)
492 {
493 	struct mana_port_context *apc = netdev_priv(ndev);
494 	struct {
495 		u16 intr_modr_rx_usec;
496 		u16 intr_modr_rx_comp;
497 		u16 intr_modr_tx_usec;
498 		u16 intr_modr_tx_comp;
499 		u8 cqe_coalescing_enable;
500 		u8 cqe8_coalescing_enable;
501 		bool rx_dim_enabled;
502 		bool tx_dim_enabled;
503 	} saved;
504 	bool modr_changed = false;
505 	bool dim_changed = false;
506 	struct gdma_context *gc;
507 	u32 max_cqe_frames;
508 	int err;
509 
510 	gc = apc->ac->gdma_dev->gdma_context;
511 	max_cqe_frames = gc->cqe8_coalescing_sup ? MANA_CQE_COAL_PKTS_8 :
512 						   MANA_RXCOMP_OOB_NUM_PPI;
513 
514 	/* Both static and dynamic interrupt moderation (DIM) rely on the
515 	 * same HW capability advertised by the PF.
516 	 */
517 	if ((ec->use_adaptive_rx_coalesce || ec->use_adaptive_tx_coalesce ||
518 	     ec->rx_coalesce_usecs || ec->tx_coalesce_usecs ||
519 	     ec->rx_max_coalesced_frames || ec->tx_max_coalesced_frames) &&
520 	    !(gc->pf_cap_flags1 & GDMA_PF_CAP_FLAG_1_DYN_INTERRUPT_MODERATION)) {
521 		NL_SET_ERR_MSG(extack,
522 			       "Interrupt Moderation is not supported by HW");
523 		return -EOPNOTSUPP;
524 	}
525 
526 	if (kernel_coal->rx_cqe_frames != 1 &&
527 	    kernel_coal->rx_cqe_frames != MANA_RXCOMP_OOB_NUM_PPI &&
528 	    kernel_coal->rx_cqe_frames != max_cqe_frames) {
529 		NL_SET_ERR_MSG_FMT(extack,
530 				   "rx-frames must be 1 or %u%s, got %u",
531 				   MANA_RXCOMP_OOB_NUM_PPI,
532 				   gc->cqe8_coalescing_sup ? " or 8" : "",
533 				   kernel_coal->rx_cqe_frames);
534 		return -EINVAL;
535 	}
536 
537 	if (ec->rx_coalesce_usecs > MANA_INTR_MODR_USEC_MAX ||
538 	    ec->tx_coalesce_usecs > MANA_INTR_MODR_USEC_MAX) {
539 		NL_SET_ERR_MSG_FMT(extack,
540 				   "coalesce usecs must be <= %lu",
541 				   MANA_INTR_MODR_USEC_MAX);
542 		return -EINVAL;
543 	}
544 
545 	if (ec->rx_max_coalesced_frames > MANA_INTR_MODR_COMP_MAX ||
546 	    ec->tx_max_coalesced_frames > MANA_INTR_MODR_COMP_MAX) {
547 		NL_SET_ERR_MSG_FMT(extack,
548 				   "coalesce frames must be <= %lu",
549 				   MANA_INTR_MODR_COMP_MAX);
550 		return -EINVAL;
551 	}
552 
553 	if (ec->rx_coalesce_usecs != apc->intr_modr_rx_usec ||
554 	    ec->rx_max_coalesced_frames != apc->intr_modr_rx_comp ||
555 	    ec->tx_coalesce_usecs != apc->intr_modr_tx_usec ||
556 	    ec->tx_max_coalesced_frames != apc->intr_modr_tx_comp)
557 		modr_changed = true;
558 
559 	saved.intr_modr_rx_usec = apc->intr_modr_rx_usec;
560 	saved.intr_modr_rx_comp = apc->intr_modr_rx_comp;
561 	saved.intr_modr_tx_usec = apc->intr_modr_tx_usec;
562 	saved.intr_modr_tx_comp = apc->intr_modr_tx_comp;
563 
564 	apc->intr_modr_rx_usec = ec->rx_coalesce_usecs;
565 	apc->intr_modr_rx_comp = ec->rx_max_coalesced_frames;
566 	apc->intr_modr_tx_usec = ec->tx_coalesce_usecs;
567 	apc->intr_modr_tx_comp = ec->tx_max_coalesced_frames;
568 
569 	if (!!ec->use_adaptive_rx_coalesce != apc->rx_dim_enabled ||
570 	    !!ec->use_adaptive_tx_coalesce != apc->tx_dim_enabled)
571 		dim_changed = true;
572 
573 	saved.rx_dim_enabled = apc->rx_dim_enabled;
574 	saved.tx_dim_enabled = apc->tx_dim_enabled;
575 
576 	saved.cqe_coalescing_enable = apc->cqe_coalescing_enable;
577 	saved.cqe8_coalescing_enable = apc->cqe8_coalescing_enable;
578 	apc->cqe_coalescing_enable =
579 		kernel_coal->rx_cqe_frames >= MANA_RXCOMP_OOB_NUM_PPI;
580 	apc->cqe8_coalescing_enable =
581 		kernel_coal->rx_cqe_frames == MANA_CQE_COAL_PKTS_8;
582 
583 	if (!apc->port_is_up) {
584 		WRITE_ONCE(apc->rx_dim_enabled, !!ec->use_adaptive_rx_coalesce);
585 		WRITE_ONCE(apc->tx_dim_enabled, !!ec->use_adaptive_tx_coalesce);
586 		return 0;
587 	}
588 
589 	if (apc->cqe_coalescing_enable != saved.cqe_coalescing_enable ||
590 	    apc->cqe8_coalescing_enable != saved.cqe8_coalescing_enable) {
591 		/* CQE coalescing setting is applied via RSS configuration. */
592 		err = mana_config_rss(apc, TRI_STATE_TRUE, false, false);
593 		if (err) {
594 			netdev_err(ndev, "Change CQE coalescing failed: %d\n",
595 				   err);
596 			apc->cqe_coalescing_enable =
597 				saved.cqe_coalescing_enable;
598 			apc->cqe8_coalescing_enable =
599 				saved.cqe8_coalescing_enable;
600 			apc->intr_modr_rx_usec = saved.intr_modr_rx_usec;
601 			apc->intr_modr_rx_comp = saved.intr_modr_rx_comp;
602 			apc->intr_modr_tx_usec = saved.intr_modr_tx_usec;
603 			apc->intr_modr_tx_comp = saved.intr_modr_tx_comp;
604 			return err;
605 		}
606 	}
607 
608 	if (modr_changed || dim_changed) {
609 		bool new_rx_dim = !!ec->use_adaptive_rx_coalesce;
610 		bool new_tx_dim = !!ec->use_adaptive_tx_coalesce;
611 		bool disable_rx_dim = saved.rx_dim_enabled && !new_rx_dim;
612 		bool disable_tx_dim = saved.tx_dim_enabled && !new_tx_dim;
613 		bool enable_rx_dim = !saved.rx_dim_enabled && new_rx_dim;
614 		bool enable_tx_dim = !saved.tx_dim_enabled && new_tx_dim;
615 		int q;
616 
617 		/* On disable: clear the per-port flag first and
618 		 * synchronize_net() so any in-flight NAPI poll observes
619 		 * the new value and will not schedule further DIM work;
620 		 * then drain pending work and restore the static
621 		 * moderation values.
622 		 */
623 		if (disable_rx_dim)
624 			WRITE_ONCE(apc->rx_dim_enabled, false);
625 		if (disable_tx_dim)
626 			WRITE_ONCE(apc->tx_dim_enabled, false);
627 		if (disable_rx_dim || disable_tx_dim)
628 			synchronize_net();
629 
630 		for (q = 0; q < apc->num_queues; q++) {
631 			struct mana_cq *rx_cq = &apc->rxqs[q]->rx_cq;
632 			struct mana_cq *tx_cq = &apc->tx_qp[q]->tx_cq;
633 
634 			if (disable_rx_dim)
635 				mana_dim_change(rx_cq, false);
636 			else if (enable_rx_dim)
637 				mana_dim_change(rx_cq, true);
638 			else if (!new_rx_dim && modr_changed)
639 				mana_gd_ring_dim(rx_cq->gdma_cq,
640 						 apc->intr_modr_rx_usec, true,
641 						 apc->intr_modr_rx_comp, true);
642 
643 			if (disable_tx_dim)
644 				mana_dim_change(tx_cq, false);
645 			else if (enable_tx_dim)
646 				mana_dim_change(tx_cq, true);
647 			else if (!new_tx_dim && modr_changed)
648 				mana_gd_ring_dim(tx_cq->gdma_cq,
649 						 apc->intr_modr_tx_usec, true,
650 						 apc->intr_modr_tx_comp, true);
651 		}
652 
653 		/* Publish the enable flag with release semantics so a
654 		 * concurrent NAPI poll that observes it set also sees the DIM
655 		 * (re)init done by mana_dim_change() above.
656 		 */
657 		if (enable_rx_dim)
658 			/* pairs with smp_load_acquire() in mana_update_rx_dim() */
659 			smp_store_release(&apc->rx_dim_enabled, true);
660 		if (enable_tx_dim)
661 			/* pairs with smp_load_acquire() in mana_update_tx_dim() */
662 			smp_store_release(&apc->tx_dim_enabled, true);
663 	}
664 
665 	return 0;
666 }
667 
668 /* mana_set_channels - change the number of queues on a port
669  *
670  * Returns -EBUSY if RDMA holds the vport with EQs sized to the
671  * current num_queues.
672  */
673 static int mana_set_channels(struct net_device *ndev,
674 			     struct ethtool_channels *channels)
675 {
676 	struct mana_port_context *apc = netdev_priv(ndev);
677 	unsigned int new_count = channels->combined_count;
678 	unsigned int old_count = apc->num_queues;
679 	int err;
680 
681 	/* Set channel_changing to block RDMA from grabbing the vport
682 	 * during the detach/attach window. mana_cfg_vport() checks
683 	 * this flag under vport_mutex and returns -EBUSY if set.
684 	 */
685 	mutex_lock(&apc->vport_mutex);
686 	if (!apc->port_is_up && apc->vport_use_count) {
687 		mutex_unlock(&apc->vport_mutex);
688 		return -EBUSY;
689 	}
690 	apc->channel_changing = true;
691 	mutex_unlock(&apc->vport_mutex);
692 
693 	err = mana_pre_alloc_rxbufs(apc, ndev->mtu, new_count);
694 	if (err) {
695 		netdev_err(ndev, "Insufficient memory for new allocations");
696 		goto clear_flag;
697 	}
698 
699 	err = mana_detach(ndev, false);
700 	if (err) {
701 		netdev_err(ndev, "mana_detach failed: %d\n", err);
702 		goto out;
703 	}
704 
705 	apc->num_queues = new_count;
706 	err = mana_attach(ndev);
707 	if (err) {
708 		apc->num_queues = old_count;
709 		netdev_err(ndev, "mana_attach failed: %d\n", err);
710 	}
711 
712 out:
713 	mana_pre_dealloc_rxbufs(apc);
714 clear_flag:
715 	mutex_lock(&apc->vport_mutex);
716 	apc->channel_changing = false;
717 	mutex_unlock(&apc->vport_mutex);
718 	return err;
719 }
720 
721 static void mana_get_ringparam(struct net_device *ndev,
722 			       struct ethtool_ringparam *ring,
723 			       struct kernel_ethtool_ringparam *kernel_ring,
724 			       struct netlink_ext_ack *extack)
725 {
726 	struct mana_port_context *apc = netdev_priv(ndev);
727 
728 	ring->rx_pending = apc->rx_queue_size;
729 	ring->tx_pending = apc->tx_queue_size;
730 	ring->rx_max_pending = MAX_RX_BUFFERS_PER_QUEUE;
731 	ring->tx_max_pending = MAX_TX_BUFFERS_PER_QUEUE;
732 }
733 
734 static int mana_set_ringparam(struct net_device *ndev,
735 			      struct ethtool_ringparam *ring,
736 			      struct kernel_ethtool_ringparam *kernel_ring,
737 			      struct netlink_ext_ack *extack)
738 {
739 	struct mana_port_context *apc = netdev_priv(ndev);
740 	u32 new_tx, new_rx;
741 	u32 old_tx, old_rx;
742 	int err;
743 
744 	old_tx = apc->tx_queue_size;
745 	old_rx = apc->rx_queue_size;
746 
747 	if (ring->tx_pending < MIN_TX_BUFFERS_PER_QUEUE) {
748 		NL_SET_ERR_MSG_FMT(extack, "tx:%d less than the min:%d", ring->tx_pending,
749 				   MIN_TX_BUFFERS_PER_QUEUE);
750 		return -EINVAL;
751 	}
752 
753 	if (ring->rx_pending < MIN_RX_BUFFERS_PER_QUEUE) {
754 		NL_SET_ERR_MSG_FMT(extack, "rx:%d less than the min:%d", ring->rx_pending,
755 				   MIN_RX_BUFFERS_PER_QUEUE);
756 		return -EINVAL;
757 	}
758 
759 	new_rx = roundup_pow_of_two(ring->rx_pending);
760 	new_tx = roundup_pow_of_two(ring->tx_pending);
761 	netdev_info(ndev, "Using nearest power of 2 values for Txq:%d Rxq:%d\n",
762 		    new_tx, new_rx);
763 
764 	/* pre-allocating new buffers to prevent failures in mana_attach() later */
765 	apc->rx_queue_size = new_rx;
766 	err = mana_pre_alloc_rxbufs(apc, ndev->mtu, apc->num_queues);
767 	apc->rx_queue_size = old_rx;
768 	if (err) {
769 		netdev_err(ndev, "Insufficient memory for new allocations\n");
770 		return err;
771 	}
772 
773 	err = mana_detach(ndev, false);
774 	if (err) {
775 		netdev_err(ndev, "mana_detach failed: %d\n", err);
776 		goto out;
777 	}
778 
779 	apc->tx_queue_size = new_tx;
780 	apc->rx_queue_size = new_rx;
781 
782 	err = mana_attach(ndev);
783 	if (err) {
784 		netdev_err(ndev, "mana_attach failed: %d\n", err);
785 		apc->tx_queue_size = old_tx;
786 		apc->rx_queue_size = old_rx;
787 	}
788 out:
789 	mana_pre_dealloc_rxbufs(apc);
790 	return err;
791 }
792 
793 static int mana_get_link_ksettings(struct net_device *ndev,
794 				   struct ethtool_link_ksettings *cmd)
795 {
796 	struct mana_port_context *apc = netdev_priv(ndev);
797 	int err;
798 
799 	err = mana_query_link_cfg(apc);
800 	cmd->base.speed = (err) ? SPEED_UNKNOWN : apc->max_speed;
801 
802 	cmd->base.duplex = DUPLEX_FULL;
803 	cmd->base.port = PORT_OTHER;
804 
805 	return 0;
806 }
807 
808 static u32 mana_get_priv_flags(struct net_device *ndev)
809 {
810 	struct mana_port_context *apc = netdev_priv(ndev);
811 
812 	return apc->priv_flags;
813 }
814 
815 static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags)
816 {
817 	struct mana_port_context *apc = netdev_priv(ndev);
818 	u32 changed = apc->priv_flags ^ priv_flags;
819 	u32 old_priv_flags = apc->priv_flags;
820 	int err = 0;
821 
822 	if (!changed)
823 		return 0;
824 
825 	/* Reject unknown bits */
826 	if (priv_flags & ~GENMASK(MANA_PRIV_FLAG_MAX - 1, 0))
827 		return -EINVAL;
828 
829 	apc->priv_flags = priv_flags;
830 
831 	if (changed & BIT(MANA_PRIV_FLAG_USE_FULL_PAGE_RXBUF)) {
832 		if (!apc->port_is_up)
833 			return 0;
834 
835 		/* If XDP is attached or the MTU already forces one buffer per
836 		 * page, single-buffer-per-page is used regardless of this
837 		 * flag. Skip the expensive detach/attach cycle since nothing
838 		 * changes.
839 		 */
840 		if (mana_single_rxbuf_per_page_forced(apc, ndev->mtu))
841 			return 0;
842 
843 		/* Block RDMA from grabbing the vport during detach/attach */
844 		mutex_lock(&apc->vport_mutex);
845 		apc->channel_changing = true;
846 		mutex_unlock(&apc->vport_mutex);
847 
848 		err = mana_pre_alloc_rxbufs(apc, ndev->mtu, apc->num_queues);
849 		if (err) {
850 			netdev_err(ndev,
851 				   "Insufficient memory for new allocations\n");
852 			apc->priv_flags = old_priv_flags;
853 			goto clear_flag;
854 		}
855 
856 		err = mana_detach(ndev, false);
857 		if (err) {
858 			netdev_err(ndev, "mana_detach failed: %d\n", err);
859 			apc->priv_flags = old_priv_flags;
860 			goto out;
861 		}
862 
863 		err = mana_attach(ndev);
864 		if (err) {
865 			netdev_err(ndev, "mana_attach failed: %d\n", err);
866 			apc->priv_flags = old_priv_flags;
867 		}
868 	}
869 
870 out:
871 	mana_pre_dealloc_rxbufs(apc);
872 clear_flag:
873 	mutex_lock(&apc->vport_mutex);
874 	apc->channel_changing = false;
875 	mutex_unlock(&apc->vport_mutex);
876 
877 	return err;
878 }
879 
880 const struct ethtool_ops mana_ethtool_ops = {
881 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_CQE_FRAMES |
882 				     ETHTOOL_COALESCE_RX_USECS |
883 				     ETHTOOL_COALESCE_RX_MAX_FRAMES |
884 				     ETHTOOL_COALESCE_TX_USECS |
885 				     ETHTOOL_COALESCE_TX_MAX_FRAMES |
886 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX |
887 				     ETHTOOL_COALESCE_USE_ADAPTIVE_TX,
888 	.op_needs_rtnl		= ETHTOOL_OP_NEEDS_RTNL_SCHANNELS |
889 				  ETHTOOL_OP_NEEDS_RTNL_SRINGPARAM |
890 				  ETHTOOL_OP_NEEDS_RTNL_SPFLAGS |
891 				  ETHTOOL_OP_NEEDS_RTNL_GLINK,
892 	.get_ethtool_stats	= mana_get_ethtool_stats,
893 	.get_sset_count		= mana_get_sset_count,
894 	.get_strings		= mana_get_strings,
895 	.get_rx_ring_count	= mana_get_rx_ring_count,
896 	.get_rxfh_key_size	= mana_get_rxfh_key_size,
897 	.get_rxfh_indir_size	= mana_rss_indir_size,
898 	.get_rxfh		= mana_get_rxfh,
899 	.set_rxfh		= mana_set_rxfh,
900 	.get_channels		= mana_get_channels,
901 	.set_channels		= mana_set_channels,
902 	.get_coalesce		= mana_get_coalesce,
903 	.set_coalesce		= mana_set_coalesce,
904 	.get_ringparam          = mana_get_ringparam,
905 	.set_ringparam          = mana_set_ringparam,
906 	.get_link_ksettings	= mana_get_link_ksettings,
907 	.get_link		= ethtool_op_get_link,
908 	.get_priv_flags		= mana_get_priv_flags,
909 	.set_priv_flags		= mana_set_priv_flags,
910 };
911