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