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