Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.9.4.
  1// SPDX-License-Identifier: GPL-2.0
  2/*******************************************************************************
  3 *
  4 * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  5 * Copyright(c) 2013 - 2016 Intel Corporation.
  6 *
  7 * This program is free software; you can redistribute it and/or modify it
  8 * under the terms and conditions of the GNU General Public License,
  9 * version 2, as published by the Free Software Foundation.
 10 *
 11 * This program is distributed in the hope it will be useful, but WITHOUT
 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 14 * more details.
 15 *
 16 * You should have received a copy of the GNU General Public License along
 17 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 18 *
 19 * The full GNU General Public License is included in this distribution in
 20 * the file called "COPYING".
 21 *
 22 * Contact Information:
 23 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
 24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 25 *
 26 ******************************************************************************/
 27
 28/* ethtool support for i40evf */
 29#include "i40evf.h"
 30
 31#include <linux/uaccess.h>
 32
 33struct i40evf_stats {
 34	char stat_string[ETH_GSTRING_LEN];
 35	int stat_offset;
 36};
 37
 38#define I40EVF_STAT(_name, _stat) { \
 39	.stat_string = _name, \
 40	.stat_offset = offsetof(struct i40evf_adapter, _stat) \
 41}
 42
 43/* All stats are u64, so we don't need to track the size of the field. */
 44static const struct i40evf_stats i40evf_gstrings_stats[] = {
 45	I40EVF_STAT("rx_bytes", current_stats.rx_bytes),
 46	I40EVF_STAT("rx_unicast", current_stats.rx_unicast),
 47	I40EVF_STAT("rx_multicast", current_stats.rx_multicast),
 48	I40EVF_STAT("rx_broadcast", current_stats.rx_broadcast),
 49	I40EVF_STAT("rx_discards", current_stats.rx_discards),
 50	I40EVF_STAT("rx_unknown_protocol", current_stats.rx_unknown_protocol),
 51	I40EVF_STAT("tx_bytes", current_stats.tx_bytes),
 52	I40EVF_STAT("tx_unicast", current_stats.tx_unicast),
 53	I40EVF_STAT("tx_multicast", current_stats.tx_multicast),
 54	I40EVF_STAT("tx_broadcast", current_stats.tx_broadcast),
 55	I40EVF_STAT("tx_discards", current_stats.tx_discards),
 56	I40EVF_STAT("tx_errors", current_stats.tx_errors),
 57};
 58
 59#define I40EVF_GLOBAL_STATS_LEN ARRAY_SIZE(i40evf_gstrings_stats)
 60#define I40EVF_QUEUE_STATS_LEN(_dev) \
 61	(((struct i40evf_adapter *)\
 62		netdev_priv(_dev))->num_active_queues \
 63		  * 2 * (sizeof(struct i40e_queue_stats) / sizeof(u64)))
 64#define I40EVF_STATS_LEN(_dev) \
 65	(I40EVF_GLOBAL_STATS_LEN + I40EVF_QUEUE_STATS_LEN(_dev))
 66
 67/* For now we have one and only one private flag and it is only defined
 68 * when we have support for the SKIP_CPU_SYNC DMA attribute.  Instead
 69 * of leaving all this code sitting around empty we will strip it unless
 70 * our one private flag is actually available.
 71 */
 72struct i40evf_priv_flags {
 73	char flag_string[ETH_GSTRING_LEN];
 74	u32 flag;
 75	bool read_only;
 76};
 77
 78#define I40EVF_PRIV_FLAG(_name, _flag, _read_only) { \
 79	.flag_string = _name, \
 80	.flag = _flag, \
 81	.read_only = _read_only, \
 82}
 83
 84static const struct i40evf_priv_flags i40evf_gstrings_priv_flags[] = {
 85	I40EVF_PRIV_FLAG("legacy-rx", I40EVF_FLAG_LEGACY_RX, 0),
 86};
 87
 88#define I40EVF_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40evf_gstrings_priv_flags)
 89
 90/**
 91 * i40evf_get_link_ksettings - Get Link Speed and Duplex settings
 92 * @netdev: network interface device structure
 93 * @cmd: ethtool command
 94 *
 95 * Reports speed/duplex settings. Because this is a VF, we don't know what
 96 * kind of link we really have, so we fake it.
 97 **/
 98static int i40evf_get_link_ksettings(struct net_device *netdev,
 99				     struct ethtool_link_ksettings *cmd)
100{
101	struct i40evf_adapter *adapter = netdev_priv(netdev);
102
103	ethtool_link_ksettings_zero_link_mode(cmd, supported);
104	cmd->base.autoneg = AUTONEG_DISABLE;
105	cmd->base.port = PORT_NONE;
106	/* Set speed and duplex */
107	switch (adapter->link_speed) {
108	case I40E_LINK_SPEED_40GB:
109		cmd->base.speed = SPEED_40000;
110		break;
111	case I40E_LINK_SPEED_25GB:
112#ifdef SPEED_25000
113		cmd->base.speed = SPEED_25000;
114#else
115		netdev_info(netdev,
116			    "Speed is 25G, display not supported by this version of ethtool.\n");
117#endif
118		break;
119	case I40E_LINK_SPEED_20GB:
120		cmd->base.speed = SPEED_20000;
121		break;
122	case I40E_LINK_SPEED_10GB:
123		cmd->base.speed = SPEED_10000;
124		break;
125	case I40E_LINK_SPEED_1GB:
126		cmd->base.speed = SPEED_1000;
127		break;
128	case I40E_LINK_SPEED_100MB:
129		cmd->base.speed = SPEED_100;
130		break;
131	default:
132		break;
133	}
134	cmd->base.duplex = DUPLEX_FULL;
135
136	return 0;
137}
138
139/**
140 * i40evf_get_sset_count - Get length of string set
141 * @netdev: network interface device structure
142 * @sset: id of string set
143 *
144 * Reports size of string table. This driver only supports
145 * strings for statistics.
146 **/
147static int i40evf_get_sset_count(struct net_device *netdev, int sset)
148{
149	if (sset == ETH_SS_STATS)
150		return I40EVF_STATS_LEN(netdev);
151	else if (sset == ETH_SS_PRIV_FLAGS)
152		return I40EVF_PRIV_FLAGS_STR_LEN;
153	else
154		return -EINVAL;
155}
156
157/**
158 * i40evf_get_ethtool_stats - report device statistics
159 * @netdev: network interface device structure
160 * @stats: ethtool statistics structure
161 * @data: pointer to data buffer
162 *
163 * All statistics are added to the data buffer as an array of u64.
164 **/
165static void i40evf_get_ethtool_stats(struct net_device *netdev,
166				     struct ethtool_stats *stats, u64 *data)
167{
168	struct i40evf_adapter *adapter = netdev_priv(netdev);
169	unsigned int i, j;
170	char *p;
171
172	for (i = 0; i < I40EVF_GLOBAL_STATS_LEN; i++) {
173		p = (char *)adapter + i40evf_gstrings_stats[i].stat_offset;
174		data[i] =  *(u64 *)p;
175	}
176	for (j = 0; j < adapter->num_active_queues; j++) {
177		data[i++] = adapter->tx_rings[j].stats.packets;
178		data[i++] = adapter->tx_rings[j].stats.bytes;
179	}
180	for (j = 0; j < adapter->num_active_queues; j++) {
181		data[i++] = adapter->rx_rings[j].stats.packets;
182		data[i++] = adapter->rx_rings[j].stats.bytes;
183	}
184}
185
186/**
187 * i40evf_get_strings - Get string set
188 * @netdev: network interface device structure
189 * @sset: id of string set
190 * @data: buffer for string data
191 *
192 * Builds stats string table.
193 **/
194static void i40evf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
195{
196	struct i40evf_adapter *adapter = netdev_priv(netdev);
197	u8 *p = data;
198	int i;
199
200	if (sset == ETH_SS_STATS) {
201		for (i = 0; i < (int)I40EVF_GLOBAL_STATS_LEN; i++) {
202			memcpy(p, i40evf_gstrings_stats[i].stat_string,
203			       ETH_GSTRING_LEN);
204			p += ETH_GSTRING_LEN;
205		}
206		for (i = 0; i < adapter->num_active_queues; i++) {
207			snprintf(p, ETH_GSTRING_LEN, "tx-%u.packets", i);
208			p += ETH_GSTRING_LEN;
209			snprintf(p, ETH_GSTRING_LEN, "tx-%u.bytes", i);
210			p += ETH_GSTRING_LEN;
211		}
212		for (i = 0; i < adapter->num_active_queues; i++) {
213			snprintf(p, ETH_GSTRING_LEN, "rx-%u.packets", i);
214			p += ETH_GSTRING_LEN;
215			snprintf(p, ETH_GSTRING_LEN, "rx-%u.bytes", i);
216			p += ETH_GSTRING_LEN;
217		}
218	} else if (sset == ETH_SS_PRIV_FLAGS) {
219		for (i = 0; i < I40EVF_PRIV_FLAGS_STR_LEN; i++) {
220			snprintf(p, ETH_GSTRING_LEN, "%s",
221				 i40evf_gstrings_priv_flags[i].flag_string);
222			p += ETH_GSTRING_LEN;
223		}
224	}
225}
226
227/**
228 * i40evf_get_priv_flags - report device private flags
229 * @dev: network interface device structure
230 *
231 * The get string set count and the string set should be matched for each
232 * flag returned.  Add new strings for each flag to the i40e_gstrings_priv_flags
233 * array.
234 *
235 * Returns a u32 bitmap of flags.
236 **/
237static u32 i40evf_get_priv_flags(struct net_device *netdev)
238{
239	struct i40evf_adapter *adapter = netdev_priv(netdev);
240	u32 i, ret_flags = 0;
241
242	for (i = 0; i < I40EVF_PRIV_FLAGS_STR_LEN; i++) {
243		const struct i40evf_priv_flags *priv_flags;
244
245		priv_flags = &i40evf_gstrings_priv_flags[i];
246
247		if (priv_flags->flag & adapter->flags)
248			ret_flags |= BIT(i);
249	}
250
251	return ret_flags;
252}
253
254/**
255 * i40evf_set_priv_flags - set private flags
256 * @dev: network interface device structure
257 * @flags: bit flags to be set
258 **/
259static int i40evf_set_priv_flags(struct net_device *netdev, u32 flags)
260{
261	struct i40evf_adapter *adapter = netdev_priv(netdev);
262	u32 orig_flags, new_flags, changed_flags;
263	u32 i;
264
265	orig_flags = READ_ONCE(adapter->flags);
266	new_flags = orig_flags;
267
268	for (i = 0; i < I40EVF_PRIV_FLAGS_STR_LEN; i++) {
269		const struct i40evf_priv_flags *priv_flags;
270
271		priv_flags = &i40evf_gstrings_priv_flags[i];
272
273		if (flags & BIT(i))
274			new_flags |= priv_flags->flag;
275		else
276			new_flags &= ~(priv_flags->flag);
277
278		if (priv_flags->read_only &&
279		    ((orig_flags ^ new_flags) & ~BIT(i)))
280			return -EOPNOTSUPP;
281	}
282
283	/* Before we finalize any flag changes, any checks which we need to
284	 * perform to determine if the new flags will be supported should go
285	 * here...
286	 */
287
288	/* Compare and exchange the new flags into place. If we failed, that
289	 * is if cmpxchg returns anything but the old value, this means
290	 * something else must have modified the flags variable since we
291	 * copied it. We'll just punt with an error and log something in the
292	 * message buffer.
293	 */
294	if (cmpxchg(&adapter->flags, orig_flags, new_flags) != orig_flags) {
295		dev_warn(&adapter->pdev->dev,
296			 "Unable to update adapter->flags as it was modified by another thread...\n");
297		return -EAGAIN;
298	}
299
300	changed_flags = orig_flags ^ new_flags;
301
302	/* Process any additional changes needed as a result of flag changes.
303	 * The changed_flags value reflects the list of bits that were changed
304	 * in the code above.
305	 */
306
307	/* issue a reset to force legacy-rx change to take effect */
308	if (changed_flags & I40EVF_FLAG_LEGACY_RX) {
309		if (netif_running(netdev)) {
310			adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
311			schedule_work(&adapter->reset_task);
312		}
313	}
314
315	return 0;
316}
317
318/**
319 * i40evf_get_msglevel - Get debug message level
320 * @netdev: network interface device structure
321 *
322 * Returns current debug message level.
323 **/
324static u32 i40evf_get_msglevel(struct net_device *netdev)
325{
326	struct i40evf_adapter *adapter = netdev_priv(netdev);
327
328	return adapter->msg_enable;
329}
330
331/**
332 * i40evf_set_msglevel - Set debug message level
333 * @netdev: network interface device structure
334 * @data: message level
335 *
336 * Set current debug message level. Higher values cause the driver to
337 * be noisier.
338 **/
339static void i40evf_set_msglevel(struct net_device *netdev, u32 data)
340{
341	struct i40evf_adapter *adapter = netdev_priv(netdev);
342
343	if (I40E_DEBUG_USER & data)
344		adapter->hw.debug_mask = data;
345	adapter->msg_enable = data;
346}
347
348/**
349 * i40evf_get_drvinfo - Get driver info
350 * @netdev: network interface device structure
351 * @drvinfo: ethool driver info structure
352 *
353 * Returns information about the driver and device for display to the user.
354 **/
355static void i40evf_get_drvinfo(struct net_device *netdev,
356			       struct ethtool_drvinfo *drvinfo)
357{
358	struct i40evf_adapter *adapter = netdev_priv(netdev);
359
360	strlcpy(drvinfo->driver, i40evf_driver_name, 32);
361	strlcpy(drvinfo->version, i40evf_driver_version, 32);
362	strlcpy(drvinfo->fw_version, "N/A", 4);
363	strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
364	drvinfo->n_priv_flags = I40EVF_PRIV_FLAGS_STR_LEN;
365}
366
367/**
368 * i40evf_get_ringparam - Get ring parameters
369 * @netdev: network interface device structure
370 * @ring: ethtool ringparam structure
371 *
372 * Returns current ring parameters. TX and RX rings are reported separately,
373 * but the number of rings is not reported.
374 **/
375static void i40evf_get_ringparam(struct net_device *netdev,
376				 struct ethtool_ringparam *ring)
377{
378	struct i40evf_adapter *adapter = netdev_priv(netdev);
379
380	ring->rx_max_pending = I40EVF_MAX_RXD;
381	ring->tx_max_pending = I40EVF_MAX_TXD;
382	ring->rx_pending = adapter->rx_desc_count;
383	ring->tx_pending = adapter->tx_desc_count;
384}
385
386/**
387 * i40evf_set_ringparam - Set ring parameters
388 * @netdev: network interface device structure
389 * @ring: ethtool ringparam structure
390 *
391 * Sets ring parameters. TX and RX rings are controlled separately, but the
392 * number of rings is not specified, so all rings get the same settings.
393 **/
394static int i40evf_set_ringparam(struct net_device *netdev,
395				struct ethtool_ringparam *ring)
396{
397	struct i40evf_adapter *adapter = netdev_priv(netdev);
398	u32 new_rx_count, new_tx_count;
399
400	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
401		return -EINVAL;
402
403	new_tx_count = clamp_t(u32, ring->tx_pending,
404			       I40EVF_MIN_TXD,
405			       I40EVF_MAX_TXD);
406	new_tx_count = ALIGN(new_tx_count, I40EVF_REQ_DESCRIPTOR_MULTIPLE);
407
408	new_rx_count = clamp_t(u32, ring->rx_pending,
409			       I40EVF_MIN_RXD,
410			       I40EVF_MAX_RXD);
411	new_rx_count = ALIGN(new_rx_count, I40EVF_REQ_DESCRIPTOR_MULTIPLE);
412
413	/* if nothing to do return success */
414	if ((new_tx_count == adapter->tx_desc_count) &&
415	    (new_rx_count == adapter->rx_desc_count))
416		return 0;
417
418	adapter->tx_desc_count = new_tx_count;
419	adapter->rx_desc_count = new_rx_count;
420
421	if (netif_running(netdev)) {
422		adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
423		schedule_work(&adapter->reset_task);
424	}
425
426	return 0;
427}
428
429/**
430 * __i40evf_get_coalesce - get per-queue coalesce settings
431 * @netdev: the netdev to check
432 * @ec: ethtool coalesce data structure
433 * @queue: which queue to pick
434 *
435 * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
436 * are per queue. If queue is <0 then we default to queue 0 as the
437 * representative value.
438 **/
439static int __i40evf_get_coalesce(struct net_device *netdev,
440				 struct ethtool_coalesce *ec,
441				 int queue)
442{
443	struct i40evf_adapter *adapter = netdev_priv(netdev);
444	struct i40e_vsi *vsi = &adapter->vsi;
445	struct i40e_ring *rx_ring, *tx_ring;
446
447	ec->tx_max_coalesced_frames = vsi->work_limit;
448	ec->rx_max_coalesced_frames = vsi->work_limit;
449
450	/* Rx and Tx usecs per queue value. If user doesn't specify the
451	 * queue, return queue 0's value to represent.
452	 */
453	if (queue < 0)
454		queue = 0;
455	else if (queue >= adapter->num_active_queues)
456		return -EINVAL;
457
458	rx_ring = &adapter->rx_rings[queue];
459	tx_ring = &adapter->tx_rings[queue];
460
461	if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
462		ec->use_adaptive_rx_coalesce = 1;
463
464	if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
465		ec->use_adaptive_tx_coalesce = 1;
466
467	ec->rx_coalesce_usecs = rx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
468	ec->tx_coalesce_usecs = tx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
469
470	return 0;
471}
472
473/**
474 * i40evf_get_coalesce - Get interrupt coalescing settings
475 * @netdev: network interface device structure
476 * @ec: ethtool coalesce structure
477 *
478 * Returns current coalescing settings. This is referred to elsewhere in the
479 * driver as Interrupt Throttle Rate, as this is how the hardware describes
480 * this functionality. Note that if per-queue settings have been modified this
481 * only represents the settings of queue 0.
482 **/
483static int i40evf_get_coalesce(struct net_device *netdev,
484			       struct ethtool_coalesce *ec)
485{
486	return __i40evf_get_coalesce(netdev, ec, -1);
487}
488
489/**
490 * i40evf_get_per_queue_coalesce - get coalesce values for specific queue
491 * @netdev: netdev to read
492 * @ec: coalesce settings from ethtool
493 * @queue: the queue to read
494 *
495 * Read specific queue's coalesce settings.
496 **/
497static int i40evf_get_per_queue_coalesce(struct net_device *netdev,
498					 u32 queue,
499					 struct ethtool_coalesce *ec)
500{
501	return __i40evf_get_coalesce(netdev, ec, queue);
502}
503
504/**
505 * i40evf_set_itr_per_queue - set ITR values for specific queue
506 * @adapter: the VF adapter struct to set values for
507 * @ec: coalesce settings from ethtool
508 * @queue: the queue to modify
509 *
510 * Change the ITR settings for a specific queue.
511 **/
512static void i40evf_set_itr_per_queue(struct i40evf_adapter *adapter,
513				     struct ethtool_coalesce *ec,
514				     int queue)
515{
516	struct i40e_ring *rx_ring = &adapter->rx_rings[queue];
517	struct i40e_ring *tx_ring = &adapter->tx_rings[queue];
518	struct i40e_q_vector *q_vector;
519
520	rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
521	tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
522
523	rx_ring->itr_setting |= I40E_ITR_DYNAMIC;
524	if (!ec->use_adaptive_rx_coalesce)
525		rx_ring->itr_setting ^= I40E_ITR_DYNAMIC;
526
527	tx_ring->itr_setting |= I40E_ITR_DYNAMIC;
528	if (!ec->use_adaptive_tx_coalesce)
529		tx_ring->itr_setting ^= I40E_ITR_DYNAMIC;
530
531	q_vector = rx_ring->q_vector;
532	q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
533
534	q_vector = tx_ring->q_vector;
535	q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
536
537	/* The interrupt handler itself will take care of programming
538	 * the Tx and Rx ITR values based on the values we have entered
539	 * into the q_vector, no need to write the values now.
540	 */
541}
542
543/**
544 * __i40evf_set_coalesce - set coalesce settings for particular queue
545 * @netdev: the netdev to change
546 * @ec: ethtool coalesce settings
547 * @queue: the queue to change
548 *
549 * Sets the coalesce settings for a particular queue.
550 **/
551static int __i40evf_set_coalesce(struct net_device *netdev,
552				 struct ethtool_coalesce *ec,
553				 int queue)
554{
555	struct i40evf_adapter *adapter = netdev_priv(netdev);
556	struct i40e_vsi *vsi = &adapter->vsi;
557	int i;
558
559	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
560		vsi->work_limit = ec->tx_max_coalesced_frames_irq;
561
562	if (ec->rx_coalesce_usecs == 0) {
563		if (ec->use_adaptive_rx_coalesce)
564			netif_info(adapter, drv, netdev, "rx-usecs=0, need to disable adaptive-rx for a complete disable\n");
565	} else if ((ec->rx_coalesce_usecs < I40E_MIN_ITR) ||
566		   (ec->rx_coalesce_usecs > I40E_MAX_ITR)) {
567		netif_info(adapter, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
568		return -EINVAL;
569	}
570
571	else
572	if (ec->tx_coalesce_usecs == 0) {
573		if (ec->use_adaptive_tx_coalesce)
574			netif_info(adapter, drv, netdev, "tx-usecs=0, need to disable adaptive-tx for a complete disable\n");
575	} else if ((ec->tx_coalesce_usecs < I40E_MIN_ITR) ||
576		   (ec->tx_coalesce_usecs > I40E_MAX_ITR)) {
577		netif_info(adapter, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
578		return -EINVAL;
579	}
580
581	/* Rx and Tx usecs has per queue value. If user doesn't specify the
582	 * queue, apply to all queues.
583	 */
584	if (queue < 0) {
585		for (i = 0; i < adapter->num_active_queues; i++)
586			i40evf_set_itr_per_queue(adapter, ec, i);
587	} else if (queue < adapter->num_active_queues) {
588		i40evf_set_itr_per_queue(adapter, ec, queue);
589	} else {
590		netif_info(adapter, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
591			   adapter->num_active_queues - 1);
592		return -EINVAL;
593	}
594
595	return 0;
596}
597
598/**
599 * i40evf_set_coalesce - Set interrupt coalescing settings
600 * @netdev: network interface device structure
601 * @ec: ethtool coalesce structure
602 *
603 * Change current coalescing settings for every queue.
604 **/
605static int i40evf_set_coalesce(struct net_device *netdev,
606			       struct ethtool_coalesce *ec)
607{
608	return __i40evf_set_coalesce(netdev, ec, -1);
609}
610
611/**
612 * i40evf_set_per_queue_coalesce - set specific queue's coalesce settings
613 * @netdev: the netdev to change
614 * @ec: ethtool's coalesce settings
615 * @queue: the queue to modify
616 *
617 * Modifies a specific queue's coalesce settings.
618 */
619static int i40evf_set_per_queue_coalesce(struct net_device *netdev,
620					 u32 queue,
621					 struct ethtool_coalesce *ec)
622{
623	return __i40evf_set_coalesce(netdev, ec, queue);
624}
625
626/**
627 * i40evf_get_rxnfc - command to get RX flow classification rules
628 * @netdev: network interface device structure
629 * @cmd: ethtool rxnfc command
630 *
631 * Returns Success if the command is supported.
632 **/
633static int i40evf_get_rxnfc(struct net_device *netdev,
634			    struct ethtool_rxnfc *cmd,
635			    u32 *rule_locs)
636{
637	struct i40evf_adapter *adapter = netdev_priv(netdev);
638	int ret = -EOPNOTSUPP;
639
640	switch (cmd->cmd) {
641	case ETHTOOL_GRXRINGS:
642		cmd->data = adapter->num_active_queues;
643		ret = 0;
644		break;
645	case ETHTOOL_GRXFH:
646		netdev_info(netdev,
647			    "RSS hash info is not available to vf, use pf.\n");
648		break;
649	default:
650		break;
651	}
652
653	return ret;
654}
655/**
656 * i40evf_get_channels: get the number of channels supported by the device
657 * @netdev: network interface device structure
658 * @ch: channel information structure
659 *
660 * For the purposes of our device, we only use combined channels, i.e. a tx/rx
661 * queue pair. Report one extra channel to match our "other" MSI-X vector.
662 **/
663static void i40evf_get_channels(struct net_device *netdev,
664				struct ethtool_channels *ch)
665{
666	struct i40evf_adapter *adapter = netdev_priv(netdev);
667
668	/* Report maximum channels */
669	ch->max_combined = I40EVF_MAX_REQ_QUEUES;
670
671	ch->max_other = NONQ_VECS;
672	ch->other_count = NONQ_VECS;
673
674	ch->combined_count = adapter->num_active_queues;
675}
676
677/**
678 * i40evf_set_channels: set the new channel count
679 * @netdev: network interface device structure
680 * @ch: channel information structure
681 *
682 * Negotiate a new number of channels with the PF then do a reset.  During
683 * reset we'll realloc queues and fix the RSS table.  Returns 0 on success,
684 * negative on failure.
685 **/
686static int i40evf_set_channels(struct net_device *netdev,
687			       struct ethtool_channels *ch)
688{
689	struct i40evf_adapter *adapter = netdev_priv(netdev);
690	int num_req = ch->combined_count;
691
692	if (num_req != adapter->num_active_queues &&
693	    !(adapter->vf_res->vf_cap_flags &
694	      VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)) {
695		dev_info(&adapter->pdev->dev, "PF is not capable of queue negotiation.\n");
696		return -EINVAL;
697	}
698
699	if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
700	    adapter->num_tc) {
701		dev_info(&adapter->pdev->dev, "Cannot set channels since ADq is enabled.\n");
702		return -EINVAL;
703	}
704
705	/* All of these should have already been checked by ethtool before this
706	 * even gets to us, but just to be sure.
707	 */
708	if (num_req <= 0 || num_req > I40EVF_MAX_REQ_QUEUES)
709		return -EINVAL;
710
711	if (ch->rx_count || ch->tx_count || ch->other_count != NONQ_VECS)
712		return -EINVAL;
713
714	adapter->num_req_queues = num_req;
715	return i40evf_request_queues(adapter, num_req);
716}
717
718/**
719 * i40evf_get_rxfh_key_size - get the RSS hash key size
720 * @netdev: network interface device structure
721 *
722 * Returns the table size.
723 **/
724static u32 i40evf_get_rxfh_key_size(struct net_device *netdev)
725{
726	struct i40evf_adapter *adapter = netdev_priv(netdev);
727
728	return adapter->rss_key_size;
729}
730
731/**
732 * i40evf_get_rxfh_indir_size - get the rx flow hash indirection table size
733 * @netdev: network interface device structure
734 *
735 * Returns the table size.
736 **/
737static u32 i40evf_get_rxfh_indir_size(struct net_device *netdev)
738{
739	struct i40evf_adapter *adapter = netdev_priv(netdev);
740
741	return adapter->rss_lut_size;
742}
743
744/**
745 * i40evf_get_rxfh - get the rx flow hash indirection table
746 * @netdev: network interface device structure
747 * @indir: indirection table
748 * @key: hash key
749 *
750 * Reads the indirection table directly from the hardware. Always returns 0.
751 **/
752static int i40evf_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
753			   u8 *hfunc)
754{
755	struct i40evf_adapter *adapter = netdev_priv(netdev);
756	u16 i;
757
758	if (hfunc)
759		*hfunc = ETH_RSS_HASH_TOP;
760	if (!indir)
761		return 0;
762
763	memcpy(key, adapter->rss_key, adapter->rss_key_size);
764
765	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
766	for (i = 0; i < adapter->rss_lut_size; i++)
767		indir[i] = (u32)adapter->rss_lut[i];
768
769	return 0;
770}
771
772/**
773 * i40evf_set_rxfh - set the rx flow hash indirection table
774 * @netdev: network interface device structure
775 * @indir: indirection table
776 * @key: hash key
777 *
778 * Returns -EINVAL if the table specifies an inavlid queue id, otherwise
779 * returns 0 after programming the table.
780 **/
781static int i40evf_set_rxfh(struct net_device *netdev, const u32 *indir,
782			   const u8 *key, const u8 hfunc)
783{
784	struct i40evf_adapter *adapter = netdev_priv(netdev);
785	u16 i;
786
787	/* We do not allow change in unsupported parameters */
788	if (key ||
789	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
790		return -EOPNOTSUPP;
791	if (!indir)
792		return 0;
793
794	if (key) {
795		memcpy(adapter->rss_key, key, adapter->rss_key_size);
796	}
797
798	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
799	for (i = 0; i < adapter->rss_lut_size; i++)
800		adapter->rss_lut[i] = (u8)(indir[i]);
801
802	return i40evf_config_rss(adapter);
803}
804
805static const struct ethtool_ops i40evf_ethtool_ops = {
806	.get_drvinfo		= i40evf_get_drvinfo,
807	.get_link		= ethtool_op_get_link,
808	.get_ringparam		= i40evf_get_ringparam,
809	.set_ringparam		= i40evf_set_ringparam,
810	.get_strings		= i40evf_get_strings,
811	.get_ethtool_stats	= i40evf_get_ethtool_stats,
812	.get_sset_count		= i40evf_get_sset_count,
813	.get_priv_flags		= i40evf_get_priv_flags,
814	.set_priv_flags		= i40evf_set_priv_flags,
815	.get_msglevel		= i40evf_get_msglevel,
816	.set_msglevel		= i40evf_set_msglevel,
817	.get_coalesce		= i40evf_get_coalesce,
818	.set_coalesce		= i40evf_set_coalesce,
819	.get_per_queue_coalesce = i40evf_get_per_queue_coalesce,
820	.set_per_queue_coalesce = i40evf_set_per_queue_coalesce,
821	.get_rxnfc		= i40evf_get_rxnfc,
822	.get_rxfh_indir_size	= i40evf_get_rxfh_indir_size,
823	.get_rxfh		= i40evf_get_rxfh,
824	.set_rxfh		= i40evf_set_rxfh,
825	.get_channels		= i40evf_get_channels,
826	.set_channels		= i40evf_set_channels,
827	.get_rxfh_key_size	= i40evf_get_rxfh_key_size,
828	.get_link_ksettings	= i40evf_get_link_ksettings,
829};
830
831/**
832 * i40evf_set_ethtool_ops - Initialize ethtool ops struct
833 * @netdev: network interface device structure
834 *
835 * Sets ethtool ops struct in our netdev so that ethtool can call
836 * our functions.
837 **/
838void i40evf_set_ethtool_ops(struct net_device *netdev)
839{
840	netdev->ethtool_ops = &i40evf_ethtool_ops;
841}