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