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