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