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