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