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) (offsetof(struct qede_stats, stat_name))
79 #define QEDE_STAT_STRING(stat_name) (#stat_name)
80 #define _QEDE_STAT(stat_name, pf_only) \
81 	 {QEDE_STAT_OFFSET(stat_name), QEDE_STAT_STRING(stat_name), pf_only}
82 #define QEDE_PF_STAT(stat_name)	_QEDE_STAT(stat_name, true)
83 #define QEDE_STAT(stat_name)	_QEDE_STAT(stat_name, false)
84 static const struct {
85 	u64 offset;
86 	char string[ETH_GSTRING_LEN];
87 	bool pf_only;
88 } qede_stats_arr[] = {
89 	QEDE_STAT(rx_ucast_bytes),
90 	QEDE_STAT(rx_mcast_bytes),
91 	QEDE_STAT(rx_bcast_bytes),
92 	QEDE_STAT(rx_ucast_pkts),
93 	QEDE_STAT(rx_mcast_pkts),
94 	QEDE_STAT(rx_bcast_pkts),
95 
96 	QEDE_STAT(tx_ucast_bytes),
97 	QEDE_STAT(tx_mcast_bytes),
98 	QEDE_STAT(tx_bcast_bytes),
99 	QEDE_STAT(tx_ucast_pkts),
100 	QEDE_STAT(tx_mcast_pkts),
101 	QEDE_STAT(tx_bcast_pkts),
102 
103 	QEDE_PF_STAT(rx_64_byte_packets),
104 	QEDE_PF_STAT(rx_65_to_127_byte_packets),
105 	QEDE_PF_STAT(rx_128_to_255_byte_packets),
106 	QEDE_PF_STAT(rx_256_to_511_byte_packets),
107 	QEDE_PF_STAT(rx_512_to_1023_byte_packets),
108 	QEDE_PF_STAT(rx_1024_to_1518_byte_packets),
109 	QEDE_PF_STAT(rx_1519_to_1522_byte_packets),
110 	QEDE_PF_STAT(rx_1519_to_2047_byte_packets),
111 	QEDE_PF_STAT(rx_2048_to_4095_byte_packets),
112 	QEDE_PF_STAT(rx_4096_to_9216_byte_packets),
113 	QEDE_PF_STAT(rx_9217_to_16383_byte_packets),
114 	QEDE_PF_STAT(tx_64_byte_packets),
115 	QEDE_PF_STAT(tx_65_to_127_byte_packets),
116 	QEDE_PF_STAT(tx_128_to_255_byte_packets),
117 	QEDE_PF_STAT(tx_256_to_511_byte_packets),
118 	QEDE_PF_STAT(tx_512_to_1023_byte_packets),
119 	QEDE_PF_STAT(tx_1024_to_1518_byte_packets),
120 	QEDE_PF_STAT(tx_1519_to_2047_byte_packets),
121 	QEDE_PF_STAT(tx_2048_to_4095_byte_packets),
122 	QEDE_PF_STAT(tx_4096_to_9216_byte_packets),
123 	QEDE_PF_STAT(tx_9217_to_16383_byte_packets),
124 
125 	QEDE_PF_STAT(rx_mac_crtl_frames),
126 	QEDE_PF_STAT(tx_mac_ctrl_frames),
127 	QEDE_PF_STAT(rx_pause_frames),
128 	QEDE_PF_STAT(tx_pause_frames),
129 	QEDE_PF_STAT(rx_pfc_frames),
130 	QEDE_PF_STAT(tx_pfc_frames),
131 
132 	QEDE_PF_STAT(rx_crc_errors),
133 	QEDE_PF_STAT(rx_align_errors),
134 	QEDE_PF_STAT(rx_carrier_errors),
135 	QEDE_PF_STAT(rx_oversize_packets),
136 	QEDE_PF_STAT(rx_jabbers),
137 	QEDE_PF_STAT(rx_undersize_packets),
138 	QEDE_PF_STAT(rx_fragments),
139 	QEDE_PF_STAT(tx_lpi_entry_count),
140 	QEDE_PF_STAT(tx_total_collisions),
141 	QEDE_PF_STAT(brb_truncates),
142 	QEDE_PF_STAT(brb_discards),
143 	QEDE_STAT(no_buff_discards),
144 	QEDE_PF_STAT(mftag_filter_discards),
145 	QEDE_PF_STAT(mac_filter_discards),
146 	QEDE_STAT(tx_err_drop_pkts),
147 	QEDE_STAT(ttl0_discard),
148 	QEDE_STAT(packet_too_big_discard),
149 
150 	QEDE_STAT(coalesced_pkts),
151 	QEDE_STAT(coalesced_events),
152 	QEDE_STAT(coalesced_aborts_num),
153 	QEDE_STAT(non_coalesced_pkts),
154 	QEDE_STAT(coalesced_bytes),
155 };
156 
157 #define QEDE_NUM_STATS	ARRAY_SIZE(qede_stats_arr)
158 
159 enum {
160 	QEDE_PRI_FLAG_CMT,
161 	QEDE_PRI_FLAG_LEN,
162 };
163 
164 static const char qede_private_arr[QEDE_PRI_FLAG_LEN][ETH_GSTRING_LEN] = {
165 	"Coupled-Function",
166 };
167 
168 enum qede_ethtool_tests {
169 	QEDE_ETHTOOL_INT_LOOPBACK,
170 	QEDE_ETHTOOL_INTERRUPT_TEST,
171 	QEDE_ETHTOOL_MEMORY_TEST,
172 	QEDE_ETHTOOL_REGISTER_TEST,
173 	QEDE_ETHTOOL_CLOCK_TEST,
174 	QEDE_ETHTOOL_NVRAM_TEST,
175 	QEDE_ETHTOOL_TEST_MAX
176 };
177 
178 static const char qede_tests_str_arr[QEDE_ETHTOOL_TEST_MAX][ETH_GSTRING_LEN] = {
179 	"Internal loopback (offline)",
180 	"Interrupt (online)\t",
181 	"Memory (online)\t\t",
182 	"Register (online)\t",
183 	"Clock (online)\t\t",
184 	"Nvram (online)\t\t",
185 };
186 
187 static void qede_get_strings_stats_txq(struct qede_dev *edev,
188 				       struct qede_tx_queue *txq, u8 **buf)
189 {
190 	int i;
191 
192 	for (i = 0; i < QEDE_NUM_TQSTATS; i++) {
193 		if (txq->is_xdp)
194 			sprintf(*buf, "%d [XDP]: %s",
195 				QEDE_TXQ_XDP_TO_IDX(edev, txq),
196 				qede_tqstats_arr[i].string);
197 		else
198 			sprintf(*buf, "%d: %s", txq->index,
199 				qede_tqstats_arr[i].string);
200 		*buf += ETH_GSTRING_LEN;
201 	}
202 }
203 
204 static void qede_get_strings_stats_rxq(struct qede_dev *edev,
205 				       struct qede_rx_queue *rxq, u8 **buf)
206 {
207 	int i;
208 
209 	for (i = 0; i < QEDE_NUM_RQSTATS; i++) {
210 		sprintf(*buf, "%d: %s", rxq->rxq_id,
211 			qede_rqstats_arr[i].string);
212 		*buf += ETH_GSTRING_LEN;
213 	}
214 }
215 
216 static void qede_get_strings_stats(struct qede_dev *edev, u8 *buf)
217 {
218 	struct qede_fastpath *fp;
219 	int i;
220 
221 	/* Account for queue statistics */
222 	for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
223 		fp = &edev->fp_array[i];
224 
225 		if (fp->type & QEDE_FASTPATH_RX)
226 			qede_get_strings_stats_rxq(edev, fp->rxq, &buf);
227 
228 		if (fp->type & QEDE_FASTPATH_XDP)
229 			qede_get_strings_stats_txq(edev, fp->xdp_tx, &buf);
230 
231 		if (fp->type & QEDE_FASTPATH_TX)
232 			qede_get_strings_stats_txq(edev, fp->txq, &buf);
233 	}
234 
235 	/* Account for non-queue statistics */
236 	for (i = 0; i < QEDE_NUM_STATS; i++) {
237 		if (IS_VF(edev) && qede_stats_arr[i].pf_only)
238 			continue;
239 		strcpy(buf, qede_stats_arr[i].string);
240 		buf += ETH_GSTRING_LEN;
241 	}
242 }
243 
244 static void qede_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
245 {
246 	struct qede_dev *edev = netdev_priv(dev);
247 
248 	switch (stringset) {
249 	case ETH_SS_STATS:
250 		qede_get_strings_stats(edev, buf);
251 		break;
252 	case ETH_SS_PRIV_FLAGS:
253 		memcpy(buf, qede_private_arr,
254 		       ETH_GSTRING_LEN * QEDE_PRI_FLAG_LEN);
255 		break;
256 	case ETH_SS_TEST:
257 		memcpy(buf, qede_tests_str_arr,
258 		       ETH_GSTRING_LEN * QEDE_ETHTOOL_TEST_MAX);
259 		break;
260 	default:
261 		DP_VERBOSE(edev, QED_MSG_DEBUG,
262 			   "Unsupported stringset 0x%08x\n", stringset);
263 	}
264 }
265 
266 static void qede_get_ethtool_stats_txq(struct qede_tx_queue *txq, u64 **buf)
267 {
268 	int i;
269 
270 	for (i = 0; i < QEDE_NUM_TQSTATS; i++) {
271 		**buf = *((u64 *)(((void *)txq) + qede_tqstats_arr[i].offset));
272 		(*buf)++;
273 	}
274 }
275 
276 static void qede_get_ethtool_stats_rxq(struct qede_rx_queue *rxq, u64 **buf)
277 {
278 	int i;
279 
280 	for (i = 0; i < QEDE_NUM_RQSTATS; i++) {
281 		**buf = *((u64 *)(((void *)rxq) + qede_rqstats_arr[i].offset));
282 		(*buf)++;
283 	}
284 }
285 
286 static void qede_get_ethtool_stats(struct net_device *dev,
287 				   struct ethtool_stats *stats, u64 *buf)
288 {
289 	struct qede_dev *edev = netdev_priv(dev);
290 	struct qede_fastpath *fp;
291 	int i;
292 
293 	qede_fill_by_demand_stats(edev);
294 
295 	/* Need to protect the access to the fastpath array */
296 	__qede_lock(edev);
297 
298 	for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
299 		fp = &edev->fp_array[i];
300 
301 		if (fp->type & QEDE_FASTPATH_RX)
302 			qede_get_ethtool_stats_rxq(fp->rxq, &buf);
303 
304 		if (fp->type & QEDE_FASTPATH_XDP)
305 			qede_get_ethtool_stats_txq(fp->xdp_tx, &buf);
306 
307 		if (fp->type & QEDE_FASTPATH_TX)
308 			qede_get_ethtool_stats_txq(fp->txq, &buf);
309 	}
310 
311 	for (i = 0; i < QEDE_NUM_STATS; i++) {
312 		if (IS_VF(edev) && qede_stats_arr[i].pf_only)
313 			continue;
314 		*buf = *((u64 *)(((void *)&edev->stats) +
315 				 qede_stats_arr[i].offset));
316 
317 		buf++;
318 	}
319 
320 	__qede_unlock(edev);
321 }
322 
323 static int qede_get_sset_count(struct net_device *dev, int stringset)
324 {
325 	struct qede_dev *edev = netdev_priv(dev);
326 	int num_stats = QEDE_NUM_STATS;
327 
328 	switch (stringset) {
329 	case ETH_SS_STATS:
330 		if (IS_VF(edev)) {
331 			int i;
332 
333 			for (i = 0; i < QEDE_NUM_STATS; i++)
334 				if (qede_stats_arr[i].pf_only)
335 					num_stats--;
336 		}
337 
338 		/* Account for the Regular Tx statistics */
339 		num_stats += QEDE_TSS_COUNT(edev) * QEDE_NUM_TQSTATS;
340 
341 		/* Account for the Regular Rx statistics */
342 		num_stats += QEDE_RSS_COUNT(edev) * QEDE_NUM_RQSTATS;
343 
344 		/* Account for XDP statistics [if needed] */
345 		if (edev->xdp_prog)
346 			num_stats += QEDE_RSS_COUNT(edev) * QEDE_NUM_TQSTATS;
347 		return num_stats;
348 
349 	case ETH_SS_PRIV_FLAGS:
350 		return QEDE_PRI_FLAG_LEN;
351 	case ETH_SS_TEST:
352 		if (!IS_VF(edev))
353 			return QEDE_ETHTOOL_TEST_MAX;
354 		else
355 			return 0;
356 	default:
357 		DP_VERBOSE(edev, QED_MSG_DEBUG,
358 			   "Unsupported stringset 0x%08x\n", stringset);
359 		return -EINVAL;
360 	}
361 }
362 
363 static u32 qede_get_priv_flags(struct net_device *dev)
364 {
365 	struct qede_dev *edev = netdev_priv(dev);
366 
367 	return (!!(edev->dev_info.common.num_hwfns > 1)) << QEDE_PRI_FLAG_CMT;
368 }
369 
370 struct qede_link_mode_mapping {
371 	u32 qed_link_mode;
372 	u32 ethtool_link_mode;
373 };
374 
375 static const struct qede_link_mode_mapping qed_lm_map[] = {
376 	{QED_LM_FIBRE_BIT, ETHTOOL_LINK_MODE_FIBRE_BIT},
377 	{QED_LM_Autoneg_BIT, ETHTOOL_LINK_MODE_Autoneg_BIT},
378 	{QED_LM_Asym_Pause_BIT, ETHTOOL_LINK_MODE_Asym_Pause_BIT},
379 	{QED_LM_Pause_BIT, ETHTOOL_LINK_MODE_Pause_BIT},
380 	{QED_LM_1000baseT_Half_BIT, ETHTOOL_LINK_MODE_1000baseT_Half_BIT},
381 	{QED_LM_1000baseT_Full_BIT, ETHTOOL_LINK_MODE_1000baseT_Full_BIT},
382 	{QED_LM_10000baseKR_Full_BIT, ETHTOOL_LINK_MODE_10000baseKR_Full_BIT},
383 	{QED_LM_25000baseKR_Full_BIT, ETHTOOL_LINK_MODE_25000baseKR_Full_BIT},
384 	{QED_LM_40000baseLR4_Full_BIT, ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT},
385 	{QED_LM_50000baseKR2_Full_BIT, ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT},
386 	{QED_LM_100000baseKR4_Full_BIT,
387 	 ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT},
388 };
389 
390 #define QEDE_DRV_TO_ETHTOOL_CAPS(caps, lk_ksettings, name)	\
391 {								\
392 	int i;							\
393 								\
394 	for (i = 0; i < ARRAY_SIZE(qed_lm_map); i++) {		\
395 		if ((caps) & (qed_lm_map[i].qed_link_mode))	\
396 			__set_bit(qed_lm_map[i].ethtool_link_mode,\
397 				  lk_ksettings->link_modes.name); \
398 	}							\
399 }
400 
401 #define QEDE_ETHTOOL_TO_DRV_CAPS(caps, lk_ksettings, name)	\
402 {								\
403 	int i;							\
404 								\
405 	for (i = 0; i < ARRAY_SIZE(qed_lm_map); i++) {		\
406 		if (test_bit(qed_lm_map[i].ethtool_link_mode,	\
407 			     lk_ksettings->link_modes.name))	\
408 			caps |= qed_lm_map[i].qed_link_mode;	\
409 	}							\
410 }
411 
412 static int qede_get_link_ksettings(struct net_device *dev,
413 				   struct ethtool_link_ksettings *cmd)
414 {
415 	struct ethtool_link_settings *base = &cmd->base;
416 	struct qede_dev *edev = netdev_priv(dev);
417 	struct qed_link_output current_link;
418 
419 	__qede_lock(edev);
420 
421 	memset(&current_link, 0, sizeof(current_link));
422 	edev->ops->common->get_link(edev->cdev, &current_link);
423 
424 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
425 	QEDE_DRV_TO_ETHTOOL_CAPS(current_link.supported_caps, cmd, supported)
426 
427 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
428 	QEDE_DRV_TO_ETHTOOL_CAPS(current_link.advertised_caps, cmd, advertising)
429 
430 	ethtool_link_ksettings_zero_link_mode(cmd, lp_advertising);
431 	QEDE_DRV_TO_ETHTOOL_CAPS(current_link.lp_caps, cmd, lp_advertising)
432 
433 	if ((edev->state == QEDE_STATE_OPEN) && (current_link.link_up)) {
434 		base->speed = current_link.speed;
435 		base->duplex = current_link.duplex;
436 	} else {
437 		base->speed = SPEED_UNKNOWN;
438 		base->duplex = DUPLEX_UNKNOWN;
439 	}
440 
441 	__qede_unlock(edev);
442 
443 	base->port = current_link.port;
444 	base->autoneg = (current_link.autoneg) ? AUTONEG_ENABLE :
445 			AUTONEG_DISABLE;
446 
447 	return 0;
448 }
449 
450 static int qede_set_link_ksettings(struct net_device *dev,
451 				   const struct ethtool_link_ksettings *cmd)
452 {
453 	const struct ethtool_link_settings *base = &cmd->base;
454 	struct qede_dev *edev = netdev_priv(dev);
455 	struct qed_link_output current_link;
456 	struct qed_link_params params;
457 
458 	if (!edev->ops || !edev->ops->common->can_link_change(edev->cdev)) {
459 		DP_INFO(edev, "Link settings are not allowed to be changed\n");
460 		return -EOPNOTSUPP;
461 	}
462 	memset(&current_link, 0, sizeof(current_link));
463 	memset(&params, 0, sizeof(params));
464 	edev->ops->common->get_link(edev->cdev, &current_link);
465 
466 	params.override_flags |= QED_LINK_OVERRIDE_SPEED_ADV_SPEEDS;
467 	params.override_flags |= QED_LINK_OVERRIDE_SPEED_AUTONEG;
468 	if (base->autoneg == AUTONEG_ENABLE) {
469 		params.autoneg = true;
470 		params.forced_speed = 0;
471 		QEDE_ETHTOOL_TO_DRV_CAPS(params.adv_speeds, cmd, advertising)
472 	} else {		/* forced speed */
473 		params.override_flags |= QED_LINK_OVERRIDE_SPEED_FORCED_SPEED;
474 		params.autoneg = false;
475 		params.forced_speed = base->speed;
476 		switch (base->speed) {
477 		case SPEED_10000:
478 			if (!(current_link.supported_caps &
479 			      QED_LM_10000baseKR_Full_BIT)) {
480 				DP_INFO(edev, "10G speed not supported\n");
481 				return -EINVAL;
482 			}
483 			params.adv_speeds = QED_LM_10000baseKR_Full_BIT;
484 			break;
485 		case SPEED_25000:
486 			if (!(current_link.supported_caps &
487 			      QED_LM_25000baseKR_Full_BIT)) {
488 				DP_INFO(edev, "25G speed not supported\n");
489 				return -EINVAL;
490 			}
491 			params.adv_speeds = QED_LM_25000baseKR_Full_BIT;
492 			break;
493 		case SPEED_40000:
494 			if (!(current_link.supported_caps &
495 			      QED_LM_40000baseLR4_Full_BIT)) {
496 				DP_INFO(edev, "40G speed not supported\n");
497 				return -EINVAL;
498 			}
499 			params.adv_speeds = QED_LM_40000baseLR4_Full_BIT;
500 			break;
501 		case SPEED_50000:
502 			if (!(current_link.supported_caps &
503 			      QED_LM_50000baseKR2_Full_BIT)) {
504 				DP_INFO(edev, "50G speed not supported\n");
505 				return -EINVAL;
506 			}
507 			params.adv_speeds = QED_LM_50000baseKR2_Full_BIT;
508 			break;
509 		case SPEED_100000:
510 			if (!(current_link.supported_caps &
511 			      QED_LM_100000baseKR4_Full_BIT)) {
512 				DP_INFO(edev, "100G speed not supported\n");
513 				return -EINVAL;
514 			}
515 			params.adv_speeds = QED_LM_100000baseKR4_Full_BIT;
516 			break;
517 		default:
518 			DP_INFO(edev, "Unsupported speed %u\n", base->speed);
519 			return -EINVAL;
520 		}
521 	}
522 
523 	params.link_up = true;
524 	edev->ops->common->set_link(edev->cdev, &params);
525 
526 	return 0;
527 }
528 
529 static void qede_get_drvinfo(struct net_device *ndev,
530 			     struct ethtool_drvinfo *info)
531 {
532 	char mfw[ETHTOOL_FWVERS_LEN], storm[ETHTOOL_FWVERS_LEN];
533 	struct qede_dev *edev = netdev_priv(ndev);
534 
535 	strlcpy(info->driver, "qede", sizeof(info->driver));
536 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
537 
538 	snprintf(storm, ETHTOOL_FWVERS_LEN, "%d.%d.%d.%d",
539 		 edev->dev_info.common.fw_major,
540 		 edev->dev_info.common.fw_minor,
541 		 edev->dev_info.common.fw_rev,
542 		 edev->dev_info.common.fw_eng);
543 
544 	snprintf(mfw, ETHTOOL_FWVERS_LEN, "%d.%d.%d.%d",
545 		 (edev->dev_info.common.mfw_rev >> 24) & 0xFF,
546 		 (edev->dev_info.common.mfw_rev >> 16) & 0xFF,
547 		 (edev->dev_info.common.mfw_rev >> 8) & 0xFF,
548 		 edev->dev_info.common.mfw_rev & 0xFF);
549 
550 	if ((strlen(storm) + strlen(mfw) + strlen("mfw storm  ")) <
551 	    sizeof(info->fw_version)) {
552 		snprintf(info->fw_version, sizeof(info->fw_version),
553 			 "mfw %s storm %s", mfw, storm);
554 	} else {
555 		snprintf(info->fw_version, sizeof(info->fw_version),
556 			 "%s %s", mfw, storm);
557 	}
558 
559 	strlcpy(info->bus_info, pci_name(edev->pdev), sizeof(info->bus_info));
560 }
561 
562 static void qede_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
563 {
564 	struct qede_dev *edev = netdev_priv(ndev);
565 
566 	if (edev->dev_info.common.wol_support) {
567 		wol->supported = WAKE_MAGIC;
568 		wol->wolopts = edev->wol_enabled ? WAKE_MAGIC : 0;
569 	}
570 }
571 
572 static int qede_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
573 {
574 	struct qede_dev *edev = netdev_priv(ndev);
575 	bool wol_requested;
576 	int rc;
577 
578 	if (wol->wolopts & ~WAKE_MAGIC) {
579 		DP_INFO(edev,
580 			"Can't support WoL options other than magic-packet\n");
581 		return -EINVAL;
582 	}
583 
584 	wol_requested = !!(wol->wolopts & WAKE_MAGIC);
585 	if (wol_requested == edev->wol_enabled)
586 		return 0;
587 
588 	/* Need to actually change configuration */
589 	if (!edev->dev_info.common.wol_support) {
590 		DP_INFO(edev, "Device doesn't support WoL\n");
591 		return -EINVAL;
592 	}
593 
594 	rc = edev->ops->common->update_wol(edev->cdev, wol_requested);
595 	if (!rc)
596 		edev->wol_enabled = wol_requested;
597 
598 	return rc;
599 }
600 
601 static u32 qede_get_msglevel(struct net_device *ndev)
602 {
603 	struct qede_dev *edev = netdev_priv(ndev);
604 
605 	return ((u32)edev->dp_level << QED_LOG_LEVEL_SHIFT) | edev->dp_module;
606 }
607 
608 static void qede_set_msglevel(struct net_device *ndev, u32 level)
609 {
610 	struct qede_dev *edev = netdev_priv(ndev);
611 	u32 dp_module = 0;
612 	u8 dp_level = 0;
613 
614 	qede_config_debug(level, &dp_module, &dp_level);
615 
616 	edev->dp_level = dp_level;
617 	edev->dp_module = dp_module;
618 	edev->ops->common->update_msglvl(edev->cdev,
619 					 dp_module, dp_level);
620 }
621 
622 static int qede_nway_reset(struct net_device *dev)
623 {
624 	struct qede_dev *edev = netdev_priv(dev);
625 	struct qed_link_output current_link;
626 	struct qed_link_params link_params;
627 
628 	if (!edev->ops || !edev->ops->common->can_link_change(edev->cdev)) {
629 		DP_INFO(edev, "Link settings are not allowed to be changed\n");
630 		return -EOPNOTSUPP;
631 	}
632 
633 	if (!netif_running(dev))
634 		return 0;
635 
636 	memset(&current_link, 0, sizeof(current_link));
637 	edev->ops->common->get_link(edev->cdev, &current_link);
638 	if (!current_link.link_up)
639 		return 0;
640 
641 	/* Toggle the link */
642 	memset(&link_params, 0, sizeof(link_params));
643 	link_params.link_up = false;
644 	edev->ops->common->set_link(edev->cdev, &link_params);
645 	link_params.link_up = true;
646 	edev->ops->common->set_link(edev->cdev, &link_params);
647 
648 	return 0;
649 }
650 
651 static u32 qede_get_link(struct net_device *dev)
652 {
653 	struct qede_dev *edev = netdev_priv(dev);
654 	struct qed_link_output current_link;
655 
656 	memset(&current_link, 0, sizeof(current_link));
657 	edev->ops->common->get_link(edev->cdev, &current_link);
658 
659 	return current_link.link_up;
660 }
661 
662 static int qede_get_coalesce(struct net_device *dev,
663 			     struct ethtool_coalesce *coal)
664 {
665 	struct qede_dev *edev = netdev_priv(dev);
666 	u16 rxc, txc;
667 
668 	memset(coal, 0, sizeof(struct ethtool_coalesce));
669 	edev->ops->common->get_coalesce(edev->cdev, &rxc, &txc);
670 
671 	coal->rx_coalesce_usecs = rxc;
672 	coal->tx_coalesce_usecs = txc;
673 
674 	return 0;
675 }
676 
677 static int qede_set_coalesce(struct net_device *dev,
678 			     struct ethtool_coalesce *coal)
679 {
680 	struct qede_dev *edev = netdev_priv(dev);
681 	int i, rc = 0;
682 	u16 rxc, txc;
683 	u8 sb_id;
684 
685 	if (!netif_running(dev)) {
686 		DP_INFO(edev, "Interface is down\n");
687 		return -EINVAL;
688 	}
689 
690 	if (coal->rx_coalesce_usecs > QED_COALESCE_MAX ||
691 	    coal->tx_coalesce_usecs > QED_COALESCE_MAX) {
692 		DP_INFO(edev,
693 			"Can't support requested %s coalesce value [max supported value %d]\n",
694 			coal->rx_coalesce_usecs > QED_COALESCE_MAX ? "rx"
695 								   : "tx",
696 			QED_COALESCE_MAX);
697 		return -EINVAL;
698 	}
699 
700 	rxc = (u16)coal->rx_coalesce_usecs;
701 	txc = (u16)coal->tx_coalesce_usecs;
702 	for_each_queue(i) {
703 		sb_id = edev->fp_array[i].sb_info->igu_sb_id;
704 		rc = edev->ops->common->set_coalesce(edev->cdev, rxc, txc,
705 						     (u8)i, sb_id);
706 		if (rc) {
707 			DP_INFO(edev, "Set coalesce error, rc = %d\n", rc);
708 			return rc;
709 		}
710 	}
711 
712 	return rc;
713 }
714 
715 static void qede_get_ringparam(struct net_device *dev,
716 			       struct ethtool_ringparam *ering)
717 {
718 	struct qede_dev *edev = netdev_priv(dev);
719 
720 	ering->rx_max_pending = NUM_RX_BDS_MAX;
721 	ering->rx_pending = edev->q_num_rx_buffers;
722 	ering->tx_max_pending = NUM_TX_BDS_MAX;
723 	ering->tx_pending = edev->q_num_tx_buffers;
724 }
725 
726 static int qede_set_ringparam(struct net_device *dev,
727 			      struct ethtool_ringparam *ering)
728 {
729 	struct qede_dev *edev = netdev_priv(dev);
730 
731 	DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
732 		   "Set ring params command parameters: rx_pending = %d, tx_pending = %d\n",
733 		   ering->rx_pending, ering->tx_pending);
734 
735 	/* Validate legality of configuration */
736 	if (ering->rx_pending > NUM_RX_BDS_MAX ||
737 	    ering->rx_pending < NUM_RX_BDS_MIN ||
738 	    ering->tx_pending > NUM_TX_BDS_MAX ||
739 	    ering->tx_pending < NUM_TX_BDS_MIN) {
740 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
741 			   "Can only support Rx Buffer size [0%08x,...,0x%08x] and Tx Buffer size [0x%08x,...,0x%08x]\n",
742 			   NUM_RX_BDS_MIN, NUM_RX_BDS_MAX,
743 			   NUM_TX_BDS_MIN, NUM_TX_BDS_MAX);
744 		return -EINVAL;
745 	}
746 
747 	/* Change ring size and re-load */
748 	edev->q_num_rx_buffers = ering->rx_pending;
749 	edev->q_num_tx_buffers = ering->tx_pending;
750 
751 	qede_reload(edev, NULL, false);
752 
753 	return 0;
754 }
755 
756 static void qede_get_pauseparam(struct net_device *dev,
757 				struct ethtool_pauseparam *epause)
758 {
759 	struct qede_dev *edev = netdev_priv(dev);
760 	struct qed_link_output current_link;
761 
762 	memset(&current_link, 0, sizeof(current_link));
763 	edev->ops->common->get_link(edev->cdev, &current_link);
764 
765 	if (current_link.pause_config & QED_LINK_PAUSE_AUTONEG_ENABLE)
766 		epause->autoneg = true;
767 	if (current_link.pause_config & QED_LINK_PAUSE_RX_ENABLE)
768 		epause->rx_pause = true;
769 	if (current_link.pause_config & QED_LINK_PAUSE_TX_ENABLE)
770 		epause->tx_pause = true;
771 
772 	DP_VERBOSE(edev, QED_MSG_DEBUG,
773 		   "ethtool_pauseparam: cmd %d  autoneg %d  rx_pause %d  tx_pause %d\n",
774 		   epause->cmd, epause->autoneg, epause->rx_pause,
775 		   epause->tx_pause);
776 }
777 
778 static int qede_set_pauseparam(struct net_device *dev,
779 			       struct ethtool_pauseparam *epause)
780 {
781 	struct qede_dev *edev = netdev_priv(dev);
782 	struct qed_link_params params;
783 	struct qed_link_output current_link;
784 
785 	if (!edev->ops || !edev->ops->common->can_link_change(edev->cdev)) {
786 		DP_INFO(edev,
787 			"Pause settings are not allowed to be changed\n");
788 		return -EOPNOTSUPP;
789 	}
790 
791 	memset(&current_link, 0, sizeof(current_link));
792 	edev->ops->common->get_link(edev->cdev, &current_link);
793 
794 	memset(&params, 0, sizeof(params));
795 	params.override_flags |= QED_LINK_OVERRIDE_PAUSE_CONFIG;
796 	if (epause->autoneg) {
797 		if (!(current_link.supported_caps & QED_LM_Autoneg_BIT)) {
798 			DP_INFO(edev, "autoneg not supported\n");
799 			return -EINVAL;
800 		}
801 		params.pause_config |= QED_LINK_PAUSE_AUTONEG_ENABLE;
802 	}
803 	if (epause->rx_pause)
804 		params.pause_config |= QED_LINK_PAUSE_RX_ENABLE;
805 	if (epause->tx_pause)
806 		params.pause_config |= QED_LINK_PAUSE_TX_ENABLE;
807 
808 	params.link_up = true;
809 	edev->ops->common->set_link(edev->cdev, &params);
810 
811 	return 0;
812 }
813 
814 static void qede_get_regs(struct net_device *ndev,
815 			  struct ethtool_regs *regs, void *buffer)
816 {
817 	struct qede_dev *edev = netdev_priv(ndev);
818 
819 	regs->version = 0;
820 	memset(buffer, 0, regs->len);
821 
822 	if (edev->ops && edev->ops->common)
823 		edev->ops->common->dbg_all_data(edev->cdev, buffer);
824 }
825 
826 static int qede_get_regs_len(struct net_device *ndev)
827 {
828 	struct qede_dev *edev = netdev_priv(ndev);
829 
830 	if (edev->ops && edev->ops->common)
831 		return edev->ops->common->dbg_all_data_size(edev->cdev);
832 	else
833 		return -EINVAL;
834 }
835 
836 static void qede_update_mtu(struct qede_dev *edev,
837 			    struct qede_reload_args *args)
838 {
839 	edev->ndev->mtu = args->u.mtu;
840 }
841 
842 /* Netdevice NDOs */
843 int qede_change_mtu(struct net_device *ndev, int new_mtu)
844 {
845 	struct qede_dev *edev = netdev_priv(ndev);
846 	struct qede_reload_args args;
847 
848 	DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
849 		   "Configuring MTU size of %d\n", new_mtu);
850 
851 	/* Set the mtu field and re-start the interface if needed */
852 	args.u.mtu = new_mtu;
853 	args.func = &qede_update_mtu;
854 	qede_reload(edev, &args, false);
855 
856 	edev->ops->common->update_mtu(edev->cdev, new_mtu);
857 
858 	return 0;
859 }
860 
861 static void qede_get_channels(struct net_device *dev,
862 			      struct ethtool_channels *channels)
863 {
864 	struct qede_dev *edev = netdev_priv(dev);
865 
866 	channels->max_combined = QEDE_MAX_RSS_CNT(edev);
867 	channels->max_rx = QEDE_MAX_RSS_CNT(edev);
868 	channels->max_tx = QEDE_MAX_RSS_CNT(edev);
869 	channels->combined_count = QEDE_QUEUE_CNT(edev) - edev->fp_num_tx -
870 					edev->fp_num_rx;
871 	channels->tx_count = edev->fp_num_tx;
872 	channels->rx_count = edev->fp_num_rx;
873 }
874 
875 static int qede_set_channels(struct net_device *dev,
876 			     struct ethtool_channels *channels)
877 {
878 	struct qede_dev *edev = netdev_priv(dev);
879 	u32 count;
880 
881 	DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
882 		   "set-channels command parameters: rx = %d, tx = %d, other = %d, combined = %d\n",
883 		   channels->rx_count, channels->tx_count,
884 		   channels->other_count, channels->combined_count);
885 
886 	count = channels->rx_count + channels->tx_count +
887 			channels->combined_count;
888 
889 	/* We don't support `other' channels */
890 	if (channels->other_count) {
891 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
892 			   "command parameters not supported\n");
893 		return -EINVAL;
894 	}
895 
896 	if (!(channels->combined_count || (channels->rx_count &&
897 					   channels->tx_count))) {
898 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
899 			   "need to request at least one transmit and one receive channel\n");
900 		return -EINVAL;
901 	}
902 
903 	if (count > QEDE_MAX_RSS_CNT(edev)) {
904 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
905 			   "requested channels = %d max supported channels = %d\n",
906 			   count, QEDE_MAX_RSS_CNT(edev));
907 		return -EINVAL;
908 	}
909 
910 	/* Check if there was a change in the active parameters */
911 	if ((count == QEDE_QUEUE_CNT(edev)) &&
912 	    (channels->tx_count == edev->fp_num_tx) &&
913 	    (channels->rx_count == edev->fp_num_rx)) {
914 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
915 			   "No change in active parameters\n");
916 		return 0;
917 	}
918 
919 	/* We need the number of queues to be divisible between the hwfns */
920 	if ((count % edev->dev_info.common.num_hwfns) ||
921 	    (channels->tx_count % edev->dev_info.common.num_hwfns) ||
922 	    (channels->rx_count % edev->dev_info.common.num_hwfns)) {
923 		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
924 			   "Number of channels must be divisible by %04x\n",
925 			   edev->dev_info.common.num_hwfns);
926 		return -EINVAL;
927 	}
928 
929 	/* Set number of queues and reload if necessary */
930 	edev->req_queues = count;
931 	edev->req_num_tx = channels->tx_count;
932 	edev->req_num_rx = channels->rx_count;
933 	/* Reset the indirection table if rx queue count is updated */
934 	if ((edev->req_queues - edev->req_num_tx) != QEDE_RSS_COUNT(edev)) {
935 		edev->rss_params_inited &= ~QEDE_RSS_INDIR_INITED;
936 		memset(edev->rss_ind_table, 0, sizeof(edev->rss_ind_table));
937 	}
938 
939 	qede_reload(edev, NULL, false);
940 
941 	return 0;
942 }
943 
944 static int qede_get_ts_info(struct net_device *dev,
945 			    struct ethtool_ts_info *info)
946 {
947 	struct qede_dev *edev = netdev_priv(dev);
948 
949 	return qede_ptp_get_ts_info(edev, info);
950 }
951 
952 static int qede_set_phys_id(struct net_device *dev,
953 			    enum ethtool_phys_id_state state)
954 {
955 	struct qede_dev *edev = netdev_priv(dev);
956 	u8 led_state = 0;
957 
958 	switch (state) {
959 	case ETHTOOL_ID_ACTIVE:
960 		return 1;	/* cycle on/off once per second */
961 
962 	case ETHTOOL_ID_ON:
963 		led_state = QED_LED_MODE_ON;
964 		break;
965 
966 	case ETHTOOL_ID_OFF:
967 		led_state = QED_LED_MODE_OFF;
968 		break;
969 
970 	case ETHTOOL_ID_INACTIVE:
971 		led_state = QED_LED_MODE_RESTORE;
972 		break;
973 	}
974 
975 	edev->ops->common->set_led(edev->cdev, led_state);
976 
977 	return 0;
978 }
979 
980 static int qede_get_rss_flags(struct qede_dev *edev, struct ethtool_rxnfc *info)
981 {
982 	info->data = RXH_IP_SRC | RXH_IP_DST;
983 
984 	switch (info->flow_type) {
985 	case TCP_V4_FLOW:
986 	case TCP_V6_FLOW:
987 		info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
988 		break;
989 	case UDP_V4_FLOW:
990 		if (edev->rss_caps & QED_RSS_IPV4_UDP)
991 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
992 		break;
993 	case UDP_V6_FLOW:
994 		if (edev->rss_caps & QED_RSS_IPV6_UDP)
995 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
996 		break;
997 	case IPV4_FLOW:
998 	case IPV6_FLOW:
999 		break;
1000 	default:
1001 		info->data = 0;
1002 		break;
1003 	}
1004 
1005 	return 0;
1006 }
1007 
1008 static int qede_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1009 			  u32 *rules __always_unused)
1010 {
1011 	struct qede_dev *edev = netdev_priv(dev);
1012 
1013 	switch (info->cmd) {
1014 	case ETHTOOL_GRXRINGS:
1015 		info->data = QEDE_RSS_COUNT(edev);
1016 		return 0;
1017 	case ETHTOOL_GRXFH:
1018 		return qede_get_rss_flags(edev, info);
1019 	default:
1020 		DP_ERR(edev, "Command parameters not supported\n");
1021 		return -EOPNOTSUPP;
1022 	}
1023 }
1024 
1025 static int qede_set_rss_flags(struct qede_dev *edev, struct ethtool_rxnfc *info)
1026 {
1027 	struct qed_update_vport_params *vport_update_params;
1028 	u8 set_caps = 0, clr_caps = 0;
1029 	int rc = 0;
1030 
1031 	DP_VERBOSE(edev, QED_MSG_DEBUG,
1032 		   "Set rss flags command parameters: flow type = %d, data = %llu\n",
1033 		   info->flow_type, info->data);
1034 
1035 	switch (info->flow_type) {
1036 	case TCP_V4_FLOW:
1037 	case TCP_V6_FLOW:
1038 		/* For TCP only 4-tuple hash is supported */
1039 		if (info->data ^ (RXH_IP_SRC | RXH_IP_DST |
1040 				  RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1041 			DP_INFO(edev, "Command parameters not supported\n");
1042 			return -EINVAL;
1043 		}
1044 		return 0;
1045 	case UDP_V4_FLOW:
1046 		/* For UDP either 2-tuple hash or 4-tuple hash is supported */
1047 		if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1048 				   RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1049 			set_caps = QED_RSS_IPV4_UDP;
1050 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1051 				   "UDP 4-tuple enabled\n");
1052 		} else if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1053 			clr_caps = QED_RSS_IPV4_UDP;
1054 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1055 				   "UDP 4-tuple disabled\n");
1056 		} else {
1057 			return -EINVAL;
1058 		}
1059 		break;
1060 	case UDP_V6_FLOW:
1061 		/* For UDP either 2-tuple hash or 4-tuple hash is supported */
1062 		if (info->data == (RXH_IP_SRC | RXH_IP_DST |
1063 				   RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1064 			set_caps = QED_RSS_IPV6_UDP;
1065 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1066 				   "UDP 4-tuple enabled\n");
1067 		} else if (info->data == (RXH_IP_SRC | RXH_IP_DST)) {
1068 			clr_caps = QED_RSS_IPV6_UDP;
1069 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1070 				   "UDP 4-tuple disabled\n");
1071 		} else {
1072 			return -EINVAL;
1073 		}
1074 		break;
1075 	case IPV4_FLOW:
1076 	case IPV6_FLOW:
1077 		/* For IP only 2-tuple hash is supported */
1078 		if (info->data ^ (RXH_IP_SRC | RXH_IP_DST)) {
1079 			DP_INFO(edev, "Command parameters not supported\n");
1080 			return -EINVAL;
1081 		}
1082 		return 0;
1083 	case SCTP_V4_FLOW:
1084 	case AH_ESP_V4_FLOW:
1085 	case AH_V4_FLOW:
1086 	case ESP_V4_FLOW:
1087 	case SCTP_V6_FLOW:
1088 	case AH_ESP_V6_FLOW:
1089 	case AH_V6_FLOW:
1090 	case ESP_V6_FLOW:
1091 	case IP_USER_FLOW:
1092 	case ETHER_FLOW:
1093 		/* RSS is not supported for these protocols */
1094 		if (info->data) {
1095 			DP_INFO(edev, "Command parameters not supported\n");
1096 			return -EINVAL;
1097 		}
1098 		return 0;
1099 	default:
1100 		return -EINVAL;
1101 	}
1102 
1103 	/* No action is needed if there is no change in the rss capability */
1104 	if (edev->rss_caps == ((edev->rss_caps & ~clr_caps) | set_caps))
1105 		return 0;
1106 
1107 	/* Update internal configuration */
1108 	edev->rss_caps = ((edev->rss_caps & ~clr_caps) | set_caps);
1109 	edev->rss_params_inited |= QEDE_RSS_CAPS_INITED;
1110 
1111 	/* Re-configure if possible */
1112 	__qede_lock(edev);
1113 	if (edev->state == QEDE_STATE_OPEN) {
1114 		vport_update_params = vzalloc(sizeof(*vport_update_params));
1115 		if (!vport_update_params) {
1116 			__qede_unlock(edev);
1117 			return -ENOMEM;
1118 		}
1119 		qede_fill_rss_params(edev, &vport_update_params->rss_params,
1120 				     &vport_update_params->update_rss_flg);
1121 		rc = edev->ops->vport_update(edev->cdev, vport_update_params);
1122 		vfree(vport_update_params);
1123 	}
1124 	__qede_unlock(edev);
1125 
1126 	return rc;
1127 }
1128 
1129 static int qede_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
1130 {
1131 	struct qede_dev *edev = netdev_priv(dev);
1132 
1133 	switch (info->cmd) {
1134 	case ETHTOOL_SRXFH:
1135 		return qede_set_rss_flags(edev, info);
1136 	default:
1137 		DP_INFO(edev, "Command parameters not supported\n");
1138 		return -EOPNOTSUPP;
1139 	}
1140 }
1141 
1142 static u32 qede_get_rxfh_indir_size(struct net_device *dev)
1143 {
1144 	return QED_RSS_IND_TABLE_SIZE;
1145 }
1146 
1147 static u32 qede_get_rxfh_key_size(struct net_device *dev)
1148 {
1149 	struct qede_dev *edev = netdev_priv(dev);
1150 
1151 	return sizeof(edev->rss_key);
1152 }
1153 
1154 static int qede_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
1155 {
1156 	struct qede_dev *edev = netdev_priv(dev);
1157 	int i;
1158 
1159 	if (hfunc)
1160 		*hfunc = ETH_RSS_HASH_TOP;
1161 
1162 	if (!indir)
1163 		return 0;
1164 
1165 	for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++)
1166 		indir[i] = edev->rss_ind_table[i];
1167 
1168 	if (key)
1169 		memcpy(key, edev->rss_key, qede_get_rxfh_key_size(dev));
1170 
1171 	return 0;
1172 }
1173 
1174 static int qede_set_rxfh(struct net_device *dev, const u32 *indir,
1175 			 const u8 *key, const u8 hfunc)
1176 {
1177 	struct qed_update_vport_params *vport_update_params;
1178 	struct qede_dev *edev = netdev_priv(dev);
1179 	int i, rc = 0;
1180 
1181 	if (edev->dev_info.common.num_hwfns > 1) {
1182 		DP_INFO(edev,
1183 			"RSS configuration is not supported for 100G devices\n");
1184 		return -EOPNOTSUPP;
1185 	}
1186 
1187 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1188 		return -EOPNOTSUPP;
1189 
1190 	if (!indir && !key)
1191 		return 0;
1192 
1193 	if (indir) {
1194 		for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++)
1195 			edev->rss_ind_table[i] = indir[i];
1196 		edev->rss_params_inited |= QEDE_RSS_INDIR_INITED;
1197 	}
1198 
1199 	if (key) {
1200 		memcpy(&edev->rss_key, key, qede_get_rxfh_key_size(dev));
1201 		edev->rss_params_inited |= QEDE_RSS_KEY_INITED;
1202 	}
1203 
1204 	__qede_lock(edev);
1205 	if (edev->state == QEDE_STATE_OPEN) {
1206 		vport_update_params = vzalloc(sizeof(*vport_update_params));
1207 		if (!vport_update_params) {
1208 			__qede_unlock(edev);
1209 			return -ENOMEM;
1210 		}
1211 		qede_fill_rss_params(edev, &vport_update_params->rss_params,
1212 				     &vport_update_params->update_rss_flg);
1213 		rc = edev->ops->vport_update(edev->cdev, vport_update_params);
1214 		vfree(vport_update_params);
1215 	}
1216 	__qede_unlock(edev);
1217 
1218 	return rc;
1219 }
1220 
1221 /* This function enables the interrupt generation and the NAPI on the device */
1222 static void qede_netif_start(struct qede_dev *edev)
1223 {
1224 	int i;
1225 
1226 	if (!netif_running(edev->ndev))
1227 		return;
1228 
1229 	for_each_queue(i) {
1230 		/* Update and reenable interrupts */
1231 		qed_sb_ack(edev->fp_array[i].sb_info, IGU_INT_ENABLE, 1);
1232 		napi_enable(&edev->fp_array[i].napi);
1233 	}
1234 }
1235 
1236 /* This function disables the NAPI and the interrupt generation on the device */
1237 static void qede_netif_stop(struct qede_dev *edev)
1238 {
1239 	int i;
1240 
1241 	for_each_queue(i) {
1242 		napi_disable(&edev->fp_array[i].napi);
1243 		/* Disable interrupts */
1244 		qed_sb_ack(edev->fp_array[i].sb_info, IGU_INT_DISABLE, 0);
1245 	}
1246 }
1247 
1248 static int qede_selftest_transmit_traffic(struct qede_dev *edev,
1249 					  struct sk_buff *skb)
1250 {
1251 	struct qede_tx_queue *txq = NULL;
1252 	struct eth_tx_1st_bd *first_bd;
1253 	dma_addr_t mapping;
1254 	int i, idx, val;
1255 
1256 	for_each_queue(i) {
1257 		if (edev->fp_array[i].type & QEDE_FASTPATH_TX) {
1258 			txq = edev->fp_array[i].txq;
1259 			break;
1260 		}
1261 	}
1262 
1263 	if (!txq) {
1264 		DP_NOTICE(edev, "Tx path is not available\n");
1265 		return -1;
1266 	}
1267 
1268 	/* Fill the entry in the SW ring and the BDs in the FW ring */
1269 	idx = txq->sw_tx_prod & NUM_TX_BDS_MAX;
1270 	txq->sw_tx_ring.skbs[idx].skb = skb;
1271 	first_bd = qed_chain_produce(&txq->tx_pbl);
1272 	memset(first_bd, 0, sizeof(*first_bd));
1273 	val = 1 << ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT;
1274 	first_bd->data.bd_flags.bitfields = val;
1275 	val = skb->len & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK;
1276 	first_bd->data.bitfields |= (val << ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT);
1277 
1278 	/* Map skb linear data for DMA and set in the first BD */
1279 	mapping = dma_map_single(&edev->pdev->dev, skb->data,
1280 				 skb_headlen(skb), DMA_TO_DEVICE);
1281 	if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
1282 		DP_NOTICE(edev, "SKB mapping failed\n");
1283 		return -ENOMEM;
1284 	}
1285 	BD_SET_UNMAP_ADDR_LEN(first_bd, mapping, skb_headlen(skb));
1286 
1287 	/* update the first BD with the actual num BDs */
1288 	first_bd->data.nbds = 1;
1289 	txq->sw_tx_prod++;
1290 	/* 'next page' entries are counted in the producer value */
1291 	val = cpu_to_le16(qed_chain_get_prod_idx(&txq->tx_pbl));
1292 	txq->tx_db.data.bd_prod = val;
1293 
1294 	/* wmb makes sure that the BDs data is updated before updating the
1295 	 * producer, otherwise FW may read old data from the BDs.
1296 	 */
1297 	wmb();
1298 	barrier();
1299 	writel(txq->tx_db.raw, txq->doorbell_addr);
1300 
1301 	/* mmiowb is needed to synchronize doorbell writes from more than one
1302 	 * processor. It guarantees that the write arrives to the device before
1303 	 * the queue lock is released and another start_xmit is called (possibly
1304 	 * on another CPU). Without this barrier, the next doorbell can bypass
1305 	 * this doorbell. This is applicable to IA64/Altix systems.
1306 	 */
1307 	mmiowb();
1308 
1309 	for (i = 0; i < QEDE_SELFTEST_POLL_COUNT; i++) {
1310 		if (qede_txq_has_work(txq))
1311 			break;
1312 		usleep_range(100, 200);
1313 	}
1314 
1315 	if (!qede_txq_has_work(txq)) {
1316 		DP_NOTICE(edev, "Tx completion didn't happen\n");
1317 		return -1;
1318 	}
1319 
1320 	first_bd = (struct eth_tx_1st_bd *)qed_chain_consume(&txq->tx_pbl);
1321 	dma_unmap_single(&edev->pdev->dev, BD_UNMAP_ADDR(first_bd),
1322 			 BD_UNMAP_LEN(first_bd), DMA_TO_DEVICE);
1323 	txq->sw_tx_cons++;
1324 	txq->sw_tx_ring.skbs[idx].skb = NULL;
1325 
1326 	return 0;
1327 }
1328 
1329 static int qede_selftest_receive_traffic(struct qede_dev *edev)
1330 {
1331 	u16 hw_comp_cons, sw_comp_cons, sw_rx_index, len;
1332 	struct eth_fast_path_rx_reg_cqe *fp_cqe;
1333 	struct qede_rx_queue *rxq = NULL;
1334 	struct sw_rx_data *sw_rx_data;
1335 	union eth_rx_cqe *cqe;
1336 	int i, iter, rc = 0;
1337 	u8 *data_ptr;
1338 
1339 	for_each_queue(i) {
1340 		if (edev->fp_array[i].type & QEDE_FASTPATH_RX) {
1341 			rxq = edev->fp_array[i].rxq;
1342 			break;
1343 		}
1344 	}
1345 
1346 	if (!rxq) {
1347 		DP_NOTICE(edev, "Rx path is not available\n");
1348 		return -1;
1349 	}
1350 
1351 	/* The packet is expected to receive on rx-queue 0 even though RSS is
1352 	 * enabled. This is because the queue 0 is configured as the default
1353 	 * queue and that the loopback traffic is not IP.
1354 	 */
1355 	for (iter = 0; iter < QEDE_SELFTEST_POLL_COUNT; iter++) {
1356 		if (!qede_has_rx_work(rxq)) {
1357 			usleep_range(100, 200);
1358 			continue;
1359 		}
1360 
1361 		hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
1362 		sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1363 
1364 		/* Memory barrier to prevent the CPU from doing speculative
1365 		 * reads of CQE/BD before reading hw_comp_cons. If the CQE is
1366 		 * read before it is written by FW, then FW writes CQE and SB,
1367 		 * and then the CPU reads the hw_comp_cons, it will use an old
1368 		 * CQE.
1369 		 */
1370 		rmb();
1371 
1372 		/* Get the CQE from the completion ring */
1373 		cqe = (union eth_rx_cqe *)qed_chain_consume(&rxq->rx_comp_ring);
1374 
1375 		/* Get the data from the SW ring */
1376 		sw_rx_index = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1377 		sw_rx_data = &rxq->sw_rx_ring[sw_rx_index];
1378 		fp_cqe = &cqe->fast_path_regular;
1379 		len =  le16_to_cpu(fp_cqe->len_on_first_bd);
1380 		data_ptr = (u8 *)(page_address(sw_rx_data->data) +
1381 				  fp_cqe->placement_offset +
1382 				  sw_rx_data->page_offset);
1383 		if (ether_addr_equal(data_ptr,  edev->ndev->dev_addr) &&
1384 		    ether_addr_equal(data_ptr + ETH_ALEN,
1385 				     edev->ndev->dev_addr)) {
1386 			for (i = ETH_HLEN; i < len; i++)
1387 				if (data_ptr[i] != (unsigned char)(i & 0xff)) {
1388 					rc = -1;
1389 					break;
1390 				}
1391 
1392 			qede_recycle_rx_bd_ring(rxq, 1);
1393 			qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1394 			break;
1395 		}
1396 
1397 		DP_INFO(edev, "Not the transmitted packet\n");
1398 		qede_recycle_rx_bd_ring(rxq, 1);
1399 		qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1400 	}
1401 
1402 	if (iter == QEDE_SELFTEST_POLL_COUNT) {
1403 		DP_NOTICE(edev, "Failed to receive the traffic\n");
1404 		return -1;
1405 	}
1406 
1407 	qede_update_rx_prod(edev, rxq);
1408 
1409 	return rc;
1410 }
1411 
1412 static int qede_selftest_run_loopback(struct qede_dev *edev, u32 loopback_mode)
1413 {
1414 	struct qed_link_params link_params;
1415 	struct sk_buff *skb = NULL;
1416 	int rc = 0, i;
1417 	u32 pkt_size;
1418 	u8 *packet;
1419 
1420 	if (!netif_running(edev->ndev)) {
1421 		DP_NOTICE(edev, "Interface is down\n");
1422 		return -EINVAL;
1423 	}
1424 
1425 	qede_netif_stop(edev);
1426 
1427 	/* Bring up the link in Loopback mode */
1428 	memset(&link_params, 0, sizeof(link_params));
1429 	link_params.link_up = true;
1430 	link_params.override_flags = QED_LINK_OVERRIDE_LOOPBACK_MODE;
1431 	link_params.loopback_mode = loopback_mode;
1432 	edev->ops->common->set_link(edev->cdev, &link_params);
1433 
1434 	/* Wait for loopback configuration to apply */
1435 	msleep_interruptible(500);
1436 
1437 	/* prepare the loopback packet */
1438 	pkt_size = edev->ndev->mtu + ETH_HLEN;
1439 
1440 	skb = netdev_alloc_skb(edev->ndev, pkt_size);
1441 	if (!skb) {
1442 		DP_INFO(edev, "Can't allocate skb\n");
1443 		rc = -ENOMEM;
1444 		goto test_loopback_exit;
1445 	}
1446 	packet = skb_put(skb, pkt_size);
1447 	ether_addr_copy(packet, edev->ndev->dev_addr);
1448 	ether_addr_copy(packet + ETH_ALEN, edev->ndev->dev_addr);
1449 	memset(packet + (2 * ETH_ALEN), 0x77, (ETH_HLEN - (2 * ETH_ALEN)));
1450 	for (i = ETH_HLEN; i < pkt_size; i++)
1451 		packet[i] = (unsigned char)(i & 0xff);
1452 
1453 	rc = qede_selftest_transmit_traffic(edev, skb);
1454 	if (rc)
1455 		goto test_loopback_exit;
1456 
1457 	rc = qede_selftest_receive_traffic(edev);
1458 	if (rc)
1459 		goto test_loopback_exit;
1460 
1461 	DP_VERBOSE(edev, NETIF_MSG_RX_STATUS, "Loopback test successful\n");
1462 
1463 test_loopback_exit:
1464 	dev_kfree_skb(skb);
1465 
1466 	/* Bring up the link in Normal mode */
1467 	memset(&link_params, 0, sizeof(link_params));
1468 	link_params.link_up = true;
1469 	link_params.override_flags = QED_LINK_OVERRIDE_LOOPBACK_MODE;
1470 	link_params.loopback_mode = QED_LINK_LOOPBACK_NONE;
1471 	edev->ops->common->set_link(edev->cdev, &link_params);
1472 
1473 	/* Wait for loopback configuration to apply */
1474 	msleep_interruptible(500);
1475 
1476 	qede_netif_start(edev);
1477 
1478 	return rc;
1479 }
1480 
1481 static void qede_self_test(struct net_device *dev,
1482 			   struct ethtool_test *etest, u64 *buf)
1483 {
1484 	struct qede_dev *edev = netdev_priv(dev);
1485 
1486 	DP_VERBOSE(edev, QED_MSG_DEBUG,
1487 		   "Self-test command parameters: offline = %d, external_lb = %d\n",
1488 		   (etest->flags & ETH_TEST_FL_OFFLINE),
1489 		   (etest->flags & ETH_TEST_FL_EXTERNAL_LB) >> 2);
1490 
1491 	memset(buf, 0, sizeof(u64) * QEDE_ETHTOOL_TEST_MAX);
1492 
1493 	if (etest->flags & ETH_TEST_FL_OFFLINE) {
1494 		if (qede_selftest_run_loopback(edev,
1495 					       QED_LINK_LOOPBACK_INT_PHY)) {
1496 			buf[QEDE_ETHTOOL_INT_LOOPBACK] = 1;
1497 			etest->flags |= ETH_TEST_FL_FAILED;
1498 		}
1499 	}
1500 
1501 	if (edev->ops->common->selftest->selftest_interrupt(edev->cdev)) {
1502 		buf[QEDE_ETHTOOL_INTERRUPT_TEST] = 1;
1503 		etest->flags |= ETH_TEST_FL_FAILED;
1504 	}
1505 
1506 	if (edev->ops->common->selftest->selftest_memory(edev->cdev)) {
1507 		buf[QEDE_ETHTOOL_MEMORY_TEST] = 1;
1508 		etest->flags |= ETH_TEST_FL_FAILED;
1509 	}
1510 
1511 	if (edev->ops->common->selftest->selftest_register(edev->cdev)) {
1512 		buf[QEDE_ETHTOOL_REGISTER_TEST] = 1;
1513 		etest->flags |= ETH_TEST_FL_FAILED;
1514 	}
1515 
1516 	if (edev->ops->common->selftest->selftest_clock(edev->cdev)) {
1517 		buf[QEDE_ETHTOOL_CLOCK_TEST] = 1;
1518 		etest->flags |= ETH_TEST_FL_FAILED;
1519 	}
1520 
1521 	if (edev->ops->common->selftest->selftest_nvram(edev->cdev)) {
1522 		buf[QEDE_ETHTOOL_NVRAM_TEST] = 1;
1523 		etest->flags |= ETH_TEST_FL_FAILED;
1524 	}
1525 }
1526 
1527 static int qede_set_tunable(struct net_device *dev,
1528 			    const struct ethtool_tunable *tuna,
1529 			    const void *data)
1530 {
1531 	struct qede_dev *edev = netdev_priv(dev);
1532 	u32 val;
1533 
1534 	switch (tuna->id) {
1535 	case ETHTOOL_RX_COPYBREAK:
1536 		val = *(u32 *)data;
1537 		if (val < QEDE_MIN_PKT_LEN || val > QEDE_RX_HDR_SIZE) {
1538 			DP_VERBOSE(edev, QED_MSG_DEBUG,
1539 				   "Invalid rx copy break value, range is [%u, %u]",
1540 				   QEDE_MIN_PKT_LEN, QEDE_RX_HDR_SIZE);
1541 			return -EINVAL;
1542 		}
1543 
1544 		edev->rx_copybreak = *(u32 *)data;
1545 		break;
1546 	default:
1547 		return -EOPNOTSUPP;
1548 	}
1549 
1550 	return 0;
1551 }
1552 
1553 static int qede_get_tunable(struct net_device *dev,
1554 			    const struct ethtool_tunable *tuna, void *data)
1555 {
1556 	struct qede_dev *edev = netdev_priv(dev);
1557 
1558 	switch (tuna->id) {
1559 	case ETHTOOL_RX_COPYBREAK:
1560 		*(u32 *)data = edev->rx_copybreak;
1561 		break;
1562 	default:
1563 		return -EOPNOTSUPP;
1564 	}
1565 
1566 	return 0;
1567 }
1568 
1569 static const struct ethtool_ops qede_ethtool_ops = {
1570 	.get_link_ksettings = qede_get_link_ksettings,
1571 	.set_link_ksettings = qede_set_link_ksettings,
1572 	.get_drvinfo = qede_get_drvinfo,
1573 	.get_regs_len = qede_get_regs_len,
1574 	.get_regs = qede_get_regs,
1575 	.get_wol = qede_get_wol,
1576 	.set_wol = qede_set_wol,
1577 	.get_msglevel = qede_get_msglevel,
1578 	.set_msglevel = qede_set_msglevel,
1579 	.nway_reset = qede_nway_reset,
1580 	.get_link = qede_get_link,
1581 	.get_coalesce = qede_get_coalesce,
1582 	.set_coalesce = qede_set_coalesce,
1583 	.get_ringparam = qede_get_ringparam,
1584 	.set_ringparam = qede_set_ringparam,
1585 	.get_pauseparam = qede_get_pauseparam,
1586 	.set_pauseparam = qede_set_pauseparam,
1587 	.get_strings = qede_get_strings,
1588 	.set_phys_id = qede_set_phys_id,
1589 	.get_ethtool_stats = qede_get_ethtool_stats,
1590 	.get_priv_flags = qede_get_priv_flags,
1591 	.get_sset_count = qede_get_sset_count,
1592 	.get_rxnfc = qede_get_rxnfc,
1593 	.set_rxnfc = qede_set_rxnfc,
1594 	.get_rxfh_indir_size = qede_get_rxfh_indir_size,
1595 	.get_rxfh_key_size = qede_get_rxfh_key_size,
1596 	.get_rxfh = qede_get_rxfh,
1597 	.set_rxfh = qede_set_rxfh,
1598 	.get_ts_info = qede_get_ts_info,
1599 	.get_channels = qede_get_channels,
1600 	.set_channels = qede_set_channels,
1601 	.self_test = qede_self_test,
1602 	.get_tunable = qede_get_tunable,
1603 	.set_tunable = qede_set_tunable,
1604 };
1605 
1606 static const struct ethtool_ops qede_vf_ethtool_ops = {
1607 	.get_link_ksettings = qede_get_link_ksettings,
1608 	.get_drvinfo = qede_get_drvinfo,
1609 	.get_msglevel = qede_get_msglevel,
1610 	.set_msglevel = qede_set_msglevel,
1611 	.get_link = qede_get_link,
1612 	.get_ringparam = qede_get_ringparam,
1613 	.set_ringparam = qede_set_ringparam,
1614 	.get_strings = qede_get_strings,
1615 	.get_ethtool_stats = qede_get_ethtool_stats,
1616 	.get_priv_flags = qede_get_priv_flags,
1617 	.get_sset_count = qede_get_sset_count,
1618 	.get_rxnfc = qede_get_rxnfc,
1619 	.set_rxnfc = qede_set_rxnfc,
1620 	.get_rxfh_indir_size = qede_get_rxfh_indir_size,
1621 	.get_rxfh_key_size = qede_get_rxfh_key_size,
1622 	.get_rxfh = qede_get_rxfh,
1623 	.set_rxfh = qede_set_rxfh,
1624 	.get_channels = qede_get_channels,
1625 	.set_channels = qede_set_channels,
1626 	.get_tunable = qede_get_tunable,
1627 	.set_tunable = qede_set_tunable,
1628 };
1629 
1630 void qede_set_ethtool_ops(struct net_device *dev)
1631 {
1632 	struct qede_dev *edev = netdev_priv(dev);
1633 
1634 	if (IS_VF(edev))
1635 		dev->ethtool_ops = &qede_vf_ethtool_ops;
1636 	else
1637 		dev->ethtool_ops = &qede_ethtool_ops;
1638 }
1639