]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/intel/i40e/i40e_ethtool.c
d16a5a6b24fc27dc9ad1e2d6e6b637c32c99a56d
[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 - 2016 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26
27 /* ethtool support for 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
44 #define I40E_NETDEV_STAT(_net_stat) \
45                 I40E_STAT(struct rtnl_link_stats64, #_net_stat, _net_stat)
46 #define I40E_PF_STAT(_name, _stat) \
47                 I40E_STAT(struct i40e_pf, _name, _stat)
48 #define I40E_VSI_STAT(_name, _stat) \
49                 I40E_STAT(struct i40e_vsi, _name, _stat)
50 #define I40E_VEB_STAT(_name, _stat) \
51                 I40E_STAT(struct i40e_veb, _name, _stat)
52
53 static const struct i40e_stats i40e_gstrings_net_stats[] = {
54         I40E_NETDEV_STAT(rx_packets),
55         I40E_NETDEV_STAT(tx_packets),
56         I40E_NETDEV_STAT(rx_bytes),
57         I40E_NETDEV_STAT(tx_bytes),
58         I40E_NETDEV_STAT(rx_errors),
59         I40E_NETDEV_STAT(tx_errors),
60         I40E_NETDEV_STAT(rx_dropped),
61         I40E_NETDEV_STAT(tx_dropped),
62         I40E_NETDEV_STAT(collisions),
63         I40E_NETDEV_STAT(rx_length_errors),
64         I40E_NETDEV_STAT(rx_crc_errors),
65 };
66
67 static const struct i40e_stats i40e_gstrings_veb_stats[] = {
68         I40E_VEB_STAT("rx_bytes", stats.rx_bytes),
69         I40E_VEB_STAT("tx_bytes", stats.tx_bytes),
70         I40E_VEB_STAT("rx_unicast", stats.rx_unicast),
71         I40E_VEB_STAT("tx_unicast", stats.tx_unicast),
72         I40E_VEB_STAT("rx_multicast", stats.rx_multicast),
73         I40E_VEB_STAT("tx_multicast", stats.tx_multicast),
74         I40E_VEB_STAT("rx_broadcast", stats.rx_broadcast),
75         I40E_VEB_STAT("tx_broadcast", stats.tx_broadcast),
76         I40E_VEB_STAT("rx_discards", stats.rx_discards),
77         I40E_VEB_STAT("tx_discards", stats.tx_discards),
78         I40E_VEB_STAT("tx_errors", stats.tx_errors),
79         I40E_VEB_STAT("rx_unknown_protocol", stats.rx_unknown_protocol),
80 };
81
82 static const struct i40e_stats i40e_gstrings_misc_stats[] = {
83         I40E_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
84         I40E_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
85         I40E_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
86         I40E_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
87         I40E_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
88         I40E_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
89         I40E_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
90         I40E_VSI_STAT("tx_linearize", tx_linearize),
91         I40E_VSI_STAT("tx_force_wb", tx_force_wb),
92         I40E_VSI_STAT("tx_lost_interrupt", tx_lost_interrupt),
93         I40E_VSI_STAT("rx_alloc_fail", rx_buf_failed),
94         I40E_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
95 };
96
97 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
98  * but they are separate.  This device supports Virtualization, and
99  * as such might have several netdevs supporting VMDq and FCoE going
100  * through a single port.  The NETDEV_STATs are for individual netdevs
101  * seen at the top of the stack, and the PF_STATs are for the physical
102  * function at the bottom of the stack hosting those netdevs.
103  *
104  * The PF_STATs are appended to the netdev stats only when ethtool -S
105  * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
106  */
107 static const struct i40e_stats i40e_gstrings_stats[] = {
108         I40E_PF_STAT("rx_bytes", stats.eth.rx_bytes),
109         I40E_PF_STAT("tx_bytes", stats.eth.tx_bytes),
110         I40E_PF_STAT("rx_unicast", stats.eth.rx_unicast),
111         I40E_PF_STAT("tx_unicast", stats.eth.tx_unicast),
112         I40E_PF_STAT("rx_multicast", stats.eth.rx_multicast),
113         I40E_PF_STAT("tx_multicast", stats.eth.tx_multicast),
114         I40E_PF_STAT("rx_broadcast", stats.eth.rx_broadcast),
115         I40E_PF_STAT("tx_broadcast", stats.eth.tx_broadcast),
116         I40E_PF_STAT("tx_errors", stats.eth.tx_errors),
117         I40E_PF_STAT("rx_dropped", stats.eth.rx_discards),
118         I40E_PF_STAT("tx_dropped_link_down", stats.tx_dropped_link_down),
119         I40E_PF_STAT("rx_crc_errors", stats.crc_errors),
120         I40E_PF_STAT("illegal_bytes", stats.illegal_bytes),
121         I40E_PF_STAT("mac_local_faults", stats.mac_local_faults),
122         I40E_PF_STAT("mac_remote_faults", stats.mac_remote_faults),
123         I40E_PF_STAT("tx_timeout", tx_timeout_count),
124         I40E_PF_STAT("rx_csum_bad", hw_csum_rx_error),
125         I40E_PF_STAT("rx_length_errors", stats.rx_length_errors),
126         I40E_PF_STAT("link_xon_rx", stats.link_xon_rx),
127         I40E_PF_STAT("link_xoff_rx", stats.link_xoff_rx),
128         I40E_PF_STAT("link_xon_tx", stats.link_xon_tx),
129         I40E_PF_STAT("link_xoff_tx", stats.link_xoff_tx),
130         I40E_PF_STAT("rx_size_64", stats.rx_size_64),
131         I40E_PF_STAT("rx_size_127", stats.rx_size_127),
132         I40E_PF_STAT("rx_size_255", stats.rx_size_255),
133         I40E_PF_STAT("rx_size_511", stats.rx_size_511),
134         I40E_PF_STAT("rx_size_1023", stats.rx_size_1023),
135         I40E_PF_STAT("rx_size_1522", stats.rx_size_1522),
136         I40E_PF_STAT("rx_size_big", stats.rx_size_big),
137         I40E_PF_STAT("tx_size_64", stats.tx_size_64),
138         I40E_PF_STAT("tx_size_127", stats.tx_size_127),
139         I40E_PF_STAT("tx_size_255", stats.tx_size_255),
140         I40E_PF_STAT("tx_size_511", stats.tx_size_511),
141         I40E_PF_STAT("tx_size_1023", stats.tx_size_1023),
142         I40E_PF_STAT("tx_size_1522", stats.tx_size_1522),
143         I40E_PF_STAT("tx_size_big", stats.tx_size_big),
144         I40E_PF_STAT("rx_undersize", stats.rx_undersize),
145         I40E_PF_STAT("rx_fragments", stats.rx_fragments),
146         I40E_PF_STAT("rx_oversize", stats.rx_oversize),
147         I40E_PF_STAT("rx_jabber", stats.rx_jabber),
148         I40E_PF_STAT("VF_admin_queue_requests", vf_aq_requests),
149         I40E_PF_STAT("arq_overflows", arq_overflows),
150         I40E_PF_STAT("rx_hwtstamp_cleared", rx_hwtstamp_cleared),
151         I40E_PF_STAT("fdir_flush_cnt", fd_flush_cnt),
152         I40E_PF_STAT("fdir_atr_match", stats.fd_atr_match),
153         I40E_PF_STAT("fdir_atr_tunnel_match", stats.fd_atr_tunnel_match),
154         I40E_PF_STAT("fdir_atr_status", stats.fd_atr_status),
155         I40E_PF_STAT("fdir_sb_match", stats.fd_sb_match),
156         I40E_PF_STAT("fdir_sb_status", stats.fd_sb_status),
157
158         /* LPI stats */
159         I40E_PF_STAT("tx_lpi_status", stats.tx_lpi_status),
160         I40E_PF_STAT("rx_lpi_status", stats.rx_lpi_status),
161         I40E_PF_STAT("tx_lpi_count", stats.tx_lpi_count),
162         I40E_PF_STAT("rx_lpi_count", stats.rx_lpi_count),
163 };
164
165 #ifdef I40E_FCOE
166 static const struct i40e_stats i40e_gstrings_fcoe_stats[] = {
167         I40E_VSI_STAT("fcoe_bad_fccrc", fcoe_stats.fcoe_bad_fccrc),
168         I40E_VSI_STAT("rx_fcoe_dropped", fcoe_stats.rx_fcoe_dropped),
169         I40E_VSI_STAT("rx_fcoe_packets", fcoe_stats.rx_fcoe_packets),
170         I40E_VSI_STAT("rx_fcoe_dwords", fcoe_stats.rx_fcoe_dwords),
171         I40E_VSI_STAT("fcoe_ddp_count", fcoe_stats.fcoe_ddp_count),
172         I40E_VSI_STAT("fcoe_last_error", fcoe_stats.fcoe_last_error),
173         I40E_VSI_STAT("tx_fcoe_packets", fcoe_stats.tx_fcoe_packets),
174         I40E_VSI_STAT("tx_fcoe_dwords", fcoe_stats.tx_fcoe_dwords),
175 };
176
177 #endif /* I40E_FCOE */
178 #define I40E_QUEUE_STATS_LEN(n) \
179         (((struct i40e_netdev_priv *)netdev_priv((n)))->vsi->num_queue_pairs \
180             * 2 /* Tx and Rx together */                                     \
181             * (sizeof(struct i40e_queue_stats) / sizeof(u64)))
182 #define I40E_GLOBAL_STATS_LEN   ARRAY_SIZE(i40e_gstrings_stats)
183 #define I40E_NETDEV_STATS_LEN   ARRAY_SIZE(i40e_gstrings_net_stats)
184 #define I40E_MISC_STATS_LEN     ARRAY_SIZE(i40e_gstrings_misc_stats)
185 #ifdef I40E_FCOE
186 #define I40E_FCOE_STATS_LEN     ARRAY_SIZE(i40e_gstrings_fcoe_stats)
187 #define I40E_VSI_STATS_LEN(n)   (I40E_NETDEV_STATS_LEN + \
188                                  I40E_FCOE_STATS_LEN + \
189                                  I40E_MISC_STATS_LEN + \
190                                  I40E_QUEUE_STATS_LEN((n)))
191 #else
192 #define I40E_VSI_STATS_LEN(n)   (I40E_NETDEV_STATS_LEN + \
193                                  I40E_MISC_STATS_LEN + \
194                                  I40E_QUEUE_STATS_LEN((n)))
195 #endif /* I40E_FCOE */
196 #define I40E_PFC_STATS_LEN ( \
197                 (FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_rx) + \
198                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_rx) + \
199                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_tx) + \
200                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_tx) + \
201                  FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_2_xoff)) \
202                  / sizeof(u64))
203 #define I40E_VEB_TC_STATS_LEN ( \
204                 (FIELD_SIZEOF(struct i40e_veb, tc_stats.tc_rx_packets) + \
205                  FIELD_SIZEOF(struct i40e_veb, tc_stats.tc_rx_bytes) + \
206                  FIELD_SIZEOF(struct i40e_veb, tc_stats.tc_tx_packets) + \
207                  FIELD_SIZEOF(struct i40e_veb, tc_stats.tc_tx_bytes)) \
208                  / sizeof(u64))
209 #define I40E_VEB_STATS_LEN      ARRAY_SIZE(i40e_gstrings_veb_stats)
210 #define I40E_VEB_STATS_TOTAL    (I40E_VEB_STATS_LEN + I40E_VEB_TC_STATS_LEN)
211 #define I40E_PF_STATS_LEN(n)    (I40E_GLOBAL_STATS_LEN + \
212                                  I40E_PFC_STATS_LEN + \
213                                  I40E_VSI_STATS_LEN((n)))
214
215 enum i40e_ethtool_test_id {
216         I40E_ETH_TEST_REG = 0,
217         I40E_ETH_TEST_EEPROM,
218         I40E_ETH_TEST_INTR,
219         I40E_ETH_TEST_LINK,
220 };
221
222 static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
223         "Register test  (offline)",
224         "Eeprom test    (offline)",
225         "Interrupt test (offline)",
226         "Link test   (on/offline)"
227 };
228
229 #define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
230
231 static const char i40e_priv_flags_strings[][ETH_GSTRING_LEN] = {
232         "MFP",
233         "LinkPolling",
234         "flow-director-atr",
235         "veb-stats",
236         "hw-atr-eviction",
237 };
238
239 #define I40E_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_priv_flags_strings)
240
241 /* Private flags with a global effect, restricted to PF 0 */
242 static const char i40e_gl_priv_flags_strings[][ETH_GSTRING_LEN] = {
243         "vf-true-promisc-support",
244 };
245
246 #define I40E_GL_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_gl_priv_flags_strings)
247
248 /**
249  * i40e_partition_setting_complaint - generic complaint for MFP restriction
250  * @pf: the PF struct
251  **/
252 static void i40e_partition_setting_complaint(struct i40e_pf *pf)
253 {
254         dev_info(&pf->pdev->dev,
255                  "The link settings are allowed to be changed only from the first partition of a given port. Please switch to the first partition in order to change the setting.\n");
256 }
257
258 /**
259  * i40e_phy_type_to_ethtool - convert the phy_types to ethtool link modes
260  * @phy_types: PHY types to convert
261  * @supported: pointer to the ethtool supported variable to fill in
262  * @advertising: pointer to the ethtool advertising variable to fill in
263  *
264  **/
265 static void i40e_phy_type_to_ethtool(struct i40e_pf *pf, u32 *supported,
266                                      u32 *advertising)
267 {
268         struct i40e_link_status *hw_link_info = &pf->hw.phy.link_info;
269         u64 phy_types = pf->hw.phy.phy_types;
270
271         *supported = 0x0;
272         *advertising = 0x0;
273
274         if (phy_types & I40E_CAP_PHY_TYPE_SGMII) {
275                 *supported |= SUPPORTED_Autoneg |
276                               SUPPORTED_1000baseT_Full;
277                 *advertising |= ADVERTISED_Autoneg;
278                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
279                         *advertising |= ADVERTISED_1000baseT_Full;
280                 if (pf->flags & I40E_FLAG_100M_SGMII_CAPABLE) {
281                         *supported |= SUPPORTED_100baseT_Full;
282                         *advertising |= ADVERTISED_100baseT_Full;
283                 }
284         }
285         if (phy_types & I40E_CAP_PHY_TYPE_XAUI ||
286             phy_types & I40E_CAP_PHY_TYPE_XFI ||
287             phy_types & I40E_CAP_PHY_TYPE_SFI ||
288             phy_types & I40E_CAP_PHY_TYPE_10GBASE_SFPP_CU ||
289             phy_types & I40E_CAP_PHY_TYPE_10GBASE_AOC)
290                 *supported |= SUPPORTED_10000baseT_Full;
291         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU ||
292             phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
293             phy_types & I40E_CAP_PHY_TYPE_10GBASE_T ||
294             phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR ||
295             phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR) {
296                 *supported |= SUPPORTED_Autoneg |
297                               SUPPORTED_10000baseT_Full;
298                 *advertising |= ADVERTISED_Autoneg;
299                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
300                         *advertising |= ADVERTISED_10000baseT_Full;
301         }
302         if (phy_types & I40E_CAP_PHY_TYPE_XLAUI ||
303             phy_types & I40E_CAP_PHY_TYPE_XLPPI ||
304             phy_types & I40E_CAP_PHY_TYPE_40GBASE_AOC)
305                 *supported |= SUPPORTED_40000baseCR4_Full;
306         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
307             phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4) {
308                 *supported |= SUPPORTED_Autoneg |
309                               SUPPORTED_40000baseCR4_Full;
310                 *advertising |= ADVERTISED_Autoneg;
311                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_40GB)
312                         *advertising |= ADVERTISED_40000baseCR4_Full;
313         }
314         if (phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) {
315                 *supported |= SUPPORTED_Autoneg |
316                               SUPPORTED_100baseT_Full;
317                 *advertising |= ADVERTISED_Autoneg;
318                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
319                         *advertising |= ADVERTISED_100baseT_Full;
320         }
321         if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_T ||
322             phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
323             phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
324             phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL) {
325                 *supported |= SUPPORTED_Autoneg |
326                               SUPPORTED_1000baseT_Full;
327                 *advertising |= ADVERTISED_Autoneg;
328                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
329                         *advertising |= ADVERTISED_1000baseT_Full;
330         }
331         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_SR4)
332                 *supported |= SUPPORTED_40000baseSR4_Full;
333         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_LR4)
334                 *supported |= SUPPORTED_40000baseLR4_Full;
335         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4) {
336                 *supported |= SUPPORTED_40000baseKR4_Full |
337                               SUPPORTED_Autoneg;
338                 *advertising |= ADVERTISED_40000baseKR4_Full |
339                                 ADVERTISED_Autoneg;
340         }
341         if (phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2) {
342                 *supported |= SUPPORTED_20000baseKR2_Full |
343                               SUPPORTED_Autoneg;
344                 *advertising |= ADVERTISED_Autoneg;
345                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_20GB)
346                         *advertising |= ADVERTISED_20000baseKR2_Full;
347         }
348         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR) {
349                 if (!(pf->flags & I40E_FLAG_HAVE_CRT_RETIMER))
350                         *supported |= SUPPORTED_10000baseKR_Full |
351                                       SUPPORTED_Autoneg;
352                 *advertising |= ADVERTISED_Autoneg;
353                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
354                         if (!(pf->flags & I40E_FLAG_HAVE_CRT_RETIMER))
355                                 *advertising |= ADVERTISED_10000baseKR_Full;
356         }
357         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4) {
358                 *supported |= SUPPORTED_10000baseKX4_Full |
359                               SUPPORTED_Autoneg;
360                 *advertising |= ADVERTISED_Autoneg;
361                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
362                         *advertising |= ADVERTISED_10000baseKX4_Full;
363         }
364         if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX) {
365                 if (!(pf->flags & I40E_FLAG_HAVE_CRT_RETIMER))
366                         *supported |= SUPPORTED_1000baseKX_Full |
367                                       SUPPORTED_Autoneg;
368                 *advertising |= ADVERTISED_Autoneg;
369                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
370                         if (!(pf->flags & I40E_FLAG_HAVE_CRT_RETIMER))
371                                 *advertising |= ADVERTISED_1000baseKX_Full;
372         }
373         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR ||
374             phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR ||
375             phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
376             phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR) {
377                 *supported |= SUPPORTED_Autoneg;
378                 *advertising |= ADVERTISED_Autoneg;
379         }
380 }
381
382 /**
383  * i40e_get_settings_link_up - Get the Link settings for when link is up
384  * @hw: hw structure
385  * @ecmd: ethtool command to fill in
386  * @netdev: network interface device structure
387  *
388  **/
389 static void i40e_get_settings_link_up(struct i40e_hw *hw,
390                                       struct ethtool_link_ksettings *cmd,
391                                       struct net_device *netdev,
392                                       struct i40e_pf *pf)
393 {
394         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
395         u32 link_speed = hw_link_info->link_speed;
396         u32 e_advertising = 0x0;
397         u32 e_supported = 0x0;
398         u32 supported, advertising;
399
400         ethtool_convert_link_mode_to_legacy_u32(&supported,
401                                                 cmd->link_modes.supported);
402         ethtool_convert_link_mode_to_legacy_u32(&advertising,
403                                                 cmd->link_modes.advertising);
404
405         /* Initialize supported and advertised settings based on phy settings */
406         switch (hw_link_info->phy_type) {
407         case I40E_PHY_TYPE_40GBASE_CR4:
408         case I40E_PHY_TYPE_40GBASE_CR4_CU:
409                 supported = SUPPORTED_Autoneg |
410                             SUPPORTED_40000baseCR4_Full;
411                 advertising = ADVERTISED_Autoneg |
412                               ADVERTISED_40000baseCR4_Full;
413                 break;
414         case I40E_PHY_TYPE_XLAUI:
415         case I40E_PHY_TYPE_XLPPI:
416         case I40E_PHY_TYPE_40GBASE_AOC:
417                 supported = SUPPORTED_40000baseCR4_Full;
418                 break;
419         case I40E_PHY_TYPE_40GBASE_SR4:
420                 supported = SUPPORTED_40000baseSR4_Full;
421                 break;
422         case I40E_PHY_TYPE_40GBASE_LR4:
423                 supported = SUPPORTED_40000baseLR4_Full;
424                 break;
425         case I40E_PHY_TYPE_10GBASE_SR:
426         case I40E_PHY_TYPE_10GBASE_LR:
427         case I40E_PHY_TYPE_1000BASE_SX:
428         case I40E_PHY_TYPE_1000BASE_LX:
429                 supported = SUPPORTED_10000baseT_Full;
430                 if (hw_link_info->module_type[2] &
431                     I40E_MODULE_TYPE_1000BASE_SX ||
432                     hw_link_info->module_type[2] &
433                     I40E_MODULE_TYPE_1000BASE_LX) {
434                         supported |= SUPPORTED_1000baseT_Full;
435                         if (hw_link_info->requested_speeds &
436                             I40E_LINK_SPEED_1GB)
437                                 advertising |= ADVERTISED_1000baseT_Full;
438                 }
439                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
440                         advertising |= ADVERTISED_10000baseT_Full;
441                 break;
442         case I40E_PHY_TYPE_10GBASE_T:
443         case I40E_PHY_TYPE_1000BASE_T:
444         case I40E_PHY_TYPE_100BASE_TX:
445                 supported = SUPPORTED_Autoneg |
446                             SUPPORTED_10000baseT_Full |
447                             SUPPORTED_1000baseT_Full |
448                             SUPPORTED_100baseT_Full;
449                 advertising = ADVERTISED_Autoneg;
450                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
451                         advertising |= ADVERTISED_10000baseT_Full;
452                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
453                         advertising |= ADVERTISED_1000baseT_Full;
454                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
455                         advertising |= ADVERTISED_100baseT_Full;
456                 break;
457         case I40E_PHY_TYPE_1000BASE_T_OPTICAL:
458                 supported = SUPPORTED_Autoneg |
459                             SUPPORTED_1000baseT_Full;
460                 advertising = ADVERTISED_Autoneg |
461                               ADVERTISED_1000baseT_Full;
462                 break;
463         case I40E_PHY_TYPE_10GBASE_CR1_CU:
464         case I40E_PHY_TYPE_10GBASE_CR1:
465                 supported = SUPPORTED_Autoneg |
466                             SUPPORTED_10000baseT_Full;
467                 advertising = ADVERTISED_Autoneg |
468                               ADVERTISED_10000baseT_Full;
469                 break;
470         case I40E_PHY_TYPE_XAUI:
471         case I40E_PHY_TYPE_XFI:
472         case I40E_PHY_TYPE_SFI:
473         case I40E_PHY_TYPE_10GBASE_SFPP_CU:
474         case I40E_PHY_TYPE_10GBASE_AOC:
475                 supported = SUPPORTED_10000baseT_Full;
476                 advertising = SUPPORTED_10000baseT_Full;
477                 break;
478         case I40E_PHY_TYPE_SGMII:
479                 supported = SUPPORTED_Autoneg |
480                             SUPPORTED_1000baseT_Full;
481                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
482                         advertising |= ADVERTISED_1000baseT_Full;
483                 if (pf->flags & I40E_FLAG_100M_SGMII_CAPABLE) {
484                         supported |= SUPPORTED_100baseT_Full;
485                         if (hw_link_info->requested_speeds &
486                             I40E_LINK_SPEED_100MB)
487                                 advertising |= ADVERTISED_100baseT_Full;
488                 }
489                 break;
490         case I40E_PHY_TYPE_40GBASE_KR4:
491         case I40E_PHY_TYPE_20GBASE_KR2:
492         case I40E_PHY_TYPE_10GBASE_KR:
493         case I40E_PHY_TYPE_10GBASE_KX4:
494         case I40E_PHY_TYPE_1000BASE_KX:
495                 supported |= SUPPORTED_40000baseKR4_Full |
496                              SUPPORTED_20000baseKR2_Full |
497                              SUPPORTED_10000baseKR_Full |
498                              SUPPORTED_10000baseKX4_Full |
499                              SUPPORTED_1000baseKX_Full |
500                              SUPPORTED_Autoneg;
501                 advertising |= ADVERTISED_40000baseKR4_Full |
502                                ADVERTISED_20000baseKR2_Full |
503                                ADVERTISED_10000baseKR_Full |
504                                ADVERTISED_10000baseKX4_Full |
505                                ADVERTISED_1000baseKX_Full |
506                                ADVERTISED_Autoneg;
507                 break;
508         case I40E_PHY_TYPE_25GBASE_KR:
509         case I40E_PHY_TYPE_25GBASE_CR:
510         case I40E_PHY_TYPE_25GBASE_SR:
511         case I40E_PHY_TYPE_25GBASE_LR:
512                 supported = SUPPORTED_Autoneg;
513                 advertising = ADVERTISED_Autoneg;
514                 /* TODO: add speeds when ethtool is ready to support*/
515                 break;
516         default:
517                 /* if we got here and link is up something bad is afoot */
518                 netdev_info(netdev, "WARNING: Link is up but PHY type 0x%x is not recognized.\n",
519                             hw_link_info->phy_type);
520         }
521
522         /* Now that we've worked out everything that could be supported by the
523          * current PHY type, get what is supported by the NVM and them to
524          * get what is truly supported
525          */
526         i40e_phy_type_to_ethtool(pf, &e_supported,
527                                  &e_advertising);
528
529         supported = supported & e_supported;
530         advertising = advertising & e_advertising;
531
532         /* Set speed and duplex */
533         switch (link_speed) {
534         case I40E_LINK_SPEED_40GB:
535                 cmd->base.speed = SPEED_40000;
536                 break;
537         case I40E_LINK_SPEED_25GB:
538 #ifdef SPEED_25000
539                 cmd->base.speed = SPEED_25000;
540 #else
541                 netdev_info(netdev,
542                             "Speed is 25G, display not supported by this version of ethtool.\n");
543 #endif
544                 break;
545         case I40E_LINK_SPEED_20GB:
546                 cmd->base.speed = SPEED_20000;
547                 break;
548         case I40E_LINK_SPEED_10GB:
549                 cmd->base.speed = SPEED_10000;
550                 break;
551         case I40E_LINK_SPEED_1GB:
552                 cmd->base.speed = SPEED_1000;
553                 break;
554         case I40E_LINK_SPEED_100MB:
555                 cmd->base.speed = SPEED_100;
556                 break;
557         default:
558                 break;
559         }
560         cmd->base.duplex = DUPLEX_FULL;
561
562         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
563                                                 supported);
564         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
565                                                 advertising);
566 }
567
568 /**
569  * i40e_get_settings_link_down - Get the Link settings for when link is down
570  * @hw: hw structure
571  * @ecmd: ethtool command to fill in
572  *
573  * Reports link settings that can be determined when link is down
574  **/
575 static void i40e_get_settings_link_down(struct i40e_hw *hw,
576                                         struct ethtool_link_ksettings *cmd,
577                                         struct i40e_pf *pf)
578 {
579         u32 supported, advertising;
580
581         /* link is down and the driver needs to fall back on
582          * supported phy types to figure out what info to display
583          */
584         i40e_phy_type_to_ethtool(pf, &supported, &advertising);
585
586         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
587                                                 supported);
588         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
589                                                 advertising);
590
591         /* With no link speed and duplex are unknown */
592         cmd->base.speed = SPEED_UNKNOWN;
593         cmd->base.duplex = DUPLEX_UNKNOWN;
594 }
595
596 /**
597  * i40e_get_settings - Get Link Speed and Duplex settings
598  * @netdev: network interface device structure
599  * @ecmd: ethtool command
600  *
601  * Reports speed/duplex settings based on media_type
602  **/
603 static int i40e_get_link_ksettings(struct net_device *netdev,
604                                    struct ethtool_link_ksettings *cmd)
605 {
606         struct i40e_netdev_priv *np = netdev_priv(netdev);
607         struct i40e_pf *pf = np->vsi->back;
608         struct i40e_hw *hw = &pf->hw;
609         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
610         bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
611         u32 advertising;
612
613         if (link_up)
614                 i40e_get_settings_link_up(hw, cmd, netdev, pf);
615         else
616                 i40e_get_settings_link_down(hw, cmd, pf);
617
618         /* Now set the settings that don't rely on link being up/down */
619         /* Set autoneg settings */
620         cmd->base.autoneg = ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
621                           AUTONEG_ENABLE : AUTONEG_DISABLE);
622
623         switch (hw->phy.media_type) {
624         case I40E_MEDIA_TYPE_BACKPLANE:
625                 ethtool_link_ksettings_add_link_mode(cmd, supported,
626                                                      Autoneg);
627                 ethtool_link_ksettings_add_link_mode(cmd, supported,
628                                                      Backplane);
629                 ethtool_link_ksettings_add_link_mode(cmd, advertising,
630                                                      Autoneg);
631                 ethtool_link_ksettings_add_link_mode(cmd, advertising,
632                                                      Backplane);
633                 cmd->base.port = PORT_NONE;
634                 break;
635         case I40E_MEDIA_TYPE_BASET:
636                 ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
637                 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
638                 cmd->base.port = PORT_TP;
639                 break;
640         case I40E_MEDIA_TYPE_DA:
641         case I40E_MEDIA_TYPE_CX4:
642                 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
643                 ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
644                 cmd->base.port = PORT_DA;
645                 break;
646         case I40E_MEDIA_TYPE_FIBER:
647                 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
648                 cmd->base.port = PORT_FIBRE;
649                 break;
650         case I40E_MEDIA_TYPE_UNKNOWN:
651         default:
652                 cmd->base.port = PORT_OTHER;
653                 break;
654         }
655
656         /* Set flow control settings */
657         ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
658
659         switch (hw->fc.requested_mode) {
660         case I40E_FC_FULL:
661                 ethtool_link_ksettings_add_link_mode(cmd, advertising,
662                                                      Pause);
663                 break;
664         case I40E_FC_TX_PAUSE:
665                 ethtool_link_ksettings_add_link_mode(cmd, advertising,
666                                                      Asym_Pause);
667                 break;
668         case I40E_FC_RX_PAUSE:
669                 ethtool_link_ksettings_add_link_mode(cmd, advertising,
670                                                      Pause);
671                 ethtool_link_ksettings_add_link_mode(cmd, advertising,
672                                                      Asym_Pause);
673                 break;
674         default:
675                 ethtool_convert_link_mode_to_legacy_u32(
676                         &advertising, cmd->link_modes.advertising);
677
678                 advertising &= ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
679
680                 ethtool_convert_legacy_u32_to_link_mode(
681                         cmd->link_modes.advertising, advertising);
682                 break;
683         }
684
685         return 0;
686 }
687
688 /**
689  * i40e_set_settings - Set Speed and Duplex
690  * @netdev: network interface device structure
691  * @ecmd: ethtool command
692  *
693  * Set speed/duplex per media_types advertised/forced
694  **/
695 static int i40e_set_link_ksettings(struct net_device *netdev,
696                                    const struct ethtool_link_ksettings *cmd)
697 {
698         struct i40e_netdev_priv *np = netdev_priv(netdev);
699         struct i40e_aq_get_phy_abilities_resp abilities;
700         struct i40e_aq_set_phy_config config;
701         struct i40e_pf *pf = np->vsi->back;
702         struct i40e_vsi *vsi = np->vsi;
703         struct i40e_hw *hw = &pf->hw;
704         struct ethtool_link_ksettings safe_cmd;
705         struct ethtool_link_ksettings copy_cmd;
706         i40e_status status = 0;
707         bool change = false;
708         int err = 0;
709         u32 autoneg;
710         u32 advertise;
711         u32 tmp;
712
713         /* Changing port settings is not supported if this isn't the
714          * port's controlling PF
715          */
716         if (hw->partition_id != 1) {
717                 i40e_partition_setting_complaint(pf);
718                 return -EOPNOTSUPP;
719         }
720
721         if (vsi != pf->vsi[pf->lan_vsi])
722                 return -EOPNOTSUPP;
723
724         if (hw->phy.media_type != I40E_MEDIA_TYPE_BASET &&
725             hw->phy.media_type != I40E_MEDIA_TYPE_FIBER &&
726             hw->phy.media_type != I40E_MEDIA_TYPE_BACKPLANE &&
727             hw->phy.media_type != I40E_MEDIA_TYPE_DA &&
728             hw->phy.link_info.link_info & I40E_AQ_LINK_UP)
729                 return -EOPNOTSUPP;
730
731         if (hw->device_id == I40E_DEV_ID_KX_B ||
732             hw->device_id == I40E_DEV_ID_KX_C ||
733             hw->device_id == I40E_DEV_ID_20G_KR2 ||
734             hw->device_id == I40E_DEV_ID_20G_KR2_A) {
735                 netdev_info(netdev, "Changing settings is not supported on backplane.\n");
736                 return -EOPNOTSUPP;
737         }
738
739         /* copy the cmd to copy_cmd to avoid modifying the origin */
740         memcpy(&copy_cmd, cmd, sizeof(struct ethtool_link_ksettings));
741
742         /* get our own copy of the bits to check against */
743         memset(&safe_cmd, 0, sizeof(struct ethtool_link_ksettings));
744         i40e_get_link_ksettings(netdev, &safe_cmd);
745
746         /* save autoneg and speed out of cmd */
747         autoneg = cmd->base.autoneg;
748         ethtool_convert_link_mode_to_legacy_u32(&advertise,
749                                                 cmd->link_modes.advertising);
750
751         /* set autoneg and speed back to what they currently are */
752         copy_cmd.base.autoneg = safe_cmd.base.autoneg;
753         ethtool_convert_link_mode_to_legacy_u32(
754                 &tmp, safe_cmd.link_modes.advertising);
755         ethtool_convert_legacy_u32_to_link_mode(
756                 copy_cmd.link_modes.advertising, tmp);
757
758         copy_cmd.base.cmd = safe_cmd.base.cmd;
759
760         /* If copy_cmd and safe_cmd are not the same now, then they are
761          * trying to set something that we do not support
762          */
763         if (memcmp(&copy_cmd, &safe_cmd, sizeof(struct ethtool_link_ksettings)))
764                 return -EOPNOTSUPP;
765
766         while (test_bit(__I40E_CONFIG_BUSY, &vsi->state))
767                 usleep_range(1000, 2000);
768
769         /* Get the current phy config */
770         status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
771                                               NULL);
772         if (status)
773                 return -EAGAIN;
774
775         /* Copy abilities to config in case autoneg is not
776          * set below
777          */
778         memset(&config, 0, sizeof(struct i40e_aq_set_phy_config));
779         config.abilities = abilities.abilities;
780
781         /* Check autoneg */
782         if (autoneg == AUTONEG_ENABLE) {
783                 /* If autoneg was not already enabled */
784                 if (!(hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED)) {
785                         /* If autoneg is not supported, return error */
786                         if (!ethtool_link_ksettings_test_link_mode(
787                                     &safe_cmd, supported, Autoneg)) {
788                                 netdev_info(netdev, "Autoneg not supported on this phy\n");
789                                 return -EINVAL;
790                         }
791                         /* Autoneg is allowed to change */
792                         config.abilities = abilities.abilities |
793                                            I40E_AQ_PHY_ENABLE_AN;
794                         change = true;
795                 }
796         } else {
797                 /* If autoneg is currently enabled */
798                 if (hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED) {
799                         /* If autoneg is supported 10GBASE_T is the only PHY
800                          * that can disable it, so otherwise return error
801                          */
802                         if (ethtool_link_ksettings_test_link_mode(
803                                     &safe_cmd, supported, Autoneg) &&
804                             hw->phy.link_info.phy_type !=
805                             I40E_PHY_TYPE_10GBASE_T) {
806                                 netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
807                                 return -EINVAL;
808                         }
809                         /* Autoneg is allowed to change */
810                         config.abilities = abilities.abilities &
811                                            ~I40E_AQ_PHY_ENABLE_AN;
812                         change = true;
813                 }
814         }
815
816         ethtool_convert_link_mode_to_legacy_u32(&tmp,
817                                                 safe_cmd.link_modes.supported);
818         if (advertise & ~tmp)
819                 return -EINVAL;
820
821         if (advertise & ADVERTISED_100baseT_Full)
822                 config.link_speed |= I40E_LINK_SPEED_100MB;
823         if (advertise & ADVERTISED_1000baseT_Full ||
824             advertise & ADVERTISED_1000baseKX_Full)
825                 config.link_speed |= I40E_LINK_SPEED_1GB;
826         if (advertise & ADVERTISED_10000baseT_Full ||
827             advertise & ADVERTISED_10000baseKX4_Full ||
828             advertise & ADVERTISED_10000baseKR_Full)
829                 config.link_speed |= I40E_LINK_SPEED_10GB;
830         if (advertise & ADVERTISED_20000baseKR2_Full)
831                 config.link_speed |= I40E_LINK_SPEED_20GB;
832         if (advertise & ADVERTISED_40000baseKR4_Full ||
833             advertise & ADVERTISED_40000baseCR4_Full ||
834             advertise & ADVERTISED_40000baseSR4_Full ||
835             advertise & ADVERTISED_40000baseLR4_Full)
836                 config.link_speed |= I40E_LINK_SPEED_40GB;
837
838         /* If speed didn't get set, set it to what it currently is.
839          * This is needed because if advertise is 0 (as it is when autoneg
840          * is disabled) then speed won't get set.
841          */
842         if (!config.link_speed)
843                 config.link_speed = abilities.link_speed;
844
845         if (change || (abilities.link_speed != config.link_speed)) {
846                 /* copy over the rest of the abilities */
847                 config.phy_type = abilities.phy_type;
848                 config.phy_type_ext = abilities.phy_type_ext;
849                 config.eee_capability = abilities.eee_capability;
850                 config.eeer = abilities.eeer_val;
851                 config.low_power_ctrl = abilities.d3_lpan;
852                 config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
853                                     I40E_AQ_PHY_FEC_CONFIG_MASK;
854
855                 /* save the requested speeds */
856                 hw->phy.link_info.requested_speeds = config.link_speed;
857                 /* set link and auto negotiation so changes take effect */
858                 config.abilities |= I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
859                 /* If link is up put link down */
860                 if (hw->phy.link_info.link_info & I40E_AQ_LINK_UP) {
861                         /* Tell the OS link is going down, the link will go
862                          * back up when fw says it is ready asynchronously
863                          */
864                         i40e_print_link_message(vsi, false);
865                         netif_carrier_off(netdev);
866                         netif_tx_stop_all_queues(netdev);
867                 }
868
869                 /* make the aq call */
870                 status = i40e_aq_set_phy_config(hw, &config, NULL);
871                 if (status) {
872                         netdev_info(netdev, "Set phy config failed, err %s aq_err %s\n",
873                                     i40e_stat_str(hw, status),
874                                     i40e_aq_str(hw, hw->aq.asq_last_status));
875                         return -EAGAIN;
876                 }
877
878                 status = i40e_update_link_info(hw);
879                 if (status)
880                         netdev_dbg(netdev, "Updating link info failed with err %s aq_err %s\n",
881                                    i40e_stat_str(hw, status),
882                                    i40e_aq_str(hw, hw->aq.asq_last_status));
883
884         } else {
885                 netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
886         }
887
888         return err;
889 }
890
891 static int i40e_nway_reset(struct net_device *netdev)
892 {
893         /* restart autonegotiation */
894         struct i40e_netdev_priv *np = netdev_priv(netdev);
895         struct i40e_pf *pf = np->vsi->back;
896         struct i40e_hw *hw = &pf->hw;
897         bool link_up = hw->phy.link_info.link_info & I40E_AQ_LINK_UP;
898         i40e_status ret = 0;
899
900         ret = i40e_aq_set_link_restart_an(hw, link_up, NULL);
901         if (ret) {
902                 netdev_info(netdev, "link restart failed, err %s aq_err %s\n",
903                             i40e_stat_str(hw, ret),
904                             i40e_aq_str(hw, hw->aq.asq_last_status));
905                 return -EIO;
906         }
907
908         return 0;
909 }
910
911 /**
912  * i40e_get_pauseparam -  Get Flow Control status
913  * Return tx/rx-pause status
914  **/
915 static void i40e_get_pauseparam(struct net_device *netdev,
916                                 struct ethtool_pauseparam *pause)
917 {
918         struct i40e_netdev_priv *np = netdev_priv(netdev);
919         struct i40e_pf *pf = np->vsi->back;
920         struct i40e_hw *hw = &pf->hw;
921         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
922         struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
923
924         pause->autoneg =
925                 ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
926                   AUTONEG_ENABLE : AUTONEG_DISABLE);
927
928         /* PFC enabled so report LFC as off */
929         if (dcbx_cfg->pfc.pfcenable) {
930                 pause->rx_pause = 0;
931                 pause->tx_pause = 0;
932                 return;
933         }
934
935         if (hw->fc.current_mode == I40E_FC_RX_PAUSE) {
936                 pause->rx_pause = 1;
937         } else if (hw->fc.current_mode == I40E_FC_TX_PAUSE) {
938                 pause->tx_pause = 1;
939         } else if (hw->fc.current_mode == I40E_FC_FULL) {
940                 pause->rx_pause = 1;
941                 pause->tx_pause = 1;
942         }
943 }
944
945 /**
946  * i40e_set_pauseparam - Set Flow Control parameter
947  * @netdev: network interface device structure
948  * @pause: return tx/rx flow control status
949  **/
950 static int i40e_set_pauseparam(struct net_device *netdev,
951                                struct ethtool_pauseparam *pause)
952 {
953         struct i40e_netdev_priv *np = netdev_priv(netdev);
954         struct i40e_pf *pf = np->vsi->back;
955         struct i40e_vsi *vsi = np->vsi;
956         struct i40e_hw *hw = &pf->hw;
957         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
958         struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
959         bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
960         i40e_status status;
961         u8 aq_failures;
962         int err = 0;
963
964         /* Changing the port's flow control is not supported if this isn't the
965          * port's controlling PF
966          */
967         if (hw->partition_id != 1) {
968                 i40e_partition_setting_complaint(pf);
969                 return -EOPNOTSUPP;
970         }
971
972         if (vsi != pf->vsi[pf->lan_vsi])
973                 return -EOPNOTSUPP;
974
975         if (pause->autoneg != ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
976             AUTONEG_ENABLE : AUTONEG_DISABLE)) {
977                 netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
978                 return -EOPNOTSUPP;
979         }
980
981         /* If we have link and don't have autoneg */
982         if (!test_bit(__I40E_DOWN, &pf->state) &&
983             !(hw_link_info->an_info & I40E_AQ_AN_COMPLETED)) {
984                 /* Send message that it might not necessarily work*/
985                 netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
986         }
987
988         if (dcbx_cfg->pfc.pfcenable) {
989                 netdev_info(netdev,
990                             "Priority flow control enabled. Cannot set link flow control.\n");
991                 return -EOPNOTSUPP;
992         }
993
994         if (pause->rx_pause && pause->tx_pause)
995                 hw->fc.requested_mode = I40E_FC_FULL;
996         else if (pause->rx_pause && !pause->tx_pause)
997                 hw->fc.requested_mode = I40E_FC_RX_PAUSE;
998         else if (!pause->rx_pause && pause->tx_pause)
999                 hw->fc.requested_mode = I40E_FC_TX_PAUSE;
1000         else if (!pause->rx_pause && !pause->tx_pause)
1001                 hw->fc.requested_mode = I40E_FC_NONE;
1002         else
1003                  return -EINVAL;
1004
1005         /* Tell the OS link is going down, the link will go back up when fw
1006          * says it is ready asynchronously
1007          */
1008         i40e_print_link_message(vsi, false);
1009         netif_carrier_off(netdev);
1010         netif_tx_stop_all_queues(netdev);
1011
1012         /* Set the fc mode and only restart an if link is up*/
1013         status = i40e_set_fc(hw, &aq_failures, link_up);
1014
1015         if (aq_failures & I40E_SET_FC_AQ_FAIL_GET) {
1016                 netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %s aq_err %s\n",
1017                             i40e_stat_str(hw, status),
1018                             i40e_aq_str(hw, hw->aq.asq_last_status));
1019                 err = -EAGAIN;
1020         }
1021         if (aq_failures & I40E_SET_FC_AQ_FAIL_SET) {
1022                 netdev_info(netdev, "Set fc failed on the set_phy_config call with err %s aq_err %s\n",
1023                             i40e_stat_str(hw, status),
1024                             i40e_aq_str(hw, hw->aq.asq_last_status));
1025                 err = -EAGAIN;
1026         }
1027         if (aq_failures & I40E_SET_FC_AQ_FAIL_UPDATE) {
1028                 netdev_info(netdev, "Set fc failed on the get_link_info call with err %s aq_err %s\n",
1029                             i40e_stat_str(hw, status),
1030                             i40e_aq_str(hw, hw->aq.asq_last_status));
1031                 err = -EAGAIN;
1032         }
1033
1034         if (!test_bit(__I40E_DOWN, &pf->state)) {
1035                 /* Give it a little more time to try to come back */
1036                 msleep(75);
1037                 if (!test_bit(__I40E_DOWN, &pf->state))
1038                         return i40e_nway_reset(netdev);
1039         }
1040
1041         return err;
1042 }
1043
1044 static u32 i40e_get_msglevel(struct net_device *netdev)
1045 {
1046         struct i40e_netdev_priv *np = netdev_priv(netdev);
1047         struct i40e_pf *pf = np->vsi->back;
1048         u32 debug_mask = pf->hw.debug_mask;
1049
1050         if (debug_mask)
1051                 netdev_info(netdev, "i40e debug_mask: 0x%08X\n", debug_mask);
1052
1053         return pf->msg_enable;
1054 }
1055
1056 static void i40e_set_msglevel(struct net_device *netdev, u32 data)
1057 {
1058         struct i40e_netdev_priv *np = netdev_priv(netdev);
1059         struct i40e_pf *pf = np->vsi->back;
1060
1061         if (I40E_DEBUG_USER & data)
1062                 pf->hw.debug_mask = data;
1063         else
1064                 pf->msg_enable = data;
1065 }
1066
1067 static int i40e_get_regs_len(struct net_device *netdev)
1068 {
1069         int reg_count = 0;
1070         int i;
1071
1072         for (i = 0; i40e_reg_list[i].offset != 0; i++)
1073                 reg_count += i40e_reg_list[i].elements;
1074
1075         return reg_count * sizeof(u32);
1076 }
1077
1078 static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
1079                           void *p)
1080 {
1081         struct i40e_netdev_priv *np = netdev_priv(netdev);
1082         struct i40e_pf *pf = np->vsi->back;
1083         struct i40e_hw *hw = &pf->hw;
1084         u32 *reg_buf = p;
1085         int i, j, ri;
1086         u32 reg;
1087
1088         /* Tell ethtool which driver-version-specific regs output we have.
1089          *
1090          * At some point, if we have ethtool doing special formatting of
1091          * this data, it will rely on this version number to know how to
1092          * interpret things.  Hence, this needs to be updated if/when the
1093          * diags register table is changed.
1094          */
1095         regs->version = 1;
1096
1097         /* loop through the diags reg table for what to print */
1098         ri = 0;
1099         for (i = 0; i40e_reg_list[i].offset != 0; i++) {
1100                 for (j = 0; j < i40e_reg_list[i].elements; j++) {
1101                         reg = i40e_reg_list[i].offset
1102                                 + (j * i40e_reg_list[i].stride);
1103                         reg_buf[ri++] = rd32(hw, reg);
1104                 }
1105         }
1106
1107 }
1108
1109 static int i40e_get_eeprom(struct net_device *netdev,
1110                            struct ethtool_eeprom *eeprom, u8 *bytes)
1111 {
1112         struct i40e_netdev_priv *np = netdev_priv(netdev);
1113         struct i40e_hw *hw = &np->vsi->back->hw;
1114         struct i40e_pf *pf = np->vsi->back;
1115         int ret_val = 0, len, offset;
1116         u8 *eeprom_buff;
1117         u16 i, sectors;
1118         bool last;
1119         u32 magic;
1120
1121 #define I40E_NVM_SECTOR_SIZE  4096
1122         if (eeprom->len == 0)
1123                 return -EINVAL;
1124
1125         /* check for NVMUpdate access method */
1126         magic = hw->vendor_id | (hw->device_id << 16);
1127         if (eeprom->magic && eeprom->magic != magic) {
1128                 struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1129                 int errno = 0;
1130
1131                 /* make sure it is the right magic for NVMUpdate */
1132                 if ((eeprom->magic >> 16) != hw->device_id)
1133                         errno = -EINVAL;
1134                 else if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
1135                          test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
1136                         errno = -EBUSY;
1137                 else
1138                         ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1139
1140                 if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1141                         dev_info(&pf->pdev->dev,
1142                                  "NVMUpdate read failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1143                                  ret_val, hw->aq.asq_last_status, errno,
1144                                  (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1145                                  cmd->offset, cmd->data_size);
1146
1147                 return errno;
1148         }
1149
1150         /* normal ethtool get_eeprom support */
1151         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
1152
1153         eeprom_buff = kzalloc(eeprom->len, GFP_KERNEL);
1154         if (!eeprom_buff)
1155                 return -ENOMEM;
1156
1157         ret_val = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
1158         if (ret_val) {
1159                 dev_info(&pf->pdev->dev,
1160                          "Failed Acquiring NVM resource for read err=%d status=0x%x\n",
1161                          ret_val, hw->aq.asq_last_status);
1162                 goto free_buff;
1163         }
1164
1165         sectors = eeprom->len / I40E_NVM_SECTOR_SIZE;
1166         sectors += (eeprom->len % I40E_NVM_SECTOR_SIZE) ? 1 : 0;
1167         len = I40E_NVM_SECTOR_SIZE;
1168         last = false;
1169         for (i = 0; i < sectors; i++) {
1170                 if (i == (sectors - 1)) {
1171                         len = eeprom->len - (I40E_NVM_SECTOR_SIZE * i);
1172                         last = true;
1173                 }
1174                 offset = eeprom->offset + (I40E_NVM_SECTOR_SIZE * i),
1175                 ret_val = i40e_aq_read_nvm(hw, 0x0, offset, len,
1176                                 (u8 *)eeprom_buff + (I40E_NVM_SECTOR_SIZE * i),
1177                                 last, NULL);
1178                 if (ret_val && hw->aq.asq_last_status == I40E_AQ_RC_EPERM) {
1179                         dev_info(&pf->pdev->dev,
1180                                  "read NVM failed, invalid offset 0x%x\n",
1181                                  offset);
1182                         break;
1183                 } else if (ret_val &&
1184                            hw->aq.asq_last_status == I40E_AQ_RC_EACCES) {
1185                         dev_info(&pf->pdev->dev,
1186                                  "read NVM failed, access, offset 0x%x\n",
1187                                  offset);
1188                         break;
1189                 } else if (ret_val) {
1190                         dev_info(&pf->pdev->dev,
1191                                  "read NVM failed offset %d err=%d status=0x%x\n",
1192                                  offset, ret_val, hw->aq.asq_last_status);
1193                         break;
1194                 }
1195         }
1196
1197         i40e_release_nvm(hw);
1198         memcpy(bytes, (u8 *)eeprom_buff, eeprom->len);
1199 free_buff:
1200         kfree(eeprom_buff);
1201         return ret_val;
1202 }
1203
1204 static int i40e_get_eeprom_len(struct net_device *netdev)
1205 {
1206         struct i40e_netdev_priv *np = netdev_priv(netdev);
1207         struct i40e_hw *hw = &np->vsi->back->hw;
1208         u32 val;
1209
1210 #define X722_EEPROM_SCOPE_LIMIT 0x5B9FFF
1211         if (hw->mac.type == I40E_MAC_X722) {
1212                 val = X722_EEPROM_SCOPE_LIMIT + 1;
1213                 return val;
1214         }
1215         val = (rd32(hw, I40E_GLPCI_LBARCTRL)
1216                 & I40E_GLPCI_LBARCTRL_FL_SIZE_MASK)
1217                 >> I40E_GLPCI_LBARCTRL_FL_SIZE_SHIFT;
1218         /* register returns value in power of 2, 64Kbyte chunks. */
1219         val = (64 * 1024) * BIT(val);
1220         return val;
1221 }
1222
1223 static int i40e_set_eeprom(struct net_device *netdev,
1224                            struct ethtool_eeprom *eeprom, u8 *bytes)
1225 {
1226         struct i40e_netdev_priv *np = netdev_priv(netdev);
1227         struct i40e_hw *hw = &np->vsi->back->hw;
1228         struct i40e_pf *pf = np->vsi->back;
1229         struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1230         int ret_val = 0;
1231         int errno = 0;
1232         u32 magic;
1233
1234         /* normal ethtool set_eeprom is not supported */
1235         magic = hw->vendor_id | (hw->device_id << 16);
1236         if (eeprom->magic == magic)
1237                 errno = -EOPNOTSUPP;
1238         /* check for NVMUpdate access method */
1239         else if (!eeprom->magic || (eeprom->magic >> 16) != hw->device_id)
1240                 errno = -EINVAL;
1241         else if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
1242                  test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
1243                 errno = -EBUSY;
1244         else
1245                 ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1246
1247         if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1248                 dev_info(&pf->pdev->dev,
1249                          "NVMUpdate write failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1250                          ret_val, hw->aq.asq_last_status, errno,
1251                          (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1252                          cmd->offset, cmd->data_size);
1253
1254         return errno;
1255 }
1256
1257 static void i40e_get_drvinfo(struct net_device *netdev,
1258                              struct ethtool_drvinfo *drvinfo)
1259 {
1260         struct i40e_netdev_priv *np = netdev_priv(netdev);
1261         struct i40e_vsi *vsi = np->vsi;
1262         struct i40e_pf *pf = vsi->back;
1263
1264         strlcpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
1265         strlcpy(drvinfo->version, i40e_driver_version_str,
1266                 sizeof(drvinfo->version));
1267         strlcpy(drvinfo->fw_version, i40e_nvm_version_str(&pf->hw),
1268                 sizeof(drvinfo->fw_version));
1269         strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
1270                 sizeof(drvinfo->bus_info));
1271         drvinfo->n_priv_flags = I40E_PRIV_FLAGS_STR_LEN;
1272         if (pf->hw.pf_id == 0)
1273                 drvinfo->n_priv_flags += I40E_GL_PRIV_FLAGS_STR_LEN;
1274 }
1275
1276 static void i40e_get_ringparam(struct net_device *netdev,
1277                                struct ethtool_ringparam *ring)
1278 {
1279         struct i40e_netdev_priv *np = netdev_priv(netdev);
1280         struct i40e_pf *pf = np->vsi->back;
1281         struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
1282
1283         ring->rx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
1284         ring->tx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
1285         ring->rx_mini_max_pending = 0;
1286         ring->rx_jumbo_max_pending = 0;
1287         ring->rx_pending = vsi->rx_rings[0]->count;
1288         ring->tx_pending = vsi->tx_rings[0]->count;
1289         ring->rx_mini_pending = 0;
1290         ring->rx_jumbo_pending = 0;
1291 }
1292
1293 static int i40e_set_ringparam(struct net_device *netdev,
1294                               struct ethtool_ringparam *ring)
1295 {
1296         struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
1297         struct i40e_netdev_priv *np = netdev_priv(netdev);
1298         struct i40e_hw *hw = &np->vsi->back->hw;
1299         struct i40e_vsi *vsi = np->vsi;
1300         struct i40e_pf *pf = vsi->back;
1301         u32 new_rx_count, new_tx_count;
1302         int i, err = 0;
1303
1304         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
1305                 return -EINVAL;
1306
1307         if (ring->tx_pending > I40E_MAX_NUM_DESCRIPTORS ||
1308             ring->tx_pending < I40E_MIN_NUM_DESCRIPTORS ||
1309             ring->rx_pending > I40E_MAX_NUM_DESCRIPTORS ||
1310             ring->rx_pending < I40E_MIN_NUM_DESCRIPTORS) {
1311                 netdev_info(netdev,
1312                             "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d]\n",
1313                             ring->tx_pending, ring->rx_pending,
1314                             I40E_MIN_NUM_DESCRIPTORS, I40E_MAX_NUM_DESCRIPTORS);
1315                 return -EINVAL;
1316         }
1317
1318         new_tx_count = ALIGN(ring->tx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
1319         new_rx_count = ALIGN(ring->rx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
1320
1321         /* if nothing to do return success */
1322         if ((new_tx_count == vsi->tx_rings[0]->count) &&
1323             (new_rx_count == vsi->rx_rings[0]->count))
1324                 return 0;
1325
1326         while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
1327                 usleep_range(1000, 2000);
1328
1329         if (!netif_running(vsi->netdev)) {
1330                 /* simple case - set for the next time the netdev is started */
1331                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1332                         vsi->tx_rings[i]->count = new_tx_count;
1333                         vsi->rx_rings[i]->count = new_rx_count;
1334                 }
1335                 goto done;
1336         }
1337
1338         /* We can't just free everything and then setup again,
1339          * because the ISRs in MSI-X mode get passed pointers
1340          * to the Tx and Rx ring structs.
1341          */
1342
1343         /* alloc updated Tx resources */
1344         if (new_tx_count != vsi->tx_rings[0]->count) {
1345                 netdev_info(netdev,
1346                             "Changing Tx descriptor count from %d to %d.\n",
1347                             vsi->tx_rings[0]->count, new_tx_count);
1348                 tx_rings = kcalloc(vsi->alloc_queue_pairs,
1349                                    sizeof(struct i40e_ring), GFP_KERNEL);
1350                 if (!tx_rings) {
1351                         err = -ENOMEM;
1352                         goto done;
1353                 }
1354
1355                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1356                         /* clone ring and setup updated count */
1357                         tx_rings[i] = *vsi->tx_rings[i];
1358                         tx_rings[i].count = new_tx_count;
1359                         /* the desc and bi pointers will be reallocated in the
1360                          * setup call
1361                          */
1362                         tx_rings[i].desc = NULL;
1363                         tx_rings[i].rx_bi = NULL;
1364                         err = i40e_setup_tx_descriptors(&tx_rings[i]);
1365                         if (err) {
1366                                 while (i) {
1367                                         i--;
1368                                         i40e_free_tx_resources(&tx_rings[i]);
1369                                 }
1370                                 kfree(tx_rings);
1371                                 tx_rings = NULL;
1372
1373                                 goto done;
1374                         }
1375                 }
1376         }
1377
1378         /* alloc updated Rx resources */
1379         if (new_rx_count != vsi->rx_rings[0]->count) {
1380                 netdev_info(netdev,
1381                             "Changing Rx descriptor count from %d to %d\n",
1382                             vsi->rx_rings[0]->count, new_rx_count);
1383                 rx_rings = kcalloc(vsi->alloc_queue_pairs,
1384                                    sizeof(struct i40e_ring), GFP_KERNEL);
1385                 if (!rx_rings) {
1386                         err = -ENOMEM;
1387                         goto free_tx;
1388                 }
1389
1390                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1391                         struct i40e_ring *ring;
1392                         u16 unused;
1393
1394                         /* clone ring and setup updated count */
1395                         rx_rings[i] = *vsi->rx_rings[i];
1396                         rx_rings[i].count = new_rx_count;
1397                         /* the desc and bi pointers will be reallocated in the
1398                          * setup call
1399                          */
1400                         rx_rings[i].desc = NULL;
1401                         rx_rings[i].rx_bi = NULL;
1402                         /* this is to allow wr32 to have something to write to
1403                          * during early allocation of Rx buffers
1404                          */
1405                         rx_rings[i].tail = hw->hw_addr + I40E_PRTGEN_STATUS;
1406                         err = i40e_setup_rx_descriptors(&rx_rings[i]);
1407                         if (err)
1408                                 goto rx_unwind;
1409
1410                         /* now allocate the Rx buffers to make sure the OS
1411                          * has enough memory, any failure here means abort
1412                          */
1413                         ring = &rx_rings[i];
1414                         unused = I40E_DESC_UNUSED(ring);
1415                         err = i40e_alloc_rx_buffers(ring, unused);
1416 rx_unwind:
1417                         if (err) {
1418                                 do {
1419                                         i40e_free_rx_resources(&rx_rings[i]);
1420                                 } while (i--);
1421                                 kfree(rx_rings);
1422                                 rx_rings = NULL;
1423
1424                                 goto free_tx;
1425                         }
1426                 }
1427         }
1428
1429         /* Bring interface down, copy in the new ring info,
1430          * then restore the interface
1431          */
1432         i40e_down(vsi);
1433
1434         if (tx_rings) {
1435                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1436                         i40e_free_tx_resources(vsi->tx_rings[i]);
1437                         *vsi->tx_rings[i] = tx_rings[i];
1438                 }
1439                 kfree(tx_rings);
1440                 tx_rings = NULL;
1441         }
1442
1443         if (rx_rings) {
1444                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1445                         i40e_free_rx_resources(vsi->rx_rings[i]);
1446                         /* get the real tail offset */
1447                         rx_rings[i].tail = vsi->rx_rings[i]->tail;
1448                         /* this is to fake out the allocation routine
1449                          * into thinking it has to realloc everything
1450                          * but the recycling logic will let us re-use
1451                          * the buffers allocated above
1452                          */
1453                         rx_rings[i].next_to_use = 0;
1454                         rx_rings[i].next_to_clean = 0;
1455                         rx_rings[i].next_to_alloc = 0;
1456                         /* do a struct copy */
1457                         *vsi->rx_rings[i] = rx_rings[i];
1458                 }
1459                 kfree(rx_rings);
1460                 rx_rings = NULL;
1461         }
1462
1463         i40e_up(vsi);
1464
1465 free_tx:
1466         /* error cleanup if the Rx allocations failed after getting Tx */
1467         if (tx_rings) {
1468                 for (i = 0; i < vsi->num_queue_pairs; i++)
1469                         i40e_free_tx_resources(&tx_rings[i]);
1470                 kfree(tx_rings);
1471                 tx_rings = NULL;
1472         }
1473
1474 done:
1475         clear_bit(__I40E_CONFIG_BUSY, &pf->state);
1476
1477         return err;
1478 }
1479
1480 static int i40e_get_sset_count(struct net_device *netdev, int sset)
1481 {
1482         struct i40e_netdev_priv *np = netdev_priv(netdev);
1483         struct i40e_vsi *vsi = np->vsi;
1484         struct i40e_pf *pf = vsi->back;
1485
1486         switch (sset) {
1487         case ETH_SS_TEST:
1488                 return I40E_TEST_LEN;
1489         case ETH_SS_STATS:
1490                 if (vsi == pf->vsi[pf->lan_vsi] && pf->hw.partition_id == 1) {
1491                         int len = I40E_PF_STATS_LEN(netdev);
1492
1493                         if ((pf->lan_veb != I40E_NO_VEB) &&
1494                             (pf->flags & I40E_FLAG_VEB_STATS_ENABLED))
1495                                 len += I40E_VEB_STATS_TOTAL;
1496                         return len;
1497                 } else {
1498                         return I40E_VSI_STATS_LEN(netdev);
1499                 }
1500         case ETH_SS_PRIV_FLAGS:
1501                 return I40E_PRIV_FLAGS_STR_LEN +
1502                         (pf->hw.pf_id == 0 ? I40E_GL_PRIV_FLAGS_STR_LEN : 0);
1503         default:
1504                 return -EOPNOTSUPP;
1505         }
1506 }
1507
1508 static void i40e_get_ethtool_stats(struct net_device *netdev,
1509                                    struct ethtool_stats *stats, u64 *data)
1510 {
1511         struct i40e_netdev_priv *np = netdev_priv(netdev);
1512         struct i40e_ring *tx_ring, *rx_ring;
1513         struct i40e_vsi *vsi = np->vsi;
1514         struct i40e_pf *pf = vsi->back;
1515         int i = 0;
1516         char *p;
1517         int j;
1518         struct rtnl_link_stats64 *net_stats = i40e_get_vsi_stats_struct(vsi);
1519         unsigned int start;
1520
1521         i40e_update_stats(vsi);
1522
1523         for (j = 0; j < I40E_NETDEV_STATS_LEN; j++) {
1524                 p = (char *)net_stats + i40e_gstrings_net_stats[j].stat_offset;
1525                 data[i++] = (i40e_gstrings_net_stats[j].sizeof_stat ==
1526                         sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1527         }
1528         for (j = 0; j < I40E_MISC_STATS_LEN; j++) {
1529                 p = (char *)vsi + i40e_gstrings_misc_stats[j].stat_offset;
1530                 data[i++] = (i40e_gstrings_misc_stats[j].sizeof_stat ==
1531                             sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1532         }
1533 #ifdef I40E_FCOE
1534         for (j = 0; j < I40E_FCOE_STATS_LEN; j++) {
1535                 p = (char *)vsi + i40e_gstrings_fcoe_stats[j].stat_offset;
1536                 data[i++] = (i40e_gstrings_fcoe_stats[j].sizeof_stat ==
1537                         sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1538         }
1539 #endif
1540         rcu_read_lock();
1541         for (j = 0; j < vsi->num_queue_pairs; j++) {
1542                 tx_ring = ACCESS_ONCE(vsi->tx_rings[j]);
1543
1544                 if (!tx_ring)
1545                         continue;
1546
1547                 /* process Tx ring statistics */
1548                 do {
1549                         start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
1550                         data[i] = tx_ring->stats.packets;
1551                         data[i + 1] = tx_ring->stats.bytes;
1552                 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
1553                 i += 2;
1554
1555                 /* Rx ring is the 2nd half of the queue pair */
1556                 rx_ring = &tx_ring[1];
1557                 do {
1558                         start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
1559                         data[i] = rx_ring->stats.packets;
1560                         data[i + 1] = rx_ring->stats.bytes;
1561                 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
1562                 i += 2;
1563         }
1564         rcu_read_unlock();
1565         if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
1566                 return;
1567
1568         if ((pf->lan_veb != I40E_NO_VEB) &&
1569             (pf->flags & I40E_FLAG_VEB_STATS_ENABLED)) {
1570                 struct i40e_veb *veb = pf->veb[pf->lan_veb];
1571
1572                 for (j = 0; j < I40E_VEB_STATS_LEN; j++) {
1573                         p = (char *)veb;
1574                         p += i40e_gstrings_veb_stats[j].stat_offset;
1575                         data[i++] = (i40e_gstrings_veb_stats[j].sizeof_stat ==
1576                                      sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1577                 }
1578                 for (j = 0; j < I40E_MAX_TRAFFIC_CLASS; j++) {
1579                         data[i++] = veb->tc_stats.tc_tx_packets[j];
1580                         data[i++] = veb->tc_stats.tc_tx_bytes[j];
1581                         data[i++] = veb->tc_stats.tc_rx_packets[j];
1582                         data[i++] = veb->tc_stats.tc_rx_bytes[j];
1583                 }
1584         }
1585         for (j = 0; j < I40E_GLOBAL_STATS_LEN; j++) {
1586                 p = (char *)pf + i40e_gstrings_stats[j].stat_offset;
1587                 data[i++] = (i40e_gstrings_stats[j].sizeof_stat ==
1588                              sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1589         }
1590         for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
1591                 data[i++] = pf->stats.priority_xon_tx[j];
1592                 data[i++] = pf->stats.priority_xoff_tx[j];
1593         }
1594         for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
1595                 data[i++] = pf->stats.priority_xon_rx[j];
1596                 data[i++] = pf->stats.priority_xoff_rx[j];
1597         }
1598         for (j = 0; j < I40E_MAX_USER_PRIORITY; j++)
1599                 data[i++] = pf->stats.priority_xon_2_xoff[j];
1600 }
1601
1602 static void i40e_get_strings(struct net_device *netdev, u32 stringset,
1603                              u8 *data)
1604 {
1605         struct i40e_netdev_priv *np = netdev_priv(netdev);
1606         struct i40e_vsi *vsi = np->vsi;
1607         struct i40e_pf *pf = vsi->back;
1608         char *p = (char *)data;
1609         int i;
1610
1611         switch (stringset) {
1612         case ETH_SS_TEST:
1613                 memcpy(data, i40e_gstrings_test,
1614                        I40E_TEST_LEN * ETH_GSTRING_LEN);
1615                 break;
1616         case ETH_SS_STATS:
1617                 for (i = 0; i < I40E_NETDEV_STATS_LEN; i++) {
1618                         snprintf(p, ETH_GSTRING_LEN, "%s",
1619                                  i40e_gstrings_net_stats[i].stat_string);
1620                         p += ETH_GSTRING_LEN;
1621                 }
1622                 for (i = 0; i < I40E_MISC_STATS_LEN; i++) {
1623                         snprintf(p, ETH_GSTRING_LEN, "%s",
1624                                  i40e_gstrings_misc_stats[i].stat_string);
1625                         p += ETH_GSTRING_LEN;
1626                 }
1627 #ifdef I40E_FCOE
1628                 for (i = 0; i < I40E_FCOE_STATS_LEN; i++) {
1629                         snprintf(p, ETH_GSTRING_LEN, "%s",
1630                                  i40e_gstrings_fcoe_stats[i].stat_string);
1631                         p += ETH_GSTRING_LEN;
1632                 }
1633 #endif
1634                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1635                         snprintf(p, ETH_GSTRING_LEN, "tx-%d.tx_packets", i);
1636                         p += ETH_GSTRING_LEN;
1637                         snprintf(p, ETH_GSTRING_LEN, "tx-%d.tx_bytes", i);
1638                         p += ETH_GSTRING_LEN;
1639                         snprintf(p, ETH_GSTRING_LEN, "rx-%d.rx_packets", i);
1640                         p += ETH_GSTRING_LEN;
1641                         snprintf(p, ETH_GSTRING_LEN, "rx-%d.rx_bytes", i);
1642                         p += ETH_GSTRING_LEN;
1643                 }
1644                 if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
1645                         return;
1646
1647                 if ((pf->lan_veb != I40E_NO_VEB) &&
1648                     (pf->flags & I40E_FLAG_VEB_STATS_ENABLED)) {
1649                         for (i = 0; i < I40E_VEB_STATS_LEN; i++) {
1650                                 snprintf(p, ETH_GSTRING_LEN, "veb.%s",
1651                                         i40e_gstrings_veb_stats[i].stat_string);
1652                                 p += ETH_GSTRING_LEN;
1653                         }
1654                         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1655                                 snprintf(p, ETH_GSTRING_LEN,
1656                                          "veb.tc_%d_tx_packets", i);
1657                                 p += ETH_GSTRING_LEN;
1658                                 snprintf(p, ETH_GSTRING_LEN,
1659                                          "veb.tc_%d_tx_bytes", i);
1660                                 p += ETH_GSTRING_LEN;
1661                                 snprintf(p, ETH_GSTRING_LEN,
1662                                          "veb.tc_%d_rx_packets", i);
1663                                 p += ETH_GSTRING_LEN;
1664                                 snprintf(p, ETH_GSTRING_LEN,
1665                                          "veb.tc_%d_rx_bytes", i);
1666                                 p += ETH_GSTRING_LEN;
1667                         }
1668                 }
1669                 for (i = 0; i < I40E_GLOBAL_STATS_LEN; i++) {
1670                         snprintf(p, ETH_GSTRING_LEN, "port.%s",
1671                                  i40e_gstrings_stats[i].stat_string);
1672                         p += ETH_GSTRING_LEN;
1673                 }
1674                 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1675                         snprintf(p, ETH_GSTRING_LEN,
1676                                  "port.tx_priority_%d_xon", i);
1677                         p += ETH_GSTRING_LEN;
1678                         snprintf(p, ETH_GSTRING_LEN,
1679                                  "port.tx_priority_%d_xoff", i);
1680                         p += ETH_GSTRING_LEN;
1681                 }
1682                 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1683                         snprintf(p, ETH_GSTRING_LEN,
1684                                  "port.rx_priority_%d_xon", i);
1685                         p += ETH_GSTRING_LEN;
1686                         snprintf(p, ETH_GSTRING_LEN,
1687                                  "port.rx_priority_%d_xoff", i);
1688                         p += ETH_GSTRING_LEN;
1689                 }
1690                 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1691                         snprintf(p, ETH_GSTRING_LEN,
1692                                  "port.rx_priority_%d_xon_2_xoff", i);
1693                         p += ETH_GSTRING_LEN;
1694                 }
1695                 /* BUG_ON(p - data != I40E_STATS_LEN * ETH_GSTRING_LEN); */
1696                 break;
1697         case ETH_SS_PRIV_FLAGS:
1698                 memcpy(data, i40e_priv_flags_strings,
1699                        I40E_PRIV_FLAGS_STR_LEN * ETH_GSTRING_LEN);
1700                 data += I40E_PRIV_FLAGS_STR_LEN * ETH_GSTRING_LEN;
1701                 if (pf->hw.pf_id == 0)
1702                         memcpy(data, i40e_gl_priv_flags_strings,
1703                                I40E_GL_PRIV_FLAGS_STR_LEN * ETH_GSTRING_LEN);
1704                 break;
1705         default:
1706                 break;
1707         }
1708 }
1709
1710 static int i40e_get_ts_info(struct net_device *dev,
1711                             struct ethtool_ts_info *info)
1712 {
1713         struct i40e_pf *pf = i40e_netdev_to_pf(dev);
1714
1715         /* only report HW timestamping if PTP is enabled */
1716         if (!(pf->flags & I40E_FLAG_PTP))
1717                 return ethtool_op_get_ts_info(dev, info);
1718
1719         info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
1720                                 SOF_TIMESTAMPING_RX_SOFTWARE |
1721                                 SOF_TIMESTAMPING_SOFTWARE |
1722                                 SOF_TIMESTAMPING_TX_HARDWARE |
1723                                 SOF_TIMESTAMPING_RX_HARDWARE |
1724                                 SOF_TIMESTAMPING_RAW_HARDWARE;
1725
1726         if (pf->ptp_clock)
1727                 info->phc_index = ptp_clock_index(pf->ptp_clock);
1728         else
1729                 info->phc_index = -1;
1730
1731         info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
1732
1733         info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1734                            BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1735                            BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
1736                            BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ);
1737
1738         if (pf->flags & I40E_FLAG_PTP_L4_CAPABLE)
1739                 info->rx_filters |= BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
1740                                     BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
1741                                     BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
1742                                     BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
1743                                     BIT(HWTSTAMP_FILTER_PTP_V2_SYNC) |
1744                                     BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
1745                                     BIT(HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
1746                                     BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
1747
1748         return 0;
1749 }
1750
1751 static int i40e_link_test(struct net_device *netdev, u64 *data)
1752 {
1753         struct i40e_netdev_priv *np = netdev_priv(netdev);
1754         struct i40e_pf *pf = np->vsi->back;
1755         i40e_status status;
1756         bool link_up = false;
1757
1758         netif_info(pf, hw, netdev, "link test\n");
1759         status = i40e_get_link_status(&pf->hw, &link_up);
1760         if (status) {
1761                 netif_err(pf, drv, netdev, "link query timed out, please retry test\n");
1762                 *data = 1;
1763                 return *data;
1764         }
1765
1766         if (link_up)
1767                 *data = 0;
1768         else
1769                 *data = 1;
1770
1771         return *data;
1772 }
1773
1774 static int i40e_reg_test(struct net_device *netdev, u64 *data)
1775 {
1776         struct i40e_netdev_priv *np = netdev_priv(netdev);
1777         struct i40e_pf *pf = np->vsi->back;
1778
1779         netif_info(pf, hw, netdev, "register test\n");
1780         *data = i40e_diag_reg_test(&pf->hw);
1781
1782         return *data;
1783 }
1784
1785 static int i40e_eeprom_test(struct net_device *netdev, u64 *data)
1786 {
1787         struct i40e_netdev_priv *np = netdev_priv(netdev);
1788         struct i40e_pf *pf = np->vsi->back;
1789
1790         netif_info(pf, hw, netdev, "eeprom test\n");
1791         *data = i40e_diag_eeprom_test(&pf->hw);
1792
1793         /* forcebly clear the NVM Update state machine */
1794         pf->hw.nvmupd_state = I40E_NVMUPD_STATE_INIT;
1795
1796         return *data;
1797 }
1798
1799 static int i40e_intr_test(struct net_device *netdev, u64 *data)
1800 {
1801         struct i40e_netdev_priv *np = netdev_priv(netdev);
1802         struct i40e_pf *pf = np->vsi->back;
1803         u16 swc_old = pf->sw_int_count;
1804
1805         netif_info(pf, hw, netdev, "interrupt test\n");
1806         wr32(&pf->hw, I40E_PFINT_DYN_CTL0,
1807              (I40E_PFINT_DYN_CTL0_INTENA_MASK |
1808               I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
1809               I40E_PFINT_DYN_CTL0_ITR_INDX_MASK |
1810               I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK |
1811               I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK));
1812         usleep_range(1000, 2000);
1813         *data = (swc_old == pf->sw_int_count);
1814
1815         return *data;
1816 }
1817
1818 static inline bool i40e_active_vfs(struct i40e_pf *pf)
1819 {
1820         struct i40e_vf *vfs = pf->vf;
1821         int i;
1822
1823         for (i = 0; i < pf->num_alloc_vfs; i++)
1824                 if (test_bit(I40E_VF_STAT_ACTIVE, &vfs[i].vf_states))
1825                         return true;
1826         return false;
1827 }
1828
1829 static inline bool i40e_active_vmdqs(struct i40e_pf *pf)
1830 {
1831         return !!i40e_find_vsi_by_type(pf, I40E_VSI_VMDQ2);
1832 }
1833
1834 static void i40e_diag_test(struct net_device *netdev,
1835                            struct ethtool_test *eth_test, u64 *data)
1836 {
1837         struct i40e_netdev_priv *np = netdev_priv(netdev);
1838         bool if_running = netif_running(netdev);
1839         struct i40e_pf *pf = np->vsi->back;
1840
1841         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1842                 /* Offline tests */
1843                 netif_info(pf, drv, netdev, "offline testing starting\n");
1844
1845                 set_bit(__I40E_TESTING, &pf->state);
1846
1847                 if (i40e_active_vfs(pf) || i40e_active_vmdqs(pf)) {
1848                         dev_warn(&pf->pdev->dev,
1849                                  "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
1850                         data[I40E_ETH_TEST_REG]         = 1;
1851                         data[I40E_ETH_TEST_EEPROM]      = 1;
1852                         data[I40E_ETH_TEST_INTR]        = 1;
1853                         data[I40E_ETH_TEST_LINK]        = 1;
1854                         eth_test->flags |= ETH_TEST_FL_FAILED;
1855                         clear_bit(__I40E_TESTING, &pf->state);
1856                         goto skip_ol_tests;
1857                 }
1858
1859                 /* If the device is online then take it offline */
1860                 if (if_running)
1861                         /* indicate we're in test mode */
1862                         i40e_close(netdev);
1863                 else
1864                         /* This reset does not affect link - if it is
1865                          * changed to a type of reset that does affect
1866                          * link then the following link test would have
1867                          * to be moved to before the reset
1868                          */
1869                         i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED));
1870
1871                 if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
1872                         eth_test->flags |= ETH_TEST_FL_FAILED;
1873
1874                 if (i40e_eeprom_test(netdev, &data[I40E_ETH_TEST_EEPROM]))
1875                         eth_test->flags |= ETH_TEST_FL_FAILED;
1876
1877                 if (i40e_intr_test(netdev, &data[I40E_ETH_TEST_INTR]))
1878                         eth_test->flags |= ETH_TEST_FL_FAILED;
1879
1880                 /* run reg test last, a reset is required after it */
1881                 if (i40e_reg_test(netdev, &data[I40E_ETH_TEST_REG]))
1882                         eth_test->flags |= ETH_TEST_FL_FAILED;
1883
1884                 clear_bit(__I40E_TESTING, &pf->state);
1885                 i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED));
1886
1887                 if (if_running)
1888                         i40e_open(netdev);
1889         } else {
1890                 /* Online tests */
1891                 netif_info(pf, drv, netdev, "online testing starting\n");
1892
1893                 if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
1894                         eth_test->flags |= ETH_TEST_FL_FAILED;
1895
1896                 /* Offline only tests, not run in online; pass by default */
1897                 data[I40E_ETH_TEST_REG] = 0;
1898                 data[I40E_ETH_TEST_EEPROM] = 0;
1899                 data[I40E_ETH_TEST_INTR] = 0;
1900         }
1901
1902 skip_ol_tests:
1903
1904         netif_info(pf, drv, netdev, "testing finished\n");
1905 }
1906
1907 static void i40e_get_wol(struct net_device *netdev,
1908                          struct ethtool_wolinfo *wol)
1909 {
1910         struct i40e_netdev_priv *np = netdev_priv(netdev);
1911         struct i40e_pf *pf = np->vsi->back;
1912         struct i40e_hw *hw = &pf->hw;
1913         u16 wol_nvm_bits;
1914
1915         /* NVM bit on means WoL disabled for the port */
1916         i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
1917         if ((BIT(hw->port) & wol_nvm_bits) || (hw->partition_id != 1)) {
1918                 wol->supported = 0;
1919                 wol->wolopts = 0;
1920         } else {
1921                 wol->supported = WAKE_MAGIC;
1922                 wol->wolopts = (pf->wol_en ? WAKE_MAGIC : 0);
1923         }
1924 }
1925
1926 /**
1927  * i40e_set_wol - set the WakeOnLAN configuration
1928  * @netdev: the netdev in question
1929  * @wol: the ethtool WoL setting data
1930  **/
1931 static int i40e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1932 {
1933         struct i40e_netdev_priv *np = netdev_priv(netdev);
1934         struct i40e_pf *pf = np->vsi->back;
1935         struct i40e_vsi *vsi = np->vsi;
1936         struct i40e_hw *hw = &pf->hw;
1937         u16 wol_nvm_bits;
1938
1939         /* WoL not supported if this isn't the controlling PF on the port */
1940         if (hw->partition_id != 1) {
1941                 i40e_partition_setting_complaint(pf);
1942                 return -EOPNOTSUPP;
1943         }
1944
1945         if (vsi != pf->vsi[pf->lan_vsi])
1946                 return -EOPNOTSUPP;
1947
1948         /* NVM bit on means WoL disabled for the port */
1949         i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
1950         if (BIT(hw->port) & wol_nvm_bits)
1951                 return -EOPNOTSUPP;
1952
1953         /* only magic packet is supported */
1954         if (wol->wolopts && (wol->wolopts != WAKE_MAGIC))
1955                 return -EOPNOTSUPP;
1956
1957         /* is this a new value? */
1958         if (pf->wol_en != !!wol->wolopts) {
1959                 pf->wol_en = !!wol->wolopts;
1960                 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
1961         }
1962
1963         return 0;
1964 }
1965
1966 static int i40e_set_phys_id(struct net_device *netdev,
1967                             enum ethtool_phys_id_state state)
1968 {
1969         struct i40e_netdev_priv *np = netdev_priv(netdev);
1970         i40e_status ret = 0;
1971         struct i40e_pf *pf = np->vsi->back;
1972         struct i40e_hw *hw = &pf->hw;
1973         int blink_freq = 2;
1974         u16 temp_status;
1975
1976         switch (state) {
1977         case ETHTOOL_ID_ACTIVE:
1978                 if (!(pf->flags & I40E_FLAG_PHY_CONTROLS_LEDS)) {
1979                         pf->led_status = i40e_led_get(hw);
1980                 } else {
1981                         i40e_aq_set_phy_debug(hw, I40E_PHY_DEBUG_ALL, NULL);
1982                         ret = i40e_led_get_phy(hw, &temp_status,
1983                                                &pf->phy_led_val);
1984                         pf->led_status = temp_status;
1985                 }
1986                 return blink_freq;
1987         case ETHTOOL_ID_ON:
1988                 if (!(pf->flags & I40E_FLAG_PHY_CONTROLS_LEDS))
1989                         i40e_led_set(hw, 0xf, false);
1990                 else
1991                         ret = i40e_led_set_phy(hw, true, pf->led_status, 0);
1992                 break;
1993         case ETHTOOL_ID_OFF:
1994                 if (!(pf->flags & I40E_FLAG_PHY_CONTROLS_LEDS))
1995                         i40e_led_set(hw, 0x0, false);
1996                 else
1997                         ret = i40e_led_set_phy(hw, false, pf->led_status, 0);
1998                 break;
1999         case ETHTOOL_ID_INACTIVE:
2000                 if (!(pf->flags & I40E_FLAG_PHY_CONTROLS_LEDS)) {
2001                         i40e_led_set(hw, pf->led_status, false);
2002                 } else {
2003                         ret = i40e_led_set_phy(hw, false, pf->led_status,
2004                                                (pf->phy_led_val |
2005                                                I40E_PHY_LED_MODE_ORIG));
2006                         i40e_aq_set_phy_debug(hw, 0, NULL);
2007                 }
2008                 break;
2009         default:
2010                 break;
2011         }
2012                 if (ret)
2013                         return -ENOENT;
2014                 else
2015                         return 0;
2016 }
2017
2018 /* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
2019  * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
2020  * 125us (8000 interrupts per second) == ITR(62)
2021  */
2022
2023 /**
2024  * __i40e_get_coalesce - get per-queue coalesce settings
2025  * @netdev: the netdev to check
2026  * @ec: ethtool coalesce data structure
2027  * @queue: which queue to pick
2028  *
2029  * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
2030  * are per queue. If queue is <0 then we default to queue 0 as the
2031  * representative value.
2032  **/
2033 static int __i40e_get_coalesce(struct net_device *netdev,
2034                                struct ethtool_coalesce *ec,
2035                                int queue)
2036 {
2037         struct i40e_netdev_priv *np = netdev_priv(netdev);
2038         struct i40e_ring *rx_ring, *tx_ring;
2039         struct i40e_vsi *vsi = np->vsi;
2040
2041         ec->tx_max_coalesced_frames_irq = vsi->work_limit;
2042         ec->rx_max_coalesced_frames_irq = vsi->work_limit;
2043
2044         /* rx and tx usecs has per queue value. If user doesn't specify the queue,
2045          * return queue 0's value to represent.
2046          */
2047         if (queue < 0) {
2048                 queue = 0;
2049         } else if (queue >= vsi->num_queue_pairs) {
2050                 return -EINVAL;
2051         }
2052
2053         rx_ring = vsi->rx_rings[queue];
2054         tx_ring = vsi->tx_rings[queue];
2055
2056         if (ITR_IS_DYNAMIC(rx_ring->rx_itr_setting))
2057                 ec->use_adaptive_rx_coalesce = 1;
2058
2059         if (ITR_IS_DYNAMIC(tx_ring->tx_itr_setting))
2060                 ec->use_adaptive_tx_coalesce = 1;
2061
2062         ec->rx_coalesce_usecs = rx_ring->rx_itr_setting & ~I40E_ITR_DYNAMIC;
2063         ec->tx_coalesce_usecs = tx_ring->tx_itr_setting & ~I40E_ITR_DYNAMIC;
2064
2065
2066         /* we use the _usecs_high to store/set the interrupt rate limit
2067          * that the hardware supports, that almost but not quite
2068          * fits the original intent of the ethtool variable,
2069          * the rx_coalesce_usecs_high limits total interrupts
2070          * per second from both tx/rx sources.
2071          */
2072         ec->rx_coalesce_usecs_high = vsi->int_rate_limit;
2073         ec->tx_coalesce_usecs_high = vsi->int_rate_limit;
2074
2075         return 0;
2076 }
2077
2078 /**
2079  * i40e_get_coalesce - get a netdev's coalesce settings
2080  * @netdev: the netdev to check
2081  * @ec: ethtool coalesce data structure
2082  *
2083  * Gets the coalesce settings for a particular netdev. Note that if user has
2084  * modified per-queue settings, this only guarantees to represent queue 0. See
2085  * __i40e_get_coalesce for more details.
2086  **/
2087 static int i40e_get_coalesce(struct net_device *netdev,
2088                              struct ethtool_coalesce *ec)
2089 {
2090         return __i40e_get_coalesce(netdev, ec, -1);
2091 }
2092
2093 /**
2094  * i40e_get_per_queue_coalesce - gets coalesce settings for particular queue
2095  * @netdev: netdev structure
2096  * @ec: ethtool's coalesce settings
2097  * @queue: the particular queue to read
2098  *
2099  * Will read a specific queue's coalesce settings
2100  **/
2101 static int i40e_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
2102                                        struct ethtool_coalesce *ec)
2103 {
2104         return __i40e_get_coalesce(netdev, ec, queue);
2105 }
2106
2107 /**
2108  * i40e_set_itr_per_queue - set ITR values for specific queue
2109  * @vsi: the VSI to set values for
2110  * @ec: coalesce settings from ethtool
2111  * @queue: the queue to modify
2112  *
2113  * Change the ITR settings for a specific queue.
2114  **/
2115
2116 static void i40e_set_itr_per_queue(struct i40e_vsi *vsi,
2117                                    struct ethtool_coalesce *ec,
2118                                    int queue)
2119 {
2120         struct i40e_pf *pf = vsi->back;
2121         struct i40e_hw *hw = &pf->hw;
2122         struct i40e_q_vector *q_vector;
2123         u16 vector, intrl;
2124
2125         intrl = i40e_intrl_usec_to_reg(vsi->int_rate_limit);
2126
2127         vsi->rx_rings[queue]->rx_itr_setting = ec->rx_coalesce_usecs;
2128         vsi->tx_rings[queue]->tx_itr_setting = ec->tx_coalesce_usecs;
2129
2130         if (ec->use_adaptive_rx_coalesce)
2131                 vsi->rx_rings[queue]->rx_itr_setting |= I40E_ITR_DYNAMIC;
2132         else
2133                 vsi->rx_rings[queue]->rx_itr_setting &= ~I40E_ITR_DYNAMIC;
2134
2135         if (ec->use_adaptive_tx_coalesce)
2136                 vsi->tx_rings[queue]->tx_itr_setting |= I40E_ITR_DYNAMIC;
2137         else
2138                 vsi->tx_rings[queue]->tx_itr_setting &= ~I40E_ITR_DYNAMIC;
2139
2140         q_vector = vsi->rx_rings[queue]->q_vector;
2141         q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[queue]->rx_itr_setting);
2142         vector = vsi->base_vector + q_vector->v_idx;
2143         wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1), q_vector->rx.itr);
2144
2145         q_vector = vsi->tx_rings[queue]->q_vector;
2146         q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[queue]->tx_itr_setting);
2147         vector = vsi->base_vector + q_vector->v_idx;
2148         wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1), q_vector->tx.itr);
2149
2150         wr32(hw, I40E_PFINT_RATEN(vector - 1), intrl);
2151         i40e_flush(hw);
2152 }
2153
2154 /**
2155  * __i40e_set_coalesce - set coalesce settings for particular queue
2156  * @netdev: the netdev to change
2157  * @ec: ethtool coalesce settings
2158  * @queue: the queue to change
2159  *
2160  * Sets the coalesce settings for a particular queue.
2161  **/
2162 static int __i40e_set_coalesce(struct net_device *netdev,
2163                                struct ethtool_coalesce *ec,
2164                                int queue)
2165 {
2166         struct i40e_netdev_priv *np = netdev_priv(netdev);
2167         struct i40e_vsi *vsi = np->vsi;
2168         struct i40e_pf *pf = vsi->back;
2169         u16 intrl_reg;
2170         int i;
2171
2172         if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
2173                 vsi->work_limit = ec->tx_max_coalesced_frames_irq;
2174
2175         /* tx_coalesce_usecs_high is ignored, use rx-usecs-high instead */
2176         if (ec->tx_coalesce_usecs_high != vsi->int_rate_limit) {
2177                 netif_info(pf, drv, netdev, "tx-usecs-high is not used, please program rx-usecs-high\n");
2178                 return -EINVAL;
2179         }
2180
2181         if (ec->rx_coalesce_usecs_high > INTRL_REG_TO_USEC(I40E_MAX_INTRL)) {
2182                 netif_info(pf, drv, netdev, "Invalid value, rx-usecs-high range is 0-%lu\n",
2183                            INTRL_REG_TO_USEC(I40E_MAX_INTRL));
2184                 return -EINVAL;
2185         }
2186
2187         if (ec->rx_coalesce_usecs == 0) {
2188                 if (ec->use_adaptive_rx_coalesce)
2189                         netif_info(pf, drv, netdev, "rx-usecs=0, need to disable adaptive-rx for a complete disable\n");
2190         } else if ((ec->rx_coalesce_usecs < (I40E_MIN_ITR << 1)) ||
2191                    (ec->rx_coalesce_usecs > (I40E_MAX_ITR << 1))) {
2192                         netif_info(pf, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
2193                         return -EINVAL;
2194         }
2195
2196         intrl_reg = i40e_intrl_usec_to_reg(ec->rx_coalesce_usecs_high);
2197         vsi->int_rate_limit = INTRL_REG_TO_USEC(intrl_reg);
2198         if (vsi->int_rate_limit != ec->rx_coalesce_usecs_high) {
2199                 netif_info(pf, drv, netdev, "Interrupt rate limit rounded down to %d\n",
2200                            vsi->int_rate_limit);
2201         }
2202
2203         if (ec->tx_coalesce_usecs == 0) {
2204                 if (ec->use_adaptive_tx_coalesce)
2205                         netif_info(pf, drv, netdev, "tx-usecs=0, need to disable adaptive-tx for a complete disable\n");
2206         } else if ((ec->tx_coalesce_usecs < (I40E_MIN_ITR << 1)) ||
2207                    (ec->tx_coalesce_usecs > (I40E_MAX_ITR << 1))) {
2208                         netif_info(pf, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
2209                         return -EINVAL;
2210         }
2211
2212         /* rx and tx usecs has per queue value. If user doesn't specify the queue,
2213          * apply to all queues.
2214          */
2215         if (queue < 0) {
2216                 for (i = 0; i < vsi->num_queue_pairs; i++)
2217                         i40e_set_itr_per_queue(vsi, ec, i);
2218         } else if (queue < vsi->num_queue_pairs) {
2219                 i40e_set_itr_per_queue(vsi, ec, queue);
2220         } else {
2221                 netif_info(pf, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
2222                            vsi->num_queue_pairs - 1);
2223                 return -EINVAL;
2224         }
2225
2226         return 0;
2227 }
2228
2229 /**
2230  * i40e_set_coalesce - set coalesce settings for every queue on the netdev
2231  * @netdev: the netdev to change
2232  * @ec: ethtool coalesce settings
2233  *
2234  * This will set each queue to the same coalesce settings.
2235  **/
2236 static int i40e_set_coalesce(struct net_device *netdev,
2237                              struct ethtool_coalesce *ec)
2238 {
2239         return __i40e_set_coalesce(netdev, ec, -1);
2240 }
2241
2242 /**
2243  * i40e_set_per_queue_coalesce - set specific queue's coalesce settings
2244  * @netdev: the netdev to change
2245  * @ec: ethtool's coalesce settings
2246  * @queue: the queue to change
2247  *
2248  * Sets the specified queue's coalesce settings.
2249  **/
2250 static int i40e_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
2251                                        struct ethtool_coalesce *ec)
2252 {
2253         return __i40e_set_coalesce(netdev, ec, queue);
2254 }
2255
2256 /**
2257  * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
2258  * @pf: pointer to the physical function struct
2259  * @cmd: ethtool rxnfc command
2260  *
2261  * Returns Success if the flow is supported, else Invalid Input.
2262  **/
2263 static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
2264 {
2265         struct i40e_hw *hw = &pf->hw;
2266         u8 flow_pctype = 0;
2267         u64 i_set = 0;
2268
2269         cmd->data = 0;
2270
2271         switch (cmd->flow_type) {
2272         case TCP_V4_FLOW:
2273                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
2274                 break;
2275         case UDP_V4_FLOW:
2276                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
2277                 break;
2278         case TCP_V6_FLOW:
2279                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
2280                 break;
2281         case UDP_V6_FLOW:
2282                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
2283                 break;
2284         case SCTP_V4_FLOW:
2285         case AH_ESP_V4_FLOW:
2286         case AH_V4_FLOW:
2287         case ESP_V4_FLOW:
2288         case IPV4_FLOW:
2289         case SCTP_V6_FLOW:
2290         case AH_ESP_V6_FLOW:
2291         case AH_V6_FLOW:
2292         case ESP_V6_FLOW:
2293         case IPV6_FLOW:
2294                 /* Default is src/dest for IP, no matter the L4 hashing */
2295                 cmd->data |= RXH_IP_SRC | RXH_IP_DST;
2296                 break;
2297         default:
2298                 return -EINVAL;
2299         }
2300
2301         /* Read flow based hash input set register */
2302         if (flow_pctype) {
2303                 i_set = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0,
2304                                               flow_pctype)) |
2305                         ((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1,
2306                                                flow_pctype)) << 32);
2307         }
2308
2309         /* Process bits of hash input set */
2310         if (i_set) {
2311                 if (i_set & I40E_L4_SRC_MASK)
2312                         cmd->data |= RXH_L4_B_0_1;
2313                 if (i_set & I40E_L4_DST_MASK)
2314                         cmd->data |= RXH_L4_B_2_3;
2315
2316                 if (cmd->flow_type == TCP_V4_FLOW ||
2317                     cmd->flow_type == UDP_V4_FLOW) {
2318                         if (i_set & I40E_L3_SRC_MASK)
2319                                 cmd->data |= RXH_IP_SRC;
2320                         if (i_set & I40E_L3_DST_MASK)
2321                                 cmd->data |= RXH_IP_DST;
2322                 } else if (cmd->flow_type == TCP_V6_FLOW ||
2323                           cmd->flow_type == UDP_V6_FLOW) {
2324                         if (i_set & I40E_L3_V6_SRC_MASK)
2325                                 cmd->data |= RXH_IP_SRC;
2326                         if (i_set & I40E_L3_V6_DST_MASK)
2327                                 cmd->data |= RXH_IP_DST;
2328                 }
2329         }
2330
2331         return 0;
2332 }
2333
2334 /**
2335  * i40e_get_ethtool_fdir_all - Populates the rule count of a command
2336  * @pf: Pointer to the physical function struct
2337  * @cmd: The command to get or set Rx flow classification rules
2338  * @rule_locs: Array of used rule locations
2339  *
2340  * This function populates both the total and actual rule count of
2341  * the ethtool flow classification command
2342  *
2343  * Returns 0 on success or -EMSGSIZE if entry not found
2344  **/
2345 static int i40e_get_ethtool_fdir_all(struct i40e_pf *pf,
2346                                      struct ethtool_rxnfc *cmd,
2347                                      u32 *rule_locs)
2348 {
2349         struct i40e_fdir_filter *rule;
2350         struct hlist_node *node2;
2351         int cnt = 0;
2352
2353         /* report total rule count */
2354         cmd->data = i40e_get_fd_cnt_all(pf);
2355
2356         hlist_for_each_entry_safe(rule, node2,
2357                                   &pf->fdir_filter_list, fdir_node) {
2358                 if (cnt == cmd->rule_cnt)
2359                         return -EMSGSIZE;
2360
2361                 rule_locs[cnt] = rule->fd_id;
2362                 cnt++;
2363         }
2364
2365         cmd->rule_cnt = cnt;
2366
2367         return 0;
2368 }
2369
2370 /**
2371  * i40e_get_ethtool_fdir_entry - Look up a filter based on Rx flow
2372  * @pf: Pointer to the physical function struct
2373  * @cmd: The command to get or set Rx flow classification rules
2374  *
2375  * This function looks up a filter based on the Rx flow classification
2376  * command and fills the flow spec info for it if found
2377  *
2378  * Returns 0 on success or -EINVAL if filter not found
2379  **/
2380 static int i40e_get_ethtool_fdir_entry(struct i40e_pf *pf,
2381                                        struct ethtool_rxnfc *cmd)
2382 {
2383         struct ethtool_rx_flow_spec *fsp =
2384                         (struct ethtool_rx_flow_spec *)&cmd->fs;
2385         struct i40e_fdir_filter *rule = NULL;
2386         struct hlist_node *node2;
2387
2388         hlist_for_each_entry_safe(rule, node2,
2389                                   &pf->fdir_filter_list, fdir_node) {
2390                 if (fsp->location <= rule->fd_id)
2391                         break;
2392         }
2393
2394         if (!rule || fsp->location != rule->fd_id)
2395                 return -EINVAL;
2396
2397         fsp->flow_type = rule->flow_type;
2398         if (fsp->flow_type == IP_USER_FLOW) {
2399                 fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
2400                 fsp->h_u.usr_ip4_spec.proto = 0;
2401                 fsp->m_u.usr_ip4_spec.proto = 0;
2402         }
2403
2404         /* Reverse the src and dest notion, since the HW views them from
2405          * Tx perspective where as the user expects it from Rx filter view.
2406          */
2407         fsp->h_u.tcp_ip4_spec.psrc = rule->dst_port;
2408         fsp->h_u.tcp_ip4_spec.pdst = rule->src_port;
2409         fsp->h_u.tcp_ip4_spec.ip4src = rule->dst_ip;
2410         fsp->h_u.tcp_ip4_spec.ip4dst = rule->src_ip;
2411
2412         if (rule->dest_ctl == I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET)
2413                 fsp->ring_cookie = RX_CLS_FLOW_DISC;
2414         else
2415                 fsp->ring_cookie = rule->q_index;
2416
2417         if (rule->dest_vsi != pf->vsi[pf->lan_vsi]->id) {
2418                 struct i40e_vsi *vsi;
2419
2420                 vsi = i40e_find_vsi_from_id(pf, rule->dest_vsi);
2421                 if (vsi && vsi->type == I40E_VSI_SRIOV) {
2422                         fsp->h_ext.data[1] = htonl(vsi->vf_id);
2423                         fsp->m_ext.data[1] = htonl(0x1);
2424                 }
2425         }
2426
2427         return 0;
2428 }
2429
2430 /**
2431  * i40e_get_rxnfc - command to get RX flow classification rules
2432  * @netdev: network interface device structure
2433  * @cmd: ethtool rxnfc command
2434  *
2435  * Returns Success if the command is supported.
2436  **/
2437 static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2438                           u32 *rule_locs)
2439 {
2440         struct i40e_netdev_priv *np = netdev_priv(netdev);
2441         struct i40e_vsi *vsi = np->vsi;
2442         struct i40e_pf *pf = vsi->back;
2443         int ret = -EOPNOTSUPP;
2444
2445         switch (cmd->cmd) {
2446         case ETHTOOL_GRXRINGS:
2447                 cmd->data = vsi->num_queue_pairs;
2448                 ret = 0;
2449                 break;
2450         case ETHTOOL_GRXFH:
2451                 ret = i40e_get_rss_hash_opts(pf, cmd);
2452                 break;
2453         case ETHTOOL_GRXCLSRLCNT:
2454                 cmd->rule_cnt = pf->fdir_pf_active_filters;
2455                 /* report total rule count */
2456                 cmd->data = i40e_get_fd_cnt_all(pf);
2457                 ret = 0;
2458                 break;
2459         case ETHTOOL_GRXCLSRULE:
2460                 ret = i40e_get_ethtool_fdir_entry(pf, cmd);
2461                 break;
2462         case ETHTOOL_GRXCLSRLALL:
2463                 ret = i40e_get_ethtool_fdir_all(pf, cmd, rule_locs);
2464                 break;
2465         default:
2466                 break;
2467         }
2468
2469         return ret;
2470 }
2471
2472 /**
2473  * i40e_get_rss_hash_bits - Read RSS Hash bits from register
2474  * @nfc: pointer to user request
2475  * @i_setc bits currently set
2476  *
2477  * Returns value of bits to be set per user request
2478  **/
2479 static u64 i40e_get_rss_hash_bits(struct ethtool_rxnfc *nfc, u64 i_setc)
2480 {
2481         u64 i_set = i_setc;
2482         u64 src_l3 = 0, dst_l3 = 0;
2483
2484         if (nfc->data & RXH_L4_B_0_1)
2485                 i_set |= I40E_L4_SRC_MASK;
2486         else
2487                 i_set &= ~I40E_L4_SRC_MASK;
2488         if (nfc->data & RXH_L4_B_2_3)
2489                 i_set |= I40E_L4_DST_MASK;
2490         else
2491                 i_set &= ~I40E_L4_DST_MASK;
2492
2493         if (nfc->flow_type == TCP_V6_FLOW || nfc->flow_type == UDP_V6_FLOW) {
2494                 src_l3 = I40E_L3_V6_SRC_MASK;
2495                 dst_l3 = I40E_L3_V6_DST_MASK;
2496         } else if (nfc->flow_type == TCP_V4_FLOW ||
2497                   nfc->flow_type == UDP_V4_FLOW) {
2498                 src_l3 = I40E_L3_SRC_MASK;
2499                 dst_l3 = I40E_L3_DST_MASK;
2500         } else {
2501                 /* Any other flow type are not supported here */
2502                 return i_set;
2503         }
2504
2505         if (nfc->data & RXH_IP_SRC)
2506                 i_set |= src_l3;
2507         else
2508                 i_set &= ~src_l3;
2509         if (nfc->data & RXH_IP_DST)
2510                 i_set |= dst_l3;
2511         else
2512                 i_set &= ~dst_l3;
2513
2514         return i_set;
2515 }
2516
2517 /**
2518  * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
2519  * @pf: pointer to the physical function struct
2520  * @cmd: ethtool rxnfc command
2521  *
2522  * Returns Success if the flow input set is supported.
2523  **/
2524 static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
2525 {
2526         struct i40e_hw *hw = &pf->hw;
2527         u64 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
2528                    ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
2529         u8 flow_pctype = 0;
2530         u64 i_set, i_setc;
2531
2532         /* RSS does not support anything other than hashing
2533          * to queues on src and dst IPs and ports
2534          */
2535         if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
2536                           RXH_L4_B_0_1 | RXH_L4_B_2_3))
2537                 return -EINVAL;
2538
2539         switch (nfc->flow_type) {
2540         case TCP_V4_FLOW:
2541                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
2542                 if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE)
2543                         hena |=
2544                           BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK);
2545                 break;
2546         case TCP_V6_FLOW:
2547                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
2548                 if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE)
2549                         hena |=
2550                           BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK);
2551                 if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE)
2552                         hena |=
2553                           BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK);
2554                 break;
2555         case UDP_V4_FLOW:
2556                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
2557                 if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE)
2558                         hena |=
2559                           BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
2560                           BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP);
2561
2562                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
2563                 break;
2564         case UDP_V6_FLOW:
2565                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
2566                 if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE)
2567                         hena |=
2568                           BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
2569                           BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP);
2570
2571                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
2572                 break;
2573         case AH_ESP_V4_FLOW:
2574         case AH_V4_FLOW:
2575         case ESP_V4_FLOW:
2576         case SCTP_V4_FLOW:
2577                 if ((nfc->data & RXH_L4_B_0_1) ||
2578                     (nfc->data & RXH_L4_B_2_3))
2579                         return -EINVAL;
2580                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
2581                 break;
2582         case AH_ESP_V6_FLOW:
2583         case AH_V6_FLOW:
2584         case ESP_V6_FLOW:
2585         case SCTP_V6_FLOW:
2586                 if ((nfc->data & RXH_L4_B_0_1) ||
2587                     (nfc->data & RXH_L4_B_2_3))
2588                         return -EINVAL;
2589                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
2590                 break;
2591         case IPV4_FLOW:
2592                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
2593                         BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
2594                 break;
2595         case IPV6_FLOW:
2596                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
2597                         BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
2598                 break;
2599         default:
2600                 return -EINVAL;
2601         }
2602
2603         if (flow_pctype) {
2604                 i_setc = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0,
2605                                                flow_pctype)) |
2606                         ((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1,
2607                                                flow_pctype)) << 32);
2608                 i_set = i40e_get_rss_hash_bits(nfc, i_setc);
2609                 i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(0, flow_pctype),
2610                                   (u32)i_set);
2611                 i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(1, flow_pctype),
2612                                   (u32)(i_set >> 32));
2613                 hena |= BIT_ULL(flow_pctype);
2614         }
2615
2616         i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
2617         i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
2618         i40e_flush(hw);
2619
2620         return 0;
2621 }
2622
2623 /**
2624  * i40e_match_fdir_input_set - Match a new filter against an existing one
2625  * @rule: The filter already added
2626  * @input: The new filter to comapre against
2627  *
2628  * Returns true if the two input set match
2629  **/
2630 static bool i40e_match_fdir_input_set(struct i40e_fdir_filter *rule,
2631                                       struct i40e_fdir_filter *input)
2632 {
2633         if ((rule->dst_ip != input->dst_ip) ||
2634             (rule->src_ip != input->src_ip) ||
2635             (rule->dst_port != input->dst_port) ||
2636             (rule->src_port != input->src_port))
2637                 return false;
2638         return true;
2639 }
2640
2641 /**
2642  * i40e_update_ethtool_fdir_entry - Updates the fdir filter entry
2643  * @vsi: Pointer to the targeted VSI
2644  * @input: The filter to update or NULL to indicate deletion
2645  * @sw_idx: Software index to the filter
2646  * @cmd: The command to get or set Rx flow classification rules
2647  *
2648  * This function updates (or deletes) a Flow Director entry from
2649  * the hlist of the corresponding PF
2650  *
2651  * Returns 0 on success
2652  **/
2653 static int i40e_update_ethtool_fdir_entry(struct i40e_vsi *vsi,
2654                                           struct i40e_fdir_filter *input,
2655                                           u16 sw_idx,
2656                                           struct ethtool_rxnfc *cmd)
2657 {
2658         struct i40e_fdir_filter *rule, *parent;
2659         struct i40e_pf *pf = vsi->back;
2660         struct hlist_node *node2;
2661         int err = -EINVAL;
2662
2663         parent = NULL;
2664         rule = NULL;
2665
2666         hlist_for_each_entry_safe(rule, node2,
2667                                   &pf->fdir_filter_list, fdir_node) {
2668                 /* hash found, or no matching entry */
2669                 if (rule->fd_id >= sw_idx)
2670                         break;
2671                 parent = rule;
2672         }
2673
2674         /* if there is an old rule occupying our place remove it */
2675         if (rule && (rule->fd_id == sw_idx)) {
2676                 if (input && !i40e_match_fdir_input_set(rule, input))
2677                         err = i40e_add_del_fdir(vsi, rule, false);
2678                 else if (!input)
2679                         err = i40e_add_del_fdir(vsi, rule, false);
2680                 hlist_del(&rule->fdir_node);
2681                 kfree(rule);
2682                 pf->fdir_pf_active_filters--;
2683         }
2684
2685         /* If no input this was a delete, err should be 0 if a rule was
2686          * successfully found and removed from the list else -EINVAL
2687          */
2688         if (!input)
2689                 return err;
2690
2691         /* initialize node and set software index */
2692         INIT_HLIST_NODE(&input->fdir_node);
2693
2694         /* add filter to the list */
2695         if (parent)
2696                 hlist_add_behind(&input->fdir_node, &parent->fdir_node);
2697         else
2698                 hlist_add_head(&input->fdir_node,
2699                                &pf->fdir_filter_list);
2700
2701         /* update counts */
2702         pf->fdir_pf_active_filters++;
2703
2704         return 0;
2705 }
2706
2707 /**
2708  * i40e_del_fdir_entry - Deletes a Flow Director filter entry
2709  * @vsi: Pointer to the targeted VSI
2710  * @cmd: The command to get or set Rx flow classification rules
2711  *
2712  * The function removes a Flow Director filter entry from the
2713  * hlist of the corresponding PF
2714  *
2715  * Returns 0 on success
2716  */
2717 static int i40e_del_fdir_entry(struct i40e_vsi *vsi,
2718                                struct ethtool_rxnfc *cmd)
2719 {
2720         struct ethtool_rx_flow_spec *fsp =
2721                 (struct ethtool_rx_flow_spec *)&cmd->fs;
2722         struct i40e_pf *pf = vsi->back;
2723         int ret = 0;
2724
2725         if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
2726             test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
2727                 return -EBUSY;
2728
2729         if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
2730                 return -EBUSY;
2731
2732         ret = i40e_update_ethtool_fdir_entry(vsi, NULL, fsp->location, cmd);
2733
2734         i40e_fdir_check_and_reenable(pf);
2735         return ret;
2736 }
2737
2738 /**
2739  * i40e_add_fdir_ethtool - Add/Remove Flow Director filters
2740  * @vsi: pointer to the targeted VSI
2741  * @cmd: command to get or set RX flow classification rules
2742  *
2743  * Add Flow Director filters for a specific flow spec based on their
2744  * protocol.  Returns 0 if the filters were successfully added.
2745  **/
2746 static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
2747                                  struct ethtool_rxnfc *cmd)
2748 {
2749         struct ethtool_rx_flow_spec *fsp;
2750         struct i40e_fdir_filter *input;
2751         struct i40e_pf *pf;
2752         int ret = -EINVAL;
2753         u16 vf_id;
2754
2755         if (!vsi)
2756                 return -EINVAL;
2757         pf = vsi->back;
2758
2759         if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
2760                 return -EOPNOTSUPP;
2761
2762         if (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)
2763                 return -ENOSPC;
2764
2765         if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) ||
2766             test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state))
2767                 return -EBUSY;
2768
2769         if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
2770                 return -EBUSY;
2771
2772         fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
2773
2774         if (fsp->location >= (pf->hw.func_caps.fd_filters_best_effort +
2775                               pf->hw.func_caps.fd_filters_guaranteed)) {
2776                 return -EINVAL;
2777         }
2778
2779         if ((fsp->ring_cookie != RX_CLS_FLOW_DISC) &&
2780             (fsp->ring_cookie >= vsi->num_queue_pairs))
2781                 return -EINVAL;
2782
2783         input = kzalloc(sizeof(*input), GFP_KERNEL);
2784
2785         if (!input)
2786                 return -ENOMEM;
2787
2788         input->fd_id = fsp->location;
2789
2790         if (fsp->ring_cookie == RX_CLS_FLOW_DISC)
2791                 input->dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
2792         else
2793                 input->dest_ctl =
2794                              I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX;
2795
2796         input->q_index = fsp->ring_cookie;
2797         input->flex_off = 0;
2798         input->pctype = 0;
2799         input->dest_vsi = vsi->id;
2800         input->fd_status = I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID;
2801         input->cnt_index  = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
2802         input->flow_type = fsp->flow_type;
2803         input->ip4_proto = fsp->h_u.usr_ip4_spec.proto;
2804
2805         /* Reverse the src and dest notion, since the HW expects them to be from
2806          * Tx perspective where as the input from user is from Rx filter view.
2807          */
2808         input->dst_port = fsp->h_u.tcp_ip4_spec.psrc;
2809         input->src_port = fsp->h_u.tcp_ip4_spec.pdst;
2810         input->dst_ip = fsp->h_u.tcp_ip4_spec.ip4src;
2811         input->src_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
2812
2813         if (ntohl(fsp->m_ext.data[1])) {
2814                 vf_id = ntohl(fsp->h_ext.data[1]);
2815                 if (vf_id >= pf->num_alloc_vfs) {
2816                         netif_info(pf, drv, vsi->netdev,
2817                                    "Invalid VF id %d\n", vf_id);
2818                         goto free_input;
2819                 }
2820                 /* Find vsi id from vf id and override dest vsi */
2821                 input->dest_vsi = pf->vf[vf_id].lan_vsi_id;
2822                 if (input->q_index >= pf->vf[vf_id].num_queue_pairs) {
2823                         netif_info(pf, drv, vsi->netdev,
2824                                    "Invalid queue id %d for VF %d\n",
2825                                    input->q_index, vf_id);
2826                         goto free_input;
2827                 }
2828         }
2829
2830         ret = i40e_add_del_fdir(vsi, input, true);
2831         if (ret)
2832                 goto free_input;
2833
2834         /* Add the input filter to the fdir_input_list, possibly replacing
2835          * a previous filter. Do not free the input structure after adding it
2836          * to the list as this would cause a use-after-free bug.
2837          */
2838         i40e_update_ethtool_fdir_entry(vsi, input, fsp->location, NULL);
2839
2840         return 0;
2841
2842 free_input:
2843         kfree(input);
2844         return ret;
2845 }
2846
2847 /**
2848  * i40e_set_rxnfc - command to set RX flow classification rules
2849  * @netdev: network interface device structure
2850  * @cmd: ethtool rxnfc command
2851  *
2852  * Returns Success if the command is supported.
2853  **/
2854 static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
2855 {
2856         struct i40e_netdev_priv *np = netdev_priv(netdev);
2857         struct i40e_vsi *vsi = np->vsi;
2858         struct i40e_pf *pf = vsi->back;
2859         int ret = -EOPNOTSUPP;
2860
2861         switch (cmd->cmd) {
2862         case ETHTOOL_SRXFH:
2863                 ret = i40e_set_rss_hash_opt(pf, cmd);
2864                 break;
2865         case ETHTOOL_SRXCLSRLINS:
2866                 ret = i40e_add_fdir_ethtool(vsi, cmd);
2867                 break;
2868         case ETHTOOL_SRXCLSRLDEL:
2869                 ret = i40e_del_fdir_entry(vsi, cmd);
2870                 break;
2871         default:
2872                 break;
2873         }
2874
2875         return ret;
2876 }
2877
2878 /**
2879  * i40e_max_channels - get Max number of combined channels supported
2880  * @vsi: vsi pointer
2881  **/
2882 static unsigned int i40e_max_channels(struct i40e_vsi *vsi)
2883 {
2884         /* TODO: This code assumes DCB and FD is disabled for now. */
2885         return vsi->alloc_queue_pairs;
2886 }
2887
2888 /**
2889  * i40e_get_channels - Get the current channels enabled and max supported etc.
2890  * @netdev: network interface device structure
2891  * @ch: ethtool channels structure
2892  *
2893  * We don't support separate tx and rx queues as channels. The other count
2894  * represents how many queues are being used for control. max_combined counts
2895  * how many queue pairs we can support. They may not be mapped 1 to 1 with
2896  * q_vectors since we support a lot more queue pairs than q_vectors.
2897  **/
2898 static void i40e_get_channels(struct net_device *dev,
2899                                struct ethtool_channels *ch)
2900 {
2901         struct i40e_netdev_priv *np = netdev_priv(dev);
2902         struct i40e_vsi *vsi = np->vsi;
2903         struct i40e_pf *pf = vsi->back;
2904
2905         /* report maximum channels */
2906         ch->max_combined = i40e_max_channels(vsi);
2907
2908         /* report info for other vector */
2909         ch->other_count = (pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0;
2910         ch->max_other = ch->other_count;
2911
2912         /* Note: This code assumes DCB is disabled for now. */
2913         ch->combined_count = vsi->num_queue_pairs;
2914 }
2915
2916 /**
2917  * i40e_set_channels - Set the new channels count.
2918  * @netdev: network interface device structure
2919  * @ch: ethtool channels structure
2920  *
2921  * The new channels count may not be the same as requested by the user
2922  * since it gets rounded down to a power of 2 value.
2923  **/
2924 static int i40e_set_channels(struct net_device *dev,
2925                               struct ethtool_channels *ch)
2926 {
2927         const u8 drop = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
2928         struct i40e_netdev_priv *np = netdev_priv(dev);
2929         unsigned int count = ch->combined_count;
2930         struct i40e_vsi *vsi = np->vsi;
2931         struct i40e_pf *pf = vsi->back;
2932         struct i40e_fdir_filter *rule;
2933         struct hlist_node *node2;
2934         int new_count;
2935         int err = 0;
2936
2937         /* We do not support setting channels for any other VSI at present */
2938         if (vsi->type != I40E_VSI_MAIN)
2939                 return -EINVAL;
2940
2941         /* verify they are not requesting separate vectors */
2942         if (!count || ch->rx_count || ch->tx_count)
2943                 return -EINVAL;
2944
2945         /* verify other_count has not changed */
2946         if (ch->other_count != ((pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0))
2947                 return -EINVAL;
2948
2949         /* verify the number of channels does not exceed hardware limits */
2950         if (count > i40e_max_channels(vsi))
2951                 return -EINVAL;
2952
2953         /* verify that the number of channels does not invalidate any current
2954          * flow director rules
2955          */
2956         hlist_for_each_entry_safe(rule, node2,
2957                                   &pf->fdir_filter_list, fdir_node) {
2958                 if (rule->dest_ctl != drop && count <= rule->q_index) {
2959                         dev_warn(&pf->pdev->dev,
2960                                  "Existing user defined filter %d assigns flow to queue %d\n",
2961                                  rule->fd_id, rule->q_index);
2962                         err = -EINVAL;
2963                 }
2964         }
2965
2966         if (err) {
2967                 dev_err(&pf->pdev->dev,
2968                         "Existing filter rules must be deleted to reduce combined channel count to %d\n",
2969                         count);
2970                 return err;
2971         }
2972
2973         /* update feature limits from largest to smallest supported values */
2974         /* TODO: Flow director limit, DCB etc */
2975
2976         /* use rss_reconfig to rebuild with new queue count and update traffic
2977          * class queue mapping
2978          */
2979         new_count = i40e_reconfig_rss_queues(pf, count);
2980         if (new_count > 0)
2981                 return 0;
2982         else
2983                 return -EINVAL;
2984 }
2985
2986 /**
2987  * i40e_get_rxfh_key_size - get the RSS hash key size
2988  * @netdev: network interface device structure
2989  *
2990  * Returns the table size.
2991  **/
2992 static u32 i40e_get_rxfh_key_size(struct net_device *netdev)
2993 {
2994         return I40E_HKEY_ARRAY_SIZE;
2995 }
2996
2997 /**
2998  * i40e_get_rxfh_indir_size - get the rx flow hash indirection table size
2999  * @netdev: network interface device structure
3000  *
3001  * Returns the table size.
3002  **/
3003 static u32 i40e_get_rxfh_indir_size(struct net_device *netdev)
3004 {
3005         return I40E_HLUT_ARRAY_SIZE;
3006 }
3007
3008 static int i40e_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
3009                          u8 *hfunc)
3010 {
3011         struct i40e_netdev_priv *np = netdev_priv(netdev);
3012         struct i40e_vsi *vsi = np->vsi;
3013         u8 *lut, *seed = NULL;
3014         int ret;
3015         u16 i;
3016
3017         if (hfunc)
3018                 *hfunc = ETH_RSS_HASH_TOP;
3019
3020         if (!indir)
3021                 return 0;
3022
3023         seed = key;
3024         lut = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
3025         if (!lut)
3026                 return -ENOMEM;
3027         ret = i40e_get_rss(vsi, seed, lut, I40E_HLUT_ARRAY_SIZE);
3028         if (ret)
3029                 goto out;
3030         for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
3031                 indir[i] = (u32)(lut[i]);
3032
3033 out:
3034         kfree(lut);
3035
3036         return ret;
3037 }
3038
3039 /**
3040  * i40e_set_rxfh - set the rx flow hash indirection table
3041  * @netdev: network interface device structure
3042  * @indir: indirection table
3043  * @key: hash key
3044  *
3045  * Returns -EINVAL if the table specifies an invalid queue id, otherwise
3046  * returns 0 after programming the table.
3047  **/
3048 static int i40e_set_rxfh(struct net_device *netdev, const u32 *indir,
3049                          const u8 *key, const u8 hfunc)
3050 {
3051         struct i40e_netdev_priv *np = netdev_priv(netdev);
3052         struct i40e_vsi *vsi = np->vsi;
3053         struct i40e_pf *pf = vsi->back;
3054         u8 *seed = NULL;
3055         u16 i;
3056
3057         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3058                 return -EOPNOTSUPP;
3059
3060         if (key) {
3061                 if (!vsi->rss_hkey_user) {
3062                         vsi->rss_hkey_user = kzalloc(I40E_HKEY_ARRAY_SIZE,
3063                                                      GFP_KERNEL);
3064                         if (!vsi->rss_hkey_user)
3065                                 return -ENOMEM;
3066                 }
3067                 memcpy(vsi->rss_hkey_user, key, I40E_HKEY_ARRAY_SIZE);
3068                 seed = vsi->rss_hkey_user;
3069         }
3070         if (!vsi->rss_lut_user) {
3071                 vsi->rss_lut_user = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
3072                 if (!vsi->rss_lut_user)
3073                         return -ENOMEM;
3074         }
3075
3076         /* Each 32 bits pointed by 'indir' is stored with a lut entry */
3077         if (indir)
3078                 for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
3079                         vsi->rss_lut_user[i] = (u8)(indir[i]);
3080         else
3081                 i40e_fill_rss_lut(pf, vsi->rss_lut_user, I40E_HLUT_ARRAY_SIZE,
3082                                   vsi->rss_size);
3083
3084         return i40e_config_rss(vsi, seed, vsi->rss_lut_user,
3085                                I40E_HLUT_ARRAY_SIZE);
3086 }
3087
3088 /**
3089  * i40e_get_priv_flags - report device private flags
3090  * @dev: network interface device structure
3091  *
3092  * The get string set count and the string set should be matched for each
3093  * flag returned.  Add new strings for each flag to the i40e_priv_flags_strings
3094  * array.
3095  *
3096  * Returns a u32 bitmap of flags.
3097  **/
3098 static u32 i40e_get_priv_flags(struct net_device *dev)
3099 {
3100         struct i40e_netdev_priv *np = netdev_priv(dev);
3101         struct i40e_vsi *vsi = np->vsi;
3102         struct i40e_pf *pf = vsi->back;
3103         u32 ret_flags = 0;
3104
3105         ret_flags |= pf->flags & I40E_FLAG_LINK_POLLING_ENABLED ?
3106                 I40E_PRIV_FLAGS_LINKPOLL_FLAG : 0;
3107         ret_flags |= pf->flags & I40E_FLAG_FD_ATR_ENABLED ?
3108                 I40E_PRIV_FLAGS_FD_ATR : 0;
3109         ret_flags |= pf->flags & I40E_FLAG_VEB_STATS_ENABLED ?
3110                 I40E_PRIV_FLAGS_VEB_STATS : 0;
3111         ret_flags |= pf->hw_disabled_flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE ?
3112                 0 : I40E_PRIV_FLAGS_HW_ATR_EVICT;
3113         if (pf->hw.pf_id == 0) {
3114                 ret_flags |= pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT ?
3115                         I40E_PRIV_FLAGS_TRUE_PROMISC_SUPPORT : 0;
3116         }
3117
3118         return ret_flags;
3119 }
3120
3121 /**
3122  * i40e_set_priv_flags - set private flags
3123  * @dev: network interface device structure
3124  * @flags: bit flags to be set
3125  **/
3126 static int i40e_set_priv_flags(struct net_device *dev, u32 flags)
3127 {
3128         struct i40e_netdev_priv *np = netdev_priv(dev);
3129         struct i40e_vsi *vsi = np->vsi;
3130         struct i40e_pf *pf = vsi->back;
3131         u16 sw_flags = 0, valid_flags = 0;
3132         bool reset_required = false;
3133         bool promisc_change = false;
3134         int ret;
3135
3136         /* NOTE: MFP is not settable */
3137
3138         if (flags & I40E_PRIV_FLAGS_LINKPOLL_FLAG)
3139                 pf->flags |= I40E_FLAG_LINK_POLLING_ENABLED;
3140         else
3141                 pf->flags &= ~I40E_FLAG_LINK_POLLING_ENABLED;
3142
3143         /* allow the user to control the state of the Flow
3144          * Director ATR (Application Targeted Routing) feature
3145          * of the driver
3146          */
3147         if (flags & I40E_PRIV_FLAGS_FD_ATR) {
3148                 pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
3149         } else {
3150                 pf->flags &= ~I40E_FLAG_FD_ATR_ENABLED;
3151                 pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
3152
3153                 /* flush current ATR settings */
3154                 set_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
3155         }
3156
3157         if ((flags & I40E_PRIV_FLAGS_VEB_STATS) &&
3158             !(pf->flags & I40E_FLAG_VEB_STATS_ENABLED)) {
3159                 pf->flags |= I40E_FLAG_VEB_STATS_ENABLED;
3160                 reset_required = true;
3161         } else if (!(flags & I40E_PRIV_FLAGS_VEB_STATS) &&
3162                    (pf->flags & I40E_FLAG_VEB_STATS_ENABLED)) {
3163                 pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
3164                 reset_required = true;
3165         }
3166
3167         if (pf->hw.pf_id == 0) {
3168                 if ((flags & I40E_PRIV_FLAGS_TRUE_PROMISC_SUPPORT) &&
3169                     !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
3170                         pf->flags |= I40E_FLAG_TRUE_PROMISC_SUPPORT;
3171                         promisc_change = true;
3172                 } else if (!(flags & I40E_PRIV_FLAGS_TRUE_PROMISC_SUPPORT) &&
3173                            (pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
3174                         pf->flags &= ~I40E_FLAG_TRUE_PROMISC_SUPPORT;
3175                         promisc_change = true;
3176                 }
3177         }
3178         if (promisc_change) {
3179                 if (!(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
3180                         sw_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
3181                 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
3182                 ret = i40e_aq_set_switch_config(&pf->hw, sw_flags, valid_flags,
3183                                                 NULL);
3184                 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
3185                         dev_info(&pf->pdev->dev,
3186                                  "couldn't set switch config bits, err %s aq_err %s\n",
3187                                  i40e_stat_str(&pf->hw, ret),
3188                                  i40e_aq_str(&pf->hw,
3189                                              pf->hw.aq.asq_last_status));
3190                         /* not a fatal problem, just keep going */
3191                 }
3192         }
3193
3194         if ((flags & I40E_PRIV_FLAGS_HW_ATR_EVICT) &&
3195             (pf->flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE))
3196                 pf->hw_disabled_flags &= ~I40E_FLAG_HW_ATR_EVICT_CAPABLE;
3197         else
3198                 pf->hw_disabled_flags |= I40E_FLAG_HW_ATR_EVICT_CAPABLE;
3199
3200         /* if needed, issue reset to cause things to take effect */
3201         if (reset_required)
3202                 i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED));
3203
3204         return 0;
3205 }
3206
3207 static const struct ethtool_ops i40e_ethtool_ops = {
3208         .get_drvinfo            = i40e_get_drvinfo,
3209         .get_regs_len           = i40e_get_regs_len,
3210         .get_regs               = i40e_get_regs,
3211         .nway_reset             = i40e_nway_reset,
3212         .get_link               = ethtool_op_get_link,
3213         .get_wol                = i40e_get_wol,
3214         .set_wol                = i40e_set_wol,
3215         .set_eeprom             = i40e_set_eeprom,
3216         .get_eeprom_len         = i40e_get_eeprom_len,
3217         .get_eeprom             = i40e_get_eeprom,
3218         .get_ringparam          = i40e_get_ringparam,
3219         .set_ringparam          = i40e_set_ringparam,
3220         .get_pauseparam         = i40e_get_pauseparam,
3221         .set_pauseparam         = i40e_set_pauseparam,
3222         .get_msglevel           = i40e_get_msglevel,
3223         .set_msglevel           = i40e_set_msglevel,
3224         .get_rxnfc              = i40e_get_rxnfc,
3225         .set_rxnfc              = i40e_set_rxnfc,
3226         .self_test              = i40e_diag_test,
3227         .get_strings            = i40e_get_strings,
3228         .set_phys_id            = i40e_set_phys_id,
3229         .get_sset_count         = i40e_get_sset_count,
3230         .get_ethtool_stats      = i40e_get_ethtool_stats,
3231         .get_coalesce           = i40e_get_coalesce,
3232         .set_coalesce           = i40e_set_coalesce,
3233         .get_rxfh_key_size      = i40e_get_rxfh_key_size,
3234         .get_rxfh_indir_size    = i40e_get_rxfh_indir_size,
3235         .get_rxfh               = i40e_get_rxfh,
3236         .set_rxfh               = i40e_set_rxfh,
3237         .get_channels           = i40e_get_channels,
3238         .set_channels           = i40e_set_channels,
3239         .get_ts_info            = i40e_get_ts_info,
3240         .get_priv_flags         = i40e_get_priv_flags,
3241         .set_priv_flags         = i40e_set_priv_flags,
3242         .get_per_queue_coalesce = i40e_get_per_queue_coalesce,
3243         .set_per_queue_coalesce = i40e_set_per_queue_coalesce,
3244         .get_link_ksettings     = i40e_get_link_ksettings,
3245         .set_link_ksettings     = i40e_set_link_ksettings,
3246 };
3247
3248 void i40e_set_ethtool_ops(struct net_device *netdev)
3249 {
3250         netdev->ethtool_ops = &i40e_ethtool_ops;
3251 }