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