xref: /linux/drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c (revision 5d085ad2e68cceec8332b23ea8f630a28b506366)
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_request_update_promisc_mode(h);
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->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[0].desc_num;
597 	param->rx_pending = priv->ring[queue_num].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(const 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 	/* hw doesn't support use specified speed and duplex to negotiate,
720 	 * unnecessary to check them when autoneg on.
721 	 */
722 	if (cmd->base.autoneg)
723 		return 0;
724 
725 	if (ops->get_ksettings_an_result) {
726 		ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
727 		if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
728 		    cmd->base.duplex == duplex)
729 			return 0;
730 	}
731 
732 	if (ops->get_media_type)
733 		ops->get_media_type(handle, &media_type, &module_type);
734 
735 	if (cmd->base.duplex == DUPLEX_HALF &&
736 	    media_type != HNAE3_MEDIA_TYPE_COPPER) {
737 		netdev_err(netdev,
738 			   "only copper port supports half duplex!");
739 		return -EINVAL;
740 	}
741 
742 	if (ops->check_port_speed) {
743 		ret = ops->check_port_speed(handle, cmd->base.speed);
744 		if (ret) {
745 			netdev_err(netdev, "unsupported speed\n");
746 			return ret;
747 		}
748 	}
749 
750 	return 0;
751 }
752 
753 static int hns3_set_link_ksettings(struct net_device *netdev,
754 				   const struct ethtool_link_ksettings *cmd)
755 {
756 	struct hnae3_handle *handle = hns3_get_handle(netdev);
757 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
758 	int ret;
759 
760 	/* Chip don't support this mode. */
761 	if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
762 		return -EINVAL;
763 
764 	netif_dbg(handle, drv, netdev,
765 		  "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
766 		  netdev->phydev ? "phy" : "mac",
767 		  cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
768 
769 	/* Only support ksettings_set for netdev with phy attached for now */
770 	if (netdev->phydev)
771 		return phy_ethtool_ksettings_set(netdev->phydev, cmd);
772 
773 	if (handle->pdev->revision == 0x20)
774 		return -EOPNOTSUPP;
775 
776 	ret = hns3_check_ksettings_param(netdev, cmd);
777 	if (ret)
778 		return ret;
779 
780 	if (ops->set_autoneg) {
781 		ret = ops->set_autoneg(handle, cmd->base.autoneg);
782 		if (ret)
783 			return ret;
784 	}
785 
786 	/* hw doesn't support use specified speed and duplex to negotiate,
787 	 * ignore them when autoneg on.
788 	 */
789 	if (cmd->base.autoneg) {
790 		netdev_info(netdev,
791 			    "autoneg is on, ignore the speed and duplex\n");
792 		return 0;
793 	}
794 
795 	if (ops->cfg_mac_speed_dup_h)
796 		ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
797 					       cmd->base.duplex);
798 
799 	return ret;
800 }
801 
802 static u32 hns3_get_rss_key_size(struct net_device *netdev)
803 {
804 	struct hnae3_handle *h = hns3_get_handle(netdev);
805 
806 	if (!h->ae_algo->ops->get_rss_key_size)
807 		return 0;
808 
809 	return h->ae_algo->ops->get_rss_key_size(h);
810 }
811 
812 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
813 {
814 	struct hnae3_handle *h = hns3_get_handle(netdev);
815 
816 	if (!h->ae_algo->ops->get_rss_indir_size)
817 		return 0;
818 
819 	return h->ae_algo->ops->get_rss_indir_size(h);
820 }
821 
822 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
823 			u8 *hfunc)
824 {
825 	struct hnae3_handle *h = hns3_get_handle(netdev);
826 
827 	if (!h->ae_algo->ops->get_rss)
828 		return -EOPNOTSUPP;
829 
830 	return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
831 }
832 
833 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
834 			const u8 *key, const u8 hfunc)
835 {
836 	struct hnae3_handle *h = hns3_get_handle(netdev);
837 
838 	if (!h->ae_algo->ops->set_rss)
839 		return -EOPNOTSUPP;
840 
841 	if ((h->pdev->revision == 0x20 &&
842 	     hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
843 	     hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
844 		netdev_err(netdev, "hash func not supported\n");
845 		return -EOPNOTSUPP;
846 	}
847 
848 	if (!indir) {
849 		netdev_err(netdev,
850 			   "set rss failed for indir is empty\n");
851 		return -EOPNOTSUPP;
852 	}
853 
854 	return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
855 }
856 
857 static int hns3_get_rxnfc(struct net_device *netdev,
858 			  struct ethtool_rxnfc *cmd,
859 			  u32 *rule_locs)
860 {
861 	struct hnae3_handle *h = hns3_get_handle(netdev);
862 
863 	switch (cmd->cmd) {
864 	case ETHTOOL_GRXRINGS:
865 		cmd->data = h->kinfo.num_tqps;
866 		return 0;
867 	case ETHTOOL_GRXFH:
868 		if (h->ae_algo->ops->get_rss_tuple)
869 			return h->ae_algo->ops->get_rss_tuple(h, cmd);
870 		return -EOPNOTSUPP;
871 	case ETHTOOL_GRXCLSRLCNT:
872 		if (h->ae_algo->ops->get_fd_rule_cnt)
873 			return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
874 		return -EOPNOTSUPP;
875 	case ETHTOOL_GRXCLSRULE:
876 		if (h->ae_algo->ops->get_fd_rule_info)
877 			return h->ae_algo->ops->get_fd_rule_info(h, cmd);
878 		return -EOPNOTSUPP;
879 	case ETHTOOL_GRXCLSRLALL:
880 		if (h->ae_algo->ops->get_fd_all_rules)
881 			return h->ae_algo->ops->get_fd_all_rules(h, cmd,
882 								 rule_locs);
883 		return -EOPNOTSUPP;
884 	default:
885 		return -EOPNOTSUPP;
886 	}
887 }
888 
889 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
890 					u32 tx_desc_num, u32 rx_desc_num)
891 {
892 	struct hnae3_handle *h = priv->ae_handle;
893 	int i;
894 
895 	h->kinfo.num_tx_desc = tx_desc_num;
896 	h->kinfo.num_rx_desc = rx_desc_num;
897 
898 	for (i = 0; i < h->kinfo.num_tqps; i++) {
899 		priv->ring[i].desc_num = tx_desc_num;
900 		priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
901 	}
902 }
903 
904 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
905 {
906 	struct hnae3_handle *handle = priv->ae_handle;
907 	struct hns3_enet_ring *tmp_rings;
908 	int i;
909 
910 	tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
911 			    sizeof(struct hns3_enet_ring), GFP_KERNEL);
912 	if (!tmp_rings)
913 		return NULL;
914 
915 	for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
916 		memcpy(&tmp_rings[i], &priv->ring[i],
917 		       sizeof(struct hns3_enet_ring));
918 		tmp_rings[i].skb = NULL;
919 	}
920 
921 	return tmp_rings;
922 }
923 
924 static int hns3_check_ringparam(struct net_device *ndev,
925 				struct ethtool_ringparam *param)
926 {
927 	if (hns3_nic_resetting(ndev))
928 		return -EBUSY;
929 
930 	if (param->rx_mini_pending || param->rx_jumbo_pending)
931 		return -EINVAL;
932 
933 	if (param->tx_pending > HNS3_RING_MAX_PENDING ||
934 	    param->tx_pending < HNS3_RING_MIN_PENDING ||
935 	    param->rx_pending > HNS3_RING_MAX_PENDING ||
936 	    param->rx_pending < HNS3_RING_MIN_PENDING) {
937 		netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
938 			   HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
939 		return -EINVAL;
940 	}
941 
942 	return 0;
943 }
944 
945 static int hns3_set_ringparam(struct net_device *ndev,
946 			      struct ethtool_ringparam *param)
947 {
948 	struct hns3_nic_priv *priv = netdev_priv(ndev);
949 	struct hnae3_handle *h = priv->ae_handle;
950 	struct hns3_enet_ring *tmp_rings;
951 	bool if_running = netif_running(ndev);
952 	u32 old_tx_desc_num, new_tx_desc_num;
953 	u32 old_rx_desc_num, new_rx_desc_num;
954 	u16 queue_num = h->kinfo.num_tqps;
955 	int ret, i;
956 
957 	ret = hns3_check_ringparam(ndev, param);
958 	if (ret)
959 		return ret;
960 
961 	/* Hardware requires that its descriptors must be multiple of eight */
962 	new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
963 	new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
964 	old_tx_desc_num = priv->ring[0].desc_num;
965 	old_rx_desc_num = priv->ring[queue_num].desc_num;
966 	if (old_tx_desc_num == new_tx_desc_num &&
967 	    old_rx_desc_num == new_rx_desc_num)
968 		return 0;
969 
970 	tmp_rings = hns3_backup_ringparam(priv);
971 	if (!tmp_rings) {
972 		netdev_err(ndev,
973 			   "backup ring param failed by allocating memory fail\n");
974 		return -ENOMEM;
975 	}
976 
977 	netdev_info(ndev,
978 		    "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
979 		    old_tx_desc_num, old_rx_desc_num,
980 		    new_tx_desc_num, new_rx_desc_num);
981 
982 	if (if_running)
983 		ndev->netdev_ops->ndo_stop(ndev);
984 
985 	hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
986 	ret = hns3_init_all_ring(priv);
987 	if (ret) {
988 		netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
989 			   ret);
990 
991 		hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
992 					    old_rx_desc_num);
993 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
994 			memcpy(&priv->ring[i], &tmp_rings[i],
995 			       sizeof(struct hns3_enet_ring));
996 	} else {
997 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
998 			hns3_fini_ring(&tmp_rings[i]);
999 	}
1000 
1001 	kfree(tmp_rings);
1002 
1003 	if (if_running)
1004 		ret = ndev->netdev_ops->ndo_open(ndev);
1005 
1006 	return ret;
1007 }
1008 
1009 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1010 {
1011 	struct hnae3_handle *h = hns3_get_handle(netdev);
1012 
1013 	switch (cmd->cmd) {
1014 	case ETHTOOL_SRXFH:
1015 		if (h->ae_algo->ops->set_rss_tuple)
1016 			return h->ae_algo->ops->set_rss_tuple(h, cmd);
1017 		return -EOPNOTSUPP;
1018 	case ETHTOOL_SRXCLSRLINS:
1019 		if (h->ae_algo->ops->add_fd_entry)
1020 			return h->ae_algo->ops->add_fd_entry(h, cmd);
1021 		return -EOPNOTSUPP;
1022 	case ETHTOOL_SRXCLSRLDEL:
1023 		if (h->ae_algo->ops->del_fd_entry)
1024 			return h->ae_algo->ops->del_fd_entry(h, cmd);
1025 		return -EOPNOTSUPP;
1026 	default:
1027 		return -EOPNOTSUPP;
1028 	}
1029 }
1030 
1031 static int hns3_nway_reset(struct net_device *netdev)
1032 {
1033 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1034 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1035 	struct phy_device *phy = netdev->phydev;
1036 	int autoneg;
1037 
1038 	if (!netif_running(netdev))
1039 		return 0;
1040 
1041 	if (hns3_nic_resetting(netdev)) {
1042 		netdev_err(netdev, "dev resetting!");
1043 		return -EBUSY;
1044 	}
1045 
1046 	if (!ops->get_autoneg || !ops->restart_autoneg)
1047 		return -EOPNOTSUPP;
1048 
1049 	autoneg = ops->get_autoneg(handle);
1050 	if (autoneg != AUTONEG_ENABLE) {
1051 		netdev_err(netdev,
1052 			   "Autoneg is off, don't support to restart it\n");
1053 		return -EINVAL;
1054 	}
1055 
1056 	netif_dbg(handle, drv, netdev,
1057 		  "nway reset (using %s)\n", phy ? "phy" : "mac");
1058 
1059 	if (phy)
1060 		return genphy_restart_aneg(phy);
1061 
1062 	if (handle->pdev->revision == 0x20)
1063 		return -EOPNOTSUPP;
1064 
1065 	return ops->restart_autoneg(handle);
1066 }
1067 
1068 static void hns3_get_channels(struct net_device *netdev,
1069 			      struct ethtool_channels *ch)
1070 {
1071 	struct hnae3_handle *h = hns3_get_handle(netdev);
1072 
1073 	if (h->ae_algo->ops->get_channels)
1074 		h->ae_algo->ops->get_channels(h, ch);
1075 }
1076 
1077 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
1078 				       struct ethtool_coalesce *cmd)
1079 {
1080 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1081 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1082 	struct hnae3_handle *h = priv->ae_handle;
1083 	u16 queue_num = h->kinfo.num_tqps;
1084 
1085 	if (hns3_nic_resetting(netdev))
1086 		return -EBUSY;
1087 
1088 	if (queue >= queue_num) {
1089 		netdev_err(netdev,
1090 			   "Invalid queue value %u! Queue max id=%u\n",
1091 			   queue, queue_num - 1);
1092 		return -EINVAL;
1093 	}
1094 
1095 	tx_vector = priv->ring[queue].tqp_vector;
1096 	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1097 
1098 	cmd->use_adaptive_tx_coalesce =
1099 			tx_vector->tx_group.coal.gl_adapt_enable;
1100 	cmd->use_adaptive_rx_coalesce =
1101 			rx_vector->rx_group.coal.gl_adapt_enable;
1102 
1103 	cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
1104 	cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
1105 
1106 	cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1107 	cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1108 
1109 	return 0;
1110 }
1111 
1112 static int hns3_get_coalesce(struct net_device *netdev,
1113 			     struct ethtool_coalesce *cmd)
1114 {
1115 	return hns3_get_coalesce_per_queue(netdev, 0, cmd);
1116 }
1117 
1118 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1119 				       struct ethtool_coalesce *cmd)
1120 {
1121 	u32 rx_gl, tx_gl;
1122 
1123 	if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
1124 		netdev_err(netdev,
1125 			   "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
1126 			   HNS3_INT_GL_MAX);
1127 		return -EINVAL;
1128 	}
1129 
1130 	if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
1131 		netdev_err(netdev,
1132 			   "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
1133 			   HNS3_INT_GL_MAX);
1134 		return -EINVAL;
1135 	}
1136 
1137 	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1138 	if (rx_gl != cmd->rx_coalesce_usecs) {
1139 		netdev_info(netdev,
1140 			    "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1141 			    cmd->rx_coalesce_usecs, rx_gl);
1142 	}
1143 
1144 	tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1145 	if (tx_gl != cmd->tx_coalesce_usecs) {
1146 		netdev_info(netdev,
1147 			    "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1148 			    cmd->tx_coalesce_usecs, tx_gl);
1149 	}
1150 
1151 	return 0;
1152 }
1153 
1154 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1155 				       struct ethtool_coalesce *cmd)
1156 {
1157 	u32 rl;
1158 
1159 	if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1160 		netdev_err(netdev,
1161 			   "tx_usecs_high must be same as rx_usecs_high.\n");
1162 		return -EINVAL;
1163 	}
1164 
1165 	if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1166 		netdev_err(netdev,
1167 			   "Invalid usecs_high value, usecs_high range is 0-%d\n",
1168 			   HNS3_INT_RL_MAX);
1169 		return -EINVAL;
1170 	}
1171 
1172 	rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1173 	if (rl != cmd->rx_coalesce_usecs_high) {
1174 		netdev_info(netdev,
1175 			    "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1176 			    cmd->rx_coalesce_usecs_high, rl);
1177 	}
1178 
1179 	return 0;
1180 }
1181 
1182 static int hns3_check_coalesce_para(struct net_device *netdev,
1183 				    struct ethtool_coalesce *cmd)
1184 {
1185 	int ret;
1186 
1187 	ret = hns3_check_gl_coalesce_para(netdev, cmd);
1188 	if (ret) {
1189 		netdev_err(netdev,
1190 			   "Check gl coalesce param fail. ret = %d\n", ret);
1191 		return ret;
1192 	}
1193 
1194 	ret = hns3_check_rl_coalesce_para(netdev, cmd);
1195 	if (ret) {
1196 		netdev_err(netdev,
1197 			   "Check rl coalesce param fail. ret = %d\n", ret);
1198 		return ret;
1199 	}
1200 
1201 	if (cmd->use_adaptive_tx_coalesce == 1 ||
1202 	    cmd->use_adaptive_rx_coalesce == 1) {
1203 		netdev_info(netdev,
1204 			    "adaptive-tx=%u and adaptive-rx=%u, tx_usecs or rx_usecs will changed dynamically.\n",
1205 			    cmd->use_adaptive_tx_coalesce,
1206 			    cmd->use_adaptive_rx_coalesce);
1207 	}
1208 
1209 	return 0;
1210 }
1211 
1212 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1213 					struct ethtool_coalesce *cmd,
1214 					u32 queue)
1215 {
1216 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1217 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1218 	struct hnae3_handle *h = priv->ae_handle;
1219 	int queue_num = h->kinfo.num_tqps;
1220 
1221 	tx_vector = priv->ring[queue].tqp_vector;
1222 	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1223 
1224 	tx_vector->tx_group.coal.gl_adapt_enable =
1225 				cmd->use_adaptive_tx_coalesce;
1226 	rx_vector->rx_group.coal.gl_adapt_enable =
1227 				cmd->use_adaptive_rx_coalesce;
1228 
1229 	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1230 	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1231 
1232 	hns3_set_vector_coalesce_tx_gl(tx_vector,
1233 				       tx_vector->tx_group.coal.int_gl);
1234 	hns3_set_vector_coalesce_rx_gl(rx_vector,
1235 				       rx_vector->rx_group.coal.int_gl);
1236 
1237 	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1238 	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1239 }
1240 
1241 static int hns3_set_coalesce(struct net_device *netdev,
1242 			     struct ethtool_coalesce *cmd)
1243 {
1244 	struct hnae3_handle *h = hns3_get_handle(netdev);
1245 	u16 queue_num = h->kinfo.num_tqps;
1246 	int ret;
1247 	int i;
1248 
1249 	if (hns3_nic_resetting(netdev))
1250 		return -EBUSY;
1251 
1252 	ret = hns3_check_coalesce_para(netdev, cmd);
1253 	if (ret)
1254 		return ret;
1255 
1256 	h->kinfo.int_rl_setting =
1257 		hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1258 
1259 	for (i = 0; i < queue_num; i++)
1260 		hns3_set_coalesce_per_queue(netdev, cmd, i);
1261 
1262 	return 0;
1263 }
1264 
1265 static int hns3_get_regs_len(struct net_device *netdev)
1266 {
1267 	struct hnae3_handle *h = hns3_get_handle(netdev);
1268 
1269 	if (!h->ae_algo->ops->get_regs_len)
1270 		return -EOPNOTSUPP;
1271 
1272 	return h->ae_algo->ops->get_regs_len(h);
1273 }
1274 
1275 static void hns3_get_regs(struct net_device *netdev,
1276 			  struct ethtool_regs *cmd, void *data)
1277 {
1278 	struct hnae3_handle *h = hns3_get_handle(netdev);
1279 
1280 	if (!h->ae_algo->ops->get_regs)
1281 		return;
1282 
1283 	h->ae_algo->ops->get_regs(h, &cmd->version, data);
1284 }
1285 
1286 static int hns3_set_phys_id(struct net_device *netdev,
1287 			    enum ethtool_phys_id_state state)
1288 {
1289 	struct hnae3_handle *h = hns3_get_handle(netdev);
1290 
1291 	if (!h->ae_algo->ops->set_led_id)
1292 		return -EOPNOTSUPP;
1293 
1294 	return h->ae_algo->ops->set_led_id(h, state);
1295 }
1296 
1297 static u32 hns3_get_msglevel(struct net_device *netdev)
1298 {
1299 	struct hnae3_handle *h = hns3_get_handle(netdev);
1300 
1301 	return h->msg_enable;
1302 }
1303 
1304 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1305 {
1306 	struct hnae3_handle *h = hns3_get_handle(netdev);
1307 
1308 	h->msg_enable = msg_level;
1309 }
1310 
1311 /* Translate local fec value into ethtool value. */
1312 static unsigned int loc_to_eth_fec(u8 loc_fec)
1313 {
1314 	u32 eth_fec = 0;
1315 
1316 	if (loc_fec & BIT(HNAE3_FEC_AUTO))
1317 		eth_fec |= ETHTOOL_FEC_AUTO;
1318 	if (loc_fec & BIT(HNAE3_FEC_RS))
1319 		eth_fec |= ETHTOOL_FEC_RS;
1320 	if (loc_fec & BIT(HNAE3_FEC_BASER))
1321 		eth_fec |= ETHTOOL_FEC_BASER;
1322 
1323 	/* if nothing is set, then FEC is off */
1324 	if (!eth_fec)
1325 		eth_fec = ETHTOOL_FEC_OFF;
1326 
1327 	return eth_fec;
1328 }
1329 
1330 /* Translate ethtool fec value into local value. */
1331 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1332 {
1333 	u32 loc_fec = 0;
1334 
1335 	if (eth_fec & ETHTOOL_FEC_OFF)
1336 		return loc_fec;
1337 
1338 	if (eth_fec & ETHTOOL_FEC_AUTO)
1339 		loc_fec |= BIT(HNAE3_FEC_AUTO);
1340 	if (eth_fec & ETHTOOL_FEC_RS)
1341 		loc_fec |= BIT(HNAE3_FEC_RS);
1342 	if (eth_fec & ETHTOOL_FEC_BASER)
1343 		loc_fec |= BIT(HNAE3_FEC_BASER);
1344 
1345 	return loc_fec;
1346 }
1347 
1348 static int hns3_get_fecparam(struct net_device *netdev,
1349 			     struct ethtool_fecparam *fec)
1350 {
1351 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1352 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1353 	u8 fec_ability;
1354 	u8 fec_mode;
1355 
1356 	if (handle->pdev->revision == 0x20)
1357 		return -EOPNOTSUPP;
1358 
1359 	if (!ops->get_fec)
1360 		return -EOPNOTSUPP;
1361 
1362 	ops->get_fec(handle, &fec_ability, &fec_mode);
1363 
1364 	fec->fec = loc_to_eth_fec(fec_ability);
1365 	fec->active_fec = loc_to_eth_fec(fec_mode);
1366 
1367 	return 0;
1368 }
1369 
1370 static int hns3_set_fecparam(struct net_device *netdev,
1371 			     struct ethtool_fecparam *fec)
1372 {
1373 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1374 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1375 	u32 fec_mode;
1376 
1377 	if (handle->pdev->revision == 0x20)
1378 		return -EOPNOTSUPP;
1379 
1380 	if (!ops->set_fec)
1381 		return -EOPNOTSUPP;
1382 	fec_mode = eth_to_loc_fec(fec->fec);
1383 
1384 	netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1385 
1386 	return ops->set_fec(handle, fec_mode);
1387 }
1388 
1389 #define HNS3_ETHTOOL_COALESCE	(ETHTOOL_COALESCE_USECS |		\
1390 				 ETHTOOL_COALESCE_USE_ADAPTIVE |	\
1391 				 ETHTOOL_COALESCE_RX_USECS_HIGH |	\
1392 				 ETHTOOL_COALESCE_TX_USECS_HIGH)
1393 
1394 static const struct ethtool_ops hns3vf_ethtool_ops = {
1395 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1396 	.get_drvinfo = hns3_get_drvinfo,
1397 	.get_ringparam = hns3_get_ringparam,
1398 	.set_ringparam = hns3_set_ringparam,
1399 	.get_strings = hns3_get_strings,
1400 	.get_ethtool_stats = hns3_get_stats,
1401 	.get_sset_count = hns3_get_sset_count,
1402 	.get_rxnfc = hns3_get_rxnfc,
1403 	.set_rxnfc = hns3_set_rxnfc,
1404 	.get_rxfh_key_size = hns3_get_rss_key_size,
1405 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1406 	.get_rxfh = hns3_get_rss,
1407 	.set_rxfh = hns3_set_rss,
1408 	.get_link_ksettings = hns3_get_link_ksettings,
1409 	.get_channels = hns3_get_channels,
1410 	.set_channels = hns3_set_channels,
1411 	.get_coalesce = hns3_get_coalesce,
1412 	.set_coalesce = hns3_set_coalesce,
1413 	.get_regs_len = hns3_get_regs_len,
1414 	.get_regs = hns3_get_regs,
1415 	.get_link = hns3_get_link,
1416 	.get_msglevel = hns3_get_msglevel,
1417 	.set_msglevel = hns3_set_msglevel,
1418 };
1419 
1420 static const struct ethtool_ops hns3_ethtool_ops = {
1421 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1422 	.self_test = hns3_self_test,
1423 	.get_drvinfo = hns3_get_drvinfo,
1424 	.get_link = hns3_get_link,
1425 	.get_ringparam = hns3_get_ringparam,
1426 	.set_ringparam = hns3_set_ringparam,
1427 	.get_pauseparam = hns3_get_pauseparam,
1428 	.set_pauseparam = hns3_set_pauseparam,
1429 	.get_strings = hns3_get_strings,
1430 	.get_ethtool_stats = hns3_get_stats,
1431 	.get_sset_count = hns3_get_sset_count,
1432 	.get_rxnfc = hns3_get_rxnfc,
1433 	.set_rxnfc = hns3_set_rxnfc,
1434 	.get_rxfh_key_size = hns3_get_rss_key_size,
1435 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1436 	.get_rxfh = hns3_get_rss,
1437 	.set_rxfh = hns3_set_rss,
1438 	.get_link_ksettings = hns3_get_link_ksettings,
1439 	.set_link_ksettings = hns3_set_link_ksettings,
1440 	.nway_reset = hns3_nway_reset,
1441 	.get_channels = hns3_get_channels,
1442 	.set_channels = hns3_set_channels,
1443 	.get_coalesce = hns3_get_coalesce,
1444 	.set_coalesce = hns3_set_coalesce,
1445 	.get_regs_len = hns3_get_regs_len,
1446 	.get_regs = hns3_get_regs,
1447 	.set_phys_id = hns3_set_phys_id,
1448 	.get_msglevel = hns3_get_msglevel,
1449 	.set_msglevel = hns3_set_msglevel,
1450 	.get_fecparam = hns3_get_fecparam,
1451 	.set_fecparam = hns3_set_fecparam,
1452 };
1453 
1454 void hns3_ethtool_set_ops(struct net_device *netdev)
1455 {
1456 	struct hnae3_handle *h = hns3_get_handle(netdev);
1457 
1458 	if (h->flags & HNAE3_SUPPORT_VF)
1459 		netdev->ethtool_ops = &hns3vf_ethtool_ops;
1460 	else
1461 		netdev->ethtool_ops = &hns3_ethtool_ops;
1462 }
1463