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