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