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