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