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