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