1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 /* ethtool support for iavf */
5 #include "iavf.h"
6 
7 #include <linux/uaccess.h>
8 
9 /* ethtool statistics helpers */
10 
11 /**
12  * struct iavf_stats - definition for an ethtool statistic
13  * @stat_string: statistic name to display in ethtool -S output
14  * @sizeof_stat: the sizeof() the stat, must be no greater than sizeof(u64)
15  * @stat_offset: offsetof() the stat from a base pointer
16  *
17  * This structure defines a statistic to be added to the ethtool stats buffer.
18  * It defines a statistic as offset from a common base pointer. Stats should
19  * be defined in constant arrays using the IAVF_STAT macro, with every element
20  * of the array using the same _type for calculating the sizeof_stat and
21  * stat_offset.
22  *
23  * The @sizeof_stat is expected to be sizeof(u8), sizeof(u16), sizeof(u32) or
24  * sizeof(u64). Other sizes are not expected and will produce a WARN_ONCE from
25  * the iavf_add_ethtool_stat() helper function.
26  *
27  * The @stat_string is interpreted as a format string, allowing formatted
28  * values to be inserted while looping over multiple structures for a given
29  * statistics array. Thus, every statistic string in an array should have the
30  * same type and number of format specifiers, to be formatted by variadic
31  * arguments to the iavf_add_stat_string() helper function.
32  **/
33 struct iavf_stats {
34 	char stat_string[ETH_GSTRING_LEN];
35 	int sizeof_stat;
36 	int stat_offset;
37 };
38 
39 /* Helper macro to define an iavf_stat structure with proper size and type.
40  * Use this when defining constant statistics arrays. Note that @_type expects
41  * only a type name and is used multiple times.
42  */
43 #define IAVF_STAT(_type, _name, _stat) { \
44 	.stat_string = _name, \
45 	.sizeof_stat = sizeof_field(_type, _stat), \
46 	.stat_offset = offsetof(_type, _stat) \
47 }
48 
49 /* Helper macro for defining some statistics related to queues */
50 #define IAVF_QUEUE_STAT(_name, _stat) \
51 	IAVF_STAT(struct iavf_ring, _name, _stat)
52 
53 /* Stats associated with a Tx or Rx ring */
54 static const struct iavf_stats iavf_gstrings_queue_stats[] = {
55 	IAVF_QUEUE_STAT("%s-%u.packets", stats.packets),
56 	IAVF_QUEUE_STAT("%s-%u.bytes", stats.bytes),
57 };
58 
59 /**
60  * iavf_add_one_ethtool_stat - copy the stat into the supplied buffer
61  * @data: location to store the stat value
62  * @pointer: basis for where to copy from
63  * @stat: the stat definition
64  *
65  * Copies the stat data defined by the pointer and stat structure pair into
66  * the memory supplied as data. Used to implement iavf_add_ethtool_stats and
67  * iavf_add_queue_stats. If the pointer is null, data will be zero'd.
68  */
69 static void
70 iavf_add_one_ethtool_stat(u64 *data, void *pointer,
71 			  const struct iavf_stats *stat)
72 {
73 	char *p;
74 
75 	if (!pointer) {
76 		/* ensure that the ethtool data buffer is zero'd for any stats
77 		 * which don't have a valid pointer.
78 		 */
79 		*data = 0;
80 		return;
81 	}
82 
83 	p = (char *)pointer + stat->stat_offset;
84 	switch (stat->sizeof_stat) {
85 	case sizeof(u64):
86 		*data = *((u64 *)p);
87 		break;
88 	case sizeof(u32):
89 		*data = *((u32 *)p);
90 		break;
91 	case sizeof(u16):
92 		*data = *((u16 *)p);
93 		break;
94 	case sizeof(u8):
95 		*data = *((u8 *)p);
96 		break;
97 	default:
98 		WARN_ONCE(1, "unexpected stat size for %s",
99 			  stat->stat_string);
100 		*data = 0;
101 	}
102 }
103 
104 /**
105  * __iavf_add_ethtool_stats - copy stats into the ethtool supplied buffer
106  * @data: ethtool stats buffer
107  * @pointer: location to copy stats from
108  * @stats: array of stats to copy
109  * @size: the size of the stats definition
110  *
111  * Copy the stats defined by the stats array using the pointer as a base into
112  * the data buffer supplied by ethtool. Updates the data pointer to point to
113  * the next empty location for successive calls to __iavf_add_ethtool_stats.
114  * If pointer is null, set the data values to zero and update the pointer to
115  * skip these stats.
116  **/
117 static void
118 __iavf_add_ethtool_stats(u64 **data, void *pointer,
119 			 const struct iavf_stats stats[],
120 			 const unsigned int size)
121 {
122 	unsigned int i;
123 
124 	for (i = 0; i < size; i++)
125 		iavf_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
126 }
127 
128 /**
129  * iavf_add_ethtool_stats - copy stats into ethtool supplied buffer
130  * @data: ethtool stats buffer
131  * @pointer: location where stats are stored
132  * @stats: static const array of stat definitions
133  *
134  * Macro to ease the use of __iavf_add_ethtool_stats by taking a static
135  * constant stats array and passing the ARRAY_SIZE(). This avoids typos by
136  * ensuring that we pass the size associated with the given stats array.
137  *
138  * The parameter @stats is evaluated twice, so parameters with side effects
139  * should be avoided.
140  **/
141 #define iavf_add_ethtool_stats(data, pointer, stats) \
142 	__iavf_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
143 
144 /**
145  * iavf_add_queue_stats - copy queue statistics into supplied buffer
146  * @data: ethtool stats buffer
147  * @ring: the ring to copy
148  *
149  * Queue statistics must be copied while protected by
150  * u64_stats_fetch_begin_irq, so we can't directly use iavf_add_ethtool_stats.
151  * Assumes that queue stats are defined in iavf_gstrings_queue_stats. If the
152  * ring pointer is null, zero out the queue stat values and update the data
153  * pointer. Otherwise safely copy the stats from the ring into the supplied
154  * buffer and update the data pointer when finished.
155  *
156  * This function expects to be called while under rcu_read_lock().
157  **/
158 static void
159 iavf_add_queue_stats(u64 **data, struct iavf_ring *ring)
160 {
161 	const unsigned int size = ARRAY_SIZE(iavf_gstrings_queue_stats);
162 	const struct iavf_stats *stats = iavf_gstrings_queue_stats;
163 	unsigned int start;
164 	unsigned int i;
165 
166 	/* To avoid invalid statistics values, ensure that we keep retrying
167 	 * the copy until we get a consistent value according to
168 	 * u64_stats_fetch_retry_irq. But first, make sure our ring is
169 	 * non-null before attempting to access its syncp.
170 	 */
171 	do {
172 		start = !ring ? 0 : u64_stats_fetch_begin_irq(&ring->syncp);
173 		for (i = 0; i < size; i++)
174 			iavf_add_one_ethtool_stat(&(*data)[i], ring, &stats[i]);
175 	} while (ring && u64_stats_fetch_retry_irq(&ring->syncp, start));
176 
177 	/* Once we successfully copy the stats in, update the data pointer */
178 	*data += size;
179 }
180 
181 /**
182  * __iavf_add_stat_strings - copy stat strings into ethtool buffer
183  * @p: ethtool supplied buffer
184  * @stats: stat definitions array
185  * @size: size of the stats array
186  *
187  * Format and copy the strings described by stats into the buffer pointed at
188  * by p.
189  **/
190 static void __iavf_add_stat_strings(u8 **p, const struct iavf_stats stats[],
191 				    const unsigned int size, ...)
192 {
193 	unsigned int i;
194 
195 	for (i = 0; i < size; i++) {
196 		va_list args;
197 
198 		va_start(args, size);
199 		vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
200 		*p += ETH_GSTRING_LEN;
201 		va_end(args);
202 	}
203 }
204 
205 /**
206  * iavf_add_stat_strings - copy stat strings into ethtool buffer
207  * @p: ethtool supplied buffer
208  * @stats: stat definitions array
209  *
210  * Format and copy the strings described by the const static stats value into
211  * the buffer pointed at by p.
212  *
213  * The parameter @stats is evaluated twice, so parameters with side effects
214  * should be avoided. Additionally, stats must be an array such that
215  * ARRAY_SIZE can be called on it.
216  **/
217 #define iavf_add_stat_strings(p, stats, ...) \
218 	__iavf_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
219 
220 #define VF_STAT(_name, _stat) \
221 	IAVF_STAT(struct iavf_adapter, _name, _stat)
222 
223 static const struct iavf_stats iavf_gstrings_stats[] = {
224 	VF_STAT("rx_bytes", current_stats.rx_bytes),
225 	VF_STAT("rx_unicast", current_stats.rx_unicast),
226 	VF_STAT("rx_multicast", current_stats.rx_multicast),
227 	VF_STAT("rx_broadcast", current_stats.rx_broadcast),
228 	VF_STAT("rx_discards", current_stats.rx_discards),
229 	VF_STAT("rx_unknown_protocol", current_stats.rx_unknown_protocol),
230 	VF_STAT("tx_bytes", current_stats.tx_bytes),
231 	VF_STAT("tx_unicast", current_stats.tx_unicast),
232 	VF_STAT("tx_multicast", current_stats.tx_multicast),
233 	VF_STAT("tx_broadcast", current_stats.tx_broadcast),
234 	VF_STAT("tx_discards", current_stats.tx_discards),
235 	VF_STAT("tx_errors", current_stats.tx_errors),
236 };
237 
238 #define IAVF_STATS_LEN	ARRAY_SIZE(iavf_gstrings_stats)
239 
240 #define IAVF_QUEUE_STATS_LEN	ARRAY_SIZE(iavf_gstrings_queue_stats)
241 
242 /* For now we have one and only one private flag and it is only defined
243  * when we have support for the SKIP_CPU_SYNC DMA attribute.  Instead
244  * of leaving all this code sitting around empty we will strip it unless
245  * our one private flag is actually available.
246  */
247 struct iavf_priv_flags {
248 	char flag_string[ETH_GSTRING_LEN];
249 	u32 flag;
250 	bool read_only;
251 };
252 
253 #define IAVF_PRIV_FLAG(_name, _flag, _read_only) { \
254 	.flag_string = _name, \
255 	.flag = _flag, \
256 	.read_only = _read_only, \
257 }
258 
259 static const struct iavf_priv_flags iavf_gstrings_priv_flags[] = {
260 	IAVF_PRIV_FLAG("legacy-rx", IAVF_FLAG_LEGACY_RX, 0),
261 };
262 
263 #define IAVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(iavf_gstrings_priv_flags)
264 
265 /**
266  * iavf_get_link_ksettings - Get Link Speed and Duplex settings
267  * @netdev: network interface device structure
268  * @cmd: ethtool command
269  *
270  * Reports speed/duplex settings. Because this is a VF, we don't know what
271  * kind of link we really have, so we fake it.
272  **/
273 static int iavf_get_link_ksettings(struct net_device *netdev,
274 				   struct ethtool_link_ksettings *cmd)
275 {
276 	struct iavf_adapter *adapter = netdev_priv(netdev);
277 
278 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
279 	cmd->base.autoneg = AUTONEG_DISABLE;
280 	cmd->base.port = PORT_NONE;
281 	cmd->base.duplex = DUPLEX_FULL;
282 
283 	if (ADV_LINK_SUPPORT(adapter)) {
284 		if (adapter->link_speed_mbps &&
285 		    adapter->link_speed_mbps < U32_MAX)
286 			cmd->base.speed = adapter->link_speed_mbps;
287 		else
288 			cmd->base.speed = SPEED_UNKNOWN;
289 
290 		return 0;
291 	}
292 
293 	switch (adapter->link_speed) {
294 	case VIRTCHNL_LINK_SPEED_40GB:
295 		cmd->base.speed = SPEED_40000;
296 		break;
297 	case VIRTCHNL_LINK_SPEED_25GB:
298 		cmd->base.speed = SPEED_25000;
299 		break;
300 	case VIRTCHNL_LINK_SPEED_20GB:
301 		cmd->base.speed = SPEED_20000;
302 		break;
303 	case VIRTCHNL_LINK_SPEED_10GB:
304 		cmd->base.speed = SPEED_10000;
305 		break;
306 	case VIRTCHNL_LINK_SPEED_5GB:
307 		cmd->base.speed = SPEED_5000;
308 		break;
309 	case VIRTCHNL_LINK_SPEED_2_5GB:
310 		cmd->base.speed = SPEED_2500;
311 		break;
312 	case VIRTCHNL_LINK_SPEED_1GB:
313 		cmd->base.speed = SPEED_1000;
314 		break;
315 	case VIRTCHNL_LINK_SPEED_100MB:
316 		cmd->base.speed = SPEED_100;
317 		break;
318 	default:
319 		break;
320 	}
321 
322 	return 0;
323 }
324 
325 /**
326  * iavf_get_sset_count - Get length of string set
327  * @netdev: network interface device structure
328  * @sset: id of string set
329  *
330  * Reports size of various string tables.
331  **/
332 static int iavf_get_sset_count(struct net_device *netdev, int sset)
333 {
334 	if (sset == ETH_SS_STATS)
335 		return IAVF_STATS_LEN +
336 			(IAVF_QUEUE_STATS_LEN * 2 * IAVF_MAX_REQ_QUEUES);
337 	else if (sset == ETH_SS_PRIV_FLAGS)
338 		return IAVF_PRIV_FLAGS_STR_LEN;
339 	else
340 		return -EINVAL;
341 }
342 
343 /**
344  * iavf_get_ethtool_stats - report device statistics
345  * @netdev: network interface device structure
346  * @stats: ethtool statistics structure
347  * @data: pointer to data buffer
348  *
349  * All statistics are added to the data buffer as an array of u64.
350  **/
351 static void iavf_get_ethtool_stats(struct net_device *netdev,
352 				   struct ethtool_stats *stats, u64 *data)
353 {
354 	struct iavf_adapter *adapter = netdev_priv(netdev);
355 	unsigned int i;
356 
357 	/* Explicitly request stats refresh */
358 	iavf_schedule_request_stats(adapter);
359 
360 	iavf_add_ethtool_stats(&data, adapter, iavf_gstrings_stats);
361 
362 	rcu_read_lock();
363 	for (i = 0; i < IAVF_MAX_REQ_QUEUES; i++) {
364 		struct iavf_ring *ring;
365 
366 		/* Avoid accessing un-allocated queues */
367 		ring = (i < adapter->num_active_queues ?
368 			&adapter->tx_rings[i] : NULL);
369 		iavf_add_queue_stats(&data, ring);
370 
371 		/* Avoid accessing un-allocated queues */
372 		ring = (i < adapter->num_active_queues ?
373 			&adapter->rx_rings[i] : NULL);
374 		iavf_add_queue_stats(&data, ring);
375 	}
376 	rcu_read_unlock();
377 }
378 
379 /**
380  * iavf_get_priv_flag_strings - Get private flag strings
381  * @netdev: network interface device structure
382  * @data: buffer for string data
383  *
384  * Builds the private flags string table
385  **/
386 static void iavf_get_priv_flag_strings(struct net_device *netdev, u8 *data)
387 {
388 	unsigned int i;
389 
390 	for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
391 		snprintf(data, ETH_GSTRING_LEN, "%s",
392 			 iavf_gstrings_priv_flags[i].flag_string);
393 		data += ETH_GSTRING_LEN;
394 	}
395 }
396 
397 /**
398  * iavf_get_stat_strings - Get stat strings
399  * @netdev: network interface device structure
400  * @data: buffer for string data
401  *
402  * Builds the statistics string table
403  **/
404 static void iavf_get_stat_strings(struct net_device *netdev, u8 *data)
405 {
406 	unsigned int i;
407 
408 	iavf_add_stat_strings(&data, iavf_gstrings_stats);
409 
410 	/* Queues are always allocated in pairs, so we just use num_tx_queues
411 	 * for both Tx and Rx queues.
412 	 */
413 	for (i = 0; i < netdev->num_tx_queues; i++) {
414 		iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
415 				      "tx", i);
416 		iavf_add_stat_strings(&data, iavf_gstrings_queue_stats,
417 				      "rx", i);
418 	}
419 }
420 
421 /**
422  * iavf_get_strings - Get string set
423  * @netdev: network interface device structure
424  * @sset: id of string set
425  * @data: buffer for string data
426  *
427  * Builds string tables for various string sets
428  **/
429 static void iavf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
430 {
431 	switch (sset) {
432 	case ETH_SS_STATS:
433 		iavf_get_stat_strings(netdev, data);
434 		break;
435 	case ETH_SS_PRIV_FLAGS:
436 		iavf_get_priv_flag_strings(netdev, data);
437 		break;
438 	default:
439 		break;
440 	}
441 }
442 
443 /**
444  * iavf_get_priv_flags - report device private flags
445  * @netdev: network interface device structure
446  *
447  * The get string set count and the string set should be matched for each
448  * flag returned.  Add new strings for each flag to the iavf_gstrings_priv_flags
449  * array.
450  *
451  * Returns a u32 bitmap of flags.
452  **/
453 static u32 iavf_get_priv_flags(struct net_device *netdev)
454 {
455 	struct iavf_adapter *adapter = netdev_priv(netdev);
456 	u32 i, ret_flags = 0;
457 
458 	for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
459 		const struct iavf_priv_flags *priv_flags;
460 
461 		priv_flags = &iavf_gstrings_priv_flags[i];
462 
463 		if (priv_flags->flag & adapter->flags)
464 			ret_flags |= BIT(i);
465 	}
466 
467 	return ret_flags;
468 }
469 
470 /**
471  * iavf_set_priv_flags - set private flags
472  * @netdev: network interface device structure
473  * @flags: bit flags to be set
474  **/
475 static int iavf_set_priv_flags(struct net_device *netdev, u32 flags)
476 {
477 	struct iavf_adapter *adapter = netdev_priv(netdev);
478 	u32 orig_flags, new_flags, changed_flags;
479 	u32 i;
480 
481 	orig_flags = READ_ONCE(adapter->flags);
482 	new_flags = orig_flags;
483 
484 	for (i = 0; i < IAVF_PRIV_FLAGS_STR_LEN; i++) {
485 		const struct iavf_priv_flags *priv_flags;
486 
487 		priv_flags = &iavf_gstrings_priv_flags[i];
488 
489 		if (flags & BIT(i))
490 			new_flags |= priv_flags->flag;
491 		else
492 			new_flags &= ~(priv_flags->flag);
493 
494 		if (priv_flags->read_only &&
495 		    ((orig_flags ^ new_flags) & ~BIT(i)))
496 			return -EOPNOTSUPP;
497 	}
498 
499 	/* Before we finalize any flag changes, any checks which we need to
500 	 * perform to determine if the new flags will be supported should go
501 	 * here...
502 	 */
503 
504 	/* Compare and exchange the new flags into place. If we failed, that
505 	 * is if cmpxchg returns anything but the old value, this means
506 	 * something else must have modified the flags variable since we
507 	 * copied it. We'll just punt with an error and log something in the
508 	 * message buffer.
509 	 */
510 	if (cmpxchg(&adapter->flags, orig_flags, new_flags) != orig_flags) {
511 		dev_warn(&adapter->pdev->dev,
512 			 "Unable to update adapter->flags as it was modified by another thread...\n");
513 		return -EAGAIN;
514 	}
515 
516 	changed_flags = orig_flags ^ new_flags;
517 
518 	/* Process any additional changes needed as a result of flag changes.
519 	 * The changed_flags value reflects the list of bits that were changed
520 	 * in the code above.
521 	 */
522 
523 	/* issue a reset to force legacy-rx change to take effect */
524 	if (changed_flags & IAVF_FLAG_LEGACY_RX) {
525 		if (netif_running(netdev)) {
526 			adapter->flags |= IAVF_FLAG_RESET_NEEDED;
527 			queue_work(iavf_wq, &adapter->reset_task);
528 		}
529 	}
530 
531 	return 0;
532 }
533 
534 /**
535  * iavf_get_msglevel - Get debug message level
536  * @netdev: network interface device structure
537  *
538  * Returns current debug message level.
539  **/
540 static u32 iavf_get_msglevel(struct net_device *netdev)
541 {
542 	struct iavf_adapter *adapter = netdev_priv(netdev);
543 
544 	return adapter->msg_enable;
545 }
546 
547 /**
548  * iavf_set_msglevel - Set debug message level
549  * @netdev: network interface device structure
550  * @data: message level
551  *
552  * Set current debug message level. Higher values cause the driver to
553  * be noisier.
554  **/
555 static void iavf_set_msglevel(struct net_device *netdev, u32 data)
556 {
557 	struct iavf_adapter *adapter = netdev_priv(netdev);
558 
559 	if (IAVF_DEBUG_USER & data)
560 		adapter->hw.debug_mask = data;
561 	adapter->msg_enable = data;
562 }
563 
564 /**
565  * iavf_get_drvinfo - Get driver info
566  * @netdev: network interface device structure
567  * @drvinfo: ethool driver info structure
568  *
569  * Returns information about the driver and device for display to the user.
570  **/
571 static void iavf_get_drvinfo(struct net_device *netdev,
572 			     struct ethtool_drvinfo *drvinfo)
573 {
574 	struct iavf_adapter *adapter = netdev_priv(netdev);
575 
576 	strlcpy(drvinfo->driver, iavf_driver_name, 32);
577 	strlcpy(drvinfo->fw_version, "N/A", 4);
578 	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
579 	drvinfo->n_priv_flags = IAVF_PRIV_FLAGS_STR_LEN;
580 }
581 
582 /**
583  * iavf_get_ringparam - Get ring parameters
584  * @netdev: network interface device structure
585  * @ring: ethtool ringparam structure
586  *
587  * Returns current ring parameters. TX and RX rings are reported separately,
588  * but the number of rings is not reported.
589  **/
590 static void iavf_get_ringparam(struct net_device *netdev,
591 			       struct ethtool_ringparam *ring)
592 {
593 	struct iavf_adapter *adapter = netdev_priv(netdev);
594 
595 	ring->rx_max_pending = IAVF_MAX_RXD;
596 	ring->tx_max_pending = IAVF_MAX_TXD;
597 	ring->rx_pending = adapter->rx_desc_count;
598 	ring->tx_pending = adapter->tx_desc_count;
599 }
600 
601 /**
602  * iavf_set_ringparam - Set ring parameters
603  * @netdev: network interface device structure
604  * @ring: ethtool ringparam structure
605  *
606  * Sets ring parameters. TX and RX rings are controlled separately, but the
607  * number of rings is not specified, so all rings get the same settings.
608  **/
609 static int iavf_set_ringparam(struct net_device *netdev,
610 			      struct ethtool_ringparam *ring)
611 {
612 	struct iavf_adapter *adapter = netdev_priv(netdev);
613 	u32 new_rx_count, new_tx_count;
614 
615 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
616 		return -EINVAL;
617 
618 	new_tx_count = clamp_t(u32, ring->tx_pending,
619 			       IAVF_MIN_TXD,
620 			       IAVF_MAX_TXD);
621 	new_tx_count = ALIGN(new_tx_count, IAVF_REQ_DESCRIPTOR_MULTIPLE);
622 
623 	new_rx_count = clamp_t(u32, ring->rx_pending,
624 			       IAVF_MIN_RXD,
625 			       IAVF_MAX_RXD);
626 	new_rx_count = ALIGN(new_rx_count, IAVF_REQ_DESCRIPTOR_MULTIPLE);
627 
628 	/* if nothing to do return success */
629 	if ((new_tx_count == adapter->tx_desc_count) &&
630 	    (new_rx_count == adapter->rx_desc_count))
631 		return 0;
632 
633 	adapter->tx_desc_count = new_tx_count;
634 	adapter->rx_desc_count = new_rx_count;
635 
636 	if (netif_running(netdev)) {
637 		adapter->flags |= IAVF_FLAG_RESET_NEEDED;
638 		queue_work(iavf_wq, &adapter->reset_task);
639 	}
640 
641 	return 0;
642 }
643 
644 /**
645  * __iavf_get_coalesce - get per-queue coalesce settings
646  * @netdev: the netdev to check
647  * @ec: ethtool coalesce data structure
648  * @queue: which queue to pick
649  *
650  * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
651  * are per queue. If queue is <0 then we default to queue 0 as the
652  * representative value.
653  **/
654 static int __iavf_get_coalesce(struct net_device *netdev,
655 			       struct ethtool_coalesce *ec, int queue)
656 {
657 	struct iavf_adapter *adapter = netdev_priv(netdev);
658 	struct iavf_vsi *vsi = &adapter->vsi;
659 	struct iavf_ring *rx_ring, *tx_ring;
660 
661 	ec->tx_max_coalesced_frames = vsi->work_limit;
662 	ec->rx_max_coalesced_frames = vsi->work_limit;
663 
664 	/* Rx and Tx usecs per queue value. If user doesn't specify the
665 	 * queue, return queue 0's value to represent.
666 	 */
667 	if (queue < 0)
668 		queue = 0;
669 	else if (queue >= adapter->num_active_queues)
670 		return -EINVAL;
671 
672 	rx_ring = &adapter->rx_rings[queue];
673 	tx_ring = &adapter->tx_rings[queue];
674 
675 	if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
676 		ec->use_adaptive_rx_coalesce = 1;
677 
678 	if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
679 		ec->use_adaptive_tx_coalesce = 1;
680 
681 	ec->rx_coalesce_usecs = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
682 	ec->tx_coalesce_usecs = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
683 
684 	return 0;
685 }
686 
687 /**
688  * iavf_get_coalesce - Get interrupt coalescing settings
689  * @netdev: network interface device structure
690  * @ec: ethtool coalesce structure
691  * @kernel_coal: ethtool CQE mode setting structure
692  * @extack: extack for reporting error messages
693  *
694  * Returns current coalescing settings. This is referred to elsewhere in the
695  * driver as Interrupt Throttle Rate, as this is how the hardware describes
696  * this functionality. Note that if per-queue settings have been modified this
697  * only represents the settings of queue 0.
698  **/
699 static int iavf_get_coalesce(struct net_device *netdev,
700 			     struct ethtool_coalesce *ec,
701 			     struct kernel_ethtool_coalesce *kernel_coal,
702 			     struct netlink_ext_ack *extack)
703 {
704 	return __iavf_get_coalesce(netdev, ec, -1);
705 }
706 
707 /**
708  * iavf_get_per_queue_coalesce - get coalesce values for specific queue
709  * @netdev: netdev to read
710  * @ec: coalesce settings from ethtool
711  * @queue: the queue to read
712  *
713  * Read specific queue's coalesce settings.
714  **/
715 static int iavf_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
716 				       struct ethtool_coalesce *ec)
717 {
718 	return __iavf_get_coalesce(netdev, ec, queue);
719 }
720 
721 /**
722  * iavf_set_itr_per_queue - set ITR values for specific queue
723  * @adapter: the VF adapter struct to set values for
724  * @ec: coalesce settings from ethtool
725  * @queue: the queue to modify
726  *
727  * Change the ITR settings for a specific queue.
728  **/
729 static int iavf_set_itr_per_queue(struct iavf_adapter *adapter,
730 				  struct ethtool_coalesce *ec, int queue)
731 {
732 	struct iavf_ring *rx_ring = &adapter->rx_rings[queue];
733 	struct iavf_ring *tx_ring = &adapter->tx_rings[queue];
734 	struct iavf_q_vector *q_vector;
735 	u16 itr_setting;
736 
737 	itr_setting = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
738 
739 	if (ec->rx_coalesce_usecs != itr_setting &&
740 	    ec->use_adaptive_rx_coalesce) {
741 		netif_info(adapter, drv, adapter->netdev,
742 			   "Rx interrupt throttling cannot be changed if adaptive-rx is enabled\n");
743 		return -EINVAL;
744 	}
745 
746 	itr_setting = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
747 
748 	if (ec->tx_coalesce_usecs != itr_setting &&
749 	    ec->use_adaptive_tx_coalesce) {
750 		netif_info(adapter, drv, adapter->netdev,
751 			   "Tx interrupt throttling cannot be changed if adaptive-tx is enabled\n");
752 		return -EINVAL;
753 	}
754 
755 	rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
756 	tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
757 
758 	rx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
759 	if (!ec->use_adaptive_rx_coalesce)
760 		rx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
761 
762 	tx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
763 	if (!ec->use_adaptive_tx_coalesce)
764 		tx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
765 
766 	q_vector = rx_ring->q_vector;
767 	q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
768 
769 	q_vector = tx_ring->q_vector;
770 	q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
771 
772 	/* The interrupt handler itself will take care of programming
773 	 * the Tx and Rx ITR values based on the values we have entered
774 	 * into the q_vector, no need to write the values now.
775 	 */
776 	return 0;
777 }
778 
779 /**
780  * __iavf_set_coalesce - set coalesce settings for particular queue
781  * @netdev: the netdev to change
782  * @ec: ethtool coalesce settings
783  * @queue: the queue to change
784  *
785  * Sets the coalesce settings for a particular queue.
786  **/
787 static int __iavf_set_coalesce(struct net_device *netdev,
788 			       struct ethtool_coalesce *ec, int queue)
789 {
790 	struct iavf_adapter *adapter = netdev_priv(netdev);
791 	struct iavf_vsi *vsi = &adapter->vsi;
792 	int i;
793 
794 	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
795 		vsi->work_limit = ec->tx_max_coalesced_frames_irq;
796 
797 	if (ec->rx_coalesce_usecs == 0) {
798 		if (ec->use_adaptive_rx_coalesce)
799 			netif_info(adapter, drv, netdev, "rx-usecs=0, need to disable adaptive-rx for a complete disable\n");
800 	} else if ((ec->rx_coalesce_usecs < IAVF_MIN_ITR) ||
801 		   (ec->rx_coalesce_usecs > IAVF_MAX_ITR)) {
802 		netif_info(adapter, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
803 		return -EINVAL;
804 	} else if (ec->tx_coalesce_usecs == 0) {
805 		if (ec->use_adaptive_tx_coalesce)
806 			netif_info(adapter, drv, netdev, "tx-usecs=0, need to disable adaptive-tx for a complete disable\n");
807 	} else if ((ec->tx_coalesce_usecs < IAVF_MIN_ITR) ||
808 		   (ec->tx_coalesce_usecs > IAVF_MAX_ITR)) {
809 		netif_info(adapter, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
810 		return -EINVAL;
811 	}
812 
813 	/* Rx and Tx usecs has per queue value. If user doesn't specify the
814 	 * queue, apply to all queues.
815 	 */
816 	if (queue < 0) {
817 		for (i = 0; i < adapter->num_active_queues; i++)
818 			if (iavf_set_itr_per_queue(adapter, ec, i))
819 				return -EINVAL;
820 	} else if (queue < adapter->num_active_queues) {
821 		if (iavf_set_itr_per_queue(adapter, ec, queue))
822 			return -EINVAL;
823 	} else {
824 		netif_info(adapter, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
825 			   adapter->num_active_queues - 1);
826 		return -EINVAL;
827 	}
828 
829 	return 0;
830 }
831 
832 /**
833  * iavf_set_coalesce - Set interrupt coalescing settings
834  * @netdev: network interface device structure
835  * @ec: ethtool coalesce structure
836  * @kernel_coal: ethtool CQE mode setting structure
837  * @extack: extack for reporting error messages
838  *
839  * Change current coalescing settings for every queue.
840  **/
841 static int iavf_set_coalesce(struct net_device *netdev,
842 			     struct ethtool_coalesce *ec,
843 			     struct kernel_ethtool_coalesce *kernel_coal,
844 			     struct netlink_ext_ack *extack)
845 {
846 	return __iavf_set_coalesce(netdev, ec, -1);
847 }
848 
849 /**
850  * iavf_set_per_queue_coalesce - set specific queue's coalesce settings
851  * @netdev: the netdev to change
852  * @ec: ethtool's coalesce settings
853  * @queue: the queue to modify
854  *
855  * Modifies a specific queue's coalesce settings.
856  */
857 static int iavf_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
858 				       struct ethtool_coalesce *ec)
859 {
860 	return __iavf_set_coalesce(netdev, ec, queue);
861 }
862 
863 /**
864  * iavf_fltr_to_ethtool_flow - convert filter type values to ethtool
865  * flow type values
866  * @flow: filter type to be converted
867  *
868  * Returns the corresponding ethtool flow type.
869  */
870 static int iavf_fltr_to_ethtool_flow(enum iavf_fdir_flow_type flow)
871 {
872 	switch (flow) {
873 	case IAVF_FDIR_FLOW_IPV4_TCP:
874 		return TCP_V4_FLOW;
875 	case IAVF_FDIR_FLOW_IPV4_UDP:
876 		return UDP_V4_FLOW;
877 	case IAVF_FDIR_FLOW_IPV4_SCTP:
878 		return SCTP_V4_FLOW;
879 	case IAVF_FDIR_FLOW_IPV4_AH:
880 		return AH_V4_FLOW;
881 	case IAVF_FDIR_FLOW_IPV4_ESP:
882 		return ESP_V4_FLOW;
883 	case IAVF_FDIR_FLOW_IPV4_OTHER:
884 		return IPV4_USER_FLOW;
885 	case IAVF_FDIR_FLOW_IPV6_TCP:
886 		return TCP_V6_FLOW;
887 	case IAVF_FDIR_FLOW_IPV6_UDP:
888 		return UDP_V6_FLOW;
889 	case IAVF_FDIR_FLOW_IPV6_SCTP:
890 		return SCTP_V6_FLOW;
891 	case IAVF_FDIR_FLOW_IPV6_AH:
892 		return AH_V6_FLOW;
893 	case IAVF_FDIR_FLOW_IPV6_ESP:
894 		return ESP_V6_FLOW;
895 	case IAVF_FDIR_FLOW_IPV6_OTHER:
896 		return IPV6_USER_FLOW;
897 	case IAVF_FDIR_FLOW_NON_IP_L2:
898 		return ETHER_FLOW;
899 	default:
900 		/* 0 is undefined ethtool flow */
901 		return 0;
902 	}
903 }
904 
905 /**
906  * iavf_ethtool_flow_to_fltr - convert ethtool flow type to filter enum
907  * @eth: Ethtool flow type to be converted
908  *
909  * Returns flow enum
910  */
911 static enum iavf_fdir_flow_type iavf_ethtool_flow_to_fltr(int eth)
912 {
913 	switch (eth) {
914 	case TCP_V4_FLOW:
915 		return IAVF_FDIR_FLOW_IPV4_TCP;
916 	case UDP_V4_FLOW:
917 		return IAVF_FDIR_FLOW_IPV4_UDP;
918 	case SCTP_V4_FLOW:
919 		return IAVF_FDIR_FLOW_IPV4_SCTP;
920 	case AH_V4_FLOW:
921 		return IAVF_FDIR_FLOW_IPV4_AH;
922 	case ESP_V4_FLOW:
923 		return IAVF_FDIR_FLOW_IPV4_ESP;
924 	case IPV4_USER_FLOW:
925 		return IAVF_FDIR_FLOW_IPV4_OTHER;
926 	case TCP_V6_FLOW:
927 		return IAVF_FDIR_FLOW_IPV6_TCP;
928 	case UDP_V6_FLOW:
929 		return IAVF_FDIR_FLOW_IPV6_UDP;
930 	case SCTP_V6_FLOW:
931 		return IAVF_FDIR_FLOW_IPV6_SCTP;
932 	case AH_V6_FLOW:
933 		return IAVF_FDIR_FLOW_IPV6_AH;
934 	case ESP_V6_FLOW:
935 		return IAVF_FDIR_FLOW_IPV6_ESP;
936 	case IPV6_USER_FLOW:
937 		return IAVF_FDIR_FLOW_IPV6_OTHER;
938 	case ETHER_FLOW:
939 		return IAVF_FDIR_FLOW_NON_IP_L2;
940 	default:
941 		return IAVF_FDIR_FLOW_NONE;
942 	}
943 }
944 
945 /**
946  * iavf_is_mask_valid - check mask field set
947  * @mask: full mask to check
948  * @field: field for which mask should be valid
949  *
950  * If the mask is fully set return true. If it is not valid for field return
951  * false.
952  */
953 static bool iavf_is_mask_valid(u64 mask, u64 field)
954 {
955 	return (mask & field) == field;
956 }
957 
958 /**
959  * iavf_parse_rx_flow_user_data - deconstruct user-defined data
960  * @fsp: pointer to ethtool Rx flow specification
961  * @fltr: pointer to Flow Director filter for userdef data storage
962  *
963  * Returns 0 on success, negative error value on failure
964  */
965 static int
966 iavf_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
967 			     struct iavf_fdir_fltr *fltr)
968 {
969 	struct iavf_flex_word *flex;
970 	int i, cnt = 0;
971 
972 	if (!(fsp->flow_type & FLOW_EXT))
973 		return 0;
974 
975 	for (i = 0; i < IAVF_FLEX_WORD_NUM; i++) {
976 #define IAVF_USERDEF_FLEX_WORD_M	GENMASK(15, 0)
977 #define IAVF_USERDEF_FLEX_OFFS_S	16
978 #define IAVF_USERDEF_FLEX_OFFS_M	GENMASK(31, IAVF_USERDEF_FLEX_OFFS_S)
979 #define IAVF_USERDEF_FLEX_FLTR_M	GENMASK(31, 0)
980 		u32 value = be32_to_cpu(fsp->h_ext.data[i]);
981 		u32 mask = be32_to_cpu(fsp->m_ext.data[i]);
982 
983 		if (!value || !mask)
984 			continue;
985 
986 		if (!iavf_is_mask_valid(mask, IAVF_USERDEF_FLEX_FLTR_M))
987 			return -EINVAL;
988 
989 		/* 504 is the maximum value for offsets, and offset is measured
990 		 * from the start of the MAC address.
991 		 */
992 #define IAVF_USERDEF_FLEX_MAX_OFFS_VAL 504
993 		flex = &fltr->flex_words[cnt++];
994 		flex->word = value & IAVF_USERDEF_FLEX_WORD_M;
995 		flex->offset = (value & IAVF_USERDEF_FLEX_OFFS_M) >>
996 			     IAVF_USERDEF_FLEX_OFFS_S;
997 		if (flex->offset > IAVF_USERDEF_FLEX_MAX_OFFS_VAL)
998 			return -EINVAL;
999 	}
1000 
1001 	fltr->flex_cnt = cnt;
1002 
1003 	return 0;
1004 }
1005 
1006 /**
1007  * iavf_fill_rx_flow_ext_data - fill the additional data
1008  * @fsp: pointer to ethtool Rx flow specification
1009  * @fltr: pointer to Flow Director filter to get additional data
1010  */
1011 static void
1012 iavf_fill_rx_flow_ext_data(struct ethtool_rx_flow_spec *fsp,
1013 			   struct iavf_fdir_fltr *fltr)
1014 {
1015 	if (!fltr->ext_mask.usr_def[0] && !fltr->ext_mask.usr_def[1])
1016 		return;
1017 
1018 	fsp->flow_type |= FLOW_EXT;
1019 
1020 	memcpy(fsp->h_ext.data, fltr->ext_data.usr_def, sizeof(fsp->h_ext.data));
1021 	memcpy(fsp->m_ext.data, fltr->ext_mask.usr_def, sizeof(fsp->m_ext.data));
1022 }
1023 
1024 /**
1025  * iavf_get_ethtool_fdir_entry - fill ethtool structure with Flow Director filter data
1026  * @adapter: the VF adapter structure that contains filter list
1027  * @cmd: ethtool command data structure to receive the filter data
1028  *
1029  * Returns 0 as expected for success by ethtool
1030  */
1031 static int
1032 iavf_get_ethtool_fdir_entry(struct iavf_adapter *adapter,
1033 			    struct ethtool_rxnfc *cmd)
1034 {
1035 	struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1036 	struct iavf_fdir_fltr *rule = NULL;
1037 	int ret = 0;
1038 
1039 	if (!FDIR_FLTR_SUPPORT(adapter))
1040 		return -EOPNOTSUPP;
1041 
1042 	spin_lock_bh(&adapter->fdir_fltr_lock);
1043 
1044 	rule = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1045 	if (!rule) {
1046 		ret = -EINVAL;
1047 		goto release_lock;
1048 	}
1049 
1050 	fsp->flow_type = iavf_fltr_to_ethtool_flow(rule->flow_type);
1051 
1052 	memset(&fsp->m_u, 0, sizeof(fsp->m_u));
1053 	memset(&fsp->m_ext, 0, sizeof(fsp->m_ext));
1054 
1055 	switch (fsp->flow_type) {
1056 	case TCP_V4_FLOW:
1057 	case UDP_V4_FLOW:
1058 	case SCTP_V4_FLOW:
1059 		fsp->h_u.tcp_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1060 		fsp->h_u.tcp_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1061 		fsp->h_u.tcp_ip4_spec.psrc = rule->ip_data.src_port;
1062 		fsp->h_u.tcp_ip4_spec.pdst = rule->ip_data.dst_port;
1063 		fsp->h_u.tcp_ip4_spec.tos = rule->ip_data.tos;
1064 		fsp->m_u.tcp_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1065 		fsp->m_u.tcp_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1066 		fsp->m_u.tcp_ip4_spec.psrc = rule->ip_mask.src_port;
1067 		fsp->m_u.tcp_ip4_spec.pdst = rule->ip_mask.dst_port;
1068 		fsp->m_u.tcp_ip4_spec.tos = rule->ip_mask.tos;
1069 		break;
1070 	case AH_V4_FLOW:
1071 	case ESP_V4_FLOW:
1072 		fsp->h_u.ah_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1073 		fsp->h_u.ah_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1074 		fsp->h_u.ah_ip4_spec.spi = rule->ip_data.spi;
1075 		fsp->h_u.ah_ip4_spec.tos = rule->ip_data.tos;
1076 		fsp->m_u.ah_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1077 		fsp->m_u.ah_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1078 		fsp->m_u.ah_ip4_spec.spi = rule->ip_mask.spi;
1079 		fsp->m_u.ah_ip4_spec.tos = rule->ip_mask.tos;
1080 		break;
1081 	case IPV4_USER_FLOW:
1082 		fsp->h_u.usr_ip4_spec.ip4src = rule->ip_data.v4_addrs.src_ip;
1083 		fsp->h_u.usr_ip4_spec.ip4dst = rule->ip_data.v4_addrs.dst_ip;
1084 		fsp->h_u.usr_ip4_spec.l4_4_bytes = rule->ip_data.l4_header;
1085 		fsp->h_u.usr_ip4_spec.tos = rule->ip_data.tos;
1086 		fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
1087 		fsp->h_u.usr_ip4_spec.proto = rule->ip_data.proto;
1088 		fsp->m_u.usr_ip4_spec.ip4src = rule->ip_mask.v4_addrs.src_ip;
1089 		fsp->m_u.usr_ip4_spec.ip4dst = rule->ip_mask.v4_addrs.dst_ip;
1090 		fsp->m_u.usr_ip4_spec.l4_4_bytes = rule->ip_mask.l4_header;
1091 		fsp->m_u.usr_ip4_spec.tos = rule->ip_mask.tos;
1092 		fsp->m_u.usr_ip4_spec.ip_ver = 0xFF;
1093 		fsp->m_u.usr_ip4_spec.proto = rule->ip_mask.proto;
1094 		break;
1095 	case TCP_V6_FLOW:
1096 	case UDP_V6_FLOW:
1097 	case SCTP_V6_FLOW:
1098 		memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1099 		       sizeof(struct in6_addr));
1100 		memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1101 		       sizeof(struct in6_addr));
1102 		fsp->h_u.tcp_ip6_spec.psrc = rule->ip_data.src_port;
1103 		fsp->h_u.tcp_ip6_spec.pdst = rule->ip_data.dst_port;
1104 		fsp->h_u.tcp_ip6_spec.tclass = rule->ip_data.tclass;
1105 		memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1106 		       sizeof(struct in6_addr));
1107 		memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1108 		       sizeof(struct in6_addr));
1109 		fsp->m_u.tcp_ip6_spec.psrc = rule->ip_mask.src_port;
1110 		fsp->m_u.tcp_ip6_spec.pdst = rule->ip_mask.dst_port;
1111 		fsp->m_u.tcp_ip6_spec.tclass = rule->ip_mask.tclass;
1112 		break;
1113 	case AH_V6_FLOW:
1114 	case ESP_V6_FLOW:
1115 		memcpy(fsp->h_u.ah_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1116 		       sizeof(struct in6_addr));
1117 		memcpy(fsp->h_u.ah_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1118 		       sizeof(struct in6_addr));
1119 		fsp->h_u.ah_ip6_spec.spi = rule->ip_data.spi;
1120 		fsp->h_u.ah_ip6_spec.tclass = rule->ip_data.tclass;
1121 		memcpy(fsp->m_u.ah_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1122 		       sizeof(struct in6_addr));
1123 		memcpy(fsp->m_u.ah_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1124 		       sizeof(struct in6_addr));
1125 		fsp->m_u.ah_ip6_spec.spi = rule->ip_mask.spi;
1126 		fsp->m_u.ah_ip6_spec.tclass = rule->ip_mask.tclass;
1127 		break;
1128 	case IPV6_USER_FLOW:
1129 		memcpy(fsp->h_u.usr_ip6_spec.ip6src, &rule->ip_data.v6_addrs.src_ip,
1130 		       sizeof(struct in6_addr));
1131 		memcpy(fsp->h_u.usr_ip6_spec.ip6dst, &rule->ip_data.v6_addrs.dst_ip,
1132 		       sizeof(struct in6_addr));
1133 		fsp->h_u.usr_ip6_spec.l4_4_bytes = rule->ip_data.l4_header;
1134 		fsp->h_u.usr_ip6_spec.tclass = rule->ip_data.tclass;
1135 		fsp->h_u.usr_ip6_spec.l4_proto = rule->ip_data.proto;
1136 		memcpy(fsp->m_u.usr_ip6_spec.ip6src, &rule->ip_mask.v6_addrs.src_ip,
1137 		       sizeof(struct in6_addr));
1138 		memcpy(fsp->m_u.usr_ip6_spec.ip6dst, &rule->ip_mask.v6_addrs.dst_ip,
1139 		       sizeof(struct in6_addr));
1140 		fsp->m_u.usr_ip6_spec.l4_4_bytes = rule->ip_mask.l4_header;
1141 		fsp->m_u.usr_ip6_spec.tclass = rule->ip_mask.tclass;
1142 		fsp->m_u.usr_ip6_spec.l4_proto = rule->ip_mask.proto;
1143 		break;
1144 	case ETHER_FLOW:
1145 		fsp->h_u.ether_spec.h_proto = rule->eth_data.etype;
1146 		fsp->m_u.ether_spec.h_proto = rule->eth_mask.etype;
1147 		break;
1148 	default:
1149 		ret = -EINVAL;
1150 		break;
1151 	}
1152 
1153 	iavf_fill_rx_flow_ext_data(fsp, rule);
1154 
1155 	if (rule->action == VIRTCHNL_ACTION_DROP)
1156 		fsp->ring_cookie = RX_CLS_FLOW_DISC;
1157 	else
1158 		fsp->ring_cookie = rule->q_index;
1159 
1160 release_lock:
1161 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1162 	return ret;
1163 }
1164 
1165 /**
1166  * iavf_get_fdir_fltr_ids - fill buffer with filter IDs of active filters
1167  * @adapter: the VF adapter structure containing the filter list
1168  * @cmd: ethtool command data structure
1169  * @rule_locs: ethtool array passed in from OS to receive filter IDs
1170  *
1171  * Returns 0 as expected for success by ethtool
1172  */
1173 static int
1174 iavf_get_fdir_fltr_ids(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd,
1175 		       u32 *rule_locs)
1176 {
1177 	struct iavf_fdir_fltr *fltr;
1178 	unsigned int cnt = 0;
1179 	int val = 0;
1180 
1181 	if (!FDIR_FLTR_SUPPORT(adapter))
1182 		return -EOPNOTSUPP;
1183 
1184 	cmd->data = IAVF_MAX_FDIR_FILTERS;
1185 
1186 	spin_lock_bh(&adapter->fdir_fltr_lock);
1187 
1188 	list_for_each_entry(fltr, &adapter->fdir_list_head, list) {
1189 		if (cnt == cmd->rule_cnt) {
1190 			val = -EMSGSIZE;
1191 			goto release_lock;
1192 		}
1193 		rule_locs[cnt] = fltr->loc;
1194 		cnt++;
1195 	}
1196 
1197 release_lock:
1198 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1199 	if (!val)
1200 		cmd->rule_cnt = cnt;
1201 
1202 	return val;
1203 }
1204 
1205 /**
1206  * iavf_add_fdir_fltr_info - Set the input set for Flow Director filter
1207  * @adapter: pointer to the VF adapter structure
1208  * @fsp: pointer to ethtool Rx flow specification
1209  * @fltr: filter structure
1210  */
1211 static int
1212 iavf_add_fdir_fltr_info(struct iavf_adapter *adapter, struct ethtool_rx_flow_spec *fsp,
1213 			struct iavf_fdir_fltr *fltr)
1214 {
1215 	u32 flow_type, q_index = 0;
1216 	enum virtchnl_action act;
1217 	int err;
1218 
1219 	if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
1220 		act = VIRTCHNL_ACTION_DROP;
1221 	} else {
1222 		q_index = fsp->ring_cookie;
1223 		if (q_index >= adapter->num_active_queues)
1224 			return -EINVAL;
1225 
1226 		act = VIRTCHNL_ACTION_QUEUE;
1227 	}
1228 
1229 	fltr->action = act;
1230 	fltr->loc = fsp->location;
1231 	fltr->q_index = q_index;
1232 
1233 	if (fsp->flow_type & FLOW_EXT) {
1234 		memcpy(fltr->ext_data.usr_def, fsp->h_ext.data,
1235 		       sizeof(fltr->ext_data.usr_def));
1236 		memcpy(fltr->ext_mask.usr_def, fsp->m_ext.data,
1237 		       sizeof(fltr->ext_mask.usr_def));
1238 	}
1239 
1240 	flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
1241 	fltr->flow_type = iavf_ethtool_flow_to_fltr(flow_type);
1242 
1243 	switch (flow_type) {
1244 	case TCP_V4_FLOW:
1245 	case UDP_V4_FLOW:
1246 	case SCTP_V4_FLOW:
1247 		fltr->ip_data.v4_addrs.src_ip = fsp->h_u.tcp_ip4_spec.ip4src;
1248 		fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
1249 		fltr->ip_data.src_port = fsp->h_u.tcp_ip4_spec.psrc;
1250 		fltr->ip_data.dst_port = fsp->h_u.tcp_ip4_spec.pdst;
1251 		fltr->ip_data.tos = fsp->h_u.tcp_ip4_spec.tos;
1252 		fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.tcp_ip4_spec.ip4src;
1253 		fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.tcp_ip4_spec.ip4dst;
1254 		fltr->ip_mask.src_port = fsp->m_u.tcp_ip4_spec.psrc;
1255 		fltr->ip_mask.dst_port = fsp->m_u.tcp_ip4_spec.pdst;
1256 		fltr->ip_mask.tos = fsp->m_u.tcp_ip4_spec.tos;
1257 		break;
1258 	case AH_V4_FLOW:
1259 	case ESP_V4_FLOW:
1260 		fltr->ip_data.v4_addrs.src_ip = fsp->h_u.ah_ip4_spec.ip4src;
1261 		fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.ah_ip4_spec.ip4dst;
1262 		fltr->ip_data.spi = fsp->h_u.ah_ip4_spec.spi;
1263 		fltr->ip_data.tos = fsp->h_u.ah_ip4_spec.tos;
1264 		fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.ah_ip4_spec.ip4src;
1265 		fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.ah_ip4_spec.ip4dst;
1266 		fltr->ip_mask.spi = fsp->m_u.ah_ip4_spec.spi;
1267 		fltr->ip_mask.tos = fsp->m_u.ah_ip4_spec.tos;
1268 		break;
1269 	case IPV4_USER_FLOW:
1270 		fltr->ip_data.v4_addrs.src_ip = fsp->h_u.usr_ip4_spec.ip4src;
1271 		fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.usr_ip4_spec.ip4dst;
1272 		fltr->ip_data.l4_header = fsp->h_u.usr_ip4_spec.l4_4_bytes;
1273 		fltr->ip_data.tos = fsp->h_u.usr_ip4_spec.tos;
1274 		fltr->ip_data.proto = fsp->h_u.usr_ip4_spec.proto;
1275 		fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.usr_ip4_spec.ip4src;
1276 		fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.usr_ip4_spec.ip4dst;
1277 		fltr->ip_mask.l4_header = fsp->m_u.usr_ip4_spec.l4_4_bytes;
1278 		fltr->ip_mask.tos = fsp->m_u.usr_ip4_spec.tos;
1279 		fltr->ip_mask.proto = fsp->m_u.usr_ip4_spec.proto;
1280 		break;
1281 	case TCP_V6_FLOW:
1282 	case UDP_V6_FLOW:
1283 	case SCTP_V6_FLOW:
1284 		memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1285 		       sizeof(struct in6_addr));
1286 		memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1287 		       sizeof(struct in6_addr));
1288 		fltr->ip_data.src_port = fsp->h_u.tcp_ip6_spec.psrc;
1289 		fltr->ip_data.dst_port = fsp->h_u.tcp_ip6_spec.pdst;
1290 		fltr->ip_data.tclass = fsp->h_u.tcp_ip6_spec.tclass;
1291 		memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1292 		       sizeof(struct in6_addr));
1293 		memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1294 		       sizeof(struct in6_addr));
1295 		fltr->ip_mask.src_port = fsp->m_u.tcp_ip6_spec.psrc;
1296 		fltr->ip_mask.dst_port = fsp->m_u.tcp_ip6_spec.pdst;
1297 		fltr->ip_mask.tclass = fsp->m_u.tcp_ip6_spec.tclass;
1298 		break;
1299 	case AH_V6_FLOW:
1300 	case ESP_V6_FLOW:
1301 		memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.ah_ip6_spec.ip6src,
1302 		       sizeof(struct in6_addr));
1303 		memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.ah_ip6_spec.ip6dst,
1304 		       sizeof(struct in6_addr));
1305 		fltr->ip_data.spi = fsp->h_u.ah_ip6_spec.spi;
1306 		fltr->ip_data.tclass = fsp->h_u.ah_ip6_spec.tclass;
1307 		memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.ah_ip6_spec.ip6src,
1308 		       sizeof(struct in6_addr));
1309 		memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.ah_ip6_spec.ip6dst,
1310 		       sizeof(struct in6_addr));
1311 		fltr->ip_mask.spi = fsp->m_u.ah_ip6_spec.spi;
1312 		fltr->ip_mask.tclass = fsp->m_u.ah_ip6_spec.tclass;
1313 		break;
1314 	case IPV6_USER_FLOW:
1315 		memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1316 		       sizeof(struct in6_addr));
1317 		memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1318 		       sizeof(struct in6_addr));
1319 		fltr->ip_data.l4_header = fsp->h_u.usr_ip6_spec.l4_4_bytes;
1320 		fltr->ip_data.tclass = fsp->h_u.usr_ip6_spec.tclass;
1321 		fltr->ip_data.proto = fsp->h_u.usr_ip6_spec.l4_proto;
1322 		memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1323 		       sizeof(struct in6_addr));
1324 		memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1325 		       sizeof(struct in6_addr));
1326 		fltr->ip_mask.l4_header = fsp->m_u.usr_ip6_spec.l4_4_bytes;
1327 		fltr->ip_mask.tclass = fsp->m_u.usr_ip6_spec.tclass;
1328 		fltr->ip_mask.proto = fsp->m_u.usr_ip6_spec.l4_proto;
1329 		break;
1330 	case ETHER_FLOW:
1331 		fltr->eth_data.etype = fsp->h_u.ether_spec.h_proto;
1332 		fltr->eth_mask.etype = fsp->m_u.ether_spec.h_proto;
1333 		break;
1334 	default:
1335 		/* not doing un-parsed flow types */
1336 		return -EINVAL;
1337 	}
1338 
1339 	if (iavf_fdir_is_dup_fltr(adapter, fltr))
1340 		return -EEXIST;
1341 
1342 	err = iavf_parse_rx_flow_user_data(fsp, fltr);
1343 	if (err)
1344 		return err;
1345 
1346 	return iavf_fill_fdir_add_msg(adapter, fltr);
1347 }
1348 
1349 /**
1350  * iavf_add_fdir_ethtool - add Flow Director filter
1351  * @adapter: pointer to the VF adapter structure
1352  * @cmd: command to add Flow Director filter
1353  *
1354  * Returns 0 on success and negative values for failure
1355  */
1356 static int iavf_add_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1357 {
1358 	struct ethtool_rx_flow_spec *fsp = &cmd->fs;
1359 	struct iavf_fdir_fltr *fltr;
1360 	int count = 50;
1361 	int err;
1362 
1363 	if (!FDIR_FLTR_SUPPORT(adapter))
1364 		return -EOPNOTSUPP;
1365 
1366 	if (fsp->flow_type & FLOW_MAC_EXT)
1367 		return -EINVAL;
1368 
1369 	if (adapter->fdir_active_fltr >= IAVF_MAX_FDIR_FILTERS) {
1370 		dev_err(&adapter->pdev->dev,
1371 			"Unable to add Flow Director filter because VF reached the limit of max allowed filters (%u)\n",
1372 			IAVF_MAX_FDIR_FILTERS);
1373 		return -ENOSPC;
1374 	}
1375 
1376 	spin_lock_bh(&adapter->fdir_fltr_lock);
1377 	if (iavf_find_fdir_fltr_by_loc(adapter, fsp->location)) {
1378 		dev_err(&adapter->pdev->dev, "Failed to add Flow Director filter, it already exists\n");
1379 		spin_unlock_bh(&adapter->fdir_fltr_lock);
1380 		return -EEXIST;
1381 	}
1382 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1383 
1384 	fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1385 	if (!fltr)
1386 		return -ENOMEM;
1387 
1388 	while (!mutex_trylock(&adapter->crit_lock)) {
1389 		if (--count == 0) {
1390 			kfree(fltr);
1391 			return -EINVAL;
1392 		}
1393 		udelay(1);
1394 	}
1395 
1396 	err = iavf_add_fdir_fltr_info(adapter, fsp, fltr);
1397 	if (err)
1398 		goto ret;
1399 
1400 	spin_lock_bh(&adapter->fdir_fltr_lock);
1401 	iavf_fdir_list_add_fltr(adapter, fltr);
1402 	adapter->fdir_active_fltr++;
1403 	fltr->state = IAVF_FDIR_FLTR_ADD_REQUEST;
1404 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_FDIR_FILTER;
1405 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1406 
1407 	mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1408 
1409 ret:
1410 	if (err && fltr)
1411 		kfree(fltr);
1412 
1413 	mutex_unlock(&adapter->crit_lock);
1414 	return err;
1415 }
1416 
1417 /**
1418  * iavf_del_fdir_ethtool - delete Flow Director filter
1419  * @adapter: pointer to the VF adapter structure
1420  * @cmd: command to delete Flow Director filter
1421  *
1422  * Returns 0 on success and negative values for failure
1423  */
1424 static int iavf_del_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1425 {
1426 	struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1427 	struct iavf_fdir_fltr *fltr = NULL;
1428 	int err = 0;
1429 
1430 	if (!FDIR_FLTR_SUPPORT(adapter))
1431 		return -EOPNOTSUPP;
1432 
1433 	spin_lock_bh(&adapter->fdir_fltr_lock);
1434 	fltr = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1435 	if (fltr) {
1436 		if (fltr->state == IAVF_FDIR_FLTR_ACTIVE) {
1437 			fltr->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1438 			adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1439 		} else {
1440 			err = -EBUSY;
1441 		}
1442 	} else if (adapter->fdir_active_fltr) {
1443 		err = -EINVAL;
1444 	}
1445 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1446 
1447 	if (fltr && fltr->state == IAVF_FDIR_FLTR_DEL_REQUEST)
1448 		mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1449 
1450 	return err;
1451 }
1452 
1453 /**
1454  * iavf_adv_rss_parse_hdrs - parses headers from RSS hash input
1455  * @cmd: ethtool rxnfc command
1456  *
1457  * This function parses the rxnfc command and returns intended
1458  * header types for RSS configuration
1459  */
1460 static u32 iavf_adv_rss_parse_hdrs(struct ethtool_rxnfc *cmd)
1461 {
1462 	u32 hdrs = IAVF_ADV_RSS_FLOW_SEG_HDR_NONE;
1463 
1464 	switch (cmd->flow_type) {
1465 	case TCP_V4_FLOW:
1466 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1467 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1468 		break;
1469 	case UDP_V4_FLOW:
1470 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1471 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1472 		break;
1473 	case SCTP_V4_FLOW:
1474 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1475 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1476 		break;
1477 	case TCP_V6_FLOW:
1478 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1479 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1480 		break;
1481 	case UDP_V6_FLOW:
1482 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1483 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1484 		break;
1485 	case SCTP_V6_FLOW:
1486 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1487 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1488 		break;
1489 	default:
1490 		break;
1491 	}
1492 
1493 	return hdrs;
1494 }
1495 
1496 /**
1497  * iavf_adv_rss_parse_hash_flds - parses hash fields from RSS hash input
1498  * @cmd: ethtool rxnfc command
1499  *
1500  * This function parses the rxnfc command and returns intended hash fields for
1501  * RSS configuration
1502  */
1503 static u64 iavf_adv_rss_parse_hash_flds(struct ethtool_rxnfc *cmd)
1504 {
1505 	u64 hfld = IAVF_ADV_RSS_HASH_INVALID;
1506 
1507 	if (cmd->data & RXH_IP_SRC || cmd->data & RXH_IP_DST) {
1508 		switch (cmd->flow_type) {
1509 		case TCP_V4_FLOW:
1510 		case UDP_V4_FLOW:
1511 		case SCTP_V4_FLOW:
1512 			if (cmd->data & RXH_IP_SRC)
1513 				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_SA;
1514 			if (cmd->data & RXH_IP_DST)
1515 				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_DA;
1516 			break;
1517 		case TCP_V6_FLOW:
1518 		case UDP_V6_FLOW:
1519 		case SCTP_V6_FLOW:
1520 			if (cmd->data & RXH_IP_SRC)
1521 				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_SA;
1522 			if (cmd->data & RXH_IP_DST)
1523 				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_DA;
1524 			break;
1525 		default:
1526 			break;
1527 		}
1528 	}
1529 
1530 	if (cmd->data & RXH_L4_B_0_1 || cmd->data & RXH_L4_B_2_3) {
1531 		switch (cmd->flow_type) {
1532 		case TCP_V4_FLOW:
1533 		case TCP_V6_FLOW:
1534 			if (cmd->data & RXH_L4_B_0_1)
1535 				hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT;
1536 			if (cmd->data & RXH_L4_B_2_3)
1537 				hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT;
1538 			break;
1539 		case UDP_V4_FLOW:
1540 		case UDP_V6_FLOW:
1541 			if (cmd->data & RXH_L4_B_0_1)
1542 				hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT;
1543 			if (cmd->data & RXH_L4_B_2_3)
1544 				hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT;
1545 			break;
1546 		case SCTP_V4_FLOW:
1547 		case SCTP_V6_FLOW:
1548 			if (cmd->data & RXH_L4_B_0_1)
1549 				hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT;
1550 			if (cmd->data & RXH_L4_B_2_3)
1551 				hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT;
1552 			break;
1553 		default:
1554 			break;
1555 		}
1556 	}
1557 
1558 	return hfld;
1559 }
1560 
1561 /**
1562  * iavf_set_adv_rss_hash_opt - Enable/Disable flow types for RSS hash
1563  * @adapter: pointer to the VF adapter structure
1564  * @cmd: ethtool rxnfc command
1565  *
1566  * Returns Success if the flow input set is supported.
1567  */
1568 static int
1569 iavf_set_adv_rss_hash_opt(struct iavf_adapter *adapter,
1570 			  struct ethtool_rxnfc *cmd)
1571 {
1572 	struct iavf_adv_rss *rss_old, *rss_new;
1573 	bool rss_new_add = false;
1574 	int count = 50, err = 0;
1575 	u64 hash_flds;
1576 	u32 hdrs;
1577 
1578 	if (!ADV_RSS_SUPPORT(adapter))
1579 		return -EOPNOTSUPP;
1580 
1581 	hdrs = iavf_adv_rss_parse_hdrs(cmd);
1582 	if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1583 		return -EINVAL;
1584 
1585 	hash_flds = iavf_adv_rss_parse_hash_flds(cmd);
1586 	if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1587 		return -EINVAL;
1588 
1589 	rss_new = kzalloc(sizeof(*rss_new), GFP_KERNEL);
1590 	if (!rss_new)
1591 		return -ENOMEM;
1592 
1593 	if (iavf_fill_adv_rss_cfg_msg(&rss_new->cfg_msg, hdrs, hash_flds)) {
1594 		kfree(rss_new);
1595 		return -EINVAL;
1596 	}
1597 
1598 	while (!mutex_trylock(&adapter->crit_lock)) {
1599 		if (--count == 0) {
1600 			kfree(rss_new);
1601 			return -EINVAL;
1602 		}
1603 
1604 		udelay(1);
1605 	}
1606 
1607 	spin_lock_bh(&adapter->adv_rss_lock);
1608 	rss_old = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1609 	if (rss_old) {
1610 		if (rss_old->state != IAVF_ADV_RSS_ACTIVE) {
1611 			err = -EBUSY;
1612 		} else if (rss_old->hash_flds != hash_flds) {
1613 			rss_old->state = IAVF_ADV_RSS_ADD_REQUEST;
1614 			rss_old->hash_flds = hash_flds;
1615 			memcpy(&rss_old->cfg_msg, &rss_new->cfg_msg,
1616 			       sizeof(rss_new->cfg_msg));
1617 			adapter->aq_required |= IAVF_FLAG_AQ_ADD_ADV_RSS_CFG;
1618 		} else {
1619 			err = -EEXIST;
1620 		}
1621 	} else {
1622 		rss_new_add = true;
1623 		rss_new->state = IAVF_ADV_RSS_ADD_REQUEST;
1624 		rss_new->packet_hdrs = hdrs;
1625 		rss_new->hash_flds = hash_flds;
1626 		list_add_tail(&rss_new->list, &adapter->adv_rss_list_head);
1627 		adapter->aq_required |= IAVF_FLAG_AQ_ADD_ADV_RSS_CFG;
1628 	}
1629 	spin_unlock_bh(&adapter->adv_rss_lock);
1630 
1631 	if (!err)
1632 		mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1633 
1634 	mutex_unlock(&adapter->crit_lock);
1635 
1636 	if (!rss_new_add)
1637 		kfree(rss_new);
1638 
1639 	return err;
1640 }
1641 
1642 /**
1643  * iavf_get_adv_rss_hash_opt - Retrieve hash fields for a given flow-type
1644  * @adapter: pointer to the VF adapter structure
1645  * @cmd: ethtool rxnfc command
1646  *
1647  * Returns Success if the flow input set is supported.
1648  */
1649 static int
1650 iavf_get_adv_rss_hash_opt(struct iavf_adapter *adapter,
1651 			  struct ethtool_rxnfc *cmd)
1652 {
1653 	struct iavf_adv_rss *rss;
1654 	u64 hash_flds;
1655 	u32 hdrs;
1656 
1657 	if (!ADV_RSS_SUPPORT(adapter))
1658 		return -EOPNOTSUPP;
1659 
1660 	cmd->data = 0;
1661 
1662 	hdrs = iavf_adv_rss_parse_hdrs(cmd);
1663 	if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1664 		return -EINVAL;
1665 
1666 	spin_lock_bh(&adapter->adv_rss_lock);
1667 	rss = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1668 	if (rss)
1669 		hash_flds = rss->hash_flds;
1670 	else
1671 		hash_flds = IAVF_ADV_RSS_HASH_INVALID;
1672 	spin_unlock_bh(&adapter->adv_rss_lock);
1673 
1674 	if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1675 		return -EINVAL;
1676 
1677 	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_SA |
1678 			 IAVF_ADV_RSS_HASH_FLD_IPV6_SA))
1679 		cmd->data |= (u64)RXH_IP_SRC;
1680 
1681 	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_DA |
1682 			 IAVF_ADV_RSS_HASH_FLD_IPV6_DA))
1683 		cmd->data |= (u64)RXH_IP_DST;
1684 
1685 	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT |
1686 			 IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT |
1687 			 IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT))
1688 		cmd->data |= (u64)RXH_L4_B_0_1;
1689 
1690 	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT |
1691 			 IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT |
1692 			 IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT))
1693 		cmd->data |= (u64)RXH_L4_B_2_3;
1694 
1695 	return 0;
1696 }
1697 
1698 /**
1699  * iavf_set_rxnfc - command to set Rx flow rules.
1700  * @netdev: network interface device structure
1701  * @cmd: ethtool rxnfc command
1702  *
1703  * Returns 0 for success and negative values for errors
1704  */
1705 static int iavf_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1706 {
1707 	struct iavf_adapter *adapter = netdev_priv(netdev);
1708 	int ret = -EOPNOTSUPP;
1709 
1710 	switch (cmd->cmd) {
1711 	case ETHTOOL_SRXCLSRLINS:
1712 		ret = iavf_add_fdir_ethtool(adapter, cmd);
1713 		break;
1714 	case ETHTOOL_SRXCLSRLDEL:
1715 		ret = iavf_del_fdir_ethtool(adapter, cmd);
1716 		break;
1717 	case ETHTOOL_SRXFH:
1718 		ret = iavf_set_adv_rss_hash_opt(adapter, cmd);
1719 		break;
1720 	default:
1721 		break;
1722 	}
1723 
1724 	return ret;
1725 }
1726 
1727 /**
1728  * iavf_get_rxnfc - command to get RX flow classification rules
1729  * @netdev: network interface device structure
1730  * @cmd: ethtool rxnfc command
1731  * @rule_locs: pointer to store rule locations
1732  *
1733  * Returns Success if the command is supported.
1734  **/
1735 static int iavf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1736 			  u32 *rule_locs)
1737 {
1738 	struct iavf_adapter *adapter = netdev_priv(netdev);
1739 	int ret = -EOPNOTSUPP;
1740 
1741 	switch (cmd->cmd) {
1742 	case ETHTOOL_GRXRINGS:
1743 		cmd->data = adapter->num_active_queues;
1744 		ret = 0;
1745 		break;
1746 	case ETHTOOL_GRXCLSRLCNT:
1747 		if (!FDIR_FLTR_SUPPORT(adapter))
1748 			break;
1749 		cmd->rule_cnt = adapter->fdir_active_fltr;
1750 		cmd->data = IAVF_MAX_FDIR_FILTERS;
1751 		ret = 0;
1752 		break;
1753 	case ETHTOOL_GRXCLSRULE:
1754 		ret = iavf_get_ethtool_fdir_entry(adapter, cmd);
1755 		break;
1756 	case ETHTOOL_GRXCLSRLALL:
1757 		ret = iavf_get_fdir_fltr_ids(adapter, cmd, (u32 *)rule_locs);
1758 		break;
1759 	case ETHTOOL_GRXFH:
1760 		ret = iavf_get_adv_rss_hash_opt(adapter, cmd);
1761 		break;
1762 	default:
1763 		break;
1764 	}
1765 
1766 	return ret;
1767 }
1768 /**
1769  * iavf_get_channels: get the number of channels supported by the device
1770  * @netdev: network interface device structure
1771  * @ch: channel information structure
1772  *
1773  * For the purposes of our device, we only use combined channels, i.e. a tx/rx
1774  * queue pair. Report one extra channel to match our "other" MSI-X vector.
1775  **/
1776 static void iavf_get_channels(struct net_device *netdev,
1777 			      struct ethtool_channels *ch)
1778 {
1779 	struct iavf_adapter *adapter = netdev_priv(netdev);
1780 
1781 	/* Report maximum channels */
1782 	ch->max_combined = adapter->vsi_res->num_queue_pairs;
1783 
1784 	ch->max_other = NONQ_VECS;
1785 	ch->other_count = NONQ_VECS;
1786 
1787 	ch->combined_count = adapter->num_active_queues;
1788 }
1789 
1790 /**
1791  * iavf_set_channels: set the new channel count
1792  * @netdev: network interface device structure
1793  * @ch: channel information structure
1794  *
1795  * Negotiate a new number of channels with the PF then do a reset.  During
1796  * reset we'll realloc queues and fix the RSS table.  Returns 0 on success,
1797  * negative on failure.
1798  **/
1799 static int iavf_set_channels(struct net_device *netdev,
1800 			     struct ethtool_channels *ch)
1801 {
1802 	struct iavf_adapter *adapter = netdev_priv(netdev);
1803 	u32 num_req = ch->combined_count;
1804 	int i;
1805 
1806 	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1807 	    adapter->num_tc) {
1808 		dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
1809 		return -EINVAL;
1810 	}
1811 
1812 	/* All of these should have already been checked by ethtool before this
1813 	 * even gets to us, but just to be sure.
1814 	 */
1815 	if (num_req == 0 || num_req > adapter->vsi_res->num_queue_pairs)
1816 		return -EINVAL;
1817 
1818 	if (num_req == adapter->num_active_queues)
1819 		return 0;
1820 
1821 	if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
1822 		return -EINVAL;
1823 
1824 	adapter->num_req_queues = num_req;
1825 	adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1826 	iavf_schedule_reset(adapter);
1827 
1828 	/* wait for the reset is done */
1829 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
1830 		msleep(IAVF_RESET_WAIT_MS);
1831 		if (adapter->flags & IAVF_FLAG_RESET_PENDING)
1832 			continue;
1833 		break;
1834 	}
1835 	if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
1836 		adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1837 		adapter->num_active_queues = num_req;
1838 		return -EOPNOTSUPP;
1839 	}
1840 
1841 	return 0;
1842 }
1843 
1844 /**
1845  * iavf_get_rxfh_key_size - get the RSS hash key size
1846  * @netdev: network interface device structure
1847  *
1848  * Returns the table size.
1849  **/
1850 static u32 iavf_get_rxfh_key_size(struct net_device *netdev)
1851 {
1852 	struct iavf_adapter *adapter = netdev_priv(netdev);
1853 
1854 	return adapter->rss_key_size;
1855 }
1856 
1857 /**
1858  * iavf_get_rxfh_indir_size - get the rx flow hash indirection table size
1859  * @netdev: network interface device structure
1860  *
1861  * Returns the table size.
1862  **/
1863 static u32 iavf_get_rxfh_indir_size(struct net_device *netdev)
1864 {
1865 	struct iavf_adapter *adapter = netdev_priv(netdev);
1866 
1867 	return adapter->rss_lut_size;
1868 }
1869 
1870 /**
1871  * iavf_get_rxfh - get the rx flow hash indirection table
1872  * @netdev: network interface device structure
1873  * @indir: indirection table
1874  * @key: hash key
1875  * @hfunc: hash function in use
1876  *
1877  * Reads the indirection table directly from the hardware. Always returns 0.
1878  **/
1879 static int iavf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1880 			 u8 *hfunc)
1881 {
1882 	struct iavf_adapter *adapter = netdev_priv(netdev);
1883 	u16 i;
1884 
1885 	if (hfunc)
1886 		*hfunc = ETH_RSS_HASH_TOP;
1887 	if (key)
1888 		memcpy(key, adapter->rss_key, adapter->rss_key_size);
1889 
1890 	if (indir)
1891 		/* Each 32 bits pointed by 'indir' is stored with a lut entry */
1892 		for (i = 0; i < adapter->rss_lut_size; i++)
1893 			indir[i] = (u32)adapter->rss_lut[i];
1894 
1895 	return 0;
1896 }
1897 
1898 /**
1899  * iavf_set_rxfh - set the rx flow hash indirection table
1900  * @netdev: network interface device structure
1901  * @indir: indirection table
1902  * @key: hash key
1903  * @hfunc: hash function to use
1904  *
1905  * Returns -EINVAL if the table specifies an inavlid queue id, otherwise
1906  * returns 0 after programming the table.
1907  **/
1908 static int iavf_set_rxfh(struct net_device *netdev, const u32 *indir,
1909 			 const u8 *key, const u8 hfunc)
1910 {
1911 	struct iavf_adapter *adapter = netdev_priv(netdev);
1912 	u16 i;
1913 
1914 	/* We do not allow change in unsupported parameters */
1915 	if (key ||
1916 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1917 		return -EOPNOTSUPP;
1918 	if (!indir)
1919 		return 0;
1920 
1921 	if (key)
1922 		memcpy(adapter->rss_key, key, adapter->rss_key_size);
1923 
1924 	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
1925 	for (i = 0; i < adapter->rss_lut_size; i++)
1926 		adapter->rss_lut[i] = (u8)(indir[i]);
1927 
1928 	return iavf_config_rss(adapter);
1929 }
1930 
1931 static const struct ethtool_ops iavf_ethtool_ops = {
1932 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1933 				     ETHTOOL_COALESCE_MAX_FRAMES |
1934 				     ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
1935 				     ETHTOOL_COALESCE_USE_ADAPTIVE,
1936 	.get_drvinfo		= iavf_get_drvinfo,
1937 	.get_link		= ethtool_op_get_link,
1938 	.get_ringparam		= iavf_get_ringparam,
1939 	.set_ringparam		= iavf_set_ringparam,
1940 	.get_strings		= iavf_get_strings,
1941 	.get_ethtool_stats	= iavf_get_ethtool_stats,
1942 	.get_sset_count		= iavf_get_sset_count,
1943 	.get_priv_flags		= iavf_get_priv_flags,
1944 	.set_priv_flags		= iavf_set_priv_flags,
1945 	.get_msglevel		= iavf_get_msglevel,
1946 	.set_msglevel		= iavf_set_msglevel,
1947 	.get_coalesce		= iavf_get_coalesce,
1948 	.set_coalesce		= iavf_set_coalesce,
1949 	.get_per_queue_coalesce = iavf_get_per_queue_coalesce,
1950 	.set_per_queue_coalesce = iavf_set_per_queue_coalesce,
1951 	.set_rxnfc		= iavf_set_rxnfc,
1952 	.get_rxnfc		= iavf_get_rxnfc,
1953 	.get_rxfh_indir_size	= iavf_get_rxfh_indir_size,
1954 	.get_rxfh		= iavf_get_rxfh,
1955 	.set_rxfh		= iavf_set_rxfh,
1956 	.get_channels		= iavf_get_channels,
1957 	.set_channels		= iavf_set_channels,
1958 	.get_rxfh_key_size	= iavf_get_rxfh_key_size,
1959 	.get_link_ksettings	= iavf_get_link_ksettings,
1960 };
1961 
1962 /**
1963  * iavf_set_ethtool_ops - Initialize ethtool ops struct
1964  * @netdev: network interface device structure
1965  *
1966  * Sets ethtool ops struct in our netdev so that ethtool can call
1967  * our functions.
1968  **/
1969 void iavf_set_ethtool_ops(struct net_device *netdev)
1970 {
1971 	netdev->ethtool_ops = &iavf_ethtool_ops;
1972 }
1973