1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2014 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26 
27 /* ethtool support for i40e */
28 
29 #include "i40e.h"
30 #include "i40e_diag.h"
31 
32 struct i40e_stats {
33 	char stat_string[ETH_GSTRING_LEN];
34 	int sizeof_stat;
35 	int stat_offset;
36 };
37 
38 #define I40E_STAT(_type, _name, _stat) { \
39 	.stat_string = _name, \
40 	.sizeof_stat = FIELD_SIZEOF(_type, _stat), \
41 	.stat_offset = offsetof(_type, _stat) \
42 }
43 #define I40E_NETDEV_STAT(_net_stat) \
44 		I40E_STAT(struct net_device_stats, #_net_stat, _net_stat)
45 #define I40E_PF_STAT(_name, _stat) \
46 		I40E_STAT(struct i40e_pf, _name, _stat)
47 #define I40E_VSI_STAT(_name, _stat) \
48 		I40E_STAT(struct i40e_vsi, _name, _stat)
49 #define I40E_VEB_STAT(_name, _stat) \
50 		I40E_STAT(struct i40e_veb, _name, _stat)
51 
52 static const struct i40e_stats i40e_gstrings_net_stats[] = {
53 	I40E_NETDEV_STAT(rx_packets),
54 	I40E_NETDEV_STAT(tx_packets),
55 	I40E_NETDEV_STAT(rx_bytes),
56 	I40E_NETDEV_STAT(tx_bytes),
57 	I40E_NETDEV_STAT(rx_errors),
58 	I40E_NETDEV_STAT(tx_errors),
59 	I40E_NETDEV_STAT(rx_dropped),
60 	I40E_NETDEV_STAT(tx_dropped),
61 	I40E_NETDEV_STAT(collisions),
62 	I40E_NETDEV_STAT(rx_length_errors),
63 	I40E_NETDEV_STAT(rx_crc_errors),
64 };
65 
66 static const struct i40e_stats i40e_gstrings_veb_stats[] = {
67 	I40E_VEB_STAT("rx_bytes", stats.rx_bytes),
68 	I40E_VEB_STAT("tx_bytes", stats.tx_bytes),
69 	I40E_VEB_STAT("rx_unicast", stats.rx_unicast),
70 	I40E_VEB_STAT("tx_unicast", stats.tx_unicast),
71 	I40E_VEB_STAT("rx_multicast", stats.rx_multicast),
72 	I40E_VEB_STAT("tx_multicast", stats.tx_multicast),
73 	I40E_VEB_STAT("rx_broadcast", stats.rx_broadcast),
74 	I40E_VEB_STAT("tx_broadcast", stats.tx_broadcast),
75 	I40E_VEB_STAT("rx_discards", stats.rx_discards),
76 	I40E_VEB_STAT("tx_discards", stats.tx_discards),
77 	I40E_VEB_STAT("tx_errors", stats.tx_errors),
78 	I40E_VEB_STAT("rx_unknown_protocol", stats.rx_unknown_protocol),
79 };
80 
81 static const struct i40e_stats i40e_gstrings_misc_stats[] = {
82 	I40E_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
83 	I40E_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
84 	I40E_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
85 	I40E_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
86 	I40E_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
87 	I40E_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
88 	I40E_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
89 };
90 
91 static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
92 				 struct ethtool_rxnfc *cmd);
93 
94 /* These PF_STATs might look like duplicates of some NETDEV_STATs,
95  * but they are separate.  This device supports Virtualization, and
96  * as such might have several netdevs supporting VMDq and FCoE going
97  * through a single port.  The NETDEV_STATs are for individual netdevs
98  * seen at the top of the stack, and the PF_STATs are for the physical
99  * function at the bottom of the stack hosting those netdevs.
100  *
101  * The PF_STATs are appended to the netdev stats only when ethtool -S
102  * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
103  */
104 static struct i40e_stats i40e_gstrings_stats[] = {
105 	I40E_PF_STAT("rx_bytes", stats.eth.rx_bytes),
106 	I40E_PF_STAT("tx_bytes", stats.eth.tx_bytes),
107 	I40E_PF_STAT("rx_unicast", stats.eth.rx_unicast),
108 	I40E_PF_STAT("tx_unicast", stats.eth.tx_unicast),
109 	I40E_PF_STAT("rx_multicast", stats.eth.rx_multicast),
110 	I40E_PF_STAT("tx_multicast", stats.eth.tx_multicast),
111 	I40E_PF_STAT("rx_broadcast", stats.eth.rx_broadcast),
112 	I40E_PF_STAT("tx_broadcast", stats.eth.tx_broadcast),
113 	I40E_PF_STAT("tx_errors", stats.eth.tx_errors),
114 	I40E_PF_STAT("rx_dropped", stats.eth.rx_discards),
115 	I40E_PF_STAT("tx_dropped", stats.eth.tx_discards),
116 	I40E_PF_STAT("tx_dropped_link_down", stats.tx_dropped_link_down),
117 	I40E_PF_STAT("crc_errors", stats.crc_errors),
118 	I40E_PF_STAT("illegal_bytes", stats.illegal_bytes),
119 	I40E_PF_STAT("mac_local_faults", stats.mac_local_faults),
120 	I40E_PF_STAT("mac_remote_faults", stats.mac_remote_faults),
121 	I40E_PF_STAT("tx_timeout", tx_timeout_count),
122 	I40E_PF_STAT("rx_csum_bad", hw_csum_rx_error),
123 	I40E_PF_STAT("rx_length_errors", stats.rx_length_errors),
124 	I40E_PF_STAT("link_xon_rx", stats.link_xon_rx),
125 	I40E_PF_STAT("link_xoff_rx", stats.link_xoff_rx),
126 	I40E_PF_STAT("link_xon_tx", stats.link_xon_tx),
127 	I40E_PF_STAT("link_xoff_tx", stats.link_xoff_tx),
128 	I40E_PF_STAT("rx_size_64", stats.rx_size_64),
129 	I40E_PF_STAT("rx_size_127", stats.rx_size_127),
130 	I40E_PF_STAT("rx_size_255", stats.rx_size_255),
131 	I40E_PF_STAT("rx_size_511", stats.rx_size_511),
132 	I40E_PF_STAT("rx_size_1023", stats.rx_size_1023),
133 	I40E_PF_STAT("rx_size_1522", stats.rx_size_1522),
134 	I40E_PF_STAT("rx_size_big", stats.rx_size_big),
135 	I40E_PF_STAT("tx_size_64", stats.tx_size_64),
136 	I40E_PF_STAT("tx_size_127", stats.tx_size_127),
137 	I40E_PF_STAT("tx_size_255", stats.tx_size_255),
138 	I40E_PF_STAT("tx_size_511", stats.tx_size_511),
139 	I40E_PF_STAT("tx_size_1023", stats.tx_size_1023),
140 	I40E_PF_STAT("tx_size_1522", stats.tx_size_1522),
141 	I40E_PF_STAT("tx_size_big", stats.tx_size_big),
142 	I40E_PF_STAT("rx_undersize", stats.rx_undersize),
143 	I40E_PF_STAT("rx_fragments", stats.rx_fragments),
144 	I40E_PF_STAT("rx_oversize", stats.rx_oversize),
145 	I40E_PF_STAT("rx_jabber", stats.rx_jabber),
146 	I40E_PF_STAT("VF_admin_queue_requests", vf_aq_requests),
147 	I40E_PF_STAT("rx_hwtstamp_cleared", rx_hwtstamp_cleared),
148 	I40E_PF_STAT("fdir_atr_match", stats.fd_atr_match),
149 	I40E_PF_STAT("fdir_sb_match", stats.fd_sb_match),
150 
151 	/* LPI stats */
152 	I40E_PF_STAT("tx_lpi_status", stats.tx_lpi_status),
153 	I40E_PF_STAT("rx_lpi_status", stats.rx_lpi_status),
154 	I40E_PF_STAT("tx_lpi_count", stats.tx_lpi_count),
155 	I40E_PF_STAT("rx_lpi_count", stats.rx_lpi_count),
156 };
157 
158 #define I40E_QUEUE_STATS_LEN(n) \
159 	(((struct i40e_netdev_priv *)netdev_priv((n)))->vsi->num_queue_pairs \
160 	    * 2 /* Tx and Rx together */                                     \
161 	    * (sizeof(struct i40e_queue_stats) / sizeof(u64)))
162 #define I40E_GLOBAL_STATS_LEN	ARRAY_SIZE(i40e_gstrings_stats)
163 #define I40E_NETDEV_STATS_LEN   ARRAY_SIZE(i40e_gstrings_net_stats)
164 #define I40E_MISC_STATS_LEN	ARRAY_SIZE(i40e_gstrings_misc_stats)
165 #define I40E_VSI_STATS_LEN(n)   (I40E_NETDEV_STATS_LEN + \
166 				 I40E_MISC_STATS_LEN + \
167 				 I40E_QUEUE_STATS_LEN((n)))
168 #define I40E_PFC_STATS_LEN ( \
169 		(FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_rx) + \
170 		 FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_rx) + \
171 		 FIELD_SIZEOF(struct i40e_pf, stats.priority_xoff_tx) + \
172 		 FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_tx) + \
173 		 FIELD_SIZEOF(struct i40e_pf, stats.priority_xon_2_xoff)) \
174 		 / sizeof(u64))
175 #define I40E_VEB_STATS_LEN	ARRAY_SIZE(i40e_gstrings_veb_stats)
176 #define I40E_PF_STATS_LEN(n)	(I40E_GLOBAL_STATS_LEN + \
177 				 I40E_PFC_STATS_LEN + \
178 				 I40E_VSI_STATS_LEN((n)))
179 
180 enum i40e_ethtool_test_id {
181 	I40E_ETH_TEST_REG = 0,
182 	I40E_ETH_TEST_EEPROM,
183 	I40E_ETH_TEST_INTR,
184 	I40E_ETH_TEST_LOOPBACK,
185 	I40E_ETH_TEST_LINK,
186 };
187 
188 static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
189 	"Register test  (offline)",
190 	"Eeprom test    (offline)",
191 	"Interrupt test (offline)",
192 	"Loopback test  (offline)",
193 	"Link test   (on/offline)"
194 };
195 
196 #define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
197 
198 /**
199  * i40e_get_settings - Get Link Speed and Duplex settings
200  * @netdev: network interface device structure
201  * @ecmd: ethtool command
202  *
203  * Reports speed/duplex settings based on media_type
204  **/
205 static int i40e_get_settings(struct net_device *netdev,
206 			     struct ethtool_cmd *ecmd)
207 {
208 	struct i40e_netdev_priv *np = netdev_priv(netdev);
209 	struct i40e_pf *pf = np->vsi->back;
210 	struct i40e_hw *hw = &pf->hw;
211 	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
212 	bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
213 	u32 link_speed = hw_link_info->link_speed;
214 
215 	/* hardware is either in 40G mode or 10G mode
216 	 * NOTE: this section initializes supported and advertising
217 	 */
218 	switch (hw_link_info->phy_type) {
219 	case I40E_PHY_TYPE_40GBASE_CR4:
220 	case I40E_PHY_TYPE_40GBASE_CR4_CU:
221 		ecmd->supported = SUPPORTED_40000baseCR4_Full;
222 		ecmd->advertising = ADVERTISED_40000baseCR4_Full;
223 		break;
224 	case I40E_PHY_TYPE_40GBASE_KR4:
225 		ecmd->supported = SUPPORTED_40000baseKR4_Full;
226 		ecmd->advertising = ADVERTISED_40000baseKR4_Full;
227 		break;
228 	case I40E_PHY_TYPE_40GBASE_SR4:
229 		ecmd->supported = SUPPORTED_40000baseSR4_Full;
230 		ecmd->advertising = ADVERTISED_40000baseSR4_Full;
231 		break;
232 	case I40E_PHY_TYPE_40GBASE_LR4:
233 		ecmd->supported = SUPPORTED_40000baseLR4_Full;
234 		ecmd->advertising = ADVERTISED_40000baseLR4_Full;
235 		break;
236 	case I40E_PHY_TYPE_10GBASE_KX4:
237 		ecmd->supported = SUPPORTED_10000baseKX4_Full;
238 		ecmd->advertising = ADVERTISED_10000baseKX4_Full;
239 		break;
240 	case I40E_PHY_TYPE_10GBASE_KR:
241 		ecmd->supported = SUPPORTED_10000baseKR_Full;
242 		ecmd->advertising = ADVERTISED_10000baseKR_Full;
243 		break;
244 	default:
245 		if (i40e_is_40G_device(hw->device_id)) {
246 			ecmd->supported = SUPPORTED_40000baseSR4_Full;
247 			ecmd->advertising = ADVERTISED_40000baseSR4_Full;
248 		} else {
249 			ecmd->supported = SUPPORTED_10000baseT_Full;
250 			ecmd->advertising = ADVERTISED_10000baseT_Full;
251 		}
252 		break;
253 	}
254 
255 	ecmd->supported |= SUPPORTED_Autoneg;
256 	ecmd->advertising |= ADVERTISED_Autoneg;
257 	ecmd->autoneg = ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
258 			  AUTONEG_ENABLE : AUTONEG_DISABLE);
259 
260 	switch (hw->phy.media_type) {
261 	case I40E_MEDIA_TYPE_BACKPLANE:
262 		ecmd->supported |= SUPPORTED_Backplane;
263 		ecmd->advertising |= ADVERTISED_Backplane;
264 		ecmd->port = PORT_NONE;
265 		break;
266 	case I40E_MEDIA_TYPE_BASET:
267 		ecmd->supported |= SUPPORTED_TP;
268 		ecmd->advertising |= ADVERTISED_TP;
269 		ecmd->port = PORT_TP;
270 		break;
271 	case I40E_MEDIA_TYPE_DA:
272 	case I40E_MEDIA_TYPE_CX4:
273 		ecmd->supported |= SUPPORTED_FIBRE;
274 		ecmd->advertising |= ADVERTISED_FIBRE;
275 		ecmd->port = PORT_DA;
276 		break;
277 	case I40E_MEDIA_TYPE_FIBER:
278 		ecmd->supported |= SUPPORTED_FIBRE;
279 		ecmd->advertising |= ADVERTISED_FIBRE;
280 		ecmd->port = PORT_FIBRE;
281 		break;
282 	case I40E_MEDIA_TYPE_UNKNOWN:
283 	default:
284 		ecmd->port = PORT_OTHER;
285 		break;
286 	}
287 
288 	ecmd->transceiver = XCVR_EXTERNAL;
289 
290 	if (link_up) {
291 		switch (link_speed) {
292 		case I40E_LINK_SPEED_40GB:
293 			/* need a SPEED_40000 in ethtool.h */
294 			ethtool_cmd_speed_set(ecmd, 40000);
295 			break;
296 		case I40E_LINK_SPEED_10GB:
297 			ethtool_cmd_speed_set(ecmd, SPEED_10000);
298 			break;
299 		default:
300 			break;
301 		}
302 		ecmd->duplex = DUPLEX_FULL;
303 	} else {
304 		ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
305 		ecmd->duplex = DUPLEX_UNKNOWN;
306 	}
307 
308 	return 0;
309 }
310 
311 /**
312  * i40e_get_pauseparam -  Get Flow Control status
313  * Return tx/rx-pause status
314  **/
315 static void i40e_get_pauseparam(struct net_device *netdev,
316 				struct ethtool_pauseparam *pause)
317 {
318 	struct i40e_netdev_priv *np = netdev_priv(netdev);
319 	struct i40e_pf *pf = np->vsi->back;
320 	struct i40e_hw *hw = &pf->hw;
321 	struct i40e_link_status *hw_link_info = &hw->phy.link_info;
322 
323 	pause->autoneg =
324 		((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
325 		  AUTONEG_ENABLE : AUTONEG_DISABLE);
326 
327 	if (hw->fc.current_mode == I40E_FC_RX_PAUSE) {
328 		pause->rx_pause = 1;
329 	} else if (hw->fc.current_mode == I40E_FC_TX_PAUSE) {
330 		pause->tx_pause = 1;
331 	} else if (hw->fc.current_mode == I40E_FC_FULL) {
332 		pause->rx_pause = 1;
333 		pause->tx_pause = 1;
334 	}
335 }
336 
337 static u32 i40e_get_msglevel(struct net_device *netdev)
338 {
339 	struct i40e_netdev_priv *np = netdev_priv(netdev);
340 	struct i40e_pf *pf = np->vsi->back;
341 
342 	return pf->msg_enable;
343 }
344 
345 static void i40e_set_msglevel(struct net_device *netdev, u32 data)
346 {
347 	struct i40e_netdev_priv *np = netdev_priv(netdev);
348 	struct i40e_pf *pf = np->vsi->back;
349 
350 	if (I40E_DEBUG_USER & data)
351 		pf->hw.debug_mask = data;
352 	pf->msg_enable = data;
353 }
354 
355 static int i40e_get_regs_len(struct net_device *netdev)
356 {
357 	int reg_count = 0;
358 	int i;
359 
360 	for (i = 0; i40e_reg_list[i].offset != 0; i++)
361 		reg_count += i40e_reg_list[i].elements;
362 
363 	return reg_count * sizeof(u32);
364 }
365 
366 static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
367 			  void *p)
368 {
369 	struct i40e_netdev_priv *np = netdev_priv(netdev);
370 	struct i40e_pf *pf = np->vsi->back;
371 	struct i40e_hw *hw = &pf->hw;
372 	u32 *reg_buf = p;
373 	int i, j, ri;
374 	u32 reg;
375 
376 	/* Tell ethtool which driver-version-specific regs output we have.
377 	 *
378 	 * At some point, if we have ethtool doing special formatting of
379 	 * this data, it will rely on this version number to know how to
380 	 * interpret things.  Hence, this needs to be updated if/when the
381 	 * diags register table is changed.
382 	 */
383 	regs->version = 1;
384 
385 	/* loop through the diags reg table for what to print */
386 	ri = 0;
387 	for (i = 0; i40e_reg_list[i].offset != 0; i++) {
388 		for (j = 0; j < i40e_reg_list[i].elements; j++) {
389 			reg = i40e_reg_list[i].offset
390 				+ (j * i40e_reg_list[i].stride);
391 			reg_buf[ri++] = rd32(hw, reg);
392 		}
393 	}
394 
395 }
396 
397 static int i40e_get_eeprom(struct net_device *netdev,
398 			   struct ethtool_eeprom *eeprom, u8 *bytes)
399 {
400 	struct i40e_netdev_priv *np = netdev_priv(netdev);
401 	struct i40e_hw *hw = &np->vsi->back->hw;
402 	struct i40e_pf *pf = np->vsi->back;
403 	int ret_val = 0, len;
404 	u8 *eeprom_buff;
405 	u16 i, sectors;
406 	bool last;
407 #define I40E_NVM_SECTOR_SIZE  4096
408 	if (eeprom->len == 0)
409 		return -EINVAL;
410 
411 	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
412 
413 	eeprom_buff = kzalloc(eeprom->len, GFP_KERNEL);
414 	if (!eeprom_buff)
415 		return -ENOMEM;
416 
417 	ret_val = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
418 	if (ret_val) {
419 		dev_info(&pf->pdev->dev,
420 			 "Failed Acquiring NVM resource for read err=%d status=0x%x\n",
421 			 ret_val, hw->aq.asq_last_status);
422 		goto free_buff;
423 	}
424 
425 	sectors = eeprom->len / I40E_NVM_SECTOR_SIZE;
426 	sectors += (eeprom->len % I40E_NVM_SECTOR_SIZE) ? 1 : 0;
427 	len = I40E_NVM_SECTOR_SIZE;
428 	last = false;
429 	for (i = 0; i < sectors; i++) {
430 		if (i == (sectors - 1)) {
431 			len = eeprom->len - (I40E_NVM_SECTOR_SIZE * i);
432 			last = true;
433 		}
434 		ret_val = i40e_aq_read_nvm(hw, 0x0,
435 				eeprom->offset + (I40E_NVM_SECTOR_SIZE * i),
436 				len,
437 				eeprom_buff + (I40E_NVM_SECTOR_SIZE * i),
438 				last, NULL);
439 		if (ret_val) {
440 			dev_info(&pf->pdev->dev,
441 				 "read NVM failed err=%d status=0x%x\n",
442 				 ret_val, hw->aq.asq_last_status);
443 			goto release_nvm;
444 		}
445 	}
446 
447 release_nvm:
448 	i40e_release_nvm(hw);
449 	memcpy(bytes, eeprom_buff, eeprom->len);
450 free_buff:
451 	kfree(eeprom_buff);
452 	return ret_val;
453 }
454 
455 static int i40e_get_eeprom_len(struct net_device *netdev)
456 {
457 	struct i40e_netdev_priv *np = netdev_priv(netdev);
458 	struct i40e_hw *hw = &np->vsi->back->hw;
459 	u32 val;
460 
461 	val = (rd32(hw, I40E_GLPCI_LBARCTRL)
462 		& I40E_GLPCI_LBARCTRL_FL_SIZE_MASK)
463 		>> I40E_GLPCI_LBARCTRL_FL_SIZE_SHIFT;
464 	/* register returns value in power of 2, 64Kbyte chunks. */
465 	val = (64 * 1024) * (1 << val);
466 	return val;
467 }
468 
469 static void i40e_get_drvinfo(struct net_device *netdev,
470 			     struct ethtool_drvinfo *drvinfo)
471 {
472 	struct i40e_netdev_priv *np = netdev_priv(netdev);
473 	struct i40e_vsi *vsi = np->vsi;
474 	struct i40e_pf *pf = vsi->back;
475 
476 	strlcpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
477 	strlcpy(drvinfo->version, i40e_driver_version_str,
478 		sizeof(drvinfo->version));
479 	strlcpy(drvinfo->fw_version, i40e_fw_version_str(&pf->hw),
480 		sizeof(drvinfo->fw_version));
481 	strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
482 		sizeof(drvinfo->bus_info));
483 }
484 
485 static void i40e_get_ringparam(struct net_device *netdev,
486 			       struct ethtool_ringparam *ring)
487 {
488 	struct i40e_netdev_priv *np = netdev_priv(netdev);
489 	struct i40e_pf *pf = np->vsi->back;
490 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
491 
492 	ring->rx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
493 	ring->tx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
494 	ring->rx_mini_max_pending = 0;
495 	ring->rx_jumbo_max_pending = 0;
496 	ring->rx_pending = vsi->rx_rings[0]->count;
497 	ring->tx_pending = vsi->tx_rings[0]->count;
498 	ring->rx_mini_pending = 0;
499 	ring->rx_jumbo_pending = 0;
500 }
501 
502 static int i40e_set_ringparam(struct net_device *netdev,
503 			      struct ethtool_ringparam *ring)
504 {
505 	struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
506 	struct i40e_netdev_priv *np = netdev_priv(netdev);
507 	struct i40e_vsi *vsi = np->vsi;
508 	struct i40e_pf *pf = vsi->back;
509 	u32 new_rx_count, new_tx_count;
510 	int i, err = 0;
511 
512 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
513 		return -EINVAL;
514 
515 	if (ring->tx_pending > I40E_MAX_NUM_DESCRIPTORS ||
516 	    ring->tx_pending < I40E_MIN_NUM_DESCRIPTORS ||
517 	    ring->rx_pending > I40E_MAX_NUM_DESCRIPTORS ||
518 	    ring->rx_pending < I40E_MIN_NUM_DESCRIPTORS) {
519 		netdev_info(netdev,
520 			    "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d]\n",
521 			    ring->tx_pending, ring->rx_pending,
522 			    I40E_MIN_NUM_DESCRIPTORS, I40E_MAX_NUM_DESCRIPTORS);
523 		return -EINVAL;
524 	}
525 
526 	new_tx_count = ALIGN(ring->tx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
527 	new_rx_count = ALIGN(ring->rx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
528 
529 	/* if nothing to do return success */
530 	if ((new_tx_count == vsi->tx_rings[0]->count) &&
531 	    (new_rx_count == vsi->rx_rings[0]->count))
532 		return 0;
533 
534 	while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
535 		usleep_range(1000, 2000);
536 
537 	if (!netif_running(vsi->netdev)) {
538 		/* simple case - set for the next time the netdev is started */
539 		for (i = 0; i < vsi->num_queue_pairs; i++) {
540 			vsi->tx_rings[i]->count = new_tx_count;
541 			vsi->rx_rings[i]->count = new_rx_count;
542 		}
543 		goto done;
544 	}
545 
546 	/* We can't just free everything and then setup again,
547 	 * because the ISRs in MSI-X mode get passed pointers
548 	 * to the Tx and Rx ring structs.
549 	 */
550 
551 	/* alloc updated Tx resources */
552 	if (new_tx_count != vsi->tx_rings[0]->count) {
553 		netdev_info(netdev,
554 			    "Changing Tx descriptor count from %d to %d.\n",
555 			    vsi->tx_rings[0]->count, new_tx_count);
556 		tx_rings = kcalloc(vsi->alloc_queue_pairs,
557 				   sizeof(struct i40e_ring), GFP_KERNEL);
558 		if (!tx_rings) {
559 			err = -ENOMEM;
560 			goto done;
561 		}
562 
563 		for (i = 0; i < vsi->num_queue_pairs; i++) {
564 			/* clone ring and setup updated count */
565 			tx_rings[i] = *vsi->tx_rings[i];
566 			tx_rings[i].count = new_tx_count;
567 			err = i40e_setup_tx_descriptors(&tx_rings[i]);
568 			if (err) {
569 				while (i) {
570 					i--;
571 					i40e_free_tx_resources(&tx_rings[i]);
572 				}
573 				kfree(tx_rings);
574 				tx_rings = NULL;
575 
576 				goto done;
577 			}
578 		}
579 	}
580 
581 	/* alloc updated Rx resources */
582 	if (new_rx_count != vsi->rx_rings[0]->count) {
583 		netdev_info(netdev,
584 			    "Changing Rx descriptor count from %d to %d\n",
585 			    vsi->rx_rings[0]->count, new_rx_count);
586 		rx_rings = kcalloc(vsi->alloc_queue_pairs,
587 				   sizeof(struct i40e_ring), GFP_KERNEL);
588 		if (!rx_rings) {
589 			err = -ENOMEM;
590 			goto free_tx;
591 		}
592 
593 		for (i = 0; i < vsi->num_queue_pairs; i++) {
594 			/* clone ring and setup updated count */
595 			rx_rings[i] = *vsi->rx_rings[i];
596 			rx_rings[i].count = new_rx_count;
597 			err = i40e_setup_rx_descriptors(&rx_rings[i]);
598 			if (err) {
599 				while (i) {
600 					i--;
601 					i40e_free_rx_resources(&rx_rings[i]);
602 				}
603 				kfree(rx_rings);
604 				rx_rings = NULL;
605 
606 				goto free_tx;
607 			}
608 		}
609 	}
610 
611 	/* Bring interface down, copy in the new ring info,
612 	 * then restore the interface
613 	 */
614 	i40e_down(vsi);
615 
616 	if (tx_rings) {
617 		for (i = 0; i < vsi->num_queue_pairs; i++) {
618 			i40e_free_tx_resources(vsi->tx_rings[i]);
619 			*vsi->tx_rings[i] = tx_rings[i];
620 		}
621 		kfree(tx_rings);
622 		tx_rings = NULL;
623 	}
624 
625 	if (rx_rings) {
626 		for (i = 0; i < vsi->num_queue_pairs; i++) {
627 			i40e_free_rx_resources(vsi->rx_rings[i]);
628 			*vsi->rx_rings[i] = rx_rings[i];
629 		}
630 		kfree(rx_rings);
631 		rx_rings = NULL;
632 	}
633 
634 	i40e_up(vsi);
635 
636 free_tx:
637 	/* error cleanup if the Rx allocations failed after getting Tx */
638 	if (tx_rings) {
639 		for (i = 0; i < vsi->num_queue_pairs; i++)
640 			i40e_free_tx_resources(&tx_rings[i]);
641 		kfree(tx_rings);
642 		tx_rings = NULL;
643 	}
644 
645 done:
646 	clear_bit(__I40E_CONFIG_BUSY, &pf->state);
647 
648 	return err;
649 }
650 
651 static int i40e_get_sset_count(struct net_device *netdev, int sset)
652 {
653 	struct i40e_netdev_priv *np = netdev_priv(netdev);
654 	struct i40e_vsi *vsi = np->vsi;
655 	struct i40e_pf *pf = vsi->back;
656 
657 	switch (sset) {
658 	case ETH_SS_TEST:
659 		return I40E_TEST_LEN;
660 	case ETH_SS_STATS:
661 		if (vsi == pf->vsi[pf->lan_vsi]) {
662 			int len = I40E_PF_STATS_LEN(netdev);
663 
664 			if (pf->lan_veb != I40E_NO_VEB)
665 				len += I40E_VEB_STATS_LEN;
666 			return len;
667 		} else {
668 			return I40E_VSI_STATS_LEN(netdev);
669 		}
670 	default:
671 		return -EOPNOTSUPP;
672 	}
673 }
674 
675 static void i40e_get_ethtool_stats(struct net_device *netdev,
676 				   struct ethtool_stats *stats, u64 *data)
677 {
678 	struct i40e_netdev_priv *np = netdev_priv(netdev);
679 	struct i40e_ring *tx_ring, *rx_ring;
680 	struct i40e_vsi *vsi = np->vsi;
681 	struct i40e_pf *pf = vsi->back;
682 	int i = 0;
683 	char *p;
684 	int j;
685 	struct rtnl_link_stats64 *net_stats = i40e_get_vsi_stats_struct(vsi);
686 	unsigned int start;
687 
688 	i40e_update_stats(vsi);
689 
690 	for (j = 0; j < I40E_NETDEV_STATS_LEN; j++) {
691 		p = (char *)net_stats + i40e_gstrings_net_stats[j].stat_offset;
692 		data[i++] = (i40e_gstrings_net_stats[j].sizeof_stat ==
693 			sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
694 	}
695 	for (j = 0; j < I40E_MISC_STATS_LEN; j++) {
696 		p = (char *)vsi + i40e_gstrings_misc_stats[j].stat_offset;
697 		data[i++] = (i40e_gstrings_misc_stats[j].sizeof_stat ==
698 			    sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
699 	}
700 	rcu_read_lock();
701 	for (j = 0; j < vsi->num_queue_pairs; j++) {
702 		tx_ring = ACCESS_ONCE(vsi->tx_rings[j]);
703 
704 		if (!tx_ring)
705 			continue;
706 
707 		/* process Tx ring statistics */
708 		do {
709 			start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
710 			data[i] = tx_ring->stats.packets;
711 			data[i + 1] = tx_ring->stats.bytes;
712 		} while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
713 		i += 2;
714 
715 		/* Rx ring is the 2nd half of the queue pair */
716 		rx_ring = &tx_ring[1];
717 		do {
718 			start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
719 			data[i] = rx_ring->stats.packets;
720 			data[i + 1] = rx_ring->stats.bytes;
721 		} while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
722 		i += 2;
723 	}
724 	rcu_read_unlock();
725 	if (vsi != pf->vsi[pf->lan_vsi])
726 		return;
727 
728 	if (pf->lan_veb != I40E_NO_VEB) {
729 		struct i40e_veb *veb = pf->veb[pf->lan_veb];
730 		for (j = 0; j < I40E_VEB_STATS_LEN; j++) {
731 			p = (char *)veb;
732 			p += i40e_gstrings_veb_stats[j].stat_offset;
733 			data[i++] = (i40e_gstrings_veb_stats[j].sizeof_stat ==
734 				     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
735 		}
736 	}
737 	for (j = 0; j < I40E_GLOBAL_STATS_LEN; j++) {
738 		p = (char *)pf + i40e_gstrings_stats[j].stat_offset;
739 		data[i++] = (i40e_gstrings_stats[j].sizeof_stat ==
740 			     sizeof(u64)) ? *(u64 *)p : *(u32 *)p;
741 	}
742 	for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
743 		data[i++] = pf->stats.priority_xon_tx[j];
744 		data[i++] = pf->stats.priority_xoff_tx[j];
745 	}
746 	for (j = 0; j < I40E_MAX_USER_PRIORITY; j++) {
747 		data[i++] = pf->stats.priority_xon_rx[j];
748 		data[i++] = pf->stats.priority_xoff_rx[j];
749 	}
750 	for (j = 0; j < I40E_MAX_USER_PRIORITY; j++)
751 		data[i++] = pf->stats.priority_xon_2_xoff[j];
752 }
753 
754 static void i40e_get_strings(struct net_device *netdev, u32 stringset,
755 			     u8 *data)
756 {
757 	struct i40e_netdev_priv *np = netdev_priv(netdev);
758 	struct i40e_vsi *vsi = np->vsi;
759 	struct i40e_pf *pf = vsi->back;
760 	char *p = (char *)data;
761 	int i;
762 
763 	switch (stringset) {
764 	case ETH_SS_TEST:
765 		for (i = 0; i < I40E_TEST_LEN; i++) {
766 			memcpy(data, i40e_gstrings_test[i], ETH_GSTRING_LEN);
767 			data += ETH_GSTRING_LEN;
768 		}
769 		break;
770 	case ETH_SS_STATS:
771 		for (i = 0; i < I40E_NETDEV_STATS_LEN; i++) {
772 			snprintf(p, ETH_GSTRING_LEN, "%s",
773 				 i40e_gstrings_net_stats[i].stat_string);
774 			p += ETH_GSTRING_LEN;
775 		}
776 		for (i = 0; i < I40E_MISC_STATS_LEN; i++) {
777 			snprintf(p, ETH_GSTRING_LEN, "%s",
778 				 i40e_gstrings_misc_stats[i].stat_string);
779 			p += ETH_GSTRING_LEN;
780 		}
781 		for (i = 0; i < vsi->num_queue_pairs; i++) {
782 			snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_packets", i);
783 			p += ETH_GSTRING_LEN;
784 			snprintf(p, ETH_GSTRING_LEN, "tx-%u.tx_bytes", i);
785 			p += ETH_GSTRING_LEN;
786 			snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_packets", i);
787 			p += ETH_GSTRING_LEN;
788 			snprintf(p, ETH_GSTRING_LEN, "rx-%u.rx_bytes", i);
789 			p += ETH_GSTRING_LEN;
790 		}
791 		if (vsi != pf->vsi[pf->lan_vsi])
792 			return;
793 
794 		if (pf->lan_veb != I40E_NO_VEB) {
795 			for (i = 0; i < I40E_VEB_STATS_LEN; i++) {
796 				snprintf(p, ETH_GSTRING_LEN, "veb.%s",
797 					i40e_gstrings_veb_stats[i].stat_string);
798 				p += ETH_GSTRING_LEN;
799 			}
800 		}
801 		for (i = 0; i < I40E_GLOBAL_STATS_LEN; i++) {
802 			snprintf(p, ETH_GSTRING_LEN, "port.%s",
803 				 i40e_gstrings_stats[i].stat_string);
804 			p += ETH_GSTRING_LEN;
805 		}
806 		for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
807 			snprintf(p, ETH_GSTRING_LEN,
808 				 "port.tx_priority_%u_xon", i);
809 			p += ETH_GSTRING_LEN;
810 			snprintf(p, ETH_GSTRING_LEN,
811 				 "port.tx_priority_%u_xoff", i);
812 			p += ETH_GSTRING_LEN;
813 		}
814 		for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
815 			snprintf(p, ETH_GSTRING_LEN,
816 				 "port.rx_priority_%u_xon", i);
817 			p += ETH_GSTRING_LEN;
818 			snprintf(p, ETH_GSTRING_LEN,
819 				 "port.rx_priority_%u_xoff", i);
820 			p += ETH_GSTRING_LEN;
821 		}
822 		for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
823 			snprintf(p, ETH_GSTRING_LEN,
824 				 "port.rx_priority_%u_xon_2_xoff", i);
825 			p += ETH_GSTRING_LEN;
826 		}
827 		/* BUG_ON(p - data != I40E_STATS_LEN * ETH_GSTRING_LEN); */
828 		break;
829 	}
830 }
831 
832 static int i40e_get_ts_info(struct net_device *dev,
833 			    struct ethtool_ts_info *info)
834 {
835 	struct i40e_pf *pf = i40e_netdev_to_pf(dev);
836 
837 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
838 				SOF_TIMESTAMPING_RX_SOFTWARE |
839 				SOF_TIMESTAMPING_SOFTWARE |
840 				SOF_TIMESTAMPING_TX_HARDWARE |
841 				SOF_TIMESTAMPING_RX_HARDWARE |
842 				SOF_TIMESTAMPING_RAW_HARDWARE;
843 
844 	if (pf->ptp_clock)
845 		info->phc_index = ptp_clock_index(pf->ptp_clock);
846 	else
847 		info->phc_index = -1;
848 
849 	info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
850 
851 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
852 			   (1 << HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
853 			   (1 << HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
854 			   (1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
855 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
856 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
857 			   (1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
858 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
859 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
860 			   (1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
861 			   (1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
862 			   (1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
863 
864 	return 0;
865 }
866 
867 static int i40e_link_test(struct net_device *netdev, u64 *data)
868 {
869 	struct i40e_netdev_priv *np = netdev_priv(netdev);
870 	struct i40e_pf *pf = np->vsi->back;
871 
872 	netif_info(pf, hw, netdev, "link test\n");
873 	if (i40e_get_link_status(&pf->hw))
874 		*data = 0;
875 	else
876 		*data = 1;
877 
878 	return *data;
879 }
880 
881 static int i40e_reg_test(struct net_device *netdev, u64 *data)
882 {
883 	struct i40e_netdev_priv *np = netdev_priv(netdev);
884 	struct i40e_pf *pf = np->vsi->back;
885 
886 	netif_info(pf, hw, netdev, "register test\n");
887 	*data = i40e_diag_reg_test(&pf->hw);
888 
889 	return *data;
890 }
891 
892 static int i40e_eeprom_test(struct net_device *netdev, u64 *data)
893 {
894 	struct i40e_netdev_priv *np = netdev_priv(netdev);
895 	struct i40e_pf *pf = np->vsi->back;
896 
897 	netif_info(pf, hw, netdev, "eeprom test\n");
898 	*data = i40e_diag_eeprom_test(&pf->hw);
899 
900 	return *data;
901 }
902 
903 static int i40e_intr_test(struct net_device *netdev, u64 *data)
904 {
905 	struct i40e_netdev_priv *np = netdev_priv(netdev);
906 	struct i40e_pf *pf = np->vsi->back;
907 	u16 swc_old = pf->sw_int_count;
908 
909 	netif_info(pf, hw, netdev, "interrupt test\n");
910 	wr32(&pf->hw, I40E_PFINT_DYN_CTL0,
911 	     (I40E_PFINT_DYN_CTL0_INTENA_MASK |
912 	      I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
913 	usleep_range(1000, 2000);
914 	*data = (swc_old == pf->sw_int_count);
915 
916 	return *data;
917 }
918 
919 static int i40e_loopback_test(struct net_device *netdev, u64 *data)
920 {
921 	struct i40e_netdev_priv *np = netdev_priv(netdev);
922 	struct i40e_pf *pf = np->vsi->back;
923 
924 	netif_info(pf, hw, netdev, "loopback test not implemented\n");
925 	*data = 0;
926 
927 	return *data;
928 }
929 
930 static void i40e_diag_test(struct net_device *netdev,
931 			   struct ethtool_test *eth_test, u64 *data)
932 {
933 	struct i40e_netdev_priv *np = netdev_priv(netdev);
934 	struct i40e_pf *pf = np->vsi->back;
935 
936 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
937 		/* Offline tests */
938 		netif_info(pf, drv, netdev, "offline testing starting\n");
939 
940 		set_bit(__I40E_TESTING, &pf->state);
941 
942 		/* Link test performed before hardware reset
943 		 * so autoneg doesn't interfere with test result
944 		 */
945 		if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
946 			eth_test->flags |= ETH_TEST_FL_FAILED;
947 
948 		if (i40e_eeprom_test(netdev, &data[I40E_ETH_TEST_EEPROM]))
949 			eth_test->flags |= ETH_TEST_FL_FAILED;
950 
951 		if (i40e_intr_test(netdev, &data[I40E_ETH_TEST_INTR]))
952 			eth_test->flags |= ETH_TEST_FL_FAILED;
953 
954 		if (i40e_loopback_test(netdev, &data[I40E_ETH_TEST_LOOPBACK]))
955 			eth_test->flags |= ETH_TEST_FL_FAILED;
956 
957 		/* run reg test last, a reset is required after it */
958 		if (i40e_reg_test(netdev, &data[I40E_ETH_TEST_REG]))
959 			eth_test->flags |= ETH_TEST_FL_FAILED;
960 
961 		clear_bit(__I40E_TESTING, &pf->state);
962 		i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
963 	} else {
964 		/* Online tests */
965 		netif_info(pf, drv, netdev, "online testing starting\n");
966 
967 		if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
968 			eth_test->flags |= ETH_TEST_FL_FAILED;
969 
970 		/* Offline only tests, not run in online; pass by default */
971 		data[I40E_ETH_TEST_REG] = 0;
972 		data[I40E_ETH_TEST_EEPROM] = 0;
973 		data[I40E_ETH_TEST_INTR] = 0;
974 		data[I40E_ETH_TEST_LOOPBACK] = 0;
975 	}
976 
977 	netif_info(pf, drv, netdev, "testing finished\n");
978 }
979 
980 static void i40e_get_wol(struct net_device *netdev,
981 			 struct ethtool_wolinfo *wol)
982 {
983 	struct i40e_netdev_priv *np = netdev_priv(netdev);
984 	struct i40e_pf *pf = np->vsi->back;
985 	struct i40e_hw *hw = &pf->hw;
986 	u16 wol_nvm_bits;
987 
988 	/* NVM bit on means WoL disabled for the port */
989 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
990 	if ((1 << hw->port) & wol_nvm_bits) {
991 		wol->supported = 0;
992 		wol->wolopts = 0;
993 	} else {
994 		wol->supported = WAKE_MAGIC;
995 		wol->wolopts = (pf->wol_en ? WAKE_MAGIC : 0);
996 	}
997 }
998 
999 static int i40e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
1000 {
1001 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1002 	struct i40e_pf *pf = np->vsi->back;
1003 	struct i40e_hw *hw = &pf->hw;
1004 	u16 wol_nvm_bits;
1005 
1006 	/* NVM bit on means WoL disabled for the port */
1007 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
1008 	if (((1 << hw->port) & wol_nvm_bits))
1009 		return -EOPNOTSUPP;
1010 
1011 	/* only magic packet is supported */
1012 	if (wol->wolopts && (wol->wolopts != WAKE_MAGIC))
1013 		return -EOPNOTSUPP;
1014 
1015 	/* is this a new value? */
1016 	if (pf->wol_en != !!wol->wolopts) {
1017 		pf->wol_en = !!wol->wolopts;
1018 		device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
1019 	}
1020 
1021 	return 0;
1022 }
1023 
1024 static int i40e_nway_reset(struct net_device *netdev)
1025 {
1026 	/* restart autonegotiation */
1027 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1028 	struct i40e_pf *pf = np->vsi->back;
1029 	struct i40e_hw *hw = &pf->hw;
1030 	i40e_status ret = 0;
1031 
1032 	ret = i40e_aq_set_link_restart_an(hw, NULL);
1033 	if (ret) {
1034 		netdev_info(netdev, "link restart failed, aq_err=%d\n",
1035 			    pf->hw.aq.asq_last_status);
1036 		return -EIO;
1037 	}
1038 
1039 	return 0;
1040 }
1041 
1042 static int i40e_set_phys_id(struct net_device *netdev,
1043 			    enum ethtool_phys_id_state state)
1044 {
1045 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1046 	struct i40e_pf *pf = np->vsi->back;
1047 	struct i40e_hw *hw = &pf->hw;
1048 	int blink_freq = 2;
1049 
1050 	switch (state) {
1051 	case ETHTOOL_ID_ACTIVE:
1052 		pf->led_status = i40e_led_get(hw);
1053 		return blink_freq;
1054 	case ETHTOOL_ID_ON:
1055 		i40e_led_set(hw, 0xF, false);
1056 		break;
1057 	case ETHTOOL_ID_OFF:
1058 		i40e_led_set(hw, 0x0, false);
1059 		break;
1060 	case ETHTOOL_ID_INACTIVE:
1061 		i40e_led_set(hw, pf->led_status, false);
1062 		break;
1063 	}
1064 
1065 	return 0;
1066 }
1067 
1068 /* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
1069  * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
1070  * 125us (8000 interrupts per second) == ITR(62)
1071  */
1072 
1073 static int i40e_get_coalesce(struct net_device *netdev,
1074 			     struct ethtool_coalesce *ec)
1075 {
1076 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1077 	struct i40e_vsi *vsi = np->vsi;
1078 
1079 	ec->tx_max_coalesced_frames_irq = vsi->work_limit;
1080 	ec->rx_max_coalesced_frames_irq = vsi->work_limit;
1081 
1082 	if (ITR_IS_DYNAMIC(vsi->rx_itr_setting))
1083 		ec->use_adaptive_rx_coalesce = 1;
1084 
1085 	if (ITR_IS_DYNAMIC(vsi->tx_itr_setting))
1086 		ec->use_adaptive_tx_coalesce = 1;
1087 
1088 	ec->rx_coalesce_usecs = vsi->rx_itr_setting & ~I40E_ITR_DYNAMIC;
1089 	ec->tx_coalesce_usecs = vsi->tx_itr_setting & ~I40E_ITR_DYNAMIC;
1090 
1091 	return 0;
1092 }
1093 
1094 static int i40e_set_coalesce(struct net_device *netdev,
1095 			     struct ethtool_coalesce *ec)
1096 {
1097 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1098 	struct i40e_q_vector *q_vector;
1099 	struct i40e_vsi *vsi = np->vsi;
1100 	struct i40e_pf *pf = vsi->back;
1101 	struct i40e_hw *hw = &pf->hw;
1102 	u16 vector;
1103 	int i;
1104 
1105 	if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
1106 		vsi->work_limit = ec->tx_max_coalesced_frames_irq;
1107 
1108 	if ((ec->rx_coalesce_usecs >= (I40E_MIN_ITR << 1)) &&
1109 	    (ec->rx_coalesce_usecs <= (I40E_MAX_ITR << 1)))
1110 		vsi->rx_itr_setting = ec->rx_coalesce_usecs;
1111 	else
1112 		return -EINVAL;
1113 
1114 	if ((ec->tx_coalesce_usecs >= (I40E_MIN_ITR << 1)) &&
1115 	    (ec->tx_coalesce_usecs <= (I40E_MAX_ITR << 1)))
1116 		vsi->tx_itr_setting = ec->tx_coalesce_usecs;
1117 	else
1118 		return -EINVAL;
1119 
1120 	if (ec->use_adaptive_rx_coalesce)
1121 		vsi->rx_itr_setting |= I40E_ITR_DYNAMIC;
1122 	else
1123 		vsi->rx_itr_setting &= ~I40E_ITR_DYNAMIC;
1124 
1125 	if (ec->use_adaptive_tx_coalesce)
1126 		vsi->tx_itr_setting |= I40E_ITR_DYNAMIC;
1127 	else
1128 		vsi->tx_itr_setting &= ~I40E_ITR_DYNAMIC;
1129 
1130 	vector = vsi->base_vector;
1131 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
1132 		q_vector = vsi->q_vectors[i];
1133 		q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
1134 		wr32(hw, I40E_PFINT_ITRN(0, vector - 1), q_vector->rx.itr);
1135 		q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
1136 		wr32(hw, I40E_PFINT_ITRN(1, vector - 1), q_vector->tx.itr);
1137 		i40e_flush(hw);
1138 	}
1139 
1140 	return 0;
1141 }
1142 
1143 /**
1144  * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
1145  * @pf: pointer to the physical function struct
1146  * @cmd: ethtool rxnfc command
1147  *
1148  * Returns Success if the flow is supported, else Invalid Input.
1149  **/
1150 static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
1151 {
1152 	cmd->data = 0;
1153 
1154 	/* Report default options for RSS on i40e */
1155 	switch (cmd->flow_type) {
1156 	case TCP_V4_FLOW:
1157 	case UDP_V4_FLOW:
1158 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1159 	/* fall through to add IP fields */
1160 	case SCTP_V4_FLOW:
1161 	case AH_ESP_V4_FLOW:
1162 	case AH_V4_FLOW:
1163 	case ESP_V4_FLOW:
1164 	case IPV4_FLOW:
1165 		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1166 		break;
1167 	case TCP_V6_FLOW:
1168 	case UDP_V6_FLOW:
1169 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1170 	/* fall through to add IP fields */
1171 	case SCTP_V6_FLOW:
1172 	case AH_ESP_V6_FLOW:
1173 	case AH_V6_FLOW:
1174 	case ESP_V6_FLOW:
1175 	case IPV6_FLOW:
1176 		cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1177 		break;
1178 	default:
1179 		return -EINVAL;
1180 	}
1181 
1182 	return 0;
1183 }
1184 
1185 /**
1186  * i40e_get_ethtool_fdir_all - Populates the rule count of a command
1187  * @pf: Pointer to the physical function struct
1188  * @cmd: The command to get or set Rx flow classification rules
1189  * @rule_locs: Array of used rule locations
1190  *
1191  * This function populates both the total and actual rule count of
1192  * the ethtool flow classification command
1193  *
1194  * Returns 0 on success or -EMSGSIZE if entry not found
1195  **/
1196 static int i40e_get_ethtool_fdir_all(struct i40e_pf *pf,
1197 				     struct ethtool_rxnfc *cmd,
1198 				     u32 *rule_locs)
1199 {
1200 	struct i40e_fdir_filter *rule;
1201 	struct hlist_node *node2;
1202 	int cnt = 0;
1203 
1204 	/* report total rule count */
1205 	cmd->data = i40e_get_fd_cnt_all(pf);
1206 
1207 	hlist_for_each_entry_safe(rule, node2,
1208 				  &pf->fdir_filter_list, fdir_node) {
1209 		if (cnt == cmd->rule_cnt)
1210 			return -EMSGSIZE;
1211 
1212 		rule_locs[cnt] = rule->fd_id;
1213 		cnt++;
1214 	}
1215 
1216 	cmd->rule_cnt = cnt;
1217 
1218 	return 0;
1219 }
1220 
1221 /**
1222  * i40e_get_ethtool_fdir_entry - Look up a filter based on Rx flow
1223  * @pf: Pointer to the physical function struct
1224  * @cmd: The command to get or set Rx flow classification rules
1225  *
1226  * This function looks up a filter based on the Rx flow classification
1227  * command and fills the flow spec info for it if found
1228  *
1229  * Returns 0 on success or -EINVAL if filter not found
1230  **/
1231 static int i40e_get_ethtool_fdir_entry(struct i40e_pf *pf,
1232 				       struct ethtool_rxnfc *cmd)
1233 {
1234 	struct ethtool_rx_flow_spec *fsp =
1235 			(struct ethtool_rx_flow_spec *)&cmd->fs;
1236 	struct i40e_fdir_filter *rule = NULL;
1237 	struct hlist_node *node2;
1238 
1239 	hlist_for_each_entry_safe(rule, node2,
1240 				  &pf->fdir_filter_list, fdir_node) {
1241 		if (fsp->location <= rule->fd_id)
1242 			break;
1243 	}
1244 
1245 	if (!rule || fsp->location != rule->fd_id)
1246 		return -EINVAL;
1247 
1248 	fsp->flow_type = rule->flow_type;
1249 	if (fsp->flow_type == IP_USER_FLOW) {
1250 		fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
1251 		fsp->h_u.usr_ip4_spec.proto = 0;
1252 		fsp->m_u.usr_ip4_spec.proto = 0;
1253 	}
1254 
1255 	/* Reverse the src and dest notion, since the HW views them from
1256 	 * Tx perspective where as the user expects it from Rx filter view.
1257 	 */
1258 	fsp->h_u.tcp_ip4_spec.psrc = rule->dst_port;
1259 	fsp->h_u.tcp_ip4_spec.pdst = rule->src_port;
1260 	fsp->h_u.tcp_ip4_spec.ip4src = rule->dst_ip[0];
1261 	fsp->h_u.tcp_ip4_spec.ip4dst = rule->src_ip[0];
1262 
1263 	if (rule->dest_ctl == I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET)
1264 		fsp->ring_cookie = RX_CLS_FLOW_DISC;
1265 	else
1266 		fsp->ring_cookie = rule->q_index;
1267 
1268 	return 0;
1269 }
1270 
1271 /**
1272  * i40e_get_rxnfc - command to get RX flow classification rules
1273  * @netdev: network interface device structure
1274  * @cmd: ethtool rxnfc command
1275  *
1276  * Returns Success if the command is supported.
1277  **/
1278 static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1279 			  u32 *rule_locs)
1280 {
1281 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1282 	struct i40e_vsi *vsi = np->vsi;
1283 	struct i40e_pf *pf = vsi->back;
1284 	int ret = -EOPNOTSUPP;
1285 
1286 	switch (cmd->cmd) {
1287 	case ETHTOOL_GRXRINGS:
1288 		cmd->data = vsi->alloc_queue_pairs;
1289 		ret = 0;
1290 		break;
1291 	case ETHTOOL_GRXFH:
1292 		ret = i40e_get_rss_hash_opts(pf, cmd);
1293 		break;
1294 	case ETHTOOL_GRXCLSRLCNT:
1295 		cmd->rule_cnt = pf->fdir_pf_active_filters;
1296 		/* report total rule count */
1297 		cmd->data = i40e_get_fd_cnt_all(pf);
1298 		ret = 0;
1299 		break;
1300 	case ETHTOOL_GRXCLSRULE:
1301 		ret = i40e_get_ethtool_fdir_entry(pf, cmd);
1302 		break;
1303 	case ETHTOOL_GRXCLSRLALL:
1304 		ret = i40e_get_ethtool_fdir_all(pf, cmd, rule_locs);
1305 		break;
1306 	default:
1307 		break;
1308 	}
1309 
1310 	return ret;
1311 }
1312 
1313 /**
1314  * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
1315  * @pf: pointer to the physical function struct
1316  * @cmd: ethtool rxnfc command
1317  *
1318  * Returns Success if the flow input set is supported.
1319  **/
1320 static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
1321 {
1322 	struct i40e_hw *hw = &pf->hw;
1323 	u64 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
1324 		   ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
1325 
1326 	/* RSS does not support anything other than hashing
1327 	 * to queues on src and dst IPs and ports
1328 	 */
1329 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
1330 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
1331 		return -EINVAL;
1332 
1333 	/* We need at least the IP SRC and DEST fields for hashing */
1334 	if (!(nfc->data & RXH_IP_SRC) ||
1335 	    !(nfc->data & RXH_IP_DST))
1336 		return -EINVAL;
1337 
1338 	switch (nfc->flow_type) {
1339 	case TCP_V4_FLOW:
1340 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1341 		case 0:
1342 			hena &= ~((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
1343 			break;
1344 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1345 			hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP);
1346 			break;
1347 		default:
1348 			return -EINVAL;
1349 		}
1350 		break;
1351 	case TCP_V6_FLOW:
1352 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1353 		case 0:
1354 			hena &= ~((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
1355 			break;
1356 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1357 			hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP);
1358 			break;
1359 		default:
1360 			return -EINVAL;
1361 		}
1362 		break;
1363 	case UDP_V4_FLOW:
1364 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1365 		case 0:
1366 			hena &= ~(((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
1367 				  ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4));
1368 			break;
1369 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1370 			hena |= (((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
1371 				  ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4));
1372 			break;
1373 		default:
1374 			return -EINVAL;
1375 		}
1376 		break;
1377 	case UDP_V6_FLOW:
1378 		switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1379 		case 0:
1380 			hena &= ~(((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
1381 				  ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6));
1382 			break;
1383 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1384 			hena |= (((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
1385 				 ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6));
1386 			break;
1387 		default:
1388 			return -EINVAL;
1389 		}
1390 		break;
1391 	case AH_ESP_V4_FLOW:
1392 	case AH_V4_FLOW:
1393 	case ESP_V4_FLOW:
1394 	case SCTP_V4_FLOW:
1395 		if ((nfc->data & RXH_L4_B_0_1) ||
1396 		    (nfc->data & RXH_L4_B_2_3))
1397 			return -EINVAL;
1398 		hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
1399 		break;
1400 	case AH_ESP_V6_FLOW:
1401 	case AH_V6_FLOW:
1402 	case ESP_V6_FLOW:
1403 	case SCTP_V6_FLOW:
1404 		if ((nfc->data & RXH_L4_B_0_1) ||
1405 		    (nfc->data & RXH_L4_B_2_3))
1406 			return -EINVAL;
1407 		hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
1408 		break;
1409 	case IPV4_FLOW:
1410 		hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
1411 			((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4);
1412 		break;
1413 	case IPV6_FLOW:
1414 		hena |= ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
1415 			((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6);
1416 		break;
1417 	default:
1418 		return -EINVAL;
1419 	}
1420 
1421 	wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
1422 	wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
1423 	i40e_flush(hw);
1424 
1425 	return 0;
1426 }
1427 
1428 /**
1429  * i40e_match_fdir_input_set - Match a new filter against an existing one
1430  * @rule: The filter already added
1431  * @input: The new filter to comapre against
1432  *
1433  * Returns true if the two input set match
1434  **/
1435 static bool i40e_match_fdir_input_set(struct i40e_fdir_filter *rule,
1436 				      struct i40e_fdir_filter *input)
1437 {
1438 	if ((rule->dst_ip[0] != input->dst_ip[0]) ||
1439 	    (rule->src_ip[0] != input->src_ip[0]) ||
1440 	    (rule->dst_port != input->dst_port) ||
1441 	    (rule->src_port != input->src_port))
1442 		return false;
1443 	return true;
1444 }
1445 
1446 /**
1447  * i40e_update_ethtool_fdir_entry - Updates the fdir filter entry
1448  * @vsi: Pointer to the targeted VSI
1449  * @input: The filter to update or NULL to indicate deletion
1450  * @sw_idx: Software index to the filter
1451  * @cmd: The command to get or set Rx flow classification rules
1452  *
1453  * This function updates (or deletes) a Flow Director entry from
1454  * the hlist of the corresponding PF
1455  *
1456  * Returns 0 on success
1457  **/
1458 static int i40e_update_ethtool_fdir_entry(struct i40e_vsi *vsi,
1459 					  struct i40e_fdir_filter *input,
1460 					  u16 sw_idx,
1461 					  struct ethtool_rxnfc *cmd)
1462 {
1463 	struct i40e_fdir_filter *rule, *parent;
1464 	struct i40e_pf *pf = vsi->back;
1465 	struct hlist_node *node2;
1466 	int err = -EINVAL;
1467 
1468 	parent = NULL;
1469 	rule = NULL;
1470 
1471 	hlist_for_each_entry_safe(rule, node2,
1472 				  &pf->fdir_filter_list, fdir_node) {
1473 		/* hash found, or no matching entry */
1474 		if (rule->fd_id >= sw_idx)
1475 			break;
1476 		parent = rule;
1477 	}
1478 
1479 	/* if there is an old rule occupying our place remove it */
1480 	if (rule && (rule->fd_id == sw_idx)) {
1481 		if (input && !i40e_match_fdir_input_set(rule, input))
1482 			err = i40e_add_del_fdir(vsi, rule, false);
1483 		else if (!input)
1484 			err = i40e_add_del_fdir(vsi, rule, false);
1485 		hlist_del(&rule->fdir_node);
1486 		kfree(rule);
1487 		pf->fdir_pf_active_filters--;
1488 	}
1489 
1490 	/* If no input this was a delete, err should be 0 if a rule was
1491 	 * successfully found and removed from the list else -EINVAL
1492 	 */
1493 	if (!input)
1494 		return err;
1495 
1496 	/* initialize node and set software index */
1497 	INIT_HLIST_NODE(&input->fdir_node);
1498 
1499 	/* add filter to the list */
1500 	if (parent)
1501 		hlist_add_after(&parent->fdir_node, &input->fdir_node);
1502 	else
1503 		hlist_add_head(&input->fdir_node,
1504 			       &pf->fdir_filter_list);
1505 
1506 	/* update counts */
1507 	pf->fdir_pf_active_filters++;
1508 
1509 	return 0;
1510 }
1511 
1512 /**
1513  * i40e_del_fdir_entry - Deletes a Flow Director filter entry
1514  * @vsi: Pointer to the targeted VSI
1515  * @cmd: The command to get or set Rx flow classification rules
1516  *
1517  * The function removes a Flow Director filter entry from the
1518  * hlist of the corresponding PF
1519  *
1520  * Returns 0 on success
1521  */
1522 static int i40e_del_fdir_entry(struct i40e_vsi *vsi,
1523 			       struct ethtool_rxnfc *cmd)
1524 {
1525 	struct ethtool_rx_flow_spec *fsp =
1526 		(struct ethtool_rx_flow_spec *)&cmd->fs;
1527 	struct i40e_pf *pf = vsi->back;
1528 	int ret = 0;
1529 
1530 	ret = i40e_update_ethtool_fdir_entry(vsi, NULL, fsp->location, cmd);
1531 
1532 	i40e_fdir_check_and_reenable(pf);
1533 	return ret;
1534 }
1535 
1536 /**
1537  * i40e_add_fdir_ethtool - Add/Remove Flow Director filters
1538  * @vsi: pointer to the targeted VSI
1539  * @cmd: command to get or set RX flow classification rules
1540  *
1541  * Add Flow Director filters for a specific flow spec based on their
1542  * protocol.  Returns 0 if the filters were successfully added.
1543  **/
1544 static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
1545 				 struct ethtool_rxnfc *cmd)
1546 {
1547 	struct ethtool_rx_flow_spec *fsp;
1548 	struct i40e_fdir_filter *input;
1549 	struct i40e_pf *pf;
1550 	int ret = -EINVAL;
1551 
1552 	if (!vsi)
1553 		return -EINVAL;
1554 
1555 	pf = vsi->back;
1556 
1557 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
1558 		return -EOPNOTSUPP;
1559 
1560 	if (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)
1561 		return -ENOSPC;
1562 
1563 	fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
1564 
1565 	if (fsp->location >= (pf->hw.func_caps.fd_filters_best_effort +
1566 			      pf->hw.func_caps.fd_filters_guaranteed)) {
1567 		return -EINVAL;
1568 	}
1569 
1570 	if ((fsp->ring_cookie != RX_CLS_FLOW_DISC) &&
1571 	    (fsp->ring_cookie >= vsi->num_queue_pairs))
1572 		return -EINVAL;
1573 
1574 	input = kzalloc(sizeof(*input), GFP_KERNEL);
1575 
1576 	if (!input)
1577 		return -ENOMEM;
1578 
1579 	input->fd_id = fsp->location;
1580 
1581 	if (fsp->ring_cookie == RX_CLS_FLOW_DISC)
1582 		input->dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
1583 	else
1584 		input->dest_ctl =
1585 			     I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX;
1586 
1587 	input->q_index = fsp->ring_cookie;
1588 	input->flex_off = 0;
1589 	input->pctype = 0;
1590 	input->dest_vsi = vsi->id;
1591 	input->fd_status = I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID;
1592 	input->cnt_index  = pf->fd_sb_cnt_idx;
1593 	input->flow_type = fsp->flow_type;
1594 	input->ip4_proto = fsp->h_u.usr_ip4_spec.proto;
1595 
1596 	/* Reverse the src and dest notion, since the HW expects them to be from
1597 	 * Tx perspective where as the input from user is from Rx filter view.
1598 	 */
1599 	input->dst_port = fsp->h_u.tcp_ip4_spec.psrc;
1600 	input->src_port = fsp->h_u.tcp_ip4_spec.pdst;
1601 	input->dst_ip[0] = fsp->h_u.tcp_ip4_spec.ip4src;
1602 	input->src_ip[0] = fsp->h_u.tcp_ip4_spec.ip4dst;
1603 
1604 	ret = i40e_add_del_fdir(vsi, input, true);
1605 	if (ret)
1606 		kfree(input);
1607 	else
1608 		i40e_update_ethtool_fdir_entry(vsi, input, fsp->location, NULL);
1609 
1610 	return ret;
1611 }
1612 
1613 /**
1614  * i40e_set_rxnfc - command to set RX flow classification rules
1615  * @netdev: network interface device structure
1616  * @cmd: ethtool rxnfc command
1617  *
1618  * Returns Success if the command is supported.
1619  **/
1620 static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1621 {
1622 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1623 	struct i40e_vsi *vsi = np->vsi;
1624 	struct i40e_pf *pf = vsi->back;
1625 	int ret = -EOPNOTSUPP;
1626 
1627 	switch (cmd->cmd) {
1628 	case ETHTOOL_SRXFH:
1629 		ret = i40e_set_rss_hash_opt(pf, cmd);
1630 		break;
1631 	case ETHTOOL_SRXCLSRLINS:
1632 		ret = i40e_add_fdir_ethtool(vsi, cmd);
1633 		break;
1634 	case ETHTOOL_SRXCLSRLDEL:
1635 		ret = i40e_del_fdir_entry(vsi, cmd);
1636 		break;
1637 	default:
1638 		break;
1639 	}
1640 
1641 	return ret;
1642 }
1643 
1644 /**
1645  * i40e_max_channels - get Max number of combined channels supported
1646  * @vsi: vsi pointer
1647  **/
1648 static unsigned int i40e_max_channels(struct i40e_vsi *vsi)
1649 {
1650 	/* TODO: This code assumes DCB and FD is disabled for now. */
1651 	return vsi->alloc_queue_pairs;
1652 }
1653 
1654 /**
1655  * i40e_get_channels - Get the current channels enabled and max supported etc.
1656  * @netdev: network interface device structure
1657  * @ch: ethtool channels structure
1658  *
1659  * We don't support separate tx and rx queues as channels. The other count
1660  * represents how many queues are being used for control. max_combined counts
1661  * how many queue pairs we can support. They may not be mapped 1 to 1 with
1662  * q_vectors since we support a lot more queue pairs than q_vectors.
1663  **/
1664 static void i40e_get_channels(struct net_device *dev,
1665 			       struct ethtool_channels *ch)
1666 {
1667 	struct i40e_netdev_priv *np = netdev_priv(dev);
1668 	struct i40e_vsi *vsi = np->vsi;
1669 	struct i40e_pf *pf = vsi->back;
1670 
1671 	/* report maximum channels */
1672 	ch->max_combined = i40e_max_channels(vsi);
1673 
1674 	/* report info for other vector */
1675 	ch->other_count = (pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0;
1676 	ch->max_other = ch->other_count;
1677 
1678 	/* Note: This code assumes DCB is disabled for now. */
1679 	ch->combined_count = vsi->num_queue_pairs;
1680 }
1681 
1682 /**
1683  * i40e_set_channels - Set the new channels count.
1684  * @netdev: network interface device structure
1685  * @ch: ethtool channels structure
1686  *
1687  * The new channels count may not be the same as requested by the user
1688  * since it gets rounded down to a power of 2 value.
1689  **/
1690 static int i40e_set_channels(struct net_device *dev,
1691 			      struct ethtool_channels *ch)
1692 {
1693 	struct i40e_netdev_priv *np = netdev_priv(dev);
1694 	unsigned int count = ch->combined_count;
1695 	struct i40e_vsi *vsi = np->vsi;
1696 	struct i40e_pf *pf = vsi->back;
1697 	int new_count;
1698 
1699 	/* We do not support setting channels for any other VSI at present */
1700 	if (vsi->type != I40E_VSI_MAIN)
1701 		return -EINVAL;
1702 
1703 	/* verify they are not requesting separate vectors */
1704 	if (!count || ch->rx_count || ch->tx_count)
1705 		return -EINVAL;
1706 
1707 	/* verify other_count has not changed */
1708 	if (ch->other_count != ((pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0))
1709 		return -EINVAL;
1710 
1711 	/* verify the number of channels does not exceed hardware limits */
1712 	if (count > i40e_max_channels(vsi))
1713 		return -EINVAL;
1714 
1715 	/* update feature limits from largest to smallest supported values */
1716 	/* TODO: Flow director limit, DCB etc */
1717 
1718 	/* cap RSS limit */
1719 	if (count > pf->rss_size_max)
1720 		count = pf->rss_size_max;
1721 
1722 	/* use rss_reconfig to rebuild with new queue count and update traffic
1723 	 * class queue mapping
1724 	 */
1725 	new_count = i40e_reconfig_rss_queues(pf, count);
1726 	if (new_count > 0)
1727 		return 0;
1728 	else
1729 		return -EINVAL;
1730 }
1731 
1732 static const struct ethtool_ops i40e_ethtool_ops = {
1733 	.get_settings		= i40e_get_settings,
1734 	.get_drvinfo		= i40e_get_drvinfo,
1735 	.get_regs_len		= i40e_get_regs_len,
1736 	.get_regs		= i40e_get_regs,
1737 	.nway_reset		= i40e_nway_reset,
1738 	.get_link		= ethtool_op_get_link,
1739 	.get_wol		= i40e_get_wol,
1740 	.set_wol		= i40e_set_wol,
1741 	.get_eeprom_len		= i40e_get_eeprom_len,
1742 	.get_eeprom		= i40e_get_eeprom,
1743 	.get_ringparam		= i40e_get_ringparam,
1744 	.set_ringparam		= i40e_set_ringparam,
1745 	.get_pauseparam		= i40e_get_pauseparam,
1746 	.get_msglevel		= i40e_get_msglevel,
1747 	.set_msglevel		= i40e_set_msglevel,
1748 	.get_rxnfc		= i40e_get_rxnfc,
1749 	.set_rxnfc		= i40e_set_rxnfc,
1750 	.self_test		= i40e_diag_test,
1751 	.get_strings		= i40e_get_strings,
1752 	.set_phys_id		= i40e_set_phys_id,
1753 	.get_sset_count		= i40e_get_sset_count,
1754 	.get_ethtool_stats	= i40e_get_ethtool_stats,
1755 	.get_coalesce		= i40e_get_coalesce,
1756 	.set_coalesce		= i40e_set_coalesce,
1757 	.get_channels		= i40e_get_channels,
1758 	.set_channels		= i40e_set_channels,
1759 	.get_ts_info		= i40e_get_ts_info,
1760 };
1761 
1762 void i40e_set_ethtool_ops(struct net_device *netdev)
1763 {
1764 	netdev->ethtool_ops = &i40e_ethtool_ops;
1765 }
1766