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