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