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