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