• 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 	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  **/
iavf_get_priv_flag_strings(struct net_device * netdev,u8 * data)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  **/
iavf_get_stat_strings(struct net_device * netdev,u8 * data)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  **/
iavf_get_strings(struct net_device * netdev,u32 sset,u8 * data)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  **/
iavf_get_priv_flags(struct net_device * netdev)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  **/
iavf_set_priv_flags(struct net_device * netdev,u32 flags)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  **/
iavf_get_msglevel(struct net_device * netdev)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  **/
iavf_set_msglevel(struct net_device * netdev,u32 data)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  **/
iavf_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)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  **/
iavf_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)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  **/
iavf_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)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 	if (ring->tx_pending > IAVF_MAX_TXD ||
616 	    ring->tx_pending < IAVF_MIN_TXD ||
617 	    ring->rx_pending > IAVF_MAX_RXD ||
618 	    ring->rx_pending < IAVF_MIN_RXD) {
619 		netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
620 			   ring->tx_pending, ring->rx_pending, IAVF_MIN_TXD,
621 			   IAVF_MAX_RXD, IAVF_REQ_DESCRIPTOR_MULTIPLE);
622 		return -EINVAL;
623 	}
624 
625 	new_tx_count = ALIGN(ring->tx_pending, IAVF_REQ_DESCRIPTOR_MULTIPLE);
626 	if (new_tx_count != ring->tx_pending)
627 		netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n",
628 			    new_tx_count);
629 
630 	new_rx_count = ALIGN(ring->rx_pending, IAVF_REQ_DESCRIPTOR_MULTIPLE);
631 	if (new_rx_count != ring->rx_pending)
632 		netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n",
633 			    new_rx_count);
634 
635 	/* if nothing to do return success */
636 	if ((new_tx_count == adapter->tx_desc_count) &&
637 	    (new_rx_count == adapter->rx_desc_count)) {
638 		netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
639 		return 0;
640 	}
641 
642 	if (new_tx_count != adapter->tx_desc_count) {
643 		netdev_dbg(netdev, "Changing Tx descriptor count from %d to %d\n",
644 			   adapter->tx_desc_count, new_tx_count);
645 		adapter->tx_desc_count = new_tx_count;
646 	}
647 
648 	if (new_rx_count != adapter->rx_desc_count) {
649 		netdev_dbg(netdev, "Changing Rx descriptor count from %d to %d\n",
650 			   adapter->rx_desc_count, new_rx_count);
651 		adapter->rx_desc_count = new_rx_count;
652 	}
653 
654 	if (netif_running(netdev)) {
655 		adapter->flags |= IAVF_FLAG_RESET_NEEDED;
656 		queue_work(iavf_wq, &adapter->reset_task);
657 	}
658 
659 	return 0;
660 }
661 
662 /**
663  * __iavf_get_coalesce - get per-queue coalesce settings
664  * @netdev: the netdev to check
665  * @ec: ethtool coalesce data structure
666  * @queue: which queue to pick
667  *
668  * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
669  * are per queue. If queue is <0 then we default to queue 0 as the
670  * representative value.
671  **/
__iavf_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,int queue)672 static int __iavf_get_coalesce(struct net_device *netdev,
673 			       struct ethtool_coalesce *ec, int queue)
674 {
675 	struct iavf_adapter *adapter = netdev_priv(netdev);
676 	struct iavf_vsi *vsi = &adapter->vsi;
677 	struct iavf_ring *rx_ring, *tx_ring;
678 
679 	ec->tx_max_coalesced_frames = vsi->work_limit;
680 	ec->rx_max_coalesced_frames = vsi->work_limit;
681 
682 	/* Rx and Tx usecs per queue value. If user doesn't specify the
683 	 * queue, return queue 0's value to represent.
684 	 */
685 	if (queue < 0)
686 		queue = 0;
687 	else if (queue >= adapter->num_active_queues)
688 		return -EINVAL;
689 
690 	rx_ring = &adapter->rx_rings[queue];
691 	tx_ring = &adapter->tx_rings[queue];
692 
693 	if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
694 		ec->use_adaptive_rx_coalesce = 1;
695 
696 	if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
697 		ec->use_adaptive_tx_coalesce = 1;
698 
699 	ec->rx_coalesce_usecs = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
700 	ec->tx_coalesce_usecs = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
701 
702 	return 0;
703 }
704 
705 /**
706  * iavf_get_coalesce - Get interrupt coalescing settings
707  * @netdev: network interface device structure
708  * @ec: ethtool coalesce structure
709  *
710  * Returns current coalescing settings. This is referred to elsewhere in the
711  * driver as Interrupt Throttle Rate, as this is how the hardware describes
712  * this functionality. Note that if per-queue settings have been modified this
713  * only represents the settings of queue 0.
714  **/
iavf_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec)715 static int iavf_get_coalesce(struct net_device *netdev,
716 			     struct ethtool_coalesce *ec)
717 {
718 	return __iavf_get_coalesce(netdev, ec, -1);
719 }
720 
721 /**
722  * iavf_get_per_queue_coalesce - get coalesce values for specific queue
723  * @netdev: netdev to read
724  * @ec: coalesce settings from ethtool
725  * @queue: the queue to read
726  *
727  * Read specific queue's coalesce settings.
728  **/
iavf_get_per_queue_coalesce(struct net_device * netdev,u32 queue,struct ethtool_coalesce * ec)729 static int iavf_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
730 				       struct ethtool_coalesce *ec)
731 {
732 	return __iavf_get_coalesce(netdev, ec, queue);
733 }
734 
735 /**
736  * iavf_set_itr_per_queue - set ITR values for specific queue
737  * @adapter: the VF adapter struct to set values for
738  * @ec: coalesce settings from ethtool
739  * @queue: the queue to modify
740  *
741  * Change the ITR settings for a specific queue.
742  **/
iavf_set_itr_per_queue(struct iavf_adapter * adapter,struct ethtool_coalesce * ec,int queue)743 static int iavf_set_itr_per_queue(struct iavf_adapter *adapter,
744 				  struct ethtool_coalesce *ec, int queue)
745 {
746 	struct iavf_ring *rx_ring = &adapter->rx_rings[queue];
747 	struct iavf_ring *tx_ring = &adapter->tx_rings[queue];
748 	struct iavf_q_vector *q_vector;
749 	u16 itr_setting;
750 
751 	itr_setting = rx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
752 
753 	if (ec->rx_coalesce_usecs != itr_setting &&
754 	    ec->use_adaptive_rx_coalesce) {
755 		netif_info(adapter, drv, adapter->netdev,
756 			   "Rx interrupt throttling cannot be changed if adaptive-rx is enabled\n");
757 		return -EINVAL;
758 	}
759 
760 	itr_setting = tx_ring->itr_setting & ~IAVF_ITR_DYNAMIC;
761 
762 	if (ec->tx_coalesce_usecs != itr_setting &&
763 	    ec->use_adaptive_tx_coalesce) {
764 		netif_info(adapter, drv, adapter->netdev,
765 			   "Tx interrupt throttling cannot be changed if adaptive-tx is enabled\n");
766 		return -EINVAL;
767 	}
768 
769 	rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
770 	tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
771 
772 	rx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
773 	if (!ec->use_adaptive_rx_coalesce)
774 		rx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
775 
776 	tx_ring->itr_setting |= IAVF_ITR_DYNAMIC;
777 	if (!ec->use_adaptive_tx_coalesce)
778 		tx_ring->itr_setting ^= IAVF_ITR_DYNAMIC;
779 
780 	q_vector = rx_ring->q_vector;
781 	q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
782 
783 	q_vector = tx_ring->q_vector;
784 	q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
785 
786 	/* The interrupt handler itself will take care of programming
787 	 * the Tx and Rx ITR values based on the values we have entered
788 	 * into the q_vector, no need to write the values now.
789 	 */
790 	return 0;
791 }
792 
793 /**
794  * __iavf_set_coalesce - set coalesce settings for particular queue
795  * @netdev: the netdev to change
796  * @ec: ethtool coalesce settings
797  * @queue: the queue to change
798  *
799  * Sets the coalesce settings for a particular queue.
800  **/
__iavf_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,int queue)801 static int __iavf_set_coalesce(struct net_device *netdev,
802 			       struct ethtool_coalesce *ec, int queue)
803 {
804 	struct iavf_adapter *adapter = netdev_priv(netdev);
805 	struct iavf_vsi *vsi = &adapter->vsi;
806 	int i;
807 
808 	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
809 		vsi->work_limit = ec->tx_max_coalesced_frames_irq;
810 
811 	if (ec->rx_coalesce_usecs == 0) {
812 		if (ec->use_adaptive_rx_coalesce)
813 			netif_info(adapter, drv, netdev, "rx-usecs=0, need to disable adaptive-rx for a complete disable\n");
814 	} else if ((ec->rx_coalesce_usecs < IAVF_MIN_ITR) ||
815 		   (ec->rx_coalesce_usecs > IAVF_MAX_ITR)) {
816 		netif_info(adapter, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
817 		return -EINVAL;
818 	} else if (ec->tx_coalesce_usecs == 0) {
819 		if (ec->use_adaptive_tx_coalesce)
820 			netif_info(adapter, drv, netdev, "tx-usecs=0, need to disable adaptive-tx for a complete disable\n");
821 	} else if ((ec->tx_coalesce_usecs < IAVF_MIN_ITR) ||
822 		   (ec->tx_coalesce_usecs > IAVF_MAX_ITR)) {
823 		netif_info(adapter, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
824 		return -EINVAL;
825 	}
826 
827 	/* Rx and Tx usecs has per queue value. If user doesn't specify the
828 	 * queue, apply to all queues.
829 	 */
830 	if (queue < 0) {
831 		for (i = 0; i < adapter->num_active_queues; i++)
832 			if (iavf_set_itr_per_queue(adapter, ec, i))
833 				return -EINVAL;
834 	} else if (queue < adapter->num_active_queues) {
835 		if (iavf_set_itr_per_queue(adapter, ec, queue))
836 			return -EINVAL;
837 	} else {
838 		netif_info(adapter, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
839 			   adapter->num_active_queues - 1);
840 		return -EINVAL;
841 	}
842 
843 	return 0;
844 }
845 
846 /**
847  * iavf_set_coalesce - Set interrupt coalescing settings
848  * @netdev: network interface device structure
849  * @ec: ethtool coalesce structure
850  *
851  * Change current coalescing settings for every queue.
852  **/
iavf_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec)853 static int iavf_set_coalesce(struct net_device *netdev,
854 			     struct ethtool_coalesce *ec)
855 {
856 	return __iavf_set_coalesce(netdev, ec, -1);
857 }
858 
859 /**
860  * iavf_set_per_queue_coalesce - set specific queue's coalesce settings
861  * @netdev: the netdev to change
862  * @ec: ethtool's coalesce settings
863  * @queue: the queue to modify
864  *
865  * Modifies a specific queue's coalesce settings.
866  */
iavf_set_per_queue_coalesce(struct net_device * netdev,u32 queue,struct ethtool_coalesce * ec)867 static int iavf_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
868 				       struct ethtool_coalesce *ec)
869 {
870 	return __iavf_set_coalesce(netdev, ec, queue);
871 }
872 
873 /**
874  * iavf_get_rxnfc - command to get RX flow classification rules
875  * @netdev: network interface device structure
876  * @cmd: ethtool rxnfc command
877  * @rule_locs: pointer to store rule locations
878  *
879  * Returns Success if the command is supported.
880  **/
iavf_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd,u32 * rule_locs)881 static int iavf_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
882 			  u32 *rule_locs)
883 {
884 	struct iavf_adapter *adapter = netdev_priv(netdev);
885 	int ret = -EOPNOTSUPP;
886 
887 	switch (cmd->cmd) {
888 	case ETHTOOL_GRXRINGS:
889 		cmd->data = adapter->num_active_queues;
890 		ret = 0;
891 		break;
892 	case ETHTOOL_GRXFH:
893 		netdev_info(netdev,
894 			    "RSS hash info is not available to vf, use pf.\n");
895 		break;
896 	default:
897 		break;
898 	}
899 
900 	return ret;
901 }
902 /**
903  * iavf_get_channels: get the number of channels supported by the device
904  * @netdev: network interface device structure
905  * @ch: channel information structure
906  *
907  * For the purposes of our device, we only use combined channels, i.e. a tx/rx
908  * queue pair. Report one extra channel to match our "other" MSI-X vector.
909  **/
iavf_get_channels(struct net_device * netdev,struct ethtool_channels * ch)910 static void iavf_get_channels(struct net_device *netdev,
911 			      struct ethtool_channels *ch)
912 {
913 	struct iavf_adapter *adapter = netdev_priv(netdev);
914 
915 	/* Report maximum channels */
916 	ch->max_combined = adapter->vsi_res->num_queue_pairs;
917 
918 	ch->max_other = NONQ_VECS;
919 	ch->other_count = NONQ_VECS;
920 
921 	ch->combined_count = adapter->num_active_queues;
922 }
923 
924 /**
925  * iavf_set_channels: set the new channel count
926  * @netdev: network interface device structure
927  * @ch: channel information structure
928  *
929  * Negotiate a new number of channels with the PF then do a reset.  During
930  * reset we'll realloc queues and fix the RSS table.  Returns 0 on success,
931  * negative on failure.
932  **/
iavf_set_channels(struct net_device * netdev,struct ethtool_channels * ch)933 static int iavf_set_channels(struct net_device *netdev,
934 			     struct ethtool_channels *ch)
935 {
936 	struct iavf_adapter *adapter = netdev_priv(netdev);
937 	u32 num_req = ch->combined_count;
938 	int i;
939 
940 	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
941 	    adapter->num_tc) {
942 		dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
943 		return -EINVAL;
944 	}
945 
946 	/* All of these should have already been checked by ethtool before this
947 	 * even gets to us, but just to be sure.
948 	 */
949 	if (num_req == 0 || num_req > adapter->vsi_res->num_queue_pairs)
950 		return -EINVAL;
951 
952 	if (num_req == adapter->num_active_queues)
953 		return 0;
954 
955 	if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
956 		return -EINVAL;
957 
958 	adapter->num_req_queues = num_req;
959 	adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
960 	iavf_schedule_reset(adapter);
961 
962 	/* wait for the reset is done */
963 	for (i = 0; i < IAVF_RESET_WAIT_COMPLETE_COUNT; i++) {
964 		msleep(IAVF_RESET_WAIT_MS);
965 		if (adapter->flags & IAVF_FLAG_RESET_PENDING)
966 			continue;
967 		break;
968 	}
969 	if (i == IAVF_RESET_WAIT_COMPLETE_COUNT) {
970 		adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
971 		adapter->num_req_queues = 0;
972 		return -EOPNOTSUPP;
973 	}
974 
975 	return 0;
976 }
977 
978 /**
979  * iavf_get_rxfh_key_size - get the RSS hash key size
980  * @netdev: network interface device structure
981  *
982  * Returns the table size.
983  **/
iavf_get_rxfh_key_size(struct net_device * netdev)984 static u32 iavf_get_rxfh_key_size(struct net_device *netdev)
985 {
986 	struct iavf_adapter *adapter = netdev_priv(netdev);
987 
988 	return adapter->rss_key_size;
989 }
990 
991 /**
992  * iavf_get_rxfh_indir_size - get the rx flow hash indirection table size
993  * @netdev: network interface device structure
994  *
995  * Returns the table size.
996  **/
iavf_get_rxfh_indir_size(struct net_device * netdev)997 static u32 iavf_get_rxfh_indir_size(struct net_device *netdev)
998 {
999 	struct iavf_adapter *adapter = netdev_priv(netdev);
1000 
1001 	return adapter->rss_lut_size;
1002 }
1003 
1004 /**
1005  * iavf_get_rxfh - get the rx flow hash indirection table
1006  * @netdev: network interface device structure
1007  * @indir: indirection table
1008  * @key: hash key
1009  * @hfunc: hash function in use
1010  *
1011  * Reads the indirection table directly from the hardware. Always returns 0.
1012  **/
iavf_get_rxfh(struct net_device * netdev,u32 * indir,u8 * key,u8 * hfunc)1013 static int iavf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1014 			 u8 *hfunc)
1015 {
1016 	struct iavf_adapter *adapter = netdev_priv(netdev);
1017 	u16 i;
1018 
1019 	if (hfunc)
1020 		*hfunc = ETH_RSS_HASH_TOP;
1021 	if (key)
1022 		memcpy(key, adapter->rss_key, adapter->rss_key_size);
1023 
1024 	if (indir)
1025 		/* Each 32 bits pointed by 'indir' is stored with a lut entry */
1026 		for (i = 0; i < adapter->rss_lut_size; i++)
1027 			indir[i] = (u32)adapter->rss_lut[i];
1028 
1029 	return 0;
1030 }
1031 
1032 /**
1033  * iavf_set_rxfh - set the rx flow hash indirection table
1034  * @netdev: network interface device structure
1035  * @indir: indirection table
1036  * @key: hash key
1037  * @hfunc: hash function to use
1038  *
1039  * Returns -EINVAL if the table specifies an inavlid queue id, otherwise
1040  * returns 0 after programming the table.
1041  **/
iavf_set_rxfh(struct net_device * netdev,const u32 * indir,const u8 * key,const u8 hfunc)1042 static int iavf_set_rxfh(struct net_device *netdev, const u32 *indir,
1043 			 const u8 *key, const u8 hfunc)
1044 {
1045 	struct iavf_adapter *adapter = netdev_priv(netdev);
1046 	u16 i;
1047 
1048 	/* We do not allow change in unsupported parameters */
1049 	if (key ||
1050 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
1051 		return -EOPNOTSUPP;
1052 	if (!indir)
1053 		return 0;
1054 
1055 	if (key)
1056 		memcpy(adapter->rss_key, key, adapter->rss_key_size);
1057 
1058 	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
1059 	for (i = 0; i < adapter->rss_lut_size; i++)
1060 		adapter->rss_lut[i] = (u8)(indir[i]);
1061 
1062 	return iavf_config_rss(adapter);
1063 }
1064 
1065 static const struct ethtool_ops iavf_ethtool_ops = {
1066 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1067 				     ETHTOOL_COALESCE_MAX_FRAMES |
1068 				     ETHTOOL_COALESCE_MAX_FRAMES_IRQ |
1069 				     ETHTOOL_COALESCE_USE_ADAPTIVE,
1070 	.get_drvinfo		= iavf_get_drvinfo,
1071 	.get_link		= ethtool_op_get_link,
1072 	.get_ringparam		= iavf_get_ringparam,
1073 	.set_ringparam		= iavf_set_ringparam,
1074 	.get_strings		= iavf_get_strings,
1075 	.get_ethtool_stats	= iavf_get_ethtool_stats,
1076 	.get_sset_count		= iavf_get_sset_count,
1077 	.get_priv_flags		= iavf_get_priv_flags,
1078 	.set_priv_flags		= iavf_set_priv_flags,
1079 	.get_msglevel		= iavf_get_msglevel,
1080 	.set_msglevel		= iavf_set_msglevel,
1081 	.get_coalesce		= iavf_get_coalesce,
1082 	.set_coalesce		= iavf_set_coalesce,
1083 	.get_per_queue_coalesce = iavf_get_per_queue_coalesce,
1084 	.set_per_queue_coalesce = iavf_set_per_queue_coalesce,
1085 	.get_rxnfc		= iavf_get_rxnfc,
1086 	.get_rxfh_indir_size	= iavf_get_rxfh_indir_size,
1087 	.get_rxfh		= iavf_get_rxfh,
1088 	.set_rxfh		= iavf_set_rxfh,
1089 	.get_channels		= iavf_get_channels,
1090 	.set_channels		= iavf_set_channels,
1091 	.get_rxfh_key_size	= iavf_get_rxfh_key_size,
1092 	.get_link_ksettings	= iavf_get_link_ksettings,
1093 };
1094 
1095 /**
1096  * iavf_set_ethtool_ops - Initialize ethtool ops struct
1097  * @netdev: network interface device structure
1098  *
1099  * Sets ethtool ops struct in our netdev so that ethtool can call
1100  * our functions.
1101  **/
iavf_set_ethtool_ops(struct net_device * netdev)1102 void iavf_set_ethtool_ops(struct net_device *netdev)
1103 {
1104 	netdev->ethtool_ops = &iavf_ethtool_ops;
1105 }
1106