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