• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
iavf_add_one_ethtool_stat(u64 * data,void * pointer,const struct iavf_stats * stat)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
__iavf_add_ethtool_stats(u64 ** data,void * pointer,const struct iavf_stats stats[],const unsigned int size)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
iavf_add_queue_stats(u64 ** data,struct iavf_ring * ring)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  **/
__iavf_add_stat_strings(u8 ** p,const struct iavf_stats stats[],const unsigned int size,...)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  **/
iavf_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * cmd)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  **/
iavf_get_sset_count(struct net_device * netdev,int sset)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  **/
iavf_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)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  **/
iavf_get_priv_flag_strings(struct net_device * netdev,u8 * data)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  **/
iavf_get_stat_strings(struct net_device * netdev,u8 * data)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  **/
iavf_get_strings(struct net_device * netdev,u32 sset,u8 * data)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  **/
iavf_get_priv_flags(struct net_device * netdev)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  **/
iavf_set_priv_flags(struct net_device * netdev,u32 flags)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  **/
iavf_get_msglevel(struct net_device * netdev)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  **/
iavf_set_msglevel(struct net_device * netdev,u32 data)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  **/
iavf_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)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  **/
iavf_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)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  **/
iavf_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)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  **/
__iavf_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,int queue)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  **/
iavf_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)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  **/
iavf_get_per_queue_coalesce(struct net_device * netdev,u32 queue,struct ethtool_coalesce * ec)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  **/
iavf_set_itr_per_queue(struct iavf_adapter * adapter,struct ethtool_coalesce * ec,int queue)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  **/
__iavf_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,int queue)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  **/
iavf_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)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  */
iavf_set_per_queue_coalesce(struct net_device * netdev,u32 queue,struct ethtool_coalesce * ec)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  */
iavf_fltr_to_ethtool_flow(enum iavf_fdir_flow_type flow)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  */
iavf_ethtool_flow_to_fltr(int eth)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  */
iavf_is_mask_valid(u64 mask,u64 field)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
iavf_parse_rx_flow_user_data(struct ethtool_rx_flow_spec * fsp,struct iavf_fdir_fltr * fltr)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
iavf_fill_rx_flow_ext_data(struct ethtool_rx_flow_spec * fsp,struct iavf_fdir_fltr * fltr)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
iavf_get_ethtool_fdir_entry(struct iavf_adapter * adapter,struct ethtool_rxnfc * cmd)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
iavf_get_fdir_fltr_ids(struct iavf_adapter * adapter,struct ethtool_rxnfc * cmd,u32 * rule_locs)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
iavf_add_fdir_fltr_info(struct iavf_adapter * adapter,struct ethtool_rx_flow_spec * fsp,struct iavf_fdir_fltr * fltr)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 		fltr->ip_ver = 4;
1279 		break;
1280 	case AH_V4_FLOW:
1281 	case ESP_V4_FLOW:
1282 		fltr->ip_data.v4_addrs.src_ip = fsp->h_u.ah_ip4_spec.ip4src;
1283 		fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.ah_ip4_spec.ip4dst;
1284 		fltr->ip_data.spi = fsp->h_u.ah_ip4_spec.spi;
1285 		fltr->ip_data.tos = fsp->h_u.ah_ip4_spec.tos;
1286 		fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.ah_ip4_spec.ip4src;
1287 		fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.ah_ip4_spec.ip4dst;
1288 		fltr->ip_mask.spi = fsp->m_u.ah_ip4_spec.spi;
1289 		fltr->ip_mask.tos = fsp->m_u.ah_ip4_spec.tos;
1290 		fltr->ip_ver = 4;
1291 		break;
1292 	case IPV4_USER_FLOW:
1293 		fltr->ip_data.v4_addrs.src_ip = fsp->h_u.usr_ip4_spec.ip4src;
1294 		fltr->ip_data.v4_addrs.dst_ip = fsp->h_u.usr_ip4_spec.ip4dst;
1295 		fltr->ip_data.l4_header = fsp->h_u.usr_ip4_spec.l4_4_bytes;
1296 		fltr->ip_data.tos = fsp->h_u.usr_ip4_spec.tos;
1297 		fltr->ip_data.proto = fsp->h_u.usr_ip4_spec.proto;
1298 		fltr->ip_mask.v4_addrs.src_ip = fsp->m_u.usr_ip4_spec.ip4src;
1299 		fltr->ip_mask.v4_addrs.dst_ip = fsp->m_u.usr_ip4_spec.ip4dst;
1300 		fltr->ip_mask.l4_header = fsp->m_u.usr_ip4_spec.l4_4_bytes;
1301 		fltr->ip_mask.tos = fsp->m_u.usr_ip4_spec.tos;
1302 		fltr->ip_mask.proto = fsp->m_u.usr_ip4_spec.proto;
1303 		fltr->ip_ver = 4;
1304 		break;
1305 	case TCP_V6_FLOW:
1306 	case UDP_V6_FLOW:
1307 	case SCTP_V6_FLOW:
1308 		memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1309 		       sizeof(struct in6_addr));
1310 		memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1311 		       sizeof(struct in6_addr));
1312 		fltr->ip_data.src_port = fsp->h_u.tcp_ip6_spec.psrc;
1313 		fltr->ip_data.dst_port = fsp->h_u.tcp_ip6_spec.pdst;
1314 		fltr->ip_data.tclass = fsp->h_u.tcp_ip6_spec.tclass;
1315 		memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1316 		       sizeof(struct in6_addr));
1317 		memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1318 		       sizeof(struct in6_addr));
1319 		fltr->ip_mask.src_port = fsp->m_u.tcp_ip6_spec.psrc;
1320 		fltr->ip_mask.dst_port = fsp->m_u.tcp_ip6_spec.pdst;
1321 		fltr->ip_mask.tclass = fsp->m_u.tcp_ip6_spec.tclass;
1322 		fltr->ip_ver = 6;
1323 		break;
1324 	case AH_V6_FLOW:
1325 	case ESP_V6_FLOW:
1326 		memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.ah_ip6_spec.ip6src,
1327 		       sizeof(struct in6_addr));
1328 		memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.ah_ip6_spec.ip6dst,
1329 		       sizeof(struct in6_addr));
1330 		fltr->ip_data.spi = fsp->h_u.ah_ip6_spec.spi;
1331 		fltr->ip_data.tclass = fsp->h_u.ah_ip6_spec.tclass;
1332 		memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.ah_ip6_spec.ip6src,
1333 		       sizeof(struct in6_addr));
1334 		memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.ah_ip6_spec.ip6dst,
1335 		       sizeof(struct in6_addr));
1336 		fltr->ip_mask.spi = fsp->m_u.ah_ip6_spec.spi;
1337 		fltr->ip_mask.tclass = fsp->m_u.ah_ip6_spec.tclass;
1338 		fltr->ip_ver = 6;
1339 		break;
1340 	case IPV6_USER_FLOW:
1341 		memcpy(&fltr->ip_data.v6_addrs.src_ip, fsp->h_u.usr_ip6_spec.ip6src,
1342 		       sizeof(struct in6_addr));
1343 		memcpy(&fltr->ip_data.v6_addrs.dst_ip, fsp->h_u.usr_ip6_spec.ip6dst,
1344 		       sizeof(struct in6_addr));
1345 		fltr->ip_data.l4_header = fsp->h_u.usr_ip6_spec.l4_4_bytes;
1346 		fltr->ip_data.tclass = fsp->h_u.usr_ip6_spec.tclass;
1347 		fltr->ip_data.proto = fsp->h_u.usr_ip6_spec.l4_proto;
1348 		memcpy(&fltr->ip_mask.v6_addrs.src_ip, fsp->m_u.usr_ip6_spec.ip6src,
1349 		       sizeof(struct in6_addr));
1350 		memcpy(&fltr->ip_mask.v6_addrs.dst_ip, fsp->m_u.usr_ip6_spec.ip6dst,
1351 		       sizeof(struct in6_addr));
1352 		fltr->ip_mask.l4_header = fsp->m_u.usr_ip6_spec.l4_4_bytes;
1353 		fltr->ip_mask.tclass = fsp->m_u.usr_ip6_spec.tclass;
1354 		fltr->ip_mask.proto = fsp->m_u.usr_ip6_spec.l4_proto;
1355 		fltr->ip_ver = 6;
1356 		break;
1357 	case ETHER_FLOW:
1358 		fltr->eth_data.etype = fsp->h_u.ether_spec.h_proto;
1359 		fltr->eth_mask.etype = fsp->m_u.ether_spec.h_proto;
1360 		break;
1361 	default:
1362 		/* not doing un-parsed flow types */
1363 		return -EINVAL;
1364 	}
1365 
1366 	err = iavf_validate_fdir_fltr_masks(adapter, fltr);
1367 	if (err)
1368 		return err;
1369 
1370 	if (iavf_fdir_is_dup_fltr(adapter, fltr))
1371 		return -EEXIST;
1372 
1373 	err = iavf_parse_rx_flow_user_data(fsp, fltr);
1374 	if (err)
1375 		return err;
1376 
1377 	return iavf_fill_fdir_add_msg(adapter, fltr);
1378 }
1379 
1380 /**
1381  * iavf_add_fdir_ethtool - add Flow Director filter
1382  * @adapter: pointer to the VF adapter structure
1383  * @cmd: command to add Flow Director filter
1384  *
1385  * Returns 0 on success and negative values for failure
1386  */
iavf_add_fdir_ethtool(struct iavf_adapter * adapter,struct ethtool_rxnfc * cmd)1387 static int iavf_add_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1388 {
1389 	struct ethtool_rx_flow_spec *fsp = &cmd->fs;
1390 	struct iavf_fdir_fltr *fltr;
1391 	int count = 50;
1392 	int err;
1393 
1394 	if (!FDIR_FLTR_SUPPORT(adapter))
1395 		return -EOPNOTSUPP;
1396 
1397 	if (fsp->flow_type & FLOW_MAC_EXT)
1398 		return -EINVAL;
1399 
1400 	spin_lock_bh(&adapter->fdir_fltr_lock);
1401 	if (adapter->fdir_active_fltr >= IAVF_MAX_FDIR_FILTERS) {
1402 		spin_unlock_bh(&adapter->fdir_fltr_lock);
1403 		dev_err(&adapter->pdev->dev,
1404 			"Unable to add Flow Director filter because VF reached the limit of max allowed filters (%u)\n",
1405 			IAVF_MAX_FDIR_FILTERS);
1406 		return -ENOSPC;
1407 	}
1408 
1409 	if (iavf_find_fdir_fltr_by_loc(adapter, fsp->location)) {
1410 		dev_err(&adapter->pdev->dev, "Failed to add Flow Director filter, it already exists\n");
1411 		spin_unlock_bh(&adapter->fdir_fltr_lock);
1412 		return -EEXIST;
1413 	}
1414 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1415 
1416 	fltr = kzalloc(sizeof(*fltr), GFP_KERNEL);
1417 	if (!fltr)
1418 		return -ENOMEM;
1419 
1420 	while (!mutex_trylock(&adapter->crit_lock)) {
1421 		if (--count == 0) {
1422 			kfree(fltr);
1423 			return -EINVAL;
1424 		}
1425 		udelay(1);
1426 	}
1427 
1428 	err = iavf_add_fdir_fltr_info(adapter, fsp, fltr);
1429 	if (err)
1430 		goto ret;
1431 
1432 	spin_lock_bh(&adapter->fdir_fltr_lock);
1433 	iavf_fdir_list_add_fltr(adapter, fltr);
1434 	adapter->fdir_active_fltr++;
1435 	fltr->state = IAVF_FDIR_FLTR_ADD_REQUEST;
1436 	adapter->aq_required |= IAVF_FLAG_AQ_ADD_FDIR_FILTER;
1437 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1438 
1439 	mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1440 
1441 ret:
1442 	if (err && fltr)
1443 		kfree(fltr);
1444 
1445 	mutex_unlock(&adapter->crit_lock);
1446 	return err;
1447 }
1448 
1449 /**
1450  * iavf_del_fdir_ethtool - delete Flow Director filter
1451  * @adapter: pointer to the VF adapter structure
1452  * @cmd: command to delete Flow Director filter
1453  *
1454  * Returns 0 on success and negative values for failure
1455  */
iavf_del_fdir_ethtool(struct iavf_adapter * adapter,struct ethtool_rxnfc * cmd)1456 static int iavf_del_fdir_ethtool(struct iavf_adapter *adapter, struct ethtool_rxnfc *cmd)
1457 {
1458 	struct ethtool_rx_flow_spec *fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1459 	struct iavf_fdir_fltr *fltr = NULL;
1460 	int err = 0;
1461 
1462 	if (!FDIR_FLTR_SUPPORT(adapter))
1463 		return -EOPNOTSUPP;
1464 
1465 	spin_lock_bh(&adapter->fdir_fltr_lock);
1466 	fltr = iavf_find_fdir_fltr_by_loc(adapter, fsp->location);
1467 	if (fltr) {
1468 		if (fltr->state == IAVF_FDIR_FLTR_ACTIVE) {
1469 			fltr->state = IAVF_FDIR_FLTR_DEL_REQUEST;
1470 			adapter->aq_required |= IAVF_FLAG_AQ_DEL_FDIR_FILTER;
1471 		} else {
1472 			err = -EBUSY;
1473 		}
1474 	} else if (adapter->fdir_active_fltr) {
1475 		err = -EINVAL;
1476 	}
1477 	spin_unlock_bh(&adapter->fdir_fltr_lock);
1478 
1479 	if (fltr && fltr->state == IAVF_FDIR_FLTR_DEL_REQUEST)
1480 		mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1481 
1482 	return err;
1483 }
1484 
1485 /**
1486  * iavf_adv_rss_parse_hdrs - parses headers from RSS hash input
1487  * @cmd: ethtool rxnfc command
1488  *
1489  * This function parses the rxnfc command and returns intended
1490  * header types for RSS configuration
1491  */
iavf_adv_rss_parse_hdrs(struct ethtool_rxnfc * cmd)1492 static u32 iavf_adv_rss_parse_hdrs(struct ethtool_rxnfc *cmd)
1493 {
1494 	u32 hdrs = IAVF_ADV_RSS_FLOW_SEG_HDR_NONE;
1495 
1496 	switch (cmd->flow_type) {
1497 	case TCP_V4_FLOW:
1498 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1499 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1500 		break;
1501 	case UDP_V4_FLOW:
1502 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1503 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1504 		break;
1505 	case SCTP_V4_FLOW:
1506 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1507 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV4;
1508 		break;
1509 	case TCP_V6_FLOW:
1510 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_TCP |
1511 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1512 		break;
1513 	case UDP_V6_FLOW:
1514 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_UDP |
1515 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1516 		break;
1517 	case SCTP_V6_FLOW:
1518 		hdrs |= IAVF_ADV_RSS_FLOW_SEG_HDR_SCTP |
1519 			IAVF_ADV_RSS_FLOW_SEG_HDR_IPV6;
1520 		break;
1521 	default:
1522 		break;
1523 	}
1524 
1525 	return hdrs;
1526 }
1527 
1528 /**
1529  * iavf_adv_rss_parse_hash_flds - parses hash fields from RSS hash input
1530  * @cmd: ethtool rxnfc command
1531  *
1532  * This function parses the rxnfc command and returns intended hash fields for
1533  * RSS configuration
1534  */
iavf_adv_rss_parse_hash_flds(struct ethtool_rxnfc * cmd)1535 static u64 iavf_adv_rss_parse_hash_flds(struct ethtool_rxnfc *cmd)
1536 {
1537 	u64 hfld = IAVF_ADV_RSS_HASH_INVALID;
1538 
1539 	if (cmd->data & RXH_IP_SRC || cmd->data & RXH_IP_DST) {
1540 		switch (cmd->flow_type) {
1541 		case TCP_V4_FLOW:
1542 		case UDP_V4_FLOW:
1543 		case SCTP_V4_FLOW:
1544 			if (cmd->data & RXH_IP_SRC)
1545 				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_SA;
1546 			if (cmd->data & RXH_IP_DST)
1547 				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV4_DA;
1548 			break;
1549 		case TCP_V6_FLOW:
1550 		case UDP_V6_FLOW:
1551 		case SCTP_V6_FLOW:
1552 			if (cmd->data & RXH_IP_SRC)
1553 				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_SA;
1554 			if (cmd->data & RXH_IP_DST)
1555 				hfld |= IAVF_ADV_RSS_HASH_FLD_IPV6_DA;
1556 			break;
1557 		default:
1558 			break;
1559 		}
1560 	}
1561 
1562 	if (cmd->data & RXH_L4_B_0_1 || cmd->data & RXH_L4_B_2_3) {
1563 		switch (cmd->flow_type) {
1564 		case TCP_V4_FLOW:
1565 		case TCP_V6_FLOW:
1566 			if (cmd->data & RXH_L4_B_0_1)
1567 				hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT;
1568 			if (cmd->data & RXH_L4_B_2_3)
1569 				hfld |= IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT;
1570 			break;
1571 		case UDP_V4_FLOW:
1572 		case UDP_V6_FLOW:
1573 			if (cmd->data & RXH_L4_B_0_1)
1574 				hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT;
1575 			if (cmd->data & RXH_L4_B_2_3)
1576 				hfld |= IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT;
1577 			break;
1578 		case SCTP_V4_FLOW:
1579 		case SCTP_V6_FLOW:
1580 			if (cmd->data & RXH_L4_B_0_1)
1581 				hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT;
1582 			if (cmd->data & RXH_L4_B_2_3)
1583 				hfld |= IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT;
1584 			break;
1585 		default:
1586 			break;
1587 		}
1588 	}
1589 
1590 	return hfld;
1591 }
1592 
1593 /**
1594  * iavf_set_adv_rss_hash_opt - Enable/Disable flow types for RSS hash
1595  * @adapter: pointer to the VF adapter structure
1596  * @cmd: ethtool rxnfc command
1597  *
1598  * Returns Success if the flow input set is supported.
1599  */
1600 static int
iavf_set_adv_rss_hash_opt(struct iavf_adapter * adapter,struct ethtool_rxnfc * cmd)1601 iavf_set_adv_rss_hash_opt(struct iavf_adapter *adapter,
1602 			  struct ethtool_rxnfc *cmd)
1603 {
1604 	struct iavf_adv_rss *rss_old, *rss_new;
1605 	bool rss_new_add = false;
1606 	int count = 50, err = 0;
1607 	u64 hash_flds;
1608 	u32 hdrs;
1609 
1610 	if (!ADV_RSS_SUPPORT(adapter))
1611 		return -EOPNOTSUPP;
1612 
1613 	hdrs = iavf_adv_rss_parse_hdrs(cmd);
1614 	if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1615 		return -EINVAL;
1616 
1617 	hash_flds = iavf_adv_rss_parse_hash_flds(cmd);
1618 	if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1619 		return -EINVAL;
1620 
1621 	rss_new = kzalloc(sizeof(*rss_new), GFP_KERNEL);
1622 	if (!rss_new)
1623 		return -ENOMEM;
1624 
1625 	if (iavf_fill_adv_rss_cfg_msg(&rss_new->cfg_msg, hdrs, hash_flds)) {
1626 		kfree(rss_new);
1627 		return -EINVAL;
1628 	}
1629 
1630 	while (!mutex_trylock(&adapter->crit_lock)) {
1631 		if (--count == 0) {
1632 			kfree(rss_new);
1633 			return -EINVAL;
1634 		}
1635 
1636 		udelay(1);
1637 	}
1638 
1639 	spin_lock_bh(&adapter->adv_rss_lock);
1640 	rss_old = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1641 	if (rss_old) {
1642 		if (rss_old->state != IAVF_ADV_RSS_ACTIVE) {
1643 			err = -EBUSY;
1644 		} else if (rss_old->hash_flds != hash_flds) {
1645 			rss_old->state = IAVF_ADV_RSS_ADD_REQUEST;
1646 			rss_old->hash_flds = hash_flds;
1647 			memcpy(&rss_old->cfg_msg, &rss_new->cfg_msg,
1648 			       sizeof(rss_new->cfg_msg));
1649 			adapter->aq_required |= IAVF_FLAG_AQ_ADD_ADV_RSS_CFG;
1650 		} else {
1651 			err = -EEXIST;
1652 		}
1653 	} else {
1654 		rss_new_add = true;
1655 		rss_new->state = IAVF_ADV_RSS_ADD_REQUEST;
1656 		rss_new->packet_hdrs = hdrs;
1657 		rss_new->hash_flds = hash_flds;
1658 		list_add_tail(&rss_new->list, &adapter->adv_rss_list_head);
1659 		adapter->aq_required |= IAVF_FLAG_AQ_ADD_ADV_RSS_CFG;
1660 	}
1661 	spin_unlock_bh(&adapter->adv_rss_lock);
1662 
1663 	if (!err)
1664 		mod_delayed_work(iavf_wq, &adapter->watchdog_task, 0);
1665 
1666 	mutex_unlock(&adapter->crit_lock);
1667 
1668 	if (!rss_new_add)
1669 		kfree(rss_new);
1670 
1671 	return err;
1672 }
1673 
1674 /**
1675  * iavf_get_adv_rss_hash_opt - Retrieve hash fields for a given flow-type
1676  * @adapter: pointer to the VF adapter structure
1677  * @cmd: ethtool rxnfc command
1678  *
1679  * Returns Success if the flow input set is supported.
1680  */
1681 static int
iavf_get_adv_rss_hash_opt(struct iavf_adapter * adapter,struct ethtool_rxnfc * cmd)1682 iavf_get_adv_rss_hash_opt(struct iavf_adapter *adapter,
1683 			  struct ethtool_rxnfc *cmd)
1684 {
1685 	struct iavf_adv_rss *rss;
1686 	u64 hash_flds;
1687 	u32 hdrs;
1688 
1689 	if (!ADV_RSS_SUPPORT(adapter))
1690 		return -EOPNOTSUPP;
1691 
1692 	cmd->data = 0;
1693 
1694 	hdrs = iavf_adv_rss_parse_hdrs(cmd);
1695 	if (hdrs == IAVF_ADV_RSS_FLOW_SEG_HDR_NONE)
1696 		return -EINVAL;
1697 
1698 	spin_lock_bh(&adapter->adv_rss_lock);
1699 	rss = iavf_find_adv_rss_cfg_by_hdrs(adapter, hdrs);
1700 	if (rss)
1701 		hash_flds = rss->hash_flds;
1702 	else
1703 		hash_flds = IAVF_ADV_RSS_HASH_INVALID;
1704 	spin_unlock_bh(&adapter->adv_rss_lock);
1705 
1706 	if (hash_flds == IAVF_ADV_RSS_HASH_INVALID)
1707 		return -EINVAL;
1708 
1709 	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_SA |
1710 			 IAVF_ADV_RSS_HASH_FLD_IPV6_SA))
1711 		cmd->data |= (u64)RXH_IP_SRC;
1712 
1713 	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_IPV4_DA |
1714 			 IAVF_ADV_RSS_HASH_FLD_IPV6_DA))
1715 		cmd->data |= (u64)RXH_IP_DST;
1716 
1717 	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_SRC_PORT |
1718 			 IAVF_ADV_RSS_HASH_FLD_UDP_SRC_PORT |
1719 			 IAVF_ADV_RSS_HASH_FLD_SCTP_SRC_PORT))
1720 		cmd->data |= (u64)RXH_L4_B_0_1;
1721 
1722 	if (hash_flds & (IAVF_ADV_RSS_HASH_FLD_TCP_DST_PORT |
1723 			 IAVF_ADV_RSS_HASH_FLD_UDP_DST_PORT |
1724 			 IAVF_ADV_RSS_HASH_FLD_SCTP_DST_PORT))
1725 		cmd->data |= (u64)RXH_L4_B_2_3;
1726 
1727 	return 0;
1728 }
1729 
1730 /**
1731  * iavf_set_rxnfc - command to set Rx flow rules.
1732  * @netdev: network interface device structure
1733  * @cmd: ethtool rxnfc command
1734  *
1735  * Returns 0 for success and negative values for errors
1736  */
iavf_set_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd)1737 static int iavf_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1738 {
1739 	struct iavf_adapter *adapter = netdev_priv(netdev);
1740 	int ret = -EOPNOTSUPP;
1741 
1742 	switch (cmd->cmd) {
1743 	case ETHTOOL_SRXCLSRLINS:
1744 		ret = iavf_add_fdir_ethtool(adapter, cmd);
1745 		break;
1746 	case ETHTOOL_SRXCLSRLDEL:
1747 		ret = iavf_del_fdir_ethtool(adapter, cmd);
1748 		break;
1749 	case ETHTOOL_SRXFH:
1750 		ret = iavf_set_adv_rss_hash_opt(adapter, cmd);
1751 		break;
1752 	default:
1753 		break;
1754 	}
1755 
1756 	return ret;
1757 }
1758 
1759 /**
1760  * iavf_get_rxnfc - command to get RX flow classification rules
1761  * @netdev: network interface device structure
1762  * @cmd: ethtool rxnfc command
1763  * @rule_locs: pointer to store rule locations
1764  *
1765  * Returns Success if the command is supported.
1766  **/
iavf_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd,u32 * rule_locs)1767 static int iavf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1768 			  u32 *rule_locs)
1769 {
1770 	struct iavf_adapter *adapter = netdev_priv(netdev);
1771 	int ret = -EOPNOTSUPP;
1772 
1773 	switch (cmd->cmd) {
1774 	case ETHTOOL_GRXRINGS:
1775 		cmd->data = adapter->num_active_queues;
1776 		ret = 0;
1777 		break;
1778 	case ETHTOOL_GRXCLSRLCNT:
1779 		if (!FDIR_FLTR_SUPPORT(adapter))
1780 			break;
1781 		spin_lock_bh(&adapter->fdir_fltr_lock);
1782 		cmd->rule_cnt = adapter->fdir_active_fltr;
1783 		spin_unlock_bh(&adapter->fdir_fltr_lock);
1784 		cmd->data = IAVF_MAX_FDIR_FILTERS;
1785 		ret = 0;
1786 		break;
1787 	case ETHTOOL_GRXCLSRULE:
1788 		ret = iavf_get_ethtool_fdir_entry(adapter, cmd);
1789 		break;
1790 	case ETHTOOL_GRXCLSRLALL:
1791 		ret = iavf_get_fdir_fltr_ids(adapter, cmd, (u32 *)rule_locs);
1792 		break;
1793 	case ETHTOOL_GRXFH:
1794 		ret = iavf_get_adv_rss_hash_opt(adapter, cmd);
1795 		break;
1796 	default:
1797 		break;
1798 	}
1799 
1800 	return ret;
1801 }
1802 /**
1803  * iavf_get_channels: get the number of channels supported by the device
1804  * @netdev: network interface device structure
1805  * @ch: channel information structure
1806  *
1807  * For the purposes of our device, we only use combined channels, i.e. a tx/rx
1808  * queue pair. Report one extra channel to match our "other" MSI-X vector.
1809  **/
iavf_get_channels(struct net_device * netdev,struct ethtool_channels * ch)1810 static void iavf_get_channels(struct net_device *netdev,
1811 			      struct ethtool_channels *ch)
1812 {
1813 	struct iavf_adapter *adapter = netdev_priv(netdev);
1814 
1815 	/* Report maximum channels */
1816 	ch->max_combined = adapter->vsi_res->num_queue_pairs;
1817 
1818 	ch->max_other = NONQ_VECS;
1819 	ch->other_count = NONQ_VECS;
1820 
1821 	ch->combined_count = adapter->num_active_queues;
1822 }
1823 
1824 /**
1825  * iavf_set_channels: set the new channel count
1826  * @netdev: network interface device structure
1827  * @ch: channel information structure
1828  *
1829  * Negotiate a new number of channels with the PF then do a reset.  During
1830  * reset we'll realloc queues and fix the RSS table.  Returns 0 on success,
1831  * negative on failure.
1832  **/
iavf_set_channels(struct net_device * netdev,struct ethtool_channels * ch)1833 static int iavf_set_channels(struct net_device *netdev,
1834 			     struct ethtool_channels *ch)
1835 {
1836 	struct iavf_adapter *adapter = netdev_priv(netdev);
1837 	u32 num_req = ch->combined_count;
1838 	int i;
1839 
1840 	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
1841 	    adapter->num_tc) {
1842 		dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
1843 		return -EINVAL;
1844 	}
1845 
1846 	/* All of these should have already been checked by ethtool before this
1847 	 * even gets to us, but just to be sure.
1848 	 */
1849 	if (num_req == 0 || num_req > adapter->vsi_res->num_queue_pairs)
1850 		return -EINVAL;
1851 
1852 	if (num_req == adapter->num_active_queues)
1853 		return 0;
1854 
1855 	if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
1856 		return -EINVAL;
1857 
1858 	adapter->num_req_queues = num_req;
1859 	adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1860 	iavf_schedule_reset(adapter);
1861 
1862 	/* wait for the reset is done */
1863 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
1864 		msleep(IAVF_RESET_WAIT_MS);
1865 		if (adapter->flags & IAVF_FLAG_RESET_PENDING)
1866 			continue;
1867 		break;
1868 	}
1869 	if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
1870 		adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1871 		adapter->num_req_queues = 0;
1872 		return -EOPNOTSUPP;
1873 	}
1874 
1875 	return 0;
1876 }
1877 
1878 /**
1879  * iavf_get_rxfh_key_size - get the RSS hash key size
1880  * @netdev: network interface device structure
1881  *
1882  * Returns the table size.
1883  **/
iavf_get_rxfh_key_size(struct net_device * netdev)1884 static u32 iavf_get_rxfh_key_size(struct net_device *netdev)
1885 {
1886 	struct iavf_adapter *adapter = netdev_priv(netdev);
1887 
1888 	return adapter->rss_key_size;
1889 }
1890 
1891 /**
1892  * iavf_get_rxfh_indir_size - get the rx flow hash indirection table size
1893  * @netdev: network interface device structure
1894  *
1895  * Returns the table size.
1896  **/
iavf_get_rxfh_indir_size(struct net_device * netdev)1897 static u32 iavf_get_rxfh_indir_size(struct net_device *netdev)
1898 {
1899 	struct iavf_adapter *adapter = netdev_priv(netdev);
1900 
1901 	return adapter->rss_lut_size;
1902 }
1903 
1904 /**
1905  * iavf_get_rxfh - get the rx flow hash indirection table
1906  * @netdev: network interface device structure
1907  * @indir: indirection table
1908  * @key: hash key
1909  * @hfunc: hash function in use
1910  *
1911  * Reads the indirection table directly from the hardware. Always returns 0.
1912  **/
iavf_get_rxfh(struct net_device * netdev,u32 * indir,u8 * key,u8 * hfunc)1913 static int iavf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1914 			 u8 *hfunc)
1915 {
1916 	struct iavf_adapter *adapter = netdev_priv(netdev);
1917 	u16 i;
1918 
1919 	if (hfunc)
1920 		*hfunc = ETH_RSS_HASH_TOP;
1921 	if (key)
1922 		memcpy(key, adapter->rss_key, adapter->rss_key_size);
1923 
1924 	if (indir)
1925 		/* Each 32 bits pointed by 'indir' is stored with a lut entry */
1926 		for (i = 0; i < adapter->rss_lut_size; i++)
1927 			indir[i] = (u32)adapter->rss_lut[i];
1928 
1929 	return 0;
1930 }
1931 
1932 /**
1933  * iavf_set_rxfh - set the rx flow hash indirection table
1934  * @netdev: network interface device structure
1935  * @indir: indirection table
1936  * @key: hash key
1937  * @hfunc: hash function to use
1938  *
1939  * Returns -EINVAL if the table specifies an inavlid queue id, otherwise
1940  * returns 0 after programming the table.
1941  **/
iavf_set_rxfh(struct net_device * netdev,const u32 * indir,const u8 * key,const u8 hfunc)1942 static int iavf_set_rxfh(struct net_device *netdev, const u32 *indir,
1943 			 const u8 *key, const u8 hfunc)
1944 {
1945 	struct iavf_adapter *adapter = netdev_priv(netdev);
1946 	u16 i;
1947 
1948 	/* We do not allow change in unsupported parameters */
1949 	if (key ||
1950 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1951 		return -EOPNOTSUPP;
1952 	if (!indir)
1953 		return 0;
1954 
1955 	if (key)
1956 		memcpy(adapter->rss_key, key, adapter->rss_key_size);
1957 
1958 	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
1959 	for (i = 0; i < adapter->rss_lut_size; i++)
1960 		adapter->rss_lut[i] = (u8)(indir[i]);
1961 
1962 	return iavf_config_rss(adapter);
1963 }
1964 
1965 static const struct ethtool_ops iavf_ethtool_ops = {
1966 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1967 				     ETHTOOL_COALESCE_MAX_FRAMES |
1968 				     ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
1969 				     ETHTOOL_COALESCE_USE_ADAPTIVE,
1970 	.get_drvinfo		= iavf_get_drvinfo,
1971 	.get_link		= ethtool_op_get_link,
1972 	.get_ringparam		= iavf_get_ringparam,
1973 	.set_ringparam		= iavf_set_ringparam,
1974 	.get_strings		= iavf_get_strings,
1975 	.get_ethtool_stats	= iavf_get_ethtool_stats,
1976 	.get_sset_count		= iavf_get_sset_count,
1977 	.get_priv_flags		= iavf_get_priv_flags,
1978 	.set_priv_flags		= iavf_set_priv_flags,
1979 	.get_msglevel		= iavf_get_msglevel,
1980 	.set_msglevel		= iavf_set_msglevel,
1981 	.get_coalesce		= iavf_get_coalesce,
1982 	.set_coalesce		= iavf_set_coalesce,
1983 	.get_per_queue_coalesce = iavf_get_per_queue_coalesce,
1984 	.set_per_queue_coalesce = iavf_set_per_queue_coalesce,
1985 	.set_rxnfc		= iavf_set_rxnfc,
1986 	.get_rxnfc		= iavf_get_rxnfc,
1987 	.get_rxfh_indir_size	= iavf_get_rxfh_indir_size,
1988 	.get_rxfh		= iavf_get_rxfh,
1989 	.set_rxfh		= iavf_set_rxfh,
1990 	.get_channels		= iavf_get_channels,
1991 	.set_channels		= iavf_set_channels,
1992 	.get_rxfh_key_size	= iavf_get_rxfh_key_size,
1993 	.get_link_ksettings	= iavf_get_link_ksettings,
1994 };
1995 
1996 /**
1997  * iavf_set_ethtool_ops - Initialize ethtool ops struct
1998  * @netdev: network interface device structure
1999  *
2000  * Sets ethtool ops struct in our netdev so that ethtool can call
2001  * our functions.
2002  **/
iavf_set_ethtool_ops(struct net_device * netdev)2003 void iavf_set_ethtool_ops(struct net_device *netdev)
2004 {
2005 	netdev->ethtool_ops = &iavf_ethtool_ops;
2006 }
2007