Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / ice / ice_ethtool.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3
4 /* ethtool support for ice */
5
6 #include "ice.h"
7
8 struct ice_stats {
9         char stat_string[ETH_GSTRING_LEN];
10         int sizeof_stat;
11         int stat_offset;
12 };
13
14 #define ICE_STAT(_type, _name, _stat) { \
15         .stat_string = _name, \
16         .sizeof_stat = FIELD_SIZEOF(_type, _stat), \
17         .stat_offset = offsetof(_type, _stat) \
18 }
19
20 #define ICE_VSI_STAT(_name, _stat) \
21                 ICE_STAT(struct ice_vsi, _name, _stat)
22 #define ICE_PF_STAT(_name, _stat) \
23                 ICE_STAT(struct ice_pf, _name, _stat)
24
25 static int ice_q_stats_len(struct net_device *netdev)
26 {
27         struct ice_netdev_priv *np = netdev_priv(netdev);
28
29         return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) *
30                 (sizeof(struct ice_q_stats) / sizeof(u64)));
31 }
32
33 #define ICE_PF_STATS_LEN        ARRAY_SIZE(ice_gstrings_pf_stats)
34 #define ICE_VSI_STATS_LEN       ARRAY_SIZE(ice_gstrings_vsi_stats)
35
36 #define ICE_ALL_STATS_LEN(n)    (ICE_PF_STATS_LEN + ICE_VSI_STATS_LEN + \
37                                  ice_q_stats_len(n))
38
39 static const struct ice_stats ice_gstrings_vsi_stats[] = {
40         ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
41         ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
42         ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
43         ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
44         ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
45         ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
46         ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes),
47         ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes),
48         ICE_VSI_STAT("rx_discards", eth_stats.rx_discards),
49         ICE_VSI_STAT("tx_errors", eth_stats.tx_errors),
50         ICE_VSI_STAT("tx_linearize", tx_linearize),
51         ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
52         ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed),
53         ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
54 };
55
56 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
57  * but they aren't. This device is capable of supporting multiple
58  * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual
59  * netdevs whereas the PF_STATs are for the physical function that's
60  * hosting these netdevs.
61  *
62  * The PF_STATs are appended to the netdev stats only when ethtool -S
63  * is queried on the base PF netdev.
64  */
65 static const struct ice_stats ice_gstrings_pf_stats[] = {
66         ICE_PF_STAT("tx_bytes", stats.eth.tx_bytes),
67         ICE_PF_STAT("rx_bytes", stats.eth.rx_bytes),
68         ICE_PF_STAT("tx_unicast", stats.eth.tx_unicast),
69         ICE_PF_STAT("rx_unicast", stats.eth.rx_unicast),
70         ICE_PF_STAT("tx_multicast", stats.eth.tx_multicast),
71         ICE_PF_STAT("rx_multicast", stats.eth.rx_multicast),
72         ICE_PF_STAT("tx_broadcast", stats.eth.tx_broadcast),
73         ICE_PF_STAT("rx_broadcast", stats.eth.rx_broadcast),
74         ICE_PF_STAT("tx_errors", stats.eth.tx_errors),
75         ICE_PF_STAT("tx_size_64", stats.tx_size_64),
76         ICE_PF_STAT("rx_size_64", stats.rx_size_64),
77         ICE_PF_STAT("tx_size_127", stats.tx_size_127),
78         ICE_PF_STAT("rx_size_127", stats.rx_size_127),
79         ICE_PF_STAT("tx_size_255", stats.tx_size_255),
80         ICE_PF_STAT("rx_size_255", stats.rx_size_255),
81         ICE_PF_STAT("tx_size_511", stats.tx_size_511),
82         ICE_PF_STAT("rx_size_511", stats.rx_size_511),
83         ICE_PF_STAT("tx_size_1023", stats.tx_size_1023),
84         ICE_PF_STAT("rx_size_1023", stats.rx_size_1023),
85         ICE_PF_STAT("tx_size_1522", stats.tx_size_1522),
86         ICE_PF_STAT("rx_size_1522", stats.rx_size_1522),
87         ICE_PF_STAT("tx_size_big", stats.tx_size_big),
88         ICE_PF_STAT("rx_size_big", stats.rx_size_big),
89         ICE_PF_STAT("link_xon_tx", stats.link_xon_tx),
90         ICE_PF_STAT("link_xon_rx", stats.link_xon_rx),
91         ICE_PF_STAT("link_xoff_tx", stats.link_xoff_tx),
92         ICE_PF_STAT("link_xoff_rx", stats.link_xoff_rx),
93         ICE_PF_STAT("tx_dropped_link_down", stats.tx_dropped_link_down),
94         ICE_PF_STAT("rx_undersize", stats.rx_undersize),
95         ICE_PF_STAT("rx_fragments", stats.rx_fragments),
96         ICE_PF_STAT("rx_oversize", stats.rx_oversize),
97         ICE_PF_STAT("rx_jabber", stats.rx_jabber),
98         ICE_PF_STAT("rx_csum_bad", hw_csum_rx_error),
99         ICE_PF_STAT("rx_length_errors", stats.rx_len_errors),
100         ICE_PF_STAT("rx_dropped", stats.eth.rx_discards),
101         ICE_PF_STAT("rx_crc_errors", stats.crc_errors),
102         ICE_PF_STAT("illegal_bytes", stats.illegal_bytes),
103         ICE_PF_STAT("mac_local_faults", stats.mac_local_faults),
104         ICE_PF_STAT("mac_remote_faults", stats.mac_remote_faults),
105 };
106
107 static const u32 ice_regs_dump_list[] = {
108         PFGEN_STATE,
109         PRTGEN_STATUS,
110         QRX_CTRL(0),
111         QINT_TQCTL(0),
112         QINT_RQCTL(0),
113         PFINT_OICR_ENA,
114         QRX_ITR(0),
115 };
116
117 /**
118  * ice_nvm_version_str - format the NVM version strings
119  * @hw: ptr to the hardware info
120  */
121 static char *ice_nvm_version_str(struct ice_hw *hw)
122 {
123         static char buf[ICE_ETHTOOL_FWVER_LEN];
124         u8 ver, patch;
125         u32 full_ver;
126         u16 build;
127
128         full_ver = hw->nvm.oem_ver;
129         ver = (u8)((full_ver & ICE_OEM_VER_MASK) >> ICE_OEM_VER_SHIFT);
130         build = (u16)((full_ver & ICE_OEM_VER_BUILD_MASK) >>
131                       ICE_OEM_VER_BUILD_SHIFT);
132         patch = (u8)(full_ver & ICE_OEM_VER_PATCH_MASK);
133
134         snprintf(buf, sizeof(buf), "%x.%02x 0x%x %d.%d.%d",
135                  (hw->nvm.ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT,
136                  (hw->nvm.ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT,
137                  hw->nvm.eetrack, ver, build, patch);
138
139         return buf;
140 }
141
142 static void
143 ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
144 {
145         struct ice_netdev_priv *np = netdev_priv(netdev);
146         struct ice_vsi *vsi = np->vsi;
147         struct ice_pf *pf = vsi->back;
148
149         strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
150         strlcpy(drvinfo->version, ice_drv_ver, sizeof(drvinfo->version));
151         strlcpy(drvinfo->fw_version, ice_nvm_version_str(&pf->hw),
152                 sizeof(drvinfo->fw_version));
153         strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
154                 sizeof(drvinfo->bus_info));
155 }
156
157 static int ice_get_regs_len(struct net_device __always_unused *netdev)
158 {
159         return sizeof(ice_regs_dump_list);
160 }
161
162 static void
163 ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
164 {
165         struct ice_netdev_priv *np = netdev_priv(netdev);
166         struct ice_pf *pf = np->vsi->back;
167         struct ice_hw *hw = &pf->hw;
168         u32 *regs_buf = (u32 *)p;
169         int i;
170
171         regs->version = 1;
172
173         for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i)
174                 regs_buf[i] = rd32(hw, ice_regs_dump_list[i]);
175 }
176
177 static u32 ice_get_msglevel(struct net_device *netdev)
178 {
179         struct ice_netdev_priv *np = netdev_priv(netdev);
180         struct ice_pf *pf = np->vsi->back;
181
182 #ifndef CONFIG_DYNAMIC_DEBUG
183         if (pf->hw.debug_mask)
184                 netdev_info(netdev, "hw debug_mask: 0x%llX\n",
185                             pf->hw.debug_mask);
186 #endif /* !CONFIG_DYNAMIC_DEBUG */
187
188         return pf->msg_enable;
189 }
190
191 static void ice_set_msglevel(struct net_device *netdev, u32 data)
192 {
193         struct ice_netdev_priv *np = netdev_priv(netdev);
194         struct ice_pf *pf = np->vsi->back;
195
196 #ifndef CONFIG_DYNAMIC_DEBUG
197         if (ICE_DBG_USER & data)
198                 pf->hw.debug_mask = data;
199         else
200                 pf->msg_enable = data;
201 #else
202         pf->msg_enable = data;
203 #endif /* !CONFIG_DYNAMIC_DEBUG */
204 }
205
206 static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
207 {
208         struct ice_netdev_priv *np = netdev_priv(netdev);
209         struct ice_vsi *vsi = np->vsi;
210         char *p = (char *)data;
211         unsigned int i;
212
213         switch (stringset) {
214         case ETH_SS_STATS:
215                 for (i = 0; i < ICE_VSI_STATS_LEN; i++) {
216                         snprintf(p, ETH_GSTRING_LEN, "%s",
217                                  ice_gstrings_vsi_stats[i].stat_string);
218                         p += ETH_GSTRING_LEN;
219                 }
220
221                 ice_for_each_alloc_txq(vsi, i) {
222                         snprintf(p, ETH_GSTRING_LEN,
223                                  "tx-queue-%u.tx_packets", i);
224                         p += ETH_GSTRING_LEN;
225                         snprintf(p, ETH_GSTRING_LEN, "tx-queue-%u.tx_bytes", i);
226                         p += ETH_GSTRING_LEN;
227                 }
228
229                 ice_for_each_alloc_rxq(vsi, i) {
230                         snprintf(p, ETH_GSTRING_LEN,
231                                  "rx-queue-%u.rx_packets", i);
232                         p += ETH_GSTRING_LEN;
233                         snprintf(p, ETH_GSTRING_LEN, "rx-queue-%u.rx_bytes", i);
234                         p += ETH_GSTRING_LEN;
235                 }
236
237                 if (vsi->type != ICE_VSI_PF)
238                         return;
239
240                 for (i = 0; i < ICE_PF_STATS_LEN; i++) {
241                         snprintf(p, ETH_GSTRING_LEN, "port.%s",
242                                  ice_gstrings_pf_stats[i].stat_string);
243                         p += ETH_GSTRING_LEN;
244                 }
245
246                 break;
247         default:
248                 break;
249         }
250 }
251
252 static int ice_get_sset_count(struct net_device *netdev, int sset)
253 {
254         switch (sset) {
255         case ETH_SS_STATS:
256                 /* The number (and order) of strings reported *must* remain
257                  * constant for a given netdevice. This function must not
258                  * report a different number based on run time parameters
259                  * (such as the number of queues in use, or the setting of
260                  * a private ethtool flag). This is due to the nature of the
261                  * ethtool stats API.
262                  *
263                  * Userspace programs such as ethtool must make 3 separate
264                  * ioctl requests, one for size, one for the strings, and
265                  * finally one for the stats. Since these cross into
266                  * userspace, changes to the number or size could result in
267                  * undefined memory access or incorrect string<->value
268                  * correlations for statistics.
269                  *
270                  * Even if it appears to be safe, changes to the size or
271                  * order of strings will suffer from race conditions and are
272                  * not safe.
273                  */
274                 return ICE_ALL_STATS_LEN(netdev);
275         default:
276                 return -EOPNOTSUPP;
277         }
278 }
279
280 static void
281 ice_get_ethtool_stats(struct net_device *netdev,
282                       struct ethtool_stats __always_unused *stats, u64 *data)
283 {
284         struct ice_netdev_priv *np = netdev_priv(netdev);
285         struct ice_vsi *vsi = np->vsi;
286         struct ice_pf *pf = vsi->back;
287         struct ice_ring *ring;
288         unsigned int j = 0;
289         int i = 0;
290         char *p;
291
292         for (j = 0; j < ICE_VSI_STATS_LEN; j++) {
293                 p = (char *)vsi + ice_gstrings_vsi_stats[j].stat_offset;
294                 data[i++] = (ice_gstrings_vsi_stats[j].sizeof_stat ==
295                             sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
296         }
297
298         /* populate per queue stats */
299         rcu_read_lock();
300
301         ice_for_each_alloc_txq(vsi, j) {
302                 ring = READ_ONCE(vsi->tx_rings[j]);
303                 if (ring) {
304                         data[i++] = ring->stats.pkts;
305                         data[i++] = ring->stats.bytes;
306                 } else {
307                         data[i++] = 0;
308                         data[i++] = 0;
309                 }
310         }
311
312         ice_for_each_alloc_rxq(vsi, j) {
313                 ring = READ_ONCE(vsi->rx_rings[j]);
314                 if (ring) {
315                         data[i++] = ring->stats.pkts;
316                         data[i++] = ring->stats.bytes;
317                 } else {
318                         data[i++] = 0;
319                         data[i++] = 0;
320                 }
321         }
322
323         rcu_read_unlock();
324
325         if (vsi->type != ICE_VSI_PF)
326                 return;
327
328         for (j = 0; j < ICE_PF_STATS_LEN; j++) {
329                 p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset;
330                 data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat ==
331                              sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
332         }
333 }
334
335 /**
336  * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes
337  * @netdev: network interface device structure
338  * @ks: ethtool link ksettings struct to fill out
339  */
340 static void ice_phy_type_to_ethtool(struct net_device *netdev,
341                                     struct ethtool_link_ksettings *ks)
342 {
343         struct ice_netdev_priv *np = netdev_priv(netdev);
344         struct ice_link_status *hw_link_info;
345         struct ice_vsi *vsi = np->vsi;
346         u64 phy_types_low;
347
348         hw_link_info = &vsi->port_info->phy.link_info;
349         phy_types_low = vsi->port_info->phy.phy_type_low;
350
351         ethtool_link_ksettings_zero_link_mode(ks, supported);
352         ethtool_link_ksettings_zero_link_mode(ks, advertising);
353
354         if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
355             phy_types_low & ICE_PHY_TYPE_LOW_100M_SGMII) {
356                 ethtool_link_ksettings_add_link_mode(ks, supported,
357                                                      100baseT_Full);
358                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100MB)
359                         ethtool_link_ksettings_add_link_mode(ks, advertising,
360                                                              100baseT_Full);
361         }
362         if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
363             phy_types_low & ICE_PHY_TYPE_LOW_1G_SGMII) {
364                 ethtool_link_ksettings_add_link_mode(ks, supported,
365                                                      1000baseT_Full);
366                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
367                         ethtool_link_ksettings_add_link_mode(ks, advertising,
368                                                              1000baseT_Full);
369         }
370         if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX) {
371                 ethtool_link_ksettings_add_link_mode(ks, supported,
372                                                      1000baseKX_Full);
373                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
374                         ethtool_link_ksettings_add_link_mode(ks, advertising,
375                                                              1000baseKX_Full);
376         }
377         if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_SX ||
378             phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_LX) {
379                 ethtool_link_ksettings_add_link_mode(ks, supported,
380                                                      1000baseX_Full);
381                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB)
382                         ethtool_link_ksettings_add_link_mode(ks, advertising,
383                                                              1000baseX_Full);
384         }
385         if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T) {
386                 ethtool_link_ksettings_add_link_mode(ks, supported,
387                                                      2500baseT_Full);
388                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
389                         ethtool_link_ksettings_add_link_mode(ks, advertising,
390                                                              2500baseT_Full);
391         }
392         if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_X ||
393             phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX) {
394                 ethtool_link_ksettings_add_link_mode(ks, supported,
395                                                      2500baseX_Full);
396                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB)
397                         ethtool_link_ksettings_add_link_mode(ks, advertising,
398                                                              2500baseX_Full);
399         }
400         if (phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
401             phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR) {
402                 ethtool_link_ksettings_add_link_mode(ks, supported,
403                                                      5000baseT_Full);
404                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_5GB)
405                         ethtool_link_ksettings_add_link_mode(ks, advertising,
406                                                              5000baseT_Full);
407         }
408         if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
409             phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_DA ||
410             phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC ||
411             phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_C2C) {
412                 ethtool_link_ksettings_add_link_mode(ks, supported,
413                                                      10000baseT_Full);
414                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
415                         ethtool_link_ksettings_add_link_mode(ks, advertising,
416                                                              10000baseT_Full);
417         }
418         if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1) {
419                 ethtool_link_ksettings_add_link_mode(ks, supported,
420                                                      10000baseKR_Full);
421                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
422                         ethtool_link_ksettings_add_link_mode(ks, advertising,
423                                                              10000baseKR_Full);
424         }
425         if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_SR) {
426                 ethtool_link_ksettings_add_link_mode(ks, supported,
427                                                      10000baseSR_Full);
428                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
429                         ethtool_link_ksettings_add_link_mode(ks, advertising,
430                                                              10000baseSR_Full);
431         }
432         if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_LR) {
433                 ethtool_link_ksettings_add_link_mode(ks, supported,
434                                                      10000baseLR_Full);
435                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB)
436                         ethtool_link_ksettings_add_link_mode(ks, advertising,
437                                                              10000baseLR_Full);
438         }
439         if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
440             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
441             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
442             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
443             phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC ||
444             phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_C2C) {
445                 ethtool_link_ksettings_add_link_mode(ks, supported,
446                                                      25000baseCR_Full);
447                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
448                         ethtool_link_ksettings_add_link_mode(ks, advertising,
449                                                              25000baseCR_Full);
450         }
451         if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_SR ||
452             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_LR) {
453                 ethtool_link_ksettings_add_link_mode(ks, supported,
454                                                      25000baseSR_Full);
455                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
456                         ethtool_link_ksettings_add_link_mode(ks, advertising,
457                                                              25000baseSR_Full);
458         }
459         if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
460             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
461             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1) {
462                 ethtool_link_ksettings_add_link_mode(ks, supported,
463                                                      25000baseKR_Full);
464                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB)
465                         ethtool_link_ksettings_add_link_mode(ks, advertising,
466                                                              25000baseKR_Full);
467         }
468         if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
469                 ethtool_link_ksettings_add_link_mode(ks, supported,
470                                                      40000baseKR4_Full);
471                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
472                         ethtool_link_ksettings_add_link_mode(ks, advertising,
473                                                              40000baseKR4_Full);
474         }
475         if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
476             phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC ||
477             phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI) {
478                 ethtool_link_ksettings_add_link_mode(ks, supported,
479                                                      40000baseCR4_Full);
480                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
481                         ethtool_link_ksettings_add_link_mode(ks, advertising,
482                                                              40000baseCR4_Full);
483         }
484         if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_SR4) {
485                 ethtool_link_ksettings_add_link_mode(ks, supported,
486                                                      40000baseSR4_Full);
487                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
488                         ethtool_link_ksettings_add_link_mode(ks, advertising,
489                                                              40000baseSR4_Full);
490         }
491         if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_LR4) {
492                 ethtool_link_ksettings_add_link_mode(ks, supported,
493                                                      40000baseLR4_Full);
494                 if (hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB)
495                         ethtool_link_ksettings_add_link_mode(ks, advertising,
496                                                              40000baseLR4_Full);
497         }
498
499         /* Autoneg PHY types */
500         if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX ||
501             phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T ||
502             phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX ||
503             phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T ||
504             phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX ||
505             phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T ||
506             phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR ||
507             phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T ||
508             phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 ||
509             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T ||
510             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR ||
511             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S ||
512             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 ||
513             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR ||
514             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S ||
515             phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1 ||
516             phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 ||
517             phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) {
518                 ethtool_link_ksettings_add_link_mode(ks, supported,
519                                                      Autoneg);
520                 ethtool_link_ksettings_add_link_mode(ks, advertising,
521                                                      Autoneg);
522         }
523 }
524
525 #define TEST_SET_BITS_TIMEOUT   50
526 #define TEST_SET_BITS_SLEEP_MAX 2000
527 #define TEST_SET_BITS_SLEEP_MIN 1000
528
529 /**
530  * ice_get_settings_link_up - Get Link settings for when link is up
531  * @ks: ethtool ksettings to fill in
532  * @netdev: network interface device structure
533  */
534 static void ice_get_settings_link_up(struct ethtool_link_ksettings *ks,
535                                      struct net_device *netdev)
536 {
537         struct ice_netdev_priv *np = netdev_priv(netdev);
538         struct ethtool_link_ksettings cap_ksettings;
539         struct ice_link_status *link_info;
540         struct ice_vsi *vsi = np->vsi;
541         bool unrecog_phy_low = false;
542
543         link_info = &vsi->port_info->phy.link_info;
544
545         /* Initialize supported and advertised settings based on phy settings */
546         switch (link_info->phy_type_low) {
547         case ICE_PHY_TYPE_LOW_100BASE_TX:
548                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
549                 ethtool_link_ksettings_add_link_mode(ks, supported,
550                                                      100baseT_Full);
551                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
552                 ethtool_link_ksettings_add_link_mode(ks, advertising,
553                                                      100baseT_Full);
554                 break;
555         case ICE_PHY_TYPE_LOW_100M_SGMII:
556                 ethtool_link_ksettings_add_link_mode(ks, supported,
557                                                      100baseT_Full);
558                 break;
559         case ICE_PHY_TYPE_LOW_1000BASE_T:
560                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
561                 ethtool_link_ksettings_add_link_mode(ks, supported,
562                                                      1000baseT_Full);
563                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
564                 ethtool_link_ksettings_add_link_mode(ks, advertising,
565                                                      1000baseT_Full);
566                 break;
567         case ICE_PHY_TYPE_LOW_1G_SGMII:
568                 ethtool_link_ksettings_add_link_mode(ks, supported,
569                                                      1000baseT_Full);
570                 break;
571         case ICE_PHY_TYPE_LOW_1000BASE_SX:
572         case ICE_PHY_TYPE_LOW_1000BASE_LX:
573                 ethtool_link_ksettings_add_link_mode(ks, supported,
574                                                      1000baseX_Full);
575                 break;
576         case ICE_PHY_TYPE_LOW_1000BASE_KX:
577                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
578                 ethtool_link_ksettings_add_link_mode(ks, supported,
579                                                      1000baseKX_Full);
580                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
581                 ethtool_link_ksettings_add_link_mode(ks, advertising,
582                                                      1000baseKX_Full);
583                 break;
584         case ICE_PHY_TYPE_LOW_2500BASE_T:
585                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
586                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
587                 ethtool_link_ksettings_add_link_mode(ks, supported,
588                                                      2500baseT_Full);
589                 ethtool_link_ksettings_add_link_mode(ks, advertising,
590                                                      2500baseT_Full);
591                 break;
592         case ICE_PHY_TYPE_LOW_2500BASE_X:
593                 ethtool_link_ksettings_add_link_mode(ks, supported,
594                                                      2500baseX_Full);
595                 break;
596         case ICE_PHY_TYPE_LOW_2500BASE_KX:
597                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
598                 ethtool_link_ksettings_add_link_mode(ks, supported,
599                                                      2500baseX_Full);
600                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
601                 ethtool_link_ksettings_add_link_mode(ks, advertising,
602                                                      2500baseX_Full);
603                 break;
604         case ICE_PHY_TYPE_LOW_5GBASE_T:
605         case ICE_PHY_TYPE_LOW_5GBASE_KR:
606                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
607                 ethtool_link_ksettings_add_link_mode(ks, supported,
608                                                      5000baseT_Full);
609                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
610                 ethtool_link_ksettings_add_link_mode(ks, advertising,
611                                                      5000baseT_Full);
612                 break;
613         case ICE_PHY_TYPE_LOW_10GBASE_T:
614                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
615                 ethtool_link_ksettings_add_link_mode(ks, supported,
616                                                      10000baseT_Full);
617                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
618                 ethtool_link_ksettings_add_link_mode(ks, advertising,
619                                                      10000baseT_Full);
620                 break;
621         case ICE_PHY_TYPE_LOW_10G_SFI_DA:
622         case ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC:
623         case ICE_PHY_TYPE_LOW_10G_SFI_C2C:
624                 ethtool_link_ksettings_add_link_mode(ks, supported,
625                                                      10000baseT_Full);
626                 break;
627         case ICE_PHY_TYPE_LOW_10GBASE_SR:
628                 ethtool_link_ksettings_add_link_mode(ks, supported,
629                                                      10000baseSR_Full);
630                 break;
631         case ICE_PHY_TYPE_LOW_10GBASE_LR:
632                 ethtool_link_ksettings_add_link_mode(ks, supported,
633                                                      10000baseLR_Full);
634                 break;
635         case ICE_PHY_TYPE_LOW_10GBASE_KR_CR1:
636                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
637                 ethtool_link_ksettings_add_link_mode(ks, supported,
638                                                      10000baseKR_Full);
639                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
640                 ethtool_link_ksettings_add_link_mode(ks, advertising,
641                                                      10000baseKR_Full);
642                 break;
643         case ICE_PHY_TYPE_LOW_25GBASE_T:
644         case ICE_PHY_TYPE_LOW_25GBASE_CR:
645         case ICE_PHY_TYPE_LOW_25GBASE_CR_S:
646         case ICE_PHY_TYPE_LOW_25GBASE_CR1:
647                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
648                 ethtool_link_ksettings_add_link_mode(ks, supported,
649                                                      25000baseCR_Full);
650                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
651                 ethtool_link_ksettings_add_link_mode(ks, advertising,
652                                                      25000baseCR_Full);
653                 break;
654         case ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC:
655                 ethtool_link_ksettings_add_link_mode(ks, supported,
656                                                      25000baseCR_Full);
657                 break;
658         case ICE_PHY_TYPE_LOW_25GBASE_SR:
659         case ICE_PHY_TYPE_LOW_25GBASE_LR:
660                 ethtool_link_ksettings_add_link_mode(ks, supported,
661                                                      25000baseSR_Full);
662                 break;
663         case ICE_PHY_TYPE_LOW_25GBASE_KR:
664         case ICE_PHY_TYPE_LOW_25GBASE_KR1:
665         case ICE_PHY_TYPE_LOW_25GBASE_KR_S:
666                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
667                 ethtool_link_ksettings_add_link_mode(ks, supported,
668                                                      25000baseKR_Full);
669                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
670                 ethtool_link_ksettings_add_link_mode(ks, advertising,
671                                                      25000baseKR_Full);
672                 break;
673         case ICE_PHY_TYPE_LOW_40GBASE_CR4:
674                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
675                 ethtool_link_ksettings_add_link_mode(ks, supported,
676                                                      40000baseCR4_Full);
677                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
678                 ethtool_link_ksettings_add_link_mode(ks, advertising,
679                                                      40000baseCR4_Full);
680                 break;
681         case ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC:
682         case ICE_PHY_TYPE_LOW_40G_XLAUI:
683                 ethtool_link_ksettings_add_link_mode(ks, supported,
684                                                      40000baseCR4_Full);
685                 break;
686         case ICE_PHY_TYPE_LOW_40GBASE_SR4:
687                 ethtool_link_ksettings_add_link_mode(ks, supported,
688                                                      40000baseSR4_Full);
689                 break;
690         case ICE_PHY_TYPE_LOW_40GBASE_LR4:
691                 ethtool_link_ksettings_add_link_mode(ks, supported,
692                                                      40000baseLR4_Full);
693                 break;
694         case ICE_PHY_TYPE_LOW_40GBASE_KR4:
695                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
696                 ethtool_link_ksettings_add_link_mode(ks, supported,
697                                                      40000baseKR4_Full);
698                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
699                 ethtool_link_ksettings_add_link_mode(ks, advertising,
700                                                      40000baseKR4_Full);
701                 break;
702         default:
703                 unrecog_phy_low = true;
704         }
705
706         if (unrecog_phy_low) {
707                 /* if we got here and link is up something bad is afoot */
708                 netdev_info(netdev, "WARNING: Unrecognized PHY_Low (0x%llx).\n",
709                             (u64)link_info->phy_type_low);
710         }
711
712         /* Now that we've worked out everything that could be supported by the
713          * current PHY type, get what is supported by the NVM and intersect
714          * them to get what is truly supported
715          */
716         memset(&cap_ksettings, 0, sizeof(struct ethtool_link_ksettings));
717         ice_phy_type_to_ethtool(netdev, &cap_ksettings);
718         ethtool_intersect_link_masks(ks, &cap_ksettings);
719
720         switch (link_info->link_speed) {
721         case ICE_AQ_LINK_SPEED_40GB:
722                 ks->base.speed = SPEED_40000;
723                 break;
724         case ICE_AQ_LINK_SPEED_25GB:
725                 ks->base.speed = SPEED_25000;
726                 break;
727         case ICE_AQ_LINK_SPEED_20GB:
728                 ks->base.speed = SPEED_20000;
729                 break;
730         case ICE_AQ_LINK_SPEED_10GB:
731                 ks->base.speed = SPEED_10000;
732                 break;
733         case ICE_AQ_LINK_SPEED_5GB:
734                 ks->base.speed = SPEED_5000;
735                 break;
736         case ICE_AQ_LINK_SPEED_2500MB:
737                 ks->base.speed = SPEED_2500;
738                 break;
739         case ICE_AQ_LINK_SPEED_1000MB:
740                 ks->base.speed = SPEED_1000;
741                 break;
742         case ICE_AQ_LINK_SPEED_100MB:
743                 ks->base.speed = SPEED_100;
744                 break;
745         default:
746                 netdev_info(netdev,
747                             "WARNING: Unrecognized link_speed (0x%x).\n",
748                             link_info->link_speed);
749                 break;
750         }
751         ks->base.duplex = DUPLEX_FULL;
752 }
753
754 /**
755  * ice_get_settings_link_down - Get the Link settings when link is down
756  * @ks: ethtool ksettings to fill in
757  * @netdev: network interface device structure
758  *
759  * Reports link settings that can be determined when link is down
760  */
761 static void
762 ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
763                            struct net_device __always_unused *netdev)
764 {
765         /* link is down and the driver needs to fall back on
766          * supported phy types to figure out what info to display
767          */
768         ice_phy_type_to_ethtool(netdev, ks);
769
770         /* With no link, speed and duplex are unknown */
771         ks->base.speed = SPEED_UNKNOWN;
772         ks->base.duplex = DUPLEX_UNKNOWN;
773 }
774
775 /**
776  * ice_get_link_ksettings - Get Link Speed and Duplex settings
777  * @netdev: network interface device structure
778  * @ks: ethtool ksettings
779  *
780  * Reports speed/duplex settings based on media_type
781  */
782 static int ice_get_link_ksettings(struct net_device *netdev,
783                                   struct ethtool_link_ksettings *ks)
784 {
785         struct ice_netdev_priv *np = netdev_priv(netdev);
786         struct ice_link_status *hw_link_info;
787         struct ice_vsi *vsi = np->vsi;
788
789         ethtool_link_ksettings_zero_link_mode(ks, supported);
790         ethtool_link_ksettings_zero_link_mode(ks, advertising);
791         hw_link_info = &vsi->port_info->phy.link_info;
792
793         /* set speed and duplex */
794         if (hw_link_info->link_info & ICE_AQ_LINK_UP)
795                 ice_get_settings_link_up(ks, netdev);
796         else
797                 ice_get_settings_link_down(ks, netdev);
798
799         /* set autoneg settings */
800         ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
801                 AUTONEG_ENABLE : AUTONEG_DISABLE;
802
803         /* set media type settings */
804         switch (vsi->port_info->phy.media_type) {
805         case ICE_MEDIA_FIBER:
806                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
807                 ks->base.port = PORT_FIBRE;
808                 break;
809         case ICE_MEDIA_BASET:
810                 ethtool_link_ksettings_add_link_mode(ks, supported, TP);
811                 ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
812                 ks->base.port = PORT_TP;
813                 break;
814         case ICE_MEDIA_BACKPLANE:
815                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
816                 ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
817                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
818                 ethtool_link_ksettings_add_link_mode(ks, advertising,
819                                                      Backplane);
820                 ks->base.port = PORT_NONE;
821                 break;
822         case ICE_MEDIA_DA:
823                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
824                 ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
825                 ks->base.port = PORT_DA;
826                 break;
827         default:
828                 ks->base.port = PORT_OTHER;
829                 break;
830         }
831
832         /* flow control is symmetric and always supported */
833         ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
834
835         switch (vsi->port_info->fc.req_mode) {
836         case ICE_FC_FULL:
837                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
838                 break;
839         case ICE_FC_TX_PAUSE:
840                 ethtool_link_ksettings_add_link_mode(ks, advertising,
841                                                      Asym_Pause);
842                 break;
843         case ICE_FC_RX_PAUSE:
844                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
845                 ethtool_link_ksettings_add_link_mode(ks, advertising,
846                                                      Asym_Pause);
847                 break;
848         case ICE_FC_PFC:
849         default:
850                 ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
851                 ethtool_link_ksettings_del_link_mode(ks, advertising,
852                                                      Asym_Pause);
853                 break;
854         }
855
856         return 0;
857 }
858
859 /**
860  * ice_ksettings_find_adv_link_speed - Find advertising link speed
861  * @ks: ethtool ksettings
862  */
863 static u16
864 ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
865 {
866         u16 adv_link_speed = 0;
867
868         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
869                                                   100baseT_Full))
870                 adv_link_speed |= ICE_AQ_LINK_SPEED_100MB;
871         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
872                                                   1000baseX_Full))
873                 adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
874         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
875                                                   1000baseT_Full) ||
876             ethtool_link_ksettings_test_link_mode(ks, advertising,
877                                                   1000baseKX_Full))
878                 adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB;
879         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
880                                                   2500baseT_Full))
881                 adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
882         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
883                                                   2500baseX_Full))
884                 adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB;
885         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
886                                                   5000baseT_Full))
887                 adv_link_speed |= ICE_AQ_LINK_SPEED_5GB;
888         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
889                                                   10000baseT_Full) ||
890             ethtool_link_ksettings_test_link_mode(ks, advertising,
891                                                   10000baseKR_Full))
892                 adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
893         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
894                                                   10000baseSR_Full) ||
895             ethtool_link_ksettings_test_link_mode(ks, advertising,
896                                                   10000baseLR_Full))
897                 adv_link_speed |= ICE_AQ_LINK_SPEED_10GB;
898         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
899                                                   25000baseCR_Full) ||
900             ethtool_link_ksettings_test_link_mode(ks, advertising,
901                                                   25000baseSR_Full) ||
902             ethtool_link_ksettings_test_link_mode(ks, advertising,
903                                                   25000baseKR_Full))
904                 adv_link_speed |= ICE_AQ_LINK_SPEED_25GB;
905         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
906                                                   40000baseCR4_Full) ||
907             ethtool_link_ksettings_test_link_mode(ks, advertising,
908                                                   40000baseSR4_Full) ||
909             ethtool_link_ksettings_test_link_mode(ks, advertising,
910                                                   40000baseLR4_Full) ||
911             ethtool_link_ksettings_test_link_mode(ks, advertising,
912                                                   40000baseKR4_Full))
913                 adv_link_speed |= ICE_AQ_LINK_SPEED_40GB;
914
915         return adv_link_speed;
916 }
917
918 /**
919  * ice_setup_autoneg
920  * @p: port info
921  * @ks: ethtool_link_ksettings
922  * @config: configuration that will be sent down to FW
923  * @autoneg_enabled: autonegotiation is enabled or not
924  * @autoneg_changed: will there a change in autonegotiation
925  * @netdev: network interface device structure
926  *
927  * Setup PHY autonegotiation feature
928  */
929 static int
930 ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
931                   struct ice_aqc_set_phy_cfg_data *config,
932                   u8 autoneg_enabled, u8 *autoneg_changed,
933                   struct net_device *netdev)
934 {
935         int err = 0;
936
937         *autoneg_changed = 0;
938
939         /* Check autoneg */
940         if (autoneg_enabled == AUTONEG_ENABLE) {
941                 /* If autoneg was not already enabled */
942                 if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
943                         /* If autoneg is not supported, return error */
944                         if (!ethtool_link_ksettings_test_link_mode(ks,
945                                                                    supported,
946                                                                    Autoneg)) {
947                                 netdev_info(netdev, "Autoneg not supported on this phy.\n");
948                                 err = -EINVAL;
949                         } else {
950                                 /* Autoneg is allowed to change */
951                                 config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
952                                 *autoneg_changed = 1;
953                         }
954                 }
955         } else {
956                 /* If autoneg is currently enabled */
957                 if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
958                         /* If autoneg is supported 10GBASE_T is the only phy
959                          * that can disable it, so otherwise return error
960                          */
961                         if (ethtool_link_ksettings_test_link_mode(ks,
962                                                                   supported,
963                                                                   Autoneg)) {
964                                 netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
965                                 err = -EINVAL;
966                         } else {
967                                 /* Autoneg is allowed to change */
968                                 config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
969                                 *autoneg_changed = 1;
970                         }
971                 }
972         }
973
974         return err;
975 }
976
977 /**
978  * ice_set_link_ksettings - Set Speed and Duplex
979  * @netdev: network interface device structure
980  * @ks: ethtool ksettings
981  *
982  * Set speed/duplex per media_types advertised/forced
983  */
984 static int ice_set_link_ksettings(struct net_device *netdev,
985                                   const struct ethtool_link_ksettings *ks)
986 {
987         u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT, lport = 0;
988         struct ice_netdev_priv *np = netdev_priv(netdev);
989         struct ethtool_link_ksettings safe_ks, copy_ks;
990         struct ice_aqc_get_phy_caps_data *abilities;
991         u16 adv_link_speed, curr_link_speed, idx;
992         struct ice_aqc_set_phy_cfg_data config;
993         struct ice_pf *pf = np->vsi->back;
994         struct ice_port_info *p;
995         u8 autoneg_changed = 0;
996         enum ice_status status;
997         u64 phy_type_low;
998         int err = 0;
999         bool linkup;
1000
1001         p = np->vsi->port_info;
1002
1003         if (!p)
1004                 return -EOPNOTSUPP;
1005
1006         /* Check if this is lan vsi */
1007         for (idx = 0 ; idx <  pf->num_alloc_vsi ; idx++) {
1008                 if (pf->vsi[idx]->type == ICE_VSI_PF) {
1009                         if (np->vsi != pf->vsi[idx])
1010                                 return -EOPNOTSUPP;
1011                         break;
1012                 }
1013         }
1014
1015         if (p->phy.media_type != ICE_MEDIA_BASET &&
1016             p->phy.media_type != ICE_MEDIA_FIBER &&
1017             p->phy.media_type != ICE_MEDIA_BACKPLANE &&
1018             p->phy.media_type != ICE_MEDIA_DA &&
1019             p->phy.link_info.link_info & ICE_AQ_LINK_UP)
1020                 return -EOPNOTSUPP;
1021
1022         /* copy the ksettings to copy_ks to avoid modifying the original */
1023         memcpy(&copy_ks, ks, sizeof(struct ethtool_link_ksettings));
1024
1025         /* save autoneg out of ksettings */
1026         autoneg = copy_ks.base.autoneg;
1027
1028         memset(&safe_ks, 0, sizeof(safe_ks));
1029
1030         /* Get link modes supported by hardware.*/
1031         ice_phy_type_to_ethtool(netdev, &safe_ks);
1032
1033         /* and check against modes requested by user.
1034          * Return an error if unsupported mode was set.
1035          */
1036         if (!bitmap_subset(copy_ks.link_modes.advertising,
1037                            safe_ks.link_modes.supported,
1038                            __ETHTOOL_LINK_MODE_MASK_NBITS))
1039                 return -EINVAL;
1040
1041         /* get our own copy of the bits to check against */
1042         memset(&safe_ks, 0, sizeof(struct ethtool_link_ksettings));
1043         safe_ks.base.cmd = copy_ks.base.cmd;
1044         safe_ks.base.link_mode_masks_nwords =
1045                 copy_ks.base.link_mode_masks_nwords;
1046         ice_get_link_ksettings(netdev, &safe_ks);
1047
1048         /* set autoneg back to what it currently is */
1049         copy_ks.base.autoneg = safe_ks.base.autoneg;
1050         /* we don't compare the speed */
1051         copy_ks.base.speed = safe_ks.base.speed;
1052
1053         /* If copy_ks.base and safe_ks.base are not the same now, then they are
1054          * trying to set something that we do not support.
1055          */
1056         if (memcmp(&copy_ks.base, &safe_ks.base,
1057                    sizeof(struct ethtool_link_settings)))
1058                 return -EOPNOTSUPP;
1059
1060         while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
1061                 timeout--;
1062                 if (!timeout)
1063                         return -EBUSY;
1064                 usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
1065         }
1066
1067         abilities = devm_kzalloc(&pf->pdev->dev, sizeof(*abilities),
1068                                  GFP_KERNEL);
1069         if (!abilities)
1070                 return -ENOMEM;
1071
1072         /* Get the current phy config */
1073         status = ice_aq_get_phy_caps(p, false, ICE_AQC_REPORT_SW_CFG, abilities,
1074                                      NULL);
1075         if (status) {
1076                 err = -EAGAIN;
1077                 goto done;
1078         }
1079
1080         /* Copy abilities to config in case autoneg is not set below */
1081         memset(&config, 0, sizeof(struct ice_aqc_set_phy_cfg_data));
1082         config.caps = abilities->caps & ~ICE_AQC_PHY_AN_MODE;
1083         if (abilities->caps & ICE_AQC_PHY_AN_MODE)
1084                 config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1085
1086         /* Check autoneg */
1087         err = ice_setup_autoneg(p, &safe_ks, &config, autoneg, &autoneg_changed,
1088                                 netdev);
1089
1090         if (err)
1091                 goto done;
1092
1093         /* Call to get the current link speed */
1094         p->phy.get_link_info = true;
1095         status = ice_get_link_status(p, &linkup);
1096         if (status) {
1097                 err = -EAGAIN;
1098                 goto done;
1099         }
1100
1101         curr_link_speed = p->phy.link_info.link_speed;
1102         adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
1103
1104         /* If speed didn't get set, set it to what it currently is.
1105          * This is needed because if advertise is 0 (as it is when autoneg
1106          * is disabled) then speed won't get set.
1107          */
1108         if (!adv_link_speed)
1109                 adv_link_speed = curr_link_speed;
1110
1111         /* Convert the advertise link speeds to their corresponded PHY_TYPE */
1112         ice_update_phy_type(&phy_type_low, adv_link_speed);
1113
1114         if (!autoneg_changed && adv_link_speed == curr_link_speed) {
1115                 netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
1116                 goto done;
1117         }
1118
1119         /* copy over the rest of the abilities */
1120         config.low_power_ctrl = abilities->low_power_ctrl;
1121         config.eee_cap = abilities->eee_cap;
1122         config.eeer_value = abilities->eeer_value;
1123         config.link_fec_opt = abilities->link_fec_options;
1124
1125         /* save the requested speeds */
1126         p->phy.link_info.req_speeds = adv_link_speed;
1127
1128         /* set link and auto negotiation so changes take effect */
1129         config.caps |= ICE_AQ_PHY_ENA_LINK;
1130
1131         if (phy_type_low) {
1132                 config.phy_type_low = cpu_to_le64(phy_type_low) &
1133                         abilities->phy_type_low;
1134         } else {
1135                 err = -EAGAIN;
1136                 netdev_info(netdev, "Nothing changed. No PHY_TYPE is corresponded to advertised link speed.\n");
1137                 goto done;
1138         }
1139
1140         /* If link is up put link down */
1141         if (p->phy.link_info.link_info & ICE_AQ_LINK_UP) {
1142                 /* Tell the OS link is going down, the link will go
1143                  * back up when fw says it is ready asynchronously
1144                  */
1145                 ice_print_link_msg(np->vsi, false);
1146                 netif_carrier_off(netdev);
1147                 netif_tx_stop_all_queues(netdev);
1148         }
1149
1150         /* make the aq call */
1151         status = ice_aq_set_phy_cfg(&pf->hw, lport, &config, NULL);
1152         if (status) {
1153                 netdev_info(netdev, "Set phy config failed,\n");
1154                 err = -EAGAIN;
1155         }
1156
1157 done:
1158         devm_kfree(&pf->pdev->dev, abilities);
1159         clear_bit(__ICE_CFG_BUSY, pf->state);
1160
1161         return err;
1162 }
1163
1164 /**
1165  * ice_get_rxnfc - command to get RX flow classification rules
1166  * @netdev: network interface device structure
1167  * @cmd: ethtool rxnfc command
1168  * @rule_locs: buffer to rturn Rx flow classification rules
1169  *
1170  * Returns Success if the command is supported.
1171  */
1172 static int ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1173                          u32 __always_unused *rule_locs)
1174 {
1175         struct ice_netdev_priv *np = netdev_priv(netdev);
1176         struct ice_vsi *vsi = np->vsi;
1177         int ret = -EOPNOTSUPP;
1178
1179         switch (cmd->cmd) {
1180         case ETHTOOL_GRXRINGS:
1181                 cmd->data = vsi->rss_size;
1182                 ret = 0;
1183                 break;
1184         default:
1185                 break;
1186         }
1187
1188         return ret;
1189 }
1190
1191 static void
1192 ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
1193 {
1194         struct ice_netdev_priv *np = netdev_priv(netdev);
1195         struct ice_vsi *vsi = np->vsi;
1196
1197         ring->rx_max_pending = ICE_MAX_NUM_DESC;
1198         ring->tx_max_pending = ICE_MAX_NUM_DESC;
1199         ring->rx_pending = vsi->rx_rings[0]->count;
1200         ring->tx_pending = vsi->tx_rings[0]->count;
1201
1202         /* Rx mini and jumbo rings are not supported */
1203         ring->rx_mini_max_pending = 0;
1204         ring->rx_jumbo_max_pending = 0;
1205         ring->rx_mini_pending = 0;
1206         ring->rx_jumbo_pending = 0;
1207 }
1208
1209 static int
1210 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
1211 {
1212         struct ice_ring *tx_rings = NULL, *rx_rings = NULL;
1213         struct ice_netdev_priv *np = netdev_priv(netdev);
1214         struct ice_vsi *vsi = np->vsi;
1215         struct ice_pf *pf = vsi->back;
1216         int i, timeout = 50, err = 0;
1217         u32 new_rx_cnt, new_tx_cnt;
1218
1219         if (ring->tx_pending > ICE_MAX_NUM_DESC ||
1220             ring->tx_pending < ICE_MIN_NUM_DESC ||
1221             ring->rx_pending > ICE_MAX_NUM_DESC ||
1222             ring->rx_pending < ICE_MIN_NUM_DESC) {
1223                 netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
1224                            ring->tx_pending, ring->rx_pending,
1225                            ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
1226                            ICE_REQ_DESC_MULTIPLE);
1227                 return -EINVAL;
1228         }
1229
1230         new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
1231         if (new_tx_cnt != ring->tx_pending)
1232                 netdev_info(netdev,
1233                             "Requested Tx descriptor count rounded up to %d\n",
1234                             new_tx_cnt);
1235         new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
1236         if (new_rx_cnt != ring->rx_pending)
1237                 netdev_info(netdev,
1238                             "Requested Rx descriptor count rounded up to %d\n",
1239                             new_rx_cnt);
1240
1241         /* if nothing to do return success */
1242         if (new_tx_cnt == vsi->tx_rings[0]->count &&
1243             new_rx_cnt == vsi->rx_rings[0]->count) {
1244                 netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
1245                 return 0;
1246         }
1247
1248         while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
1249                 timeout--;
1250                 if (!timeout)
1251                         return -EBUSY;
1252                 usleep_range(1000, 2000);
1253         }
1254
1255         /* set for the next time the netdev is started */
1256         if (!netif_running(vsi->netdev)) {
1257                 for (i = 0; i < vsi->alloc_txq; i++)
1258                         vsi->tx_rings[i]->count = new_tx_cnt;
1259                 for (i = 0; i < vsi->alloc_rxq; i++)
1260                         vsi->rx_rings[i]->count = new_rx_cnt;
1261                 netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
1262                 goto done;
1263         }
1264
1265         if (new_tx_cnt == vsi->tx_rings[0]->count)
1266                 goto process_rx;
1267
1268         /* alloc updated Tx resources */
1269         netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
1270                     vsi->tx_rings[0]->count, new_tx_cnt);
1271
1272         tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
1273                                 sizeof(struct ice_ring), GFP_KERNEL);
1274         if (!tx_rings) {
1275                 err = -ENOMEM;
1276                 goto done;
1277         }
1278
1279         for (i = 0; i < vsi->alloc_txq; i++) {
1280                 /* clone ring and setup updated count */
1281                 tx_rings[i] = *vsi->tx_rings[i];
1282                 tx_rings[i].count = new_tx_cnt;
1283                 tx_rings[i].desc = NULL;
1284                 tx_rings[i].tx_buf = NULL;
1285                 err = ice_setup_tx_ring(&tx_rings[i]);
1286                 if (err) {
1287                         while (i) {
1288                                 i--;
1289                                 ice_clean_tx_ring(&tx_rings[i]);
1290                         }
1291                         devm_kfree(&pf->pdev->dev, tx_rings);
1292                         goto done;
1293                 }
1294         }
1295
1296 process_rx:
1297         if (new_rx_cnt == vsi->rx_rings[0]->count)
1298                 goto process_link;
1299
1300         /* alloc updated Rx resources */
1301         netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
1302                     vsi->rx_rings[0]->count, new_rx_cnt);
1303
1304         rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
1305                                 sizeof(struct ice_ring), GFP_KERNEL);
1306         if (!rx_rings) {
1307                 err = -ENOMEM;
1308                 goto done;
1309         }
1310
1311         for (i = 0; i < vsi->alloc_rxq; i++) {
1312                 /* clone ring and setup updated count */
1313                 rx_rings[i] = *vsi->rx_rings[i];
1314                 rx_rings[i].count = new_rx_cnt;
1315                 rx_rings[i].desc = NULL;
1316                 rx_rings[i].rx_buf = NULL;
1317                 /* this is to allow wr32 to have something to write to
1318                  * during early allocation of Rx buffers
1319                  */
1320                 rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
1321
1322                 err = ice_setup_rx_ring(&rx_rings[i]);
1323                 if (err)
1324                         goto rx_unwind;
1325
1326                 /* allocate Rx buffers */
1327                 err = ice_alloc_rx_bufs(&rx_rings[i],
1328                                         ICE_DESC_UNUSED(&rx_rings[i]));
1329 rx_unwind:
1330                 if (err) {
1331                         while (i) {
1332                                 i--;
1333                                 ice_free_rx_ring(&rx_rings[i]);
1334                         }
1335                         devm_kfree(&pf->pdev->dev, rx_rings);
1336                         err = -ENOMEM;
1337                         goto free_tx;
1338                 }
1339         }
1340
1341 process_link:
1342         /* Bring interface down, copy in the new ring info, then restore the
1343          * interface. if VSI is up, bring it down and then back up
1344          */
1345         if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
1346                 ice_down(vsi);
1347
1348                 if (tx_rings) {
1349                         for (i = 0; i < vsi->alloc_txq; i++) {
1350                                 ice_free_tx_ring(vsi->tx_rings[i]);
1351                                 *vsi->tx_rings[i] = tx_rings[i];
1352                         }
1353                         devm_kfree(&pf->pdev->dev, tx_rings);
1354                 }
1355
1356                 if (rx_rings) {
1357                         for (i = 0; i < vsi->alloc_rxq; i++) {
1358                                 ice_free_rx_ring(vsi->rx_rings[i]);
1359                                 /* copy the real tail offset */
1360                                 rx_rings[i].tail = vsi->rx_rings[i]->tail;
1361                                 /* this is to fake out the allocation routine
1362                                  * into thinking it has to realloc everything
1363                                  * but the recycling logic will let us re-use
1364                                  * the buffers allocated above
1365                                  */
1366                                 rx_rings[i].next_to_use = 0;
1367                                 rx_rings[i].next_to_clean = 0;
1368                                 rx_rings[i].next_to_alloc = 0;
1369                                 *vsi->rx_rings[i] = rx_rings[i];
1370                         }
1371                         devm_kfree(&pf->pdev->dev, rx_rings);
1372                 }
1373
1374                 ice_up(vsi);
1375         }
1376         goto done;
1377
1378 free_tx:
1379         /* error cleanup if the Rx allocations failed after getting Tx */
1380         if (tx_rings) {
1381                 for (i = 0; i < vsi->alloc_txq; i++)
1382                         ice_free_tx_ring(&tx_rings[i]);
1383                 devm_kfree(&pf->pdev->dev, tx_rings);
1384         }
1385
1386 done:
1387         clear_bit(__ICE_CFG_BUSY, pf->state);
1388         return err;
1389 }
1390
1391 static int ice_nway_reset(struct net_device *netdev)
1392 {
1393         /* restart autonegotiation */
1394         struct ice_netdev_priv *np = netdev_priv(netdev);
1395         struct ice_vsi *vsi = np->vsi;
1396         struct ice_port_info *pi;
1397         enum ice_status status;
1398
1399         pi = vsi->port_info;
1400         /* If VSI state is up, then restart autoneg with link up */
1401         if (!test_bit(__ICE_DOWN, vsi->back->state))
1402                 status = ice_aq_set_link_restart_an(pi, true, NULL);
1403         else
1404                 status = ice_aq_set_link_restart_an(pi, false, NULL);
1405
1406         if (status) {
1407                 netdev_info(netdev, "link restart failed, err %d aq_err %d\n",
1408                             status, pi->hw->adminq.sq_last_status);
1409                 return -EIO;
1410         }
1411
1412         return 0;
1413 }
1414
1415 /**
1416  * ice_get_pauseparam - Get Flow Control status
1417  * @netdev: network interface device structure
1418  * @pause: ethernet pause (flow control) parameters
1419  */
1420 static void
1421 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
1422 {
1423         struct ice_netdev_priv *np = netdev_priv(netdev);
1424         struct ice_port_info *pi;
1425
1426         pi = np->vsi->port_info;
1427         pause->autoneg =
1428                 ((pi->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) ?
1429                  AUTONEG_ENABLE : AUTONEG_DISABLE);
1430
1431         if (pi->fc.current_mode == ICE_FC_RX_PAUSE) {
1432                 pause->rx_pause = 1;
1433         } else if (pi->fc.current_mode == ICE_FC_TX_PAUSE) {
1434                 pause->tx_pause = 1;
1435         } else if (pi->fc.current_mode == ICE_FC_FULL) {
1436                 pause->rx_pause = 1;
1437                 pause->tx_pause = 1;
1438         }
1439 }
1440
1441 /**
1442  * ice_set_pauseparam - Set Flow Control parameter
1443  * @netdev: network interface device structure
1444  * @pause: return Tx/Rx flow control status
1445  */
1446 static int
1447 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
1448 {
1449         struct ice_netdev_priv *np = netdev_priv(netdev);
1450         struct ice_link_status *hw_link_info;
1451         struct ice_pf *pf = np->vsi->back;
1452         struct ice_vsi *vsi = np->vsi;
1453         struct ice_hw *hw = &pf->hw;
1454         struct ice_port_info *pi;
1455         enum ice_status status;
1456         u8 aq_failures;
1457         bool link_up;
1458         int err = 0;
1459
1460         pi = vsi->port_info;
1461         hw_link_info = &pi->phy.link_info;
1462         link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
1463
1464         /* Changing the port's flow control is not supported if this isn't the
1465          * PF VSI
1466          */
1467         if (vsi->type != ICE_VSI_PF) {
1468                 netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
1469                 return -EOPNOTSUPP;
1470         }
1471
1472         if (pause->autoneg != (hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
1473                 netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
1474                 return -EOPNOTSUPP;
1475         }
1476
1477         /* If we have link and don't have autoneg */
1478         if (!test_bit(__ICE_DOWN, pf->state) &&
1479             !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
1480                 /* Send message that it might not necessarily work*/
1481                 netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
1482         }
1483
1484         if (pause->rx_pause && pause->tx_pause)
1485                 pi->fc.req_mode = ICE_FC_FULL;
1486         else if (pause->rx_pause && !pause->tx_pause)
1487                 pi->fc.req_mode = ICE_FC_RX_PAUSE;
1488         else if (!pause->rx_pause && pause->tx_pause)
1489                 pi->fc.req_mode = ICE_FC_TX_PAUSE;
1490         else if (!pause->rx_pause && !pause->tx_pause)
1491                 pi->fc.req_mode = ICE_FC_NONE;
1492         else
1493                 return -EINVAL;
1494
1495         /* Tell the OS link is going down, the link will go back up when fw
1496          * says it is ready asynchronously
1497          */
1498         ice_print_link_msg(vsi, false);
1499         netif_carrier_off(netdev);
1500         netif_tx_stop_all_queues(netdev);
1501
1502         /* Set the FC mode and only restart AN if link is up */
1503         status = ice_set_fc(pi, &aq_failures, link_up);
1504
1505         if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
1506                 netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %d\n",
1507                             status, hw->adminq.sq_last_status);
1508                 err = -EAGAIN;
1509         } else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
1510                 netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %d\n",
1511                             status, hw->adminq.sq_last_status);
1512                 err = -EAGAIN;
1513         } else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
1514                 netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %d\n",
1515                             status, hw->adminq.sq_last_status);
1516                 err = -EAGAIN;
1517         }
1518
1519         if (!test_bit(__ICE_DOWN, pf->state)) {
1520                 /* Give it a little more time to try to come back. If still
1521                  * down, restart autoneg link or reinitialize the interface.
1522                  */
1523                 msleep(75);
1524                 if (!test_bit(__ICE_DOWN, pf->state))
1525                         return ice_nway_reset(netdev);
1526
1527                 ice_down(vsi);
1528                 ice_up(vsi);
1529         }
1530
1531         return err;
1532 }
1533
1534 /**
1535  * ice_get_rxfh_key_size - get the RSS hash key size
1536  * @netdev: network interface device structure
1537  *
1538  * Returns the table size.
1539  */
1540 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
1541 {
1542         return ICE_VSIQF_HKEY_ARRAY_SIZE;
1543 }
1544
1545 /**
1546  * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
1547  * @netdev: network interface device structure
1548  *
1549  * Returns the table size.
1550  */
1551 static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
1552 {
1553         struct ice_netdev_priv *np = netdev_priv(netdev);
1554
1555         return np->vsi->rss_table_size;
1556 }
1557
1558 /**
1559  * ice_get_rxfh - get the Rx flow hash indirection table
1560  * @netdev: network interface device structure
1561  * @indir: indirection table
1562  * @key: hash key
1563  * @hfunc: hash function
1564  *
1565  * Reads the indirection table directly from the hardware.
1566  */
1567 static int
1568 ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1569 {
1570         struct ice_netdev_priv *np = netdev_priv(netdev);
1571         struct ice_vsi *vsi = np->vsi;
1572         struct ice_pf *pf = vsi->back;
1573         int ret = 0, i;
1574         u8 *lut;
1575
1576         if (hfunc)
1577                 *hfunc = ETH_RSS_HASH_TOP;
1578
1579         if (!indir)
1580                 return 0;
1581
1582         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
1583                 /* RSS not supported return error here */
1584                 netdev_warn(netdev, "RSS is not configured on this VSI!\n");
1585                 return -EIO;
1586         }
1587
1588         lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
1589         if (!lut)
1590                 return -ENOMEM;
1591
1592         if (ice_get_rss(vsi, key, lut, vsi->rss_table_size)) {
1593                 ret = -EIO;
1594                 goto out;
1595         }
1596
1597         for (i = 0; i < vsi->rss_table_size; i++)
1598                 indir[i] = (u32)(lut[i]);
1599
1600 out:
1601         devm_kfree(&pf->pdev->dev, lut);
1602         return ret;
1603 }
1604
1605 /**
1606  * ice_set_rxfh - set the Rx flow hash indirection table
1607  * @netdev: network interface device structure
1608  * @indir: indirection table
1609  * @key: hash key
1610  * @hfunc: hash function
1611  *
1612  * Returns -EINVAL if the table specifies an invalid queue id, otherwise
1613  * returns 0 after programming the table.
1614  */
1615 static int ice_set_rxfh(struct net_device *netdev, const u32 *indir,
1616                         const u8 *key, const u8 hfunc)
1617 {
1618         struct ice_netdev_priv *np = netdev_priv(netdev);
1619         struct ice_vsi *vsi = np->vsi;
1620         struct ice_pf *pf = vsi->back;
1621         u8 *seed = NULL;
1622
1623         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1624                 return -EOPNOTSUPP;
1625
1626         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
1627                 /* RSS not supported return error here */
1628                 netdev_warn(netdev, "RSS is not configured on this VSI!\n");
1629                 return -EIO;
1630         }
1631
1632         if (key) {
1633                 if (!vsi->rss_hkey_user) {
1634                         vsi->rss_hkey_user =
1635                                 devm_kzalloc(&pf->pdev->dev,
1636                                              ICE_VSIQF_HKEY_ARRAY_SIZE,
1637                                              GFP_KERNEL);
1638                         if (!vsi->rss_hkey_user)
1639                                 return -ENOMEM;
1640                 }
1641                 memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
1642                 seed = vsi->rss_hkey_user;
1643         }
1644
1645         if (!vsi->rss_lut_user) {
1646                 vsi->rss_lut_user = devm_kzalloc(&pf->pdev->dev,
1647                                                  vsi->rss_table_size,
1648                                                  GFP_KERNEL);
1649                 if (!vsi->rss_lut_user)
1650                         return -ENOMEM;
1651         }
1652
1653         /* Each 32 bits pointed by 'indir' is stored with a lut entry */
1654         if (indir) {
1655                 int i;
1656
1657                 for (i = 0; i < vsi->rss_table_size; i++)
1658                         vsi->rss_lut_user[i] = (u8)(indir[i]);
1659         } else {
1660                 ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
1661                                  vsi->rss_size);
1662         }
1663
1664         if (ice_set_rss(vsi, seed, vsi->rss_lut_user, vsi->rss_table_size))
1665                 return -EIO;
1666
1667         return 0;
1668 }
1669
1670 static const struct ethtool_ops ice_ethtool_ops = {
1671         .get_link_ksettings     = ice_get_link_ksettings,
1672         .set_link_ksettings     = ice_set_link_ksettings,
1673         .get_drvinfo            = ice_get_drvinfo,
1674         .get_regs_len           = ice_get_regs_len,
1675         .get_regs               = ice_get_regs,
1676         .get_msglevel           = ice_get_msglevel,
1677         .set_msglevel           = ice_set_msglevel,
1678         .get_link               = ethtool_op_get_link,
1679         .get_strings            = ice_get_strings,
1680         .get_ethtool_stats      = ice_get_ethtool_stats,
1681         .get_sset_count         = ice_get_sset_count,
1682         .get_rxnfc              = ice_get_rxnfc,
1683         .get_ringparam          = ice_get_ringparam,
1684         .set_ringparam          = ice_set_ringparam,
1685         .nway_reset             = ice_nway_reset,
1686         .get_pauseparam         = ice_get_pauseparam,
1687         .set_pauseparam         = ice_set_pauseparam,
1688         .get_rxfh_key_size      = ice_get_rxfh_key_size,
1689         .get_rxfh_indir_size    = ice_get_rxfh_indir_size,
1690         .get_rxfh               = ice_get_rxfh,
1691         .set_rxfh               = ice_set_rxfh,
1692 };
1693
1694 /**
1695  * ice_set_ethtool_ops - setup netdev ethtool ops
1696  * @netdev: network interface device structure
1697  *
1698  * setup netdev ethtool ops with ice specific ops
1699  */
1700 void ice_set_ethtool_ops(struct net_device *netdev)
1701 {
1702         netdev->ethtool_ops = &ice_ethtool_ops;
1703 }