GNU Linux-libre 4.19.304-gnu1
[releases.git] / drivers / net / ethernet / intel / i40e / i40e_ethtool.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3
4 /* ethtool support for i40e */
5
6 #include "i40e.h"
7 #include "i40e_diag.h"
8
9 struct i40e_stats {
10         /* The stat_string is expected to be a format string formatted using
11          * vsnprintf by i40e_add_stat_strings. Every member of a stats array
12          * should use the same format specifiers as they will be formatted
13          * using the same variadic arguments.
14          */
15         char stat_string[ETH_GSTRING_LEN];
16         int sizeof_stat;
17         int stat_offset;
18 };
19
20 #define I40E_STAT(_type, _name, _stat) { \
21         .stat_string = _name, \
22         .sizeof_stat = FIELD_SIZEOF(_type, _stat), \
23         .stat_offset = offsetof(_type, _stat) \
24 }
25
26 #define I40E_NETDEV_STAT(_net_stat) \
27         I40E_STAT(struct rtnl_link_stats64, #_net_stat, _net_stat)
28 #define I40E_PF_STAT(_name, _stat) \
29         I40E_STAT(struct i40e_pf, _name, _stat)
30 #define I40E_VSI_STAT(_name, _stat) \
31         I40E_STAT(struct i40e_vsi, _name, _stat)
32 #define I40E_VEB_STAT(_name, _stat) \
33         I40E_STAT(struct i40e_veb, _name, _stat)
34 #define I40E_PFC_STAT(_name, _stat) \
35         I40E_STAT(struct i40e_pfc_stats, _name, _stat)
36
37 static const struct i40e_stats i40e_gstrings_net_stats[] = {
38         I40E_NETDEV_STAT(rx_packets),
39         I40E_NETDEV_STAT(tx_packets),
40         I40E_NETDEV_STAT(rx_bytes),
41         I40E_NETDEV_STAT(tx_bytes),
42         I40E_NETDEV_STAT(rx_errors),
43         I40E_NETDEV_STAT(tx_errors),
44         I40E_NETDEV_STAT(rx_dropped),
45         I40E_NETDEV_STAT(tx_dropped),
46         I40E_NETDEV_STAT(collisions),
47         I40E_NETDEV_STAT(rx_length_errors),
48         I40E_NETDEV_STAT(rx_crc_errors),
49 };
50
51 static const struct i40e_stats i40e_gstrings_veb_stats[] = {
52         I40E_VEB_STAT("veb.rx_bytes", stats.rx_bytes),
53         I40E_VEB_STAT("veb.tx_bytes", stats.tx_bytes),
54         I40E_VEB_STAT("veb.rx_unicast", stats.rx_unicast),
55         I40E_VEB_STAT("veb.tx_unicast", stats.tx_unicast),
56         I40E_VEB_STAT("veb.rx_multicast", stats.rx_multicast),
57         I40E_VEB_STAT("veb.tx_multicast", stats.tx_multicast),
58         I40E_VEB_STAT("veb.rx_broadcast", stats.rx_broadcast),
59         I40E_VEB_STAT("veb.tx_broadcast", stats.tx_broadcast),
60         I40E_VEB_STAT("veb.rx_discards", stats.rx_discards),
61         I40E_VEB_STAT("veb.tx_discards", stats.tx_discards),
62         I40E_VEB_STAT("veb.tx_errors", stats.tx_errors),
63         I40E_VEB_STAT("veb.rx_unknown_protocol", stats.rx_unknown_protocol),
64 };
65
66 static const struct i40e_stats i40e_gstrings_veb_tc_stats[] = {
67         I40E_VEB_STAT("veb.tc_%u_tx_packets", tc_stats.tc_tx_packets),
68         I40E_VEB_STAT("veb.tc_%u_tx_bytes", tc_stats.tc_tx_bytes),
69         I40E_VEB_STAT("veb.tc_%u_rx_packets", tc_stats.tc_rx_packets),
70         I40E_VEB_STAT("veb.tc_%u_rx_bytes", tc_stats.tc_rx_bytes),
71 };
72
73 static const struct i40e_stats i40e_gstrings_misc_stats[] = {
74         I40E_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
75         I40E_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
76         I40E_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
77         I40E_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
78         I40E_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
79         I40E_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
80         I40E_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
81         I40E_VSI_STAT("tx_linearize", tx_linearize),
82         I40E_VSI_STAT("tx_force_wb", tx_force_wb),
83         I40E_VSI_STAT("tx_busy", tx_busy),
84         I40E_VSI_STAT("rx_alloc_fail", rx_buf_failed),
85         I40E_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
86 };
87
88 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
89  * but they are separate.  This device supports Virtualization, and
90  * as such might have several netdevs supporting VMDq and FCoE going
91  * through a single port.  The NETDEV_STATs are for individual netdevs
92  * seen at the top of the stack, and the PF_STATs are for the physical
93  * function at the bottom of the stack hosting those netdevs.
94  *
95  * The PF_STATs are appended to the netdev stats only when ethtool -S
96  * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
97  */
98 static const struct i40e_stats i40e_gstrings_stats[] = {
99         I40E_PF_STAT("port.rx_bytes", stats.eth.rx_bytes),
100         I40E_PF_STAT("port.tx_bytes", stats.eth.tx_bytes),
101         I40E_PF_STAT("port.rx_unicast", stats.eth.rx_unicast),
102         I40E_PF_STAT("port.tx_unicast", stats.eth.tx_unicast),
103         I40E_PF_STAT("port.rx_multicast", stats.eth.rx_multicast),
104         I40E_PF_STAT("port.tx_multicast", stats.eth.tx_multicast),
105         I40E_PF_STAT("port.rx_broadcast", stats.eth.rx_broadcast),
106         I40E_PF_STAT("port.tx_broadcast", stats.eth.tx_broadcast),
107         I40E_PF_STAT("port.tx_errors", stats.eth.tx_errors),
108         I40E_PF_STAT("port.rx_dropped", stats.eth.rx_discards),
109         I40E_PF_STAT("port.tx_dropped_link_down", stats.tx_dropped_link_down),
110         I40E_PF_STAT("port.rx_crc_errors", stats.crc_errors),
111         I40E_PF_STAT("port.illegal_bytes", stats.illegal_bytes),
112         I40E_PF_STAT("port.mac_local_faults", stats.mac_local_faults),
113         I40E_PF_STAT("port.mac_remote_faults", stats.mac_remote_faults),
114         I40E_PF_STAT("port.tx_timeout", tx_timeout_count),
115         I40E_PF_STAT("port.rx_csum_bad", hw_csum_rx_error),
116         I40E_PF_STAT("port.rx_length_errors", stats.rx_length_errors),
117         I40E_PF_STAT("port.link_xon_rx", stats.link_xon_rx),
118         I40E_PF_STAT("port.link_xoff_rx", stats.link_xoff_rx),
119         I40E_PF_STAT("port.link_xon_tx", stats.link_xon_tx),
120         I40E_PF_STAT("port.link_xoff_tx", stats.link_xoff_tx),
121         I40E_PF_STAT("port.rx_size_64", stats.rx_size_64),
122         I40E_PF_STAT("port.rx_size_127", stats.rx_size_127),
123         I40E_PF_STAT("port.rx_size_255", stats.rx_size_255),
124         I40E_PF_STAT("port.rx_size_511", stats.rx_size_511),
125         I40E_PF_STAT("port.rx_size_1023", stats.rx_size_1023),
126         I40E_PF_STAT("port.rx_size_1522", stats.rx_size_1522),
127         I40E_PF_STAT("port.rx_size_big", stats.rx_size_big),
128         I40E_PF_STAT("port.tx_size_64", stats.tx_size_64),
129         I40E_PF_STAT("port.tx_size_127", stats.tx_size_127),
130         I40E_PF_STAT("port.tx_size_255", stats.tx_size_255),
131         I40E_PF_STAT("port.tx_size_511", stats.tx_size_511),
132         I40E_PF_STAT("port.tx_size_1023", stats.tx_size_1023),
133         I40E_PF_STAT("port.tx_size_1522", stats.tx_size_1522),
134         I40E_PF_STAT("port.tx_size_big", stats.tx_size_big),
135         I40E_PF_STAT("port.rx_undersize", stats.rx_undersize),
136         I40E_PF_STAT("port.rx_fragments", stats.rx_fragments),
137         I40E_PF_STAT("port.rx_oversize", stats.rx_oversize),
138         I40E_PF_STAT("port.rx_jabber", stats.rx_jabber),
139         I40E_PF_STAT("port.VF_admin_queue_requests", vf_aq_requests),
140         I40E_PF_STAT("port.arq_overflows", arq_overflows),
141         I40E_PF_STAT("port.tx_hwtstamp_timeouts", tx_hwtstamp_timeouts),
142         I40E_PF_STAT("port.rx_hwtstamp_cleared", rx_hwtstamp_cleared),
143         I40E_PF_STAT("port.tx_hwtstamp_skipped", tx_hwtstamp_skipped),
144         I40E_PF_STAT("port.fdir_flush_cnt", fd_flush_cnt),
145         I40E_PF_STAT("port.fdir_atr_match", stats.fd_atr_match),
146         I40E_PF_STAT("port.fdir_atr_tunnel_match", stats.fd_atr_tunnel_match),
147         I40E_PF_STAT("port.fdir_atr_status", stats.fd_atr_status),
148         I40E_PF_STAT("port.fdir_sb_match", stats.fd_sb_match),
149         I40E_PF_STAT("port.fdir_sb_status", stats.fd_sb_status),
150
151         /* LPI stats */
152         I40E_PF_STAT("port.tx_lpi_status", stats.tx_lpi_status),
153         I40E_PF_STAT("port.rx_lpi_status", stats.rx_lpi_status),
154         I40E_PF_STAT("port.tx_lpi_count", stats.tx_lpi_count),
155         I40E_PF_STAT("port.rx_lpi_count", stats.rx_lpi_count),
156 };
157
158 struct i40e_pfc_stats {
159         u64 priority_xon_rx;
160         u64 priority_xoff_rx;
161         u64 priority_xon_tx;
162         u64 priority_xoff_tx;
163         u64 priority_xon_2_xoff;
164 };
165
166 static const struct i40e_stats i40e_gstrings_pfc_stats[] = {
167         I40E_PFC_STAT("port.tx_priority_%u_xon_tx", priority_xon_tx),
168         I40E_PFC_STAT("port.tx_priority_%u_xoff_tx", priority_xoff_tx),
169         I40E_PFC_STAT("port.rx_priority_%u_xon_rx", priority_xon_rx),
170         I40E_PFC_STAT("port.rx_priority_%u_xoff_rx", priority_xoff_rx),
171         I40E_PFC_STAT("port.rx_priority_%u_xon_2_xoff", priority_xon_2_xoff),
172 };
173
174 /* We use num_tx_queues here as a proxy for the maximum number of queues
175  * available because we always allocate queues symmetrically.
176  */
177 #define I40E_MAX_NUM_QUEUES(n) ((n)->num_tx_queues)
178 #define I40E_QUEUE_STATS_LEN(n)                                              \
179            (I40E_MAX_NUM_QUEUES(n)                                           \
180             * 2 /* Tx and Rx together */                                     \
181             * (sizeof(struct i40e_queue_stats) / sizeof(u64)))
182 #define I40E_GLOBAL_STATS_LEN   ARRAY_SIZE(i40e_gstrings_stats)
183 #define I40E_NETDEV_STATS_LEN   ARRAY_SIZE(i40e_gstrings_net_stats)
184 #define I40E_MISC_STATS_LEN     ARRAY_SIZE(i40e_gstrings_misc_stats)
185 #define I40E_VSI_STATS_LEN(n)   (I40E_NETDEV_STATS_LEN + \
186                                  I40E_MISC_STATS_LEN + \
187                                  I40E_QUEUE_STATS_LEN((n)))
188
189 #define I40E_PFC_STATS_LEN      (ARRAY_SIZE(i40e_gstrings_pfc_stats) * \
190                                  I40E_MAX_USER_PRIORITY)
191
192 #define I40E_VEB_STATS_LEN      (ARRAY_SIZE(i40e_gstrings_veb_stats) + \
193                                  (ARRAY_SIZE(i40e_gstrings_veb_tc_stats) * \
194                                   I40E_MAX_TRAFFIC_CLASS))
195
196 #define I40E_PF_STATS_LEN(n)    (I40E_GLOBAL_STATS_LEN + \
197                                  I40E_PFC_STATS_LEN + \
198                                  I40E_VEB_STATS_LEN + \
199                                  I40E_VSI_STATS_LEN((n)))
200
201 enum i40e_ethtool_test_id {
202         I40E_ETH_TEST_REG = 0,
203         I40E_ETH_TEST_EEPROM,
204         I40E_ETH_TEST_INTR,
205         I40E_ETH_TEST_LINK,
206 };
207
208 static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
209         "Register test  (offline)",
210         "Eeprom test    (offline)",
211         "Interrupt test (offline)",
212         "Link test   (on/offline)"
213 };
214
215 #define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
216
217 struct i40e_priv_flags {
218         char flag_string[ETH_GSTRING_LEN];
219         u64 flag;
220         bool read_only;
221 };
222
223 #define I40E_PRIV_FLAG(_name, _flag, _read_only) { \
224         .flag_string = _name, \
225         .flag = _flag, \
226         .read_only = _read_only, \
227 }
228
229 static const struct i40e_priv_flags i40e_gstrings_priv_flags[] = {
230         /* NOTE: MFP setting cannot be changed */
231         I40E_PRIV_FLAG("MFP", I40E_FLAG_MFP_ENABLED, 1),
232         I40E_PRIV_FLAG("LinkPolling", I40E_FLAG_LINK_POLLING_ENABLED, 0),
233         I40E_PRIV_FLAG("flow-director-atr", I40E_FLAG_FD_ATR_ENABLED, 0),
234         I40E_PRIV_FLAG("veb-stats", I40E_FLAG_VEB_STATS_ENABLED, 0),
235         I40E_PRIV_FLAG("hw-atr-eviction", I40E_FLAG_HW_ATR_EVICT_ENABLED, 0),
236         I40E_PRIV_FLAG("link-down-on-close",
237                        I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED, 0),
238         I40E_PRIV_FLAG("legacy-rx", I40E_FLAG_LEGACY_RX, 0),
239         I40E_PRIV_FLAG("disable-source-pruning",
240                        I40E_FLAG_SOURCE_PRUNING_DISABLED, 0),
241         I40E_PRIV_FLAG("disable-fw-lldp", I40E_FLAG_DISABLE_FW_LLDP, 0),
242 };
243
244 #define I40E_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_gstrings_priv_flags)
245
246 /* Private flags with a global effect, restricted to PF 0 */
247 static const struct i40e_priv_flags i40e_gl_gstrings_priv_flags[] = {
248         I40E_PRIV_FLAG("vf-true-promisc-support",
249                        I40E_FLAG_TRUE_PROMISC_SUPPORT, 0),
250 };
251
252 #define I40E_GL_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_gl_gstrings_priv_flags)
253
254 /**
255  * i40e_partition_setting_complaint - generic complaint for MFP restriction
256  * @pf: the PF struct
257  **/
258 static void i40e_partition_setting_complaint(struct i40e_pf *pf)
259 {
260         dev_info(&pf->pdev->dev,
261                  "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");
262 }
263
264 /**
265  * i40e_phy_type_to_ethtool - convert the phy_types to ethtool link modes
266  * @pf: PF struct with phy_types
267  * @ks: ethtool link ksettings struct to fill out
268  *
269  **/
270 static void i40e_phy_type_to_ethtool(struct i40e_pf *pf,
271                                      struct ethtool_link_ksettings *ks)
272 {
273         struct i40e_link_status *hw_link_info = &pf->hw.phy.link_info;
274         u64 phy_types = pf->hw.phy.phy_types;
275
276         ethtool_link_ksettings_zero_link_mode(ks, supported);
277         ethtool_link_ksettings_zero_link_mode(ks, advertising);
278
279         if (phy_types & I40E_CAP_PHY_TYPE_SGMII) {
280                 ethtool_link_ksettings_add_link_mode(ks, supported,
281                                                      1000baseT_Full);
282                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
283                         ethtool_link_ksettings_add_link_mode(ks, advertising,
284                                                              1000baseT_Full);
285                 if (pf->hw_features & I40E_HW_100M_SGMII_CAPABLE) {
286                         ethtool_link_ksettings_add_link_mode(ks, supported,
287                                                              100baseT_Full);
288                         ethtool_link_ksettings_add_link_mode(ks, advertising,
289                                                              100baseT_Full);
290                 }
291         }
292         if (phy_types & I40E_CAP_PHY_TYPE_XAUI ||
293             phy_types & I40E_CAP_PHY_TYPE_XFI ||
294             phy_types & I40E_CAP_PHY_TYPE_SFI ||
295             phy_types & I40E_CAP_PHY_TYPE_10GBASE_SFPP_CU ||
296             phy_types & I40E_CAP_PHY_TYPE_10GBASE_AOC) {
297                 ethtool_link_ksettings_add_link_mode(ks, supported,
298                                                      10000baseT_Full);
299                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
300                         ethtool_link_ksettings_add_link_mode(ks, advertising,
301                                                              10000baseT_Full);
302         }
303         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_T) {
304                 ethtool_link_ksettings_add_link_mode(ks, supported,
305                                                      10000baseT_Full);
306                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
307                         ethtool_link_ksettings_add_link_mode(ks, advertising,
308                                                              10000baseT_Full);
309         }
310         if (phy_types & I40E_CAP_PHY_TYPE_XLAUI ||
311             phy_types & I40E_CAP_PHY_TYPE_XLPPI ||
312             phy_types & I40E_CAP_PHY_TYPE_40GBASE_AOC)
313                 ethtool_link_ksettings_add_link_mode(ks, supported,
314                                                      40000baseCR4_Full);
315         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
316             phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4) {
317                 ethtool_link_ksettings_add_link_mode(ks, supported,
318                                                      40000baseCR4_Full);
319                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_40GB)
320                         ethtool_link_ksettings_add_link_mode(ks, advertising,
321                                                              40000baseCR4_Full);
322         }
323         if (phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) {
324                 ethtool_link_ksettings_add_link_mode(ks, supported,
325                                                      100baseT_Full);
326                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
327                         ethtool_link_ksettings_add_link_mode(ks, advertising,
328                                                              100baseT_Full);
329         }
330         if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_T) {
331                 ethtool_link_ksettings_add_link_mode(ks, supported,
332                                                      1000baseT_Full);
333                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
334                         ethtool_link_ksettings_add_link_mode(ks, advertising,
335                                                              1000baseT_Full);
336         }
337         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_SR4)
338                 ethtool_link_ksettings_add_link_mode(ks, supported,
339                                                      40000baseSR4_Full);
340         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_LR4)
341                 ethtool_link_ksettings_add_link_mode(ks, supported,
342                                                      40000baseLR4_Full);
343         if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4) {
344                 ethtool_link_ksettings_add_link_mode(ks, supported,
345                                                      40000baseLR4_Full);
346                 ethtool_link_ksettings_add_link_mode(ks, advertising,
347                                                      40000baseLR4_Full);
348         }
349         if (phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2) {
350                 ethtool_link_ksettings_add_link_mode(ks, supported,
351                                                      20000baseKR2_Full);
352                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_20GB)
353                         ethtool_link_ksettings_add_link_mode(ks, advertising,
354                                                              20000baseKR2_Full);
355         }
356         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4) {
357                 ethtool_link_ksettings_add_link_mode(ks, supported,
358                                                      10000baseKX4_Full);
359                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
360                         ethtool_link_ksettings_add_link_mode(ks, advertising,
361                                                              10000baseKX4_Full);
362         }
363         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR &&
364             !(pf->hw_features & I40E_HW_HAVE_CRT_RETIMER)) {
365                 ethtool_link_ksettings_add_link_mode(ks, supported,
366                                                      10000baseKR_Full);
367                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
368                         ethtool_link_ksettings_add_link_mode(ks, advertising,
369                                                              10000baseKR_Full);
370         }
371         if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX &&
372             !(pf->hw_features & I40E_HW_HAVE_CRT_RETIMER)) {
373                 ethtool_link_ksettings_add_link_mode(ks, supported,
374                                                      1000baseKX_Full);
375                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
376                         ethtool_link_ksettings_add_link_mode(ks, advertising,
377                                                              1000baseKX_Full);
378         }
379         /* need to add 25G PHY types */
380         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR) {
381                 ethtool_link_ksettings_add_link_mode(ks, supported,
382                                                      25000baseKR_Full);
383                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
384                         ethtool_link_ksettings_add_link_mode(ks, advertising,
385                                                              25000baseKR_Full);
386         }
387         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR) {
388                 ethtool_link_ksettings_add_link_mode(ks, supported,
389                                                      25000baseCR_Full);
390                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
391                         ethtool_link_ksettings_add_link_mode(ks, advertising,
392                                                              25000baseCR_Full);
393         }
394         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
395             phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR) {
396                 ethtool_link_ksettings_add_link_mode(ks, supported,
397                                                      25000baseSR_Full);
398                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
399                         ethtool_link_ksettings_add_link_mode(ks, advertising,
400                                                              25000baseSR_Full);
401         }
402         if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_AOC ||
403             phy_types & I40E_CAP_PHY_TYPE_25GBASE_ACC) {
404                 ethtool_link_ksettings_add_link_mode(ks, supported,
405                                                      25000baseCR_Full);
406                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
407                         ethtool_link_ksettings_add_link_mode(ks, advertising,
408                                                              25000baseCR_Full);
409         }
410         /* need to add new 10G PHY types */
411         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
412             phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU) {
413                 ethtool_link_ksettings_add_link_mode(ks, supported,
414                                                      10000baseCR_Full);
415                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
416                         ethtool_link_ksettings_add_link_mode(ks, advertising,
417                                                              10000baseCR_Full);
418         }
419         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR) {
420                 ethtool_link_ksettings_add_link_mode(ks, supported,
421                                                      10000baseSR_Full);
422                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
423                         ethtool_link_ksettings_add_link_mode(ks, advertising,
424                                                              10000baseSR_Full);
425         }
426         if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR) {
427                 ethtool_link_ksettings_add_link_mode(ks, supported,
428                                                      10000baseLR_Full);
429                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
430                         ethtool_link_ksettings_add_link_mode(ks, advertising,
431                                                              10000baseLR_Full);
432         }
433         if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
434             phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
435             phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL) {
436                 ethtool_link_ksettings_add_link_mode(ks, supported,
437                                                      1000baseX_Full);
438                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
439                         ethtool_link_ksettings_add_link_mode(ks, advertising,
440                                                              1000baseX_Full);
441         }
442         /* Autoneg PHY types */
443         if (phy_types & I40E_CAP_PHY_TYPE_SGMII ||
444             phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4 ||
445             phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
446             phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4 ||
447             phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
448             phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR ||
449             phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR ||
450             phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR ||
451             phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2 ||
452             phy_types & I40E_CAP_PHY_TYPE_10GBASE_T ||
453             phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR ||
454             phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR ||
455             phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4 ||
456             phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR ||
457             phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU ||
458             phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
459             phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL ||
460             phy_types & I40E_CAP_PHY_TYPE_1000BASE_T ||
461             phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
462             phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
463             phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX ||
464             phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) {
465                 ethtool_link_ksettings_add_link_mode(ks, supported,
466                                                      Autoneg);
467                 ethtool_link_ksettings_add_link_mode(ks, advertising,
468                                                      Autoneg);
469         }
470 }
471
472 /**
473  * i40e_get_settings_link_up - Get the Link settings for when link is up
474  * @hw: hw structure
475  * @ks: ethtool ksettings to fill in
476  * @netdev: network interface device structure
477  * @pf: pointer to physical function struct
478  **/
479 static void i40e_get_settings_link_up(struct i40e_hw *hw,
480                                       struct ethtool_link_ksettings *ks,
481                                       struct net_device *netdev,
482                                       struct i40e_pf *pf)
483 {
484         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
485         struct ethtool_link_ksettings cap_ksettings;
486         u32 link_speed = hw_link_info->link_speed;
487
488         /* Initialize supported and advertised settings based on phy settings */
489         switch (hw_link_info->phy_type) {
490         case I40E_PHY_TYPE_40GBASE_CR4:
491         case I40E_PHY_TYPE_40GBASE_CR4_CU:
492                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
493                 ethtool_link_ksettings_add_link_mode(ks, supported,
494                                                      40000baseCR4_Full);
495                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
496                 ethtool_link_ksettings_add_link_mode(ks, advertising,
497                                                      40000baseCR4_Full);
498                 break;
499         case I40E_PHY_TYPE_XLAUI:
500         case I40E_PHY_TYPE_XLPPI:
501         case I40E_PHY_TYPE_40GBASE_AOC:
502                 ethtool_link_ksettings_add_link_mode(ks, supported,
503                                                      40000baseCR4_Full);
504                 break;
505         case I40E_PHY_TYPE_40GBASE_SR4:
506                 ethtool_link_ksettings_add_link_mode(ks, supported,
507                                                      40000baseSR4_Full);
508                 break;
509         case I40E_PHY_TYPE_40GBASE_LR4:
510                 ethtool_link_ksettings_add_link_mode(ks, supported,
511                                                      40000baseLR4_Full);
512                 break;
513         case I40E_PHY_TYPE_25GBASE_SR:
514         case I40E_PHY_TYPE_25GBASE_LR:
515         case I40E_PHY_TYPE_10GBASE_SR:
516         case I40E_PHY_TYPE_10GBASE_LR:
517         case I40E_PHY_TYPE_1000BASE_SX:
518         case I40E_PHY_TYPE_1000BASE_LX:
519                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
520                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
521                 ethtool_link_ksettings_add_link_mode(ks, supported,
522                                                      25000baseSR_Full);
523                 ethtool_link_ksettings_add_link_mode(ks, advertising,
524                                                      25000baseSR_Full);
525                 ethtool_link_ksettings_add_link_mode(ks, supported,
526                                                      10000baseSR_Full);
527                 ethtool_link_ksettings_add_link_mode(ks, advertising,
528                                                      10000baseSR_Full);
529                 ethtool_link_ksettings_add_link_mode(ks, supported,
530                                                      10000baseLR_Full);
531                 ethtool_link_ksettings_add_link_mode(ks, advertising,
532                                                      10000baseLR_Full);
533                 ethtool_link_ksettings_add_link_mode(ks, supported,
534                                                      1000baseX_Full);
535                 ethtool_link_ksettings_add_link_mode(ks, advertising,
536                                                      1000baseX_Full);
537                 ethtool_link_ksettings_add_link_mode(ks, supported,
538                                                      10000baseT_Full);
539                 if (hw_link_info->module_type[2] &
540                     I40E_MODULE_TYPE_1000BASE_SX ||
541                     hw_link_info->module_type[2] &
542                     I40E_MODULE_TYPE_1000BASE_LX) {
543                         ethtool_link_ksettings_add_link_mode(ks, supported,
544                                                              1000baseT_Full);
545                         if (hw_link_info->requested_speeds &
546                             I40E_LINK_SPEED_1GB)
547                                 ethtool_link_ksettings_add_link_mode(
548                                      ks, advertising, 1000baseT_Full);
549                 }
550                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
551                         ethtool_link_ksettings_add_link_mode(ks, advertising,
552                                                              10000baseT_Full);
553                 break;
554         case I40E_PHY_TYPE_10GBASE_T:
555         case I40E_PHY_TYPE_1000BASE_T:
556         case I40E_PHY_TYPE_100BASE_TX:
557                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
558                 ethtool_link_ksettings_add_link_mode(ks, supported,
559                                                      10000baseT_Full);
560                 ethtool_link_ksettings_add_link_mode(ks, supported,
561                                                      1000baseT_Full);
562                 ethtool_link_ksettings_add_link_mode(ks, supported,
563                                                      100baseT_Full);
564                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
565                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
566                         ethtool_link_ksettings_add_link_mode(ks, advertising,
567                                                              10000baseT_Full);
568                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
569                         ethtool_link_ksettings_add_link_mode(ks, advertising,
570                                                              1000baseT_Full);
571                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
572                         ethtool_link_ksettings_add_link_mode(ks, advertising,
573                                                              100baseT_Full);
574                 break;
575         case I40E_PHY_TYPE_1000BASE_T_OPTICAL:
576                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
577                 ethtool_link_ksettings_add_link_mode(ks, supported,
578                                                      1000baseT_Full);
579                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
580                 ethtool_link_ksettings_add_link_mode(ks, advertising,
581                                                      1000baseT_Full);
582                 break;
583         case I40E_PHY_TYPE_10GBASE_CR1_CU:
584         case I40E_PHY_TYPE_10GBASE_CR1:
585                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
586                 ethtool_link_ksettings_add_link_mode(ks, supported,
587                                                      10000baseT_Full);
588                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
589                 ethtool_link_ksettings_add_link_mode(ks, advertising,
590                                                      10000baseT_Full);
591                 break;
592         case I40E_PHY_TYPE_XAUI:
593         case I40E_PHY_TYPE_XFI:
594         case I40E_PHY_TYPE_SFI:
595         case I40E_PHY_TYPE_10GBASE_SFPP_CU:
596         case I40E_PHY_TYPE_10GBASE_AOC:
597                 ethtool_link_ksettings_add_link_mode(ks, supported,
598                                                      10000baseT_Full);
599                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
600                         ethtool_link_ksettings_add_link_mode(ks, advertising,
601                                                              10000baseT_Full);
602                 break;
603         case I40E_PHY_TYPE_SGMII:
604                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
605                 ethtool_link_ksettings_add_link_mode(ks, supported,
606                                                      1000baseT_Full);
607                 if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
608                         ethtool_link_ksettings_add_link_mode(ks, advertising,
609                                                              1000baseT_Full);
610                 if (pf->hw_features & I40E_HW_100M_SGMII_CAPABLE) {
611                         ethtool_link_ksettings_add_link_mode(ks, supported,
612                                                              100baseT_Full);
613                         if (hw_link_info->requested_speeds &
614                             I40E_LINK_SPEED_100MB)
615                                 ethtool_link_ksettings_add_link_mode(
616                                       ks, advertising, 100baseT_Full);
617                 }
618                 break;
619         case I40E_PHY_TYPE_40GBASE_KR4:
620         case I40E_PHY_TYPE_25GBASE_KR:
621         case I40E_PHY_TYPE_20GBASE_KR2:
622         case I40E_PHY_TYPE_10GBASE_KR:
623         case I40E_PHY_TYPE_10GBASE_KX4:
624         case I40E_PHY_TYPE_1000BASE_KX:
625                 ethtool_link_ksettings_add_link_mode(ks, supported,
626                                                      40000baseKR4_Full);
627                 ethtool_link_ksettings_add_link_mode(ks, supported,
628                                                      25000baseKR_Full);
629                 ethtool_link_ksettings_add_link_mode(ks, supported,
630                                                      20000baseKR2_Full);
631                 ethtool_link_ksettings_add_link_mode(ks, supported,
632                                                      10000baseKR_Full);
633                 ethtool_link_ksettings_add_link_mode(ks, supported,
634                                                      10000baseKX4_Full);
635                 ethtool_link_ksettings_add_link_mode(ks, supported,
636                                                      1000baseKX_Full);
637                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
638                 ethtool_link_ksettings_add_link_mode(ks, advertising,
639                                                      40000baseKR4_Full);
640                 ethtool_link_ksettings_add_link_mode(ks, advertising,
641                                                      25000baseKR_Full);
642                 ethtool_link_ksettings_add_link_mode(ks, advertising,
643                                                      20000baseKR2_Full);
644                 ethtool_link_ksettings_add_link_mode(ks, advertising,
645                                                      10000baseKR_Full);
646                 ethtool_link_ksettings_add_link_mode(ks, advertising,
647                                                      10000baseKX4_Full);
648                 ethtool_link_ksettings_add_link_mode(ks, advertising,
649                                                      1000baseKX_Full);
650                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
651                 break;
652         case I40E_PHY_TYPE_25GBASE_CR:
653                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
654                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
655                 ethtool_link_ksettings_add_link_mode(ks, supported,
656                                                      25000baseCR_Full);
657                 ethtool_link_ksettings_add_link_mode(ks, advertising,
658                                                      25000baseCR_Full);
659                 break;
660         case I40E_PHY_TYPE_25GBASE_AOC:
661         case I40E_PHY_TYPE_25GBASE_ACC:
662                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
663                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
664                 ethtool_link_ksettings_add_link_mode(ks, supported,
665                                                      25000baseCR_Full);
666
667                 ethtool_link_ksettings_add_link_mode(ks, advertising,
668                                                      25000baseCR_Full);
669                 ethtool_link_ksettings_add_link_mode(ks, supported,
670                                                      10000baseCR_Full);
671                 ethtool_link_ksettings_add_link_mode(ks, advertising,
672                                                      10000baseCR_Full);
673                 break;
674         default:
675                 /* if we got here and link is up something bad is afoot */
676                 netdev_info(netdev,
677                             "WARNING: Link is up but PHY type 0x%x is not recognized, or incorrect cable is in use\n",
678                             hw_link_info->phy_type);
679         }
680
681         /* Now that we've worked out everything that could be supported by the
682          * current PHY type, get what is supported by the NVM and intersect
683          * them to get what is truly supported
684          */
685         memset(&cap_ksettings, 0, sizeof(struct ethtool_link_ksettings));
686         i40e_phy_type_to_ethtool(pf, &cap_ksettings);
687         ethtool_intersect_link_masks(ks, &cap_ksettings);
688
689         /* Set speed and duplex */
690         switch (link_speed) {
691         case I40E_LINK_SPEED_40GB:
692                 ks->base.speed = SPEED_40000;
693                 break;
694         case I40E_LINK_SPEED_25GB:
695                 ks->base.speed = SPEED_25000;
696                 break;
697         case I40E_LINK_SPEED_20GB:
698                 ks->base.speed = SPEED_20000;
699                 break;
700         case I40E_LINK_SPEED_10GB:
701                 ks->base.speed = SPEED_10000;
702                 break;
703         case I40E_LINK_SPEED_1GB:
704                 ks->base.speed = SPEED_1000;
705                 break;
706         case I40E_LINK_SPEED_100MB:
707                 ks->base.speed = SPEED_100;
708                 break;
709         default:
710                 break;
711         }
712         ks->base.duplex = DUPLEX_FULL;
713 }
714
715 /**
716  * i40e_get_settings_link_down - Get the Link settings for when link is down
717  * @hw: hw structure
718  * @ks: ethtool ksettings to fill in
719  * @pf: pointer to physical function struct
720  *
721  * Reports link settings that can be determined when link is down
722  **/
723 static void i40e_get_settings_link_down(struct i40e_hw *hw,
724                                         struct ethtool_link_ksettings *ks,
725                                         struct i40e_pf *pf)
726 {
727         /* link is down and the driver needs to fall back on
728          * supported phy types to figure out what info to display
729          */
730         i40e_phy_type_to_ethtool(pf, ks);
731
732         /* With no link speed and duplex are unknown */
733         ks->base.speed = SPEED_UNKNOWN;
734         ks->base.duplex = DUPLEX_UNKNOWN;
735 }
736
737 /**
738  * i40e_get_link_ksettings - Get Link Speed and Duplex settings
739  * @netdev: network interface device structure
740  * @ks: ethtool ksettings
741  *
742  * Reports speed/duplex settings based on media_type
743  **/
744 static int i40e_get_link_ksettings(struct net_device *netdev,
745                                    struct ethtool_link_ksettings *ks)
746 {
747         struct i40e_netdev_priv *np = netdev_priv(netdev);
748         struct i40e_pf *pf = np->vsi->back;
749         struct i40e_hw *hw = &pf->hw;
750         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
751         bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
752
753         ethtool_link_ksettings_zero_link_mode(ks, supported);
754         ethtool_link_ksettings_zero_link_mode(ks, advertising);
755
756         if (link_up)
757                 i40e_get_settings_link_up(hw, ks, netdev, pf);
758         else
759                 i40e_get_settings_link_down(hw, ks, pf);
760
761         /* Now set the settings that don't rely on link being up/down */
762         /* Set autoneg settings */
763         ks->base.autoneg = ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
764                             AUTONEG_ENABLE : AUTONEG_DISABLE);
765
766         /* Set media type settings */
767         switch (hw->phy.media_type) {
768         case I40E_MEDIA_TYPE_BACKPLANE:
769                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
770                 ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
771                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
772                 ethtool_link_ksettings_add_link_mode(ks, advertising,
773                                                      Backplane);
774                 ks->base.port = PORT_NONE;
775                 break;
776         case I40E_MEDIA_TYPE_BASET:
777                 ethtool_link_ksettings_add_link_mode(ks, supported, TP);
778                 ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
779                 ks->base.port = PORT_TP;
780                 break;
781         case I40E_MEDIA_TYPE_DA:
782         case I40E_MEDIA_TYPE_CX4:
783                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
784                 ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
785                 ks->base.port = PORT_DA;
786                 break;
787         case I40E_MEDIA_TYPE_FIBER:
788                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
789                 ks->base.port = PORT_FIBRE;
790                 break;
791         case I40E_MEDIA_TYPE_UNKNOWN:
792         default:
793                 ks->base.port = PORT_OTHER;
794                 break;
795         }
796
797         /* Set flow control settings */
798         ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
799         ethtool_link_ksettings_add_link_mode(ks, supported, Asym_Pause);
800
801         switch (hw->fc.requested_mode) {
802         case I40E_FC_FULL:
803                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
804                 break;
805         case I40E_FC_TX_PAUSE:
806                 ethtool_link_ksettings_add_link_mode(ks, advertising,
807                                                      Asym_Pause);
808                 break;
809         case I40E_FC_RX_PAUSE:
810                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
811                 ethtool_link_ksettings_add_link_mode(ks, advertising,
812                                                      Asym_Pause);
813                 break;
814         default:
815                 ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
816                 ethtool_link_ksettings_del_link_mode(ks, advertising,
817                                                      Asym_Pause);
818                 break;
819         }
820
821         return 0;
822 }
823
824 /**
825  * i40e_set_link_ksettings - Set Speed and Duplex
826  * @netdev: network interface device structure
827  * @ks: ethtool ksettings
828  *
829  * Set speed/duplex per media_types advertised/forced
830  **/
831 static int i40e_set_link_ksettings(struct net_device *netdev,
832                                    const struct ethtool_link_ksettings *ks)
833 {
834         struct i40e_netdev_priv *np = netdev_priv(netdev);
835         struct i40e_aq_get_phy_abilities_resp abilities;
836         struct ethtool_link_ksettings safe_ks;
837         struct ethtool_link_ksettings copy_ks;
838         struct i40e_aq_set_phy_config config;
839         struct i40e_pf *pf = np->vsi->back;
840         struct i40e_vsi *vsi = np->vsi;
841         struct i40e_hw *hw = &pf->hw;
842         bool autoneg_changed = false;
843         i40e_status status = 0;
844         int timeout = 50;
845         int err = 0;
846         u8 autoneg;
847
848         /* Changing port settings is not supported if this isn't the
849          * port's controlling PF
850          */
851         if (hw->partition_id != 1) {
852                 i40e_partition_setting_complaint(pf);
853                 return -EOPNOTSUPP;
854         }
855         if (vsi != pf->vsi[pf->lan_vsi])
856                 return -EOPNOTSUPP;
857         if (hw->phy.media_type != I40E_MEDIA_TYPE_BASET &&
858             hw->phy.media_type != I40E_MEDIA_TYPE_FIBER &&
859             hw->phy.media_type != I40E_MEDIA_TYPE_BACKPLANE &&
860             hw->phy.media_type != I40E_MEDIA_TYPE_DA &&
861             hw->phy.link_info.link_info & I40E_AQ_LINK_UP)
862                 return -EOPNOTSUPP;
863         if (hw->device_id == I40E_DEV_ID_KX_B ||
864             hw->device_id == I40E_DEV_ID_KX_C ||
865             hw->device_id == I40E_DEV_ID_20G_KR2 ||
866             hw->device_id == I40E_DEV_ID_20G_KR2_A ||
867             hw->device_id == I40E_DEV_ID_25G_B ||
868             hw->device_id == I40E_DEV_ID_KX_X722) {
869                 netdev_info(netdev, "Changing settings is not supported on backplane.\n");
870                 return -EOPNOTSUPP;
871         }
872
873         /* copy the ksettings to copy_ks to avoid modifying the origin */
874         memcpy(&copy_ks, ks, sizeof(struct ethtool_link_ksettings));
875
876         /* save autoneg out of ksettings */
877         autoneg = copy_ks.base.autoneg;
878
879         /* get our own copy of the bits to check against */
880         memset(&safe_ks, 0, sizeof(struct ethtool_link_ksettings));
881         safe_ks.base.cmd = copy_ks.base.cmd;
882         safe_ks.base.link_mode_masks_nwords =
883                 copy_ks.base.link_mode_masks_nwords;
884         i40e_get_link_ksettings(netdev, &safe_ks);
885
886         /* Get link modes supported by hardware and check against modes
887          * requested by the user.  Return an error if unsupported mode was set.
888          */
889         if (!bitmap_subset(copy_ks.link_modes.advertising,
890                            safe_ks.link_modes.supported,
891                            __ETHTOOL_LINK_MODE_MASK_NBITS))
892                 return -EINVAL;
893
894         /* set autoneg back to what it currently is */
895         copy_ks.base.autoneg = safe_ks.base.autoneg;
896
897         /* If copy_ks.base and safe_ks.base are not the same now, then they are
898          * trying to set something that we do not support.
899          */
900         if (memcmp(&copy_ks.base, &safe_ks.base,
901                    sizeof(struct ethtool_link_settings)))
902                 return -EOPNOTSUPP;
903
904         while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
905                 timeout--;
906                 if (!timeout)
907                         return -EBUSY;
908                 usleep_range(1000, 2000);
909         }
910
911         /* Get the current phy config */
912         status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
913                                               NULL);
914         if (status) {
915                 err = -EAGAIN;
916                 goto done;
917         }
918
919         /* Copy abilities to config in case autoneg is not
920          * set below
921          */
922         memset(&config, 0, sizeof(struct i40e_aq_set_phy_config));
923         config.abilities = abilities.abilities;
924
925         /* Check autoneg */
926         if (autoneg == AUTONEG_ENABLE) {
927                 /* If autoneg was not already enabled */
928                 if (!(hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED)) {
929                         /* If autoneg is not supported, return error */
930                         if (!ethtool_link_ksettings_test_link_mode(&safe_ks,
931                                                                    supported,
932                                                                    Autoneg)) {
933                                 netdev_info(netdev, "Autoneg not supported on this phy\n");
934                                 err = -EINVAL;
935                                 goto done;
936                         }
937                         /* Autoneg is allowed to change */
938                         config.abilities = abilities.abilities |
939                                            I40E_AQ_PHY_ENABLE_AN;
940                         autoneg_changed = true;
941                 }
942         } else {
943                 /* If autoneg is currently enabled */
944                 if (hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED) {
945                         /* If autoneg is supported 10GBASE_T is the only PHY
946                          * that can disable it, so otherwise return error
947                          */
948                         if (ethtool_link_ksettings_test_link_mode(&safe_ks,
949                                                                   supported,
950                                                                   Autoneg) &&
951                             hw->phy.media_type != I40E_MEDIA_TYPE_BASET) {
952                                 netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
953                                 err = -EINVAL;
954                                 goto done;
955                         }
956                         /* Autoneg is allowed to change */
957                         config.abilities = abilities.abilities &
958                                            ~I40E_AQ_PHY_ENABLE_AN;
959                         autoneg_changed = true;
960                 }
961         }
962
963         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
964                                                   100baseT_Full))
965                 config.link_speed |= I40E_LINK_SPEED_100MB;
966         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
967                                                   1000baseT_Full) ||
968             ethtool_link_ksettings_test_link_mode(ks, advertising,
969                                                   1000baseX_Full) ||
970             ethtool_link_ksettings_test_link_mode(ks, advertising,
971                                                   1000baseKX_Full))
972                 config.link_speed |= I40E_LINK_SPEED_1GB;
973         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
974                                                   10000baseT_Full) ||
975             ethtool_link_ksettings_test_link_mode(ks, advertising,
976                                                   10000baseKX4_Full) ||
977             ethtool_link_ksettings_test_link_mode(ks, advertising,
978                                                   10000baseKR_Full) ||
979             ethtool_link_ksettings_test_link_mode(ks, advertising,
980                                                   10000baseCR_Full) ||
981             ethtool_link_ksettings_test_link_mode(ks, advertising,
982                                                   10000baseSR_Full) ||
983             ethtool_link_ksettings_test_link_mode(ks, advertising,
984                                                   10000baseLR_Full))
985                 config.link_speed |= I40E_LINK_SPEED_10GB;
986         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
987                                                   20000baseKR2_Full))
988                 config.link_speed |= I40E_LINK_SPEED_20GB;
989         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
990                                                   25000baseCR_Full) ||
991             ethtool_link_ksettings_test_link_mode(ks, advertising,
992                                                   25000baseKR_Full) ||
993             ethtool_link_ksettings_test_link_mode(ks, advertising,
994                                                   25000baseSR_Full))
995                 config.link_speed |= I40E_LINK_SPEED_25GB;
996         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
997                                                   40000baseKR4_Full) ||
998             ethtool_link_ksettings_test_link_mode(ks, advertising,
999                                                   40000baseCR4_Full) ||
1000             ethtool_link_ksettings_test_link_mode(ks, advertising,
1001                                                   40000baseSR4_Full) ||
1002             ethtool_link_ksettings_test_link_mode(ks, advertising,
1003                                                   40000baseLR4_Full))
1004                 config.link_speed |= I40E_LINK_SPEED_40GB;
1005
1006         /* If speed didn't get set, set it to what it currently is.
1007          * This is needed because if advertise is 0 (as it is when autoneg
1008          * is disabled) then speed won't get set.
1009          */
1010         if (!config.link_speed)
1011                 config.link_speed = abilities.link_speed;
1012         if (autoneg_changed || abilities.link_speed != config.link_speed) {
1013                 /* copy over the rest of the abilities */
1014                 config.phy_type = abilities.phy_type;
1015                 config.phy_type_ext = abilities.phy_type_ext;
1016                 config.eee_capability = abilities.eee_capability;
1017                 config.eeer = abilities.eeer_val;
1018                 config.low_power_ctrl = abilities.d3_lpan;
1019                 config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
1020                                     I40E_AQ_PHY_FEC_CONFIG_MASK;
1021
1022                 /* save the requested speeds */
1023                 hw->phy.link_info.requested_speeds = config.link_speed;
1024                 /* set link and auto negotiation so changes take effect */
1025                 config.abilities |= I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
1026                 /* If link is up put link down */
1027                 if (hw->phy.link_info.link_info & I40E_AQ_LINK_UP) {
1028                         /* Tell the OS link is going down, the link will go
1029                          * back up when fw says it is ready asynchronously
1030                          */
1031                         i40e_print_link_message(vsi, false);
1032                         netif_carrier_off(netdev);
1033                         netif_tx_stop_all_queues(netdev);
1034                 }
1035
1036                 /* make the aq call */
1037                 status = i40e_aq_set_phy_config(hw, &config, NULL);
1038                 if (status) {
1039                         netdev_info(netdev,
1040                                     "Set phy config failed, err %s aq_err %s\n",
1041                                     i40e_stat_str(hw, status),
1042                                     i40e_aq_str(hw, hw->aq.asq_last_status));
1043                         err = -EAGAIN;
1044                         goto done;
1045                 }
1046
1047                 status = i40e_update_link_info(hw);
1048                 if (status)
1049                         netdev_dbg(netdev,
1050                                    "Updating link info failed with err %s aq_err %s\n",
1051                                    i40e_stat_str(hw, status),
1052                                    i40e_aq_str(hw, hw->aq.asq_last_status));
1053
1054         } else {
1055                 netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
1056         }
1057
1058 done:
1059         clear_bit(__I40E_CONFIG_BUSY, pf->state);
1060
1061         return err;
1062 }
1063
1064 static int i40e_nway_reset(struct net_device *netdev)
1065 {
1066         /* restart autonegotiation */
1067         struct i40e_netdev_priv *np = netdev_priv(netdev);
1068         struct i40e_pf *pf = np->vsi->back;
1069         struct i40e_hw *hw = &pf->hw;
1070         bool link_up = hw->phy.link_info.link_info & I40E_AQ_LINK_UP;
1071         i40e_status ret = 0;
1072
1073         ret = i40e_aq_set_link_restart_an(hw, link_up, NULL);
1074         if (ret) {
1075                 netdev_info(netdev, "link restart failed, err %s aq_err %s\n",
1076                             i40e_stat_str(hw, ret),
1077                             i40e_aq_str(hw, hw->aq.asq_last_status));
1078                 return -EIO;
1079         }
1080
1081         return 0;
1082 }
1083
1084 /**
1085  * i40e_get_pauseparam -  Get Flow Control status
1086  * @netdev: netdevice structure
1087  * @pause: buffer to return pause parameters
1088  *
1089  * Return tx/rx-pause status
1090  **/
1091 static void i40e_get_pauseparam(struct net_device *netdev,
1092                                 struct ethtool_pauseparam *pause)
1093 {
1094         struct i40e_netdev_priv *np = netdev_priv(netdev);
1095         struct i40e_pf *pf = np->vsi->back;
1096         struct i40e_hw *hw = &pf->hw;
1097         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
1098         struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
1099
1100         pause->autoneg =
1101                 ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
1102                   AUTONEG_ENABLE : AUTONEG_DISABLE);
1103
1104         /* PFC enabled so report LFC as off */
1105         if (dcbx_cfg->pfc.pfcenable) {
1106                 pause->rx_pause = 0;
1107                 pause->tx_pause = 0;
1108                 return;
1109         }
1110
1111         if (hw->fc.current_mode == I40E_FC_RX_PAUSE) {
1112                 pause->rx_pause = 1;
1113         } else if (hw->fc.current_mode == I40E_FC_TX_PAUSE) {
1114                 pause->tx_pause = 1;
1115         } else if (hw->fc.current_mode == I40E_FC_FULL) {
1116                 pause->rx_pause = 1;
1117                 pause->tx_pause = 1;
1118         }
1119 }
1120
1121 /**
1122  * i40e_set_pauseparam - Set Flow Control parameter
1123  * @netdev: network interface device structure
1124  * @pause: return tx/rx flow control status
1125  **/
1126 static int i40e_set_pauseparam(struct net_device *netdev,
1127                                struct ethtool_pauseparam *pause)
1128 {
1129         struct i40e_netdev_priv *np = netdev_priv(netdev);
1130         struct i40e_pf *pf = np->vsi->back;
1131         struct i40e_vsi *vsi = np->vsi;
1132         struct i40e_hw *hw = &pf->hw;
1133         struct i40e_link_status *hw_link_info = &hw->phy.link_info;
1134         struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
1135         bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
1136         i40e_status status;
1137         u8 aq_failures;
1138         int err = 0;
1139         u32 is_an;
1140
1141         /* Changing the port's flow control is not supported if this isn't the
1142          * port's controlling PF
1143          */
1144         if (hw->partition_id != 1) {
1145                 i40e_partition_setting_complaint(pf);
1146                 return -EOPNOTSUPP;
1147         }
1148
1149         if (vsi != pf->vsi[pf->lan_vsi])
1150                 return -EOPNOTSUPP;
1151
1152         is_an = hw_link_info->an_info & I40E_AQ_AN_COMPLETED;
1153         if (pause->autoneg != is_an) {
1154                 netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
1155                 return -EOPNOTSUPP;
1156         }
1157
1158         /* If we have link and don't have autoneg */
1159         if (!test_bit(__I40E_DOWN, pf->state) && !is_an) {
1160                 /* Send message that it might not necessarily work*/
1161                 netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
1162         }
1163
1164         if (dcbx_cfg->pfc.pfcenable) {
1165                 netdev_info(netdev,
1166                             "Priority flow control enabled. Cannot set link flow control.\n");
1167                 return -EOPNOTSUPP;
1168         }
1169
1170         if (pause->rx_pause && pause->tx_pause)
1171                 hw->fc.requested_mode = I40E_FC_FULL;
1172         else if (pause->rx_pause && !pause->tx_pause)
1173                 hw->fc.requested_mode = I40E_FC_RX_PAUSE;
1174         else if (!pause->rx_pause && pause->tx_pause)
1175                 hw->fc.requested_mode = I40E_FC_TX_PAUSE;
1176         else if (!pause->rx_pause && !pause->tx_pause)
1177                 hw->fc.requested_mode = I40E_FC_NONE;
1178         else
1179                  return -EINVAL;
1180
1181         /* Tell the OS link is going down, the link will go back up when fw
1182          * says it is ready asynchronously
1183          */
1184         i40e_print_link_message(vsi, false);
1185         netif_carrier_off(netdev);
1186         netif_tx_stop_all_queues(netdev);
1187
1188         /* Set the fc mode and only restart an if link is up*/
1189         status = i40e_set_fc(hw, &aq_failures, link_up);
1190
1191         if (aq_failures & I40E_SET_FC_AQ_FAIL_GET) {
1192                 netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %s aq_err %s\n",
1193                             i40e_stat_str(hw, status),
1194                             i40e_aq_str(hw, hw->aq.asq_last_status));
1195                 err = -EAGAIN;
1196         }
1197         if (aq_failures & I40E_SET_FC_AQ_FAIL_SET) {
1198                 netdev_info(netdev, "Set fc failed on the set_phy_config call with err %s aq_err %s\n",
1199                             i40e_stat_str(hw, status),
1200                             i40e_aq_str(hw, hw->aq.asq_last_status));
1201                 err = -EAGAIN;
1202         }
1203         if (aq_failures & I40E_SET_FC_AQ_FAIL_UPDATE) {
1204                 netdev_info(netdev, "Set fc failed on the get_link_info call with err %s aq_err %s\n",
1205                             i40e_stat_str(hw, status),
1206                             i40e_aq_str(hw, hw->aq.asq_last_status));
1207                 err = -EAGAIN;
1208         }
1209
1210         if (!test_bit(__I40E_DOWN, pf->state) && is_an) {
1211                 /* Give it a little more time to try to come back */
1212                 msleep(75);
1213                 if (!test_bit(__I40E_DOWN, pf->state))
1214                         return i40e_nway_reset(netdev);
1215         }
1216
1217         return err;
1218 }
1219
1220 static u32 i40e_get_msglevel(struct net_device *netdev)
1221 {
1222         struct i40e_netdev_priv *np = netdev_priv(netdev);
1223         struct i40e_pf *pf = np->vsi->back;
1224         u32 debug_mask = pf->hw.debug_mask;
1225
1226         if (debug_mask)
1227                 netdev_info(netdev, "i40e debug_mask: 0x%08X\n", debug_mask);
1228
1229         return pf->msg_enable;
1230 }
1231
1232 static void i40e_set_msglevel(struct net_device *netdev, u32 data)
1233 {
1234         struct i40e_netdev_priv *np = netdev_priv(netdev);
1235         struct i40e_pf *pf = np->vsi->back;
1236
1237         if (I40E_DEBUG_USER & data)
1238                 pf->hw.debug_mask = data;
1239         else
1240                 pf->msg_enable = data;
1241 }
1242
1243 static int i40e_get_regs_len(struct net_device *netdev)
1244 {
1245         int reg_count = 0;
1246         int i;
1247
1248         for (i = 0; i40e_reg_list[i].offset != 0; i++)
1249                 reg_count += i40e_reg_list[i].elements;
1250
1251         return reg_count * sizeof(u32);
1252 }
1253
1254 static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
1255                           void *p)
1256 {
1257         struct i40e_netdev_priv *np = netdev_priv(netdev);
1258         struct i40e_pf *pf = np->vsi->back;
1259         struct i40e_hw *hw = &pf->hw;
1260         u32 *reg_buf = p;
1261         unsigned int i, j, ri;
1262         u32 reg;
1263
1264         /* Tell ethtool which driver-version-specific regs output we have.
1265          *
1266          * At some point, if we have ethtool doing special formatting of
1267          * this data, it will rely on this version number to know how to
1268          * interpret things.  Hence, this needs to be updated if/when the
1269          * diags register table is changed.
1270          */
1271         regs->version = 1;
1272
1273         /* loop through the diags reg table for what to print */
1274         ri = 0;
1275         for (i = 0; i40e_reg_list[i].offset != 0; i++) {
1276                 for (j = 0; j < i40e_reg_list[i].elements; j++) {
1277                         reg = i40e_reg_list[i].offset
1278                                 + (j * i40e_reg_list[i].stride);
1279                         reg_buf[ri++] = rd32(hw, reg);
1280                 }
1281         }
1282
1283 }
1284
1285 static int i40e_get_eeprom(struct net_device *netdev,
1286                            struct ethtool_eeprom *eeprom, u8 *bytes)
1287 {
1288         struct i40e_netdev_priv *np = netdev_priv(netdev);
1289         struct i40e_hw *hw = &np->vsi->back->hw;
1290         struct i40e_pf *pf = np->vsi->back;
1291         int ret_val = 0, len, offset;
1292         u8 *eeprom_buff;
1293         u16 i, sectors;
1294         bool last;
1295         u32 magic;
1296
1297 #define I40E_NVM_SECTOR_SIZE  4096
1298         if (eeprom->len == 0)
1299                 return -EINVAL;
1300
1301         /* check for NVMUpdate access method */
1302         magic = hw->vendor_id | (hw->device_id << 16);
1303         if (eeprom->magic && eeprom->magic != magic) {
1304                 struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1305                 int errno = 0;
1306
1307                 /* make sure it is the right magic for NVMUpdate */
1308                 if ((eeprom->magic >> 16) != hw->device_id)
1309                         errno = -EINVAL;
1310                 else if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
1311                          test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
1312                         errno = -EBUSY;
1313                 else
1314                         ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1315
1316                 if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1317                         dev_info(&pf->pdev->dev,
1318                                  "NVMUpdate read failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1319                                  ret_val, hw->aq.asq_last_status, errno,
1320                                  (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1321                                  cmd->offset, cmd->data_size);
1322
1323                 return errno;
1324         }
1325
1326         /* normal ethtool get_eeprom support */
1327         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
1328
1329         eeprom_buff = kzalloc(eeprom->len, GFP_KERNEL);
1330         if (!eeprom_buff)
1331                 return -ENOMEM;
1332
1333         ret_val = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
1334         if (ret_val) {
1335                 dev_info(&pf->pdev->dev,
1336                          "Failed Acquiring NVM resource for read err=%d status=0x%x\n",
1337                          ret_val, hw->aq.asq_last_status);
1338                 goto free_buff;
1339         }
1340
1341         sectors = eeprom->len / I40E_NVM_SECTOR_SIZE;
1342         sectors += (eeprom->len % I40E_NVM_SECTOR_SIZE) ? 1 : 0;
1343         len = I40E_NVM_SECTOR_SIZE;
1344         last = false;
1345         for (i = 0; i < sectors; i++) {
1346                 if (i == (sectors - 1)) {
1347                         len = eeprom->len - (I40E_NVM_SECTOR_SIZE * i);
1348                         last = true;
1349                 }
1350                 offset = eeprom->offset + (I40E_NVM_SECTOR_SIZE * i),
1351                 ret_val = i40e_aq_read_nvm(hw, 0x0, offset, len,
1352                                 (u8 *)eeprom_buff + (I40E_NVM_SECTOR_SIZE * i),
1353                                 last, NULL);
1354                 if (ret_val && hw->aq.asq_last_status == I40E_AQ_RC_EPERM) {
1355                         dev_info(&pf->pdev->dev,
1356                                  "read NVM failed, invalid offset 0x%x\n",
1357                                  offset);
1358                         break;
1359                 } else if (ret_val &&
1360                            hw->aq.asq_last_status == I40E_AQ_RC_EACCES) {
1361                         dev_info(&pf->pdev->dev,
1362                                  "read NVM failed, access, offset 0x%x\n",
1363                                  offset);
1364                         break;
1365                 } else if (ret_val) {
1366                         dev_info(&pf->pdev->dev,
1367                                  "read NVM failed offset %d err=%d status=0x%x\n",
1368                                  offset, ret_val, hw->aq.asq_last_status);
1369                         break;
1370                 }
1371         }
1372
1373         i40e_release_nvm(hw);
1374         memcpy(bytes, (u8 *)eeprom_buff, eeprom->len);
1375 free_buff:
1376         kfree(eeprom_buff);
1377         return ret_val;
1378 }
1379
1380 static int i40e_get_eeprom_len(struct net_device *netdev)
1381 {
1382         struct i40e_netdev_priv *np = netdev_priv(netdev);
1383         struct i40e_hw *hw = &np->vsi->back->hw;
1384         u32 val;
1385
1386 #define X722_EEPROM_SCOPE_LIMIT 0x5B9FFF
1387         if (hw->mac.type == I40E_MAC_X722) {
1388                 val = X722_EEPROM_SCOPE_LIMIT + 1;
1389                 return val;
1390         }
1391         val = (rd32(hw, I40E_GLPCI_LBARCTRL)
1392                 & I40E_GLPCI_LBARCTRL_FL_SIZE_MASK)
1393                 >> I40E_GLPCI_LBARCTRL_FL_SIZE_SHIFT;
1394         /* register returns value in power of 2, 64Kbyte chunks. */
1395         val = (64 * 1024) * BIT(val);
1396         return val;
1397 }
1398
1399 static int i40e_set_eeprom(struct net_device *netdev,
1400                            struct ethtool_eeprom *eeprom, u8 *bytes)
1401 {
1402         struct i40e_netdev_priv *np = netdev_priv(netdev);
1403         struct i40e_hw *hw = &np->vsi->back->hw;
1404         struct i40e_pf *pf = np->vsi->back;
1405         struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
1406         int ret_val = 0;
1407         int errno = 0;
1408         u32 magic;
1409
1410         /* normal ethtool set_eeprom is not supported */
1411         magic = hw->vendor_id | (hw->device_id << 16);
1412         if (eeprom->magic == magic)
1413                 errno = -EOPNOTSUPP;
1414         /* check for NVMUpdate access method */
1415         else if (!eeprom->magic || (eeprom->magic >> 16) != hw->device_id)
1416                 errno = -EINVAL;
1417         else if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
1418                  test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
1419                 errno = -EBUSY;
1420         else
1421                 ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
1422
1423         if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
1424                 dev_info(&pf->pdev->dev,
1425                          "NVMUpdate write failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
1426                          ret_val, hw->aq.asq_last_status, errno,
1427                          (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
1428                          cmd->offset, cmd->data_size);
1429
1430         return errno;
1431 }
1432
1433 static void i40e_get_drvinfo(struct net_device *netdev,
1434                              struct ethtool_drvinfo *drvinfo)
1435 {
1436         struct i40e_netdev_priv *np = netdev_priv(netdev);
1437         struct i40e_vsi *vsi = np->vsi;
1438         struct i40e_pf *pf = vsi->back;
1439
1440         strlcpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
1441         strlcpy(drvinfo->version, i40e_driver_version_str,
1442                 sizeof(drvinfo->version));
1443         strlcpy(drvinfo->fw_version, i40e_nvm_version_str(&pf->hw),
1444                 sizeof(drvinfo->fw_version));
1445         strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
1446                 sizeof(drvinfo->bus_info));
1447         drvinfo->n_priv_flags = I40E_PRIV_FLAGS_STR_LEN;
1448         if (pf->hw.pf_id == 0)
1449                 drvinfo->n_priv_flags += I40E_GL_PRIV_FLAGS_STR_LEN;
1450 }
1451
1452 static void i40e_get_ringparam(struct net_device *netdev,
1453                                struct ethtool_ringparam *ring)
1454 {
1455         struct i40e_netdev_priv *np = netdev_priv(netdev);
1456         struct i40e_pf *pf = np->vsi->back;
1457         struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
1458
1459         ring->rx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
1460         ring->tx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
1461         ring->rx_mini_max_pending = 0;
1462         ring->rx_jumbo_max_pending = 0;
1463         ring->rx_pending = vsi->rx_rings[0]->count;
1464         ring->tx_pending = vsi->tx_rings[0]->count;
1465         ring->rx_mini_pending = 0;
1466         ring->rx_jumbo_pending = 0;
1467 }
1468
1469 static bool i40e_active_tx_ring_index(struct i40e_vsi *vsi, u16 index)
1470 {
1471         if (i40e_enabled_xdp_vsi(vsi)) {
1472                 return index < vsi->num_queue_pairs ||
1473                         (index >= vsi->alloc_queue_pairs &&
1474                          index < vsi->alloc_queue_pairs + vsi->num_queue_pairs);
1475         }
1476
1477         return index < vsi->num_queue_pairs;
1478 }
1479
1480 static int i40e_set_ringparam(struct net_device *netdev,
1481                               struct ethtool_ringparam *ring)
1482 {
1483         struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
1484         struct i40e_netdev_priv *np = netdev_priv(netdev);
1485         struct i40e_hw *hw = &np->vsi->back->hw;
1486         struct i40e_vsi *vsi = np->vsi;
1487         struct i40e_pf *pf = vsi->back;
1488         u32 new_rx_count, new_tx_count;
1489         u16 tx_alloc_queue_pairs;
1490         int timeout = 50;
1491         int i, err = 0;
1492
1493         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
1494                 return -EINVAL;
1495
1496         if (ring->tx_pending > I40E_MAX_NUM_DESCRIPTORS ||
1497             ring->tx_pending < I40E_MIN_NUM_DESCRIPTORS ||
1498             ring->rx_pending > I40E_MAX_NUM_DESCRIPTORS ||
1499             ring->rx_pending < I40E_MIN_NUM_DESCRIPTORS) {
1500                 netdev_info(netdev,
1501                             "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d]\n",
1502                             ring->tx_pending, ring->rx_pending,
1503                             I40E_MIN_NUM_DESCRIPTORS, I40E_MAX_NUM_DESCRIPTORS);
1504                 return -EINVAL;
1505         }
1506
1507         new_tx_count = ALIGN(ring->tx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
1508         new_rx_count = ALIGN(ring->rx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
1509
1510         /* if nothing to do return success */
1511         if ((new_tx_count == vsi->tx_rings[0]->count) &&
1512             (new_rx_count == vsi->rx_rings[0]->count))
1513                 return 0;
1514
1515         while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
1516                 timeout--;
1517                 if (!timeout)
1518                         return -EBUSY;
1519                 usleep_range(1000, 2000);
1520         }
1521
1522         if (!netif_running(vsi->netdev)) {
1523                 /* simple case - set for the next time the netdev is started */
1524                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1525                         vsi->tx_rings[i]->count = new_tx_count;
1526                         vsi->rx_rings[i]->count = new_rx_count;
1527                         if (i40e_enabled_xdp_vsi(vsi))
1528                                 vsi->xdp_rings[i]->count = new_tx_count;
1529                 }
1530                 goto done;
1531         }
1532
1533         /* We can't just free everything and then setup again,
1534          * because the ISRs in MSI-X mode get passed pointers
1535          * to the Tx and Rx ring structs.
1536          */
1537
1538         /* alloc updated Tx and XDP Tx resources */
1539         tx_alloc_queue_pairs = vsi->alloc_queue_pairs *
1540                                (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
1541         if (new_tx_count != vsi->tx_rings[0]->count) {
1542                 netdev_info(netdev,
1543                             "Changing Tx descriptor count from %d to %d.\n",
1544                             vsi->tx_rings[0]->count, new_tx_count);
1545                 tx_rings = kcalloc(tx_alloc_queue_pairs,
1546                                    sizeof(struct i40e_ring), GFP_KERNEL);
1547                 if (!tx_rings) {
1548                         err = -ENOMEM;
1549                         goto done;
1550                 }
1551
1552                 for (i = 0; i < tx_alloc_queue_pairs; i++) {
1553                         if (!i40e_active_tx_ring_index(vsi, i))
1554                                 continue;
1555
1556                         tx_rings[i] = *vsi->tx_rings[i];
1557                         tx_rings[i].count = new_tx_count;
1558                         /* the desc and bi pointers will be reallocated in the
1559                          * setup call
1560                          */
1561                         tx_rings[i].desc = NULL;
1562                         tx_rings[i].rx_bi = NULL;
1563                         err = i40e_setup_tx_descriptors(&tx_rings[i]);
1564                         if (err) {
1565                                 while (i) {
1566                                         i--;
1567                                         if (!i40e_active_tx_ring_index(vsi, i))
1568                                                 continue;
1569                                         i40e_free_tx_resources(&tx_rings[i]);
1570                                 }
1571                                 kfree(tx_rings);
1572                                 tx_rings = NULL;
1573
1574                                 goto done;
1575                         }
1576                 }
1577         }
1578
1579         /* alloc updated Rx resources */
1580         if (new_rx_count != vsi->rx_rings[0]->count) {
1581                 netdev_info(netdev,
1582                             "Changing Rx descriptor count from %d to %d\n",
1583                             vsi->rx_rings[0]->count, new_rx_count);
1584                 rx_rings = kcalloc(vsi->alloc_queue_pairs,
1585                                    sizeof(struct i40e_ring), GFP_KERNEL);
1586                 if (!rx_rings) {
1587                         err = -ENOMEM;
1588                         goto free_tx;
1589                 }
1590
1591                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1592                         u16 unused;
1593
1594                         /* clone ring and setup updated count */
1595                         rx_rings[i] = *vsi->rx_rings[i];
1596                         rx_rings[i].count = new_rx_count;
1597                         /* the desc and bi pointers will be reallocated in the
1598                          * setup call
1599                          */
1600                         rx_rings[i].desc = NULL;
1601                         rx_rings[i].rx_bi = NULL;
1602                         /* Clear cloned XDP RX-queue info before setup call */
1603                         memset(&rx_rings[i].xdp_rxq, 0, sizeof(rx_rings[i].xdp_rxq));
1604                         /* this is to allow wr32 to have something to write to
1605                          * during early allocation of Rx buffers
1606                          */
1607                         rx_rings[i].tail = hw->hw_addr + I40E_PRTGEN_STATUS;
1608                         err = i40e_setup_rx_descriptors(&rx_rings[i]);
1609                         if (err)
1610                                 goto rx_unwind;
1611
1612                         /* now allocate the Rx buffers to make sure the OS
1613                          * has enough memory, any failure here means abort
1614                          */
1615                         unused = I40E_DESC_UNUSED(&rx_rings[i]);
1616                         err = i40e_alloc_rx_buffers(&rx_rings[i], unused);
1617 rx_unwind:
1618                         if (err) {
1619                                 do {
1620                                         i40e_free_rx_resources(&rx_rings[i]);
1621                                 } while (i--);
1622                                 kfree(rx_rings);
1623                                 rx_rings = NULL;
1624
1625                                 goto free_tx;
1626                         }
1627                 }
1628         }
1629
1630         /* Bring interface down, copy in the new ring info,
1631          * then restore the interface
1632          */
1633         i40e_down(vsi);
1634
1635         if (tx_rings) {
1636                 for (i = 0; i < tx_alloc_queue_pairs; i++) {
1637                         if (i40e_active_tx_ring_index(vsi, i)) {
1638                                 i40e_free_tx_resources(vsi->tx_rings[i]);
1639                                 *vsi->tx_rings[i] = tx_rings[i];
1640                         }
1641                 }
1642                 kfree(tx_rings);
1643                 tx_rings = NULL;
1644         }
1645
1646         if (rx_rings) {
1647                 for (i = 0; i < vsi->num_queue_pairs; i++) {
1648                         i40e_free_rx_resources(vsi->rx_rings[i]);
1649                         /* get the real tail offset */
1650                         rx_rings[i].tail = vsi->rx_rings[i]->tail;
1651                         /* this is to fake out the allocation routine
1652                          * into thinking it has to realloc everything
1653                          * but the recycling logic will let us re-use
1654                          * the buffers allocated above
1655                          */
1656                         rx_rings[i].next_to_use = 0;
1657                         rx_rings[i].next_to_clean = 0;
1658                         rx_rings[i].next_to_alloc = 0;
1659                         /* do a struct copy */
1660                         *vsi->rx_rings[i] = rx_rings[i];
1661                 }
1662                 kfree(rx_rings);
1663                 rx_rings = NULL;
1664         }
1665
1666         i40e_up(vsi);
1667
1668 free_tx:
1669         /* error cleanup if the Rx allocations failed after getting Tx */
1670         if (tx_rings) {
1671                 for (i = 0; i < tx_alloc_queue_pairs; i++) {
1672                         if (i40e_active_tx_ring_index(vsi, i))
1673                                 i40e_free_tx_resources(vsi->tx_rings[i]);
1674                 }
1675                 kfree(tx_rings);
1676                 tx_rings = NULL;
1677         }
1678
1679 done:
1680         clear_bit(__I40E_CONFIG_BUSY, pf->state);
1681
1682         return err;
1683 }
1684
1685 /**
1686  * i40e_get_stats_count - return the stats count for a device
1687  * @netdev: the netdev to return the count for
1688  *
1689  * Returns the total number of statistics for this netdev. Note that even
1690  * though this is a function, it is required that the count for a specific
1691  * netdev must never change. Basing the count on static values such as the
1692  * maximum number of queues or the device type is ok. However, the API for
1693  * obtaining stats is *not* safe against changes based on non-static
1694  * values such as the *current* number of queues, or runtime flags.
1695  *
1696  * If a statistic is not always enabled, return it as part of the count
1697  * anyways, always return its string, and report its value as zero.
1698  **/
1699 static int i40e_get_stats_count(struct net_device *netdev)
1700 {
1701         struct i40e_netdev_priv *np = netdev_priv(netdev);
1702         struct i40e_vsi *vsi = np->vsi;
1703         struct i40e_pf *pf = vsi->back;
1704
1705         if (vsi == pf->vsi[pf->lan_vsi] && pf->hw.partition_id == 1)
1706                 return I40E_PF_STATS_LEN(netdev);
1707         else
1708                 return I40E_VSI_STATS_LEN(netdev);
1709 }
1710
1711 static int i40e_get_sset_count(struct net_device *netdev, int sset)
1712 {
1713         struct i40e_netdev_priv *np = netdev_priv(netdev);
1714         struct i40e_vsi *vsi = np->vsi;
1715         struct i40e_pf *pf = vsi->back;
1716
1717         switch (sset) {
1718         case ETH_SS_TEST:
1719                 return I40E_TEST_LEN;
1720         case ETH_SS_STATS:
1721                 return i40e_get_stats_count(netdev);
1722         case ETH_SS_PRIV_FLAGS:
1723                 return I40E_PRIV_FLAGS_STR_LEN +
1724                         (pf->hw.pf_id == 0 ? I40E_GL_PRIV_FLAGS_STR_LEN : 0);
1725         default:
1726                 return -EOPNOTSUPP;
1727         }
1728 }
1729
1730 /**
1731  * i40e_add_one_ethtool_stat - copy the stat into the supplied buffer
1732  * @data: location to store the stat value
1733  * @pointer: basis for where to copy from
1734  * @stat: the stat definition
1735  *
1736  * Copies the stat data defined by the pointer and stat structure pair into
1737  * the memory supplied as data. Used to implement i40e_add_ethtool_stats.
1738  * If the pointer is null, data will be zero'd.
1739  */
1740 static inline void
1741 i40e_add_one_ethtool_stat(u64 *data, void *pointer,
1742                           const struct i40e_stats *stat)
1743 {
1744         char *p;
1745
1746         if (!pointer) {
1747                 /* ensure that the ethtool data buffer is zero'd for any stats
1748                  * which don't have a valid pointer.
1749                  */
1750                 *data = 0;
1751                 return;
1752         }
1753
1754         p = (char *)pointer + stat->stat_offset;
1755         switch (stat->sizeof_stat) {
1756         case sizeof(u64):
1757                 *data = *((u64 *)p);
1758                 break;
1759         case sizeof(u32):
1760                 *data = *((u32 *)p);
1761                 break;
1762         case sizeof(u16):
1763                 *data = *((u16 *)p);
1764                 break;
1765         case sizeof(u8):
1766                 *data = *((u8 *)p);
1767                 break;
1768         default:
1769                 WARN_ONCE(1, "unexpected stat size for %s",
1770                           stat->stat_string);
1771                 *data = 0;
1772         }
1773 }
1774
1775 /**
1776  * __i40e_add_ethtool_stats - copy stats into the ethtool supplied buffer
1777  * @data: ethtool stats buffer
1778  * @pointer: location to copy stats from
1779  * @stats: array of stats to copy
1780  * @size: the size of the stats definition
1781  *
1782  * Copy the stats defined by the stats array using the pointer as a base into
1783  * the data buffer supplied by ethtool. Updates the data pointer to point to
1784  * the next empty location for successive calls to __i40e_add_ethtool_stats.
1785  * If pointer is null, set the data values to zero and update the pointer to
1786  * skip these stats.
1787  **/
1788 static inline void
1789 __i40e_add_ethtool_stats(u64 **data, void *pointer,
1790                          const struct i40e_stats stats[],
1791                          const unsigned int size)
1792 {
1793         unsigned int i;
1794
1795         for (i = 0; i < size; i++)
1796                 i40e_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
1797 }
1798
1799 /**
1800  * i40e_add_ethtool_stats - copy stats into ethtool supplied buffer
1801  * @data: ethtool stats buffer
1802  * @pointer: location where stats are stored
1803  * @stats: static const array of stat definitions
1804  *
1805  * Macro to ease the use of __i40e_add_ethtool_stats by taking a static
1806  * constant stats array and passing the ARRAY_SIZE(). This avoids typos by
1807  * ensuring that we pass the size associated with the given stats array.
1808  * Assumes that stats is an array.
1809  **/
1810 #define i40e_add_ethtool_stats(data, pointer, stats) \
1811         __i40e_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
1812
1813 /**
1814  * i40e_get_pfc_stats - copy HW PFC statistics to formatted structure
1815  * @pf: the PF device structure
1816  * @i: the priority value to copy
1817  *
1818  * The PFC stats are found as arrays in pf->stats, which is not easy to pass
1819  * into i40e_add_ethtool_stats. Produce a formatted i40e_pfc_stats structure
1820  * of the PFC stats for the given priority.
1821  **/
1822 static inline struct i40e_pfc_stats
1823 i40e_get_pfc_stats(struct i40e_pf *pf, unsigned int i)
1824 {
1825 #define I40E_GET_PFC_STAT(stat, priority) \
1826         .stat = pf->stats.stat[priority]
1827
1828         struct i40e_pfc_stats pfc = {
1829                 I40E_GET_PFC_STAT(priority_xon_rx, i),
1830                 I40E_GET_PFC_STAT(priority_xoff_rx, i),
1831                 I40E_GET_PFC_STAT(priority_xon_tx, i),
1832                 I40E_GET_PFC_STAT(priority_xoff_tx, i),
1833                 I40E_GET_PFC_STAT(priority_xon_2_xoff, i),
1834         };
1835         return pfc;
1836 }
1837
1838 /**
1839  * i40e_get_ethtool_stats - copy stat values into supplied buffer
1840  * @netdev: the netdev to collect stats for
1841  * @stats: ethtool stats command structure
1842  * @data: ethtool supplied buffer
1843  *
1844  * Copy the stats values for this netdev into the buffer. Expects data to be
1845  * pre-allocated to the size returned by i40e_get_stats_count.. Note that all
1846  * statistics must be copied in a static order, and the count must not change
1847  * for a given netdev. See i40e_get_stats_count for more details.
1848  *
1849  * If a statistic is not currently valid (such as a disabled queue), this
1850  * function reports its value as zero.
1851  **/
1852 static void i40e_get_ethtool_stats(struct net_device *netdev,
1853                                    struct ethtool_stats *stats, u64 *data)
1854 {
1855         struct i40e_netdev_priv *np = netdev_priv(netdev);
1856         struct i40e_ring *tx_ring, *rx_ring;
1857         struct i40e_vsi *vsi = np->vsi;
1858         struct i40e_pf *pf = vsi->back;
1859         struct i40e_veb *veb = pf->veb[pf->lan_veb];
1860         unsigned int i;
1861         unsigned int start;
1862         bool veb_stats;
1863         u64 *p = data;
1864
1865         i40e_update_stats(vsi);
1866
1867         i40e_add_ethtool_stats(&data, i40e_get_vsi_stats_struct(vsi),
1868                                i40e_gstrings_net_stats);
1869
1870         i40e_add_ethtool_stats(&data, vsi, i40e_gstrings_misc_stats);
1871
1872         rcu_read_lock();
1873         for (i = 0; i < I40E_MAX_NUM_QUEUES(netdev) ; i++) {
1874                 tx_ring = READ_ONCE(vsi->tx_rings[i]);
1875
1876                 if (!tx_ring) {
1877                         /* Bump the stat counter to skip these stats, and make
1878                          * sure the memory is zero'd
1879                          */
1880                         *(data++) = 0;
1881                         *(data++) = 0;
1882                         *(data++) = 0;
1883                         *(data++) = 0;
1884                         continue;
1885                 }
1886
1887                 /* process Tx ring statistics */
1888                 do {
1889                         start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
1890                         data[0] = tx_ring->stats.packets;
1891                         data[1] = tx_ring->stats.bytes;
1892                 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
1893                 data += 2;
1894
1895                 /* Rx ring is the 2nd half of the queue pair */
1896                 rx_ring = &tx_ring[1];
1897                 do {
1898                         start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
1899                         data[0] = rx_ring->stats.packets;
1900                         data[1] = rx_ring->stats.bytes;
1901                 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
1902                 data += 2;
1903         }
1904         rcu_read_unlock();
1905         if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
1906                 goto check_data_pointer;
1907
1908         veb_stats = ((pf->lan_veb != I40E_NO_VEB) &&
1909                      (pf->flags & I40E_FLAG_VEB_STATS_ENABLED));
1910
1911         /* If veb stats aren't enabled, pass NULL instead of the veb so that
1912          * we initialize stats to zero and update the data pointer
1913          * intelligently
1914          */
1915         i40e_add_ethtool_stats(&data, veb_stats ? veb : NULL,
1916                                i40e_gstrings_veb_stats);
1917
1918         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
1919                 i40e_add_ethtool_stats(&data, veb_stats ? veb : NULL,
1920                                        i40e_gstrings_veb_tc_stats);
1921
1922         i40e_add_ethtool_stats(&data, pf, i40e_gstrings_stats);
1923
1924         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
1925                 struct i40e_pfc_stats pfc = i40e_get_pfc_stats(pf, i);
1926
1927                 i40e_add_ethtool_stats(&data, &pfc, i40e_gstrings_pfc_stats);
1928         }
1929
1930 check_data_pointer:
1931         WARN_ONCE(data - p != i40e_get_stats_count(netdev),
1932                   "ethtool stats count mismatch!");
1933 }
1934
1935 /**
1936  * __i40e_add_stat_strings - copy stat strings into ethtool buffer
1937  * @p: ethtool supplied buffer
1938  * @stats: stat definitions array
1939  * @size: size of the stats array
1940  *
1941  * Format and copy the strings described by stats into the buffer pointed at
1942  * by p.
1943  **/
1944 static void __i40e_add_stat_strings(u8 **p, const struct i40e_stats stats[],
1945                                     const unsigned int size, ...)
1946 {
1947         unsigned int i;
1948
1949         for (i = 0; i < size; i++) {
1950                 va_list args;
1951
1952                 va_start(args, size);
1953                 vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
1954                 *p += ETH_GSTRING_LEN;
1955                 va_end(args);
1956         }
1957 }
1958
1959 /**
1960  * 40e_add_stat_strings - copy stat strings into ethtool buffer
1961  * @p: ethtool supplied buffer
1962  * @stats: stat definitions array
1963  *
1964  * Format and copy the strings described by the const static stats value into
1965  * the buffer pointed at by p. Assumes that stats can have ARRAY_SIZE called
1966  * for it.
1967  **/
1968 #define i40e_add_stat_strings(p, stats, ...) \
1969         __i40e_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
1970
1971 /**
1972  * i40e_get_stat_strings - copy stat strings into supplied buffer
1973  * @netdev: the netdev to collect strings for
1974  * @data: supplied buffer to copy strings into
1975  *
1976  * Copy the strings related to stats for this netdev. Expects data to be
1977  * pre-allocated with the size reported by i40e_get_stats_count. Note that the
1978  * strings must be copied in a static order and the total count must not
1979  * change for a given netdev. See i40e_get_stats_count for more details.
1980  **/
1981 static void i40e_get_stat_strings(struct net_device *netdev, u8 *data)
1982 {
1983         struct i40e_netdev_priv *np = netdev_priv(netdev);
1984         struct i40e_vsi *vsi = np->vsi;
1985         struct i40e_pf *pf = vsi->back;
1986         unsigned int i;
1987         u8 *p = data;
1988
1989         i40e_add_stat_strings(&data, i40e_gstrings_net_stats);
1990
1991         i40e_add_stat_strings(&data, i40e_gstrings_misc_stats);
1992
1993         for (i = 0; i < I40E_MAX_NUM_QUEUES(netdev); i++) {
1994                 snprintf(data, ETH_GSTRING_LEN, "tx-%u.tx_packets", i);
1995                 data += ETH_GSTRING_LEN;
1996                 snprintf(data, ETH_GSTRING_LEN, "tx-%u.tx_bytes", i);
1997                 data += ETH_GSTRING_LEN;
1998                 snprintf(data, ETH_GSTRING_LEN, "rx-%u.rx_packets", i);
1999                 data += ETH_GSTRING_LEN;
2000                 snprintf(data, ETH_GSTRING_LEN, "rx-%u.rx_bytes", i);
2001                 data += ETH_GSTRING_LEN;
2002         }
2003         if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
2004                 return;
2005
2006         i40e_add_stat_strings(&data, i40e_gstrings_veb_stats);
2007
2008         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
2009                 i40e_add_stat_strings(&data, i40e_gstrings_veb_tc_stats, i);
2010
2011         i40e_add_stat_strings(&data, i40e_gstrings_stats);
2012
2013         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
2014                 i40e_add_stat_strings(&data, i40e_gstrings_pfc_stats, i);
2015
2016         WARN_ONCE(data - p != i40e_get_stats_count(netdev) * ETH_GSTRING_LEN,
2017                   "stat strings count mismatch!");
2018 }
2019
2020 static void i40e_get_priv_flag_strings(struct net_device *netdev, u8 *data)
2021 {
2022         struct i40e_netdev_priv *np = netdev_priv(netdev);
2023         struct i40e_vsi *vsi = np->vsi;
2024         struct i40e_pf *pf = vsi->back;
2025         char *p = (char *)data;
2026         unsigned int i;
2027
2028         for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
2029                 snprintf(p, ETH_GSTRING_LEN, "%s",
2030                          i40e_gstrings_priv_flags[i].flag_string);
2031                 p += ETH_GSTRING_LEN;
2032         }
2033         if (pf->hw.pf_id != 0)
2034                 return;
2035         for (i = 0; i < I40E_GL_PRIV_FLAGS_STR_LEN; i++) {
2036                 snprintf(p, ETH_GSTRING_LEN, "%s",
2037                          i40e_gl_gstrings_priv_flags[i].flag_string);
2038                 p += ETH_GSTRING_LEN;
2039         }
2040 }
2041
2042 static void i40e_get_strings(struct net_device *netdev, u32 stringset,
2043                              u8 *data)
2044 {
2045         switch (stringset) {
2046         case ETH_SS_TEST:
2047                 memcpy(data, i40e_gstrings_test,
2048                        I40E_TEST_LEN * ETH_GSTRING_LEN);
2049                 break;
2050         case ETH_SS_STATS:
2051                 i40e_get_stat_strings(netdev, data);
2052                 break;
2053         case ETH_SS_PRIV_FLAGS:
2054                 i40e_get_priv_flag_strings(netdev, data);
2055                 break;
2056         default:
2057                 break;
2058         }
2059 }
2060
2061 static int i40e_get_ts_info(struct net_device *dev,
2062                             struct ethtool_ts_info *info)
2063 {
2064         struct i40e_pf *pf = i40e_netdev_to_pf(dev);
2065
2066         /* only report HW timestamping if PTP is enabled */
2067         if (!(pf->flags & I40E_FLAG_PTP))
2068                 return ethtool_op_get_ts_info(dev, info);
2069
2070         info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
2071                                 SOF_TIMESTAMPING_RX_SOFTWARE |
2072                                 SOF_TIMESTAMPING_SOFTWARE |
2073                                 SOF_TIMESTAMPING_TX_HARDWARE |
2074                                 SOF_TIMESTAMPING_RX_HARDWARE |
2075                                 SOF_TIMESTAMPING_RAW_HARDWARE;
2076
2077         if (pf->ptp_clock)
2078                 info->phc_index = ptp_clock_index(pf->ptp_clock);
2079         else
2080                 info->phc_index = -1;
2081
2082         info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
2083
2084         info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
2085                            BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2086                            BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
2087                            BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ);
2088
2089         if (pf->hw_features & I40E_HW_PTP_L4_CAPABLE)
2090                 info->rx_filters |= BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
2091                                     BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
2092                                     BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
2093                                     BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
2094                                     BIT(HWTSTAMP_FILTER_PTP_V2_SYNC) |
2095                                     BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
2096                                     BIT(HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
2097                                     BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
2098
2099         return 0;
2100 }
2101
2102 static int i40e_link_test(struct net_device *netdev, u64 *data)
2103 {
2104         struct i40e_netdev_priv *np = netdev_priv(netdev);
2105         struct i40e_pf *pf = np->vsi->back;
2106         i40e_status status;
2107         bool link_up = false;
2108
2109         netif_info(pf, hw, netdev, "link test\n");
2110         status = i40e_get_link_status(&pf->hw, &link_up);
2111         if (status) {
2112                 netif_err(pf, drv, netdev, "link query timed out, please retry test\n");
2113                 *data = 1;
2114                 return *data;
2115         }
2116
2117         if (link_up)
2118                 *data = 0;
2119         else
2120                 *data = 1;
2121
2122         return *data;
2123 }
2124
2125 static int i40e_reg_test(struct net_device *netdev, u64 *data)
2126 {
2127         struct i40e_netdev_priv *np = netdev_priv(netdev);
2128         struct i40e_pf *pf = np->vsi->back;
2129
2130         netif_info(pf, hw, netdev, "register test\n");
2131         *data = i40e_diag_reg_test(&pf->hw);
2132
2133         return *data;
2134 }
2135
2136 static int i40e_eeprom_test(struct net_device *netdev, u64 *data)
2137 {
2138         struct i40e_netdev_priv *np = netdev_priv(netdev);
2139         struct i40e_pf *pf = np->vsi->back;
2140
2141         netif_info(pf, hw, netdev, "eeprom test\n");
2142         *data = i40e_diag_eeprom_test(&pf->hw);
2143
2144         /* forcebly clear the NVM Update state machine */
2145         pf->hw.nvmupd_state = I40E_NVMUPD_STATE_INIT;
2146
2147         return *data;
2148 }
2149
2150 static int i40e_intr_test(struct net_device *netdev, u64 *data)
2151 {
2152         struct i40e_netdev_priv *np = netdev_priv(netdev);
2153         struct i40e_pf *pf = np->vsi->back;
2154         u16 swc_old = pf->sw_int_count;
2155
2156         netif_info(pf, hw, netdev, "interrupt test\n");
2157         wr32(&pf->hw, I40E_PFINT_DYN_CTL0,
2158              (I40E_PFINT_DYN_CTL0_INTENA_MASK |
2159               I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
2160               I40E_PFINT_DYN_CTL0_ITR_INDX_MASK |
2161               I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK |
2162               I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK));
2163         usleep_range(1000, 2000);
2164         *data = (swc_old == pf->sw_int_count);
2165
2166         return *data;
2167 }
2168
2169 static inline bool i40e_active_vfs(struct i40e_pf *pf)
2170 {
2171         struct i40e_vf *vfs = pf->vf;
2172         int i;
2173
2174         for (i = 0; i < pf->num_alloc_vfs; i++)
2175                 if (test_bit(I40E_VF_STATE_ACTIVE, &vfs[i].vf_states))
2176                         return true;
2177         return false;
2178 }
2179
2180 static inline bool i40e_active_vmdqs(struct i40e_pf *pf)
2181 {
2182         return !!i40e_find_vsi_by_type(pf, I40E_VSI_VMDQ2);
2183 }
2184
2185 static void i40e_diag_test(struct net_device *netdev,
2186                            struct ethtool_test *eth_test, u64 *data)
2187 {
2188         struct i40e_netdev_priv *np = netdev_priv(netdev);
2189         bool if_running = netif_running(netdev);
2190         struct i40e_pf *pf = np->vsi->back;
2191
2192         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
2193                 /* Offline tests */
2194                 netif_info(pf, drv, netdev, "offline testing starting\n");
2195
2196                 set_bit(__I40E_TESTING, pf->state);
2197
2198                 if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
2199                     test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
2200                         dev_warn(&pf->pdev->dev,
2201                                  "Cannot start offline testing when PF is in reset state.\n");
2202                         goto skip_ol_tests;
2203                 }
2204
2205                 if (i40e_active_vfs(pf) || i40e_active_vmdqs(pf)) {
2206                         dev_warn(&pf->pdev->dev,
2207                                  "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
2208                         goto skip_ol_tests;
2209                 }
2210
2211                 /* If the device is online then take it offline */
2212                 if (if_running)
2213                         /* indicate we're in test mode */
2214                         i40e_close(netdev);
2215                 else
2216                         /* This reset does not affect link - if it is
2217                          * changed to a type of reset that does affect
2218                          * link then the following link test would have
2219                          * to be moved to before the reset
2220                          */
2221                         i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
2222
2223                 if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
2224                         eth_test->flags |= ETH_TEST_FL_FAILED;
2225
2226                 if (i40e_eeprom_test(netdev, &data[I40E_ETH_TEST_EEPROM]))
2227                         eth_test->flags |= ETH_TEST_FL_FAILED;
2228
2229                 if (i40e_intr_test(netdev, &data[I40E_ETH_TEST_INTR]))
2230                         eth_test->flags |= ETH_TEST_FL_FAILED;
2231
2232                 /* run reg test last, a reset is required after it */
2233                 if (i40e_reg_test(netdev, &data[I40E_ETH_TEST_REG]))
2234                         eth_test->flags |= ETH_TEST_FL_FAILED;
2235
2236                 clear_bit(__I40E_TESTING, pf->state);
2237                 i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
2238
2239                 if (if_running)
2240                         i40e_open(netdev);
2241         } else {
2242                 /* Online tests */
2243                 netif_info(pf, drv, netdev, "online testing starting\n");
2244
2245                 if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
2246                         eth_test->flags |= ETH_TEST_FL_FAILED;
2247
2248                 /* Offline only tests, not run in online; pass by default */
2249                 data[I40E_ETH_TEST_REG] = 0;
2250                 data[I40E_ETH_TEST_EEPROM] = 0;
2251                 data[I40E_ETH_TEST_INTR] = 0;
2252         }
2253
2254         netif_info(pf, drv, netdev, "testing finished\n");
2255         return;
2256
2257 skip_ol_tests:
2258         data[I40E_ETH_TEST_REG]         = 1;
2259         data[I40E_ETH_TEST_EEPROM]      = 1;
2260         data[I40E_ETH_TEST_INTR]        = 1;
2261         data[I40E_ETH_TEST_LINK]        = 1;
2262         eth_test->flags |= ETH_TEST_FL_FAILED;
2263         clear_bit(__I40E_TESTING, pf->state);
2264         netif_info(pf, drv, netdev, "testing failed\n");
2265 }
2266
2267 static void i40e_get_wol(struct net_device *netdev,
2268                          struct ethtool_wolinfo *wol)
2269 {
2270         struct i40e_netdev_priv *np = netdev_priv(netdev);
2271         struct i40e_pf *pf = np->vsi->back;
2272         struct i40e_hw *hw = &pf->hw;
2273         u16 wol_nvm_bits;
2274
2275         /* NVM bit on means WoL disabled for the port */
2276         i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
2277         if ((BIT(hw->port) & wol_nvm_bits) || (hw->partition_id != 1)) {
2278                 wol->supported = 0;
2279                 wol->wolopts = 0;
2280         } else {
2281                 wol->supported = WAKE_MAGIC;
2282                 wol->wolopts = (pf->wol_en ? WAKE_MAGIC : 0);
2283         }
2284 }
2285
2286 /**
2287  * i40e_set_wol - set the WakeOnLAN configuration
2288  * @netdev: the netdev in question
2289  * @wol: the ethtool WoL setting data
2290  **/
2291 static int i40e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2292 {
2293         struct i40e_netdev_priv *np = netdev_priv(netdev);
2294         struct i40e_pf *pf = np->vsi->back;
2295         struct i40e_vsi *vsi = np->vsi;
2296         struct i40e_hw *hw = &pf->hw;
2297         u16 wol_nvm_bits;
2298
2299         /* WoL not supported if this isn't the controlling PF on the port */
2300         if (hw->partition_id != 1) {
2301                 i40e_partition_setting_complaint(pf);
2302                 return -EOPNOTSUPP;
2303         }
2304
2305         if (vsi != pf->vsi[pf->lan_vsi])
2306                 return -EOPNOTSUPP;
2307
2308         /* NVM bit on means WoL disabled for the port */
2309         i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
2310         if (BIT(hw->port) & wol_nvm_bits)
2311                 return -EOPNOTSUPP;
2312
2313         /* only magic packet is supported */
2314         if (wol->wolopts && (wol->wolopts != WAKE_MAGIC))
2315                 return -EOPNOTSUPP;
2316
2317         /* is this a new value? */
2318         if (pf->wol_en != !!wol->wolopts) {
2319                 pf->wol_en = !!wol->wolopts;
2320                 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
2321         }
2322
2323         return 0;
2324 }
2325
2326 static int i40e_set_phys_id(struct net_device *netdev,
2327                             enum ethtool_phys_id_state state)
2328 {
2329         struct i40e_netdev_priv *np = netdev_priv(netdev);
2330         i40e_status ret = 0;
2331         struct i40e_pf *pf = np->vsi->back;
2332         struct i40e_hw *hw = &pf->hw;
2333         int blink_freq = 2;
2334         u16 temp_status;
2335
2336         switch (state) {
2337         case ETHTOOL_ID_ACTIVE:
2338                 if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS)) {
2339                         pf->led_status = i40e_led_get(hw);
2340                 } else {
2341                         if (!(hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE))
2342                                 i40e_aq_set_phy_debug(hw, I40E_PHY_DEBUG_ALL,
2343                                                       NULL);
2344                         ret = i40e_led_get_phy(hw, &temp_status,
2345                                                &pf->phy_led_val);
2346                         pf->led_status = temp_status;
2347                 }
2348                 return blink_freq;
2349         case ETHTOOL_ID_ON:
2350                 if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS))
2351                         i40e_led_set(hw, 0xf, false);
2352                 else
2353                         ret = i40e_led_set_phy(hw, true, pf->led_status, 0);
2354                 break;
2355         case ETHTOOL_ID_OFF:
2356                 if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS))
2357                         i40e_led_set(hw, 0x0, false);
2358                 else
2359                         ret = i40e_led_set_phy(hw, false, pf->led_status, 0);
2360                 break;
2361         case ETHTOOL_ID_INACTIVE:
2362                 if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS)) {
2363                         i40e_led_set(hw, pf->led_status, false);
2364                 } else {
2365                         ret = i40e_led_set_phy(hw, false, pf->led_status,
2366                                                (pf->phy_led_val |
2367                                                I40E_PHY_LED_MODE_ORIG));
2368                         if (!(hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE))
2369                                 i40e_aq_set_phy_debug(hw, 0, NULL);
2370                 }
2371                 break;
2372         default:
2373                 break;
2374         }
2375                 if (ret)
2376                         return -ENOENT;
2377                 else
2378                         return 0;
2379 }
2380
2381 /* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
2382  * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
2383  * 125us (8000 interrupts per second) == ITR(62)
2384  */
2385
2386 /**
2387  * __i40e_get_coalesce - get per-queue coalesce settings
2388  * @netdev: the netdev to check
2389  * @ec: ethtool coalesce data structure
2390  * @queue: which queue to pick
2391  *
2392  * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
2393  * are per queue. If queue is <0 then we default to queue 0 as the
2394  * representative value.
2395  **/
2396 static int __i40e_get_coalesce(struct net_device *netdev,
2397                                struct ethtool_coalesce *ec,
2398                                int queue)
2399 {
2400         struct i40e_netdev_priv *np = netdev_priv(netdev);
2401         struct i40e_ring *rx_ring, *tx_ring;
2402         struct i40e_vsi *vsi = np->vsi;
2403
2404         ec->tx_max_coalesced_frames_irq = vsi->work_limit;
2405         ec->rx_max_coalesced_frames_irq = vsi->work_limit;
2406
2407         /* rx and tx usecs has per queue value. If user doesn't specify the
2408          * queue, return queue 0's value to represent.
2409          */
2410         if (queue < 0)
2411                 queue = 0;
2412         else if (queue >= vsi->num_queue_pairs)
2413                 return -EINVAL;
2414
2415         rx_ring = vsi->rx_rings[queue];
2416         tx_ring = vsi->tx_rings[queue];
2417
2418         if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
2419                 ec->use_adaptive_rx_coalesce = 1;
2420
2421         if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
2422                 ec->use_adaptive_tx_coalesce = 1;
2423
2424         ec->rx_coalesce_usecs = rx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
2425         ec->tx_coalesce_usecs = tx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
2426
2427         /* we use the _usecs_high to store/set the interrupt rate limit
2428          * that the hardware supports, that almost but not quite
2429          * fits the original intent of the ethtool variable,
2430          * the rx_coalesce_usecs_high limits total interrupts
2431          * per second from both tx/rx sources.
2432          */
2433         ec->rx_coalesce_usecs_high = vsi->int_rate_limit;
2434         ec->tx_coalesce_usecs_high = vsi->int_rate_limit;
2435
2436         return 0;
2437 }
2438
2439 /**
2440  * i40e_get_coalesce - get a netdev's coalesce settings
2441  * @netdev: the netdev to check
2442  * @ec: ethtool coalesce data structure
2443  *
2444  * Gets the coalesce settings for a particular netdev. Note that if user has
2445  * modified per-queue settings, this only guarantees to represent queue 0. See
2446  * __i40e_get_coalesce for more details.
2447  **/
2448 static int i40e_get_coalesce(struct net_device *netdev,
2449                              struct ethtool_coalesce *ec)
2450 {
2451         return __i40e_get_coalesce(netdev, ec, -1);
2452 }
2453
2454 /**
2455  * i40e_get_per_queue_coalesce - gets coalesce settings for particular queue
2456  * @netdev: netdev structure
2457  * @ec: ethtool's coalesce settings
2458  * @queue: the particular queue to read
2459  *
2460  * Will read a specific queue's coalesce settings
2461  **/
2462 static int i40e_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
2463                                        struct ethtool_coalesce *ec)
2464 {
2465         return __i40e_get_coalesce(netdev, ec, queue);
2466 }
2467
2468 /**
2469  * i40e_set_itr_per_queue - set ITR values for specific queue
2470  * @vsi: the VSI to set values for
2471  * @ec: coalesce settings from ethtool
2472  * @queue: the queue to modify
2473  *
2474  * Change the ITR settings for a specific queue.
2475  **/
2476 static void i40e_set_itr_per_queue(struct i40e_vsi *vsi,
2477                                    struct ethtool_coalesce *ec,
2478                                    int queue)
2479 {
2480         struct i40e_ring *rx_ring = vsi->rx_rings[queue];
2481         struct i40e_ring *tx_ring = vsi->tx_rings[queue];
2482         struct i40e_pf *pf = vsi->back;
2483         struct i40e_hw *hw = &pf->hw;
2484         struct i40e_q_vector *q_vector;
2485         u16 intrl;
2486
2487         intrl = i40e_intrl_usec_to_reg(vsi->int_rate_limit);
2488
2489         rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
2490         tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
2491
2492         if (ec->use_adaptive_rx_coalesce)
2493                 rx_ring->itr_setting |= I40E_ITR_DYNAMIC;
2494         else
2495                 rx_ring->itr_setting &= ~I40E_ITR_DYNAMIC;
2496
2497         if (ec->use_adaptive_tx_coalesce)
2498                 tx_ring->itr_setting |= I40E_ITR_DYNAMIC;
2499         else
2500                 tx_ring->itr_setting &= ~I40E_ITR_DYNAMIC;
2501
2502         q_vector = rx_ring->q_vector;
2503         q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
2504
2505         q_vector = tx_ring->q_vector;
2506         q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
2507
2508         /* The interrupt handler itself will take care of programming
2509          * the Tx and Rx ITR values based on the values we have entered
2510          * into the q_vector, no need to write the values now.
2511          */
2512
2513         wr32(hw, I40E_PFINT_RATEN(q_vector->reg_idx), intrl);
2514         i40e_flush(hw);
2515 }
2516
2517 /**
2518  * __i40e_set_coalesce - set coalesce settings for particular queue
2519  * @netdev: the netdev to change
2520  * @ec: ethtool coalesce settings
2521  * @queue: the queue to change
2522  *
2523  * Sets the coalesce settings for a particular queue.
2524  **/
2525 static int __i40e_set_coalesce(struct net_device *netdev,
2526                                struct ethtool_coalesce *ec,
2527                                int queue)
2528 {
2529         struct i40e_netdev_priv *np = netdev_priv(netdev);
2530         u16 intrl_reg, cur_rx_itr, cur_tx_itr;
2531         struct i40e_vsi *vsi = np->vsi;
2532         struct i40e_pf *pf = vsi->back;
2533         int i;
2534
2535         if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
2536                 vsi->work_limit = ec->tx_max_coalesced_frames_irq;
2537
2538         if (queue < 0) {
2539                 cur_rx_itr = vsi->rx_rings[0]->itr_setting;
2540                 cur_tx_itr = vsi->tx_rings[0]->itr_setting;
2541         } else if (queue < vsi->num_queue_pairs) {
2542                 cur_rx_itr = vsi->rx_rings[queue]->itr_setting;
2543                 cur_tx_itr = vsi->tx_rings[queue]->itr_setting;
2544         } else {
2545                 netif_info(pf, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
2546                            vsi->num_queue_pairs - 1);
2547                 return -EINVAL;
2548         }
2549
2550         cur_tx_itr &= ~I40E_ITR_DYNAMIC;
2551         cur_rx_itr &= ~I40E_ITR_DYNAMIC;
2552
2553         /* tx_coalesce_usecs_high is ignored, use rx-usecs-high instead */
2554         if (ec->tx_coalesce_usecs_high != vsi->int_rate_limit) {
2555                 netif_info(pf, drv, netdev, "tx-usecs-high is not used, please program rx-usecs-high\n");
2556                 return -EINVAL;
2557         }
2558
2559         if (ec->rx_coalesce_usecs_high > INTRL_REG_TO_USEC(I40E_MAX_INTRL)) {
2560                 netif_info(pf, drv, netdev, "Invalid value, rx-usecs-high range is 0-%lu\n",
2561                            INTRL_REG_TO_USEC(I40E_MAX_INTRL));
2562                 return -EINVAL;
2563         }
2564
2565         if (ec->rx_coalesce_usecs != cur_rx_itr &&
2566             ec->use_adaptive_rx_coalesce) {
2567                 netif_info(pf, drv, netdev, "RX interrupt moderation cannot be changed if adaptive-rx is enabled.\n");
2568                 return -EINVAL;
2569         }
2570
2571         if (ec->rx_coalesce_usecs > I40E_MAX_ITR) {
2572                 netif_info(pf, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
2573                 return -EINVAL;
2574         }
2575
2576         if (ec->tx_coalesce_usecs != cur_tx_itr &&
2577             ec->use_adaptive_tx_coalesce) {
2578                 netif_info(pf, drv, netdev, "TX interrupt moderation cannot be changed if adaptive-tx is enabled.\n");
2579                 return -EINVAL;
2580         }
2581
2582         if (ec->tx_coalesce_usecs > I40E_MAX_ITR) {
2583                 netif_info(pf, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
2584                 return -EINVAL;
2585         }
2586
2587         if (ec->use_adaptive_rx_coalesce && !cur_rx_itr)
2588                 ec->rx_coalesce_usecs = I40E_MIN_ITR;
2589
2590         if (ec->use_adaptive_tx_coalesce && !cur_tx_itr)
2591                 ec->tx_coalesce_usecs = I40E_MIN_ITR;
2592
2593         intrl_reg = i40e_intrl_usec_to_reg(ec->rx_coalesce_usecs_high);
2594         vsi->int_rate_limit = INTRL_REG_TO_USEC(intrl_reg);
2595         if (vsi->int_rate_limit != ec->rx_coalesce_usecs_high) {
2596                 netif_info(pf, drv, netdev, "Interrupt rate limit rounded down to %d\n",
2597                            vsi->int_rate_limit);
2598         }
2599
2600         /* rx and tx usecs has per queue value. If user doesn't specify the
2601          * queue, apply to all queues.
2602          */
2603         if (queue < 0) {
2604                 for (i = 0; i < vsi->num_queue_pairs; i++)
2605                         i40e_set_itr_per_queue(vsi, ec, i);
2606         } else {
2607                 i40e_set_itr_per_queue(vsi, ec, queue);
2608         }
2609
2610         return 0;
2611 }
2612
2613 /**
2614  * i40e_set_coalesce - set coalesce settings for every queue on the netdev
2615  * @netdev: the netdev to change
2616  * @ec: ethtool coalesce settings
2617  *
2618  * This will set each queue to the same coalesce settings.
2619  **/
2620 static int i40e_set_coalesce(struct net_device *netdev,
2621                              struct ethtool_coalesce *ec)
2622 {
2623         return __i40e_set_coalesce(netdev, ec, -1);
2624 }
2625
2626 /**
2627  * i40e_set_per_queue_coalesce - set specific queue's coalesce settings
2628  * @netdev: the netdev to change
2629  * @ec: ethtool's coalesce settings
2630  * @queue: the queue to change
2631  *
2632  * Sets the specified queue's coalesce settings.
2633  **/
2634 static int i40e_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
2635                                        struct ethtool_coalesce *ec)
2636 {
2637         return __i40e_set_coalesce(netdev, ec, queue);
2638 }
2639
2640 /**
2641  * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
2642  * @pf: pointer to the physical function struct
2643  * @cmd: ethtool rxnfc command
2644  *
2645  * Returns Success if the flow is supported, else Invalid Input.
2646  **/
2647 static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
2648 {
2649         struct i40e_hw *hw = &pf->hw;
2650         u8 flow_pctype = 0;
2651         u64 i_set = 0;
2652
2653         cmd->data = 0;
2654
2655         switch (cmd->flow_type) {
2656         case TCP_V4_FLOW:
2657                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
2658                 break;
2659         case UDP_V4_FLOW:
2660                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
2661                 break;
2662         case TCP_V6_FLOW:
2663                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
2664                 break;
2665         case UDP_V6_FLOW:
2666                 flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
2667                 break;
2668         case SCTP_V4_FLOW:
2669         case AH_ESP_V4_FLOW:
2670         case AH_V4_FLOW:
2671         case ESP_V4_FLOW:
2672         case IPV4_FLOW:
2673         case SCTP_V6_FLOW:
2674         case AH_ESP_V6_FLOW:
2675         case AH_V6_FLOW:
2676         case ESP_V6_FLOW:
2677         case IPV6_FLOW:
2678                 /* Default is src/dest for IP, no matter the L4 hashing */
2679                 cmd->data |= RXH_IP_SRC | RXH_IP_DST;
2680                 break;
2681         default:
2682                 return -EINVAL;
2683         }
2684
2685         /* Read flow based hash input set register */
2686         if (flow_pctype) {
2687                 i_set = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0,
2688                                               flow_pctype)) |
2689                         ((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1,
2690                                                flow_pctype)) << 32);
2691         }
2692
2693         /* Process bits of hash input set */
2694         if (i_set) {
2695                 if (i_set & I40E_L4_SRC_MASK)
2696                         cmd->data |= RXH_L4_B_0_1;
2697                 if (i_set & I40E_L4_DST_MASK)
2698                         cmd->data |= RXH_L4_B_2_3;
2699
2700                 if (cmd->flow_type == TCP_V4_FLOW ||
2701                     cmd->flow_type == UDP_V4_FLOW) {
2702                         if (hw->mac.type == I40E_MAC_X722) {
2703                                 if (i_set & I40E_X722_L3_SRC_MASK)
2704                                         cmd->data |= RXH_IP_SRC;
2705                                 if (i_set & I40E_X722_L3_DST_MASK)
2706                                         cmd->data |= RXH_IP_DST;
2707                         } else {
2708                                 if (i_set & I40E_L3_SRC_MASK)
2709                                         cmd->data |= RXH_IP_SRC;
2710                                 if (i_set & I40E_L3_DST_MASK)
2711                                         cmd->data |= RXH_IP_DST;
2712                         }
2713                 } else if (cmd->flow_type == TCP_V6_FLOW ||
2714                           cmd->flow_type == UDP_V6_FLOW) {
2715                         if (i_set & I40E_L3_V6_SRC_MASK)
2716                                 cmd->data |= RXH_IP_SRC;
2717                         if (i_set & I40E_L3_V6_DST_MASK)
2718                                 cmd->data |= RXH_IP_DST;
2719                 }
2720         }
2721
2722         return 0;
2723 }
2724
2725 /**
2726  * i40e_check_mask - Check whether a mask field is set
2727  * @mask: the full mask value
2728  * @field: mask of the field to check
2729  *
2730  * If the given mask is fully set, return positive value. If the mask for the
2731  * field is fully unset, return zero. Otherwise return a negative error code.
2732  **/
2733 static int i40e_check_mask(u64 mask, u64 field)
2734 {
2735         u64 value = mask & field;
2736
2737         if (value == field)
2738                 return 1;
2739         else if (!value)
2740                 return 0;
2741         else
2742                 return -1;
2743 }
2744
2745 /**
2746  * i40e_parse_rx_flow_user_data - Deconstruct user-defined data
2747  * @fsp: pointer to rx flow specification
2748  * @data: pointer to userdef data structure for storage
2749  *
2750  * Read the user-defined data and deconstruct the value into a structure. No
2751  * other code should read the user-defined data, so as to ensure that every
2752  * place consistently reads the value correctly.
2753  *
2754  * The user-defined field is a 64bit Big Endian format value, which we
2755  * deconstruct by reading bits or bit fields from it. Single bit flags shall
2756  * be defined starting from the highest bits, while small bit field values
2757  * shall be defined starting from the lowest bits.
2758  *
2759  * Returns 0 if the data is valid, and non-zero if the userdef data is invalid
2760  * and the filter should be rejected. The data structure will always be
2761  * modified even if FLOW_EXT is not set.
2762  *
2763  **/
2764 static int i40e_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
2765                                         struct i40e_rx_flow_userdef *data)
2766 {
2767         u64 value, mask;
2768         int valid;
2769
2770         /* Zero memory first so it's always consistent. */
2771         memset(data, 0, sizeof(*data));
2772
2773         if (!(fsp->flow_type & FLOW_EXT))
2774                 return 0;
2775
2776         value = be64_to_cpu(*((__be64 *)fsp->h_ext.data));
2777         mask = be64_to_cpu(*((__be64 *)fsp->m_ext.data));
2778
2779 #define I40E_USERDEF_FLEX_WORD          GENMASK_ULL(15, 0)
2780 #define I40E_USERDEF_FLEX_OFFSET        GENMASK_ULL(31, 16)
2781 #define I40E_USERDEF_FLEX_FILTER        GENMASK_ULL(31, 0)
2782
2783         valid = i40e_check_mask(mask, I40E_USERDEF_FLEX_FILTER);
2784         if (valid < 0) {
2785                 return -EINVAL;
2786         } else if (valid) {
2787                 data->flex_word = value & I40E_USERDEF_FLEX_WORD;
2788                 data->flex_offset =
2789                         (value & I40E_USERDEF_FLEX_OFFSET) >> 16;
2790                 data->flex_filter = true;
2791         }
2792
2793         return 0;
2794 }
2795
2796 /**
2797  * i40e_fill_rx_flow_user_data - Fill in user-defined data field
2798  * @fsp: pointer to rx_flow specification
2799  * @data: pointer to return userdef data
2800  *
2801  * Reads the userdef data structure and properly fills in the user defined
2802  * fields of the rx_flow_spec.
2803  **/
2804 static void i40e_fill_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
2805                                         struct i40e_rx_flow_userdef *data)
2806 {
2807         u64 value = 0, mask = 0;
2808
2809         if (data->flex_filter) {
2810                 value |= data->flex_word;
2811                 value |= (u64)data->flex_offset << 16;
2812                 mask |= I40E_USERDEF_FLEX_FILTER;
2813         }
2814
2815         if (value || mask)
2816                 fsp->flow_type |= FLOW_EXT;
2817
2818         *((__be64 *)fsp->h_ext.data) = cpu_to_be64(value);
2819         *((__be64 *)fsp->m_ext.data) = cpu_to_be64(mask);
2820 }
2821
2822 /**
2823  * i40e_get_ethtool_fdir_all - Populates the rule count of a command
2824  * @pf: Pointer to the physical function struct
2825  * @cmd: The command to get or set Rx flow classification rules
2826  * @rule_locs: Array of used rule locations
2827  *
2828  * This function populates both the total and actual rule count of
2829  * the ethtool flow classification command
2830  *
2831  * Returns 0 on success or -EMSGSIZE if entry not found
2832  **/
2833 static int i40e_get_ethtool_fdir_all(struct i40e_pf *pf,
2834                                      struct ethtool_rxnfc *cmd,
2835                                      u32 *rule_locs)
2836 {
2837         struct i40e_fdir_filter *rule;
2838         struct hlist_node *node2;
2839         int cnt = 0;
2840
2841         /* report total rule count */
2842         cmd->data = i40e_get_fd_cnt_all(pf);
2843
2844         hlist_for_each_entry_safe(rule, node2,
2845                                   &pf->fdir_filter_list, fdir_node) {
2846                 if (cnt == cmd->rule_cnt)
2847                         return -EMSGSIZE;
2848
2849                 rule_locs[cnt] = rule->fd_id;
2850                 cnt++;
2851         }
2852
2853         cmd->rule_cnt = cnt;
2854
2855         return 0;
2856 }
2857
2858 /**
2859  * i40e_get_ethtool_fdir_entry - Look up a filter based on Rx flow
2860  * @pf: Pointer to the physical function struct
2861  * @cmd: The command to get or set Rx flow classification rules
2862  *
2863  * This function looks up a filter based on the Rx flow classification
2864  * command and fills the flow spec info for it if found
2865  *
2866  * Returns 0 on success or -EINVAL if filter not found
2867  **/
2868 static int i40e_get_ethtool_fdir_entry(struct i40e_pf *pf,
2869                                        struct ethtool_rxnfc *cmd)
2870 {
2871         struct ethtool_rx_flow_spec *fsp =
2872                         (struct ethtool_rx_flow_spec *)&cmd->fs;
2873         struct i40e_rx_flow_userdef userdef = {0};
2874         struct i40e_fdir_filter *rule = NULL;
2875         struct hlist_node *node2;
2876         u64 input_set;
2877         u16 index;
2878
2879         hlist_for_each_entry_safe(rule, node2,
2880                                   &pf->fdir_filter_list, fdir_node) {
2881                 if (fsp->location <= rule->fd_id)
2882                         break;
2883         }
2884
2885         if (!rule || fsp->location != rule->fd_id)
2886                 return -EINVAL;
2887
2888         fsp->flow_type = rule->flow_type;
2889         if (fsp->flow_type == IP_USER_FLOW) {
2890                 fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
2891                 fsp->h_u.usr_ip4_spec.proto = 0;
2892                 fsp->m_u.usr_ip4_spec.proto = 0;
2893         }
2894
2895         /* Reverse the src and dest notion, since the HW views them from
2896          * Tx perspective where as the user expects it from Rx filter view.
2897          */
2898         fsp->h_u.tcp_ip4_spec.psrc = rule->dst_port;
2899         fsp->h_u.tcp_ip4_spec.pdst = rule->src_port;
2900         fsp->h_u.tcp_ip4_spec.ip4src = rule->dst_ip;
2901         fsp->h_u.tcp_ip4_spec.ip4dst = rule->src_ip;
2902
2903         switch (rule->flow_type) {
2904         case SCTP_V4_FLOW:
2905                 index = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
2906                 break;
2907         case TCP_V4_FLOW:
2908                 index = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
2909                 break;
2910         case UDP_V4_FLOW:
2911                 index = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
2912                 break;
2913         case IP_USER_FLOW:
2914                 index = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
2915                 break;
2916         default:
2917                 /* If we have stored a filter with a flow type not listed here
2918                  * it is almost certainly a driver bug. WARN(), and then
2919                  * assign the input_set as if all fields are enabled to avoid
2920                  * reading unassigned memory.
2921                  */
2922                 WARN(1, "Missing input set index for flow_type %d\n",
2923                      rule->flow_type);
2924                 input_set = 0xFFFFFFFFFFFFFFFFULL;
2925                 goto no_input_set;
2926         }
2927
2928         input_set = i40e_read_fd_input_set(pf, index);
2929
2930 no_input_set:
2931         if (input_set & I40E_L3_SRC_MASK)
2932                 fsp->m_u.tcp_ip4_spec.ip4src = htonl(0xFFFFFFFF);
2933
2934         if (input_set & I40E_L3_DST_MASK)
2935                 fsp->m_u.tcp_ip4_spec.ip4dst = htonl(0xFFFFFFFF);
2936
2937         if (input_set & I40E_L4_SRC_MASK)
2938                 fsp->m_u.tcp_ip4_spec.psrc = htons(0xFFFF);
2939
2940         if (input_set & I40E_L4_DST_MASK)
2941                 fsp->m_u.tcp_ip4_spec.pdst = htons(0xFFFF);
2942
2943         if (rule->dest_ctl == I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET)
2944                 fsp->ring_cookie = RX_CLS_FLOW_DISC;
2945         else
2946                 fsp->ring_cookie = rule->q_index;
2947
2948         if (rule->dest_vsi != pf->vsi[pf->lan_vsi]->id) {
2949                 struct i40e_vsi *vsi;
2950
2951                 vsi = i40e_find_vsi_from_id(pf, rule->dest_vsi);
2952                 if (vsi && vsi->type == I40E_VSI_SRIOV) {
2953                         /* VFs are zero-indexed by the driver, but ethtool
2954                          * expects them to be one-indexed, so add one here
2955                          */
2956                         u64 ring_vf = vsi->vf_id + 1;
2957
2958                         ring_vf <<= ETHTOOL_RX_FLOW_SPEC_RING_VF_OFF;
2959                         fsp->ring_cookie |= ring_vf;
2960                 }
2961         }
2962
2963         if (rule->flex_filter) {
2964                 userdef.flex_filter = true;
2965                 userdef.flex_word = be16_to_cpu(rule->flex_word);
2966                 userdef.flex_offset = rule->flex_offset;
2967         }
2968
2969         i40e_fill_rx_flow_user_data(fsp, &userdef);
2970
2971         return 0;
2972 }
2973
2974 /**
2975  * i40e_get_rxnfc - command to get RX flow classification rules
2976  * @netdev: network interface device structure
2977  * @cmd: ethtool rxnfc command
2978  * @rule_locs: pointer to store rule data
2979  *
2980  * Returns Success if the command is supported.
2981  **/
2982 static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
2983                           u32 *rule_locs)
2984 {
2985         struct i40e_netdev_priv *np = netdev_priv(netdev);
2986         struct i40e_vsi *vsi = np->vsi;
2987         struct i40e_pf *pf = vsi->back;
2988         int ret = -EOPNOTSUPP;
2989
2990         switch (cmd->cmd) {
2991         case ETHTOOL_GRXRINGS:
2992                 cmd->data = vsi->rss_size;
2993                 ret = 0;
2994                 break;
2995         case ETHTOOL_GRXFH:
2996                 ret = i40e_get_rss_hash_opts(pf, cmd);
2997                 break;
2998         case ETHTOOL_GRXCLSRLCNT:
2999                 cmd->rule_cnt = pf->fdir_pf_active_filters;
3000                 /* report total rule count */
3001                 cmd->data = i40e_get_fd_cnt_all(pf);
3002                 ret = 0;
3003                 break;
3004         case ETHTOOL_GRXCLSRULE:
3005                 ret = i40e_get_ethtool_fdir_entry(pf, cmd);
3006                 break;
3007         case ETHTOOL_GRXCLSRLALL:
3008                 ret = i40e_get_ethtool_fdir_all(pf, cmd, rule_locs);
3009                 break;
3010         default:
3011                 break;
3012         }
3013
3014         return ret;
3015 }
3016
3017 /**
3018  * i40e_get_rss_hash_bits - Read RSS Hash bits from register
3019  * @hw: hw structure
3020  * @nfc: pointer to user request
3021  * @i_setc: bits currently set
3022  *
3023  * Returns value of bits to be set per user request
3024  **/
3025 static u64 i40e_get_rss_hash_bits(struct i40e_hw *hw,
3026                                   struct ethtool_rxnfc *nfc,
3027                                   u64 i_setc)
3028 {
3029         u64 i_set = i_setc;
3030         u64 src_l3 = 0, dst_l3 = 0;
3031
3032         if (nfc->data & RXH_L4_B_0_1)
3033                 i_set |= I40E_L4_SRC_MASK;
3034         else
3035                 i_set &= ~I40E_L4_SRC_MASK;
3036         if (nfc->data & RXH_L4_B_2_3)
3037                 i_set |= I40E_L4_DST_MASK;
3038         else
3039                 i_set &= ~I40E_L4_DST_MASK;
3040
3041         if (nfc->flow_type == TCP_V6_FLOW || nfc->flow_type == UDP_V6_FLOW) {
3042                 src_l3 = I40E_L3_V6_SRC_MASK;
3043                 dst_l3 = I40E_L3_V6_DST_MASK;
3044         } else if (nfc->flow_type == TCP_V4_FLOW ||
3045                   nfc->flow_type == UDP_V4_FLOW) {
3046                 if (hw->mac.type == I40E_MAC_X722) {
3047                         src_l3 = I40E_X722_L3_SRC_MASK;
3048                         dst_l3 = I40E_X722_L3_DST_MASK;
3049                 } else {
3050                         src_l3 = I40E_L3_SRC_MASK;
3051                         dst_l3 = I40E_L3_DST_MASK;
3052                 }
3053         } else {
3054                 /* Any other flow type are not supported here */
3055                 return i_set;
3056         }
3057
3058         if (nfc->data & RXH_IP_SRC)
3059                 i_set |= src_l3;
3060         else
3061                 i_set &= ~src_l3;
3062         if (nfc->data & RXH_IP_DST)
3063                 i_set |= dst_l3;
3064         else
3065                 i_set &= ~dst_l3;
3066
3067         return i_set;
3068 }
3069
3070 #define FLOW_PCTYPES_SIZE 64
3071 /**
3072  * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
3073  * @pf: pointer to the physical function struct
3074  * @nfc: ethtool rxnfc command
3075  *
3076  * Returns Success if the flow input set is supported.
3077  **/
3078 static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
3079 {
3080         struct i40e_hw *hw = &pf->hw;
3081         u64 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
3082                    ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
3083         DECLARE_BITMAP(flow_pctypes, FLOW_PCTYPES_SIZE);
3084         u64 i_set, i_setc;
3085
3086         bitmap_zero(flow_pctypes, FLOW_PCTYPES_SIZE);
3087
3088         if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3089                 dev_err(&pf->pdev->dev,
3090                         "Change of RSS hash input set is not supported when MFP mode is enabled\n");
3091                 return -EOPNOTSUPP;
3092         }
3093
3094         /* RSS does not support anything other than hashing
3095          * to queues on src and dst IPs and ports
3096          */
3097         if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
3098                           RXH_L4_B_0_1 | RXH_L4_B_2_3))
3099                 return -EINVAL;
3100
3101         switch (nfc->flow_type) {
3102         case TCP_V4_FLOW:
3103                 set_bit(I40E_FILTER_PCTYPE_NONF_IPV4_TCP, flow_pctypes);
3104                 if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
3105                         set_bit(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK,
3106                                 flow_pctypes);
3107                 break;
3108         case TCP_V6_FLOW:
3109                 set_bit(I40E_FILTER_PCTYPE_NONF_IPV6_TCP, flow_pctypes);
3110                 if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
3111                         set_bit(I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK,
3112                                 flow_pctypes);
3113                 break;
3114         case UDP_V4_FLOW:
3115                 set_bit(I40E_FILTER_PCTYPE_NONF_IPV4_UDP, flow_pctypes);
3116                 if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
3117                         set_bit(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP,
3118                                 flow_pctypes);
3119                         set_bit(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP,
3120                                 flow_pctypes);
3121                 }
3122                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
3123                 break;
3124         case UDP_V6_FLOW:
3125                 set_bit(I40E_FILTER_PCTYPE_NONF_IPV6_UDP, flow_pctypes);
3126                 if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
3127                         set_bit(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP,
3128                                 flow_pctypes);
3129                         set_bit(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP,
3130                                 flow_pctypes);
3131                 }
3132                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
3133                 break;
3134         case AH_ESP_V4_FLOW:
3135         case AH_V4_FLOW:
3136         case ESP_V4_FLOW:
3137         case SCTP_V4_FLOW:
3138                 if ((nfc->data & RXH_L4_B_0_1) ||
3139                     (nfc->data & RXH_L4_B_2_3))
3140                         return -EINVAL;
3141                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
3142                 break;
3143         case AH_ESP_V6_FLOW:
3144         case AH_V6_FLOW:
3145         case ESP_V6_FLOW:
3146         case SCTP_V6_FLOW:
3147                 if ((nfc->data & RXH_L4_B_0_1) ||
3148                     (nfc->data & RXH_L4_B_2_3))
3149                         return -EINVAL;
3150                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
3151                 break;
3152         case IPV4_FLOW:
3153                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
3154                         BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
3155                 break;
3156         case IPV6_FLOW:
3157                 hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
3158                         BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
3159                 break;
3160         default:
3161                 return -EINVAL;
3162         }
3163
3164         if (bitmap_weight(flow_pctypes, FLOW_PCTYPES_SIZE)) {
3165                 u8 flow_id;
3166
3167                 for_each_set_bit(flow_id, flow_pctypes, FLOW_PCTYPES_SIZE) {
3168                         i_setc = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0, flow_id)) |
3169                                  ((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1, flow_id)) << 32);
3170                         i_set = i40e_get_rss_hash_bits(&pf->hw, nfc, i_setc);
3171
3172                         i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(0, flow_id),
3173                                           (u32)i_set);
3174                         i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(1, flow_id),
3175                                           (u32)(i_set >> 32));
3176                         hena |= BIT_ULL(flow_id);
3177                 }
3178         }
3179
3180         i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
3181         i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
3182         i40e_flush(hw);
3183
3184         return 0;
3185 }
3186
3187 /**
3188  * i40e_update_ethtool_fdir_entry - Updates the fdir filter entry
3189  * @vsi: Pointer to the targeted VSI
3190  * @input: The filter to update or NULL to indicate deletion
3191  * @sw_idx: Software index to the filter
3192  * @cmd: The command to get or set Rx flow classification rules
3193  *
3194  * This function updates (or deletes) a Flow Director entry from
3195  * the hlist of the corresponding PF
3196  *
3197  * Returns 0 on success
3198  **/
3199 static int i40e_update_ethtool_fdir_entry(struct i40e_vsi *vsi,
3200                                           struct i40e_fdir_filter *input,
3201                                           u16 sw_idx,
3202                                           struct ethtool_rxnfc *cmd)
3203 {
3204         struct i40e_fdir_filter *rule, *parent;
3205         struct i40e_pf *pf = vsi->back;
3206         struct hlist_node *node2;
3207         int err = -EINVAL;
3208
3209         parent = NULL;
3210         rule = NULL;
3211
3212         hlist_for_each_entry_safe(rule, node2,
3213                                   &pf->fdir_filter_list, fdir_node) {
3214                 /* hash found, or no matching entry */
3215                 if (rule->fd_id >= sw_idx)
3216                         break;
3217                 parent = rule;
3218         }
3219
3220         /* if there is an old rule occupying our place remove it */
3221         if (rule && (rule->fd_id == sw_idx)) {
3222                 /* Remove this rule, since we're either deleting it, or
3223                  * replacing it.
3224                  */
3225                 err = i40e_add_del_fdir(vsi, rule, false);
3226                 hlist_del(&rule->fdir_node);
3227                 kfree(rule);
3228                 pf->fdir_pf_active_filters--;
3229         }
3230
3231         /* If we weren't given an input, this is a delete, so just return the
3232          * error code indicating if there was an entry at the requested slot
3233          */
3234         if (!input)
3235                 return err;
3236
3237         /* Otherwise, install the new rule as requested */
3238         INIT_HLIST_NODE(&input->fdir_node);
3239
3240         /* add filter to the list */
3241         if (parent)
3242                 hlist_add_behind(&input->fdir_node, &parent->fdir_node);
3243         else
3244                 hlist_add_head(&input->fdir_node,
3245                                &pf->fdir_filter_list);
3246
3247         /* update counts */
3248         pf->fdir_pf_active_filters++;
3249
3250         return 0;
3251 }
3252
3253 /**
3254  * i40e_prune_flex_pit_list - Cleanup unused entries in FLX_PIT table
3255  * @pf: pointer to PF structure
3256  *
3257  * This function searches the list of filters and determines which FLX_PIT
3258  * entries are still required. It will prune any entries which are no longer
3259  * in use after the deletion.
3260  **/
3261 static void i40e_prune_flex_pit_list(struct i40e_pf *pf)
3262 {
3263         struct i40e_flex_pit *entry, *tmp;
3264         struct i40e_fdir_filter *rule;
3265
3266         /* First, we'll check the l3 table */
3267         list_for_each_entry_safe(entry, tmp, &pf->l3_flex_pit_list, list) {
3268                 bool found = false;
3269
3270                 hlist_for_each_entry(rule, &pf->fdir_filter_list, fdir_node) {
3271                         if (rule->flow_type != IP_USER_FLOW)
3272                                 continue;
3273                         if (rule->flex_filter &&
3274                             rule->flex_offset == entry->src_offset) {
3275                                 found = true;
3276                                 break;
3277                         }
3278                 }
3279
3280                 /* If we didn't find the filter, then we can prune this entry
3281                  * from the list.
3282                  */
3283                 if (!found) {
3284                         list_del(&entry->list);
3285                         kfree(entry);
3286                 }
3287         }
3288
3289         /* Followed by the L4 table */
3290         list_for_each_entry_safe(entry, tmp, &pf->l4_flex_pit_list, list) {
3291                 bool found = false;
3292
3293                 hlist_for_each_entry(rule, &pf->fdir_filter_list, fdir_node) {
3294                         /* Skip this filter if it's L3, since we already
3295                          * checked those in the above loop
3296                          */
3297                         if (rule->flow_type == IP_USER_FLOW)
3298                                 continue;
3299                         if (rule->flex_filter &&
3300                             rule->flex_offset == entry->src_offset) {
3301                                 found = true;
3302                                 break;
3303                         }
3304                 }
3305
3306                 /* If we didn't find the filter, then we can prune this entry
3307                  * from the list.
3308                  */
3309                 if (!found) {
3310                         list_del(&entry->list);
3311                         kfree(entry);
3312                 }
3313         }
3314 }
3315
3316 /**
3317  * i40e_del_fdir_entry - Deletes a Flow Director filter entry
3318  * @vsi: Pointer to the targeted VSI
3319  * @cmd: The command to get or set Rx flow classification rules
3320  *
3321  * The function removes a Flow Director filter entry from the
3322  * hlist of the corresponding PF
3323  *
3324  * Returns 0 on success
3325  */
3326 static int i40e_del_fdir_entry(struct i40e_vsi *vsi,
3327                                struct ethtool_rxnfc *cmd)
3328 {
3329         struct ethtool_rx_flow_spec *fsp =
3330                 (struct ethtool_rx_flow_spec *)&cmd->fs;
3331         struct i40e_pf *pf = vsi->back;
3332         int ret = 0;
3333
3334         if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
3335             test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
3336                 return -EBUSY;
3337
3338         if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
3339                 return -EBUSY;
3340
3341         ret = i40e_update_ethtool_fdir_entry(vsi, NULL, fsp->location, cmd);
3342
3343         i40e_prune_flex_pit_list(pf);
3344
3345         i40e_fdir_check_and_reenable(pf);
3346         return ret;
3347 }
3348
3349 /**
3350  * i40e_unused_pit_index - Find an unused PIT index for given list
3351  * @pf: the PF data structure
3352  *
3353  * Find the first unused flexible PIT index entry. We search both the L3 and
3354  * L4 flexible PIT lists so that the returned index is unique and unused by
3355  * either currently programmed L3 or L4 filters. We use a bit field as storage
3356  * to track which indexes are already used.
3357  **/
3358 static u8 i40e_unused_pit_index(struct i40e_pf *pf)
3359 {
3360         unsigned long available_index = 0xFF;
3361         struct i40e_flex_pit *entry;
3362
3363         /* We need to make sure that the new index isn't in use by either L3
3364          * or L4 filters so that IP_USER_FLOW filters can program both L3 and
3365          * L4 to use the same index.
3366          */
3367
3368         list_for_each_entry(entry, &pf->l4_flex_pit_list, list)
3369                 clear_bit(entry->pit_index, &available_index);
3370
3371         list_for_each_entry(entry, &pf->l3_flex_pit_list, list)
3372                 clear_bit(entry->pit_index, &available_index);
3373
3374         return find_first_bit(&available_index, 8);
3375 }
3376
3377 /**
3378  * i40e_find_flex_offset - Find an existing flex src_offset
3379  * @flex_pit_list: L3 or L4 flex PIT list
3380  * @src_offset: new src_offset to find
3381  *
3382  * Searches the flex_pit_list for an existing offset. If no offset is
3383  * currently programmed, then this will return an ERR_PTR if there is no space
3384  * to add a new offset, otherwise it returns NULL.
3385  **/
3386 static
3387 struct i40e_flex_pit *i40e_find_flex_offset(struct list_head *flex_pit_list,
3388                                             u16 src_offset)
3389 {
3390         struct i40e_flex_pit *entry;
3391         int size = 0;
3392
3393         /* Search for the src_offset first. If we find a matching entry
3394          * already programmed, we can simply re-use it.
3395          */
3396         list_for_each_entry(entry, flex_pit_list, list) {
3397                 size++;
3398                 if (entry->src_offset == src_offset)
3399                         return entry;
3400         }
3401
3402         /* If we haven't found an entry yet, then the provided src offset has
3403          * not yet been programmed. We will program the src offset later on,
3404          * but we need to indicate whether there is enough space to do so
3405          * here. We'll make use of ERR_PTR for this purpose.
3406          */
3407         if (size >= I40E_FLEX_PIT_TABLE_SIZE)
3408                 return ERR_PTR(-ENOSPC);
3409
3410         return NULL;
3411 }
3412
3413 /**
3414  * i40e_add_flex_offset - Add src_offset to flex PIT table list
3415  * @flex_pit_list: L3 or L4 flex PIT list
3416  * @src_offset: new src_offset to add
3417  * @pit_index: the PIT index to program
3418  *
3419  * This function programs the new src_offset to the list. It is expected that
3420  * i40e_find_flex_offset has already been tried and returned NULL, indicating
3421  * that this offset is not programmed, and that the list has enough space to
3422  * store another offset.
3423  *
3424  * Returns 0 on success, and negative value on error.
3425  **/
3426 static int i40e_add_flex_offset(struct list_head *flex_pit_list,
3427                                 u16 src_offset,
3428                                 u8 pit_index)
3429 {
3430         struct i40e_flex_pit *new_pit, *entry;
3431
3432         new_pit = kzalloc(sizeof(*entry), GFP_KERNEL);
3433         if (!new_pit)
3434                 return -ENOMEM;
3435
3436         new_pit->src_offset = src_offset;
3437         new_pit->pit_index = pit_index;
3438
3439         /* We need to insert this item such that the list is sorted by
3440          * src_offset in ascending order.
3441          */
3442         list_for_each_entry(entry, flex_pit_list, list) {
3443                 if (new_pit->src_offset < entry->src_offset) {
3444                         list_add_tail(&new_pit->list, &entry->list);
3445                         return 0;
3446                 }
3447
3448                 /* If we found an entry with our offset already programmed we
3449                  * can simply return here, after freeing the memory. However,
3450                  * if the pit_index does not match we need to report an error.
3451                  */
3452                 if (new_pit->src_offset == entry->src_offset) {
3453                         int err = 0;
3454
3455                         /* If the PIT index is not the same we can't re-use
3456                          * the entry, so we must report an error.
3457                          */
3458                         if (new_pit->pit_index != entry->pit_index)
3459                                 err = -EINVAL;
3460
3461                         kfree(new_pit);
3462                         return err;
3463                 }
3464         }
3465
3466         /* If we reached here, then we haven't yet added the item. This means
3467          * that we should add the item at the end of the list.
3468          */
3469         list_add_tail(&new_pit->list, flex_pit_list);
3470         return 0;
3471 }
3472
3473 /**
3474  * __i40e_reprogram_flex_pit - Re-program specific FLX_PIT table
3475  * @pf: Pointer to the PF structure
3476  * @flex_pit_list: list of flexible src offsets in use
3477  * @flex_pit_start: index to first entry for this section of the table
3478  *
3479  * In order to handle flexible data, the hardware uses a table of values
3480  * called the FLX_PIT table. This table is used to indicate which sections of
3481  * the input correspond to what PIT index values. Unfortunately, hardware is
3482  * very restrictive about programming this table. Entries must be ordered by
3483  * src_offset in ascending order, without duplicates. Additionally, unused
3484  * entries must be set to the unused index value, and must have valid size and
3485  * length according to the src_offset ordering.
3486  *
3487  * This function will reprogram the FLX_PIT register from a book-keeping
3488  * structure that we guarantee is already ordered correctly, and has no more
3489  * than 3 entries.
3490  *
3491  * To make things easier, we only support flexible values of one word length,
3492  * rather than allowing variable length flexible values.
3493  **/
3494 static void __i40e_reprogram_flex_pit(struct i40e_pf *pf,
3495                                       struct list_head *flex_pit_list,
3496                                       int flex_pit_start)
3497 {
3498         struct i40e_flex_pit *entry = NULL;
3499         u16 last_offset = 0;
3500         int i = 0, j = 0;
3501
3502         /* First, loop over the list of flex PIT entries, and reprogram the
3503          * registers.
3504          */
3505         list_for_each_entry(entry, flex_pit_list, list) {
3506                 /* We have to be careful when programming values for the
3507                  * largest SRC_OFFSET value. It is possible that adding
3508                  * additional empty values at the end would overflow the space
3509                  * for the SRC_OFFSET in the FLX_PIT register. To avoid this,
3510                  * we check here and add the empty values prior to adding the
3511                  * largest value.
3512                  *
3513                  * To determine this, we will use a loop from i+1 to 3, which
3514                  * will determine whether the unused entries would have valid
3515                  * SRC_OFFSET. Note that there cannot be extra entries past
3516                  * this value, because the only valid values would have been
3517                  * larger than I40E_MAX_FLEX_SRC_OFFSET, and thus would not
3518                  * have been added to the list in the first place.
3519                  */
3520                 for (j = i + 1; j < 3; j++) {
3521                         u16 offset = entry->src_offset + j;
3522                         int index = flex_pit_start + i;
3523                         u32 value = I40E_FLEX_PREP_VAL(I40E_FLEX_DEST_UNUSED,
3524                                                        1,
3525                                                        offset - 3);
3526
3527                         if (offset > I40E_MAX_FLEX_SRC_OFFSET) {
3528                                 i40e_write_rx_ctl(&pf->hw,
3529                                                   I40E_PRTQF_FLX_PIT(index),
3530                                                   value);
3531                                 i++;
3532                         }
3533                 }
3534
3535                 /* Now, we can program the actual value into the table */
3536                 i40e_write_rx_ctl(&pf->hw,
3537                                   I40E_PRTQF_FLX_PIT(flex_pit_start + i),
3538                                   I40E_FLEX_PREP_VAL(entry->pit_index + 50,
3539                                                      1,
3540                                                      entry->src_offset));
3541                 i++;
3542         }
3543
3544         /* In order to program the last entries in the table, we need to
3545          * determine the valid offset. If the list is empty, we'll just start
3546          * with 0. Otherwise, we'll start with the last item offset and add 1.
3547          * This ensures that all entries have valid sizes. If we don't do this
3548          * correctly, the hardware will disable flexible field parsing.
3549          */
3550         if (!list_empty(flex_pit_list))
3551                 last_offset = list_prev_entry(entry, list)->src_offset + 1;
3552
3553         for (; i < 3; i++, last_offset++) {
3554                 i40e_write_rx_ctl(&pf->hw,
3555                                   I40E_PRTQF_FLX_PIT(flex_pit_start + i),
3556                                   I40E_FLEX_PREP_VAL(I40E_FLEX_DEST_UNUSED,
3557                                                      1,
3558                                                      last_offset));
3559         }
3560 }
3561
3562 /**
3563  * i40e_reprogram_flex_pit - Reprogram all FLX_PIT tables after input set change
3564  * @pf: pointer to the PF structure
3565  *
3566  * This function reprograms both the L3 and L4 FLX_PIT tables. See the
3567  * internal helper function for implementation details.
3568  **/
3569 static void i40e_reprogram_flex_pit(struct i40e_pf *pf)
3570 {
3571         __i40e_reprogram_flex_pit(pf, &pf->l3_flex_pit_list,
3572                                   I40E_FLEX_PIT_IDX_START_L3);
3573
3574         __i40e_reprogram_flex_pit(pf, &pf->l4_flex_pit_list,
3575                                   I40E_FLEX_PIT_IDX_START_L4);
3576
3577         /* We also need to program the L3 and L4 GLQF ORT register */
3578         i40e_write_rx_ctl(&pf->hw,
3579                           I40E_GLQF_ORT(I40E_L3_GLQF_ORT_IDX),
3580                           I40E_ORT_PREP_VAL(I40E_FLEX_PIT_IDX_START_L3,
3581                                             3, 1));
3582
3583         i40e_write_rx_ctl(&pf->hw,
3584                           I40E_GLQF_ORT(I40E_L4_GLQF_ORT_IDX),
3585                           I40E_ORT_PREP_VAL(I40E_FLEX_PIT_IDX_START_L4,
3586                                             3, 1));
3587 }
3588
3589 /**
3590  * i40e_flow_str - Converts a flow_type into a human readable string
3591  * @fsp: the flow specification
3592  *
3593  * Currently only flow types we support are included here, and the string
3594  * value attempts to match what ethtool would use to configure this flow type.
3595  **/
3596 static const char *i40e_flow_str(struct ethtool_rx_flow_spec *fsp)
3597 {
3598         switch (fsp->flow_type & ~FLOW_EXT) {
3599         case TCP_V4_FLOW:
3600                 return "tcp4";
3601         case UDP_V4_FLOW:
3602                 return "udp4";
3603         case SCTP_V4_FLOW:
3604                 return "sctp4";
3605         case IP_USER_FLOW:
3606                 return "ip4";
3607         default:
3608                 return "unknown";
3609         }
3610 }
3611
3612 /**
3613  * i40e_pit_index_to_mask - Return the FLEX mask for a given PIT index
3614  * @pit_index: PIT index to convert
3615  *
3616  * Returns the mask for a given PIT index. Will return 0 if the pit_index is
3617  * of range.
3618  **/
3619 static u64 i40e_pit_index_to_mask(int pit_index)
3620 {
3621         switch (pit_index) {
3622         case 0:
3623                 return I40E_FLEX_50_MASK;
3624         case 1:
3625                 return I40E_FLEX_51_MASK;
3626         case 2:
3627                 return I40E_FLEX_52_MASK;
3628         case 3:
3629                 return I40E_FLEX_53_MASK;
3630         case 4:
3631                 return I40E_FLEX_54_MASK;
3632         case 5:
3633                 return I40E_FLEX_55_MASK;
3634         case 6:
3635                 return I40E_FLEX_56_MASK;
3636         case 7:
3637                 return I40E_FLEX_57_MASK;
3638         default:
3639                 return 0;
3640         }
3641 }
3642
3643 /**
3644  * i40e_print_input_set - Show changes between two input sets
3645  * @vsi: the vsi being configured
3646  * @old: the old input set
3647  * @new: the new input set
3648  *
3649  * Print the difference between old and new input sets by showing which series
3650  * of words are toggled on or off. Only displays the bits we actually support
3651  * changing.
3652  **/
3653 static void i40e_print_input_set(struct i40e_vsi *vsi, u64 old, u64 new)
3654 {
3655         struct i40e_pf *pf = vsi->back;
3656         bool old_value, new_value;
3657         int i;
3658
3659         old_value = !!(old & I40E_L3_SRC_MASK);
3660         new_value = !!(new & I40E_L3_SRC_MASK);
3661         if (old_value != new_value)
3662                 netif_info(pf, drv, vsi->netdev, "L3 source address: %s -> %s\n",
3663                            old_value ? "ON" : "OFF",
3664                            new_value ? "ON" : "OFF");
3665
3666         old_value = !!(old & I40E_L3_DST_MASK);
3667         new_value = !!(new & I40E_L3_DST_MASK);
3668         if (old_value != new_value)
3669                 netif_info(pf, drv, vsi->netdev, "L3 destination address: %s -> %s\n",
3670                            old_value ? "ON" : "OFF",
3671                            new_value ? "ON" : "OFF");
3672
3673         old_value = !!(old & I40E_L4_SRC_MASK);
3674         new_value = !!(new & I40E_L4_SRC_MASK);
3675         if (old_value != new_value)
3676                 netif_info(pf, drv, vsi->netdev, "L4 source port: %s -> %s\n",
3677                            old_value ? "ON" : "OFF",
3678                            new_value ? "ON" : "OFF");
3679
3680         old_value = !!(old & I40E_L4_DST_MASK);
3681         new_value = !!(new & I40E_L4_DST_MASK);
3682         if (old_value != new_value)
3683                 netif_info(pf, drv, vsi->netdev, "L4 destination port: %s -> %s\n",
3684                            old_value ? "ON" : "OFF",
3685                            new_value ? "ON" : "OFF");
3686
3687         old_value = !!(old & I40E_VERIFY_TAG_MASK);
3688         new_value = !!(new & I40E_VERIFY_TAG_MASK);
3689         if (old_value != new_value)
3690                 netif_info(pf, drv, vsi->netdev, "SCTP verification tag: %s -> %s\n",
3691                            old_value ? "ON" : "OFF",
3692                            new_value ? "ON" : "OFF");
3693
3694         /* Show change of flexible filter entries */
3695         for (i = 0; i < I40E_FLEX_INDEX_ENTRIES; i++) {
3696                 u64 flex_mask = i40e_pit_index_to_mask(i);
3697
3698                 old_value = !!(old & flex_mask);
3699                 new_value = !!(new & flex_mask);
3700                 if (old_value != new_value)
3701                         netif_info(pf, drv, vsi->netdev, "FLEX index %d: %s -> %s\n",
3702                                    i,
3703                                    old_value ? "ON" : "OFF",
3704                                    new_value ? "ON" : "OFF");
3705         }
3706
3707         netif_info(pf, drv, vsi->netdev, "  Current input set: %0llx\n",
3708                    old);
3709         netif_info(pf, drv, vsi->netdev, "Requested input set: %0llx\n",
3710                    new);
3711 }
3712
3713 /**
3714  * i40e_check_fdir_input_set - Check that a given rx_flow_spec mask is valid
3715  * @vsi: pointer to the targeted VSI
3716  * @fsp: pointer to Rx flow specification
3717  * @userdef: userdefined data from flow specification
3718  *
3719  * Ensures that a given ethtool_rx_flow_spec has a valid mask. Some support
3720  * for partial matches exists with a few limitations. First, hardware only
3721  * supports masking by word boundary (2 bytes) and not per individual bit.
3722  * Second, hardware is limited to using one mask for a flow type and cannot
3723  * use a separate mask for each filter.
3724  *
3725  * To support these limitations, if we already have a configured filter for
3726  * the specified type, this function enforces that new filters of the type
3727  * match the configured input set. Otherwise, if we do not have a filter of
3728  * the specified type, we allow the input set to be updated to match the
3729  * desired filter.
3730  *
3731  * To help ensure that administrators understand why filters weren't displayed
3732  * as supported, we print a diagnostic message displaying how the input set
3733  * would change and warning to delete the preexisting filters if required.
3734  *
3735  * Returns 0 on successful input set match, and a negative return code on
3736  * failure.
3737  **/
3738 static int i40e_check_fdir_input_set(struct i40e_vsi *vsi,
3739                                      struct ethtool_rx_flow_spec *fsp,
3740                                      struct i40e_rx_flow_userdef *userdef)
3741 {
3742         struct i40e_pf *pf = vsi->back;
3743         struct ethtool_tcpip4_spec *tcp_ip4_spec;
3744         struct ethtool_usrip4_spec *usr_ip4_spec;
3745         u64 current_mask, new_mask;
3746         bool new_flex_offset = false;
3747         bool flex_l3 = false;
3748         u16 *fdir_filter_count;
3749         u16 index, src_offset = 0;
3750         u8 pit_index = 0;
3751         int err;
3752
3753         switch (fsp->flow_type & ~FLOW_EXT) {
3754         case SCTP_V4_FLOW:
3755                 index = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
3756                 fdir_filter_count = &pf->fd_sctp4_filter_cnt;
3757                 break;
3758         case TCP_V4_FLOW:
3759                 index = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
3760                 fdir_filter_count = &pf->fd_tcp4_filter_cnt;
3761                 break;
3762         case UDP_V4_FLOW:
3763                 index = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
3764                 fdir_filter_count = &pf->fd_udp4_filter_cnt;
3765                 break;
3766         case IP_USER_FLOW:
3767                 index = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
3768                 fdir_filter_count = &pf->fd_ip4_filter_cnt;
3769                 flex_l3 = true;
3770                 break;
3771         default:
3772                 return -EOPNOTSUPP;
3773         }
3774
3775         /* Read the current input set from register memory. */
3776         current_mask = i40e_read_fd_input_set(pf, index);
3777         new_mask = current_mask;
3778
3779         /* Determine, if any, the required changes to the input set in order
3780          * to support the provided mask.
3781          *
3782          * Hardware only supports masking at word (2 byte) granularity and does
3783          * not support full bitwise masking. This implementation simplifies
3784          * even further and only supports fully enabled or fully disabled
3785          * masks for each field, even though we could split the ip4src and
3786          * ip4dst fields.
3787          */
3788         switch (fsp->flow_type & ~FLOW_EXT) {
3789         case SCTP_V4_FLOW:
3790                 new_mask &= ~I40E_VERIFY_TAG_MASK;
3791                 /* Fall through */
3792         case TCP_V4_FLOW:
3793         case UDP_V4_FLOW:
3794                 tcp_ip4_spec = &fsp->m_u.tcp_ip4_spec;
3795
3796                 /* IPv4 source address */
3797                 if (tcp_ip4_spec->ip4src == htonl(0xFFFFFFFF))
3798                         new_mask |= I40E_L3_SRC_MASK;
3799                 else if (!tcp_ip4_spec->ip4src)
3800                         new_mask &= ~I40E_L3_SRC_MASK;
3801                 else
3802                         return -EOPNOTSUPP;
3803
3804                 /* IPv4 destination address */
3805                 if (tcp_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
3806                         new_mask |= I40E_L3_DST_MASK;
3807                 else if (!tcp_ip4_spec->ip4dst)
3808                         new_mask &= ~I40E_L3_DST_MASK;
3809                 else
3810                         return -EOPNOTSUPP;
3811
3812                 /* L4 source port */
3813                 if (tcp_ip4_spec->psrc == htons(0xFFFF))
3814                         new_mask |= I40E_L4_SRC_MASK;
3815                 else if (!tcp_ip4_spec->psrc)
3816                         new_mask &= ~I40E_L4_SRC_MASK;
3817                 else
3818                         return -EOPNOTSUPP;
3819
3820                 /* L4 destination port */
3821                 if (tcp_ip4_spec->pdst == htons(0xFFFF))
3822                         new_mask |= I40E_L4_DST_MASK;
3823                 else if (!tcp_ip4_spec->pdst)
3824                         new_mask &= ~I40E_L4_DST_MASK;
3825                 else
3826                         return -EOPNOTSUPP;
3827
3828                 /* Filtering on Type of Service is not supported. */
3829                 if (tcp_ip4_spec->tos)
3830                         return -EOPNOTSUPP;
3831
3832                 break;
3833         case IP_USER_FLOW:
3834                 usr_ip4_spec = &fsp->m_u.usr_ip4_spec;
3835
3836                 /* IPv4 source address */
3837                 if (usr_ip4_spec->ip4src == htonl(0xFFFFFFFF))
3838                         new_mask |= I40E_L3_SRC_MASK;
3839                 else if (!usr_ip4_spec->ip4src)
3840                         new_mask &= ~I40E_L3_SRC_MASK;
3841                 else
3842                         return -EOPNOTSUPP;
3843
3844                 /* IPv4 destination address */
3845                 if (usr_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
3846                         new_mask |= I40E_L3_DST_MASK;
3847                 else if (!usr_ip4_spec->ip4dst)
3848                         new_mask &= ~I40E_L3_DST_MASK;
3849                 else
3850                         return -EOPNOTSUPP;
3851
3852                 /* First 4 bytes of L4 header */
3853                 if (usr_ip4_spec->l4_4_bytes)
3854                         return -EOPNOTSUPP;
3855
3856                 /* Filtering on Type of Service is not supported. */
3857                 if (usr_ip4_spec->tos)
3858                         return -EOPNOTSUPP;
3859
3860                 /* Filtering on IP version is not supported */
3861                 if (usr_ip4_spec->ip_ver)
3862                         return -EINVAL;
3863
3864                 /* Filtering on L4 protocol is not supported */
3865                 if (usr_ip4_spec->proto)
3866                         return -EINVAL;
3867
3868                 break;
3869         default:
3870                 return -EOPNOTSUPP;
3871         }
3872
3873         /* First, clear all flexible filter entries */
3874         new_mask &= ~I40E_FLEX_INPUT_MASK;
3875
3876         /* If we have a flexible filter, try to add this offset to the correct
3877          * flexible filter PIT list. Once finished, we can update the mask.
3878          * If the src_offset changed, we will get a new mask value which will
3879          * trigger an input set change.
3880          */
3881         if (userdef->flex_filter) {
3882                 struct i40e_flex_pit *l3_flex_pit = NULL, *flex_pit = NULL;
3883
3884                 /* Flexible offset must be even, since the flexible payload
3885                  * must be aligned on 2-byte boundary.
3886                  */
3887                 if (userdef->flex_offset & 0x1) {
3888                         dev_warn(&pf->pdev->dev,
3889                                  "Flexible data offset must be 2-byte aligned\n");
3890                         return -EINVAL;
3891                 }
3892
3893                 src_offset = userdef->flex_offset >> 1;
3894
3895                 /* FLX_PIT source offset value is only so large */
3896                 if (src_offset > I40E_MAX_FLEX_SRC_OFFSET) {
3897                         dev_warn(&pf->pdev->dev,
3898                                  "Flexible data must reside within first 64 bytes of the packet payload\n");
3899                         return -EINVAL;
3900                 }
3901
3902                 /* See if this offset has already been programmed. If we get
3903                  * an ERR_PTR, then the filter is not safe to add. Otherwise,
3904                  * if we get a NULL pointer, this means we will need to add
3905                  * the offset.
3906                  */
3907                 flex_pit = i40e_find_flex_offset(&pf->l4_flex_pit_list,
3908                                                  src_offset);
3909                 if (IS_ERR(flex_pit))
3910                         return PTR_ERR(flex_pit);
3911
3912                 /* IP_USER_FLOW filters match both L4 (ICMP) and L3 (unknown)
3913                  * packet types, and thus we need to program both L3 and L4
3914                  * flexible values. These must have identical flexible index,
3915                  * as otherwise we can't correctly program the input set. So
3916                  * we'll find both an L3 and L4 index and make sure they are
3917                  * the same.
3918                  */
3919                 if (flex_l3) {
3920                         l3_flex_pit =
3921                                 i40e_find_flex_offset(&pf->l3_flex_pit_list,
3922                                                       src_offset);
3923                         if (IS_ERR(l3_flex_pit))
3924                                 return PTR_ERR(l3_flex_pit);
3925
3926                         if (flex_pit) {
3927                                 /* If we already had a matching L4 entry, we
3928                                  * need to make sure that the L3 entry we
3929                                  * obtained uses the same index.
3930                                  */
3931                                 if (l3_flex_pit) {
3932                                         if (l3_flex_pit->pit_index !=
3933                                             flex_pit->pit_index) {
3934                                                 return -EINVAL;
3935                                         }
3936                                 } else {
3937                                         new_flex_offset = true;
3938                                 }
3939                         } else {
3940                                 flex_pit = l3_flex_pit;
3941                         }
3942                 }
3943
3944                 /* If we didn't find an existing flex offset, we need to
3945                  * program a new one. However, we don't immediately program it
3946                  * here because we will wait to program until after we check
3947                  * that it is safe to change the input set.
3948                  */
3949                 if (!flex_pit) {
3950                         new_flex_offset = true;
3951                         pit_index = i40e_unused_pit_index(pf);
3952                 } else {
3953                         pit_index = flex_pit->pit_index;
3954                 }
3955
3956                 /* Update the mask with the new offset */
3957                 new_mask |= i40e_pit_index_to_mask(pit_index);
3958         }
3959
3960         /* If the mask and flexible filter offsets for this filter match the
3961          * currently programmed values we don't need any input set change, so
3962          * this filter is safe to install.
3963          */
3964         if (new_mask == current_mask && !new_flex_offset)
3965                 return 0;
3966
3967         netif_info(pf, drv, vsi->netdev, "Input set change requested for %s flows:\n",
3968                    i40e_flow_str(fsp));
3969         i40e_print_input_set(vsi, current_mask, new_mask);
3970         if (new_flex_offset) {
3971                 netif_info(pf, drv, vsi->netdev, "FLEX index %d: Offset -> %d",
3972                            pit_index, src_offset);
3973         }
3974
3975         /* Hardware input sets are global across multiple ports, so even the
3976          * main port cannot change them when in MFP mode as this would impact
3977          * any filters on the other ports.
3978          */
3979         if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3980                 netif_err(pf, drv, vsi->netdev, "Cannot change Flow Director input sets while MFP is enabled\n");
3981                 return -EOPNOTSUPP;
3982         }
3983
3984         /* This filter requires us to update the input set. However, hardware
3985          * only supports one input set per flow type, and does not support
3986          * separate masks for each filter. This means that we can only support
3987          * a single mask for all filters of a specific type.
3988          *
3989          * If we have preexisting filters, they obviously depend on the
3990          * current programmed input set. Display a diagnostic message in this
3991          * case explaining why the filter could not be accepted.
3992          */
3993         if (*fdir_filter_count) {
3994                 netif_err(pf, drv, vsi->netdev, "Cannot change input set for %s flows until %d preexisting filters are removed\n",
3995                           i40e_flow_str(fsp),
3996                           *fdir_filter_count);
3997                 return -EOPNOTSUPP;
3998         }
3999
4000         i40e_write_fd_input_set(pf, index, new_mask);
4001
4002         /* IP_USER_FLOW filters match both IPv4/Other and IPv4/Fragmented
4003          * frames. If we're programming the input set for IPv4/Other, we also
4004          * need to program the IPv4/Fragmented input set. Since we don't have
4005          * separate support, we'll always assume and enforce that the two flow
4006          * types must have matching input sets.
4007          */
4008         if (index == I40E_FILTER_PCTYPE_NONF_IPV4_OTHER)
4009                 i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
4010                                         new_mask);
4011
4012         /* Add the new offset and update table, if necessary */
4013         if (new_flex_offset) {
4014                 err = i40e_add_flex_offset(&pf->l4_flex_pit_list, src_offset,
4015                                            pit_index);
4016                 if (err)
4017                         return err;
4018
4019                 if (flex_l3) {
4020                         err = i40e_add_flex_offset(&pf->l3_flex_pit_list,
4021                                                    src_offset,
4022                                                    pit_index);
4023                         if (err)
4024                                 return err;
4025                 }
4026
4027                 i40e_reprogram_flex_pit(pf);
4028         }
4029
4030         return 0;
4031 }
4032
4033 /**
4034  * i40e_match_fdir_filter - Return true of two filters match
4035  * @a: pointer to filter struct
4036  * @b: pointer to filter struct
4037  *
4038  * Returns true if the two filters match exactly the same criteria. I.e. they
4039  * match the same flow type and have the same parameters. We don't need to
4040  * check any input-set since all filters of the same flow type must use the
4041  * same input set.
4042  **/
4043 static bool i40e_match_fdir_filter(struct i40e_fdir_filter *a,
4044                                    struct i40e_fdir_filter *b)
4045 {
4046         /* The filters do not much if any of these criteria differ. */
4047         if (a->dst_ip != b->dst_ip ||
4048             a->src_ip != b->src_ip ||
4049             a->dst_port != b->dst_port ||
4050             a->src_port != b->src_port ||
4051             a->flow_type != b->flow_type ||
4052             a->ip4_proto != b->ip4_proto)
4053                 return false;
4054
4055         return true;
4056 }
4057
4058 /**
4059  * i40e_disallow_matching_filters - Check that new filters differ
4060  * @vsi: pointer to the targeted VSI
4061  * @input: new filter to check
4062  *
4063  * Due to hardware limitations, it is not possible for two filters that match
4064  * similar criteria to be programmed at the same time. This is true for a few
4065  * reasons:
4066  *
4067  * (a) all filters matching a particular flow type must use the same input
4068  * set, that is they must match the same criteria.
4069  * (b) different flow types will never match the same packet, as the flow type
4070  * is decided by hardware before checking which rules apply.
4071  * (c) hardware has no way to distinguish which order filters apply in.
4072  *
4073  * Due to this, we can't really support using the location data to order
4074  * filters in the hardware parsing. It is technically possible for the user to
4075  * request two filters matching the same criteria but which select different
4076  * queues. In this case, rather than keep both filters in the list, we reject
4077  * the 2nd filter when the user requests adding it.
4078  *
4079  * This avoids needing to track location for programming the filter to
4080  * hardware, and ensures that we avoid some strange scenarios involving
4081  * deleting filters which match the same criteria.
4082  **/
4083 static int i40e_disallow_matching_filters(struct i40e_vsi *vsi,
4084                                           struct i40e_fdir_filter *input)
4085 {
4086         struct i40e_pf *pf = vsi->back;
4087         struct i40e_fdir_filter *rule;
4088         struct hlist_node *node2;
4089
4090         /* Loop through every filter, and check that it doesn't match */
4091         hlist_for_each_entry_safe(rule, node2,
4092                                   &pf->fdir_filter_list, fdir_node) {
4093                 /* Don't check the filters match if they share the same fd_id,
4094                  * since the new filter is actually just updating the target
4095                  * of the old filter.
4096                  */
4097                 if (rule->fd_id == input->fd_id)
4098                         continue;
4099
4100                 /* If any filters match, then print a warning message to the
4101                  * kernel message buffer and bail out.
4102                  */
4103                 if (i40e_match_fdir_filter(rule, input)) {
4104                         dev_warn(&pf->pdev->dev,
4105                                  "Existing user defined filter %d already matches this flow.\n",
4106                                  rule->fd_id);
4107                         return -EINVAL;
4108                 }
4109         }
4110
4111         return 0;
4112 }
4113
4114 /**
4115  * i40e_add_fdir_ethtool - Add/Remove Flow Director filters
4116  * @vsi: pointer to the targeted VSI
4117  * @cmd: command to get or set RX flow classification rules
4118  *
4119  * Add Flow Director filters for a specific flow spec based on their
4120  * protocol.  Returns 0 if the filters were successfully added.
4121  **/
4122 static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
4123                                  struct ethtool_rxnfc *cmd)
4124 {
4125         struct i40e_rx_flow_userdef userdef;
4126         struct ethtool_rx_flow_spec *fsp;
4127         struct i40e_fdir_filter *input;
4128         u16 dest_vsi = 0, q_index = 0;
4129         struct i40e_pf *pf;
4130         int ret = -EINVAL;
4131         u8 dest_ctl;
4132
4133         if (!vsi)
4134                 return -EINVAL;
4135         pf = vsi->back;
4136
4137         if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
4138                 return -EOPNOTSUPP;
4139
4140         if (test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
4141                 return -ENOSPC;
4142
4143         if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
4144             test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
4145                 return -EBUSY;
4146
4147         if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
4148                 return -EBUSY;
4149
4150         fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
4151
4152         /* Parse the user-defined field */
4153         if (i40e_parse_rx_flow_user_data(fsp, &userdef))
4154                 return -EINVAL;
4155
4156         /* Extended MAC field is not supported */
4157         if (fsp->flow_type & FLOW_MAC_EXT)
4158                 return -EINVAL;
4159
4160         ret = i40e_check_fdir_input_set(vsi, fsp, &userdef);
4161         if (ret)
4162                 return ret;
4163
4164         if (fsp->location >= (pf->hw.func_caps.fd_filters_best_effort +
4165                               pf->hw.func_caps.fd_filters_guaranteed)) {
4166                 return -EINVAL;
4167         }
4168
4169         /* ring_cookie is either the drop index, or is a mask of the queue
4170          * index and VF id we wish to target.
4171          */
4172         if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
4173                 dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
4174         } else {
4175                 u32 ring = ethtool_get_flow_spec_ring(fsp->ring_cookie);
4176                 u8 vf = ethtool_get_flow_spec_ring_vf(fsp->ring_cookie);
4177
4178                 if (!vf) {
4179                         if (ring >= vsi->num_queue_pairs)
4180                                 return -EINVAL;
4181                         dest_vsi = vsi->id;
4182                 } else {
4183                         /* VFs are zero-indexed, so we subtract one here */
4184                         vf--;
4185
4186                         if (vf >= pf->num_alloc_vfs)
4187                                 return -EINVAL;
4188                         if (ring >= pf->vf[vf].num_queue_pairs)
4189                                 return -EINVAL;
4190                         dest_vsi = pf->vf[vf].lan_vsi_id;
4191                 }
4192                 dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX;
4193                 q_index = ring;
4194         }
4195
4196         input = kzalloc(sizeof(*input), GFP_KERNEL);
4197
4198         if (!input)
4199                 return -ENOMEM;
4200
4201         input->fd_id = fsp->location;
4202         input->q_index = q_index;
4203         input->dest_vsi = dest_vsi;
4204         input->dest_ctl = dest_ctl;
4205         input->fd_status = I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID;
4206         input->cnt_index  = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
4207         input->dst_ip = fsp->h_u.tcp_ip4_spec.ip4src;
4208         input->src_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
4209         input->flow_type = fsp->flow_type & ~FLOW_EXT;
4210         input->ip4_proto = fsp->h_u.usr_ip4_spec.proto;
4211
4212         /* Reverse the src and dest notion, since the HW expects them to be from
4213          * Tx perspective where as the input from user is from Rx filter view.
4214          */
4215         input->dst_port = fsp->h_u.tcp_ip4_spec.psrc;
4216         input->src_port = fsp->h_u.tcp_ip4_spec.pdst;
4217         input->dst_ip = fsp->h_u.tcp_ip4_spec.ip4src;
4218         input->src_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
4219
4220         if (userdef.flex_filter) {
4221                 input->flex_filter = true;
4222                 input->flex_word = cpu_to_be16(userdef.flex_word);
4223                 input->flex_offset = userdef.flex_offset;
4224         }
4225
4226         /* Avoid programming two filters with identical match criteria. */
4227         ret = i40e_disallow_matching_filters(vsi, input);
4228         if (ret)
4229                 goto free_filter_memory;
4230
4231         /* Add the input filter to the fdir_input_list, possibly replacing
4232          * a previous filter. Do not free the input structure after adding it
4233          * to the list as this would cause a use-after-free bug.
4234          */
4235         i40e_update_ethtool_fdir_entry(vsi, input, fsp->location, NULL);
4236         ret = i40e_add_del_fdir(vsi, input, true);
4237         if (ret)
4238                 goto remove_sw_rule;
4239         return 0;
4240
4241 remove_sw_rule:
4242         hlist_del(&input->fdir_node);
4243         pf->fdir_pf_active_filters--;
4244 free_filter_memory:
4245         kfree(input);
4246         return ret;
4247 }
4248
4249 /**
4250  * i40e_set_rxnfc - command to set RX flow classification rules
4251  * @netdev: network interface device structure
4252  * @cmd: ethtool rxnfc command
4253  *
4254  * Returns Success if the command is supported.
4255  **/
4256 static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
4257 {
4258         struct i40e_netdev_priv *np = netdev_priv(netdev);
4259         struct i40e_vsi *vsi = np->vsi;
4260         struct i40e_pf *pf = vsi->back;
4261         int ret = -EOPNOTSUPP;
4262
4263         switch (cmd->cmd) {
4264         case ETHTOOL_SRXFH:
4265                 ret = i40e_set_rss_hash_opt(pf, cmd);
4266                 break;
4267         case ETHTOOL_SRXCLSRLINS:
4268                 ret = i40e_add_fdir_ethtool(vsi, cmd);
4269                 break;
4270         case ETHTOOL_SRXCLSRLDEL:
4271                 ret = i40e_del_fdir_entry(vsi, cmd);
4272                 break;
4273         default:
4274                 break;
4275         }
4276
4277         return ret;
4278 }
4279
4280 /**
4281  * i40e_max_channels - get Max number of combined channels supported
4282  * @vsi: vsi pointer
4283  **/
4284 static unsigned int i40e_max_channels(struct i40e_vsi *vsi)
4285 {
4286         /* TODO: This code assumes DCB and FD is disabled for now. */
4287         return vsi->alloc_queue_pairs;
4288 }
4289
4290 /**
4291  * i40e_get_channels - Get the current channels enabled and max supported etc.
4292  * @dev: network interface device structure
4293  * @ch: ethtool channels structure
4294  *
4295  * We don't support separate tx and rx queues as channels. The other count
4296  * represents how many queues are being used for control. max_combined counts
4297  * how many queue pairs we can support. They may not be mapped 1 to 1 with
4298  * q_vectors since we support a lot more queue pairs than q_vectors.
4299  **/
4300 static void i40e_get_channels(struct net_device *dev,
4301                               struct ethtool_channels *ch)
4302 {
4303         struct i40e_netdev_priv *np = netdev_priv(dev);
4304         struct i40e_vsi *vsi = np->vsi;
4305         struct i40e_pf *pf = vsi->back;
4306
4307         /* report maximum channels */
4308         ch->max_combined = i40e_max_channels(vsi);
4309
4310         /* report info for other vector */
4311         ch->other_count = (pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0;
4312         ch->max_other = ch->other_count;
4313
4314         /* Note: This code assumes DCB is disabled for now. */
4315         ch->combined_count = vsi->num_queue_pairs;
4316 }
4317
4318 /**
4319  * i40e_set_channels - Set the new channels count.
4320  * @dev: network interface device structure
4321  * @ch: ethtool channels structure
4322  *
4323  * The new channels count may not be the same as requested by the user
4324  * since it gets rounded down to a power of 2 value.
4325  **/
4326 static int i40e_set_channels(struct net_device *dev,
4327                              struct ethtool_channels *ch)
4328 {
4329         const u8 drop = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
4330         struct i40e_netdev_priv *np = netdev_priv(dev);
4331         unsigned int count = ch->combined_count;
4332         struct i40e_vsi *vsi = np->vsi;
4333         struct i40e_pf *pf = vsi->back;
4334         struct i40e_fdir_filter *rule;
4335         struct hlist_node *node2;
4336         int new_count;
4337         int err = 0;
4338
4339         /* We do not support setting channels for any other VSI at present */
4340         if (vsi->type != I40E_VSI_MAIN)
4341                 return -EINVAL;
4342
4343         /* We do not support setting channels via ethtool when TCs are
4344          * configured through mqprio
4345          */
4346         if (pf->flags & I40E_FLAG_TC_MQPRIO)
4347                 return -EINVAL;
4348
4349         /* verify they are not requesting separate vectors */
4350         if (!count || ch->rx_count || ch->tx_count)
4351                 return -EINVAL;
4352
4353         /* verify other_count has not changed */
4354         if (ch->other_count != ((pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0))
4355                 return -EINVAL;
4356
4357         /* verify the number of channels does not exceed hardware limits */
4358         if (count > i40e_max_channels(vsi))
4359                 return -EINVAL;
4360
4361         /* verify that the number of channels does not invalidate any current
4362          * flow director rules
4363          */
4364         hlist_for_each_entry_safe(rule, node2,
4365                                   &pf->fdir_filter_list, fdir_node) {
4366                 if (rule->dest_ctl != drop && count <= rule->q_index) {
4367                         dev_warn(&pf->pdev->dev,
4368                                  "Existing user defined filter %d assigns flow to queue %d\n",
4369                                  rule->fd_id, rule->q_index);
4370                         err = -EINVAL;
4371                 }
4372         }
4373
4374         if (err) {
4375                 dev_err(&pf->pdev->dev,
4376                         "Existing filter rules must be deleted to reduce combined channel count to %d\n",
4377                         count);
4378                 return err;
4379         }
4380
4381         /* update feature limits from largest to smallest supported values */
4382         /* TODO: Flow director limit, DCB etc */
4383
4384         /* use rss_reconfig to rebuild with new queue count and update traffic
4385          * class queue mapping
4386          */
4387         new_count = i40e_reconfig_rss_queues(pf, count);
4388         if (new_count > 0)
4389                 return 0;
4390         else
4391                 return -EINVAL;
4392 }
4393
4394 /**
4395  * i40e_get_rxfh_key_size - get the RSS hash key size
4396  * @netdev: network interface device structure
4397  *
4398  * Returns the table size.
4399  **/
4400 static u32 i40e_get_rxfh_key_size(struct net_device *netdev)
4401 {
4402         return I40E_HKEY_ARRAY_SIZE;
4403 }
4404
4405 /**
4406  * i40e_get_rxfh_indir_size - get the rx flow hash indirection table size
4407  * @netdev: network interface device structure
4408  *
4409  * Returns the table size.
4410  **/
4411 static u32 i40e_get_rxfh_indir_size(struct net_device *netdev)
4412 {
4413         return I40E_HLUT_ARRAY_SIZE;
4414 }
4415
4416 /**
4417  * i40e_get_rxfh - get the rx flow hash indirection table
4418  * @netdev: network interface device structure
4419  * @indir: indirection table
4420  * @key: hash key
4421  * @hfunc: hash function
4422  *
4423  * Reads the indirection table directly from the hardware. Returns 0 on
4424  * success.
4425  **/
4426 static int i40e_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
4427                          u8 *hfunc)
4428 {
4429         struct i40e_netdev_priv *np = netdev_priv(netdev);
4430         struct i40e_vsi *vsi = np->vsi;
4431         u8 *lut, *seed = NULL;
4432         int ret;
4433         u16 i;
4434
4435         if (hfunc)
4436                 *hfunc = ETH_RSS_HASH_TOP;
4437
4438         if (!indir)
4439                 return 0;
4440
4441         seed = key;
4442         lut = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
4443         if (!lut)
4444                 return -ENOMEM;
4445         ret = i40e_get_rss(vsi, seed, lut, I40E_HLUT_ARRAY_SIZE);
4446         if (ret)
4447                 goto out;
4448         for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
4449                 indir[i] = (u32)(lut[i]);
4450
4451 out:
4452         kfree(lut);
4453
4454         return ret;
4455 }
4456
4457 /**
4458  * i40e_set_rxfh - set the rx flow hash indirection table
4459  * @netdev: network interface device structure
4460  * @indir: indirection table
4461  * @key: hash key
4462  * @hfunc: hash function to use
4463  *
4464  * Returns -EINVAL if the table specifies an invalid queue id, otherwise
4465  * returns 0 after programming the table.
4466  **/
4467 static int i40e_set_rxfh(struct net_device *netdev, const u32 *indir,
4468                          const u8 *key, const u8 hfunc)
4469 {
4470         struct i40e_netdev_priv *np = netdev_priv(netdev);
4471         struct i40e_vsi *vsi = np->vsi;
4472         struct i40e_pf *pf = vsi->back;
4473         u8 *seed = NULL;
4474         u16 i;
4475
4476         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
4477                 return -EOPNOTSUPP;
4478
4479         if (key) {
4480                 if (!vsi->rss_hkey_user) {
4481                         vsi->rss_hkey_user = kzalloc(I40E_HKEY_ARRAY_SIZE,
4482                                                      GFP_KERNEL);
4483                         if (!vsi->rss_hkey_user)
4484                                 return -ENOMEM;
4485                 }
4486                 memcpy(vsi->rss_hkey_user, key, I40E_HKEY_ARRAY_SIZE);
4487                 seed = vsi->rss_hkey_user;
4488         }
4489         if (!vsi->rss_lut_user) {
4490                 vsi->rss_lut_user = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
4491                 if (!vsi->rss_lut_user)
4492                         return -ENOMEM;
4493         }
4494
4495         /* Each 32 bits pointed by 'indir' is stored with a lut entry */
4496         if (indir)
4497                 for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
4498                         vsi->rss_lut_user[i] = (u8)(indir[i]);
4499         else
4500                 i40e_fill_rss_lut(pf, vsi->rss_lut_user, I40E_HLUT_ARRAY_SIZE,
4501                                   vsi->rss_size);
4502
4503         return i40e_config_rss(vsi, seed, vsi->rss_lut_user,
4504                                I40E_HLUT_ARRAY_SIZE);
4505 }
4506
4507 /**
4508  * i40e_get_priv_flags - report device private flags
4509  * @dev: network interface device structure
4510  *
4511  * The get string set count and the string set should be matched for each
4512  * flag returned.  Add new strings for each flag to the i40e_gstrings_priv_flags
4513  * array.
4514  *
4515  * Returns a u32 bitmap of flags.
4516  **/
4517 static u32 i40e_get_priv_flags(struct net_device *dev)
4518 {
4519         struct i40e_netdev_priv *np = netdev_priv(dev);
4520         struct i40e_vsi *vsi = np->vsi;
4521         struct i40e_pf *pf = vsi->back;
4522         u32 i, j, ret_flags = 0;
4523
4524         for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
4525                 const struct i40e_priv_flags *priv_flags;
4526
4527                 priv_flags = &i40e_gstrings_priv_flags[i];
4528
4529                 if (priv_flags->flag & pf->flags)
4530                         ret_flags |= BIT(i);
4531         }
4532
4533         if (pf->hw.pf_id != 0)
4534                 return ret_flags;
4535
4536         for (j = 0; j < I40E_GL_PRIV_FLAGS_STR_LEN; j++) {
4537                 const struct i40e_priv_flags *priv_flags;
4538
4539                 priv_flags = &i40e_gl_gstrings_priv_flags[j];
4540
4541                 if (priv_flags->flag & pf->flags)
4542                         ret_flags |= BIT(i + j);
4543         }
4544
4545         return ret_flags;
4546 }
4547
4548 /**
4549  * i40e_set_priv_flags - set private flags
4550  * @dev: network interface device structure
4551  * @flags: bit flags to be set
4552  **/
4553 static int i40e_set_priv_flags(struct net_device *dev, u32 flags)
4554 {
4555         struct i40e_netdev_priv *np = netdev_priv(dev);
4556         struct i40e_vsi *vsi = np->vsi;
4557         struct i40e_pf *pf = vsi->back;
4558         u64 orig_flags, new_flags, changed_flags;
4559         u32 i, j;
4560
4561         orig_flags = READ_ONCE(pf->flags);
4562         new_flags = orig_flags;
4563
4564         for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
4565                 const struct i40e_priv_flags *priv_flags;
4566
4567                 priv_flags = &i40e_gstrings_priv_flags[i];
4568
4569                 if (flags & BIT(i))
4570                         new_flags |= priv_flags->flag;
4571                 else
4572                         new_flags &= ~(priv_flags->flag);
4573
4574                 /* If this is a read-only flag, it can't be changed */
4575                 if (priv_flags->read_only &&
4576                     ((orig_flags ^ new_flags) & ~BIT(i)))
4577                         return -EOPNOTSUPP;
4578         }
4579
4580         if (pf->hw.pf_id != 0)
4581                 goto flags_complete;
4582
4583         for (j = 0; j < I40E_GL_PRIV_FLAGS_STR_LEN; j++) {
4584                 const struct i40e_priv_flags *priv_flags;
4585
4586                 priv_flags = &i40e_gl_gstrings_priv_flags[j];
4587
4588                 if (flags & BIT(i + j))
4589                         new_flags |= priv_flags->flag;
4590                 else
4591                         new_flags &= ~(priv_flags->flag);
4592
4593                 /* If this is a read-only flag, it can't be changed */
4594                 if (priv_flags->read_only &&
4595                     ((orig_flags ^ new_flags) & ~BIT(i)))
4596                         return -EOPNOTSUPP;
4597         }
4598
4599 flags_complete:
4600         changed_flags = orig_flags ^ new_flags;
4601
4602         /* Before we finalize any flag changes, we need to perform some
4603          * checks to ensure that the changes are supported and safe.
4604          */
4605
4606         /* ATR eviction is not supported on all devices */
4607         if ((new_flags & I40E_FLAG_HW_ATR_EVICT_ENABLED) &&
4608             !(pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE))
4609                 return -EOPNOTSUPP;
4610
4611         /* If the driver detected FW LLDP was disabled on init, this flag could
4612          * be set, however we do not support _changing_ the flag if NPAR is
4613          * enabled or FW API version < 1.7.  There are situations where older
4614          * FW versions/NPAR enabled PFs could disable LLDP, however we _must_
4615          * not allow the user to enable/disable LLDP with this flag on
4616          * unsupported FW versions.
4617          */
4618         if (changed_flags & I40E_FLAG_DISABLE_FW_LLDP) {
4619                 if (!(pf->hw_features & I40E_HW_STOPPABLE_FW_LLDP)) {
4620                         dev_warn(&pf->pdev->dev,
4621                                  "Device does not support changing FW LLDP\n");
4622                         return -EOPNOTSUPP;
4623                 }
4624         }
4625
4626         /* Now that we've checked to ensure that the new flags are valid, load
4627          * them into place. Since we only modify flags either (a) during
4628          * initialization or (b) while holding the RTNL lock, we don't need
4629          * anything fancy here.
4630          */
4631         pf->flags = new_flags;
4632
4633         /* Process any additional changes needed as a result of flag changes.
4634          * The changed_flags value reflects the list of bits that were
4635          * changed in the code above.
4636          */
4637
4638         /* Flush current ATR settings if ATR was disabled */
4639         if ((changed_flags & I40E_FLAG_FD_ATR_ENABLED) &&
4640             !(pf->flags & I40E_FLAG_FD_ATR_ENABLED)) {
4641                 set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
4642                 set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
4643         }
4644
4645         if (changed_flags & I40E_FLAG_TRUE_PROMISC_SUPPORT) {
4646                 u16 sw_flags = 0, valid_flags = 0;
4647                 int ret;
4648
4649                 if (!(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
4650                         sw_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
4651                 valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
4652                 ret = i40e_aq_set_switch_config(&pf->hw, sw_flags, valid_flags,
4653                                                 0, NULL);
4654                 if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
4655                         dev_info(&pf->pdev->dev,
4656                                  "couldn't set switch config bits, err %s aq_err %s\n",
4657                                  i40e_stat_str(&pf->hw, ret),
4658                                  i40e_aq_str(&pf->hw,
4659                                              pf->hw.aq.asq_last_status));
4660                         /* not a fatal problem, just keep going */
4661                 }
4662         }
4663
4664         if ((changed_flags & pf->flags &
4665              I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED) &&
4666             (pf->flags & I40E_FLAG_MFP_ENABLED))
4667                 dev_warn(&pf->pdev->dev,
4668                          "Turning on link-down-on-close flag may affect other partitions\n");
4669
4670         if (changed_flags & I40E_FLAG_DISABLE_FW_LLDP) {
4671                 if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
4672                         struct i40e_dcbx_config *dcbcfg;
4673
4674                         i40e_aq_stop_lldp(&pf->hw, true, NULL);
4675                         i40e_aq_set_dcb_parameters(&pf->hw, true, NULL);
4676                         /* reset local_dcbx_config to default */
4677                         dcbcfg = &pf->hw.local_dcbx_config;
4678                         dcbcfg->etscfg.willing = 1;
4679                         dcbcfg->etscfg.maxtcs = 0;
4680                         dcbcfg->etscfg.tcbwtable[0] = 100;
4681                         for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++)
4682                                 dcbcfg->etscfg.tcbwtable[i] = 0;
4683                         for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
4684                                 dcbcfg->etscfg.prioritytable[i] = 0;
4685                         dcbcfg->etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
4686                         dcbcfg->pfc.willing = 1;
4687                         dcbcfg->pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
4688                 } else {
4689                         i40e_aq_start_lldp(&pf->hw, NULL);
4690                 }
4691         }
4692
4693         /* Issue reset to cause things to take effect, as additional bits
4694          * are added we will need to create a mask of bits requiring reset
4695          */
4696         if (changed_flags & (I40E_FLAG_VEB_STATS_ENABLED |
4697                              I40E_FLAG_LEGACY_RX |
4698                              I40E_FLAG_SOURCE_PRUNING_DISABLED |
4699                              I40E_FLAG_DISABLE_FW_LLDP))
4700                 i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
4701
4702         return 0;
4703 }
4704
4705 /**
4706  * i40e_get_module_info - get (Q)SFP+ module type info
4707  * @netdev: network interface device structure
4708  * @modinfo: module EEPROM size and layout information structure
4709  **/
4710 static int i40e_get_module_info(struct net_device *netdev,
4711                                 struct ethtool_modinfo *modinfo)
4712 {
4713         struct i40e_netdev_priv *np = netdev_priv(netdev);
4714         struct i40e_vsi *vsi = np->vsi;
4715         struct i40e_pf *pf = vsi->back;
4716         struct i40e_hw *hw = &pf->hw;
4717         u32 sff8472_comp = 0;
4718         u32 sff8472_swap = 0;
4719         u32 sff8636_rev = 0;
4720         i40e_status status;
4721         u32 type = 0;
4722
4723         /* Check if firmware supports reading module EEPROM. */
4724         if (!(hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE)) {
4725                 netdev_err(vsi->netdev, "Module EEPROM memory read not supported. Please update the NVM image.\n");
4726                 return -EINVAL;
4727         }
4728
4729         status = i40e_update_link_info(hw);
4730         if (status)
4731                 return -EIO;
4732
4733         if (hw->phy.link_info.phy_type == I40E_PHY_TYPE_EMPTY) {
4734                 netdev_err(vsi->netdev, "Cannot read module EEPROM memory. No module connected.\n");
4735                 return -EINVAL;
4736         }
4737
4738         type = hw->phy.link_info.module_type[0];
4739
4740         switch (type) {
4741         case I40E_MODULE_TYPE_SFP:
4742                 status = i40e_aq_get_phy_register(hw,
4743                                 I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
4744                                 I40E_I2C_EEPROM_DEV_ADDR,
4745                                 I40E_MODULE_SFF_8472_COMP,
4746                                 &sff8472_comp, NULL);
4747                 if (status)
4748                         return -EIO;
4749
4750                 status = i40e_aq_get_phy_register(hw,
4751                                 I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
4752                                 I40E_I2C_EEPROM_DEV_ADDR,
4753                                 I40E_MODULE_SFF_8472_SWAP,
4754                                 &sff8472_swap, NULL);
4755                 if (status)
4756                         return -EIO;
4757
4758                 /* Check if the module requires address swap to access
4759                  * the other EEPROM memory page.
4760                  */
4761                 if (sff8472_swap & I40E_MODULE_SFF_ADDR_MODE) {
4762                         netdev_warn(vsi->netdev, "Module address swap to access page 0xA2 is not supported.\n");
4763                         modinfo->type = ETH_MODULE_SFF_8079;
4764                         modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4765                 } else if (sff8472_comp == 0x00) {
4766                         /* Module is not SFF-8472 compliant */
4767                         modinfo->type = ETH_MODULE_SFF_8079;
4768                         modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4769                 } else {
4770                         modinfo->type = ETH_MODULE_SFF_8472;
4771                         modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
4772                 }
4773                 break;
4774         case I40E_MODULE_TYPE_QSFP_PLUS:
4775                 /* Read from memory page 0. */
4776                 status = i40e_aq_get_phy_register(hw,
4777                                 I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
4778                                 0,
4779                                 I40E_MODULE_REVISION_ADDR,
4780                                 &sff8636_rev, NULL);
4781                 if (status)
4782                         return -EIO;
4783                 /* Determine revision compliance byte */
4784                 if (sff8636_rev > 0x02) {
4785                         /* Module is SFF-8636 compliant */
4786                         modinfo->type = ETH_MODULE_SFF_8636;
4787                         modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
4788                 } else {
4789                         modinfo->type = ETH_MODULE_SFF_8436;
4790                         modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
4791                 }
4792                 break;
4793         case I40E_MODULE_TYPE_QSFP28:
4794                 modinfo->type = ETH_MODULE_SFF_8636;
4795                 modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
4796                 break;
4797         default:
4798                 netdev_err(vsi->netdev, "Module type unrecognized\n");
4799                 return -EINVAL;
4800         }
4801         return 0;
4802 }
4803
4804 /**
4805  * i40e_get_module_eeprom - fills buffer with (Q)SFP+ module memory contents
4806  * @netdev: network interface device structure
4807  * @ee: EEPROM dump request structure
4808  * @data: buffer to be filled with EEPROM contents
4809  **/
4810 static int i40e_get_module_eeprom(struct net_device *netdev,
4811                                   struct ethtool_eeprom *ee,
4812                                   u8 *data)
4813 {
4814         struct i40e_netdev_priv *np = netdev_priv(netdev);
4815         struct i40e_vsi *vsi = np->vsi;
4816         struct i40e_pf *pf = vsi->back;
4817         struct i40e_hw *hw = &pf->hw;
4818         bool is_sfp = false;
4819         i40e_status status;
4820         u32 value = 0;
4821         int i;
4822
4823         if (!ee || !ee->len || !data)
4824                 return -EINVAL;
4825
4826         if (hw->phy.link_info.module_type[0] == I40E_MODULE_TYPE_SFP)
4827                 is_sfp = true;
4828
4829         for (i = 0; i < ee->len; i++) {
4830                 u32 offset = i + ee->offset;
4831                 u32 addr = is_sfp ? I40E_I2C_EEPROM_DEV_ADDR : 0;
4832
4833                 /* Check if we need to access the other memory page */
4834                 if (is_sfp) {
4835                         if (offset >= ETH_MODULE_SFF_8079_LEN) {
4836                                 offset -= ETH_MODULE_SFF_8079_LEN;
4837                                 addr = I40E_I2C_EEPROM_DEV_ADDR2;
4838                         }
4839                 } else {
4840                         while (offset >= ETH_MODULE_SFF_8436_LEN) {
4841                                 /* Compute memory page number and offset. */
4842                                 offset -= ETH_MODULE_SFF_8436_LEN / 2;
4843                                 addr++;
4844                         }
4845                 }
4846
4847                 status = i40e_aq_get_phy_register(hw,
4848                                 I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
4849                                 addr, offset, &value, NULL);
4850                 if (status)
4851                         return -EIO;
4852                 data[i] = value;
4853         }
4854         return 0;
4855 }
4856
4857 static const struct ethtool_ops i40e_ethtool_ops = {
4858         .get_drvinfo            = i40e_get_drvinfo,
4859         .get_regs_len           = i40e_get_regs_len,
4860         .get_regs               = i40e_get_regs,
4861         .nway_reset             = i40e_nway_reset,
4862         .get_link               = ethtool_op_get_link,
4863         .get_wol                = i40e_get_wol,
4864         .set_wol                = i40e_set_wol,
4865         .set_eeprom             = i40e_set_eeprom,
4866         .get_eeprom_len         = i40e_get_eeprom_len,
4867         .get_eeprom             = i40e_get_eeprom,
4868         .get_ringparam          = i40e_get_ringparam,
4869         .set_ringparam          = i40e_set_ringparam,
4870         .get_pauseparam         = i40e_get_pauseparam,
4871         .set_pauseparam         = i40e_set_pauseparam,
4872         .get_msglevel           = i40e_get_msglevel,
4873         .set_msglevel           = i40e_set_msglevel,
4874         .get_rxnfc              = i40e_get_rxnfc,
4875         .set_rxnfc              = i40e_set_rxnfc,
4876         .self_test              = i40e_diag_test,
4877         .get_strings            = i40e_get_strings,
4878         .set_phys_id            = i40e_set_phys_id,
4879         .get_sset_count         = i40e_get_sset_count,
4880         .get_ethtool_stats      = i40e_get_ethtool_stats,
4881         .get_coalesce           = i40e_get_coalesce,
4882         .set_coalesce           = i40e_set_coalesce,
4883         .get_rxfh_key_size      = i40e_get_rxfh_key_size,
4884         .get_rxfh_indir_size    = i40e_get_rxfh_indir_size,
4885         .get_rxfh               = i40e_get_rxfh,
4886         .set_rxfh               = i40e_set_rxfh,
4887         .get_channels           = i40e_get_channels,
4888         .set_channels           = i40e_set_channels,
4889         .get_module_info        = i40e_get_module_info,
4890         .get_module_eeprom      = i40e_get_module_eeprom,
4891         .get_ts_info            = i40e_get_ts_info,
4892         .get_priv_flags         = i40e_get_priv_flags,
4893         .set_priv_flags         = i40e_set_priv_flags,
4894         .get_per_queue_coalesce = i40e_get_per_queue_coalesce,
4895         .set_per_queue_coalesce = i40e_set_per_queue_coalesce,
4896         .get_link_ksettings     = i40e_get_link_ksettings,
4897         .set_link_ksettings     = i40e_set_link_ksettings,
4898 };
4899
4900 void i40e_set_ethtool_ops(struct net_device *netdev)
4901 {
4902         netdev->ethtool_ops = &i40e_ethtool_ops;
4903 }