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