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