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