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