xref: /openbmc/linux/drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c (revision 04295878beac396dae47ba93141cae0d9386e7ef)
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 /* tqp related stats */
22 #define HNS3_TQP_STAT(_string, _member)	{			\
23 	.stats_string = _string,				\
24 	.stats_offset = offsetof(struct hns3_enet_ring, stats) +\
25 			offsetof(struct ring_stats, _member),   \
26 }
27 
28 static const struct hns3_stats hns3_txq_stats[] = {
29 	/* Tx per-queue statistics */
30 	HNS3_TQP_STAT("dropped", sw_err_cnt),
31 	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
32 	HNS3_TQP_STAT("packets", tx_pkts),
33 	HNS3_TQP_STAT("bytes", tx_bytes),
34 	HNS3_TQP_STAT("more", tx_more),
35 	HNS3_TQP_STAT("wake", restart_queue),
36 	HNS3_TQP_STAT("busy", tx_busy),
37 	HNS3_TQP_STAT("copy", tx_copy),
38 	HNS3_TQP_STAT("vlan_err", tx_vlan_err),
39 	HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
40 	HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
41 	HNS3_TQP_STAT("tso_err", tx_tso_err),
42 };
43 
44 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
45 
46 static const struct hns3_stats hns3_rxq_stats[] = {
47 	/* Rx per-queue statistics */
48 	HNS3_TQP_STAT("dropped", sw_err_cnt),
49 	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
50 	HNS3_TQP_STAT("packets", rx_pkts),
51 	HNS3_TQP_STAT("bytes", rx_bytes),
52 	HNS3_TQP_STAT("errors", rx_err_cnt),
53 	HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
54 	HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
55 	HNS3_TQP_STAT("err_bd_num", err_bd_num),
56 	HNS3_TQP_STAT("l2_err", l2_err),
57 	HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
58 	HNS3_TQP_STAT("multicast", rx_multicast),
59 	HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
60 };
61 
62 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
63 
64 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
65 
66 #define HNS3_SELF_TEST_TYPE_NUM         4
67 #define HNS3_NIC_LB_TEST_PKT_NUM	1
68 #define HNS3_NIC_LB_TEST_RING_ID	0
69 #define HNS3_NIC_LB_TEST_PACKET_SIZE	128
70 #define HNS3_NIC_LB_SETUP_USEC		10000
71 
72 /* Nic loopback test err  */
73 #define HNS3_NIC_LB_TEST_NO_MEM_ERR	1
74 #define HNS3_NIC_LB_TEST_TX_CNT_ERR	2
75 #define HNS3_NIC_LB_TEST_RX_CNT_ERR	3
76 
77 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
78 {
79 	struct hnae3_handle *h = hns3_get_handle(ndev);
80 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
81 	bool vlan_filter_enable;
82 	int ret;
83 
84 	if (!h->ae_algo->ops->set_loopback ||
85 	    !h->ae_algo->ops->set_promisc_mode)
86 		return -EOPNOTSUPP;
87 
88 	switch (loop) {
89 	case HNAE3_LOOP_SERIAL_SERDES:
90 	case HNAE3_LOOP_PARALLEL_SERDES:
91 	case HNAE3_LOOP_APP:
92 	case HNAE3_LOOP_PHY:
93 		ret = h->ae_algo->ops->set_loopback(h, loop, en);
94 		break;
95 	default:
96 		ret = -ENOTSUPP;
97 		break;
98 	}
99 
100 	if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
101 		return ret;
102 
103 	if (en) {
104 		h->ae_algo->ops->set_promisc_mode(h, true, true);
105 	} else {
106 		/* recover promisc mode before loopback test */
107 		hns3_request_update_promisc_mode(h);
108 		vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
109 		hns3_enable_vlan_filter(ndev, vlan_filter_enable);
110 	}
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_nic_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 int hns3_get_sset_count(struct net_device *netdev, int stringset)
398 {
399 	struct hnae3_handle *h = hns3_get_handle(netdev);
400 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
401 
402 	if (!ops->get_sset_count)
403 		return -EOPNOTSUPP;
404 
405 	switch (stringset) {
406 	case ETH_SS_STATS:
407 		return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
408 			ops->get_sset_count(h, stringset));
409 
410 	case ETH_SS_TEST:
411 		return ops->get_sset_count(h, stringset);
412 
413 	default:
414 		return -EOPNOTSUPP;
415 	}
416 }
417 
418 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
419 		u32 stat_count, u32 num_tqps, const char *prefix)
420 {
421 #define MAX_PREFIX_SIZE (6 + 4)
422 	u32 size_left;
423 	u32 i, j;
424 	u32 n1;
425 
426 	for (i = 0; i < num_tqps; i++) {
427 		for (j = 0; j < stat_count; j++) {
428 			data[ETH_GSTRING_LEN - 1] = '\0';
429 
430 			/* first, prepend the prefix string */
431 			n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%d_",
432 				       prefix, i);
433 			size_left = (ETH_GSTRING_LEN - 1) - n1;
434 
435 			/* now, concatenate the stats string to it */
436 			strncat(data, stats[j].stats_string, size_left);
437 			data += ETH_GSTRING_LEN;
438 		}
439 	}
440 
441 	return data;
442 }
443 
444 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
445 {
446 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
447 	const char tx_prefix[] = "txq";
448 	const char rx_prefix[] = "rxq";
449 
450 	/* get strings for Tx */
451 	data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
452 				   kinfo->num_tqps, tx_prefix);
453 
454 	/* get strings for Rx */
455 	data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
456 				   kinfo->num_tqps, rx_prefix);
457 
458 	return data;
459 }
460 
461 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
462 {
463 	struct hnae3_handle *h = hns3_get_handle(netdev);
464 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
465 	char *buff = (char *)data;
466 
467 	if (!ops->get_strings)
468 		return;
469 
470 	switch (stringset) {
471 	case ETH_SS_STATS:
472 		buff = hns3_get_strings_tqps(h, buff);
473 		ops->get_strings(h, stringset, (u8 *)buff);
474 		break;
475 	case ETH_SS_TEST:
476 		ops->get_strings(h, stringset, data);
477 		break;
478 	default:
479 		break;
480 	}
481 }
482 
483 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
484 {
485 	struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
486 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
487 	struct hns3_enet_ring *ring;
488 	u8 *stat;
489 	int i, j;
490 
491 	/* get stats for Tx */
492 	for (i = 0; i < kinfo->num_tqps; i++) {
493 		ring = &nic_priv->ring[i];
494 		for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
495 			stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
496 			*data++ = *(u64 *)stat;
497 		}
498 	}
499 
500 	/* get stats for Rx */
501 	for (i = 0; i < kinfo->num_tqps; i++) {
502 		ring = &nic_priv->ring[i + kinfo->num_tqps];
503 		for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
504 			stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
505 			*data++ = *(u64 *)stat;
506 		}
507 	}
508 
509 	return data;
510 }
511 
512 /* hns3_get_stats - get detail statistics.
513  * @netdev: net device
514  * @stats: statistics info.
515  * @data: statistics data.
516  */
517 static void hns3_get_stats(struct net_device *netdev,
518 			   struct ethtool_stats *stats, u64 *data)
519 {
520 	struct hnae3_handle *h = hns3_get_handle(netdev);
521 	u64 *p = data;
522 
523 	if (hns3_nic_resetting(netdev)) {
524 		netdev_err(netdev, "dev resetting, could not get stats\n");
525 		return;
526 	}
527 
528 	if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
529 		netdev_err(netdev, "could not get any statistics\n");
530 		return;
531 	}
532 
533 	h->ae_algo->ops->update_stats(h, &netdev->stats);
534 
535 	/* get per-queue stats */
536 	p = hns3_get_stats_tqps(h, p);
537 
538 	/* get MAC & other misc hardware stats */
539 	h->ae_algo->ops->get_stats(h, p);
540 }
541 
542 static void hns3_get_drvinfo(struct net_device *netdev,
543 			     struct ethtool_drvinfo *drvinfo)
544 {
545 	struct hns3_nic_priv *priv = netdev_priv(netdev);
546 	struct hnae3_handle *h = priv->ae_handle;
547 	u32 fw_version;
548 
549 	if (!h->ae_algo->ops->get_fw_version) {
550 		netdev_err(netdev, "could not get fw version!\n");
551 		return;
552 	}
553 
554 	strncpy(drvinfo->driver, h->pdev->driver->name,
555 		sizeof(drvinfo->driver));
556 	drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
557 
558 	strncpy(drvinfo->bus_info, pci_name(h->pdev),
559 		sizeof(drvinfo->bus_info));
560 	drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
561 
562 	fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
563 
564 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
565 		 "%lu.%lu.%lu.%lu",
566 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
567 				 HNAE3_FW_VERSION_BYTE3_SHIFT),
568 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
569 				 HNAE3_FW_VERSION_BYTE2_SHIFT),
570 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
571 				 HNAE3_FW_VERSION_BYTE1_SHIFT),
572 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
573 				 HNAE3_FW_VERSION_BYTE0_SHIFT));
574 }
575 
576 static u32 hns3_get_link(struct net_device *netdev)
577 {
578 	struct hnae3_handle *h = hns3_get_handle(netdev);
579 
580 	if (h->ae_algo->ops->get_status)
581 		return h->ae_algo->ops->get_status(h);
582 	else
583 		return 0;
584 }
585 
586 static void hns3_get_ringparam(struct net_device *netdev,
587 			       struct ethtool_ringparam *param)
588 {
589 	struct hns3_nic_priv *priv = netdev_priv(netdev);
590 	struct hnae3_handle *h = priv->ae_handle;
591 	int queue_num = h->kinfo.num_tqps;
592 
593 	if (hns3_nic_resetting(netdev)) {
594 		netdev_err(netdev, "dev resetting!");
595 		return;
596 	}
597 
598 	param->tx_max_pending = HNS3_RING_MAX_PENDING;
599 	param->rx_max_pending = HNS3_RING_MAX_PENDING;
600 
601 	param->tx_pending = priv->ring[0].desc_num;
602 	param->rx_pending = priv->ring[queue_num].desc_num;
603 }
604 
605 static void hns3_get_pauseparam(struct net_device *netdev,
606 				struct ethtool_pauseparam *param)
607 {
608 	struct hnae3_handle *h = hns3_get_handle(netdev);
609 
610 	if (h->ae_algo->ops->get_pauseparam)
611 		h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
612 			&param->rx_pause, &param->tx_pause);
613 }
614 
615 static int hns3_set_pauseparam(struct net_device *netdev,
616 			       struct ethtool_pauseparam *param)
617 {
618 	struct hnae3_handle *h = hns3_get_handle(netdev);
619 
620 	netif_dbg(h, drv, netdev,
621 		  "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
622 		  param->autoneg, param->rx_pause, param->tx_pause);
623 
624 	if (h->ae_algo->ops->set_pauseparam)
625 		return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
626 						       param->rx_pause,
627 						       param->tx_pause);
628 	return -EOPNOTSUPP;
629 }
630 
631 static void hns3_get_ksettings(struct hnae3_handle *h,
632 			       struct ethtool_link_ksettings *cmd)
633 {
634 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
635 
636 	/* 1.auto_neg & speed & duplex from cmd */
637 	if (ops->get_ksettings_an_result)
638 		ops->get_ksettings_an_result(h,
639 					     &cmd->base.autoneg,
640 					     &cmd->base.speed,
641 					     &cmd->base.duplex);
642 
643 	/* 2.get link mode */
644 	if (ops->get_link_mode)
645 		ops->get_link_mode(h,
646 				   cmd->link_modes.supported,
647 				   cmd->link_modes.advertising);
648 
649 	/* 3.mdix_ctrl&mdix get from phy reg */
650 	if (ops->get_mdix_mode)
651 		ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
652 				   &cmd->base.eth_tp_mdix);
653 }
654 
655 static int hns3_get_link_ksettings(struct net_device *netdev,
656 				   struct ethtool_link_ksettings *cmd)
657 {
658 	struct hnae3_handle *h = hns3_get_handle(netdev);
659 	const struct hnae3_ae_ops *ops;
660 	u8 module_type;
661 	u8 media_type;
662 	u8 link_stat;
663 
664 	ops = h->ae_algo->ops;
665 	if (ops->get_media_type)
666 		ops->get_media_type(h, &media_type, &module_type);
667 	else
668 		return -EOPNOTSUPP;
669 
670 	switch (media_type) {
671 	case HNAE3_MEDIA_TYPE_NONE:
672 		cmd->base.port = PORT_NONE;
673 		hns3_get_ksettings(h, cmd);
674 		break;
675 	case HNAE3_MEDIA_TYPE_FIBER:
676 		if (module_type == HNAE3_MODULE_TYPE_CR)
677 			cmd->base.port = PORT_DA;
678 		else
679 			cmd->base.port = PORT_FIBRE;
680 
681 		hns3_get_ksettings(h, cmd);
682 		break;
683 	case HNAE3_MEDIA_TYPE_BACKPLANE:
684 		cmd->base.port = PORT_NONE;
685 		hns3_get_ksettings(h, cmd);
686 		break;
687 	case HNAE3_MEDIA_TYPE_COPPER:
688 		cmd->base.port = PORT_TP;
689 		if (!netdev->phydev)
690 			hns3_get_ksettings(h, cmd);
691 		else
692 			phy_ethtool_ksettings_get(netdev->phydev, cmd);
693 		break;
694 	default:
695 
696 		netdev_warn(netdev, "Unknown media type");
697 		return 0;
698 	}
699 
700 	/* mdio_support */
701 	cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
702 
703 	link_stat = hns3_get_link(netdev);
704 	if (!link_stat) {
705 		cmd->base.speed = SPEED_UNKNOWN;
706 		cmd->base.duplex = DUPLEX_UNKNOWN;
707 	}
708 
709 	return 0;
710 }
711 
712 static int hns3_check_ksettings_param(const struct net_device *netdev,
713 				      const struct ethtool_link_ksettings *cmd)
714 {
715 	struct hnae3_handle *handle = hns3_get_handle(netdev);
716 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
717 	u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
718 	u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
719 	u8 autoneg;
720 	u32 speed;
721 	u8 duplex;
722 	int ret;
723 
724 	/* hw doesn't support use specified speed and duplex to negotiate,
725 	 * unnecessary to check them when autoneg on.
726 	 */
727 	if (cmd->base.autoneg)
728 		return 0;
729 
730 	if (ops->get_ksettings_an_result) {
731 		ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
732 		if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
733 		    cmd->base.duplex == duplex)
734 			return 0;
735 	}
736 
737 	if (ops->get_media_type)
738 		ops->get_media_type(handle, &media_type, &module_type);
739 
740 	if (cmd->base.duplex == DUPLEX_HALF &&
741 	    media_type != HNAE3_MEDIA_TYPE_COPPER) {
742 		netdev_err(netdev,
743 			   "only copper port supports half duplex!");
744 		return -EINVAL;
745 	}
746 
747 	if (ops->check_port_speed) {
748 		ret = ops->check_port_speed(handle, cmd->base.speed);
749 		if (ret) {
750 			netdev_err(netdev, "unsupported speed\n");
751 			return ret;
752 		}
753 	}
754 
755 	return 0;
756 }
757 
758 static int hns3_set_link_ksettings(struct net_device *netdev,
759 				   const struct ethtool_link_ksettings *cmd)
760 {
761 	struct hnae3_handle *handle = hns3_get_handle(netdev);
762 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
763 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
764 	int ret;
765 
766 	/* Chip don't support this mode. */
767 	if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
768 		return -EINVAL;
769 
770 	netif_dbg(handle, drv, netdev,
771 		  "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
772 		  netdev->phydev ? "phy" : "mac",
773 		  cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
774 
775 	/* Only support ksettings_set for netdev with phy attached for now */
776 	if (netdev->phydev) {
777 		if (cmd->base.speed == SPEED_1000 &&
778 		    cmd->base.autoneg == AUTONEG_DISABLE)
779 			return -EINVAL;
780 
781 		return phy_ethtool_ksettings_set(netdev->phydev, cmd);
782 	}
783 
784 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
785 		return -EOPNOTSUPP;
786 
787 	ret = hns3_check_ksettings_param(netdev, cmd);
788 	if (ret)
789 		return ret;
790 
791 	if (ops->set_autoneg) {
792 		ret = ops->set_autoneg(handle, cmd->base.autoneg);
793 		if (ret)
794 			return ret;
795 	}
796 
797 	/* hw doesn't support use specified speed and duplex to negotiate,
798 	 * ignore them when autoneg on.
799 	 */
800 	if (cmd->base.autoneg) {
801 		netdev_info(netdev,
802 			    "autoneg is on, ignore the speed and duplex\n");
803 		return 0;
804 	}
805 
806 	if (ops->cfg_mac_speed_dup_h)
807 		ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
808 					       cmd->base.duplex);
809 
810 	return ret;
811 }
812 
813 static u32 hns3_get_rss_key_size(struct net_device *netdev)
814 {
815 	struct hnae3_handle *h = hns3_get_handle(netdev);
816 
817 	if (!h->ae_algo->ops->get_rss_key_size)
818 		return 0;
819 
820 	return h->ae_algo->ops->get_rss_key_size(h);
821 }
822 
823 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
824 {
825 	struct hnae3_handle *h = hns3_get_handle(netdev);
826 
827 	if (!h->ae_algo->ops->get_rss_indir_size)
828 		return 0;
829 
830 	return h->ae_algo->ops->get_rss_indir_size(h);
831 }
832 
833 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
834 			u8 *hfunc)
835 {
836 	struct hnae3_handle *h = hns3_get_handle(netdev);
837 
838 	if (!h->ae_algo->ops->get_rss)
839 		return -EOPNOTSUPP;
840 
841 	return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
842 }
843 
844 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
845 			const u8 *key, const u8 hfunc)
846 {
847 	struct hnae3_handle *h = hns3_get_handle(netdev);
848 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
849 
850 	if (!h->ae_algo->ops->set_rss)
851 		return -EOPNOTSUPP;
852 
853 	if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
854 	     hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
855 	     hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
856 		netdev_err(netdev, "hash func not supported\n");
857 		return -EOPNOTSUPP;
858 	}
859 
860 	if (!indir) {
861 		netdev_err(netdev,
862 			   "set rss failed for indir is empty\n");
863 		return -EOPNOTSUPP;
864 	}
865 
866 	return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
867 }
868 
869 static int hns3_get_rxnfc(struct net_device *netdev,
870 			  struct ethtool_rxnfc *cmd,
871 			  u32 *rule_locs)
872 {
873 	struct hnae3_handle *h = hns3_get_handle(netdev);
874 
875 	switch (cmd->cmd) {
876 	case ETHTOOL_GRXRINGS:
877 		cmd->data = h->kinfo.num_tqps;
878 		return 0;
879 	case ETHTOOL_GRXFH:
880 		if (h->ae_algo->ops->get_rss_tuple)
881 			return h->ae_algo->ops->get_rss_tuple(h, cmd);
882 		return -EOPNOTSUPP;
883 	case ETHTOOL_GRXCLSRLCNT:
884 		if (h->ae_algo->ops->get_fd_rule_cnt)
885 			return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
886 		return -EOPNOTSUPP;
887 	case ETHTOOL_GRXCLSRULE:
888 		if (h->ae_algo->ops->get_fd_rule_info)
889 			return h->ae_algo->ops->get_fd_rule_info(h, cmd);
890 		return -EOPNOTSUPP;
891 	case ETHTOOL_GRXCLSRLALL:
892 		if (h->ae_algo->ops->get_fd_all_rules)
893 			return h->ae_algo->ops->get_fd_all_rules(h, cmd,
894 								 rule_locs);
895 		return -EOPNOTSUPP;
896 	default:
897 		return -EOPNOTSUPP;
898 	}
899 }
900 
901 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
902 					u32 tx_desc_num, u32 rx_desc_num)
903 {
904 	struct hnae3_handle *h = priv->ae_handle;
905 	int i;
906 
907 	h->kinfo.num_tx_desc = tx_desc_num;
908 	h->kinfo.num_rx_desc = rx_desc_num;
909 
910 	for (i = 0; i < h->kinfo.num_tqps; i++) {
911 		priv->ring[i].desc_num = tx_desc_num;
912 		priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
913 	}
914 }
915 
916 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
917 {
918 	struct hnae3_handle *handle = priv->ae_handle;
919 	struct hns3_enet_ring *tmp_rings;
920 	int i;
921 
922 	tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
923 			    sizeof(struct hns3_enet_ring), GFP_KERNEL);
924 	if (!tmp_rings)
925 		return NULL;
926 
927 	for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
928 		memcpy(&tmp_rings[i], &priv->ring[i],
929 		       sizeof(struct hns3_enet_ring));
930 		tmp_rings[i].skb = NULL;
931 	}
932 
933 	return tmp_rings;
934 }
935 
936 static int hns3_check_ringparam(struct net_device *ndev,
937 				struct ethtool_ringparam *param)
938 {
939 	if (hns3_nic_resetting(ndev))
940 		return -EBUSY;
941 
942 	if (param->rx_mini_pending || param->rx_jumbo_pending)
943 		return -EINVAL;
944 
945 	if (param->tx_pending > HNS3_RING_MAX_PENDING ||
946 	    param->tx_pending < HNS3_RING_MIN_PENDING ||
947 	    param->rx_pending > HNS3_RING_MAX_PENDING ||
948 	    param->rx_pending < HNS3_RING_MIN_PENDING) {
949 		netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
950 			   HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
951 		return -EINVAL;
952 	}
953 
954 	return 0;
955 }
956 
957 static int hns3_set_ringparam(struct net_device *ndev,
958 			      struct ethtool_ringparam *param)
959 {
960 	struct hns3_nic_priv *priv = netdev_priv(ndev);
961 	struct hnae3_handle *h = priv->ae_handle;
962 	struct hns3_enet_ring *tmp_rings;
963 	bool if_running = netif_running(ndev);
964 	u32 old_tx_desc_num, new_tx_desc_num;
965 	u32 old_rx_desc_num, new_rx_desc_num;
966 	u16 queue_num = h->kinfo.num_tqps;
967 	int ret, i;
968 
969 	ret = hns3_check_ringparam(ndev, param);
970 	if (ret)
971 		return ret;
972 
973 	/* Hardware requires that its descriptors must be multiple of eight */
974 	new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
975 	new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
976 	old_tx_desc_num = priv->ring[0].desc_num;
977 	old_rx_desc_num = priv->ring[queue_num].desc_num;
978 	if (old_tx_desc_num == new_tx_desc_num &&
979 	    old_rx_desc_num == new_rx_desc_num)
980 		return 0;
981 
982 	tmp_rings = hns3_backup_ringparam(priv);
983 	if (!tmp_rings) {
984 		netdev_err(ndev,
985 			   "backup ring param failed by allocating memory fail\n");
986 		return -ENOMEM;
987 	}
988 
989 	netdev_info(ndev,
990 		    "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
991 		    old_tx_desc_num, old_rx_desc_num,
992 		    new_tx_desc_num, new_rx_desc_num);
993 
994 	if (if_running)
995 		ndev->netdev_ops->ndo_stop(ndev);
996 
997 	hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
998 	ret = hns3_init_all_ring(priv);
999 	if (ret) {
1000 		netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1001 			   ret);
1002 
1003 		hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1004 					    old_rx_desc_num);
1005 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1006 			memcpy(&priv->ring[i], &tmp_rings[i],
1007 			       sizeof(struct hns3_enet_ring));
1008 	} else {
1009 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1010 			hns3_fini_ring(&tmp_rings[i]);
1011 	}
1012 
1013 	kfree(tmp_rings);
1014 
1015 	if (if_running)
1016 		ret = ndev->netdev_ops->ndo_open(ndev);
1017 
1018 	return ret;
1019 }
1020 
1021 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1022 {
1023 	struct hnae3_handle *h = hns3_get_handle(netdev);
1024 
1025 	switch (cmd->cmd) {
1026 	case ETHTOOL_SRXFH:
1027 		if (h->ae_algo->ops->set_rss_tuple)
1028 			return h->ae_algo->ops->set_rss_tuple(h, cmd);
1029 		return -EOPNOTSUPP;
1030 	case ETHTOOL_SRXCLSRLINS:
1031 		if (h->ae_algo->ops->add_fd_entry)
1032 			return h->ae_algo->ops->add_fd_entry(h, cmd);
1033 		return -EOPNOTSUPP;
1034 	case ETHTOOL_SRXCLSRLDEL:
1035 		if (h->ae_algo->ops->del_fd_entry)
1036 			return h->ae_algo->ops->del_fd_entry(h, cmd);
1037 		return -EOPNOTSUPP;
1038 	default:
1039 		return -EOPNOTSUPP;
1040 	}
1041 }
1042 
1043 static int hns3_nway_reset(struct net_device *netdev)
1044 {
1045 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1046 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1047 	struct phy_device *phy = netdev->phydev;
1048 	int autoneg;
1049 
1050 	if (!netif_running(netdev))
1051 		return 0;
1052 
1053 	if (hns3_nic_resetting(netdev)) {
1054 		netdev_err(netdev, "dev resetting!");
1055 		return -EBUSY;
1056 	}
1057 
1058 	if (!ops->get_autoneg || !ops->restart_autoneg)
1059 		return -EOPNOTSUPP;
1060 
1061 	autoneg = ops->get_autoneg(handle);
1062 	if (autoneg != AUTONEG_ENABLE) {
1063 		netdev_err(netdev,
1064 			   "Autoneg is off, don't support to restart it\n");
1065 		return -EINVAL;
1066 	}
1067 
1068 	netif_dbg(handle, drv, netdev,
1069 		  "nway reset (using %s)\n", phy ? "phy" : "mac");
1070 
1071 	if (phy)
1072 		return genphy_restart_aneg(phy);
1073 
1074 	return ops->restart_autoneg(handle);
1075 }
1076 
1077 static void hns3_get_channels(struct net_device *netdev,
1078 			      struct ethtool_channels *ch)
1079 {
1080 	struct hnae3_handle *h = hns3_get_handle(netdev);
1081 
1082 	if (h->ae_algo->ops->get_channels)
1083 		h->ae_algo->ops->get_channels(h, ch);
1084 }
1085 
1086 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
1087 				       struct ethtool_coalesce *cmd)
1088 {
1089 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1090 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1091 	struct hnae3_handle *h = priv->ae_handle;
1092 	u16 queue_num = h->kinfo.num_tqps;
1093 
1094 	if (hns3_nic_resetting(netdev))
1095 		return -EBUSY;
1096 
1097 	if (queue >= queue_num) {
1098 		netdev_err(netdev,
1099 			   "Invalid queue value %u! Queue max id=%u\n",
1100 			   queue, queue_num - 1);
1101 		return -EINVAL;
1102 	}
1103 
1104 	tx_vector = priv->ring[queue].tqp_vector;
1105 	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1106 
1107 	cmd->use_adaptive_tx_coalesce =
1108 			tx_vector->tx_group.coal.adapt_enable;
1109 	cmd->use_adaptive_rx_coalesce =
1110 			rx_vector->rx_group.coal.adapt_enable;
1111 
1112 	cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
1113 	cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
1114 
1115 	cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1116 	cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1117 
1118 	cmd->tx_max_coalesced_frames = tx_vector->tx_group.coal.int_ql;
1119 	cmd->rx_max_coalesced_frames = rx_vector->rx_group.coal.int_ql;
1120 
1121 	return 0;
1122 }
1123 
1124 static int hns3_get_coalesce(struct net_device *netdev,
1125 			     struct ethtool_coalesce *cmd)
1126 {
1127 	return hns3_get_coalesce_per_queue(netdev, 0, cmd);
1128 }
1129 
1130 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1131 				       struct ethtool_coalesce *cmd)
1132 {
1133 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1134 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1135 	u32 rx_gl, tx_gl;
1136 
1137 	if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1138 		netdev_err(netdev,
1139 			   "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1140 			   ae_dev->dev_specs.max_int_gl);
1141 		return -EINVAL;
1142 	}
1143 
1144 	if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1145 		netdev_err(netdev,
1146 			   "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1147 			   ae_dev->dev_specs.max_int_gl);
1148 		return -EINVAL;
1149 	}
1150 
1151 	/* device version above V3(include V3), GL uses 1us unit,
1152 	 * so the round down is not needed.
1153 	 */
1154 	if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1155 		return 0;
1156 
1157 	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1158 	if (rx_gl != cmd->rx_coalesce_usecs) {
1159 		netdev_info(netdev,
1160 			    "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1161 			    cmd->rx_coalesce_usecs, rx_gl);
1162 	}
1163 
1164 	tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1165 	if (tx_gl != cmd->tx_coalesce_usecs) {
1166 		netdev_info(netdev,
1167 			    "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1168 			    cmd->tx_coalesce_usecs, tx_gl);
1169 	}
1170 
1171 	return 0;
1172 }
1173 
1174 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1175 				       struct ethtool_coalesce *cmd)
1176 {
1177 	u32 rl;
1178 
1179 	if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1180 		netdev_err(netdev,
1181 			   "tx_usecs_high must be same as rx_usecs_high.\n");
1182 		return -EINVAL;
1183 	}
1184 
1185 	if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1186 		netdev_err(netdev,
1187 			   "Invalid usecs_high value, usecs_high range is 0-%d\n",
1188 			   HNS3_INT_RL_MAX);
1189 		return -EINVAL;
1190 	}
1191 
1192 	rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1193 	if (rl != cmd->rx_coalesce_usecs_high) {
1194 		netdev_info(netdev,
1195 			    "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1196 			    cmd->rx_coalesce_usecs_high, rl);
1197 	}
1198 
1199 	return 0;
1200 }
1201 
1202 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1203 					struct ethtool_coalesce *cmd)
1204 {
1205 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1206 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1207 
1208 	if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1209 	    !ae_dev->dev_specs.int_ql_max) {
1210 		netdev_err(netdev, "coalesced frames is not supported\n");
1211 		return -EOPNOTSUPP;
1212 	}
1213 
1214 	if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1215 	    cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1216 		netdev_err(netdev,
1217 			   "invalid coalesced_frames value, range is 0-%u\n",
1218 			   ae_dev->dev_specs.int_ql_max);
1219 		return -ERANGE;
1220 	}
1221 
1222 	return 0;
1223 }
1224 
1225 static int hns3_check_coalesce_para(struct net_device *netdev,
1226 				    struct ethtool_coalesce *cmd)
1227 {
1228 	int ret;
1229 
1230 	ret = hns3_check_gl_coalesce_para(netdev, cmd);
1231 	if (ret) {
1232 		netdev_err(netdev,
1233 			   "Check gl coalesce param fail. ret = %d\n", ret);
1234 		return ret;
1235 	}
1236 
1237 	ret = hns3_check_rl_coalesce_para(netdev, cmd);
1238 	if (ret) {
1239 		netdev_err(netdev,
1240 			   "Check rl coalesce param fail. ret = %d\n", ret);
1241 		return ret;
1242 	}
1243 
1244 	ret = hns3_check_ql_coalesce_param(netdev, cmd);
1245 	if (ret)
1246 		return ret;
1247 
1248 	if (cmd->use_adaptive_tx_coalesce == 1 ||
1249 	    cmd->use_adaptive_rx_coalesce == 1) {
1250 		netdev_info(netdev,
1251 			    "adaptive-tx=%u and adaptive-rx=%u, tx_usecs or rx_usecs will changed dynamically.\n",
1252 			    cmd->use_adaptive_tx_coalesce,
1253 			    cmd->use_adaptive_rx_coalesce);
1254 	}
1255 
1256 	return 0;
1257 }
1258 
1259 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1260 					struct ethtool_coalesce *cmd,
1261 					u32 queue)
1262 {
1263 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1264 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1265 	struct hnae3_handle *h = priv->ae_handle;
1266 	int queue_num = h->kinfo.num_tqps;
1267 
1268 	tx_vector = priv->ring[queue].tqp_vector;
1269 	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1270 
1271 	tx_vector->tx_group.coal.adapt_enable =
1272 				cmd->use_adaptive_tx_coalesce;
1273 	rx_vector->rx_group.coal.adapt_enable =
1274 				cmd->use_adaptive_rx_coalesce;
1275 
1276 	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1277 	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1278 
1279 	tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1280 	rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1281 
1282 	hns3_set_vector_coalesce_tx_gl(tx_vector,
1283 				       tx_vector->tx_group.coal.int_gl);
1284 	hns3_set_vector_coalesce_rx_gl(rx_vector,
1285 				       rx_vector->rx_group.coal.int_gl);
1286 
1287 	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1288 	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1289 
1290 	if (tx_vector->tx_group.coal.ql_enable)
1291 		hns3_set_vector_coalesce_tx_ql(tx_vector,
1292 					       tx_vector->tx_group.coal.int_ql);
1293 	if (rx_vector->rx_group.coal.ql_enable)
1294 		hns3_set_vector_coalesce_rx_ql(rx_vector,
1295 					       rx_vector->rx_group.coal.int_ql);
1296 }
1297 
1298 static int hns3_set_coalesce(struct net_device *netdev,
1299 			     struct ethtool_coalesce *cmd)
1300 {
1301 	struct hnae3_handle *h = hns3_get_handle(netdev);
1302 	u16 queue_num = h->kinfo.num_tqps;
1303 	int ret;
1304 	int i;
1305 
1306 	if (hns3_nic_resetting(netdev))
1307 		return -EBUSY;
1308 
1309 	ret = hns3_check_coalesce_para(netdev, cmd);
1310 	if (ret)
1311 		return ret;
1312 
1313 	h->kinfo.int_rl_setting =
1314 		hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1315 
1316 	for (i = 0; i < queue_num; i++)
1317 		hns3_set_coalesce_per_queue(netdev, cmd, i);
1318 
1319 	return 0;
1320 }
1321 
1322 static int hns3_get_regs_len(struct net_device *netdev)
1323 {
1324 	struct hnae3_handle *h = hns3_get_handle(netdev);
1325 
1326 	if (!h->ae_algo->ops->get_regs_len)
1327 		return -EOPNOTSUPP;
1328 
1329 	return h->ae_algo->ops->get_regs_len(h);
1330 }
1331 
1332 static void hns3_get_regs(struct net_device *netdev,
1333 			  struct ethtool_regs *cmd, void *data)
1334 {
1335 	struct hnae3_handle *h = hns3_get_handle(netdev);
1336 
1337 	if (!h->ae_algo->ops->get_regs)
1338 		return;
1339 
1340 	h->ae_algo->ops->get_regs(h, &cmd->version, data);
1341 }
1342 
1343 static int hns3_set_phys_id(struct net_device *netdev,
1344 			    enum ethtool_phys_id_state state)
1345 {
1346 	struct hnae3_handle *h = hns3_get_handle(netdev);
1347 
1348 	if (!h->ae_algo->ops->set_led_id)
1349 		return -EOPNOTSUPP;
1350 
1351 	return h->ae_algo->ops->set_led_id(h, state);
1352 }
1353 
1354 static u32 hns3_get_msglevel(struct net_device *netdev)
1355 {
1356 	struct hnae3_handle *h = hns3_get_handle(netdev);
1357 
1358 	return h->msg_enable;
1359 }
1360 
1361 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1362 {
1363 	struct hnae3_handle *h = hns3_get_handle(netdev);
1364 
1365 	h->msg_enable = msg_level;
1366 }
1367 
1368 /* Translate local fec value into ethtool value. */
1369 static unsigned int loc_to_eth_fec(u8 loc_fec)
1370 {
1371 	u32 eth_fec = 0;
1372 
1373 	if (loc_fec & BIT(HNAE3_FEC_AUTO))
1374 		eth_fec |= ETHTOOL_FEC_AUTO;
1375 	if (loc_fec & BIT(HNAE3_FEC_RS))
1376 		eth_fec |= ETHTOOL_FEC_RS;
1377 	if (loc_fec & BIT(HNAE3_FEC_BASER))
1378 		eth_fec |= ETHTOOL_FEC_BASER;
1379 
1380 	/* if nothing is set, then FEC is off */
1381 	if (!eth_fec)
1382 		eth_fec = ETHTOOL_FEC_OFF;
1383 
1384 	return eth_fec;
1385 }
1386 
1387 /* Translate ethtool fec value into local value. */
1388 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1389 {
1390 	u32 loc_fec = 0;
1391 
1392 	if (eth_fec & ETHTOOL_FEC_OFF)
1393 		return loc_fec;
1394 
1395 	if (eth_fec & ETHTOOL_FEC_AUTO)
1396 		loc_fec |= BIT(HNAE3_FEC_AUTO);
1397 	if (eth_fec & ETHTOOL_FEC_RS)
1398 		loc_fec |= BIT(HNAE3_FEC_RS);
1399 	if (eth_fec & ETHTOOL_FEC_BASER)
1400 		loc_fec |= BIT(HNAE3_FEC_BASER);
1401 
1402 	return loc_fec;
1403 }
1404 
1405 static int hns3_get_fecparam(struct net_device *netdev,
1406 			     struct ethtool_fecparam *fec)
1407 {
1408 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1409 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1410 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1411 	u8 fec_ability;
1412 	u8 fec_mode;
1413 
1414 	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1415 		return -EOPNOTSUPP;
1416 
1417 	if (!ops->get_fec)
1418 		return -EOPNOTSUPP;
1419 
1420 	ops->get_fec(handle, &fec_ability, &fec_mode);
1421 
1422 	fec->fec = loc_to_eth_fec(fec_ability);
1423 	fec->active_fec = loc_to_eth_fec(fec_mode);
1424 
1425 	return 0;
1426 }
1427 
1428 static int hns3_set_fecparam(struct net_device *netdev,
1429 			     struct ethtool_fecparam *fec)
1430 {
1431 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1432 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1433 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1434 	u32 fec_mode;
1435 
1436 	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1437 		return -EOPNOTSUPP;
1438 
1439 	if (!ops->set_fec)
1440 		return -EOPNOTSUPP;
1441 	fec_mode = eth_to_loc_fec(fec->fec);
1442 
1443 	netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1444 
1445 	return ops->set_fec(handle, fec_mode);
1446 }
1447 
1448 static int hns3_get_module_info(struct net_device *netdev,
1449 				struct ethtool_modinfo *modinfo)
1450 {
1451 #define HNS3_SFF_8636_V1_3 0x03
1452 
1453 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1454 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1455 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1456 	struct hns3_sfp_type sfp_type;
1457 	int ret;
1458 
1459 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1460 	    !ops->get_module_eeprom)
1461 		return -EOPNOTSUPP;
1462 
1463 	memset(&sfp_type, 0, sizeof(sfp_type));
1464 	ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1465 				     (u8 *)&sfp_type);
1466 	if (ret)
1467 		return ret;
1468 
1469 	switch (sfp_type.type) {
1470 	case SFF8024_ID_SFP:
1471 		modinfo->type = ETH_MODULE_SFF_8472;
1472 		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1473 		break;
1474 	case SFF8024_ID_QSFP_8438:
1475 		modinfo->type = ETH_MODULE_SFF_8436;
1476 		modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1477 		break;
1478 	case SFF8024_ID_QSFP_8436_8636:
1479 		if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1480 			modinfo->type = ETH_MODULE_SFF_8436;
1481 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1482 		} else {
1483 			modinfo->type = ETH_MODULE_SFF_8636;
1484 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1485 		}
1486 		break;
1487 	case SFF8024_ID_QSFP28_8636:
1488 		modinfo->type = ETH_MODULE_SFF_8636;
1489 		modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1490 		break;
1491 	default:
1492 		netdev_err(netdev, "Optical module unknown: %#x\n",
1493 			   sfp_type.type);
1494 		return -EINVAL;
1495 	}
1496 
1497 	return 0;
1498 }
1499 
1500 static int hns3_get_module_eeprom(struct net_device *netdev,
1501 				  struct ethtool_eeprom *ee, u8 *data)
1502 {
1503 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1504 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1505 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1506 
1507 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1508 	    !ops->get_module_eeprom)
1509 		return -EOPNOTSUPP;
1510 
1511 	if (!ee->len)
1512 		return -EINVAL;
1513 
1514 	memset(data, 0, ee->len);
1515 
1516 	return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1517 }
1518 
1519 #define HNS3_ETHTOOL_COALESCE	(ETHTOOL_COALESCE_USECS |		\
1520 				 ETHTOOL_COALESCE_USE_ADAPTIVE |	\
1521 				 ETHTOOL_COALESCE_RX_USECS_HIGH |	\
1522 				 ETHTOOL_COALESCE_TX_USECS_HIGH |	\
1523 				 ETHTOOL_COALESCE_MAX_FRAMES)
1524 
1525 static const struct ethtool_ops hns3vf_ethtool_ops = {
1526 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1527 	.get_drvinfo = hns3_get_drvinfo,
1528 	.get_ringparam = hns3_get_ringparam,
1529 	.set_ringparam = hns3_set_ringparam,
1530 	.get_strings = hns3_get_strings,
1531 	.get_ethtool_stats = hns3_get_stats,
1532 	.get_sset_count = hns3_get_sset_count,
1533 	.get_rxnfc = hns3_get_rxnfc,
1534 	.set_rxnfc = hns3_set_rxnfc,
1535 	.get_rxfh_key_size = hns3_get_rss_key_size,
1536 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1537 	.get_rxfh = hns3_get_rss,
1538 	.set_rxfh = hns3_set_rss,
1539 	.get_link_ksettings = hns3_get_link_ksettings,
1540 	.get_channels = hns3_get_channels,
1541 	.set_channels = hns3_set_channels,
1542 	.get_coalesce = hns3_get_coalesce,
1543 	.set_coalesce = hns3_set_coalesce,
1544 	.get_regs_len = hns3_get_regs_len,
1545 	.get_regs = hns3_get_regs,
1546 	.get_link = hns3_get_link,
1547 	.get_msglevel = hns3_get_msglevel,
1548 	.set_msglevel = hns3_set_msglevel,
1549 };
1550 
1551 static const struct ethtool_ops hns3_ethtool_ops = {
1552 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1553 	.self_test = hns3_self_test,
1554 	.get_drvinfo = hns3_get_drvinfo,
1555 	.get_link = hns3_get_link,
1556 	.get_ringparam = hns3_get_ringparam,
1557 	.set_ringparam = hns3_set_ringparam,
1558 	.get_pauseparam = hns3_get_pauseparam,
1559 	.set_pauseparam = hns3_set_pauseparam,
1560 	.get_strings = hns3_get_strings,
1561 	.get_ethtool_stats = hns3_get_stats,
1562 	.get_sset_count = hns3_get_sset_count,
1563 	.get_rxnfc = hns3_get_rxnfc,
1564 	.set_rxnfc = hns3_set_rxnfc,
1565 	.get_rxfh_key_size = hns3_get_rss_key_size,
1566 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1567 	.get_rxfh = hns3_get_rss,
1568 	.set_rxfh = hns3_set_rss,
1569 	.get_link_ksettings = hns3_get_link_ksettings,
1570 	.set_link_ksettings = hns3_set_link_ksettings,
1571 	.nway_reset = hns3_nway_reset,
1572 	.get_channels = hns3_get_channels,
1573 	.set_channels = hns3_set_channels,
1574 	.get_coalesce = hns3_get_coalesce,
1575 	.set_coalesce = hns3_set_coalesce,
1576 	.get_regs_len = hns3_get_regs_len,
1577 	.get_regs = hns3_get_regs,
1578 	.set_phys_id = hns3_set_phys_id,
1579 	.get_msglevel = hns3_get_msglevel,
1580 	.set_msglevel = hns3_set_msglevel,
1581 	.get_fecparam = hns3_get_fecparam,
1582 	.set_fecparam = hns3_set_fecparam,
1583 	.get_module_info = hns3_get_module_info,
1584 	.get_module_eeprom = hns3_get_module_eeprom,
1585 };
1586 
1587 void hns3_ethtool_set_ops(struct net_device *netdev)
1588 {
1589 	struct hnae3_handle *h = hns3_get_handle(netdev);
1590 
1591 	if (h->flags & HNAE3_SUPPORT_VF)
1592 		netdev->ethtool_ops = &hns3vf_ethtool_ops;
1593 	else
1594 		netdev->ethtool_ops = &hns3_ethtool_ops;
1595 }
1596