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