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