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 < ARRAY_SIZE(phy_type_low_lkup); 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 < ARRAY_SIZE(phy_type_high_lkup); 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 	if (vsi->tx_rings && vsi->rx_rings) {
2685 		ring->rx_pending = vsi->rx_rings[0]->count;
2686 		ring->tx_pending = vsi->tx_rings[0]->count;
2687 	} else {
2688 		ring->rx_pending = 0;
2689 		ring->tx_pending = 0;
2690 	}
2691 
2692 	/* Rx mini and jumbo rings are not supported */
2693 	ring->rx_mini_max_pending = 0;
2694 	ring->rx_jumbo_max_pending = 0;
2695 	ring->rx_mini_pending = 0;
2696 	ring->rx_jumbo_pending = 0;
2697 }
2698 
2699 static int
2700 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
2701 		  struct kernel_ethtool_ringparam *kernel_ring,
2702 		  struct netlink_ext_ack *extack)
2703 {
2704 	struct ice_netdev_priv *np = netdev_priv(netdev);
2705 	struct ice_tx_ring *xdp_rings = NULL;
2706 	struct ice_tx_ring *tx_rings = NULL;
2707 	struct ice_rx_ring *rx_rings = NULL;
2708 	struct ice_vsi *vsi = np->vsi;
2709 	struct ice_pf *pf = vsi->back;
2710 	int i, timeout = 50, err = 0;
2711 	u16 new_rx_cnt, new_tx_cnt;
2712 
2713 	if (ring->tx_pending > ICE_MAX_NUM_DESC ||
2714 	    ring->tx_pending < ICE_MIN_NUM_DESC ||
2715 	    ring->rx_pending > ICE_MAX_NUM_DESC ||
2716 	    ring->rx_pending < ICE_MIN_NUM_DESC) {
2717 		netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n",
2718 			   ring->tx_pending, ring->rx_pending,
2719 			   ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC,
2720 			   ICE_REQ_DESC_MULTIPLE);
2721 		return -EINVAL;
2722 	}
2723 
2724 	/* Return if there is no rings (device is reloading) */
2725 	if (!vsi->tx_rings || !vsi->rx_rings)
2726 		return -EBUSY;
2727 
2728 	new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE);
2729 	if (new_tx_cnt != ring->tx_pending)
2730 		netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n",
2731 			    new_tx_cnt);
2732 	new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE);
2733 	if (new_rx_cnt != ring->rx_pending)
2734 		netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n",
2735 			    new_rx_cnt);
2736 
2737 	/* if nothing to do return success */
2738 	if (new_tx_cnt == vsi->tx_rings[0]->count &&
2739 	    new_rx_cnt == vsi->rx_rings[0]->count) {
2740 		netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n");
2741 		return 0;
2742 	}
2743 
2744 	/* If there is a AF_XDP UMEM attached to any of Rx rings,
2745 	 * disallow changing the number of descriptors -- regardless
2746 	 * if the netdev is running or not.
2747 	 */
2748 	if (ice_xsk_any_rx_ring_ena(vsi))
2749 		return -EBUSY;
2750 
2751 	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
2752 		timeout--;
2753 		if (!timeout)
2754 			return -EBUSY;
2755 		usleep_range(1000, 2000);
2756 	}
2757 
2758 	/* set for the next time the netdev is started */
2759 	if (!netif_running(vsi->netdev)) {
2760 		ice_for_each_alloc_txq(vsi, i)
2761 			vsi->tx_rings[i]->count = new_tx_cnt;
2762 		ice_for_each_alloc_rxq(vsi, i)
2763 			vsi->rx_rings[i]->count = new_rx_cnt;
2764 		if (ice_is_xdp_ena_vsi(vsi))
2765 			ice_for_each_xdp_txq(vsi, i)
2766 				vsi->xdp_rings[i]->count = new_tx_cnt;
2767 		vsi->num_tx_desc = (u16)new_tx_cnt;
2768 		vsi->num_rx_desc = (u16)new_rx_cnt;
2769 		netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n");
2770 		goto done;
2771 	}
2772 
2773 	if (new_tx_cnt == vsi->tx_rings[0]->count)
2774 		goto process_rx;
2775 
2776 	/* alloc updated Tx resources */
2777 	netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n",
2778 		    vsi->tx_rings[0]->count, new_tx_cnt);
2779 
2780 	tx_rings = kcalloc(vsi->num_txq, sizeof(*tx_rings), GFP_KERNEL);
2781 	if (!tx_rings) {
2782 		err = -ENOMEM;
2783 		goto done;
2784 	}
2785 
2786 	ice_for_each_txq(vsi, i) {
2787 		/* clone ring and setup updated count */
2788 		tx_rings[i] = *vsi->tx_rings[i];
2789 		tx_rings[i].count = new_tx_cnt;
2790 		tx_rings[i].desc = NULL;
2791 		tx_rings[i].tx_buf = NULL;
2792 		tx_rings[i].tx_tstamps = &pf->ptp.port.tx;
2793 		err = ice_setup_tx_ring(&tx_rings[i]);
2794 		if (err) {
2795 			while (i--)
2796 				ice_clean_tx_ring(&tx_rings[i]);
2797 			kfree(tx_rings);
2798 			goto done;
2799 		}
2800 	}
2801 
2802 	if (!ice_is_xdp_ena_vsi(vsi))
2803 		goto process_rx;
2804 
2805 	/* alloc updated XDP resources */
2806 	netdev_info(netdev, "Changing XDP descriptor count from %d to %d\n",
2807 		    vsi->xdp_rings[0]->count, new_tx_cnt);
2808 
2809 	xdp_rings = kcalloc(vsi->num_xdp_txq, sizeof(*xdp_rings), GFP_KERNEL);
2810 	if (!xdp_rings) {
2811 		err = -ENOMEM;
2812 		goto free_tx;
2813 	}
2814 
2815 	ice_for_each_xdp_txq(vsi, i) {
2816 		/* clone ring and setup updated count */
2817 		xdp_rings[i] = *vsi->xdp_rings[i];
2818 		xdp_rings[i].count = new_tx_cnt;
2819 		xdp_rings[i].desc = NULL;
2820 		xdp_rings[i].tx_buf = NULL;
2821 		err = ice_setup_tx_ring(&xdp_rings[i]);
2822 		if (err) {
2823 			while (i--)
2824 				ice_clean_tx_ring(&xdp_rings[i]);
2825 			kfree(xdp_rings);
2826 			goto free_tx;
2827 		}
2828 		ice_set_ring_xdp(&xdp_rings[i]);
2829 	}
2830 
2831 process_rx:
2832 	if (new_rx_cnt == vsi->rx_rings[0]->count)
2833 		goto process_link;
2834 
2835 	/* alloc updated Rx resources */
2836 	netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n",
2837 		    vsi->rx_rings[0]->count, new_rx_cnt);
2838 
2839 	rx_rings = kcalloc(vsi->num_rxq, sizeof(*rx_rings), GFP_KERNEL);
2840 	if (!rx_rings) {
2841 		err = -ENOMEM;
2842 		goto done;
2843 	}
2844 
2845 	ice_for_each_rxq(vsi, i) {
2846 		/* clone ring and setup updated count */
2847 		rx_rings[i] = *vsi->rx_rings[i];
2848 		rx_rings[i].count = new_rx_cnt;
2849 		rx_rings[i].cached_phctime = pf->ptp.cached_phc_time;
2850 		rx_rings[i].desc = NULL;
2851 		rx_rings[i].rx_buf = NULL;
2852 		/* this is to allow wr32 to have something to write to
2853 		 * during early allocation of Rx buffers
2854 		 */
2855 		rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS;
2856 
2857 		err = ice_setup_rx_ring(&rx_rings[i]);
2858 		if (err)
2859 			goto rx_unwind;
2860 
2861 		/* allocate Rx buffers */
2862 		err = ice_alloc_rx_bufs(&rx_rings[i],
2863 					ICE_RX_DESC_UNUSED(&rx_rings[i]));
2864 rx_unwind:
2865 		if (err) {
2866 			while (i) {
2867 				i--;
2868 				ice_free_rx_ring(&rx_rings[i]);
2869 			}
2870 			kfree(rx_rings);
2871 			err = -ENOMEM;
2872 			goto free_tx;
2873 		}
2874 	}
2875 
2876 process_link:
2877 	/* Bring interface down, copy in the new ring info, then restore the
2878 	 * interface. if VSI is up, bring it down and then back up
2879 	 */
2880 	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
2881 		ice_down(vsi);
2882 
2883 		if (tx_rings) {
2884 			ice_for_each_txq(vsi, i) {
2885 				ice_free_tx_ring(vsi->tx_rings[i]);
2886 				*vsi->tx_rings[i] = tx_rings[i];
2887 			}
2888 			kfree(tx_rings);
2889 		}
2890 
2891 		if (rx_rings) {
2892 			ice_for_each_rxq(vsi, i) {
2893 				ice_free_rx_ring(vsi->rx_rings[i]);
2894 				/* copy the real tail offset */
2895 				rx_rings[i].tail = vsi->rx_rings[i]->tail;
2896 				/* this is to fake out the allocation routine
2897 				 * into thinking it has to realloc everything
2898 				 * but the recycling logic will let us re-use
2899 				 * the buffers allocated above
2900 				 */
2901 				rx_rings[i].next_to_use = 0;
2902 				rx_rings[i].next_to_clean = 0;
2903 				rx_rings[i].next_to_alloc = 0;
2904 				*vsi->rx_rings[i] = rx_rings[i];
2905 			}
2906 			kfree(rx_rings);
2907 		}
2908 
2909 		if (xdp_rings) {
2910 			ice_for_each_xdp_txq(vsi, i) {
2911 				ice_free_tx_ring(vsi->xdp_rings[i]);
2912 				*vsi->xdp_rings[i] = xdp_rings[i];
2913 			}
2914 			kfree(xdp_rings);
2915 		}
2916 
2917 		vsi->num_tx_desc = new_tx_cnt;
2918 		vsi->num_rx_desc = new_rx_cnt;
2919 		ice_up(vsi);
2920 	}
2921 	goto done;
2922 
2923 free_tx:
2924 	/* error cleanup if the Rx allocations failed after getting Tx */
2925 	if (tx_rings) {
2926 		ice_for_each_txq(vsi, i)
2927 			ice_free_tx_ring(&tx_rings[i]);
2928 		kfree(tx_rings);
2929 	}
2930 
2931 done:
2932 	clear_bit(ICE_CFG_BUSY, pf->state);
2933 	return err;
2934 }
2935 
2936 /**
2937  * ice_get_pauseparam - Get Flow Control status
2938  * @netdev: network interface device structure
2939  * @pause: ethernet pause (flow control) parameters
2940  *
2941  * Get requested flow control status from PHY capability.
2942  * If autoneg is true, then ethtool will send the ETHTOOL_GSET ioctl which
2943  * is handled by ice_get_link_ksettings. ice_get_link_ksettings will report
2944  * the negotiated Rx/Tx pause via lp_advertising.
2945  */
2946 static void
2947 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2948 {
2949 	struct ice_netdev_priv *np = netdev_priv(netdev);
2950 	struct ice_port_info *pi = np->vsi->port_info;
2951 	struct ice_aqc_get_phy_caps_data *pcaps;
2952 	struct ice_dcbx_cfg *dcbx_cfg;
2953 	int status;
2954 
2955 	/* Initialize pause params */
2956 	pause->rx_pause = 0;
2957 	pause->tx_pause = 0;
2958 
2959 	dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
2960 
2961 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2962 	if (!pcaps)
2963 		return;
2964 
2965 	/* Get current PHY config */
2966 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2967 				     NULL);
2968 	if (status)
2969 		goto out;
2970 
2971 	pause->autoneg = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
2972 							     AUTONEG_DISABLE;
2973 
2974 	if (dcbx_cfg->pfc.pfcena)
2975 		/* PFC enabled so report LFC as off */
2976 		goto out;
2977 
2978 	if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE)
2979 		pause->tx_pause = 1;
2980 	if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)
2981 		pause->rx_pause = 1;
2982 
2983 out:
2984 	kfree(pcaps);
2985 }
2986 
2987 /**
2988  * ice_set_pauseparam - Set Flow Control parameter
2989  * @netdev: network interface device structure
2990  * @pause: return Tx/Rx flow control status
2991  */
2992 static int
2993 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
2994 {
2995 	struct ice_netdev_priv *np = netdev_priv(netdev);
2996 	struct ice_aqc_get_phy_caps_data *pcaps;
2997 	struct ice_link_status *hw_link_info;
2998 	struct ice_pf *pf = np->vsi->back;
2999 	struct ice_dcbx_cfg *dcbx_cfg;
3000 	struct ice_vsi *vsi = np->vsi;
3001 	struct ice_hw *hw = &pf->hw;
3002 	struct ice_port_info *pi;
3003 	u8 aq_failures;
3004 	bool link_up;
3005 	u32 is_an;
3006 	int err;
3007 
3008 	pi = vsi->port_info;
3009 	hw_link_info = &pi->phy.link_info;
3010 	dcbx_cfg = &pi->qos_cfg.local_dcbx_cfg;
3011 	link_up = hw_link_info->link_info & ICE_AQ_LINK_UP;
3012 
3013 	/* Changing the port's flow control is not supported if this isn't the
3014 	 * PF VSI
3015 	 */
3016 	if (vsi->type != ICE_VSI_PF) {
3017 		netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n");
3018 		return -EOPNOTSUPP;
3019 	}
3020 
3021 	/* Get pause param reports configured and negotiated flow control pause
3022 	 * when ETHTOOL_GLINKSETTINGS is defined. Since ETHTOOL_GLINKSETTINGS is
3023 	 * defined get pause param pause->autoneg reports SW configured setting,
3024 	 * so compare pause->autoneg with SW configured to prevent the user from
3025 	 * using set pause param to chance autoneg.
3026 	 */
3027 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
3028 	if (!pcaps)
3029 		return -ENOMEM;
3030 
3031 	/* Get current PHY config */
3032 	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
3033 				  NULL);
3034 	if (err) {
3035 		kfree(pcaps);
3036 		return err;
3037 	}
3038 
3039 	is_an = ice_is_phy_caps_an_enabled(pcaps) ? AUTONEG_ENABLE :
3040 						    AUTONEG_DISABLE;
3041 
3042 	kfree(pcaps);
3043 
3044 	if (pause->autoneg != is_an) {
3045 		netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
3046 		return -EOPNOTSUPP;
3047 	}
3048 
3049 	/* If we have link and don't have autoneg */
3050 	if (!test_bit(ICE_DOWN, pf->state) &&
3051 	    !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) {
3052 		/* Send message that it might not necessarily work*/
3053 		netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
3054 	}
3055 
3056 	if (dcbx_cfg->pfc.pfcena) {
3057 		netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n");
3058 		return -EOPNOTSUPP;
3059 	}
3060 	if (pause->rx_pause && pause->tx_pause)
3061 		pi->fc.req_mode = ICE_FC_FULL;
3062 	else if (pause->rx_pause && !pause->tx_pause)
3063 		pi->fc.req_mode = ICE_FC_RX_PAUSE;
3064 	else if (!pause->rx_pause && pause->tx_pause)
3065 		pi->fc.req_mode = ICE_FC_TX_PAUSE;
3066 	else if (!pause->rx_pause && !pause->tx_pause)
3067 		pi->fc.req_mode = ICE_FC_NONE;
3068 	else
3069 		return -EINVAL;
3070 
3071 	/* Set the FC mode and only restart AN if link is up */
3072 	err = ice_set_fc(pi, &aq_failures, link_up);
3073 
3074 	if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) {
3075 		netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %d aq_err %s\n",
3076 			    err, ice_aq_str(hw->adminq.sq_last_status));
3077 		err = -EAGAIN;
3078 	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) {
3079 		netdev_info(netdev, "Set fc failed on the set_phy_config call with err %d aq_err %s\n",
3080 			    err, ice_aq_str(hw->adminq.sq_last_status));
3081 		err = -EAGAIN;
3082 	} else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) {
3083 		netdev_info(netdev, "Set fc failed on the get_link_info call with err %d aq_err %s\n",
3084 			    err, ice_aq_str(hw->adminq.sq_last_status));
3085 		err = -EAGAIN;
3086 	}
3087 
3088 	return err;
3089 }
3090 
3091 /**
3092  * ice_get_rxfh_key_size - get the RSS hash key size
3093  * @netdev: network interface device structure
3094  *
3095  * Returns the table size.
3096  */
3097 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev)
3098 {
3099 	return ICE_VSIQF_HKEY_ARRAY_SIZE;
3100 }
3101 
3102 /**
3103  * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size
3104  * @netdev: network interface device structure
3105  *
3106  * Returns the table size.
3107  */
3108 static u32 ice_get_rxfh_indir_size(struct net_device *netdev)
3109 {
3110 	struct ice_netdev_priv *np = netdev_priv(netdev);
3111 
3112 	return np->vsi->rss_table_size;
3113 }
3114 
3115 static int
3116 ice_get_rxfh_context(struct net_device *netdev, u32 *indir,
3117 		     u8 *key, u8 *hfunc, u32 rss_context)
3118 {
3119 	struct ice_netdev_priv *np = netdev_priv(netdev);
3120 	struct ice_vsi *vsi = np->vsi;
3121 	struct ice_pf *pf = vsi->back;
3122 	u16 qcount, offset;
3123 	int err, num_tc, i;
3124 	u8 *lut;
3125 
3126 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3127 		netdev_warn(netdev, "RSS is not supported on this VSI!\n");
3128 		return -EOPNOTSUPP;
3129 	}
3130 
3131 	if (rss_context && !ice_is_adq_active(pf)) {
3132 		netdev_err(netdev, "RSS context cannot be non-zero when ADQ is not configured.\n");
3133 		return -EINVAL;
3134 	}
3135 
3136 	qcount = vsi->mqprio_qopt.qopt.count[rss_context];
3137 	offset = vsi->mqprio_qopt.qopt.offset[rss_context];
3138 
3139 	if (rss_context && ice_is_adq_active(pf)) {
3140 		num_tc = vsi->mqprio_qopt.qopt.num_tc;
3141 		if (rss_context >= num_tc) {
3142 			netdev_err(netdev, "RSS context:%d  > num_tc:%d\n",
3143 				   rss_context, num_tc);
3144 			return -EINVAL;
3145 		}
3146 		/* Use channel VSI of given TC */
3147 		vsi = vsi->tc_map_vsi[rss_context];
3148 	}
3149 
3150 	if (hfunc)
3151 		*hfunc = ETH_RSS_HASH_TOP;
3152 
3153 	if (!indir)
3154 		return 0;
3155 
3156 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3157 	if (!lut)
3158 		return -ENOMEM;
3159 
3160 	err = ice_get_rss_key(vsi, key);
3161 	if (err)
3162 		goto out;
3163 
3164 	err = ice_get_rss_lut(vsi, lut, vsi->rss_table_size);
3165 	if (err)
3166 		goto out;
3167 
3168 	if (ice_is_adq_active(pf)) {
3169 		for (i = 0; i < vsi->rss_table_size; i++)
3170 			indir[i] = offset + lut[i] % qcount;
3171 		goto out;
3172 	}
3173 
3174 	for (i = 0; i < vsi->rss_table_size; i++)
3175 		indir[i] = lut[i];
3176 
3177 out:
3178 	kfree(lut);
3179 	return err;
3180 }
3181 
3182 /**
3183  * ice_get_rxfh - get the Rx flow hash indirection table
3184  * @netdev: network interface device structure
3185  * @indir: indirection table
3186  * @key: hash key
3187  * @hfunc: hash function
3188  *
3189  * Reads the indirection table directly from the hardware.
3190  */
3191 static int
3192 ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
3193 {
3194 	return ice_get_rxfh_context(netdev, indir, key, hfunc, 0);
3195 }
3196 
3197 /**
3198  * ice_set_rxfh - set the Rx flow hash indirection table
3199  * @netdev: network interface device structure
3200  * @indir: indirection table
3201  * @key: hash key
3202  * @hfunc: hash function
3203  *
3204  * Returns -EINVAL if the table specifies an invalid queue ID, otherwise
3205  * returns 0 after programming the table.
3206  */
3207 static int
3208 ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key,
3209 	     const u8 hfunc)
3210 {
3211 	struct ice_netdev_priv *np = netdev_priv(netdev);
3212 	struct ice_vsi *vsi = np->vsi;
3213 	struct ice_pf *pf = vsi->back;
3214 	struct device *dev;
3215 	int err;
3216 
3217 	dev = ice_pf_to_dev(pf);
3218 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3219 		return -EOPNOTSUPP;
3220 
3221 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
3222 		/* RSS not supported return error here */
3223 		netdev_warn(netdev, "RSS is not configured on this VSI!\n");
3224 		return -EIO;
3225 	}
3226 
3227 	if (ice_is_adq_active(pf)) {
3228 		netdev_err(netdev, "Cannot change RSS params with ADQ configured.\n");
3229 		return -EOPNOTSUPP;
3230 	}
3231 
3232 	if (key) {
3233 		if (!vsi->rss_hkey_user) {
3234 			vsi->rss_hkey_user =
3235 				devm_kzalloc(dev, ICE_VSIQF_HKEY_ARRAY_SIZE,
3236 					     GFP_KERNEL);
3237 			if (!vsi->rss_hkey_user)
3238 				return -ENOMEM;
3239 		}
3240 		memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE);
3241 
3242 		err = ice_set_rss_key(vsi, vsi->rss_hkey_user);
3243 		if (err)
3244 			return err;
3245 	}
3246 
3247 	if (!vsi->rss_lut_user) {
3248 		vsi->rss_lut_user = devm_kzalloc(dev, vsi->rss_table_size,
3249 						 GFP_KERNEL);
3250 		if (!vsi->rss_lut_user)
3251 			return -ENOMEM;
3252 	}
3253 
3254 	/* Each 32 bits pointed by 'indir' is stored with a lut entry */
3255 	if (indir) {
3256 		int i;
3257 
3258 		for (i = 0; i < vsi->rss_table_size; i++)
3259 			vsi->rss_lut_user[i] = (u8)(indir[i]);
3260 	} else {
3261 		ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size,
3262 				 vsi->rss_size);
3263 	}
3264 
3265 	err = ice_set_rss_lut(vsi, vsi->rss_lut_user, vsi->rss_table_size);
3266 	if (err)
3267 		return err;
3268 
3269 	return 0;
3270 }
3271 
3272 static int
3273 ice_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
3274 {
3275 	struct ice_pf *pf = ice_netdev_to_pf(dev);
3276 
3277 	/* only report timestamping if PTP is enabled */
3278 	if (!test_bit(ICE_FLAG_PTP, pf->flags))
3279 		return ethtool_op_get_ts_info(dev, info);
3280 
3281 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
3282 				SOF_TIMESTAMPING_RX_SOFTWARE |
3283 				SOF_TIMESTAMPING_SOFTWARE |
3284 				SOF_TIMESTAMPING_TX_HARDWARE |
3285 				SOF_TIMESTAMPING_RX_HARDWARE |
3286 				SOF_TIMESTAMPING_RAW_HARDWARE;
3287 
3288 	info->phc_index = ice_get_ptp_clock_index(pf);
3289 
3290 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
3291 
3292 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
3293 
3294 	return 0;
3295 }
3296 
3297 /**
3298  * ice_get_max_txq - return the maximum number of Tx queues for in a PF
3299  * @pf: PF structure
3300  */
3301 static int ice_get_max_txq(struct ice_pf *pf)
3302 {
3303 	return min3(pf->num_lan_msix, (u16)num_online_cpus(),
3304 		    (u16)pf->hw.func_caps.common_cap.num_txq);
3305 }
3306 
3307 /**
3308  * ice_get_max_rxq - return the maximum number of Rx queues for in a PF
3309  * @pf: PF structure
3310  */
3311 static int ice_get_max_rxq(struct ice_pf *pf)
3312 {
3313 	return min3(pf->num_lan_msix, (u16)num_online_cpus(),
3314 		    (u16)pf->hw.func_caps.common_cap.num_rxq);
3315 }
3316 
3317 /**
3318  * ice_get_combined_cnt - return the current number of combined channels
3319  * @vsi: PF VSI pointer
3320  *
3321  * Go through all queue vectors and count ones that have both Rx and Tx ring
3322  * attached
3323  */
3324 static u32 ice_get_combined_cnt(struct ice_vsi *vsi)
3325 {
3326 	u32 combined = 0;
3327 	int q_idx;
3328 
3329 	ice_for_each_q_vector(vsi, q_idx) {
3330 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
3331 
3332 		if (q_vector->rx.rx_ring && q_vector->tx.tx_ring)
3333 			combined++;
3334 	}
3335 
3336 	return combined;
3337 }
3338 
3339 /**
3340  * ice_get_channels - get the current and max supported channels
3341  * @dev: network interface device structure
3342  * @ch: ethtool channel data structure
3343  */
3344 static void
3345 ice_get_channels(struct net_device *dev, struct ethtool_channels *ch)
3346 {
3347 	struct ice_netdev_priv *np = netdev_priv(dev);
3348 	struct ice_vsi *vsi = np->vsi;
3349 	struct ice_pf *pf = vsi->back;
3350 
3351 	/* report maximum channels */
3352 	ch->max_rx = ice_get_max_rxq(pf);
3353 	ch->max_tx = ice_get_max_txq(pf);
3354 	ch->max_combined = min_t(int, ch->max_rx, ch->max_tx);
3355 
3356 	/* report current channels */
3357 	ch->combined_count = ice_get_combined_cnt(vsi);
3358 	ch->rx_count = vsi->num_rxq - ch->combined_count;
3359 	ch->tx_count = vsi->num_txq - ch->combined_count;
3360 
3361 	/* report other queues */
3362 	ch->other_count = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
3363 	ch->max_other = ch->other_count;
3364 }
3365 
3366 /**
3367  * ice_get_valid_rss_size - return valid number of RSS queues
3368  * @hw: pointer to the HW structure
3369  * @new_size: requested RSS queues
3370  */
3371 static int ice_get_valid_rss_size(struct ice_hw *hw, int new_size)
3372 {
3373 	struct ice_hw_common_caps *caps = &hw->func_caps.common_cap;
3374 
3375 	return min_t(int, new_size, BIT(caps->rss_table_entry_width));
3376 }
3377 
3378 /**
3379  * ice_vsi_set_dflt_rss_lut - set default RSS LUT with requested RSS size
3380  * @vsi: VSI to reconfigure RSS LUT on
3381  * @req_rss_size: requested range of queue numbers for hashing
3382  *
3383  * Set the VSI's RSS parameters, configure the RSS LUT based on these.
3384  */
3385 static int ice_vsi_set_dflt_rss_lut(struct ice_vsi *vsi, int req_rss_size)
3386 {
3387 	struct ice_pf *pf = vsi->back;
3388 	struct device *dev;
3389 	struct ice_hw *hw;
3390 	int err;
3391 	u8 *lut;
3392 
3393 	dev = ice_pf_to_dev(pf);
3394 	hw = &pf->hw;
3395 
3396 	if (!req_rss_size)
3397 		return -EINVAL;
3398 
3399 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
3400 	if (!lut)
3401 		return -ENOMEM;
3402 
3403 	/* set RSS LUT parameters */
3404 	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3405 		vsi->rss_size = 1;
3406 	else
3407 		vsi->rss_size = ice_get_valid_rss_size(hw, req_rss_size);
3408 
3409 	/* create/set RSS LUT */
3410 	ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
3411 	err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
3412 	if (err)
3413 		dev_err(dev, "Cannot set RSS lut, err %d aq_err %s\n", err,
3414 			ice_aq_str(hw->adminq.sq_last_status));
3415 
3416 	kfree(lut);
3417 	return err;
3418 }
3419 
3420 /**
3421  * ice_set_channels - set the number channels
3422  * @dev: network interface device structure
3423  * @ch: ethtool channel data structure
3424  */
3425 static int ice_set_channels(struct net_device *dev, struct ethtool_channels *ch)
3426 {
3427 	struct ice_netdev_priv *np = netdev_priv(dev);
3428 	struct ice_vsi *vsi = np->vsi;
3429 	struct ice_pf *pf = vsi->back;
3430 	int new_rx = 0, new_tx = 0;
3431 	bool locked = false;
3432 	int ret = 0;
3433 
3434 	/* do not support changing channels in Safe Mode */
3435 	if (ice_is_safe_mode(pf)) {
3436 		netdev_err(dev, "Changing channel in Safe Mode is not supported\n");
3437 		return -EOPNOTSUPP;
3438 	}
3439 	/* do not support changing other_count */
3440 	if (ch->other_count != (test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1U : 0U))
3441 		return -EINVAL;
3442 
3443 	if (ice_is_adq_active(pf)) {
3444 		netdev_err(dev, "Cannot set channels with ADQ configured.\n");
3445 		return -EOPNOTSUPP;
3446 	}
3447 
3448 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags) && pf->hw.fdir_active_fltr) {
3449 		netdev_err(dev, "Cannot set channels when Flow Director filters are active\n");
3450 		return -EOPNOTSUPP;
3451 	}
3452 
3453 	if (ch->rx_count && ch->tx_count) {
3454 		netdev_err(dev, "Dedicated RX or TX channels cannot be used simultaneously\n");
3455 		return -EINVAL;
3456 	}
3457 
3458 	new_rx = ch->combined_count + ch->rx_count;
3459 	new_tx = ch->combined_count + ch->tx_count;
3460 
3461 	if (new_rx < vsi->tc_cfg.numtc) {
3462 		netdev_err(dev, "Cannot set less Rx channels, than Traffic Classes you have (%u)\n",
3463 			   vsi->tc_cfg.numtc);
3464 		return -EINVAL;
3465 	}
3466 	if (new_tx < vsi->tc_cfg.numtc) {
3467 		netdev_err(dev, "Cannot set less Tx channels, than Traffic Classes you have (%u)\n",
3468 			   vsi->tc_cfg.numtc);
3469 		return -EINVAL;
3470 	}
3471 	if (new_rx > ice_get_max_rxq(pf)) {
3472 		netdev_err(dev, "Maximum allowed Rx channels is %d\n",
3473 			   ice_get_max_rxq(pf));
3474 		return -EINVAL;
3475 	}
3476 	if (new_tx > ice_get_max_txq(pf)) {
3477 		netdev_err(dev, "Maximum allowed Tx channels is %d\n",
3478 			   ice_get_max_txq(pf));
3479 		return -EINVAL;
3480 	}
3481 
3482 	if (pf->adev) {
3483 		mutex_lock(&pf->adev_mutex);
3484 		device_lock(&pf->adev->dev);
3485 		locked = true;
3486 		if (pf->adev->dev.driver) {
3487 			netdev_err(dev, "Cannot change channels when RDMA is active\n");
3488 			ret = -EBUSY;
3489 			goto adev_unlock;
3490 		}
3491 	}
3492 
3493 	ice_vsi_recfg_qs(vsi, new_rx, new_tx, locked);
3494 
3495 	if (!netif_is_rxfh_configured(dev)) {
3496 		ret = ice_vsi_set_dflt_rss_lut(vsi, new_rx);
3497 		goto adev_unlock;
3498 	}
3499 
3500 	/* Update rss_size due to change in Rx queues */
3501 	vsi->rss_size = ice_get_valid_rss_size(&pf->hw, new_rx);
3502 
3503 adev_unlock:
3504 	if (locked) {
3505 		device_unlock(&pf->adev->dev);
3506 		mutex_unlock(&pf->adev_mutex);
3507 	}
3508 	return ret;
3509 }
3510 
3511 /**
3512  * ice_get_wol - get current Wake on LAN configuration
3513  * @netdev: network interface device structure
3514  * @wol: Ethtool structure to retrieve WoL settings
3515  */
3516 static void ice_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3517 {
3518 	struct ice_netdev_priv *np = netdev_priv(netdev);
3519 	struct ice_pf *pf = np->vsi->back;
3520 
3521 	if (np->vsi->type != ICE_VSI_PF)
3522 		netdev_warn(netdev, "Wake on LAN is not supported on this interface!\n");
3523 
3524 	/* Get WoL settings based on the HW capability */
3525 	if (ice_is_wol_supported(&pf->hw)) {
3526 		wol->supported = WAKE_MAGIC;
3527 		wol->wolopts = pf->wol_ena ? WAKE_MAGIC : 0;
3528 	} else {
3529 		wol->supported = 0;
3530 		wol->wolopts = 0;
3531 	}
3532 }
3533 
3534 /**
3535  * ice_set_wol - set Wake on LAN on supported device
3536  * @netdev: network interface device structure
3537  * @wol: Ethtool structure to set WoL
3538  */
3539 static int ice_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
3540 {
3541 	struct ice_netdev_priv *np = netdev_priv(netdev);
3542 	struct ice_vsi *vsi = np->vsi;
3543 	struct ice_pf *pf = vsi->back;
3544 
3545 	if (vsi->type != ICE_VSI_PF || !ice_is_wol_supported(&pf->hw))
3546 		return -EOPNOTSUPP;
3547 
3548 	/* only magic packet is supported */
3549 	if (wol->wolopts && wol->wolopts != WAKE_MAGIC)
3550 		return -EOPNOTSUPP;
3551 
3552 	/* Set WoL only if there is a new value */
3553 	if (pf->wol_ena != !!wol->wolopts) {
3554 		pf->wol_ena = !!wol->wolopts;
3555 		device_set_wakeup_enable(ice_pf_to_dev(pf), pf->wol_ena);
3556 		netdev_dbg(netdev, "WoL magic packet %sabled\n",
3557 			   pf->wol_ena ? "en" : "dis");
3558 	}
3559 
3560 	return 0;
3561 }
3562 
3563 /**
3564  * ice_get_rc_coalesce - get ITR values for specific ring container
3565  * @ec: ethtool structure to fill with driver's coalesce settings
3566  * @rc: ring container that the ITR values will come from
3567  *
3568  * Query the device for ice_ring_container specific ITR values. This is
3569  * done per ice_ring_container because each q_vector can have 1 or more rings
3570  * and all of said ring(s) will have the same ITR values.
3571  *
3572  * Returns 0 on success, negative otherwise.
3573  */
3574 static int
3575 ice_get_rc_coalesce(struct ethtool_coalesce *ec, struct ice_ring_container *rc)
3576 {
3577 	if (!rc->rx_ring)
3578 		return -EINVAL;
3579 
3580 	switch (rc->type) {
3581 	case ICE_RX_CONTAINER:
3582 		ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc);
3583 		ec->rx_coalesce_usecs = rc->itr_setting;
3584 		ec->rx_coalesce_usecs_high = rc->rx_ring->q_vector->intrl;
3585 		break;
3586 	case ICE_TX_CONTAINER:
3587 		ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc);
3588 		ec->tx_coalesce_usecs = rc->itr_setting;
3589 		break;
3590 	default:
3591 		dev_dbg(ice_pf_to_dev(rc->rx_ring->vsi->back), "Invalid c_type %d\n", rc->type);
3592 		return -EINVAL;
3593 	}
3594 
3595 	return 0;
3596 }
3597 
3598 /**
3599  * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings
3600  * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings
3601  * @ec: coalesce settings to program the device with
3602  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3603  *
3604  * Return 0 on success, and negative under the following conditions:
3605  * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed.
3606  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3607  */
3608 static int
3609 ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3610 {
3611 	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3612 		if (ice_get_rc_coalesce(ec,
3613 					&vsi->rx_rings[q_num]->q_vector->rx))
3614 			return -EINVAL;
3615 		if (ice_get_rc_coalesce(ec,
3616 					&vsi->tx_rings[q_num]->q_vector->tx))
3617 			return -EINVAL;
3618 	} else if (q_num < vsi->num_rxq) {
3619 		if (ice_get_rc_coalesce(ec,
3620 					&vsi->rx_rings[q_num]->q_vector->rx))
3621 			return -EINVAL;
3622 	} else if (q_num < vsi->num_txq) {
3623 		if (ice_get_rc_coalesce(ec,
3624 					&vsi->tx_rings[q_num]->q_vector->tx))
3625 			return -EINVAL;
3626 	} else {
3627 		return -EINVAL;
3628 	}
3629 
3630 	return 0;
3631 }
3632 
3633 /**
3634  * __ice_get_coalesce - get ITR/INTRL values for the device
3635  * @netdev: pointer to the netdev associated with this query
3636  * @ec: ethtool structure to fill with driver's coalesce settings
3637  * @q_num: queue number to get the coalesce settings for
3638  *
3639  * If the caller passes in a negative q_num then we return coalesce settings
3640  * based on queue number 0, else use the actual q_num passed in.
3641  */
3642 static int
3643 __ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3644 		   int q_num)
3645 {
3646 	struct ice_netdev_priv *np = netdev_priv(netdev);
3647 	struct ice_vsi *vsi = np->vsi;
3648 
3649 	if (q_num < 0)
3650 		q_num = 0;
3651 
3652 	if (ice_get_q_coalesce(vsi, ec, q_num))
3653 		return -EINVAL;
3654 
3655 	return 0;
3656 }
3657 
3658 static int ice_get_coalesce(struct net_device *netdev,
3659 			    struct ethtool_coalesce *ec,
3660 			    struct kernel_ethtool_coalesce *kernel_coal,
3661 			    struct netlink_ext_ack *extack)
3662 {
3663 	return __ice_get_coalesce(netdev, ec, -1);
3664 }
3665 
3666 static int
3667 ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num,
3668 		       struct ethtool_coalesce *ec)
3669 {
3670 	return __ice_get_coalesce(netdev, ec, q_num);
3671 }
3672 
3673 /**
3674  * ice_set_rc_coalesce - set ITR values for specific ring container
3675  * @ec: ethtool structure from user to update ITR settings
3676  * @rc: ring container that the ITR values will come from
3677  * @vsi: VSI associated to the ring container
3678  *
3679  * Set specific ITR values. This is done per ice_ring_container because each
3680  * q_vector can have 1 or more rings and all of said ring(s) will have the same
3681  * ITR values.
3682  *
3683  * Returns 0 on success, negative otherwise.
3684  */
3685 static int
3686 ice_set_rc_coalesce(struct ethtool_coalesce *ec,
3687 		    struct ice_ring_container *rc, struct ice_vsi *vsi)
3688 {
3689 	const char *c_type_str = (rc->type == ICE_RX_CONTAINER) ? "rx" : "tx";
3690 	u32 use_adaptive_coalesce, coalesce_usecs;
3691 	struct ice_pf *pf = vsi->back;
3692 	u16 itr_setting;
3693 
3694 	if (!rc->rx_ring)
3695 		return -EINVAL;
3696 
3697 	switch (rc->type) {
3698 	case ICE_RX_CONTAINER:
3699 	{
3700 		struct ice_q_vector *q_vector = rc->rx_ring->q_vector;
3701 
3702 		if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL ||
3703 		    (ec->rx_coalesce_usecs_high &&
3704 		     ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) {
3705 			netdev_info(vsi->netdev, "Invalid value, %s-usecs-high valid values are 0 (disabled), %d-%d\n",
3706 				    c_type_str, pf->hw.intrl_gran,
3707 				    ICE_MAX_INTRL);
3708 			return -EINVAL;
3709 		}
3710 		if (ec->rx_coalesce_usecs_high != q_vector->intrl &&
3711 		    (ec->use_adaptive_rx_coalesce || ec->use_adaptive_tx_coalesce)) {
3712 			netdev_info(vsi->netdev, "Invalid value, %s-usecs-high cannot be changed if adaptive-tx or adaptive-rx is enabled\n",
3713 				    c_type_str);
3714 			return -EINVAL;
3715 		}
3716 		if (ec->rx_coalesce_usecs_high != q_vector->intrl)
3717 			q_vector->intrl = ec->rx_coalesce_usecs_high;
3718 
3719 		use_adaptive_coalesce = ec->use_adaptive_rx_coalesce;
3720 		coalesce_usecs = ec->rx_coalesce_usecs;
3721 
3722 		break;
3723 	}
3724 	case ICE_TX_CONTAINER:
3725 		use_adaptive_coalesce = ec->use_adaptive_tx_coalesce;
3726 		coalesce_usecs = ec->tx_coalesce_usecs;
3727 
3728 		break;
3729 	default:
3730 		dev_dbg(ice_pf_to_dev(pf), "Invalid container type %d\n",
3731 			rc->type);
3732 		return -EINVAL;
3733 	}
3734 
3735 	itr_setting = rc->itr_setting;
3736 	if (coalesce_usecs != itr_setting && use_adaptive_coalesce) {
3737 		netdev_info(vsi->netdev, "%s interrupt throttling cannot be changed if adaptive-%s is enabled\n",
3738 			    c_type_str, c_type_str);
3739 		return -EINVAL;
3740 	}
3741 
3742 	if (coalesce_usecs > ICE_ITR_MAX) {
3743 		netdev_info(vsi->netdev, "Invalid value, %s-usecs range is 0-%d\n",
3744 			    c_type_str, ICE_ITR_MAX);
3745 		return -EINVAL;
3746 	}
3747 
3748 	if (use_adaptive_coalesce) {
3749 		rc->itr_mode = ITR_DYNAMIC;
3750 	} else {
3751 		rc->itr_mode = ITR_STATIC;
3752 		/* store user facing value how it was set */
3753 		rc->itr_setting = coalesce_usecs;
3754 		/* write the change to the register */
3755 		ice_write_itr(rc, coalesce_usecs);
3756 		/* force writes to take effect immediately, the flush shouldn't
3757 		 * be done in the functions above because the intent is for
3758 		 * them to do lazy writes.
3759 		 */
3760 		ice_flush(&pf->hw);
3761 	}
3762 
3763 	return 0;
3764 }
3765 
3766 /**
3767  * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings
3768  * @vsi: VSI associated to the queue that need updating
3769  * @ec: coalesce settings to program the device with
3770  * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index
3771  *
3772  * Return 0 on success, and negative under the following conditions:
3773  * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed.
3774  * 2. The q_num passed in is not a valid number/index for Tx and Rx rings.
3775  */
3776 static int
3777 ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num)
3778 {
3779 	if (q_num < vsi->num_rxq && q_num < vsi->num_txq) {
3780 		if (ice_set_rc_coalesce(ec,
3781 					&vsi->rx_rings[q_num]->q_vector->rx,
3782 					vsi))
3783 			return -EINVAL;
3784 
3785 		if (ice_set_rc_coalesce(ec,
3786 					&vsi->tx_rings[q_num]->q_vector->tx,
3787 					vsi))
3788 			return -EINVAL;
3789 	} else if (q_num < vsi->num_rxq) {
3790 		if (ice_set_rc_coalesce(ec,
3791 					&vsi->rx_rings[q_num]->q_vector->rx,
3792 					vsi))
3793 			return -EINVAL;
3794 	} else if (q_num < vsi->num_txq) {
3795 		if (ice_set_rc_coalesce(ec,
3796 					&vsi->tx_rings[q_num]->q_vector->tx,
3797 					vsi))
3798 			return -EINVAL;
3799 	} else {
3800 		return -EINVAL;
3801 	}
3802 
3803 	return 0;
3804 }
3805 
3806 /**
3807  * ice_print_if_odd_usecs - print message if user tries to set odd [tx|rx]-usecs
3808  * @netdev: netdev used for print
3809  * @itr_setting: previous user setting
3810  * @use_adaptive_coalesce: if adaptive coalesce is enabled or being enabled
3811  * @coalesce_usecs: requested value of [tx|rx]-usecs
3812  * @c_type_str: either "rx" or "tx" to match user set field of [tx|rx]-usecs
3813  */
3814 static void
3815 ice_print_if_odd_usecs(struct net_device *netdev, u16 itr_setting,
3816 		       u32 use_adaptive_coalesce, u32 coalesce_usecs,
3817 		       const char *c_type_str)
3818 {
3819 	if (use_adaptive_coalesce)
3820 		return;
3821 
3822 	if (itr_setting != coalesce_usecs && (coalesce_usecs % 2))
3823 		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",
3824 			    c_type_str, coalesce_usecs, c_type_str,
3825 			    ITR_REG_ALIGN(coalesce_usecs));
3826 }
3827 
3828 /**
3829  * __ice_set_coalesce - set ITR/INTRL values for the device
3830  * @netdev: pointer to the netdev associated with this query
3831  * @ec: ethtool structure to fill with driver's coalesce settings
3832  * @q_num: queue number to get the coalesce settings for
3833  *
3834  * If the caller passes in a negative q_num then we set the coalesce settings
3835  * for all Tx/Rx queues, else use the actual q_num passed in.
3836  */
3837 static int
3838 __ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec,
3839 		   int q_num)
3840 {
3841 	struct ice_netdev_priv *np = netdev_priv(netdev);
3842 	struct ice_vsi *vsi = np->vsi;
3843 
3844 	if (q_num < 0) {
3845 		struct ice_q_vector *q_vector = vsi->q_vectors[0];
3846 		int v_idx;
3847 
3848 		if (q_vector) {
3849 			ice_print_if_odd_usecs(netdev, q_vector->rx.itr_setting,
3850 					       ec->use_adaptive_rx_coalesce,
3851 					       ec->rx_coalesce_usecs, "rx");
3852 
3853 			ice_print_if_odd_usecs(netdev, q_vector->tx.itr_setting,
3854 					       ec->use_adaptive_tx_coalesce,
3855 					       ec->tx_coalesce_usecs, "tx");
3856 		}
3857 
3858 		ice_for_each_q_vector(vsi, v_idx) {
3859 			/* In some cases if DCB is configured the num_[rx|tx]q
3860 			 * can be less than vsi->num_q_vectors. This check
3861 			 * accounts for that so we don't report a false failure
3862 			 */
3863 			if (v_idx >= vsi->num_rxq && v_idx >= vsi->num_txq)
3864 				goto set_complete;
3865 
3866 			if (ice_set_q_coalesce(vsi, ec, v_idx))
3867 				return -EINVAL;
3868 
3869 			ice_set_q_vector_intrl(vsi->q_vectors[v_idx]);
3870 		}
3871 		goto set_complete;
3872 	}
3873 
3874 	if (ice_set_q_coalesce(vsi, ec, q_num))
3875 		return -EINVAL;
3876 
3877 	ice_set_q_vector_intrl(vsi->q_vectors[q_num]);
3878 
3879 set_complete:
3880 	return 0;
3881 }
3882 
3883 static int ice_set_coalesce(struct net_device *netdev,
3884 			    struct ethtool_coalesce *ec,
3885 			    struct kernel_ethtool_coalesce *kernel_coal,
3886 			    struct netlink_ext_ack *extack)
3887 {
3888 	return __ice_set_coalesce(netdev, ec, -1);
3889 }
3890 
3891 static int
3892 ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num,
3893 		       struct ethtool_coalesce *ec)
3894 {
3895 	return __ice_set_coalesce(netdev, ec, q_num);
3896 }
3897 
3898 static void
3899 ice_repr_get_drvinfo(struct net_device *netdev,
3900 		     struct ethtool_drvinfo *drvinfo)
3901 {
3902 	struct ice_repr *repr = ice_netdev_to_repr(netdev);
3903 
3904 	if (ice_check_vf_ready_for_cfg(repr->vf))
3905 		return;
3906 
3907 	__ice_get_drvinfo(netdev, drvinfo, repr->src_vsi);
3908 }
3909 
3910 static void
3911 ice_repr_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
3912 {
3913 	struct ice_repr *repr = ice_netdev_to_repr(netdev);
3914 
3915 	/* for port representors only ETH_SS_STATS is supported */
3916 	if (ice_check_vf_ready_for_cfg(repr->vf) ||
3917 	    stringset != ETH_SS_STATS)
3918 		return;
3919 
3920 	__ice_get_strings(netdev, stringset, data, repr->src_vsi);
3921 }
3922 
3923 static void
3924 ice_repr_get_ethtool_stats(struct net_device *netdev,
3925 			   struct ethtool_stats __always_unused *stats,
3926 			   u64 *data)
3927 {
3928 	struct ice_repr *repr = ice_netdev_to_repr(netdev);
3929 
3930 	if (ice_check_vf_ready_for_cfg(repr->vf))
3931 		return;
3932 
3933 	__ice_get_ethtool_stats(netdev, stats, data, repr->src_vsi);
3934 }
3935 
3936 static int ice_repr_get_sset_count(struct net_device *netdev, int sset)
3937 {
3938 	switch (sset) {
3939 	case ETH_SS_STATS:
3940 		return ICE_VSI_STATS_LEN;
3941 	default:
3942 		return -EOPNOTSUPP;
3943 	}
3944 }
3945 
3946 #define ICE_I2C_EEPROM_DEV_ADDR		0xA0
3947 #define ICE_I2C_EEPROM_DEV_ADDR2	0xA2
3948 #define ICE_MODULE_TYPE_SFP		0x03
3949 #define ICE_MODULE_TYPE_QSFP_PLUS	0x0D
3950 #define ICE_MODULE_TYPE_QSFP28		0x11
3951 #define ICE_MODULE_SFF_ADDR_MODE	0x04
3952 #define ICE_MODULE_SFF_DIAG_CAPAB	0x40
3953 #define ICE_MODULE_REVISION_ADDR	0x01
3954 #define ICE_MODULE_SFF_8472_COMP	0x5E
3955 #define ICE_MODULE_SFF_8472_SWAP	0x5C
3956 #define ICE_MODULE_QSFP_MAX_LEN		640
3957 
3958 /**
3959  * ice_get_module_info - get SFF module type and revision information
3960  * @netdev: network interface device structure
3961  * @modinfo: module EEPROM size and layout information structure
3962  */
3963 static int
3964 ice_get_module_info(struct net_device *netdev,
3965 		    struct ethtool_modinfo *modinfo)
3966 {
3967 	struct ice_netdev_priv *np = netdev_priv(netdev);
3968 	struct ice_vsi *vsi = np->vsi;
3969 	struct ice_pf *pf = vsi->back;
3970 	struct ice_hw *hw = &pf->hw;
3971 	u8 sff8472_comp = 0;
3972 	u8 sff8472_swap = 0;
3973 	u8 sff8636_rev = 0;
3974 	u8 value = 0;
3975 	int status;
3976 
3977 	status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 0x00, 0x00,
3978 				   0, &value, 1, 0, NULL);
3979 	if (status)
3980 		return status;
3981 
3982 	switch (value) {
3983 	case ICE_MODULE_TYPE_SFP:
3984 		status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
3985 					   ICE_MODULE_SFF_8472_COMP, 0x00, 0,
3986 					   &sff8472_comp, 1, 0, NULL);
3987 		if (status)
3988 			return status;
3989 		status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
3990 					   ICE_MODULE_SFF_8472_SWAP, 0x00, 0,
3991 					   &sff8472_swap, 1, 0, NULL);
3992 		if (status)
3993 			return status;
3994 
3995 		if (sff8472_swap & ICE_MODULE_SFF_ADDR_MODE) {
3996 			modinfo->type = ETH_MODULE_SFF_8079;
3997 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
3998 		} else if (sff8472_comp &&
3999 			   (sff8472_swap & ICE_MODULE_SFF_DIAG_CAPAB)) {
4000 			modinfo->type = ETH_MODULE_SFF_8472;
4001 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
4002 		} else {
4003 			modinfo->type = ETH_MODULE_SFF_8079;
4004 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4005 		}
4006 		break;
4007 	case ICE_MODULE_TYPE_QSFP_PLUS:
4008 	case ICE_MODULE_TYPE_QSFP28:
4009 		status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR,
4010 					   ICE_MODULE_REVISION_ADDR, 0x00, 0,
4011 					   &sff8636_rev, 1, 0, NULL);
4012 		if (status)
4013 			return status;
4014 		/* Check revision compliance */
4015 		if (sff8636_rev > 0x02) {
4016 			/* Module is SFF-8636 compliant */
4017 			modinfo->type = ETH_MODULE_SFF_8636;
4018 			modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
4019 		} else {
4020 			modinfo->type = ETH_MODULE_SFF_8436;
4021 			modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN;
4022 		}
4023 		break;
4024 	default:
4025 		netdev_warn(netdev, "SFF Module Type not recognized.\n");
4026 		return -EINVAL;
4027 	}
4028 	return 0;
4029 }
4030 
4031 /**
4032  * ice_get_module_eeprom - fill buffer with SFF EEPROM contents
4033  * @netdev: network interface device structure
4034  * @ee: EEPROM dump request structure
4035  * @data: buffer to be filled with EEPROM contents
4036  */
4037 static int
4038 ice_get_module_eeprom(struct net_device *netdev,
4039 		      struct ethtool_eeprom *ee, u8 *data)
4040 {
4041 	struct ice_netdev_priv *np = netdev_priv(netdev);
4042 #define SFF_READ_BLOCK_SIZE 8
4043 	u8 value[SFF_READ_BLOCK_SIZE] = { 0 };
4044 	u8 addr = ICE_I2C_EEPROM_DEV_ADDR;
4045 	struct ice_vsi *vsi = np->vsi;
4046 	struct ice_pf *pf = vsi->back;
4047 	struct ice_hw *hw = &pf->hw;
4048 	bool is_sfp = false;
4049 	unsigned int i, j;
4050 	u16 offset = 0;
4051 	u8 page = 0;
4052 	int status;
4053 
4054 	if (!ee || !ee->len || !data)
4055 		return -EINVAL;
4056 
4057 	status = ice_aq_sff_eeprom(hw, 0, addr, offset, page, 0, value, 1, 0,
4058 				   NULL);
4059 	if (status)
4060 		return status;
4061 
4062 	if (value[0] == ICE_MODULE_TYPE_SFP)
4063 		is_sfp = true;
4064 
4065 	memset(data, 0, ee->len);
4066 	for (i = 0; i < ee->len; i += SFF_READ_BLOCK_SIZE) {
4067 		offset = i + ee->offset;
4068 		page = 0;
4069 
4070 		/* Check if we need to access the other memory page */
4071 		if (is_sfp) {
4072 			if (offset >= ETH_MODULE_SFF_8079_LEN) {
4073 				offset -= ETH_MODULE_SFF_8079_LEN;
4074 				addr = ICE_I2C_EEPROM_DEV_ADDR2;
4075 			}
4076 		} else {
4077 			while (offset >= ETH_MODULE_SFF_8436_LEN) {
4078 				/* Compute memory page number and offset. */
4079 				offset -= ETH_MODULE_SFF_8436_LEN / 2;
4080 				page++;
4081 			}
4082 		}
4083 
4084 		/* Bit 2 of EEPROM address 0x02 declares upper
4085 		 * pages are disabled on QSFP modules.
4086 		 * SFP modules only ever use page 0.
4087 		 */
4088 		if (page == 0 || !(data[0x2] & 0x4)) {
4089 			u32 copy_len;
4090 
4091 			/* If i2c bus is busy due to slow page change or
4092 			 * link management access, call can fail. This is normal.
4093 			 * So we retry this a few times.
4094 			 */
4095 			for (j = 0; j < 4; j++) {
4096 				status = ice_aq_sff_eeprom(hw, 0, addr, offset, page,
4097 							   !is_sfp, value,
4098 							   SFF_READ_BLOCK_SIZE,
4099 							   0, NULL);
4100 				netdev_dbg(netdev, "SFF %02X %02X %02X %X = %02X%02X%02X%02X.%02X%02X%02X%02X (%X)\n",
4101 					   addr, offset, page, is_sfp,
4102 					   value[0], value[1], value[2], value[3],
4103 					   value[4], value[5], value[6], value[7],
4104 					   status);
4105 				if (status) {
4106 					usleep_range(1500, 2500);
4107 					memset(value, 0, SFF_READ_BLOCK_SIZE);
4108 					continue;
4109 				}
4110 				break;
4111 			}
4112 
4113 			/* Make sure we have enough room for the new block */
4114 			copy_len = min_t(u32, SFF_READ_BLOCK_SIZE, ee->len - i);
4115 			memcpy(data + i, value, copy_len);
4116 		}
4117 	}
4118 	return 0;
4119 }
4120 
4121 static const struct ethtool_ops ice_ethtool_ops = {
4122 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
4123 				     ETHTOOL_COALESCE_USE_ADAPTIVE |
4124 				     ETHTOOL_COALESCE_RX_USECS_HIGH,
4125 	.get_link_ksettings	= ice_get_link_ksettings,
4126 	.set_link_ksettings	= ice_set_link_ksettings,
4127 	.get_drvinfo		= ice_get_drvinfo,
4128 	.get_regs_len		= ice_get_regs_len,
4129 	.get_regs		= ice_get_regs,
4130 	.get_wol		= ice_get_wol,
4131 	.set_wol		= ice_set_wol,
4132 	.get_msglevel		= ice_get_msglevel,
4133 	.set_msglevel		= ice_set_msglevel,
4134 	.self_test		= ice_self_test,
4135 	.get_link		= ethtool_op_get_link,
4136 	.get_eeprom_len		= ice_get_eeprom_len,
4137 	.get_eeprom		= ice_get_eeprom,
4138 	.get_coalesce		= ice_get_coalesce,
4139 	.set_coalesce		= ice_set_coalesce,
4140 	.get_strings		= ice_get_strings,
4141 	.set_phys_id		= ice_set_phys_id,
4142 	.get_ethtool_stats      = ice_get_ethtool_stats,
4143 	.get_priv_flags		= ice_get_priv_flags,
4144 	.set_priv_flags		= ice_set_priv_flags,
4145 	.get_sset_count		= ice_get_sset_count,
4146 	.get_rxnfc		= ice_get_rxnfc,
4147 	.set_rxnfc		= ice_set_rxnfc,
4148 	.get_ringparam		= ice_get_ringparam,
4149 	.set_ringparam		= ice_set_ringparam,
4150 	.nway_reset		= ice_nway_reset,
4151 	.get_pauseparam		= ice_get_pauseparam,
4152 	.set_pauseparam		= ice_set_pauseparam,
4153 	.get_rxfh_key_size	= ice_get_rxfh_key_size,
4154 	.get_rxfh_indir_size	= ice_get_rxfh_indir_size,
4155 	.get_rxfh_context	= ice_get_rxfh_context,
4156 	.get_rxfh		= ice_get_rxfh,
4157 	.set_rxfh		= ice_set_rxfh,
4158 	.get_channels		= ice_get_channels,
4159 	.set_channels		= ice_set_channels,
4160 	.get_ts_info		= ice_get_ts_info,
4161 	.get_per_queue_coalesce	= ice_get_per_q_coalesce,
4162 	.set_per_queue_coalesce	= ice_set_per_q_coalesce,
4163 	.get_fecparam		= ice_get_fecparam,
4164 	.set_fecparam		= ice_set_fecparam,
4165 	.get_module_info	= ice_get_module_info,
4166 	.get_module_eeprom	= ice_get_module_eeprom,
4167 };
4168 
4169 static const struct ethtool_ops ice_ethtool_safe_mode_ops = {
4170 	.get_link_ksettings	= ice_get_link_ksettings,
4171 	.set_link_ksettings	= ice_set_link_ksettings,
4172 	.get_drvinfo		= ice_get_drvinfo,
4173 	.get_regs_len		= ice_get_regs_len,
4174 	.get_regs		= ice_get_regs,
4175 	.get_wol		= ice_get_wol,
4176 	.set_wol		= ice_set_wol,
4177 	.get_msglevel		= ice_get_msglevel,
4178 	.set_msglevel		= ice_set_msglevel,
4179 	.get_link		= ethtool_op_get_link,
4180 	.get_eeprom_len		= ice_get_eeprom_len,
4181 	.get_eeprom		= ice_get_eeprom,
4182 	.get_strings		= ice_get_strings,
4183 	.get_ethtool_stats	= ice_get_ethtool_stats,
4184 	.get_sset_count		= ice_get_sset_count,
4185 	.get_ringparam		= ice_get_ringparam,
4186 	.set_ringparam		= ice_set_ringparam,
4187 	.nway_reset		= ice_nway_reset,
4188 	.get_channels		= ice_get_channels,
4189 };
4190 
4191 /**
4192  * ice_set_ethtool_safe_mode_ops - setup safe mode ethtool ops
4193  * @netdev: network interface device structure
4194  */
4195 void ice_set_ethtool_safe_mode_ops(struct net_device *netdev)
4196 {
4197 	netdev->ethtool_ops = &ice_ethtool_safe_mode_ops;
4198 }
4199 
4200 static const struct ethtool_ops ice_ethtool_repr_ops = {
4201 	.get_drvinfo		= ice_repr_get_drvinfo,
4202 	.get_link		= ethtool_op_get_link,
4203 	.get_strings		= ice_repr_get_strings,
4204 	.get_ethtool_stats      = ice_repr_get_ethtool_stats,
4205 	.get_sset_count		= ice_repr_get_sset_count,
4206 };
4207 
4208 /**
4209  * ice_set_ethtool_repr_ops - setup VF's port representor ethtool ops
4210  * @netdev: network interface device structure
4211  */
4212 void ice_set_ethtool_repr_ops(struct net_device *netdev)
4213 {
4214 	netdev->ethtool_ops = &ice_ethtool_repr_ops;
4215 }
4216 
4217 /**
4218  * ice_set_ethtool_ops - setup netdev ethtool ops
4219  * @netdev: network interface device structure
4220  *
4221  * setup netdev ethtool ops with ice specific ops
4222  */
4223 void ice_set_ethtool_ops(struct net_device *netdev)
4224 {
4225 	netdev->ethtool_ops = &ice_ethtool_ops;
4226 }
4227