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