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