1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * aQuantia Corporation Network Driver
4  * Copyright (C) 2014-2019 aQuantia Corporation. All rights reserved
5  */
6 
7 /* File aq_ethtool.c: Definition of ethertool related functions. */
8 
9 #include "aq_ethtool.h"
10 #include "aq_nic.h"
11 #include "aq_vec.h"
12 #include "aq_ptp.h"
13 #include "aq_filters.h"
14 
15 #include <linux/ptp_clock_kernel.h>
16 
17 static void aq_ethtool_get_regs(struct net_device *ndev,
18 				struct ethtool_regs *regs, void *p)
19 {
20 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
21 	u32 regs_count;
22 
23 	regs_count = aq_nic_get_regs_count(aq_nic);
24 
25 	memset(p, 0, regs_count * sizeof(u32));
26 	aq_nic_get_regs(aq_nic, regs, p);
27 }
28 
29 static int aq_ethtool_get_regs_len(struct net_device *ndev)
30 {
31 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
32 	u32 regs_count;
33 
34 	regs_count = aq_nic_get_regs_count(aq_nic);
35 
36 	return regs_count * sizeof(u32);
37 }
38 
39 static u32 aq_ethtool_get_link(struct net_device *ndev)
40 {
41 	return ethtool_op_get_link(ndev);
42 }
43 
44 static int aq_ethtool_get_link_ksettings(struct net_device *ndev,
45 					 struct ethtool_link_ksettings *cmd)
46 {
47 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
48 
49 	aq_nic_get_link_ksettings(aq_nic, cmd);
50 	cmd->base.speed = netif_carrier_ok(ndev) ?
51 				aq_nic_get_link_speed(aq_nic) : 0U;
52 
53 	return 0;
54 }
55 
56 static int
57 aq_ethtool_set_link_ksettings(struct net_device *ndev,
58 			      const struct ethtool_link_ksettings *cmd)
59 {
60 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
61 
62 	return aq_nic_set_link_ksettings(aq_nic, cmd);
63 }
64 
65 static const char aq_ethtool_stat_names[][ETH_GSTRING_LEN] = {
66 	"InPackets",
67 	"InUCast",
68 	"InMCast",
69 	"InBCast",
70 	"InErrors",
71 	"OutPackets",
72 	"OutUCast",
73 	"OutMCast",
74 	"OutBCast",
75 	"InUCastOctets",
76 	"OutUCastOctets",
77 	"InMCastOctets",
78 	"OutMCastOctets",
79 	"InBCastOctets",
80 	"OutBCastOctets",
81 	"InOctets",
82 	"OutOctets",
83 	"InPacketsDma",
84 	"OutPacketsDma",
85 	"InOctetsDma",
86 	"OutOctetsDma",
87 	"InDroppedDma",
88 };
89 
90 static const char aq_ethtool_queue_stat_names[][ETH_GSTRING_LEN] = {
91 	"Queue[%d] InPackets",
92 	"Queue[%d] OutPackets",
93 	"Queue[%d] Restarts",
94 	"Queue[%d] InJumboPackets",
95 	"Queue[%d] InLroPackets",
96 	"Queue[%d] InErrors",
97 };
98 
99 static const char aq_ethtool_priv_flag_names[][ETH_GSTRING_LEN] = {
100 	"DMASystemLoopback",
101 	"PKTSystemLoopback",
102 	"DMANetworkLoopback",
103 	"PHYInternalLoopback",
104 	"PHYExternalLoopback",
105 };
106 
107 static void aq_ethtool_stats(struct net_device *ndev,
108 			     struct ethtool_stats *stats, u64 *data)
109 {
110 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
111 	struct aq_nic_cfg_s *cfg;
112 
113 	cfg = aq_nic_get_cfg(aq_nic);
114 
115 	memset(data, 0, (ARRAY_SIZE(aq_ethtool_stat_names) +
116 			 ARRAY_SIZE(aq_ethtool_queue_stat_names) *
117 			 cfg->vecs) * sizeof(u64));
118 	aq_nic_get_stats(aq_nic, data);
119 }
120 
121 static void aq_ethtool_get_drvinfo(struct net_device *ndev,
122 				   struct ethtool_drvinfo *drvinfo)
123 {
124 	struct pci_dev *pdev = to_pci_dev(ndev->dev.parent);
125 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
126 	struct aq_nic_cfg_s *cfg;
127 	u32 firmware_version;
128 	u32 regs_count;
129 
130 	cfg = aq_nic_get_cfg(aq_nic);
131 	firmware_version = aq_nic_get_fw_version(aq_nic);
132 	regs_count = aq_nic_get_regs_count(aq_nic);
133 
134 	strlcat(drvinfo->driver, AQ_CFG_DRV_NAME, sizeof(drvinfo->driver));
135 	strlcat(drvinfo->version, AQ_CFG_DRV_VERSION, sizeof(drvinfo->version));
136 
137 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
138 		 "%u.%u.%u", firmware_version >> 24,
139 		 (firmware_version >> 16) & 0xFFU, firmware_version & 0xFFFFU);
140 
141 	strlcpy(drvinfo->bus_info, pdev ? pci_name(pdev) : "",
142 		sizeof(drvinfo->bus_info));
143 	drvinfo->n_stats = ARRAY_SIZE(aq_ethtool_stat_names) +
144 		cfg->vecs * ARRAY_SIZE(aq_ethtool_queue_stat_names);
145 	drvinfo->testinfo_len = 0;
146 	drvinfo->regdump_len = regs_count;
147 	drvinfo->eedump_len = 0;
148 }
149 
150 static void aq_ethtool_get_strings(struct net_device *ndev,
151 				   u32 stringset, u8 *data)
152 {
153 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
154 	struct aq_nic_cfg_s *cfg;
155 	u8 *p = data;
156 	int i, si;
157 
158 	cfg = aq_nic_get_cfg(aq_nic);
159 
160 	switch (stringset) {
161 	case ETH_SS_STATS:
162 		memcpy(p, aq_ethtool_stat_names,
163 		       sizeof(aq_ethtool_stat_names));
164 		p = p + sizeof(aq_ethtool_stat_names);
165 		for (i = 0; i < cfg->vecs; i++) {
166 			for (si = 0;
167 				si < ARRAY_SIZE(aq_ethtool_queue_stat_names);
168 				si++) {
169 				snprintf(p, ETH_GSTRING_LEN,
170 					 aq_ethtool_queue_stat_names[si], i);
171 				p += ETH_GSTRING_LEN;
172 			}
173 		}
174 		break;
175 	case ETH_SS_PRIV_FLAGS:
176 		memcpy(p, aq_ethtool_priv_flag_names,
177 		       sizeof(aq_ethtool_priv_flag_names));
178 		break;
179 	}
180 }
181 
182 static int aq_ethtool_set_phys_id(struct net_device *ndev,
183 				  enum ethtool_phys_id_state state)
184 {
185 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
186 	struct aq_hw_s *hw = aq_nic->aq_hw;
187 	int ret = 0;
188 
189 	if (!aq_nic->aq_fw_ops->led_control)
190 		return -EOPNOTSUPP;
191 
192 	mutex_lock(&aq_nic->fwreq_mutex);
193 
194 	switch (state) {
195 	case ETHTOOL_ID_ACTIVE:
196 		ret = aq_nic->aq_fw_ops->led_control(hw, AQ_HW_LED_BLINK |
197 				 AQ_HW_LED_BLINK << 2 | AQ_HW_LED_BLINK << 4);
198 		break;
199 	case ETHTOOL_ID_INACTIVE:
200 		ret = aq_nic->aq_fw_ops->led_control(hw, AQ_HW_LED_DEFAULT);
201 		break;
202 	default:
203 		break;
204 	}
205 
206 	mutex_unlock(&aq_nic->fwreq_mutex);
207 
208 	return ret;
209 }
210 
211 static int aq_ethtool_get_sset_count(struct net_device *ndev, int stringset)
212 {
213 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
214 	struct aq_nic_cfg_s *cfg;
215 	int ret = 0;
216 
217 	cfg = aq_nic_get_cfg(aq_nic);
218 
219 	switch (stringset) {
220 	case ETH_SS_STATS:
221 		ret = ARRAY_SIZE(aq_ethtool_stat_names) +
222 			cfg->vecs * ARRAY_SIZE(aq_ethtool_queue_stat_names);
223 		break;
224 	case ETH_SS_PRIV_FLAGS:
225 		ret = ARRAY_SIZE(aq_ethtool_priv_flag_names);
226 		break;
227 	default:
228 		ret = -EOPNOTSUPP;
229 	}
230 
231 	return ret;
232 }
233 
234 static u32 aq_ethtool_get_rss_indir_size(struct net_device *ndev)
235 {
236 	return AQ_CFG_RSS_INDIRECTION_TABLE_MAX;
237 }
238 
239 static u32 aq_ethtool_get_rss_key_size(struct net_device *ndev)
240 {
241 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
242 	struct aq_nic_cfg_s *cfg;
243 
244 	cfg = aq_nic_get_cfg(aq_nic);
245 
246 	return sizeof(cfg->aq_rss.hash_secret_key);
247 }
248 
249 static int aq_ethtool_get_rss(struct net_device *ndev, u32 *indir, u8 *key,
250 			      u8 *hfunc)
251 {
252 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
253 	struct aq_nic_cfg_s *cfg;
254 	unsigned int i = 0U;
255 
256 	cfg = aq_nic_get_cfg(aq_nic);
257 
258 	if (hfunc)
259 		*hfunc = ETH_RSS_HASH_TOP; /* Toeplitz */
260 	if (indir) {
261 		for (i = 0; i < AQ_CFG_RSS_INDIRECTION_TABLE_MAX; i++)
262 			indir[i] = cfg->aq_rss.indirection_table[i];
263 	}
264 	if (key)
265 		memcpy(key, cfg->aq_rss.hash_secret_key,
266 		       sizeof(cfg->aq_rss.hash_secret_key));
267 
268 	return 0;
269 }
270 
271 static int aq_ethtool_set_rss(struct net_device *netdev, const u32 *indir,
272 			      const u8 *key, const u8 hfunc)
273 {
274 	struct aq_nic_s *aq_nic = netdev_priv(netdev);
275 	struct aq_nic_cfg_s *cfg;
276 	unsigned int i = 0U;
277 	u32 rss_entries;
278 	int err = 0;
279 
280 	cfg = aq_nic_get_cfg(aq_nic);
281 	rss_entries = cfg->aq_rss.indirection_table_size;
282 
283 	/* We do not allow change in unsupported parameters */
284 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
285 		return -EOPNOTSUPP;
286 	/* Fill out the redirection table */
287 	if (indir)
288 		for (i = 0; i < rss_entries; i++)
289 			cfg->aq_rss.indirection_table[i] = indir[i];
290 
291 	/* Fill out the rss hash key */
292 	if (key) {
293 		memcpy(cfg->aq_rss.hash_secret_key, key,
294 		       sizeof(cfg->aq_rss.hash_secret_key));
295 		err = aq_nic->aq_hw_ops->hw_rss_hash_set(aq_nic->aq_hw,
296 			&cfg->aq_rss);
297 		if (err)
298 			return err;
299 	}
300 
301 	err = aq_nic->aq_hw_ops->hw_rss_set(aq_nic->aq_hw, &cfg->aq_rss);
302 
303 	return err;
304 }
305 
306 static int aq_ethtool_get_rxnfc(struct net_device *ndev,
307 				struct ethtool_rxnfc *cmd,
308 				u32 *rule_locs)
309 {
310 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
311 	struct aq_nic_cfg_s *cfg;
312 	int err = 0;
313 
314 	cfg = aq_nic_get_cfg(aq_nic);
315 
316 	switch (cmd->cmd) {
317 	case ETHTOOL_GRXRINGS:
318 		cmd->data = cfg->vecs;
319 		break;
320 	case ETHTOOL_GRXCLSRLCNT:
321 		cmd->rule_cnt = aq_get_rxnfc_count_all_rules(aq_nic);
322 		break;
323 	case ETHTOOL_GRXCLSRULE:
324 		err = aq_get_rxnfc_rule(aq_nic, cmd);
325 		break;
326 	case ETHTOOL_GRXCLSRLALL:
327 		err = aq_get_rxnfc_all_rules(aq_nic, cmd, rule_locs);
328 		break;
329 	default:
330 		err = -EOPNOTSUPP;
331 		break;
332 	}
333 
334 	return err;
335 }
336 
337 static int aq_ethtool_set_rxnfc(struct net_device *ndev,
338 				struct ethtool_rxnfc *cmd)
339 {
340 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
341 	int err = 0;
342 
343 	switch (cmd->cmd) {
344 	case ETHTOOL_SRXCLSRLINS:
345 		err = aq_add_rxnfc_rule(aq_nic, cmd);
346 		break;
347 	case ETHTOOL_SRXCLSRLDEL:
348 		err = aq_del_rxnfc_rule(aq_nic, cmd);
349 		break;
350 	default:
351 		err = -EOPNOTSUPP;
352 		break;
353 	}
354 
355 	return err;
356 }
357 
358 static int aq_ethtool_get_coalesce(struct net_device *ndev,
359 				   struct ethtool_coalesce *coal)
360 {
361 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
362 	struct aq_nic_cfg_s *cfg;
363 
364 	cfg = aq_nic_get_cfg(aq_nic);
365 
366 	if (cfg->itr == AQ_CFG_INTERRUPT_MODERATION_ON ||
367 	    cfg->itr == AQ_CFG_INTERRUPT_MODERATION_AUTO) {
368 		coal->rx_coalesce_usecs = cfg->rx_itr;
369 		coal->tx_coalesce_usecs = cfg->tx_itr;
370 		coal->rx_max_coalesced_frames = 0;
371 		coal->tx_max_coalesced_frames = 0;
372 	} else {
373 		coal->rx_coalesce_usecs = 0;
374 		coal->tx_coalesce_usecs = 0;
375 		coal->rx_max_coalesced_frames = 1;
376 		coal->tx_max_coalesced_frames = 1;
377 	}
378 
379 	return 0;
380 }
381 
382 static int aq_ethtool_set_coalesce(struct net_device *ndev,
383 				   struct ethtool_coalesce *coal)
384 {
385 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
386 	struct aq_nic_cfg_s *cfg;
387 
388 	cfg = aq_nic_get_cfg(aq_nic);
389 
390 	/* This is not yet supported
391 	 */
392 	if (coal->use_adaptive_rx_coalesce || coal->use_adaptive_tx_coalesce)
393 		return -EOPNOTSUPP;
394 
395 	/* Atlantic only supports timing based coalescing
396 	 */
397 	if (coal->rx_max_coalesced_frames > 1 ||
398 	    coal->rx_coalesce_usecs_irq ||
399 	    coal->rx_max_coalesced_frames_irq)
400 		return -EOPNOTSUPP;
401 
402 	if (coal->tx_max_coalesced_frames > 1 ||
403 	    coal->tx_coalesce_usecs_irq ||
404 	    coal->tx_max_coalesced_frames_irq)
405 		return -EOPNOTSUPP;
406 
407 	/* We do not support frame counting. Check this
408 	 */
409 	if (!(coal->rx_max_coalesced_frames == !coal->rx_coalesce_usecs))
410 		return -EOPNOTSUPP;
411 	if (!(coal->tx_max_coalesced_frames == !coal->tx_coalesce_usecs))
412 		return -EOPNOTSUPP;
413 
414 	if (coal->rx_coalesce_usecs > AQ_CFG_INTERRUPT_MODERATION_USEC_MAX ||
415 	    coal->tx_coalesce_usecs > AQ_CFG_INTERRUPT_MODERATION_USEC_MAX)
416 		return -EINVAL;
417 
418 	cfg->itr = AQ_CFG_INTERRUPT_MODERATION_ON;
419 
420 	cfg->rx_itr = coal->rx_coalesce_usecs;
421 	cfg->tx_itr = coal->tx_coalesce_usecs;
422 
423 	return aq_nic_update_interrupt_moderation_settings(aq_nic);
424 }
425 
426 static void aq_ethtool_get_wol(struct net_device *ndev,
427 			       struct ethtool_wolinfo *wol)
428 {
429 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
430 	struct aq_nic_cfg_s *cfg;
431 
432 	cfg = aq_nic_get_cfg(aq_nic);
433 
434 	wol->supported = AQ_NIC_WOL_MODES;
435 	wol->wolopts = cfg->wol;
436 }
437 
438 static int aq_ethtool_set_wol(struct net_device *ndev,
439 			      struct ethtool_wolinfo *wol)
440 {
441 	struct pci_dev *pdev = to_pci_dev(ndev->dev.parent);
442 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
443 	struct aq_nic_cfg_s *cfg;
444 	int err = 0;
445 
446 	cfg = aq_nic_get_cfg(aq_nic);
447 
448 	if (wol->wolopts & ~AQ_NIC_WOL_MODES)
449 		return -EOPNOTSUPP;
450 
451 	cfg->wol = wol->wolopts;
452 
453 	err = device_set_wakeup_enable(&pdev->dev, !!cfg->wol);
454 
455 	return err;
456 }
457 
458 static int aq_ethtool_get_ts_info(struct net_device *ndev,
459 				  struct ethtool_ts_info *info)
460 {
461 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
462 
463 	ethtool_op_get_ts_info(ndev, info);
464 
465 	if (!aq_nic->aq_ptp)
466 		return 0;
467 
468 	info->so_timestamping |=
469 		SOF_TIMESTAMPING_TX_HARDWARE |
470 		SOF_TIMESTAMPING_RX_HARDWARE |
471 		SOF_TIMESTAMPING_RAW_HARDWARE;
472 
473 	info->tx_types = BIT(HWTSTAMP_TX_OFF) |
474 			 BIT(HWTSTAMP_TX_ON);
475 
476 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE);
477 
478 	info->rx_filters |= BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
479 			    BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
480 			    BIT(HWTSTAMP_FILTER_PTP_V2_EVENT);
481 
482 	info->phc_index = ptp_clock_index(aq_ptp_get_ptp_clock(aq_nic->aq_ptp));
483 
484 	return 0;
485 }
486 
487 static enum hw_atl_fw2x_rate eee_mask_to_ethtool_mask(u32 speed)
488 {
489 	u32 rate = 0;
490 
491 	if (speed & AQ_NIC_RATE_EEE_10G)
492 		rate |= SUPPORTED_10000baseT_Full;
493 
494 	if (speed & AQ_NIC_RATE_EEE_2GS)
495 		rate |= SUPPORTED_2500baseX_Full;
496 
497 	if (speed & AQ_NIC_RATE_EEE_1G)
498 		rate |= SUPPORTED_1000baseT_Full;
499 
500 	return rate;
501 }
502 
503 static int aq_ethtool_get_eee(struct net_device *ndev, struct ethtool_eee *eee)
504 {
505 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
506 	u32 rate, supported_rates;
507 	int err = 0;
508 
509 	if (!aq_nic->aq_fw_ops->get_eee_rate)
510 		return -EOPNOTSUPP;
511 
512 	mutex_lock(&aq_nic->fwreq_mutex);
513 	err = aq_nic->aq_fw_ops->get_eee_rate(aq_nic->aq_hw, &rate,
514 					      &supported_rates);
515 	mutex_unlock(&aq_nic->fwreq_mutex);
516 	if (err < 0)
517 		return err;
518 
519 	eee->supported = eee_mask_to_ethtool_mask(supported_rates);
520 
521 	if (aq_nic->aq_nic_cfg.eee_speeds)
522 		eee->advertised = eee->supported;
523 
524 	eee->lp_advertised = eee_mask_to_ethtool_mask(rate);
525 
526 	eee->eee_enabled = !!eee->advertised;
527 
528 	eee->tx_lpi_enabled = eee->eee_enabled;
529 	if (eee->advertised & eee->lp_advertised)
530 		eee->eee_active = true;
531 
532 	return 0;
533 }
534 
535 static int aq_ethtool_set_eee(struct net_device *ndev, struct ethtool_eee *eee)
536 {
537 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
538 	u32 rate, supported_rates;
539 	struct aq_nic_cfg_s *cfg;
540 	int err = 0;
541 
542 	cfg = aq_nic_get_cfg(aq_nic);
543 
544 	if (unlikely(!aq_nic->aq_fw_ops->get_eee_rate ||
545 		     !aq_nic->aq_fw_ops->set_eee_rate))
546 		return -EOPNOTSUPP;
547 
548 	mutex_lock(&aq_nic->fwreq_mutex);
549 	err = aq_nic->aq_fw_ops->get_eee_rate(aq_nic->aq_hw, &rate,
550 					      &supported_rates);
551 	mutex_unlock(&aq_nic->fwreq_mutex);
552 	if (err < 0)
553 		return err;
554 
555 	if (eee->eee_enabled) {
556 		rate = supported_rates;
557 		cfg->eee_speeds = rate;
558 	} else {
559 		rate = 0;
560 		cfg->eee_speeds = 0;
561 	}
562 
563 	mutex_lock(&aq_nic->fwreq_mutex);
564 	err = aq_nic->aq_fw_ops->set_eee_rate(aq_nic->aq_hw, rate);
565 	mutex_unlock(&aq_nic->fwreq_mutex);
566 
567 	return err;
568 }
569 
570 static int aq_ethtool_nway_reset(struct net_device *ndev)
571 {
572 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
573 	int err = 0;
574 
575 	if (unlikely(!aq_nic->aq_fw_ops->renegotiate))
576 		return -EOPNOTSUPP;
577 
578 	if (netif_running(ndev)) {
579 		mutex_lock(&aq_nic->fwreq_mutex);
580 		err = aq_nic->aq_fw_ops->renegotiate(aq_nic->aq_hw);
581 		mutex_unlock(&aq_nic->fwreq_mutex);
582 	}
583 
584 	return err;
585 }
586 
587 static void aq_ethtool_get_pauseparam(struct net_device *ndev,
588 				      struct ethtool_pauseparam *pause)
589 {
590 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
591 	u32 fc = aq_nic->aq_nic_cfg.fc.req;
592 
593 	pause->autoneg = 0;
594 
595 	pause->rx_pause = !!(fc & AQ_NIC_FC_RX);
596 	pause->tx_pause = !!(fc & AQ_NIC_FC_TX);
597 
598 }
599 
600 static int aq_ethtool_set_pauseparam(struct net_device *ndev,
601 				     struct ethtool_pauseparam *pause)
602 {
603 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
604 	int err = 0;
605 
606 	if (!aq_nic->aq_fw_ops->set_flow_control)
607 		return -EOPNOTSUPP;
608 
609 	if (pause->autoneg == AUTONEG_ENABLE)
610 		return -EOPNOTSUPP;
611 
612 	if (pause->rx_pause)
613 		aq_nic->aq_hw->aq_nic_cfg->fc.req |= AQ_NIC_FC_RX;
614 	else
615 		aq_nic->aq_hw->aq_nic_cfg->fc.req &= ~AQ_NIC_FC_RX;
616 
617 	if (pause->tx_pause)
618 		aq_nic->aq_hw->aq_nic_cfg->fc.req |= AQ_NIC_FC_TX;
619 	else
620 		aq_nic->aq_hw->aq_nic_cfg->fc.req &= ~AQ_NIC_FC_TX;
621 
622 	mutex_lock(&aq_nic->fwreq_mutex);
623 	err = aq_nic->aq_fw_ops->set_flow_control(aq_nic->aq_hw);
624 	mutex_unlock(&aq_nic->fwreq_mutex);
625 
626 	return err;
627 }
628 
629 static void aq_get_ringparam(struct net_device *ndev,
630 			     struct ethtool_ringparam *ring)
631 {
632 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
633 	struct aq_nic_cfg_s *cfg;
634 
635 	cfg = aq_nic_get_cfg(aq_nic);
636 
637 	ring->rx_pending = cfg->rxds;
638 	ring->tx_pending = cfg->txds;
639 
640 	ring->rx_max_pending = cfg->aq_hw_caps->rxds_max;
641 	ring->tx_max_pending = cfg->aq_hw_caps->txds_max;
642 }
643 
644 static int aq_set_ringparam(struct net_device *ndev,
645 			    struct ethtool_ringparam *ring)
646 {
647 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
648 	const struct aq_hw_caps_s *hw_caps;
649 	bool ndev_running = false;
650 	struct aq_nic_cfg_s *cfg;
651 	int err = 0;
652 
653 	cfg = aq_nic_get_cfg(aq_nic);
654 	hw_caps = cfg->aq_hw_caps;
655 
656 	if (ring->rx_mini_pending || ring->rx_jumbo_pending) {
657 		err = -EOPNOTSUPP;
658 		goto err_exit;
659 	}
660 
661 	if (netif_running(ndev)) {
662 		ndev_running = true;
663 		dev_close(ndev);
664 	}
665 
666 	aq_nic_free_vectors(aq_nic);
667 
668 	cfg->rxds = max(ring->rx_pending, hw_caps->rxds_min);
669 	cfg->rxds = min(cfg->rxds, hw_caps->rxds_max);
670 	cfg->rxds = ALIGN(cfg->rxds, AQ_HW_RXD_MULTIPLE);
671 
672 	cfg->txds = max(ring->tx_pending, hw_caps->txds_min);
673 	cfg->txds = min(cfg->txds, hw_caps->txds_max);
674 	cfg->txds = ALIGN(cfg->txds, AQ_HW_TXD_MULTIPLE);
675 
676 	for (aq_nic->aq_vecs = 0; aq_nic->aq_vecs < cfg->vecs;
677 	     aq_nic->aq_vecs++) {
678 		aq_nic->aq_vec[aq_nic->aq_vecs] =
679 		    aq_vec_alloc(aq_nic, aq_nic->aq_vecs, cfg);
680 		if (unlikely(!aq_nic->aq_vec[aq_nic->aq_vecs])) {
681 			err = -ENOMEM;
682 			goto err_exit;
683 		}
684 	}
685 	if (ndev_running)
686 		err = dev_open(ndev, NULL);
687 
688 err_exit:
689 	return err;
690 }
691 
692 static u32 aq_get_msg_level(struct net_device *ndev)
693 {
694 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
695 
696 	return aq_nic->msg_enable;
697 }
698 
699 static void aq_set_msg_level(struct net_device *ndev, u32 data)
700 {
701 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
702 
703 	aq_nic->msg_enable = data;
704 }
705 
706 static u32 aq_ethtool_get_priv_flags(struct net_device *ndev)
707 {
708 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
709 
710 	return aq_nic->aq_nic_cfg.priv_flags;
711 }
712 
713 static int aq_ethtool_set_priv_flags(struct net_device *ndev, u32 flags)
714 {
715 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
716 	struct aq_nic_cfg_s *cfg;
717 	u32 priv_flags;
718 
719 	cfg = aq_nic_get_cfg(aq_nic);
720 	priv_flags = cfg->priv_flags;
721 
722 	if (flags & ~AQ_PRIV_FLAGS_MASK)
723 		return -EOPNOTSUPP;
724 
725 	cfg->priv_flags = flags;
726 
727 	if ((priv_flags ^ flags) & BIT(AQ_HW_LOOPBACK_DMA_NET)) {
728 		if (netif_running(ndev)) {
729 			dev_close(ndev);
730 
731 			dev_open(ndev, NULL);
732 		}
733 	} else if ((priv_flags ^ flags) & AQ_HW_LOOPBACK_MASK) {
734 		aq_nic_set_loopback(aq_nic);
735 	}
736 
737 	return 0;
738 }
739 
740 const struct ethtool_ops aq_ethtool_ops = {
741 	.get_link            = aq_ethtool_get_link,
742 	.get_regs_len        = aq_ethtool_get_regs_len,
743 	.get_regs            = aq_ethtool_get_regs,
744 	.get_drvinfo         = aq_ethtool_get_drvinfo,
745 	.get_strings         = aq_ethtool_get_strings,
746 	.set_phys_id         = aq_ethtool_set_phys_id,
747 	.get_rxfh_indir_size = aq_ethtool_get_rss_indir_size,
748 	.get_wol             = aq_ethtool_get_wol,
749 	.set_wol             = aq_ethtool_set_wol,
750 	.nway_reset          = aq_ethtool_nway_reset,
751 	.get_ringparam       = aq_get_ringparam,
752 	.set_ringparam       = aq_set_ringparam,
753 	.get_eee             = aq_ethtool_get_eee,
754 	.set_eee             = aq_ethtool_set_eee,
755 	.get_pauseparam      = aq_ethtool_get_pauseparam,
756 	.set_pauseparam      = aq_ethtool_set_pauseparam,
757 	.get_rxfh_key_size   = aq_ethtool_get_rss_key_size,
758 	.get_rxfh            = aq_ethtool_get_rss,
759 	.set_rxfh            = aq_ethtool_set_rss,
760 	.get_rxnfc           = aq_ethtool_get_rxnfc,
761 	.set_rxnfc           = aq_ethtool_set_rxnfc,
762 	.get_msglevel        = aq_get_msg_level,
763 	.set_msglevel        = aq_set_msg_level,
764 	.get_sset_count      = aq_ethtool_get_sset_count,
765 	.get_ethtool_stats   = aq_ethtool_stats,
766 	.get_priv_flags      = aq_ethtool_get_priv_flags,
767 	.set_priv_flags      = aq_ethtool_set_priv_flags,
768 	.get_link_ksettings  = aq_ethtool_get_link_ksettings,
769 	.set_link_ksettings  = aq_ethtool_set_link_ksettings,
770 	.get_coalesce	     = aq_ethtool_get_coalesce,
771 	.set_coalesce	     = aq_ethtool_set_coalesce,
772 	.get_ts_info         = aq_ethtool_get_ts_info,
773 };
774