xref: /linux/drivers/net/ethernet/qlogic/qede/qede_ethtool.c (revision b7d3826c2ed6c3e626e7ae796c5df2c0d2551c6a)
1 /* QLogic qede NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and /or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <linux/version.h>
33 #include <linux/types.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/ethtool.h>
37 #include <linux/string.h>
38 #include <linux/pci.h>
39 #include <linux/capability.h>
40 #include <linux/vmalloc.h>
41 #include "qede.h"
42 #include "qede_ptp.h"
43 
44 #define QEDE_RQSTAT_OFFSET(stat_name) \
45 	 (offsetof(struct qede_rx_queue, stat_name))
46 #define QEDE_RQSTAT_STRING(stat_name) (#stat_name)
47 #define QEDE_RQSTAT(stat_name) \
48 	 {QEDE_RQSTAT_OFFSET(stat_name), QEDE_RQSTAT_STRING(stat_name)}
49 
50 #define QEDE_SELFTEST_POLL_COUNT 100
51 
52 static const struct {
53 	u64 offset;
54 	char string[ETH_GSTRING_LEN];
55 } qede_rqstats_arr[] = {
56 	QEDE_RQSTAT(rcv_pkts),
57 	QEDE_RQSTAT(rx_hw_errors),
58 	QEDE_RQSTAT(rx_alloc_errors),
59 	QEDE_RQSTAT(rx_ip_frags),
60 	QEDE_RQSTAT(xdp_no_pass),
61 };
62 
63 #define QEDE_NUM_RQSTATS ARRAY_SIZE(qede_rqstats_arr)
64 #define QEDE_TQSTAT_OFFSET(stat_name) \
65 	(offsetof(struct qede_tx_queue, stat_name))
66 #define QEDE_TQSTAT_STRING(stat_name) (#stat_name)
67 #define QEDE_TQSTAT(stat_name) \
68 	{QEDE_TQSTAT_OFFSET(stat_name), QEDE_TQSTAT_STRING(stat_name)}
69 #define QEDE_NUM_TQSTATS ARRAY_SIZE(qede_tqstats_arr)
70 static const struct {
71 	u64 offset;
72 	char string[ETH_GSTRING_LEN];
73 } qede_tqstats_arr[] = {
74 	QEDE_TQSTAT(xmit_pkts),
75 	QEDE_TQSTAT(stopped_cnt),
76 };
77 
78 #define QEDE_STAT_OFFSET(stat_name, type, base) \
79 	(offsetof(type, stat_name) + (base))
80 #define QEDE_STAT_STRING(stat_name)	(#stat_name)
81 #define _QEDE_STAT(stat_name, type, base, attr) \
82 	{QEDE_STAT_OFFSET(stat_name, type, base), \
83 	 QEDE_STAT_STRING(stat_name), \
84 	 attr}
85 #define QEDE_STAT(stat_name) \
86 	_QEDE_STAT(stat_name, struct qede_stats_common, 0, 0x0)
87 #define QEDE_PF_STAT(stat_name) \
88 	_QEDE_STAT(stat_name, struct qede_stats_common, 0, \
89 		   BIT(QEDE_STAT_PF_ONLY))
90 #define QEDE_PF_BB_STAT(stat_name) \
91 	_QEDE_STAT(stat_name, struct qede_stats_bb, \
92 		   offsetof(struct qede_stats, bb), \
93 		   BIT(QEDE_STAT_PF_ONLY) | BIT(QEDE_STAT_BB_ONLY))
94 #define QEDE_PF_AH_STAT(stat_name) \
95 	_QEDE_STAT(stat_name, struct qede_stats_ah, \
96 		   offsetof(struct qede_stats, ah), \
97 		   BIT(QEDE_STAT_PF_ONLY) | BIT(QEDE_STAT_AH_ONLY))
98 static const struct {
99 	u64 offset;
100 	char string[ETH_GSTRING_LEN];
101 	unsigned long attr;
102 #define QEDE_STAT_PF_ONLY	0
103 #define QEDE_STAT_BB_ONLY	1
104 #define QEDE_STAT_AH_ONLY	2
105 } qede_stats_arr[] = {
106 	QEDE_STAT(rx_ucast_bytes),
107 	QEDE_STAT(rx_mcast_bytes),
108 	QEDE_STAT(rx_bcast_bytes),
109 	QEDE_STAT(rx_ucast_pkts),
110 	QEDE_STAT(rx_mcast_pkts),
111 	QEDE_STAT(rx_bcast_pkts),
112 
113 	QEDE_STAT(tx_ucast_bytes),
114 	QEDE_STAT(tx_mcast_bytes),
115 	QEDE_STAT(tx_bcast_bytes),
116 	QEDE_STAT(tx_ucast_pkts),
117 	QEDE_STAT(tx_mcast_pkts),
118 	QEDE_STAT(tx_bcast_pkts),
119 
120 	QEDE_PF_STAT(rx_64_byte_packets),
121 	QEDE_PF_STAT(rx_65_to_127_byte_packets),
122 	QEDE_PF_STAT(rx_128_to_255_byte_packets),
123 	QEDE_PF_STAT(rx_256_to_511_byte_packets),
124 	QEDE_PF_STAT(rx_512_to_1023_byte_packets),
125 	QEDE_PF_STAT(rx_1024_to_1518_byte_packets),
126 	QEDE_PF_BB_STAT(rx_1519_to_1522_byte_packets),
127 	QEDE_PF_BB_STAT(rx_1519_to_2047_byte_packets),
128 	QEDE_PF_BB_STAT(rx_2048_to_4095_byte_packets),
129 	QEDE_PF_BB_STAT(rx_4096_to_9216_byte_packets),
130 	QEDE_PF_BB_STAT(rx_9217_to_16383_byte_packets),
131 	QEDE_PF_AH_STAT(rx_1519_to_max_byte_packets),
132 	QEDE_PF_STAT(tx_64_byte_packets),
133 	QEDE_PF_STAT(tx_65_to_127_byte_packets),
134 	QEDE_PF_STAT(tx_128_to_255_byte_packets),
135 	QEDE_PF_STAT(tx_256_to_511_byte_packets),
136 	QEDE_PF_STAT(tx_512_to_1023_byte_packets),
137 	QEDE_PF_STAT(tx_1024_to_1518_byte_packets),
138 	QEDE_PF_BB_STAT(tx_1519_to_2047_byte_packets),
139 	QEDE_PF_BB_STAT(tx_2048_to_4095_byte_packets),
140 	QEDE_PF_BB_STAT(tx_4096_to_9216_byte_packets),
141 	QEDE_PF_BB_STAT(tx_9217_to_16383_byte_packets),
142 	QEDE_PF_AH_STAT(tx_1519_to_max_byte_packets),
143 	QEDE_PF_STAT(rx_mac_crtl_frames),
144 	QEDE_PF_STAT(tx_mac_ctrl_frames),
145 	QEDE_PF_STAT(rx_pause_frames),
146 	QEDE_PF_STAT(tx_pause_frames),
147 	QEDE_PF_STAT(rx_pfc_frames),
148 	QEDE_PF_STAT(tx_pfc_frames),
149 
150 	QEDE_PF_STAT(rx_crc_errors),
151 	QEDE_PF_STAT(rx_align_errors),
152 	QEDE_PF_STAT(rx_carrier_errors),
153 	QEDE_PF_STAT(rx_oversize_packets),
154 	QEDE_PF_STAT(rx_jabbers),
155 	QEDE_PF_STAT(rx_undersize_packets),
156 	QEDE_PF_STAT(rx_fragments),
157 	QEDE_PF_BB_STAT(tx_lpi_entry_count),
158 	QEDE_PF_BB_STAT(tx_total_collisions),
159 	QEDE_PF_STAT(brb_truncates),
160 	QEDE_PF_STAT(brb_discards),
161 	QEDE_STAT(no_buff_discards),
162 	QEDE_PF_STAT(mftag_filter_discards),
163 	QEDE_PF_STAT(mac_filter_discards),
164 	QEDE_PF_STAT(gft_filter_drop),
165 	QEDE_STAT(tx_err_drop_pkts),
166 	QEDE_STAT(ttl0_discard),
167 	QEDE_STAT(packet_too_big_discard),
168 
169 	QEDE_STAT(coalesced_pkts),
170 	QEDE_STAT(coalesced_events),
171 	QEDE_STAT(coalesced_aborts_num),
172 	QEDE_STAT(non_coalesced_pkts),
173 	QEDE_STAT(coalesced_bytes),
174 
175 	QEDE_STAT(link_change_count),
176 };
177 
178 #define QEDE_NUM_STATS	ARRAY_SIZE(qede_stats_arr)
179 #define QEDE_STAT_IS_PF_ONLY(i) \
180 	test_bit(QEDE_STAT_PF_ONLY, &qede_stats_arr[i].attr)
181 #define QEDE_STAT_IS_BB_ONLY(i) \
182 	test_bit(QEDE_STAT_BB_ONLY, &qede_stats_arr[i].attr)
183 #define QEDE_STAT_IS_AH_ONLY(i) \
184 	test_bit(QEDE_STAT_AH_ONLY, &qede_stats_arr[i].attr)
185 
186 enum {
187 	QEDE_PRI_FLAG_CMT,
188 	QEDE_PRI_FLAG_LEN,
189 };
190 
191 static const char qede_private_arr[QEDE_PRI_FLAG_LEN][ETH_GSTRING_LEN] = {
192 	"Coupled-Function",
193 };
194 
195 enum qede_ethtool_tests {
196 	QEDE_ETHTOOL_INT_LOOPBACK,
197 	QEDE_ETHTOOL_INTERRUPT_TEST,
198 	QEDE_ETHTOOL_MEMORY_TEST,
199 	QEDE_ETHTOOL_REGISTER_TEST,
200 	QEDE_ETHTOOL_CLOCK_TEST,
201 	QEDE_ETHTOOL_NVRAM_TEST,
202 	QEDE_ETHTOOL_TEST_MAX
203 };
204 
205 static const char qede_tests_str_arr[QEDE_ETHTOOL_TEST_MAX][ETH_GSTRING_LEN] = {
206 	"Internal loopback (offline)",
207 	"Interrupt (online)\t",
208 	"Memory (online)\t\t",
209 	"Register (online)\t",
210 	"Clock (online)\t\t",
211 	"Nvram (online)\t\t",
212 };
213 
214 static void qede_get_strings_stats_txq(struct qede_dev *edev,
215 				       struct qede_tx_queue *txq, u8 **buf)
216 {
217 	int i;
218 
219 	for (i = 0; i < QEDE_NUM_TQSTATS; i++) {
220 		if (txq->is_xdp)
221 			sprintf(*buf, "%d [XDP]: %s",
222 				QEDE_TXQ_XDP_TO_IDX(edev, txq),
223 				qede_tqstats_arr[i].string);
224 		else
225 			sprintf(*buf, "%d_%d: %s", txq->index, txq->cos,
226 				qede_tqstats_arr[i].string);
227 		*buf += ETH_GSTRING_LEN;
228 	}
229 }
230 
231 static void qede_get_strings_stats_rxq(struct qede_dev *edev,
232 				       struct qede_rx_queue *rxq, u8 **buf)
233 {
234 	int i;
235 
236 	for (i = 0; i < QEDE_NUM_RQSTATS; i++) {
237 		sprintf(*buf, "%d: %s", rxq->rxq_id,
238 			qede_rqstats_arr[i].string);
239 		*buf += ETH_GSTRING_LEN;
240 	}
241 }
242 
243 static bool qede_is_irrelevant_stat(struct qede_dev *edev, int stat_index)
244 {
245 	return (IS_VF(edev) && QEDE_STAT_IS_PF_ONLY(stat_index)) ||
246 	       (QEDE_IS_BB(edev) && QEDE_STAT_IS_AH_ONLY(stat_index)) ||
247 	       (QEDE_IS_AH(edev) && QEDE_STAT_IS_BB_ONLY(stat_index));
248 }
249 
250 static void qede_get_strings_stats(struct qede_dev *edev, u8 *buf)
251 {
252 	struct qede_fastpath *fp;
253 	int i;
254 
255 	/* Account for queue statistics */
256 	for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
257 		fp = &edev->fp_array[i];
258 
259 		if (fp->type & QEDE_FASTPATH_RX)
260 			qede_get_strings_stats_rxq(edev, fp->rxq, &buf);
261 
262 		if (fp->type & QEDE_FASTPATH_XDP)
263 			qede_get_strings_stats_txq(edev, fp->xdp_tx, &buf);
264 
265 		if (fp->type & QEDE_FASTPATH_TX) {
266 			int cos;
267 
268 			for_each_cos_in_txq(edev, cos)
269 				qede_get_strings_stats_txq(edev,
270 							   &fp->txq[cos], &buf);
271 		}
272 	}
273 
274 	/* Account for non-queue statistics */
275 	for (i = 0; i < QEDE_NUM_STATS; i++) {
276 		if (qede_is_irrelevant_stat(edev, i))
277 			continue;
278 		strcpy(buf, qede_stats_arr[i].string);
279 		buf += ETH_GSTRING_LEN;
280 	}
281 }
282 
283 static void qede_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
284 {
285 	struct qede_dev *edev = netdev_priv(dev);
286 
287 	switch (stringset) {
288 	case ETH_SS_STATS:
289 		qede_get_strings_stats(edev, buf);
290 		break;
291 	case ETH_SS_PRIV_FLAGS:
292 		memcpy(buf, qede_private_arr,
293 		       ETH_GSTRING_LEN * QEDE_PRI_FLAG_LEN);
294 		break;
295 	case ETH_SS_TEST:
296 		memcpy(buf, qede_tests_str_arr,
297 		       ETH_GSTRING_LEN * QEDE_ETHTOOL_TEST_MAX);
298 		break;
299 	default:
300 		DP_VERBOSE(edev, QED_MSG_DEBUG,
301 			   "Unsupported stringset 0x%08x\n", stringset);
302 	}
303 }
304 
305 static void qede_get_ethtool_stats_txq(struct qede_tx_queue *txq, u64 **buf)
306 {
307 	int i;
308 
309 	for (i = 0; i < QEDE_NUM_TQSTATS; i++) {
310 		**buf = *((u64 *)(((void *)txq) + qede_tqstats_arr[i].offset));
311 		(*buf)++;
312 	}
313 }
314 
315 static void qede_get_ethtool_stats_rxq(struct qede_rx_queue *rxq, u64 **buf)
316 {
317 	int i;
318 
319 	for (i = 0; i < QEDE_NUM_RQSTATS; i++) {
320 		**buf = *((u64 *)(((void *)rxq) + qede_rqstats_arr[i].offset));
321 		(*buf)++;
322 	}
323 }
324 
325 static void qede_get_ethtool_stats(struct net_device *dev,
326 				   struct ethtool_stats *stats, u64 *buf)
327 {
328 	struct qede_dev *edev = netdev_priv(dev);
329 	struct qede_fastpath *fp;
330 	int i;
331 
332 	qede_fill_by_demand_stats(edev);
333 
334 	/* Need to protect the access to the fastpath array */
335 	__qede_lock(edev);
336 
337 	for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
338 		fp = &edev->fp_array[i];
339 
340 		if (fp->type & QEDE_FASTPATH_RX)
341 			qede_get_ethtool_stats_rxq(fp->rxq, &buf);
342 
343 		if (fp->type & QEDE_FASTPATH_XDP)
344 			qede_get_ethtool_stats_txq(fp->xdp_tx, &buf);
345 
346 		if (fp->type & QEDE_FASTPATH_TX) {
347 			int cos;
348 
349 			for_each_cos_in_txq(edev, cos)
350 				qede_get_ethtool_stats_txq(&fp->txq[cos], &buf);
351 		}
352 	}
353 
354 	for (i = 0; i < QEDE_NUM_STATS; i++) {
355 		if (qede_is_irrelevant_stat(edev, i))
356 			continue;
357 		*buf = *((u64 *)(((void *)&edev->stats) +
358 				 qede_stats_arr[i].offset));
359 
360 		buf++;
361 	}
362 
363 	__qede_unlock(edev);
364 }
365 
366 static int qede_get_sset_count(struct net_device *dev, int stringset)
367 {
368 	struct qede_dev *edev = netdev_priv(dev);
369 	int num_stats = QEDE_NUM_STATS, i;
370 
371 	switch (stringset) {
372 	case ETH_SS_STATS:
373 		for (i = 0; i < QEDE_NUM_STATS; i++)
374 			if (qede_is_irrelevant_stat(edev, i))
375 				num_stats--;
376 
377 		/* Account for the Regular Tx statistics */
378 		num_stats += QEDE_TSS_COUNT(edev) * QEDE_NUM_TQSTATS *
379 				edev->dev_info.num_tc;
380 
381 		/* Account for the Regular Rx statistics */
382 		num_stats += QEDE_RSS_COUNT(edev) * QEDE_NUM_RQSTATS;
383 
384 		/* Account for XDP statistics [if needed] */
385 		if (edev->xdp_prog)
386 			num_stats += QEDE_RSS_COUNT(edev) * QEDE_NUM_TQSTATS;
387 		return num_stats;
388 
389 	case ETH_SS_PRIV_FLAGS:
390 		return QEDE_PRI_FLAG_LEN;
391 	case ETH_SS_TEST:
392 		if (!IS_VF(edev))
393 			return QEDE_ETHTOOL_TEST_MAX;
394 		else
395 			return 0;
396 	default:
397 		DP_VERBOSE(edev, QED_MSG_DEBUG,
398 			   "Unsupported stringset 0x%08x\n", stringset);
399 		return -EINVAL;
400 	}
401 }
402 
403 static u32 qede_get_priv_flags(struct net_device *dev)
404 {
405 	struct qede_dev *edev = netdev_priv(dev);
406 
407 	return (!!(edev->dev_info.common.num_hwfns > 1)) << QEDE_PRI_FLAG_CMT;
408 }
409 
410 struct qede_link_mode_mapping {
411 	u32 qed_link_mode;
412 	u32 ethtool_link_mode;
413 };
414 
415 static const struct qede_link_mode_mapping qed_lm_map[] = {
416 	{QED_LM_FIBRE_BIT, ETHTOOL_LINK_MODE_FIBRE_BIT},
417 	{QED_LM_Autoneg_BIT, ETHTOOL_LINK_MODE_Autoneg_BIT},
418 	{QED_LM_Asym_Pause_BIT, ETHTOOL_LINK_MODE_Asym_Pause_BIT},
419 	{QED_LM_Pause_BIT, ETHTOOL_LINK_MODE_Pause_BIT},
420 	{QED_LM_1000baseT_Half_BIT, ETHTOOL_LINK_MODE_1000baseT_Half_BIT},
421 	{QED_LM_1000baseT_Full_BIT, ETHTOOL_LINK_MODE_1000baseT_Full_BIT},
422 	{QED_LM_10000baseKR_Full_BIT, ETHTOOL_LINK_MODE_10000baseKR_Full_BIT},
423 	{QED_LM_20000baseKR2_Full_BIT, ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT},
424 	{QED_LM_25000baseKR_Full_BIT, ETHTOOL_LINK_MODE_25000baseKR_Full_BIT},
425 	{QED_LM_40000baseLR4_Full_BIT, ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT},
426 	{QED_LM_50000baseKR2_Full_BIT, ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT},
427 	{QED_LM_100000baseKR4_Full_BIT,
428 	 ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT},
429 };
430 
431 #define QEDE_DRV_TO_ETHTOOL_CAPS(caps, lk_ksettings, name)	\
432 {								\
433 	int i;							\
434 								\
435 	for (i = 0; i < ARRAY_SIZE(qed_lm_map); i++) {		\
436 		if ((caps) & (qed_lm_map[i].qed_link_mode))	\
437 			__set_bit(qed_lm_map[i].ethtool_link_mode,\
438 				  lk_ksettings->link_modes.name); \
439 	}							\
440 }
441 
442 #define QEDE_ETHTOOL_TO_DRV_CAPS(caps, lk_ksettings, name)	\
443 {								\
444 	int i;							\
445 								\
446 	for (i = 0; i < ARRAY_SIZE(qed_lm_map); i++) {		\
447 		if (test_bit(qed_lm_map[i].ethtool_link_mode,	\
448 			     lk_ksettings->link_modes.name))	\
449 			caps |= qed_lm_map[i].qed_link_mode;	\
450 	}							\
451 }
452 
453 static int qede_get_link_ksettings(struct net_device *dev,
454 				   struct ethtool_link_ksettings *cmd)
455 {
456 	struct ethtool_link_settings *base = &cmd->base;
457 	struct qede_dev *edev = netdev_priv(dev);
458 	struct qed_link_output current_link;
459 
460 	__qede_lock(edev);
461 
462 	memset(&current_link, 0, sizeof(current_link));
463 	edev->ops->common->get_link(edev->cdev, &current_link);
464 
465 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
466 	QEDE_DRV_TO_ETHTOOL_CAPS(current_link.supported_caps, cmd, supported)
467 
468 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
469 	QEDE_DRV_TO_ETHTOOL_CAPS(current_link.advertised_caps, cmd, advertising)
470 
471 	ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
472 	QEDE_DRV_TO_ETHTOOL_CAPS(current_link.lp_caps, cmd, lp_advertising)
473 
474 	if ((edev->state == QEDE_STATE_OPEN) && (current_link.link_up)) {
475 		base->speed = current_link.speed;
476 		base->duplex = current_link.duplex;
477 	} else {
478 		base->speed = SPEED_UNKNOWN;
479 		base->duplex = DUPLEX_UNKNOWN;
480 	}
481 
482 	__qede_unlock(edev);
483 
484 	base->port = current_link.port;
485 	base->autoneg = (current_link.autoneg) ? AUTONEG_ENABLE :
486 			AUTONEG_DISABLE;
487 
488 	return 0;
489 }
490 
491 static int qede_set_link_ksettings(struct net_device *dev,
492 				   const struct ethtool_link_ksettings *cmd)
493 {
494 	const struct ethtool_link_settings *base = &cmd->base;
495 	struct qede_dev *edev = netdev_priv(dev);
496 	struct qed_link_output current_link;
497 	struct qed_link_params params;
498 
499 	if (!edev->ops || !edev->ops->common->can_link_change(edev->cdev)) {
500 		DP_INFO(edev, "Link settings are not allowed to be changed\n");
501 		return -EOPNOTSUPP;
502 	}
503 	memset(&current_link, 0, sizeof(current_link));
504 	memset(&params, 0, sizeof(params));
505 	edev->ops->common->get_link(edev->cdev, &current_link);
506 
507 	params.override_flags |= QED_LINK_OVERRIDE_SPEED_ADV_SPEEDS;
508 	params.override_flags |= QED_LINK_OVERRIDE_SPEED_AUTONEG;
509 	if (base->autoneg == AUTONEG_ENABLE) {
510 		if (!(current_link.supported_caps & QED_LM_Autoneg_BIT)) {
511 			DP_INFO(edev, "Auto negotiation is not supported\n");
512 			return -EOPNOTSUPP;
513 		}
514 
515 		params.autoneg = true;
516 		params.forced_speed = 0;
517 		QEDE_ETHTOOL_TO_DRV_CAPS(params.adv_speeds, cmd, advertising)
518 	} else {		/* forced speed */
519 		params.override_flags |= QED_LINK_OVERRIDE_SPEED_FORCED_SPEED;
520 		params.autoneg = false;
521 		params.forced_speed = base->speed;
522 		switch (base->speed) {
523 		case SPEED_1000:
524 			if (!(current_link.supported_caps &
525 			      QED_LM_1000baseT_Full_BIT)) {
526 				DP_INFO(edev, "1G speed not supported\n");
527 				return -EINVAL;
528 			}
529 			params.adv_speeds = QED_LM_1000baseT_Full_BIT;
530 			break;
531 		case SPEED_10000:
532 			if (!(current_link.supported_caps &
533 			      QED_LM_10000baseKR_Full_BIT)) {
534 				DP_INFO(edev, "10G speed not supported\n");
535 				return -EINVAL;
536 			}
537 			params.adv_speeds = QED_LM_10000baseKR_Full_BIT;
538 			break;
539 		case SPEED_20000:
540 			if (!(current_link.supported_caps &
541 			      QED_LM_20000baseKR2_Full_BIT)) {
542 				DP_INFO(edev, "20G speed not supported\n");
543 				return -EINVAL;
544 			}
545 			params.adv_speeds = QED_LM_20000baseKR2_Full_BIT;
546 			break;
547 		case SPEED_25000:
548 			if (!(current_link.supported_caps &
549 			      QED_LM_25000baseKR_Full_BIT)) {
550 				DP_INFO(edev, "25G speed not supported\n");
551 				return -EINVAL;
552 			}
553 			params.adv_speeds = QED_LM_25000baseKR_Full_BIT;
554 			break;
555 		case SPEED_40000:
556 			if (!(current_link.supported_caps &
557 			      QED_LM_40000baseLR4_Full_BIT)) {
558 				DP_INFO(edev, "40G speed not supported\n");
559 				return -EINVAL;
560 			}
561 			params.adv_speeds = QED_LM_40000baseLR4_Full_BIT;
562 			break;
563 		case SPEED_50000:
564 			if (!(current_link.supported_caps &
565 			      QED_LM_50000baseKR2_Full_BIT)) {
566 				DP_INFO(edev, "50G speed not supported\n");
567 				return -EINVAL;
568 			}
569 			params.adv_speeds = QED_LM_50000baseKR2_Full_BIT;
570 			break;
571 		case SPEED_100000:
572 			if (!(current_link.supported_caps &
573 			      QED_LM_100000baseKR4_Full_BIT)) {
574 				DP_INFO(edev, "100G speed not supported\n");
575 				return -EINVAL;
576 			}
577 			params.adv_speeds = QED_LM_100000baseKR4_Full_BIT;
578 			break;
579 		default:
580 			DP_INFO(edev, "Unsupported speed %u\n", base->speed);
581 			return -EINVAL;
582 		}
583 	}
584 
585 	params.link_up = true;
586 	edev->ops->common->set_link(edev->cdev, &params);
587 
588 	return 0;
589 }
590 
591 static void qede_get_drvinfo(struct net_device *ndev,
592 			     struct ethtool_drvinfo *info)
593 {
594 	char mfw[ETHTOOL_FWVERS_LEN], storm[ETHTOOL_FWVERS_LEN];
595 	struct qede_dev *edev = netdev_priv(ndev);
596 
597 	strlcpy(info->driver, "qede", sizeof(info->driver));
598 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
599 
600 	snprintf(storm, ETHTOOL_FWVERS_LEN, "%d.%d.%d.%d",
601 		 edev->dev_info.common.fw_major,
602 		 edev->dev_info.common.fw_minor,
603 		 edev->dev_info.common.fw_rev,
604 		 edev->dev_info.common.fw_eng);
605 
606 	snprintf(mfw, ETHTOOL_FWVERS_LEN, "%d.%d.%d.%d",
607 		 (edev->dev_info.common.mfw_rev >> 24) & 0xFF,
608 		 (edev->dev_info.common.mfw_rev >> 16) & 0xFF,
609 		 (edev->dev_info.common.mfw_rev >> 8) & 0xFF,
610 		 edev->dev_info.common.mfw_rev & 0xFF);
611 
612 	if ((strlen(storm) + strlen(mfw) + strlen("mfw storm  ")) <
613 	    sizeof(info->fw_version)) {
614 		snprintf(info->fw_version, sizeof(info->fw_version),
615 			 "mfw %s storm %s", mfw, storm);
616 	} else {
617 		snprintf(info->fw_version, sizeof(info->fw_version),
618 			 "%s %s", mfw, storm);
619 	}
620 
621 	strlcpy(info->bus_info, pci_name(edev->pdev), sizeof(info->bus_info));
622 }
623 
624 static void qede_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
625 {
626 	struct qede_dev *edev = netdev_priv(ndev);
627 
628 	if (edev->dev_info.common.wol_support) {
629 		wol->supported = WAKE_MAGIC;
630 		wol->wolopts = edev->wol_enabled ? WAKE_MAGIC : 0;
631 	}
632 }
633 
634 static int qede_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
635 {
636 	struct qede_dev *edev = netdev_priv(ndev);
637 	bool wol_requested;
638 	int rc;
639 
640 	if (wol->wolopts & ~WAKE_MAGIC) {
641 		DP_INFO(edev,
642 			"Can't support WoL options other than magic-packet\n");
643 		return -EINVAL;
644 	}
645 
646 	wol_requested = !!(wol->wolopts & WAKE_MAGIC);
647 	if (wol_requested == edev->wol_enabled)
648 		return 0;
649 
650 	/* Need to actually change configuration */
651 	if (!edev->dev_info.common.wol_support) {
652 		DP_INFO(edev, "Device doesn't support WoL\n");
653 		return -EINVAL;
654 	}
655 
656 	rc = edev->ops->common->update_wol(edev->cdev, wol_requested);
657 	if (!rc)
658 		edev->wol_enabled = wol_requested;
659 
660 	return rc;
661 }
662 
663 static u32 qede_get_msglevel(struct net_device *ndev)
664 {
665 	struct qede_dev *edev = netdev_priv(ndev);
666 
667 	return ((u32)edev->dp_level << QED_LOG_LEVEL_SHIFT) | edev->dp_module;
668 }
669 
670 static void qede_set_msglevel(struct net_device *ndev, u32 level)
671 {
672 	struct qede_dev *edev = netdev_priv(ndev);
673 	u32 dp_module = 0;
674 	u8 dp_level = 0;
675 
676 	qede_config_debug(level, &dp_module, &dp_level);
677 
678 	edev->dp_level = dp_level;
679 	edev->dp_module = dp_module;
680 	edev->ops->common->update_msglvl(edev->cdev,
681 					 dp_module, dp_level);
682 }
683 
684 static int qede_nway_reset(struct net_device *dev)
685 {
686 	struct qede_dev *edev = netdev_priv(dev);
687 	struct qed_link_output current_link;
688 	struct qed_link_params link_params;
689 
690 	if (!edev->ops || !edev->ops->common->can_link_change(edev->cdev)) {
691 		DP_INFO(edev, "Link settings are not allowed to be changed\n");
692 		return -EOPNOTSUPP;
693 	}
694 
695 	if (!netif_running(dev))
696 		return 0;
697 
698 	memset(&current_link, 0, sizeof(current_link));
699 	edev->ops->common->get_link(edev->cdev, &current_link);
700 	if (!current_link.link_up)
701 		return 0;
702 
703 	/* Toggle the link */
704 	memset(&link_params, 0, sizeof(link_params));
705 	link_params.link_up = false;
706 	edev->ops->common->set_link(edev->cdev, &link_params);
707 	link_params.link_up = true;
708 	edev->ops->common->set_link(edev->cdev, &link_params);
709 
710 	return 0;
711 }
712 
713 static u32 qede_get_link(struct net_device *dev)
714 {
715 	struct qede_dev *edev = netdev_priv(dev);
716 	struct qed_link_output current_link;
717 
718 	memset(&current_link, 0, sizeof(current_link));
719 	edev->ops->common->get_link(edev->cdev, &current_link);
720 
721 	return current_link.link_up;
722 }
723 
724 static int qede_flash_device(struct net_device *dev,
725 			     struct ethtool_flash *flash)
726 {
727 	struct qede_dev *edev = netdev_priv(dev);
728 
729 	return edev->ops->common->nvm_flash(edev->cdev, flash->data);
730 }
731 
732 static int qede_get_coalesce(struct net_device *dev,
733 			     struct ethtool_coalesce *coal)
734 {
735 	void *rx_handle = NULL, *tx_handle = NULL;
736 	struct qede_dev *edev = netdev_priv(dev);
737 	u16 rx_coal, tx_coal, i, rc = 0;
738 	struct qede_fastpath *fp;
739 
740 	rx_coal = QED_DEFAULT_RX_USECS;
741 	tx_coal = QED_DEFAULT_TX_USECS;
742 
743 	memset(coal, 0, sizeof(struct ethtool_coalesce));
744 
745 	__qede_lock(edev);
746 	if (edev->state == QEDE_STATE_OPEN) {
747 		for_each_queue(i) {
748 			fp = &edev->fp_array[i];
749 
750 			if (fp->type & QEDE_FASTPATH_RX) {
751 				rx_handle = fp->rxq->handle;
752 				break;
753 			}
754 		}
755 
756 		rc = edev->ops->get_coalesce(edev->cdev, &rx_coal, rx_handle);
757 		if (rc) {
758 			DP_INFO(edev, "Read Rx coalesce error\n");
759 			goto out;
760 		}
761 
762 		for_each_queue(i) {
763 			struct qede_tx_queue *txq;
764 
765 			fp = &edev->fp_array[i];
766 
767 			/* All TX queues of given fastpath uses same
768 			 * coalescing value, so no need to iterate over
769 			 * all TCs, TC0 txq should suffice.
770 			 */
771 			if (fp->type & QEDE_FASTPATH_TX) {
772 				txq = QEDE_FP_TC0_TXQ(fp);
773 				tx_handle = txq->handle;
774 				break;
775 			}
776 		}
777 
778 		rc = edev->ops->get_coalesce(edev->cdev, &tx_coal, tx_handle);
779 		if (rc)
780 			DP_INFO(edev, "Read Tx coalesce error\n");
781 	}
782 
783 out:
784 	__qede_unlock(edev);
785 
786 	coal->rx_coalesce_usecs = rx_coal;
787 	coal->tx_coalesce_usecs = tx_coal;
788 
789 	return rc;
790 }
791 
792 static int qede_set_coalesce(struct net_device *dev,
793 			     struct ethtool_coalesce *coal)
794 {
795 	struct qede_dev *edev = netdev_priv(dev);
796 	struct qede_fastpath *fp;
797 	int i, rc = 0;
798 	u16 rxc, txc;
799 
800 	if (!netif_running(dev)) {
801 		DP_INFO(edev, "Interface is down\n");
802 		return -EINVAL;
803 	}
804 
805 	if (coal->rx_coalesce_usecs > QED_COALESCE_MAX ||
806 	    coal->tx_coalesce_usecs > QED_COALESCE_MAX) {
807 		DP_INFO(edev,
808 			"Can't support requested %s coalesce value [max supported value %d]\n",
809 			coal->rx_coalesce_usecs > QED_COALESCE_MAX ? "rx" :
810 			"tx", QED_COALESCE_MAX);
811 		return -EINVAL;
812 	}
813 
814 	rxc = (u16)coal->rx_coalesce_usecs;
815 	txc = (u16)coal->tx_coalesce_usecs;
816 	for_each_queue(i) {
817 		fp = &edev->fp_array[i];
818 
819 		if (edev->fp_array[i].type & QEDE_FASTPATH_RX) {
820 			rc = edev->ops->common->set_coalesce(edev->cdev,
821 							     rxc, 0,
822 							     fp->rxq->handle);
823 			if (rc) {
824 				DP_INFO(edev,
825 					"Set RX coalesce error, rc = %d\n", rc);
826 				return rc;
827 			}
828 		}
829 
830 		if (edev->fp_array[i].type & QEDE_FASTPATH_TX) {
831 			struct qede_tx_queue *txq;
832 
833 			/* All TX queues of given fastpath uses same
834 			 * coalescing value, so no need to iterate over
835 			 * all TCs, TC0 txq should suffice.
836 			 */
837 			txq = QEDE_FP_TC0_TXQ(fp);
838 
839 			rc = edev->ops->common->set_coalesce(edev->cdev,
840 							     0, txc,
841 							     txq->handle);
842 			if (rc) {
843 				DP_INFO(edev,
844 					"Set TX coalesce error, rc = %d\n", rc);
845 				return rc;
846 			}
847 		}
848 	}
849 
850 	return rc;
851 }
852 
853 static void qede_get_ringparam(struct net_device *dev,
854 			       struct ethtool_ringparam *ering)
855 {
856 	struct qede_dev *edev = netdev_priv(dev);
857 
858 	ering->rx_max_pending = NUM_RX_BDS_MAX;
859 	ering->rx_pending = edev->q_num_rx_buffers;
860 	ering->tx_max_pending = NUM_TX_BDS_MAX;
861 	ering->tx_pending = edev->q_num_tx_buffers;
862 }
863 
864 static int qede_set_ringparam(struct net_device *dev,
865 			      struct ethtool_ringparam *ering)
866 {
867 	struct qede_dev *edev = netdev_priv(dev);
868 
869 	DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
870 		   "Set ring params command parameters: rx_pending = %d, tx_pending = %d\n",
871 		   ering->rx_pending, ering->tx_pending);
872 
873 	/* Validate legality of configuration */
874 	if (ering->rx_pending > NUM_RX_BDS_MAX ||
875 	    ering->rx_pending < NUM_RX_BDS_MIN ||
876 	    ering->tx_pending > NUM_TX_BDS_MAX ||
877 	    ering->tx_pending < NUM_TX_BDS_MIN) {
878 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
879 			   "Can only support Rx Buffer size [0%08x,...,0x%08x] and Tx Buffer size [0x%08x,...,0x%08x]\n",
880 			   NUM_RX_BDS_MIN, NUM_RX_BDS_MAX,
881 			   NUM_TX_BDS_MIN, NUM_TX_BDS_MAX);
882 		return -EINVAL;
883 	}
884 
885 	/* Change ring size and re-load */
886 	edev->q_num_rx_buffers = ering->rx_pending;
887 	edev->q_num_tx_buffers = ering->tx_pending;
888 
889 	qede_reload(edev, NULL, false);
890 
891 	return 0;
892 }
893 
894 static void qede_get_pauseparam(struct net_device *dev,
895 				struct ethtool_pauseparam *epause)
896 {
897 	struct qede_dev *edev = netdev_priv(dev);
898 	struct qed_link_output current_link;
899 
900 	memset(&current_link, 0, sizeof(current_link));
901 	edev->ops->common->get_link(edev->cdev, &current_link);
902 
903 	if (current_link.pause_config & QED_LINK_PAUSE_AUTONEG_ENABLE)
904 		epause->autoneg = true;
905 	if (current_link.pause_config & QED_LINK_PAUSE_RX_ENABLE)
906 		epause->rx_pause = true;
907 	if (current_link.pause_config & QED_LINK_PAUSE_TX_ENABLE)
908 		epause->tx_pause = true;
909 
910 	DP_VERBOSE(edev, QED_MSG_DEBUG,
911 		   "ethtool_pauseparam: cmd %d  autoneg %d  rx_pause %d  tx_pause %d\n",
912 		   epause->cmd, epause->autoneg, epause->rx_pause,
913 		   epause->tx_pause);
914 }
915 
916 static int qede_set_pauseparam(struct net_device *dev,
917 			       struct ethtool_pauseparam *epause)
918 {
919 	struct qede_dev *edev = netdev_priv(dev);
920 	struct qed_link_params params;
921 	struct qed_link_output current_link;
922 
923 	if (!edev->ops || !edev->ops->common->can_link_change(edev->cdev)) {
924 		DP_INFO(edev,
925 			"Pause settings are not allowed to be changed\n");
926 		return -EOPNOTSUPP;
927 	}
928 
929 	memset(&current_link, 0, sizeof(current_link));
930 	edev->ops->common->get_link(edev->cdev, &current_link);
931 
932 	memset(&params, 0, sizeof(params));
933 	params.override_flags |= QED_LINK_OVERRIDE_PAUSE_CONFIG;
934 	if (epause->autoneg) {
935 		if (!(current_link.supported_caps & QED_LM_Autoneg_BIT)) {
936 			DP_INFO(edev, "autoneg not supported\n");
937 			return -EINVAL;
938 		}
939 		params.pause_config |= QED_LINK_PAUSE_AUTONEG_ENABLE;
940 	}
941 	if (epause->rx_pause)
942 		params.pause_config |= QED_LINK_PAUSE_RX_ENABLE;
943 	if (epause->tx_pause)
944 		params.pause_config |= QED_LINK_PAUSE_TX_ENABLE;
945 
946 	params.link_up = true;
947 	edev->ops->common->set_link(edev->cdev, &params);
948 
949 	return 0;
950 }
951 
952 static void qede_get_regs(struct net_device *ndev,
953 			  struct ethtool_regs *regs, void *buffer)
954 {
955 	struct qede_dev *edev = netdev_priv(ndev);
956 
957 	regs->version = 0;
958 	memset(buffer, 0, regs->len);
959 
960 	if (edev->ops && edev->ops->common)
961 		edev->ops->common->dbg_all_data(edev->cdev, buffer);
962 }
963 
964 static int qede_get_regs_len(struct net_device *ndev)
965 {
966 	struct qede_dev *edev = netdev_priv(ndev);
967 
968 	if (edev->ops && edev->ops->common)
969 		return edev->ops->common->dbg_all_data_size(edev->cdev);
970 	else
971 		return -EINVAL;
972 }
973 
974 static void qede_update_mtu(struct qede_dev *edev,
975 			    struct qede_reload_args *args)
976 {
977 	edev->ndev->mtu = args->u.mtu;
978 }
979 
980 /* Netdevice NDOs */
981 int qede_change_mtu(struct net_device *ndev, int new_mtu)
982 {
983 	struct qede_dev *edev = netdev_priv(ndev);
984 	struct qede_reload_args args;
985 
986 	DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
987 		   "Configuring MTU size of %d\n", new_mtu);
988 
989 	if (new_mtu > PAGE_SIZE)
990 		ndev->features &= ~NETIF_F_GRO_HW;
991 
992 	/* Set the mtu field and re-start the interface if needed */
993 	args.u.mtu = new_mtu;
994 	args.func = &qede_update_mtu;
995 	qede_reload(edev, &args, false);
996 
997 	edev->ops->common->update_mtu(edev->cdev, new_mtu);
998 
999 	return 0;
1000 }
1001 
1002 static void qede_get_channels(struct net_device *dev,
1003 			      struct ethtool_channels *channels)
1004 {
1005 	struct qede_dev *edev = netdev_priv(dev);
1006 
1007 	channels->max_combined = QEDE_MAX_RSS_CNT(edev);
1008 	channels->max_rx = QEDE_MAX_RSS_CNT(edev);
1009 	channels->max_tx = QEDE_MAX_RSS_CNT(edev);
1010 	channels->combined_count = QEDE_QUEUE_CNT(edev) - edev->fp_num_tx -
1011 					edev->fp_num_rx;
1012 	channels->tx_count = edev->fp_num_tx;
1013 	channels->rx_count = edev->fp_num_rx;
1014 }
1015 
1016 static int qede_set_channels(struct net_device *dev,
1017 			     struct ethtool_channels *channels)
1018 {
1019 	struct qede_dev *edev = netdev_priv(dev);
1020 	u32 count;
1021 
1022 	DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
1023 		   "set-channels command parameters: rx = %d, tx = %d, other = %d, combined = %d\n",
1024 		   channels->rx_count, channels->tx_count,
1025 		   channels->other_count, channels->combined_count);
1026 
1027 	count = channels->rx_count + channels->tx_count +
1028 			channels->combined_count;
1029 
1030 	/* We don't support `other' channels */
1031 	if (channels->other_count) {
1032 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
1033 			   "command parameters not supported\n");
1034 		return -EINVAL;
1035 	}
1036 
1037 	if (!(channels->combined_count || (channels->rx_count &&
1038 					   channels->tx_count))) {
1039 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
1040 			   "need to request at least one transmit and one receive channel\n");
1041 		return -EINVAL;
1042 	}
1043 
1044 	if (count > QEDE_MAX_RSS_CNT(edev)) {
1045 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
1046 			   "requested channels = %d max supported channels = %d\n",
1047 			   count, QEDE_MAX_RSS_CNT(edev));
1048 		return -EINVAL;
1049 	}
1050 
1051 	/* Check if there was a change in the active parameters */
1052 	if ((count == QEDE_QUEUE_CNT(edev)) &&
1053 	    (channels->tx_count == edev->fp_num_tx) &&
1054 	    (channels->rx_count == edev->fp_num_rx)) {
1055 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
1056 			   "No change in active parameters\n");
1057 		return 0;
1058 	}
1059 
1060 	/* We need the number of queues to be divisible between the hwfns */
1061 	if ((count % edev->dev_info.common.num_hwfns) ||
1062 	    (channels->tx_count % edev->dev_info.common.num_hwfns) ||
1063 	    (channels->rx_count % edev->dev_info.common.num_hwfns)) {
1064 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
1065 			   "Number of channels must be divisible by %04x\n",
1066 			   edev->dev_info.common.num_hwfns);
1067 		return -EINVAL;
1068 	}
1069 
1070 	/* Set number of queues and reload if necessary */
1071 	edev->req_queues = count;
1072 	edev->req_num_tx = channels->tx_count;
1073 	edev->req_num_rx = channels->rx_count;
1074 	/* Reset the indirection table if rx queue count is updated */
1075 	if ((edev->req_queues - edev->req_num_tx) != QEDE_RSS_COUNT(edev)) {
1076 		edev->rss_params_inited &= ~QEDE_RSS_INDIR_INITED;
1077 		memset(edev->rss_ind_table, 0, sizeof(edev->rss_ind_table));
1078 	}
1079 
1080 	qede_reload(edev, NULL, false);
1081 
1082 	return 0;
1083 }
1084 
1085 static int qede_get_ts_info(struct net_device *dev,
1086 			    struct ethtool_ts_info *info)
1087 {
1088 	struct qede_dev *edev = netdev_priv(dev);
1089 
1090 	return qede_ptp_get_ts_info(edev, info);
1091 }
1092 
1093 static int qede_set_phys_id(struct net_device *dev,
1094 			    enum ethtool_phys_id_state state)
1095 {
1096 	struct qede_dev *edev = netdev_priv(dev);
1097 	u8 led_state = 0;
1098 
1099 	switch (state) {
1100 	case ETHTOOL_ID_ACTIVE:
1101 		return 1;	/* cycle on/off once per second */
1102 
1103 	case ETHTOOL_ID_ON:
1104 		led_state = QED_LED_MODE_ON;
1105 		break;
1106 
1107 	case ETHTOOL_ID_OFF:
1108 		led_state = QED_LED_MODE_OFF;
1109 		break;
1110 
1111 	case ETHTOOL_ID_INACTIVE:
1112 		led_state = QED_LED_MODE_RESTORE;
1113 		break;
1114 	}
1115 
1116 	edev->ops->common->set_led(edev->cdev, led_state);
1117 
1118 	return 0;
1119 }
1120 
1121 static int qede_get_rss_flags(struct qede_dev *edev, struct ethtool_rxnfc *info)
1122 {
1123 	info->data = RXH_IP_SRC | RXH_IP_DST;
1124 
1125 	switch (info->flow_type) {
1126 	case TCP_V4_FLOW:
1127 	case TCP_V6_FLOW:
1128 		info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1129 		break;
1130 	case UDP_V4_FLOW:
1131 		if (edev->rss_caps & QED_RSS_IPV4_UDP)
1132 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1133 		break;
1134 	case UDP_V6_FLOW:
1135 		if (edev->rss_caps & QED_RSS_IPV6_UDP)
1136 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1137 		break;
1138 	case IPV4_FLOW:
1139 	case IPV6_FLOW:
1140 		break;
1141 	default:
1142 		info->data = 0;
1143 		break;
1144 	}
1145 
1146 	return 0;
1147 }
1148 
1149 static int qede_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1150 			  u32 *rule_locs)
1151 {
1152 	struct qede_dev *edev = netdev_priv(dev);
1153 	int rc = 0;
1154 
1155 	switch (info->cmd) {
1156 	case ETHTOOL_GRXRINGS:
1157 		info->data = QEDE_RSS_COUNT(edev);
1158 		break;
1159 	case ETHTOOL_GRXFH:
1160 		rc = qede_get_rss_flags(edev, info);
1161 		break;
1162 	case ETHTOOL_GRXCLSRLCNT:
1163 		info->rule_cnt = qede_get_arfs_filter_count(edev);
1164 		info->data = QEDE_RFS_MAX_FLTR;
1165 		break;
1166 	case ETHTOOL_GRXCLSRULE:
1167 		rc = qede_get_cls_rule_entry(edev, info);
1168 		break;
1169 	case ETHTOOL_GRXCLSRLALL:
1170 		rc = qede_get_cls_rule_all(edev, info, rule_locs);
1171 		break;
1172 	default:
1173 		DP_ERR(edev, "Command parameters not supported\n");
1174 		rc = -EOPNOTSUPP;
1175 	}
1176 
1177 	return rc;
1178 }
1179 
1180 static int qede_set_rss_flags(struct qede_dev *edev, struct ethtool_rxnfc *info)
1181 {
1182 	struct qed_update_vport_params *vport_update_params;
1183 	u8 set_caps = 0, clr_caps = 0;
1184 	int rc = 0;
1185 
1186 	DP_VERBOSE(edev, QED_MSG_DEBUG,
1187 		   "Set rss flags command parameters: flow type = %d, data = %llu\n",
1188 		   info->flow_type, info->data);
1189 
1190 	switch (info->flow_type) {
1191 	case TCP_V4_FLOW:
1192 	case TCP_V6_FLOW:
1193 		/* For TCP only 4-tuple hash is supported */
1194 		if (info->data ^ (RXH_IP_SRC | RXH_IP_DST |
1195 				  RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1196 			DP_INFO(edev, "Command parameters not supported\n");
1197 			return -EINVAL;
1198 		}
1199 		return 0;
1200 	case UDP_V4_FLOW:
1201 		/* For UDP either 2-tuple hash or 4-tuple hash is supported */
1202 		if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1203 				   RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1204 			set_caps = QED_RSS_IPV4_UDP;
1205 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1206 				   "UDP 4-tuple enabled\n");
1207 		} else if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1208 			clr_caps = QED_RSS_IPV4_UDP;
1209 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1210 				   "UDP 4-tuple disabled\n");
1211 		} else {
1212 			return -EINVAL;
1213 		}
1214 		break;
1215 	case UDP_V6_FLOW:
1216 		/* For UDP either 2-tuple hash or 4-tuple hash is supported */
1217 		if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1218 				   RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1219 			set_caps = QED_RSS_IPV6_UDP;
1220 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1221 				   "UDP 4-tuple enabled\n");
1222 		} else if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1223 			clr_caps = QED_RSS_IPV6_UDP;
1224 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1225 				   "UDP 4-tuple disabled\n");
1226 		} else {
1227 			return -EINVAL;
1228 		}
1229 		break;
1230 	case IPV4_FLOW:
1231 	case IPV6_FLOW:
1232 		/* For IP only 2-tuple hash is supported */
1233 		if (info->data ^ (RXH_IP_SRC | RXH_IP_DST)) {
1234 			DP_INFO(edev, "Command parameters not supported\n");
1235 			return -EINVAL;
1236 		}
1237 		return 0;
1238 	case SCTP_V4_FLOW:
1239 	case AH_ESP_V4_FLOW:
1240 	case AH_V4_FLOW:
1241 	case ESP_V4_FLOW:
1242 	case SCTP_V6_FLOW:
1243 	case AH_ESP_V6_FLOW:
1244 	case AH_V6_FLOW:
1245 	case ESP_V6_FLOW:
1246 	case IP_USER_FLOW:
1247 	case ETHER_FLOW:
1248 		/* RSS is not supported for these protocols */
1249 		if (info->data) {
1250 			DP_INFO(edev, "Command parameters not supported\n");
1251 			return -EINVAL;
1252 		}
1253 		return 0;
1254 	default:
1255 		return -EINVAL;
1256 	}
1257 
1258 	/* No action is needed if there is no change in the rss capability */
1259 	if (edev->rss_caps == ((edev->rss_caps & ~clr_caps) | set_caps))
1260 		return 0;
1261 
1262 	/* Update internal configuration */
1263 	edev->rss_caps = ((edev->rss_caps & ~clr_caps) | set_caps);
1264 	edev->rss_params_inited |= QEDE_RSS_CAPS_INITED;
1265 
1266 	/* Re-configure if possible */
1267 	__qede_lock(edev);
1268 	if (edev->state == QEDE_STATE_OPEN) {
1269 		vport_update_params = vzalloc(sizeof(*vport_update_params));
1270 		if (!vport_update_params) {
1271 			__qede_unlock(edev);
1272 			return -ENOMEM;
1273 		}
1274 		qede_fill_rss_params(edev, &vport_update_params->rss_params,
1275 				     &vport_update_params->update_rss_flg);
1276 		rc = edev->ops->vport_update(edev->cdev, vport_update_params);
1277 		vfree(vport_update_params);
1278 	}
1279 	__qede_unlock(edev);
1280 
1281 	return rc;
1282 }
1283 
1284 static int qede_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
1285 {
1286 	struct qede_dev *edev = netdev_priv(dev);
1287 	int rc;
1288 
1289 	switch (info->cmd) {
1290 	case ETHTOOL_SRXFH:
1291 		rc = qede_set_rss_flags(edev, info);
1292 		break;
1293 	case ETHTOOL_SRXCLSRLINS:
1294 		rc = qede_add_cls_rule(edev, info);
1295 		break;
1296 	case ETHTOOL_SRXCLSRLDEL:
1297 		rc = qede_delete_flow_filter(edev, info->fs.location);
1298 		break;
1299 	default:
1300 		DP_INFO(edev, "Command parameters not supported\n");
1301 		rc = -EOPNOTSUPP;
1302 	}
1303 
1304 	return rc;
1305 }
1306 
1307 static u32 qede_get_rxfh_indir_size(struct net_device *dev)
1308 {
1309 	return QED_RSS_IND_TABLE_SIZE;
1310 }
1311 
1312 static u32 qede_get_rxfh_key_size(struct net_device *dev)
1313 {
1314 	struct qede_dev *edev = netdev_priv(dev);
1315 
1316 	return sizeof(edev->rss_key);
1317 }
1318 
1319 static int qede_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
1320 {
1321 	struct qede_dev *edev = netdev_priv(dev);
1322 	int i;
1323 
1324 	if (hfunc)
1325 		*hfunc = ETH_RSS_HASH_TOP;
1326 
1327 	if (!indir)
1328 		return 0;
1329 
1330 	for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++)
1331 		indir[i] = edev->rss_ind_table[i];
1332 
1333 	if (key)
1334 		memcpy(key, edev->rss_key, qede_get_rxfh_key_size(dev));
1335 
1336 	return 0;
1337 }
1338 
1339 static int qede_set_rxfh(struct net_device *dev, const u32 *indir,
1340 			 const u8 *key, const u8 hfunc)
1341 {
1342 	struct qed_update_vport_params *vport_update_params;
1343 	struct qede_dev *edev = netdev_priv(dev);
1344 	int i, rc = 0;
1345 
1346 	if (edev->dev_info.common.num_hwfns > 1) {
1347 		DP_INFO(edev,
1348 			"RSS configuration is not supported for 100G devices\n");
1349 		return -EOPNOTSUPP;
1350 	}
1351 
1352 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1353 		return -EOPNOTSUPP;
1354 
1355 	if (!indir && !key)
1356 		return 0;
1357 
1358 	if (indir) {
1359 		for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++)
1360 			edev->rss_ind_table[i] = indir[i];
1361 		edev->rss_params_inited |= QEDE_RSS_INDIR_INITED;
1362 	}
1363 
1364 	if (key) {
1365 		memcpy(&edev->rss_key, key, qede_get_rxfh_key_size(dev));
1366 		edev->rss_params_inited |= QEDE_RSS_KEY_INITED;
1367 	}
1368 
1369 	__qede_lock(edev);
1370 	if (edev->state == QEDE_STATE_OPEN) {
1371 		vport_update_params = vzalloc(sizeof(*vport_update_params));
1372 		if (!vport_update_params) {
1373 			__qede_unlock(edev);
1374 			return -ENOMEM;
1375 		}
1376 		qede_fill_rss_params(edev, &vport_update_params->rss_params,
1377 				     &vport_update_params->update_rss_flg);
1378 		rc = edev->ops->vport_update(edev->cdev, vport_update_params);
1379 		vfree(vport_update_params);
1380 	}
1381 	__qede_unlock(edev);
1382 
1383 	return rc;
1384 }
1385 
1386 /* This function enables the interrupt generation and the NAPI on the device */
1387 static void qede_netif_start(struct qede_dev *edev)
1388 {
1389 	int i;
1390 
1391 	if (!netif_running(edev->ndev))
1392 		return;
1393 
1394 	for_each_queue(i) {
1395 		/* Update and reenable interrupts */
1396 		qed_sb_ack(edev->fp_array[i].sb_info, IGU_INT_ENABLE, 1);
1397 		napi_enable(&edev->fp_array[i].napi);
1398 	}
1399 }
1400 
1401 /* This function disables the NAPI and the interrupt generation on the device */
1402 static void qede_netif_stop(struct qede_dev *edev)
1403 {
1404 	int i;
1405 
1406 	for_each_queue(i) {
1407 		napi_disable(&edev->fp_array[i].napi);
1408 		/* Disable interrupts */
1409 		qed_sb_ack(edev->fp_array[i].sb_info, IGU_INT_DISABLE, 0);
1410 	}
1411 }
1412 
1413 static int qede_selftest_transmit_traffic(struct qede_dev *edev,
1414 					  struct sk_buff *skb)
1415 {
1416 	struct qede_tx_queue *txq = NULL;
1417 	struct eth_tx_1st_bd *first_bd;
1418 	dma_addr_t mapping;
1419 	int i, idx;
1420 	u16 val;
1421 
1422 	for_each_queue(i) {
1423 		struct qede_fastpath *fp = &edev->fp_array[i];
1424 
1425 		if (fp->type & QEDE_FASTPATH_TX) {
1426 			txq = QEDE_FP_TC0_TXQ(fp);
1427 			break;
1428 		}
1429 	}
1430 
1431 	if (!txq) {
1432 		DP_NOTICE(edev, "Tx path is not available\n");
1433 		return -1;
1434 	}
1435 
1436 	/* Fill the entry in the SW ring and the BDs in the FW ring */
1437 	idx = txq->sw_tx_prod;
1438 	txq->sw_tx_ring.skbs[idx].skb = skb;
1439 	first_bd = qed_chain_produce(&txq->tx_pbl);
1440 	memset(first_bd, 0, sizeof(*first_bd));
1441 	val = 1 << ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT;
1442 	first_bd->data.bd_flags.bitfields = val;
1443 	val = skb->len & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK;
1444 	val = val << ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT;
1445 	first_bd->data.bitfields |= cpu_to_le16(val);
1446 
1447 	/* Map skb linear data for DMA and set in the first BD */
1448 	mapping = dma_map_single(&edev->pdev->dev, skb->data,
1449 				 skb_headlen(skb), DMA_TO_DEVICE);
1450 	if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
1451 		DP_NOTICE(edev, "SKB mapping failed\n");
1452 		return -ENOMEM;
1453 	}
1454 	BD_SET_UNMAP_ADDR_LEN(first_bd, mapping, skb_headlen(skb));
1455 
1456 	/* update the first BD with the actual num BDs */
1457 	first_bd->data.nbds = 1;
1458 	txq->sw_tx_prod = (txq->sw_tx_prod + 1) % txq->num_tx_buffers;
1459 	/* 'next page' entries are counted in the producer value */
1460 	val = qed_chain_get_prod_idx(&txq->tx_pbl);
1461 	txq->tx_db.data.bd_prod = cpu_to_le16(val);
1462 
1463 	/* wmb makes sure that the BDs data is updated before updating the
1464 	 * producer, otherwise FW may read old data from the BDs.
1465 	 */
1466 	wmb();
1467 	barrier();
1468 	writel(txq->tx_db.raw, txq->doorbell_addr);
1469 
1470 	/* mmiowb is needed to synchronize doorbell writes from more than one
1471 	 * processor. It guarantees that the write arrives to the device before
1472 	 * the queue lock is released and another start_xmit is called (possibly
1473 	 * on another CPU). Without this barrier, the next doorbell can bypass
1474 	 * this doorbell. This is applicable to IA64/Altix systems.
1475 	 */
1476 	mmiowb();
1477 
1478 	for (i = 0; i < QEDE_SELFTEST_POLL_COUNT; i++) {
1479 		if (qede_txq_has_work(txq))
1480 			break;
1481 		usleep_range(100, 200);
1482 	}
1483 
1484 	if (!qede_txq_has_work(txq)) {
1485 		DP_NOTICE(edev, "Tx completion didn't happen\n");
1486 		return -1;
1487 	}
1488 
1489 	first_bd = (struct eth_tx_1st_bd *)qed_chain_consume(&txq->tx_pbl);
1490 	dma_unmap_single(&edev->pdev->dev, BD_UNMAP_ADDR(first_bd),
1491 			 BD_UNMAP_LEN(first_bd), DMA_TO_DEVICE);
1492 	txq->sw_tx_cons = (txq->sw_tx_cons + 1) % txq->num_tx_buffers;
1493 	txq->sw_tx_ring.skbs[idx].skb = NULL;
1494 
1495 	return 0;
1496 }
1497 
1498 static int qede_selftest_receive_traffic(struct qede_dev *edev)
1499 {
1500 	u16 hw_comp_cons, sw_comp_cons, sw_rx_index, len;
1501 	struct eth_fast_path_rx_reg_cqe *fp_cqe;
1502 	struct qede_rx_queue *rxq = NULL;
1503 	struct sw_rx_data *sw_rx_data;
1504 	union eth_rx_cqe *cqe;
1505 	int i, iter, rc = 0;
1506 	u8 *data_ptr;
1507 
1508 	for_each_queue(i) {
1509 		if (edev->fp_array[i].type & QEDE_FASTPATH_RX) {
1510 			rxq = edev->fp_array[i].rxq;
1511 			break;
1512 		}
1513 	}
1514 
1515 	if (!rxq) {
1516 		DP_NOTICE(edev, "Rx path is not available\n");
1517 		return -1;
1518 	}
1519 
1520 	/* The packet is expected to receive on rx-queue 0 even though RSS is
1521 	 * enabled. This is because the queue 0 is configured as the default
1522 	 * queue and that the loopback traffic is not IP.
1523 	 */
1524 	for (iter = 0; iter < QEDE_SELFTEST_POLL_COUNT; iter++) {
1525 		if (!qede_has_rx_work(rxq)) {
1526 			usleep_range(100, 200);
1527 			continue;
1528 		}
1529 
1530 		hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
1531 		sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1532 
1533 		/* Memory barrier to prevent the CPU from doing speculative
1534 		 * reads of CQE/BD before reading hw_comp_cons. If the CQE is
1535 		 * read before it is written by FW, then FW writes CQE and SB,
1536 		 * and then the CPU reads the hw_comp_cons, it will use an old
1537 		 * CQE.
1538 		 */
1539 		rmb();
1540 
1541 		/* Get the CQE from the completion ring */
1542 		cqe = (union eth_rx_cqe *)qed_chain_consume(&rxq->rx_comp_ring);
1543 
1544 		/* Get the data from the SW ring */
1545 		sw_rx_index = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1546 		sw_rx_data = &rxq->sw_rx_ring[sw_rx_index];
1547 		fp_cqe = &cqe->fast_path_regular;
1548 		len =  le16_to_cpu(fp_cqe->len_on_first_bd);
1549 		data_ptr = (u8 *)(page_address(sw_rx_data->data) +
1550 				  fp_cqe->placement_offset +
1551 				  sw_rx_data->page_offset +
1552 				  rxq->rx_headroom);
1553 		if (ether_addr_equal(data_ptr,  edev->ndev->dev_addr) &&
1554 		    ether_addr_equal(data_ptr + ETH_ALEN,
1555 				     edev->ndev->dev_addr)) {
1556 			for (i = ETH_HLEN; i < len; i++)
1557 				if (data_ptr[i] != (unsigned char)(i & 0xff)) {
1558 					rc = -1;
1559 					break;
1560 				}
1561 
1562 			qede_recycle_rx_bd_ring(rxq, 1);
1563 			qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1564 			break;
1565 		}
1566 
1567 		DP_INFO(edev, "Not the transmitted packet\n");
1568 		qede_recycle_rx_bd_ring(rxq, 1);
1569 		qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1570 	}
1571 
1572 	if (iter == QEDE_SELFTEST_POLL_COUNT) {
1573 		DP_NOTICE(edev, "Failed to receive the traffic\n");
1574 		return -1;
1575 	}
1576 
1577 	qede_update_rx_prod(edev, rxq);
1578 
1579 	return rc;
1580 }
1581 
1582 static int qede_selftest_run_loopback(struct qede_dev *edev, u32 loopback_mode)
1583 {
1584 	struct qed_link_params link_params;
1585 	struct sk_buff *skb = NULL;
1586 	int rc = 0, i;
1587 	u32 pkt_size;
1588 	u8 *packet;
1589 
1590 	if (!netif_running(edev->ndev)) {
1591 		DP_NOTICE(edev, "Interface is down\n");
1592 		return -EINVAL;
1593 	}
1594 
1595 	qede_netif_stop(edev);
1596 
1597 	/* Bring up the link in Loopback mode */
1598 	memset(&link_params, 0, sizeof(link_params));
1599 	link_params.link_up = true;
1600 	link_params.override_flags = QED_LINK_OVERRIDE_LOOPBACK_MODE;
1601 	link_params.loopback_mode = loopback_mode;
1602 	edev->ops->common->set_link(edev->cdev, &link_params);
1603 
1604 	/* Wait for loopback configuration to apply */
1605 	msleep_interruptible(500);
1606 
1607 	/* prepare the loopback packet */
1608 	pkt_size = edev->ndev->mtu + ETH_HLEN;
1609 
1610 	skb = netdev_alloc_skb(edev->ndev, pkt_size);
1611 	if (!skb) {
1612 		DP_INFO(edev, "Can't allocate skb\n");
1613 		rc = -ENOMEM;
1614 		goto test_loopback_exit;
1615 	}
1616 	packet = skb_put(skb, pkt_size);
1617 	ether_addr_copy(packet, edev->ndev->dev_addr);
1618 	ether_addr_copy(packet + ETH_ALEN, edev->ndev->dev_addr);
1619 	memset(packet + (2 * ETH_ALEN), 0x77, (ETH_HLEN - (2 * ETH_ALEN)));
1620 	for (i = ETH_HLEN; i < pkt_size; i++)
1621 		packet[i] = (unsigned char)(i & 0xff);
1622 
1623 	rc = qede_selftest_transmit_traffic(edev, skb);
1624 	if (rc)
1625 		goto test_loopback_exit;
1626 
1627 	rc = qede_selftest_receive_traffic(edev);
1628 	if (rc)
1629 		goto test_loopback_exit;
1630 
1631 	DP_VERBOSE(edev, NETIF_MSG_RX_STATUS, "Loopback test successful\n");
1632 
1633 test_loopback_exit:
1634 	dev_kfree_skb(skb);
1635 
1636 	/* Bring up the link in Normal mode */
1637 	memset(&link_params, 0, sizeof(link_params));
1638 	link_params.link_up = true;
1639 	link_params.override_flags = QED_LINK_OVERRIDE_LOOPBACK_MODE;
1640 	link_params.loopback_mode = QED_LINK_LOOPBACK_NONE;
1641 	edev->ops->common->set_link(edev->cdev, &link_params);
1642 
1643 	/* Wait for loopback configuration to apply */
1644 	msleep_interruptible(500);
1645 
1646 	qede_netif_start(edev);
1647 
1648 	return rc;
1649 }
1650 
1651 static void qede_self_test(struct net_device *dev,
1652 			   struct ethtool_test *etest, u64 *buf)
1653 {
1654 	struct qede_dev *edev = netdev_priv(dev);
1655 
1656 	DP_VERBOSE(edev, QED_MSG_DEBUG,
1657 		   "Self-test command parameters: offline = %d, external_lb = %d\n",
1658 		   (etest->flags & ETH_TEST_FL_OFFLINE),
1659 		   (etest->flags & ETH_TEST_FL_EXTERNAL_LB) >> 2);
1660 
1661 	memset(buf, 0, sizeof(u64) * QEDE_ETHTOOL_TEST_MAX);
1662 
1663 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
1664 		if (qede_selftest_run_loopback(edev,
1665 					       QED_LINK_LOOPBACK_INT_PHY)) {
1666 			buf[QEDE_ETHTOOL_INT_LOOPBACK] = 1;
1667 			etest->flags |= ETH_TEST_FL_FAILED;
1668 		}
1669 	}
1670 
1671 	if (edev->ops->common->selftest->selftest_interrupt(edev->cdev)) {
1672 		buf[QEDE_ETHTOOL_INTERRUPT_TEST] = 1;
1673 		etest->flags |= ETH_TEST_FL_FAILED;
1674 	}
1675 
1676 	if (edev->ops->common->selftest->selftest_memory(edev->cdev)) {
1677 		buf[QEDE_ETHTOOL_MEMORY_TEST] = 1;
1678 		etest->flags |= ETH_TEST_FL_FAILED;
1679 	}
1680 
1681 	if (edev->ops->common->selftest->selftest_register(edev->cdev)) {
1682 		buf[QEDE_ETHTOOL_REGISTER_TEST] = 1;
1683 		etest->flags |= ETH_TEST_FL_FAILED;
1684 	}
1685 
1686 	if (edev->ops->common->selftest->selftest_clock(edev->cdev)) {
1687 		buf[QEDE_ETHTOOL_CLOCK_TEST] = 1;
1688 		etest->flags |= ETH_TEST_FL_FAILED;
1689 	}
1690 
1691 	if (edev->ops->common->selftest->selftest_nvram(edev->cdev)) {
1692 		buf[QEDE_ETHTOOL_NVRAM_TEST] = 1;
1693 		etest->flags |= ETH_TEST_FL_FAILED;
1694 	}
1695 }
1696 
1697 static int qede_set_tunable(struct net_device *dev,
1698 			    const struct ethtool_tunable *tuna,
1699 			    const void *data)
1700 {
1701 	struct qede_dev *edev = netdev_priv(dev);
1702 	u32 val;
1703 
1704 	switch (tuna->id) {
1705 	case ETHTOOL_RX_COPYBREAK:
1706 		val = *(u32 *)data;
1707 		if (val < QEDE_MIN_PKT_LEN || val > QEDE_RX_HDR_SIZE) {
1708 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1709 				   "Invalid rx copy break value, range is [%u, %u]",
1710 				   QEDE_MIN_PKT_LEN, QEDE_RX_HDR_SIZE);
1711 			return -EINVAL;
1712 		}
1713 
1714 		edev->rx_copybreak = *(u32 *)data;
1715 		break;
1716 	default:
1717 		return -EOPNOTSUPP;
1718 	}
1719 
1720 	return 0;
1721 }
1722 
1723 static int qede_get_tunable(struct net_device *dev,
1724 			    const struct ethtool_tunable *tuna, void *data)
1725 {
1726 	struct qede_dev *edev = netdev_priv(dev);
1727 
1728 	switch (tuna->id) {
1729 	case ETHTOOL_RX_COPYBREAK:
1730 		*(u32 *)data = edev->rx_copybreak;
1731 		break;
1732 	default:
1733 		return -EOPNOTSUPP;
1734 	}
1735 
1736 	return 0;
1737 }
1738 
1739 static int qede_get_eee(struct net_device *dev, struct ethtool_eee *edata)
1740 {
1741 	struct qede_dev *edev = netdev_priv(dev);
1742 	struct qed_link_output current_link;
1743 
1744 	memset(&current_link, 0, sizeof(current_link));
1745 	edev->ops->common->get_link(edev->cdev, &current_link);
1746 
1747 	if (!current_link.eee_supported) {
1748 		DP_INFO(edev, "EEE is not supported\n");
1749 		return -EOPNOTSUPP;
1750 	}
1751 
1752 	if (current_link.eee.adv_caps & QED_EEE_1G_ADV)
1753 		edata->advertised = ADVERTISED_1000baseT_Full;
1754 	if (current_link.eee.adv_caps & QED_EEE_10G_ADV)
1755 		edata->advertised |= ADVERTISED_10000baseT_Full;
1756 	if (current_link.sup_caps & QED_EEE_1G_ADV)
1757 		edata->supported = ADVERTISED_1000baseT_Full;
1758 	if (current_link.sup_caps & QED_EEE_10G_ADV)
1759 		edata->supported |= ADVERTISED_10000baseT_Full;
1760 	if (current_link.eee.lp_adv_caps & QED_EEE_1G_ADV)
1761 		edata->lp_advertised = ADVERTISED_1000baseT_Full;
1762 	if (current_link.eee.lp_adv_caps & QED_EEE_10G_ADV)
1763 		edata->lp_advertised |= ADVERTISED_10000baseT_Full;
1764 
1765 	edata->tx_lpi_timer = current_link.eee.tx_lpi_timer;
1766 	edata->eee_enabled = current_link.eee.enable;
1767 	edata->tx_lpi_enabled = current_link.eee.tx_lpi_enable;
1768 	edata->eee_active = current_link.eee_active;
1769 
1770 	return 0;
1771 }
1772 
1773 static int qede_set_eee(struct net_device *dev, struct ethtool_eee *edata)
1774 {
1775 	struct qede_dev *edev = netdev_priv(dev);
1776 	struct qed_link_output current_link;
1777 	struct qed_link_params params;
1778 
1779 	if (!edev->ops->common->can_link_change(edev->cdev)) {
1780 		DP_INFO(edev, "Link settings are not allowed to be changed\n");
1781 		return -EOPNOTSUPP;
1782 	}
1783 
1784 	memset(&current_link, 0, sizeof(current_link));
1785 	edev->ops->common->get_link(edev->cdev, &current_link);
1786 
1787 	if (!current_link.eee_supported) {
1788 		DP_INFO(edev, "EEE is not supported\n");
1789 		return -EOPNOTSUPP;
1790 	}
1791 
1792 	memset(&params, 0, sizeof(params));
1793 	params.override_flags |= QED_LINK_OVERRIDE_EEE_CONFIG;
1794 
1795 	if (!(edata->advertised & (ADVERTISED_1000baseT_Full |
1796 				   ADVERTISED_10000baseT_Full)) ||
1797 	    ((edata->advertised & (ADVERTISED_1000baseT_Full |
1798 				   ADVERTISED_10000baseT_Full)) !=
1799 	     edata->advertised)) {
1800 		DP_VERBOSE(edev, QED_MSG_DEBUG,
1801 			   "Invalid advertised capabilities %d\n",
1802 			   edata->advertised);
1803 		return -EINVAL;
1804 	}
1805 
1806 	if (edata->advertised & ADVERTISED_1000baseT_Full)
1807 		params.eee.adv_caps = QED_EEE_1G_ADV;
1808 	if (edata->advertised & ADVERTISED_10000baseT_Full)
1809 		params.eee.adv_caps |= QED_EEE_10G_ADV;
1810 	params.eee.enable = edata->eee_enabled;
1811 	params.eee.tx_lpi_enable = edata->tx_lpi_enabled;
1812 	params.eee.tx_lpi_timer = edata->tx_lpi_timer;
1813 
1814 	params.link_up = true;
1815 	edev->ops->common->set_link(edev->cdev, &params);
1816 
1817 	return 0;
1818 }
1819 
1820 static int qede_get_module_info(struct net_device *dev,
1821 				struct ethtool_modinfo *modinfo)
1822 {
1823 	struct qede_dev *edev = netdev_priv(dev);
1824 	u8 buf[4];
1825 	int rc;
1826 
1827 	/* Read first 4 bytes to find the sfp type */
1828 	rc = edev->ops->common->read_module_eeprom(edev->cdev, buf,
1829 						   QED_I2C_DEV_ADDR_A0, 0, 4);
1830 	if (rc) {
1831 		DP_ERR(edev, "Failed reading EEPROM data %d\n", rc);
1832 		return rc;
1833 	}
1834 
1835 	switch (buf[0]) {
1836 	case 0x3: /* SFP, SFP+, SFP-28 */
1837 		modinfo->type = ETH_MODULE_SFF_8472;
1838 		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1839 		break;
1840 	case 0xc: /* QSFP */
1841 	case 0xd: /* QSFP+ */
1842 		modinfo->type = ETH_MODULE_SFF_8436;
1843 		modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1844 		break;
1845 	case 0x11: /* QSFP-28 */
1846 		modinfo->type = ETH_MODULE_SFF_8636;
1847 		modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1848 		break;
1849 	default:
1850 		DP_ERR(edev, "Unknown transceiver type 0x%x\n", buf[0]);
1851 		return -EINVAL;
1852 	}
1853 
1854 	return 0;
1855 }
1856 
1857 static int qede_get_module_eeprom(struct net_device *dev,
1858 				  struct ethtool_eeprom *ee, u8 *data)
1859 {
1860 	struct qede_dev *edev = netdev_priv(dev);
1861 	u32 start_addr = ee->offset, size = 0;
1862 	u8 *buf = data;
1863 	int rc = 0;
1864 
1865 	/* Read A0 section */
1866 	if (ee->offset < ETH_MODULE_SFF_8079_LEN) {
1867 		/* Limit transfer size to the A0 section boundary */
1868 		if (ee->offset + ee->len > ETH_MODULE_SFF_8079_LEN)
1869 			size = ETH_MODULE_SFF_8079_LEN - ee->offset;
1870 		else
1871 			size = ee->len;
1872 
1873 		rc = edev->ops->common->read_module_eeprom(edev->cdev, buf,
1874 							   QED_I2C_DEV_ADDR_A0,
1875 							   start_addr, size);
1876 		if (rc) {
1877 			DP_ERR(edev, "Failed reading A0 section  %d\n", rc);
1878 			return rc;
1879 		}
1880 
1881 		buf += size;
1882 		start_addr += size;
1883 	}
1884 
1885 	/* Read A2 section */
1886 	if (start_addr >= ETH_MODULE_SFF_8079_LEN &&
1887 	    start_addr < ETH_MODULE_SFF_8472_LEN) {
1888 		size = ee->len - size;
1889 		/* Limit transfer size to the A2 section boundary */
1890 		if (start_addr + size > ETH_MODULE_SFF_8472_LEN)
1891 			size = ETH_MODULE_SFF_8472_LEN - start_addr;
1892 		start_addr -= ETH_MODULE_SFF_8079_LEN;
1893 		rc = edev->ops->common->read_module_eeprom(edev->cdev, buf,
1894 							   QED_I2C_DEV_ADDR_A2,
1895 							   start_addr, size);
1896 		if (rc) {
1897 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1898 				   "Failed reading A2 section %d\n", rc);
1899 			return 0;
1900 		}
1901 	}
1902 
1903 	return rc;
1904 }
1905 
1906 static const struct ethtool_ops qede_ethtool_ops = {
1907 	.get_link_ksettings = qede_get_link_ksettings,
1908 	.set_link_ksettings = qede_set_link_ksettings,
1909 	.get_drvinfo = qede_get_drvinfo,
1910 	.get_regs_len = qede_get_regs_len,
1911 	.get_regs = qede_get_regs,
1912 	.get_wol = qede_get_wol,
1913 	.set_wol = qede_set_wol,
1914 	.get_msglevel = qede_get_msglevel,
1915 	.set_msglevel = qede_set_msglevel,
1916 	.nway_reset = qede_nway_reset,
1917 	.get_link = qede_get_link,
1918 	.get_coalesce = qede_get_coalesce,
1919 	.set_coalesce = qede_set_coalesce,
1920 	.get_ringparam = qede_get_ringparam,
1921 	.set_ringparam = qede_set_ringparam,
1922 	.get_pauseparam = qede_get_pauseparam,
1923 	.set_pauseparam = qede_set_pauseparam,
1924 	.get_strings = qede_get_strings,
1925 	.set_phys_id = qede_set_phys_id,
1926 	.get_ethtool_stats = qede_get_ethtool_stats,
1927 	.get_priv_flags = qede_get_priv_flags,
1928 	.get_sset_count = qede_get_sset_count,
1929 	.get_rxnfc = qede_get_rxnfc,
1930 	.set_rxnfc = qede_set_rxnfc,
1931 	.get_rxfh_indir_size = qede_get_rxfh_indir_size,
1932 	.get_rxfh_key_size = qede_get_rxfh_key_size,
1933 	.get_rxfh = qede_get_rxfh,
1934 	.set_rxfh = qede_set_rxfh,
1935 	.get_ts_info = qede_get_ts_info,
1936 	.get_channels = qede_get_channels,
1937 	.set_channels = qede_set_channels,
1938 	.self_test = qede_self_test,
1939 	.get_module_info = qede_get_module_info,
1940 	.get_module_eeprom = qede_get_module_eeprom,
1941 	.get_eee = qede_get_eee,
1942 	.set_eee = qede_set_eee,
1943 
1944 	.get_tunable = qede_get_tunable,
1945 	.set_tunable = qede_set_tunable,
1946 	.flash_device = qede_flash_device,
1947 };
1948 
1949 static const struct ethtool_ops qede_vf_ethtool_ops = {
1950 	.get_link_ksettings = qede_get_link_ksettings,
1951 	.get_drvinfo = qede_get_drvinfo,
1952 	.get_msglevel = qede_get_msglevel,
1953 	.set_msglevel = qede_set_msglevel,
1954 	.get_link = qede_get_link,
1955 	.get_coalesce = qede_get_coalesce,
1956 	.set_coalesce = qede_set_coalesce,
1957 	.get_ringparam = qede_get_ringparam,
1958 	.set_ringparam = qede_set_ringparam,
1959 	.get_strings = qede_get_strings,
1960 	.get_ethtool_stats = qede_get_ethtool_stats,
1961 	.get_priv_flags = qede_get_priv_flags,
1962 	.get_sset_count = qede_get_sset_count,
1963 	.get_rxnfc = qede_get_rxnfc,
1964 	.set_rxnfc = qede_set_rxnfc,
1965 	.get_rxfh_indir_size = qede_get_rxfh_indir_size,
1966 	.get_rxfh_key_size = qede_get_rxfh_key_size,
1967 	.get_rxfh = qede_get_rxfh,
1968 	.set_rxfh = qede_set_rxfh,
1969 	.get_channels = qede_get_channels,
1970 	.set_channels = qede_set_channels,
1971 	.get_tunable = qede_get_tunable,
1972 	.set_tunable = qede_set_tunable,
1973 };
1974 
1975 void qede_set_ethtool_ops(struct net_device *dev)
1976 {
1977 	struct qede_dev *edev = netdev_priv(dev);
1978 
1979 	if (IS_VF(edev))
1980 		dev->ethtool_ops = &qede_vf_ethtool_ops;
1981 	else
1982 		dev->ethtool_ops = &qede_ethtool_ops;
1983 }
1984