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 			       struct kernel_ethtool_ringparam *kernel_param,
648 			       struct netlink_ext_ack *extack)
649 {
650 	struct hns3_nic_priv *priv = netdev_priv(netdev);
651 	struct hnae3_handle *h = priv->ae_handle;
652 	int rx_queue_index = h->kinfo.num_tqps;
653 
654 	if (hns3_nic_resetting(netdev)) {
655 		netdev_err(netdev, "dev resetting!");
656 		return;
657 	}
658 
659 	param->tx_max_pending = HNS3_RING_MAX_PENDING;
660 	param->rx_max_pending = HNS3_RING_MAX_PENDING;
661 
662 	param->tx_pending = priv->ring[0].desc_num;
663 	param->rx_pending = priv->ring[rx_queue_index].desc_num;
664 	kernel_param->rx_buf_len = priv->ring[rx_queue_index].buf_size;
665 }
666 
667 static void hns3_get_pauseparam(struct net_device *netdev,
668 				struct ethtool_pauseparam *param)
669 {
670 	struct hnae3_handle *h = hns3_get_handle(netdev);
671 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
672 
673 	if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
674 		return;
675 
676 	if (h->ae_algo->ops->get_pauseparam)
677 		h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
678 			&param->rx_pause, &param->tx_pause);
679 }
680 
681 static int hns3_set_pauseparam(struct net_device *netdev,
682 			       struct ethtool_pauseparam *param)
683 {
684 	struct hnae3_handle *h = hns3_get_handle(netdev);
685 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
686 
687 	if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
688 		return -EOPNOTSUPP;
689 
690 	netif_dbg(h, drv, netdev,
691 		  "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
692 		  param->autoneg, param->rx_pause, param->tx_pause);
693 
694 	if (h->ae_algo->ops->set_pauseparam)
695 		return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
696 						       param->rx_pause,
697 						       param->tx_pause);
698 	return -EOPNOTSUPP;
699 }
700 
701 static void hns3_get_ksettings(struct hnae3_handle *h,
702 			       struct ethtool_link_ksettings *cmd)
703 {
704 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
705 
706 	/* 1.auto_neg & speed & duplex from cmd */
707 	if (ops->get_ksettings_an_result)
708 		ops->get_ksettings_an_result(h,
709 					     &cmd->base.autoneg,
710 					     &cmd->base.speed,
711 					     &cmd->base.duplex);
712 
713 	/* 2.get link mode */
714 	if (ops->get_link_mode)
715 		ops->get_link_mode(h,
716 				   cmd->link_modes.supported,
717 				   cmd->link_modes.advertising);
718 
719 	/* 3.mdix_ctrl&mdix get from phy reg */
720 	if (ops->get_mdix_mode)
721 		ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
722 				   &cmd->base.eth_tp_mdix);
723 }
724 
725 static int hns3_get_link_ksettings(struct net_device *netdev,
726 				   struct ethtool_link_ksettings *cmd)
727 {
728 	struct hnae3_handle *h = hns3_get_handle(netdev);
729 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
730 	const struct hnae3_ae_ops *ops;
731 	u8 module_type;
732 	u8 media_type;
733 	u8 link_stat;
734 
735 	ops = h->ae_algo->ops;
736 	if (ops->get_media_type)
737 		ops->get_media_type(h, &media_type, &module_type);
738 	else
739 		return -EOPNOTSUPP;
740 
741 	switch (media_type) {
742 	case HNAE3_MEDIA_TYPE_NONE:
743 		cmd->base.port = PORT_NONE;
744 		hns3_get_ksettings(h, cmd);
745 		break;
746 	case HNAE3_MEDIA_TYPE_FIBER:
747 		if (module_type == HNAE3_MODULE_TYPE_CR)
748 			cmd->base.port = PORT_DA;
749 		else
750 			cmd->base.port = PORT_FIBRE;
751 
752 		hns3_get_ksettings(h, cmd);
753 		break;
754 	case HNAE3_MEDIA_TYPE_BACKPLANE:
755 		cmd->base.port = PORT_NONE;
756 		hns3_get_ksettings(h, cmd);
757 		break;
758 	case HNAE3_MEDIA_TYPE_COPPER:
759 		cmd->base.port = PORT_TP;
760 		if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
761 		    ops->get_phy_link_ksettings)
762 			ops->get_phy_link_ksettings(h, cmd);
763 		else if (!netdev->phydev)
764 			hns3_get_ksettings(h, cmd);
765 		else
766 			phy_ethtool_ksettings_get(netdev->phydev, cmd);
767 		break;
768 	default:
769 
770 		netdev_warn(netdev, "Unknown media type");
771 		return 0;
772 	}
773 
774 	/* mdio_support */
775 	cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
776 
777 	link_stat = hns3_get_link(netdev);
778 	if (!link_stat) {
779 		cmd->base.speed = SPEED_UNKNOWN;
780 		cmd->base.duplex = DUPLEX_UNKNOWN;
781 	}
782 
783 	return 0;
784 }
785 
786 static int hns3_check_ksettings_param(const struct net_device *netdev,
787 				      const struct ethtool_link_ksettings *cmd)
788 {
789 	struct hnae3_handle *handle = hns3_get_handle(netdev);
790 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
791 	u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
792 	u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
793 	u8 autoneg;
794 	u32 speed;
795 	u8 duplex;
796 	int ret;
797 
798 	/* hw doesn't support use specified speed and duplex to negotiate,
799 	 * unnecessary to check them when autoneg on.
800 	 */
801 	if (cmd->base.autoneg)
802 		return 0;
803 
804 	if (ops->get_ksettings_an_result) {
805 		ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
806 		if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
807 		    cmd->base.duplex == duplex)
808 			return 0;
809 	}
810 
811 	if (ops->get_media_type)
812 		ops->get_media_type(handle, &media_type, &module_type);
813 
814 	if (cmd->base.duplex == DUPLEX_HALF &&
815 	    media_type != HNAE3_MEDIA_TYPE_COPPER) {
816 		netdev_err(netdev,
817 			   "only copper port supports half duplex!");
818 		return -EINVAL;
819 	}
820 
821 	if (ops->check_port_speed) {
822 		ret = ops->check_port_speed(handle, cmd->base.speed);
823 		if (ret) {
824 			netdev_err(netdev, "unsupported speed\n");
825 			return ret;
826 		}
827 	}
828 
829 	return 0;
830 }
831 
832 static int hns3_set_link_ksettings(struct net_device *netdev,
833 				   const struct ethtool_link_ksettings *cmd)
834 {
835 	struct hnae3_handle *handle = hns3_get_handle(netdev);
836 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
837 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
838 	int ret;
839 
840 	/* Chip don't support this mode. */
841 	if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
842 		return -EINVAL;
843 
844 	netif_dbg(handle, drv, netdev,
845 		  "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
846 		  netdev->phydev ? "phy" : "mac",
847 		  cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
848 
849 	/* Only support ksettings_set for netdev with phy attached for now */
850 	if (netdev->phydev) {
851 		if (cmd->base.speed == SPEED_1000 &&
852 		    cmd->base.autoneg == AUTONEG_DISABLE)
853 			return -EINVAL;
854 
855 		return phy_ethtool_ksettings_set(netdev->phydev, cmd);
856 	} else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
857 		   ops->set_phy_link_ksettings) {
858 		return ops->set_phy_link_ksettings(handle, cmd);
859 	}
860 
861 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
862 		return -EOPNOTSUPP;
863 
864 	ret = hns3_check_ksettings_param(netdev, cmd);
865 	if (ret)
866 		return ret;
867 
868 	if (ops->set_autoneg) {
869 		ret = ops->set_autoneg(handle, cmd->base.autoneg);
870 		if (ret)
871 			return ret;
872 	}
873 
874 	/* hw doesn't support use specified speed and duplex to negotiate,
875 	 * ignore them when autoneg on.
876 	 */
877 	if (cmd->base.autoneg) {
878 		netdev_info(netdev,
879 			    "autoneg is on, ignore the speed and duplex\n");
880 		return 0;
881 	}
882 
883 	if (ops->cfg_mac_speed_dup_h)
884 		ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
885 					       cmd->base.duplex);
886 
887 	return ret;
888 }
889 
890 static u32 hns3_get_rss_key_size(struct net_device *netdev)
891 {
892 	struct hnae3_handle *h = hns3_get_handle(netdev);
893 
894 	if (!h->ae_algo->ops->get_rss_key_size)
895 		return 0;
896 
897 	return h->ae_algo->ops->get_rss_key_size(h);
898 }
899 
900 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
901 {
902 	struct hnae3_handle *h = hns3_get_handle(netdev);
903 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
904 
905 	return ae_dev->dev_specs.rss_ind_tbl_size;
906 }
907 
908 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
909 			u8 *hfunc)
910 {
911 	struct hnae3_handle *h = hns3_get_handle(netdev);
912 
913 	if (!h->ae_algo->ops->get_rss)
914 		return -EOPNOTSUPP;
915 
916 	return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
917 }
918 
919 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
920 			const u8 *key, const u8 hfunc)
921 {
922 	struct hnae3_handle *h = hns3_get_handle(netdev);
923 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
924 
925 	if (!h->ae_algo->ops->set_rss)
926 		return -EOPNOTSUPP;
927 
928 	if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
929 	     hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
930 	     hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
931 		netdev_err(netdev, "hash func not supported\n");
932 		return -EOPNOTSUPP;
933 	}
934 
935 	if (!indir) {
936 		netdev_err(netdev,
937 			   "set rss failed for indir is empty\n");
938 		return -EOPNOTSUPP;
939 	}
940 
941 	return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
942 }
943 
944 static int hns3_get_rxnfc(struct net_device *netdev,
945 			  struct ethtool_rxnfc *cmd,
946 			  u32 *rule_locs)
947 {
948 	struct hnae3_handle *h = hns3_get_handle(netdev);
949 
950 	switch (cmd->cmd) {
951 	case ETHTOOL_GRXRINGS:
952 		cmd->data = h->kinfo.num_tqps;
953 		return 0;
954 	case ETHTOOL_GRXFH:
955 		if (h->ae_algo->ops->get_rss_tuple)
956 			return h->ae_algo->ops->get_rss_tuple(h, cmd);
957 		return -EOPNOTSUPP;
958 	case ETHTOOL_GRXCLSRLCNT:
959 		if (h->ae_algo->ops->get_fd_rule_cnt)
960 			return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
961 		return -EOPNOTSUPP;
962 	case ETHTOOL_GRXCLSRULE:
963 		if (h->ae_algo->ops->get_fd_rule_info)
964 			return h->ae_algo->ops->get_fd_rule_info(h, cmd);
965 		return -EOPNOTSUPP;
966 	case ETHTOOL_GRXCLSRLALL:
967 		if (h->ae_algo->ops->get_fd_all_rules)
968 			return h->ae_algo->ops->get_fd_all_rules(h, cmd,
969 								 rule_locs);
970 		return -EOPNOTSUPP;
971 	default:
972 		return -EOPNOTSUPP;
973 	}
974 }
975 
976 static const struct hns3_reset_type_map hns3_reset_type[] = {
977 	{ETH_RESET_MGMT, HNAE3_IMP_RESET},
978 	{ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
979 	{ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
980 };
981 
982 static const struct hns3_reset_type_map hns3vf_reset_type[] = {
983 	{ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
984 };
985 
986 static int hns3_set_reset(struct net_device *netdev, u32 *flags)
987 {
988 	enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
989 	struct hnae3_handle *h = hns3_get_handle(netdev);
990 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
991 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
992 	const struct hns3_reset_type_map *rst_type_map;
993 	enum ethtool_reset_flags rst_flags;
994 	u32 i, size;
995 
996 	if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
997 		return -EBUSY;
998 
999 	if (!ops->set_default_reset_request || !ops->reset_event)
1000 		return -EOPNOTSUPP;
1001 
1002 	if (h->flags & HNAE3_SUPPORT_VF) {
1003 		rst_type_map = hns3vf_reset_type;
1004 		size = ARRAY_SIZE(hns3vf_reset_type);
1005 	} else {
1006 		rst_type_map = hns3_reset_type;
1007 		size = ARRAY_SIZE(hns3_reset_type);
1008 	}
1009 
1010 	for (i = 0; i < size; i++) {
1011 		if (rst_type_map[i].rst_flags == *flags) {
1012 			rst_type = rst_type_map[i].rst_type;
1013 			rst_flags = rst_type_map[i].rst_flags;
1014 			break;
1015 		}
1016 	}
1017 
1018 	if (rst_type == HNAE3_NONE_RESET ||
1019 	    (rst_type == HNAE3_IMP_RESET &&
1020 	     ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
1021 		return -EOPNOTSUPP;
1022 
1023 	netdev_info(netdev, "Setting reset type %d\n", rst_type);
1024 
1025 	ops->set_default_reset_request(ae_dev, rst_type);
1026 
1027 	ops->reset_event(h->pdev, h);
1028 
1029 	*flags &= ~rst_flags;
1030 
1031 	return 0;
1032 }
1033 
1034 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
1035 					u32 tx_desc_num, u32 rx_desc_num)
1036 {
1037 	struct hnae3_handle *h = priv->ae_handle;
1038 	int i;
1039 
1040 	h->kinfo.num_tx_desc = tx_desc_num;
1041 	h->kinfo.num_rx_desc = rx_desc_num;
1042 
1043 	for (i = 0; i < h->kinfo.num_tqps; i++) {
1044 		priv->ring[i].desc_num = tx_desc_num;
1045 		priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1046 	}
1047 }
1048 
1049 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1050 {
1051 	struct hnae3_handle *handle = priv->ae_handle;
1052 	struct hns3_enet_ring *tmp_rings;
1053 	int i;
1054 
1055 	tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
1056 			    sizeof(struct hns3_enet_ring), GFP_KERNEL);
1057 	if (!tmp_rings)
1058 		return NULL;
1059 
1060 	for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1061 		memcpy(&tmp_rings[i], &priv->ring[i],
1062 		       sizeof(struct hns3_enet_ring));
1063 		tmp_rings[i].skb = NULL;
1064 	}
1065 
1066 	return tmp_rings;
1067 }
1068 
1069 static int hns3_check_ringparam(struct net_device *ndev,
1070 				struct ethtool_ringparam *param,
1071 				struct kernel_ethtool_ringparam *kernel_param)
1072 {
1073 #define RX_BUF_LEN_2K 2048
1074 #define RX_BUF_LEN_4K 4096
1075 	if (hns3_nic_resetting(ndev))
1076 		return -EBUSY;
1077 
1078 	if (param->rx_mini_pending || param->rx_jumbo_pending)
1079 		return -EINVAL;
1080 
1081 	if (kernel_param->rx_buf_len != RX_BUF_LEN_2K &&
1082 	    kernel_param->rx_buf_len != RX_BUF_LEN_4K) {
1083 		netdev_err(ndev, "Rx buf len only support 2048 and 4096\n");
1084 		return -EINVAL;
1085 	}
1086 
1087 	if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1088 	    param->tx_pending < HNS3_RING_MIN_PENDING ||
1089 	    param->rx_pending > HNS3_RING_MAX_PENDING ||
1090 	    param->rx_pending < HNS3_RING_MIN_PENDING) {
1091 		netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1092 			   HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1093 		return -EINVAL;
1094 	}
1095 
1096 	return 0;
1097 }
1098 
1099 static int hns3_change_rx_buf_len(struct net_device *ndev, u32 rx_buf_len)
1100 {
1101 	struct hns3_nic_priv *priv = netdev_priv(ndev);
1102 	struct hnae3_handle *h = priv->ae_handle;
1103 	int i;
1104 
1105 	h->kinfo.rx_buf_len = rx_buf_len;
1106 
1107 	for (i = 0; i < h->kinfo.num_tqps; i++) {
1108 		h->kinfo.tqp[i]->buf_size = rx_buf_len;
1109 		priv->ring[i + h->kinfo.num_tqps].buf_size = rx_buf_len;
1110 	}
1111 
1112 	return 0;
1113 }
1114 
1115 static int hns3_set_ringparam(struct net_device *ndev,
1116 			      struct ethtool_ringparam *param,
1117 			      struct kernel_ethtool_ringparam *kernel_param,
1118 			      struct netlink_ext_ack *extack)
1119 {
1120 	struct hns3_nic_priv *priv = netdev_priv(ndev);
1121 	struct hnae3_handle *h = priv->ae_handle;
1122 	struct hns3_enet_ring *tmp_rings;
1123 	bool if_running = netif_running(ndev);
1124 	u32 old_tx_desc_num, new_tx_desc_num;
1125 	u32 old_rx_desc_num, new_rx_desc_num;
1126 	u16 queue_num = h->kinfo.num_tqps;
1127 	u32 old_rx_buf_len;
1128 	int ret, i;
1129 
1130 	ret = hns3_check_ringparam(ndev, param, kernel_param);
1131 	if (ret)
1132 		return ret;
1133 
1134 	/* Hardware requires that its descriptors must be multiple of eight */
1135 	new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1136 	new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1137 	old_tx_desc_num = priv->ring[0].desc_num;
1138 	old_rx_desc_num = priv->ring[queue_num].desc_num;
1139 	old_rx_buf_len = priv->ring[queue_num].buf_size;
1140 	if (old_tx_desc_num == new_tx_desc_num &&
1141 	    old_rx_desc_num == new_rx_desc_num &&
1142 	    kernel_param->rx_buf_len == old_rx_buf_len)
1143 		return 0;
1144 
1145 	tmp_rings = hns3_backup_ringparam(priv);
1146 	if (!tmp_rings) {
1147 		netdev_err(ndev,
1148 			   "backup ring param failed by allocating memory fail\n");
1149 		return -ENOMEM;
1150 	}
1151 
1152 	netdev_info(ndev,
1153 		    "Changing Tx/Rx ring depth from %u/%u to %u/%u, Changing rx buffer len from %d to %d\n",
1154 		    old_tx_desc_num, old_rx_desc_num,
1155 		    new_tx_desc_num, new_rx_desc_num,
1156 		    old_rx_buf_len, kernel_param->rx_buf_len);
1157 
1158 	if (if_running)
1159 		ndev->netdev_ops->ndo_stop(ndev);
1160 
1161 	hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1162 	hns3_change_rx_buf_len(ndev, kernel_param->rx_buf_len);
1163 	ret = hns3_init_all_ring(priv);
1164 	if (ret) {
1165 		netdev_err(ndev, "set ringparam fail, revert to old value(%d)\n",
1166 			   ret);
1167 
1168 		hns3_change_rx_buf_len(ndev, old_rx_buf_len);
1169 		hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1170 					    old_rx_desc_num);
1171 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1172 			memcpy(&priv->ring[i], &tmp_rings[i],
1173 			       sizeof(struct hns3_enet_ring));
1174 	} else {
1175 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1176 			hns3_fini_ring(&tmp_rings[i]);
1177 	}
1178 
1179 	kfree(tmp_rings);
1180 
1181 	if (if_running)
1182 		ret = ndev->netdev_ops->ndo_open(ndev);
1183 
1184 	return ret;
1185 }
1186 
1187 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1188 {
1189 	struct hnae3_handle *h = hns3_get_handle(netdev);
1190 
1191 	switch (cmd->cmd) {
1192 	case ETHTOOL_SRXFH:
1193 		if (h->ae_algo->ops->set_rss_tuple)
1194 			return h->ae_algo->ops->set_rss_tuple(h, cmd);
1195 		return -EOPNOTSUPP;
1196 	case ETHTOOL_SRXCLSRLINS:
1197 		if (h->ae_algo->ops->add_fd_entry)
1198 			return h->ae_algo->ops->add_fd_entry(h, cmd);
1199 		return -EOPNOTSUPP;
1200 	case ETHTOOL_SRXCLSRLDEL:
1201 		if (h->ae_algo->ops->del_fd_entry)
1202 			return h->ae_algo->ops->del_fd_entry(h, cmd);
1203 		return -EOPNOTSUPP;
1204 	default:
1205 		return -EOPNOTSUPP;
1206 	}
1207 }
1208 
1209 static int hns3_nway_reset(struct net_device *netdev)
1210 {
1211 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1212 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1213 	struct phy_device *phy = netdev->phydev;
1214 	int autoneg;
1215 
1216 	if (!netif_running(netdev))
1217 		return 0;
1218 
1219 	if (hns3_nic_resetting(netdev)) {
1220 		netdev_err(netdev, "dev resetting!");
1221 		return -EBUSY;
1222 	}
1223 
1224 	if (!ops->get_autoneg || !ops->restart_autoneg)
1225 		return -EOPNOTSUPP;
1226 
1227 	autoneg = ops->get_autoneg(handle);
1228 	if (autoneg != AUTONEG_ENABLE) {
1229 		netdev_err(netdev,
1230 			   "Autoneg is off, don't support to restart it\n");
1231 		return -EINVAL;
1232 	}
1233 
1234 	netif_dbg(handle, drv, netdev,
1235 		  "nway reset (using %s)\n", phy ? "phy" : "mac");
1236 
1237 	if (phy)
1238 		return genphy_restart_aneg(phy);
1239 
1240 	return ops->restart_autoneg(handle);
1241 }
1242 
1243 static void hns3_get_channels(struct net_device *netdev,
1244 			      struct ethtool_channels *ch)
1245 {
1246 	struct hnae3_handle *h = hns3_get_handle(netdev);
1247 
1248 	if (h->ae_algo->ops->get_channels)
1249 		h->ae_algo->ops->get_channels(h, ch);
1250 }
1251 
1252 static int hns3_get_coalesce(struct net_device *netdev,
1253 			     struct ethtool_coalesce *cmd,
1254 			     struct kernel_ethtool_coalesce *kernel_coal,
1255 			     struct netlink_ext_ack *extack)
1256 {
1257 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1258 	struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1259 	struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1260 	struct hnae3_handle *h = priv->ae_handle;
1261 
1262 	if (hns3_nic_resetting(netdev))
1263 		return -EBUSY;
1264 
1265 	cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1266 	cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1267 
1268 	cmd->tx_coalesce_usecs = tx_coal->int_gl;
1269 	cmd->rx_coalesce_usecs = rx_coal->int_gl;
1270 
1271 	cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1272 	cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1273 
1274 	cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1275 	cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1276 
1277 	kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode ==
1278 					DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1279 	kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode ==
1280 					DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1281 
1282 	return 0;
1283 }
1284 
1285 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1286 				       struct ethtool_coalesce *cmd)
1287 {
1288 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1289 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1290 	u32 rx_gl, tx_gl;
1291 
1292 	if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1293 		netdev_err(netdev,
1294 			   "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1295 			   ae_dev->dev_specs.max_int_gl);
1296 		return -EINVAL;
1297 	}
1298 
1299 	if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1300 		netdev_err(netdev,
1301 			   "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1302 			   ae_dev->dev_specs.max_int_gl);
1303 		return -EINVAL;
1304 	}
1305 
1306 	/* device version above V3(include V3), GL uses 1us unit,
1307 	 * so the round down is not needed.
1308 	 */
1309 	if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1310 		return 0;
1311 
1312 	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1313 	if (rx_gl != cmd->rx_coalesce_usecs) {
1314 		netdev_info(netdev,
1315 			    "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1316 			    cmd->rx_coalesce_usecs, rx_gl);
1317 	}
1318 
1319 	tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1320 	if (tx_gl != cmd->tx_coalesce_usecs) {
1321 		netdev_info(netdev,
1322 			    "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1323 			    cmd->tx_coalesce_usecs, tx_gl);
1324 	}
1325 
1326 	return 0;
1327 }
1328 
1329 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1330 				       struct ethtool_coalesce *cmd)
1331 {
1332 	u32 rl;
1333 
1334 	if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1335 		netdev_err(netdev,
1336 			   "tx_usecs_high must be same as rx_usecs_high.\n");
1337 		return -EINVAL;
1338 	}
1339 
1340 	if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1341 		netdev_err(netdev,
1342 			   "Invalid usecs_high value, usecs_high range is 0-%d\n",
1343 			   HNS3_INT_RL_MAX);
1344 		return -EINVAL;
1345 	}
1346 
1347 	rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1348 	if (rl != cmd->rx_coalesce_usecs_high) {
1349 		netdev_info(netdev,
1350 			    "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1351 			    cmd->rx_coalesce_usecs_high, rl);
1352 	}
1353 
1354 	return 0;
1355 }
1356 
1357 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1358 					struct ethtool_coalesce *cmd)
1359 {
1360 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1361 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1362 
1363 	if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1364 	    !ae_dev->dev_specs.int_ql_max) {
1365 		netdev_err(netdev, "coalesced frames is not supported\n");
1366 		return -EOPNOTSUPP;
1367 	}
1368 
1369 	if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1370 	    cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1371 		netdev_err(netdev,
1372 			   "invalid coalesced_frames value, range is 0-%u\n",
1373 			   ae_dev->dev_specs.int_ql_max);
1374 		return -ERANGE;
1375 	}
1376 
1377 	return 0;
1378 }
1379 
1380 static int hns3_check_coalesce_para(struct net_device *netdev,
1381 				    struct ethtool_coalesce *cmd)
1382 {
1383 	int ret;
1384 
1385 	ret = hns3_check_gl_coalesce_para(netdev, cmd);
1386 	if (ret) {
1387 		netdev_err(netdev,
1388 			   "Check gl coalesce param fail. ret = %d\n", ret);
1389 		return ret;
1390 	}
1391 
1392 	ret = hns3_check_rl_coalesce_para(netdev, cmd);
1393 	if (ret) {
1394 		netdev_err(netdev,
1395 			   "Check rl coalesce param fail. ret = %d\n", ret);
1396 		return ret;
1397 	}
1398 
1399 	return hns3_check_ql_coalesce_param(netdev, cmd);
1400 }
1401 
1402 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1403 					struct ethtool_coalesce *cmd,
1404 					u32 queue)
1405 {
1406 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1407 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1408 	struct hnae3_handle *h = priv->ae_handle;
1409 	int queue_num = h->kinfo.num_tqps;
1410 
1411 	tx_vector = priv->ring[queue].tqp_vector;
1412 	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1413 
1414 	tx_vector->tx_group.coal.adapt_enable =
1415 				cmd->use_adaptive_tx_coalesce;
1416 	rx_vector->rx_group.coal.adapt_enable =
1417 				cmd->use_adaptive_rx_coalesce;
1418 
1419 	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1420 	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1421 
1422 	tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1423 	rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1424 
1425 	hns3_set_vector_coalesce_tx_gl(tx_vector,
1426 				       tx_vector->tx_group.coal.int_gl);
1427 	hns3_set_vector_coalesce_rx_gl(rx_vector,
1428 				       rx_vector->rx_group.coal.int_gl);
1429 
1430 	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1431 	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1432 
1433 	if (tx_vector->tx_group.coal.ql_enable)
1434 		hns3_set_vector_coalesce_tx_ql(tx_vector,
1435 					       tx_vector->tx_group.coal.int_ql);
1436 	if (rx_vector->rx_group.coal.ql_enable)
1437 		hns3_set_vector_coalesce_rx_ql(rx_vector,
1438 					       rx_vector->rx_group.coal.int_ql);
1439 }
1440 
1441 static int hns3_set_coalesce(struct net_device *netdev,
1442 			     struct ethtool_coalesce *cmd,
1443 			     struct kernel_ethtool_coalesce *kernel_coal,
1444 			     struct netlink_ext_ack *extack)
1445 {
1446 	struct hnae3_handle *h = hns3_get_handle(netdev);
1447 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1448 	struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1449 	struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1450 	u16 queue_num = h->kinfo.num_tqps;
1451 	enum dim_cq_period_mode tx_mode;
1452 	enum dim_cq_period_mode rx_mode;
1453 	int ret;
1454 	int i;
1455 
1456 	if (hns3_nic_resetting(netdev))
1457 		return -EBUSY;
1458 
1459 	ret = hns3_check_coalesce_para(netdev, cmd);
1460 	if (ret)
1461 		return ret;
1462 
1463 	h->kinfo.int_rl_setting =
1464 		hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1465 
1466 	tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1467 	rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1468 
1469 	tx_coal->int_gl = cmd->tx_coalesce_usecs;
1470 	rx_coal->int_gl = cmd->rx_coalesce_usecs;
1471 
1472 	tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1473 	rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1474 
1475 	for (i = 0; i < queue_num; i++)
1476 		hns3_set_coalesce_per_queue(netdev, cmd, i);
1477 
1478 	tx_mode = kernel_coal->use_cqe_mode_tx ?
1479 		  DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1480 		  DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1481 	rx_mode = kernel_coal->use_cqe_mode_rx ?
1482 		  DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1483 		  DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1484 	hns3_cq_period_mode_init(priv, tx_mode, rx_mode);
1485 
1486 	return 0;
1487 }
1488 
1489 static int hns3_get_regs_len(struct net_device *netdev)
1490 {
1491 	struct hnae3_handle *h = hns3_get_handle(netdev);
1492 
1493 	if (!h->ae_algo->ops->get_regs_len)
1494 		return -EOPNOTSUPP;
1495 
1496 	return h->ae_algo->ops->get_regs_len(h);
1497 }
1498 
1499 static void hns3_get_regs(struct net_device *netdev,
1500 			  struct ethtool_regs *cmd, void *data)
1501 {
1502 	struct hnae3_handle *h = hns3_get_handle(netdev);
1503 
1504 	if (!h->ae_algo->ops->get_regs)
1505 		return;
1506 
1507 	h->ae_algo->ops->get_regs(h, &cmd->version, data);
1508 }
1509 
1510 static int hns3_set_phys_id(struct net_device *netdev,
1511 			    enum ethtool_phys_id_state state)
1512 {
1513 	struct hnae3_handle *h = hns3_get_handle(netdev);
1514 
1515 	if (!h->ae_algo->ops->set_led_id)
1516 		return -EOPNOTSUPP;
1517 
1518 	return h->ae_algo->ops->set_led_id(h, state);
1519 }
1520 
1521 static u32 hns3_get_msglevel(struct net_device *netdev)
1522 {
1523 	struct hnae3_handle *h = hns3_get_handle(netdev);
1524 
1525 	return h->msg_enable;
1526 }
1527 
1528 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1529 {
1530 	struct hnae3_handle *h = hns3_get_handle(netdev);
1531 
1532 	h->msg_enable = msg_level;
1533 }
1534 
1535 /* Translate local fec value into ethtool value. */
1536 static unsigned int loc_to_eth_fec(u8 loc_fec)
1537 {
1538 	u32 eth_fec = 0;
1539 
1540 	if (loc_fec & BIT(HNAE3_FEC_AUTO))
1541 		eth_fec |= ETHTOOL_FEC_AUTO;
1542 	if (loc_fec & BIT(HNAE3_FEC_RS))
1543 		eth_fec |= ETHTOOL_FEC_RS;
1544 	if (loc_fec & BIT(HNAE3_FEC_BASER))
1545 		eth_fec |= ETHTOOL_FEC_BASER;
1546 
1547 	/* if nothing is set, then FEC is off */
1548 	if (!eth_fec)
1549 		eth_fec = ETHTOOL_FEC_OFF;
1550 
1551 	return eth_fec;
1552 }
1553 
1554 /* Translate ethtool fec value into local value. */
1555 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1556 {
1557 	u32 loc_fec = 0;
1558 
1559 	if (eth_fec & ETHTOOL_FEC_OFF)
1560 		return loc_fec;
1561 
1562 	if (eth_fec & ETHTOOL_FEC_AUTO)
1563 		loc_fec |= BIT(HNAE3_FEC_AUTO);
1564 	if (eth_fec & ETHTOOL_FEC_RS)
1565 		loc_fec |= BIT(HNAE3_FEC_RS);
1566 	if (eth_fec & ETHTOOL_FEC_BASER)
1567 		loc_fec |= BIT(HNAE3_FEC_BASER);
1568 
1569 	return loc_fec;
1570 }
1571 
1572 static int hns3_get_fecparam(struct net_device *netdev,
1573 			     struct ethtool_fecparam *fec)
1574 {
1575 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1576 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1577 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1578 	u8 fec_ability;
1579 	u8 fec_mode;
1580 
1581 	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1582 		return -EOPNOTSUPP;
1583 
1584 	if (!ops->get_fec)
1585 		return -EOPNOTSUPP;
1586 
1587 	ops->get_fec(handle, &fec_ability, &fec_mode);
1588 
1589 	fec->fec = loc_to_eth_fec(fec_ability);
1590 	fec->active_fec = loc_to_eth_fec(fec_mode);
1591 
1592 	return 0;
1593 }
1594 
1595 static int hns3_set_fecparam(struct net_device *netdev,
1596 			     struct ethtool_fecparam *fec)
1597 {
1598 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1599 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1600 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1601 	u32 fec_mode;
1602 
1603 	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1604 		return -EOPNOTSUPP;
1605 
1606 	if (!ops->set_fec)
1607 		return -EOPNOTSUPP;
1608 	fec_mode = eth_to_loc_fec(fec->fec);
1609 
1610 	netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1611 
1612 	return ops->set_fec(handle, fec_mode);
1613 }
1614 
1615 static int hns3_get_module_info(struct net_device *netdev,
1616 				struct ethtool_modinfo *modinfo)
1617 {
1618 #define HNS3_SFF_8636_V1_3 0x03
1619 
1620 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1621 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1622 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1623 	struct hns3_sfp_type sfp_type;
1624 	int ret;
1625 
1626 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1627 	    !ops->get_module_eeprom)
1628 		return -EOPNOTSUPP;
1629 
1630 	memset(&sfp_type, 0, sizeof(sfp_type));
1631 	ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1632 				     (u8 *)&sfp_type);
1633 	if (ret)
1634 		return ret;
1635 
1636 	switch (sfp_type.type) {
1637 	case SFF8024_ID_SFP:
1638 		modinfo->type = ETH_MODULE_SFF_8472;
1639 		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1640 		break;
1641 	case SFF8024_ID_QSFP_8438:
1642 		modinfo->type = ETH_MODULE_SFF_8436;
1643 		modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1644 		break;
1645 	case SFF8024_ID_QSFP_8436_8636:
1646 		if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1647 			modinfo->type = ETH_MODULE_SFF_8436;
1648 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1649 		} else {
1650 			modinfo->type = ETH_MODULE_SFF_8636;
1651 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1652 		}
1653 		break;
1654 	case SFF8024_ID_QSFP28_8636:
1655 		modinfo->type = ETH_MODULE_SFF_8636;
1656 		modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1657 		break;
1658 	default:
1659 		netdev_err(netdev, "Optical module unknown: %#x\n",
1660 			   sfp_type.type);
1661 		return -EINVAL;
1662 	}
1663 
1664 	return 0;
1665 }
1666 
1667 static int hns3_get_module_eeprom(struct net_device *netdev,
1668 				  struct ethtool_eeprom *ee, u8 *data)
1669 {
1670 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1671 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1672 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1673 
1674 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1675 	    !ops->get_module_eeprom)
1676 		return -EOPNOTSUPP;
1677 
1678 	if (!ee->len)
1679 		return -EINVAL;
1680 
1681 	memset(data, 0, ee->len);
1682 
1683 	return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1684 }
1685 
1686 static u32 hns3_get_priv_flags(struct net_device *netdev)
1687 {
1688 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1689 
1690 	return handle->priv_flags;
1691 }
1692 
1693 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1694 {
1695 	u32 i;
1696 
1697 	for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1698 		if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1699 			netdev_err(h->netdev, "%s is unsupported\n",
1700 				   hns3_priv_flags[i].name);
1701 			return -EOPNOTSUPP;
1702 		}
1703 
1704 	return 0;
1705 }
1706 
1707 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1708 {
1709 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1710 	u32 changed = pflags ^ handle->priv_flags;
1711 	int ret;
1712 	u32 i;
1713 
1714 	ret = hns3_check_priv_flags(handle, changed);
1715 	if (ret)
1716 		return ret;
1717 
1718 	for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1719 		if (changed & BIT(i)) {
1720 			bool enable = !(handle->priv_flags & BIT(i));
1721 
1722 			if (enable)
1723 				handle->priv_flags |= BIT(i);
1724 			else
1725 				handle->priv_flags &= ~BIT(i);
1726 			hns3_priv_flags[i].handler(netdev, enable);
1727 		}
1728 	}
1729 
1730 	return 0;
1731 }
1732 
1733 static int hns3_get_tunable(struct net_device *netdev,
1734 			    const struct ethtool_tunable *tuna,
1735 			    void *data)
1736 {
1737 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1738 	struct hnae3_handle *h = priv->ae_handle;
1739 	int ret = 0;
1740 
1741 	switch (tuna->id) {
1742 	case ETHTOOL_TX_COPYBREAK:
1743 		/* all the tx rings have the same tx_copybreak */
1744 		*(u32 *)data = priv->tx_copybreak;
1745 		break;
1746 	case ETHTOOL_RX_COPYBREAK:
1747 		*(u32 *)data = priv->rx_copybreak;
1748 		break;
1749 	case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1750 		*(u32 *)data = h->kinfo.tx_spare_buf_size;
1751 		break;
1752 	default:
1753 		ret = -EOPNOTSUPP;
1754 		break;
1755 	}
1756 
1757 	return ret;
1758 }
1759 
1760 static int hns3_set_tx_spare_buf_size(struct net_device *netdev,
1761 				      u32 data)
1762 {
1763 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1764 	struct hnae3_handle *h = priv->ae_handle;
1765 	int ret;
1766 
1767 	if (hns3_nic_resetting(netdev))
1768 		return -EBUSY;
1769 
1770 	h->kinfo.tx_spare_buf_size = data;
1771 
1772 	ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT);
1773 	if (ret)
1774 		return ret;
1775 
1776 	ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1777 	if (ret)
1778 		return ret;
1779 
1780 	ret = hns3_reset_notify(h, HNAE3_INIT_CLIENT);
1781 	if (ret)
1782 		return ret;
1783 
1784 	ret = hns3_reset_notify(h, HNAE3_UP_CLIENT);
1785 	if (ret)
1786 		hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
1787 
1788 	return ret;
1789 }
1790 
1791 static int hns3_set_tunable(struct net_device *netdev,
1792 			    const struct ethtool_tunable *tuna,
1793 			    const void *data)
1794 {
1795 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1796 	u32 old_tx_spare_buf_size, new_tx_spare_buf_size;
1797 	struct hnae3_handle *h = priv->ae_handle;
1798 	int i, ret = 0;
1799 
1800 	switch (tuna->id) {
1801 	case ETHTOOL_TX_COPYBREAK:
1802 		priv->tx_copybreak = *(u32 *)data;
1803 
1804 		for (i = 0; i < h->kinfo.num_tqps; i++)
1805 			priv->ring[i].tx_copybreak = priv->tx_copybreak;
1806 
1807 		break;
1808 	case ETHTOOL_RX_COPYBREAK:
1809 		priv->rx_copybreak = *(u32 *)data;
1810 
1811 		for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1812 			priv->ring[i].rx_copybreak = priv->rx_copybreak;
1813 
1814 		break;
1815 	case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
1816 		old_tx_spare_buf_size = h->kinfo.tx_spare_buf_size;
1817 		new_tx_spare_buf_size = *(u32 *)data;
1818 		ret = hns3_set_tx_spare_buf_size(netdev, new_tx_spare_buf_size);
1819 		if (ret) {
1820 			int ret1;
1821 
1822 			netdev_warn(netdev,
1823 				    "change tx spare buf size fail, revert to old value\n");
1824 			ret1 = hns3_set_tx_spare_buf_size(netdev,
1825 							  old_tx_spare_buf_size);
1826 			if (ret1) {
1827 				netdev_err(netdev,
1828 					   "revert to old tx spare buf size fail\n");
1829 				return ret1;
1830 			}
1831 
1832 			return ret;
1833 		}
1834 		break;
1835 	default:
1836 		ret = -EOPNOTSUPP;
1837 		break;
1838 	}
1839 
1840 	return ret;
1841 }
1842 
1843 #define HNS3_ETHTOOL_COALESCE	(ETHTOOL_COALESCE_USECS |		\
1844 				 ETHTOOL_COALESCE_USE_ADAPTIVE |	\
1845 				 ETHTOOL_COALESCE_RX_USECS_HIGH |	\
1846 				 ETHTOOL_COALESCE_TX_USECS_HIGH |	\
1847 				 ETHTOOL_COALESCE_MAX_FRAMES |		\
1848 				 ETHTOOL_COALESCE_USE_CQE)
1849 
1850 #define HNS3_ETHTOOL_RING	ETHTOOL_RING_USE_RX_BUF_LEN
1851 
1852 static int hns3_get_ts_info(struct net_device *netdev,
1853 			    struct ethtool_ts_info *info)
1854 {
1855 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1856 
1857 	if (handle->ae_algo->ops->get_ts_info)
1858 		return handle->ae_algo->ops->get_ts_info(handle, info);
1859 
1860 	return ethtool_op_get_ts_info(netdev, info);
1861 }
1862 
1863 static const struct hns3_ethtool_link_ext_state_mapping
1864 hns3_link_ext_state_map[] = {
1865 	{1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1866 		ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
1867 	{2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1868 		ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},
1869 
1870 	{256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1871 		ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
1872 	{257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1873 		ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
1874 	{512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1875 		ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},
1876 
1877 	{513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1878 		ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
1879 	{514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1880 		ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
1881 	{515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1882 		ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},
1883 
1884 	{768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1885 		ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
1886 	{769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1887 		ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
1888 	{770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1889 		ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},
1890 
1891 	{1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
1892 	{1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
1893 		ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},
1894 
1895 	{1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
1896 };
1897 
1898 static int hns3_get_link_ext_state(struct net_device *netdev,
1899 				   struct ethtool_link_ext_state_info *info)
1900 {
1901 	const struct hns3_ethtool_link_ext_state_mapping *map;
1902 	struct hnae3_handle *h = hns3_get_handle(netdev);
1903 	u32 status_code, i;
1904 	int ret;
1905 
1906 	if (netif_carrier_ok(netdev))
1907 		return -ENODATA;
1908 
1909 	if (!h->ae_algo->ops->get_link_diagnosis_info)
1910 		return -EOPNOTSUPP;
1911 
1912 	ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
1913 	if (ret)
1914 		return ret;
1915 
1916 	for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
1917 		map = &hns3_link_ext_state_map[i];
1918 		if (map->status_code == status_code) {
1919 			info->link_ext_state = map->link_ext_state;
1920 			info->__link_ext_substate = map->link_ext_substate;
1921 			return 0;
1922 		}
1923 	}
1924 
1925 	return -ENODATA;
1926 }
1927 
1928 static const struct ethtool_ops hns3vf_ethtool_ops = {
1929 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1930 	.supported_ring_params = HNS3_ETHTOOL_RING,
1931 	.get_drvinfo = hns3_get_drvinfo,
1932 	.get_ringparam = hns3_get_ringparam,
1933 	.set_ringparam = hns3_set_ringparam,
1934 	.get_strings = hns3_get_strings,
1935 	.get_ethtool_stats = hns3_get_stats,
1936 	.get_sset_count = hns3_get_sset_count,
1937 	.get_rxnfc = hns3_get_rxnfc,
1938 	.set_rxnfc = hns3_set_rxnfc,
1939 	.get_rxfh_key_size = hns3_get_rss_key_size,
1940 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1941 	.get_rxfh = hns3_get_rss,
1942 	.set_rxfh = hns3_set_rss,
1943 	.get_link_ksettings = hns3_get_link_ksettings,
1944 	.get_channels = hns3_get_channels,
1945 	.set_channels = hns3_set_channels,
1946 	.get_coalesce = hns3_get_coalesce,
1947 	.set_coalesce = hns3_set_coalesce,
1948 	.get_regs_len = hns3_get_regs_len,
1949 	.get_regs = hns3_get_regs,
1950 	.get_link = hns3_get_link,
1951 	.get_msglevel = hns3_get_msglevel,
1952 	.set_msglevel = hns3_set_msglevel,
1953 	.get_priv_flags = hns3_get_priv_flags,
1954 	.set_priv_flags = hns3_set_priv_flags,
1955 	.get_tunable = hns3_get_tunable,
1956 	.set_tunable = hns3_set_tunable,
1957 	.reset = hns3_set_reset,
1958 };
1959 
1960 static const struct ethtool_ops hns3_ethtool_ops = {
1961 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1962 	.supported_ring_params = HNS3_ETHTOOL_RING,
1963 	.self_test = hns3_self_test,
1964 	.get_drvinfo = hns3_get_drvinfo,
1965 	.get_link = hns3_get_link,
1966 	.get_ringparam = hns3_get_ringparam,
1967 	.set_ringparam = hns3_set_ringparam,
1968 	.get_pauseparam = hns3_get_pauseparam,
1969 	.set_pauseparam = hns3_set_pauseparam,
1970 	.get_strings = hns3_get_strings,
1971 	.get_ethtool_stats = hns3_get_stats,
1972 	.get_sset_count = hns3_get_sset_count,
1973 	.get_rxnfc = hns3_get_rxnfc,
1974 	.set_rxnfc = hns3_set_rxnfc,
1975 	.get_rxfh_key_size = hns3_get_rss_key_size,
1976 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1977 	.get_rxfh = hns3_get_rss,
1978 	.set_rxfh = hns3_set_rss,
1979 	.get_link_ksettings = hns3_get_link_ksettings,
1980 	.set_link_ksettings = hns3_set_link_ksettings,
1981 	.nway_reset = hns3_nway_reset,
1982 	.get_channels = hns3_get_channels,
1983 	.set_channels = hns3_set_channels,
1984 	.get_coalesce = hns3_get_coalesce,
1985 	.set_coalesce = hns3_set_coalesce,
1986 	.get_regs_len = hns3_get_regs_len,
1987 	.get_regs = hns3_get_regs,
1988 	.set_phys_id = hns3_set_phys_id,
1989 	.get_msglevel = hns3_get_msglevel,
1990 	.set_msglevel = hns3_set_msglevel,
1991 	.get_fecparam = hns3_get_fecparam,
1992 	.set_fecparam = hns3_set_fecparam,
1993 	.get_module_info = hns3_get_module_info,
1994 	.get_module_eeprom = hns3_get_module_eeprom,
1995 	.get_priv_flags = hns3_get_priv_flags,
1996 	.set_priv_flags = hns3_set_priv_flags,
1997 	.get_ts_info = hns3_get_ts_info,
1998 	.get_tunable = hns3_get_tunable,
1999 	.set_tunable = hns3_set_tunable,
2000 	.reset = hns3_set_reset,
2001 	.get_link_ext_state = hns3_get_link_ext_state,
2002 };
2003 
2004 void hns3_ethtool_set_ops(struct net_device *netdev)
2005 {
2006 	struct hnae3_handle *h = hns3_get_handle(netdev);
2007 
2008 	if (h->flags & HNAE3_SUPPORT_VF)
2009 		netdev->ethtool_ops = &hns3vf_ethtool_ops;
2010 	else
2011 		netdev->ethtool_ops = &hns3_ethtool_ops;
2012 }
2013