1 /* Intel Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2015 Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
16  * Contact Information:
17  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19  */
20 
21 #include <linux/vmalloc.h>
22 
23 #include "fm10k.h"
24 
25 struct fm10k_stats {
26 	char stat_string[ETH_GSTRING_LEN];
27 	int sizeof_stat;
28 	int stat_offset;
29 };
30 
31 #define FM10K_NETDEV_STAT(_net_stat) { \
32 	.stat_string = #_net_stat, \
33 	.sizeof_stat = FIELD_SIZEOF(struct net_device_stats, _net_stat), \
34 	.stat_offset = offsetof(struct net_device_stats, _net_stat) \
35 }
36 
37 static const struct fm10k_stats fm10k_gstrings_net_stats[] = {
38 	FM10K_NETDEV_STAT(tx_packets),
39 	FM10K_NETDEV_STAT(tx_bytes),
40 	FM10K_NETDEV_STAT(tx_errors),
41 	FM10K_NETDEV_STAT(rx_packets),
42 	FM10K_NETDEV_STAT(rx_bytes),
43 	FM10K_NETDEV_STAT(rx_errors),
44 	FM10K_NETDEV_STAT(rx_dropped),
45 
46 	/* detailed Rx errors */
47 	FM10K_NETDEV_STAT(rx_length_errors),
48 	FM10K_NETDEV_STAT(rx_crc_errors),
49 	FM10K_NETDEV_STAT(rx_fifo_errors),
50 };
51 
52 #define FM10K_NETDEV_STATS_LEN	ARRAY_SIZE(fm10k_gstrings_net_stats)
53 
54 #define FM10K_STAT(_name, _stat) { \
55 	.stat_string = _name, \
56 	.sizeof_stat = FIELD_SIZEOF(struct fm10k_intfc, _stat), \
57 	.stat_offset = offsetof(struct fm10k_intfc, _stat) \
58 }
59 
60 static const struct fm10k_stats fm10k_gstrings_global_stats[] = {
61 	FM10K_STAT("tx_restart_queue", restart_queue),
62 	FM10K_STAT("tx_busy", tx_busy),
63 	FM10K_STAT("tx_csum_errors", tx_csum_errors),
64 	FM10K_STAT("rx_alloc_failed", alloc_failed),
65 	FM10K_STAT("rx_csum_errors", rx_csum_errors),
66 
67 	FM10K_STAT("tx_packets_nic", tx_packets_nic),
68 	FM10K_STAT("tx_bytes_nic", tx_bytes_nic),
69 	FM10K_STAT("rx_packets_nic", rx_packets_nic),
70 	FM10K_STAT("rx_bytes_nic", rx_bytes_nic),
71 	FM10K_STAT("rx_drops_nic", rx_drops_nic),
72 	FM10K_STAT("rx_overrun_pf", rx_overrun_pf),
73 	FM10K_STAT("rx_overrun_vf", rx_overrun_vf),
74 
75 	FM10K_STAT("swapi_status", hw.swapi.status),
76 	FM10K_STAT("mac_rules_used", hw.swapi.mac.used),
77 	FM10K_STAT("mac_rules_avail", hw.swapi.mac.avail),
78 
79 	FM10K_STAT("tx_hang_count", tx_timeout_count),
80 
81 	FM10K_STAT("tx_hwtstamp_timeouts", tx_hwtstamp_timeouts),
82 };
83 
84 static const struct fm10k_stats fm10k_gstrings_debug_stats[] = {
85 	FM10K_STAT("hw_sm_mbx_full", hw_sm_mbx_full),
86 	FM10K_STAT("hw_csum_tx_good", hw_csum_tx_good),
87 	FM10K_STAT("hw_csum_rx_good", hw_csum_rx_good),
88 	FM10K_STAT("rx_switch_errors", rx_switch_errors),
89 	FM10K_STAT("rx_drops", rx_drops),
90 	FM10K_STAT("rx_pp_errors", rx_pp_errors),
91 	FM10K_STAT("rx_link_errors", rx_link_errors),
92 	FM10K_STAT("rx_length_errors", rx_length_errors),
93 };
94 
95 static const struct fm10k_stats fm10k_gstrings_pf_stats[] = {
96 	FM10K_STAT("timeout", stats.timeout.count),
97 	FM10K_STAT("ur", stats.ur.count),
98 	FM10K_STAT("ca", stats.ca.count),
99 	FM10K_STAT("um", stats.um.count),
100 	FM10K_STAT("xec", stats.xec.count),
101 	FM10K_STAT("vlan_drop", stats.vlan_drop.count),
102 	FM10K_STAT("loopback_drop", stats.loopback_drop.count),
103 	FM10K_STAT("nodesc_drop", stats.nodesc_drop.count),
104 };
105 
106 #define FM10K_MBX_STAT(_name, _stat) { \
107 	.stat_string = _name, \
108 	.sizeof_stat = FIELD_SIZEOF(struct fm10k_mbx_info, _stat), \
109 	.stat_offset = offsetof(struct fm10k_mbx_info, _stat) \
110 }
111 
112 static const struct fm10k_stats fm10k_gstrings_mbx_stats[] = {
113 	FM10K_MBX_STAT("mbx_tx_busy", tx_busy),
114 	FM10K_MBX_STAT("mbx_tx_dropped", tx_dropped),
115 	FM10K_MBX_STAT("mbx_tx_messages", tx_messages),
116 	FM10K_MBX_STAT("mbx_tx_dwords", tx_dwords),
117 	FM10K_MBX_STAT("mbx_tx_mbmem_pulled", tx_mbmem_pulled),
118 	FM10K_MBX_STAT("mbx_rx_messages", rx_messages),
119 	FM10K_MBX_STAT("mbx_rx_dwords", rx_dwords),
120 	FM10K_MBX_STAT("mbx_rx_parse_err", rx_parse_err),
121 	FM10K_MBX_STAT("mbx_rx_mbmem_pushed", rx_mbmem_pushed),
122 };
123 
124 #define FM10K_GLOBAL_STATS_LEN ARRAY_SIZE(fm10k_gstrings_global_stats)
125 #define FM10K_DEBUG_STATS_LEN ARRAY_SIZE(fm10k_gstrings_debug_stats)
126 #define FM10K_PF_STATS_LEN ARRAY_SIZE(fm10k_gstrings_pf_stats)
127 #define FM10K_MBX_STATS_LEN ARRAY_SIZE(fm10k_gstrings_mbx_stats)
128 
129 #define FM10K_QUEUE_STATS_LEN(_n) \
130 	((_n) * 2 * (sizeof(struct fm10k_queue_stats) / sizeof(u64)))
131 
132 #define FM10K_STATIC_STATS_LEN (FM10K_GLOBAL_STATS_LEN + \
133 				FM10K_NETDEV_STATS_LEN + \
134 				FM10K_MBX_STATS_LEN)
135 
136 static const char fm10k_gstrings_test[][ETH_GSTRING_LEN] = {
137 	"Mailbox test (on/offline)"
138 };
139 
140 #define FM10K_TEST_LEN (sizeof(fm10k_gstrings_test) / ETH_GSTRING_LEN)
141 
142 enum fm10k_self_test_types {
143 	FM10K_TEST_MBX,
144 	FM10K_TEST_MAX = FM10K_TEST_LEN
145 };
146 
147 enum {
148 	FM10K_PRV_FLAG_DEBUG_STATS,
149 	FM10K_PRV_FLAG_LEN,
150 };
151 
152 static const char fm10k_prv_flags[FM10K_PRV_FLAG_LEN][ETH_GSTRING_LEN] = {
153 	"debug-statistics",
154 };
155 
156 static void fm10k_get_stat_strings(struct net_device *dev, u8 *data)
157 {
158 	struct fm10k_intfc *interface = netdev_priv(dev);
159 	struct fm10k_iov_data *iov_data = interface->iov_data;
160 	char *p = (char *)data;
161 	unsigned int i;
162 	unsigned int j;
163 
164 	for (i = 0; i < FM10K_NETDEV_STATS_LEN; i++) {
165 		memcpy(p, fm10k_gstrings_net_stats[i].stat_string,
166 		       ETH_GSTRING_LEN);
167 		p += ETH_GSTRING_LEN;
168 	}
169 
170 	for (i = 0; i < FM10K_GLOBAL_STATS_LEN; i++) {
171 		memcpy(p, fm10k_gstrings_global_stats[i].stat_string,
172 		       ETH_GSTRING_LEN);
173 		p += ETH_GSTRING_LEN;
174 	}
175 
176 	if (interface->flags & FM10K_FLAG_DEBUG_STATS) {
177 		for (i = 0; i < FM10K_DEBUG_STATS_LEN; i++) {
178 			memcpy(p, fm10k_gstrings_debug_stats[i].stat_string,
179 			       ETH_GSTRING_LEN);
180 			p += ETH_GSTRING_LEN;
181 		}
182 	}
183 
184 	for (i = 0; i < FM10K_MBX_STATS_LEN; i++) {
185 		memcpy(p, fm10k_gstrings_mbx_stats[i].stat_string,
186 		       ETH_GSTRING_LEN);
187 		p += ETH_GSTRING_LEN;
188 	}
189 
190 	if (interface->hw.mac.type != fm10k_mac_vf) {
191 		for (i = 0; i < FM10K_PF_STATS_LEN; i++) {
192 			memcpy(p, fm10k_gstrings_pf_stats[i].stat_string,
193 			       ETH_GSTRING_LEN);
194 			p += ETH_GSTRING_LEN;
195 		}
196 	}
197 
198 	if ((interface->flags & FM10K_FLAG_DEBUG_STATS) && iov_data) {
199 		for (i = 0; i < iov_data->num_vfs; i++) {
200 			for (j = 0; j < FM10K_MBX_STATS_LEN; j++) {
201 				snprintf(p,
202 					 ETH_GSTRING_LEN,
203 					 "vf_%u_%s", i,
204 					 fm10k_gstrings_mbx_stats[j].stat_string);
205 				p += ETH_GSTRING_LEN;
206 			}
207 		}
208 	}
209 
210 	for (i = 0; i < interface->hw.mac.max_queues; i++) {
211 		snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_packets", i);
212 		p += ETH_GSTRING_LEN;
213 		snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_bytes", i);
214 		p += ETH_GSTRING_LEN;
215 		snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_packets", i);
216 		p += ETH_GSTRING_LEN;
217 		snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_bytes", i);
218 		p += ETH_GSTRING_LEN;
219 	}
220 }
221 
222 static void fm10k_get_strings(struct net_device *dev,
223 			      u32 stringset, u8 *data)
224 {
225 	char *p = (char *)data;
226 
227 	switch (stringset) {
228 	case ETH_SS_TEST:
229 		memcpy(data, *fm10k_gstrings_test,
230 		       FM10K_TEST_LEN * ETH_GSTRING_LEN);
231 		break;
232 	case ETH_SS_STATS:
233 		fm10k_get_stat_strings(dev, data);
234 		break;
235 	case ETH_SS_PRIV_FLAGS:
236 		memcpy(p, fm10k_prv_flags,
237 		       FM10K_PRV_FLAG_LEN * ETH_GSTRING_LEN);
238 		break;
239 	}
240 }
241 
242 static int fm10k_get_sset_count(struct net_device *dev, int sset)
243 {
244 	struct fm10k_intfc *interface = netdev_priv(dev);
245 	struct fm10k_iov_data *iov_data = interface->iov_data;
246 	struct fm10k_hw *hw = &interface->hw;
247 	int stats_len = FM10K_STATIC_STATS_LEN;
248 
249 	switch (sset) {
250 	case ETH_SS_TEST:
251 		return FM10K_TEST_LEN;
252 	case ETH_SS_STATS:
253 		stats_len += FM10K_QUEUE_STATS_LEN(hw->mac.max_queues);
254 
255 		if (hw->mac.type != fm10k_mac_vf)
256 			stats_len += FM10K_PF_STATS_LEN;
257 
258 		if (interface->flags & FM10K_FLAG_DEBUG_STATS) {
259 			stats_len += FM10K_DEBUG_STATS_LEN;
260 
261 			if (iov_data)
262 				stats_len += FM10K_MBX_STATS_LEN *
263 					iov_data->num_vfs;
264 		}
265 
266 		return stats_len;
267 	case ETH_SS_PRIV_FLAGS:
268 		return FM10K_PRV_FLAG_LEN;
269 	default:
270 		return -EOPNOTSUPP;
271 	}
272 }
273 
274 static void fm10k_get_ethtool_stats(struct net_device *netdev,
275 				    struct ethtool_stats __always_unused *stats,
276 				    u64 *data)
277 {
278 	const int stat_count = sizeof(struct fm10k_queue_stats) / sizeof(u64);
279 	struct fm10k_intfc *interface = netdev_priv(netdev);
280 	struct fm10k_iov_data *iov_data = interface->iov_data;
281 	struct net_device_stats *net_stats = &netdev->stats;
282 	char *p;
283 	int i, j;
284 
285 	fm10k_update_stats(interface);
286 
287 	for (i = 0; i < FM10K_NETDEV_STATS_LEN; i++) {
288 		p = (char *)net_stats + fm10k_gstrings_net_stats[i].stat_offset;
289 		*(data++) = (fm10k_gstrings_net_stats[i].sizeof_stat ==
290 			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
291 	}
292 
293 	for (i = 0; i < FM10K_GLOBAL_STATS_LEN; i++) {
294 		p = (char *)interface +
295 		    fm10k_gstrings_global_stats[i].stat_offset;
296 		*(data++) = (fm10k_gstrings_global_stats[i].sizeof_stat ==
297 			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
298 	}
299 
300 	if (interface->flags & FM10K_FLAG_DEBUG_STATS) {
301 		for (i = 0; i < FM10K_DEBUG_STATS_LEN; i++) {
302 			p = (char *)interface +
303 				fm10k_gstrings_debug_stats[i].stat_offset;
304 			*(data++) = (fm10k_gstrings_debug_stats[i].sizeof_stat ==
305 				     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
306 		}
307 	}
308 
309 	for (i = 0; i < FM10K_MBX_STATS_LEN; i++) {
310 		p = (char *)&interface->hw.mbx +
311 			fm10k_gstrings_mbx_stats[i].stat_offset;
312 		*(data++) = (fm10k_gstrings_mbx_stats[i].sizeof_stat ==
313 			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
314 	}
315 
316 	if (interface->hw.mac.type != fm10k_mac_vf) {
317 		for (i = 0; i < FM10K_PF_STATS_LEN; i++) {
318 			p = (char *)interface +
319 			    fm10k_gstrings_pf_stats[i].stat_offset;
320 			*(data++) = (fm10k_gstrings_pf_stats[i].sizeof_stat ==
321 				     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
322 		}
323 	}
324 
325 	if ((interface->flags & FM10K_FLAG_DEBUG_STATS) && iov_data) {
326 		for (i = 0; i < iov_data->num_vfs; i++) {
327 			struct fm10k_vf_info *vf_info;
328 
329 			vf_info = &iov_data->vf_info[i];
330 
331 			/* skip stats if we don't have a vf info */
332 			if (!vf_info) {
333 				data += FM10K_MBX_STATS_LEN;
334 				continue;
335 			}
336 
337 			for (j = 0; j < FM10K_MBX_STATS_LEN; j++) {
338 				p = (char *)&vf_info->mbx +
339 					fm10k_gstrings_mbx_stats[j].stat_offset;
340 				*(data++) = (fm10k_gstrings_mbx_stats[j].sizeof_stat ==
341 					     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
342 			}
343 		}
344 	}
345 
346 	for (i = 0; i < interface->hw.mac.max_queues; i++) {
347 		struct fm10k_ring *ring;
348 		u64 *queue_stat;
349 
350 		ring = interface->tx_ring[i];
351 		if (ring)
352 			queue_stat = (u64 *)&ring->stats;
353 		for (j = 0; j < stat_count; j++)
354 			*(data++) = ring ? queue_stat[j] : 0;
355 
356 		ring = interface->rx_ring[i];
357 		if (ring)
358 			queue_stat = (u64 *)&ring->stats;
359 		for (j = 0; j < stat_count; j++)
360 			*(data++) = ring ? queue_stat[j] : 0;
361 	}
362 }
363 
364 /* If function below adds more registers this define needs to be updated */
365 #define FM10K_REGS_LEN_Q 29
366 
367 static void fm10k_get_reg_q(struct fm10k_hw *hw, u32 *buff, int i)
368 {
369 	int idx = 0;
370 
371 	buff[idx++] = fm10k_read_reg(hw, FM10K_RDBAL(i));
372 	buff[idx++] = fm10k_read_reg(hw, FM10K_RDBAH(i));
373 	buff[idx++] = fm10k_read_reg(hw, FM10K_RDLEN(i));
374 	buff[idx++] = fm10k_read_reg(hw, FM10K_TPH_RXCTRL(i));
375 	buff[idx++] = fm10k_read_reg(hw, FM10K_RDH(i));
376 	buff[idx++] = fm10k_read_reg(hw, FM10K_RDT(i));
377 	buff[idx++] = fm10k_read_reg(hw, FM10K_RXQCTL(i));
378 	buff[idx++] = fm10k_read_reg(hw, FM10K_RXDCTL(i));
379 	buff[idx++] = fm10k_read_reg(hw, FM10K_RXINT(i));
380 	buff[idx++] = fm10k_read_reg(hw, FM10K_SRRCTL(i));
381 	buff[idx++] = fm10k_read_reg(hw, FM10K_QPRC(i));
382 	buff[idx++] = fm10k_read_reg(hw, FM10K_QPRDC(i));
383 	buff[idx++] = fm10k_read_reg(hw, FM10K_QBRC_L(i));
384 	buff[idx++] = fm10k_read_reg(hw, FM10K_QBRC_H(i));
385 	buff[idx++] = fm10k_read_reg(hw, FM10K_TDBAL(i));
386 	buff[idx++] = fm10k_read_reg(hw, FM10K_TDBAH(i));
387 	buff[idx++] = fm10k_read_reg(hw, FM10K_TDLEN(i));
388 	buff[idx++] = fm10k_read_reg(hw, FM10K_TPH_TXCTRL(i));
389 	buff[idx++] = fm10k_read_reg(hw, FM10K_TDH(i));
390 	buff[idx++] = fm10k_read_reg(hw, FM10K_TDT(i));
391 	buff[idx++] = fm10k_read_reg(hw, FM10K_TXDCTL(i));
392 	buff[idx++] = fm10k_read_reg(hw, FM10K_TXQCTL(i));
393 	buff[idx++] = fm10k_read_reg(hw, FM10K_TXINT(i));
394 	buff[idx++] = fm10k_read_reg(hw, FM10K_QPTC(i));
395 	buff[idx++] = fm10k_read_reg(hw, FM10K_QBTC_L(i));
396 	buff[idx++] = fm10k_read_reg(hw, FM10K_QBTC_H(i));
397 	buff[idx++] = fm10k_read_reg(hw, FM10K_TQDLOC(i));
398 	buff[idx++] = fm10k_read_reg(hw, FM10K_TX_SGLORT(i));
399 	buff[idx++] = fm10k_read_reg(hw, FM10K_PFVTCTL(i));
400 
401 	BUG_ON(idx != FM10K_REGS_LEN_Q);
402 }
403 
404 /* If function above adds more registers this define needs to be updated */
405 #define FM10K_REGS_LEN_VSI 43
406 
407 static void fm10k_get_reg_vsi(struct fm10k_hw *hw, u32 *buff, int i)
408 {
409 	int idx = 0, j;
410 
411 	buff[idx++] = fm10k_read_reg(hw, FM10K_MRQC(i));
412 	for (j = 0; j < 10; j++)
413 		buff[idx++] = fm10k_read_reg(hw, FM10K_RSSRK(i, j));
414 	for (j = 0; j < 32; j++)
415 		buff[idx++] = fm10k_read_reg(hw, FM10K_RETA(i, j));
416 
417 	BUG_ON(idx != FM10K_REGS_LEN_VSI);
418 }
419 
420 static void fm10k_get_regs(struct net_device *netdev,
421 			   struct ethtool_regs *regs, void *p)
422 {
423 	struct fm10k_intfc *interface = netdev_priv(netdev);
424 	struct fm10k_hw *hw = &interface->hw;
425 	u32 *buff = p;
426 	u16 i;
427 
428 	regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;
429 
430 	switch (hw->mac.type) {
431 	case fm10k_mac_pf:
432 		/* General PF Registers */
433 		*(buff++) = fm10k_read_reg(hw, FM10K_CTRL);
434 		*(buff++) = fm10k_read_reg(hw, FM10K_CTRL_EXT);
435 		*(buff++) = fm10k_read_reg(hw, FM10K_GCR);
436 		*(buff++) = fm10k_read_reg(hw, FM10K_GCR_EXT);
437 
438 		for (i = 0; i < 8; i++) {
439 			*(buff++) = fm10k_read_reg(hw, FM10K_DGLORTMAP(i));
440 			*(buff++) = fm10k_read_reg(hw, FM10K_DGLORTDEC(i));
441 		}
442 
443 		for (i = 0; i < 65; i++) {
444 			fm10k_get_reg_vsi(hw, buff, i);
445 			buff += FM10K_REGS_LEN_VSI;
446 		}
447 
448 		*(buff++) = fm10k_read_reg(hw, FM10K_DMA_CTRL);
449 		*(buff++) = fm10k_read_reg(hw, FM10K_DMA_CTRL2);
450 
451 		for (i = 0; i < FM10K_MAX_QUEUES_PF; i++) {
452 			fm10k_get_reg_q(hw, buff, i);
453 			buff += FM10K_REGS_LEN_Q;
454 		}
455 
456 		*(buff++) = fm10k_read_reg(hw, FM10K_TPH_CTRL);
457 
458 		for (i = 0; i < 8; i++)
459 			*(buff++) = fm10k_read_reg(hw, FM10K_INT_MAP(i));
460 
461 		/* Interrupt Throttling Registers */
462 		for (i = 0; i < 130; i++)
463 			*(buff++) = fm10k_read_reg(hw, FM10K_ITR(i));
464 
465 		break;
466 	case fm10k_mac_vf:
467 		/* General VF registers */
468 		*(buff++) = fm10k_read_reg(hw, FM10K_VFCTRL);
469 		*(buff++) = fm10k_read_reg(hw, FM10K_VFINT_MAP);
470 		*(buff++) = fm10k_read_reg(hw, FM10K_VFSYSTIME);
471 
472 		/* Interrupt Throttling Registers */
473 		for (i = 0; i < 8; i++)
474 			*(buff++) = fm10k_read_reg(hw, FM10K_VFITR(i));
475 
476 		fm10k_get_reg_vsi(hw, buff, 0);
477 		buff += FM10K_REGS_LEN_VSI;
478 
479 		for (i = 0; i < FM10K_MAX_QUEUES_POOL; i++) {
480 			if (i < hw->mac.max_queues)
481 				fm10k_get_reg_q(hw, buff, i);
482 			else
483 				memset(buff, 0, sizeof(u32) * FM10K_REGS_LEN_Q);
484 			buff += FM10K_REGS_LEN_Q;
485 		}
486 
487 		break;
488 	default:
489 		return;
490 	}
491 }
492 
493 /* If function above adds more registers these define need to be updated */
494 #define FM10K_REGS_LEN_PF \
495 (162 + (65 * FM10K_REGS_LEN_VSI) + (FM10K_MAX_QUEUES_PF * FM10K_REGS_LEN_Q))
496 #define FM10K_REGS_LEN_VF \
497 (11 + FM10K_REGS_LEN_VSI + (FM10K_MAX_QUEUES_POOL * FM10K_REGS_LEN_Q))
498 
499 static int fm10k_get_regs_len(struct net_device *netdev)
500 {
501 	struct fm10k_intfc *interface = netdev_priv(netdev);
502 	struct fm10k_hw *hw = &interface->hw;
503 
504 	switch (hw->mac.type) {
505 	case fm10k_mac_pf:
506 		return FM10K_REGS_LEN_PF * sizeof(u32);
507 	case fm10k_mac_vf:
508 		return FM10K_REGS_LEN_VF * sizeof(u32);
509 	default:
510 		return 0;
511 	}
512 }
513 
514 static void fm10k_get_drvinfo(struct net_device *dev,
515 			      struct ethtool_drvinfo *info)
516 {
517 	struct fm10k_intfc *interface = netdev_priv(dev);
518 
519 	strncpy(info->driver, fm10k_driver_name,
520 		sizeof(info->driver) - 1);
521 	strncpy(info->version, fm10k_driver_version,
522 		sizeof(info->version) - 1);
523 	strncpy(info->bus_info, pci_name(interface->pdev),
524 		sizeof(info->bus_info) - 1);
525 }
526 
527 static void fm10k_get_pauseparam(struct net_device *dev,
528 				 struct ethtool_pauseparam *pause)
529 {
530 	struct fm10k_intfc *interface = netdev_priv(dev);
531 
532 	/* record fixed values for autoneg and tx pause */
533 	pause->autoneg = 0;
534 	pause->tx_pause = 1;
535 
536 	pause->rx_pause = interface->rx_pause ? 1 : 0;
537 }
538 
539 static int fm10k_set_pauseparam(struct net_device *dev,
540 				struct ethtool_pauseparam *pause)
541 {
542 	struct fm10k_intfc *interface = netdev_priv(dev);
543 	struct fm10k_hw *hw = &interface->hw;
544 
545 	if (pause->autoneg || !pause->tx_pause)
546 		return -EINVAL;
547 
548 	/* we can only support pause on the PF to avoid head-of-line blocking */
549 	if (hw->mac.type == fm10k_mac_pf)
550 		interface->rx_pause = pause->rx_pause ? ~0 : 0;
551 	else if (pause->rx_pause)
552 		return -EINVAL;
553 
554 	if (netif_running(dev))
555 		fm10k_update_rx_drop_en(interface);
556 
557 	return 0;
558 }
559 
560 static u32 fm10k_get_msglevel(struct net_device *netdev)
561 {
562 	struct fm10k_intfc *interface = netdev_priv(netdev);
563 
564 	return interface->msg_enable;
565 }
566 
567 static void fm10k_set_msglevel(struct net_device *netdev, u32 data)
568 {
569 	struct fm10k_intfc *interface = netdev_priv(netdev);
570 
571 	interface->msg_enable = data;
572 }
573 
574 static void fm10k_get_ringparam(struct net_device *netdev,
575 				struct ethtool_ringparam *ring)
576 {
577 	struct fm10k_intfc *interface = netdev_priv(netdev);
578 
579 	ring->rx_max_pending = FM10K_MAX_RXD;
580 	ring->tx_max_pending = FM10K_MAX_TXD;
581 	ring->rx_mini_max_pending = 0;
582 	ring->rx_jumbo_max_pending = 0;
583 	ring->rx_pending = interface->rx_ring_count;
584 	ring->tx_pending = interface->tx_ring_count;
585 	ring->rx_mini_pending = 0;
586 	ring->rx_jumbo_pending = 0;
587 }
588 
589 static int fm10k_set_ringparam(struct net_device *netdev,
590 			       struct ethtool_ringparam *ring)
591 {
592 	struct fm10k_intfc *interface = netdev_priv(netdev);
593 	struct fm10k_ring *temp_ring;
594 	int i, err = 0;
595 	u32 new_rx_count, new_tx_count;
596 
597 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
598 		return -EINVAL;
599 
600 	new_tx_count = clamp_t(u32, ring->tx_pending,
601 			       FM10K_MIN_TXD, FM10K_MAX_TXD);
602 	new_tx_count = ALIGN(new_tx_count, FM10K_REQ_TX_DESCRIPTOR_MULTIPLE);
603 
604 	new_rx_count = clamp_t(u32, ring->rx_pending,
605 			       FM10K_MIN_RXD, FM10K_MAX_RXD);
606 	new_rx_count = ALIGN(new_rx_count, FM10K_REQ_RX_DESCRIPTOR_MULTIPLE);
607 
608 	if ((new_tx_count == interface->tx_ring_count) &&
609 	    (new_rx_count == interface->rx_ring_count)) {
610 		/* nothing to do */
611 		return 0;
612 	}
613 
614 	while (test_and_set_bit(__FM10K_RESETTING, &interface->state))
615 		usleep_range(1000, 2000);
616 
617 	if (!netif_running(interface->netdev)) {
618 		for (i = 0; i < interface->num_tx_queues; i++)
619 			interface->tx_ring[i]->count = new_tx_count;
620 		for (i = 0; i < interface->num_rx_queues; i++)
621 			interface->rx_ring[i]->count = new_rx_count;
622 		interface->tx_ring_count = new_tx_count;
623 		interface->rx_ring_count = new_rx_count;
624 		goto clear_reset;
625 	}
626 
627 	/* allocate temporary buffer to store rings in */
628 	i = max_t(int, interface->num_tx_queues, interface->num_rx_queues);
629 	temp_ring = vmalloc(i * sizeof(struct fm10k_ring));
630 
631 	if (!temp_ring) {
632 		err = -ENOMEM;
633 		goto clear_reset;
634 	}
635 
636 	fm10k_down(interface);
637 
638 	/* Setup new Tx resources and free the old Tx resources in that order.
639 	 * We can then assign the new resources to the rings via a memcpy.
640 	 * The advantage to this approach is that we are guaranteed to still
641 	 * have resources even in the case of an allocation failure.
642 	 */
643 	if (new_tx_count != interface->tx_ring_count) {
644 		for (i = 0; i < interface->num_tx_queues; i++) {
645 			memcpy(&temp_ring[i], interface->tx_ring[i],
646 			       sizeof(struct fm10k_ring));
647 
648 			temp_ring[i].count = new_tx_count;
649 			err = fm10k_setup_tx_resources(&temp_ring[i]);
650 			if (err) {
651 				while (i) {
652 					i--;
653 					fm10k_free_tx_resources(&temp_ring[i]);
654 				}
655 				goto err_setup;
656 			}
657 		}
658 
659 		for (i = 0; i < interface->num_tx_queues; i++) {
660 			fm10k_free_tx_resources(interface->tx_ring[i]);
661 
662 			memcpy(interface->tx_ring[i], &temp_ring[i],
663 			       sizeof(struct fm10k_ring));
664 		}
665 
666 		interface->tx_ring_count = new_tx_count;
667 	}
668 
669 	/* Repeat the process for the Rx rings if needed */
670 	if (new_rx_count != interface->rx_ring_count) {
671 		for (i = 0; i < interface->num_rx_queues; i++) {
672 			memcpy(&temp_ring[i], interface->rx_ring[i],
673 			       sizeof(struct fm10k_ring));
674 
675 			temp_ring[i].count = new_rx_count;
676 			err = fm10k_setup_rx_resources(&temp_ring[i]);
677 			if (err) {
678 				while (i) {
679 					i--;
680 					fm10k_free_rx_resources(&temp_ring[i]);
681 				}
682 				goto err_setup;
683 			}
684 		}
685 
686 		for (i = 0; i < interface->num_rx_queues; i++) {
687 			fm10k_free_rx_resources(interface->rx_ring[i]);
688 
689 			memcpy(interface->rx_ring[i], &temp_ring[i],
690 			       sizeof(struct fm10k_ring));
691 		}
692 
693 		interface->rx_ring_count = new_rx_count;
694 	}
695 
696 err_setup:
697 	fm10k_up(interface);
698 	vfree(temp_ring);
699 clear_reset:
700 	clear_bit(__FM10K_RESETTING, &interface->state);
701 	return err;
702 }
703 
704 static int fm10k_get_coalesce(struct net_device *dev,
705 			      struct ethtool_coalesce *ec)
706 {
707 	struct fm10k_intfc *interface = netdev_priv(dev);
708 
709 	ec->use_adaptive_tx_coalesce = ITR_IS_ADAPTIVE(interface->tx_itr);
710 	ec->tx_coalesce_usecs = interface->tx_itr & ~FM10K_ITR_ADAPTIVE;
711 
712 	ec->use_adaptive_rx_coalesce = ITR_IS_ADAPTIVE(interface->rx_itr);
713 	ec->rx_coalesce_usecs = interface->rx_itr & ~FM10K_ITR_ADAPTIVE;
714 
715 	return 0;
716 }
717 
718 static int fm10k_set_coalesce(struct net_device *dev,
719 			      struct ethtool_coalesce *ec)
720 {
721 	struct fm10k_intfc *interface = netdev_priv(dev);
722 	struct fm10k_q_vector *qv;
723 	u16 tx_itr, rx_itr;
724 	int i;
725 
726 	/* verify limits */
727 	if ((ec->rx_coalesce_usecs > FM10K_ITR_MAX) ||
728 	    (ec->tx_coalesce_usecs > FM10K_ITR_MAX))
729 		return -EINVAL;
730 
731 	/* record settings */
732 	tx_itr = ec->tx_coalesce_usecs;
733 	rx_itr = ec->rx_coalesce_usecs;
734 
735 	/* set initial values for adaptive ITR */
736 	if (ec->use_adaptive_tx_coalesce)
737 		tx_itr = FM10K_ITR_ADAPTIVE | FM10K_TX_ITR_DEFAULT;
738 
739 	if (ec->use_adaptive_rx_coalesce)
740 		rx_itr = FM10K_ITR_ADAPTIVE | FM10K_RX_ITR_DEFAULT;
741 
742 	/* update interface */
743 	interface->tx_itr = tx_itr;
744 	interface->rx_itr = rx_itr;
745 
746 	/* update q_vectors */
747 	for (i = 0; i < interface->num_q_vectors; i++) {
748 		qv = interface->q_vector[i];
749 		qv->tx.itr = tx_itr;
750 		qv->rx.itr = rx_itr;
751 	}
752 
753 	return 0;
754 }
755 
756 static int fm10k_get_rss_hash_opts(struct fm10k_intfc *interface,
757 				   struct ethtool_rxnfc *cmd)
758 {
759 	cmd->data = 0;
760 
761 	/* Report default options for RSS on fm10k */
762 	switch (cmd->flow_type) {
763 	case TCP_V4_FLOW:
764 	case TCP_V6_FLOW:
765 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
766 		/* fall through */
767 	case UDP_V4_FLOW:
768 		if (interface->flags & FM10K_FLAG_RSS_FIELD_IPV4_UDP)
769 			cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
770 		/* fall through */
771 	case SCTP_V4_FLOW:
772 	case SCTP_V6_FLOW:
773 	case AH_ESP_V4_FLOW:
774 	case AH_ESP_V6_FLOW:
775 	case AH_V4_FLOW:
776 	case AH_V6_FLOW:
777 	case ESP_V4_FLOW:
778 	case ESP_V6_FLOW:
779 	case IPV4_FLOW:
780 	case IPV6_FLOW:
781 		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
782 		break;
783 	case UDP_V6_FLOW:
784 		if (interface->flags & FM10K_FLAG_RSS_FIELD_IPV6_UDP)
785 			cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
786 		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
787 		break;
788 	default:
789 		return -EINVAL;
790 	}
791 
792 	return 0;
793 }
794 
795 static int fm10k_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
796 			   u32 __always_unused *rule_locs)
797 {
798 	struct fm10k_intfc *interface = netdev_priv(dev);
799 	int ret = -EOPNOTSUPP;
800 
801 	switch (cmd->cmd) {
802 	case ETHTOOL_GRXRINGS:
803 		cmd->data = interface->num_rx_queues;
804 		ret = 0;
805 		break;
806 	case ETHTOOL_GRXFH:
807 		ret = fm10k_get_rss_hash_opts(interface, cmd);
808 		break;
809 	default:
810 		break;
811 	}
812 
813 	return ret;
814 }
815 
816 #define UDP_RSS_FLAGS (FM10K_FLAG_RSS_FIELD_IPV4_UDP | \
817 		       FM10K_FLAG_RSS_FIELD_IPV6_UDP)
818 static int fm10k_set_rss_hash_opt(struct fm10k_intfc *interface,
819 				  struct ethtool_rxnfc *nfc)
820 {
821 	u32 flags = interface->flags;
822 
823 	/* RSS does not support anything other than hashing
824 	 * to queues on src and dst IPs and ports
825 	 */
826 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
827 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
828 		return -EINVAL;
829 
830 	switch (nfc->flow_type) {
831 	case TCP_V4_FLOW:
832 	case TCP_V6_FLOW:
833 		if (!(nfc->data & RXH_IP_SRC) ||
834 		    !(nfc->data & RXH_IP_DST) ||
835 		    !(nfc->data & RXH_L4_B_0_1) ||
836 		    !(nfc->data & RXH_L4_B_2_3))
837 			return -EINVAL;
838 		break;
839 	case UDP_V4_FLOW:
840 		if (!(nfc->data & RXH_IP_SRC) ||
841 		    !(nfc->data & RXH_IP_DST))
842 			return -EINVAL;
843 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
844 		case 0:
845 			flags &= ~FM10K_FLAG_RSS_FIELD_IPV4_UDP;
846 			break;
847 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
848 			flags |= FM10K_FLAG_RSS_FIELD_IPV4_UDP;
849 			break;
850 		default:
851 			return -EINVAL;
852 		}
853 		break;
854 	case UDP_V6_FLOW:
855 		if (!(nfc->data & RXH_IP_SRC) ||
856 		    !(nfc->data & RXH_IP_DST))
857 			return -EINVAL;
858 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
859 		case 0:
860 			flags &= ~FM10K_FLAG_RSS_FIELD_IPV6_UDP;
861 			break;
862 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
863 			flags |= FM10K_FLAG_RSS_FIELD_IPV6_UDP;
864 			break;
865 		default:
866 			return -EINVAL;
867 		}
868 		break;
869 	case AH_ESP_V4_FLOW:
870 	case AH_V4_FLOW:
871 	case ESP_V4_FLOW:
872 	case SCTP_V4_FLOW:
873 	case AH_ESP_V6_FLOW:
874 	case AH_V6_FLOW:
875 	case ESP_V6_FLOW:
876 	case SCTP_V6_FLOW:
877 		if (!(nfc->data & RXH_IP_SRC) ||
878 		    !(nfc->data & RXH_IP_DST) ||
879 		    (nfc->data & RXH_L4_B_0_1) ||
880 		    (nfc->data & RXH_L4_B_2_3))
881 			return -EINVAL;
882 		break;
883 	default:
884 		return -EINVAL;
885 	}
886 
887 	/* if we changed something we need to update flags */
888 	if (flags != interface->flags) {
889 		struct fm10k_hw *hw = &interface->hw;
890 		u32 mrqc;
891 
892 		if ((flags & UDP_RSS_FLAGS) &&
893 		    !(interface->flags & UDP_RSS_FLAGS))
894 			netif_warn(interface, drv, interface->netdev,
895 				   "enabling UDP RSS: fragmented packets may arrive out of order to the stack above\n");
896 
897 		interface->flags = flags;
898 
899 		/* Perform hash on these packet types */
900 		mrqc = FM10K_MRQC_IPV4 |
901 		       FM10K_MRQC_TCP_IPV4 |
902 		       FM10K_MRQC_IPV6 |
903 		       FM10K_MRQC_TCP_IPV6;
904 
905 		if (flags & FM10K_FLAG_RSS_FIELD_IPV4_UDP)
906 			mrqc |= FM10K_MRQC_UDP_IPV4;
907 		if (flags & FM10K_FLAG_RSS_FIELD_IPV6_UDP)
908 			mrqc |= FM10K_MRQC_UDP_IPV6;
909 
910 		fm10k_write_reg(hw, FM10K_MRQC(0), mrqc);
911 	}
912 
913 	return 0;
914 }
915 
916 static int fm10k_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
917 {
918 	struct fm10k_intfc *interface = netdev_priv(dev);
919 	int ret = -EOPNOTSUPP;
920 
921 	switch (cmd->cmd) {
922 	case ETHTOOL_SRXFH:
923 		ret = fm10k_set_rss_hash_opt(interface, cmd);
924 		break;
925 	default:
926 		break;
927 	}
928 
929 	return ret;
930 }
931 
932 static int fm10k_mbx_test(struct fm10k_intfc *interface, u64 *data)
933 {
934 	struct fm10k_hw *hw = &interface->hw;
935 	struct fm10k_mbx_info *mbx = &hw->mbx;
936 	u32 attr_flag, test_msg[6];
937 	unsigned long timeout;
938 	int err;
939 
940 	/* For now this is a VF only feature */
941 	if (hw->mac.type != fm10k_mac_vf)
942 		return 0;
943 
944 	/* loop through both nested and unnested attribute types */
945 	for (attr_flag = (1 << FM10K_TEST_MSG_UNSET);
946 	     attr_flag < (1 << (2 * FM10K_TEST_MSG_NESTED));
947 	     attr_flag += attr_flag) {
948 		/* generate message to be tested */
949 		fm10k_tlv_msg_test_create(test_msg, attr_flag);
950 
951 		fm10k_mbx_lock(interface);
952 		mbx->test_result = FM10K_NOT_IMPLEMENTED;
953 		err = mbx->ops.enqueue_tx(hw, mbx, test_msg);
954 		fm10k_mbx_unlock(interface);
955 
956 		/* wait up to 1 second for response */
957 		timeout = jiffies + HZ;
958 		do {
959 			if (err < 0)
960 				goto err_out;
961 
962 			usleep_range(500, 1000);
963 
964 			fm10k_mbx_lock(interface);
965 			mbx->ops.process(hw, mbx);
966 			fm10k_mbx_unlock(interface);
967 
968 			err = mbx->test_result;
969 			if (!err)
970 				break;
971 		} while (time_is_after_jiffies(timeout));
972 
973 		/* reporting errors */
974 		if (err)
975 			goto err_out;
976 	}
977 
978 err_out:
979 	*data = err < 0 ? (attr_flag) : (err > 0);
980 	return err;
981 }
982 
983 static void fm10k_self_test(struct net_device *dev,
984 			    struct ethtool_test *eth_test, u64 *data)
985 {
986 	struct fm10k_intfc *interface = netdev_priv(dev);
987 	struct fm10k_hw *hw = &interface->hw;
988 
989 	memset(data, 0, sizeof(*data) * FM10K_TEST_LEN);
990 
991 	if (FM10K_REMOVED(hw)) {
992 		netif_err(interface, drv, dev,
993 			  "Interface removed - test blocked\n");
994 		eth_test->flags |= ETH_TEST_FL_FAILED;
995 		return;
996 	}
997 
998 	if (fm10k_mbx_test(interface, &data[FM10K_TEST_MBX]))
999 		eth_test->flags |= ETH_TEST_FL_FAILED;
1000 }
1001 
1002 static u32 fm10k_get_priv_flags(struct net_device *netdev)
1003 {
1004 	struct fm10k_intfc *interface = netdev_priv(netdev);
1005 	u32 priv_flags = 0;
1006 
1007 	if (interface->flags & FM10K_FLAG_DEBUG_STATS)
1008 		priv_flags |= 1 << FM10K_PRV_FLAG_DEBUG_STATS;
1009 
1010 	return priv_flags;
1011 }
1012 
1013 static int fm10k_set_priv_flags(struct net_device *netdev, u32 priv_flags)
1014 {
1015 	struct fm10k_intfc *interface = netdev_priv(netdev);
1016 
1017 	if (priv_flags >= (1 << FM10K_PRV_FLAG_LEN))
1018 		return -EINVAL;
1019 
1020 	if (priv_flags & (1 << FM10K_PRV_FLAG_DEBUG_STATS))
1021 		interface->flags |= FM10K_FLAG_DEBUG_STATS;
1022 	else
1023 		interface->flags &= ~FM10K_FLAG_DEBUG_STATS;
1024 
1025 	return 0;
1026 }
1027 
1028 static u32 fm10k_get_reta_size(struct net_device __always_unused *netdev)
1029 {
1030 	return FM10K_RETA_SIZE * FM10K_RETA_ENTRIES_PER_REG;
1031 }
1032 
1033 static int fm10k_get_reta(struct net_device *netdev, u32 *indir)
1034 {
1035 	struct fm10k_intfc *interface = netdev_priv(netdev);
1036 	int i;
1037 
1038 	if (!indir)
1039 		return 0;
1040 
1041 	for (i = 0; i < FM10K_RETA_SIZE; i++, indir += 4) {
1042 		u32 reta = interface->reta[i];
1043 
1044 		indir[0] = (reta << 24) >> 24;
1045 		indir[1] = (reta << 16) >> 24;
1046 		indir[2] = (reta <<  8) >> 24;
1047 		indir[3] = (reta) >> 24;
1048 	}
1049 
1050 	return 0;
1051 }
1052 
1053 static int fm10k_set_reta(struct net_device *netdev, const u32 *indir)
1054 {
1055 	struct fm10k_intfc *interface = netdev_priv(netdev);
1056 	struct fm10k_hw *hw = &interface->hw;
1057 	int i;
1058 	u16 rss_i;
1059 
1060 	if (!indir)
1061 		return 0;
1062 
1063 	/* Verify user input. */
1064 	rss_i = interface->ring_feature[RING_F_RSS].indices;
1065 	for (i = fm10k_get_reta_size(netdev); i--;) {
1066 		if (indir[i] < rss_i)
1067 			continue;
1068 		return -EINVAL;
1069 	}
1070 
1071 	/* record entries to reta table */
1072 	for (i = 0; i < FM10K_RETA_SIZE; i++, indir += 4) {
1073 		u32 reta = indir[0] |
1074 			   (indir[1] << 8) |
1075 			   (indir[2] << 16) |
1076 			   (indir[3] << 24);
1077 
1078 		if (interface->reta[i] == reta)
1079 			continue;
1080 
1081 		interface->reta[i] = reta;
1082 		fm10k_write_reg(hw, FM10K_RETA(0, i), reta);
1083 	}
1084 
1085 	return 0;
1086 }
1087 
1088 static u32 fm10k_get_rssrk_size(struct net_device __always_unused *netdev)
1089 {
1090 	return FM10K_RSSRK_SIZE * FM10K_RSSRK_ENTRIES_PER_REG;
1091 }
1092 
1093 static int fm10k_get_rssh(struct net_device *netdev, u32 *indir, u8 *key,
1094 			  u8 *hfunc)
1095 {
1096 	struct fm10k_intfc *interface = netdev_priv(netdev);
1097 	int i, err;
1098 
1099 	if (hfunc)
1100 		*hfunc = ETH_RSS_HASH_TOP;
1101 
1102 	err = fm10k_get_reta(netdev, indir);
1103 	if (err || !key)
1104 		return err;
1105 
1106 	for (i = 0; i < FM10K_RSSRK_SIZE; i++, key += 4)
1107 		*(__le32 *)key = cpu_to_le32(interface->rssrk[i]);
1108 
1109 	return 0;
1110 }
1111 
1112 static int fm10k_set_rssh(struct net_device *netdev, const u32 *indir,
1113 			  const u8 *key, const u8 hfunc)
1114 {
1115 	struct fm10k_intfc *interface = netdev_priv(netdev);
1116 	struct fm10k_hw *hw = &interface->hw;
1117 	int i, err;
1118 
1119 	/* We do not allow change in unsupported parameters */
1120 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1121 		return -EOPNOTSUPP;
1122 
1123 	err = fm10k_set_reta(netdev, indir);
1124 	if (err || !key)
1125 		return err;
1126 
1127 	for (i = 0; i < FM10K_RSSRK_SIZE; i++, key += 4) {
1128 		u32 rssrk = le32_to_cpu(*(__le32 *)key);
1129 
1130 		if (interface->rssrk[i] == rssrk)
1131 			continue;
1132 
1133 		interface->rssrk[i] = rssrk;
1134 		fm10k_write_reg(hw, FM10K_RSSRK(0, i), rssrk);
1135 	}
1136 
1137 	return 0;
1138 }
1139 
1140 static unsigned int fm10k_max_channels(struct net_device *dev)
1141 {
1142 	struct fm10k_intfc *interface = netdev_priv(dev);
1143 	unsigned int max_combined = interface->hw.mac.max_queues;
1144 	u8 tcs = netdev_get_num_tc(dev);
1145 
1146 	/* For QoS report channels per traffic class */
1147 	if (tcs > 1)
1148 		max_combined = 1 << (fls(max_combined / tcs) - 1);
1149 
1150 	return max_combined;
1151 }
1152 
1153 static void fm10k_get_channels(struct net_device *dev,
1154 			       struct ethtool_channels *ch)
1155 {
1156 	struct fm10k_intfc *interface = netdev_priv(dev);
1157 	struct fm10k_hw *hw = &interface->hw;
1158 
1159 	/* report maximum channels */
1160 	ch->max_combined = fm10k_max_channels(dev);
1161 
1162 	/* report info for other vector */
1163 	ch->max_other = NON_Q_VECTORS(hw);
1164 	ch->other_count = ch->max_other;
1165 
1166 	/* record RSS queues */
1167 	ch->combined_count = interface->ring_feature[RING_F_RSS].indices;
1168 }
1169 
1170 static int fm10k_set_channels(struct net_device *dev,
1171 			      struct ethtool_channels *ch)
1172 {
1173 	struct fm10k_intfc *interface = netdev_priv(dev);
1174 	unsigned int count = ch->combined_count;
1175 	struct fm10k_hw *hw = &interface->hw;
1176 
1177 	/* verify they are not requesting separate vectors */
1178 	if (!count || ch->rx_count || ch->tx_count)
1179 		return -EINVAL;
1180 
1181 	/* verify other_count has not changed */
1182 	if (ch->other_count != NON_Q_VECTORS(hw))
1183 		return -EINVAL;
1184 
1185 	/* verify the number of channels does not exceed hardware limits */
1186 	if (count > fm10k_max_channels(dev))
1187 		return -EINVAL;
1188 
1189 	interface->ring_feature[RING_F_RSS].limit = count;
1190 
1191 	/* use setup TC to update any traffic class queue mapping */
1192 	return fm10k_setup_tc(dev, netdev_get_num_tc(dev));
1193 }
1194 
1195 static int fm10k_get_ts_info(struct net_device *dev,
1196 			     struct ethtool_ts_info *info)
1197 {
1198 	struct fm10k_intfc *interface = netdev_priv(dev);
1199 
1200 	info->so_timestamping =
1201 		SOF_TIMESTAMPING_TX_SOFTWARE |
1202 		SOF_TIMESTAMPING_RX_SOFTWARE |
1203 		SOF_TIMESTAMPING_SOFTWARE |
1204 		SOF_TIMESTAMPING_TX_HARDWARE |
1205 		SOF_TIMESTAMPING_RX_HARDWARE |
1206 		SOF_TIMESTAMPING_RAW_HARDWARE;
1207 
1208 	if (interface->ptp_clock)
1209 		info->phc_index = ptp_clock_index(interface->ptp_clock);
1210 	else
1211 		info->phc_index = -1;
1212 
1213 	info->tx_types = (1 << HWTSTAMP_TX_OFF) |
1214 			 (1 << HWTSTAMP_TX_ON);
1215 
1216 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1217 			   (1 << HWTSTAMP_FILTER_ALL);
1218 
1219 	return 0;
1220 }
1221 
1222 static const struct ethtool_ops fm10k_ethtool_ops = {
1223 	.get_strings		= fm10k_get_strings,
1224 	.get_sset_count		= fm10k_get_sset_count,
1225 	.get_ethtool_stats      = fm10k_get_ethtool_stats,
1226 	.get_drvinfo		= fm10k_get_drvinfo,
1227 	.get_link		= ethtool_op_get_link,
1228 	.get_pauseparam		= fm10k_get_pauseparam,
1229 	.set_pauseparam		= fm10k_set_pauseparam,
1230 	.get_msglevel		= fm10k_get_msglevel,
1231 	.set_msglevel		= fm10k_set_msglevel,
1232 	.get_ringparam		= fm10k_get_ringparam,
1233 	.set_ringparam		= fm10k_set_ringparam,
1234 	.get_coalesce		= fm10k_get_coalesce,
1235 	.set_coalesce		= fm10k_set_coalesce,
1236 	.get_rxnfc		= fm10k_get_rxnfc,
1237 	.set_rxnfc		= fm10k_set_rxnfc,
1238 	.get_regs               = fm10k_get_regs,
1239 	.get_regs_len           = fm10k_get_regs_len,
1240 	.self_test		= fm10k_self_test,
1241 	.get_priv_flags		= fm10k_get_priv_flags,
1242 	.set_priv_flags		= fm10k_set_priv_flags,
1243 	.get_rxfh_indir_size	= fm10k_get_reta_size,
1244 	.get_rxfh_key_size	= fm10k_get_rssrk_size,
1245 	.get_rxfh		= fm10k_get_rssh,
1246 	.set_rxfh		= fm10k_set_rssh,
1247 	.get_channels		= fm10k_get_channels,
1248 	.set_channels		= fm10k_set_channels,
1249 	.get_ts_info            = fm10k_get_ts_info,
1250 };
1251 
1252 void fm10k_set_ethtool_ops(struct net_device *dev)
1253 {
1254 	dev->ethtool_ops = &fm10k_ethtool_ops;
1255 }
1256