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