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