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