xref: /linux/drivers/net/ethernet/microsoft/mana/mana_ethtool.c (revision ca220141fa8ebae09765a242076b2b77338106b0)
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_coalesced_err", offsetof(struct mana_ethtool_stats,
24 					rx_coalesced_err)},
25 	{"rx_cqe_unknown_type", offsetof(struct mana_ethtool_stats,
26 					rx_cqe_unknown_type)},
27 };
28 
29 static const struct mana_stats_desc mana_hc_stats[] = {
30 	{"hc_rx_discards_no_wqe", offsetof(struct mana_ethtool_hc_stats,
31 					   hc_rx_discards_no_wqe)},
32 	{"hc_rx_err_vport_disabled", offsetof(struct mana_ethtool_hc_stats,
33 					      hc_rx_err_vport_disabled)},
34 	{"hc_rx_bytes", offsetof(struct mana_ethtool_hc_stats, hc_rx_bytes)},
35 	{"hc_rx_ucast_pkts", offsetof(struct mana_ethtool_hc_stats,
36 				      hc_rx_ucast_pkts)},
37 	{"hc_rx_ucast_bytes", offsetof(struct mana_ethtool_hc_stats,
38 				       hc_rx_ucast_bytes)},
39 	{"hc_rx_bcast_pkts", offsetof(struct mana_ethtool_hc_stats,
40 				      hc_rx_bcast_pkts)},
41 	{"hc_rx_bcast_bytes", offsetof(struct mana_ethtool_hc_stats,
42 				       hc_rx_bcast_bytes)},
43 	{"hc_rx_mcast_pkts", offsetof(struct mana_ethtool_hc_stats,
44 				      hc_rx_mcast_pkts)},
45 	{"hc_rx_mcast_bytes", offsetof(struct mana_ethtool_hc_stats,
46 				       hc_rx_mcast_bytes)},
47 	{"hc_tx_err_gf_disabled", offsetof(struct mana_ethtool_hc_stats,
48 					   hc_tx_err_gf_disabled)},
49 	{"hc_tx_err_vport_disabled", offsetof(struct mana_ethtool_hc_stats,
50 					      hc_tx_err_vport_disabled)},
51 	{"hc_tx_err_inval_vportoffset_pkt",
52 	 offsetof(struct mana_ethtool_hc_stats,
53 		  hc_tx_err_inval_vportoffset_pkt)},
54 	{"hc_tx_err_vlan_enforcement", offsetof(struct mana_ethtool_hc_stats,
55 						hc_tx_err_vlan_enforcement)},
56 	{"hc_tx_err_eth_type_enforcement",
57 	 offsetof(struct mana_ethtool_hc_stats, hc_tx_err_eth_type_enforcement)},
58 	{"hc_tx_err_sa_enforcement", offsetof(struct mana_ethtool_hc_stats,
59 					      hc_tx_err_sa_enforcement)},
60 	{"hc_tx_err_sqpdid_enforcement",
61 	 offsetof(struct mana_ethtool_hc_stats, hc_tx_err_sqpdid_enforcement)},
62 	{"hc_tx_err_cqpdid_enforcement",
63 	 offsetof(struct mana_ethtool_hc_stats, hc_tx_err_cqpdid_enforcement)},
64 	{"hc_tx_err_mtu_violation", offsetof(struct mana_ethtool_hc_stats,
65 					     hc_tx_err_mtu_violation)},
66 	{"hc_tx_err_inval_oob", offsetof(struct mana_ethtool_hc_stats,
67 					 hc_tx_err_inval_oob)},
68 	{"hc_tx_err_gdma", offsetof(struct mana_ethtool_hc_stats,
69 				    hc_tx_err_gdma)},
70 	{"hc_tx_bytes", offsetof(struct mana_ethtool_hc_stats, hc_tx_bytes)},
71 	{"hc_tx_ucast_pkts", offsetof(struct mana_ethtool_hc_stats,
72 					hc_tx_ucast_pkts)},
73 	{"hc_tx_ucast_bytes", offsetof(struct mana_ethtool_hc_stats,
74 					hc_tx_ucast_bytes)},
75 	{"hc_tx_bcast_pkts", offsetof(struct mana_ethtool_hc_stats,
76 					hc_tx_bcast_pkts)},
77 	{"hc_tx_bcast_bytes", offsetof(struct mana_ethtool_hc_stats,
78 					hc_tx_bcast_bytes)},
79 	{"hc_tx_mcast_pkts", offsetof(struct mana_ethtool_hc_stats,
80 					hc_tx_mcast_pkts)},
81 	{"hc_tx_mcast_bytes", offsetof(struct mana_ethtool_hc_stats,
82 					hc_tx_mcast_bytes)},
83 };
84 
85 static const struct mana_stats_desc mana_phy_stats[] = {
86 	{ "hc_rx_pkt_drop_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_drop_phy) },
87 	{ "hc_tx_pkt_drop_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_drop_phy) },
88 	{ "hc_tc0_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc0_phy) },
89 	{ "hc_tc0_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc0_phy) },
90 	{ "hc_tc0_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc0_phy) },
91 	{ "hc_tc0_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc0_phy) },
92 	{ "hc_tc1_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc1_phy) },
93 	{ "hc_tc1_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc1_phy) },
94 	{ "hc_tc1_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc1_phy) },
95 	{ "hc_tc1_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc1_phy) },
96 	{ "hc_tc2_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc2_phy) },
97 	{ "hc_tc2_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc2_phy) },
98 	{ "hc_tc2_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc2_phy) },
99 	{ "hc_tc2_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc2_phy) },
100 	{ "hc_tc3_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc3_phy) },
101 	{ "hc_tc3_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc3_phy) },
102 	{ "hc_tc3_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc3_phy) },
103 	{ "hc_tc3_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc3_phy) },
104 	{ "hc_tc4_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc4_phy) },
105 	{ "hc_tc4_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc4_phy) },
106 	{ "hc_tc4_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc4_phy) },
107 	{ "hc_tc4_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc4_phy) },
108 	{ "hc_tc5_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc5_phy) },
109 	{ "hc_tc5_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc5_phy) },
110 	{ "hc_tc5_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc5_phy) },
111 	{ "hc_tc5_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc5_phy) },
112 	{ "hc_tc6_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc6_phy) },
113 	{ "hc_tc6_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc6_phy) },
114 	{ "hc_tc6_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc6_phy) },
115 	{ "hc_tc6_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc6_phy) },
116 	{ "hc_tc7_rx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, rx_pkt_tc7_phy) },
117 	{ "hc_tc7_rx_byte_phy", offsetof(struct mana_ethtool_phy_stats, rx_byte_tc7_phy) },
118 	{ "hc_tc7_tx_pkt_phy", offsetof(struct mana_ethtool_phy_stats, tx_pkt_tc7_phy) },
119 	{ "hc_tc7_tx_byte_phy", offsetof(struct mana_ethtool_phy_stats, tx_byte_tc7_phy) },
120 	{ "hc_tc0_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc0_phy) },
121 	{ "hc_tc0_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc0_phy) },
122 	{ "hc_tc1_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc1_phy) },
123 	{ "hc_tc1_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc1_phy) },
124 	{ "hc_tc2_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc2_phy) },
125 	{ "hc_tc2_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc2_phy) },
126 	{ "hc_tc3_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc3_phy) },
127 	{ "hc_tc3_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc3_phy) },
128 	{ "hc_tc4_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc4_phy) },
129 	{ "hc_tc4_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc4_phy) },
130 	{ "hc_tc5_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc5_phy) },
131 	{ "hc_tc5_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc5_phy) },
132 	{ "hc_tc6_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc6_phy) },
133 	{ "hc_tc6_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc6_phy) },
134 	{ "hc_tc7_rx_pause_phy", offsetof(struct mana_ethtool_phy_stats, rx_pause_tc7_phy) },
135 	{ "hc_tc7_tx_pause_phy", offsetof(struct mana_ethtool_phy_stats, tx_pause_tc7_phy) },
136 };
137 
138 static int mana_get_sset_count(struct net_device *ndev, int stringset)
139 {
140 	struct mana_port_context *apc = netdev_priv(ndev);
141 	unsigned int num_queues = apc->num_queues;
142 
143 	if (stringset != ETH_SS_STATS)
144 		return -EINVAL;
145 
146 	return ARRAY_SIZE(mana_eth_stats) + ARRAY_SIZE(mana_phy_stats) + ARRAY_SIZE(mana_hc_stats) +
147 			num_queues * (MANA_STATS_RX_COUNT + MANA_STATS_TX_COUNT);
148 }
149 
150 static void mana_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
151 {
152 	struct mana_port_context *apc = netdev_priv(ndev);
153 	unsigned int num_queues = apc->num_queues;
154 	int i;
155 
156 	if (stringset != ETH_SS_STATS)
157 		return;
158 	for (i = 0; i < ARRAY_SIZE(mana_eth_stats); i++)
159 		ethtool_puts(&data, mana_eth_stats[i].name);
160 
161 	for (i = 0; i < ARRAY_SIZE(mana_hc_stats); i++)
162 		ethtool_puts(&data, mana_hc_stats[i].name);
163 
164 	for (i = 0; i < ARRAY_SIZE(mana_phy_stats); i++)
165 		ethtool_puts(&data, mana_phy_stats[i].name);
166 
167 	for (i = 0; i < num_queues; i++) {
168 		ethtool_sprintf(&data, "rx_%d_packets", i);
169 		ethtool_sprintf(&data, "rx_%d_bytes", i);
170 		ethtool_sprintf(&data, "rx_%d_xdp_drop", i);
171 		ethtool_sprintf(&data, "rx_%d_xdp_tx", i);
172 		ethtool_sprintf(&data, "rx_%d_xdp_redirect", i);
173 	}
174 
175 	for (i = 0; i < num_queues; i++) {
176 		ethtool_sprintf(&data, "tx_%d_packets", i);
177 		ethtool_sprintf(&data, "tx_%d_bytes", i);
178 		ethtool_sprintf(&data, "tx_%d_xdp_xmit", i);
179 		ethtool_sprintf(&data, "tx_%d_tso_packets", i);
180 		ethtool_sprintf(&data, "tx_%d_tso_bytes", i);
181 		ethtool_sprintf(&data, "tx_%d_tso_inner_packets", i);
182 		ethtool_sprintf(&data, "tx_%d_tso_inner_bytes", i);
183 		ethtool_sprintf(&data, "tx_%d_long_pkt_fmt", i);
184 		ethtool_sprintf(&data, "tx_%d_short_pkt_fmt", i);
185 		ethtool_sprintf(&data, "tx_%d_csum_partial", i);
186 		ethtool_sprintf(&data, "tx_%d_mana_map_err", i);
187 	}
188 }
189 
190 static void mana_get_ethtool_stats(struct net_device *ndev,
191 				   struct ethtool_stats *e_stats, u64 *data)
192 {
193 	struct mana_port_context *apc = netdev_priv(ndev);
194 	unsigned int num_queues = apc->num_queues;
195 	void *eth_stats = &apc->eth_stats;
196 	void *hc_stats = &apc->ac->hc_stats;
197 	void *phy_stats = &apc->phy_stats;
198 	struct mana_stats_rx *rx_stats;
199 	struct mana_stats_tx *tx_stats;
200 	unsigned int start;
201 	u64 packets, bytes;
202 	u64 xdp_redirect;
203 	u64 xdp_xmit;
204 	u64 xdp_drop;
205 	u64 xdp_tx;
206 	u64 tso_packets;
207 	u64 tso_bytes;
208 	u64 tso_inner_packets;
209 	u64 tso_inner_bytes;
210 	u64 long_pkt_fmt;
211 	u64 short_pkt_fmt;
212 	u64 csum_partial;
213 	u64 mana_map_err;
214 	int q, i = 0;
215 
216 	if (!apc->port_is_up)
217 		return;
218 
219 	/* We call this mana function to get the phy stats from GDMA and includes
220 	 * aggregate tx/rx drop counters, Per-TC(Traffic Channel) tx/rx and pause
221 	 * counters.
222 	 */
223 	mana_query_phy_stats(apc);
224 
225 	for (q = 0; q < ARRAY_SIZE(mana_eth_stats); q++)
226 		data[i++] = *(u64 *)(eth_stats + mana_eth_stats[q].offset);
227 
228 	for (q = 0; q < ARRAY_SIZE(mana_hc_stats); q++)
229 		data[i++] = *(u64 *)(hc_stats + mana_hc_stats[q].offset);
230 
231 	for (q = 0; q < ARRAY_SIZE(mana_phy_stats); q++)
232 		data[i++] = *(u64 *)(phy_stats + mana_phy_stats[q].offset);
233 
234 	for (q = 0; q < num_queues; q++) {
235 		rx_stats = &apc->rxqs[q]->stats;
236 
237 		do {
238 			start = u64_stats_fetch_begin(&rx_stats->syncp);
239 			packets = rx_stats->packets;
240 			bytes = rx_stats->bytes;
241 			xdp_drop = rx_stats->xdp_drop;
242 			xdp_tx = rx_stats->xdp_tx;
243 			xdp_redirect = rx_stats->xdp_redirect;
244 		} while (u64_stats_fetch_retry(&rx_stats->syncp, start));
245 
246 		data[i++] = packets;
247 		data[i++] = bytes;
248 		data[i++] = xdp_drop;
249 		data[i++] = xdp_tx;
250 		data[i++] = xdp_redirect;
251 	}
252 
253 	for (q = 0; q < num_queues; q++) {
254 		tx_stats = &apc->tx_qp[q].txq.stats;
255 
256 		do {
257 			start = u64_stats_fetch_begin(&tx_stats->syncp);
258 			packets = tx_stats->packets;
259 			bytes = tx_stats->bytes;
260 			xdp_xmit = tx_stats->xdp_xmit;
261 			tso_packets = tx_stats->tso_packets;
262 			tso_bytes = tx_stats->tso_bytes;
263 			tso_inner_packets = tx_stats->tso_inner_packets;
264 			tso_inner_bytes = tx_stats->tso_inner_bytes;
265 			long_pkt_fmt = tx_stats->long_pkt_fmt;
266 			short_pkt_fmt = tx_stats->short_pkt_fmt;
267 			csum_partial = tx_stats->csum_partial;
268 			mana_map_err = tx_stats->mana_map_err;
269 		} while (u64_stats_fetch_retry(&tx_stats->syncp, start));
270 
271 		data[i++] = packets;
272 		data[i++] = bytes;
273 		data[i++] = xdp_xmit;
274 		data[i++] = tso_packets;
275 		data[i++] = tso_bytes;
276 		data[i++] = tso_inner_packets;
277 		data[i++] = tso_inner_bytes;
278 		data[i++] = long_pkt_fmt;
279 		data[i++] = short_pkt_fmt;
280 		data[i++] = csum_partial;
281 		data[i++] = mana_map_err;
282 	}
283 }
284 
285 static u32 mana_get_rx_ring_count(struct net_device *ndev)
286 {
287 	struct mana_port_context *apc = netdev_priv(ndev);
288 
289 	return apc->num_queues;
290 }
291 
292 static u32 mana_get_rxfh_key_size(struct net_device *ndev)
293 {
294 	return MANA_HASH_KEY_SIZE;
295 }
296 
297 static u32 mana_rss_indir_size(struct net_device *ndev)
298 {
299 	struct mana_port_context *apc = netdev_priv(ndev);
300 
301 	return apc->indir_table_sz;
302 }
303 
304 static int mana_get_rxfh(struct net_device *ndev,
305 			 struct ethtool_rxfh_param *rxfh)
306 {
307 	struct mana_port_context *apc = netdev_priv(ndev);
308 	int i;
309 
310 	rxfh->hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
311 
312 	if (rxfh->indir) {
313 		for (i = 0; i < apc->indir_table_sz; i++)
314 			rxfh->indir[i] = apc->indir_table[i];
315 	}
316 
317 	if (rxfh->key)
318 		memcpy(rxfh->key, apc->hashkey, MANA_HASH_KEY_SIZE);
319 
320 	return 0;
321 }
322 
323 static int mana_set_rxfh(struct net_device *ndev,
324 			 struct ethtool_rxfh_param *rxfh,
325 			 struct netlink_ext_ack *extack)
326 {
327 	struct mana_port_context *apc = netdev_priv(ndev);
328 	bool update_hash = false, update_table = false;
329 	u8 save_key[MANA_HASH_KEY_SIZE];
330 	u32 *save_table;
331 	int i, err;
332 
333 	if (!apc->port_is_up)
334 		return -EOPNOTSUPP;
335 
336 	if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
337 	    rxfh->hfunc != ETH_RSS_HASH_TOP)
338 		return -EOPNOTSUPP;
339 
340 	save_table = kcalloc(apc->indir_table_sz, sizeof(u32), GFP_KERNEL);
341 	if (!save_table)
342 		return -ENOMEM;
343 
344 	if (rxfh->indir) {
345 		for (i = 0; i < apc->indir_table_sz; i++)
346 			if (rxfh->indir[i] >= apc->num_queues) {
347 				err = -EINVAL;
348 				goto cleanup;
349 			}
350 
351 		update_table = true;
352 		for (i = 0; i < apc->indir_table_sz; i++) {
353 			save_table[i] = apc->indir_table[i];
354 			apc->indir_table[i] = rxfh->indir[i];
355 		}
356 	}
357 
358 	if (rxfh->key) {
359 		update_hash = true;
360 		memcpy(save_key, apc->hashkey, MANA_HASH_KEY_SIZE);
361 		memcpy(apc->hashkey, rxfh->key, MANA_HASH_KEY_SIZE);
362 	}
363 
364 	err = mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
365 
366 	if (err) { /* recover to original values */
367 		if (update_table) {
368 			for (i = 0; i < apc->indir_table_sz; i++)
369 				apc->indir_table[i] = save_table[i];
370 		}
371 
372 		if (update_hash)
373 			memcpy(apc->hashkey, save_key, MANA_HASH_KEY_SIZE);
374 
375 		mana_config_rss(apc, TRI_STATE_TRUE, update_hash, update_table);
376 	}
377 
378 cleanup:
379 	kfree(save_table);
380 
381 	return err;
382 }
383 
384 static void mana_get_channels(struct net_device *ndev,
385 			      struct ethtool_channels *channel)
386 {
387 	struct mana_port_context *apc = netdev_priv(ndev);
388 
389 	channel->max_combined = apc->max_queues;
390 	channel->combined_count = apc->num_queues;
391 }
392 
393 static int mana_set_channels(struct net_device *ndev,
394 			     struct ethtool_channels *channels)
395 {
396 	struct mana_port_context *apc = netdev_priv(ndev);
397 	unsigned int new_count = channels->combined_count;
398 	unsigned int old_count = apc->num_queues;
399 	int err;
400 
401 	err = mana_pre_alloc_rxbufs(apc, ndev->mtu, new_count);
402 	if (err) {
403 		netdev_err(ndev, "Insufficient memory for new allocations");
404 		return err;
405 	}
406 
407 	err = mana_detach(ndev, false);
408 	if (err) {
409 		netdev_err(ndev, "mana_detach failed: %d\n", err);
410 		goto out;
411 	}
412 
413 	apc->num_queues = new_count;
414 	err = mana_attach(ndev);
415 	if (err) {
416 		apc->num_queues = old_count;
417 		netdev_err(ndev, "mana_attach failed: %d\n", err);
418 	}
419 
420 out:
421 	mana_pre_dealloc_rxbufs(apc);
422 	return err;
423 }
424 
425 static void mana_get_ringparam(struct net_device *ndev,
426 			       struct ethtool_ringparam *ring,
427 			       struct kernel_ethtool_ringparam *kernel_ring,
428 			       struct netlink_ext_ack *extack)
429 {
430 	struct mana_port_context *apc = netdev_priv(ndev);
431 
432 	ring->rx_pending = apc->rx_queue_size;
433 	ring->tx_pending = apc->tx_queue_size;
434 	ring->rx_max_pending = MAX_RX_BUFFERS_PER_QUEUE;
435 	ring->tx_max_pending = MAX_TX_BUFFERS_PER_QUEUE;
436 }
437 
438 static int mana_set_ringparam(struct net_device *ndev,
439 			      struct ethtool_ringparam *ring,
440 			      struct kernel_ethtool_ringparam *kernel_ring,
441 			      struct netlink_ext_ack *extack)
442 {
443 	struct mana_port_context *apc = netdev_priv(ndev);
444 	u32 new_tx, new_rx;
445 	u32 old_tx, old_rx;
446 	int err;
447 
448 	old_tx = apc->tx_queue_size;
449 	old_rx = apc->rx_queue_size;
450 
451 	if (ring->tx_pending < MIN_TX_BUFFERS_PER_QUEUE) {
452 		NL_SET_ERR_MSG_FMT(extack, "tx:%d less than the min:%d", ring->tx_pending,
453 				   MIN_TX_BUFFERS_PER_QUEUE);
454 		return -EINVAL;
455 	}
456 
457 	if (ring->rx_pending < MIN_RX_BUFFERS_PER_QUEUE) {
458 		NL_SET_ERR_MSG_FMT(extack, "rx:%d less than the min:%d", ring->rx_pending,
459 				   MIN_RX_BUFFERS_PER_QUEUE);
460 		return -EINVAL;
461 	}
462 
463 	new_rx = roundup_pow_of_two(ring->rx_pending);
464 	new_tx = roundup_pow_of_two(ring->tx_pending);
465 	netdev_info(ndev, "Using nearest power of 2 values for Txq:%d Rxq:%d\n",
466 		    new_tx, new_rx);
467 
468 	/* pre-allocating new buffers to prevent failures in mana_attach() later */
469 	apc->rx_queue_size = new_rx;
470 	err = mana_pre_alloc_rxbufs(apc, ndev->mtu, apc->num_queues);
471 	apc->rx_queue_size = old_rx;
472 	if (err) {
473 		netdev_err(ndev, "Insufficient memory for new allocations\n");
474 		return err;
475 	}
476 
477 	err = mana_detach(ndev, false);
478 	if (err) {
479 		netdev_err(ndev, "mana_detach failed: %d\n", err);
480 		goto out;
481 	}
482 
483 	apc->tx_queue_size = new_tx;
484 	apc->rx_queue_size = new_rx;
485 
486 	err = mana_attach(ndev);
487 	if (err) {
488 		netdev_err(ndev, "mana_attach failed: %d\n", err);
489 		apc->tx_queue_size = old_tx;
490 		apc->rx_queue_size = old_rx;
491 	}
492 out:
493 	mana_pre_dealloc_rxbufs(apc);
494 	return err;
495 }
496 
497 static int mana_get_link_ksettings(struct net_device *ndev,
498 				   struct ethtool_link_ksettings *cmd)
499 {
500 	struct mana_port_context *apc = netdev_priv(ndev);
501 	int err;
502 
503 	err = mana_query_link_cfg(apc);
504 	cmd->base.speed = (err) ? SPEED_UNKNOWN : apc->max_speed;
505 
506 	cmd->base.duplex = DUPLEX_FULL;
507 	cmd->base.port = PORT_OTHER;
508 
509 	return 0;
510 }
511 
512 const struct ethtool_ops mana_ethtool_ops = {
513 	.get_ethtool_stats	= mana_get_ethtool_stats,
514 	.get_sset_count		= mana_get_sset_count,
515 	.get_strings		= mana_get_strings,
516 	.get_rx_ring_count	= mana_get_rx_ring_count,
517 	.get_rxfh_key_size	= mana_get_rxfh_key_size,
518 	.get_rxfh_indir_size	= mana_rss_indir_size,
519 	.get_rxfh		= mana_get_rxfh,
520 	.set_rxfh		= mana_set_rxfh,
521 	.get_channels		= mana_get_channels,
522 	.set_channels		= mana_set_channels,
523 	.get_ringparam          = mana_get_ringparam,
524 	.set_ringparam          = mana_set_ringparam,
525 	.get_link_ksettings	= mana_get_link_ksettings,
526 	.get_link		= ethtool_op_get_link,
527 };
528