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