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