]> Git Repo - linux.git/blob - drivers/net/ethernet/intel/ice/ice_ethtool.c
crypto: akcipher - Drop sign/verify operations
[linux.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 #include "ice_ethtool.h"
8 #include "ice_flow.h"
9 #include "ice_fltr.h"
10 #include "ice_lib.h"
11 #include "ice_dcb_lib.h"
12 #include <net/dcbnl.h>
13
14 struct ice_stats {
15         char stat_string[ETH_GSTRING_LEN];
16         int sizeof_stat;
17         int stat_offset;
18 };
19
20 #define ICE_STAT(_type, _name, _stat) { \
21         .stat_string = _name, \
22         .sizeof_stat = sizeof_field(_type, _stat), \
23         .stat_offset = offsetof(_type, _stat) \
24 }
25
26 #define ICE_VSI_STAT(_name, _stat) \
27                 ICE_STAT(struct ice_vsi, _name, _stat)
28 #define ICE_PF_STAT(_name, _stat) \
29                 ICE_STAT(struct ice_pf, _name, _stat)
30
31 static int ice_q_stats_len(struct net_device *netdev)
32 {
33         struct ice_netdev_priv *np = netdev_priv(netdev);
34
35         return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) *
36                 (sizeof(struct ice_q_stats) / sizeof(u64)));
37 }
38
39 #define ICE_PF_STATS_LEN        ARRAY_SIZE(ice_gstrings_pf_stats)
40 #define ICE_VSI_STATS_LEN       ARRAY_SIZE(ice_gstrings_vsi_stats)
41
42 #define ICE_PFC_STATS_LEN ( \
43                 (sizeof_field(struct ice_pf, stats.priority_xoff_rx) + \
44                  sizeof_field(struct ice_pf, stats.priority_xon_rx) + \
45                  sizeof_field(struct ice_pf, stats.priority_xoff_tx) + \
46                  sizeof_field(struct ice_pf, stats.priority_xon_tx)) \
47                  / sizeof(u64))
48 #define ICE_ALL_STATS_LEN(n)    (ICE_PF_STATS_LEN + ICE_PFC_STATS_LEN + \
49                                  ICE_VSI_STATS_LEN + ice_q_stats_len(n))
50
51 static const struct ice_stats ice_gstrings_vsi_stats[] = {
52         ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
53         ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
54         ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
55         ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
56         ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
57         ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
58         ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes),
59         ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes),
60         ICE_VSI_STAT("rx_dropped", eth_stats.rx_discards),
61         ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
62         ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed),
63         ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
64         ICE_VSI_STAT("tx_errors", eth_stats.tx_errors),
65         ICE_VSI_STAT("tx_linearize", tx_linearize),
66         ICE_VSI_STAT("tx_busy", tx_busy),
67         ICE_VSI_STAT("tx_restart", tx_restart),
68 };
69
70 enum ice_ethtool_test_id {
71         ICE_ETH_TEST_REG = 0,
72         ICE_ETH_TEST_EEPROM,
73         ICE_ETH_TEST_INTR,
74         ICE_ETH_TEST_LOOP,
75         ICE_ETH_TEST_LINK,
76 };
77
78 static const char ice_gstrings_test[][ETH_GSTRING_LEN] = {
79         "Register test  (offline)",
80         "EEPROM test    (offline)",
81         "Interrupt test (offline)",
82         "Loopback test  (offline)",
83         "Link test   (on/offline)",
84 };
85
86 #define ICE_TEST_LEN (sizeof(ice_gstrings_test) / ETH_GSTRING_LEN)
87
88 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
89  * but they aren't. This device is capable of supporting multiple
90  * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual
91  * netdevs whereas the PF_STATs are for the physical function that's
92  * hosting these netdevs.
93  *
94  * The PF_STATs are appended to the netdev stats only when ethtool -S
95  * is queried on the base PF netdev.
96  */
97 static const struct ice_stats ice_gstrings_pf_stats[] = {
98         ICE_PF_STAT("rx_bytes.nic", stats.eth.rx_bytes),
99         ICE_PF_STAT("tx_bytes.nic", stats.eth.tx_bytes),
100         ICE_PF_STAT("rx_unicast.nic", stats.eth.rx_unicast),
101         ICE_PF_STAT("tx_unicast.nic", stats.eth.tx_unicast),
102         ICE_PF_STAT("rx_multicast.nic", stats.eth.rx_multicast),
103         ICE_PF_STAT("tx_multicast.nic", stats.eth.tx_multicast),
104         ICE_PF_STAT("rx_broadcast.nic", stats.eth.rx_broadcast),
105         ICE_PF_STAT("tx_broadcast.nic", stats.eth.tx_broadcast),
106         ICE_PF_STAT("tx_errors.nic", stats.eth.tx_errors),
107         ICE_PF_STAT("tx_timeout.nic", tx_timeout_count),
108         ICE_PF_STAT("rx_size_64.nic", stats.rx_size_64),
109         ICE_PF_STAT("tx_size_64.nic", stats.tx_size_64),
110         ICE_PF_STAT("rx_size_127.nic", stats.rx_size_127),
111         ICE_PF_STAT("tx_size_127.nic", stats.tx_size_127),
112         ICE_PF_STAT("rx_size_255.nic", stats.rx_size_255),
113         ICE_PF_STAT("tx_size_255.nic", stats.tx_size_255),
114         ICE_PF_STAT("rx_size_511.nic", stats.rx_size_511),
115         ICE_PF_STAT("tx_size_511.nic", stats.tx_size_511),
116         ICE_PF_STAT("rx_size_1023.nic", stats.rx_size_1023),
117         ICE_PF_STAT("tx_size_1023.nic", stats.tx_size_1023),
118         ICE_PF_STAT("rx_size_1522.nic", stats.rx_size_1522),
119         ICE_PF_STAT("tx_size_1522.nic", stats.tx_size_1522),
120         ICE_PF_STAT("rx_size_big.nic", stats.rx_size_big),
121         ICE_PF_STAT("tx_size_big.nic", stats.tx_size_big),
122         ICE_PF_STAT("link_xon_rx.nic", stats.link_xon_rx),
123         ICE_PF_STAT("link_xon_tx.nic", stats.link_xon_tx),
124         ICE_PF_STAT("link_xoff_rx.nic", stats.link_xoff_rx),
125         ICE_PF_STAT("link_xoff_tx.nic", stats.link_xoff_tx),
126         ICE_PF_STAT("tx_dropped_link_down.nic", stats.tx_dropped_link_down),
127         ICE_PF_STAT("rx_undersize.nic", stats.rx_undersize),
128         ICE_PF_STAT("rx_fragments.nic", stats.rx_fragments),
129         ICE_PF_STAT("rx_oversize.nic", stats.rx_oversize),
130         ICE_PF_STAT("rx_jabber.nic", stats.rx_jabber),
131         ICE_PF_STAT("rx_csum_bad.nic", hw_csum_rx_error),
132         ICE_PF_STAT("rx_eipe_error.nic", hw_rx_eipe_error),
133         ICE_PF_STAT("rx_dropped.nic", stats.eth.rx_discards),
134         ICE_PF_STAT("rx_crc_errors.nic", stats.crc_errors),
135         ICE_PF_STAT("illegal_bytes.nic", stats.illegal_bytes),
136         ICE_PF_STAT("mac_local_faults.nic", stats.mac_local_faults),
137         ICE_PF_STAT("mac_remote_faults.nic", stats.mac_remote_faults),
138         ICE_PF_STAT("fdir_sb_match.nic", stats.fd_sb_match),
139         ICE_PF_STAT("fdir_sb_status.nic", stats.fd_sb_status),
140         ICE_PF_STAT("tx_hwtstamp_skipped", ptp.tx_hwtstamp_skipped),
141         ICE_PF_STAT("tx_hwtstamp_timeouts", ptp.tx_hwtstamp_timeouts),
142         ICE_PF_STAT("tx_hwtstamp_flushed", ptp.tx_hwtstamp_flushed),
143         ICE_PF_STAT("tx_hwtstamp_discarded", ptp.tx_hwtstamp_discarded),
144         ICE_PF_STAT("late_cached_phc_updates", ptp.late_cached_phc_updates),
145 };
146
147 static const u32 ice_regs_dump_list[] = {
148         PFGEN_STATE,
149         PRTGEN_STATUS,
150         QRX_CTRL(0),
151         QINT_TQCTL(0),
152         QINT_RQCTL(0),
153         PFINT_OICR_ENA,
154         QRX_ITR(0),
155 #define GLDCB_TLPM_PCI_DM                       0x000A0180
156         GLDCB_TLPM_PCI_DM,
157 #define GLDCB_TLPM_TC2PFC                       0x000A0194
158         GLDCB_TLPM_TC2PFC,
159 #define TCDCB_TLPM_WAIT_DM(_i)                  (0x000A0080 + ((_i) * 4))
160         TCDCB_TLPM_WAIT_DM(0),
161         TCDCB_TLPM_WAIT_DM(1),
162         TCDCB_TLPM_WAIT_DM(2),
163         TCDCB_TLPM_WAIT_DM(3),
164         TCDCB_TLPM_WAIT_DM(4),
165         TCDCB_TLPM_WAIT_DM(5),
166         TCDCB_TLPM_WAIT_DM(6),
167         TCDCB_TLPM_WAIT_DM(7),
168         TCDCB_TLPM_WAIT_DM(8),
169         TCDCB_TLPM_WAIT_DM(9),
170         TCDCB_TLPM_WAIT_DM(10),
171         TCDCB_TLPM_WAIT_DM(11),
172         TCDCB_TLPM_WAIT_DM(12),
173         TCDCB_TLPM_WAIT_DM(13),
174         TCDCB_TLPM_WAIT_DM(14),
175         TCDCB_TLPM_WAIT_DM(15),
176         TCDCB_TLPM_WAIT_DM(16),
177         TCDCB_TLPM_WAIT_DM(17),
178         TCDCB_TLPM_WAIT_DM(18),
179         TCDCB_TLPM_WAIT_DM(19),
180         TCDCB_TLPM_WAIT_DM(20),
181         TCDCB_TLPM_WAIT_DM(21),
182         TCDCB_TLPM_WAIT_DM(22),
183         TCDCB_TLPM_WAIT_DM(23),
184         TCDCB_TLPM_WAIT_DM(24),
185         TCDCB_TLPM_WAIT_DM(25),
186         TCDCB_TLPM_WAIT_DM(26),
187         TCDCB_TLPM_WAIT_DM(27),
188         TCDCB_TLPM_WAIT_DM(28),
189         TCDCB_TLPM_WAIT_DM(29),
190         TCDCB_TLPM_WAIT_DM(30),
191         TCDCB_TLPM_WAIT_DM(31),
192 #define GLPCI_WATMK_CLNT_PIPEMON                0x000BFD90
193         GLPCI_WATMK_CLNT_PIPEMON,
194 #define GLPCI_CUR_CLNT_COMMON                   0x000BFD84
195         GLPCI_CUR_CLNT_COMMON,
196 #define GLPCI_CUR_CLNT_PIPEMON                  0x000BFD88
197         GLPCI_CUR_CLNT_PIPEMON,
198 #define GLPCI_PCIERR                            0x0009DEB0
199         GLPCI_PCIERR,
200 #define GLPSM_DEBUG_CTL_STATUS                  0x000B0600
201         GLPSM_DEBUG_CTL_STATUS,
202 #define GLPSM0_DEBUG_FIFO_OVERFLOW_DETECT       0x000B0680
203         GLPSM0_DEBUG_FIFO_OVERFLOW_DETECT,
204 #define GLPSM0_DEBUG_FIFO_UNDERFLOW_DETECT      0x000B0684
205         GLPSM0_DEBUG_FIFO_UNDERFLOW_DETECT,
206 #define GLPSM0_DEBUG_DT_OUT_OF_WINDOW           0x000B0688
207         GLPSM0_DEBUG_DT_OUT_OF_WINDOW,
208 #define GLPSM0_DEBUG_INTF_HW_ERROR_DETECT       0x000B069C
209         GLPSM0_DEBUG_INTF_HW_ERROR_DETECT,
210 #define GLPSM0_DEBUG_MISC_HW_ERROR_DETECT       0x000B06A0
211         GLPSM0_DEBUG_MISC_HW_ERROR_DETECT,
212 #define GLPSM1_DEBUG_FIFO_OVERFLOW_DETECT       0x000B0E80
213         GLPSM1_DEBUG_FIFO_OVERFLOW_DETECT,
214 #define GLPSM1_DEBUG_FIFO_UNDERFLOW_DETECT      0x000B0E84
215         GLPSM1_DEBUG_FIFO_UNDERFLOW_DETECT,
216 #define GLPSM1_DEBUG_SRL_FIFO_OVERFLOW_DETECT   0x000B0E88
217         GLPSM1_DEBUG_SRL_FIFO_OVERFLOW_DETECT,
218 #define GLPSM1_DEBUG_SRL_FIFO_UNDERFLOW_DETECT  0x000B0E8C
219         GLPSM1_DEBUG_SRL_FIFO_UNDERFLOW_DETECT,
220 #define GLPSM1_DEBUG_MISC_HW_ERROR_DETECT       0x000B0E90
221         GLPSM1_DEBUG_MISC_HW_ERROR_DETECT,
222 #define GLPSM2_DEBUG_FIFO_OVERFLOW_DETECT       0x000B1680
223         GLPSM2_DEBUG_FIFO_OVERFLOW_DETECT,
224 #define GLPSM2_DEBUG_FIFO_UNDERFLOW_DETECT      0x000B1684
225         GLPSM2_DEBUG_FIFO_UNDERFLOW_DETECT,
226 #define GLPSM2_DEBUG_MISC_HW_ERROR_DETECT       0x000B1688
227         GLPSM2_DEBUG_MISC_HW_ERROR_DETECT,
228 #define GLTDPU_TCLAN_COMP_BOB(_i)               (0x00049ADC + ((_i) * 4))
229         GLTDPU_TCLAN_COMP_BOB(1),
230         GLTDPU_TCLAN_COMP_BOB(2),
231         GLTDPU_TCLAN_COMP_BOB(3),
232         GLTDPU_TCLAN_COMP_BOB(4),
233         GLTDPU_TCLAN_COMP_BOB(5),
234         GLTDPU_TCLAN_COMP_BOB(6),
235         GLTDPU_TCLAN_COMP_BOB(7),
236         GLTDPU_TCLAN_COMP_BOB(8),
237 #define GLTDPU_TCB_CMD_BOB(_i)                  (0x0004975C + ((_i) * 4))
238         GLTDPU_TCB_CMD_BOB(1),
239         GLTDPU_TCB_CMD_BOB(2),
240         GLTDPU_TCB_CMD_BOB(3),
241         GLTDPU_TCB_CMD_BOB(4),
242         GLTDPU_TCB_CMD_BOB(5),
243         GLTDPU_TCB_CMD_BOB(6),
244         GLTDPU_TCB_CMD_BOB(7),
245         GLTDPU_TCB_CMD_BOB(8),
246 #define GLTDPU_PSM_UPDATE_BOB(_i)               (0x00049B5C + ((_i) * 4))
247         GLTDPU_PSM_UPDATE_BOB(1),
248         GLTDPU_PSM_UPDATE_BOB(2),
249         GLTDPU_PSM_UPDATE_BOB(3),
250         GLTDPU_PSM_UPDATE_BOB(4),
251         GLTDPU_PSM_UPDATE_BOB(5),
252         GLTDPU_PSM_UPDATE_BOB(6),
253         GLTDPU_PSM_UPDATE_BOB(7),
254         GLTDPU_PSM_UPDATE_BOB(8),
255 #define GLTCB_CMD_IN_BOB(_i)                    (0x000AE288 + ((_i) * 4))
256         GLTCB_CMD_IN_BOB(1),
257         GLTCB_CMD_IN_BOB(2),
258         GLTCB_CMD_IN_BOB(3),
259         GLTCB_CMD_IN_BOB(4),
260         GLTCB_CMD_IN_BOB(5),
261         GLTCB_CMD_IN_BOB(6),
262         GLTCB_CMD_IN_BOB(7),
263         GLTCB_CMD_IN_BOB(8),
264 #define GLLAN_TCLAN_FETCH_CTL_FBK_BOB_CTL(_i)   (0x000FC148 + ((_i) * 4))
265         GLLAN_TCLAN_FETCH_CTL_FBK_BOB_CTL(1),
266         GLLAN_TCLAN_FETCH_CTL_FBK_BOB_CTL(2),
267         GLLAN_TCLAN_FETCH_CTL_FBK_BOB_CTL(3),
268         GLLAN_TCLAN_FETCH_CTL_FBK_BOB_CTL(4),
269         GLLAN_TCLAN_FETCH_CTL_FBK_BOB_CTL(5),
270         GLLAN_TCLAN_FETCH_CTL_FBK_BOB_CTL(6),
271         GLLAN_TCLAN_FETCH_CTL_FBK_BOB_CTL(7),
272         GLLAN_TCLAN_FETCH_CTL_FBK_BOB_CTL(8),
273 #define GLLAN_TCLAN_FETCH_CTL_SCHED_BOB_CTL(_i) (0x000FC248 + ((_i) * 4))
274         GLLAN_TCLAN_FETCH_CTL_SCHED_BOB_CTL(1),
275         GLLAN_TCLAN_FETCH_CTL_SCHED_BOB_CTL(2),
276         GLLAN_TCLAN_FETCH_CTL_SCHED_BOB_CTL(3),
277         GLLAN_TCLAN_FETCH_CTL_SCHED_BOB_CTL(4),
278         GLLAN_TCLAN_FETCH_CTL_SCHED_BOB_CTL(5),
279         GLLAN_TCLAN_FETCH_CTL_SCHED_BOB_CTL(6),
280         GLLAN_TCLAN_FETCH_CTL_SCHED_BOB_CTL(7),
281         GLLAN_TCLAN_FETCH_CTL_SCHED_BOB_CTL(8),
282 #define GLLAN_TCLAN_CACHE_CTL_BOB_CTL(_i)       (0x000FC1C8 + ((_i) * 4))
283         GLLAN_TCLAN_CACHE_CTL_BOB_CTL(1),
284         GLLAN_TCLAN_CACHE_CTL_BOB_CTL(2),
285         GLLAN_TCLAN_CACHE_CTL_BOB_CTL(3),
286         GLLAN_TCLAN_CACHE_CTL_BOB_CTL(4),
287         GLLAN_TCLAN_CACHE_CTL_BOB_CTL(5),
288         GLLAN_TCLAN_CACHE_CTL_BOB_CTL(6),
289         GLLAN_TCLAN_CACHE_CTL_BOB_CTL(7),
290         GLLAN_TCLAN_CACHE_CTL_BOB_CTL(8),
291 #define GLLAN_TCLAN_FETCH_CTL_PROC_BOB_CTL(_i)  (0x000FC188 + ((_i) * 4))
292         GLLAN_TCLAN_FETCH_CTL_PROC_BOB_CTL(1),
293         GLLAN_TCLAN_FETCH_CTL_PROC_BOB_CTL(2),
294         GLLAN_TCLAN_FETCH_CTL_PROC_BOB_CTL(3),
295         GLLAN_TCLAN_FETCH_CTL_PROC_BOB_CTL(4),
296         GLLAN_TCLAN_FETCH_CTL_PROC_BOB_CTL(5),
297         GLLAN_TCLAN_FETCH_CTL_PROC_BOB_CTL(6),
298         GLLAN_TCLAN_FETCH_CTL_PROC_BOB_CTL(7),
299         GLLAN_TCLAN_FETCH_CTL_PROC_BOB_CTL(8),
300 #define GLLAN_TCLAN_FETCH_CTL_PCIE_RD_BOB_CTL(_i) (0x000FC288 + ((_i) * 4))
301         GLLAN_TCLAN_FETCH_CTL_PCIE_RD_BOB_CTL(1),
302         GLLAN_TCLAN_FETCH_CTL_PCIE_RD_BOB_CTL(2),
303         GLLAN_TCLAN_FETCH_CTL_PCIE_RD_BOB_CTL(3),
304         GLLAN_TCLAN_FETCH_CTL_PCIE_RD_BOB_CTL(4),
305         GLLAN_TCLAN_FETCH_CTL_PCIE_RD_BOB_CTL(5),
306         GLLAN_TCLAN_FETCH_CTL_PCIE_RD_BOB_CTL(6),
307         GLLAN_TCLAN_FETCH_CTL_PCIE_RD_BOB_CTL(7),
308         GLLAN_TCLAN_FETCH_CTL_PCIE_RD_BOB_CTL(8),
309 #define PRTDCB_TCUPM_REG_CM(_i)                 (0x000BC360 + ((_i) * 4))
310         PRTDCB_TCUPM_REG_CM(0),
311         PRTDCB_TCUPM_REG_CM(1),
312         PRTDCB_TCUPM_REG_CM(2),
313         PRTDCB_TCUPM_REG_CM(3),
314 #define PRTDCB_TCUPM_REG_DM(_i)                 (0x000BC3A0 + ((_i) * 4))
315         PRTDCB_TCUPM_REG_DM(0),
316         PRTDCB_TCUPM_REG_DM(1),
317         PRTDCB_TCUPM_REG_DM(2),
318         PRTDCB_TCUPM_REG_DM(3),
319 #define PRTDCB_TLPM_REG_DM(_i)                  (0x000A0000 + ((_i) * 4))
320         PRTDCB_TLPM_REG_DM(0),
321         PRTDCB_TLPM_REG_DM(1),
322         PRTDCB_TLPM_REG_DM(2),
323         PRTDCB_TLPM_REG_DM(3),
324 };
325
326 struct ice_priv_flag {
327         char name[ETH_GSTRING_LEN];
328         u32 bitno;                      /* bit position in pf->flags */
329 };
330
331 #define ICE_PRIV_FLAG(_name, _bitno) { \
332         .name = _name, \
333         .bitno = _bitno, \
334 }
335
336 static const struct ice_priv_flag ice_gstrings_priv_flags[] = {
337         ICE_PRIV_FLAG("link-down-on-close", ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA),
338         ICE_PRIV_FLAG("fw-lldp-agent", ICE_FLAG_FW_LLDP_AGENT),
339         ICE_PRIV_FLAG("vf-true-promisc-support",
340                       ICE_FLAG_VF_TRUE_PROMISC_ENA),
341         ICE_PRIV_FLAG("mdd-auto-reset-vf", ICE_FLAG_MDD_AUTO_RESET_VF),
342         ICE_PRIV_FLAG("vf-vlan-pruning", ICE_FLAG_VF_VLAN_PRUNING),
343         ICE_PRIV_FLAG("legacy-rx", ICE_FLAG_LEGACY_RX),
344 };
345
346 #define ICE_PRIV_FLAG_ARRAY_SIZE        ARRAY_SIZE(ice_gstrings_priv_flags)
347
348 static const u32 ice_adv_lnk_speed_100[] __initconst = {
349         ETHTOOL_LINK_MODE_100baseT_Full_BIT,
350 };
351
352 static const u32 ice_adv_lnk_speed_1000[] __initconst = {
353         ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
354         ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
355         ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
356 };
357
358 static const u32 ice_adv_lnk_speed_2500[] __initconst = {
359         ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
360         ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
361 };
362
363 static const u32 ice_adv_lnk_speed_5000[] __initconst = {
364         ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
365 };
366
367 static const u32 ice_adv_lnk_speed_10000[] __initconst = {
368         ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
369         ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
370         ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
371         ETHTOOL_LINK_MODE_10000baseLR_Full_BIT,
372 };
373
374 static const u32 ice_adv_lnk_speed_25000[] __initconst = {
375         ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
376         ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
377         ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
378 };
379
380 static const u32 ice_adv_lnk_speed_40000[] __initconst = {
381         ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
382         ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
383         ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
384         ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
385 };
386
387 static const u32 ice_adv_lnk_speed_50000[] __initconst = {
388         ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
389         ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
390         ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
391 };
392
393 static const u32 ice_adv_lnk_speed_100000[] __initconst = {
394         ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
395         ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
396         ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
397         ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
398         ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT,
399         ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT,
400         ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT,
401 };
402
403 static const u32 ice_adv_lnk_speed_200000[] __initconst = {
404         ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT,
405         ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT,
406         ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT,
407         ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT,
408         ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT,
409 };
410
411 static struct ethtool_forced_speed_map ice_adv_lnk_speed_maps[] __ro_after_init = {
412         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 100),
413         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 1000),
414         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 2500),
415         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 5000),
416         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 10000),
417         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 25000),
418         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 40000),
419         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 50000),
420         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 100000),
421         ETHTOOL_FORCED_SPEED_MAP(ice_adv_lnk_speed, 200000),
422 };
423
424 void __init ice_adv_lnk_speed_maps_init(void)
425 {
426         ethtool_forced_speed_maps_init(ice_adv_lnk_speed_maps,
427                                        ARRAY_SIZE(ice_adv_lnk_speed_maps));
428 }
429
430 static void
431 __ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo,
432                   struct ice_vsi *vsi)
433 {
434         struct ice_pf *pf = vsi->back;
435         struct ice_hw *hw = &pf->hw;
436         struct ice_orom_info *orom;
437         struct ice_nvm_info *nvm;
438
439         nvm = &hw->flash.nvm;
440         orom = &hw->flash.orom;
441
442         strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
443
444         /* Display NVM version (from which the firmware version can be
445          * determined) which contains more pertinent information.
446          */
447         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
448                  "%x.%02x 0x%x %d.%d.%d", nvm->major, nvm->minor,
449                  nvm->eetrack, orom->major, orom->build, orom->patch);
450
451         strscpy(drvinfo->bus_info, pci_name(pf->pdev),
452                 sizeof(drvinfo->bus_info));
453 }
454
455 static void
456 ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
457 {
458         struct ice_netdev_priv *np = netdev_priv(netdev);
459
460         __ice_get_drvinfo(netdev, drvinfo, np->vsi);
461         drvinfo->n_priv_flags = ICE_PRIV_FLAG_ARRAY_SIZE;
462 }
463
464 static int ice_get_regs_len(struct net_device __always_unused *netdev)
465 {
466         return (sizeof(ice_regs_dump_list) +
467                 sizeof(struct ice_regdump_to_ethtool));
468 }
469
470 /**
471  * ice_ethtool_get_maxspeed - Get the max speed for given lport
472  * @hw: pointer to the HW struct
473  * @lport: logical port for which max speed is requested
474  * @max_speed: return max speed for input lport
475  *
476  * Return: 0 on success, negative on failure.
477  */
478 static int ice_ethtool_get_maxspeed(struct ice_hw *hw, u8 lport, u8 *max_speed)
479 {
480         struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX] = {};
481         bool active_valid = false, pending_valid = true;
482         u8 option_count = ICE_AQC_PORT_OPT_MAX;
483         u8 active_idx = 0, pending_idx = 0;
484         int status;
485
486         status = ice_aq_get_port_options(hw, options, &option_count, lport,
487                                          true, &active_idx, &active_valid,
488                                          &pending_idx, &pending_valid);
489         if (status)
490                 return -EIO;
491         if (!active_valid)
492                 return -EINVAL;
493
494         *max_speed = options[active_idx].max_lane_speed & ICE_AQC_PORT_OPT_MAX_LANE_M;
495         return 0;
496 }
497
498 /**
499  * ice_is_serdes_muxed - returns whether serdes is muxed in hardware
500  * @hw: pointer to the HW struct
501  *
502  * Return: true when serdes is muxed, false when serdes is not muxed.
503  */
504 static bool ice_is_serdes_muxed(struct ice_hw *hw)
505 {
506         u32 reg_value = rd32(hw, GLGEN_SWITCH_MODE_CONFIG);
507
508         return FIELD_GET(GLGEN_SWITCH_MODE_CONFIG_25X4_QUAD_M, reg_value);
509 }
510
511 static int ice_map_port_topology_for_sfp(struct ice_port_topology *port_topology,
512                                          u8 lport, bool is_muxed)
513 {
514         switch (lport) {
515         case 0:
516                 port_topology->pcs_quad_select = 0;
517                 port_topology->pcs_port = 0;
518                 port_topology->primary_serdes_lane = 0;
519                 break;
520         case 1:
521                 port_topology->pcs_quad_select = 1;
522                 port_topology->pcs_port = 0;
523                 if (is_muxed)
524                         port_topology->primary_serdes_lane = 2;
525                 else
526                         port_topology->primary_serdes_lane = 4;
527                 break;
528         case 2:
529                 port_topology->pcs_quad_select = 0;
530                 port_topology->pcs_port = 1;
531                 port_topology->primary_serdes_lane = 1;
532                 break;
533         case 3:
534                 port_topology->pcs_quad_select = 1;
535                 port_topology->pcs_port = 1;
536                 if (is_muxed)
537                         port_topology->primary_serdes_lane = 3;
538                 else
539                         port_topology->primary_serdes_lane = 5;
540                 break;
541         case 4:
542                 port_topology->pcs_quad_select = 0;
543                 port_topology->pcs_port = 2;
544                 port_topology->primary_serdes_lane = 2;
545                 break;
546         case 5:
547                 port_topology->pcs_quad_select = 1;
548                 port_topology->pcs_port = 2;
549                 port_topology->primary_serdes_lane = 6;
550                 break;
551         case 6:
552                 port_topology->pcs_quad_select = 0;
553                 port_topology->pcs_port = 3;
554                 port_topology->primary_serdes_lane = 3;
555                 break;
556         case 7:
557                 port_topology->pcs_quad_select = 1;
558                 port_topology->pcs_port = 3;
559                 port_topology->primary_serdes_lane = 7;
560                 break;
561         default:
562                 return -EINVAL;
563         }
564
565         return 0;
566 }
567
568 static int ice_map_port_topology_for_qsfp(struct ice_port_topology *port_topology,
569                                           u8 lport, bool is_muxed)
570 {
571         switch (lport) {
572         case 0:
573                 port_topology->pcs_quad_select = 0;
574                 port_topology->pcs_port = 0;
575                 port_topology->primary_serdes_lane = 0;
576                 break;
577         case 1:
578                 port_topology->pcs_quad_select = 1;
579                 port_topology->pcs_port = 0;
580                 if (is_muxed)
581                         port_topology->primary_serdes_lane = 2;
582                 else
583                         port_topology->primary_serdes_lane = 4;
584                 break;
585         case 2:
586                 port_topology->pcs_quad_select = 0;
587                 port_topology->pcs_port = 1;
588                 port_topology->primary_serdes_lane = 1;
589                 break;
590         case 3:
591                 port_topology->pcs_quad_select = 1;
592                 port_topology->pcs_port = 1;
593                 if (is_muxed)
594                         port_topology->primary_serdes_lane = 3;
595                 else
596                         port_topology->primary_serdes_lane = 5;
597                 break;
598         case 4:
599                 port_topology->pcs_quad_select = 0;
600                 port_topology->pcs_port = 2;
601                 port_topology->primary_serdes_lane = 2;
602                 break;
603         case 5:
604                 port_topology->pcs_quad_select = 1;
605                 port_topology->pcs_port = 2;
606                 port_topology->primary_serdes_lane = 6;
607                 break;
608         case 6:
609                 port_topology->pcs_quad_select = 0;
610                 port_topology->pcs_port = 3;
611                 port_topology->primary_serdes_lane = 3;
612                 break;
613         case 7:
614                 port_topology->pcs_quad_select = 1;
615                 port_topology->pcs_port = 3;
616                 port_topology->primary_serdes_lane = 7;
617                 break;
618         default:
619                 return -EINVAL;
620         }
621
622         return 0;
623 }
624
625 /**
626  * ice_get_port_topology - returns physical topology like pcsquad, pcsport,
627  *                         serdes number
628  * @hw: pointer to the HW struct
629  * @lport: logical port for which physical info requested
630  * @port_topology: buffer to hold port topology
631  *
632  * Return: 0 on success, negative on failure.
633  */
634 static int ice_get_port_topology(struct ice_hw *hw, u8 lport,
635                                  struct ice_port_topology *port_topology)
636 {
637         struct ice_aqc_get_link_topo cmd = {};
638         u16 node_handle = 0;
639         u8 cage_type = 0;
640         bool is_muxed;
641         int err;
642         u8 ctx;
643
644         ctx = ICE_AQC_LINK_TOPO_NODE_TYPE_CAGE << ICE_AQC_LINK_TOPO_NODE_TYPE_S;
645         ctx |= ICE_AQC_LINK_TOPO_NODE_CTX_PORT << ICE_AQC_LINK_TOPO_NODE_CTX_S;
646         cmd.addr.topo_params.node_type_ctx = ctx;
647
648         err = ice_aq_get_netlist_node(hw, &cmd, &cage_type, &node_handle);
649         if (err)
650                 return -EINVAL;
651
652         is_muxed = ice_is_serdes_muxed(hw);
653
654         if (cage_type == 0x11 ||        /* SFP+ */
655             cage_type == 0x12) {        /* SFP28 */
656                 port_topology->serdes_lane_count = 1;
657                 err = ice_map_port_topology_for_sfp(port_topology, lport, is_muxed);
658                 if (err)
659                         return err;
660         } else if (cage_type == 0x13 || /* QSFP */
661                    cage_type == 0x14) { /* QSFP28 */
662                 u8 max_speed = 0;
663
664                 err = ice_ethtool_get_maxspeed(hw, lport, &max_speed);
665                 if (err)
666                         return err;
667
668                 if (max_speed == ICE_AQC_PORT_OPT_MAX_LANE_100G)
669                         port_topology->serdes_lane_count = 4;
670                 else if (max_speed == ICE_AQC_PORT_OPT_MAX_LANE_50G)
671                         port_topology->serdes_lane_count = 2;
672                 else
673                         port_topology->serdes_lane_count = 1;
674
675                 err = ice_map_port_topology_for_qsfp(port_topology, lport, is_muxed);
676                 if (err)
677                         return err;
678         } else {
679                 return -EINVAL;
680         }
681
682         return 0;
683 }
684
685 /**
686  * ice_get_tx_rx_equa - read serdes tx rx equaliser param
687  * @hw: pointer to the HW struct
688  * @serdes_num: represents the serdes number
689  * @ptr: structure to read all serdes parameter for given serdes
690  *
691  * Return: all serdes equalization parameter supported per serdes number
692  */
693 static int ice_get_tx_rx_equa(struct ice_hw *hw, u8 serdes_num,
694                               struct ice_serdes_equalization_to_ethtool *ptr)
695 {
696         int err;
697
698         err = ice_aq_get_phy_equalization(hw, ICE_AQC_TX_EQU_PRE1,
699                                           ICE_AQC_OP_CODE_TX_EQU, serdes_num,
700                                           &ptr->tx_equalization_pre1);
701         if (err)
702                 return err;
703
704         err = ice_aq_get_phy_equalization(hw, ICE_AQC_TX_EQU_PRE3,
705                                           ICE_AQC_OP_CODE_TX_EQU, serdes_num,
706                                           &ptr->tx_equalization_pre3);
707         if (err)
708                 return err;
709
710         err = ice_aq_get_phy_equalization(hw, ICE_AQC_TX_EQU_ATTEN,
711                                           ICE_AQC_OP_CODE_TX_EQU, serdes_num,
712                                           &ptr->tx_equalization_atten);
713         if (err)
714                 return err;
715
716         err = ice_aq_get_phy_equalization(hw, ICE_AQC_TX_EQU_POST1,
717                                           ICE_AQC_OP_CODE_TX_EQU, serdes_num,
718                                           &ptr->tx_equalization_post1);
719         if (err)
720                 return err;
721
722         err = ice_aq_get_phy_equalization(hw, ICE_AQC_TX_EQU_PRE2,
723                                           ICE_AQC_OP_CODE_TX_EQU, serdes_num,
724                                           &ptr->tx_equalization_pre2);
725         if (err)
726                 return err;
727
728         err = ice_aq_get_phy_equalization(hw, ICE_AQC_RX_EQU_PRE2,
729                                           ICE_AQC_OP_CODE_RX_EQU, serdes_num,
730                                           &ptr->rx_equalization_pre2);
731         if (err)
732                 return err;
733
734         err = ice_aq_get_phy_equalization(hw, ICE_AQC_RX_EQU_PRE1,
735                                           ICE_AQC_OP_CODE_RX_EQU, serdes_num,
736                                           &ptr->rx_equalization_pre1);
737         if (err)
738                 return err;
739
740         err = ice_aq_get_phy_equalization(hw, ICE_AQC_RX_EQU_POST1,
741                                           ICE_AQC_OP_CODE_RX_EQU, serdes_num,
742                                           &ptr->rx_equalization_post1);
743         if (err)
744                 return err;
745
746         err = ice_aq_get_phy_equalization(hw, ICE_AQC_RX_EQU_BFLF,
747                                           ICE_AQC_OP_CODE_RX_EQU, serdes_num,
748                                           &ptr->rx_equalization_bflf);
749         if (err)
750                 return err;
751
752         err = ice_aq_get_phy_equalization(hw, ICE_AQC_RX_EQU_BFHF,
753                                           ICE_AQC_OP_CODE_RX_EQU, serdes_num,
754                                           &ptr->rx_equalization_bfhf);
755         if (err)
756                 return err;
757
758         err = ice_aq_get_phy_equalization(hw, ICE_AQC_RX_EQU_DRATE,
759                                           ICE_AQC_OP_CODE_RX_EQU, serdes_num,
760                                           &ptr->rx_equalization_drate);
761         if (err)
762                 return err;
763
764         return 0;
765 }
766
767 /**
768  * ice_get_extended_regs - returns FEC correctable, uncorrectable stats per
769  *                         pcsquad, pcsport
770  * @netdev: pointer to net device structure
771  * @p: output buffer to fill requested register dump
772  *
773  * Return: 0 on success, negative on failure.
774  */
775 static int ice_get_extended_regs(struct net_device *netdev, void *p)
776 {
777         struct ice_netdev_priv *np = netdev_priv(netdev);
778         struct ice_regdump_to_ethtool *ice_prv_regs_buf;
779         struct ice_port_topology port_topology = {};
780         struct ice_port_info *pi;
781         struct ice_pf *pf;
782         struct ice_hw *hw;
783         unsigned int i;
784         int err;
785
786         pf = np->vsi->back;
787         hw = &pf->hw;
788         pi = np->vsi->port_info;
789
790         /* Serdes parameters are not supported if not the PF VSI */
791         if (np->vsi->type != ICE_VSI_PF || !pi)
792                 return -EINVAL;
793
794         err = ice_get_port_topology(hw, pi->lport, &port_topology);
795         if (err)
796                 return -EINVAL;
797         if (port_topology.serdes_lane_count > 4)
798                 return -EINVAL;
799
800         ice_prv_regs_buf = p;
801
802         /* Get serdes equalization parameter for available serdes */
803         for (i = 0; i < port_topology.serdes_lane_count; i++) {
804                 u8 serdes_num = 0;
805
806                 serdes_num = port_topology.primary_serdes_lane + i;
807                 err = ice_get_tx_rx_equa(hw, serdes_num,
808                                          &ice_prv_regs_buf->equalization[i]);
809                 if (err)
810                         return -EINVAL;
811         }
812
813         return 0;
814 }
815
816 static void
817 ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p)
818 {
819         struct ice_netdev_priv *np = netdev_priv(netdev);
820         struct ice_pf *pf = np->vsi->back;
821         struct ice_hw *hw = &pf->hw;
822         u32 *regs_buf = (u32 *)p;
823         unsigned int i;
824
825         regs->version = 2;
826
827         for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i)
828                 regs_buf[i] = rd32(hw, ice_regs_dump_list[i]);
829
830         ice_get_extended_regs(netdev, (void *)&regs_buf[i]);
831 }
832
833 static u32 ice_get_msglevel(struct net_device *netdev)
834 {
835         struct ice_netdev_priv *np = netdev_priv(netdev);
836         struct ice_pf *pf = np->vsi->back;
837
838 #ifndef CONFIG_DYNAMIC_DEBUG
839         if (pf->hw.debug_mask)
840                 netdev_info(netdev, "hw debug_mask: 0x%llX\n",
841                             pf->hw.debug_mask);
842 #endif /* !CONFIG_DYNAMIC_DEBUG */
843
844         return pf->msg_enable;
845 }
846
847 static void ice_set_msglevel(struct net_device *netdev, u32 data)
848 {
849         struct ice_netdev_priv *np = netdev_priv(netdev);
850         struct ice_pf *pf = np->vsi->back;
851
852 #ifndef CONFIG_DYNAMIC_DEBUG
853         if (ICE_DBG_USER & data)
854                 pf->hw.debug_mask = data;
855         else
856                 pf->msg_enable = data;
857 #else
858         pf->msg_enable = data;
859 #endif /* !CONFIG_DYNAMIC_DEBUG */
860 }
861
862 static int ice_get_eeprom_len(struct net_device *netdev)
863 {
864         struct ice_netdev_priv *np = netdev_priv(netdev);
865         struct ice_pf *pf = np->vsi->back;
866
867         return (int)pf->hw.flash.flash_size;
868 }
869
870 static int
871 ice_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
872                u8 *bytes)
873 {
874         struct ice_netdev_priv *np = netdev_priv(netdev);
875         struct ice_vsi *vsi = np->vsi;
876         struct ice_pf *pf = vsi->back;
877         struct ice_hw *hw = &pf->hw;
878         struct device *dev;
879         int ret;
880         u8 *buf;
881
882         dev = ice_pf_to_dev(pf);
883
884         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
885         netdev_dbg(netdev, "GEEPROM cmd 0x%08x, offset 0x%08x, len 0x%08x\n",
886                    eeprom->cmd, eeprom->offset, eeprom->len);
887
888         buf = kzalloc(eeprom->len, GFP_KERNEL);
889         if (!buf)
890                 return -ENOMEM;
891
892         ret = ice_acquire_nvm(hw, ICE_RES_READ);
893         if (ret) {
894                 dev_err(dev, "ice_acquire_nvm failed, err %d aq_err %s\n",
895                         ret, ice_aq_str(hw->adminq.sq_last_status));
896                 goto out;
897         }
898
899         ret = ice_read_flat_nvm(hw, eeprom->offset, &eeprom->len, buf,
900                                 false);
901         if (ret) {
902                 dev_err(dev, "ice_read_flat_nvm failed, err %d aq_err %s\n",
903                         ret, ice_aq_str(hw->adminq.sq_last_status));
904                 goto release;
905         }
906
907         memcpy(bytes, buf, eeprom->len);
908 release:
909         ice_release_nvm(hw);
910 out:
911         kfree(buf);
912         return ret;
913 }
914
915 /**
916  * ice_active_vfs - check if there are any active VFs
917  * @pf: board private structure
918  *
919  * Returns true if an active VF is found, otherwise returns false
920  */
921 static bool ice_active_vfs(struct ice_pf *pf)
922 {
923         bool active = false;
924         struct ice_vf *vf;
925         unsigned int bkt;
926
927         rcu_read_lock();
928         ice_for_each_vf_rcu(pf, bkt, vf) {
929                 if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
930                         active = true;
931                         break;
932                 }
933         }
934         rcu_read_unlock();
935
936         return active;
937 }
938
939 /**
940  * ice_link_test - perform a link test on a given net_device
941  * @netdev: network interface device structure
942  *
943  * This function performs one of the self-tests required by ethtool.
944  * Returns 0 on success, non-zero on failure.
945  */
946 static u64 ice_link_test(struct net_device *netdev)
947 {
948         struct ice_netdev_priv *np = netdev_priv(netdev);
949         bool link_up = false;
950         int status;
951
952         netdev_info(netdev, "link test\n");
953         status = ice_get_link_status(np->vsi->port_info, &link_up);
954         if (status) {
955                 netdev_err(netdev, "link query error, status = %d\n",
956                            status);
957                 return 1;
958         }
959
960         if (!link_up)
961                 return 2;
962
963         return 0;
964 }
965
966 /**
967  * ice_eeprom_test - perform an EEPROM test on a given net_device
968  * @netdev: network interface device structure
969  *
970  * This function performs one of the self-tests required by ethtool.
971  * Returns 0 on success, non-zero on failure.
972  */
973 static u64 ice_eeprom_test(struct net_device *netdev)
974 {
975         struct ice_netdev_priv *np = netdev_priv(netdev);
976         struct ice_pf *pf = np->vsi->back;
977
978         netdev_info(netdev, "EEPROM test\n");
979         return !!(ice_nvm_validate_checksum(&pf->hw));
980 }
981
982 /**
983  * ice_reg_pattern_test
984  * @hw: pointer to the HW struct
985  * @reg: reg to be tested
986  * @mask: bits to be touched
987  */
988 static int ice_reg_pattern_test(struct ice_hw *hw, u32 reg, u32 mask)
989 {
990         struct ice_pf *pf = (struct ice_pf *)hw->back;
991         struct device *dev = ice_pf_to_dev(pf);
992         static const u32 patterns[] = {
993                 0x5A5A5A5A, 0xA5A5A5A5,
994                 0x00000000, 0xFFFFFFFF
995         };
996         u32 val, orig_val;
997         unsigned int i;
998
999         orig_val = rd32(hw, reg);
1000         for (i = 0; i < ARRAY_SIZE(patterns); ++i) {
1001                 u32 pattern = patterns[i] & mask;
1002
1003                 wr32(hw, reg, pattern);
1004                 val = rd32(hw, reg);
1005                 if (val == pattern)
1006                         continue;
1007                 dev_err(dev, "%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n"
1008                         , __func__, reg, pattern, val);
1009                 return 1;
1010         }
1011
1012         wr32(hw, reg, orig_val);
1013         val = rd32(hw, reg);
1014         if (val != orig_val) {
1015                 dev_err(dev, "%s: reg restore test failed - reg 0x%08x orig 0x%08x val 0x%08x\n"
1016                         , __func__, reg, orig_val, val);
1017                 return 1;
1018         }
1019
1020         return 0;
1021 }
1022
1023 /**
1024  * ice_reg_test - perform a register test on a given net_device
1025  * @netdev: network interface device structure
1026  *
1027  * This function performs one of the self-tests required by ethtool.
1028  * Returns 0 on success, non-zero on failure.
1029  */
1030 static u64 ice_reg_test(struct net_device *netdev)
1031 {
1032         struct ice_netdev_priv *np = netdev_priv(netdev);
1033         struct ice_hw *hw = np->vsi->port_info->hw;
1034         u32 int_elements = hw->func_caps.common_cap.num_msix_vectors ?
1035                 hw->func_caps.common_cap.num_msix_vectors - 1 : 1;
1036         struct ice_diag_reg_test_info {
1037                 u32 address;
1038                 u32 mask;
1039                 u32 elem_num;
1040                 u32 elem_size;
1041         } ice_reg_list[] = {
1042                 {GLINT_ITR(0, 0), 0x00000fff, int_elements,
1043                         GLINT_ITR(0, 1) - GLINT_ITR(0, 0)},
1044                 {GLINT_ITR(1, 0), 0x00000fff, int_elements,
1045                         GLINT_ITR(1, 1) - GLINT_ITR(1, 0)},
1046                 {GLINT_ITR(0, 0), 0x00000fff, int_elements,
1047                         GLINT_ITR(2, 1) - GLINT_ITR(2, 0)},
1048                 {GLINT_CTL, 0xffff0001, 1, 0}
1049         };
1050         unsigned int i;
1051
1052         netdev_dbg(netdev, "Register test\n");
1053         for (i = 0; i < ARRAY_SIZE(ice_reg_list); ++i) {
1054                 u32 j;
1055
1056                 for (j = 0; j < ice_reg_list[i].elem_num; ++j) {
1057                         u32 mask = ice_reg_list[i].mask;
1058                         u32 reg = ice_reg_list[i].address +
1059                                 (j * ice_reg_list[i].elem_size);
1060
1061                         /* bail on failure (non-zero return) */
1062                         if (ice_reg_pattern_test(hw, reg, mask))
1063                                 return 1;
1064                 }
1065         }
1066
1067         return 0;
1068 }
1069
1070 /**
1071  * ice_lbtest_prepare_rings - configure Tx/Rx test rings
1072  * @vsi: pointer to the VSI structure
1073  *
1074  * Function configures rings of a VSI for loopback test without
1075  * enabling interrupts or informing the kernel about new queues.
1076  *
1077  * Returns 0 on success, negative on failure.
1078  */
1079 static int ice_lbtest_prepare_rings(struct ice_vsi *vsi)
1080 {
1081         int status;
1082
1083         status = ice_vsi_setup_tx_rings(vsi);
1084         if (status)
1085                 goto err_setup_tx_ring;
1086
1087         status = ice_vsi_setup_rx_rings(vsi);
1088         if (status)
1089                 goto err_setup_rx_ring;
1090
1091         status = ice_vsi_cfg_lan(vsi);
1092         if (status)
1093                 goto err_setup_rx_ring;
1094
1095         status = ice_vsi_start_all_rx_rings(vsi);
1096         if (status)
1097                 goto err_start_rx_ring;
1098
1099         return 0;
1100
1101 err_start_rx_ring:
1102         ice_vsi_free_rx_rings(vsi);
1103 err_setup_rx_ring:
1104         ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
1105 err_setup_tx_ring:
1106         ice_vsi_free_tx_rings(vsi);
1107
1108         return status;
1109 }
1110
1111 /**
1112  * ice_lbtest_disable_rings - disable Tx/Rx test rings after loopback test
1113  * @vsi: pointer to the VSI structure
1114  *
1115  * Function stops and frees VSI rings after a loopback test.
1116  * Returns 0 on success, negative on failure.
1117  */
1118 static int ice_lbtest_disable_rings(struct ice_vsi *vsi)
1119 {
1120         int status;
1121
1122         status = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
1123         if (status)
1124                 netdev_err(vsi->netdev, "Failed to stop Tx rings, VSI %d error %d\n",
1125                            vsi->vsi_num, status);
1126
1127         status = ice_vsi_stop_all_rx_rings(vsi);
1128         if (status)
1129                 netdev_err(vsi->netdev, "Failed to stop Rx rings, VSI %d error %d\n",
1130                            vsi->vsi_num, status);
1131
1132         ice_vsi_free_tx_rings(vsi);
1133         ice_vsi_free_rx_rings(vsi);
1134
1135         return status;
1136 }
1137
1138 /**
1139  * ice_lbtest_create_frame - create test packet
1140  * @pf: pointer to the PF structure
1141  * @ret_data: allocated frame buffer
1142  * @size: size of the packet data
1143  *
1144  * Function allocates a frame with a test pattern on specific offsets.
1145  * Returns 0 on success, non-zero on failure.
1146  */
1147 static int ice_lbtest_create_frame(struct ice_pf *pf, u8 **ret_data, u16 size)
1148 {
1149         u8 *data;
1150
1151         if (!pf)
1152                 return -EINVAL;
1153
1154         data = kzalloc(size, GFP_KERNEL);
1155         if (!data)
1156                 return -ENOMEM;
1157
1158         /* Since the ethernet test frame should always be at least
1159          * 64 bytes long, fill some octets in the payload with test data.
1160          */
1161         memset(data, 0xFF, size);
1162         data[32] = 0xDE;
1163         data[42] = 0xAD;
1164         data[44] = 0xBE;
1165         data[46] = 0xEF;
1166
1167         *ret_data = data;
1168
1169         return 0;
1170 }
1171
1172 /**
1173  * ice_lbtest_check_frame - verify received loopback frame
1174  * @frame: pointer to the raw packet data
1175  *
1176  * Function verifies received test frame with a pattern.
1177  * Returns true if frame matches the pattern, false otherwise.
1178  */
1179 static bool ice_lbtest_check_frame(u8 *frame)
1180 {
1181         /* Validate bytes of a frame under offsets chosen earlier */
1182         if (frame[32] == 0xDE &&
1183             frame[42] == 0xAD &&
1184             frame[44] == 0xBE &&
1185             frame[46] == 0xEF &&
1186             frame[48] == 0xFF)
1187                 return true;
1188
1189         return false;
1190 }
1191
1192 /**
1193  * ice_diag_send - send test frames to the test ring
1194  * @tx_ring: pointer to the transmit ring
1195  * @data: pointer to the raw packet data
1196  * @size: size of the packet to send
1197  *
1198  * Function sends loopback packets on a test Tx ring.
1199  */
1200 static int ice_diag_send(struct ice_tx_ring *tx_ring, u8 *data, u16 size)
1201 {
1202         struct ice_tx_desc *tx_desc;
1203         struct ice_tx_buf *tx_buf;
1204         dma_addr_t dma;
1205         u64 td_cmd;
1206
1207         tx_desc = ICE_TX_DESC(tx_ring, tx_ring->next_to_use);
1208         tx_buf = &tx_ring->tx_buf[tx_ring->next_to_use];
1209
1210         dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE);
1211         if (dma_mapping_error(tx_ring->dev, dma))
1212                 return -EINVAL;
1213
1214         tx_desc->buf_addr = cpu_to_le64(dma);
1215
1216         /* These flags are required for a descriptor to be pushed out */
1217         td_cmd = (u64)(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS);
1218         tx_desc->cmd_type_offset_bsz =
1219                 cpu_to_le64(ICE_TX_DESC_DTYPE_DATA |
1220                             (td_cmd << ICE_TXD_QW1_CMD_S) |
1221                             ((u64)0 << ICE_TXD_QW1_OFFSET_S) |
1222                             ((u64)size << ICE_TXD_QW1_TX_BUF_SZ_S) |
1223                             ((u64)0 << ICE_TXD_QW1_L2TAG1_S));
1224
1225         tx_buf->next_to_watch = tx_desc;
1226
1227         /* Force memory write to complete before letting h/w know
1228          * there are new descriptors to fetch.
1229          */
1230         wmb();
1231
1232         tx_ring->next_to_use++;
1233         if (tx_ring->next_to_use >= tx_ring->count)
1234                 tx_ring->next_to_use = 0;
1235
1236         writel_relaxed(tx_ring->next_to_use, tx_ring->tail);
1237
1238         /* Wait until the packets get transmitted to the receive queue. */
1239         usleep_range(1000, 2000);
1240         dma_unmap_single(tx_ring->dev, dma, size, DMA_TO_DEVICE);
1241
1242         return 0;
1243 }
1244
1245 #define ICE_LB_FRAME_SIZE 64
1246 /**
1247  * ice_lbtest_receive_frames - receive and verify test frames
1248  * @rx_ring: pointer to the receive ring
1249  *
1250  * Function receives loopback packets and verify their correctness.
1251  * Returns number of received valid frames.
1252  */
1253 static int ice_lbtest_receive_frames(struct ice_rx_ring *rx_ring)
1254 {
1255         struct ice_rx_buf *rx_buf;
1256         int valid_frames, i;
1257         u8 *received_buf;
1258
1259         valid_frames = 0;
1260
1261         for (i = 0; i < rx_ring->count; i++) {
1262                 union ice_32b_rx_flex_desc *rx_desc;
1263
1264                 rx_desc = ICE_RX_DESC(rx_ring, i);
1265
1266                 if (!(rx_desc->wb.status_error0 &
1267                     (cpu_to_le16(BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S)) |
1268                      cpu_to_le16(BIT(ICE_RX_FLEX_DESC_STATUS0_EOF_S)))))
1269                         continue;
1270
1271                 rx_buf = &rx_ring->rx_buf[i];
1272                 received_buf = page_address(rx_buf->page) + rx_buf->page_offset;
1273
1274                 if (ice_lbtest_check_frame(received_buf))
1275                         valid_frames++;
1276         }
1277
1278         return valid_frames;
1279 }
1280
1281 /**
1282  * ice_loopback_test - perform a loopback test on a given net_device
1283  * @netdev: network interface device structure
1284  *
1285  * This function performs one of the self-tests required by ethtool.
1286  * Returns 0 on success, non-zero on failure.
1287  */
1288 static u64 ice_loopback_test(struct net_device *netdev)
1289 {
1290         struct ice_netdev_priv *np = netdev_priv(netdev);
1291         struct ice_vsi *orig_vsi = np->vsi, *test_vsi;
1292         struct ice_pf *pf = orig_vsi->back;
1293         u8 *tx_frame __free(kfree) = NULL;
1294         u8 broadcast[ETH_ALEN], ret = 0;
1295         int num_frames, valid_frames;
1296         struct ice_tx_ring *tx_ring;
1297         struct ice_rx_ring *rx_ring;
1298         int i;
1299
1300         netdev_info(netdev, "loopback test\n");
1301
1302         test_vsi = ice_lb_vsi_setup(pf, pf->hw.port_info);
1303         if (!test_vsi) {
1304                 netdev_err(netdev, "Failed to create a VSI for the loopback test\n");
1305                 return 1;
1306         }
1307
1308         test_vsi->netdev = netdev;
1309         tx_ring = test_vsi->tx_rings[0];
1310         rx_ring = test_vsi->rx_rings[0];
1311
1312         if (ice_lbtest_prepare_rings(test_vsi)) {
1313                 ret = 2;
1314                 goto lbtest_vsi_close;
1315         }
1316
1317         if (ice_alloc_rx_bufs(rx_ring, rx_ring->count)) {
1318                 ret = 3;
1319                 goto lbtest_rings_dis;
1320         }
1321
1322         /* Enable MAC loopback in firmware */
1323         if (ice_aq_set_mac_loopback(&pf->hw, true, NULL)) {
1324                 ret = 4;
1325                 goto lbtest_mac_dis;
1326         }
1327
1328         /* Test VSI needs to receive broadcast packets */
1329         eth_broadcast_addr(broadcast);
1330         if (ice_fltr_add_mac(test_vsi, broadcast, ICE_FWD_TO_VSI)) {
1331                 ret = 5;
1332                 goto lbtest_mac_dis;
1333         }
1334
1335         if (ice_lbtest_create_frame(pf, &tx_frame, ICE_LB_FRAME_SIZE)) {
1336                 ret = 7;
1337                 goto remove_mac_filters;
1338         }
1339
1340         num_frames = min_t(int, tx_ring->count, 32);
1341         for (i = 0; i < num_frames; i++) {
1342                 if (ice_diag_send(tx_ring, tx_frame, ICE_LB_FRAME_SIZE)) {
1343                         ret = 8;
1344                         goto remove_mac_filters;
1345                 }
1346         }
1347
1348         valid_frames = ice_lbtest_receive_frames(rx_ring);
1349         if (!valid_frames)
1350                 ret = 9;
1351         else if (valid_frames != num_frames)
1352                 ret = 10;
1353
1354 remove_mac_filters:
1355         if (ice_fltr_remove_mac(test_vsi, broadcast, ICE_FWD_TO_VSI))
1356                 netdev_err(netdev, "Could not remove MAC filter for the test VSI\n");
1357 lbtest_mac_dis:
1358         /* Disable MAC loopback after the test is completed. */
1359         if (ice_aq_set_mac_loopback(&pf->hw, false, NULL))
1360                 netdev_err(netdev, "Could not disable MAC loopback\n");
1361 lbtest_rings_dis:
1362         if (ice_lbtest_disable_rings(test_vsi))
1363                 netdev_err(netdev, "Could not disable test rings\n");
1364 lbtest_vsi_close:
1365         test_vsi->netdev = NULL;
1366         if (ice_vsi_release(test_vsi))
1367                 netdev_err(netdev, "Failed to remove the test VSI\n");
1368
1369         return ret;
1370 }
1371
1372 /**
1373  * ice_intr_test - perform an interrupt test on a given net_device
1374  * @netdev: network interface device structure
1375  *
1376  * This function performs one of the self-tests required by ethtool.
1377  * Returns 0 on success, non-zero on failure.
1378  */
1379 static u64 ice_intr_test(struct net_device *netdev)
1380 {
1381         struct ice_netdev_priv *np = netdev_priv(netdev);
1382         struct ice_pf *pf = np->vsi->back;
1383         u16 swic_old = pf->sw_int_count;
1384
1385         netdev_info(netdev, "interrupt test\n");
1386
1387         wr32(&pf->hw, GLINT_DYN_CTL(pf->oicr_irq.index),
1388              GLINT_DYN_CTL_SW_ITR_INDX_M |
1389              GLINT_DYN_CTL_INTENA_MSK_M |
1390              GLINT_DYN_CTL_SWINT_TRIG_M);
1391
1392         usleep_range(1000, 2000);
1393         return (swic_old == pf->sw_int_count);
1394 }
1395
1396 /**
1397  * ice_self_test - handler function for performing a self-test by ethtool
1398  * @netdev: network interface device structure
1399  * @eth_test: ethtool_test structure
1400  * @data: required by ethtool.self_test
1401  *
1402  * This function is called after invoking 'ethtool -t devname' command where
1403  * devname is the name of the network device on which ethtool should operate.
1404  * It performs a set of self-tests to check if a device works properly.
1405  */
1406 static void
1407 ice_self_test(struct net_device *netdev, struct ethtool_test *eth_test,
1408               u64 *data)
1409 {
1410         struct ice_netdev_priv *np = netdev_priv(netdev);
1411         bool if_running = netif_running(netdev);
1412         struct ice_pf *pf = np->vsi->back;
1413         struct device *dev;
1414
1415         dev = ice_pf_to_dev(pf);
1416
1417         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
1418                 netdev_info(netdev, "offline testing starting\n");
1419
1420                 set_bit(ICE_TESTING, pf->state);
1421
1422                 if (ice_active_vfs(pf)) {
1423                         dev_warn(dev, "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
1424                         data[ICE_ETH_TEST_REG] = 1;
1425                         data[ICE_ETH_TEST_EEPROM] = 1;
1426                         data[ICE_ETH_TEST_INTR] = 1;
1427                         data[ICE_ETH_TEST_LOOP] = 1;
1428                         data[ICE_ETH_TEST_LINK] = 1;
1429                         eth_test->flags |= ETH_TEST_FL_FAILED;
1430                         clear_bit(ICE_TESTING, pf->state);
1431                         goto skip_ol_tests;
1432                 }
1433                 /* If the device is online then take it offline */
1434                 if (if_running)
1435                         /* indicate we're in test mode */
1436                         ice_stop(netdev);
1437
1438                 data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
1439                 data[ICE_ETH_TEST_EEPROM] = ice_eeprom_test(netdev);
1440                 data[ICE_ETH_TEST_INTR] = ice_intr_test(netdev);
1441                 data[ICE_ETH_TEST_LOOP] = ice_loopback_test(netdev);
1442                 data[ICE_ETH_TEST_REG] = ice_reg_test(netdev);
1443
1444                 if (data[ICE_ETH_TEST_LINK] ||
1445                     data[ICE_ETH_TEST_EEPROM] ||
1446                     data[ICE_ETH_TEST_LOOP] ||
1447                     data[ICE_ETH_TEST_INTR] ||
1448                     data[ICE_ETH_TEST_REG])
1449                         eth_test->flags |= ETH_TEST_FL_FAILED;
1450
1451                 clear_bit(ICE_TESTING, pf->state);
1452
1453                 if (if_running) {
1454                         int status = ice_open(netdev);
1455
1456                         if (status) {
1457                                 dev_err(dev, "Could not open device %s, err %d\n",
1458                                         pf->int_name, status);
1459                         }
1460                 }
1461         } else {
1462                 /* Online tests */
1463                 netdev_info(netdev, "online testing starting\n");
1464
1465                 data[ICE_ETH_TEST_LINK] = ice_link_test(netdev);
1466                 if (data[ICE_ETH_TEST_LINK])
1467                         eth_test->flags |= ETH_TEST_FL_FAILED;
1468
1469                 /* Offline only tests, not run in online; pass by default */
1470                 data[ICE_ETH_TEST_REG] = 0;
1471                 data[ICE_ETH_TEST_EEPROM] = 0;
1472                 data[ICE_ETH_TEST_INTR] = 0;
1473                 data[ICE_ETH_TEST_LOOP] = 0;
1474         }
1475
1476 skip_ol_tests:
1477         netdev_info(netdev, "testing finished\n");
1478 }
1479
1480 static void
1481 __ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data,
1482                   struct ice_vsi *vsi)
1483 {
1484         unsigned int i;
1485         u8 *p = data;
1486
1487         switch (stringset) {
1488         case ETH_SS_STATS:
1489                 for (i = 0; i < ICE_VSI_STATS_LEN; i++)
1490                         ethtool_puts(&p, ice_gstrings_vsi_stats[i].stat_string);
1491
1492                 if (ice_is_port_repr_netdev(netdev))
1493                         return;
1494
1495                 ice_for_each_alloc_txq(vsi, i) {
1496                         ethtool_sprintf(&p, "tx_queue_%u_packets", i);
1497                         ethtool_sprintf(&p, "tx_queue_%u_bytes", i);
1498                 }
1499
1500                 ice_for_each_alloc_rxq(vsi, i) {
1501                         ethtool_sprintf(&p, "rx_queue_%u_packets", i);
1502                         ethtool_sprintf(&p, "rx_queue_%u_bytes", i);
1503                 }
1504
1505                 if (vsi->type != ICE_VSI_PF)
1506                         return;
1507
1508                 for (i = 0; i < ICE_PF_STATS_LEN; i++)
1509                         ethtool_puts(&p, ice_gstrings_pf_stats[i].stat_string);
1510
1511                 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
1512                         ethtool_sprintf(&p, "tx_priority_%u_xon.nic", i);
1513                         ethtool_sprintf(&p, "tx_priority_%u_xoff.nic", i);
1514                 }
1515                 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
1516                         ethtool_sprintf(&p, "rx_priority_%u_xon.nic", i);
1517                         ethtool_sprintf(&p, "rx_priority_%u_xoff.nic", i);
1518                 }
1519                 break;
1520         case ETH_SS_TEST:
1521                 memcpy(data, ice_gstrings_test, ICE_TEST_LEN * ETH_GSTRING_LEN);
1522                 break;
1523         case ETH_SS_PRIV_FLAGS:
1524                 for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++)
1525                         ethtool_puts(&p, ice_gstrings_priv_flags[i].name);
1526                 break;
1527         default:
1528                 break;
1529         }
1530 }
1531
1532 static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
1533 {
1534         struct ice_netdev_priv *np = netdev_priv(netdev);
1535
1536         __ice_get_strings(netdev, stringset, data, np->vsi);
1537 }
1538
1539 static int
1540 ice_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1541 {
1542         struct ice_netdev_priv *np = netdev_priv(netdev);
1543         bool led_active;
1544
1545         switch (state) {
1546         case ETHTOOL_ID_ACTIVE:
1547                 led_active = true;
1548                 break;
1549         case ETHTOOL_ID_INACTIVE:
1550                 led_active = false;
1551                 break;
1552         default:
1553                 return -EINVAL;
1554         }
1555
1556         if (ice_aq_set_port_id_led(np->vsi->port_info, !led_active, NULL))
1557                 return -EIO;
1558
1559         return 0;
1560 }
1561
1562 /**
1563  * ice_set_fec_cfg - Set link FEC options
1564  * @netdev: network interface device structure
1565  * @req_fec: FEC mode to configure
1566  */
1567 static int ice_set_fec_cfg(struct net_device *netdev, enum ice_fec_mode req_fec)
1568 {
1569         struct ice_netdev_priv *np = netdev_priv(netdev);
1570         struct ice_aqc_set_phy_cfg_data config = { 0 };
1571         struct ice_vsi *vsi = np->vsi;
1572         struct ice_port_info *pi;
1573
1574         pi = vsi->port_info;
1575         if (!pi)
1576                 return -EOPNOTSUPP;
1577
1578         /* Changing the FEC parameters is not supported if not the PF VSI */
1579         if (vsi->type != ICE_VSI_PF) {
1580                 netdev_info(netdev, "Changing FEC parameters only supported for PF VSI\n");
1581                 return -EOPNOTSUPP;
1582         }
1583
1584         /* Proceed only if requesting different FEC mode */
1585         if (pi->phy.curr_user_fec_req == req_fec)
1586                 return 0;
1587
1588         /* Copy the current user PHY configuration. The current user PHY
1589          * configuration is initialized during probe from PHY capabilities
1590          * software mode, and updated on set PHY configuration.
1591          */
1592         memcpy(&config, &pi->phy.curr_user_phy_cfg, sizeof(config));
1593
1594         ice_cfg_phy_fec(pi, &config, req_fec);
1595         config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1596
1597         if (ice_aq_set_phy_cfg(pi->hw, pi, &config, NULL))
1598                 return -EAGAIN;
1599
1600         /* Save requested FEC config */
1601         pi->phy.curr_user_fec_req = req_fec;
1602
1603         return 0;
1604 }
1605
1606 /**
1607  * ice_set_fecparam - Set FEC link options
1608  * @netdev: network interface device structure
1609  * @fecparam: Ethtool structure to retrieve FEC parameters
1610  */
1611 static int
1612 ice_set_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1613 {
1614         struct ice_netdev_priv *np = netdev_priv(netdev);
1615         struct ice_vsi *vsi = np->vsi;
1616         enum ice_fec_mode fec;
1617
1618         switch (fecparam->fec) {
1619         case ETHTOOL_FEC_AUTO:
1620                 fec = ICE_FEC_AUTO;
1621                 break;
1622         case ETHTOOL_FEC_RS:
1623                 fec = ICE_FEC_RS;
1624                 break;
1625         case ETHTOOL_FEC_BASER:
1626                 fec = ICE_FEC_BASER;
1627                 break;
1628         case ETHTOOL_FEC_OFF:
1629         case ETHTOOL_FEC_NONE:
1630                 fec = ICE_FEC_NONE;
1631                 break;
1632         default:
1633                 dev_warn(ice_pf_to_dev(vsi->back), "Unsupported FEC mode: %d\n",
1634                          fecparam->fec);
1635                 return -EINVAL;
1636         }
1637
1638         return ice_set_fec_cfg(netdev, fec);
1639 }
1640
1641 /**
1642  * ice_get_fecparam - Get link FEC options
1643  * @netdev: network interface device structure
1644  * @fecparam: Ethtool structure to retrieve FEC parameters
1645  */
1646 static int
1647 ice_get_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam)
1648 {
1649         struct ice_netdev_priv *np = netdev_priv(netdev);
1650         struct ice_aqc_get_phy_caps_data *caps;
1651         struct ice_link_status *link_info;
1652         struct ice_vsi *vsi = np->vsi;
1653         struct ice_port_info *pi;
1654         int err;
1655
1656         pi = vsi->port_info;
1657
1658         if (!pi)
1659                 return -EOPNOTSUPP;
1660         link_info = &pi->phy.link_info;
1661
1662         /* Set FEC mode based on negotiated link info */
1663         switch (link_info->fec_info) {
1664         case ICE_AQ_LINK_25G_KR_FEC_EN:
1665                 fecparam->active_fec = ETHTOOL_FEC_BASER;
1666                 break;
1667         case ICE_AQ_LINK_25G_RS_528_FEC_EN:
1668         case ICE_AQ_LINK_25G_RS_544_FEC_EN:
1669                 fecparam->active_fec = ETHTOOL_FEC_RS;
1670                 break;
1671         default:
1672                 fecparam->active_fec = ETHTOOL_FEC_OFF;
1673                 break;
1674         }
1675
1676         caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1677         if (!caps)
1678                 return -ENOMEM;
1679
1680         err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
1681                                   caps, NULL);
1682         if (err)
1683                 goto done;
1684
1685         /* Set supported/configured FEC modes based on PHY capability */
1686         if (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC)
1687                 fecparam->fec |= ETHTOOL_FEC_AUTO;
1688         if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
1689             caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
1690             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN ||
1691             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
1692                 fecparam->fec |= ETHTOOL_FEC_BASER;
1693         if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
1694             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ ||
1695             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
1696                 fecparam->fec |= ETHTOOL_FEC_RS;
1697         if (caps->link_fec_options == 0)
1698                 fecparam->fec |= ETHTOOL_FEC_OFF;
1699
1700 done:
1701         kfree(caps);
1702         return err;
1703 }
1704
1705 /**
1706  * ice_nway_reset - restart autonegotiation
1707  * @netdev: network interface device structure
1708  */
1709 static int ice_nway_reset(struct net_device *netdev)
1710 {
1711         struct ice_netdev_priv *np = netdev_priv(netdev);
1712         struct ice_vsi *vsi = np->vsi;
1713         int err;
1714
1715         /* If VSI state is up, then restart autoneg with link up */
1716         if (!test_bit(ICE_DOWN, vsi->back->state))
1717                 err = ice_set_link(vsi, true);
1718         else
1719                 err = ice_set_link(vsi, false);
1720
1721         return err;
1722 }
1723
1724 /**
1725  * ice_get_priv_flags - report device private flags
1726  * @netdev: network interface device structure
1727  *
1728  * The get string set count and the string set should be matched for each
1729  * flag returned.  Add new strings for each flag to the ice_gstrings_priv_flags
1730  * array.
1731  *
1732  * Returns a u32 bitmap of flags.
1733  */
1734 static u32 ice_get_priv_flags(struct net_device *netdev)
1735 {
1736         struct ice_netdev_priv *np = netdev_priv(netdev);
1737         struct ice_vsi *vsi = np->vsi;
1738         struct ice_pf *pf = vsi->back;
1739         u32 i, ret_flags = 0;
1740
1741         for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1742                 const struct ice_priv_flag *priv_flag;
1743
1744                 priv_flag = &ice_gstrings_priv_flags[i];
1745
1746                 if (test_bit(priv_flag->bitno, pf->flags))
1747                         ret_flags |= BIT(i);
1748         }
1749
1750         return ret_flags;
1751 }
1752
1753 /**
1754  * ice_set_priv_flags - set private flags
1755  * @netdev: network interface device structure
1756  * @flags: bit flags to be set
1757  */
1758 static int ice_set_priv_flags(struct net_device *netdev, u32 flags)
1759 {
1760         struct ice_netdev_priv *np = netdev_priv(netdev);
1761         DECLARE_BITMAP(change_flags, ICE_PF_FLAGS_NBITS);
1762         DECLARE_BITMAP(orig_flags, ICE_PF_FLAGS_NBITS);
1763         struct ice_vsi *vsi = np->vsi;
1764         struct ice_pf *pf = vsi->back;
1765         struct device *dev;
1766         int ret = 0;
1767         u32 i;
1768
1769         if (flags > BIT(ICE_PRIV_FLAG_ARRAY_SIZE))
1770                 return -EINVAL;
1771
1772         dev = ice_pf_to_dev(pf);
1773         set_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1774
1775         bitmap_copy(orig_flags, pf->flags, ICE_PF_FLAGS_NBITS);
1776         for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) {
1777                 const struct ice_priv_flag *priv_flag;
1778
1779                 priv_flag = &ice_gstrings_priv_flags[i];
1780
1781                 if (flags & BIT(i))
1782                         set_bit(priv_flag->bitno, pf->flags);
1783                 else
1784                         clear_bit(priv_flag->bitno, pf->flags);
1785         }
1786
1787         bitmap_xor(change_flags, pf->flags, orig_flags, ICE_PF_FLAGS_NBITS);
1788
1789         /* Do not allow change to link-down-on-close when Total Port Shutdown
1790          * is enabled.
1791          */
1792         if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, change_flags) &&
1793             test_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
1794                 dev_err(dev, "Setting link-down-on-close not supported on this port\n");
1795                 set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
1796                 ret = -EINVAL;
1797                 goto ethtool_exit;
1798         }
1799
1800         if (test_bit(ICE_FLAG_FW_LLDP_AGENT, change_flags)) {
1801                 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags)) {
1802                         int status;
1803
1804                         /* Disable FW LLDP engine */
1805                         status = ice_cfg_lldp_mib_change(&pf->hw, false);
1806
1807                         /* If unregistering for LLDP events fails, this is
1808                          * not an error state, as there shouldn't be any
1809                          * events to respond to.
1810                          */
1811                         if (status)
1812                                 dev_info(dev, "Failed to unreg for LLDP events\n");
1813
1814                         /* The AQ call to stop the FW LLDP agent will generate
1815                          * an error if the agent is already stopped.
1816                          */
1817                         status = ice_aq_stop_lldp(&pf->hw, true, true, NULL);
1818                         if (status)
1819                                 dev_warn(dev, "Fail to stop LLDP agent\n");
1820                         /* Use case for having the FW LLDP agent stopped
1821                          * will likely not need DCB, so failure to init is
1822                          * not a concern of ethtool
1823                          */
1824                         status = ice_init_pf_dcb(pf, true);
1825                         if (status)
1826                                 dev_warn(dev, "Fail to init DCB\n");
1827
1828                         pf->dcbx_cap &= ~DCB_CAP_DCBX_LLD_MANAGED;
1829                         pf->dcbx_cap |= DCB_CAP_DCBX_HOST;
1830                 } else {
1831                         bool dcbx_agent_status;
1832                         int status;
1833
1834                         if (ice_get_pfc_mode(pf) == ICE_QOS_MODE_DSCP) {
1835                                 clear_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags);
1836                                 dev_err(dev, "QoS in L3 DSCP mode, FW Agent not allowed to start\n");
1837                                 ret = -EOPNOTSUPP;
1838                                 goto ethtool_exit;
1839                         }
1840
1841                         /* Remove rule to direct LLDP packets to default VSI.
1842                          * The FW LLDP engine will now be consuming them.
1843                          */
1844                         ice_cfg_sw_lldp(vsi, false, false);
1845
1846                         /* AQ command to start FW LLDP agent will return an
1847                          * error if the agent is already started
1848                          */
1849                         status = ice_aq_start_lldp(&pf->hw, true, NULL);
1850                         if (status)
1851                                 dev_warn(dev, "Fail to start LLDP Agent\n");
1852
1853                         /* AQ command to start FW DCBX agent will fail if
1854                          * the agent is already started
1855                          */
1856                         status = ice_aq_start_stop_dcbx(&pf->hw, true,
1857                                                         &dcbx_agent_status,
1858                                                         NULL);
1859                         if (status)
1860                                 dev_dbg(dev, "Failed to start FW DCBX\n");
1861
1862                         dev_info(dev, "FW DCBX agent is %s\n",
1863                                  dcbx_agent_status ? "ACTIVE" : "DISABLED");
1864
1865                         /* Failure to configure MIB change or init DCB is not
1866                          * relevant to ethtool.  Print notification that
1867                          * registration/init failed but do not return error
1868                          * state to ethtool
1869                          */
1870                         status = ice_init_pf_dcb(pf, true);
1871                         if (status)
1872                                 dev_dbg(dev, "Fail to init DCB\n");
1873
1874                         /* Register for MIB change events */
1875                         status = ice_cfg_lldp_mib_change(&pf->hw, true);
1876                         if (status)
1877                                 dev_dbg(dev, "Fail to enable MIB change events\n");
1878
1879                         pf->dcbx_cap &= ~DCB_CAP_DCBX_HOST;
1880                         pf->dcbx_cap |= DCB_CAP_DCBX_LLD_MANAGED;
1881
1882                         ice_nway_reset(netdev);
1883                 }
1884         }
1885         if (test_bit(ICE_FLAG_LEGACY_RX, change_flags)) {
1886                 /* down and up VSI so that changes of Rx cfg are reflected. */
1887                 ice_down_up(vsi);
1888         }
1889         /* don't allow modification of this flag when a single VF is in
1890          * promiscuous mode because it's not supported
1891          */
1892         if (test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, change_flags) &&
1893             ice_is_any_vf_in_unicast_promisc(pf)) {
1894                 dev_err(dev, "Changing vf-true-promisc-support flag while VF(s) are in promiscuous mode not supported\n");
1895                 /* toggle bit back to previous state */
1896                 change_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags);
1897                 ret = -EAGAIN;
1898         }
1899
1900         if (test_bit(ICE_FLAG_VF_VLAN_PRUNING, change_flags) &&
1901             ice_has_vfs(pf)) {
1902                 dev_err(dev, "vf-vlan-pruning: VLAN pruning cannot be changed while VFs are active.\n");
1903                 /* toggle bit back to previous state */
1904                 change_bit(ICE_FLAG_VF_VLAN_PRUNING, pf->flags);
1905                 ret = -EOPNOTSUPP;
1906         }
1907 ethtool_exit:
1908         clear_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags);
1909         return ret;
1910 }
1911
1912 static int ice_get_sset_count(struct net_device *netdev, int sset)
1913 {
1914         switch (sset) {
1915         case ETH_SS_STATS:
1916                 /* The number (and order) of strings reported *must* remain
1917                  * constant for a given netdevice. This function must not
1918                  * report a different number based on run time parameters
1919                  * (such as the number of queues in use, or the setting of
1920                  * a private ethtool flag). This is due to the nature of the
1921                  * ethtool stats API.
1922                  *
1923                  * Userspace programs such as ethtool must make 3 separate
1924                  * ioctl requests, one for size, one for the strings, and
1925                  * finally one for the stats. Since these cross into
1926                  * userspace, changes to the number or size could result in
1927                  * undefined memory access or incorrect string<->value
1928                  * correlations for statistics.
1929                  *
1930                  * Even if it appears to be safe, changes to the size or
1931                  * order of strings will suffer from race conditions and are
1932                  * not safe.
1933                  */
1934                 return ICE_ALL_STATS_LEN(netdev);
1935         case ETH_SS_TEST:
1936                 return ICE_TEST_LEN;
1937         case ETH_SS_PRIV_FLAGS:
1938                 return ICE_PRIV_FLAG_ARRAY_SIZE;
1939         default:
1940                 return -EOPNOTSUPP;
1941         }
1942 }
1943
1944 static void
1945 __ice_get_ethtool_stats(struct net_device *netdev,
1946                         struct ethtool_stats __always_unused *stats, u64 *data,
1947                         struct ice_vsi *vsi)
1948 {
1949         struct ice_pf *pf = vsi->back;
1950         struct ice_tx_ring *tx_ring;
1951         struct ice_rx_ring *rx_ring;
1952         unsigned int j;
1953         int i = 0;
1954         char *p;
1955
1956         ice_update_pf_stats(pf);
1957         ice_update_vsi_stats(vsi);
1958
1959         for (j = 0; j < ICE_VSI_STATS_LEN; j++) {
1960                 p = (char *)vsi + ice_gstrings_vsi_stats[j].stat_offset;
1961                 data[i++] = (ice_gstrings_vsi_stats[j].sizeof_stat ==
1962                              sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
1963         }
1964
1965         if (ice_is_port_repr_netdev(netdev))
1966                 return;
1967
1968         /* populate per queue stats */
1969         rcu_read_lock();
1970
1971         ice_for_each_alloc_txq(vsi, j) {
1972                 tx_ring = READ_ONCE(vsi->tx_rings[j]);
1973                 if (tx_ring && tx_ring->ring_stats) {
1974                         data[i++] = tx_ring->ring_stats->stats.pkts;
1975                         data[i++] = tx_ring->ring_stats->stats.bytes;
1976                 } else {
1977                         data[i++] = 0;
1978                         data[i++] = 0;
1979                 }
1980         }
1981
1982         ice_for_each_alloc_rxq(vsi, j) {
1983                 rx_ring = READ_ONCE(vsi->rx_rings[j]);
1984                 if (rx_ring && rx_ring->ring_stats) {
1985                         data[i++] = rx_ring->ring_stats->stats.pkts;
1986                         data[i++] = rx_ring->ring_stats->stats.bytes;
1987                 } else {
1988                         data[i++] = 0;
1989                         data[i++] = 0;
1990                 }
1991         }
1992
1993         rcu_read_unlock();
1994
1995         if (vsi->type != ICE_VSI_PF)
1996                 return;
1997
1998         for (j = 0; j < ICE_PF_STATS_LEN; j++) {
1999                 p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset;
2000                 data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat ==
2001                              sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
2002         }
2003
2004         for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
2005                 data[i++] = pf->stats.priority_xon_tx[j];
2006                 data[i++] = pf->stats.priority_xoff_tx[j];
2007         }
2008
2009         for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) {
2010                 data[i++] = pf->stats.priority_xon_rx[j];
2011                 data[i++] = pf->stats.priority_xoff_rx[j];
2012         }
2013 }
2014
2015 static void
2016 ice_get_ethtool_stats(struct net_device *netdev,
2017                       struct ethtool_stats __always_unused *stats, u64 *data)
2018 {
2019         struct ice_netdev_priv *np = netdev_priv(netdev);
2020
2021         __ice_get_ethtool_stats(netdev, stats, data, np->vsi);
2022 }
2023
2024 #define ICE_PHY_TYPE_LOW_MASK_MIN_1G    (ICE_PHY_TYPE_LOW_100BASE_TX | \
2025                                          ICE_PHY_TYPE_LOW_100M_SGMII)
2026
2027 #define ICE_PHY_TYPE_LOW_MASK_MIN_25G   (ICE_PHY_TYPE_LOW_MASK_MIN_1G | \
2028                                          ICE_PHY_TYPE_LOW_1000BASE_T | \
2029                                          ICE_PHY_TYPE_LOW_1000BASE_SX | \
2030                                          ICE_PHY_TYPE_LOW_1000BASE_LX | \
2031                                          ICE_PHY_TYPE_LOW_1000BASE_KX | \
2032                                          ICE_PHY_TYPE_LOW_1G_SGMII | \
2033                                          ICE_PHY_TYPE_LOW_2500BASE_T | \
2034                                          ICE_PHY_TYPE_LOW_2500BASE_X | \
2035                                          ICE_PHY_TYPE_LOW_2500BASE_KX | \
2036                                          ICE_PHY_TYPE_LOW_5GBASE_T | \
2037                                          ICE_PHY_TYPE_LOW_5GBASE_KR | \
2038                                          ICE_PHY_TYPE_LOW_10GBASE_T | \
2039                                          ICE_PHY_TYPE_LOW_10G_SFI_DA | \
2040                                          ICE_PHY_TYPE_LOW_10GBASE_SR | \
2041                                          ICE_PHY_TYPE_LOW_10GBASE_LR | \
2042                                          ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 | \
2043                                          ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC | \
2044                                          ICE_PHY_TYPE_LOW_10G_SFI_C2C)
2045
2046 #define ICE_PHY_TYPE_LOW_MASK_100G      (ICE_PHY_TYPE_LOW_100GBASE_CR4 | \
2047                                          ICE_PHY_TYPE_LOW_100GBASE_SR4 | \
2048                                          ICE_PHY_TYPE_LOW_100GBASE_LR4 | \
2049                                          ICE_PHY_TYPE_LOW_100GBASE_KR4 | \
2050                                          ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC | \
2051                                          ICE_PHY_TYPE_LOW_100G_CAUI4 | \
2052                                          ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC | \
2053                                          ICE_PHY_TYPE_LOW_100G_AUI4 | \
2054                                          ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 | \
2055                                          ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 | \
2056                                          ICE_PHY_TYPE_LOW_100GBASE_CP2 | \
2057                                          ICE_PHY_TYPE_LOW_100GBASE_SR2 | \
2058                                          ICE_PHY_TYPE_LOW_100GBASE_DR)
2059
2060 #define ICE_PHY_TYPE_HIGH_MASK_100G     (ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4 | \
2061                                          ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC |\
2062                                          ICE_PHY_TYPE_HIGH_100G_CAUI2 | \
2063                                          ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC | \
2064                                          ICE_PHY_TYPE_HIGH_100G_AUI2)
2065
2066 #define ICE_PHY_TYPE_HIGH_MASK_200G     (ICE_PHY_TYPE_HIGH_200G_CR4_PAM4 | \
2067                                          ICE_PHY_TYPE_HIGH_200G_SR4 | \
2068                                          ICE_PHY_TYPE_HIGH_200G_FR4 | \
2069                                          ICE_PHY_TYPE_HIGH_200G_LR4 | \
2070                                          ICE_PHY_TYPE_HIGH_200G_DR4 | \
2071                                          ICE_PHY_TYPE_HIGH_200G_KR4_PAM4 | \
2072                                          ICE_PHY_TYPE_HIGH_200G_AUI4_AOC_ACC | \
2073                                          ICE_PHY_TYPE_HIGH_200G_AUI4)
2074
2075 /**
2076  * ice_mask_min_supported_speeds
2077  * @hw: pointer to the HW structure
2078  * @phy_types_high: PHY type high
2079  * @phy_types_low: PHY type low to apply minimum supported speeds mask
2080  *
2081  * Apply minimum supported speeds mask to PHY type low. These are the speeds
2082  * for ethtool supported link mode.
2083  */
2084 static void
2085 ice_mask_min_supported_speeds(struct ice_hw *hw,
2086                               u64 phy_types_high, u64 *phy_types_low)
2087 {
2088         /* if QSFP connection with 100G speed, minimum supported speed is 25G */
2089         if ((*phy_types_low & ICE_PHY_TYPE_LOW_MASK_100G) ||
2090             (phy_types_high & ICE_PHY_TYPE_HIGH_MASK_100G) ||
2091             (phy_types_high & ICE_PHY_TYPE_HIGH_MASK_200G))
2092                 *phy_types_low &= ~ICE_PHY_TYPE_LOW_MASK_MIN_25G;
2093         else if (!ice_is_100m_speed_supported(hw))
2094                 *phy_types_low &= ~ICE_PHY_TYPE_LOW_MASK_MIN_1G;
2095 }
2096
2097 /**
2098  * ice_linkmode_set_bit - set link mode bit
2099  * @phy_to_ethtool: PHY type to ethtool link mode struct to set
2100  * @ks: ethtool link ksettings struct to fill out
2101  * @req_speeds: speed requested by user
2102  * @advert_phy_type: advertised PHY type
2103  * @phy_type: PHY type
2104  */
2105 static void
2106 ice_linkmode_set_bit(const struct ice_phy_type_to_ethtool *phy_to_ethtool,
2107                      struct ethtool_link_ksettings *ks, u32 req_speeds,
2108                      u64 advert_phy_type, u32 phy_type)
2109 {
2110         linkmode_set_bit(phy_to_ethtool->link_mode, ks->link_modes.supported);
2111
2112         if (req_speeds & phy_to_ethtool->aq_link_speed ||
2113             (!req_speeds && advert_phy_type & BIT(phy_type)))
2114                 linkmode_set_bit(phy_to_ethtool->link_mode,
2115                                  ks->link_modes.advertising);
2116 }
2117
2118 /**
2119  * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes
2120  * @netdev: network interface device structure
2121  * @ks: ethtool link ksettings struct to fill out
2122  */
2123 static void
2124 ice_phy_type_to_ethtool(struct net_device *netdev,
2125                         struct ethtool_link_ksettings *ks)
2126 {
2127         struct ice_netdev_priv *np = netdev_priv(netdev);
2128         struct ice_vsi *vsi = np->vsi;
2129         struct ice_pf *pf = vsi->back;
2130         u64 advert_phy_type_lo = 0;
2131         u64 advert_phy_type_hi = 0;
2132         u64 phy_types_high = 0;
2133         u64 phy_types_low = 0;
2134         u32 req_speeds;
2135         u32 i;
2136
2137         req_speeds = vsi->port_info->phy.link_info.req_speeds;
2138
2139         /* Check if lenient mode is supported and enabled, or in strict mode.
2140          *
2141          * In lenient mode the Supported link modes are the PHY types without
2142          * media. The Advertising link mode is either 1. the user requested
2143          * speed, 2. the override PHY mask, or 3. the PHY types with media.
2144          *
2145          * In strict mode Supported link mode are the PHY type with media,
2146          * and Advertising link modes are the media PHY type or the speed
2147          * requested by user.
2148          */
2149         if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags)) {
2150                 phy_types_low = le64_to_cpu(pf->nvm_phy_type_lo);
2151                 phy_types_high = le64_to_cpu(pf->nvm_phy_type_hi);
2152
2153                 ice_mask_min_supported_speeds(&pf->hw, phy_types_high,
2154                                               &phy_types_low);
2155                 /* determine advertised modes based on link override only
2156                  * if it's supported and if the FW doesn't abstract the
2157                  * driver from having to account for link overrides
2158                  */
2159                 if (ice_fw_supports_link_override(&pf->hw) &&
2160                     !ice_fw_supports_report_dflt_cfg(&pf->hw)) {
2161                         struct ice_link_default_override_tlv *ldo;
2162
2163                         ldo = &pf->link_dflt_override;
2164                         /* If override enabled and PHY mask set, then
2165                          * Advertising link mode is the intersection of the PHY
2166                          * types without media and the override PHY mask.
2167                          */
2168                         if (ldo->options & ICE_LINK_OVERRIDE_EN &&
2169                             (ldo->phy_type_low || ldo->phy_type_high)) {
2170                                 advert_phy_type_lo =
2171                                         le64_to_cpu(pf->nvm_phy_type_lo) &
2172                                         ldo->phy_type_low;
2173                                 advert_phy_type_hi =
2174                                         le64_to_cpu(pf->nvm_phy_type_hi) &
2175                                         ldo->phy_type_high;
2176                         }
2177                 }
2178         } else {
2179                 /* strict mode */
2180                 phy_types_low = vsi->port_info->phy.phy_type_low;
2181                 phy_types_high = vsi->port_info->phy.phy_type_high;
2182         }
2183
2184         /* If Advertising link mode PHY type is not using override PHY type,
2185          * then use PHY type with media.
2186          */
2187         if (!advert_phy_type_lo && !advert_phy_type_hi) {
2188                 advert_phy_type_lo = vsi->port_info->phy.phy_type_low;
2189                 advert_phy_type_hi = vsi->port_info->phy.phy_type_high;
2190         }
2191
2192         linkmode_zero(ks->link_modes.supported);
2193         linkmode_zero(ks->link_modes.advertising);
2194
2195         for (i = 0; i < ARRAY_SIZE(phy_type_low_lkup); i++) {
2196                 if (phy_types_low & BIT_ULL(i))
2197                         ice_linkmode_set_bit(&phy_type_low_lkup[i], ks,
2198                                              req_speeds, advert_phy_type_lo,
2199                                              i);
2200         }
2201
2202         for (i = 0; i < ARRAY_SIZE(phy_type_high_lkup); i++) {
2203                 if (phy_types_high & BIT_ULL(i))
2204                         ice_linkmode_set_bit(&phy_type_high_lkup[i], ks,
2205                                              req_speeds, advert_phy_type_hi,
2206                                              i);
2207         }
2208 }
2209
2210 #define TEST_SET_BITS_TIMEOUT   50
2211 #define TEST_SET_BITS_SLEEP_MAX 2000
2212 #define TEST_SET_BITS_SLEEP_MIN 1000
2213
2214 /**
2215  * ice_get_settings_link_up - Get Link settings for when link is up
2216  * @ks: ethtool ksettings to fill in
2217  * @netdev: network interface device structure
2218  */
2219 static void
2220 ice_get_settings_link_up(struct ethtool_link_ksettings *ks,
2221                          struct net_device *netdev)
2222 {
2223         struct ice_netdev_priv *np = netdev_priv(netdev);
2224         struct ice_port_info *pi = np->vsi->port_info;
2225         struct ice_link_status *link_info;
2226         struct ice_vsi *vsi = np->vsi;
2227
2228         link_info = &vsi->port_info->phy.link_info;
2229
2230         /* Get supported and advertised settings from PHY ability with media */
2231         ice_phy_type_to_ethtool(netdev, ks);
2232
2233         switch (link_info->link_speed) {
2234         case ICE_AQ_LINK_SPEED_200GB:
2235                 ks->base.speed = SPEED_200000;
2236                 break;
2237         case ICE_AQ_LINK_SPEED_100GB:
2238                 ks->base.speed = SPEED_100000;
2239                 break;
2240         case ICE_AQ_LINK_SPEED_50GB:
2241                 ks->base.speed = SPEED_50000;
2242                 break;
2243         case ICE_AQ_LINK_SPEED_40GB:
2244                 ks->base.speed = SPEED_40000;
2245                 break;
2246         case ICE_AQ_LINK_SPEED_25GB:
2247                 ks->base.speed = SPEED_25000;
2248                 break;
2249         case ICE_AQ_LINK_SPEED_20GB:
2250                 ks->base.speed = SPEED_20000;
2251                 break;
2252         case ICE_AQ_LINK_SPEED_10GB:
2253                 ks->base.speed = SPEED_10000;
2254                 break;
2255         case ICE_AQ_LINK_SPEED_5GB:
2256                 ks->base.speed = SPEED_5000;
2257                 break;
2258         case ICE_AQ_LINK_SPEED_2500MB:
2259                 ks->base.speed = SPEED_2500;
2260                 break;
2261         case ICE_AQ_LINK_SPEED_1000MB:
2262                 ks->base.speed = SPEED_1000;
2263                 break;
2264         case ICE_AQ_LINK_SPEED_100MB:
2265                 ks->base.speed = SPEED_100;
2266                 break;
2267         default:
2268                 netdev_info(netdev, "WARNING: Unrecognized link_speed (0x%x).\n",
2269                             link_info->link_speed);
2270                 break;
2271         }
2272         ks->base.duplex = DUPLEX_FULL;
2273
2274         if (link_info->an_info & ICE_AQ_AN_COMPLETED)
2275                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
2276                                                      Autoneg);
2277
2278         /* Set flow control negotiated Rx/Tx pause */
2279         switch (pi->fc.current_mode) {
2280         case ICE_FC_FULL:
2281                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
2282                 break;
2283         case ICE_FC_TX_PAUSE:
2284                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause);
2285                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
2286                                                      Asym_Pause);
2287                 break;
2288         case ICE_FC_RX_PAUSE:
2289                 ethtool_link_ksettings_add_link_mode(ks, lp_advertising,
2290                                                      Asym_Pause);
2291                 break;
2292         case ICE_FC_PFC:
2293         default:
2294                 ethtool_link_ksettings_del_link_mode(ks, lp_advertising, Pause);
2295                 ethtool_link_ksettings_del_link_mode(ks, lp_advertising,
2296                                                      Asym_Pause);
2297                 break;
2298         }
2299 }
2300
2301 /**
2302  * ice_get_settings_link_down - Get the Link settings when link is down
2303  * @ks: ethtool ksettings to fill in
2304  * @netdev: network interface device structure
2305  *
2306  * Reports link settings that can be determined when link is down
2307  */
2308 static void
2309 ice_get_settings_link_down(struct ethtool_link_ksettings *ks,
2310                            struct net_device *netdev)
2311 {
2312         /* link is down and the driver needs to fall back on
2313          * supported PHY types to figure out what info to display
2314          */
2315         ice_phy_type_to_ethtool(netdev, ks);
2316
2317         /* With no link, speed and duplex are unknown */
2318         ks->base.speed = SPEED_UNKNOWN;
2319         ks->base.duplex = DUPLEX_UNKNOWN;
2320 }
2321
2322 /**
2323  * ice_get_link_ksettings - Get Link Speed and Duplex settings
2324  * @netdev: network interface device structure
2325  * @ks: ethtool ksettings
2326  *
2327  * Reports speed/duplex settings based on media_type
2328  */
2329 static int
2330 ice_get_link_ksettings(struct net_device *netdev,
2331                        struct ethtool_link_ksettings *ks)
2332 {
2333         struct ice_netdev_priv *np = netdev_priv(netdev);
2334         struct ice_aqc_get_phy_caps_data *caps;
2335         struct ice_link_status *hw_link_info;
2336         struct ice_vsi *vsi = np->vsi;
2337         int err;
2338
2339         ethtool_link_ksettings_zero_link_mode(ks, supported);
2340         ethtool_link_ksettings_zero_link_mode(ks, advertising);
2341         ethtool_link_ksettings_zero_link_mode(ks, lp_advertising);
2342         hw_link_info = &vsi->port_info->phy.link_info;
2343
2344         /* set speed and duplex */
2345         if (hw_link_info->link_info & ICE_AQ_LINK_UP)
2346                 ice_get_settings_link_up(ks, netdev);
2347         else
2348                 ice_get_settings_link_down(ks, netdev);
2349
2350         /* set autoneg settings */
2351         ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ?
2352                 AUTONEG_ENABLE : AUTONEG_DISABLE;
2353
2354         /* set media type settings */
2355         switch (vsi->port_info->phy.media_type) {
2356         case ICE_MEDIA_FIBER:
2357                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
2358                 ks->base.port = PORT_FIBRE;
2359                 break;
2360         case ICE_MEDIA_BASET:
2361                 ethtool_link_ksettings_add_link_mode(ks, supported, TP);
2362                 ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
2363                 ks->base.port = PORT_TP;
2364                 break;
2365         case ICE_MEDIA_BACKPLANE:
2366                 ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
2367                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2368                                                      Backplane);
2369                 ks->base.port = PORT_NONE;
2370                 break;
2371         case ICE_MEDIA_DA:
2372                 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
2373                 ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
2374                 ks->base.port = PORT_DA;
2375                 break;
2376         default:
2377                 ks->base.port = PORT_OTHER;
2378                 break;
2379         }
2380
2381         /* flow control is symmetric and always supported */
2382         ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
2383
2384         caps = kzalloc(sizeof(*caps), GFP_KERNEL);
2385         if (!caps)
2386                 return -ENOMEM;
2387
2388         err = ice_aq_get_phy_caps(vsi->port_info, false,
2389                                   ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
2390         if (err)
2391                 goto done;
2392
2393         /* Set the advertised flow control based on the PHY capability */
2394         if ((caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) &&
2395             (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)) {
2396                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2397                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2398                                                      Asym_Pause);
2399         } else if (caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) {
2400                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2401                                                      Asym_Pause);
2402         } else if (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE) {
2403                 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
2404                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2405                                                      Asym_Pause);
2406         } else {
2407                 ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
2408                 ethtool_link_ksettings_del_link_mode(ks, advertising,
2409                                                      Asym_Pause);
2410         }
2411
2412         /* Set advertised FEC modes based on PHY capability */
2413         ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_NONE);
2414
2415         if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
2416             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
2417                 ethtool_link_ksettings_add_link_mode(ks, advertising,
2418                                                      FEC_BASER);
2419         if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
2420             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
2421                 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
2422
2423         err = ice_aq_get_phy_caps(vsi->port_info, false,
2424                                   ICE_AQC_REPORT_TOPO_CAP_MEDIA, caps, NULL);
2425         if (err)
2426                 goto done;
2427
2428         /* Set supported FEC modes based on PHY capability */
2429         ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE);
2430
2431         if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN ||
2432             caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN)
2433                 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
2434         if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN)
2435                 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
2436
2437         /* Set supported and advertised autoneg */
2438         if (ice_is_phy_caps_an_enabled(caps)) {
2439                 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
2440                 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
2441         }
2442
2443 done:
2444         kfree(caps);
2445         return err;
2446 }
2447
2448 /**
2449  * ice_speed_to_aq_link - Get AQ link speed by Ethtool forced speed
2450  * @speed: ethtool forced speed
2451  */
2452 static u16 ice_speed_to_aq_link(int speed)
2453 {
2454         int aq_speed;
2455
2456         switch (speed) {
2457         case SPEED_10:
2458                 aq_speed = ICE_AQ_LINK_SPEED_10MB;
2459                 break;
2460         case SPEED_100:
2461                 aq_speed = ICE_AQ_LINK_SPEED_100MB;
2462                 break;
2463         case SPEED_1000:
2464                 aq_speed = ICE_AQ_LINK_SPEED_1000MB;
2465                 break;
2466         case SPEED_2500:
2467                 aq_speed = ICE_AQ_LINK_SPEED_2500MB;
2468                 break;
2469         case SPEED_5000:
2470                 aq_speed = ICE_AQ_LINK_SPEED_5GB;
2471                 break;
2472         case SPEED_10000:
2473                 aq_speed = ICE_AQ_LINK_SPEED_10GB;
2474                 break;
2475         case SPEED_20000:
2476                 aq_speed = ICE_AQ_LINK_SPEED_20GB;
2477                 break;
2478         case SPEED_25000:
2479                 aq_speed = ICE_AQ_LINK_SPEED_25GB;
2480                 break;
2481         case SPEED_40000:
2482                 aq_speed = ICE_AQ_LINK_SPEED_40GB;
2483                 break;
2484         case SPEED_50000:
2485                 aq_speed = ICE_AQ_LINK_SPEED_50GB;
2486                 break;
2487         case SPEED_100000:
2488                 aq_speed = ICE_AQ_LINK_SPEED_100GB;
2489                 break;
2490         default:
2491                 aq_speed = ICE_AQ_LINK_SPEED_UNKNOWN;
2492                 break;
2493         }
2494         return aq_speed;
2495 }
2496
2497 /**
2498  * ice_ksettings_find_adv_link_speed - Find advertising link speed
2499  * @ks: ethtool ksettings
2500  */
2501 static u16
2502 ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks)
2503 {
2504         const struct ethtool_forced_speed_map *map;
2505         u16 adv_link_speed = 0;
2506
2507         for (u32 i = 0; i < ARRAY_SIZE(ice_adv_lnk_speed_maps); i++) {
2508                 map = ice_adv_lnk_speed_maps + i;
2509                 if (linkmode_intersects(ks->link_modes.advertising, map->caps))
2510                         adv_link_speed |= ice_speed_to_aq_link(map->speed);
2511         }
2512
2513         return adv_link_speed;
2514 }
2515
2516 /**
2517  * ice_setup_autoneg
2518  * @p: port info
2519  * @ks: ethtool_link_ksettings
2520  * @config: configuration that will be sent down to FW
2521  * @autoneg_enabled: autonegotiation is enabled or not
2522  * @autoneg_changed: will there a change in autonegotiation
2523  * @netdev: network interface device structure
2524  *
2525  * Setup PHY autonegotiation feature
2526  */
2527 static int
2528 ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks,
2529                   struct ice_aqc_set_phy_cfg_data *config,
2530                   u8 autoneg_enabled, u8 *autoneg_changed,
2531                   struct net_device *netdev)
2532 {
2533         int err = 0;
2534
2535         *autoneg_changed = 0;
2536
2537         /* Check autoneg */
2538         if (autoneg_enabled == AUTONEG_ENABLE) {
2539                 /* If autoneg was not already enabled */
2540                 if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) {
2541                         /* If autoneg is not supported, return error */
2542                         if (!ethtool_link_ksettings_test_link_mode(ks,
2543                                                                    supported,
2544                                                                    Autoneg)) {
2545                                 netdev_info(netdev, "Autoneg not supported on this phy.\n");
2546                                 err = -EINVAL;
2547                         } else {
2548                                 /* Autoneg is allowed to change */
2549                                 config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2550                                 *autoneg_changed = 1;
2551                         }
2552                 }
2553         } else {
2554                 /* If autoneg is currently enabled */
2555                 if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) {
2556                         /* If autoneg is supported 10GBASE_T is the only PHY
2557                          * that can disable it, so otherwise return error
2558                          */
2559                         if (ethtool_link_ksettings_test_link_mode(ks,
2560                                                                   supported,
2561                                                                   Autoneg)) {
2562                                 netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
2563                                 err = -EINVAL;
2564                         } else {
2565                                 /* Autoneg is allowed to change */
2566                                 config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2567                                 *autoneg_changed = 1;
2568                         }
2569                 }
2570         }
2571
2572         return err;
2573 }
2574
2575 /**
2576  * ice_set_phy_type_from_speed - set phy_types based on speeds
2577  * and advertised modes
2578  * @ks: ethtool link ksettings struct
2579  * @phy_type_low: pointer to the lower part of phy_type
2580  * @phy_type_high: pointer to the higher part of phy_type
2581  * @adv_link_speed: targeted link speeds bitmap
2582  */
2583 static void
2584 ice_set_phy_type_from_speed(const struct ethtool_link_ksettings *ks,
2585                             u64 *phy_type_low, u64 *phy_type_high,
2586                             u16 adv_link_speed)
2587 {
2588         /* Handle 1000M speed in a special way because ice_update_phy_type
2589          * enables all link modes, but having mixed copper and optical
2590          * standards is not supported.
2591          */
2592         adv_link_speed &= ~ICE_AQ_LINK_SPEED_1000MB;
2593
2594         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2595                                                   1000baseT_Full))
2596                 *phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_T |
2597                                  ICE_PHY_TYPE_LOW_1G_SGMII;
2598
2599         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2600                                                   1000baseKX_Full))
2601                 *phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_KX;
2602
2603         if (ethtool_link_ksettings_test_link_mode(ks, advertising,
2604                                                   1000baseX_Full))
2605                 *phy_type_low |= ICE_PHY_TYPE_LOW_1000BASE_SX |
2606                                  ICE_PHY_TYPE_LOW_1000BASE_LX;
2607
2608         ice_update_phy_type(phy_type_low, phy_type_high, adv_link_speed);
2609 }
2610
2611 /**
2612  * ice_set_link_ksettings - Set Speed and Duplex
2613  * @netdev: network interface device structure
2614  * @ks: ethtool ksettings
2615  *
2616  * Set speed/duplex per media_types advertised/forced
2617  */
2618 static int
2619 ice_set_link_ksettings(struct net_device *netdev,
2620                        const struct ethtool_link_ksettings *ks)
2621 {
2622         struct ice_netdev_priv *np = netdev_priv(netdev);
2623         u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT;
2624         struct ethtool_link_ksettings copy_ks = *ks;
2625         struct ethtool_link_ksettings safe_ks = {};
2626         struct ice_aqc_get_phy_caps_data *phy_caps;
2627         struct ice_aqc_set_phy_cfg_data config;
2628         u16 adv_link_speed, curr_link_speed;
2629         struct ice_pf *pf = np->vsi->back;
2630         struct ice_port_info *pi;
2631         u8 autoneg_changed = 0;
2632         u64 phy_type_high = 0;
2633         u64 phy_type_low = 0;
2634         bool linkup;
2635         int err;
2636
2637         pi = np->vsi->port_info;
2638
2639         if (!pi)
2640                 return -EIO;
2641
2642         if (pi->phy.media_type != ICE_MEDIA_BASET &&
2643             pi->phy.media_type != ICE_MEDIA_FIBER &&
2644             pi->phy.media_type != ICE_MEDIA_BACKPLANE &&
2645             pi->phy.media_type != ICE_MEDIA_DA &&
2646             pi->phy.link_info.link_info & ICE_AQ_LINK_UP)
2647                 return -EOPNOTSUPP;
2648
2649         phy_caps = kzalloc(sizeof(*phy_caps), GFP_KERNEL);
2650         if (!phy_caps)
2651                 return -ENOMEM;
2652
2653         /* Get the PHY capabilities based on media */
2654         if (ice_fw_supports_report_dflt_cfg(pi->hw))
2655                 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2656                                           phy_caps, NULL);
2657         else
2658                 err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2659                                           phy_caps, NULL);
2660         if (err)
2661                 goto done;
2662
2663         /* save autoneg out of ksettings */
2664         autoneg = copy_ks.base.autoneg;
2665
2666         /* Get link modes supported by hardware.*/
2667         ice_phy_type_to_ethtool(netdev, &safe_ks);
2668
2669         /* and check against modes requested by user.
2670          * Return an error if unsupported mode was set.
2671          */
2672         if (!bitmap_subset(copy_ks.link_modes.advertising,
2673                            safe_ks.link_modes.supported,
2674                            __ETHTOOL_LINK_MODE_MASK_NBITS)) {
2675                 if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags))
2676                         netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
2677                 err = -EOPNOTSUPP;
2678                 goto done;
2679         }
2680
2681         /* get our own copy of the bits to check against */
2682         memset(&safe_ks, 0, sizeof(safe_ks));
2683         safe_ks.base.cmd = copy_ks.base.cmd;
2684         safe_ks.base.link_mode_masks_nwords =
2685                 copy_ks.base.link_mode_masks_nwords;
2686         ice_get_link_ksettings(netdev, &safe_ks);
2687
2688         /* set autoneg back to what it currently is */
2689         copy_ks.base.autoneg = safe_ks.base.autoneg;
2690         /* we don't compare the speed */
2691         copy_ks.base.speed = safe_ks.base.speed;
2692
2693         /* If copy_ks.base and safe_ks.base are not the same now, then they are
2694          * trying to set something that we do not support.
2695          */
2696         if (memcmp(&copy_ks.base, &safe_ks.base, sizeof(copy_ks.base))) {
2697                 err = -EOPNOTSUPP;
2698                 goto done;
2699         }
2700
2701         while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
2702                 timeout--;
2703                 if (!timeout) {
2704                         err = -EBUSY;
2705                         goto done;
2706                 }
2707                 usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX);
2708         }
2709
2710         /* Copy the current user PHY configuration. The current user PHY
2711          * configuration is initialized during probe from PHY capabilities
2712          * software mode, and updated on set PHY configuration.
2713          */
2714         config = pi->phy.curr_user_phy_cfg;
2715
2716         config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
2717
2718         /* Check autoneg */
2719         err = ice_setup_autoneg(pi, &safe_ks, &config, autoneg, &autoneg_changed,
2720                                 netdev);
2721
2722         if (err)
2723                 goto done;
2724
2725         /* Call to get the current link speed */
2726         pi->phy.get_link_info = true;
2727         err = ice_get_link_status(pi, &linkup);
2728         if (err)
2729                 goto done;
2730
2731         curr_link_speed = pi->phy.curr_user_speed_req;
2732         adv_link_speed = ice_ksettings_find_adv_link_speed(ks);
2733
2734         /* If speed didn't get set, set it to what it currently is.
2735          * This is needed because if advertise is 0 (as it is when autoneg
2736          * is disabled) then speed won't get set.
2737          */
2738         if (!adv_link_speed)
2739                 adv_link_speed = curr_link_speed;
2740
2741         /* Convert the advertise link speeds to their corresponded PHY_TYPE */
2742         ice_set_phy_type_from_speed(ks, &phy_type_low, &phy_type_high,
2743                                     adv_link_speed);
2744
2745         if (!autoneg_changed && adv_link_speed == curr_link_speed) {
2746                 netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
2747                 goto done;
2748         }
2749
2750         /* save the requested speeds */
2751         pi->phy.link_info.req_speeds = adv_link_speed;
2752
2753         /* set link and auto negotiation so changes take effect */
2754         config.caps |= ICE_AQ_PHY_ENA_LINK;
2755
2756         /* check if there is a PHY type for the requested advertised speed */
2757         if (!(phy_type_low || phy_type_high)) {
2758                 netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
2759                 err = -EOPNOTSUPP;
2760                 goto done;
2761         }
2762
2763         /* intersect requested advertised speed PHY types with media PHY types
2764          * for set PHY configuration
2765          */
2766         config.phy_type_high = cpu_to_le64(phy_type_high) &
2767                         phy_caps->phy_type_high;
2768         config.phy_type_low = cpu_to_le64(phy_type_low) &
2769                         phy_caps->phy_type_low;
2770
2771         if (!(config.phy_type_high || config.phy_type_low)) {
2772                 /* If there is no intersection and lenient mode is enabled, then
2773                  * intersect the requested advertised speed with NVM media type
2774                  * PHY types.
2775                  */
2776                 if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags)) {
2777                         config.phy_type_high = cpu_to_le64(phy_type_high) &
2778                                                pf->nvm_phy_type_hi;
2779                         config.phy_type_low = cpu_to_le64(phy_type_low) &
2780                                               pf->nvm_phy_type_lo;
2781                 } else {
2782                         netdev_info(netdev, "The selected speed is not supported by the current media. Please select a link speed that is supported by the current media.\n");
2783                         err = -EOPNOTSUPP;
2784                         goto done;
2785                 }
2786         }
2787
2788         /* If link is up put link down */
2789         if (pi->phy.link_info.link_info & ICE_AQ_LINK_UP) {
2790                 /* Tell the OS link is going down, the link will go
2791                  * back up when fw says it is ready asynchronously
2792                  */
2793                 ice_print_link_msg(np->vsi, false);
2794                 netif_carrier_off(netdev);
2795                 netif_tx_stop_all_queues(netdev);
2796         }
2797
2798         /* make the aq call */
2799         err = ice_aq_set_phy_cfg(&pf->hw, pi, &config, NULL);
2800         if (err) {
2801                 netdev_info(netdev, "Set phy config failed,\n");
2802                 goto done;
2803         }
2804
2805         /* Save speed request */
2806         pi->phy.curr_user_speed_req = adv_link_speed;
2807 done:
2808         kfree(phy_caps);
2809         clear_bit(ICE_CFG_BUSY, pf->state);
2810
2811         return err;
2812 }
2813
2814 /**
2815  * ice_parse_hdrs - parses headers from RSS hash input
2816  * @nfc: ethtool rxnfc command
2817  *
2818  * This function parses the rxnfc command and returns intended
2819  * header types for RSS configuration
2820  */
2821 static u32 ice_parse_hdrs(struct ethtool_rxnfc *nfc)
2822 {
2823         u32 hdrs = ICE_FLOW_SEG_HDR_NONE;
2824
2825         switch (nfc->flow_type) {
2826         case TCP_V4_FLOW:
2827                 hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4;
2828                 break;
2829         case UDP_V4_FLOW:
2830                 hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4;
2831                 break;
2832         case SCTP_V4_FLOW:
2833                 hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4;
2834                 break;
2835         case GTPU_V4_FLOW:
2836                 hdrs |= ICE_FLOW_SEG_HDR_GTPU_IP | ICE_FLOW_SEG_HDR_IPV4;
2837                 break;
2838         case GTPC_V4_FLOW:
2839                 hdrs |= ICE_FLOW_SEG_HDR_GTPC | ICE_FLOW_SEG_HDR_IPV4;
2840                 break;
2841         case GTPC_TEID_V4_FLOW:
2842                 hdrs |= ICE_FLOW_SEG_HDR_GTPC_TEID | ICE_FLOW_SEG_HDR_IPV4;
2843                 break;
2844         case GTPU_EH_V4_FLOW:
2845                 hdrs |= ICE_FLOW_SEG_HDR_GTPU_EH | ICE_FLOW_SEG_HDR_IPV4;
2846                 break;
2847         case GTPU_UL_V4_FLOW:
2848                 hdrs |= ICE_FLOW_SEG_HDR_GTPU_UP | ICE_FLOW_SEG_HDR_IPV4;
2849                 break;
2850         case GTPU_DL_V4_FLOW:
2851                 hdrs |= ICE_FLOW_SEG_HDR_GTPU_DWN | ICE_FLOW_SEG_HDR_IPV4;
2852                 break;
2853         case TCP_V6_FLOW:
2854                 hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6;
2855                 break;
2856         case UDP_V6_FLOW:
2857                 hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6;
2858                 break;
2859         case SCTP_V6_FLOW:
2860                 hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6;
2861                 break;
2862         case GTPU_V6_FLOW:
2863                 hdrs |= ICE_FLOW_SEG_HDR_GTPU_IP | ICE_FLOW_SEG_HDR_IPV6;
2864                 break;
2865         case GTPC_V6_FLOW:
2866                 hdrs |= ICE_FLOW_SEG_HDR_GTPC | ICE_FLOW_SEG_HDR_IPV6;
2867                 break;
2868         case GTPC_TEID_V6_FLOW:
2869                 hdrs |= ICE_FLOW_SEG_HDR_GTPC_TEID | ICE_FLOW_SEG_HDR_IPV6;
2870                 break;
2871         case GTPU_EH_V6_FLOW:
2872                 hdrs |= ICE_FLOW_SEG_HDR_GTPU_EH | ICE_FLOW_SEG_HDR_IPV6;
2873                 break;
2874         case GTPU_UL_V6_FLOW:
2875                 hdrs |= ICE_FLOW_SEG_HDR_GTPU_UP | ICE_FLOW_SEG_HDR_IPV6;
2876                 break;
2877         case GTPU_DL_V6_FLOW:
2878                 hdrs |= ICE_FLOW_SEG_HDR_GTPU_DWN | ICE_FLOW_SEG_HDR_IPV6;
2879                 break;
2880         default:
2881                 break;
2882         }
2883         return hdrs;
2884 }
2885
2886 /**
2887  * ice_parse_hash_flds - parses hash fields from RSS hash input
2888  * @nfc: ethtool rxnfc command
2889  * @symm: true if Symmetric Topelitz is set
2890  *
2891  * This function parses the rxnfc command and returns intended
2892  * hash fields for RSS configuration
2893  */
2894 static u64 ice_parse_hash_flds(struct ethtool_rxnfc *nfc, bool symm)
2895 {
2896         u64 hfld = ICE_HASH_INVALID;
2897
2898         if (nfc->data & RXH_IP_SRC || nfc->data & RXH_IP_DST) {
2899                 switch (nfc->flow_type) {
2900                 case TCP_V4_FLOW:
2901                 case UDP_V4_FLOW:
2902                 case SCTP_V4_FLOW:
2903                 case GTPU_V4_FLOW:
2904                 case GTPC_V4_FLOW:
2905                 case GTPC_TEID_V4_FLOW:
2906                 case GTPU_EH_V4_FLOW:
2907                 case GTPU_UL_V4_FLOW:
2908                 case GTPU_DL_V4_FLOW:
2909                         if (nfc->data & RXH_IP_SRC)
2910                                 hfld |= ICE_FLOW_HASH_FLD_IPV4_SA;
2911                         if (nfc->data & RXH_IP_DST)
2912                                 hfld |= ICE_FLOW_HASH_FLD_IPV4_DA;
2913                         break;
2914                 case TCP_V6_FLOW:
2915                 case UDP_V6_FLOW:
2916                 case SCTP_V6_FLOW:
2917                 case GTPU_V6_FLOW:
2918                 case GTPC_V6_FLOW:
2919                 case GTPC_TEID_V6_FLOW:
2920                 case GTPU_EH_V6_FLOW:
2921                 case GTPU_UL_V6_FLOW:
2922                 case GTPU_DL_V6_FLOW:
2923                         if (nfc->data & RXH_IP_SRC)
2924                                 hfld |= ICE_FLOW_HASH_FLD_IPV6_SA;
2925                         if (nfc->data & RXH_IP_DST)
2926                                 hfld |= ICE_FLOW_HASH_FLD_IPV6_DA;
2927                         break;
2928                 default:
2929                         break;
2930                 }
2931         }
2932
2933         if (nfc->data & RXH_L4_B_0_1 || nfc->data & RXH_L4_B_2_3) {
2934                 switch (nfc->flow_type) {
2935                 case TCP_V4_FLOW:
2936                 case TCP_V6_FLOW:
2937                         if (nfc->data & RXH_L4_B_0_1)
2938                                 hfld |= ICE_FLOW_HASH_FLD_TCP_SRC_PORT;
2939                         if (nfc->data & RXH_L4_B_2_3)
2940                                 hfld |= ICE_FLOW_HASH_FLD_TCP_DST_PORT;
2941                         break;
2942                 case UDP_V4_FLOW:
2943                 case UDP_V6_FLOW:
2944                         if (nfc->data & RXH_L4_B_0_1)
2945                                 hfld |= ICE_FLOW_HASH_FLD_UDP_SRC_PORT;
2946                         if (nfc->data & RXH_L4_B_2_3)
2947                                 hfld |= ICE_FLOW_HASH_FLD_UDP_DST_PORT;
2948                         break;
2949                 case SCTP_V4_FLOW:
2950                 case SCTP_V6_FLOW:
2951                         if (nfc->data & RXH_L4_B_0_1)
2952                                 hfld |= ICE_FLOW_HASH_FLD_SCTP_SRC_PORT;
2953                         if (nfc->data & RXH_L4_B_2_3)
2954                                 hfld |= ICE_FLOW_HASH_FLD_SCTP_DST_PORT;
2955                         break;
2956                 default:
2957                         break;
2958                 }
2959         }
2960
2961         if (nfc->data & RXH_GTP_TEID) {
2962                 switch (nfc->flow_type) {
2963                 case GTPC_TEID_V4_FLOW:
2964                 case GTPC_TEID_V6_FLOW:
2965                         hfld |= ICE_FLOW_HASH_FLD_GTPC_TEID;
2966                         break;
2967                 case GTPU_V4_FLOW:
2968                 case GTPU_V6_FLOW:
2969                         hfld |= ICE_FLOW_HASH_FLD_GTPU_IP_TEID;
2970                         break;
2971                 case GTPU_EH_V4_FLOW:
2972                 case GTPU_EH_V6_FLOW:
2973                         hfld |= ICE_FLOW_HASH_FLD_GTPU_EH_TEID;
2974                         break;
2975                 case GTPU_UL_V4_FLOW:
2976                 case GTPU_UL_V6_FLOW:
2977                         hfld |= ICE_FLOW_HASH_FLD_GTPU_UP_TEID;
2978                         break;
2979                 case GTPU_DL_V4_FLOW:
2980                 case GTPU_DL_V6_FLOW:
2981                         hfld |= ICE_FLOW_HASH_FLD_GTPU_DWN_TEID;
2982                         break;
2983                 default:
2984                         break;
2985                 }
2986         }
2987
2988         return hfld;
2989 }
2990
2991 /**
2992  * ice_set_rss_hash_opt - Enable/Disable flow types for RSS hash
2993  * @vsi: the VSI being configured
2994  * @nfc: ethtool rxnfc command
2995  *
2996  * Returns Success if the flow input set is supported.
2997  */
2998 static int
2999 ice_set_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
3000 {
3001         struct ice_pf *pf = vsi->back;
3002         struct ice_rss_hash_cfg cfg;
3003         struct device *dev;
3004         u64 hashed_flds;
3005         int status;
3006         bool symm;
3007         u32 hdrs;
3008
3009         dev = ice_pf_to_dev(pf);
3010         if (ice_is_safe_mode(pf)) {
3011                 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
3012                         vsi->vsi_num);
3013                 return -EINVAL;
3014         }
3015
3016         symm = !!(vsi->rss_hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ);
3017         hashed_flds = ice_parse_hash_flds(nfc, symm);
3018         if (hashed_flds == ICE_HASH_INVALID) {
3019                 dev_dbg(dev, "Invalid hash fields, vsi num = %d\n",
3020                         vsi->vsi_num);
3021                 return -EINVAL;
3022         }
3023
3024         hdrs = ice_parse_hdrs(nfc);
3025         if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
3026                 dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
3027                         vsi->vsi_num);
3028                 return -EINVAL;
3029         }
3030
3031         cfg.hash_flds = hashed_flds;
3032         cfg.addl_hdrs = hdrs;
3033         cfg.hdr_type = ICE_RSS_ANY_HEADERS;
3034         cfg.symm = symm;
3035
3036         status = ice_add_rss_cfg(&pf->hw, vsi, &cfg);
3037         if (status) {
3038                 dev_dbg(dev, "ice_add_rss_cfg failed, vsi num = %d, error = %d\n",
3039                         vsi->vsi_num, status);
3040                 return status;
3041         }
3042
3043         return 0;
3044 }
3045
3046 /**
3047  * ice_get_rss_hash_opt - Retrieve hash fields for a given flow-type
3048  * @vsi: the VSI being configured
3049  * @nfc: ethtool rxnfc command
3050  */
3051 static void
3052 ice_get_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc)
3053 {
3054         struct ice_pf *pf = vsi->back;
3055         struct device *dev;
3056         u64 hash_flds;
3057         bool symm;
3058         u32 hdrs;
3059
3060         dev = ice_pf_to_dev(pf);
3061
3062         nfc->data = 0;
3063         if (ice_is_safe_mode(pf)) {
3064                 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
3065                         vsi->vsi_num);
3066                 return;
3067         }
3068
3069         hdrs = ice_parse_hdrs(nfc);
3070         if (hdrs == ICE_FLOW_SEG_HDR_NONE) {
3071                 dev_dbg(dev, "Header type is not valid, vsi num = %d\n",
3072                         vsi->vsi_num);
3073                 return;
3074         }
3075
3076         hash_flds = ice_get_rss_cfg(&pf->hw, vsi->idx, hdrs, &symm);
3077         if (hash_flds == ICE_HASH_INVALID) {
3078                 dev_dbg(dev, "No hash fields found for the given header type, vsi num = %d\n",
3079                         vsi->vsi_num);
3080                 return;
3081         }
3082
3083         if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_SA ||
3084             hash_flds & ICE_FLOW_HASH_FLD_IPV6_SA)
3085                 nfc->data |= (u64)RXH_IP_SRC;
3086
3087         if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_DA ||
3088             hash_flds & ICE_FLOW_HASH_FLD_IPV6_DA)
3089                 nfc->data |= (u64)RXH_IP_DST;
3090
3091         if (hash_flds & ICE_FLOW_HASH_FLD_TCP_SRC_PORT ||
3092             hash_flds & ICE_FLOW_HASH_FLD_UDP_SRC_PORT ||
3093             hash_flds & ICE_FLOW_HASH_FLD_SCTP_SRC_PORT)
3094                 nfc->data |= (u64)RXH_L4_B_0_1;
3095
3096         if (hash_flds & ICE_FLOW_HASH_FLD_TCP_DST_PORT ||
3097             hash_flds & ICE_FLOW_HASH_FLD_UDP_DST_PORT ||
3098             hash_flds & ICE_FLOW_HASH_FLD_SCTP_DST_PORT)
3099                 nfc->data |= (u64)RXH_L4_B_2_3;
3100
3101         if (hash_flds & ICE_FLOW_HASH_FLD_GTPC_TEID ||
3102             hash_flds & ICE_FLOW_HASH_FLD_GTPU_IP_TEID ||
3103             hash_flds & ICE_FLOW_HASH_FLD_GTPU_EH_TEID ||
3104             hash_flds & ICE_FLOW_HASH_FLD_GTPU_UP_TEID ||
3105             hash_flds & ICE_FLOW_HASH_FLD_GTPU_DWN_TEID)
3106                 nfc->data |= (u64)RXH_GTP_TEID;
3107 }
3108
3109 /**
3110  * ice_set_rxnfc - command to set Rx flow rules.
3111  * @netdev: network interface device structure
3112  * @cmd: ethtool rxnfc command
3113  *
3114  * Returns 0 for success and negative values for errors
3115  */
3116 static int ice_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3117 {
3118         struct ice_netdev_priv *np = netdev_priv(netdev);
3119         struct ice_vsi *vsi = np->vsi;
3120
3121         switch (cmd->cmd) {
3122         case ETHTOOL_SRXCLSRLINS:
3123                 return ice_add_fdir_ethtool(vsi, cmd);
3124         case ETHTOOL_SRXCLSRLDEL:
3125                 return ice_del_fdir_ethtool(vsi, cmd);
3126         case ETHTOOL_SRXFH:
3127                 return ice_set_rss_hash_opt(vsi, cmd);
3128         default:
3129                 break;
3130         }
3131         return -EOPNOTSUPP;
3132 }
3133
3134 /**
3135  * ice_get_rxnfc - command to get Rx flow classification rules
3136  * @netdev: network interface device structure
3137  * @cmd: ethtool rxnfc command
3138  * @rule_locs: buffer to rturn Rx flow classification rules
3139  *
3140  * Returns Success if the command is supported.
3141  */
3142 static int
3143 ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3144               u32 __always_unused *rule_locs)
3145 {
3146         struct ice_netdev_priv *np = netdev_priv(netdev);
3147         struct ice_vsi *vsi = np->vsi;
3148         int ret = -EOPNOTSUPP;
3149         struct ice_hw *hw;
3150
3151         hw = &vsi->back->hw;
3152
3153         switch (cmd->cmd) {
3154         case ETHTOOL_GRXRINGS:
3155                 cmd->data = vsi->rss_size;
3156                 ret = 0;
3157                 break;
3158         case ETHTOOL_GRXCLSRLCNT:
3159                 cmd->rule_cnt = hw->fdir_active_fltr;
3160                 /* report total rule count */
3161                 cmd->data = ice_get_fdir_cnt_all(hw);
3162                 ret = 0;
3163                 break;
3164         case ETHTOOL_GRXCLSRULE:
3165                 ret = ice_get_ethtool_fdir_entry(hw, cmd);
3166                 break;
3167         case ETHTOOL_GRXCLSRLALL:
3168                 ret = ice_get_fdir_fltr_ids(hw, cmd, (u32 *)rule_locs);
3169                 break;
3170         case ETHTOOL_GRXFH:
3171                 ice_get_rss_hash_opt(vsi, cmd);
3172                 ret = 0;
3173                 break;
3174         default:
3175                 break;
3176         }
3177
3178         return ret;
3179 }
3180
3181 static void
3182 ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
3183                   struct kernel_ethtool_ringparam *kernel_ring,
3184                   struct netlink_ext_ack *extack)
3185 {
3186         struct ice_netdev_priv *np = netdev_priv(netdev);
3187         struct ice_vsi *vsi = np->vsi;
3188
3189         ring->rx_max_pending = ICE_MAX_NUM_DESC;
3190         ring->tx_max_pending = ICE_MAX_NUM_DESC;
3191         if (vsi->tx_rings && vsi->rx_rings) {
3192                 ring->rx_pending = vsi->rx_rings[0]->count;
3193                 ring->tx_pending = vsi->tx_rings[0]->count;
3194         } else {
3195                 ring->rx_pending = 0;
3196                 ring->tx_pending = 0;
3197         }
3198
3199         /* Rx mini and jumbo rings are not supported */
3200         ring->rx_mini_max_pending = 0;
3201         ring->rx_jumbo_max_pending = 0;
3202         ring->rx_mini_pending = 0;
3203         ring->rx_jumbo_pending = 0;
3204 }
3205
3206 static int
3207 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
3208                   struct kernel_ethtool_ringparam *kernel_ring,
3209                   struct netlink_ext_ack *extack)
3210 {
3211         struct ice_netdev_priv *np = netdev_priv(netdev);
3212         struct ice_tx_ring *xdp_rings = NULL;
3213         struct ice_tx_ring *tx_rings = NULL;
3214         struct ice_rx_ring *rx_rings = NULL;
3215         struct ice_vsi *vsi = np->vsi;
3216         struct ice_pf *pf = vsi->back;
3217         int i, timeout = 50, err = 0;
3218         u16 new_rx_cnt, new_tx_cnt;
3219
3220         if (ring->tx_pending > ICE_MAX_NUM_DESC ||
3221             ring->tx_pending < ICE_MIN_NUM_DESC ||
3222             ring->rx_pending > ICE_MAX_NUM_DESC ||
3223             ring->rx_pending < ICE_MIN_NUM_DESC) {
3224                 netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
3225                            ring->tx_pending, ring->rx_pending,
3226                            ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
3227                            ICE_REQ_DESC_MULTIPLE);
3228                 return -EINVAL;
3229         }
3230
3231         /* Return if there is no rings (device is reloading) */
3232         if (!vsi->tx_rings || !vsi->rx_rings)
3233                 return -EBUSY;
3234
3235         new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
3236         if (new_tx_cnt != ring->tx_pending)
3237                 netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n",
3238                             new_tx_cnt);
3239         new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
3240         if (new_rx_cnt != ring->rx_pending)
3241                 netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n",
3242                             new_rx_cnt);
3243
3244         /* if nothing to do return success */
3245         if (new_tx_cnt == vsi->tx_rings[0]->count &&
3246             new_rx_cnt == vsi->rx_rings[0]->count) {
3247                 netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
3248                 return 0;
3249         }
3250
3251         /* If there is a AF_XDP UMEM attached to any of Rx rings,
3252          * disallow changing the number of descriptors -- regardless
3253          * if the netdev is running or not.
3254          */
3255         if (ice_xsk_any_rx_ring_ena(vsi))
3256                 return -EBUSY;
3257
3258         while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
3259                 timeout--;
3260                 if (!timeout)
3261                         return -EBUSY;
3262                 usleep_range(1000, 2000);
3263         }
3264
3265         /* set for the next time the netdev is started */
3266         if (!netif_running(vsi->netdev)) {
3267                 ice_for_each_alloc_txq(vsi, i)
3268                         vsi->tx_rings[i]->count = new_tx_cnt;
3269                 ice_for_each_alloc_rxq(vsi, i)
3270                         vsi->rx_rings[i]->count = new_rx_cnt;
3271                 if (ice_is_xdp_ena_vsi(vsi))
3272                         ice_for_each_xdp_txq(vsi, i)
3273                                 vsi->xdp_rings[i]->count = new_tx_cnt;
3274                 vsi->num_tx_desc = (u16)new_tx_cnt;
3275                 vsi->num_rx_desc = (u16)new_rx_cnt;
3276                 netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
3277                 goto done;
3278         }
3279
3280         if (new_tx_cnt == vsi->tx_rings[0]->count)
3281                 goto process_rx;
3282
3283         /* alloc updated Tx resources */
3284         netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
3285                     vsi->tx_rings[0]->count, new_tx_cnt);
3286
3287         tx_rings = kcalloc(vsi->num_txq, sizeof(*tx_rings), GFP_KERNEL);
3288         if (!tx_rings) {
3289                 err = -ENOMEM;
3290                 goto done;
3291         }
3292
3293         ice_for_each_txq(vsi, i) {
3294                 /* clone ring and setup updated count */
3295                 tx_rings[i] = *vsi->tx_rings[i];
3296                 tx_rings[i].count = new_tx_cnt;
3297                 tx_rings[i].desc = NULL;
3298                 tx_rings[i].tx_buf = NULL;
3299                 tx_rings[i].tx_tstamps = &pf->ptp.port.tx;
3300                 err = ice_setup_tx_ring(&tx_rings[i]);
3301                 if (err) {
3302                         while (i--)
3303                                 ice_clean_tx_ring(&tx_rings[i]);
3304                         kfree(tx_rings);
3305                         goto done;
3306                 }
3307         }
3308
3309         if (!ice_is_xdp_ena_vsi(vsi))
3310                 goto process_rx;
3311
3312         /* alloc updated XDP resources */
3313         netdev_info(netdev, "Changing XDP descriptor count from %d to %d\n",
3314                     vsi->xdp_rings[0]->count, new_tx_cnt);
3315
3316         xdp_rings = kcalloc(vsi->num_xdp_txq, sizeof(*xdp_rings), GFP_KERNEL);
3317         if (!xdp_rings) {
3318                 err = -ENOMEM;
3319                 goto free_tx;
3320         }
3321
3322         ice_for_each_xdp_txq(vsi, i) {
3323                 /* clone ring and setup updated count */
3324                 xdp_rings[i] = *vsi->xdp_rings[i];
3325                 xdp_rings[i].count = new_tx_cnt;
3326                 xdp_rings[i].desc = NULL;
3327                 xdp_rings[i].tx_buf = NULL;
3328                 err = ice_setup_tx_ring(&xdp_rings[i]);
3329                 if (err) {
3330                         while (i--)
3331                                 ice_clean_tx_ring(&xdp_rings[i]);
3332                         kfree(xdp_rings);
3333                         goto free_tx;
3334                 }
3335                 ice_set_ring_xdp(&xdp_rings[i]);
3336         }
3337
3338 process_rx:
3339         if (new_rx_cnt == vsi->rx_rings[0]->count)
3340                 goto process_link;
3341
3342         /* alloc updated Rx resources */
3343         netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
3344                     vsi->rx_rings[0]->count, new_rx_cnt);
3345
3346         rx_rings = kcalloc(vsi->num_rxq, sizeof(*rx_rings), GFP_KERNEL);
3347         if (!rx_rings) {
3348                 err = -ENOMEM;
3349                 goto done;
3350         }
3351
3352         ice_for_each_rxq(vsi, i) {
3353                 /* clone ring and setup updated count */
3354                 rx_rings[i] = *vsi->rx_rings[i];
3355                 rx_rings[i].count = new_rx_cnt;
3356                 rx_rings[i].cached_phctime = pf->ptp.cached_phc_time;
3357                 rx_rings[i].desc = NULL;
3358                 rx_rings[i].rx_buf = NULL;
3359                 /* this is to allow wr32 to have something to write to
3360                  * during early allocation of Rx buffers
3361                  */
3362                 rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
3363
3364                 err = ice_setup_rx_ring(&rx_rings[i]);
3365                 if (err)
3366                         goto rx_unwind;
3367
3368                 /* allocate Rx buffers */
3369                 err = ice_alloc_rx_bufs(&rx_rings[i],
3370                                         ICE_RX_DESC_UNUSED(&rx_rings[i]));
3371 rx_unwind:
3372                 if (err) {
3373                         while (i) {
3374                                 i--;
3375                                 ice_free_rx_ring(&rx_rings[i]);
3376                         }
3377                         kfree(rx_rings);
3378                         err = -ENOMEM;
3379                         goto free_tx;
3380                 }
3381         }
3382
3383 process_link:
3384         /* Bring interface down, copy in the new ring info, then restore the
3385          * interface. if VSI is up, bring it down and then back up
3386          */
3387         if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
3388                 ice_down(vsi);
3389
3390                 if (tx_rings) {
3391                         ice_for_each_txq(vsi, i) {
3392                                 ice_free_tx_ring(vsi->tx_rings[i]);
3393                                 *vsi->tx_rings[i] = tx_rings[i];
3394                         }
3395                         kfree(tx_rings);
3396                 }
3397
3398                 if (rx_rings) {
3399                         ice_for_each_rxq(vsi, i) {
3400                                 ice_free_rx_ring(vsi->rx_rings[i]);
3401                                 /* copy the real tail offset */
3402                                 rx_rings[i].tail = vsi->rx_rings[i]->tail;
3403                                 /* this is to fake out the allocation routine
3404                                  * into thinking it has to realloc everything
3405                                  * but the recycling logic will let us re-use
3406                                  * the buffers allocated above
3407                                  */
3408                                 rx_rings[i].next_to_use = 0;
3409                                 rx_rings[i].next_to_clean = 0;
3410                                 rx_rings[i].next_to_alloc = 0;
3411                                 *vsi->rx_rings[i] = rx_rings[i];
3412                         }
3413                         kfree(rx_rings);
3414                 }
3415
3416                 if (xdp_rings) {
3417                         ice_for_each_xdp_txq(vsi, i) {
3418                                 ice_free_tx_ring(vsi->xdp_rings[i]);
3419                                 *vsi->xdp_rings[i] = xdp_rings[i];
3420                         }
3421                         kfree(xdp_rings);
3422                 }
3423
3424                 vsi->num_tx_desc = new_tx_cnt;
3425                 vsi->num_rx_desc = new_rx_cnt;
3426                 ice_up(vsi);
3427         }
3428         goto done;
3429
3430 free_tx:
3431         /* error cleanup if the Rx allocations failed after getting Tx */
3432         if (tx_rings) {
3433                 ice_for_each_txq(vsi, i)
3434                         ice_free_tx_ring(&tx_rings[i]);
3435                 kfree(tx_rings);
3436         }
3437
3438 done:
3439         clear_bit(ICE_CFG_BUSY, pf->state);
3440         return err;
3441 }
3442
3443 /**
3444  * ice_get_pauseparam - Get Flow Control status
3445  * @netdev: network interface device structure
3446  * @pause: ethernet pause (flow control) parameters
3447  *
3448  * Get requested flow control status from PHY capability.
3449  * If autoneg is true, then ethtool will send the ETHTOOL_GSET ioctl which
3450  * is handled by ice_get_link_ksettings. ice_get_link_ksettings will report
3451  * the negotiated Rx/Tx pause via lp_advertising.
3452  */
3453 static void
3454 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
3455 {
3456         struct ice_netdev_priv *np = netdev_priv(netdev);
3457         struct ice_port_info *pi = np->vsi->port_info;
3458         struct ice_aqc_get_phy_caps_data *pcaps;
3459         struct ice_dcbx_cfg *dcbx_cfg;
3460         int status;
3461
3462         /* Initialize pause params */
3463         pause->rx_pause = 0;
3464         pause->tx_pause = 0;
3465
3466         dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
3467
3468         pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
3469         if (!pcaps)
3470                 return;
3471
3472         /* Get current PHY config */
3473         status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
3474                                      NULL);
3475         if (status)
3476                 goto out;
3477
3478         pause->autoneg = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
3479                                                              AUTONEG_DISABLE;
3480
3481         if (dcbx_cfg->pfc.pfcena)
3482                 /* PFC enabled so report LFC as off */
3483                 goto out;
3484
3485         if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
3486                 pause->tx_pause = 1;
3487         if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
3488                 pause->rx_pause = 1;
3489
3490 out:
3491         kfree(pcaps);
3492 }
3493
3494 /**
3495  * ice_set_pauseparam - Set Flow Control parameter
3496  * @netdev: network interface device structure
3497  * @pause: return Tx/Rx flow control status
3498  */
3499 static int
3500 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
3501 {
3502         struct ice_netdev_priv *np = netdev_priv(netdev);
3503         struct ice_aqc_get_phy_caps_data *pcaps;
3504         struct ice_link_status *hw_link_info;
3505         struct ice_pf *pf = np->vsi->back;
3506         struct ice_dcbx_cfg *dcbx_cfg;
3507         struct ice_vsi *vsi = np->vsi;
3508         struct ice_hw *hw = &pf->hw;
3509         struct ice_port_info *pi;
3510         u8 aq_failures;
3511         bool link_up;
3512         u32 is_an;
3513         int err;
3514
3515         pi = vsi->port_info;
3516         hw_link_info = &pi->phy.link_info;
3517         dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
3518         link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
3519
3520         /* Changing the port's flow control is not supported if this isn't the
3521          * PF VSI
3522          */
3523         if (vsi->type != ICE_VSI_PF) {
3524                 netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
3525                 return -EOPNOTSUPP;
3526         }
3527
3528         /* Get pause param reports configured and negotiated flow control pause
3529          * when ETHTOOL_GLINKSETTINGS is defined. Since ETHTOOL_GLINKSETTINGS is
3530          * defined get pause param pause->autoneg reports SW configured setting,
3531          * so compare pause->autoneg with SW configured to prevent the user from
3532          * using set pause param to chance autoneg.
3533          */
3534         pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
3535         if (!pcaps)
3536                 return -ENOMEM;
3537
3538         /* Get current PHY config */
3539         err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
3540                                   NULL);
3541         if (err) {
3542                 kfree(pcaps);
3543                 return err;
3544         }
3545
3546         is_an = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
3547                                                     AUTONEG_DISABLE;
3548
3549         kfree(pcaps);
3550
3551         if (pause->autoneg != is_an) {
3552                 netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
3553                 return -EOPNOTSUPP;
3554         }
3555
3556         /* If we have link and don't have autoneg */
3557         if (!test_bit(ICE_DOWN, pf->state) &&
3558             !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
3559                 /* Send message that it might not necessarily work*/
3560                 netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
3561         }
3562
3563         if (dcbx_cfg->pfc.pfcena) {
3564                 netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
3565                 return -EOPNOTSUPP;
3566         }
3567         if (pause->rx_pause && pause->tx_pause)
3568                 pi->fc.req_mode = ICE_FC_FULL;
3569         else if (pause->rx_pause && !pause->tx_pause)
3570                 pi->fc.req_mode = ICE_FC_RX_PAUSE;
3571         else if (!pause->rx_pause && pause->tx_pause)
3572                 pi->fc.req_mode = ICE_FC_TX_PAUSE;
3573         else if (!pause->rx_pause && !pause->tx_pause)
3574                 pi->fc.req_mode = ICE_FC_NONE;
3575         else
3576                 return -EINVAL;
3577
3578         /* Set the FC mode and only restart AN if link is up */
3579         err = ice_set_fc(pi, &aq_failures, link_up);
3580
3581         if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
3582                 netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %s\n",
3583                             err, ice_aq_str(hw->adminq.sq_last_status));
3584                 err = -EAGAIN;
3585         } else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
3586                 netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %s\n",
3587                             err, ice_aq_str(hw->adminq.sq_last_status));
3588                 err = -EAGAIN;
3589         } else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
3590                 netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %s\n",
3591                             err, ice_aq_str(hw->adminq.sq_last_status));
3592                 err = -EAGAIN;
3593         }
3594
3595         return err;
3596 }
3597
3598 /**
3599  * ice_get_rxfh_key_size - get the RSS hash key size
3600  * @netdev: network interface device structure
3601  *
3602  * Returns the table size.
3603  */
3604 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
3605 {
3606         return ICE_VSIQF_HKEY_ARRAY_SIZE;
3607 }
3608
3609 /**
3610  * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
3611  * @netdev: network interface device structure
3612  *
3613  * Returns the table size.
3614  */
3615 static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
3616 {
3617         struct ice_netdev_priv *np = netdev_priv(netdev);
3618
3619         return np->vsi->rss_table_size;
3620 }
3621
3622 /**
3623  * ice_get_rxfh - get the Rx flow hash indirection table
3624  * @netdev: network interface device structure
3625  * @rxfh: pointer to param struct (indir, key, hfunc)
3626  *
3627  * Reads the indirection table directly from the hardware.
3628  */
3629 static int
3630 ice_get_rxfh(struct net_device *netdev, struct ethtool_rxfh_param *rxfh)
3631 {
3632         struct ice_netdev_priv *np = netdev_priv(netdev);
3633         u32 rss_context = rxfh->rss_context;
3634         struct ice_vsi *vsi = np->vsi;
3635         struct ice_pf *pf = vsi->back;
3636         u16 qcount, offset;
3637         int err, num_tc, i;
3638         u8 *lut;
3639
3640         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3641                 netdev_warn(netdev, "RSS is not supported on this VSI!\n");
3642                 return -EOPNOTSUPP;
3643         }
3644
3645         if (rss_context && !ice_is_adq_active(pf)) {
3646                 netdev_err(netdev, "RSS context cannot be non-zero when ADQ is not configured.\n");
3647                 return -EINVAL;
3648         }
3649
3650         qcount = vsi->mqprio_qopt.qopt.count[rss_context];
3651         offset = vsi->mqprio_qopt.qopt.offset[rss_context];
3652
3653         if (rss_context && ice_is_adq_active(pf)) {
3654                 num_tc = vsi->mqprio_qopt.qopt.num_tc;
3655                 if (rss_context >= num_tc) {
3656                         netdev_err(netdev, "RSS context:%d  > num_tc:%d\n",
3657                                    rss_context, num_tc);
3658                         return -EINVAL;
3659                 }
3660                 /* Use channel VSI of given TC */
3661                 vsi = vsi->tc_map_vsi[rss_context];
3662         }
3663
3664         rxfh->hfunc = ETH_RSS_HASH_TOP;
3665         if (vsi->rss_hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ)
3666                 rxfh->input_xfrm |= RXH_XFRM_SYM_XOR;
3667
3668         if (!rxfh->indir)
3669                 return 0;
3670
3671         lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3672         if (!lut)
3673                 return -ENOMEM;
3674
3675         err = ice_get_rss_key(vsi, rxfh->key);
3676         if (err)
3677                 goto out;
3678
3679         err = ice_get_rss_lut(vsi, lut, vsi->rss_table_size);
3680         if (err)
3681                 goto out;
3682
3683         if (ice_is_adq_active(pf)) {
3684                 for (i = 0; i < vsi->rss_table_size; i++)
3685                         rxfh->indir[i] = offset + lut[i] % qcount;
3686                 goto out;
3687         }
3688
3689         for (i = 0; i < vsi->rss_table_size; i++)
3690                 rxfh->indir[i] = lut[i];
3691
3692 out:
3693         kfree(lut);
3694         return err;
3695 }
3696
3697 /**
3698  * ice_set_rxfh - set the Rx flow hash indirection table
3699  * @netdev: network interface device structure
3700  * @rxfh: pointer to param struct (indir, key, hfunc)
3701  * @extack: extended ACK from the Netlink message
3702  *
3703  * Returns -EINVAL if the table specifies an invalid queue ID, otherwise
3704  * returns 0 after programming the table.
3705  */
3706 static int
3707 ice_set_rxfh(struct net_device *netdev, struct ethtool_rxfh_param *rxfh,
3708              struct netlink_ext_ack *extack)
3709 {
3710         struct ice_netdev_priv *np = netdev_priv(netdev);
3711         u8 hfunc = ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ;
3712         struct ice_vsi *vsi = np->vsi;
3713         struct ice_pf *pf = vsi->back;
3714         struct device *dev;
3715         int err;
3716
3717         dev = ice_pf_to_dev(pf);
3718         if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
3719             rxfh->hfunc != ETH_RSS_HASH_TOP)
3720                 return -EOPNOTSUPP;
3721
3722         if (rxfh->rss_context)
3723                 return -EOPNOTSUPP;
3724
3725         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3726                 /* RSS not supported return error here */
3727                 netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3728                 return -EIO;
3729         }
3730
3731         if (ice_is_adq_active(pf)) {
3732                 netdev_err(netdev, "Cannot change RSS params with ADQ configured.\n");
3733                 return -EOPNOTSUPP;
3734         }
3735
3736         /* Update the VSI's hash function */
3737         if (rxfh->input_xfrm & RXH_XFRM_SYM_XOR)
3738                 hfunc = ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ;
3739
3740         err = ice_set_rss_hfunc(vsi, hfunc);
3741         if (err)
3742                 return err;
3743
3744         if (rxfh->key) {
3745                 if (!vsi->rss_hkey_user) {
3746                         vsi->rss_hkey_user =
3747                                 devm_kzalloc(dev, ICE_VSIQF_HKEY_ARRAY_SIZE,
3748                                              GFP_KERNEL);
3749                         if (!vsi->rss_hkey_user)
3750                                 return -ENOMEM;
3751                 }
3752                 memcpy(vsi->rss_hkey_user, rxfh->key,
3753                        ICE_VSIQF_HKEY_ARRAY_SIZE);
3754
3755                 err = ice_set_rss_key(vsi, vsi->rss_hkey_user);
3756                 if (err)
3757                         return err;
3758         }
3759
3760         if (!vsi->rss_lut_user) {
3761                 vsi->rss_lut_user = devm_kzalloc(dev, vsi->rss_table_size,
3762                                                  GFP_KERNEL);
3763                 if (!vsi->rss_lut_user)
3764                         return -ENOMEM;
3765         }
3766
3767         /* Each 32 bits pointed by 'indir' is stored with a lut entry */
3768         if (rxfh->indir) {
3769                 int i;
3770
3771                 for (i = 0; i < vsi->rss_table_size; i++)
3772                         vsi->rss_lut_user[i] = (u8)(rxfh->indir[i]);
3773         } else {
3774                 ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
3775                                  vsi->rss_size);
3776         }
3777
3778         err = ice_set_rss_lut(vsi, vsi->rss_lut_user, vsi->rss_table_size);
3779         if (err)
3780                 return err;
3781
3782         return 0;
3783 }
3784
3785 static int
3786 ice_get_ts_info(struct net_device *dev, struct kernel_ethtool_ts_info *info)
3787 {
3788         struct ice_pf *pf = ice_netdev_to_pf(dev);
3789
3790         /* only report timestamping if PTP is enabled */
3791         if (pf->ptp.state != ICE_PTP_READY)
3792                 return ethtool_op_get_ts_info(dev, info);
3793
3794         info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
3795                                 SOF_TIMESTAMPING_TX_HARDWARE |
3796                                 SOF_TIMESTAMPING_RX_HARDWARE |
3797                                 SOF_TIMESTAMPING_RAW_HARDWARE;
3798
3799         info->phc_index = ice_ptp_clock_index(pf);
3800
3801         info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
3802
3803         info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
3804
3805         return 0;
3806 }
3807
3808 /**
3809  * ice_get_max_txq - return the maximum number of Tx queues for in a PF
3810  * @pf: PF structure
3811  */
3812 static int ice_get_max_txq(struct ice_pf *pf)
3813 {
3814         return min3(pf->num_lan_msix, (u16)num_online_cpus(),
3815                     (u16)pf->hw.func_caps.common_cap.num_txq);
3816 }
3817
3818 /**
3819  * ice_get_max_rxq - return the maximum number of Rx queues for in a PF
3820  * @pf: PF structure
3821  */
3822 static int ice_get_max_rxq(struct ice_pf *pf)
3823 {
3824         return min3(pf->num_lan_msix, (u16)num_online_cpus(),
3825                     (u16)pf->hw.func_caps.common_cap.num_rxq);
3826 }
3827
3828 /**
3829  * ice_get_combined_cnt - return the current number of combined channels
3830  * @vsi: PF VSI pointer
3831  *
3832  * Go through all queue vectors and count ones that have both Rx and Tx ring
3833  * attached
3834  */
3835 static u32 ice_get_combined_cnt(struct ice_vsi *vsi)
3836 {
3837         u32 combined = 0;
3838         int q_idx;
3839
3840         ice_for_each_q_vector(vsi, q_idx) {
3841                 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3842
3843                 if (q_vector->rx.rx_ring && q_vector->tx.tx_ring)
3844                         combined++;
3845         }
3846
3847         return combined;
3848 }
3849
3850 /**
3851  * ice_get_channels - get the current and max supported channels
3852  * @dev: network interface device structure
3853  * @ch: ethtool channel data structure
3854  */
3855 static void
3856 ice_get_channels(struct net_device *dev, struct ethtool_channels *ch)
3857 {
3858         struct ice_netdev_priv *np = netdev_priv(dev);
3859         struct ice_vsi *vsi = np->vsi;
3860         struct ice_pf *pf = vsi->back;
3861
3862         /* report maximum channels */
3863         ch->max_rx = ice_get_max_rxq(pf);
3864         ch->max_tx = ice_get_max_txq(pf);
3865         ch->max_combined = min_t(int, ch->max_rx, ch->max_tx);
3866
3867         /* report current channels */
3868         ch->combined_count = ice_get_combined_cnt(vsi);
3869         ch->rx_count = vsi->num_rxq - ch->combined_count;
3870         ch->tx_count = vsi->num_txq - ch->combined_count;
3871
3872         /* report other queues */
3873         ch->other_count = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
3874         ch->max_other = ch->other_count;
3875 }
3876
3877 /**
3878  * ice_get_valid_rss_size - return valid number of RSS queues
3879  * @hw: pointer to the HW structure
3880  * @new_size: requested RSS queues
3881  */
3882 static int ice_get_valid_rss_size(struct ice_hw *hw, int new_size)
3883 {
3884         struct ice_hw_common_caps *caps = &hw->func_caps.common_cap;
3885
3886         return min_t(int, new_size, BIT(caps->rss_table_entry_width));
3887 }
3888
3889 /**
3890  * ice_vsi_set_dflt_rss_lut - set default RSS LUT with requested RSS size
3891  * @vsi: VSI to reconfigure RSS LUT on
3892  * @req_rss_size: requested range of queue numbers for hashing
3893  *
3894  * Set the VSI's RSS parameters, configure the RSS LUT based on these.
3895  */
3896 static int ice_vsi_set_dflt_rss_lut(struct ice_vsi *vsi, int req_rss_size)
3897 {
3898         struct ice_pf *pf = vsi->back;
3899         struct device *dev;
3900         struct ice_hw *hw;
3901         int err;
3902         u8 *lut;
3903
3904         dev = ice_pf_to_dev(pf);
3905         hw = &pf->hw;
3906
3907         if (!req_rss_size)
3908                 return -EINVAL;
3909
3910         lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3911         if (!lut)
3912                 return -ENOMEM;
3913
3914         /* set RSS LUT parameters */
3915         if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3916                 vsi->rss_size = 1;
3917         else
3918                 vsi->rss_size = ice_get_valid_rss_size(hw, req_rss_size);
3919
3920         /* create/set RSS LUT */
3921         ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
3922         err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
3923         if (err)
3924                 dev_err(dev, "Cannot set RSS lut, err %d aq_err %s\n", err,
3925                         ice_aq_str(hw->adminq.sq_last_status));
3926
3927         kfree(lut);
3928         return err;
3929 }
3930
3931 /**
3932  * ice_set_channels - set the number channels
3933  * @dev: network interface device structure
3934  * @ch: ethtool channel data structure
3935  */
3936 static int ice_set_channels(struct net_device *dev, struct ethtool_channels *ch)
3937 {
3938         struct ice_netdev_priv *np = netdev_priv(dev);
3939         struct ice_vsi *vsi = np->vsi;
3940         struct ice_pf *pf = vsi->back;
3941         int new_rx = 0, new_tx = 0;
3942         bool locked = false;
3943         int ret = 0;
3944
3945         /* do not support changing channels in Safe Mode */
3946         if (ice_is_safe_mode(pf)) {
3947                 netdev_err(dev, "Changing channel in Safe Mode is not supported\n");
3948                 return -EOPNOTSUPP;
3949         }
3950         /* do not support changing other_count */
3951         if (ch->other_count != (test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1U : 0U))
3952                 return -EINVAL;
3953
3954         if (ice_is_adq_active(pf)) {
3955                 netdev_err(dev, "Cannot set channels with ADQ configured.\n");
3956                 return -EOPNOTSUPP;
3957         }
3958
3959         if (test_bit(ICE_FLAG_FD_ENA, pf->flags) && pf->hw.fdir_active_fltr) {
3960                 netdev_err(dev, "Cannot set channels when Flow Director filters are active\n");
3961                 return -EOPNOTSUPP;
3962         }
3963
3964         if (ch->rx_count && ch->tx_count) {
3965                 netdev_err(dev, "Dedicated RX or TX channels cannot be used simultaneously\n");
3966                 return -EINVAL;
3967         }
3968
3969         new_rx = ch->combined_count + ch->rx_count;
3970         new_tx = ch->combined_count + ch->tx_count;
3971
3972         if (new_rx < vsi->tc_cfg.numtc) {
3973                 netdev_err(dev, "Cannot set less Rx channels, than Traffic Classes you have (%u)\n",
3974                            vsi->tc_cfg.numtc);
3975                 return -EINVAL;
3976         }
3977         if (new_tx < vsi->tc_cfg.numtc) {
3978                 netdev_err(dev, "Cannot set less Tx channels, than Traffic Classes you have (%u)\n",
3979                            vsi->tc_cfg.numtc);
3980                 return -EINVAL;
3981         }
3982         if (new_rx > ice_get_max_rxq(pf)) {
3983                 netdev_err(dev, "Maximum allowed Rx channels is %d\n",
3984                            ice_get_max_rxq(pf));
3985                 return -EINVAL;
3986         }
3987         if (new_tx > ice_get_max_txq(pf)) {
3988                 netdev_err(dev, "Maximum allowed Tx channels is %d\n",
3989                            ice_get_max_txq(pf));
3990                 return -EINVAL;
3991         }
3992
3993         if (pf->adev) {
3994                 mutex_lock(&pf->adev_mutex);
3995                 device_lock(&pf->adev->dev);
3996                 locked = true;
3997                 if (pf->adev->dev.driver) {
3998                         netdev_err(dev, "Cannot change channels when RDMA is active\n");
3999                         ret = -EBUSY;
4000                         goto adev_unlock;
4001                 }
4002         }
4003
4004         ice_vsi_recfg_qs(vsi, new_rx, new_tx, locked);
4005
4006         if (!netif_is_rxfh_configured(dev)) {
4007                 ret = ice_vsi_set_dflt_rss_lut(vsi, new_rx);
4008                 goto adev_unlock;
4009         }
4010
4011         /* Update rss_size due to change in Rx queues */
4012         vsi->rss_size = ice_get_valid_rss_size(&pf->hw, new_rx);
4013
4014 adev_unlock:
4015         if (locked) {
4016                 device_unlock(&pf->adev->dev);
4017                 mutex_unlock(&pf->adev_mutex);
4018         }
4019         return ret;
4020 }
4021
4022 /**
4023  * ice_get_wol - get current Wake on LAN configuration
4024  * @netdev: network interface device structure
4025  * @wol: Ethtool structure to retrieve WoL settings
4026  */
4027 static void ice_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
4028 {
4029         struct ice_netdev_priv *np = netdev_priv(netdev);
4030         struct ice_pf *pf = np->vsi->back;
4031
4032         if (np->vsi->type != ICE_VSI_PF)
4033                 netdev_warn(netdev, "Wake on LAN is not supported on this interface!\n");
4034
4035         /* Get WoL settings based on the HW capability */
4036         if (ice_is_wol_supported(&pf->hw)) {
4037                 wol->supported = WAKE_MAGIC;
4038                 wol->wolopts = pf->wol_ena ? WAKE_MAGIC : 0;
4039         } else {
4040                 wol->supported = 0;
4041                 wol->wolopts = 0;
4042         }
4043 }
4044
4045 /**
4046  * ice_set_wol - set Wake on LAN on supported device
4047  * @netdev: network interface device structure
4048  * @wol: Ethtool structure to set WoL
4049  */
4050 static int ice_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
4051 {
4052         struct ice_netdev_priv *np = netdev_priv(netdev);
4053         struct ice_vsi *vsi = np->vsi;
4054         struct ice_pf *pf = vsi->back;
4055
4056         if (vsi->type != ICE_VSI_PF || !ice_is_wol_supported(&pf->hw))
4057                 return -EOPNOTSUPP;
4058
4059         /* only magic packet is supported */
4060         if (wol->wolopts && wol->wolopts != WAKE_MAGIC)
4061                 return -EOPNOTSUPP;
4062
4063         /* Set WoL only if there is a new value */
4064         if (pf->wol_ena != !!wol->wolopts) {
4065                 pf->wol_ena = !!wol->wolopts;
4066                 device_set_wakeup_enable(ice_pf_to_dev(pf), pf->wol_ena);
4067                 netdev_dbg(netdev, "WoL magic packet %sabled\n",
4068                            pf->wol_ena ? "en" : "dis");
4069         }
4070
4071         return 0;
4072 }
4073
4074 /**
4075  * ice_get_rc_coalesce - get ITR values for specific ring container
4076  * @ec: ethtool structure to fill with driver's coalesce settings
4077  * @rc: ring container that the ITR values will come from
4078  *
4079  * Query the device for ice_ring_container specific ITR values. This is
4080  * done per ice_ring_container because each q_vector can have 1 or more rings
4081  * and all of said ring(s) will have the same ITR values.
4082  *
4083  * Returns 0 on success, negative otherwise.
4084  */
4085 static int
4086 ice_get_rc_coalesce(struct ethtool_coalesce *ec, struct ice_ring_container *rc)
4087 {
4088         if (!rc->rx_ring)
4089                 return -EINVAL;
4090
4091         switch (rc->type) {
4092         case ICE_RX_CONTAINER:
4093                 ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc);
4094                 ec->rx_coalesce_usecs = rc->itr_setting;
4095                 ec->rx_coalesce_usecs_high = rc->rx_ring->q_vector->intrl;
4096                 break;
4097         case ICE_TX_CONTAINER:
4098                 ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc);
4099                 ec->tx_coalesce_usecs = rc->itr_setting;
4100                 break;
4101         default:
4102                 dev_dbg(ice_pf_to_dev(rc->rx_ring->vsi->back), "Invalid c_type %d\n", rc->type);
4103                 return -EINVAL;
4104         }
4105
4106         return 0;
4107 }
4108
4109 /**
4110  * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings
4111  * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings
4112  * @ec: coalesce settings to program the device with
4113  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
4114  *
4115  * Return 0 on success, and negative under the following conditions:
4116  * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed.
4117  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
4118  */
4119 static int
4120 ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
4121 {
4122         if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
4123                 if (ice_get_rc_coalesce(ec,
4124                                         &vsi->rx_rings[q_num]->q_vector->rx))
4125                         return -EINVAL;
4126                 if (ice_get_rc_coalesce(ec,
4127                                         &vsi->tx_rings[q_num]->q_vector->tx))
4128                         return -EINVAL;
4129         } else if (q_num < vsi->num_rxq) {
4130                 if (ice_get_rc_coalesce(ec,
4131                                         &vsi->rx_rings[q_num]->q_vector->rx))
4132                         return -EINVAL;
4133         } else if (q_num < vsi->num_txq) {
4134                 if (ice_get_rc_coalesce(ec,
4135                                         &vsi->tx_rings[q_num]->q_vector->tx))
4136                         return -EINVAL;
4137         } else {
4138                 return -EINVAL;
4139         }
4140
4141         return 0;
4142 }
4143
4144 /**
4145  * __ice_get_coalesce - get ITR/INTRL values for the device
4146  * @netdev: pointer to the netdev associated with this query
4147  * @ec: ethtool structure to fill with driver's coalesce settings
4148  * @q_num: queue number to get the coalesce settings for
4149  *
4150  * If the caller passes in a negative q_num then we return coalesce settings
4151  * based on queue number 0, else use the actual q_num passed in.
4152  */
4153 static int
4154 __ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
4155                    int q_num)
4156 {
4157         struct ice_netdev_priv *np = netdev_priv(netdev);
4158         struct ice_vsi *vsi = np->vsi;
4159
4160         if (q_num < 0)
4161                 q_num = 0;
4162
4163         if (ice_get_q_coalesce(vsi, ec, q_num))
4164                 return -EINVAL;
4165
4166         return 0;
4167 }
4168
4169 static int ice_get_coalesce(struct net_device *netdev,
4170                             struct ethtool_coalesce *ec,
4171                             struct kernel_ethtool_coalesce *kernel_coal,
4172                             struct netlink_ext_ack *extack)
4173 {
4174         return __ice_get_coalesce(netdev, ec, -1);
4175 }
4176
4177 static int
4178 ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
4179                        struct ethtool_coalesce *ec)
4180 {
4181         return __ice_get_coalesce(netdev, ec, q_num);
4182 }
4183
4184 /**
4185  * ice_set_rc_coalesce - set ITR values for specific ring container
4186  * @ec: ethtool structure from user to update ITR settings
4187  * @rc: ring container that the ITR values will come from
4188  * @vsi: VSI associated to the ring container
4189  *
4190  * Set specific ITR values. This is done per ice_ring_container because each
4191  * q_vector can have 1 or more rings and all of said ring(s) will have the same
4192  * ITR values.
4193  *
4194  * Returns 0 on success, negative otherwise.
4195  */
4196 static int
4197 ice_set_rc_coalesce(struct ethtool_coalesce *ec,
4198                     struct ice_ring_container *rc, struct ice_vsi *vsi)
4199 {
4200         const char *c_type_str = (rc->type == ICE_RX_CONTAINER) ? "rx" : "tx";
4201         u32 use_adaptive_coalesce, coalesce_usecs;
4202         struct ice_pf *pf = vsi->back;
4203         u16 itr_setting;
4204
4205         if (!rc->rx_ring)
4206                 return -EINVAL;
4207
4208         switch (rc->type) {
4209         case ICE_RX_CONTAINER:
4210         {
4211                 struct ice_q_vector *q_vector = rc->rx_ring->q_vector;
4212
4213                 if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL ||
4214                     (ec->rx_coalesce_usecs_high &&
4215                      ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) {
4216                         netdev_info(vsi->netdev, "Invalid value, %s-usecs-high valid values are 0 (disabled), %d-%d\n",
4217                                     c_type_str, pf->hw.intrl_gran,
4218                                     ICE_MAX_INTRL);
4219                         return -EINVAL;
4220                 }
4221                 if (ec->rx_coalesce_usecs_high != q_vector->intrl &&
4222                     (ec->use_adaptive_rx_coalesce || ec->use_adaptive_tx_coalesce)) {
4223                         netdev_info(vsi->netdev, "Invalid value, %s-usecs-high cannot be changed if adaptive-tx or adaptive-rx is enabled\n",
4224                                     c_type_str);
4225                         return -EINVAL;
4226                 }
4227                 if (ec->rx_coalesce_usecs_high != q_vector->intrl)
4228                         q_vector->intrl = ec->rx_coalesce_usecs_high;
4229
4230                 use_adaptive_coalesce = ec->use_adaptive_rx_coalesce;
4231                 coalesce_usecs = ec->rx_coalesce_usecs;
4232
4233                 break;
4234         }
4235         case ICE_TX_CONTAINER:
4236                 use_adaptive_coalesce = ec->use_adaptive_tx_coalesce;
4237                 coalesce_usecs = ec->tx_coalesce_usecs;
4238
4239                 break;
4240         default:
4241                 dev_dbg(ice_pf_to_dev(pf), "Invalid container type %d\n",
4242                         rc->type);
4243                 return -EINVAL;
4244         }
4245
4246         itr_setting = rc->itr_setting;
4247         if (coalesce_usecs != itr_setting && use_adaptive_coalesce) {
4248                 netdev_info(vsi->netdev, "%s interrupt throttling cannot be changed if adaptive-%s is enabled\n",
4249                             c_type_str, c_type_str);
4250                 return -EINVAL;
4251         }
4252
4253         if (coalesce_usecs > ICE_ITR_MAX) {
4254                 netdev_info(vsi->netdev, "Invalid value, %s-usecs range is 0-%d\n",
4255                             c_type_str, ICE_ITR_MAX);
4256                 return -EINVAL;
4257         }
4258
4259         if (use_adaptive_coalesce) {
4260                 rc->itr_mode = ITR_DYNAMIC;
4261         } else {
4262                 rc->itr_mode = ITR_STATIC;
4263                 /* store user facing value how it was set */
4264                 rc->itr_setting = coalesce_usecs;
4265                 /* write the change to the register */
4266                 ice_write_itr(rc, coalesce_usecs);
4267                 /* force writes to take effect immediately, the flush shouldn't
4268                  * be done in the functions above because the intent is for
4269                  * them to do lazy writes.
4270                  */
4271                 ice_flush(&pf->hw);
4272         }
4273
4274         return 0;
4275 }
4276
4277 /**
4278  * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings
4279  * @vsi: VSI associated to the queue that need updating
4280  * @ec: coalesce settings to program the device with
4281  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
4282  *
4283  * Return 0 on success, and negative under the following conditions:
4284  * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed.
4285  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
4286  */
4287 static int
4288 ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
4289 {
4290         if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
4291                 if (ice_set_rc_coalesce(ec,
4292                                         &vsi->rx_rings[q_num]->q_vector->rx,
4293                                         vsi))
4294                         return -EINVAL;
4295
4296                 if (ice_set_rc_coalesce(ec,
4297                                         &vsi->tx_rings[q_num]->q_vector->tx,
4298                                         vsi))
4299                         return -EINVAL;
4300         } else if (q_num < vsi->num_rxq) {
4301                 if (ice_set_rc_coalesce(ec,
4302                                         &vsi->rx_rings[q_num]->q_vector->rx,
4303                                         vsi))
4304                         return -EINVAL;
4305         } else if (q_num < vsi->num_txq) {
4306                 if (ice_set_rc_coalesce(ec,
4307                                         &vsi->tx_rings[q_num]->q_vector->tx,
4308                                         vsi))
4309                         return -EINVAL;
4310         } else {
4311                 return -EINVAL;
4312         }
4313
4314         return 0;
4315 }
4316
4317 /**
4318  * ice_print_if_odd_usecs - print message if user tries to set odd [tx|rx]-usecs
4319  * @netdev: netdev used for print
4320  * @itr_setting: previous user setting
4321  * @use_adaptive_coalesce: if adaptive coalesce is enabled or being enabled
4322  * @coalesce_usecs: requested value of [tx|rx]-usecs
4323  * @c_type_str: either "rx" or "tx" to match user set field of [tx|rx]-usecs
4324  */
4325 static void
4326 ice_print_if_odd_usecs(struct net_device *netdev, u16 itr_setting,
4327                        u32 use_adaptive_coalesce, u32 coalesce_usecs,
4328                        const char *c_type_str)
4329 {
4330         if (use_adaptive_coalesce)
4331                 return;
4332
4333         if (itr_setting != coalesce_usecs && (coalesce_usecs % 2))
4334                 netdev_info(netdev, "User set %s-usecs to %d, device only supports even values. Rounding down and attempting to set %s-usecs to %d\n",
4335                             c_type_str, coalesce_usecs, c_type_str,
4336                             ITR_REG_ALIGN(coalesce_usecs));
4337 }
4338
4339 /**
4340  * __ice_set_coalesce - set ITR/INTRL values for the device
4341  * @netdev: pointer to the netdev associated with this query
4342  * @ec: ethtool structure to fill with driver's coalesce settings
4343  * @q_num: queue number to get the coalesce settings for
4344  *
4345  * If the caller passes in a negative q_num then we set the coalesce settings
4346  * for all Tx/Rx queues, else use the actual q_num passed in.
4347  */
4348 static int
4349 __ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
4350                    int q_num)
4351 {
4352         struct ice_netdev_priv *np = netdev_priv(netdev);
4353         struct ice_vsi *vsi = np->vsi;
4354
4355         if (q_num < 0) {
4356                 struct ice_q_vector *q_vector = vsi->q_vectors[0];
4357                 int v_idx;
4358
4359                 if (q_vector) {
4360                         ice_print_if_odd_usecs(netdev, q_vector->rx.itr_setting,
4361                                                ec->use_adaptive_rx_coalesce,
4362                                                ec->rx_coalesce_usecs, "rx");
4363
4364                         ice_print_if_odd_usecs(netdev, q_vector->tx.itr_setting,
4365                                                ec->use_adaptive_tx_coalesce,
4366                                                ec->tx_coalesce_usecs, "tx");
4367                 }
4368
4369                 ice_for_each_q_vector(vsi, v_idx) {
4370                         /* In some cases if DCB is configured the num_[rx|tx]q
4371                          * can be less than vsi->num_q_vectors. This check
4372                          * accounts for that so we don't report a false failure
4373                          */
4374                         if (v_idx >= vsi->num_rxq && v_idx >= vsi->num_txq)
4375                                 goto set_complete;
4376
4377                         if (ice_set_q_coalesce(vsi, ec, v_idx))
4378                                 return -EINVAL;
4379
4380                         ice_set_q_vector_intrl(vsi->q_vectors[v_idx]);
4381                 }
4382                 goto set_complete;
4383         }
4384
4385         if (ice_set_q_coalesce(vsi, ec, q_num))
4386                 return -EINVAL;
4387
4388         ice_set_q_vector_intrl(vsi->q_vectors[q_num]);
4389
4390 set_complete:
4391         return 0;
4392 }
4393
4394 static int ice_set_coalesce(struct net_device *netdev,
4395                             struct ethtool_coalesce *ec,
4396                             struct kernel_ethtool_coalesce *kernel_coal,
4397                             struct netlink_ext_ack *extack)
4398 {
4399         return __ice_set_coalesce(netdev, ec, -1);
4400 }
4401
4402 static int
4403 ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
4404                        struct ethtool_coalesce *ec)
4405 {
4406         return __ice_set_coalesce(netdev, ec, q_num);
4407 }
4408
4409 static void
4410 ice_repr_get_drvinfo(struct net_device *netdev,
4411                      struct ethtool_drvinfo *drvinfo)
4412 {
4413         struct ice_repr *repr = ice_netdev_to_repr(netdev);
4414
4415         if (repr->ops.ready(repr))
4416                 return;
4417
4418         __ice_get_drvinfo(netdev, drvinfo, repr->src_vsi);
4419 }
4420
4421 static void
4422 ice_repr_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
4423 {
4424         struct ice_repr *repr = ice_netdev_to_repr(netdev);
4425
4426         /* for port representors only ETH_SS_STATS is supported */
4427         if (repr->ops.ready(repr) || stringset != ETH_SS_STATS)
4428                 return;
4429
4430         __ice_get_strings(netdev, stringset, data, repr->src_vsi);
4431 }
4432
4433 static void
4434 ice_repr_get_ethtool_stats(struct net_device *netdev,
4435                            struct ethtool_stats __always_unused *stats,
4436                            u64 *data)
4437 {
4438         struct ice_repr *repr = ice_netdev_to_repr(netdev);
4439
4440         if (repr->ops.ready(repr))
4441                 return;
4442
4443         __ice_get_ethtool_stats(netdev, stats, data, repr->src_vsi);
4444 }
4445
4446 static int ice_repr_get_sset_count(struct net_device *netdev, int sset)
4447 {
4448         switch (sset) {
4449         case ETH_SS_STATS:
4450                 return ICE_VSI_STATS_LEN;
4451         default:
4452                 return -EOPNOTSUPP;
4453         }
4454 }
4455
4456 #define ICE_I2C_EEPROM_DEV_ADDR         0xA0
4457 #define ICE_I2C_EEPROM_DEV_ADDR2        0xA2
4458 #define ICE_MODULE_TYPE_SFP             0x03
4459 #define ICE_MODULE_TYPE_QSFP_PLUS       0x0D
4460 #define ICE_MODULE_TYPE_QSFP28          0x11
4461 #define ICE_MODULE_SFF_ADDR_MODE        0x04
4462 #define ICE_MODULE_SFF_DIAG_CAPAB       0x40
4463 #define ICE_MODULE_REVISION_ADDR        0x01
4464 #define ICE_MODULE_SFF_8472_COMP        0x5E
4465 #define ICE_MODULE_SFF_8472_SWAP        0x5C
4466 #define ICE_MODULE_QSFP_MAX_LEN         640
4467
4468 /**
4469  * ice_get_module_info - get SFF module type and revision information
4470  * @netdev: network interface device structure
4471  * @modinfo: module EEPROM size and layout information structure
4472  */
4473 static int
4474 ice_get_module_info(struct net_device *netdev,
4475                     struct ethtool_modinfo *modinfo)
4476 {
4477         struct ice_netdev_priv *np = netdev_priv(netdev);
4478         struct ice_vsi *vsi = np->vsi;
4479         struct ice_pf *pf = vsi->back;
4480         struct ice_hw *hw = &pf->hw;
4481         u8 sff8472_comp = 0;
4482         u8 sff8472_swap = 0;
4483         u8 sff8636_rev = 0;
4484         u8 value = 0;
4485         int status;
4486
4487         status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 0x00, 0x00,
4488                                    0, &value, 1, 0, NULL);
4489         if (status)
4490                 return status;
4491
4492         switch (value) {
4493         case ICE_MODULE_TYPE_SFP:
4494                 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
4495                                            ICE_MODULE_SFF_8472_COMP, 0x00, 0,
4496                                            &sff8472_comp, 1, 0, NULL);
4497                 if (status)
4498                         return status;
4499                 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
4500                                            ICE_MODULE_SFF_8472_SWAP, 0x00, 0,
4501                                            &sff8472_swap, 1, 0, NULL);
4502                 if (status)
4503                         return status;
4504
4505                 if (sff8472_swap & ICE_MODULE_SFF_ADDR_MODE) {
4506                         modinfo->type = ETH_MODULE_SFF_8079;
4507                         modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4508                 } else if (sff8472_comp &&
4509                            (sff8472_swap & ICE_MODULE_SFF_DIAG_CAPAB)) {
4510                         modinfo->type = ETH_MODULE_SFF_8472;
4511                         modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
4512                 } else {
4513                         modinfo->type = ETH_MODULE_SFF_8079;
4514                         modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4515                 }
4516                 break;
4517         case ICE_MODULE_TYPE_QSFP_PLUS:
4518         case ICE_MODULE_TYPE_QSFP28:
4519                 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
4520                                            ICE_MODULE_REVISION_ADDR, 0x00, 0,
4521                                            &sff8636_rev, 1, 0, NULL);
4522                 if (status)
4523                         return status;
4524                 /* Check revision compliance */
4525                 if (sff8636_rev > 0x02) {
4526                         /* Module is SFF-8636 compliant */
4527                         modinfo->type = ETH_MODULE_SFF_8636;
4528                         modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
4529                 } else {
4530                         modinfo->type = ETH_MODULE_SFF_8436;
4531                         modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
4532                 }
4533                 break;
4534         default:
4535                 netdev_warn(netdev, "SFF Module Type not recognized.\n");
4536                 return -EINVAL;
4537         }
4538         return 0;
4539 }
4540
4541 /**
4542  * ice_get_module_eeprom - fill buffer with SFF EEPROM contents
4543  * @netdev: network interface device structure
4544  * @ee: EEPROM dump request structure
4545  * @data: buffer to be filled with EEPROM contents
4546  */
4547 static int
4548 ice_get_module_eeprom(struct net_device *netdev,
4549                       struct ethtool_eeprom *ee, u8 *data)
4550 {
4551         struct ice_netdev_priv *np = netdev_priv(netdev);
4552 #define SFF_READ_BLOCK_SIZE 8
4553         u8 value[SFF_READ_BLOCK_SIZE] = { 0 };
4554         u8 addr = ICE_I2C_EEPROM_DEV_ADDR;
4555         struct ice_vsi *vsi = np->vsi;
4556         struct ice_pf *pf = vsi->back;
4557         struct ice_hw *hw = &pf->hw;
4558         bool is_sfp = false;
4559         unsigned int i, j;
4560         u16 offset = 0;
4561         u8 page = 0;
4562         int status;
4563
4564         if (!ee || !ee->len || !data)
4565                 return -EINVAL;
4566
4567         status = ice_aq_sff_eeprom(hw, 0, addr, offset, page, 0, value, 1, 0,
4568                                    NULL);
4569         if (status)
4570                 return status;
4571
4572         if (value[0] == ICE_MODULE_TYPE_SFP)
4573                 is_sfp = true;
4574
4575         memset(data, 0, ee->len);
4576         for (i = 0; i < ee->len; i += SFF_READ_BLOCK_SIZE) {
4577                 offset = i + ee->offset;
4578                 page = 0;
4579
4580                 /* Check if we need to access the other memory page */
4581                 if (is_sfp) {
4582                         if (offset >= ETH_MODULE_SFF_8079_LEN) {
4583                                 offset -= ETH_MODULE_SFF_8079_LEN;
4584                                 addr = ICE_I2C_EEPROM_DEV_ADDR2;
4585                         }
4586                 } else {
4587                         while (offset >= ETH_MODULE_SFF_8436_LEN) {
4588                                 /* Compute memory page number and offset. */
4589                                 offset -= ETH_MODULE_SFF_8436_LEN / 2;
4590                                 page++;
4591                         }
4592                 }
4593
4594                 /* Bit 2 of EEPROM address 0x02 declares upper
4595                  * pages are disabled on QSFP modules.
4596                  * SFP modules only ever use page 0.
4597                  */
4598                 if (page == 0 || !(data[0x2] & 0x4)) {
4599                         u32 copy_len;
4600
4601                         /* If i2c bus is busy due to slow page change or
4602                          * link management access, call can fail. This is normal.
4603                          * So we retry this a few times.
4604                          */
4605                         for (j = 0; j < 4; j++) {
4606                                 status = ice_aq_sff_eeprom(hw, 0, addr, offset, page,
4607                                                            !is_sfp, value,
4608                                                            SFF_READ_BLOCK_SIZE,
4609                                                            0, NULL);
4610                                 netdev_dbg(netdev, "SFF %02X %02X %02X %X = %02X%02X%02X%02X.%02X%02X%02X%02X (%X)\n",
4611                                            addr, offset, page, is_sfp,
4612                                            value[0], value[1], value[2], value[3],
4613                                            value[4], value[5], value[6], value[7],
4614                                            status);
4615                                 if (status) {
4616                                         usleep_range(1500, 2500);
4617                                         memset(value, 0, SFF_READ_BLOCK_SIZE);
4618                                         continue;
4619                                 }
4620                                 break;
4621                         }
4622
4623                         /* Make sure we have enough room for the new block */
4624                         copy_len = min_t(u32, SFF_READ_BLOCK_SIZE, ee->len - i);
4625                         memcpy(data + i, value, copy_len);
4626                 }
4627         }
4628         return 0;
4629 }
4630
4631 /**
4632  * ice_get_port_fec_stats - returns FEC correctable, uncorrectable stats per
4633  *                          pcsquad, pcsport
4634  * @hw: pointer to the HW struct
4635  * @pcs_quad: pcsquad for input port
4636  * @pcs_port: pcsport for input port
4637  * @fec_stats: buffer to hold FEC statistics for given port
4638  *
4639  * Return: 0 on success, negative on failure.
4640  */
4641 static int ice_get_port_fec_stats(struct ice_hw *hw, u16 pcs_quad, u16 pcs_port,
4642                                   struct ethtool_fec_stats *fec_stats)
4643 {
4644         u32 fec_uncorr_low_val = 0, fec_uncorr_high_val = 0;
4645         u32 fec_corr_low_val = 0, fec_corr_high_val = 0;
4646         int err;
4647
4648         if (pcs_quad > 1 || pcs_port > 3)
4649                 return -EINVAL;
4650
4651         err = ice_aq_get_fec_stats(hw, pcs_quad, pcs_port, ICE_FEC_CORR_LOW,
4652                                    &fec_corr_low_val);
4653         if (err)
4654                 return err;
4655
4656         err = ice_aq_get_fec_stats(hw, pcs_quad, pcs_port, ICE_FEC_CORR_HIGH,
4657                                    &fec_corr_high_val);
4658         if (err)
4659                 return err;
4660
4661         err = ice_aq_get_fec_stats(hw, pcs_quad, pcs_port,
4662                                    ICE_FEC_UNCORR_LOW,
4663                                    &fec_uncorr_low_val);
4664         if (err)
4665                 return err;
4666
4667         err = ice_aq_get_fec_stats(hw, pcs_quad, pcs_port,
4668                                    ICE_FEC_UNCORR_HIGH,
4669                                    &fec_uncorr_high_val);
4670         if (err)
4671                 return err;
4672
4673         fec_stats->corrected_blocks.total = (fec_corr_high_val << 16) +
4674                                              fec_corr_low_val;
4675         fec_stats->uncorrectable_blocks.total = (fec_uncorr_high_val << 16) +
4676                                                  fec_uncorr_low_val;
4677         return 0;
4678 }
4679
4680 /**
4681  * ice_get_fec_stats - returns FEC correctable, uncorrectable stats per netdev
4682  * @netdev: network interface device structure
4683  * @fec_stats: buffer to hold FEC statistics for given port
4684  *
4685  */
4686 static void ice_get_fec_stats(struct net_device *netdev,
4687                               struct ethtool_fec_stats *fec_stats)
4688 {
4689         struct ice_netdev_priv *np = netdev_priv(netdev);
4690         struct ice_port_topology port_topology;
4691         struct ice_port_info *pi;
4692         struct ice_pf *pf;
4693         struct ice_hw *hw;
4694         int err;
4695
4696         pf = np->vsi->back;
4697         hw = &pf->hw;
4698         pi = np->vsi->port_info;
4699
4700         /* Serdes parameters are not supported if not the PF VSI */
4701         if (np->vsi->type != ICE_VSI_PF || !pi)
4702                 return;
4703
4704         err = ice_get_port_topology(hw, pi->lport, &port_topology);
4705         if (err) {
4706                 netdev_info(netdev, "Extended register dump failed Lport %d\n",
4707                             pi->lport);
4708                 return;
4709         }
4710
4711         /* Get FEC correctable, uncorrectable counter */
4712         err = ice_get_port_fec_stats(hw, port_topology.pcs_quad_select,
4713                                      port_topology.pcs_port, fec_stats);
4714         if (err)
4715                 netdev_info(netdev, "FEC stats get failed Lport %d Err %d\n",
4716                             pi->lport, err);
4717 }
4718
4719 static const struct ethtool_ops ice_ethtool_ops = {
4720         .cap_rss_ctx_supported  = true,
4721         .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
4722                                      ETHTOOL_COALESCE_USE_ADAPTIVE |
4723                                      ETHTOOL_COALESCE_RX_USECS_HIGH,
4724         .cap_rss_sym_xor_supported = true,
4725         .rxfh_per_ctx_key       = true,
4726         .get_link_ksettings     = ice_get_link_ksettings,
4727         .set_link_ksettings     = ice_set_link_ksettings,
4728         .get_fec_stats          = ice_get_fec_stats,
4729         .get_drvinfo            = ice_get_drvinfo,
4730         .get_regs_len           = ice_get_regs_len,
4731         .get_regs               = ice_get_regs,
4732         .get_wol                = ice_get_wol,
4733         .set_wol                = ice_set_wol,
4734         .get_msglevel           = ice_get_msglevel,
4735         .set_msglevel           = ice_set_msglevel,
4736         .self_test              = ice_self_test,
4737         .get_link               = ethtool_op_get_link,
4738         .get_eeprom_len         = ice_get_eeprom_len,
4739         .get_eeprom             = ice_get_eeprom,
4740         .get_coalesce           = ice_get_coalesce,
4741         .set_coalesce           = ice_set_coalesce,
4742         .get_strings            = ice_get_strings,
4743         .set_phys_id            = ice_set_phys_id,
4744         .get_ethtool_stats      = ice_get_ethtool_stats,
4745         .get_priv_flags         = ice_get_priv_flags,
4746         .set_priv_flags         = ice_set_priv_flags,
4747         .get_sset_count         = ice_get_sset_count,
4748         .get_rxnfc              = ice_get_rxnfc,
4749         .set_rxnfc              = ice_set_rxnfc,
4750         .get_ringparam          = ice_get_ringparam,
4751         .set_ringparam          = ice_set_ringparam,
4752         .nway_reset             = ice_nway_reset,
4753         .get_pauseparam         = ice_get_pauseparam,
4754         .set_pauseparam         = ice_set_pauseparam,
4755         .get_rxfh_key_size      = ice_get_rxfh_key_size,
4756         .get_rxfh_indir_size    = ice_get_rxfh_indir_size,
4757         .get_rxfh               = ice_get_rxfh,
4758         .set_rxfh               = ice_set_rxfh,
4759         .get_channels           = ice_get_channels,
4760         .set_channels           = ice_set_channels,
4761         .get_ts_info            = ice_get_ts_info,
4762         .get_per_queue_coalesce = ice_get_per_q_coalesce,
4763         .set_per_queue_coalesce = ice_set_per_q_coalesce,
4764         .get_fecparam           = ice_get_fecparam,
4765         .set_fecparam           = ice_set_fecparam,
4766         .get_module_info        = ice_get_module_info,
4767         .get_module_eeprom      = ice_get_module_eeprom,
4768 };
4769
4770 static const struct ethtool_ops ice_ethtool_safe_mode_ops = {
4771         .get_link_ksettings     = ice_get_link_ksettings,
4772         .set_link_ksettings     = ice_set_link_ksettings,
4773         .get_drvinfo            = ice_get_drvinfo,
4774         .get_regs_len           = ice_get_regs_len,
4775         .get_regs               = ice_get_regs,
4776         .get_wol                = ice_get_wol,
4777         .set_wol                = ice_set_wol,
4778         .get_msglevel           = ice_get_msglevel,
4779         .set_msglevel           = ice_set_msglevel,
4780         .get_link               = ethtool_op_get_link,
4781         .get_eeprom_len         = ice_get_eeprom_len,
4782         .get_eeprom             = ice_get_eeprom,
4783         .get_strings            = ice_get_strings,
4784         .get_ethtool_stats      = ice_get_ethtool_stats,
4785         .get_sset_count         = ice_get_sset_count,
4786         .get_ringparam          = ice_get_ringparam,
4787         .set_ringparam          = ice_set_ringparam,
4788         .nway_reset             = ice_nway_reset,
4789         .get_channels           = ice_get_channels,
4790 };
4791
4792 /**
4793  * ice_set_ethtool_safe_mode_ops - setup safe mode ethtool ops
4794  * @netdev: network interface device structure
4795  */
4796 void ice_set_ethtool_safe_mode_ops(struct net_device *netdev)
4797 {
4798         netdev->ethtool_ops = &ice_ethtool_safe_mode_ops;
4799 }
4800
4801 static const struct ethtool_ops ice_ethtool_repr_ops = {
4802         .get_drvinfo            = ice_repr_get_drvinfo,
4803         .get_link               = ethtool_op_get_link,
4804         .get_strings            = ice_repr_get_strings,
4805         .get_ethtool_stats      = ice_repr_get_ethtool_stats,
4806         .get_sset_count         = ice_repr_get_sset_count,
4807 };
4808
4809 /**
4810  * ice_set_ethtool_repr_ops - setup VF's port representor ethtool ops
4811  * @netdev: network interface device structure
4812  */
4813 void ice_set_ethtool_repr_ops(struct net_device *netdev)
4814 {
4815         netdev->ethtool_ops = &ice_ethtool_repr_ops;
4816 }
4817
4818 /**
4819  * ice_set_ethtool_ops - setup netdev ethtool ops
4820  * @netdev: network interface device structure
4821  *
4822  * setup netdev ethtool ops with ice specific ops
4823  */
4824 void ice_set_ethtool_ops(struct net_device *netdev)
4825 {
4826         netdev->ethtool_ops = &ice_ethtool_ops;
4827 }
This page took 0.321233 seconds and 4 git commands to generate.