xref: /openbmc/linux/drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c (revision a1b2f04ea527397fcacacd09e0d690927feef429)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3 
4 #include <linux/etherdevice.h>
5 #include <linux/string.h>
6 #include <linux/phy.h>
7 
8 #include "hns3_enet.h"
9 
10 struct hns3_stats {
11 	char stats_string[ETH_GSTRING_LEN];
12 	int stats_offset;
13 };
14 
15 /* tqp related stats */
16 #define HNS3_TQP_STAT(_string, _member)	{			\
17 	.stats_string = _string,				\
18 	.stats_offset = offsetof(struct hns3_enet_ring, stats) +\
19 			offsetof(struct ring_stats, _member),   \
20 }
21 
22 static const struct hns3_stats hns3_txq_stats[] = {
23 	/* Tx per-queue statistics */
24 	HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
25 	HNS3_TQP_STAT("dropped", sw_err_cnt),
26 	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
27 	HNS3_TQP_STAT("packets", tx_pkts),
28 	HNS3_TQP_STAT("bytes", tx_bytes),
29 	HNS3_TQP_STAT("errors", tx_err_cnt),
30 	HNS3_TQP_STAT("wake", restart_queue),
31 	HNS3_TQP_STAT("busy", tx_busy),
32 	HNS3_TQP_STAT("copy", tx_copy),
33 };
34 
35 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
36 
37 static const struct hns3_stats hns3_rxq_stats[] = {
38 	/* Rx per-queue statistics */
39 	HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
40 	HNS3_TQP_STAT("dropped", sw_err_cnt),
41 	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
42 	HNS3_TQP_STAT("packets", rx_pkts),
43 	HNS3_TQP_STAT("bytes", rx_bytes),
44 	HNS3_TQP_STAT("errors", rx_err_cnt),
45 	HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
46 	HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
47 	HNS3_TQP_STAT("err_bd_num", err_bd_num),
48 	HNS3_TQP_STAT("l2_err", l2_err),
49 	HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
50 	HNS3_TQP_STAT("multicast", rx_multicast),
51 	HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
52 };
53 
54 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
55 
56 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
57 
58 #define HNS3_SELF_TEST_TYPE_NUM         3
59 #define HNS3_NIC_LB_TEST_PKT_NUM	1
60 #define HNS3_NIC_LB_TEST_RING_ID	0
61 #define HNS3_NIC_LB_TEST_PACKET_SIZE	128
62 #define HNS3_NIC_LB_SETUP_USEC		10000
63 
64 /* Nic loopback test err  */
65 #define HNS3_NIC_LB_TEST_NO_MEM_ERR	1
66 #define HNS3_NIC_LB_TEST_TX_CNT_ERR	2
67 #define HNS3_NIC_LB_TEST_RX_CNT_ERR	3
68 
69 struct hns3_link_mode_mapping {
70 	u32 hns3_link_mode;
71 	u32 ethtool_link_mode;
72 };
73 
74 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
75 {
76 	struct hnae3_handle *h = hns3_get_handle(ndev);
77 	bool vlan_filter_enable;
78 	int ret;
79 
80 	if (!h->ae_algo->ops->set_loopback ||
81 	    !h->ae_algo->ops->set_promisc_mode)
82 		return -EOPNOTSUPP;
83 
84 	switch (loop) {
85 	case HNAE3_LOOP_SERIAL_SERDES:
86 	case HNAE3_LOOP_PARALLEL_SERDES:
87 	case HNAE3_LOOP_APP:
88 		ret = h->ae_algo->ops->set_loopback(h, loop, en);
89 		break;
90 	default:
91 		ret = -ENOTSUPP;
92 		break;
93 	}
94 
95 	if (ret)
96 		return ret;
97 
98 	if (en) {
99 		h->ae_algo->ops->set_promisc_mode(h, true, true);
100 	} else {
101 		/* recover promisc mode before loopback test */
102 		hns3_update_promisc_mode(ndev, h->netdev_flags);
103 		vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
104 		hns3_enable_vlan_filter(ndev, vlan_filter_enable);
105 	}
106 
107 	return ret;
108 }
109 
110 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
111 {
112 	struct hnae3_handle *h = hns3_get_handle(ndev);
113 	int ret;
114 
115 	ret = hns3_nic_reset_all_ring(h);
116 	if (ret)
117 		return ret;
118 
119 	ret = hns3_lp_setup(ndev, loop_mode, true);
120 	usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
121 
122 	return ret;
123 }
124 
125 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
126 {
127 	int ret;
128 
129 	ret = hns3_lp_setup(ndev, loop_mode, false);
130 	if (ret) {
131 		netdev_err(ndev, "lb_setup return error: %d\n", ret);
132 		return ret;
133 	}
134 
135 	usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
136 
137 	return 0;
138 }
139 
140 static void hns3_lp_setup_skb(struct sk_buff *skb)
141 {
142 	struct net_device *ndev = skb->dev;
143 	unsigned char *packet;
144 	struct ethhdr *ethh;
145 	unsigned int i;
146 
147 	skb_reserve(skb, NET_IP_ALIGN);
148 	ethh = skb_put(skb, sizeof(struct ethhdr));
149 	packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
150 
151 	memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
152 
153 	/* The dst mac addr of loopback packet is the same as the host'
154 	 * mac addr, the SSU component may loop back the packet to host
155 	 * before the packet reaches mac or serdes, which will defect
156 	 * the purpose of mac or serdes selftest.
157 	 */
158 	ethh->h_dest[5] += 0x1f;
159 	eth_zero_addr(ethh->h_source);
160 	ethh->h_proto = htons(ETH_P_ARP);
161 	skb_reset_mac_header(skb);
162 
163 	for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
164 		packet[i] = (unsigned char)(i & 0xff);
165 }
166 
167 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
168 				   struct sk_buff *skb)
169 {
170 	struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
171 	unsigned char *packet = skb->data;
172 	u32 i;
173 
174 	for (i = 0; i < skb->len; i++)
175 		if (packet[i] != (unsigned char)(i & 0xff))
176 			break;
177 
178 	/* The packet is correctly received */
179 	if (i == skb->len)
180 		tqp_vector->rx_group.total_packets++;
181 	else
182 		print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
183 			       skb->data, skb->len, true);
184 
185 	dev_kfree_skb_any(skb);
186 }
187 
188 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
189 {
190 	struct hnae3_handle *h = priv->ae_handle;
191 	struct hnae3_knic_private_info *kinfo;
192 	u32 i, rcv_good_pkt_total = 0;
193 
194 	kinfo = &h->kinfo;
195 	for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
196 		struct hns3_enet_ring *ring = priv->ring_data[i].ring;
197 		struct hns3_enet_ring_group *rx_group;
198 		u64 pre_rx_pkt;
199 
200 		rx_group = &ring->tqp_vector->rx_group;
201 		pre_rx_pkt = rx_group->total_packets;
202 
203 		preempt_disable();
204 		hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
205 		preempt_enable();
206 
207 		rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
208 		rx_group->total_packets = pre_rx_pkt;
209 	}
210 	return rcv_good_pkt_total;
211 }
212 
213 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
214 				  u32 end_ringid, u32 budget)
215 {
216 	u32 i;
217 
218 	for (i = start_ringid; i <= end_ringid; i++) {
219 		struct hns3_enet_ring *ring = priv->ring_data[i].ring;
220 
221 		hns3_clean_tx_ring(ring);
222 	}
223 }
224 
225 /**
226  * hns3_lp_run_test -  run loopback test
227  * @ndev: net device
228  * @mode: loopback type
229  */
230 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
231 {
232 	struct hns3_nic_priv *priv = netdev_priv(ndev);
233 	struct sk_buff *skb;
234 	u32 i, good_cnt;
235 	int ret_val = 0;
236 
237 	skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
238 			GFP_KERNEL);
239 	if (!skb)
240 		return HNS3_NIC_LB_TEST_NO_MEM_ERR;
241 
242 	skb->dev = ndev;
243 	hns3_lp_setup_skb(skb);
244 	skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
245 
246 	good_cnt = 0;
247 	for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
248 		netdev_tx_t tx_ret;
249 
250 		skb_get(skb);
251 		tx_ret = hns3_nic_net_xmit(skb, ndev);
252 		if (tx_ret == NETDEV_TX_OK) {
253 			good_cnt++;
254 		} else {
255 			kfree_skb(skb);
256 			netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
257 				   tx_ret);
258 		}
259 	}
260 	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
261 		ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
262 		netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
263 			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
264 		goto out;
265 	}
266 
267 	/* Allow 200 milliseconds for packets to go from Tx to Rx */
268 	msleep(200);
269 
270 	good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
271 	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
272 		ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
273 		netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
274 			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
275 	}
276 
277 out:
278 	hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
279 			      HNS3_NIC_LB_TEST_RING_ID,
280 			      HNS3_NIC_LB_TEST_PKT_NUM);
281 
282 	kfree_skb(skb);
283 	return ret_val;
284 }
285 
286 /**
287  * hns3_nic_self_test - self test
288  * @ndev: net device
289  * @eth_test: test cmd
290  * @data: test result
291  */
292 static void hns3_self_test(struct net_device *ndev,
293 			   struct ethtool_test *eth_test, u64 *data)
294 {
295 	struct hns3_nic_priv *priv = netdev_priv(ndev);
296 	struct hnae3_handle *h = priv->ae_handle;
297 	int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
298 	bool if_running = netif_running(ndev);
299 #if IS_ENABLED(CONFIG_VLAN_8021Q)
300 	bool dis_vlan_filter;
301 #endif
302 	int test_index = 0;
303 	u32 i;
304 
305 	if (hns3_nic_resetting(ndev)) {
306 		netdev_err(ndev, "dev resetting!");
307 		return;
308 	}
309 
310 	/* Only do offline selftest, or pass by default */
311 	if (eth_test->flags != ETH_TEST_FL_OFFLINE)
312 		return;
313 
314 	netif_dbg(h, drv, ndev, "self test start");
315 
316 	st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
317 	st_param[HNAE3_LOOP_APP][1] =
318 			h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
319 
320 	st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
321 	st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
322 			h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
323 
324 	st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
325 			HNAE3_LOOP_PARALLEL_SERDES;
326 	st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
327 			h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
328 
329 	if (if_running)
330 		ndev->netdev_ops->ndo_stop(ndev);
331 
332 #if IS_ENABLED(CONFIG_VLAN_8021Q)
333 	/* Disable the vlan filter for selftest does not support it */
334 	dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
335 				h->ae_algo->ops->enable_vlan_filter;
336 	if (dis_vlan_filter)
337 		h->ae_algo->ops->enable_vlan_filter(h, false);
338 #endif
339 
340 	/* Tell firmware to stop mac autoneg before loopback test start,
341 	 * otherwise loopback test may be failed when the port is still
342 	 * negotiating.
343 	 */
344 	if (h->ae_algo->ops->halt_autoneg)
345 		h->ae_algo->ops->halt_autoneg(h, true);
346 
347 	set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
348 
349 	for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
350 		enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
351 
352 		if (!st_param[i][1])
353 			continue;
354 
355 		data[test_index] = hns3_lp_up(ndev, loop_type);
356 		if (!data[test_index])
357 			data[test_index] = hns3_lp_run_test(ndev, loop_type);
358 
359 		hns3_lp_down(ndev, loop_type);
360 
361 		if (data[test_index])
362 			eth_test->flags |= ETH_TEST_FL_FAILED;
363 
364 		test_index++;
365 	}
366 
367 	clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
368 
369 	if (h->ae_algo->ops->halt_autoneg)
370 		h->ae_algo->ops->halt_autoneg(h, false);
371 
372 #if IS_ENABLED(CONFIG_VLAN_8021Q)
373 	if (dis_vlan_filter)
374 		h->ae_algo->ops->enable_vlan_filter(h, true);
375 #endif
376 
377 	if (if_running)
378 		ndev->netdev_ops->ndo_open(ndev);
379 
380 	netif_dbg(h, drv, ndev, "self test end\n");
381 }
382 
383 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
384 {
385 	struct hnae3_handle *h = hns3_get_handle(netdev);
386 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
387 
388 	if (!ops->get_sset_count)
389 		return -EOPNOTSUPP;
390 
391 	switch (stringset) {
392 	case ETH_SS_STATS:
393 		return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
394 			ops->get_sset_count(h, stringset));
395 
396 	case ETH_SS_TEST:
397 		return ops->get_sset_count(h, stringset);
398 
399 	default:
400 		return -EOPNOTSUPP;
401 	}
402 }
403 
404 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
405 		u32 stat_count, u32 num_tqps, const char *prefix)
406 {
407 #define MAX_PREFIX_SIZE (6 + 4)
408 	u32 size_left;
409 	u32 i, j;
410 	u32 n1;
411 
412 	for (i = 0; i < num_tqps; i++) {
413 		for (j = 0; j < stat_count; j++) {
414 			data[ETH_GSTRING_LEN - 1] = '\0';
415 
416 			/* first, prepend the prefix string */
417 			n1 = snprintf(data, MAX_PREFIX_SIZE, "%s%d_",
418 				      prefix, i);
419 			n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
420 			size_left = (ETH_GSTRING_LEN - 1) - n1;
421 
422 			/* now, concatenate the stats string to it */
423 			strncat(data, stats[j].stats_string, size_left);
424 			data += ETH_GSTRING_LEN;
425 		}
426 	}
427 
428 	return data;
429 }
430 
431 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
432 {
433 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
434 	const char tx_prefix[] = "txq";
435 	const char rx_prefix[] = "rxq";
436 
437 	/* get strings for Tx */
438 	data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
439 				   kinfo->num_tqps, tx_prefix);
440 
441 	/* get strings for Rx */
442 	data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
443 				   kinfo->num_tqps, rx_prefix);
444 
445 	return data;
446 }
447 
448 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
449 {
450 	struct hnae3_handle *h = hns3_get_handle(netdev);
451 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
452 	char *buff = (char *)data;
453 
454 	if (!ops->get_strings)
455 		return;
456 
457 	switch (stringset) {
458 	case ETH_SS_STATS:
459 		buff = hns3_get_strings_tqps(h, buff);
460 		ops->get_strings(h, stringset, (u8 *)buff);
461 		break;
462 	case ETH_SS_TEST:
463 		ops->get_strings(h, stringset, data);
464 		break;
465 	default:
466 		break;
467 	}
468 }
469 
470 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
471 {
472 	struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
473 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
474 	struct hns3_enet_ring *ring;
475 	u8 *stat;
476 	int i, j;
477 
478 	/* get stats for Tx */
479 	for (i = 0; i < kinfo->num_tqps; i++) {
480 		ring = nic_priv->ring_data[i].ring;
481 		for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
482 			stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
483 			*data++ = *(u64 *)stat;
484 		}
485 	}
486 
487 	/* get stats for Rx */
488 	for (i = 0; i < kinfo->num_tqps; i++) {
489 		ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
490 		for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
491 			stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
492 			*data++ = *(u64 *)stat;
493 		}
494 	}
495 
496 	return data;
497 }
498 
499 /* hns3_get_stats - get detail statistics.
500  * @netdev: net device
501  * @stats: statistics info.
502  * @data: statistics data.
503  */
504 static void hns3_get_stats(struct net_device *netdev,
505 			   struct ethtool_stats *stats, u64 *data)
506 {
507 	struct hnae3_handle *h = hns3_get_handle(netdev);
508 	u64 *p = data;
509 
510 	if (hns3_nic_resetting(netdev)) {
511 		netdev_err(netdev, "dev resetting, could not get stats\n");
512 		return;
513 	}
514 
515 	if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
516 		netdev_err(netdev, "could not get any statistics\n");
517 		return;
518 	}
519 
520 	h->ae_algo->ops->update_stats(h, &netdev->stats);
521 
522 	/* get per-queue stats */
523 	p = hns3_get_stats_tqps(h, p);
524 
525 	/* get MAC & other misc hardware stats */
526 	h->ae_algo->ops->get_stats(h, p);
527 }
528 
529 static void hns3_get_drvinfo(struct net_device *netdev,
530 			     struct ethtool_drvinfo *drvinfo)
531 {
532 	struct hns3_nic_priv *priv = netdev_priv(netdev);
533 	struct hnae3_handle *h = priv->ae_handle;
534 	u32 fw_version;
535 
536 	if (!h->ae_algo->ops->get_fw_version) {
537 		netdev_err(netdev, "could not get fw version!\n");
538 		return;
539 	}
540 
541 	strncpy(drvinfo->version, hns3_driver_version,
542 		sizeof(drvinfo->version));
543 	drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
544 
545 	strncpy(drvinfo->driver, h->pdev->driver->name,
546 		sizeof(drvinfo->driver));
547 	drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
548 
549 	strncpy(drvinfo->bus_info, pci_name(h->pdev),
550 		sizeof(drvinfo->bus_info));
551 	drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
552 
553 	fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
554 
555 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
556 		 "%lu.%lu.%lu.%lu",
557 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
558 				 HNAE3_FW_VERSION_BYTE3_SHIFT),
559 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
560 				 HNAE3_FW_VERSION_BYTE2_SHIFT),
561 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
562 				 HNAE3_FW_VERSION_BYTE1_SHIFT),
563 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
564 				 HNAE3_FW_VERSION_BYTE0_SHIFT));
565 }
566 
567 static u32 hns3_get_link(struct net_device *netdev)
568 {
569 	struct hnae3_handle *h = hns3_get_handle(netdev);
570 
571 	if (h->ae_algo->ops->get_status)
572 		return h->ae_algo->ops->get_status(h);
573 	else
574 		return 0;
575 }
576 
577 static void hns3_get_ringparam(struct net_device *netdev,
578 			       struct ethtool_ringparam *param)
579 {
580 	struct hns3_nic_priv *priv = netdev_priv(netdev);
581 	struct hnae3_handle *h = priv->ae_handle;
582 	int queue_num = h->kinfo.num_tqps;
583 
584 	if (hns3_nic_resetting(netdev)) {
585 		netdev_err(netdev, "dev resetting!");
586 		return;
587 	}
588 
589 	param->tx_max_pending = HNS3_RING_MAX_PENDING;
590 	param->rx_max_pending = HNS3_RING_MAX_PENDING;
591 
592 	param->tx_pending = priv->ring_data[0].ring->desc_num;
593 	param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
594 }
595 
596 static void hns3_get_pauseparam(struct net_device *netdev,
597 				struct ethtool_pauseparam *param)
598 {
599 	struct hnae3_handle *h = hns3_get_handle(netdev);
600 
601 	if (h->ae_algo->ops->get_pauseparam)
602 		h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
603 			&param->rx_pause, &param->tx_pause);
604 }
605 
606 static int hns3_set_pauseparam(struct net_device *netdev,
607 			       struct ethtool_pauseparam *param)
608 {
609 	struct hnae3_handle *h = hns3_get_handle(netdev);
610 
611 	netif_dbg(h, drv, netdev,
612 		  "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
613 		  param->autoneg, param->rx_pause, param->tx_pause);
614 
615 	if (h->ae_algo->ops->set_pauseparam)
616 		return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
617 						       param->rx_pause,
618 						       param->tx_pause);
619 	return -EOPNOTSUPP;
620 }
621 
622 static void hns3_get_ksettings(struct hnae3_handle *h,
623 			       struct ethtool_link_ksettings *cmd)
624 {
625 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
626 
627 	/* 1.auto_neg & speed & duplex from cmd */
628 	if (ops->get_ksettings_an_result)
629 		ops->get_ksettings_an_result(h,
630 					     &cmd->base.autoneg,
631 					     &cmd->base.speed,
632 					     &cmd->base.duplex);
633 
634 	/* 2.get link mode*/
635 	if (ops->get_link_mode)
636 		ops->get_link_mode(h,
637 				   cmd->link_modes.supported,
638 				   cmd->link_modes.advertising);
639 
640 	/* 3.mdix_ctrl&mdix get from phy reg */
641 	if (ops->get_mdix_mode)
642 		ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
643 				   &cmd->base.eth_tp_mdix);
644 }
645 
646 static int hns3_get_link_ksettings(struct net_device *netdev,
647 				   struct ethtool_link_ksettings *cmd)
648 {
649 	struct hnae3_handle *h = hns3_get_handle(netdev);
650 	const struct hnae3_ae_ops *ops;
651 	u8 module_type;
652 	u8 media_type;
653 	u8 link_stat;
654 
655 	ops = h->ae_algo->ops;
656 	if (ops->get_media_type)
657 		ops->get_media_type(h, &media_type, &module_type);
658 	else
659 		return -EOPNOTSUPP;
660 
661 	switch (media_type) {
662 	case HNAE3_MEDIA_TYPE_NONE:
663 		cmd->base.port = PORT_NONE;
664 		hns3_get_ksettings(h, cmd);
665 		break;
666 	case HNAE3_MEDIA_TYPE_FIBER:
667 		if (module_type == HNAE3_MODULE_TYPE_CR)
668 			cmd->base.port = PORT_DA;
669 		else
670 			cmd->base.port = PORT_FIBRE;
671 
672 		hns3_get_ksettings(h, cmd);
673 		break;
674 	case HNAE3_MEDIA_TYPE_BACKPLANE:
675 		cmd->base.port = PORT_NONE;
676 		hns3_get_ksettings(h, cmd);
677 		break;
678 	case HNAE3_MEDIA_TYPE_COPPER:
679 		cmd->base.port = PORT_TP;
680 		if (!netdev->phydev)
681 			hns3_get_ksettings(h, cmd);
682 		else
683 			phy_ethtool_ksettings_get(netdev->phydev, cmd);
684 		break;
685 	default:
686 
687 		netdev_warn(netdev, "Unknown media type");
688 		return 0;
689 	}
690 
691 	/* mdio_support */
692 	cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
693 
694 	link_stat = hns3_get_link(netdev);
695 	if (!link_stat) {
696 		cmd->base.speed = SPEED_UNKNOWN;
697 		cmd->base.duplex = DUPLEX_UNKNOWN;
698 	}
699 
700 	return 0;
701 }
702 
703 static int hns3_check_ksettings_param(struct net_device *netdev,
704 				      const struct ethtool_link_ksettings *cmd)
705 {
706 	struct hnae3_handle *handle = hns3_get_handle(netdev);
707 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
708 	u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
709 	u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
710 	u8 autoneg;
711 	u32 speed;
712 	u8 duplex;
713 	int ret;
714 
715 	if (ops->get_ksettings_an_result) {
716 		ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
717 		if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
718 		    cmd->base.duplex == duplex)
719 			return 0;
720 	}
721 
722 	if (ops->get_media_type)
723 		ops->get_media_type(handle, &media_type, &module_type);
724 
725 	if (cmd->base.duplex != DUPLEX_FULL &&
726 	    media_type != HNAE3_MEDIA_TYPE_COPPER) {
727 		netdev_err(netdev,
728 			   "only copper port supports half duplex!");
729 		return -EINVAL;
730 	}
731 
732 	if (ops->check_port_speed) {
733 		ret = ops->check_port_speed(handle, cmd->base.speed);
734 		if (ret) {
735 			netdev_err(netdev, "unsupported speed\n");
736 			return ret;
737 		}
738 	}
739 
740 	return 0;
741 }
742 
743 static int hns3_set_link_ksettings(struct net_device *netdev,
744 				   const struct ethtool_link_ksettings *cmd)
745 {
746 	struct hnae3_handle *handle = hns3_get_handle(netdev);
747 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
748 	int ret = 0;
749 
750 	/* Chip don't support this mode. */
751 	if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
752 		return -EINVAL;
753 
754 	netif_dbg(handle, drv, netdev,
755 		  "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
756 		  netdev->phydev ? "phy" : "mac",
757 		  cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
758 
759 	/* Only support ksettings_set for netdev with phy attached for now */
760 	if (netdev->phydev)
761 		return phy_ethtool_ksettings_set(netdev->phydev, cmd);
762 
763 	if (handle->pdev->revision == 0x20)
764 		return -EOPNOTSUPP;
765 
766 	ret = hns3_check_ksettings_param(netdev, cmd);
767 	if (ret)
768 		return ret;
769 
770 	if (ops->set_autoneg) {
771 		ret = ops->set_autoneg(handle, cmd->base.autoneg);
772 		if (ret)
773 			return ret;
774 	}
775 
776 	if (ops->cfg_mac_speed_dup_h)
777 		ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
778 					       cmd->base.duplex);
779 
780 	return ret;
781 }
782 
783 static u32 hns3_get_rss_key_size(struct net_device *netdev)
784 {
785 	struct hnae3_handle *h = hns3_get_handle(netdev);
786 
787 	if (!h->ae_algo->ops->get_rss_key_size)
788 		return 0;
789 
790 	return h->ae_algo->ops->get_rss_key_size(h);
791 }
792 
793 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
794 {
795 	struct hnae3_handle *h = hns3_get_handle(netdev);
796 
797 	if (!h->ae_algo->ops->get_rss_indir_size)
798 		return 0;
799 
800 	return h->ae_algo->ops->get_rss_indir_size(h);
801 }
802 
803 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
804 			u8 *hfunc)
805 {
806 	struct hnae3_handle *h = hns3_get_handle(netdev);
807 
808 	if (!h->ae_algo->ops->get_rss)
809 		return -EOPNOTSUPP;
810 
811 	return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
812 }
813 
814 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
815 			const u8 *key, const u8 hfunc)
816 {
817 	struct hnae3_handle *h = hns3_get_handle(netdev);
818 
819 	if (!h->ae_algo->ops->set_rss)
820 		return -EOPNOTSUPP;
821 
822 	if ((h->pdev->revision == 0x20 &&
823 	     hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
824 	     hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
825 		netdev_err(netdev, "hash func not supported\n");
826 		return -EOPNOTSUPP;
827 	}
828 
829 	if (!indir) {
830 		netdev_err(netdev,
831 			   "set rss failed for indir is empty\n");
832 		return -EOPNOTSUPP;
833 	}
834 
835 	return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
836 }
837 
838 static int hns3_get_rxnfc(struct net_device *netdev,
839 			  struct ethtool_rxnfc *cmd,
840 			  u32 *rule_locs)
841 {
842 	struct hnae3_handle *h = hns3_get_handle(netdev);
843 
844 	switch (cmd->cmd) {
845 	case ETHTOOL_GRXRINGS:
846 		cmd->data = h->kinfo.num_tqps;
847 		return 0;
848 	case ETHTOOL_GRXFH:
849 		if (h->ae_algo->ops->get_rss_tuple)
850 			return h->ae_algo->ops->get_rss_tuple(h, cmd);
851 		return -EOPNOTSUPP;
852 	case ETHTOOL_GRXCLSRLCNT:
853 		if (h->ae_algo->ops->get_fd_rule_cnt)
854 			return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
855 		return -EOPNOTSUPP;
856 	case ETHTOOL_GRXCLSRULE:
857 		if (h->ae_algo->ops->get_fd_rule_info)
858 			return h->ae_algo->ops->get_fd_rule_info(h, cmd);
859 		return -EOPNOTSUPP;
860 	case ETHTOOL_GRXCLSRLALL:
861 		if (h->ae_algo->ops->get_fd_all_rules)
862 			return h->ae_algo->ops->get_fd_all_rules(h, cmd,
863 								 rule_locs);
864 		return -EOPNOTSUPP;
865 	default:
866 		return -EOPNOTSUPP;
867 	}
868 }
869 
870 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
871 					u32 tx_desc_num, u32 rx_desc_num)
872 {
873 	struct hnae3_handle *h = priv->ae_handle;
874 	int i;
875 
876 	h->kinfo.num_tx_desc = tx_desc_num;
877 	h->kinfo.num_rx_desc = rx_desc_num;
878 
879 	for (i = 0; i < h->kinfo.num_tqps; i++) {
880 		priv->ring_data[i].ring->desc_num = tx_desc_num;
881 		priv->ring_data[i + h->kinfo.num_tqps].ring->desc_num =
882 			rx_desc_num;
883 	}
884 }
885 
886 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
887 {
888 	struct hnae3_handle *handle = priv->ae_handle;
889 	struct hns3_enet_ring *tmp_rings;
890 	int i;
891 
892 	tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
893 			    sizeof(struct hns3_enet_ring), GFP_KERNEL);
894 	if (!tmp_rings)
895 		return NULL;
896 
897 	for (i = 0; i < handle->kinfo.num_tqps * 2; i++)
898 		memcpy(&tmp_rings[i], priv->ring_data[i].ring,
899 		       sizeof(struct hns3_enet_ring));
900 
901 	return tmp_rings;
902 }
903 
904 static int hns3_check_ringparam(struct net_device *ndev,
905 				struct ethtool_ringparam *param)
906 {
907 	if (hns3_nic_resetting(ndev))
908 		return -EBUSY;
909 
910 	if (param->rx_mini_pending || param->rx_jumbo_pending)
911 		return -EINVAL;
912 
913 	if (param->tx_pending > HNS3_RING_MAX_PENDING ||
914 	    param->tx_pending < HNS3_RING_MIN_PENDING ||
915 	    param->rx_pending > HNS3_RING_MAX_PENDING ||
916 	    param->rx_pending < HNS3_RING_MIN_PENDING) {
917 		netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
918 			   HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
919 		return -EINVAL;
920 	}
921 
922 	return 0;
923 }
924 
925 static int hns3_set_ringparam(struct net_device *ndev,
926 			      struct ethtool_ringparam *param)
927 {
928 	struct hns3_nic_priv *priv = netdev_priv(ndev);
929 	struct hnae3_handle *h = priv->ae_handle;
930 	struct hns3_enet_ring *tmp_rings;
931 	bool if_running = netif_running(ndev);
932 	u32 old_tx_desc_num, new_tx_desc_num;
933 	u32 old_rx_desc_num, new_rx_desc_num;
934 	u16 queue_num = h->kinfo.num_tqps;
935 	int ret, i;
936 
937 	ret = hns3_check_ringparam(ndev, param);
938 	if (ret)
939 		return ret;
940 
941 	/* Hardware requires that its descriptors must be multiple of eight */
942 	new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
943 	new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
944 	old_tx_desc_num = priv->ring_data[0].ring->desc_num;
945 	old_rx_desc_num = priv->ring_data[queue_num].ring->desc_num;
946 	if (old_tx_desc_num == new_tx_desc_num &&
947 	    old_rx_desc_num == new_rx_desc_num)
948 		return 0;
949 
950 	tmp_rings = hns3_backup_ringparam(priv);
951 	if (!tmp_rings) {
952 		netdev_err(ndev,
953 			   "backup ring param failed by allocating memory fail\n");
954 		return -ENOMEM;
955 	}
956 
957 	netdev_info(ndev,
958 		    "Changing Tx/Rx ring depth from %d/%d to %d/%d\n",
959 		    old_tx_desc_num, old_rx_desc_num,
960 		    new_tx_desc_num, new_rx_desc_num);
961 
962 	if (if_running)
963 		ndev->netdev_ops->ndo_stop(ndev);
964 
965 	hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
966 	ret = hns3_init_all_ring(priv);
967 	if (ret) {
968 		netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
969 			   ret);
970 
971 		hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
972 					    old_rx_desc_num);
973 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
974 			memcpy(priv->ring_data[i].ring, &tmp_rings[i],
975 			       sizeof(struct hns3_enet_ring));
976 	} else {
977 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
978 			hns3_fini_ring(&tmp_rings[i]);
979 	}
980 
981 	kfree(tmp_rings);
982 
983 	if (if_running)
984 		ret = ndev->netdev_ops->ndo_open(ndev);
985 
986 	return ret;
987 }
988 
989 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
990 {
991 	struct hnae3_handle *h = hns3_get_handle(netdev);
992 
993 	switch (cmd->cmd) {
994 	case ETHTOOL_SRXFH:
995 		if (h->ae_algo->ops->set_rss_tuple)
996 			return h->ae_algo->ops->set_rss_tuple(h, cmd);
997 		return -EOPNOTSUPP;
998 	case ETHTOOL_SRXCLSRLINS:
999 		if (h->ae_algo->ops->add_fd_entry)
1000 			return h->ae_algo->ops->add_fd_entry(h, cmd);
1001 		return -EOPNOTSUPP;
1002 	case ETHTOOL_SRXCLSRLDEL:
1003 		if (h->ae_algo->ops->del_fd_entry)
1004 			return h->ae_algo->ops->del_fd_entry(h, cmd);
1005 		return -EOPNOTSUPP;
1006 	default:
1007 		return -EOPNOTSUPP;
1008 	}
1009 }
1010 
1011 static int hns3_nway_reset(struct net_device *netdev)
1012 {
1013 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1014 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1015 	struct phy_device *phy = netdev->phydev;
1016 	int autoneg;
1017 
1018 	if (!netif_running(netdev))
1019 		return 0;
1020 
1021 	if (hns3_nic_resetting(netdev)) {
1022 		netdev_err(netdev, "dev resetting!");
1023 		return -EBUSY;
1024 	}
1025 
1026 	if (!ops->get_autoneg || !ops->restart_autoneg)
1027 		return -EOPNOTSUPP;
1028 
1029 	autoneg = ops->get_autoneg(handle);
1030 	if (autoneg != AUTONEG_ENABLE) {
1031 		netdev_err(netdev,
1032 			   "Autoneg is off, don't support to restart it\n");
1033 		return -EINVAL;
1034 	}
1035 
1036 	netif_dbg(handle, drv, netdev,
1037 		  "nway reset (using %s)\n", phy ? "phy" : "mac");
1038 
1039 	if (phy)
1040 		return genphy_restart_aneg(phy);
1041 
1042 	if (handle->pdev->revision == 0x20)
1043 		return -EOPNOTSUPP;
1044 
1045 	return ops->restart_autoneg(handle);
1046 }
1047 
1048 static void hns3_get_channels(struct net_device *netdev,
1049 			      struct ethtool_channels *ch)
1050 {
1051 	struct hnae3_handle *h = hns3_get_handle(netdev);
1052 
1053 	if (h->ae_algo->ops->get_channels)
1054 		h->ae_algo->ops->get_channels(h, ch);
1055 }
1056 
1057 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
1058 				       struct ethtool_coalesce *cmd)
1059 {
1060 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1061 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1062 	struct hnae3_handle *h = priv->ae_handle;
1063 	u16 queue_num = h->kinfo.num_tqps;
1064 
1065 	if (hns3_nic_resetting(netdev))
1066 		return -EBUSY;
1067 
1068 	if (queue >= queue_num) {
1069 		netdev_err(netdev,
1070 			   "Invalid queue value %d! Queue max id=%d\n",
1071 			   queue, queue_num - 1);
1072 		return -EINVAL;
1073 	}
1074 
1075 	tx_vector = priv->ring_data[queue].ring->tqp_vector;
1076 	rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
1077 
1078 	cmd->use_adaptive_tx_coalesce =
1079 			tx_vector->tx_group.coal.gl_adapt_enable;
1080 	cmd->use_adaptive_rx_coalesce =
1081 			rx_vector->rx_group.coal.gl_adapt_enable;
1082 
1083 	cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
1084 	cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
1085 
1086 	cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1087 	cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1088 
1089 	return 0;
1090 }
1091 
1092 static int hns3_get_coalesce(struct net_device *netdev,
1093 			     struct ethtool_coalesce *cmd)
1094 {
1095 	return hns3_get_coalesce_per_queue(netdev, 0, cmd);
1096 }
1097 
1098 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1099 				       struct ethtool_coalesce *cmd)
1100 {
1101 	u32 rx_gl, tx_gl;
1102 
1103 	if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
1104 		netdev_err(netdev,
1105 			   "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
1106 			   HNS3_INT_GL_MAX);
1107 		return -EINVAL;
1108 	}
1109 
1110 	if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
1111 		netdev_err(netdev,
1112 			   "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
1113 			   HNS3_INT_GL_MAX);
1114 		return -EINVAL;
1115 	}
1116 
1117 	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1118 	if (rx_gl != cmd->rx_coalesce_usecs) {
1119 		netdev_info(netdev,
1120 			    "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
1121 			    cmd->rx_coalesce_usecs, rx_gl);
1122 	}
1123 
1124 	tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1125 	if (tx_gl != cmd->tx_coalesce_usecs) {
1126 		netdev_info(netdev,
1127 			    "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
1128 			    cmd->tx_coalesce_usecs, tx_gl);
1129 	}
1130 
1131 	return 0;
1132 }
1133 
1134 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1135 				       struct ethtool_coalesce *cmd)
1136 {
1137 	u32 rl;
1138 
1139 	if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1140 		netdev_err(netdev,
1141 			   "tx_usecs_high must be same as rx_usecs_high.\n");
1142 		return -EINVAL;
1143 	}
1144 
1145 	if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1146 		netdev_err(netdev,
1147 			   "Invalid usecs_high value, usecs_high range is 0-%d\n",
1148 			   HNS3_INT_RL_MAX);
1149 		return -EINVAL;
1150 	}
1151 
1152 	rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1153 	if (rl != cmd->rx_coalesce_usecs_high) {
1154 		netdev_info(netdev,
1155 			    "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
1156 			    cmd->rx_coalesce_usecs_high, rl);
1157 	}
1158 
1159 	return 0;
1160 }
1161 
1162 static int hns3_check_coalesce_para(struct net_device *netdev,
1163 				    struct ethtool_coalesce *cmd)
1164 {
1165 	int ret;
1166 
1167 	ret = hns3_check_gl_coalesce_para(netdev, cmd);
1168 	if (ret) {
1169 		netdev_err(netdev,
1170 			   "Check gl coalesce param fail. ret = %d\n", ret);
1171 		return ret;
1172 	}
1173 
1174 	ret = hns3_check_rl_coalesce_para(netdev, cmd);
1175 	if (ret) {
1176 		netdev_err(netdev,
1177 			   "Check rl coalesce param fail. ret = %d\n", ret);
1178 		return ret;
1179 	}
1180 
1181 	if (cmd->use_adaptive_tx_coalesce == 1 ||
1182 	    cmd->use_adaptive_rx_coalesce == 1) {
1183 		netdev_info(netdev,
1184 			    "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
1185 			    cmd->use_adaptive_tx_coalesce,
1186 			    cmd->use_adaptive_rx_coalesce);
1187 	}
1188 
1189 	return 0;
1190 }
1191 
1192 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1193 					struct ethtool_coalesce *cmd,
1194 					u32 queue)
1195 {
1196 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1197 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1198 	struct hnae3_handle *h = priv->ae_handle;
1199 	int queue_num = h->kinfo.num_tqps;
1200 
1201 	tx_vector = priv->ring_data[queue].ring->tqp_vector;
1202 	rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
1203 
1204 	tx_vector->tx_group.coal.gl_adapt_enable =
1205 				cmd->use_adaptive_tx_coalesce;
1206 	rx_vector->rx_group.coal.gl_adapt_enable =
1207 				cmd->use_adaptive_rx_coalesce;
1208 
1209 	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1210 	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1211 
1212 	hns3_set_vector_coalesce_tx_gl(tx_vector,
1213 				       tx_vector->tx_group.coal.int_gl);
1214 	hns3_set_vector_coalesce_rx_gl(rx_vector,
1215 				       rx_vector->rx_group.coal.int_gl);
1216 
1217 	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1218 	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1219 }
1220 
1221 static int hns3_set_coalesce(struct net_device *netdev,
1222 			     struct ethtool_coalesce *cmd)
1223 {
1224 	struct hnae3_handle *h = hns3_get_handle(netdev);
1225 	u16 queue_num = h->kinfo.num_tqps;
1226 	int ret;
1227 	int i;
1228 
1229 	if (hns3_nic_resetting(netdev))
1230 		return -EBUSY;
1231 
1232 	ret = hns3_check_coalesce_para(netdev, cmd);
1233 	if (ret)
1234 		return ret;
1235 
1236 	h->kinfo.int_rl_setting =
1237 		hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1238 
1239 	for (i = 0; i < queue_num; i++)
1240 		hns3_set_coalesce_per_queue(netdev, cmd, i);
1241 
1242 	return 0;
1243 }
1244 
1245 static int hns3_get_regs_len(struct net_device *netdev)
1246 {
1247 	struct hnae3_handle *h = hns3_get_handle(netdev);
1248 
1249 	if (!h->ae_algo->ops->get_regs_len)
1250 		return -EOPNOTSUPP;
1251 
1252 	return h->ae_algo->ops->get_regs_len(h);
1253 }
1254 
1255 static void hns3_get_regs(struct net_device *netdev,
1256 			  struct ethtool_regs *cmd, void *data)
1257 {
1258 	struct hnae3_handle *h = hns3_get_handle(netdev);
1259 
1260 	if (!h->ae_algo->ops->get_regs)
1261 		return;
1262 
1263 	h->ae_algo->ops->get_regs(h, &cmd->version, data);
1264 }
1265 
1266 static int hns3_set_phys_id(struct net_device *netdev,
1267 			    enum ethtool_phys_id_state state)
1268 {
1269 	struct hnae3_handle *h = hns3_get_handle(netdev);
1270 
1271 	if (!h->ae_algo->ops->set_led_id)
1272 		return -EOPNOTSUPP;
1273 
1274 	return h->ae_algo->ops->set_led_id(h, state);
1275 }
1276 
1277 static u32 hns3_get_msglevel(struct net_device *netdev)
1278 {
1279 	struct hnae3_handle *h = hns3_get_handle(netdev);
1280 
1281 	return h->msg_enable;
1282 }
1283 
1284 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1285 {
1286 	struct hnae3_handle *h = hns3_get_handle(netdev);
1287 
1288 	h->msg_enable = msg_level;
1289 }
1290 
1291 /* Translate local fec value into ethtool value. */
1292 static unsigned int loc_to_eth_fec(u8 loc_fec)
1293 {
1294 	u32 eth_fec = 0;
1295 
1296 	if (loc_fec & BIT(HNAE3_FEC_AUTO))
1297 		eth_fec |= ETHTOOL_FEC_AUTO;
1298 	if (loc_fec & BIT(HNAE3_FEC_RS))
1299 		eth_fec |= ETHTOOL_FEC_RS;
1300 	if (loc_fec & BIT(HNAE3_FEC_BASER))
1301 		eth_fec |= ETHTOOL_FEC_BASER;
1302 
1303 	/* if nothing is set, then FEC is off */
1304 	if (!eth_fec)
1305 		eth_fec = ETHTOOL_FEC_OFF;
1306 
1307 	return eth_fec;
1308 }
1309 
1310 /* Translate ethtool fec value into local value. */
1311 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1312 {
1313 	u32 loc_fec = 0;
1314 
1315 	if (eth_fec & ETHTOOL_FEC_OFF)
1316 		return loc_fec;
1317 
1318 	if (eth_fec & ETHTOOL_FEC_AUTO)
1319 		loc_fec |= BIT(HNAE3_FEC_AUTO);
1320 	if (eth_fec & ETHTOOL_FEC_RS)
1321 		loc_fec |= BIT(HNAE3_FEC_RS);
1322 	if (eth_fec & ETHTOOL_FEC_BASER)
1323 		loc_fec |= BIT(HNAE3_FEC_BASER);
1324 
1325 	return loc_fec;
1326 }
1327 
1328 static int hns3_get_fecparam(struct net_device *netdev,
1329 			     struct ethtool_fecparam *fec)
1330 {
1331 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1332 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1333 	u8 fec_ability;
1334 	u8 fec_mode;
1335 
1336 	if (handle->pdev->revision == 0x20)
1337 		return -EOPNOTSUPP;
1338 
1339 	if (!ops->get_fec)
1340 		return -EOPNOTSUPP;
1341 
1342 	ops->get_fec(handle, &fec_ability, &fec_mode);
1343 
1344 	fec->fec = loc_to_eth_fec(fec_ability);
1345 	fec->active_fec = loc_to_eth_fec(fec_mode);
1346 
1347 	return 0;
1348 }
1349 
1350 static int hns3_set_fecparam(struct net_device *netdev,
1351 			     struct ethtool_fecparam *fec)
1352 {
1353 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1354 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1355 	u32 fec_mode;
1356 
1357 	if (handle->pdev->revision == 0x20)
1358 		return -EOPNOTSUPP;
1359 
1360 	if (!ops->set_fec)
1361 		return -EOPNOTSUPP;
1362 	fec_mode = eth_to_loc_fec(fec->fec);
1363 
1364 	netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1365 
1366 	return ops->set_fec(handle, fec_mode);
1367 }
1368 
1369 static const struct ethtool_ops hns3vf_ethtool_ops = {
1370 	.get_drvinfo = hns3_get_drvinfo,
1371 	.get_ringparam = hns3_get_ringparam,
1372 	.set_ringparam = hns3_set_ringparam,
1373 	.get_strings = hns3_get_strings,
1374 	.get_ethtool_stats = hns3_get_stats,
1375 	.get_sset_count = hns3_get_sset_count,
1376 	.get_rxnfc = hns3_get_rxnfc,
1377 	.set_rxnfc = hns3_set_rxnfc,
1378 	.get_rxfh_key_size = hns3_get_rss_key_size,
1379 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1380 	.get_rxfh = hns3_get_rss,
1381 	.set_rxfh = hns3_set_rss,
1382 	.get_link_ksettings = hns3_get_link_ksettings,
1383 	.get_channels = hns3_get_channels,
1384 	.get_coalesce = hns3_get_coalesce,
1385 	.set_coalesce = hns3_set_coalesce,
1386 	.get_regs_len = hns3_get_regs_len,
1387 	.get_regs = hns3_get_regs,
1388 	.get_link = hns3_get_link,
1389 	.get_msglevel = hns3_get_msglevel,
1390 	.set_msglevel = hns3_set_msglevel,
1391 };
1392 
1393 static const struct ethtool_ops hns3_ethtool_ops = {
1394 	.self_test = hns3_self_test,
1395 	.get_drvinfo = hns3_get_drvinfo,
1396 	.get_link = hns3_get_link,
1397 	.get_ringparam = hns3_get_ringparam,
1398 	.set_ringparam = hns3_set_ringparam,
1399 	.get_pauseparam = hns3_get_pauseparam,
1400 	.set_pauseparam = hns3_set_pauseparam,
1401 	.get_strings = hns3_get_strings,
1402 	.get_ethtool_stats = hns3_get_stats,
1403 	.get_sset_count = hns3_get_sset_count,
1404 	.get_rxnfc = hns3_get_rxnfc,
1405 	.set_rxnfc = hns3_set_rxnfc,
1406 	.get_rxfh_key_size = hns3_get_rss_key_size,
1407 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1408 	.get_rxfh = hns3_get_rss,
1409 	.set_rxfh = hns3_set_rss,
1410 	.get_link_ksettings = hns3_get_link_ksettings,
1411 	.set_link_ksettings = hns3_set_link_ksettings,
1412 	.nway_reset = hns3_nway_reset,
1413 	.get_channels = hns3_get_channels,
1414 	.set_channels = hns3_set_channels,
1415 	.get_coalesce = hns3_get_coalesce,
1416 	.set_coalesce = hns3_set_coalesce,
1417 	.get_regs_len = hns3_get_regs_len,
1418 	.get_regs = hns3_get_regs,
1419 	.set_phys_id = hns3_set_phys_id,
1420 	.get_msglevel = hns3_get_msglevel,
1421 	.set_msglevel = hns3_set_msglevel,
1422 	.get_fecparam = hns3_get_fecparam,
1423 	.set_fecparam = hns3_set_fecparam,
1424 };
1425 
1426 void hns3_ethtool_set_ops(struct net_device *netdev)
1427 {
1428 	struct hnae3_handle *h = hns3_get_handle(netdev);
1429 
1430 	if (h->flags & HNAE3_SUPPORT_VF)
1431 		netdev->ethtool_ops = &hns3vf_ethtool_ops;
1432 	else
1433 		netdev->ethtool_ops = &hns3_ethtool_ops;
1434 }
1435