]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/intel/i40e/i40e_ethtool.c
i40e: Allow user to change link settings if link is down
[karo-tx-linux.git] / drivers / net / ethernet / intel / i40e / i40e_ethtool.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2014 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 i40e */
28
29 #include "i40e.h"
30 #include "i40e_diag.h"
31
32 struct i40e_stats {
33         char stat_string[ETH_GSTRING_LEN];
34         int sizeof_stat;
35         int stat_offset;
36 };
37
38 #define I40E_STAT(_type, _name, _stat) { \
39         .stat_string = _name, \
40         .sizeof_stat = FIELD_SIZEOF(_type, _stat), \
41         .stat_offset = offsetof(_type, _stat) \
42 }
43 #define I40E_NETDEV_STAT(_net_stat) \
44                 I40E_STAT(struct net_device_stats, #_net_stat, _net_stat)
45 #define I40E_PF_STAT(_name, _stat) \
46                 I40E_STAT(struct i40e_pf, _name, _stat)
47 #define I40E_VSI_STAT(_name, _stat) \
48                 I40E_STAT(struct i40e_vsi, _name, _stat)
49 #define I40E_VEB_STAT(_name, _stat) \
50                 I40E_STAT(struct i40e_veb, _name, _stat)
51
52 static const struct i40e_stats i40e_gstrings_net_stats[] = {
53         I40E_NETDEV_STAT(rx_packets),
54         I40E_NETDEV_STAT(tx_packets),
55         I40E_NETDEV_STAT(rx_bytes),
56         I40E_NETDEV_STAT(tx_bytes),
57         I40E_NETDEV_STAT(rx_errors),
58         I40E_NETDEV_STAT(tx_errors),
59         I40E_NETDEV_STAT(rx_dropped),
60         I40E_NETDEV_STAT(tx_dropped),
61         I40E_NETDEV_STAT(collisions),
62         I40E_NETDEV_STAT(rx_length_errors),
63         I40E_NETDEV_STAT(rx_crc_errors),
64 };
65
66 static const struct i40e_stats i40e_gstrings_veb_stats[] = {
67         I40E_VEB_STAT("rx_bytes", stats.rx_bytes),
68         I40E_VEB_STAT("tx_bytes", stats.tx_bytes),
69         I40E_VEB_STAT("rx_unicast", stats.rx_unicast),
70         I40E_VEB_STAT("tx_unicast", stats.tx_unicast),
71         I40E_VEB_STAT("rx_multicast", stats.rx_multicast),
72         I40E_VEB_STAT("tx_multicast", stats.tx_multicast),
73         I40E_VEB_STAT("rx_broadcast", stats.rx_broadcast),
74         I40E_VEB_STAT("tx_broadcast", stats.tx_broadcast),
75         I40E_VEB_STAT("rx_discards", stats.rx_discards),
76         I40E_VEB_STAT("tx_discards", stats.tx_discards),
77         I40E_VEB_STAT("tx_errors", stats.tx_errors),
78         I40E_VEB_STAT("rx_unknown_protocol", stats.rx_unknown_protocol),
79 };
80
81 static const struct i40e_stats i40e_gstrings_misc_stats[] = {
82         I40E_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
83         I40E_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
84         I40E_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
85         I40E_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
86         I40E_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
87         I40E_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
88         I40E_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
89 };
90
91 static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
92                                  struct ethtool_rxnfc *cmd);
93
94 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
95  * but they are separate.  This device supports Virtualization, and
96  * as such might have several netdevs supporting VMDq and FCoE going
97  * through a single port.  The NETDEV_STATs are for individual netdevs
98  * seen at the top of the stack, and the PF_STATs are for the physical
99  * function at the bottom of the stack hosting those netdevs.
100  *
101  * The PF_STATs are appended to the netdev stats only when ethtool -S
102  * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
103  */
104 static struct i40e_stats i40e_gstrings_stats[] = {
105         I40E_PF_STAT("rx_bytes", stats.eth.rx_bytes),
106         I40E_PF_STAT("tx_bytes", stats.eth.tx_bytes),
107         I40E_PF_STAT("rx_unicast", stats.eth.rx_unicast),
108         I40E_PF_STAT("tx_unicast", stats.eth.tx_unicast),
109         I40E_PF_STAT("rx_multicast", stats.eth.rx_multicast),
110         I40E_PF_STAT("tx_multicast", stats.eth.tx_multicast),
111         I40E_PF_STAT("rx_broadcast", stats.eth.rx_broadcast),
112         I40E_PF_STAT("tx_broadcast", stats.eth.tx_broadcast),
113         I40E_PF_STAT("tx_errors", stats.eth.tx_errors),
114         I40E_PF_STAT("rx_dropped", stats.eth.rx_discards),
115         I40E_PF_STAT("tx_dropped", stats.eth.tx_discards),
116         I40E_PF_STAT("tx_dropped_link_down", stats.tx_dropped_link_down),
117         I40E_PF_STAT("crc_errors", stats.crc_errors),
118         I40E_PF_STAT("illegal_bytes", stats.illegal_bytes),
119         I40E_PF_STAT("mac_local_faults", stats.mac_local_faults),
120         I40E_PF_STAT("mac_remote_faults", stats.mac_remote_faults),
121         I40E_PF_STAT("tx_timeout", tx_timeout_count),
122         I40E_PF_STAT("rx_csum_bad", hw_csum_rx_error),
123         I40E_PF_STAT("rx_length_errors", stats.rx_length_errors),
124         I40E_PF_STAT("link_xon_rx", stats.link_xon_rx),
125         I40E_PF_STAT("link_xoff_rx", stats.link_xoff_rx),
126         I40E_PF_STAT("link_xon_tx", stats.link_xon_tx),
127         I40E_PF_STAT("link_xoff_tx", stats.link_xoff_tx),
128         I40E_PF_STAT("rx_size_64", stats.rx_size_64),
129         I40E_PF_STAT("rx_size_127", stats.rx_size_127),
130         I40E_PF_STAT("rx_size_255", stats.rx_size_255),
131         I40E_PF_STAT("rx_size_511", stats.rx_size_511),
132         I40E_PF_STAT("rx_size_1023", stats.rx_size_1023),
133         I40E_PF_STAT("rx_size_1522", stats.rx_size_1522),
134         I40E_PF_STAT("rx_size_big", stats.rx_size_big),
135         I40E_PF_STAT("tx_size_64", stats.tx_size_64),
136         I40E_PF_STAT("tx_size_127", stats.tx_size_127),
137         I40E_PF_STAT("tx_size_255", stats.tx_size_255),
138         I40E_PF_STAT("tx_size_511", stats.tx_size_511),
139         I40E_PF_STAT("tx_size_1023", stats.tx_size_1023),
140         I40E_PF_STAT("tx_size_1522", stats.tx_size_1522),
141         I40E_PF_STAT("tx_size_big", stats.tx_size_big),
142         I40E_PF_STAT("rx_undersize", stats.rx_undersize),
143         I40E_PF_STAT("rx_fragments", stats.rx_fragments),
144         I40E_PF_STAT("rx_oversize", stats.rx_oversize),
145         I40E_PF_STAT("rx_jabber", stats.rx_jabber),
146         I40E_PF_STAT("VF_admin_queue_requests", vf_aq_requests),
147         I40E_PF_STAT("rx_hwtstamp_cleared", rx_hwtstamp_cleared),
148         I40E_PF_STAT("fdir_flush_cnt", fd_flush_cnt),
149         I40E_PF_STAT("fdir_atr_match", stats.fd_atr_match),
150         I40E_PF_STAT("fdir_sb_match", stats.fd_sb_match),
151
152         /* LPI stats */
153         I40E_PF_STAT("tx_lpi_status", stats.tx_lpi_status),
154         I40E_PF_STAT("rx_lpi_status", stats.rx_lpi_status),
155         I40E_PF_STAT("tx_lpi_count", stats.tx_lpi_count),
156         I40E_PF_STAT("rx_lpi_count", stats.rx_lpi_count),
157 };
158
159 #ifdef I40E_FCOE
160 static const struct i40e_stats i40e_gstrings_fcoe_stats[] = {
161         I40E_VSI_STAT("fcoe_bad_fccrc", fcoe_stats.fcoe_bad_fccrc),
162         I40E_VSI_STAT("rx_fcoe_dropped", fcoe_stats.rx_fcoe_dropped),
163         I40E_VSI_STAT("rx_fcoe_packets", fcoe_stats.rx_fcoe_packets),
164         I40E_VSI_STAT("rx_fcoe_dwords", fcoe_stats.rx_fcoe_dwords),
165         I40E_VSI_STAT("fcoe_ddp_count", fcoe_stats.fcoe_ddp_count),
166         I40E_VSI_STAT("fcoe_last_error", fcoe_stats.fcoe_last_error),
167         I40E_VSI_STAT("tx_fcoe_packets", fcoe_stats.tx_fcoe_packets),
168         I40E_VSI_STAT("tx_fcoe_dwords", fcoe_stats.tx_fcoe_dwords),
169 };
170
171 #endif /* I40E_FCOE */
172 #define I40E_QUEUE_STATS_LEN(n) \
173         (((struct i40e_netdev_priv *)netdev_priv((n)))->vsi->num_queue_pairs \
174             * 2 /* Tx and Rx together */                                     \
175             * (sizeof(struct i40e_queue_stats) / sizeof(u64)))
176 #define I40E_GLOBAL_STATS_LEN   ARRAY_SIZE(i40e_gstrings_stats)
177 #define I40E_NETDEV_STATS_LEN   ARRAY_SIZE(i40e_gstrings_net_stats)
178 #define I40E_MISC_STATS_LEN     ARRAY_SIZE(i40e_gstrings_misc_stats)
179 #ifdef I40E_FCOE
180 #define I40E_FCOE_STATS_LEN     ARRAY_SIZE(i40e_gstrings_fcoe_stats)
181 #define I40E_VSI_STATS_LEN(n)   (I40E_NETDEV_STATS_LEN + \
182                                  I40E_FCOE_STATS_LEN + \
183                                  I40E_MISC_STATS_LEN + \
184                                  I40E_QUEUE_STATS_LEN((n)))
185 #else
186 #define I40E_VSI_STATS_LEN(n)   (I40E_NETDEV_STATS_LEN + \
187                                  I40E_MISC_STATS_LEN + \
188                                  I40E_QUEUE_STATS_LEN((n)))
189 #endif /* I40E_FCOE */
190 #define I40E_PFC_STATS_LEN ( \
191                 (FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_rx) + \
192                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_rx) + \
193                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_tx) + \
194                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_tx) + \
195                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_2_xoff)) \
196                  / sizeof(u64))
197 #define I40E_VEB_STATS_LEN      ARRAY_SIZE(i40e_gstrings_veb_stats)
198 #define I40E_PF_STATS_LEN(n)    (I40E_GLOBAL_STATS_LEN + \
199                                  I40E_PFC_STATS_LEN + \
200                                  I40E_VSI_STATS_LEN((n)))
201
202 enum i40e_ethtool_test_id {
203         I40E_ETH_TEST_REG = 0,
204         I40E_ETH_TEST_EEPROM,
205         I40E_ETH_TEST_INTR,
206         I40E_ETH_TEST_LOOPBACK,
207         I40E_ETH_TEST_LINK,
208 };
209
210 static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
211         "Register test  (offline)",
212         "Eeprom test    (offline)",
213         "Interrupt test (offline)",
214         "Loopback test  (offline)",
215         "Link test   (on/offline)"
216 };
217
218 #define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
219
220 /**
221  * i40e_get_settings - Get Link Speed and Duplex settings
222  * @netdev: network interface device structure
223  * @ecmd: ethtool command
224  *
225  * Reports speed/duplex settings based on media_type
226  **/
227 static int i40e_get_settings(struct net_device *netdev,
228                              struct ethtool_cmd *ecmd)
229 {
230         struct i40e_netdev_priv *np = netdev_priv(netdev);
231         struct i40e_pf *pf = np->vsi->back;
232         struct i40e_hw *hw = &pf->hw;
233         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
234         bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
235         u32 link_speed = hw_link_info->link_speed;
236
237         /* hardware is either in 40G mode or 10G mode
238          * NOTE: this section initializes supported and advertising
239          */
240         if (!link_up) {
241                 /* link is down and the driver needs to fall back on
242                  * device ID to determine what kinds of info to display,
243                  * it's mostly a guess that may change when link is up
244                  */
245                 switch (hw->device_id) {
246                 case I40E_DEV_ID_QSFP_A:
247                 case I40E_DEV_ID_QSFP_B:
248                 case I40E_DEV_ID_QSFP_C:
249                         /* pluggable QSFP */
250                         ecmd->supported = SUPPORTED_40000baseSR4_Full |
251                                           SUPPORTED_40000baseCR4_Full |
252                                           SUPPORTED_40000baseLR4_Full;
253                         ecmd->advertising = ADVERTISED_40000baseSR4_Full |
254                                             ADVERTISED_40000baseCR4_Full |
255                                             ADVERTISED_40000baseLR4_Full;
256                         break;
257                 case I40E_DEV_ID_KX_B:
258                         /* backplane 40G */
259                         ecmd->supported = SUPPORTED_40000baseKR4_Full;
260                         ecmd->advertising = ADVERTISED_40000baseKR4_Full;
261                         break;
262                 case I40E_DEV_ID_KX_C:
263                         /* backplane 10G */
264                         ecmd->supported = SUPPORTED_10000baseKR_Full;
265                         ecmd->advertising = ADVERTISED_10000baseKR_Full;
266                         break;
267                 default:
268                         /* all the rest are 10G/1G */
269                         ecmd->supported = SUPPORTED_10000baseT_Full |
270                                           SUPPORTED_1000baseT_Full;
271                         ecmd->advertising = ADVERTISED_10000baseT_Full |
272                                             ADVERTISED_1000baseT_Full;
273                         break;
274                 }
275
276                 /* skip phy_type use as it is zero when link is down */
277                 goto no_valid_phy_type;
278         }
279
280         switch (hw_link_info->phy_type) {
281         case I40E_PHY_TYPE_40GBASE_CR4:
282         case I40E_PHY_TYPE_40GBASE_CR4_CU:
283                 ecmd->supported = SUPPORTED_Autoneg |
284                                   SUPPORTED_40000baseCR4_Full;
285                 ecmd->advertising = ADVERTISED_Autoneg |
286                                     ADVERTISED_40000baseCR4_Full;
287                 break;
288         case I40E_PHY_TYPE_40GBASE_KR4:
289                 ecmd->supported = SUPPORTED_Autoneg |
290                                   SUPPORTED_40000baseKR4_Full;
291                 ecmd->advertising = ADVERTISED_Autoneg |
292                                     ADVERTISED_40000baseKR4_Full;
293                 break;
294         case I40E_PHY_TYPE_40GBASE_SR4:
295         case I40E_PHY_TYPE_XLPPI:
296         case I40E_PHY_TYPE_XLAUI:
297                 ecmd->supported = SUPPORTED_40000baseSR4_Full;
298                 break;
299         case I40E_PHY_TYPE_40GBASE_LR4:
300                 ecmd->supported = SUPPORTED_40000baseLR4_Full;
301                 break;
302         case I40E_PHY_TYPE_10GBASE_KX4:
303                 ecmd->supported = SUPPORTED_Autoneg |
304                                   SUPPORTED_10000baseKX4_Full;
305                 ecmd->advertising = ADVERTISED_Autoneg |
306                                     ADVERTISED_10000baseKX4_Full;
307                 break;
308         case I40E_PHY_TYPE_10GBASE_KR:
309                 ecmd->supported = SUPPORTED_Autoneg |
310                                   SUPPORTED_10000baseKR_Full;
311                 ecmd->advertising = ADVERTISED_Autoneg |
312                                     ADVERTISED_10000baseKR_Full;
313                 break;
314         case I40E_PHY_TYPE_10GBASE_SR:
315         case I40E_PHY_TYPE_10GBASE_LR:
316         case I40E_PHY_TYPE_1000BASE_SX:
317         case I40E_PHY_TYPE_1000BASE_LX:
318                 ecmd->supported = SUPPORTED_10000baseT_Full;
319                 ecmd->supported |= SUPPORTED_1000baseT_Full;
320                 break;
321         case I40E_PHY_TYPE_10GBASE_CR1_CU:
322         case I40E_PHY_TYPE_10GBASE_CR1:
323         case I40E_PHY_TYPE_10GBASE_T:
324                 ecmd->supported = SUPPORTED_Autoneg |
325                                   SUPPORTED_10000baseT_Full;
326                 ecmd->advertising = ADVERTISED_Autoneg |
327                                     ADVERTISED_10000baseT_Full;
328                 break;
329         case I40E_PHY_TYPE_XAUI:
330         case I40E_PHY_TYPE_XFI:
331         case I40E_PHY_TYPE_SFI:
332         case I40E_PHY_TYPE_10GBASE_SFPP_CU:
333                 ecmd->supported = SUPPORTED_10000baseT_Full;
334                 break;
335         case I40E_PHY_TYPE_1000BASE_KX:
336         case I40E_PHY_TYPE_1000BASE_T:
337                 ecmd->supported = SUPPORTED_Autoneg |
338                                   SUPPORTED_1000baseT_Full;
339                 ecmd->advertising = ADVERTISED_Autoneg |
340                                     ADVERTISED_1000baseT_Full;
341                 break;
342         case I40E_PHY_TYPE_100BASE_TX:
343                 ecmd->supported = SUPPORTED_Autoneg |
344                                   SUPPORTED_100baseT_Full;
345                 ecmd->advertising = ADVERTISED_Autoneg |
346                                     ADVERTISED_100baseT_Full;
347                 break;
348         case I40E_PHY_TYPE_SGMII:
349                 ecmd->supported = SUPPORTED_Autoneg |
350                                   SUPPORTED_1000baseT_Full |
351                                   SUPPORTED_100baseT_Full;
352                 ecmd->advertising = ADVERTISED_Autoneg |
353                                     ADVERTISED_1000baseT_Full |
354                                     ADVERTISED_100baseT_Full;
355                 break;
356         default:
357                 /* if we got here and link is up something bad is afoot */
358                 netdev_info(netdev, "WARNING: Link is up but PHY type 0x%x is not recognized.\n",
359                             hw_link_info->phy_type);
360         }
361
362 no_valid_phy_type:
363         /* this is if autoneg is enabled or disabled */
364         ecmd->autoneg = ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
365                           AUTONEG_ENABLE : AUTONEG_DISABLE);
366
367         switch (hw->phy.media_type) {
368         case I40E_MEDIA_TYPE_BACKPLANE:
369                 ecmd->supported |= SUPPORTED_Autoneg |
370                                    SUPPORTED_Backplane;
371                 ecmd->advertising |= ADVERTISED_Autoneg |
372                                      ADVERTISED_Backplane;
373                 ecmd->port = PORT_NONE;
374                 break;
375         case I40E_MEDIA_TYPE_BASET:
376                 ecmd->supported |= SUPPORTED_TP;
377                 ecmd->advertising |= ADVERTISED_TP;
378                 ecmd->port = PORT_TP;
379                 break;
380         case I40E_MEDIA_TYPE_DA:
381         case I40E_MEDIA_TYPE_CX4:
382                 ecmd->supported |= SUPPORTED_FIBRE;
383                 ecmd->advertising |= ADVERTISED_FIBRE;
384                 ecmd->port = PORT_DA;
385                 break;
386         case I40E_MEDIA_TYPE_FIBER:
387                 ecmd->supported |= SUPPORTED_FIBRE;
388                 ecmd->port = PORT_FIBRE;
389                 break;
390         case I40E_MEDIA_TYPE_UNKNOWN:
391         default:
392                 ecmd->port = PORT_OTHER;
393                 break;
394         }
395
396         ecmd->transceiver = XCVR_EXTERNAL;
397
398         ecmd->supported |= SUPPORTED_Pause;
399
400         switch (hw->fc.current_mode) {
401         case I40E_FC_FULL:
402                 ecmd->advertising |= ADVERTISED_Pause;
403                 break;
404         case I40E_FC_TX_PAUSE:
405                 ecmd->advertising |= ADVERTISED_Asym_Pause;
406                 break;
407         case I40E_FC_RX_PAUSE:
408                 ecmd->advertising |= (ADVERTISED_Pause |
409                                       ADVERTISED_Asym_Pause);
410                 break;
411         default:
412                 ecmd->advertising &= ~(ADVERTISED_Pause |
413                                        ADVERTISED_Asym_Pause);
414                 break;
415         }
416
417         if (link_up) {
418                 switch (link_speed) {
419                 case I40E_LINK_SPEED_40GB:
420                         /* need a SPEED_40000 in ethtool.h */
421                         ethtool_cmd_speed_set(ecmd, 40000);
422                         break;
423                 case I40E_LINK_SPEED_10GB:
424                         ethtool_cmd_speed_set(ecmd, SPEED_10000);
425                         break;
426                 case I40E_LINK_SPEED_1GB:
427                         ethtool_cmd_speed_set(ecmd, SPEED_1000);
428                         break;
429                 default:
430                         break;
431                 }
432                 ecmd->duplex = DUPLEX_FULL;
433         } else {
434                 ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
435                 ecmd->duplex = DUPLEX_UNKNOWN;
436         }
437
438         return 0;
439 }
440
441 /**
442  * i40e_set_settings - Set Speed and Duplex
443  * @netdev: network interface device structure
444  * @ecmd: ethtool command
445  *
446  * Set speed/duplex per media_types advertised/forced
447  **/
448 static int i40e_set_settings(struct net_device *netdev,
449                              struct ethtool_cmd *ecmd)
450 {
451         struct i40e_netdev_priv *np = netdev_priv(netdev);
452         struct i40e_aq_get_phy_abilities_resp abilities;
453         struct i40e_aq_set_phy_config config;
454         struct i40e_pf *pf = np->vsi->back;
455         struct i40e_vsi *vsi = np->vsi;
456         struct i40e_hw *hw = &pf->hw;
457         struct ethtool_cmd safe_ecmd;
458         i40e_status status = 0;
459         bool change = false;
460         int err = 0;
461         u8 autoneg;
462         u32 advertise;
463
464         if (vsi != pf->vsi[pf->lan_vsi])
465                 return -EOPNOTSUPP;
466
467         if (hw->phy.media_type != I40E_MEDIA_TYPE_BASET &&
468             hw->phy.media_type != I40E_MEDIA_TYPE_FIBER &&
469             hw->phy.media_type != I40E_MEDIA_TYPE_BACKPLANE &&
470             hw->phy.link_info.link_info & I40E_AQ_LINK_UP)
471                 return -EOPNOTSUPP;
472
473         /* get our own copy of the bits to check against */
474         memset(&safe_ecmd, 0, sizeof(struct ethtool_cmd));
475         i40e_get_settings(netdev, &safe_ecmd);
476
477         /* save autoneg and speed out of ecmd */
478         autoneg = ecmd->autoneg;
479         advertise = ecmd->advertising;
480
481         /* set autoneg and speed back to what they currently are */
482         ecmd->autoneg = safe_ecmd.autoneg;
483         ecmd->advertising = safe_ecmd.advertising;
484
485         ecmd->cmd = safe_ecmd.cmd;
486         /* If ecmd and safe_ecmd are not the same now, then they are
487          * trying to set something that we do not support
488          */
489         if (memcmp(ecmd, &safe_ecmd, sizeof(struct ethtool_cmd)))
490                 return -EOPNOTSUPP;
491
492         while (test_bit(__I40E_CONFIG_BUSY, &vsi->state))
493                 usleep_range(1000, 2000);
494
495         /* Get the current phy config */
496         status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
497                                               NULL);
498         if (status)
499                 return -EAGAIN;
500
501         /* Copy abilities to config in case autoneg is not
502          * set below
503          */
504         memset(&config, 0, sizeof(struct i40e_aq_set_phy_config));
505         config.abilities = abilities.abilities;
506
507         /* Check autoneg */
508         if (autoneg == AUTONEG_ENABLE) {
509                 /* If autoneg is not supported, return error */
510                 if (!(safe_ecmd.supported & SUPPORTED_Autoneg)) {
511                         netdev_info(netdev, "Autoneg not supported on this phy\n");
512                         return -EINVAL;
513                 }
514                 /* If autoneg was not already enabled */
515                 if (!(hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED)) {
516                         config.abilities = abilities.abilities |
517                                            I40E_AQ_PHY_ENABLE_AN;
518                         change = true;
519                 }
520         } else {
521                 /* If autoneg is supported 10GBASE_T is the only phy that
522                  * can disable it, so otherwise return error
523                  */
524                 if (safe_ecmd.supported & SUPPORTED_Autoneg &&
525                     hw->phy.link_info.phy_type != I40E_PHY_TYPE_10GBASE_T) {
526                         netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
527                         return -EINVAL;
528                 }
529                 /* If autoneg is currently enabled */
530                 if (hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED) {
531                         config.abilities = abilities.abilities |
532                                            ~I40E_AQ_PHY_ENABLE_AN;
533                         change = true;
534                 }
535         }
536
537         if (advertise & ~safe_ecmd.supported)
538                 return -EINVAL;
539
540         if (advertise & ADVERTISED_100baseT_Full)
541                 config.link_speed |= I40E_LINK_SPEED_100MB;
542         if (advertise & ADVERTISED_1000baseT_Full ||
543             advertise & ADVERTISED_1000baseKX_Full)
544                 config.link_speed |= I40E_LINK_SPEED_1GB;
545         if (advertise & ADVERTISED_10000baseT_Full ||
546             advertise & ADVERTISED_10000baseKX4_Full ||
547             advertise & ADVERTISED_10000baseKR_Full)
548                 config.link_speed |= I40E_LINK_SPEED_10GB;
549         if (advertise & ADVERTISED_40000baseKR4_Full ||
550             advertise & ADVERTISED_40000baseCR4_Full ||
551             advertise & ADVERTISED_40000baseSR4_Full ||
552             advertise & ADVERTISED_40000baseLR4_Full)
553                 config.link_speed |= I40E_LINK_SPEED_40GB;
554
555         if (change || (abilities.link_speed != config.link_speed)) {
556                 /* copy over the rest of the abilities */
557                 config.phy_type = abilities.phy_type;
558                 config.eee_capability = abilities.eee_capability;
559                 config.eeer = abilities.eeer_val;
560                 config.low_power_ctrl = abilities.d3_lpan;
561
562                 /* If link is up set link and an so changes take effect */
563                 if (hw->phy.link_info.link_info & I40E_AQ_LINK_UP)
564                         config.abilities |= I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
565
566                 /* make the aq call */
567                 status = i40e_aq_set_phy_config(hw, &config, NULL);
568                 if (status) {
569                         netdev_info(netdev, "Set phy config failed with error %d.\n",
570                                     status);
571                         return -EAGAIN;
572                 }
573
574                 status = i40e_update_link_info(hw, true);
575                 if (status)
576                         netdev_info(netdev, "Updating link info failed with error %d\n",
577                                     status);
578
579         } else {
580                 netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
581         }
582
583         return err;
584 }
585
586 static int i40e_nway_reset(struct net_device *netdev)
587 {
588         /* restart autonegotiation */
589         struct i40e_netdev_priv *np = netdev_priv(netdev);
590         struct i40e_pf *pf = np->vsi->back;
591         struct i40e_hw *hw = &pf->hw;
592         bool link_up = hw->phy.link_info.link_info & I40E_AQ_LINK_UP;
593         i40e_status ret = 0;
594
595         ret = i40e_aq_set_link_restart_an(hw, link_up, NULL);
596         if (ret) {
597                 netdev_info(netdev, "link restart failed, aq_err=%d\n",
598                             pf->hw.aq.asq_last_status);
599                 return -EIO;
600         }
601
602         return 0;
603 }
604
605 /**
606  * i40e_get_pauseparam -  Get Flow Control status
607  * Return tx/rx-pause status
608  **/
609 static void i40e_get_pauseparam(struct net_device *netdev,
610                                 struct ethtool_pauseparam *pause)
611 {
612         struct i40e_netdev_priv *np = netdev_priv(netdev);
613         struct i40e_pf *pf = np->vsi->back;
614         struct i40e_hw *hw = &pf->hw;
615         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
616
617         pause->autoneg =
618                 ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
619                   AUTONEG_ENABLE : AUTONEG_DISABLE);
620
621         if (hw->fc.current_mode == I40E_FC_RX_PAUSE) {
622                 pause->rx_pause = 1;
623         } else if (hw->fc.current_mode == I40E_FC_TX_PAUSE) {
624                 pause->tx_pause = 1;
625         } else if (hw->fc.current_mode == I40E_FC_FULL) {
626                 pause->rx_pause = 1;
627                 pause->tx_pause = 1;
628         }
629 }
630
631 /**
632  * i40e_set_pauseparam - Set Flow Control parameter
633  * @netdev: network interface device structure
634  * @pause: return tx/rx flow control status
635  **/
636 static int i40e_set_pauseparam(struct net_device *netdev,
637                                struct ethtool_pauseparam *pause)
638 {
639         struct i40e_netdev_priv *np = netdev_priv(netdev);
640         struct i40e_pf *pf = np->vsi->back;
641         struct i40e_vsi *vsi = np->vsi;
642         struct i40e_hw *hw = &pf->hw;
643         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
644         bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
645         i40e_status status;
646         u8 aq_failures;
647         int err = 0;
648
649         if (vsi != pf->vsi[pf->lan_vsi])
650                 return -EOPNOTSUPP;
651
652         if (pause->autoneg != ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
653             AUTONEG_ENABLE : AUTONEG_DISABLE)) {
654                 netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
655                 return -EOPNOTSUPP;
656         }
657
658         /* If we have link and don't have autoneg */
659         if (!test_bit(__I40E_DOWN, &pf->state) &&
660             !(hw_link_info->an_info & I40E_AQ_AN_COMPLETED)) {
661                 /* Send message that it might not necessarily work*/
662                 netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
663         }
664
665         if (hw->fc.current_mode == I40E_FC_PFC) {
666                 netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
667                 return -EOPNOTSUPP;
668         }
669
670         if (pause->rx_pause && pause->tx_pause)
671                 hw->fc.requested_mode = I40E_FC_FULL;
672         else if (pause->rx_pause && !pause->tx_pause)
673                 hw->fc.requested_mode = I40E_FC_RX_PAUSE;
674         else if (!pause->rx_pause && pause->tx_pause)
675                 hw->fc.requested_mode = I40E_FC_TX_PAUSE;
676         else if (!pause->rx_pause && !pause->tx_pause)
677                 hw->fc.requested_mode = I40E_FC_NONE;
678         else
679                  return -EINVAL;
680
681         /* Set the fc mode and only restart an if link is up*/
682         status = i40e_set_fc(hw, &aq_failures, link_up);
683
684         if (aq_failures & I40E_SET_FC_AQ_FAIL_GET) {
685                 netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with error %d and status %d\n",
686                             status, hw->aq.asq_last_status);
687                 err = -EAGAIN;
688         }
689         if (aq_failures & I40E_SET_FC_AQ_FAIL_SET) {
690                 netdev_info(netdev, "Set fc failed on the set_phy_config call with error %d and status %d\n",
691                             status, hw->aq.asq_last_status);
692                 err = -EAGAIN;
693         }
694         if (aq_failures & I40E_SET_FC_AQ_FAIL_UPDATE) {
695                 netdev_info(netdev, "Set fc failed on the update_link_info call with error %d and status %d\n",
696                             status, hw->aq.asq_last_status);
697                 err = -EAGAIN;
698         }
699
700         if (!test_bit(__I40E_DOWN, &pf->state)) {
701                 /* Give it a little more time to try to come back */
702                 msleep(75);
703                 if (!test_bit(__I40E_DOWN, &pf->state))
704                         return i40e_nway_reset(netdev);
705         }
706
707         return err;
708 }
709
710 static u32 i40e_get_msglevel(struct net_device *netdev)
711 {
712         struct i40e_netdev_priv *np = netdev_priv(netdev);
713         struct i40e_pf *pf = np->vsi->back;
714
715         return pf->msg_enable;
716 }
717
718 static void i40e_set_msglevel(struct net_device *netdev, u32 data)
719 {
720         struct i40e_netdev_priv *np = netdev_priv(netdev);
721         struct i40e_pf *pf = np->vsi->back;
722
723         if (I40E_DEBUG_USER & data)
724                 pf->hw.debug_mask = data;
725         pf->msg_enable = data;
726 }
727
728 static int i40e_get_regs_len(struct net_device *netdev)
729 {
730         int reg_count = 0;
731         int i;
732
733         for (i = 0; i40e_reg_list[i].offset != 0; i++)
734                 reg_count += i40e_reg_list[i].elements;
735
736         return reg_count * sizeof(u32);
737 }
738
739 static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
740                           void *p)
741 {
742         struct i40e_netdev_priv *np = netdev_priv(netdev);
743         struct i40e_pf *pf = np->vsi->back;
744         struct i40e_hw *hw = &pf->hw;
745         u32 *reg_buf = p;
746         int i, j, ri;
747         u32 reg;
748
749         /* Tell ethtool which driver-version-specific regs output we have.
750          *
751          * At some point, if we have ethtool doing special formatting of
752          * this data, it will rely on this version number to know how to
753          * interpret things.  Hence, this needs to be updated if/when the
754          * diags register table is changed.
755          */
756         regs->version = 1;
757
758         /* loop through the diags reg table for what to print */
759         ri = 0;
760         for (i = 0; i40e_reg_list[i].offset != 0; i++) {
761                 for (j = 0; j < i40e_reg_list[i].elements; j++) {
762                         reg = i40e_reg_list[i].offset
763                                 + (j * i40e_reg_list[i].stride);
764                         reg_buf[ri++] = rd32(hw, reg);
765                 }
766         }
767
768 }
769
770 static int i40e_get_eeprom(struct net_device *netdev,
771                            struct ethtool_eeprom *eeprom, u8 *bytes)
772 {
773         struct i40e_netdev_priv *np = netdev_priv(netdev);
774         struct i40e_hw *hw = &np->vsi->back->hw;
775         struct i40e_pf *pf = np->vsi->back;
776         int ret_val = 0, len;
777         u8 *eeprom_buff;
778         u16 i, sectors;
779         bool last;
780         u32 magic;
781
782 #define I40E_NVM_SECTOR_SIZE  4096
783         if (eeprom->len == 0)
784                 return -EINVAL;
785
786         /* check for NVMUpdate access method */
787         magic = hw->vendor_id | (hw->device_id << 16);
788         if (eeprom->magic && eeprom->magic != magic) {
789                 int errno;
790
791                 /* make sure it is the right magic for NVMUpdate */
792                 if ((eeprom->magic >> 16) != hw->device_id)
793                         return -EINVAL;
794
795                 ret_val = i40e_nvmupd_command(hw,
796                                               (struct i40e_nvm_access *)eeprom,
797                                               bytes, &errno);
798                 if (ret_val)
799                         dev_info(&pf->pdev->dev,
800                                  "NVMUpdate read failed err=%d status=0x%x\n",
801                                  ret_val, hw->aq.asq_last_status);
802
803                 return errno;
804         }
805
806         /* normal ethtool get_eeprom support */
807         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
808
809         eeprom_buff = kzalloc(eeprom->len, GFP_KERNEL);
810         if (!eeprom_buff)
811                 return -ENOMEM;
812
813         ret_val = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
814         if (ret_val) {
815                 dev_info(&pf->pdev->dev,
816                          "Failed Acquiring NVM resource for read err=%d status=0x%x\n",
817                          ret_val, hw->aq.asq_last_status);
818                 goto free_buff;
819         }
820
821         sectors = eeprom->len / I40E_NVM_SECTOR_SIZE;
822         sectors += (eeprom->len % I40E_NVM_SECTOR_SIZE) ? 1 : 0;
823         len = I40E_NVM_SECTOR_SIZE;
824         last = false;
825         for (i = 0; i < sectors; i++) {
826                 if (i == (sectors - 1)) {
827                         len = eeprom->len - (I40E_NVM_SECTOR_SIZE * i);
828                         last = true;
829                 }
830                 ret_val = i40e_aq_read_nvm(hw, 0x0,
831                                 eeprom->offset + (I40E_NVM_SECTOR_SIZE * i),
832                                 len,
833                                 (u8 *)eeprom_buff + (I40E_NVM_SECTOR_SIZE * i),
834                                 last, NULL);
835                 if (ret_val) {
836                         dev_info(&pf->pdev->dev,
837                                  "read NVM failed err=%d status=0x%x\n",
838                                  ret_val, hw->aq.asq_last_status);
839                         goto release_nvm;
840                 }
841         }
842
843 release_nvm:
844         i40e_release_nvm(hw);
845         memcpy(bytes, (u8 *)eeprom_buff, eeprom->len);
846 free_buff:
847         kfree(eeprom_buff);
848         return ret_val;
849 }
850
851 static int i40e_get_eeprom_len(struct net_device *netdev)
852 {
853         struct i40e_netdev_priv *np = netdev_priv(netdev);
854         struct i40e_hw *hw = &np->vsi->back->hw;
855         u32 val;
856
857         val = (rd32(hw, I40E_GLPCI_LBARCTRL)
858                 & I40E_GLPCI_LBARCTRL_FL_SIZE_MASK)
859                 >> I40E_GLPCI_LBARCTRL_FL_SIZE_SHIFT;
860         /* register returns value in power of 2, 64Kbyte chunks. */
861         val = (64 * 1024) * (1 << val);
862         return val;
863 }
864
865 static int i40e_set_eeprom(struct net_device *netdev,
866                            struct ethtool_eeprom *eeprom, u8 *bytes)
867 {
868         struct i40e_netdev_priv *np = netdev_priv(netdev);
869         struct i40e_hw *hw = &np->vsi->back->hw;
870         struct i40e_pf *pf = np->vsi->back;
871         int ret_val = 0;
872         int errno;
873         u32 magic;
874
875         /* normal ethtool set_eeprom is not supported */
876         magic = hw->vendor_id | (hw->device_id << 16);
877         if (eeprom->magic == magic)
878                 return -EOPNOTSUPP;
879
880         /* check for NVMUpdate access method */
881         if (!eeprom->magic || (eeprom->magic >> 16) != hw->device_id)
882                 return -EINVAL;
883
884         if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
885             test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
886                 return -EBUSY;
887
888         ret_val = i40e_nvmupd_command(hw, (struct i40e_nvm_access *)eeprom,
889                                       bytes, &errno);
890         if (ret_val)
891                 dev_info(&pf->pdev->dev,
892                          "NVMUpdate write failed err=%d status=0x%x\n",
893                          ret_val, hw->aq.asq_last_status);
894
895         return errno;
896 }
897
898 static void i40e_get_drvinfo(struct net_device *netdev,
899                              struct ethtool_drvinfo *drvinfo)
900 {
901         struct i40e_netdev_priv *np = netdev_priv(netdev);
902         struct i40e_vsi *vsi = np->vsi;
903         struct i40e_pf *pf = vsi->back;
904
905         strlcpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
906         strlcpy(drvinfo->version, i40e_driver_version_str,
907                 sizeof(drvinfo->version));
908         strlcpy(drvinfo->fw_version, i40e_fw_version_str(&pf->hw),
909                 sizeof(drvinfo->fw_version));
910         strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
911                 sizeof(drvinfo->bus_info));
912 }
913
914 static void i40e_get_ringparam(struct net_device *netdev,
915                                struct ethtool_ringparam *ring)
916 {
917         struct i40e_netdev_priv *np = netdev_priv(netdev);
918         struct i40e_pf *pf = np->vsi->back;
919         struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
920
921         ring->rx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
922         ring->tx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
923         ring->rx_mini_max_pending = 0;
924         ring->rx_jumbo_max_pending = 0;
925         ring->rx_pending = vsi->rx_rings[0]->count;
926         ring->tx_pending = vsi->tx_rings[0]->count;
927         ring->rx_mini_pending = 0;
928         ring->rx_jumbo_pending = 0;
929 }
930
931 static int i40e_set_ringparam(struct net_device *netdev,
932                               struct ethtool_ringparam *ring)
933 {
934         struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
935         struct i40e_netdev_priv *np = netdev_priv(netdev);
936         struct i40e_vsi *vsi = np->vsi;
937         struct i40e_pf *pf = vsi->back;
938         u32 new_rx_count, new_tx_count;
939         int i, err = 0;
940
941         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
942                 return -EINVAL;
943
944         if (ring->tx_pending > I40E_MAX_NUM_DESCRIPTORS ||
945             ring->tx_pending < I40E_MIN_NUM_DESCRIPTORS ||
946             ring->rx_pending > I40E_MAX_NUM_DESCRIPTORS ||
947             ring->rx_pending < I40E_MIN_NUM_DESCRIPTORS) {
948                 netdev_info(netdev,
949                             "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d]\n",
950                             ring->tx_pending, ring->rx_pending,
951                             I40E_MIN_NUM_DESCRIPTORS, I40E_MAX_NUM_DESCRIPTORS);
952                 return -EINVAL;
953         }
954
955         new_tx_count = ALIGN(ring->tx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
956         new_rx_count = ALIGN(ring->rx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
957
958         /* if nothing to do return success */
959         if ((new_tx_count == vsi->tx_rings[0]->count) &&
960             (new_rx_count == vsi->rx_rings[0]->count))
961                 return 0;
962
963         while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
964                 usleep_range(1000, 2000);
965
966         if (!netif_running(vsi->netdev)) {
967                 /* simple case - set for the next time the netdev is started */
968                 for (i = 0; i < vsi->num_queue_pairs; i++) {
969                         vsi->tx_rings[i]->count = new_tx_count;
970                         vsi->rx_rings[i]->count = new_rx_count;
971                 }
972                 goto done;
973         }
974
975         /* We can't just free everything and then setup again,
976          * because the ISRs in MSI-X mode get passed pointers
977          * to the Tx and Rx ring structs.
978          */
979
980         /* alloc updated Tx resources */
981         if (new_tx_count != vsi->tx_rings[0]->count) {
982                 netdev_info(netdev,
983                             "Changing Tx descriptor count from %d to %d.\n",
984                             vsi->tx_rings[0]->count, new_tx_count);
985                 tx_rings = kcalloc(vsi->alloc_queue_pairs,
986                                    sizeof(struct i40e_ring), GFP_KERNEL);
987                 if (!tx_rings) {
988                         err = -ENOMEM;
989                         goto done;
990                 }
991
992                 for (i = 0; i < vsi->num_queue_pairs; i++) {
993                         /* clone ring and setup updated count */
994                         tx_rings[i] = *vsi->tx_rings[i];
995                         tx_rings[i].count = new_tx_count;
996                         err = i40e_setup_tx_descriptors(&tx_rings[i]);
997                         if (err) {
998                                 while (i) {
999                                         i--;
1000                                         i40e_free_tx_resources(&tx_rings[i]);
1001                                 }
1002                                 kfree(tx_rings);
1003                                 tx_rings = NULL;
1004
1005                                 goto done;
1006                         }
1007                 }
1008         }
1009
1010         /* alloc updated Rx resources */
1011         if (new_rx_count != vsi->rx_rings[0]->count) {
1012                 netdev_info(netdev,
1013                             "Changing Rx descriptor count from %d to %d\n",
1014                             vsi->rx_rings[0]->count, new_rx_count);
1015                 rx_rings = kcalloc(vsi->alloc_queue_pairs,
1016                                    sizeof(struct i40e_ring), GFP_KERNEL);
1017                 if (!rx_rings) {
1018                         err = -ENOMEM;
1019                         goto free_tx;
1020                 }
1021
1022                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1023                         /* clone ring and setup updated count */
1024                         rx_rings[i] = *vsi->rx_rings[i];
1025                         rx_rings[i].count = new_rx_count;
1026                         err = i40e_setup_rx_descriptors(&rx_rings[i]);
1027                         if (err) {
1028                                 while (i) {
1029                                         i--;
1030                                         i40e_free_rx_resources(&rx_rings[i]);
1031                                 }
1032                                 kfree(rx_rings);
1033                                 rx_rings = NULL;
1034
1035                                 goto free_tx;
1036                         }
1037                 }
1038         }
1039
1040         /* Bring interface down, copy in the new ring info,
1041          * then restore the interface
1042          */
1043         i40e_down(vsi);
1044
1045         if (tx_rings) {
1046                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1047                         i40e_free_tx_resources(vsi->tx_rings[i]);
1048                         *vsi->tx_rings[i] = tx_rings[i];
1049                 }
1050                 kfree(tx_rings);
1051                 tx_rings = NULL;
1052         }
1053
1054         if (rx_rings) {
1055                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1056                         i40e_free_rx_resources(vsi->rx_rings[i]);
1057                         *vsi->rx_rings[i] = rx_rings[i];
1058                 }
1059                 kfree(rx_rings);
1060                 rx_rings = NULL;
1061         }
1062
1063         i40e_up(vsi);
1064
1065 free_tx:
1066         /* error cleanup if the Rx allocations failed after getting Tx */
1067         if (tx_rings) {
1068                 for (i = 0; i < vsi->num_queue_pairs; i++)
1069                         i40e_free_tx_resources(&tx_rings[i]);
1070                 kfree(tx_rings);
1071                 tx_rings = NULL;
1072         }
1073
1074 done:
1075         clear_bit(__I40E_CONFIG_BUSY, &pf->state);
1076
1077         return err;
1078 }
1079
1080 static int i40e_get_sset_count(struct net_device *netdev, int sset)
1081 {
1082         struct i40e_netdev_priv *np = netdev_priv(netdev);
1083         struct i40e_vsi *vsi = np->vsi;
1084         struct i40e_pf *pf = vsi->back;
1085
1086         switch (sset) {
1087         case ETH_SS_TEST:
1088                 return I40E_TEST_LEN;
1089         case ETH_SS_STATS:
1090                 if (vsi == pf->vsi[pf->lan_vsi]) {
1091                         int len = I40E_PF_STATS_LEN(netdev);
1092
1093                         if (pf->lan_veb != I40E_NO_VEB)
1094                                 len += I40E_VEB_STATS_LEN;
1095                         return len;
1096                 } else {
1097                         return I40E_VSI_STATS_LEN(netdev);
1098                 }
1099         default:
1100                 return -EOPNOTSUPP;
1101         }
1102 }
1103
1104 static void i40e_get_ethtool_stats(struct net_device *netdev,
1105                                    struct ethtool_stats *stats, u64 *data)
1106 {
1107         struct i40e_netdev_priv *np = netdev_priv(netdev);
1108         struct i40e_ring *tx_ring, *rx_ring;
1109         struct i40e_vsi *vsi = np->vsi;
1110         struct i40e_pf *pf = vsi->back;
1111         int i = 0;
1112         char *p;
1113         int j;
1114         struct rtnl_link_stats64 *net_stats = i40e_get_vsi_stats_struct(vsi);
1115         unsigned int start;
1116
1117         i40e_update_stats(vsi);
1118
1119         for (j = 0; j < I40E_NETDEV_STATS_LEN; j++) {
1120                 p = (char *)net_stats + i40e_gstrings_net_stats[j].stat_offset;
1121                 data[i++] = (i40e_gstrings_net_stats[j].sizeof_stat ==
1122                         sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1123         }
1124         for (j = 0; j < I40E_MISC_STATS_LEN; j++) {
1125                 p = (char *)vsi + i40e_gstrings_misc_stats[j].stat_offset;
1126                 data[i++] = (i40e_gstrings_misc_stats[j].sizeof_stat ==
1127                             sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1128         }
1129 #ifdef I40E_FCOE
1130         for (j = 0; j < I40E_FCOE_STATS_LEN; j++) {
1131                 p = (char *)vsi + i40e_gstrings_fcoe_stats[j].stat_offset;
1132                 data[i++] = (i40e_gstrings_fcoe_stats[j].sizeof_stat ==
1133                         sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1134         }
1135 #endif
1136         rcu_read_lock();
1137         for (j = 0; j < vsi->num_queue_pairs; j++) {
1138                 tx_ring = ACCESS_ONCE(vsi->tx_rings[j]);
1139
1140                 if (!tx_ring)
1141                         continue;
1142
1143                 /* process Tx ring statistics */
1144                 do {
1145                         start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
1146                         data[i] = tx_ring->stats.packets;
1147                         data[i + 1] = tx_ring->stats.bytes;
1148                 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
1149                 i += 2;
1150
1151                 /* Rx ring is the 2nd half of the queue pair */
1152                 rx_ring = &tx_ring[1];
1153                 do {
1154                         start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
1155                         data[i] = rx_ring->stats.packets;
1156                         data[i + 1] = rx_ring->stats.bytes;
1157                 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
1158                 i += 2;
1159         }
1160         rcu_read_unlock();
1161         if (vsi != pf->vsi[pf->lan_vsi])
1162                 return;
1163
1164         if (pf->lan_veb != I40E_NO_VEB) {
1165                 struct i40e_veb *veb = pf->veb[pf->lan_veb];
1166                 for (j = 0; j < I40E_VEB_STATS_LEN; j++) {
1167                         p = (char *)veb;
1168                         p += i40e_gstrings_veb_stats[j].stat_offset;
1169                         data[i++] = (i40e_gstrings_veb_stats[j].sizeof_stat ==
1170                                      sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1171                 }
1172         }
1173         for (j = 0; j < I40E_GLOBAL_STATS_LEN; j++) {
1174                 p = (char *)pf + i40e_gstrings_stats[j].stat_offset;
1175                 data[i++] = (i40e_gstrings_stats[j].sizeof_stat ==
1176                              sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1177         }
1178         for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
1179                 data[i++] = pf->stats.priority_xon_tx[j];
1180                 data[i++] = pf->stats.priority_xoff_tx[j];
1181         }
1182         for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
1183                 data[i++] = pf->stats.priority_xon_rx[j];
1184                 data[i++] = pf->stats.priority_xoff_rx[j];
1185         }
1186         for (j = 0; j < I40E_MAX_USER_PRIORITY; j++)
1187                 data[i++] = pf->stats.priority_xon_2_xoff[j];
1188 }
1189
1190 static void i40e_get_strings(struct net_device *netdev, u32 stringset,
1191                              u8 *data)
1192 {
1193         struct i40e_netdev_priv *np = netdev_priv(netdev);
1194         struct i40e_vsi *vsi = np->vsi;
1195         struct i40e_pf *pf = vsi->back;
1196         char *p = (char *)data;
1197         int i;
1198
1199         switch (stringset) {
1200         case ETH_SS_TEST:
1201                 for (i = 0; i < I40E_TEST_LEN; i++) {
1202                         memcpy(data, i40e_gstrings_test[i], ETH_GSTRING_LEN);
1203                         data += ETH_GSTRING_LEN;
1204                 }
1205                 break;
1206         case ETH_SS_STATS:
1207                 for (i = 0; i < I40E_NETDEV_STATS_LEN; i++) {
1208                         snprintf(p, ETH_GSTRING_LEN, "%s",
1209                                  i40e_gstrings_net_stats[i].stat_string);
1210                         p += ETH_GSTRING_LEN;
1211                 }
1212                 for (i = 0; i < I40E_MISC_STATS_LEN; i++) {
1213                         snprintf(p, ETH_GSTRING_LEN, "%s",
1214                                  i40e_gstrings_misc_stats[i].stat_string);
1215                         p += ETH_GSTRING_LEN;
1216                 }
1217 #ifdef I40E_FCOE
1218                 for (i = 0; i < I40E_FCOE_STATS_LEN; i++) {
1219                         snprintf(p, ETH_GSTRING_LEN, "%s",
1220                                  i40e_gstrings_fcoe_stats[i].stat_string);
1221                         p += ETH_GSTRING_LEN;
1222                 }
1223 #endif
1224                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1225                         snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_packets", i);
1226                         p += ETH_GSTRING_LEN;
1227                         snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_bytes", i);
1228                         p += ETH_GSTRING_LEN;
1229                         snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_packets", i);
1230                         p += ETH_GSTRING_LEN;
1231                         snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_bytes", i);
1232                         p += ETH_GSTRING_LEN;
1233                 }
1234                 if (vsi != pf->vsi[pf->lan_vsi])
1235                         return;
1236
1237                 if (pf->lan_veb != I40E_NO_VEB) {
1238                         for (i = 0; i < I40E_VEB_STATS_LEN; i++) {
1239                                 snprintf(p, ETH_GSTRING_LEN, "veb.%s",
1240                                         i40e_gstrings_veb_stats[i].stat_string);
1241                                 p += ETH_GSTRING_LEN;
1242                         }
1243                 }
1244                 for (i = 0; i < I40E_GLOBAL_STATS_LEN; i++) {
1245                         snprintf(p, ETH_GSTRING_LEN, "port.%s",
1246                                  i40e_gstrings_stats[i].stat_string);
1247                         p += ETH_GSTRING_LEN;
1248                 }
1249                 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1250                         snprintf(p, ETH_GSTRING_LEN,
1251                                  "port.tx_priority_%u_xon", i);
1252                         p += ETH_GSTRING_LEN;
1253                         snprintf(p, ETH_GSTRING_LEN,
1254                                  "port.tx_priority_%u_xoff", i);
1255                         p += ETH_GSTRING_LEN;
1256                 }
1257                 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1258                         snprintf(p, ETH_GSTRING_LEN,
1259                                  "port.rx_priority_%u_xon", i);
1260                         p += ETH_GSTRING_LEN;
1261                         snprintf(p, ETH_GSTRING_LEN,
1262                                  "port.rx_priority_%u_xoff", i);
1263                         p += ETH_GSTRING_LEN;
1264                 }
1265                 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1266                         snprintf(p, ETH_GSTRING_LEN,
1267                                  "port.rx_priority_%u_xon_2_xoff", i);
1268                         p += ETH_GSTRING_LEN;
1269                 }
1270                 /* BUG_ON(p - data != I40E_STATS_LEN * ETH_GSTRING_LEN); */
1271                 break;
1272         }
1273 }
1274
1275 static int i40e_get_ts_info(struct net_device *dev,
1276                             struct ethtool_ts_info *info)
1277 {
1278         struct i40e_pf *pf = i40e_netdev_to_pf(dev);
1279
1280         info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1281                                 SOF_TIMESTAMPING_RX_SOFTWARE |
1282                                 SOF_TIMESTAMPING_SOFTWARE |
1283                                 SOF_TIMESTAMPING_TX_HARDWARE |
1284                                 SOF_TIMESTAMPING_RX_HARDWARE |
1285                                 SOF_TIMESTAMPING_RAW_HARDWARE;
1286
1287         if (pf->ptp_clock)
1288                 info->phc_index = ptp_clock_index(pf->ptp_clock);
1289         else
1290                 info->phc_index = -1;
1291
1292         info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1293
1294         info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
1295                            (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
1296                            (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
1297                            (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
1298                            (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1299                            (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1300                            (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
1301                            (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
1302                            (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
1303                            (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
1304                            (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
1305                            (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
1306
1307         return 0;
1308 }
1309
1310 static int i40e_link_test(struct net_device *netdev, u64 *data)
1311 {
1312         struct i40e_netdev_priv *np = netdev_priv(netdev);
1313         struct i40e_pf *pf = np->vsi->back;
1314
1315         netif_info(pf, hw, netdev, "link test\n");
1316         if (i40e_get_link_status(&pf->hw))
1317                 *data = 0;
1318         else
1319                 *data = 1;
1320
1321         return *data;
1322 }
1323
1324 static int i40e_reg_test(struct net_device *netdev, u64 *data)
1325 {
1326         struct i40e_netdev_priv *np = netdev_priv(netdev);
1327         struct i40e_pf *pf = np->vsi->back;
1328
1329         netif_info(pf, hw, netdev, "register test\n");
1330         *data = i40e_diag_reg_test(&pf->hw);
1331
1332         return *data;
1333 }
1334
1335 static int i40e_eeprom_test(struct net_device *netdev, u64 *data)
1336 {
1337         struct i40e_netdev_priv *np = netdev_priv(netdev);
1338         struct i40e_pf *pf = np->vsi->back;
1339
1340         netif_info(pf, hw, netdev, "eeprom test\n");
1341         *data = i40e_diag_eeprom_test(&pf->hw);
1342
1343         return *data;
1344 }
1345
1346 static int i40e_intr_test(struct net_device *netdev, u64 *data)
1347 {
1348         struct i40e_netdev_priv *np = netdev_priv(netdev);
1349         struct i40e_pf *pf = np->vsi->back;
1350         u16 swc_old = pf->sw_int_count;
1351
1352         netif_info(pf, hw, netdev, "interrupt test\n");
1353         wr32(&pf->hw, I40E_PFINT_DYN_CTL0,
1354              (I40E_PFINT_DYN_CTL0_INTENA_MASK |
1355               I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
1356         usleep_range(1000, 2000);
1357         *data = (swc_old == pf->sw_int_count);
1358
1359         return *data;
1360 }
1361
1362 static int i40e_loopback_test(struct net_device *netdev, u64 *data)
1363 {
1364         struct i40e_netdev_priv *np = netdev_priv(netdev);
1365         struct i40e_pf *pf = np->vsi->back;
1366
1367         netif_info(pf, hw, netdev, "loopback test not implemented\n");
1368         *data = 0;
1369
1370         return *data;
1371 }
1372
1373 static void i40e_diag_test(struct net_device *netdev,
1374                            struct ethtool_test *eth_test, u64 *data)
1375 {
1376         struct i40e_netdev_priv *np = netdev_priv(netdev);
1377         struct i40e_pf *pf = np->vsi->back;
1378
1379         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1380                 /* Offline tests */
1381                 netif_info(pf, drv, netdev, "offline testing starting\n");
1382
1383                 set_bit(__I40E_TESTING, &pf->state);
1384
1385                 /* Link test performed before hardware reset
1386                  * so autoneg doesn't interfere with test result
1387                  */
1388                 if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
1389                         eth_test->flags |= ETH_TEST_FL_FAILED;
1390
1391                 if (i40e_eeprom_test(netdev, &data[I40E_ETH_TEST_EEPROM]))
1392                         eth_test->flags |= ETH_TEST_FL_FAILED;
1393
1394                 if (i40e_intr_test(netdev, &data[I40E_ETH_TEST_INTR]))
1395                         eth_test->flags |= ETH_TEST_FL_FAILED;
1396
1397                 if (i40e_loopback_test(netdev, &data[I40E_ETH_TEST_LOOPBACK]))
1398                         eth_test->flags |= ETH_TEST_FL_FAILED;
1399
1400                 /* run reg test last, a reset is required after it */
1401                 if (i40e_reg_test(netdev, &data[I40E_ETH_TEST_REG]))
1402                         eth_test->flags |= ETH_TEST_FL_FAILED;
1403
1404                 clear_bit(__I40E_TESTING, &pf->state);
1405                 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
1406         } else {
1407                 /* Online tests */
1408                 netif_info(pf, drv, netdev, "online testing starting\n");
1409
1410                 if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
1411                         eth_test->flags |= ETH_TEST_FL_FAILED;
1412
1413                 /* Offline only tests, not run in online; pass by default */
1414                 data[I40E_ETH_TEST_REG] = 0;
1415                 data[I40E_ETH_TEST_EEPROM] = 0;
1416                 data[I40E_ETH_TEST_INTR] = 0;
1417                 data[I40E_ETH_TEST_LOOPBACK] = 0;
1418         }
1419
1420         netif_info(pf, drv, netdev, "testing finished\n");
1421 }
1422
1423 static void i40e_get_wol(struct net_device *netdev,
1424                          struct ethtool_wolinfo *wol)
1425 {
1426         struct i40e_netdev_priv *np = netdev_priv(netdev);
1427         struct i40e_pf *pf = np->vsi->back;
1428         struct i40e_hw *hw = &pf->hw;
1429         u16 wol_nvm_bits;
1430
1431         /* NVM bit on means WoL disabled for the port */
1432         i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
1433         if ((1 << hw->port) & wol_nvm_bits) {
1434                 wol->supported = 0;
1435                 wol->wolopts = 0;
1436         } else {
1437                 wol->supported = WAKE_MAGIC;
1438                 wol->wolopts = (pf->wol_en ? WAKE_MAGIC : 0);
1439         }
1440 }
1441
1442 static int i40e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1443 {
1444         struct i40e_netdev_priv *np = netdev_priv(netdev);
1445         struct i40e_pf *pf = np->vsi->back;
1446         struct i40e_hw *hw = &pf->hw;
1447         u16 wol_nvm_bits;
1448
1449         /* NVM bit on means WoL disabled for the port */
1450         i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
1451         if (((1 << hw->port) & wol_nvm_bits))
1452                 return -EOPNOTSUPP;
1453
1454         /* only magic packet is supported */
1455         if (wol->wolopts && (wol->wolopts != WAKE_MAGIC))
1456                 return -EOPNOTSUPP;
1457
1458         /* is this a new value? */
1459         if (pf->wol_en != !!wol->wolopts) {
1460                 pf->wol_en = !!wol->wolopts;
1461                 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
1462         }
1463
1464         return 0;
1465 }
1466
1467 static int i40e_set_phys_id(struct net_device *netdev,
1468                             enum ethtool_phys_id_state state)
1469 {
1470         struct i40e_netdev_priv *np = netdev_priv(netdev);
1471         struct i40e_pf *pf = np->vsi->back;
1472         struct i40e_hw *hw = &pf->hw;
1473         int blink_freq = 2;
1474
1475         switch (state) {
1476         case ETHTOOL_ID_ACTIVE:
1477                 pf->led_status = i40e_led_get(hw);
1478                 return blink_freq;
1479         case ETHTOOL_ID_ON:
1480                 i40e_led_set(hw, 0xF, false);
1481                 break;
1482         case ETHTOOL_ID_OFF:
1483                 i40e_led_set(hw, 0x0, false);
1484                 break;
1485         case ETHTOOL_ID_INACTIVE:
1486                 i40e_led_set(hw, pf->led_status, false);
1487                 break;
1488         }
1489
1490         return 0;
1491 }
1492
1493 /* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
1494  * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
1495  * 125us (8000 interrupts per second) == ITR(62)
1496  */
1497
1498 static int i40e_get_coalesce(struct net_device *netdev,
1499                              struct ethtool_coalesce *ec)
1500 {
1501         struct i40e_netdev_priv *np = netdev_priv(netdev);
1502         struct i40e_vsi *vsi = np->vsi;
1503
1504         ec->tx_max_coalesced_frames_irq = vsi->work_limit;
1505         ec->rx_max_coalesced_frames_irq = vsi->work_limit;
1506
1507         if (ITR_IS_DYNAMIC(vsi->rx_itr_setting))
1508                 ec->use_adaptive_rx_coalesce = 1;
1509
1510         if (ITR_IS_DYNAMIC(vsi->tx_itr_setting))
1511                 ec->use_adaptive_tx_coalesce = 1;
1512
1513         ec->rx_coalesce_usecs = vsi->rx_itr_setting & ~I40E_ITR_DYNAMIC;
1514         ec->tx_coalesce_usecs = vsi->tx_itr_setting & ~I40E_ITR_DYNAMIC;
1515
1516         return 0;
1517 }
1518
1519 static int i40e_set_coalesce(struct net_device *netdev,
1520                              struct ethtool_coalesce *ec)
1521 {
1522         struct i40e_netdev_priv *np = netdev_priv(netdev);
1523         struct i40e_q_vector *q_vector;
1524         struct i40e_vsi *vsi = np->vsi;
1525         struct i40e_pf *pf = vsi->back;
1526         struct i40e_hw *hw = &pf->hw;
1527         u16 vector;
1528         int i;
1529
1530         if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
1531                 vsi->work_limit = ec->tx_max_coalesced_frames_irq;
1532
1533         vector = vsi->base_vector;
1534         if ((ec->rx_coalesce_usecs >= (I40E_MIN_ITR << 1)) &&
1535             (ec->rx_coalesce_usecs <= (I40E_MAX_ITR << 1))) {
1536                 vsi->rx_itr_setting = ec->rx_coalesce_usecs;
1537         } else if (ec->rx_coalesce_usecs == 0) {
1538                 vsi->rx_itr_setting = ec->rx_coalesce_usecs;
1539                 i40e_irq_dynamic_disable(vsi, vector);
1540                 if (ec->use_adaptive_rx_coalesce)
1541                         netif_info(pf, drv, netdev,
1542                                    "Rx-secs=0, need to disable adaptive-Rx for a complete disable\n");
1543         } else {
1544                 netif_info(pf, drv, netdev,
1545                            "Invalid value, Rx-usecs range is 0, 8-8160\n");
1546                 return -EINVAL;
1547         }
1548
1549         if ((ec->tx_coalesce_usecs >= (I40E_MIN_ITR << 1)) &&
1550             (ec->tx_coalesce_usecs <= (I40E_MAX_ITR << 1))) {
1551                 vsi->tx_itr_setting = ec->tx_coalesce_usecs;
1552         } else if (ec->tx_coalesce_usecs == 0) {
1553                 vsi->tx_itr_setting = ec->tx_coalesce_usecs;
1554                 i40e_irq_dynamic_disable(vsi, vector);
1555                 if (ec->use_adaptive_tx_coalesce)
1556                         netif_info(pf, drv, netdev,
1557                                    "Tx-secs=0, need to disable adaptive-Tx for a complete disable\n");
1558         } else {
1559                 netif_info(pf, drv, netdev,
1560                            "Invalid value, Tx-usecs range is 0, 8-8160\n");
1561                 return -EINVAL;
1562         }
1563
1564         if (ec->use_adaptive_rx_coalesce)
1565                 vsi->rx_itr_setting |= I40E_ITR_DYNAMIC;
1566         else
1567                 vsi->rx_itr_setting &= ~I40E_ITR_DYNAMIC;
1568
1569         if (ec->use_adaptive_tx_coalesce)
1570                 vsi->tx_itr_setting |= I40E_ITR_DYNAMIC;
1571         else
1572                 vsi->tx_itr_setting &= ~I40E_ITR_DYNAMIC;
1573
1574         for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1575                 q_vector = vsi->q_vectors[i];
1576                 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
1577                 wr32(hw, I40E_PFINT_ITRN(0, vector - 1), q_vector->rx.itr);
1578                 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
1579                 wr32(hw, I40E_PFINT_ITRN(1, vector - 1), q_vector->tx.itr);
1580                 i40e_flush(hw);
1581         }
1582
1583         return 0;
1584 }
1585
1586 /**
1587  * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
1588  * @pf: pointer to the physical function struct
1589  * @cmd: ethtool rxnfc command
1590  *
1591  * Returns Success if the flow is supported, else Invalid Input.
1592  **/
1593 static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
1594 {
1595         cmd->data = 0;
1596
1597         /* Report default options for RSS on i40e */
1598         switch (cmd->flow_type) {
1599         case TCP_V4_FLOW:
1600         case UDP_V4_FLOW:
1601                 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1602         /* fall through to add IP fields */
1603         case SCTP_V4_FLOW:
1604         case AH_ESP_V4_FLOW:
1605         case AH_V4_FLOW:
1606         case ESP_V4_FLOW:
1607         case IPV4_FLOW:
1608                 cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1609                 break;
1610         case TCP_V6_FLOW:
1611         case UDP_V6_FLOW:
1612                 cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1613         /* fall through to add IP fields */
1614         case SCTP_V6_FLOW:
1615         case AH_ESP_V6_FLOW:
1616         case AH_V6_FLOW:
1617         case ESP_V6_FLOW:
1618         case IPV6_FLOW:
1619                 cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1620                 break;
1621         default:
1622                 return -EINVAL;
1623         }
1624
1625         return 0;
1626 }
1627
1628 /**
1629  * i40e_get_ethtool_fdir_all - Populates the rule count of a command
1630  * @pf: Pointer to the physical function struct
1631  * @cmd: The command to get or set Rx flow classification rules
1632  * @rule_locs: Array of used rule locations
1633  *
1634  * This function populates both the total and actual rule count of
1635  * the ethtool flow classification command
1636  *
1637  * Returns 0 on success or -EMSGSIZE if entry not found
1638  **/
1639 static int i40e_get_ethtool_fdir_all(struct i40e_pf *pf,
1640                                      struct ethtool_rxnfc *cmd,
1641                                      u32 *rule_locs)
1642 {
1643         struct i40e_fdir_filter *rule;
1644         struct hlist_node *node2;
1645         int cnt = 0;
1646
1647         /* report total rule count */
1648         cmd->data = i40e_get_fd_cnt_all(pf);
1649
1650         hlist_for_each_entry_safe(rule, node2,
1651                                   &pf->fdir_filter_list, fdir_node) {
1652                 if (cnt == cmd->rule_cnt)
1653                         return -EMSGSIZE;
1654
1655                 rule_locs[cnt] = rule->fd_id;
1656                 cnt++;
1657         }
1658
1659         cmd->rule_cnt = cnt;
1660
1661         return 0;
1662 }
1663
1664 /**
1665  * i40e_get_ethtool_fdir_entry - Look up a filter based on Rx flow
1666  * @pf: Pointer to the physical function struct
1667  * @cmd: The command to get or set Rx flow classification rules
1668  *
1669  * This function looks up a filter based on the Rx flow classification
1670  * command and fills the flow spec info for it if found
1671  *
1672  * Returns 0 on success or -EINVAL if filter not found
1673  **/
1674 static int i40e_get_ethtool_fdir_entry(struct i40e_pf *pf,
1675                                        struct ethtool_rxnfc *cmd)
1676 {
1677         struct ethtool_rx_flow_spec *fsp =
1678                         (struct ethtool_rx_flow_spec *)&cmd->fs;
1679         struct i40e_fdir_filter *rule = NULL;
1680         struct hlist_node *node2;
1681
1682         hlist_for_each_entry_safe(rule, node2,
1683                                   &pf->fdir_filter_list, fdir_node) {
1684                 if (fsp->location <= rule->fd_id)
1685                         break;
1686         }
1687
1688         if (!rule || fsp->location != rule->fd_id)
1689                 return -EINVAL;
1690
1691         fsp->flow_type = rule->flow_type;
1692         if (fsp->flow_type == IP_USER_FLOW) {
1693                 fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
1694                 fsp->h_u.usr_ip4_spec.proto = 0;
1695                 fsp->m_u.usr_ip4_spec.proto = 0;
1696         }
1697
1698         /* Reverse the src and dest notion, since the HW views them from
1699          * Tx perspective where as the user expects it from Rx filter view.
1700          */
1701         fsp->h_u.tcp_ip4_spec.psrc = rule->dst_port;
1702         fsp->h_u.tcp_ip4_spec.pdst = rule->src_port;
1703         fsp->h_u.tcp_ip4_spec.ip4src = rule->dst_ip[0];
1704         fsp->h_u.tcp_ip4_spec.ip4dst = rule->src_ip[0];
1705
1706         if (rule->dest_ctl == I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET)
1707                 fsp->ring_cookie = RX_CLS_FLOW_DISC;
1708         else
1709                 fsp->ring_cookie = rule->q_index;
1710
1711         return 0;
1712 }
1713
1714 /**
1715  * i40e_get_rxnfc - command to get RX flow classification rules
1716  * @netdev: network interface device structure
1717  * @cmd: ethtool rxnfc command
1718  *
1719  * Returns Success if the command is supported.
1720  **/
1721 static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1722                           u32 *rule_locs)
1723 {
1724         struct i40e_netdev_priv *np = netdev_priv(netdev);
1725         struct i40e_vsi *vsi = np->vsi;
1726         struct i40e_pf *pf = vsi->back;
1727         int ret = -EOPNOTSUPP;
1728
1729         switch (cmd->cmd) {
1730         case ETHTOOL_GRXRINGS:
1731                 cmd->data = vsi->alloc_queue_pairs;
1732                 ret = 0;
1733                 break;
1734         case ETHTOOL_GRXFH:
1735                 ret = i40e_get_rss_hash_opts(pf, cmd);
1736                 break;
1737         case ETHTOOL_GRXCLSRLCNT:
1738                 cmd->rule_cnt = pf->fdir_pf_active_filters;
1739                 /* report total rule count */
1740                 cmd->data = i40e_get_fd_cnt_all(pf);
1741                 ret = 0;
1742                 break;
1743         case ETHTOOL_GRXCLSRULE:
1744                 ret = i40e_get_ethtool_fdir_entry(pf, cmd);
1745                 break;
1746         case ETHTOOL_GRXCLSRLALL:
1747                 ret = i40e_get_ethtool_fdir_all(pf, cmd, rule_locs);
1748                 break;
1749         default:
1750                 break;
1751         }
1752
1753         return ret;
1754 }
1755
1756 /**
1757  * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
1758  * @pf: pointer to the physical function struct
1759  * @cmd: ethtool rxnfc command
1760  *
1761  * Returns Success if the flow input set is supported.
1762  **/
1763 static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
1764 {
1765         struct i40e_hw *hw = &pf->hw;
1766         u64 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
1767                    ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
1768
1769         /* RSS does not support anything other than hashing
1770          * to queues on src and dst IPs and ports
1771          */
1772         if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
1773                           RXH_L4_B_0_1 | RXH_L4_B_2_3))
1774                 return -EINVAL;
1775
1776         /* We need at least the IP SRC and DEST fields for hashing */
1777         if (!(nfc->data & RXH_IP_SRC) ||
1778             !(nfc->data & RXH_IP_DST))
1779                 return -EINVAL;
1780
1781         switch (nfc->flow_type) {
1782         case TCP_V4_FLOW:
1783                 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1784                 case 0:
1785                         hena &= ~((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
1786                         break;
1787                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1788                         hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
1789                         break;
1790                 default:
1791                         return -EINVAL;
1792                 }
1793                 break;
1794         case TCP_V6_FLOW:
1795                 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1796                 case 0:
1797                         hena &= ~((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
1798                         break;
1799                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1800                         hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
1801                         break;
1802                 default:
1803                         return -EINVAL;
1804                 }
1805                 break;
1806         case UDP_V4_FLOW:
1807                 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1808                 case 0:
1809                         hena &= ~(((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
1810                                   ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4));
1811                         break;
1812                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1813                         hena |= (((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
1814                                   ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4));
1815                         break;
1816                 default:
1817                         return -EINVAL;
1818                 }
1819                 break;
1820         case UDP_V6_FLOW:
1821                 switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1822                 case 0:
1823                         hena &= ~(((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
1824                                   ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6));
1825                         break;
1826                 case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1827                         hena |= (((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
1828                                  ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6));
1829                         break;
1830                 default:
1831                         return -EINVAL;
1832                 }
1833                 break;
1834         case AH_ESP_V4_FLOW:
1835         case AH_V4_FLOW:
1836         case ESP_V4_FLOW:
1837         case SCTP_V4_FLOW:
1838                 if ((nfc->data & RXH_L4_B_0_1) ||
1839                     (nfc->data & RXH_L4_B_2_3))
1840                         return -EINVAL;
1841                 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
1842                 break;
1843         case AH_ESP_V6_FLOW:
1844         case AH_V6_FLOW:
1845         case ESP_V6_FLOW:
1846         case SCTP_V6_FLOW:
1847                 if ((nfc->data & RXH_L4_B_0_1) ||
1848                     (nfc->data & RXH_L4_B_2_3))
1849                         return -EINVAL;
1850                 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
1851                 break;
1852         case IPV4_FLOW:
1853                 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
1854                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4);
1855                 break;
1856         case IPV6_FLOW:
1857                 hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
1858                         ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6);
1859                 break;
1860         default:
1861                 return -EINVAL;
1862         }
1863
1864         wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
1865         wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
1866         i40e_flush(hw);
1867
1868         return 0;
1869 }
1870
1871 /**
1872  * i40e_match_fdir_input_set - Match a new filter against an existing one
1873  * @rule: The filter already added
1874  * @input: The new filter to comapre against
1875  *
1876  * Returns true if the two input set match
1877  **/
1878 static bool i40e_match_fdir_input_set(struct i40e_fdir_filter *rule,
1879                                       struct i40e_fdir_filter *input)
1880 {
1881         if ((rule->dst_ip[0] != input->dst_ip[0]) ||
1882             (rule->src_ip[0] != input->src_ip[0]) ||
1883             (rule->dst_port != input->dst_port) ||
1884             (rule->src_port != input->src_port))
1885                 return false;
1886         return true;
1887 }
1888
1889 /**
1890  * i40e_update_ethtool_fdir_entry - Updates the fdir filter entry
1891  * @vsi: Pointer to the targeted VSI
1892  * @input: The filter to update or NULL to indicate deletion
1893  * @sw_idx: Software index to the filter
1894  * @cmd: The command to get or set Rx flow classification rules
1895  *
1896  * This function updates (or deletes) a Flow Director entry from
1897  * the hlist of the corresponding PF
1898  *
1899  * Returns 0 on success
1900  **/
1901 static int i40e_update_ethtool_fdir_entry(struct i40e_vsi *vsi,
1902                                           struct i40e_fdir_filter *input,
1903                                           u16 sw_idx,
1904                                           struct ethtool_rxnfc *cmd)
1905 {
1906         struct i40e_fdir_filter *rule, *parent;
1907         struct i40e_pf *pf = vsi->back;
1908         struct hlist_node *node2;
1909         int err = -EINVAL;
1910
1911         parent = NULL;
1912         rule = NULL;
1913
1914         hlist_for_each_entry_safe(rule, node2,
1915                                   &pf->fdir_filter_list, fdir_node) {
1916                 /* hash found, or no matching entry */
1917                 if (rule->fd_id >= sw_idx)
1918                         break;
1919                 parent = rule;
1920         }
1921
1922         /* if there is an old rule occupying our place remove it */
1923         if (rule && (rule->fd_id == sw_idx)) {
1924                 if (input && !i40e_match_fdir_input_set(rule, input))
1925                         err = i40e_add_del_fdir(vsi, rule, false);
1926                 else if (!input)
1927                         err = i40e_add_del_fdir(vsi, rule, false);
1928                 hlist_del(&rule->fdir_node);
1929                 kfree(rule);
1930                 pf->fdir_pf_active_filters--;
1931         }
1932
1933         /* If no input this was a delete, err should be 0 if a rule was
1934          * successfully found and removed from the list else -EINVAL
1935          */
1936         if (!input)
1937                 return err;
1938
1939         /* initialize node and set software index */
1940         INIT_HLIST_NODE(&input->fdir_node);
1941
1942         /* add filter to the list */
1943         if (parent)
1944                 hlist_add_behind(&input->fdir_node, &parent->fdir_node);
1945         else
1946                 hlist_add_head(&input->fdir_node,
1947                                &pf->fdir_filter_list);
1948
1949         /* update counts */
1950         pf->fdir_pf_active_filters++;
1951
1952         return 0;
1953 }
1954
1955 /**
1956  * i40e_del_fdir_entry - Deletes a Flow Director filter entry
1957  * @vsi: Pointer to the targeted VSI
1958  * @cmd: The command to get or set Rx flow classification rules
1959  *
1960  * The function removes a Flow Director filter entry from the
1961  * hlist of the corresponding PF
1962  *
1963  * Returns 0 on success
1964  */
1965 static int i40e_del_fdir_entry(struct i40e_vsi *vsi,
1966                                struct ethtool_rxnfc *cmd)
1967 {
1968         struct ethtool_rx_flow_spec *fsp =
1969                 (struct ethtool_rx_flow_spec *)&cmd->fs;
1970         struct i40e_pf *pf = vsi->back;
1971         int ret = 0;
1972
1973         if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
1974             test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
1975                 return -EBUSY;
1976
1977         if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
1978                 return -EBUSY;
1979
1980         ret = i40e_update_ethtool_fdir_entry(vsi, NULL, fsp->location, cmd);
1981
1982         i40e_fdir_check_and_reenable(pf);
1983         return ret;
1984 }
1985
1986 /**
1987  * i40e_add_fdir_ethtool - Add/Remove Flow Director filters
1988  * @vsi: pointer to the targeted VSI
1989  * @cmd: command to get or set RX flow classification rules
1990  *
1991  * Add Flow Director filters for a specific flow spec based on their
1992  * protocol.  Returns 0 if the filters were successfully added.
1993  **/
1994 static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
1995                                  struct ethtool_rxnfc *cmd)
1996 {
1997         struct ethtool_rx_flow_spec *fsp;
1998         struct i40e_fdir_filter *input;
1999         struct i40e_pf *pf;
2000         int ret = -EINVAL;
2001
2002         if (!vsi)
2003                 return -EINVAL;
2004
2005         pf = vsi->back;
2006
2007         if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2008                 return -EOPNOTSUPP;
2009
2010         if (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)
2011                 return -ENOSPC;
2012
2013         if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
2014             test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
2015                 return -EBUSY;
2016
2017         if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
2018                 return -EBUSY;
2019
2020         fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
2021
2022         if (fsp->location >= (pf->hw.func_caps.fd_filters_best_effort +
2023                               pf->hw.func_caps.fd_filters_guaranteed)) {
2024                 return -EINVAL;
2025         }
2026
2027         if ((fsp->ring_cookie != RX_CLS_FLOW_DISC) &&
2028             (fsp->ring_cookie >= vsi->num_queue_pairs))
2029                 return -EINVAL;
2030
2031         input = kzalloc(sizeof(*input), GFP_KERNEL);
2032
2033         if (!input)
2034                 return -ENOMEM;
2035
2036         input->fd_id = fsp->location;
2037
2038         if (fsp->ring_cookie == RX_CLS_FLOW_DISC)
2039                 input->dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
2040         else
2041                 input->dest_ctl =
2042                              I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX;
2043
2044         input->q_index = fsp->ring_cookie;
2045         input->flex_off = 0;
2046         input->pctype = 0;
2047         input->dest_vsi = vsi->id;
2048         input->fd_status = I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID;
2049         input->cnt_index  = pf->fd_sb_cnt_idx;
2050         input->flow_type = fsp->flow_type;
2051         input->ip4_proto = fsp->h_u.usr_ip4_spec.proto;
2052
2053         /* Reverse the src and dest notion, since the HW expects them to be from
2054          * Tx perspective where as the input from user is from Rx filter view.
2055          */
2056         input->dst_port = fsp->h_u.tcp_ip4_spec.psrc;
2057         input->src_port = fsp->h_u.tcp_ip4_spec.pdst;
2058         input->dst_ip[0] = fsp->h_u.tcp_ip4_spec.ip4src;
2059         input->src_ip[0] = fsp->h_u.tcp_ip4_spec.ip4dst;
2060
2061         ret = i40e_add_del_fdir(vsi, input, true);
2062         if (ret)
2063                 kfree(input);
2064         else
2065                 i40e_update_ethtool_fdir_entry(vsi, input, fsp->location, NULL);
2066
2067         return ret;
2068 }
2069
2070 /**
2071  * i40e_set_rxnfc - command to set RX flow classification rules
2072  * @netdev: network interface device structure
2073  * @cmd: ethtool rxnfc command
2074  *
2075  * Returns Success if the command is supported.
2076  **/
2077 static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
2078 {
2079         struct i40e_netdev_priv *np = netdev_priv(netdev);
2080         struct i40e_vsi *vsi = np->vsi;
2081         struct i40e_pf *pf = vsi->back;
2082         int ret = -EOPNOTSUPP;
2083
2084         switch (cmd->cmd) {
2085         case ETHTOOL_SRXFH:
2086                 ret = i40e_set_rss_hash_opt(pf, cmd);
2087                 break;
2088         case ETHTOOL_SRXCLSRLINS:
2089                 ret = i40e_add_fdir_ethtool(vsi, cmd);
2090                 break;
2091         case ETHTOOL_SRXCLSRLDEL:
2092                 ret = i40e_del_fdir_entry(vsi, cmd);
2093                 break;
2094         default:
2095                 break;
2096         }
2097
2098         return ret;
2099 }
2100
2101 /**
2102  * i40e_max_channels - get Max number of combined channels supported
2103  * @vsi: vsi pointer
2104  **/
2105 static unsigned int i40e_max_channels(struct i40e_vsi *vsi)
2106 {
2107         /* TODO: This code assumes DCB and FD is disabled for now. */
2108         return vsi->alloc_queue_pairs;
2109 }
2110
2111 /**
2112  * i40e_get_channels - Get the current channels enabled and max supported etc.
2113  * @netdev: network interface device structure
2114  * @ch: ethtool channels structure
2115  *
2116  * We don't support separate tx and rx queues as channels. The other count
2117  * represents how many queues are being used for control. max_combined counts
2118  * how many queue pairs we can support. They may not be mapped 1 to 1 with
2119  * q_vectors since we support a lot more queue pairs than q_vectors.
2120  **/
2121 static void i40e_get_channels(struct net_device *dev,
2122                                struct ethtool_channels *ch)
2123 {
2124         struct i40e_netdev_priv *np = netdev_priv(dev);
2125         struct i40e_vsi *vsi = np->vsi;
2126         struct i40e_pf *pf = vsi->back;
2127
2128         /* report maximum channels */
2129         ch->max_combined = i40e_max_channels(vsi);
2130
2131         /* report info for other vector */
2132         ch->other_count = (pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0;
2133         ch->max_other = ch->other_count;
2134
2135         /* Note: This code assumes DCB is disabled for now. */
2136         ch->combined_count = vsi->num_queue_pairs;
2137 }
2138
2139 /**
2140  * i40e_set_channels - Set the new channels count.
2141  * @netdev: network interface device structure
2142  * @ch: ethtool channels structure
2143  *
2144  * The new channels count may not be the same as requested by the user
2145  * since it gets rounded down to a power of 2 value.
2146  **/
2147 static int i40e_set_channels(struct net_device *dev,
2148                               struct ethtool_channels *ch)
2149 {
2150         struct i40e_netdev_priv *np = netdev_priv(dev);
2151         unsigned int count = ch->combined_count;
2152         struct i40e_vsi *vsi = np->vsi;
2153         struct i40e_pf *pf = vsi->back;
2154         int new_count;
2155
2156         /* We do not support setting channels for any other VSI at present */
2157         if (vsi->type != I40E_VSI_MAIN)
2158                 return -EINVAL;
2159
2160         /* verify they are not requesting separate vectors */
2161         if (!count || ch->rx_count || ch->tx_count)
2162                 return -EINVAL;
2163
2164         /* verify other_count has not changed */
2165         if (ch->other_count != ((pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0))
2166                 return -EINVAL;
2167
2168         /* verify the number of channels does not exceed hardware limits */
2169         if (count > i40e_max_channels(vsi))
2170                 return -EINVAL;
2171
2172         /* update feature limits from largest to smallest supported values */
2173         /* TODO: Flow director limit, DCB etc */
2174
2175         /* cap RSS limit */
2176         if (count > pf->rss_size_max)
2177                 count = pf->rss_size_max;
2178
2179         /* use rss_reconfig to rebuild with new queue count and update traffic
2180          * class queue mapping
2181          */
2182         new_count = i40e_reconfig_rss_queues(pf, count);
2183         if (new_count > 0)
2184                 return 0;
2185         else
2186                 return -EINVAL;
2187 }
2188
2189 static const struct ethtool_ops i40e_ethtool_ops = {
2190         .get_settings           = i40e_get_settings,
2191         .set_settings           = i40e_set_settings,
2192         .get_drvinfo            = i40e_get_drvinfo,
2193         .get_regs_len           = i40e_get_regs_len,
2194         .get_regs               = i40e_get_regs,
2195         .nway_reset             = i40e_nway_reset,
2196         .get_link               = ethtool_op_get_link,
2197         .get_wol                = i40e_get_wol,
2198         .set_wol                = i40e_set_wol,
2199         .set_eeprom             = i40e_set_eeprom,
2200         .get_eeprom_len         = i40e_get_eeprom_len,
2201         .get_eeprom             = i40e_get_eeprom,
2202         .get_ringparam          = i40e_get_ringparam,
2203         .set_ringparam          = i40e_set_ringparam,
2204         .get_pauseparam         = i40e_get_pauseparam,
2205         .set_pauseparam         = i40e_set_pauseparam,
2206         .get_msglevel           = i40e_get_msglevel,
2207         .set_msglevel           = i40e_set_msglevel,
2208         .get_rxnfc              = i40e_get_rxnfc,
2209         .set_rxnfc              = i40e_set_rxnfc,
2210         .self_test              = i40e_diag_test,
2211         .get_strings            = i40e_get_strings,
2212         .set_phys_id            = i40e_set_phys_id,
2213         .get_sset_count         = i40e_get_sset_count,
2214         .get_ethtool_stats      = i40e_get_ethtool_stats,
2215         .get_coalesce           = i40e_get_coalesce,
2216         .set_coalesce           = i40e_set_coalesce,
2217         .get_channels           = i40e_get_channels,
2218         .set_channels           = i40e_set_channels,
2219         .get_ts_info            = i40e_get_ts_info,
2220 };
2221
2222 void i40e_set_ethtool_ops(struct net_device *netdev)
2223 {
2224         netdev->ethtool_ops = &i40e_ethtool_ops;
2225 }