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.gl_adapt_enable;
1109 	cmd->use_adaptive_rx_coalesce =
1110 			rx_vector->rx_group.coal.gl_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 	return 0;
1119 }
1120 
1121 static int hns3_get_coalesce(struct net_device *netdev,
1122 			     struct ethtool_coalesce *cmd)
1123 {
1124 	return hns3_get_coalesce_per_queue(netdev, 0, cmd);
1125 }
1126 
1127 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1128 				       struct ethtool_coalesce *cmd)
1129 {
1130 	u32 rx_gl, tx_gl;
1131 
1132 	if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
1133 		netdev_err(netdev,
1134 			   "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
1135 			   HNS3_INT_GL_MAX);
1136 		return -EINVAL;
1137 	}
1138 
1139 	if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
1140 		netdev_err(netdev,
1141 			   "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
1142 			   HNS3_INT_GL_MAX);
1143 		return -EINVAL;
1144 	}
1145 
1146 	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1147 	if (rx_gl != cmd->rx_coalesce_usecs) {
1148 		netdev_info(netdev,
1149 			    "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1150 			    cmd->rx_coalesce_usecs, rx_gl);
1151 	}
1152 
1153 	tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1154 	if (tx_gl != cmd->tx_coalesce_usecs) {
1155 		netdev_info(netdev,
1156 			    "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1157 			    cmd->tx_coalesce_usecs, tx_gl);
1158 	}
1159 
1160 	return 0;
1161 }
1162 
1163 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1164 				       struct ethtool_coalesce *cmd)
1165 {
1166 	u32 rl;
1167 
1168 	if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1169 		netdev_err(netdev,
1170 			   "tx_usecs_high must be same as rx_usecs_high.\n");
1171 		return -EINVAL;
1172 	}
1173 
1174 	if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1175 		netdev_err(netdev,
1176 			   "Invalid usecs_high value, usecs_high range is 0-%d\n",
1177 			   HNS3_INT_RL_MAX);
1178 		return -EINVAL;
1179 	}
1180 
1181 	rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1182 	if (rl != cmd->rx_coalesce_usecs_high) {
1183 		netdev_info(netdev,
1184 			    "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1185 			    cmd->rx_coalesce_usecs_high, rl);
1186 	}
1187 
1188 	return 0;
1189 }
1190 
1191 static int hns3_check_coalesce_para(struct net_device *netdev,
1192 				    struct ethtool_coalesce *cmd)
1193 {
1194 	int ret;
1195 
1196 	ret = hns3_check_gl_coalesce_para(netdev, cmd);
1197 	if (ret) {
1198 		netdev_err(netdev,
1199 			   "Check gl coalesce param fail. ret = %d\n", ret);
1200 		return ret;
1201 	}
1202 
1203 	ret = hns3_check_rl_coalesce_para(netdev, cmd);
1204 	if (ret) {
1205 		netdev_err(netdev,
1206 			   "Check rl coalesce param fail. ret = %d\n", ret);
1207 		return ret;
1208 	}
1209 
1210 	if (cmd->use_adaptive_tx_coalesce == 1 ||
1211 	    cmd->use_adaptive_rx_coalesce == 1) {
1212 		netdev_info(netdev,
1213 			    "adaptive-tx=%u and adaptive-rx=%u, tx_usecs or rx_usecs will changed dynamically.\n",
1214 			    cmd->use_adaptive_tx_coalesce,
1215 			    cmd->use_adaptive_rx_coalesce);
1216 	}
1217 
1218 	return 0;
1219 }
1220 
1221 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1222 					struct ethtool_coalesce *cmd,
1223 					u32 queue)
1224 {
1225 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1226 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1227 	struct hnae3_handle *h = priv->ae_handle;
1228 	int queue_num = h->kinfo.num_tqps;
1229 
1230 	tx_vector = priv->ring[queue].tqp_vector;
1231 	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1232 
1233 	tx_vector->tx_group.coal.gl_adapt_enable =
1234 				cmd->use_adaptive_tx_coalesce;
1235 	rx_vector->rx_group.coal.gl_adapt_enable =
1236 				cmd->use_adaptive_rx_coalesce;
1237 
1238 	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1239 	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1240 
1241 	hns3_set_vector_coalesce_tx_gl(tx_vector,
1242 				       tx_vector->tx_group.coal.int_gl);
1243 	hns3_set_vector_coalesce_rx_gl(rx_vector,
1244 				       rx_vector->rx_group.coal.int_gl);
1245 
1246 	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1247 	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1248 }
1249 
1250 static int hns3_set_coalesce(struct net_device *netdev,
1251 			     struct ethtool_coalesce *cmd)
1252 {
1253 	struct hnae3_handle *h = hns3_get_handle(netdev);
1254 	u16 queue_num = h->kinfo.num_tqps;
1255 	int ret;
1256 	int i;
1257 
1258 	if (hns3_nic_resetting(netdev))
1259 		return -EBUSY;
1260 
1261 	ret = hns3_check_coalesce_para(netdev, cmd);
1262 	if (ret)
1263 		return ret;
1264 
1265 	h->kinfo.int_rl_setting =
1266 		hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1267 
1268 	for (i = 0; i < queue_num; i++)
1269 		hns3_set_coalesce_per_queue(netdev, cmd, i);
1270 
1271 	return 0;
1272 }
1273 
1274 static int hns3_get_regs_len(struct net_device *netdev)
1275 {
1276 	struct hnae3_handle *h = hns3_get_handle(netdev);
1277 
1278 	if (!h->ae_algo->ops->get_regs_len)
1279 		return -EOPNOTSUPP;
1280 
1281 	return h->ae_algo->ops->get_regs_len(h);
1282 }
1283 
1284 static void hns3_get_regs(struct net_device *netdev,
1285 			  struct ethtool_regs *cmd, void *data)
1286 {
1287 	struct hnae3_handle *h = hns3_get_handle(netdev);
1288 
1289 	if (!h->ae_algo->ops->get_regs)
1290 		return;
1291 
1292 	h->ae_algo->ops->get_regs(h, &cmd->version, data);
1293 }
1294 
1295 static int hns3_set_phys_id(struct net_device *netdev,
1296 			    enum ethtool_phys_id_state state)
1297 {
1298 	struct hnae3_handle *h = hns3_get_handle(netdev);
1299 
1300 	if (!h->ae_algo->ops->set_led_id)
1301 		return -EOPNOTSUPP;
1302 
1303 	return h->ae_algo->ops->set_led_id(h, state);
1304 }
1305 
1306 static u32 hns3_get_msglevel(struct net_device *netdev)
1307 {
1308 	struct hnae3_handle *h = hns3_get_handle(netdev);
1309 
1310 	return h->msg_enable;
1311 }
1312 
1313 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1314 {
1315 	struct hnae3_handle *h = hns3_get_handle(netdev);
1316 
1317 	h->msg_enable = msg_level;
1318 }
1319 
1320 /* Translate local fec value into ethtool value. */
1321 static unsigned int loc_to_eth_fec(u8 loc_fec)
1322 {
1323 	u32 eth_fec = 0;
1324 
1325 	if (loc_fec & BIT(HNAE3_FEC_AUTO))
1326 		eth_fec |= ETHTOOL_FEC_AUTO;
1327 	if (loc_fec & BIT(HNAE3_FEC_RS))
1328 		eth_fec |= ETHTOOL_FEC_RS;
1329 	if (loc_fec & BIT(HNAE3_FEC_BASER))
1330 		eth_fec |= ETHTOOL_FEC_BASER;
1331 
1332 	/* if nothing is set, then FEC is off */
1333 	if (!eth_fec)
1334 		eth_fec = ETHTOOL_FEC_OFF;
1335 
1336 	return eth_fec;
1337 }
1338 
1339 /* Translate ethtool fec value into local value. */
1340 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1341 {
1342 	u32 loc_fec = 0;
1343 
1344 	if (eth_fec & ETHTOOL_FEC_OFF)
1345 		return loc_fec;
1346 
1347 	if (eth_fec & ETHTOOL_FEC_AUTO)
1348 		loc_fec |= BIT(HNAE3_FEC_AUTO);
1349 	if (eth_fec & ETHTOOL_FEC_RS)
1350 		loc_fec |= BIT(HNAE3_FEC_RS);
1351 	if (eth_fec & ETHTOOL_FEC_BASER)
1352 		loc_fec |= BIT(HNAE3_FEC_BASER);
1353 
1354 	return loc_fec;
1355 }
1356 
1357 static int hns3_get_fecparam(struct net_device *netdev,
1358 			     struct ethtool_fecparam *fec)
1359 {
1360 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1361 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1362 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1363 	u8 fec_ability;
1364 	u8 fec_mode;
1365 
1366 	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1367 		return -EOPNOTSUPP;
1368 
1369 	if (!ops->get_fec)
1370 		return -EOPNOTSUPP;
1371 
1372 	ops->get_fec(handle, &fec_ability, &fec_mode);
1373 
1374 	fec->fec = loc_to_eth_fec(fec_ability);
1375 	fec->active_fec = loc_to_eth_fec(fec_mode);
1376 
1377 	return 0;
1378 }
1379 
1380 static int hns3_set_fecparam(struct net_device *netdev,
1381 			     struct ethtool_fecparam *fec)
1382 {
1383 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1384 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1385 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1386 	u32 fec_mode;
1387 
1388 	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1389 		return -EOPNOTSUPP;
1390 
1391 	if (!ops->set_fec)
1392 		return -EOPNOTSUPP;
1393 	fec_mode = eth_to_loc_fec(fec->fec);
1394 
1395 	netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1396 
1397 	return ops->set_fec(handle, fec_mode);
1398 }
1399 
1400 static int hns3_get_module_info(struct net_device *netdev,
1401 				struct ethtool_modinfo *modinfo)
1402 {
1403 #define HNS3_SFF_8636_V1_3 0x03
1404 
1405 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1406 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1407 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1408 	struct hns3_sfp_type sfp_type;
1409 	int ret;
1410 
1411 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1412 	    !ops->get_module_eeprom)
1413 		return -EOPNOTSUPP;
1414 
1415 	memset(&sfp_type, 0, sizeof(sfp_type));
1416 	ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1417 				     (u8 *)&sfp_type);
1418 	if (ret)
1419 		return ret;
1420 
1421 	switch (sfp_type.type) {
1422 	case SFF8024_ID_SFP:
1423 		modinfo->type = ETH_MODULE_SFF_8472;
1424 		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1425 		break;
1426 	case SFF8024_ID_QSFP_8438:
1427 		modinfo->type = ETH_MODULE_SFF_8436;
1428 		modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1429 		break;
1430 	case SFF8024_ID_QSFP_8436_8636:
1431 		if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1432 			modinfo->type = ETH_MODULE_SFF_8436;
1433 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1434 		} else {
1435 			modinfo->type = ETH_MODULE_SFF_8636;
1436 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1437 		}
1438 		break;
1439 	case SFF8024_ID_QSFP28_8636:
1440 		modinfo->type = ETH_MODULE_SFF_8636;
1441 		modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1442 		break;
1443 	default:
1444 		netdev_err(netdev, "Optical module unknown: %#x\n",
1445 			   sfp_type.type);
1446 		return -EINVAL;
1447 	}
1448 
1449 	return 0;
1450 }
1451 
1452 static int hns3_get_module_eeprom(struct net_device *netdev,
1453 				  struct ethtool_eeprom *ee, u8 *data)
1454 {
1455 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1456 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1457 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1458 
1459 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1460 	    !ops->get_module_eeprom)
1461 		return -EOPNOTSUPP;
1462 
1463 	if (!ee->len)
1464 		return -EINVAL;
1465 
1466 	memset(data, 0, ee->len);
1467 
1468 	return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1469 }
1470 
1471 #define HNS3_ETHTOOL_COALESCE	(ETHTOOL_COALESCE_USECS |		\
1472 				 ETHTOOL_COALESCE_USE_ADAPTIVE |	\
1473 				 ETHTOOL_COALESCE_RX_USECS_HIGH |	\
1474 				 ETHTOOL_COALESCE_TX_USECS_HIGH)
1475 
1476 static const struct ethtool_ops hns3vf_ethtool_ops = {
1477 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1478 	.get_drvinfo = hns3_get_drvinfo,
1479 	.get_ringparam = hns3_get_ringparam,
1480 	.set_ringparam = hns3_set_ringparam,
1481 	.get_strings = hns3_get_strings,
1482 	.get_ethtool_stats = hns3_get_stats,
1483 	.get_sset_count = hns3_get_sset_count,
1484 	.get_rxnfc = hns3_get_rxnfc,
1485 	.set_rxnfc = hns3_set_rxnfc,
1486 	.get_rxfh_key_size = hns3_get_rss_key_size,
1487 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1488 	.get_rxfh = hns3_get_rss,
1489 	.set_rxfh = hns3_set_rss,
1490 	.get_link_ksettings = hns3_get_link_ksettings,
1491 	.get_channels = hns3_get_channels,
1492 	.set_channels = hns3_set_channels,
1493 	.get_coalesce = hns3_get_coalesce,
1494 	.set_coalesce = hns3_set_coalesce,
1495 	.get_regs_len = hns3_get_regs_len,
1496 	.get_regs = hns3_get_regs,
1497 	.get_link = hns3_get_link,
1498 	.get_msglevel = hns3_get_msglevel,
1499 	.set_msglevel = hns3_set_msglevel,
1500 };
1501 
1502 static const struct ethtool_ops hns3_ethtool_ops = {
1503 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1504 	.self_test = hns3_self_test,
1505 	.get_drvinfo = hns3_get_drvinfo,
1506 	.get_link = hns3_get_link,
1507 	.get_ringparam = hns3_get_ringparam,
1508 	.set_ringparam = hns3_set_ringparam,
1509 	.get_pauseparam = hns3_get_pauseparam,
1510 	.set_pauseparam = hns3_set_pauseparam,
1511 	.get_strings = hns3_get_strings,
1512 	.get_ethtool_stats = hns3_get_stats,
1513 	.get_sset_count = hns3_get_sset_count,
1514 	.get_rxnfc = hns3_get_rxnfc,
1515 	.set_rxnfc = hns3_set_rxnfc,
1516 	.get_rxfh_key_size = hns3_get_rss_key_size,
1517 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1518 	.get_rxfh = hns3_get_rss,
1519 	.set_rxfh = hns3_set_rss,
1520 	.get_link_ksettings = hns3_get_link_ksettings,
1521 	.set_link_ksettings = hns3_set_link_ksettings,
1522 	.nway_reset = hns3_nway_reset,
1523 	.get_channels = hns3_get_channels,
1524 	.set_channels = hns3_set_channels,
1525 	.get_coalesce = hns3_get_coalesce,
1526 	.set_coalesce = hns3_set_coalesce,
1527 	.get_regs_len = hns3_get_regs_len,
1528 	.get_regs = hns3_get_regs,
1529 	.set_phys_id = hns3_set_phys_id,
1530 	.get_msglevel = hns3_get_msglevel,
1531 	.set_msglevel = hns3_set_msglevel,
1532 	.get_fecparam = hns3_get_fecparam,
1533 	.set_fecparam = hns3_set_fecparam,
1534 	.get_module_info = hns3_get_module_info,
1535 	.get_module_eeprom = hns3_get_module_eeprom,
1536 };
1537 
1538 void hns3_ethtool_set_ops(struct net_device *netdev)
1539 {
1540 	struct hnae3_handle *h = hns3_get_handle(netdev);
1541 
1542 	if (h->flags & HNAE3_SUPPORT_VF)
1543 		netdev->ethtool_ops = &hns3vf_ethtool_ops;
1544 	else
1545 		netdev->ethtool_ops = &hns3_ethtool_ops;
1546 }
1547