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