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