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