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