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