xref: /openbmc/linux/drivers/net/ethernet/intel/i40e/i40e_ethtool.c (revision dd2934a95701576203b2f61e8ded4e4a2f9183ea)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 /* ethtool support for i40e */
5 
6 #include "i40e.h"
7 #include "i40e_diag.h"
8 
9 /* ethtool statistics helpers */
10 
11 /**
12  * struct i40e_stats - definition for an ethtool statistic
13  * @stat_string: statistic name to display in ethtool -S output
14  * @sizeof_stat: the sizeof() the stat, must be no greater than sizeof(u64)
15  * @stat_offset: offsetof() the stat from a base pointer
16  *
17  * This structure defines a statistic to be added to the ethtool stats buffer.
18  * It defines a statistic as offset from a common base pointer. Stats should
19  * be defined in constant arrays using the I40E_STAT macro, with every element
20  * of the array using the same _type for calculating the sizeof_stat and
21  * stat_offset.
22  *
23  * The @sizeof_stat is expected to be sizeof(u8), sizeof(u16), sizeof(u32) or
24  * sizeof(u64). Other sizes are not expected and will produce a WARN_ONCE from
25  * the i40e_add_ethtool_stat() helper function.
26  *
27  * The @stat_string is interpreted as a format string, allowing formatted
28  * values to be inserted while looping over multiple structures for a given
29  * statistics array. Thus, every statistic string in an array should have the
30  * same type and number of format specifiers, to be formatted by variadic
31  * arguments to the i40e_add_stat_string() helper function.
32  **/
33 struct i40e_stats {
34 	char stat_string[ETH_GSTRING_LEN];
35 	int sizeof_stat;
36 	int stat_offset;
37 };
38 
39 /* Helper macro to define an i40e_stat structure with proper size and type.
40  * Use this when defining constant statistics arrays. Note that @_type expects
41  * only a type name and is used multiple times.
42  */
43 #define I40E_STAT(_type, _name, _stat) { \
44 	.stat_string = _name, \
45 	.sizeof_stat = FIELD_SIZEOF(_type, _stat), \
46 	.stat_offset = offsetof(_type, _stat) \
47 }
48 
49 /* Helper macro for defining some statistics directly copied from the netdev
50  * stats structure.
51  */
52 #define I40E_NETDEV_STAT(_net_stat) \
53 	I40E_STAT(struct rtnl_link_stats64, #_net_stat, _net_stat)
54 
55 /* Helper macro for defining some statistics related to queues */
56 #define I40E_QUEUE_STAT(_name, _stat) \
57 	I40E_STAT(struct i40e_ring, _name, _stat)
58 
59 /* Stats associated with a Tx or Rx ring */
60 static const struct i40e_stats i40e_gstrings_queue_stats[] = {
61 	I40E_QUEUE_STAT("%s-%u.packets", stats.packets),
62 	I40E_QUEUE_STAT("%s-%u.bytes", stats.bytes),
63 };
64 
65 /**
66  * i40e_add_one_ethtool_stat - copy the stat into the supplied buffer
67  * @data: location to store the stat value
68  * @pointer: basis for where to copy from
69  * @stat: the stat definition
70  *
71  * Copies the stat data defined by the pointer and stat structure pair into
72  * the memory supplied as data. Used to implement i40e_add_ethtool_stats and
73  * i40e_add_queue_stats. If the pointer is null, data will be zero'd.
74  */
75 static void
76 i40e_add_one_ethtool_stat(u64 *data, void *pointer,
77 			  const struct i40e_stats *stat)
78 {
79 	char *p;
80 
81 	if (!pointer) {
82 		/* ensure that the ethtool data buffer is zero'd for any stats
83 		 * which don't have a valid pointer.
84 		 */
85 		*data = 0;
86 		return;
87 	}
88 
89 	p = (char *)pointer + stat->stat_offset;
90 	switch (stat->sizeof_stat) {
91 	case sizeof(u64):
92 		*data = *((u64 *)p);
93 		break;
94 	case sizeof(u32):
95 		*data = *((u32 *)p);
96 		break;
97 	case sizeof(u16):
98 		*data = *((u16 *)p);
99 		break;
100 	case sizeof(u8):
101 		*data = *((u8 *)p);
102 		break;
103 	default:
104 		WARN_ONCE(1, "unexpected stat size for %s",
105 			  stat->stat_string);
106 		*data = 0;
107 	}
108 }
109 
110 /**
111  * __i40e_add_ethtool_stats - copy stats into the ethtool supplied buffer
112  * @data: ethtool stats buffer
113  * @pointer: location to copy stats from
114  * @stats: array of stats to copy
115  * @size: the size of the stats definition
116  *
117  * Copy the stats defined by the stats array using the pointer as a base into
118  * the data buffer supplied by ethtool. Updates the data pointer to point to
119  * the next empty location for successive calls to __i40e_add_ethtool_stats.
120  * If pointer is null, set the data values to zero and update the pointer to
121  * skip these stats.
122  **/
123 static void
124 __i40e_add_ethtool_stats(u64 **data, void *pointer,
125 			 const struct i40e_stats stats[],
126 			 const unsigned int size)
127 {
128 	unsigned int i;
129 
130 	for (i = 0; i < size; i++)
131 		i40e_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
132 }
133 
134 /**
135  * i40e_add_ethtool_stats - copy stats into ethtool supplied buffer
136  * @data: ethtool stats buffer
137  * @pointer: location where stats are stored
138  * @stats: static const array of stat definitions
139  *
140  * Macro to ease the use of __i40e_add_ethtool_stats by taking a static
141  * constant stats array and passing the ARRAY_SIZE(). This avoids typos by
142  * ensuring that we pass the size associated with the given stats array.
143  *
144  * The parameter @stats is evaluated twice, so parameters with side effects
145  * should be avoided.
146  **/
147 #define i40e_add_ethtool_stats(data, pointer, stats) \
148 	__i40e_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
149 
150 /**
151  * i40e_add_queue_stats - copy queue statistics into supplied buffer
152  * @data: ethtool stats buffer
153  * @ring: the ring to copy
154  *
155  * Queue statistics must be copied while protected by
156  * u64_stats_fetch_begin_irq, so we can't directly use i40e_add_ethtool_stats.
157  * Assumes that queue stats are defined in i40e_gstrings_queue_stats. If the
158  * ring pointer is null, zero out the queue stat values and update the data
159  * pointer. Otherwise safely copy the stats from the ring into the supplied
160  * buffer and update the data pointer when finished.
161  *
162  * This function expects to be called while under rcu_read_lock().
163  **/
164 static void
165 i40e_add_queue_stats(u64 **data, struct i40e_ring *ring)
166 {
167 	const unsigned int size = ARRAY_SIZE(i40e_gstrings_queue_stats);
168 	const struct i40e_stats *stats = i40e_gstrings_queue_stats;
169 	unsigned int start;
170 	unsigned int i;
171 
172 	/* To avoid invalid statistics values, ensure that we keep retrying
173 	 * the copy until we get a consistent value according to
174 	 * u64_stats_fetch_retry_irq. But first, make sure our ring is
175 	 * non-null before attempting to access its syncp.
176 	 */
177 	do {
178 		start = !ring ? 0 : u64_stats_fetch_begin_irq(&ring->syncp);
179 		for (i = 0; i < size; i++) {
180 			i40e_add_one_ethtool_stat(&(*data)[i], ring,
181 						  &stats[i]);
182 		}
183 	} while (ring && u64_stats_fetch_retry_irq(&ring->syncp, start));
184 
185 	/* Once we successfully copy the stats in, update the data pointer */
186 	*data += size;
187 }
188 
189 /**
190  * __i40e_add_stat_strings - copy stat strings into ethtool buffer
191  * @p: ethtool supplied buffer
192  * @stats: stat definitions array
193  * @size: size of the stats array
194  *
195  * Format and copy the strings described by stats into the buffer pointed at
196  * by p.
197  **/
198 static void __i40e_add_stat_strings(u8 **p, const struct i40e_stats stats[],
199 				    const unsigned int size, ...)
200 {
201 	unsigned int i;
202 
203 	for (i = 0; i < size; i++) {
204 		va_list args;
205 
206 		va_start(args, size);
207 		vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
208 		*p += ETH_GSTRING_LEN;
209 		va_end(args);
210 	}
211 }
212 
213 /**
214  * 40e_add_stat_strings - copy stat strings into ethtool buffer
215  * @p: ethtool supplied buffer
216  * @stats: stat definitions array
217  *
218  * Format and copy the strings described by the const static stats value into
219  * the buffer pointed at by p.
220  *
221  * The parameter @stats is evaluated twice, so parameters with side effects
222  * should be avoided. Additionally, stats must be an array such that
223  * ARRAY_SIZE can be called on it.
224  **/
225 #define i40e_add_stat_strings(p, stats, ...) \
226 	__i40e_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
227 
228 #define I40E_PF_STAT(_name, _stat) \
229 	I40E_STAT(struct i40e_pf, _name, _stat)
230 #define I40E_VSI_STAT(_name, _stat) \
231 	I40E_STAT(struct i40e_vsi, _name, _stat)
232 #define I40E_VEB_STAT(_name, _stat) \
233 	I40E_STAT(struct i40e_veb, _name, _stat)
234 #define I40E_PFC_STAT(_name, _stat) \
235 	I40E_STAT(struct i40e_pfc_stats, _name, _stat)
236 #define I40E_QUEUE_STAT(_name, _stat) \
237 	I40E_STAT(struct i40e_ring, _name, _stat)
238 
239 static const struct i40e_stats i40e_gstrings_net_stats[] = {
240 	I40E_NETDEV_STAT(rx_packets),
241 	I40E_NETDEV_STAT(tx_packets),
242 	I40E_NETDEV_STAT(rx_bytes),
243 	I40E_NETDEV_STAT(tx_bytes),
244 	I40E_NETDEV_STAT(rx_errors),
245 	I40E_NETDEV_STAT(tx_errors),
246 	I40E_NETDEV_STAT(rx_dropped),
247 	I40E_NETDEV_STAT(tx_dropped),
248 	I40E_NETDEV_STAT(collisions),
249 	I40E_NETDEV_STAT(rx_length_errors),
250 	I40E_NETDEV_STAT(rx_crc_errors),
251 };
252 
253 static const struct i40e_stats i40e_gstrings_veb_stats[] = {
254 	I40E_VEB_STAT("veb.rx_bytes", stats.rx_bytes),
255 	I40E_VEB_STAT("veb.tx_bytes", stats.tx_bytes),
256 	I40E_VEB_STAT("veb.rx_unicast", stats.rx_unicast),
257 	I40E_VEB_STAT("veb.tx_unicast", stats.tx_unicast),
258 	I40E_VEB_STAT("veb.rx_multicast", stats.rx_multicast),
259 	I40E_VEB_STAT("veb.tx_multicast", stats.tx_multicast),
260 	I40E_VEB_STAT("veb.rx_broadcast", stats.rx_broadcast),
261 	I40E_VEB_STAT("veb.tx_broadcast", stats.tx_broadcast),
262 	I40E_VEB_STAT("veb.rx_discards", stats.rx_discards),
263 	I40E_VEB_STAT("veb.tx_discards", stats.tx_discards),
264 	I40E_VEB_STAT("veb.tx_errors", stats.tx_errors),
265 	I40E_VEB_STAT("veb.rx_unknown_protocol", stats.rx_unknown_protocol),
266 };
267 
268 static const struct i40e_stats i40e_gstrings_veb_tc_stats[] = {
269 	I40E_VEB_STAT("veb.tc_%u_tx_packets", tc_stats.tc_tx_packets),
270 	I40E_VEB_STAT("veb.tc_%u_tx_bytes", tc_stats.tc_tx_bytes),
271 	I40E_VEB_STAT("veb.tc_%u_rx_packets", tc_stats.tc_rx_packets),
272 	I40E_VEB_STAT("veb.tc_%u_rx_bytes", tc_stats.tc_rx_bytes),
273 };
274 
275 static const struct i40e_stats i40e_gstrings_misc_stats[] = {
276 	I40E_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
277 	I40E_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
278 	I40E_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
279 	I40E_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
280 	I40E_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
281 	I40E_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
282 	I40E_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
283 	I40E_VSI_STAT("tx_linearize", tx_linearize),
284 	I40E_VSI_STAT("tx_force_wb", tx_force_wb),
285 	I40E_VSI_STAT("tx_busy", tx_busy),
286 	I40E_VSI_STAT("rx_alloc_fail", rx_buf_failed),
287 	I40E_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
288 };
289 
290 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
291  * but they are separate.  This device supports Virtualization, and
292  * as such might have several netdevs supporting VMDq and FCoE going
293  * through a single port.  The NETDEV_STATs are for individual netdevs
294  * seen at the top of the stack, and the PF_STATs are for the physical
295  * function at the bottom of the stack hosting those netdevs.
296  *
297  * The PF_STATs are appended to the netdev stats only when ethtool -S
298  * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
299  */
300 static const struct i40e_stats i40e_gstrings_stats[] = {
301 	I40E_PF_STAT("port.rx_bytes", stats.eth.rx_bytes),
302 	I40E_PF_STAT("port.tx_bytes", stats.eth.tx_bytes),
303 	I40E_PF_STAT("port.rx_unicast", stats.eth.rx_unicast),
304 	I40E_PF_STAT("port.tx_unicast", stats.eth.tx_unicast),
305 	I40E_PF_STAT("port.rx_multicast", stats.eth.rx_multicast),
306 	I40E_PF_STAT("port.tx_multicast", stats.eth.tx_multicast),
307 	I40E_PF_STAT("port.rx_broadcast", stats.eth.rx_broadcast),
308 	I40E_PF_STAT("port.tx_broadcast", stats.eth.tx_broadcast),
309 	I40E_PF_STAT("port.tx_errors", stats.eth.tx_errors),
310 	I40E_PF_STAT("port.rx_dropped", stats.eth.rx_discards),
311 	I40E_PF_STAT("port.tx_dropped_link_down", stats.tx_dropped_link_down),
312 	I40E_PF_STAT("port.rx_crc_errors", stats.crc_errors),
313 	I40E_PF_STAT("port.illegal_bytes", stats.illegal_bytes),
314 	I40E_PF_STAT("port.mac_local_faults", stats.mac_local_faults),
315 	I40E_PF_STAT("port.mac_remote_faults", stats.mac_remote_faults),
316 	I40E_PF_STAT("port.tx_timeout", tx_timeout_count),
317 	I40E_PF_STAT("port.rx_csum_bad", hw_csum_rx_error),
318 	I40E_PF_STAT("port.rx_length_errors", stats.rx_length_errors),
319 	I40E_PF_STAT("port.link_xon_rx", stats.link_xon_rx),
320 	I40E_PF_STAT("port.link_xoff_rx", stats.link_xoff_rx),
321 	I40E_PF_STAT("port.link_xon_tx", stats.link_xon_tx),
322 	I40E_PF_STAT("port.link_xoff_tx", stats.link_xoff_tx),
323 	I40E_PF_STAT("port.rx_size_64", stats.rx_size_64),
324 	I40E_PF_STAT("port.rx_size_127", stats.rx_size_127),
325 	I40E_PF_STAT("port.rx_size_255", stats.rx_size_255),
326 	I40E_PF_STAT("port.rx_size_511", stats.rx_size_511),
327 	I40E_PF_STAT("port.rx_size_1023", stats.rx_size_1023),
328 	I40E_PF_STAT("port.rx_size_1522", stats.rx_size_1522),
329 	I40E_PF_STAT("port.rx_size_big", stats.rx_size_big),
330 	I40E_PF_STAT("port.tx_size_64", stats.tx_size_64),
331 	I40E_PF_STAT("port.tx_size_127", stats.tx_size_127),
332 	I40E_PF_STAT("port.tx_size_255", stats.tx_size_255),
333 	I40E_PF_STAT("port.tx_size_511", stats.tx_size_511),
334 	I40E_PF_STAT("port.tx_size_1023", stats.tx_size_1023),
335 	I40E_PF_STAT("port.tx_size_1522", stats.tx_size_1522),
336 	I40E_PF_STAT("port.tx_size_big", stats.tx_size_big),
337 	I40E_PF_STAT("port.rx_undersize", stats.rx_undersize),
338 	I40E_PF_STAT("port.rx_fragments", stats.rx_fragments),
339 	I40E_PF_STAT("port.rx_oversize", stats.rx_oversize),
340 	I40E_PF_STAT("port.rx_jabber", stats.rx_jabber),
341 	I40E_PF_STAT("port.VF_admin_queue_requests", vf_aq_requests),
342 	I40E_PF_STAT("port.arq_overflows", arq_overflows),
343 	I40E_PF_STAT("port.tx_hwtstamp_timeouts", tx_hwtstamp_timeouts),
344 	I40E_PF_STAT("port.rx_hwtstamp_cleared", rx_hwtstamp_cleared),
345 	I40E_PF_STAT("port.tx_hwtstamp_skipped", tx_hwtstamp_skipped),
346 	I40E_PF_STAT("port.fdir_flush_cnt", fd_flush_cnt),
347 	I40E_PF_STAT("port.fdir_atr_match", stats.fd_atr_match),
348 	I40E_PF_STAT("port.fdir_atr_tunnel_match", stats.fd_atr_tunnel_match),
349 	I40E_PF_STAT("port.fdir_atr_status", stats.fd_atr_status),
350 	I40E_PF_STAT("port.fdir_sb_match", stats.fd_sb_match),
351 	I40E_PF_STAT("port.fdir_sb_status", stats.fd_sb_status),
352 
353 	/* LPI stats */
354 	I40E_PF_STAT("port.tx_lpi_status", stats.tx_lpi_status),
355 	I40E_PF_STAT("port.rx_lpi_status", stats.rx_lpi_status),
356 	I40E_PF_STAT("port.tx_lpi_count", stats.tx_lpi_count),
357 	I40E_PF_STAT("port.rx_lpi_count", stats.rx_lpi_count),
358 };
359 
360 struct i40e_pfc_stats {
361 	u64 priority_xon_rx;
362 	u64 priority_xoff_rx;
363 	u64 priority_xon_tx;
364 	u64 priority_xoff_tx;
365 	u64 priority_xon_2_xoff;
366 };
367 
368 static const struct i40e_stats i40e_gstrings_pfc_stats[] = {
369 	I40E_PFC_STAT("port.tx_priority_%u_xon_tx", priority_xon_tx),
370 	I40E_PFC_STAT("port.tx_priority_%u_xoff_tx", priority_xoff_tx),
371 	I40E_PFC_STAT("port.rx_priority_%u_xon_rx", priority_xon_rx),
372 	I40E_PFC_STAT("port.rx_priority_%u_xoff_rx", priority_xoff_rx),
373 	I40E_PFC_STAT("port.rx_priority_%u_xon_2_xoff", priority_xon_2_xoff),
374 };
375 
376 #define I40E_NETDEV_STATS_LEN	ARRAY_SIZE(i40e_gstrings_net_stats)
377 
378 #define I40E_MISC_STATS_LEN	ARRAY_SIZE(i40e_gstrings_misc_stats)
379 
380 #define I40E_VSI_STATS_LEN	(I40E_NETDEV_STATS_LEN + I40E_MISC_STATS_LEN)
381 
382 #define I40E_PFC_STATS_LEN	(ARRAY_SIZE(i40e_gstrings_pfc_stats) * \
383 				 I40E_MAX_USER_PRIORITY)
384 
385 #define I40E_VEB_STATS_LEN	(ARRAY_SIZE(i40e_gstrings_veb_stats) + \
386 				 (ARRAY_SIZE(i40e_gstrings_veb_tc_stats) * \
387 				  I40E_MAX_TRAFFIC_CLASS))
388 
389 #define I40E_GLOBAL_STATS_LEN	ARRAY_SIZE(i40e_gstrings_stats)
390 
391 #define I40E_PF_STATS_LEN	(I40E_GLOBAL_STATS_LEN + \
392 				 I40E_PFC_STATS_LEN + \
393 				 I40E_VEB_STATS_LEN + \
394 				 I40E_VSI_STATS_LEN)
395 
396 /* Length of stats for a single queue */
397 #define I40E_QUEUE_STATS_LEN	ARRAY_SIZE(i40e_gstrings_queue_stats)
398 
399 enum i40e_ethtool_test_id {
400 	I40E_ETH_TEST_REG = 0,
401 	I40E_ETH_TEST_EEPROM,
402 	I40E_ETH_TEST_INTR,
403 	I40E_ETH_TEST_LINK,
404 };
405 
406 static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
407 	"Register test  (offline)",
408 	"Eeprom test    (offline)",
409 	"Interrupt test (offline)",
410 	"Link test   (on/offline)"
411 };
412 
413 #define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
414 
415 struct i40e_priv_flags {
416 	char flag_string[ETH_GSTRING_LEN];
417 	u64 flag;
418 	bool read_only;
419 };
420 
421 #define I40E_PRIV_FLAG(_name, _flag, _read_only) { \
422 	.flag_string = _name, \
423 	.flag = _flag, \
424 	.read_only = _read_only, \
425 }
426 
427 static const struct i40e_priv_flags i40e_gstrings_priv_flags[] = {
428 	/* NOTE: MFP setting cannot be changed */
429 	I40E_PRIV_FLAG("MFP", I40E_FLAG_MFP_ENABLED, 1),
430 	I40E_PRIV_FLAG("LinkPolling", I40E_FLAG_LINK_POLLING_ENABLED, 0),
431 	I40E_PRIV_FLAG("flow-director-atr", I40E_FLAG_FD_ATR_ENABLED, 0),
432 	I40E_PRIV_FLAG("veb-stats", I40E_FLAG_VEB_STATS_ENABLED, 0),
433 	I40E_PRIV_FLAG("hw-atr-eviction", I40E_FLAG_HW_ATR_EVICT_ENABLED, 0),
434 	I40E_PRIV_FLAG("link-down-on-close",
435 		       I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED, 0),
436 	I40E_PRIV_FLAG("legacy-rx", I40E_FLAG_LEGACY_RX, 0),
437 	I40E_PRIV_FLAG("disable-source-pruning",
438 		       I40E_FLAG_SOURCE_PRUNING_DISABLED, 0),
439 	I40E_PRIV_FLAG("disable-fw-lldp", I40E_FLAG_DISABLE_FW_LLDP, 0),
440 };
441 
442 #define I40E_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_gstrings_priv_flags)
443 
444 /* Private flags with a global effect, restricted to PF 0 */
445 static const struct i40e_priv_flags i40e_gl_gstrings_priv_flags[] = {
446 	I40E_PRIV_FLAG("vf-true-promisc-support",
447 		       I40E_FLAG_TRUE_PROMISC_SUPPORT, 0),
448 };
449 
450 #define I40E_GL_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_gl_gstrings_priv_flags)
451 
452 /**
453  * i40e_partition_setting_complaint - generic complaint for MFP restriction
454  * @pf: the PF struct
455  **/
456 static void i40e_partition_setting_complaint(struct i40e_pf *pf)
457 {
458 	dev_info(&pf->pdev->dev,
459 		 "The link settings are allowed to be changed only from the first partition of a given port. Please switch to the first partition in order to change the setting.\n");
460 }
461 
462 /**
463  * i40e_phy_type_to_ethtool - convert the phy_types to ethtool link modes
464  * @pf: PF struct with phy_types
465  * @ks: ethtool link ksettings struct to fill out
466  *
467  **/
468 static void i40e_phy_type_to_ethtool(struct i40e_pf *pf,
469 				     struct ethtool_link_ksettings *ks)
470 {
471 	struct i40e_link_status *hw_link_info = &pf->hw.phy.link_info;
472 	u64 phy_types = pf->hw.phy.phy_types;
473 
474 	ethtool_link_ksettings_zero_link_mode(ks, supported);
475 	ethtool_link_ksettings_zero_link_mode(ks, advertising);
476 
477 	if (phy_types & I40E_CAP_PHY_TYPE_SGMII) {
478 		ethtool_link_ksettings_add_link_mode(ks, supported,
479 						     1000baseT_Full);
480 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
481 			ethtool_link_ksettings_add_link_mode(ks, advertising,
482 							     1000baseT_Full);
483 		if (pf->hw_features & I40E_HW_100M_SGMII_CAPABLE) {
484 			ethtool_link_ksettings_add_link_mode(ks, supported,
485 							     100baseT_Full);
486 			ethtool_link_ksettings_add_link_mode(ks, advertising,
487 							     100baseT_Full);
488 		}
489 	}
490 	if (phy_types & I40E_CAP_PHY_TYPE_XAUI ||
491 	    phy_types & I40E_CAP_PHY_TYPE_XFI ||
492 	    phy_types & I40E_CAP_PHY_TYPE_SFI ||
493 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_SFPP_CU ||
494 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_AOC) {
495 		ethtool_link_ksettings_add_link_mode(ks, supported,
496 						     10000baseT_Full);
497 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
498 			ethtool_link_ksettings_add_link_mode(ks, advertising,
499 							     10000baseT_Full);
500 	}
501 	if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_T) {
502 		ethtool_link_ksettings_add_link_mode(ks, supported,
503 						     10000baseT_Full);
504 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
505 			ethtool_link_ksettings_add_link_mode(ks, advertising,
506 							     10000baseT_Full);
507 	}
508 	if (phy_types & I40E_CAP_PHY_TYPE_XLAUI ||
509 	    phy_types & I40E_CAP_PHY_TYPE_XLPPI ||
510 	    phy_types & I40E_CAP_PHY_TYPE_40GBASE_AOC)
511 		ethtool_link_ksettings_add_link_mode(ks, supported,
512 						     40000baseCR4_Full);
513 	if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
514 	    phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4) {
515 		ethtool_link_ksettings_add_link_mode(ks, supported,
516 						     40000baseCR4_Full);
517 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_40GB)
518 			ethtool_link_ksettings_add_link_mode(ks, advertising,
519 							     40000baseCR4_Full);
520 	}
521 	if (phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) {
522 		ethtool_link_ksettings_add_link_mode(ks, supported,
523 						     100baseT_Full);
524 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
525 			ethtool_link_ksettings_add_link_mode(ks, advertising,
526 							     100baseT_Full);
527 	}
528 	if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_T) {
529 		ethtool_link_ksettings_add_link_mode(ks, supported,
530 						     1000baseT_Full);
531 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
532 			ethtool_link_ksettings_add_link_mode(ks, advertising,
533 							     1000baseT_Full);
534 	}
535 	if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_SR4)
536 		ethtool_link_ksettings_add_link_mode(ks, supported,
537 						     40000baseSR4_Full);
538 	if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_LR4)
539 		ethtool_link_ksettings_add_link_mode(ks, supported,
540 						     40000baseLR4_Full);
541 	if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4) {
542 		ethtool_link_ksettings_add_link_mode(ks, supported,
543 						     40000baseLR4_Full);
544 		ethtool_link_ksettings_add_link_mode(ks, advertising,
545 						     40000baseLR4_Full);
546 	}
547 	if (phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2) {
548 		ethtool_link_ksettings_add_link_mode(ks, supported,
549 						     20000baseKR2_Full);
550 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_20GB)
551 			ethtool_link_ksettings_add_link_mode(ks, advertising,
552 							     20000baseKR2_Full);
553 	}
554 	if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4) {
555 		ethtool_link_ksettings_add_link_mode(ks, supported,
556 						     10000baseKX4_Full);
557 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
558 			ethtool_link_ksettings_add_link_mode(ks, advertising,
559 							     10000baseKX4_Full);
560 	}
561 	if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR &&
562 	    !(pf->hw_features & I40E_HW_HAVE_CRT_RETIMER)) {
563 		ethtool_link_ksettings_add_link_mode(ks, supported,
564 						     10000baseKR_Full);
565 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
566 			ethtool_link_ksettings_add_link_mode(ks, advertising,
567 							     10000baseKR_Full);
568 	}
569 	if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX &&
570 	    !(pf->hw_features & I40E_HW_HAVE_CRT_RETIMER)) {
571 		ethtool_link_ksettings_add_link_mode(ks, supported,
572 						     1000baseKX_Full);
573 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
574 			ethtool_link_ksettings_add_link_mode(ks, advertising,
575 							     1000baseKX_Full);
576 	}
577 	/* need to add 25G PHY types */
578 	if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR) {
579 		ethtool_link_ksettings_add_link_mode(ks, supported,
580 						     25000baseKR_Full);
581 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
582 			ethtool_link_ksettings_add_link_mode(ks, advertising,
583 							     25000baseKR_Full);
584 	}
585 	if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR) {
586 		ethtool_link_ksettings_add_link_mode(ks, supported,
587 						     25000baseCR_Full);
588 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
589 			ethtool_link_ksettings_add_link_mode(ks, advertising,
590 							     25000baseCR_Full);
591 	}
592 	if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
593 	    phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR) {
594 		ethtool_link_ksettings_add_link_mode(ks, supported,
595 						     25000baseSR_Full);
596 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
597 			ethtool_link_ksettings_add_link_mode(ks, advertising,
598 							     25000baseSR_Full);
599 	}
600 	if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_AOC ||
601 	    phy_types & I40E_CAP_PHY_TYPE_25GBASE_ACC) {
602 		ethtool_link_ksettings_add_link_mode(ks, supported,
603 						     25000baseCR_Full);
604 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
605 			ethtool_link_ksettings_add_link_mode(ks, advertising,
606 							     25000baseCR_Full);
607 	}
608 	/* need to add new 10G PHY types */
609 	if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
610 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU) {
611 		ethtool_link_ksettings_add_link_mode(ks, supported,
612 						     10000baseCR_Full);
613 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
614 			ethtool_link_ksettings_add_link_mode(ks, advertising,
615 							     10000baseCR_Full);
616 	}
617 	if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR) {
618 		ethtool_link_ksettings_add_link_mode(ks, supported,
619 						     10000baseSR_Full);
620 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
621 			ethtool_link_ksettings_add_link_mode(ks, advertising,
622 							     10000baseSR_Full);
623 	}
624 	if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR) {
625 		ethtool_link_ksettings_add_link_mode(ks, supported,
626 						     10000baseLR_Full);
627 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
628 			ethtool_link_ksettings_add_link_mode(ks, advertising,
629 							     10000baseLR_Full);
630 	}
631 	if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
632 	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
633 	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL) {
634 		ethtool_link_ksettings_add_link_mode(ks, supported,
635 						     1000baseX_Full);
636 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
637 			ethtool_link_ksettings_add_link_mode(ks, advertising,
638 							     1000baseX_Full);
639 	}
640 	/* Autoneg PHY types */
641 	if (phy_types & I40E_CAP_PHY_TYPE_SGMII ||
642 	    phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4 ||
643 	    phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
644 	    phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4 ||
645 	    phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
646 	    phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR ||
647 	    phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR ||
648 	    phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR ||
649 	    phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2 ||
650 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_T ||
651 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR ||
652 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR ||
653 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4 ||
654 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR ||
655 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU ||
656 	    phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
657 	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL ||
658 	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_T ||
659 	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
660 	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
661 	    phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX ||
662 	    phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) {
663 		ethtool_link_ksettings_add_link_mode(ks, supported,
664 						     Autoneg);
665 		ethtool_link_ksettings_add_link_mode(ks, advertising,
666 						     Autoneg);
667 	}
668 }
669 
670 /**
671  * i40e_get_settings_link_up - Get the Link settings for when link is up
672  * @hw: hw structure
673  * @ks: ethtool ksettings to fill in
674  * @netdev: network interface device structure
675  * @pf: pointer to physical function struct
676  **/
677 static void i40e_get_settings_link_up(struct i40e_hw *hw,
678 				      struct ethtool_link_ksettings *ks,
679 				      struct net_device *netdev,
680 				      struct i40e_pf *pf)
681 {
682 	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
683 	struct ethtool_link_ksettings cap_ksettings;
684 	u32 link_speed = hw_link_info->link_speed;
685 
686 	/* Initialize supported and advertised settings based on phy settings */
687 	switch (hw_link_info->phy_type) {
688 	case I40E_PHY_TYPE_40GBASE_CR4:
689 	case I40E_PHY_TYPE_40GBASE_CR4_CU:
690 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
691 		ethtool_link_ksettings_add_link_mode(ks, supported,
692 						     40000baseCR4_Full);
693 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
694 		ethtool_link_ksettings_add_link_mode(ks, advertising,
695 						     40000baseCR4_Full);
696 		break;
697 	case I40E_PHY_TYPE_XLAUI:
698 	case I40E_PHY_TYPE_XLPPI:
699 	case I40E_PHY_TYPE_40GBASE_AOC:
700 		ethtool_link_ksettings_add_link_mode(ks, supported,
701 						     40000baseCR4_Full);
702 		break;
703 	case I40E_PHY_TYPE_40GBASE_SR4:
704 		ethtool_link_ksettings_add_link_mode(ks, supported,
705 						     40000baseSR4_Full);
706 		break;
707 	case I40E_PHY_TYPE_40GBASE_LR4:
708 		ethtool_link_ksettings_add_link_mode(ks, supported,
709 						     40000baseLR4_Full);
710 		break;
711 	case I40E_PHY_TYPE_25GBASE_SR:
712 	case I40E_PHY_TYPE_25GBASE_LR:
713 	case I40E_PHY_TYPE_10GBASE_SR:
714 	case I40E_PHY_TYPE_10GBASE_LR:
715 	case I40E_PHY_TYPE_1000BASE_SX:
716 	case I40E_PHY_TYPE_1000BASE_LX:
717 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
718 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
719 		ethtool_link_ksettings_add_link_mode(ks, supported,
720 						     25000baseSR_Full);
721 		ethtool_link_ksettings_add_link_mode(ks, advertising,
722 						     25000baseSR_Full);
723 		ethtool_link_ksettings_add_link_mode(ks, supported,
724 						     10000baseSR_Full);
725 		ethtool_link_ksettings_add_link_mode(ks, advertising,
726 						     10000baseSR_Full);
727 		ethtool_link_ksettings_add_link_mode(ks, supported,
728 						     10000baseLR_Full);
729 		ethtool_link_ksettings_add_link_mode(ks, advertising,
730 						     10000baseLR_Full);
731 		ethtool_link_ksettings_add_link_mode(ks, supported,
732 						     1000baseX_Full);
733 		ethtool_link_ksettings_add_link_mode(ks, advertising,
734 						     1000baseX_Full);
735 		ethtool_link_ksettings_add_link_mode(ks, supported,
736 						     10000baseT_Full);
737 		if (hw_link_info->module_type[2] &
738 		    I40E_MODULE_TYPE_1000BASE_SX ||
739 		    hw_link_info->module_type[2] &
740 		    I40E_MODULE_TYPE_1000BASE_LX) {
741 			ethtool_link_ksettings_add_link_mode(ks, supported,
742 							     1000baseT_Full);
743 			if (hw_link_info->requested_speeds &
744 			    I40E_LINK_SPEED_1GB)
745 				ethtool_link_ksettings_add_link_mode(
746 				     ks, advertising, 1000baseT_Full);
747 		}
748 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
749 			ethtool_link_ksettings_add_link_mode(ks, advertising,
750 							     10000baseT_Full);
751 		break;
752 	case I40E_PHY_TYPE_10GBASE_T:
753 	case I40E_PHY_TYPE_1000BASE_T:
754 	case I40E_PHY_TYPE_100BASE_TX:
755 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
756 		ethtool_link_ksettings_add_link_mode(ks, supported,
757 						     10000baseT_Full);
758 		ethtool_link_ksettings_add_link_mode(ks, supported,
759 						     1000baseT_Full);
760 		ethtool_link_ksettings_add_link_mode(ks, supported,
761 						     100baseT_Full);
762 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
763 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
764 			ethtool_link_ksettings_add_link_mode(ks, advertising,
765 							     10000baseT_Full);
766 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
767 			ethtool_link_ksettings_add_link_mode(ks, advertising,
768 							     1000baseT_Full);
769 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
770 			ethtool_link_ksettings_add_link_mode(ks, advertising,
771 							     100baseT_Full);
772 		break;
773 	case I40E_PHY_TYPE_1000BASE_T_OPTICAL:
774 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
775 		ethtool_link_ksettings_add_link_mode(ks, supported,
776 						     1000baseT_Full);
777 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
778 		ethtool_link_ksettings_add_link_mode(ks, advertising,
779 						     1000baseT_Full);
780 		break;
781 	case I40E_PHY_TYPE_10GBASE_CR1_CU:
782 	case I40E_PHY_TYPE_10GBASE_CR1:
783 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
784 		ethtool_link_ksettings_add_link_mode(ks, supported,
785 						     10000baseT_Full);
786 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
787 		ethtool_link_ksettings_add_link_mode(ks, advertising,
788 						     10000baseT_Full);
789 		break;
790 	case I40E_PHY_TYPE_XAUI:
791 	case I40E_PHY_TYPE_XFI:
792 	case I40E_PHY_TYPE_SFI:
793 	case I40E_PHY_TYPE_10GBASE_SFPP_CU:
794 	case I40E_PHY_TYPE_10GBASE_AOC:
795 		ethtool_link_ksettings_add_link_mode(ks, supported,
796 						     10000baseT_Full);
797 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
798 			ethtool_link_ksettings_add_link_mode(ks, advertising,
799 							     10000baseT_Full);
800 		break;
801 	case I40E_PHY_TYPE_SGMII:
802 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
803 		ethtool_link_ksettings_add_link_mode(ks, supported,
804 						     1000baseT_Full);
805 		if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
806 			ethtool_link_ksettings_add_link_mode(ks, advertising,
807 							     1000baseT_Full);
808 		if (pf->hw_features & I40E_HW_100M_SGMII_CAPABLE) {
809 			ethtool_link_ksettings_add_link_mode(ks, supported,
810 							     100baseT_Full);
811 			if (hw_link_info->requested_speeds &
812 			    I40E_LINK_SPEED_100MB)
813 				ethtool_link_ksettings_add_link_mode(
814 				      ks, advertising, 100baseT_Full);
815 		}
816 		break;
817 	case I40E_PHY_TYPE_40GBASE_KR4:
818 	case I40E_PHY_TYPE_25GBASE_KR:
819 	case I40E_PHY_TYPE_20GBASE_KR2:
820 	case I40E_PHY_TYPE_10GBASE_KR:
821 	case I40E_PHY_TYPE_10GBASE_KX4:
822 	case I40E_PHY_TYPE_1000BASE_KX:
823 		ethtool_link_ksettings_add_link_mode(ks, supported,
824 						     40000baseKR4_Full);
825 		ethtool_link_ksettings_add_link_mode(ks, supported,
826 						     25000baseKR_Full);
827 		ethtool_link_ksettings_add_link_mode(ks, supported,
828 						     20000baseKR2_Full);
829 		ethtool_link_ksettings_add_link_mode(ks, supported,
830 						     10000baseKR_Full);
831 		ethtool_link_ksettings_add_link_mode(ks, supported,
832 						     10000baseKX4_Full);
833 		ethtool_link_ksettings_add_link_mode(ks, supported,
834 						     1000baseKX_Full);
835 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
836 		ethtool_link_ksettings_add_link_mode(ks, advertising,
837 						     40000baseKR4_Full);
838 		ethtool_link_ksettings_add_link_mode(ks, advertising,
839 						     25000baseKR_Full);
840 		ethtool_link_ksettings_add_link_mode(ks, advertising,
841 						     20000baseKR2_Full);
842 		ethtool_link_ksettings_add_link_mode(ks, advertising,
843 						     10000baseKR_Full);
844 		ethtool_link_ksettings_add_link_mode(ks, advertising,
845 						     10000baseKX4_Full);
846 		ethtool_link_ksettings_add_link_mode(ks, advertising,
847 						     1000baseKX_Full);
848 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
849 		break;
850 	case I40E_PHY_TYPE_25GBASE_CR:
851 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
852 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
853 		ethtool_link_ksettings_add_link_mode(ks, supported,
854 						     25000baseCR_Full);
855 		ethtool_link_ksettings_add_link_mode(ks, advertising,
856 						     25000baseCR_Full);
857 		break;
858 	case I40E_PHY_TYPE_25GBASE_AOC:
859 	case I40E_PHY_TYPE_25GBASE_ACC:
860 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
861 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
862 		ethtool_link_ksettings_add_link_mode(ks, supported,
863 						     25000baseCR_Full);
864 
865 		ethtool_link_ksettings_add_link_mode(ks, advertising,
866 						     25000baseCR_Full);
867 		ethtool_link_ksettings_add_link_mode(ks, supported,
868 						     10000baseCR_Full);
869 		ethtool_link_ksettings_add_link_mode(ks, advertising,
870 						     10000baseCR_Full);
871 		break;
872 	default:
873 		/* if we got here and link is up something bad is afoot */
874 		netdev_info(netdev,
875 			    "WARNING: Link is up but PHY type 0x%x is not recognized.\n",
876 			    hw_link_info->phy_type);
877 	}
878 
879 	/* Now that we've worked out everything that could be supported by the
880 	 * current PHY type, get what is supported by the NVM and intersect
881 	 * them to get what is truly supported
882 	 */
883 	memset(&cap_ksettings, 0, sizeof(struct ethtool_link_ksettings));
884 	i40e_phy_type_to_ethtool(pf, &cap_ksettings);
885 	ethtool_intersect_link_masks(ks, &cap_ksettings);
886 
887 	/* Set speed and duplex */
888 	switch (link_speed) {
889 	case I40E_LINK_SPEED_40GB:
890 		ks->base.speed = SPEED_40000;
891 		break;
892 	case I40E_LINK_SPEED_25GB:
893 		ks->base.speed = SPEED_25000;
894 		break;
895 	case I40E_LINK_SPEED_20GB:
896 		ks->base.speed = SPEED_20000;
897 		break;
898 	case I40E_LINK_SPEED_10GB:
899 		ks->base.speed = SPEED_10000;
900 		break;
901 	case I40E_LINK_SPEED_1GB:
902 		ks->base.speed = SPEED_1000;
903 		break;
904 	case I40E_LINK_SPEED_100MB:
905 		ks->base.speed = SPEED_100;
906 		break;
907 	default:
908 		break;
909 	}
910 	ks->base.duplex = DUPLEX_FULL;
911 }
912 
913 /**
914  * i40e_get_settings_link_down - Get the Link settings for when link is down
915  * @hw: hw structure
916  * @ks: ethtool ksettings to fill in
917  * @pf: pointer to physical function struct
918  *
919  * Reports link settings that can be determined when link is down
920  **/
921 static void i40e_get_settings_link_down(struct i40e_hw *hw,
922 					struct ethtool_link_ksettings *ks,
923 					struct i40e_pf *pf)
924 {
925 	/* link is down and the driver needs to fall back on
926 	 * supported phy types to figure out what info to display
927 	 */
928 	i40e_phy_type_to_ethtool(pf, ks);
929 
930 	/* With no link speed and duplex are unknown */
931 	ks->base.speed = SPEED_UNKNOWN;
932 	ks->base.duplex = DUPLEX_UNKNOWN;
933 }
934 
935 /**
936  * i40e_get_link_ksettings - Get Link Speed and Duplex settings
937  * @netdev: network interface device structure
938  * @ks: ethtool ksettings
939  *
940  * Reports speed/duplex settings based on media_type
941  **/
942 static int i40e_get_link_ksettings(struct net_device *netdev,
943 				   struct ethtool_link_ksettings *ks)
944 {
945 	struct i40e_netdev_priv *np = netdev_priv(netdev);
946 	struct i40e_pf *pf = np->vsi->back;
947 	struct i40e_hw *hw = &pf->hw;
948 	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
949 	bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
950 
951 	ethtool_link_ksettings_zero_link_mode(ks, supported);
952 	ethtool_link_ksettings_zero_link_mode(ks, advertising);
953 
954 	if (link_up)
955 		i40e_get_settings_link_up(hw, ks, netdev, pf);
956 	else
957 		i40e_get_settings_link_down(hw, ks, pf);
958 
959 	/* Now set the settings that don't rely on link being up/down */
960 	/* Set autoneg settings */
961 	ks->base.autoneg = ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
962 			    AUTONEG_ENABLE : AUTONEG_DISABLE);
963 
964 	/* Set media type settings */
965 	switch (hw->phy.media_type) {
966 	case I40E_MEDIA_TYPE_BACKPLANE:
967 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
968 		ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
969 		ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
970 		ethtool_link_ksettings_add_link_mode(ks, advertising,
971 						     Backplane);
972 		ks->base.port = PORT_NONE;
973 		break;
974 	case I40E_MEDIA_TYPE_BASET:
975 		ethtool_link_ksettings_add_link_mode(ks, supported, TP);
976 		ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
977 		ks->base.port = PORT_TP;
978 		break;
979 	case I40E_MEDIA_TYPE_DA:
980 	case I40E_MEDIA_TYPE_CX4:
981 		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
982 		ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
983 		ks->base.port = PORT_DA;
984 		break;
985 	case I40E_MEDIA_TYPE_FIBER:
986 		ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
987 		ks->base.port = PORT_FIBRE;
988 		break;
989 	case I40E_MEDIA_TYPE_UNKNOWN:
990 	default:
991 		ks->base.port = PORT_OTHER;
992 		break;
993 	}
994 
995 	/* Set flow control settings */
996 	ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
997 
998 	switch (hw->fc.requested_mode) {
999 	case I40E_FC_FULL:
1000 		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1001 		break;
1002 	case I40E_FC_TX_PAUSE:
1003 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1004 						     Asym_Pause);
1005 		break;
1006 	case I40E_FC_RX_PAUSE:
1007 		ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
1008 		ethtool_link_ksettings_add_link_mode(ks, advertising,
1009 						     Asym_Pause);
1010 		break;
1011 	default:
1012 		ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
1013 		ethtool_link_ksettings_del_link_mode(ks, advertising,
1014 						     Asym_Pause);
1015 		break;
1016 	}
1017 
1018 	return 0;
1019 }
1020 
1021 /**
1022  * i40e_set_link_ksettings - Set Speed and Duplex
1023  * @netdev: network interface device structure
1024  * @ks: ethtool ksettings
1025  *
1026  * Set speed/duplex per media_types advertised/forced
1027  **/
1028 static int i40e_set_link_ksettings(struct net_device *netdev,
1029 				   const struct ethtool_link_ksettings *ks)
1030 {
1031 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1032 	struct i40e_aq_get_phy_abilities_resp abilities;
1033 	struct ethtool_link_ksettings safe_ks;
1034 	struct ethtool_link_ksettings copy_ks;
1035 	struct i40e_aq_set_phy_config config;
1036 	struct i40e_pf *pf = np->vsi->back;
1037 	struct i40e_vsi *vsi = np->vsi;
1038 	struct i40e_hw *hw = &pf->hw;
1039 	bool autoneg_changed = false;
1040 	i40e_status status = 0;
1041 	int timeout = 50;
1042 	int err = 0;
1043 	u8 autoneg;
1044 
1045 	/* Changing port settings is not supported if this isn't the
1046 	 * port's controlling PF
1047 	 */
1048 	if (hw->partition_id != 1) {
1049 		i40e_partition_setting_complaint(pf);
1050 		return -EOPNOTSUPP;
1051 	}
1052 	if (vsi != pf->vsi[pf->lan_vsi])
1053 		return -EOPNOTSUPP;
1054 	if (hw->phy.media_type != I40E_MEDIA_TYPE_BASET &&
1055 	    hw->phy.media_type != I40E_MEDIA_TYPE_FIBER &&
1056 	    hw->phy.media_type != I40E_MEDIA_TYPE_BACKPLANE &&
1057 	    hw->phy.media_type != I40E_MEDIA_TYPE_DA &&
1058 	    hw->phy.link_info.link_info & I40E_AQ_LINK_UP)
1059 		return -EOPNOTSUPP;
1060 	if (hw->device_id == I40E_DEV_ID_KX_B ||
1061 	    hw->device_id == I40E_DEV_ID_KX_C ||
1062 	    hw->device_id == I40E_DEV_ID_20G_KR2 ||
1063 	    hw->device_id == I40E_DEV_ID_20G_KR2_A ||
1064 	    hw->device_id == I40E_DEV_ID_25G_B ||
1065 	    hw->device_id == I40E_DEV_ID_KX_X722) {
1066 		netdev_info(netdev, "Changing settings is not supported on backplane.\n");
1067 		return -EOPNOTSUPP;
1068 	}
1069 
1070 	/* copy the ksettings to copy_ks to avoid modifying the origin */
1071 	memcpy(&copy_ks, ks, sizeof(struct ethtool_link_ksettings));
1072 
1073 	/* save autoneg out of ksettings */
1074 	autoneg = copy_ks.base.autoneg;
1075 
1076 	/* get our own copy of the bits to check against */
1077 	memset(&safe_ks, 0, sizeof(struct ethtool_link_ksettings));
1078 	safe_ks.base.cmd = copy_ks.base.cmd;
1079 	safe_ks.base.link_mode_masks_nwords =
1080 		copy_ks.base.link_mode_masks_nwords;
1081 	i40e_get_link_ksettings(netdev, &safe_ks);
1082 
1083 	/* Get link modes supported by hardware and check against modes
1084 	 * requested by the user.  Return an error if unsupported mode was set.
1085 	 */
1086 	if (!bitmap_subset(copy_ks.link_modes.advertising,
1087 			   safe_ks.link_modes.supported,
1088 			   __ETHTOOL_LINK_MODE_MASK_NBITS))
1089 		return -EINVAL;
1090 
1091 	/* set autoneg back to what it currently is */
1092 	copy_ks.base.autoneg = safe_ks.base.autoneg;
1093 
1094 	/* If copy_ks.base and safe_ks.base are not the same now, then they are
1095 	 * trying to set something that we do not support.
1096 	 */
1097 	if (memcmp(&copy_ks.base, &safe_ks.base,
1098 		   sizeof(struct ethtool_link_settings)))
1099 		return -EOPNOTSUPP;
1100 
1101 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
1102 		timeout--;
1103 		if (!timeout)
1104 			return -EBUSY;
1105 		usleep_range(1000, 2000);
1106 	}
1107 
1108 	/* Get the current phy config */
1109 	status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
1110 					      NULL);
1111 	if (status) {
1112 		err = -EAGAIN;
1113 		goto done;
1114 	}
1115 
1116 	/* Copy abilities to config in case autoneg is not
1117 	 * set below
1118 	 */
1119 	memset(&config, 0, sizeof(struct i40e_aq_set_phy_config));
1120 	config.abilities = abilities.abilities;
1121 
1122 	/* Check autoneg */
1123 	if (autoneg == AUTONEG_ENABLE) {
1124 		/* If autoneg was not already enabled */
1125 		if (!(hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED)) {
1126 			/* If autoneg is not supported, return error */
1127 			if (!ethtool_link_ksettings_test_link_mode(&safe_ks,
1128 								   supported,
1129 								   Autoneg)) {
1130 				netdev_info(netdev, "Autoneg not supported on this phy\n");
1131 				err = -EINVAL;
1132 				goto done;
1133 			}
1134 			/* Autoneg is allowed to change */
1135 			config.abilities = abilities.abilities |
1136 					   I40E_AQ_PHY_ENABLE_AN;
1137 			autoneg_changed = true;
1138 		}
1139 	} else {
1140 		/* If autoneg is currently enabled */
1141 		if (hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED) {
1142 			/* If autoneg is supported 10GBASE_T is the only PHY
1143 			 * that can disable it, so otherwise return error
1144 			 */
1145 			if (ethtool_link_ksettings_test_link_mode(&safe_ks,
1146 								  supported,
1147 								  Autoneg) &&
1148 			    hw->phy.link_info.phy_type !=
1149 			    I40E_PHY_TYPE_10GBASE_T) {
1150 				netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
1151 				err = -EINVAL;
1152 				goto done;
1153 			}
1154 			/* Autoneg is allowed to change */
1155 			config.abilities = abilities.abilities &
1156 					   ~I40E_AQ_PHY_ENABLE_AN;
1157 			autoneg_changed = true;
1158 		}
1159 	}
1160 
1161 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1162 						  100baseT_Full))
1163 		config.link_speed |= I40E_LINK_SPEED_100MB;
1164 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1165 						  1000baseT_Full) ||
1166 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1167 						  1000baseX_Full) ||
1168 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1169 						  1000baseKX_Full))
1170 		config.link_speed |= I40E_LINK_SPEED_1GB;
1171 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1172 						  10000baseT_Full) ||
1173 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1174 						  10000baseKX4_Full) ||
1175 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1176 						  10000baseKR_Full) ||
1177 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1178 						  10000baseCR_Full) ||
1179 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1180 						  10000baseSR_Full) ||
1181 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1182 						  10000baseLR_Full))
1183 		config.link_speed |= I40E_LINK_SPEED_10GB;
1184 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1185 						  20000baseKR2_Full))
1186 		config.link_speed |= I40E_LINK_SPEED_20GB;
1187 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1188 						  25000baseCR_Full) ||
1189 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1190 						  25000baseKR_Full) ||
1191 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1192 						  25000baseSR_Full))
1193 		config.link_speed |= I40E_LINK_SPEED_25GB;
1194 	if (ethtool_link_ksettings_test_link_mode(ks, advertising,
1195 						  40000baseKR4_Full) ||
1196 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1197 						  40000baseCR4_Full) ||
1198 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1199 						  40000baseSR4_Full) ||
1200 	    ethtool_link_ksettings_test_link_mode(ks, advertising,
1201 						  40000baseLR4_Full))
1202 		config.link_speed |= I40E_LINK_SPEED_40GB;
1203 
1204 	/* If speed didn't get set, set it to what it currently is.
1205 	 * This is needed because if advertise is 0 (as it is when autoneg
1206 	 * is disabled) then speed won't get set.
1207 	 */
1208 	if (!config.link_speed)
1209 		config.link_speed = abilities.link_speed;
1210 	if (autoneg_changed || abilities.link_speed != config.link_speed) {
1211 		/* copy over the rest of the abilities */
1212 		config.phy_type = abilities.phy_type;
1213 		config.phy_type_ext = abilities.phy_type_ext;
1214 		config.eee_capability = abilities.eee_capability;
1215 		config.eeer = abilities.eeer_val;
1216 		config.low_power_ctrl = abilities.d3_lpan;
1217 		config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
1218 				    I40E_AQ_PHY_FEC_CONFIG_MASK;
1219 
1220 		/* save the requested speeds */
1221 		hw->phy.link_info.requested_speeds = config.link_speed;
1222 		/* set link and auto negotiation so changes take effect */
1223 		config.abilities |= I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
1224 		/* If link is up put link down */
1225 		if (hw->phy.link_info.link_info & I40E_AQ_LINK_UP) {
1226 			/* Tell the OS link is going down, the link will go
1227 			 * back up when fw says it is ready asynchronously
1228 			 */
1229 			i40e_print_link_message(vsi, false);
1230 			netif_carrier_off(netdev);
1231 			netif_tx_stop_all_queues(netdev);
1232 		}
1233 
1234 		/* make the aq call */
1235 		status = i40e_aq_set_phy_config(hw, &config, NULL);
1236 		if (status) {
1237 			netdev_info(netdev,
1238 				    "Set phy config failed, err %s aq_err %s\n",
1239 				    i40e_stat_str(hw, status),
1240 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1241 			err = -EAGAIN;
1242 			goto done;
1243 		}
1244 
1245 		status = i40e_update_link_info(hw);
1246 		if (status)
1247 			netdev_dbg(netdev,
1248 				   "Updating link info failed with err %s aq_err %s\n",
1249 				   i40e_stat_str(hw, status),
1250 				   i40e_aq_str(hw, hw->aq.asq_last_status));
1251 
1252 	} else {
1253 		netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
1254 	}
1255 
1256 done:
1257 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
1258 
1259 	return err;
1260 }
1261 
1262 static int i40e_nway_reset(struct net_device *netdev)
1263 {
1264 	/* restart autonegotiation */
1265 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1266 	struct i40e_pf *pf = np->vsi->back;
1267 	struct i40e_hw *hw = &pf->hw;
1268 	bool link_up = hw->phy.link_info.link_info & I40E_AQ_LINK_UP;
1269 	i40e_status ret = 0;
1270 
1271 	ret = i40e_aq_set_link_restart_an(hw, link_up, NULL);
1272 	if (ret) {
1273 		netdev_info(netdev, "link restart failed, err %s aq_err %s\n",
1274 			    i40e_stat_str(hw, ret),
1275 			    i40e_aq_str(hw, hw->aq.asq_last_status));
1276 		return -EIO;
1277 	}
1278 
1279 	return 0;
1280 }
1281 
1282 /**
1283  * i40e_get_pauseparam -  Get Flow Control status
1284  * @netdev: netdevice structure
1285  * @pause: buffer to return pause parameters
1286  *
1287  * Return tx/rx-pause status
1288  **/
1289 static void i40e_get_pauseparam(struct net_device *netdev,
1290 				struct ethtool_pauseparam *pause)
1291 {
1292 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1293 	struct i40e_pf *pf = np->vsi->back;
1294 	struct i40e_hw *hw = &pf->hw;
1295 	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
1296 	struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
1297 
1298 	pause->autoneg =
1299 		((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
1300 		  AUTONEG_ENABLE : AUTONEG_DISABLE);
1301 
1302 	/* PFC enabled so report LFC as off */
1303 	if (dcbx_cfg->pfc.pfcenable) {
1304 		pause->rx_pause = 0;
1305 		pause->tx_pause = 0;
1306 		return;
1307 	}
1308 
1309 	if (hw->fc.current_mode == I40E_FC_RX_PAUSE) {
1310 		pause->rx_pause = 1;
1311 	} else if (hw->fc.current_mode == I40E_FC_TX_PAUSE) {
1312 		pause->tx_pause = 1;
1313 	} else if (hw->fc.current_mode == I40E_FC_FULL) {
1314 		pause->rx_pause = 1;
1315 		pause->tx_pause = 1;
1316 	}
1317 }
1318 
1319 /**
1320  * i40e_set_pauseparam - Set Flow Control parameter
1321  * @netdev: network interface device structure
1322  * @pause: return tx/rx flow control status
1323  **/
1324 static int i40e_set_pauseparam(struct net_device *netdev,
1325 			       struct ethtool_pauseparam *pause)
1326 {
1327 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1328 	struct i40e_pf *pf = np->vsi->back;
1329 	struct i40e_vsi *vsi = np->vsi;
1330 	struct i40e_hw *hw = &pf->hw;
1331 	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
1332 	struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
1333 	bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
1334 	i40e_status status;
1335 	u8 aq_failures;
1336 	int err = 0;
1337 
1338 	/* Changing the port's flow control is not supported if this isn't the
1339 	 * port's controlling PF
1340 	 */
1341 	if (hw->partition_id != 1) {
1342 		i40e_partition_setting_complaint(pf);
1343 		return -EOPNOTSUPP;
1344 	}
1345 
1346 	if (vsi != pf->vsi[pf->lan_vsi])
1347 		return -EOPNOTSUPP;
1348 
1349 	if (pause->autoneg != ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
1350 	    AUTONEG_ENABLE : AUTONEG_DISABLE)) {
1351 		netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
1352 		return -EOPNOTSUPP;
1353 	}
1354 
1355 	/* If we have link and don't have autoneg */
1356 	if (!test_bit(__I40E_DOWN, pf->state) &&
1357 	    !(hw_link_info->an_info & I40E_AQ_AN_COMPLETED)) {
1358 		/* Send message that it might not necessarily work*/
1359 		netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
1360 	}
1361 
1362 	if (dcbx_cfg->pfc.pfcenable) {
1363 		netdev_info(netdev,
1364 			    "Priority flow control enabled. Cannot set link flow control.\n");
1365 		return -EOPNOTSUPP;
1366 	}
1367 
1368 	if (pause->rx_pause && pause->tx_pause)
1369 		hw->fc.requested_mode = I40E_FC_FULL;
1370 	else if (pause->rx_pause && !pause->tx_pause)
1371 		hw->fc.requested_mode = I40E_FC_RX_PAUSE;
1372 	else if (!pause->rx_pause && pause->tx_pause)
1373 		hw->fc.requested_mode = I40E_FC_TX_PAUSE;
1374 	else if (!pause->rx_pause && !pause->tx_pause)
1375 		hw->fc.requested_mode = I40E_FC_NONE;
1376 	else
1377 		 return -EINVAL;
1378 
1379 	/* Tell the OS link is going down, the link will go back up when fw
1380 	 * says it is ready asynchronously
1381 	 */
1382 	i40e_print_link_message(vsi, false);
1383 	netif_carrier_off(netdev);
1384 	netif_tx_stop_all_queues(netdev);
1385 
1386 	/* Set the fc mode and only restart an if link is up*/
1387 	status = i40e_set_fc(hw, &aq_failures, link_up);
1388 
1389 	if (aq_failures & I40E_SET_FC_AQ_FAIL_GET) {
1390 		netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %s aq_err %s\n",
1391 			    i40e_stat_str(hw, status),
1392 			    i40e_aq_str(hw, hw->aq.asq_last_status));
1393 		err = -EAGAIN;
1394 	}
1395 	if (aq_failures & I40E_SET_FC_AQ_FAIL_SET) {
1396 		netdev_info(netdev, "Set fc failed on the set_phy_config call with err %s aq_err %s\n",
1397 			    i40e_stat_str(hw, status),
1398 			    i40e_aq_str(hw, hw->aq.asq_last_status));
1399 		err = -EAGAIN;
1400 	}
1401 	if (aq_failures & I40E_SET_FC_AQ_FAIL_UPDATE) {
1402 		netdev_info(netdev, "Set fc failed on the get_link_info call with err %s aq_err %s\n",
1403 			    i40e_stat_str(hw, status),
1404 			    i40e_aq_str(hw, hw->aq.asq_last_status));
1405 		err = -EAGAIN;
1406 	}
1407 
1408 	if (!test_bit(__I40E_DOWN, pf->state)) {
1409 		/* Give it a little more time to try to come back */
1410 		msleep(75);
1411 		if (!test_bit(__I40E_DOWN, pf->state))
1412 			return i40e_nway_reset(netdev);
1413 	}
1414 
1415 	return err;
1416 }
1417 
1418 static u32 i40e_get_msglevel(struct net_device *netdev)
1419 {
1420 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1421 	struct i40e_pf *pf = np->vsi->back;
1422 	u32 debug_mask = pf->hw.debug_mask;
1423 
1424 	if (debug_mask)
1425 		netdev_info(netdev, "i40e debug_mask: 0x%08X\n", debug_mask);
1426 
1427 	return pf->msg_enable;
1428 }
1429 
1430 static void i40e_set_msglevel(struct net_device *netdev, u32 data)
1431 {
1432 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1433 	struct i40e_pf *pf = np->vsi->back;
1434 
1435 	if (I40E_DEBUG_USER & data)
1436 		pf->hw.debug_mask = data;
1437 	else
1438 		pf->msg_enable = data;
1439 }
1440 
1441 static int i40e_get_regs_len(struct net_device *netdev)
1442 {
1443 	int reg_count = 0;
1444 	int i;
1445 
1446 	for (i = 0; i40e_reg_list[i].offset != 0; i++)
1447 		reg_count += i40e_reg_list[i].elements;
1448 
1449 	return reg_count * sizeof(u32);
1450 }
1451 
1452 static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
1453 			  void *p)
1454 {
1455 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1456 	struct i40e_pf *pf = np->vsi->back;
1457 	struct i40e_hw *hw = &pf->hw;
1458 	u32 *reg_buf = p;
1459 	unsigned int i, j, ri;
1460 	u32 reg;
1461 
1462 	/* Tell ethtool which driver-version-specific regs output we have.
1463 	 *
1464 	 * At some point, if we have ethtool doing special formatting of
1465 	 * this data, it will rely on this version number to know how to
1466 	 * interpret things.  Hence, this needs to be updated if/when the
1467 	 * diags register table is changed.
1468 	 */
1469 	regs->version = 1;
1470 
1471 	/* loop through the diags reg table for what to print */
1472 	ri = 0;
1473 	for (i = 0; i40e_reg_list[i].offset != 0; i++) {
1474 		for (j = 0; j < i40e_reg_list[i].elements; j++) {
1475 			reg = i40e_reg_list[i].offset
1476 				+ (j * i40e_reg_list[i].stride);
1477 			reg_buf[ri++] = rd32(hw, reg);
1478 		}
1479 	}
1480 
1481 }
1482 
1483 static int i40e_get_eeprom(struct net_device *netdev,
1484 			   struct ethtool_eeprom *eeprom, u8 *bytes)
1485 {
1486 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1487 	struct i40e_hw *hw = &np->vsi->back->hw;
1488 	struct i40e_pf *pf = np->vsi->back;
1489 	int ret_val = 0, len, offset;
1490 	u8 *eeprom_buff;
1491 	u16 i, sectors;
1492 	bool last;
1493 	u32 magic;
1494 
1495 #define I40E_NVM_SECTOR_SIZE  4096
1496 	if (eeprom->len == 0)
1497 		return -EINVAL;
1498 
1499 	/* check for NVMUpdate access method */
1500 	magic = hw->vendor_id | (hw->device_id << 16);
1501 	if (eeprom->magic && eeprom->magic != magic) {
1502 		struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1503 		int errno = 0;
1504 
1505 		/* make sure it is the right magic for NVMUpdate */
1506 		if ((eeprom->magic >> 16) != hw->device_id)
1507 			errno = -EINVAL;
1508 		else if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
1509 			 test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
1510 			errno = -EBUSY;
1511 		else
1512 			ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1513 
1514 		if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1515 			dev_info(&pf->pdev->dev,
1516 				 "NVMUpdate read failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1517 				 ret_val, hw->aq.asq_last_status, errno,
1518 				 (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1519 				 cmd->offset, cmd->data_size);
1520 
1521 		return errno;
1522 	}
1523 
1524 	/* normal ethtool get_eeprom support */
1525 	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
1526 
1527 	eeprom_buff = kzalloc(eeprom->len, GFP_KERNEL);
1528 	if (!eeprom_buff)
1529 		return -ENOMEM;
1530 
1531 	ret_val = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
1532 	if (ret_val) {
1533 		dev_info(&pf->pdev->dev,
1534 			 "Failed Acquiring NVM resource for read err=%d status=0x%x\n",
1535 			 ret_val, hw->aq.asq_last_status);
1536 		goto free_buff;
1537 	}
1538 
1539 	sectors = eeprom->len / I40E_NVM_SECTOR_SIZE;
1540 	sectors += (eeprom->len % I40E_NVM_SECTOR_SIZE) ? 1 : 0;
1541 	len = I40E_NVM_SECTOR_SIZE;
1542 	last = false;
1543 	for (i = 0; i < sectors; i++) {
1544 		if (i == (sectors - 1)) {
1545 			len = eeprom->len - (I40E_NVM_SECTOR_SIZE * i);
1546 			last = true;
1547 		}
1548 		offset = eeprom->offset + (I40E_NVM_SECTOR_SIZE * i),
1549 		ret_val = i40e_aq_read_nvm(hw, 0x0, offset, len,
1550 				(u8 *)eeprom_buff + (I40E_NVM_SECTOR_SIZE * i),
1551 				last, NULL);
1552 		if (ret_val && hw->aq.asq_last_status == I40E_AQ_RC_EPERM) {
1553 			dev_info(&pf->pdev->dev,
1554 				 "read NVM failed, invalid offset 0x%x\n",
1555 				 offset);
1556 			break;
1557 		} else if (ret_val &&
1558 			   hw->aq.asq_last_status == I40E_AQ_RC_EACCES) {
1559 			dev_info(&pf->pdev->dev,
1560 				 "read NVM failed, access, offset 0x%x\n",
1561 				 offset);
1562 			break;
1563 		} else if (ret_val) {
1564 			dev_info(&pf->pdev->dev,
1565 				 "read NVM failed offset %d err=%d status=0x%x\n",
1566 				 offset, ret_val, hw->aq.asq_last_status);
1567 			break;
1568 		}
1569 	}
1570 
1571 	i40e_release_nvm(hw);
1572 	memcpy(bytes, (u8 *)eeprom_buff, eeprom->len);
1573 free_buff:
1574 	kfree(eeprom_buff);
1575 	return ret_val;
1576 }
1577 
1578 static int i40e_get_eeprom_len(struct net_device *netdev)
1579 {
1580 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1581 	struct i40e_hw *hw = &np->vsi->back->hw;
1582 	u32 val;
1583 
1584 #define X722_EEPROM_SCOPE_LIMIT 0x5B9FFF
1585 	if (hw->mac.type == I40E_MAC_X722) {
1586 		val = X722_EEPROM_SCOPE_LIMIT + 1;
1587 		return val;
1588 	}
1589 	val = (rd32(hw, I40E_GLPCI_LBARCTRL)
1590 		& I40E_GLPCI_LBARCTRL_FL_SIZE_MASK)
1591 		>> I40E_GLPCI_LBARCTRL_FL_SIZE_SHIFT;
1592 	/* register returns value in power of 2, 64Kbyte chunks. */
1593 	val = (64 * 1024) * BIT(val);
1594 	return val;
1595 }
1596 
1597 static int i40e_set_eeprom(struct net_device *netdev,
1598 			   struct ethtool_eeprom *eeprom, u8 *bytes)
1599 {
1600 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1601 	struct i40e_hw *hw = &np->vsi->back->hw;
1602 	struct i40e_pf *pf = np->vsi->back;
1603 	struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1604 	int ret_val = 0;
1605 	int errno = 0;
1606 	u32 magic;
1607 
1608 	/* normal ethtool set_eeprom is not supported */
1609 	magic = hw->vendor_id | (hw->device_id << 16);
1610 	if (eeprom->magic == magic)
1611 		errno = -EOPNOTSUPP;
1612 	/* check for NVMUpdate access method */
1613 	else if (!eeprom->magic || (eeprom->magic >> 16) != hw->device_id)
1614 		errno = -EINVAL;
1615 	else if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
1616 		 test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
1617 		errno = -EBUSY;
1618 	else
1619 		ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1620 
1621 	if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1622 		dev_info(&pf->pdev->dev,
1623 			 "NVMUpdate write failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1624 			 ret_val, hw->aq.asq_last_status, errno,
1625 			 (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1626 			 cmd->offset, cmd->data_size);
1627 
1628 	return errno;
1629 }
1630 
1631 static void i40e_get_drvinfo(struct net_device *netdev,
1632 			     struct ethtool_drvinfo *drvinfo)
1633 {
1634 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1635 	struct i40e_vsi *vsi = np->vsi;
1636 	struct i40e_pf *pf = vsi->back;
1637 
1638 	strlcpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
1639 	strlcpy(drvinfo->version, i40e_driver_version_str,
1640 		sizeof(drvinfo->version));
1641 	strlcpy(drvinfo->fw_version, i40e_nvm_version_str(&pf->hw),
1642 		sizeof(drvinfo->fw_version));
1643 	strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
1644 		sizeof(drvinfo->bus_info));
1645 	drvinfo->n_priv_flags = I40E_PRIV_FLAGS_STR_LEN;
1646 	if (pf->hw.pf_id == 0)
1647 		drvinfo->n_priv_flags += I40E_GL_PRIV_FLAGS_STR_LEN;
1648 }
1649 
1650 static void i40e_get_ringparam(struct net_device *netdev,
1651 			       struct ethtool_ringparam *ring)
1652 {
1653 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1654 	struct i40e_pf *pf = np->vsi->back;
1655 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
1656 
1657 	ring->rx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
1658 	ring->tx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
1659 	ring->rx_mini_max_pending = 0;
1660 	ring->rx_jumbo_max_pending = 0;
1661 	ring->rx_pending = vsi->rx_rings[0]->count;
1662 	ring->tx_pending = vsi->tx_rings[0]->count;
1663 	ring->rx_mini_pending = 0;
1664 	ring->rx_jumbo_pending = 0;
1665 }
1666 
1667 static bool i40e_active_tx_ring_index(struct i40e_vsi *vsi, u16 index)
1668 {
1669 	if (i40e_enabled_xdp_vsi(vsi)) {
1670 		return index < vsi->num_queue_pairs ||
1671 			(index >= vsi->alloc_queue_pairs &&
1672 			 index < vsi->alloc_queue_pairs + vsi->num_queue_pairs);
1673 	}
1674 
1675 	return index < vsi->num_queue_pairs;
1676 }
1677 
1678 static int i40e_set_ringparam(struct net_device *netdev,
1679 			      struct ethtool_ringparam *ring)
1680 {
1681 	struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
1682 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1683 	struct i40e_hw *hw = &np->vsi->back->hw;
1684 	struct i40e_vsi *vsi = np->vsi;
1685 	struct i40e_pf *pf = vsi->back;
1686 	u32 new_rx_count, new_tx_count;
1687 	u16 tx_alloc_queue_pairs;
1688 	int timeout = 50;
1689 	int i, err = 0;
1690 
1691 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
1692 		return -EINVAL;
1693 
1694 	if (ring->tx_pending > I40E_MAX_NUM_DESCRIPTORS ||
1695 	    ring->tx_pending < I40E_MIN_NUM_DESCRIPTORS ||
1696 	    ring->rx_pending > I40E_MAX_NUM_DESCRIPTORS ||
1697 	    ring->rx_pending < I40E_MIN_NUM_DESCRIPTORS) {
1698 		netdev_info(netdev,
1699 			    "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d]\n",
1700 			    ring->tx_pending, ring->rx_pending,
1701 			    I40E_MIN_NUM_DESCRIPTORS, I40E_MAX_NUM_DESCRIPTORS);
1702 		return -EINVAL;
1703 	}
1704 
1705 	new_tx_count = ALIGN(ring->tx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
1706 	new_rx_count = ALIGN(ring->rx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
1707 
1708 	/* if nothing to do return success */
1709 	if ((new_tx_count == vsi->tx_rings[0]->count) &&
1710 	    (new_rx_count == vsi->rx_rings[0]->count))
1711 		return 0;
1712 
1713 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
1714 		timeout--;
1715 		if (!timeout)
1716 			return -EBUSY;
1717 		usleep_range(1000, 2000);
1718 	}
1719 
1720 	if (!netif_running(vsi->netdev)) {
1721 		/* simple case - set for the next time the netdev is started */
1722 		for (i = 0; i < vsi->num_queue_pairs; i++) {
1723 			vsi->tx_rings[i]->count = new_tx_count;
1724 			vsi->rx_rings[i]->count = new_rx_count;
1725 			if (i40e_enabled_xdp_vsi(vsi))
1726 				vsi->xdp_rings[i]->count = new_tx_count;
1727 		}
1728 		goto done;
1729 	}
1730 
1731 	/* We can't just free everything and then setup again,
1732 	 * because the ISRs in MSI-X mode get passed pointers
1733 	 * to the Tx and Rx ring structs.
1734 	 */
1735 
1736 	/* alloc updated Tx and XDP Tx resources */
1737 	tx_alloc_queue_pairs = vsi->alloc_queue_pairs *
1738 			       (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
1739 	if (new_tx_count != vsi->tx_rings[0]->count) {
1740 		netdev_info(netdev,
1741 			    "Changing Tx descriptor count from %d to %d.\n",
1742 			    vsi->tx_rings[0]->count, new_tx_count);
1743 		tx_rings = kcalloc(tx_alloc_queue_pairs,
1744 				   sizeof(struct i40e_ring), GFP_KERNEL);
1745 		if (!tx_rings) {
1746 			err = -ENOMEM;
1747 			goto done;
1748 		}
1749 
1750 		for (i = 0; i < tx_alloc_queue_pairs; i++) {
1751 			if (!i40e_active_tx_ring_index(vsi, i))
1752 				continue;
1753 
1754 			tx_rings[i] = *vsi->tx_rings[i];
1755 			tx_rings[i].count = new_tx_count;
1756 			/* the desc and bi pointers will be reallocated in the
1757 			 * setup call
1758 			 */
1759 			tx_rings[i].desc = NULL;
1760 			tx_rings[i].rx_bi = NULL;
1761 			err = i40e_setup_tx_descriptors(&tx_rings[i]);
1762 			if (err) {
1763 				while (i) {
1764 					i--;
1765 					if (!i40e_active_tx_ring_index(vsi, i))
1766 						continue;
1767 					i40e_free_tx_resources(&tx_rings[i]);
1768 				}
1769 				kfree(tx_rings);
1770 				tx_rings = NULL;
1771 
1772 				goto done;
1773 			}
1774 		}
1775 	}
1776 
1777 	/* alloc updated Rx resources */
1778 	if (new_rx_count != vsi->rx_rings[0]->count) {
1779 		netdev_info(netdev,
1780 			    "Changing Rx descriptor count from %d to %d\n",
1781 			    vsi->rx_rings[0]->count, new_rx_count);
1782 		rx_rings = kcalloc(vsi->alloc_queue_pairs,
1783 				   sizeof(struct i40e_ring), GFP_KERNEL);
1784 		if (!rx_rings) {
1785 			err = -ENOMEM;
1786 			goto free_tx;
1787 		}
1788 
1789 		for (i = 0; i < vsi->num_queue_pairs; i++) {
1790 			u16 unused;
1791 
1792 			/* clone ring and setup updated count */
1793 			rx_rings[i] = *vsi->rx_rings[i];
1794 			rx_rings[i].count = new_rx_count;
1795 			/* the desc and bi pointers will be reallocated in the
1796 			 * setup call
1797 			 */
1798 			rx_rings[i].desc = NULL;
1799 			rx_rings[i].rx_bi = NULL;
1800 			/* Clear cloned XDP RX-queue info before setup call */
1801 			memset(&rx_rings[i].xdp_rxq, 0, sizeof(rx_rings[i].xdp_rxq));
1802 			/* this is to allow wr32 to have something to write to
1803 			 * during early allocation of Rx buffers
1804 			 */
1805 			rx_rings[i].tail = hw->hw_addr + I40E_PRTGEN_STATUS;
1806 			err = i40e_setup_rx_descriptors(&rx_rings[i]);
1807 			if (err)
1808 				goto rx_unwind;
1809 
1810 			/* now allocate the Rx buffers to make sure the OS
1811 			 * has enough memory, any failure here means abort
1812 			 */
1813 			unused = I40E_DESC_UNUSED(&rx_rings[i]);
1814 			err = i40e_alloc_rx_buffers(&rx_rings[i], unused);
1815 rx_unwind:
1816 			if (err) {
1817 				do {
1818 					i40e_free_rx_resources(&rx_rings[i]);
1819 				} while (i--);
1820 				kfree(rx_rings);
1821 				rx_rings = NULL;
1822 
1823 				goto free_tx;
1824 			}
1825 		}
1826 	}
1827 
1828 	/* Bring interface down, copy in the new ring info,
1829 	 * then restore the interface
1830 	 */
1831 	i40e_down(vsi);
1832 
1833 	if (tx_rings) {
1834 		for (i = 0; i < tx_alloc_queue_pairs; i++) {
1835 			if (i40e_active_tx_ring_index(vsi, i)) {
1836 				i40e_free_tx_resources(vsi->tx_rings[i]);
1837 				*vsi->tx_rings[i] = tx_rings[i];
1838 			}
1839 		}
1840 		kfree(tx_rings);
1841 		tx_rings = NULL;
1842 	}
1843 
1844 	if (rx_rings) {
1845 		for (i = 0; i < vsi->num_queue_pairs; i++) {
1846 			i40e_free_rx_resources(vsi->rx_rings[i]);
1847 			/* get the real tail offset */
1848 			rx_rings[i].tail = vsi->rx_rings[i]->tail;
1849 			/* this is to fake out the allocation routine
1850 			 * into thinking it has to realloc everything
1851 			 * but the recycling logic will let us re-use
1852 			 * the buffers allocated above
1853 			 */
1854 			rx_rings[i].next_to_use = 0;
1855 			rx_rings[i].next_to_clean = 0;
1856 			rx_rings[i].next_to_alloc = 0;
1857 			/* do a struct copy */
1858 			*vsi->rx_rings[i] = rx_rings[i];
1859 		}
1860 		kfree(rx_rings);
1861 		rx_rings = NULL;
1862 	}
1863 
1864 	i40e_up(vsi);
1865 
1866 free_tx:
1867 	/* error cleanup if the Rx allocations failed after getting Tx */
1868 	if (tx_rings) {
1869 		for (i = 0; i < tx_alloc_queue_pairs; i++) {
1870 			if (i40e_active_tx_ring_index(vsi, i))
1871 				i40e_free_tx_resources(vsi->tx_rings[i]);
1872 		}
1873 		kfree(tx_rings);
1874 		tx_rings = NULL;
1875 	}
1876 
1877 done:
1878 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
1879 
1880 	return err;
1881 }
1882 
1883 /**
1884  * i40e_get_stats_count - return the stats count for a device
1885  * @netdev: the netdev to return the count for
1886  *
1887  * Returns the total number of statistics for this netdev. Note that even
1888  * though this is a function, it is required that the count for a specific
1889  * netdev must never change. Basing the count on static values such as the
1890  * maximum number of queues or the device type is ok. However, the API for
1891  * obtaining stats is *not* safe against changes based on non-static
1892  * values such as the *current* number of queues, or runtime flags.
1893  *
1894  * If a statistic is not always enabled, return it as part of the count
1895  * anyways, always return its string, and report its value as zero.
1896  **/
1897 static int i40e_get_stats_count(struct net_device *netdev)
1898 {
1899 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1900 	struct i40e_vsi *vsi = np->vsi;
1901 	struct i40e_pf *pf = vsi->back;
1902 	int stats_len;
1903 
1904 	if (vsi == pf->vsi[pf->lan_vsi] && pf->hw.partition_id == 1)
1905 		stats_len = I40E_PF_STATS_LEN;
1906 	else
1907 		stats_len = I40E_VSI_STATS_LEN;
1908 
1909 	/* The number of stats reported for a given net_device must remain
1910 	 * constant throughout the life of that device.
1911 	 *
1912 	 * This is because the API for obtaining the size, strings, and stats
1913 	 * is spread out over three separate ethtool ioctls. There is no safe
1914 	 * way to lock the number of stats across these calls, so we must
1915 	 * assume that they will never change.
1916 	 *
1917 	 * Due to this, we report the maximum number of queues, even if not
1918 	 * every queue is currently configured. Since we always allocate
1919 	 * queues in pairs, we'll just use netdev->num_tx_queues * 2. This
1920 	 * works because the num_tx_queues is set at device creation and never
1921 	 * changes.
1922 	 */
1923 	stats_len += I40E_QUEUE_STATS_LEN * 2 * netdev->num_tx_queues;
1924 
1925 	return stats_len;
1926 }
1927 
1928 static int i40e_get_sset_count(struct net_device *netdev, int sset)
1929 {
1930 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1931 	struct i40e_vsi *vsi = np->vsi;
1932 	struct i40e_pf *pf = vsi->back;
1933 
1934 	switch (sset) {
1935 	case ETH_SS_TEST:
1936 		return I40E_TEST_LEN;
1937 	case ETH_SS_STATS:
1938 		return i40e_get_stats_count(netdev);
1939 	case ETH_SS_PRIV_FLAGS:
1940 		return I40E_PRIV_FLAGS_STR_LEN +
1941 			(pf->hw.pf_id == 0 ? I40E_GL_PRIV_FLAGS_STR_LEN : 0);
1942 	default:
1943 		return -EOPNOTSUPP;
1944 	}
1945 }
1946 
1947 /**
1948  * i40e_get_pfc_stats - copy HW PFC statistics to formatted structure
1949  * @pf: the PF device structure
1950  * @i: the priority value to copy
1951  *
1952  * The PFC stats are found as arrays in pf->stats, which is not easy to pass
1953  * into i40e_add_ethtool_stats. Produce a formatted i40e_pfc_stats structure
1954  * of the PFC stats for the given priority.
1955  **/
1956 static inline struct i40e_pfc_stats
1957 i40e_get_pfc_stats(struct i40e_pf *pf, unsigned int i)
1958 {
1959 #define I40E_GET_PFC_STAT(stat, priority) \
1960 	.stat = pf->stats.stat[priority]
1961 
1962 	struct i40e_pfc_stats pfc = {
1963 		I40E_GET_PFC_STAT(priority_xon_rx, i),
1964 		I40E_GET_PFC_STAT(priority_xoff_rx, i),
1965 		I40E_GET_PFC_STAT(priority_xon_tx, i),
1966 		I40E_GET_PFC_STAT(priority_xoff_tx, i),
1967 		I40E_GET_PFC_STAT(priority_xon_2_xoff, i),
1968 	};
1969 	return pfc;
1970 }
1971 
1972 /**
1973  * i40e_get_ethtool_stats - copy stat values into supplied buffer
1974  * @netdev: the netdev to collect stats for
1975  * @stats: ethtool stats command structure
1976  * @data: ethtool supplied buffer
1977  *
1978  * Copy the stats values for this netdev into the buffer. Expects data to be
1979  * pre-allocated to the size returned by i40e_get_stats_count.. Note that all
1980  * statistics must be copied in a static order, and the count must not change
1981  * for a given netdev. See i40e_get_stats_count for more details.
1982  *
1983  * If a statistic is not currently valid (such as a disabled queue), this
1984  * function reports its value as zero.
1985  **/
1986 static void i40e_get_ethtool_stats(struct net_device *netdev,
1987 				   struct ethtool_stats *stats, u64 *data)
1988 {
1989 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1990 	struct i40e_vsi *vsi = np->vsi;
1991 	struct i40e_pf *pf = vsi->back;
1992 	struct i40e_veb *veb = pf->veb[pf->lan_veb];
1993 	unsigned int i;
1994 	bool veb_stats;
1995 	u64 *p = data;
1996 
1997 	i40e_update_stats(vsi);
1998 
1999 	i40e_add_ethtool_stats(&data, i40e_get_vsi_stats_struct(vsi),
2000 			       i40e_gstrings_net_stats);
2001 
2002 	i40e_add_ethtool_stats(&data, vsi, i40e_gstrings_misc_stats);
2003 
2004 	rcu_read_lock();
2005 	for (i = 0; i < netdev->num_tx_queues; i++) {
2006 		i40e_add_queue_stats(&data, READ_ONCE(vsi->tx_rings[i]));
2007 		i40e_add_queue_stats(&data, READ_ONCE(vsi->rx_rings[i]));
2008 	}
2009 	rcu_read_unlock();
2010 
2011 	if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
2012 		goto check_data_pointer;
2013 
2014 	veb_stats = ((pf->lan_veb != I40E_NO_VEB) &&
2015 		     (pf->flags & I40E_FLAG_VEB_STATS_ENABLED));
2016 
2017 	/* If veb stats aren't enabled, pass NULL instead of the veb so that
2018 	 * we initialize stats to zero and update the data pointer
2019 	 * intelligently
2020 	 */
2021 	i40e_add_ethtool_stats(&data, veb_stats ? veb : NULL,
2022 			       i40e_gstrings_veb_stats);
2023 
2024 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
2025 		i40e_add_ethtool_stats(&data, veb_stats ? veb : NULL,
2026 				       i40e_gstrings_veb_tc_stats);
2027 
2028 	i40e_add_ethtool_stats(&data, pf, i40e_gstrings_stats);
2029 
2030 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
2031 		struct i40e_pfc_stats pfc = i40e_get_pfc_stats(pf, i);
2032 
2033 		i40e_add_ethtool_stats(&data, &pfc, i40e_gstrings_pfc_stats);
2034 	}
2035 
2036 check_data_pointer:
2037 	WARN_ONCE(data - p != i40e_get_stats_count(netdev),
2038 		  "ethtool stats count mismatch!");
2039 }
2040 
2041 /**
2042  * i40e_get_stat_strings - copy stat strings into supplied buffer
2043  * @netdev: the netdev to collect strings for
2044  * @data: supplied buffer to copy strings into
2045  *
2046  * Copy the strings related to stats for this netdev. Expects data to be
2047  * pre-allocated with the size reported by i40e_get_stats_count. Note that the
2048  * strings must be copied in a static order and the total count must not
2049  * change for a given netdev. See i40e_get_stats_count for more details.
2050  **/
2051 static void i40e_get_stat_strings(struct net_device *netdev, u8 *data)
2052 {
2053 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2054 	struct i40e_vsi *vsi = np->vsi;
2055 	struct i40e_pf *pf = vsi->back;
2056 	unsigned int i;
2057 	u8 *p = data;
2058 
2059 	i40e_add_stat_strings(&data, i40e_gstrings_net_stats);
2060 
2061 	i40e_add_stat_strings(&data, i40e_gstrings_misc_stats);
2062 
2063 	for (i = 0; i < netdev->num_tx_queues; i++) {
2064 		i40e_add_stat_strings(&data, i40e_gstrings_queue_stats,
2065 				      "tx", i);
2066 		i40e_add_stat_strings(&data, i40e_gstrings_queue_stats,
2067 				      "rx", i);
2068 	}
2069 
2070 	if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
2071 		return;
2072 
2073 	i40e_add_stat_strings(&data, i40e_gstrings_veb_stats);
2074 
2075 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
2076 		i40e_add_stat_strings(&data, i40e_gstrings_veb_tc_stats, i);
2077 
2078 	i40e_add_stat_strings(&data, i40e_gstrings_stats);
2079 
2080 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
2081 		i40e_add_stat_strings(&data, i40e_gstrings_pfc_stats, i);
2082 
2083 	WARN_ONCE(data - p != i40e_get_stats_count(netdev) * ETH_GSTRING_LEN,
2084 		  "stat strings count mismatch!");
2085 }
2086 
2087 static void i40e_get_priv_flag_strings(struct net_device *netdev, u8 *data)
2088 {
2089 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2090 	struct i40e_vsi *vsi = np->vsi;
2091 	struct i40e_pf *pf = vsi->back;
2092 	char *p = (char *)data;
2093 	unsigned int i;
2094 
2095 	for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
2096 		snprintf(p, ETH_GSTRING_LEN, "%s",
2097 			 i40e_gstrings_priv_flags[i].flag_string);
2098 		p += ETH_GSTRING_LEN;
2099 	}
2100 	if (pf->hw.pf_id != 0)
2101 		return;
2102 	for (i = 0; i < I40E_GL_PRIV_FLAGS_STR_LEN; i++) {
2103 		snprintf(p, ETH_GSTRING_LEN, "%s",
2104 			 i40e_gl_gstrings_priv_flags[i].flag_string);
2105 		p += ETH_GSTRING_LEN;
2106 	}
2107 }
2108 
2109 static void i40e_get_strings(struct net_device *netdev, u32 stringset,
2110 			     u8 *data)
2111 {
2112 	switch (stringset) {
2113 	case ETH_SS_TEST:
2114 		memcpy(data, i40e_gstrings_test,
2115 		       I40E_TEST_LEN * ETH_GSTRING_LEN);
2116 		break;
2117 	case ETH_SS_STATS:
2118 		i40e_get_stat_strings(netdev, data);
2119 		break;
2120 	case ETH_SS_PRIV_FLAGS:
2121 		i40e_get_priv_flag_strings(netdev, data);
2122 		break;
2123 	default:
2124 		break;
2125 	}
2126 }
2127 
2128 static int i40e_get_ts_info(struct net_device *dev,
2129 			    struct ethtool_ts_info *info)
2130 {
2131 	struct i40e_pf *pf = i40e_netdev_to_pf(dev);
2132 
2133 	/* only report HW timestamping if PTP is enabled */
2134 	if (!(pf->flags & I40E_FLAG_PTP))
2135 		return ethtool_op_get_ts_info(dev, info);
2136 
2137 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2138 				SOF_TIMESTAMPING_RX_SOFTWARE |
2139 				SOF_TIMESTAMPING_SOFTWARE |
2140 				SOF_TIMESTAMPING_TX_HARDWARE |
2141 				SOF_TIMESTAMPING_RX_HARDWARE |
2142 				SOF_TIMESTAMPING_RAW_HARDWARE;
2143 
2144 	if (pf->ptp_clock)
2145 		info->phc_index = ptp_clock_index(pf->ptp_clock);
2146 	else
2147 		info->phc_index = -1;
2148 
2149 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
2150 
2151 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
2152 			   BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2153 			   BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
2154 			   BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ);
2155 
2156 	if (pf->hw_features & I40E_HW_PTP_L4_CAPABLE)
2157 		info->rx_filters |= BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
2158 				    BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
2159 				    BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
2160 				    BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
2161 				    BIT(HWTSTAMP_FILTER_PTP_V2_SYNC) |
2162 				    BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
2163 				    BIT(HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
2164 				    BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
2165 
2166 	return 0;
2167 }
2168 
2169 static int i40e_link_test(struct net_device *netdev, u64 *data)
2170 {
2171 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2172 	struct i40e_pf *pf = np->vsi->back;
2173 	i40e_status status;
2174 	bool link_up = false;
2175 
2176 	netif_info(pf, hw, netdev, "link test\n");
2177 	status = i40e_get_link_status(&pf->hw, &link_up);
2178 	if (status) {
2179 		netif_err(pf, drv, netdev, "link query timed out, please retry test\n");
2180 		*data = 1;
2181 		return *data;
2182 	}
2183 
2184 	if (link_up)
2185 		*data = 0;
2186 	else
2187 		*data = 1;
2188 
2189 	return *data;
2190 }
2191 
2192 static int i40e_reg_test(struct net_device *netdev, u64 *data)
2193 {
2194 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2195 	struct i40e_pf *pf = np->vsi->back;
2196 
2197 	netif_info(pf, hw, netdev, "register test\n");
2198 	*data = i40e_diag_reg_test(&pf->hw);
2199 
2200 	return *data;
2201 }
2202 
2203 static int i40e_eeprom_test(struct net_device *netdev, u64 *data)
2204 {
2205 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2206 	struct i40e_pf *pf = np->vsi->back;
2207 
2208 	netif_info(pf, hw, netdev, "eeprom test\n");
2209 	*data = i40e_diag_eeprom_test(&pf->hw);
2210 
2211 	/* forcebly clear the NVM Update state machine */
2212 	pf->hw.nvmupd_state = I40E_NVMUPD_STATE_INIT;
2213 
2214 	return *data;
2215 }
2216 
2217 static int i40e_intr_test(struct net_device *netdev, u64 *data)
2218 {
2219 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2220 	struct i40e_pf *pf = np->vsi->back;
2221 	u16 swc_old = pf->sw_int_count;
2222 
2223 	netif_info(pf, hw, netdev, "interrupt test\n");
2224 	wr32(&pf->hw, I40E_PFINT_DYN_CTL0,
2225 	     (I40E_PFINT_DYN_CTL0_INTENA_MASK |
2226 	      I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
2227 	      I40E_PFINT_DYN_CTL0_ITR_INDX_MASK |
2228 	      I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK |
2229 	      I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK));
2230 	usleep_range(1000, 2000);
2231 	*data = (swc_old == pf->sw_int_count);
2232 
2233 	return *data;
2234 }
2235 
2236 static inline bool i40e_active_vfs(struct i40e_pf *pf)
2237 {
2238 	struct i40e_vf *vfs = pf->vf;
2239 	int i;
2240 
2241 	for (i = 0; i < pf->num_alloc_vfs; i++)
2242 		if (test_bit(I40E_VF_STATE_ACTIVE, &vfs[i].vf_states))
2243 			return true;
2244 	return false;
2245 }
2246 
2247 static inline bool i40e_active_vmdqs(struct i40e_pf *pf)
2248 {
2249 	return !!i40e_find_vsi_by_type(pf, I40E_VSI_VMDQ2);
2250 }
2251 
2252 static void i40e_diag_test(struct net_device *netdev,
2253 			   struct ethtool_test *eth_test, u64 *data)
2254 {
2255 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2256 	bool if_running = netif_running(netdev);
2257 	struct i40e_pf *pf = np->vsi->back;
2258 
2259 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
2260 		/* Offline tests */
2261 		netif_info(pf, drv, netdev, "offline testing starting\n");
2262 
2263 		set_bit(__I40E_TESTING, pf->state);
2264 
2265 		if (i40e_active_vfs(pf) || i40e_active_vmdqs(pf)) {
2266 			dev_warn(&pf->pdev->dev,
2267 				 "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
2268 			data[I40E_ETH_TEST_REG]		= 1;
2269 			data[I40E_ETH_TEST_EEPROM]	= 1;
2270 			data[I40E_ETH_TEST_INTR]	= 1;
2271 			data[I40E_ETH_TEST_LINK]	= 1;
2272 			eth_test->flags |= ETH_TEST_FL_FAILED;
2273 			clear_bit(__I40E_TESTING, pf->state);
2274 			goto skip_ol_tests;
2275 		}
2276 
2277 		/* If the device is online then take it offline */
2278 		if (if_running)
2279 			/* indicate we're in test mode */
2280 			i40e_close(netdev);
2281 		else
2282 			/* This reset does not affect link - if it is
2283 			 * changed to a type of reset that does affect
2284 			 * link then the following link test would have
2285 			 * to be moved to before the reset
2286 			 */
2287 			i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
2288 
2289 		if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
2290 			eth_test->flags |= ETH_TEST_FL_FAILED;
2291 
2292 		if (i40e_eeprom_test(netdev, &data[I40E_ETH_TEST_EEPROM]))
2293 			eth_test->flags |= ETH_TEST_FL_FAILED;
2294 
2295 		if (i40e_intr_test(netdev, &data[I40E_ETH_TEST_INTR]))
2296 			eth_test->flags |= ETH_TEST_FL_FAILED;
2297 
2298 		/* run reg test last, a reset is required after it */
2299 		if (i40e_reg_test(netdev, &data[I40E_ETH_TEST_REG]))
2300 			eth_test->flags |= ETH_TEST_FL_FAILED;
2301 
2302 		clear_bit(__I40E_TESTING, pf->state);
2303 		i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
2304 
2305 		if (if_running)
2306 			i40e_open(netdev);
2307 	} else {
2308 		/* Online tests */
2309 		netif_info(pf, drv, netdev, "online testing starting\n");
2310 
2311 		if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
2312 			eth_test->flags |= ETH_TEST_FL_FAILED;
2313 
2314 		/* Offline only tests, not run in online; pass by default */
2315 		data[I40E_ETH_TEST_REG] = 0;
2316 		data[I40E_ETH_TEST_EEPROM] = 0;
2317 		data[I40E_ETH_TEST_INTR] = 0;
2318 	}
2319 
2320 skip_ol_tests:
2321 
2322 	netif_info(pf, drv, netdev, "testing finished\n");
2323 }
2324 
2325 static void i40e_get_wol(struct net_device *netdev,
2326 			 struct ethtool_wolinfo *wol)
2327 {
2328 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2329 	struct i40e_pf *pf = np->vsi->back;
2330 	struct i40e_hw *hw = &pf->hw;
2331 	u16 wol_nvm_bits;
2332 
2333 	/* NVM bit on means WoL disabled for the port */
2334 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
2335 	if ((BIT(hw->port) & wol_nvm_bits) || (hw->partition_id != 1)) {
2336 		wol->supported = 0;
2337 		wol->wolopts = 0;
2338 	} else {
2339 		wol->supported = WAKE_MAGIC;
2340 		wol->wolopts = (pf->wol_en ? WAKE_MAGIC : 0);
2341 	}
2342 }
2343 
2344 /**
2345  * i40e_set_wol - set the WakeOnLAN configuration
2346  * @netdev: the netdev in question
2347  * @wol: the ethtool WoL setting data
2348  **/
2349 static int i40e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2350 {
2351 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2352 	struct i40e_pf *pf = np->vsi->back;
2353 	struct i40e_vsi *vsi = np->vsi;
2354 	struct i40e_hw *hw = &pf->hw;
2355 	u16 wol_nvm_bits;
2356 
2357 	/* WoL not supported if this isn't the controlling PF on the port */
2358 	if (hw->partition_id != 1) {
2359 		i40e_partition_setting_complaint(pf);
2360 		return -EOPNOTSUPP;
2361 	}
2362 
2363 	if (vsi != pf->vsi[pf->lan_vsi])
2364 		return -EOPNOTSUPP;
2365 
2366 	/* NVM bit on means WoL disabled for the port */
2367 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
2368 	if (BIT(hw->port) & wol_nvm_bits)
2369 		return -EOPNOTSUPP;
2370 
2371 	/* only magic packet is supported */
2372 	if (wol->wolopts && (wol->wolopts != WAKE_MAGIC))
2373 		return -EOPNOTSUPP;
2374 
2375 	/* is this a new value? */
2376 	if (pf->wol_en != !!wol->wolopts) {
2377 		pf->wol_en = !!wol->wolopts;
2378 		device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
2379 	}
2380 
2381 	return 0;
2382 }
2383 
2384 static int i40e_set_phys_id(struct net_device *netdev,
2385 			    enum ethtool_phys_id_state state)
2386 {
2387 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2388 	i40e_status ret = 0;
2389 	struct i40e_pf *pf = np->vsi->back;
2390 	struct i40e_hw *hw = &pf->hw;
2391 	int blink_freq = 2;
2392 	u16 temp_status;
2393 
2394 	switch (state) {
2395 	case ETHTOOL_ID_ACTIVE:
2396 		if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS)) {
2397 			pf->led_status = i40e_led_get(hw);
2398 		} else {
2399 			if (!(hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE))
2400 				i40e_aq_set_phy_debug(hw, I40E_PHY_DEBUG_ALL,
2401 						      NULL);
2402 			ret = i40e_led_get_phy(hw, &temp_status,
2403 					       &pf->phy_led_val);
2404 			pf->led_status = temp_status;
2405 		}
2406 		return blink_freq;
2407 	case ETHTOOL_ID_ON:
2408 		if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS))
2409 			i40e_led_set(hw, 0xf, false);
2410 		else
2411 			ret = i40e_led_set_phy(hw, true, pf->led_status, 0);
2412 		break;
2413 	case ETHTOOL_ID_OFF:
2414 		if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS))
2415 			i40e_led_set(hw, 0x0, false);
2416 		else
2417 			ret = i40e_led_set_phy(hw, false, pf->led_status, 0);
2418 		break;
2419 	case ETHTOOL_ID_INACTIVE:
2420 		if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS)) {
2421 			i40e_led_set(hw, pf->led_status, false);
2422 		} else {
2423 			ret = i40e_led_set_phy(hw, false, pf->led_status,
2424 					       (pf->phy_led_val |
2425 					       I40E_PHY_LED_MODE_ORIG));
2426 			if (!(hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE))
2427 				i40e_aq_set_phy_debug(hw, 0, NULL);
2428 		}
2429 		break;
2430 	default:
2431 		break;
2432 	}
2433 		if (ret)
2434 			return -ENOENT;
2435 		else
2436 			return 0;
2437 }
2438 
2439 /* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
2440  * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
2441  * 125us (8000 interrupts per second) == ITR(62)
2442  */
2443 
2444 /**
2445  * __i40e_get_coalesce - get per-queue coalesce settings
2446  * @netdev: the netdev to check
2447  * @ec: ethtool coalesce data structure
2448  * @queue: which queue to pick
2449  *
2450  * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
2451  * are per queue. If queue is <0 then we default to queue 0 as the
2452  * representative value.
2453  **/
2454 static int __i40e_get_coalesce(struct net_device *netdev,
2455 			       struct ethtool_coalesce *ec,
2456 			       int queue)
2457 {
2458 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2459 	struct i40e_ring *rx_ring, *tx_ring;
2460 	struct i40e_vsi *vsi = np->vsi;
2461 
2462 	ec->tx_max_coalesced_frames_irq = vsi->work_limit;
2463 	ec->rx_max_coalesced_frames_irq = vsi->work_limit;
2464 
2465 	/* rx and tx usecs has per queue value. If user doesn't specify the
2466 	 * queue, return queue 0's value to represent.
2467 	 */
2468 	if (queue < 0)
2469 		queue = 0;
2470 	else if (queue >= vsi->num_queue_pairs)
2471 		return -EINVAL;
2472 
2473 	rx_ring = vsi->rx_rings[queue];
2474 	tx_ring = vsi->tx_rings[queue];
2475 
2476 	if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
2477 		ec->use_adaptive_rx_coalesce = 1;
2478 
2479 	if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
2480 		ec->use_adaptive_tx_coalesce = 1;
2481 
2482 	ec->rx_coalesce_usecs = rx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
2483 	ec->tx_coalesce_usecs = tx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
2484 
2485 	/* we use the _usecs_high to store/set the interrupt rate limit
2486 	 * that the hardware supports, that almost but not quite
2487 	 * fits the original intent of the ethtool variable,
2488 	 * the rx_coalesce_usecs_high limits total interrupts
2489 	 * per second from both tx/rx sources.
2490 	 */
2491 	ec->rx_coalesce_usecs_high = vsi->int_rate_limit;
2492 	ec->tx_coalesce_usecs_high = vsi->int_rate_limit;
2493 
2494 	return 0;
2495 }
2496 
2497 /**
2498  * i40e_get_coalesce - get a netdev's coalesce settings
2499  * @netdev: the netdev to check
2500  * @ec: ethtool coalesce data structure
2501  *
2502  * Gets the coalesce settings for a particular netdev. Note that if user has
2503  * modified per-queue settings, this only guarantees to represent queue 0. See
2504  * __i40e_get_coalesce for more details.
2505  **/
2506 static int i40e_get_coalesce(struct net_device *netdev,
2507 			     struct ethtool_coalesce *ec)
2508 {
2509 	return __i40e_get_coalesce(netdev, ec, -1);
2510 }
2511 
2512 /**
2513  * i40e_get_per_queue_coalesce - gets coalesce settings for particular queue
2514  * @netdev: netdev structure
2515  * @ec: ethtool's coalesce settings
2516  * @queue: the particular queue to read
2517  *
2518  * Will read a specific queue's coalesce settings
2519  **/
2520 static int i40e_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
2521 				       struct ethtool_coalesce *ec)
2522 {
2523 	return __i40e_get_coalesce(netdev, ec, queue);
2524 }
2525 
2526 /**
2527  * i40e_set_itr_per_queue - set ITR values for specific queue
2528  * @vsi: the VSI to set values for
2529  * @ec: coalesce settings from ethtool
2530  * @queue: the queue to modify
2531  *
2532  * Change the ITR settings for a specific queue.
2533  **/
2534 static void i40e_set_itr_per_queue(struct i40e_vsi *vsi,
2535 				   struct ethtool_coalesce *ec,
2536 				   int queue)
2537 {
2538 	struct i40e_ring *rx_ring = vsi->rx_rings[queue];
2539 	struct i40e_ring *tx_ring = vsi->tx_rings[queue];
2540 	struct i40e_pf *pf = vsi->back;
2541 	struct i40e_hw *hw = &pf->hw;
2542 	struct i40e_q_vector *q_vector;
2543 	u16 intrl;
2544 
2545 	intrl = i40e_intrl_usec_to_reg(vsi->int_rate_limit);
2546 
2547 	rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
2548 	tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
2549 
2550 	if (ec->use_adaptive_rx_coalesce)
2551 		rx_ring->itr_setting |= I40E_ITR_DYNAMIC;
2552 	else
2553 		rx_ring->itr_setting &= ~I40E_ITR_DYNAMIC;
2554 
2555 	if (ec->use_adaptive_tx_coalesce)
2556 		tx_ring->itr_setting |= I40E_ITR_DYNAMIC;
2557 	else
2558 		tx_ring->itr_setting &= ~I40E_ITR_DYNAMIC;
2559 
2560 	q_vector = rx_ring->q_vector;
2561 	q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
2562 
2563 	q_vector = tx_ring->q_vector;
2564 	q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
2565 
2566 	/* The interrupt handler itself will take care of programming
2567 	 * the Tx and Rx ITR values based on the values we have entered
2568 	 * into the q_vector, no need to write the values now.
2569 	 */
2570 
2571 	wr32(hw, I40E_PFINT_RATEN(q_vector->reg_idx), intrl);
2572 	i40e_flush(hw);
2573 }
2574 
2575 /**
2576  * __i40e_set_coalesce - set coalesce settings for particular queue
2577  * @netdev: the netdev to change
2578  * @ec: ethtool coalesce settings
2579  * @queue: the queue to change
2580  *
2581  * Sets the coalesce settings for a particular queue.
2582  **/
2583 static int __i40e_set_coalesce(struct net_device *netdev,
2584 			       struct ethtool_coalesce *ec,
2585 			       int queue)
2586 {
2587 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2588 	u16 intrl_reg, cur_rx_itr, cur_tx_itr;
2589 	struct i40e_vsi *vsi = np->vsi;
2590 	struct i40e_pf *pf = vsi->back;
2591 	int i;
2592 
2593 	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
2594 		vsi->work_limit = ec->tx_max_coalesced_frames_irq;
2595 
2596 	if (queue < 0) {
2597 		cur_rx_itr = vsi->rx_rings[0]->itr_setting;
2598 		cur_tx_itr = vsi->tx_rings[0]->itr_setting;
2599 	} else if (queue < vsi->num_queue_pairs) {
2600 		cur_rx_itr = vsi->rx_rings[queue]->itr_setting;
2601 		cur_tx_itr = vsi->tx_rings[queue]->itr_setting;
2602 	} else {
2603 		netif_info(pf, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
2604 			   vsi->num_queue_pairs - 1);
2605 		return -EINVAL;
2606 	}
2607 
2608 	cur_tx_itr &= ~I40E_ITR_DYNAMIC;
2609 	cur_rx_itr &= ~I40E_ITR_DYNAMIC;
2610 
2611 	/* tx_coalesce_usecs_high is ignored, use rx-usecs-high instead */
2612 	if (ec->tx_coalesce_usecs_high != vsi->int_rate_limit) {
2613 		netif_info(pf, drv, netdev, "tx-usecs-high is not used, please program rx-usecs-high\n");
2614 		return -EINVAL;
2615 	}
2616 
2617 	if (ec->rx_coalesce_usecs_high > INTRL_REG_TO_USEC(I40E_MAX_INTRL)) {
2618 		netif_info(pf, drv, netdev, "Invalid value, rx-usecs-high range is 0-%lu\n",
2619 			   INTRL_REG_TO_USEC(I40E_MAX_INTRL));
2620 		return -EINVAL;
2621 	}
2622 
2623 	if (ec->rx_coalesce_usecs != cur_rx_itr &&
2624 	    ec->use_adaptive_rx_coalesce) {
2625 		netif_info(pf, drv, netdev, "RX interrupt moderation cannot be changed if adaptive-rx is enabled.\n");
2626 		return -EINVAL;
2627 	}
2628 
2629 	if (ec->rx_coalesce_usecs > I40E_MAX_ITR) {
2630 		netif_info(pf, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
2631 		return -EINVAL;
2632 	}
2633 
2634 	if (ec->tx_coalesce_usecs != cur_tx_itr &&
2635 	    ec->use_adaptive_tx_coalesce) {
2636 		netif_info(pf, drv, netdev, "TX interrupt moderation cannot be changed if adaptive-tx is enabled.\n");
2637 		return -EINVAL;
2638 	}
2639 
2640 	if (ec->tx_coalesce_usecs > I40E_MAX_ITR) {
2641 		netif_info(pf, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
2642 		return -EINVAL;
2643 	}
2644 
2645 	if (ec->use_adaptive_rx_coalesce && !cur_rx_itr)
2646 		ec->rx_coalesce_usecs = I40E_MIN_ITR;
2647 
2648 	if (ec->use_adaptive_tx_coalesce && !cur_tx_itr)
2649 		ec->tx_coalesce_usecs = I40E_MIN_ITR;
2650 
2651 	intrl_reg = i40e_intrl_usec_to_reg(ec->rx_coalesce_usecs_high);
2652 	vsi->int_rate_limit = INTRL_REG_TO_USEC(intrl_reg);
2653 	if (vsi->int_rate_limit != ec->rx_coalesce_usecs_high) {
2654 		netif_info(pf, drv, netdev, "Interrupt rate limit rounded down to %d\n",
2655 			   vsi->int_rate_limit);
2656 	}
2657 
2658 	/* rx and tx usecs has per queue value. If user doesn't specify the
2659 	 * queue, apply to all queues.
2660 	 */
2661 	if (queue < 0) {
2662 		for (i = 0; i < vsi->num_queue_pairs; i++)
2663 			i40e_set_itr_per_queue(vsi, ec, i);
2664 	} else {
2665 		i40e_set_itr_per_queue(vsi, ec, queue);
2666 	}
2667 
2668 	return 0;
2669 }
2670 
2671 /**
2672  * i40e_set_coalesce - set coalesce settings for every queue on the netdev
2673  * @netdev: the netdev to change
2674  * @ec: ethtool coalesce settings
2675  *
2676  * This will set each queue to the same coalesce settings.
2677  **/
2678 static int i40e_set_coalesce(struct net_device *netdev,
2679 			     struct ethtool_coalesce *ec)
2680 {
2681 	return __i40e_set_coalesce(netdev, ec, -1);
2682 }
2683 
2684 /**
2685  * i40e_set_per_queue_coalesce - set specific queue's coalesce settings
2686  * @netdev: the netdev to change
2687  * @ec: ethtool's coalesce settings
2688  * @queue: the queue to change
2689  *
2690  * Sets the specified queue's coalesce settings.
2691  **/
2692 static int i40e_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
2693 				       struct ethtool_coalesce *ec)
2694 {
2695 	return __i40e_set_coalesce(netdev, ec, queue);
2696 }
2697 
2698 /**
2699  * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
2700  * @pf: pointer to the physical function struct
2701  * @cmd: ethtool rxnfc command
2702  *
2703  * Returns Success if the flow is supported, else Invalid Input.
2704  **/
2705 static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
2706 {
2707 	struct i40e_hw *hw = &pf->hw;
2708 	u8 flow_pctype = 0;
2709 	u64 i_set = 0;
2710 
2711 	cmd->data = 0;
2712 
2713 	switch (cmd->flow_type) {
2714 	case TCP_V4_FLOW:
2715 		flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
2716 		break;
2717 	case UDP_V4_FLOW:
2718 		flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
2719 		break;
2720 	case TCP_V6_FLOW:
2721 		flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
2722 		break;
2723 	case UDP_V6_FLOW:
2724 		flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
2725 		break;
2726 	case SCTP_V4_FLOW:
2727 	case AH_ESP_V4_FLOW:
2728 	case AH_V4_FLOW:
2729 	case ESP_V4_FLOW:
2730 	case IPV4_FLOW:
2731 	case SCTP_V6_FLOW:
2732 	case AH_ESP_V6_FLOW:
2733 	case AH_V6_FLOW:
2734 	case ESP_V6_FLOW:
2735 	case IPV6_FLOW:
2736 		/* Default is src/dest for IP, no matter the L4 hashing */
2737 		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
2738 		break;
2739 	default:
2740 		return -EINVAL;
2741 	}
2742 
2743 	/* Read flow based hash input set register */
2744 	if (flow_pctype) {
2745 		i_set = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0,
2746 					      flow_pctype)) |
2747 			((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1,
2748 					       flow_pctype)) << 32);
2749 	}
2750 
2751 	/* Process bits of hash input set */
2752 	if (i_set) {
2753 		if (i_set & I40E_L4_SRC_MASK)
2754 			cmd->data |= RXH_L4_B_0_1;
2755 		if (i_set & I40E_L4_DST_MASK)
2756 			cmd->data |= RXH_L4_B_2_3;
2757 
2758 		if (cmd->flow_type == TCP_V4_FLOW ||
2759 		    cmd->flow_type == UDP_V4_FLOW) {
2760 			if (i_set & I40E_L3_SRC_MASK)
2761 				cmd->data |= RXH_IP_SRC;
2762 			if (i_set & I40E_L3_DST_MASK)
2763 				cmd->data |= RXH_IP_DST;
2764 		} else if (cmd->flow_type == TCP_V6_FLOW ||
2765 			  cmd->flow_type == UDP_V6_FLOW) {
2766 			if (i_set & I40E_L3_V6_SRC_MASK)
2767 				cmd->data |= RXH_IP_SRC;
2768 			if (i_set & I40E_L3_V6_DST_MASK)
2769 				cmd->data |= RXH_IP_DST;
2770 		}
2771 	}
2772 
2773 	return 0;
2774 }
2775 
2776 /**
2777  * i40e_check_mask - Check whether a mask field is set
2778  * @mask: the full mask value
2779  * @field: mask of the field to check
2780  *
2781  * If the given mask is fully set, return positive value. If the mask for the
2782  * field is fully unset, return zero. Otherwise return a negative error code.
2783  **/
2784 static int i40e_check_mask(u64 mask, u64 field)
2785 {
2786 	u64 value = mask & field;
2787 
2788 	if (value == field)
2789 		return 1;
2790 	else if (!value)
2791 		return 0;
2792 	else
2793 		return -1;
2794 }
2795 
2796 /**
2797  * i40e_parse_rx_flow_user_data - Deconstruct user-defined data
2798  * @fsp: pointer to rx flow specification
2799  * @data: pointer to userdef data structure for storage
2800  *
2801  * Read the user-defined data and deconstruct the value into a structure. No
2802  * other code should read the user-defined data, so as to ensure that every
2803  * place consistently reads the value correctly.
2804  *
2805  * The user-defined field is a 64bit Big Endian format value, which we
2806  * deconstruct by reading bits or bit fields from it. Single bit flags shall
2807  * be defined starting from the highest bits, while small bit field values
2808  * shall be defined starting from the lowest bits.
2809  *
2810  * Returns 0 if the data is valid, and non-zero if the userdef data is invalid
2811  * and the filter should be rejected. The data structure will always be
2812  * modified even if FLOW_EXT is not set.
2813  *
2814  **/
2815 static int i40e_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
2816 					struct i40e_rx_flow_userdef *data)
2817 {
2818 	u64 value, mask;
2819 	int valid;
2820 
2821 	/* Zero memory first so it's always consistent. */
2822 	memset(data, 0, sizeof(*data));
2823 
2824 	if (!(fsp->flow_type & FLOW_EXT))
2825 		return 0;
2826 
2827 	value = be64_to_cpu(*((__be64 *)fsp->h_ext.data));
2828 	mask = be64_to_cpu(*((__be64 *)fsp->m_ext.data));
2829 
2830 #define I40E_USERDEF_FLEX_WORD		GENMASK_ULL(15, 0)
2831 #define I40E_USERDEF_FLEX_OFFSET	GENMASK_ULL(31, 16)
2832 #define I40E_USERDEF_FLEX_FILTER	GENMASK_ULL(31, 0)
2833 
2834 	valid = i40e_check_mask(mask, I40E_USERDEF_FLEX_FILTER);
2835 	if (valid < 0) {
2836 		return -EINVAL;
2837 	} else if (valid) {
2838 		data->flex_word = value & I40E_USERDEF_FLEX_WORD;
2839 		data->flex_offset =
2840 			(value & I40E_USERDEF_FLEX_OFFSET) >> 16;
2841 		data->flex_filter = true;
2842 	}
2843 
2844 	return 0;
2845 }
2846 
2847 /**
2848  * i40e_fill_rx_flow_user_data - Fill in user-defined data field
2849  * @fsp: pointer to rx_flow specification
2850  * @data: pointer to return userdef data
2851  *
2852  * Reads the userdef data structure and properly fills in the user defined
2853  * fields of the rx_flow_spec.
2854  **/
2855 static void i40e_fill_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
2856 					struct i40e_rx_flow_userdef *data)
2857 {
2858 	u64 value = 0, mask = 0;
2859 
2860 	if (data->flex_filter) {
2861 		value |= data->flex_word;
2862 		value |= (u64)data->flex_offset << 16;
2863 		mask |= I40E_USERDEF_FLEX_FILTER;
2864 	}
2865 
2866 	if (value || mask)
2867 		fsp->flow_type |= FLOW_EXT;
2868 
2869 	*((__be64 *)fsp->h_ext.data) = cpu_to_be64(value);
2870 	*((__be64 *)fsp->m_ext.data) = cpu_to_be64(mask);
2871 }
2872 
2873 /**
2874  * i40e_get_ethtool_fdir_all - Populates the rule count of a command
2875  * @pf: Pointer to the physical function struct
2876  * @cmd: The command to get or set Rx flow classification rules
2877  * @rule_locs: Array of used rule locations
2878  *
2879  * This function populates both the total and actual rule count of
2880  * the ethtool flow classification command
2881  *
2882  * Returns 0 on success or -EMSGSIZE if entry not found
2883  **/
2884 static int i40e_get_ethtool_fdir_all(struct i40e_pf *pf,
2885 				     struct ethtool_rxnfc *cmd,
2886 				     u32 *rule_locs)
2887 {
2888 	struct i40e_fdir_filter *rule;
2889 	struct hlist_node *node2;
2890 	int cnt = 0;
2891 
2892 	/* report total rule count */
2893 	cmd->data = i40e_get_fd_cnt_all(pf);
2894 
2895 	hlist_for_each_entry_safe(rule, node2,
2896 				  &pf->fdir_filter_list, fdir_node) {
2897 		if (cnt == cmd->rule_cnt)
2898 			return -EMSGSIZE;
2899 
2900 		rule_locs[cnt] = rule->fd_id;
2901 		cnt++;
2902 	}
2903 
2904 	cmd->rule_cnt = cnt;
2905 
2906 	return 0;
2907 }
2908 
2909 /**
2910  * i40e_get_ethtool_fdir_entry - Look up a filter based on Rx flow
2911  * @pf: Pointer to the physical function struct
2912  * @cmd: The command to get or set Rx flow classification rules
2913  *
2914  * This function looks up a filter based on the Rx flow classification
2915  * command and fills the flow spec info for it if found
2916  *
2917  * Returns 0 on success or -EINVAL if filter not found
2918  **/
2919 static int i40e_get_ethtool_fdir_entry(struct i40e_pf *pf,
2920 				       struct ethtool_rxnfc *cmd)
2921 {
2922 	struct ethtool_rx_flow_spec *fsp =
2923 			(struct ethtool_rx_flow_spec *)&cmd->fs;
2924 	struct i40e_rx_flow_userdef userdef = {0};
2925 	struct i40e_fdir_filter *rule = NULL;
2926 	struct hlist_node *node2;
2927 	u64 input_set;
2928 	u16 index;
2929 
2930 	hlist_for_each_entry_safe(rule, node2,
2931 				  &pf->fdir_filter_list, fdir_node) {
2932 		if (fsp->location <= rule->fd_id)
2933 			break;
2934 	}
2935 
2936 	if (!rule || fsp->location != rule->fd_id)
2937 		return -EINVAL;
2938 
2939 	fsp->flow_type = rule->flow_type;
2940 	if (fsp->flow_type == IP_USER_FLOW) {
2941 		fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
2942 		fsp->h_u.usr_ip4_spec.proto = 0;
2943 		fsp->m_u.usr_ip4_spec.proto = 0;
2944 	}
2945 
2946 	/* Reverse the src and dest notion, since the HW views them from
2947 	 * Tx perspective where as the user expects it from Rx filter view.
2948 	 */
2949 	fsp->h_u.tcp_ip4_spec.psrc = rule->dst_port;
2950 	fsp->h_u.tcp_ip4_spec.pdst = rule->src_port;
2951 	fsp->h_u.tcp_ip4_spec.ip4src = rule->dst_ip;
2952 	fsp->h_u.tcp_ip4_spec.ip4dst = rule->src_ip;
2953 
2954 	switch (rule->flow_type) {
2955 	case SCTP_V4_FLOW:
2956 		index = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
2957 		break;
2958 	case TCP_V4_FLOW:
2959 		index = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
2960 		break;
2961 	case UDP_V4_FLOW:
2962 		index = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
2963 		break;
2964 	case IP_USER_FLOW:
2965 		index = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
2966 		break;
2967 	default:
2968 		/* If we have stored a filter with a flow type not listed here
2969 		 * it is almost certainly a driver bug. WARN(), and then
2970 		 * assign the input_set as if all fields are enabled to avoid
2971 		 * reading unassigned memory.
2972 		 */
2973 		WARN(1, "Missing input set index for flow_type %d\n",
2974 		     rule->flow_type);
2975 		input_set = 0xFFFFFFFFFFFFFFFFULL;
2976 		goto no_input_set;
2977 	}
2978 
2979 	input_set = i40e_read_fd_input_set(pf, index);
2980 
2981 no_input_set:
2982 	if (input_set & I40E_L3_SRC_MASK)
2983 		fsp->m_u.tcp_ip4_spec.ip4src = htonl(0xFFFFFFFF);
2984 
2985 	if (input_set & I40E_L3_DST_MASK)
2986 		fsp->m_u.tcp_ip4_spec.ip4dst = htonl(0xFFFFFFFF);
2987 
2988 	if (input_set & I40E_L4_SRC_MASK)
2989 		fsp->m_u.tcp_ip4_spec.psrc = htons(0xFFFF);
2990 
2991 	if (input_set & I40E_L4_DST_MASK)
2992 		fsp->m_u.tcp_ip4_spec.pdst = htons(0xFFFF);
2993 
2994 	if (rule->dest_ctl == I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET)
2995 		fsp->ring_cookie = RX_CLS_FLOW_DISC;
2996 	else
2997 		fsp->ring_cookie = rule->q_index;
2998 
2999 	if (rule->dest_vsi != pf->vsi[pf->lan_vsi]->id) {
3000 		struct i40e_vsi *vsi;
3001 
3002 		vsi = i40e_find_vsi_from_id(pf, rule->dest_vsi);
3003 		if (vsi && vsi->type == I40E_VSI_SRIOV) {
3004 			/* VFs are zero-indexed by the driver, but ethtool
3005 			 * expects them to be one-indexed, so add one here
3006 			 */
3007 			u64 ring_vf = vsi->vf_id + 1;
3008 
3009 			ring_vf <<= ETHTOOL_RX_FLOW_SPEC_RING_VF_OFF;
3010 			fsp->ring_cookie |= ring_vf;
3011 		}
3012 	}
3013 
3014 	if (rule->flex_filter) {
3015 		userdef.flex_filter = true;
3016 		userdef.flex_word = be16_to_cpu(rule->flex_word);
3017 		userdef.flex_offset = rule->flex_offset;
3018 	}
3019 
3020 	i40e_fill_rx_flow_user_data(fsp, &userdef);
3021 
3022 	return 0;
3023 }
3024 
3025 /**
3026  * i40e_get_rxnfc - command to get RX flow classification rules
3027  * @netdev: network interface device structure
3028  * @cmd: ethtool rxnfc command
3029  * @rule_locs: pointer to store rule data
3030  *
3031  * Returns Success if the command is supported.
3032  **/
3033 static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3034 			  u32 *rule_locs)
3035 {
3036 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3037 	struct i40e_vsi *vsi = np->vsi;
3038 	struct i40e_pf *pf = vsi->back;
3039 	int ret = -EOPNOTSUPP;
3040 
3041 	switch (cmd->cmd) {
3042 	case ETHTOOL_GRXRINGS:
3043 		cmd->data = vsi->rss_size;
3044 		ret = 0;
3045 		break;
3046 	case ETHTOOL_GRXFH:
3047 		ret = i40e_get_rss_hash_opts(pf, cmd);
3048 		break;
3049 	case ETHTOOL_GRXCLSRLCNT:
3050 		cmd->rule_cnt = pf->fdir_pf_active_filters;
3051 		/* report total rule count */
3052 		cmd->data = i40e_get_fd_cnt_all(pf);
3053 		ret = 0;
3054 		break;
3055 	case ETHTOOL_GRXCLSRULE:
3056 		ret = i40e_get_ethtool_fdir_entry(pf, cmd);
3057 		break;
3058 	case ETHTOOL_GRXCLSRLALL:
3059 		ret = i40e_get_ethtool_fdir_all(pf, cmd, rule_locs);
3060 		break;
3061 	default:
3062 		break;
3063 	}
3064 
3065 	return ret;
3066 }
3067 
3068 /**
3069  * i40e_get_rss_hash_bits - Read RSS Hash bits from register
3070  * @nfc: pointer to user request
3071  * @i_setc: bits currently set
3072  *
3073  * Returns value of bits to be set per user request
3074  **/
3075 static u64 i40e_get_rss_hash_bits(struct ethtool_rxnfc *nfc, u64 i_setc)
3076 {
3077 	u64 i_set = i_setc;
3078 	u64 src_l3 = 0, dst_l3 = 0;
3079 
3080 	if (nfc->data & RXH_L4_B_0_1)
3081 		i_set |= I40E_L4_SRC_MASK;
3082 	else
3083 		i_set &= ~I40E_L4_SRC_MASK;
3084 	if (nfc->data & RXH_L4_B_2_3)
3085 		i_set |= I40E_L4_DST_MASK;
3086 	else
3087 		i_set &= ~I40E_L4_DST_MASK;
3088 
3089 	if (nfc->flow_type == TCP_V6_FLOW || nfc->flow_type == UDP_V6_FLOW) {
3090 		src_l3 = I40E_L3_V6_SRC_MASK;
3091 		dst_l3 = I40E_L3_V6_DST_MASK;
3092 	} else if (nfc->flow_type == TCP_V4_FLOW ||
3093 		  nfc->flow_type == UDP_V4_FLOW) {
3094 		src_l3 = I40E_L3_SRC_MASK;
3095 		dst_l3 = I40E_L3_DST_MASK;
3096 	} else {
3097 		/* Any other flow type are not supported here */
3098 		return i_set;
3099 	}
3100 
3101 	if (nfc->data & RXH_IP_SRC)
3102 		i_set |= src_l3;
3103 	else
3104 		i_set &= ~src_l3;
3105 	if (nfc->data & RXH_IP_DST)
3106 		i_set |= dst_l3;
3107 	else
3108 		i_set &= ~dst_l3;
3109 
3110 	return i_set;
3111 }
3112 
3113 /**
3114  * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
3115  * @pf: pointer to the physical function struct
3116  * @nfc: ethtool rxnfc command
3117  *
3118  * Returns Success if the flow input set is supported.
3119  **/
3120 static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
3121 {
3122 	struct i40e_hw *hw = &pf->hw;
3123 	u64 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
3124 		   ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
3125 	u8 flow_pctype = 0;
3126 	u64 i_set, i_setc;
3127 
3128 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3129 		dev_err(&pf->pdev->dev,
3130 			"Change of RSS hash input set is not supported when MFP mode is enabled\n");
3131 		return -EOPNOTSUPP;
3132 	}
3133 
3134 	/* RSS does not support anything other than hashing
3135 	 * to queues on src and dst IPs and ports
3136 	 */
3137 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
3138 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
3139 		return -EINVAL;
3140 
3141 	switch (nfc->flow_type) {
3142 	case TCP_V4_FLOW:
3143 		flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
3144 		if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
3145 			hena |=
3146 			  BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK);
3147 		break;
3148 	case TCP_V6_FLOW:
3149 		flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
3150 		if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
3151 			hena |=
3152 			  BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK);
3153 		if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
3154 			hena |=
3155 			  BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK);
3156 		break;
3157 	case UDP_V4_FLOW:
3158 		flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
3159 		if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
3160 			hena |=
3161 			  BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
3162 			  BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP);
3163 
3164 		hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
3165 		break;
3166 	case UDP_V6_FLOW:
3167 		flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
3168 		if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
3169 			hena |=
3170 			  BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
3171 			  BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP);
3172 
3173 		hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
3174 		break;
3175 	case AH_ESP_V4_FLOW:
3176 	case AH_V4_FLOW:
3177 	case ESP_V4_FLOW:
3178 	case SCTP_V4_FLOW:
3179 		if ((nfc->data & RXH_L4_B_0_1) ||
3180 		    (nfc->data & RXH_L4_B_2_3))
3181 			return -EINVAL;
3182 		hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
3183 		break;
3184 	case AH_ESP_V6_FLOW:
3185 	case AH_V6_FLOW:
3186 	case ESP_V6_FLOW:
3187 	case SCTP_V6_FLOW:
3188 		if ((nfc->data & RXH_L4_B_0_1) ||
3189 		    (nfc->data & RXH_L4_B_2_3))
3190 			return -EINVAL;
3191 		hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
3192 		break;
3193 	case IPV4_FLOW:
3194 		hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
3195 			BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
3196 		break;
3197 	case IPV6_FLOW:
3198 		hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
3199 			BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
3200 		break;
3201 	default:
3202 		return -EINVAL;
3203 	}
3204 
3205 	if (flow_pctype) {
3206 		i_setc = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0,
3207 					       flow_pctype)) |
3208 			((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1,
3209 					       flow_pctype)) << 32);
3210 		i_set = i40e_get_rss_hash_bits(nfc, i_setc);
3211 		i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(0, flow_pctype),
3212 				  (u32)i_set);
3213 		i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(1, flow_pctype),
3214 				  (u32)(i_set >> 32));
3215 		hena |= BIT_ULL(flow_pctype);
3216 	}
3217 
3218 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
3219 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
3220 	i40e_flush(hw);
3221 
3222 	return 0;
3223 }
3224 
3225 /**
3226  * i40e_update_ethtool_fdir_entry - Updates the fdir filter entry
3227  * @vsi: Pointer to the targeted VSI
3228  * @input: The filter to update or NULL to indicate deletion
3229  * @sw_idx: Software index to the filter
3230  * @cmd: The command to get or set Rx flow classification rules
3231  *
3232  * This function updates (or deletes) a Flow Director entry from
3233  * the hlist of the corresponding PF
3234  *
3235  * Returns 0 on success
3236  **/
3237 static int i40e_update_ethtool_fdir_entry(struct i40e_vsi *vsi,
3238 					  struct i40e_fdir_filter *input,
3239 					  u16 sw_idx,
3240 					  struct ethtool_rxnfc *cmd)
3241 {
3242 	struct i40e_fdir_filter *rule, *parent;
3243 	struct i40e_pf *pf = vsi->back;
3244 	struct hlist_node *node2;
3245 	int err = -EINVAL;
3246 
3247 	parent = NULL;
3248 	rule = NULL;
3249 
3250 	hlist_for_each_entry_safe(rule, node2,
3251 				  &pf->fdir_filter_list, fdir_node) {
3252 		/* hash found, or no matching entry */
3253 		if (rule->fd_id >= sw_idx)
3254 			break;
3255 		parent = rule;
3256 	}
3257 
3258 	/* if there is an old rule occupying our place remove it */
3259 	if (rule && (rule->fd_id == sw_idx)) {
3260 		/* Remove this rule, since we're either deleting it, or
3261 		 * replacing it.
3262 		 */
3263 		err = i40e_add_del_fdir(vsi, rule, false);
3264 		hlist_del(&rule->fdir_node);
3265 		kfree(rule);
3266 		pf->fdir_pf_active_filters--;
3267 	}
3268 
3269 	/* If we weren't given an input, this is a delete, so just return the
3270 	 * error code indicating if there was an entry at the requested slot
3271 	 */
3272 	if (!input)
3273 		return err;
3274 
3275 	/* Otherwise, install the new rule as requested */
3276 	INIT_HLIST_NODE(&input->fdir_node);
3277 
3278 	/* add filter to the list */
3279 	if (parent)
3280 		hlist_add_behind(&input->fdir_node, &parent->fdir_node);
3281 	else
3282 		hlist_add_head(&input->fdir_node,
3283 			       &pf->fdir_filter_list);
3284 
3285 	/* update counts */
3286 	pf->fdir_pf_active_filters++;
3287 
3288 	return 0;
3289 }
3290 
3291 /**
3292  * i40e_prune_flex_pit_list - Cleanup unused entries in FLX_PIT table
3293  * @pf: pointer to PF structure
3294  *
3295  * This function searches the list of filters and determines which FLX_PIT
3296  * entries are still required. It will prune any entries which are no longer
3297  * in use after the deletion.
3298  **/
3299 static void i40e_prune_flex_pit_list(struct i40e_pf *pf)
3300 {
3301 	struct i40e_flex_pit *entry, *tmp;
3302 	struct i40e_fdir_filter *rule;
3303 
3304 	/* First, we'll check the l3 table */
3305 	list_for_each_entry_safe(entry, tmp, &pf->l3_flex_pit_list, list) {
3306 		bool found = false;
3307 
3308 		hlist_for_each_entry(rule, &pf->fdir_filter_list, fdir_node) {
3309 			if (rule->flow_type != IP_USER_FLOW)
3310 				continue;
3311 			if (rule->flex_filter &&
3312 			    rule->flex_offset == entry->src_offset) {
3313 				found = true;
3314 				break;
3315 			}
3316 		}
3317 
3318 		/* If we didn't find the filter, then we can prune this entry
3319 		 * from the list.
3320 		 */
3321 		if (!found) {
3322 			list_del(&entry->list);
3323 			kfree(entry);
3324 		}
3325 	}
3326 
3327 	/* Followed by the L4 table */
3328 	list_for_each_entry_safe(entry, tmp, &pf->l4_flex_pit_list, list) {
3329 		bool found = false;
3330 
3331 		hlist_for_each_entry(rule, &pf->fdir_filter_list, fdir_node) {
3332 			/* Skip this filter if it's L3, since we already
3333 			 * checked those in the above loop
3334 			 */
3335 			if (rule->flow_type == IP_USER_FLOW)
3336 				continue;
3337 			if (rule->flex_filter &&
3338 			    rule->flex_offset == entry->src_offset) {
3339 				found = true;
3340 				break;
3341 			}
3342 		}
3343 
3344 		/* If we didn't find the filter, then we can prune this entry
3345 		 * from the list.
3346 		 */
3347 		if (!found) {
3348 			list_del(&entry->list);
3349 			kfree(entry);
3350 		}
3351 	}
3352 }
3353 
3354 /**
3355  * i40e_del_fdir_entry - Deletes a Flow Director filter entry
3356  * @vsi: Pointer to the targeted VSI
3357  * @cmd: The command to get or set Rx flow classification rules
3358  *
3359  * The function removes a Flow Director filter entry from the
3360  * hlist of the corresponding PF
3361  *
3362  * Returns 0 on success
3363  */
3364 static int i40e_del_fdir_entry(struct i40e_vsi *vsi,
3365 			       struct ethtool_rxnfc *cmd)
3366 {
3367 	struct ethtool_rx_flow_spec *fsp =
3368 		(struct ethtool_rx_flow_spec *)&cmd->fs;
3369 	struct i40e_pf *pf = vsi->back;
3370 	int ret = 0;
3371 
3372 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
3373 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
3374 		return -EBUSY;
3375 
3376 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
3377 		return -EBUSY;
3378 
3379 	ret = i40e_update_ethtool_fdir_entry(vsi, NULL, fsp->location, cmd);
3380 
3381 	i40e_prune_flex_pit_list(pf);
3382 
3383 	i40e_fdir_check_and_reenable(pf);
3384 	return ret;
3385 }
3386 
3387 /**
3388  * i40e_unused_pit_index - Find an unused PIT index for given list
3389  * @pf: the PF data structure
3390  *
3391  * Find the first unused flexible PIT index entry. We search both the L3 and
3392  * L4 flexible PIT lists so that the returned index is unique and unused by
3393  * either currently programmed L3 or L4 filters. We use a bit field as storage
3394  * to track which indexes are already used.
3395  **/
3396 static u8 i40e_unused_pit_index(struct i40e_pf *pf)
3397 {
3398 	unsigned long available_index = 0xFF;
3399 	struct i40e_flex_pit *entry;
3400 
3401 	/* We need to make sure that the new index isn't in use by either L3
3402 	 * or L4 filters so that IP_USER_FLOW filters can program both L3 and
3403 	 * L4 to use the same index.
3404 	 */
3405 
3406 	list_for_each_entry(entry, &pf->l4_flex_pit_list, list)
3407 		clear_bit(entry->pit_index, &available_index);
3408 
3409 	list_for_each_entry(entry, &pf->l3_flex_pit_list, list)
3410 		clear_bit(entry->pit_index, &available_index);
3411 
3412 	return find_first_bit(&available_index, 8);
3413 }
3414 
3415 /**
3416  * i40e_find_flex_offset - Find an existing flex src_offset
3417  * @flex_pit_list: L3 or L4 flex PIT list
3418  * @src_offset: new src_offset to find
3419  *
3420  * Searches the flex_pit_list for an existing offset. If no offset is
3421  * currently programmed, then this will return an ERR_PTR if there is no space
3422  * to add a new offset, otherwise it returns NULL.
3423  **/
3424 static
3425 struct i40e_flex_pit *i40e_find_flex_offset(struct list_head *flex_pit_list,
3426 					    u16 src_offset)
3427 {
3428 	struct i40e_flex_pit *entry;
3429 	int size = 0;
3430 
3431 	/* Search for the src_offset first. If we find a matching entry
3432 	 * already programmed, we can simply re-use it.
3433 	 */
3434 	list_for_each_entry(entry, flex_pit_list, list) {
3435 		size++;
3436 		if (entry->src_offset == src_offset)
3437 			return entry;
3438 	}
3439 
3440 	/* If we haven't found an entry yet, then the provided src offset has
3441 	 * not yet been programmed. We will program the src offset later on,
3442 	 * but we need to indicate whether there is enough space to do so
3443 	 * here. We'll make use of ERR_PTR for this purpose.
3444 	 */
3445 	if (size >= I40E_FLEX_PIT_TABLE_SIZE)
3446 		return ERR_PTR(-ENOSPC);
3447 
3448 	return NULL;
3449 }
3450 
3451 /**
3452  * i40e_add_flex_offset - Add src_offset to flex PIT table list
3453  * @flex_pit_list: L3 or L4 flex PIT list
3454  * @src_offset: new src_offset to add
3455  * @pit_index: the PIT index to program
3456  *
3457  * This function programs the new src_offset to the list. It is expected that
3458  * i40e_find_flex_offset has already been tried and returned NULL, indicating
3459  * that this offset is not programmed, and that the list has enough space to
3460  * store another offset.
3461  *
3462  * Returns 0 on success, and negative value on error.
3463  **/
3464 static int i40e_add_flex_offset(struct list_head *flex_pit_list,
3465 				u16 src_offset,
3466 				u8 pit_index)
3467 {
3468 	struct i40e_flex_pit *new_pit, *entry;
3469 
3470 	new_pit = kzalloc(sizeof(*entry), GFP_KERNEL);
3471 	if (!new_pit)
3472 		return -ENOMEM;
3473 
3474 	new_pit->src_offset = src_offset;
3475 	new_pit->pit_index = pit_index;
3476 
3477 	/* We need to insert this item such that the list is sorted by
3478 	 * src_offset in ascending order.
3479 	 */
3480 	list_for_each_entry(entry, flex_pit_list, list) {
3481 		if (new_pit->src_offset < entry->src_offset) {
3482 			list_add_tail(&new_pit->list, &entry->list);
3483 			return 0;
3484 		}
3485 
3486 		/* If we found an entry with our offset already programmed we
3487 		 * can simply return here, after freeing the memory. However,
3488 		 * if the pit_index does not match we need to report an error.
3489 		 */
3490 		if (new_pit->src_offset == entry->src_offset) {
3491 			int err = 0;
3492 
3493 			/* If the PIT index is not the same we can't re-use
3494 			 * the entry, so we must report an error.
3495 			 */
3496 			if (new_pit->pit_index != entry->pit_index)
3497 				err = -EINVAL;
3498 
3499 			kfree(new_pit);
3500 			return err;
3501 		}
3502 	}
3503 
3504 	/* If we reached here, then we haven't yet added the item. This means
3505 	 * that we should add the item at the end of the list.
3506 	 */
3507 	list_add_tail(&new_pit->list, flex_pit_list);
3508 	return 0;
3509 }
3510 
3511 /**
3512  * __i40e_reprogram_flex_pit - Re-program specific FLX_PIT table
3513  * @pf: Pointer to the PF structure
3514  * @flex_pit_list: list of flexible src offsets in use
3515  * @flex_pit_start: index to first entry for this section of the table
3516  *
3517  * In order to handle flexible data, the hardware uses a table of values
3518  * called the FLX_PIT table. This table is used to indicate which sections of
3519  * the input correspond to what PIT index values. Unfortunately, hardware is
3520  * very restrictive about programming this table. Entries must be ordered by
3521  * src_offset in ascending order, without duplicates. Additionally, unused
3522  * entries must be set to the unused index value, and must have valid size and
3523  * length according to the src_offset ordering.
3524  *
3525  * This function will reprogram the FLX_PIT register from a book-keeping
3526  * structure that we guarantee is already ordered correctly, and has no more
3527  * than 3 entries.
3528  *
3529  * To make things easier, we only support flexible values of one word length,
3530  * rather than allowing variable length flexible values.
3531  **/
3532 static void __i40e_reprogram_flex_pit(struct i40e_pf *pf,
3533 				      struct list_head *flex_pit_list,
3534 				      int flex_pit_start)
3535 {
3536 	struct i40e_flex_pit *entry = NULL;
3537 	u16 last_offset = 0;
3538 	int i = 0, j = 0;
3539 
3540 	/* First, loop over the list of flex PIT entries, and reprogram the
3541 	 * registers.
3542 	 */
3543 	list_for_each_entry(entry, flex_pit_list, list) {
3544 		/* We have to be careful when programming values for the
3545 		 * largest SRC_OFFSET value. It is possible that adding
3546 		 * additional empty values at the end would overflow the space
3547 		 * for the SRC_OFFSET in the FLX_PIT register. To avoid this,
3548 		 * we check here and add the empty values prior to adding the
3549 		 * largest value.
3550 		 *
3551 		 * To determine this, we will use a loop from i+1 to 3, which
3552 		 * will determine whether the unused entries would have valid
3553 		 * SRC_OFFSET. Note that there cannot be extra entries past
3554 		 * this value, because the only valid values would have been
3555 		 * larger than I40E_MAX_FLEX_SRC_OFFSET, and thus would not
3556 		 * have been added to the list in the first place.
3557 		 */
3558 		for (j = i + 1; j < 3; j++) {
3559 			u16 offset = entry->src_offset + j;
3560 			int index = flex_pit_start + i;
3561 			u32 value = I40E_FLEX_PREP_VAL(I40E_FLEX_DEST_UNUSED,
3562 						       1,
3563 						       offset - 3);
3564 
3565 			if (offset > I40E_MAX_FLEX_SRC_OFFSET) {
3566 				i40e_write_rx_ctl(&pf->hw,
3567 						  I40E_PRTQF_FLX_PIT(index),
3568 						  value);
3569 				i++;
3570 			}
3571 		}
3572 
3573 		/* Now, we can program the actual value into the table */
3574 		i40e_write_rx_ctl(&pf->hw,
3575 				  I40E_PRTQF_FLX_PIT(flex_pit_start + i),
3576 				  I40E_FLEX_PREP_VAL(entry->pit_index + 50,
3577 						     1,
3578 						     entry->src_offset));
3579 		i++;
3580 	}
3581 
3582 	/* In order to program the last entries in the table, we need to
3583 	 * determine the valid offset. If the list is empty, we'll just start
3584 	 * with 0. Otherwise, we'll start with the last item offset and add 1.
3585 	 * This ensures that all entries have valid sizes. If we don't do this
3586 	 * correctly, the hardware will disable flexible field parsing.
3587 	 */
3588 	if (!list_empty(flex_pit_list))
3589 		last_offset = list_prev_entry(entry, list)->src_offset + 1;
3590 
3591 	for (; i < 3; i++, last_offset++) {
3592 		i40e_write_rx_ctl(&pf->hw,
3593 				  I40E_PRTQF_FLX_PIT(flex_pit_start + i),
3594 				  I40E_FLEX_PREP_VAL(I40E_FLEX_DEST_UNUSED,
3595 						     1,
3596 						     last_offset));
3597 	}
3598 }
3599 
3600 /**
3601  * i40e_reprogram_flex_pit - Reprogram all FLX_PIT tables after input set change
3602  * @pf: pointer to the PF structure
3603  *
3604  * This function reprograms both the L3 and L4 FLX_PIT tables. See the
3605  * internal helper function for implementation details.
3606  **/
3607 static void i40e_reprogram_flex_pit(struct i40e_pf *pf)
3608 {
3609 	__i40e_reprogram_flex_pit(pf, &pf->l3_flex_pit_list,
3610 				  I40E_FLEX_PIT_IDX_START_L3);
3611 
3612 	__i40e_reprogram_flex_pit(pf, &pf->l4_flex_pit_list,
3613 				  I40E_FLEX_PIT_IDX_START_L4);
3614 
3615 	/* We also need to program the L3 and L4 GLQF ORT register */
3616 	i40e_write_rx_ctl(&pf->hw,
3617 			  I40E_GLQF_ORT(I40E_L3_GLQF_ORT_IDX),
3618 			  I40E_ORT_PREP_VAL(I40E_FLEX_PIT_IDX_START_L3,
3619 					    3, 1));
3620 
3621 	i40e_write_rx_ctl(&pf->hw,
3622 			  I40E_GLQF_ORT(I40E_L4_GLQF_ORT_IDX),
3623 			  I40E_ORT_PREP_VAL(I40E_FLEX_PIT_IDX_START_L4,
3624 					    3, 1));
3625 }
3626 
3627 /**
3628  * i40e_flow_str - Converts a flow_type into a human readable string
3629  * @fsp: the flow specification
3630  *
3631  * Currently only flow types we support are included here, and the string
3632  * value attempts to match what ethtool would use to configure this flow type.
3633  **/
3634 static const char *i40e_flow_str(struct ethtool_rx_flow_spec *fsp)
3635 {
3636 	switch (fsp->flow_type & ~FLOW_EXT) {
3637 	case TCP_V4_FLOW:
3638 		return "tcp4";
3639 	case UDP_V4_FLOW:
3640 		return "udp4";
3641 	case SCTP_V4_FLOW:
3642 		return "sctp4";
3643 	case IP_USER_FLOW:
3644 		return "ip4";
3645 	default:
3646 		return "unknown";
3647 	}
3648 }
3649 
3650 /**
3651  * i40e_pit_index_to_mask - Return the FLEX mask for a given PIT index
3652  * @pit_index: PIT index to convert
3653  *
3654  * Returns the mask for a given PIT index. Will return 0 if the pit_index is
3655  * of range.
3656  **/
3657 static u64 i40e_pit_index_to_mask(int pit_index)
3658 {
3659 	switch (pit_index) {
3660 	case 0:
3661 		return I40E_FLEX_50_MASK;
3662 	case 1:
3663 		return I40E_FLEX_51_MASK;
3664 	case 2:
3665 		return I40E_FLEX_52_MASK;
3666 	case 3:
3667 		return I40E_FLEX_53_MASK;
3668 	case 4:
3669 		return I40E_FLEX_54_MASK;
3670 	case 5:
3671 		return I40E_FLEX_55_MASK;
3672 	case 6:
3673 		return I40E_FLEX_56_MASK;
3674 	case 7:
3675 		return I40E_FLEX_57_MASK;
3676 	default:
3677 		return 0;
3678 	}
3679 }
3680 
3681 /**
3682  * i40e_print_input_set - Show changes between two input sets
3683  * @vsi: the vsi being configured
3684  * @old: the old input set
3685  * @new: the new input set
3686  *
3687  * Print the difference between old and new input sets by showing which series
3688  * of words are toggled on or off. Only displays the bits we actually support
3689  * changing.
3690  **/
3691 static void i40e_print_input_set(struct i40e_vsi *vsi, u64 old, u64 new)
3692 {
3693 	struct i40e_pf *pf = vsi->back;
3694 	bool old_value, new_value;
3695 	int i;
3696 
3697 	old_value = !!(old & I40E_L3_SRC_MASK);
3698 	new_value = !!(new & I40E_L3_SRC_MASK);
3699 	if (old_value != new_value)
3700 		netif_info(pf, drv, vsi->netdev, "L3 source address: %s -> %s\n",
3701 			   old_value ? "ON" : "OFF",
3702 			   new_value ? "ON" : "OFF");
3703 
3704 	old_value = !!(old & I40E_L3_DST_MASK);
3705 	new_value = !!(new & I40E_L3_DST_MASK);
3706 	if (old_value != new_value)
3707 		netif_info(pf, drv, vsi->netdev, "L3 destination address: %s -> %s\n",
3708 			   old_value ? "ON" : "OFF",
3709 			   new_value ? "ON" : "OFF");
3710 
3711 	old_value = !!(old & I40E_L4_SRC_MASK);
3712 	new_value = !!(new & I40E_L4_SRC_MASK);
3713 	if (old_value != new_value)
3714 		netif_info(pf, drv, vsi->netdev, "L4 source port: %s -> %s\n",
3715 			   old_value ? "ON" : "OFF",
3716 			   new_value ? "ON" : "OFF");
3717 
3718 	old_value = !!(old & I40E_L4_DST_MASK);
3719 	new_value = !!(new & I40E_L4_DST_MASK);
3720 	if (old_value != new_value)
3721 		netif_info(pf, drv, vsi->netdev, "L4 destination port: %s -> %s\n",
3722 			   old_value ? "ON" : "OFF",
3723 			   new_value ? "ON" : "OFF");
3724 
3725 	old_value = !!(old & I40E_VERIFY_TAG_MASK);
3726 	new_value = !!(new & I40E_VERIFY_TAG_MASK);
3727 	if (old_value != new_value)
3728 		netif_info(pf, drv, vsi->netdev, "SCTP verification tag: %s -> %s\n",
3729 			   old_value ? "ON" : "OFF",
3730 			   new_value ? "ON" : "OFF");
3731 
3732 	/* Show change of flexible filter entries */
3733 	for (i = 0; i < I40E_FLEX_INDEX_ENTRIES; i++) {
3734 		u64 flex_mask = i40e_pit_index_to_mask(i);
3735 
3736 		old_value = !!(old & flex_mask);
3737 		new_value = !!(new & flex_mask);
3738 		if (old_value != new_value)
3739 			netif_info(pf, drv, vsi->netdev, "FLEX index %d: %s -> %s\n",
3740 				   i,
3741 				   old_value ? "ON" : "OFF",
3742 				   new_value ? "ON" : "OFF");
3743 	}
3744 
3745 	netif_info(pf, drv, vsi->netdev, "  Current input set: %0llx\n",
3746 		   old);
3747 	netif_info(pf, drv, vsi->netdev, "Requested input set: %0llx\n",
3748 		   new);
3749 }
3750 
3751 /**
3752  * i40e_check_fdir_input_set - Check that a given rx_flow_spec mask is valid
3753  * @vsi: pointer to the targeted VSI
3754  * @fsp: pointer to Rx flow specification
3755  * @userdef: userdefined data from flow specification
3756  *
3757  * Ensures that a given ethtool_rx_flow_spec has a valid mask. Some support
3758  * for partial matches exists with a few limitations. First, hardware only
3759  * supports masking by word boundary (2 bytes) and not per individual bit.
3760  * Second, hardware is limited to using one mask for a flow type and cannot
3761  * use a separate mask for each filter.
3762  *
3763  * To support these limitations, if we already have a configured filter for
3764  * the specified type, this function enforces that new filters of the type
3765  * match the configured input set. Otherwise, if we do not have a filter of
3766  * the specified type, we allow the input set to be updated to match the
3767  * desired filter.
3768  *
3769  * To help ensure that administrators understand why filters weren't displayed
3770  * as supported, we print a diagnostic message displaying how the input set
3771  * would change and warning to delete the preexisting filters if required.
3772  *
3773  * Returns 0 on successful input set match, and a negative return code on
3774  * failure.
3775  **/
3776 static int i40e_check_fdir_input_set(struct i40e_vsi *vsi,
3777 				     struct ethtool_rx_flow_spec *fsp,
3778 				     struct i40e_rx_flow_userdef *userdef)
3779 {
3780 	struct i40e_pf *pf = vsi->back;
3781 	struct ethtool_tcpip4_spec *tcp_ip4_spec;
3782 	struct ethtool_usrip4_spec *usr_ip4_spec;
3783 	u64 current_mask, new_mask;
3784 	bool new_flex_offset = false;
3785 	bool flex_l3 = false;
3786 	u16 *fdir_filter_count;
3787 	u16 index, src_offset = 0;
3788 	u8 pit_index = 0;
3789 	int err;
3790 
3791 	switch (fsp->flow_type & ~FLOW_EXT) {
3792 	case SCTP_V4_FLOW:
3793 		index = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
3794 		fdir_filter_count = &pf->fd_sctp4_filter_cnt;
3795 		break;
3796 	case TCP_V4_FLOW:
3797 		index = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
3798 		fdir_filter_count = &pf->fd_tcp4_filter_cnt;
3799 		break;
3800 	case UDP_V4_FLOW:
3801 		index = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
3802 		fdir_filter_count = &pf->fd_udp4_filter_cnt;
3803 		break;
3804 	case IP_USER_FLOW:
3805 		index = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
3806 		fdir_filter_count = &pf->fd_ip4_filter_cnt;
3807 		flex_l3 = true;
3808 		break;
3809 	default:
3810 		return -EOPNOTSUPP;
3811 	}
3812 
3813 	/* Read the current input set from register memory. */
3814 	current_mask = i40e_read_fd_input_set(pf, index);
3815 	new_mask = current_mask;
3816 
3817 	/* Determine, if any, the required changes to the input set in order
3818 	 * to support the provided mask.
3819 	 *
3820 	 * Hardware only supports masking at word (2 byte) granularity and does
3821 	 * not support full bitwise masking. This implementation simplifies
3822 	 * even further and only supports fully enabled or fully disabled
3823 	 * masks for each field, even though we could split the ip4src and
3824 	 * ip4dst fields.
3825 	 */
3826 	switch (fsp->flow_type & ~FLOW_EXT) {
3827 	case SCTP_V4_FLOW:
3828 		new_mask &= ~I40E_VERIFY_TAG_MASK;
3829 		/* Fall through */
3830 	case TCP_V4_FLOW:
3831 	case UDP_V4_FLOW:
3832 		tcp_ip4_spec = &fsp->m_u.tcp_ip4_spec;
3833 
3834 		/* IPv4 source address */
3835 		if (tcp_ip4_spec->ip4src == htonl(0xFFFFFFFF))
3836 			new_mask |= I40E_L3_SRC_MASK;
3837 		else if (!tcp_ip4_spec->ip4src)
3838 			new_mask &= ~I40E_L3_SRC_MASK;
3839 		else
3840 			return -EOPNOTSUPP;
3841 
3842 		/* IPv4 destination address */
3843 		if (tcp_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
3844 			new_mask |= I40E_L3_DST_MASK;
3845 		else if (!tcp_ip4_spec->ip4dst)
3846 			new_mask &= ~I40E_L3_DST_MASK;
3847 		else
3848 			return -EOPNOTSUPP;
3849 
3850 		/* L4 source port */
3851 		if (tcp_ip4_spec->psrc == htons(0xFFFF))
3852 			new_mask |= I40E_L4_SRC_MASK;
3853 		else if (!tcp_ip4_spec->psrc)
3854 			new_mask &= ~I40E_L4_SRC_MASK;
3855 		else
3856 			return -EOPNOTSUPP;
3857 
3858 		/* L4 destination port */
3859 		if (tcp_ip4_spec->pdst == htons(0xFFFF))
3860 			new_mask |= I40E_L4_DST_MASK;
3861 		else if (!tcp_ip4_spec->pdst)
3862 			new_mask &= ~I40E_L4_DST_MASK;
3863 		else
3864 			return -EOPNOTSUPP;
3865 
3866 		/* Filtering on Type of Service is not supported. */
3867 		if (tcp_ip4_spec->tos)
3868 			return -EOPNOTSUPP;
3869 
3870 		break;
3871 	case IP_USER_FLOW:
3872 		usr_ip4_spec = &fsp->m_u.usr_ip4_spec;
3873 
3874 		/* IPv4 source address */
3875 		if (usr_ip4_spec->ip4src == htonl(0xFFFFFFFF))
3876 			new_mask |= I40E_L3_SRC_MASK;
3877 		else if (!usr_ip4_spec->ip4src)
3878 			new_mask &= ~I40E_L3_SRC_MASK;
3879 		else
3880 			return -EOPNOTSUPP;
3881 
3882 		/* IPv4 destination address */
3883 		if (usr_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
3884 			new_mask |= I40E_L3_DST_MASK;
3885 		else if (!usr_ip4_spec->ip4dst)
3886 			new_mask &= ~I40E_L3_DST_MASK;
3887 		else
3888 			return -EOPNOTSUPP;
3889 
3890 		/* First 4 bytes of L4 header */
3891 		if (usr_ip4_spec->l4_4_bytes == htonl(0xFFFFFFFF))
3892 			new_mask |= I40E_L4_SRC_MASK | I40E_L4_DST_MASK;
3893 		else if (!usr_ip4_spec->l4_4_bytes)
3894 			new_mask &= ~(I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
3895 		else
3896 			return -EOPNOTSUPP;
3897 
3898 		/* Filtering on Type of Service is not supported. */
3899 		if (usr_ip4_spec->tos)
3900 			return -EOPNOTSUPP;
3901 
3902 		/* Filtering on IP version is not supported */
3903 		if (usr_ip4_spec->ip_ver)
3904 			return -EINVAL;
3905 
3906 		/* Filtering on L4 protocol is not supported */
3907 		if (usr_ip4_spec->proto)
3908 			return -EINVAL;
3909 
3910 		break;
3911 	default:
3912 		return -EOPNOTSUPP;
3913 	}
3914 
3915 	/* First, clear all flexible filter entries */
3916 	new_mask &= ~I40E_FLEX_INPUT_MASK;
3917 
3918 	/* If we have a flexible filter, try to add this offset to the correct
3919 	 * flexible filter PIT list. Once finished, we can update the mask.
3920 	 * If the src_offset changed, we will get a new mask value which will
3921 	 * trigger an input set change.
3922 	 */
3923 	if (userdef->flex_filter) {
3924 		struct i40e_flex_pit *l3_flex_pit = NULL, *flex_pit = NULL;
3925 
3926 		/* Flexible offset must be even, since the flexible payload
3927 		 * must be aligned on 2-byte boundary.
3928 		 */
3929 		if (userdef->flex_offset & 0x1) {
3930 			dev_warn(&pf->pdev->dev,
3931 				 "Flexible data offset must be 2-byte aligned\n");
3932 			return -EINVAL;
3933 		}
3934 
3935 		src_offset = userdef->flex_offset >> 1;
3936 
3937 		/* FLX_PIT source offset value is only so large */
3938 		if (src_offset > I40E_MAX_FLEX_SRC_OFFSET) {
3939 			dev_warn(&pf->pdev->dev,
3940 				 "Flexible data must reside within first 64 bytes of the packet payload\n");
3941 			return -EINVAL;
3942 		}
3943 
3944 		/* See if this offset has already been programmed. If we get
3945 		 * an ERR_PTR, then the filter is not safe to add. Otherwise,
3946 		 * if we get a NULL pointer, this means we will need to add
3947 		 * the offset.
3948 		 */
3949 		flex_pit = i40e_find_flex_offset(&pf->l4_flex_pit_list,
3950 						 src_offset);
3951 		if (IS_ERR(flex_pit))
3952 			return PTR_ERR(flex_pit);
3953 
3954 		/* IP_USER_FLOW filters match both L4 (ICMP) and L3 (unknown)
3955 		 * packet types, and thus we need to program both L3 and L4
3956 		 * flexible values. These must have identical flexible index,
3957 		 * as otherwise we can't correctly program the input set. So
3958 		 * we'll find both an L3 and L4 index and make sure they are
3959 		 * the same.
3960 		 */
3961 		if (flex_l3) {
3962 			l3_flex_pit =
3963 				i40e_find_flex_offset(&pf->l3_flex_pit_list,
3964 						      src_offset);
3965 			if (IS_ERR(l3_flex_pit))
3966 				return PTR_ERR(l3_flex_pit);
3967 
3968 			if (flex_pit) {
3969 				/* If we already had a matching L4 entry, we
3970 				 * need to make sure that the L3 entry we
3971 				 * obtained uses the same index.
3972 				 */
3973 				if (l3_flex_pit) {
3974 					if (l3_flex_pit->pit_index !=
3975 					    flex_pit->pit_index) {
3976 						return -EINVAL;
3977 					}
3978 				} else {
3979 					new_flex_offset = true;
3980 				}
3981 			} else {
3982 				flex_pit = l3_flex_pit;
3983 			}
3984 		}
3985 
3986 		/* If we didn't find an existing flex offset, we need to
3987 		 * program a new one. However, we don't immediately program it
3988 		 * here because we will wait to program until after we check
3989 		 * that it is safe to change the input set.
3990 		 */
3991 		if (!flex_pit) {
3992 			new_flex_offset = true;
3993 			pit_index = i40e_unused_pit_index(pf);
3994 		} else {
3995 			pit_index = flex_pit->pit_index;
3996 		}
3997 
3998 		/* Update the mask with the new offset */
3999 		new_mask |= i40e_pit_index_to_mask(pit_index);
4000 	}
4001 
4002 	/* If the mask and flexible filter offsets for this filter match the
4003 	 * currently programmed values we don't need any input set change, so
4004 	 * this filter is safe to install.
4005 	 */
4006 	if (new_mask == current_mask && !new_flex_offset)
4007 		return 0;
4008 
4009 	netif_info(pf, drv, vsi->netdev, "Input set change requested for %s flows:\n",
4010 		   i40e_flow_str(fsp));
4011 	i40e_print_input_set(vsi, current_mask, new_mask);
4012 	if (new_flex_offset) {
4013 		netif_info(pf, drv, vsi->netdev, "FLEX index %d: Offset -> %d",
4014 			   pit_index, src_offset);
4015 	}
4016 
4017 	/* Hardware input sets are global across multiple ports, so even the
4018 	 * main port cannot change them when in MFP mode as this would impact
4019 	 * any filters on the other ports.
4020 	 */
4021 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4022 		netif_err(pf, drv, vsi->netdev, "Cannot change Flow Director input sets while MFP is enabled\n");
4023 		return -EOPNOTSUPP;
4024 	}
4025 
4026 	/* This filter requires us to update the input set. However, hardware
4027 	 * only supports one input set per flow type, and does not support
4028 	 * separate masks for each filter. This means that we can only support
4029 	 * a single mask for all filters of a specific type.
4030 	 *
4031 	 * If we have preexisting filters, they obviously depend on the
4032 	 * current programmed input set. Display a diagnostic message in this
4033 	 * case explaining why the filter could not be accepted.
4034 	 */
4035 	if (*fdir_filter_count) {
4036 		netif_err(pf, drv, vsi->netdev, "Cannot change input set for %s flows until %d preexisting filters are removed\n",
4037 			  i40e_flow_str(fsp),
4038 			  *fdir_filter_count);
4039 		return -EOPNOTSUPP;
4040 	}
4041 
4042 	i40e_write_fd_input_set(pf, index, new_mask);
4043 
4044 	/* IP_USER_FLOW filters match both IPv4/Other and IPv4/Fragmented
4045 	 * frames. If we're programming the input set for IPv4/Other, we also
4046 	 * need to program the IPv4/Fragmented input set. Since we don't have
4047 	 * separate support, we'll always assume and enforce that the two flow
4048 	 * types must have matching input sets.
4049 	 */
4050 	if (index == I40E_FILTER_PCTYPE_NONF_IPV4_OTHER)
4051 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
4052 					new_mask);
4053 
4054 	/* Add the new offset and update table, if necessary */
4055 	if (new_flex_offset) {
4056 		err = i40e_add_flex_offset(&pf->l4_flex_pit_list, src_offset,
4057 					   pit_index);
4058 		if (err)
4059 			return err;
4060 
4061 		if (flex_l3) {
4062 			err = i40e_add_flex_offset(&pf->l3_flex_pit_list,
4063 						   src_offset,
4064 						   pit_index);
4065 			if (err)
4066 				return err;
4067 		}
4068 
4069 		i40e_reprogram_flex_pit(pf);
4070 	}
4071 
4072 	return 0;
4073 }
4074 
4075 /**
4076  * i40e_match_fdir_filter - Return true of two filters match
4077  * @a: pointer to filter struct
4078  * @b: pointer to filter struct
4079  *
4080  * Returns true if the two filters match exactly the same criteria. I.e. they
4081  * match the same flow type and have the same parameters. We don't need to
4082  * check any input-set since all filters of the same flow type must use the
4083  * same input set.
4084  **/
4085 static bool i40e_match_fdir_filter(struct i40e_fdir_filter *a,
4086 				   struct i40e_fdir_filter *b)
4087 {
4088 	/* The filters do not much if any of these criteria differ. */
4089 	if (a->dst_ip != b->dst_ip ||
4090 	    a->src_ip != b->src_ip ||
4091 	    a->dst_port != b->dst_port ||
4092 	    a->src_port != b->src_port ||
4093 	    a->flow_type != b->flow_type ||
4094 	    a->ip4_proto != b->ip4_proto)
4095 		return false;
4096 
4097 	return true;
4098 }
4099 
4100 /**
4101  * i40e_disallow_matching_filters - Check that new filters differ
4102  * @vsi: pointer to the targeted VSI
4103  * @input: new filter to check
4104  *
4105  * Due to hardware limitations, it is not possible for two filters that match
4106  * similar criteria to be programmed at the same time. This is true for a few
4107  * reasons:
4108  *
4109  * (a) all filters matching a particular flow type must use the same input
4110  * set, that is they must match the same criteria.
4111  * (b) different flow types will never match the same packet, as the flow type
4112  * is decided by hardware before checking which rules apply.
4113  * (c) hardware has no way to distinguish which order filters apply in.
4114  *
4115  * Due to this, we can't really support using the location data to order
4116  * filters in the hardware parsing. It is technically possible for the user to
4117  * request two filters matching the same criteria but which select different
4118  * queues. In this case, rather than keep both filters in the list, we reject
4119  * the 2nd filter when the user requests adding it.
4120  *
4121  * This avoids needing to track location for programming the filter to
4122  * hardware, and ensures that we avoid some strange scenarios involving
4123  * deleting filters which match the same criteria.
4124  **/
4125 static int i40e_disallow_matching_filters(struct i40e_vsi *vsi,
4126 					  struct i40e_fdir_filter *input)
4127 {
4128 	struct i40e_pf *pf = vsi->back;
4129 	struct i40e_fdir_filter *rule;
4130 	struct hlist_node *node2;
4131 
4132 	/* Loop through every filter, and check that it doesn't match */
4133 	hlist_for_each_entry_safe(rule, node2,
4134 				  &pf->fdir_filter_list, fdir_node) {
4135 		/* Don't check the filters match if they share the same fd_id,
4136 		 * since the new filter is actually just updating the target
4137 		 * of the old filter.
4138 		 */
4139 		if (rule->fd_id == input->fd_id)
4140 			continue;
4141 
4142 		/* If any filters match, then print a warning message to the
4143 		 * kernel message buffer and bail out.
4144 		 */
4145 		if (i40e_match_fdir_filter(rule, input)) {
4146 			dev_warn(&pf->pdev->dev,
4147 				 "Existing user defined filter %d already matches this flow.\n",
4148 				 rule->fd_id);
4149 			return -EINVAL;
4150 		}
4151 	}
4152 
4153 	return 0;
4154 }
4155 
4156 /**
4157  * i40e_add_fdir_ethtool - Add/Remove Flow Director filters
4158  * @vsi: pointer to the targeted VSI
4159  * @cmd: command to get or set RX flow classification rules
4160  *
4161  * Add Flow Director filters for a specific flow spec based on their
4162  * protocol.  Returns 0 if the filters were successfully added.
4163  **/
4164 static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
4165 				 struct ethtool_rxnfc *cmd)
4166 {
4167 	struct i40e_rx_flow_userdef userdef;
4168 	struct ethtool_rx_flow_spec *fsp;
4169 	struct i40e_fdir_filter *input;
4170 	u16 dest_vsi = 0, q_index = 0;
4171 	struct i40e_pf *pf;
4172 	int ret = -EINVAL;
4173 	u8 dest_ctl;
4174 
4175 	if (!vsi)
4176 		return -EINVAL;
4177 	pf = vsi->back;
4178 
4179 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
4180 		return -EOPNOTSUPP;
4181 
4182 	if (test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
4183 		return -ENOSPC;
4184 
4185 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
4186 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
4187 		return -EBUSY;
4188 
4189 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
4190 		return -EBUSY;
4191 
4192 	fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
4193 
4194 	/* Parse the user-defined field */
4195 	if (i40e_parse_rx_flow_user_data(fsp, &userdef))
4196 		return -EINVAL;
4197 
4198 	/* Extended MAC field is not supported */
4199 	if (fsp->flow_type & FLOW_MAC_EXT)
4200 		return -EINVAL;
4201 
4202 	ret = i40e_check_fdir_input_set(vsi, fsp, &userdef);
4203 	if (ret)
4204 		return ret;
4205 
4206 	if (fsp->location >= (pf->hw.func_caps.fd_filters_best_effort +
4207 			      pf->hw.func_caps.fd_filters_guaranteed)) {
4208 		return -EINVAL;
4209 	}
4210 
4211 	/* ring_cookie is either the drop index, or is a mask of the queue
4212 	 * index and VF id we wish to target.
4213 	 */
4214 	if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
4215 		dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
4216 	} else {
4217 		u32 ring = ethtool_get_flow_spec_ring(fsp->ring_cookie);
4218 		u8 vf = ethtool_get_flow_spec_ring_vf(fsp->ring_cookie);
4219 
4220 		if (!vf) {
4221 			if (ring >= vsi->num_queue_pairs)
4222 				return -EINVAL;
4223 			dest_vsi = vsi->id;
4224 		} else {
4225 			/* VFs are zero-indexed, so we subtract one here */
4226 			vf--;
4227 
4228 			if (vf >= pf->num_alloc_vfs)
4229 				return -EINVAL;
4230 			if (ring >= pf->vf[vf].num_queue_pairs)
4231 				return -EINVAL;
4232 			dest_vsi = pf->vf[vf].lan_vsi_id;
4233 		}
4234 		dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX;
4235 		q_index = ring;
4236 	}
4237 
4238 	input = kzalloc(sizeof(*input), GFP_KERNEL);
4239 
4240 	if (!input)
4241 		return -ENOMEM;
4242 
4243 	input->fd_id = fsp->location;
4244 	input->q_index = q_index;
4245 	input->dest_vsi = dest_vsi;
4246 	input->dest_ctl = dest_ctl;
4247 	input->fd_status = I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID;
4248 	input->cnt_index  = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
4249 	input->dst_ip = fsp->h_u.tcp_ip4_spec.ip4src;
4250 	input->src_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
4251 	input->flow_type = fsp->flow_type & ~FLOW_EXT;
4252 	input->ip4_proto = fsp->h_u.usr_ip4_spec.proto;
4253 
4254 	/* Reverse the src and dest notion, since the HW expects them to be from
4255 	 * Tx perspective where as the input from user is from Rx filter view.
4256 	 */
4257 	input->dst_port = fsp->h_u.tcp_ip4_spec.psrc;
4258 	input->src_port = fsp->h_u.tcp_ip4_spec.pdst;
4259 	input->dst_ip = fsp->h_u.tcp_ip4_spec.ip4src;
4260 	input->src_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
4261 
4262 	if (userdef.flex_filter) {
4263 		input->flex_filter = true;
4264 		input->flex_word = cpu_to_be16(userdef.flex_word);
4265 		input->flex_offset = userdef.flex_offset;
4266 	}
4267 
4268 	/* Avoid programming two filters with identical match criteria. */
4269 	ret = i40e_disallow_matching_filters(vsi, input);
4270 	if (ret)
4271 		goto free_filter_memory;
4272 
4273 	/* Add the input filter to the fdir_input_list, possibly replacing
4274 	 * a previous filter. Do not free the input structure after adding it
4275 	 * to the list as this would cause a use-after-free bug.
4276 	 */
4277 	i40e_update_ethtool_fdir_entry(vsi, input, fsp->location, NULL);
4278 	ret = i40e_add_del_fdir(vsi, input, true);
4279 	if (ret)
4280 		goto remove_sw_rule;
4281 	return 0;
4282 
4283 remove_sw_rule:
4284 	hlist_del(&input->fdir_node);
4285 	pf->fdir_pf_active_filters--;
4286 free_filter_memory:
4287 	kfree(input);
4288 	return ret;
4289 }
4290 
4291 /**
4292  * i40e_set_rxnfc - command to set RX flow classification rules
4293  * @netdev: network interface device structure
4294  * @cmd: ethtool rxnfc command
4295  *
4296  * Returns Success if the command is supported.
4297  **/
4298 static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
4299 {
4300 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4301 	struct i40e_vsi *vsi = np->vsi;
4302 	struct i40e_pf *pf = vsi->back;
4303 	int ret = -EOPNOTSUPP;
4304 
4305 	switch (cmd->cmd) {
4306 	case ETHTOOL_SRXFH:
4307 		ret = i40e_set_rss_hash_opt(pf, cmd);
4308 		break;
4309 	case ETHTOOL_SRXCLSRLINS:
4310 		ret = i40e_add_fdir_ethtool(vsi, cmd);
4311 		break;
4312 	case ETHTOOL_SRXCLSRLDEL:
4313 		ret = i40e_del_fdir_entry(vsi, cmd);
4314 		break;
4315 	default:
4316 		break;
4317 	}
4318 
4319 	return ret;
4320 }
4321 
4322 /**
4323  * i40e_max_channels - get Max number of combined channels supported
4324  * @vsi: vsi pointer
4325  **/
4326 static unsigned int i40e_max_channels(struct i40e_vsi *vsi)
4327 {
4328 	/* TODO: This code assumes DCB and FD is disabled for now. */
4329 	return vsi->alloc_queue_pairs;
4330 }
4331 
4332 /**
4333  * i40e_get_channels - Get the current channels enabled and max supported etc.
4334  * @dev: network interface device structure
4335  * @ch: ethtool channels structure
4336  *
4337  * We don't support separate tx and rx queues as channels. The other count
4338  * represents how many queues are being used for control. max_combined counts
4339  * how many queue pairs we can support. They may not be mapped 1 to 1 with
4340  * q_vectors since we support a lot more queue pairs than q_vectors.
4341  **/
4342 static void i40e_get_channels(struct net_device *dev,
4343 			      struct ethtool_channels *ch)
4344 {
4345 	struct i40e_netdev_priv *np = netdev_priv(dev);
4346 	struct i40e_vsi *vsi = np->vsi;
4347 	struct i40e_pf *pf = vsi->back;
4348 
4349 	/* report maximum channels */
4350 	ch->max_combined = i40e_max_channels(vsi);
4351 
4352 	/* report info for other vector */
4353 	ch->other_count = (pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0;
4354 	ch->max_other = ch->other_count;
4355 
4356 	/* Note: This code assumes DCB is disabled for now. */
4357 	ch->combined_count = vsi->num_queue_pairs;
4358 }
4359 
4360 /**
4361  * i40e_set_channels - Set the new channels count.
4362  * @dev: network interface device structure
4363  * @ch: ethtool channels structure
4364  *
4365  * The new channels count may not be the same as requested by the user
4366  * since it gets rounded down to a power of 2 value.
4367  **/
4368 static int i40e_set_channels(struct net_device *dev,
4369 			     struct ethtool_channels *ch)
4370 {
4371 	const u8 drop = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
4372 	struct i40e_netdev_priv *np = netdev_priv(dev);
4373 	unsigned int count = ch->combined_count;
4374 	struct i40e_vsi *vsi = np->vsi;
4375 	struct i40e_pf *pf = vsi->back;
4376 	struct i40e_fdir_filter *rule;
4377 	struct hlist_node *node2;
4378 	int new_count;
4379 	int err = 0;
4380 
4381 	/* We do not support setting channels for any other VSI at present */
4382 	if (vsi->type != I40E_VSI_MAIN)
4383 		return -EINVAL;
4384 
4385 	/* We do not support setting channels via ethtool when TCs are
4386 	 * configured through mqprio
4387 	 */
4388 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
4389 		return -EINVAL;
4390 
4391 	/* verify they are not requesting separate vectors */
4392 	if (!count || ch->rx_count || ch->tx_count)
4393 		return -EINVAL;
4394 
4395 	/* verify other_count has not changed */
4396 	if (ch->other_count != ((pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0))
4397 		return -EINVAL;
4398 
4399 	/* verify the number of channels does not exceed hardware limits */
4400 	if (count > i40e_max_channels(vsi))
4401 		return -EINVAL;
4402 
4403 	/* verify that the number of channels does not invalidate any current
4404 	 * flow director rules
4405 	 */
4406 	hlist_for_each_entry_safe(rule, node2,
4407 				  &pf->fdir_filter_list, fdir_node) {
4408 		if (rule->dest_ctl != drop && count <= rule->q_index) {
4409 			dev_warn(&pf->pdev->dev,
4410 				 "Existing user defined filter %d assigns flow to queue %d\n",
4411 				 rule->fd_id, rule->q_index);
4412 			err = -EINVAL;
4413 		}
4414 	}
4415 
4416 	if (err) {
4417 		dev_err(&pf->pdev->dev,
4418 			"Existing filter rules must be deleted to reduce combined channel count to %d\n",
4419 			count);
4420 		return err;
4421 	}
4422 
4423 	/* update feature limits from largest to smallest supported values */
4424 	/* TODO: Flow director limit, DCB etc */
4425 
4426 	/* use rss_reconfig to rebuild with new queue count and update traffic
4427 	 * class queue mapping
4428 	 */
4429 	new_count = i40e_reconfig_rss_queues(pf, count);
4430 	if (new_count > 0)
4431 		return 0;
4432 	else
4433 		return -EINVAL;
4434 }
4435 
4436 /**
4437  * i40e_get_rxfh_key_size - get the RSS hash key size
4438  * @netdev: network interface device structure
4439  *
4440  * Returns the table size.
4441  **/
4442 static u32 i40e_get_rxfh_key_size(struct net_device *netdev)
4443 {
4444 	return I40E_HKEY_ARRAY_SIZE;
4445 }
4446 
4447 /**
4448  * i40e_get_rxfh_indir_size - get the rx flow hash indirection table size
4449  * @netdev: network interface device structure
4450  *
4451  * Returns the table size.
4452  **/
4453 static u32 i40e_get_rxfh_indir_size(struct net_device *netdev)
4454 {
4455 	return I40E_HLUT_ARRAY_SIZE;
4456 }
4457 
4458 /**
4459  * i40e_get_rxfh - get the rx flow hash indirection table
4460  * @netdev: network interface device structure
4461  * @indir: indirection table
4462  * @key: hash key
4463  * @hfunc: hash function
4464  *
4465  * Reads the indirection table directly from the hardware. Returns 0 on
4466  * success.
4467  **/
4468 static int i40e_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
4469 			 u8 *hfunc)
4470 {
4471 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4472 	struct i40e_vsi *vsi = np->vsi;
4473 	u8 *lut, *seed = NULL;
4474 	int ret;
4475 	u16 i;
4476 
4477 	if (hfunc)
4478 		*hfunc = ETH_RSS_HASH_TOP;
4479 
4480 	if (!indir)
4481 		return 0;
4482 
4483 	seed = key;
4484 	lut = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
4485 	if (!lut)
4486 		return -ENOMEM;
4487 	ret = i40e_get_rss(vsi, seed, lut, I40E_HLUT_ARRAY_SIZE);
4488 	if (ret)
4489 		goto out;
4490 	for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
4491 		indir[i] = (u32)(lut[i]);
4492 
4493 out:
4494 	kfree(lut);
4495 
4496 	return ret;
4497 }
4498 
4499 /**
4500  * i40e_set_rxfh - set the rx flow hash indirection table
4501  * @netdev: network interface device structure
4502  * @indir: indirection table
4503  * @key: hash key
4504  * @hfunc: hash function to use
4505  *
4506  * Returns -EINVAL if the table specifies an invalid queue id, otherwise
4507  * returns 0 after programming the table.
4508  **/
4509 static int i40e_set_rxfh(struct net_device *netdev, const u32 *indir,
4510 			 const u8 *key, const u8 hfunc)
4511 {
4512 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4513 	struct i40e_vsi *vsi = np->vsi;
4514 	struct i40e_pf *pf = vsi->back;
4515 	u8 *seed = NULL;
4516 	u16 i;
4517 
4518 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
4519 		return -EOPNOTSUPP;
4520 
4521 	if (key) {
4522 		if (!vsi->rss_hkey_user) {
4523 			vsi->rss_hkey_user = kzalloc(I40E_HKEY_ARRAY_SIZE,
4524 						     GFP_KERNEL);
4525 			if (!vsi->rss_hkey_user)
4526 				return -ENOMEM;
4527 		}
4528 		memcpy(vsi->rss_hkey_user, key, I40E_HKEY_ARRAY_SIZE);
4529 		seed = vsi->rss_hkey_user;
4530 	}
4531 	if (!vsi->rss_lut_user) {
4532 		vsi->rss_lut_user = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
4533 		if (!vsi->rss_lut_user)
4534 			return -ENOMEM;
4535 	}
4536 
4537 	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
4538 	if (indir)
4539 		for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
4540 			vsi->rss_lut_user[i] = (u8)(indir[i]);
4541 	else
4542 		i40e_fill_rss_lut(pf, vsi->rss_lut_user, I40E_HLUT_ARRAY_SIZE,
4543 				  vsi->rss_size);
4544 
4545 	return i40e_config_rss(vsi, seed, vsi->rss_lut_user,
4546 			       I40E_HLUT_ARRAY_SIZE);
4547 }
4548 
4549 /**
4550  * i40e_get_priv_flags - report device private flags
4551  * @dev: network interface device structure
4552  *
4553  * The get string set count and the string set should be matched for each
4554  * flag returned.  Add new strings for each flag to the i40e_gstrings_priv_flags
4555  * array.
4556  *
4557  * Returns a u32 bitmap of flags.
4558  **/
4559 static u32 i40e_get_priv_flags(struct net_device *dev)
4560 {
4561 	struct i40e_netdev_priv *np = netdev_priv(dev);
4562 	struct i40e_vsi *vsi = np->vsi;
4563 	struct i40e_pf *pf = vsi->back;
4564 	u32 i, j, ret_flags = 0;
4565 
4566 	for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
4567 		const struct i40e_priv_flags *priv_flags;
4568 
4569 		priv_flags = &i40e_gstrings_priv_flags[i];
4570 
4571 		if (priv_flags->flag & pf->flags)
4572 			ret_flags |= BIT(i);
4573 	}
4574 
4575 	if (pf->hw.pf_id != 0)
4576 		return ret_flags;
4577 
4578 	for (j = 0; j < I40E_GL_PRIV_FLAGS_STR_LEN; j++) {
4579 		const struct i40e_priv_flags *priv_flags;
4580 
4581 		priv_flags = &i40e_gl_gstrings_priv_flags[j];
4582 
4583 		if (priv_flags->flag & pf->flags)
4584 			ret_flags |= BIT(i + j);
4585 	}
4586 
4587 	return ret_flags;
4588 }
4589 
4590 /**
4591  * i40e_set_priv_flags - set private flags
4592  * @dev: network interface device structure
4593  * @flags: bit flags to be set
4594  **/
4595 static int i40e_set_priv_flags(struct net_device *dev, u32 flags)
4596 {
4597 	struct i40e_netdev_priv *np = netdev_priv(dev);
4598 	struct i40e_vsi *vsi = np->vsi;
4599 	struct i40e_pf *pf = vsi->back;
4600 	u64 orig_flags, new_flags, changed_flags;
4601 	u32 i, j;
4602 
4603 	orig_flags = READ_ONCE(pf->flags);
4604 	new_flags = orig_flags;
4605 
4606 	for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
4607 		const struct i40e_priv_flags *priv_flags;
4608 
4609 		priv_flags = &i40e_gstrings_priv_flags[i];
4610 
4611 		if (flags & BIT(i))
4612 			new_flags |= priv_flags->flag;
4613 		else
4614 			new_flags &= ~(priv_flags->flag);
4615 
4616 		/* If this is a read-only flag, it can't be changed */
4617 		if (priv_flags->read_only &&
4618 		    ((orig_flags ^ new_flags) & ~BIT(i)))
4619 			return -EOPNOTSUPP;
4620 	}
4621 
4622 	if (pf->hw.pf_id != 0)
4623 		goto flags_complete;
4624 
4625 	for (j = 0; j < I40E_GL_PRIV_FLAGS_STR_LEN; j++) {
4626 		const struct i40e_priv_flags *priv_flags;
4627 
4628 		priv_flags = &i40e_gl_gstrings_priv_flags[j];
4629 
4630 		if (flags & BIT(i + j))
4631 			new_flags |= priv_flags->flag;
4632 		else
4633 			new_flags &= ~(priv_flags->flag);
4634 
4635 		/* If this is a read-only flag, it can't be changed */
4636 		if (priv_flags->read_only &&
4637 		    ((orig_flags ^ new_flags) & ~BIT(i)))
4638 			return -EOPNOTSUPP;
4639 	}
4640 
4641 flags_complete:
4642 	changed_flags = orig_flags ^ new_flags;
4643 
4644 	/* Before we finalize any flag changes, we need to perform some
4645 	 * checks to ensure that the changes are supported and safe.
4646 	 */
4647 
4648 	/* ATR eviction is not supported on all devices */
4649 	if ((new_flags & I40E_FLAG_HW_ATR_EVICT_ENABLED) &&
4650 	    !(pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE))
4651 		return -EOPNOTSUPP;
4652 
4653 	/* If the driver detected FW LLDP was disabled on init, this flag could
4654 	 * be set, however we do not support _changing_ the flag if NPAR is
4655 	 * enabled or FW API version < 1.7.  There are situations where older
4656 	 * FW versions/NPAR enabled PFs could disable LLDP, however we _must_
4657 	 * not allow the user to enable/disable LLDP with this flag on
4658 	 * unsupported FW versions.
4659 	 */
4660 	if (changed_flags & I40E_FLAG_DISABLE_FW_LLDP) {
4661 		if (!(pf->hw_features & I40E_HW_STOPPABLE_FW_LLDP)) {
4662 			dev_warn(&pf->pdev->dev,
4663 				 "Device does not support changing FW LLDP\n");
4664 			return -EOPNOTSUPP;
4665 		}
4666 	}
4667 
4668 	/* Now that we've checked to ensure that the new flags are valid, load
4669 	 * them into place. Since we only modify flags either (a) during
4670 	 * initialization or (b) while holding the RTNL lock, we don't need
4671 	 * anything fancy here.
4672 	 */
4673 	pf->flags = new_flags;
4674 
4675 	/* Process any additional changes needed as a result of flag changes.
4676 	 * The changed_flags value reflects the list of bits that were
4677 	 * changed in the code above.
4678 	 */
4679 
4680 	/* Flush current ATR settings if ATR was disabled */
4681 	if ((changed_flags & I40E_FLAG_FD_ATR_ENABLED) &&
4682 	    !(pf->flags & I40E_FLAG_FD_ATR_ENABLED)) {
4683 		set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
4684 		set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
4685 	}
4686 
4687 	if (changed_flags & I40E_FLAG_TRUE_PROMISC_SUPPORT) {
4688 		u16 sw_flags = 0, valid_flags = 0;
4689 		int ret;
4690 
4691 		if (!(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
4692 			sw_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
4693 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
4694 		ret = i40e_aq_set_switch_config(&pf->hw, sw_flags, valid_flags,
4695 						0, NULL);
4696 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
4697 			dev_info(&pf->pdev->dev,
4698 				 "couldn't set switch config bits, err %s aq_err %s\n",
4699 				 i40e_stat_str(&pf->hw, ret),
4700 				 i40e_aq_str(&pf->hw,
4701 					     pf->hw.aq.asq_last_status));
4702 			/* not a fatal problem, just keep going */
4703 		}
4704 	}
4705 
4706 	if ((changed_flags & pf->flags &
4707 	     I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED) &&
4708 	    (pf->flags & I40E_FLAG_MFP_ENABLED))
4709 		dev_warn(&pf->pdev->dev,
4710 			 "Turning on link-down-on-close flag may affect other partitions\n");
4711 
4712 	if (changed_flags & I40E_FLAG_DISABLE_FW_LLDP) {
4713 		if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
4714 			struct i40e_dcbx_config *dcbcfg;
4715 
4716 			i40e_aq_stop_lldp(&pf->hw, true, NULL);
4717 			i40e_aq_set_dcb_parameters(&pf->hw, true, NULL);
4718 			/* reset local_dcbx_config to default */
4719 			dcbcfg = &pf->hw.local_dcbx_config;
4720 			dcbcfg->etscfg.willing = 1;
4721 			dcbcfg->etscfg.maxtcs = 0;
4722 			dcbcfg->etscfg.tcbwtable[0] = 100;
4723 			for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++)
4724 				dcbcfg->etscfg.tcbwtable[i] = 0;
4725 			for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4726 				dcbcfg->etscfg.prioritytable[i] = 0;
4727 			dcbcfg->etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
4728 			dcbcfg->pfc.willing = 1;
4729 			dcbcfg->pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
4730 		} else {
4731 			i40e_aq_start_lldp(&pf->hw, NULL);
4732 		}
4733 	}
4734 
4735 	/* Issue reset to cause things to take effect, as additional bits
4736 	 * are added we will need to create a mask of bits requiring reset
4737 	 */
4738 	if (changed_flags & (I40E_FLAG_VEB_STATS_ENABLED |
4739 			     I40E_FLAG_LEGACY_RX |
4740 			     I40E_FLAG_SOURCE_PRUNING_DISABLED |
4741 			     I40E_FLAG_DISABLE_FW_LLDP))
4742 		i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
4743 
4744 	return 0;
4745 }
4746 
4747 /**
4748  * i40e_get_module_info - get (Q)SFP+ module type info
4749  * @netdev: network interface device structure
4750  * @modinfo: module EEPROM size and layout information structure
4751  **/
4752 static int i40e_get_module_info(struct net_device *netdev,
4753 				struct ethtool_modinfo *modinfo)
4754 {
4755 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4756 	struct i40e_vsi *vsi = np->vsi;
4757 	struct i40e_pf *pf = vsi->back;
4758 	struct i40e_hw *hw = &pf->hw;
4759 	u32 sff8472_comp = 0;
4760 	u32 sff8472_swap = 0;
4761 	u32 sff8636_rev = 0;
4762 	i40e_status status;
4763 	u32 type = 0;
4764 
4765 	/* Check if firmware supports reading module EEPROM. */
4766 	if (!(hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE)) {
4767 		netdev_err(vsi->netdev, "Module EEPROM memory read not supported. Please update the NVM image.\n");
4768 		return -EINVAL;
4769 	}
4770 
4771 	status = i40e_update_link_info(hw);
4772 	if (status)
4773 		return -EIO;
4774 
4775 	if (hw->phy.link_info.phy_type == I40E_PHY_TYPE_EMPTY) {
4776 		netdev_err(vsi->netdev, "Cannot read module EEPROM memory. No module connected.\n");
4777 		return -EINVAL;
4778 	}
4779 
4780 	type = hw->phy.link_info.module_type[0];
4781 
4782 	switch (type) {
4783 	case I40E_MODULE_TYPE_SFP:
4784 		status = i40e_aq_get_phy_register(hw,
4785 				I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
4786 				I40E_I2C_EEPROM_DEV_ADDR,
4787 				I40E_MODULE_SFF_8472_COMP,
4788 				&sff8472_comp, NULL);
4789 		if (status)
4790 			return -EIO;
4791 
4792 		status = i40e_aq_get_phy_register(hw,
4793 				I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
4794 				I40E_I2C_EEPROM_DEV_ADDR,
4795 				I40E_MODULE_SFF_8472_SWAP,
4796 				&sff8472_swap, NULL);
4797 		if (status)
4798 			return -EIO;
4799 
4800 		/* Check if the module requires address swap to access
4801 		 * the other EEPROM memory page.
4802 		 */
4803 		if (sff8472_swap & I40E_MODULE_SFF_ADDR_MODE) {
4804 			netdev_warn(vsi->netdev, "Module address swap to access page 0xA2 is not supported.\n");
4805 			modinfo->type = ETH_MODULE_SFF_8079;
4806 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4807 		} else if (sff8472_comp == 0x00) {
4808 			/* Module is not SFF-8472 compliant */
4809 			modinfo->type = ETH_MODULE_SFF_8079;
4810 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4811 		} else {
4812 			modinfo->type = ETH_MODULE_SFF_8472;
4813 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
4814 		}
4815 		break;
4816 	case I40E_MODULE_TYPE_QSFP_PLUS:
4817 		/* Read from memory page 0. */
4818 		status = i40e_aq_get_phy_register(hw,
4819 				I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
4820 				0,
4821 				I40E_MODULE_REVISION_ADDR,
4822 				&sff8636_rev, NULL);
4823 		if (status)
4824 			return -EIO;
4825 		/* Determine revision compliance byte */
4826 		if (sff8636_rev > 0x02) {
4827 			/* Module is SFF-8636 compliant */
4828 			modinfo->type = ETH_MODULE_SFF_8636;
4829 			modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
4830 		} else {
4831 			modinfo->type = ETH_MODULE_SFF_8436;
4832 			modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
4833 		}
4834 		break;
4835 	case I40E_MODULE_TYPE_QSFP28:
4836 		modinfo->type = ETH_MODULE_SFF_8636;
4837 		modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
4838 		break;
4839 	default:
4840 		netdev_err(vsi->netdev, "Module type unrecognized\n");
4841 		return -EINVAL;
4842 	}
4843 	return 0;
4844 }
4845 
4846 /**
4847  * i40e_get_module_eeprom - fills buffer with (Q)SFP+ module memory contents
4848  * @netdev: network interface device structure
4849  * @ee: EEPROM dump request structure
4850  * @data: buffer to be filled with EEPROM contents
4851  **/
4852 static int i40e_get_module_eeprom(struct net_device *netdev,
4853 				  struct ethtool_eeprom *ee,
4854 				  u8 *data)
4855 {
4856 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4857 	struct i40e_vsi *vsi = np->vsi;
4858 	struct i40e_pf *pf = vsi->back;
4859 	struct i40e_hw *hw = &pf->hw;
4860 	bool is_sfp = false;
4861 	i40e_status status;
4862 	u32 value = 0;
4863 	int i;
4864 
4865 	if (!ee || !ee->len || !data)
4866 		return -EINVAL;
4867 
4868 	if (hw->phy.link_info.module_type[0] == I40E_MODULE_TYPE_SFP)
4869 		is_sfp = true;
4870 
4871 	for (i = 0; i < ee->len; i++) {
4872 		u32 offset = i + ee->offset;
4873 		u32 addr = is_sfp ? I40E_I2C_EEPROM_DEV_ADDR : 0;
4874 
4875 		/* Check if we need to access the other memory page */
4876 		if (is_sfp) {
4877 			if (offset >= ETH_MODULE_SFF_8079_LEN) {
4878 				offset -= ETH_MODULE_SFF_8079_LEN;
4879 				addr = I40E_I2C_EEPROM_DEV_ADDR2;
4880 			}
4881 		} else {
4882 			while (offset >= ETH_MODULE_SFF_8436_LEN) {
4883 				/* Compute memory page number and offset. */
4884 				offset -= ETH_MODULE_SFF_8436_LEN / 2;
4885 				addr++;
4886 			}
4887 		}
4888 
4889 		status = i40e_aq_get_phy_register(hw,
4890 				I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
4891 				addr, offset, &value, NULL);
4892 		if (status)
4893 			return -EIO;
4894 		data[i] = value;
4895 	}
4896 	return 0;
4897 }
4898 
4899 static const struct ethtool_ops i40e_ethtool_ops = {
4900 	.get_drvinfo		= i40e_get_drvinfo,
4901 	.get_regs_len		= i40e_get_regs_len,
4902 	.get_regs		= i40e_get_regs,
4903 	.nway_reset		= i40e_nway_reset,
4904 	.get_link		= ethtool_op_get_link,
4905 	.get_wol		= i40e_get_wol,
4906 	.set_wol		= i40e_set_wol,
4907 	.set_eeprom		= i40e_set_eeprom,
4908 	.get_eeprom_len		= i40e_get_eeprom_len,
4909 	.get_eeprom		= i40e_get_eeprom,
4910 	.get_ringparam		= i40e_get_ringparam,
4911 	.set_ringparam		= i40e_set_ringparam,
4912 	.get_pauseparam		= i40e_get_pauseparam,
4913 	.set_pauseparam		= i40e_set_pauseparam,
4914 	.get_msglevel		= i40e_get_msglevel,
4915 	.set_msglevel		= i40e_set_msglevel,
4916 	.get_rxnfc		= i40e_get_rxnfc,
4917 	.set_rxnfc		= i40e_set_rxnfc,
4918 	.self_test		= i40e_diag_test,
4919 	.get_strings		= i40e_get_strings,
4920 	.set_phys_id		= i40e_set_phys_id,
4921 	.get_sset_count		= i40e_get_sset_count,
4922 	.get_ethtool_stats	= i40e_get_ethtool_stats,
4923 	.get_coalesce		= i40e_get_coalesce,
4924 	.set_coalesce		= i40e_set_coalesce,
4925 	.get_rxfh_key_size	= i40e_get_rxfh_key_size,
4926 	.get_rxfh_indir_size	= i40e_get_rxfh_indir_size,
4927 	.get_rxfh		= i40e_get_rxfh,
4928 	.set_rxfh		= i40e_set_rxfh,
4929 	.get_channels		= i40e_get_channels,
4930 	.set_channels		= i40e_set_channels,
4931 	.get_module_info	= i40e_get_module_info,
4932 	.get_module_eeprom	= i40e_get_module_eeprom,
4933 	.get_ts_info		= i40e_get_ts_info,
4934 	.get_priv_flags		= i40e_get_priv_flags,
4935 	.set_priv_flags		= i40e_set_priv_flags,
4936 	.get_per_queue_coalesce	= i40e_get_per_queue_coalesce,
4937 	.set_per_queue_coalesce	= i40e_set_per_queue_coalesce,
4938 	.get_link_ksettings	= i40e_get_link_ksettings,
4939 	.set_link_ksettings	= i40e_set_link_ksettings,
4940 };
4941 
4942 void i40e_set_ethtool_ops(struct net_device *netdev)
4943 {
4944 	netdev->ethtool_ops = &i40e_ethtool_ops;
4945 }
4946