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