1 /*
2  * Copyright (c) 2016~2017 Hisilicon Limited.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 
10 #include <linux/etherdevice.h>
11 #include <linux/string.h>
12 #include <linux/phy.h>
13 
14 #include "hns3_enet.h"
15 
16 struct hns3_stats {
17 	char stats_string[ETH_GSTRING_LEN];
18 	int stats_offset;
19 };
20 
21 /* tqp related stats */
22 #define HNS3_TQP_STAT(_string, _member)	{			\
23 	.stats_string = _string,				\
24 	.stats_offset = offsetof(struct hns3_enet_ring, stats) +\
25 			offsetof(struct ring_stats, _member),   \
26 }
27 
28 static const struct hns3_stats hns3_txq_stats[] = {
29 	/* Tx per-queue statistics */
30 	HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
31 	HNS3_TQP_STAT("tx_dropped", sw_err_cnt),
32 	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
33 	HNS3_TQP_STAT("packets", tx_pkts),
34 	HNS3_TQP_STAT("bytes", tx_bytes),
35 	HNS3_TQP_STAT("errors", tx_err_cnt),
36 	HNS3_TQP_STAT("tx_wake", restart_queue),
37 	HNS3_TQP_STAT("tx_busy", tx_busy),
38 };
39 
40 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
41 
42 static const struct hns3_stats hns3_rxq_stats[] = {
43 	/* Rx per-queue statistics */
44 	HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
45 	HNS3_TQP_STAT("rx_dropped", sw_err_cnt),
46 	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
47 	HNS3_TQP_STAT("packets", rx_pkts),
48 	HNS3_TQP_STAT("bytes", rx_bytes),
49 	HNS3_TQP_STAT("errors", rx_err_cnt),
50 	HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
51 	HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
52 	HNS3_TQP_STAT("non_vld_descs", non_vld_descs),
53 	HNS3_TQP_STAT("err_bd_num", err_bd_num),
54 	HNS3_TQP_STAT("l2_err", l2_err),
55 	HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
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_TPYE_NUM		1
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 
67 /* Nic loopback test err  */
68 #define HNS3_NIC_LB_TEST_NO_MEM_ERR	1
69 #define HNS3_NIC_LB_TEST_TX_CNT_ERR	2
70 #define HNS3_NIC_LB_TEST_RX_CNT_ERR	3
71 
72 struct hns3_link_mode_mapping {
73 	u32 hns3_link_mode;
74 	u32 ethtool_link_mode;
75 };
76 
77 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
78 {
79 	struct hnae3_handle *h = hns3_get_handle(ndev);
80 	int ret;
81 
82 	if (!h->ae_algo->ops->set_loopback ||
83 	    !h->ae_algo->ops->set_promisc_mode)
84 		return -EOPNOTSUPP;
85 
86 	switch (loop) {
87 	case HNAE3_MAC_INTER_LOOP_MAC:
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)
96 		return ret;
97 
98 	h->ae_algo->ops->set_promisc_mode(h, en, en);
99 
100 	return ret;
101 }
102 
103 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
104 {
105 	struct hnae3_handle *h = hns3_get_handle(ndev);
106 	int ret;
107 
108 	if (!h->ae_algo->ops->start)
109 		return -EOPNOTSUPP;
110 
111 	ret = hns3_nic_reset_all_ring(h);
112 	if (ret)
113 		return ret;
114 
115 	ret = h->ae_algo->ops->start(h);
116 	if (ret) {
117 		netdev_err(ndev,
118 			   "hns3_lb_up ae start return error: %d\n", ret);
119 		return ret;
120 	}
121 
122 	ret = hns3_lp_setup(ndev, loop_mode, true);
123 	usleep_range(10000, 20000);
124 
125 	return ret;
126 }
127 
128 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
129 {
130 	struct hnae3_handle *h = hns3_get_handle(ndev);
131 	int ret;
132 
133 	if (!h->ae_algo->ops->stop)
134 		return -EOPNOTSUPP;
135 
136 	ret = hns3_lp_setup(ndev, loop_mode, false);
137 	if (ret) {
138 		netdev_err(ndev, "lb_setup return error: %d\n", ret);
139 		return ret;
140 	}
141 
142 	h->ae_algo->ops->stop(h);
143 	usleep_range(10000, 20000);
144 
145 	return 0;
146 }
147 
148 static void hns3_lp_setup_skb(struct sk_buff *skb)
149 {
150 	struct net_device *ndev = skb->dev;
151 	unsigned char *packet;
152 	struct ethhdr *ethh;
153 	unsigned int i;
154 
155 	skb_reserve(skb, NET_IP_ALIGN);
156 	ethh = skb_put(skb, sizeof(struct ethhdr));
157 	packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
158 
159 	memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
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 		hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
205 
206 		rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
207 		rx_group->total_packets = pre_rx_pkt;
208 	}
209 	return rcv_good_pkt_total;
210 }
211 
212 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
213 				  u32 end_ringid, u32 budget)
214 {
215 	u32 i;
216 
217 	for (i = start_ringid; i <= end_ringid; i++) {
218 		struct hns3_enet_ring *ring = priv->ring_data[i].ring;
219 
220 		hns3_clean_tx_ring(ring, budget);
221 	}
222 }
223 
224 /**
225  * hns3_lp_run_test -  run loopback test
226  * @ndev: net device
227  * @mode: loopback type
228  */
229 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
230 {
231 	struct hns3_nic_priv *priv = netdev_priv(ndev);
232 	struct sk_buff *skb;
233 	u32 i, good_cnt;
234 	int ret_val = 0;
235 
236 	skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
237 			GFP_KERNEL);
238 	if (!skb)
239 		return HNS3_NIC_LB_TEST_NO_MEM_ERR;
240 
241 	skb->dev = ndev;
242 	hns3_lp_setup_skb(skb);
243 	skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
244 
245 	good_cnt = 0;
246 	for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
247 		netdev_tx_t tx_ret;
248 
249 		skb_get(skb);
250 		tx_ret = hns3_nic_net_xmit(skb, ndev);
251 		if (tx_ret == NETDEV_TX_OK)
252 			good_cnt++;
253 		else
254 			netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
255 				   tx_ret);
256 	}
257 	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
258 		ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
259 		netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
260 			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
261 		goto out;
262 	}
263 
264 	/* Allow 200 milliseconds for packets to go from Tx to Rx */
265 	msleep(200);
266 
267 	good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
268 	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
269 		ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
270 		netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
271 			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
272 	}
273 
274 out:
275 	hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
276 			      HNS3_NIC_LB_TEST_RING_ID,
277 			      HNS3_NIC_LB_TEST_PKT_NUM);
278 
279 	kfree_skb(skb);
280 	return ret_val;
281 }
282 
283 /**
284  * hns3_nic_self_test - self test
285  * @ndev: net device
286  * @eth_test: test cmd
287  * @data: test result
288  */
289 static void hns3_self_test(struct net_device *ndev,
290 			   struct ethtool_test *eth_test, u64 *data)
291 {
292 	struct hns3_nic_priv *priv = netdev_priv(ndev);
293 	struct hnae3_handle *h = priv->ae_handle;
294 	int st_param[HNS3_SELF_TEST_TPYE_NUM][2];
295 	bool if_running = netif_running(ndev);
296 #if IS_ENABLED(CONFIG_VLAN_8021Q)
297 	bool dis_vlan_filter;
298 #endif
299 	int test_index = 0;
300 	u32 i;
301 
302 	/* Only do offline selftest, or pass by default */
303 	if (eth_test->flags != ETH_TEST_FL_OFFLINE)
304 		return;
305 
306 	st_param[HNAE3_MAC_INTER_LOOP_MAC][0] = HNAE3_MAC_INTER_LOOP_MAC;
307 	st_param[HNAE3_MAC_INTER_LOOP_MAC][1] =
308 			h->flags & HNAE3_SUPPORT_MAC_LOOPBACK;
309 
310 	if (if_running)
311 		dev_close(ndev);
312 
313 #if IS_ENABLED(CONFIG_VLAN_8021Q)
314 	/* Disable the vlan filter for selftest does not support it */
315 	dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
316 				h->ae_algo->ops->enable_vlan_filter;
317 	if (dis_vlan_filter)
318 		h->ae_algo->ops->enable_vlan_filter(h, false);
319 #endif
320 
321 	set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
322 
323 	for (i = 0; i < HNS3_SELF_TEST_TPYE_NUM; i++) {
324 		enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
325 
326 		if (!st_param[i][1])
327 			continue;
328 
329 		data[test_index] = hns3_lp_up(ndev, loop_type);
330 		if (!data[test_index]) {
331 			data[test_index] = hns3_lp_run_test(ndev, loop_type);
332 			hns3_lp_down(ndev, loop_type);
333 		}
334 
335 		if (data[test_index])
336 			eth_test->flags |= ETH_TEST_FL_FAILED;
337 
338 		test_index++;
339 	}
340 
341 	clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
342 
343 #if IS_ENABLED(CONFIG_VLAN_8021Q)
344 	if (dis_vlan_filter)
345 		h->ae_algo->ops->enable_vlan_filter(h, true);
346 #endif
347 
348 	if (if_running)
349 		dev_open(ndev);
350 }
351 
352 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
353 {
354 	struct hnae3_handle *h = hns3_get_handle(netdev);
355 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
356 
357 	if (!ops->get_sset_count)
358 		return -EOPNOTSUPP;
359 
360 	switch (stringset) {
361 	case ETH_SS_STATS:
362 		return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
363 			ops->get_sset_count(h, stringset));
364 
365 	case ETH_SS_TEST:
366 		return ops->get_sset_count(h, stringset);
367 	}
368 
369 	return 0;
370 }
371 
372 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
373 		u32 stat_count, u32 num_tqps, const char *prefix)
374 {
375 #define MAX_PREFIX_SIZE (6 + 4)
376 	u32 size_left;
377 	u32 i, j;
378 	u32 n1;
379 
380 	for (i = 0; i < num_tqps; i++) {
381 		for (j = 0; j < stat_count; j++) {
382 			data[ETH_GSTRING_LEN - 1] = '\0';
383 
384 			/* first, prepend the prefix string */
385 			n1 = snprintf(data, MAX_PREFIX_SIZE, "%s#%d_",
386 				      prefix, i);
387 			n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
388 			size_left = (ETH_GSTRING_LEN - 1) - n1;
389 
390 			/* now, concatenate the stats string to it */
391 			strncat(data, stats[j].stats_string, size_left);
392 			data += ETH_GSTRING_LEN;
393 		}
394 	}
395 
396 	return data;
397 }
398 
399 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
400 {
401 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
402 	const char tx_prefix[] = "txq";
403 	const char rx_prefix[] = "rxq";
404 
405 	/* get strings for Tx */
406 	data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
407 				   kinfo->num_tqps, tx_prefix);
408 
409 	/* get strings for Rx */
410 	data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
411 				   kinfo->num_tqps, rx_prefix);
412 
413 	return data;
414 }
415 
416 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
417 {
418 	struct hnae3_handle *h = hns3_get_handle(netdev);
419 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
420 	char *buff = (char *)data;
421 
422 	if (!ops->get_strings)
423 		return;
424 
425 	switch (stringset) {
426 	case ETH_SS_STATS:
427 		buff = hns3_get_strings_tqps(h, buff);
428 		h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
429 		break;
430 	case ETH_SS_TEST:
431 		ops->get_strings(h, stringset, data);
432 		break;
433 	}
434 }
435 
436 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
437 {
438 	struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
439 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
440 	struct hns3_enet_ring *ring;
441 	u8 *stat;
442 	int i, j;
443 
444 	/* get stats for Tx */
445 	for (i = 0; i < kinfo->num_tqps; i++) {
446 		ring = nic_priv->ring_data[i].ring;
447 		for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
448 			stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
449 			*data++ = *(u64 *)stat;
450 		}
451 	}
452 
453 	/* get stats for Rx */
454 	for (i = 0; i < kinfo->num_tqps; i++) {
455 		ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
456 		for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
457 			stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
458 			*data++ = *(u64 *)stat;
459 		}
460 	}
461 
462 	return data;
463 }
464 
465 /* hns3_get_stats - get detail statistics.
466  * @netdev: net device
467  * @stats: statistics info.
468  * @data: statistics data.
469  */
470 static void hns3_get_stats(struct net_device *netdev,
471 			   struct ethtool_stats *stats, u64 *data)
472 {
473 	struct hnae3_handle *h = hns3_get_handle(netdev);
474 	u64 *p = data;
475 
476 	if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
477 		netdev_err(netdev, "could not get any statistics\n");
478 		return;
479 	}
480 
481 	h->ae_algo->ops->update_stats(h, &netdev->stats);
482 
483 	/* get per-queue stats */
484 	p = hns3_get_stats_tqps(h, p);
485 
486 	/* get MAC & other misc hardware stats */
487 	h->ae_algo->ops->get_stats(h, p);
488 }
489 
490 static void hns3_get_drvinfo(struct net_device *netdev,
491 			     struct ethtool_drvinfo *drvinfo)
492 {
493 	struct hns3_nic_priv *priv = netdev_priv(netdev);
494 	struct hnae3_handle *h = priv->ae_handle;
495 
496 	strncpy(drvinfo->version, hns3_driver_version,
497 		sizeof(drvinfo->version));
498 	drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
499 
500 	strncpy(drvinfo->driver, h->pdev->driver->name,
501 		sizeof(drvinfo->driver));
502 	drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
503 
504 	strncpy(drvinfo->bus_info, pci_name(h->pdev),
505 		sizeof(drvinfo->bus_info));
506 	drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
507 
508 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
509 		 priv->ae_handle->ae_algo->ops->get_fw_version(h));
510 }
511 
512 static u32 hns3_get_link(struct net_device *netdev)
513 {
514 	struct hnae3_handle *h = hns3_get_handle(netdev);
515 
516 	if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
517 		return h->ae_algo->ops->get_status(h);
518 	else
519 		return 0;
520 }
521 
522 static void hns3_get_ringparam(struct net_device *netdev,
523 			       struct ethtool_ringparam *param)
524 {
525 	struct hns3_nic_priv *priv = netdev_priv(netdev);
526 	struct hnae3_handle *h = priv->ae_handle;
527 	int queue_num = h->kinfo.num_tqps;
528 
529 	param->tx_max_pending = HNS3_RING_MAX_PENDING;
530 	param->rx_max_pending = HNS3_RING_MAX_PENDING;
531 
532 	param->tx_pending = priv->ring_data[0].ring->desc_num;
533 	param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
534 }
535 
536 static void hns3_get_pauseparam(struct net_device *netdev,
537 				struct ethtool_pauseparam *param)
538 {
539 	struct hnae3_handle *h = hns3_get_handle(netdev);
540 
541 	if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
542 		h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
543 			&param->rx_pause, &param->tx_pause);
544 }
545 
546 static int hns3_set_pauseparam(struct net_device *netdev,
547 			       struct ethtool_pauseparam *param)
548 {
549 	struct hnae3_handle *h = hns3_get_handle(netdev);
550 
551 	if (h->ae_algo->ops->set_pauseparam)
552 		return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
553 						       param->rx_pause,
554 						       param->tx_pause);
555 	return -EOPNOTSUPP;
556 }
557 
558 static int hns3_get_link_ksettings(struct net_device *netdev,
559 				   struct ethtool_link_ksettings *cmd)
560 {
561 	struct hnae3_handle *h = hns3_get_handle(netdev);
562 	u32 flowctrl_adv = 0;
563 	u8 link_stat;
564 
565 	if (!h->ae_algo || !h->ae_algo->ops)
566 		return -EOPNOTSUPP;
567 
568 	/* 1.auto_neg & speed & duplex from cmd */
569 	if (netdev->phydev) {
570 		phy_ethtool_ksettings_get(netdev->phydev, cmd);
571 
572 		return 0;
573 	}
574 
575 	if (h->ae_algo->ops->get_ksettings_an_result)
576 		h->ae_algo->ops->get_ksettings_an_result(h,
577 							 &cmd->base.autoneg,
578 							 &cmd->base.speed,
579 							 &cmd->base.duplex);
580 	else
581 		return -EOPNOTSUPP;
582 
583 	link_stat = hns3_get_link(netdev);
584 	if (!link_stat) {
585 		cmd->base.speed = SPEED_UNKNOWN;
586 		cmd->base.duplex = DUPLEX_UNKNOWN;
587 	}
588 
589 	/* 2.get link mode and port type*/
590 	if (h->ae_algo->ops->get_link_mode)
591 		h->ae_algo->ops->get_link_mode(h,
592 					       cmd->link_modes.supported,
593 					       cmd->link_modes.advertising);
594 
595 	cmd->base.port = PORT_NONE;
596 	if (h->ae_algo->ops->get_port_type)
597 		h->ae_algo->ops->get_port_type(h,
598 					       &cmd->base.port);
599 
600 	/* 3.mdix_ctrl&mdix get from phy reg */
601 	if (h->ae_algo->ops->get_mdix_mode)
602 		h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
603 					       &cmd->base.eth_tp_mdix);
604 	/* 4.mdio_support */
605 	cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
606 
607 	/* 5.get flow control setttings */
608 	if (h->ae_algo->ops->get_flowctrl_adv)
609 		h->ae_algo->ops->get_flowctrl_adv(h, &flowctrl_adv);
610 
611 	if (flowctrl_adv & ADVERTISED_Pause)
612 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
613 						     Pause);
614 
615 	if (flowctrl_adv & ADVERTISED_Asym_Pause)
616 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
617 						     Asym_Pause);
618 
619 	return 0;
620 }
621 
622 static int hns3_set_link_ksettings(struct net_device *netdev,
623 				   const struct ethtool_link_ksettings *cmd)
624 {
625 	/* Only support ksettings_set for netdev with phy attached for now */
626 	if (netdev->phydev)
627 		return phy_ethtool_ksettings_set(netdev->phydev, cmd);
628 
629 	return -EOPNOTSUPP;
630 }
631 
632 static u32 hns3_get_rss_key_size(struct net_device *netdev)
633 {
634 	struct hnae3_handle *h = hns3_get_handle(netdev);
635 
636 	if (!h->ae_algo || !h->ae_algo->ops ||
637 	    !h->ae_algo->ops->get_rss_key_size)
638 		return 0;
639 
640 	return h->ae_algo->ops->get_rss_key_size(h);
641 }
642 
643 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
644 {
645 	struct hnae3_handle *h = hns3_get_handle(netdev);
646 
647 	if (!h->ae_algo || !h->ae_algo->ops ||
648 	    !h->ae_algo->ops->get_rss_indir_size)
649 		return 0;
650 
651 	return h->ae_algo->ops->get_rss_indir_size(h);
652 }
653 
654 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
655 			u8 *hfunc)
656 {
657 	struct hnae3_handle *h = hns3_get_handle(netdev);
658 
659 	if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
660 		return -EOPNOTSUPP;
661 
662 	return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
663 }
664 
665 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
666 			const u8 *key, const u8 hfunc)
667 {
668 	struct hnae3_handle *h = hns3_get_handle(netdev);
669 
670 	if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
671 		return -EOPNOTSUPP;
672 
673 	/* currently we only support Toeplitz hash */
674 	if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
675 		netdev_err(netdev,
676 			   "hash func not supported (only Toeplitz hash)\n");
677 		return -EOPNOTSUPP;
678 	}
679 	if (!indir) {
680 		netdev_err(netdev,
681 			   "set rss failed for indir is empty\n");
682 		return -EOPNOTSUPP;
683 	}
684 
685 	return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
686 }
687 
688 static int hns3_get_rxnfc(struct net_device *netdev,
689 			  struct ethtool_rxnfc *cmd,
690 			  u32 *rule_locs)
691 {
692 	struct hnae3_handle *h = hns3_get_handle(netdev);
693 
694 	if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss_tuple)
695 		return -EOPNOTSUPP;
696 
697 	switch (cmd->cmd) {
698 	case ETHTOOL_GRXRINGS:
699 		cmd->data = h->kinfo.rss_size;
700 		break;
701 	case ETHTOOL_GRXFH:
702 		return h->ae_algo->ops->get_rss_tuple(h, cmd);
703 	default:
704 		return -EOPNOTSUPP;
705 	}
706 
707 	return 0;
708 }
709 
710 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
711 				       u32 new_desc_num)
712 {
713 	struct hnae3_handle *h = priv->ae_handle;
714 	int i;
715 
716 	h->kinfo.num_desc = new_desc_num;
717 
718 	for (i = 0; i < h->kinfo.num_tqps * 2; i++)
719 		priv->ring_data[i].ring->desc_num = new_desc_num;
720 
721 	return hns3_init_all_ring(priv);
722 }
723 
724 static int hns3_set_ringparam(struct net_device *ndev,
725 			      struct ethtool_ringparam *param)
726 {
727 	struct hns3_nic_priv *priv = netdev_priv(ndev);
728 	struct hnae3_handle *h = priv->ae_handle;
729 	bool if_running = netif_running(ndev);
730 	u32 old_desc_num, new_desc_num;
731 	int ret;
732 
733 	if (param->rx_mini_pending || param->rx_jumbo_pending)
734 		return -EINVAL;
735 
736 	if (param->tx_pending != param->rx_pending) {
737 		netdev_err(ndev,
738 			   "Descriptors of tx and rx must be equal");
739 		return -EINVAL;
740 	}
741 
742 	if (param->tx_pending > HNS3_RING_MAX_PENDING ||
743 	    param->tx_pending < HNS3_RING_MIN_PENDING) {
744 		netdev_err(ndev,
745 			   "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
746 			   param->tx_pending, HNS3_RING_MIN_PENDING,
747 			   HNS3_RING_MAX_PENDING);
748 		return -EINVAL;
749 	}
750 
751 	new_desc_num = param->tx_pending;
752 
753 	/* Hardware requires that its descriptors must be multiple of eight */
754 	new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
755 	old_desc_num = h->kinfo.num_desc;
756 	if (old_desc_num == new_desc_num)
757 		return 0;
758 
759 	netdev_info(ndev,
760 		    "Changing descriptor count from %d to %d.\n",
761 		    old_desc_num, new_desc_num);
762 
763 	if (if_running)
764 		dev_close(ndev);
765 
766 	ret = hns3_uninit_all_ring(priv);
767 	if (ret)
768 		return ret;
769 
770 	ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
771 	if (ret) {
772 		ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
773 		if (ret) {
774 			netdev_err(ndev,
775 				   "Revert to old bd num fail, ret=%d.\n", ret);
776 			return ret;
777 		}
778 	}
779 
780 	if (if_running)
781 		ret = dev_open(ndev);
782 
783 	return ret;
784 }
785 
786 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
787 {
788 	struct hnae3_handle *h = hns3_get_handle(netdev);
789 
790 	if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss_tuple)
791 		return -EOPNOTSUPP;
792 
793 	switch (cmd->cmd) {
794 	case ETHTOOL_SRXFH:
795 		return h->ae_algo->ops->set_rss_tuple(h, cmd);
796 	default:
797 		return -EOPNOTSUPP;
798 	}
799 }
800 
801 static int hns3_nway_reset(struct net_device *netdev)
802 {
803 	struct phy_device *phy = netdev->phydev;
804 
805 	if (!netif_running(netdev))
806 		return 0;
807 
808 	/* Only support nway_reset for netdev with phy attached for now */
809 	if (!phy)
810 		return -EOPNOTSUPP;
811 
812 	if (phy->autoneg != AUTONEG_ENABLE)
813 		return -EINVAL;
814 
815 	return genphy_restart_aneg(phy);
816 }
817 
818 static void hns3_get_channels(struct net_device *netdev,
819 			      struct ethtool_channels *ch)
820 {
821 	struct hnae3_handle *h = hns3_get_handle(netdev);
822 
823 	if (h->ae_algo->ops->get_channels)
824 		h->ae_algo->ops->get_channels(h, ch);
825 }
826 
827 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
828 				       struct ethtool_coalesce *cmd)
829 {
830 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
831 	struct hns3_nic_priv *priv = netdev_priv(netdev);
832 	struct hnae3_handle *h = priv->ae_handle;
833 	u16 queue_num = h->kinfo.num_tqps;
834 
835 	if (queue >= queue_num) {
836 		netdev_err(netdev,
837 			   "Invalid queue value %d! Queue max id=%d\n",
838 			   queue, queue_num - 1);
839 		return -EINVAL;
840 	}
841 
842 	tx_vector = priv->ring_data[queue].ring->tqp_vector;
843 	rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
844 
845 	cmd->use_adaptive_tx_coalesce =
846 			tx_vector->tx_group.coal.gl_adapt_enable;
847 	cmd->use_adaptive_rx_coalesce =
848 			rx_vector->rx_group.coal.gl_adapt_enable;
849 
850 	cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
851 	cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
852 
853 	cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
854 	cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
855 
856 	return 0;
857 }
858 
859 static int hns3_get_coalesce(struct net_device *netdev,
860 			     struct ethtool_coalesce *cmd)
861 {
862 	return hns3_get_coalesce_per_queue(netdev, 0, cmd);
863 }
864 
865 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
866 				       struct ethtool_coalesce *cmd)
867 {
868 	u32 rx_gl, tx_gl;
869 
870 	if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
871 		netdev_err(netdev,
872 			   "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
873 			   HNS3_INT_GL_MAX);
874 		return -EINVAL;
875 	}
876 
877 	if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
878 		netdev_err(netdev,
879 			   "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
880 			   HNS3_INT_GL_MAX);
881 		return -EINVAL;
882 	}
883 
884 	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
885 	if (rx_gl != cmd->rx_coalesce_usecs) {
886 		netdev_info(netdev,
887 			    "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
888 			    cmd->rx_coalesce_usecs, rx_gl);
889 	}
890 
891 	tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
892 	if (tx_gl != cmd->tx_coalesce_usecs) {
893 		netdev_info(netdev,
894 			    "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
895 			    cmd->tx_coalesce_usecs, tx_gl);
896 	}
897 
898 	return 0;
899 }
900 
901 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
902 				       struct ethtool_coalesce *cmd)
903 {
904 	u32 rl;
905 
906 	if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
907 		netdev_err(netdev,
908 			   "tx_usecs_high must be same as rx_usecs_high.\n");
909 		return -EINVAL;
910 	}
911 
912 	if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
913 		netdev_err(netdev,
914 			   "Invalid usecs_high value, usecs_high range is 0-%d\n",
915 			   HNS3_INT_RL_MAX);
916 		return -EINVAL;
917 	}
918 
919 	rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
920 	if (rl != cmd->rx_coalesce_usecs_high) {
921 		netdev_info(netdev,
922 			    "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
923 			    cmd->rx_coalesce_usecs_high, rl);
924 	}
925 
926 	return 0;
927 }
928 
929 static int hns3_check_coalesce_para(struct net_device *netdev,
930 				    struct ethtool_coalesce *cmd)
931 {
932 	int ret;
933 
934 	ret = hns3_check_gl_coalesce_para(netdev, cmd);
935 	if (ret) {
936 		netdev_err(netdev,
937 			   "Check gl coalesce param fail. ret = %d\n", ret);
938 		return ret;
939 	}
940 
941 	ret = hns3_check_rl_coalesce_para(netdev, cmd);
942 	if (ret) {
943 		netdev_err(netdev,
944 			   "Check rl coalesce param fail. ret = %d\n", ret);
945 		return ret;
946 	}
947 
948 	if (cmd->use_adaptive_tx_coalesce == 1 ||
949 	    cmd->use_adaptive_rx_coalesce == 1) {
950 		netdev_info(netdev,
951 			    "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
952 			    cmd->use_adaptive_tx_coalesce,
953 			    cmd->use_adaptive_rx_coalesce);
954 	}
955 
956 	return 0;
957 }
958 
959 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
960 					struct ethtool_coalesce *cmd,
961 					u32 queue)
962 {
963 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
964 	struct hns3_nic_priv *priv = netdev_priv(netdev);
965 	struct hnae3_handle *h = priv->ae_handle;
966 	int queue_num = h->kinfo.num_tqps;
967 
968 	tx_vector = priv->ring_data[queue].ring->tqp_vector;
969 	rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
970 
971 	tx_vector->tx_group.coal.gl_adapt_enable =
972 				cmd->use_adaptive_tx_coalesce;
973 	rx_vector->rx_group.coal.gl_adapt_enable =
974 				cmd->use_adaptive_rx_coalesce;
975 
976 	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
977 	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
978 
979 	hns3_set_vector_coalesce_tx_gl(tx_vector,
980 				       tx_vector->tx_group.coal.int_gl);
981 	hns3_set_vector_coalesce_rx_gl(rx_vector,
982 				       rx_vector->rx_group.coal.int_gl);
983 
984 	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
985 	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
986 }
987 
988 static int hns3_set_coalesce(struct net_device *netdev,
989 			     struct ethtool_coalesce *cmd)
990 {
991 	struct hnae3_handle *h = hns3_get_handle(netdev);
992 	u16 queue_num = h->kinfo.num_tqps;
993 	int ret;
994 	int i;
995 
996 	ret = hns3_check_coalesce_para(netdev, cmd);
997 	if (ret)
998 		return ret;
999 
1000 	h->kinfo.int_rl_setting =
1001 		hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1002 
1003 	for (i = 0; i < queue_num; i++)
1004 		hns3_set_coalesce_per_queue(netdev, cmd, i);
1005 
1006 	return 0;
1007 }
1008 
1009 static int hns3_get_regs_len(struct net_device *netdev)
1010 {
1011 	struct hnae3_handle *h = hns3_get_handle(netdev);
1012 
1013 	if (!h->ae_algo->ops->get_regs_len)
1014 		return -EOPNOTSUPP;
1015 
1016 	return h->ae_algo->ops->get_regs_len(h);
1017 }
1018 
1019 static void hns3_get_regs(struct net_device *netdev,
1020 			  struct ethtool_regs *cmd, void *data)
1021 {
1022 	struct hnae3_handle *h = hns3_get_handle(netdev);
1023 
1024 	if (!h->ae_algo->ops->get_regs)
1025 		return;
1026 
1027 	h->ae_algo->ops->get_regs(h, &cmd->version, data);
1028 }
1029 
1030 static int hns3_set_phys_id(struct net_device *netdev,
1031 			    enum ethtool_phys_id_state state)
1032 {
1033 	struct hnae3_handle *h = hns3_get_handle(netdev);
1034 
1035 	if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id)
1036 		return -EOPNOTSUPP;
1037 
1038 	return h->ae_algo->ops->set_led_id(h, state);
1039 }
1040 
1041 static const struct ethtool_ops hns3vf_ethtool_ops = {
1042 	.get_drvinfo = hns3_get_drvinfo,
1043 	.get_ringparam = hns3_get_ringparam,
1044 	.set_ringparam = hns3_set_ringparam,
1045 	.get_strings = hns3_get_strings,
1046 	.get_ethtool_stats = hns3_get_stats,
1047 	.get_sset_count = hns3_get_sset_count,
1048 	.get_rxnfc = hns3_get_rxnfc,
1049 	.get_rxfh_key_size = hns3_get_rss_key_size,
1050 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1051 	.get_rxfh = hns3_get_rss,
1052 	.set_rxfh = hns3_set_rss,
1053 	.get_link_ksettings = hns3_get_link_ksettings,
1054 	.get_channels = hns3_get_channels,
1055 	.get_coalesce = hns3_get_coalesce,
1056 	.set_coalesce = hns3_set_coalesce,
1057 	.get_link = hns3_get_link,
1058 };
1059 
1060 static const struct ethtool_ops hns3_ethtool_ops = {
1061 	.self_test = hns3_self_test,
1062 	.get_drvinfo = hns3_get_drvinfo,
1063 	.get_link = hns3_get_link,
1064 	.get_ringparam = hns3_get_ringparam,
1065 	.set_ringparam = hns3_set_ringparam,
1066 	.get_pauseparam = hns3_get_pauseparam,
1067 	.set_pauseparam = hns3_set_pauseparam,
1068 	.get_strings = hns3_get_strings,
1069 	.get_ethtool_stats = hns3_get_stats,
1070 	.get_sset_count = hns3_get_sset_count,
1071 	.get_rxnfc = hns3_get_rxnfc,
1072 	.set_rxnfc = hns3_set_rxnfc,
1073 	.get_rxfh_key_size = hns3_get_rss_key_size,
1074 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1075 	.get_rxfh = hns3_get_rss,
1076 	.set_rxfh = hns3_set_rss,
1077 	.get_link_ksettings = hns3_get_link_ksettings,
1078 	.set_link_ksettings = hns3_set_link_ksettings,
1079 	.nway_reset = hns3_nway_reset,
1080 	.get_channels = hns3_get_channels,
1081 	.set_channels = hns3_set_channels,
1082 	.get_coalesce = hns3_get_coalesce,
1083 	.set_coalesce = hns3_set_coalesce,
1084 	.get_regs_len = hns3_get_regs_len,
1085 	.get_regs = hns3_get_regs,
1086 	.set_phys_id = hns3_set_phys_id,
1087 };
1088 
1089 void hns3_ethtool_set_ops(struct net_device *netdev)
1090 {
1091 	struct hnae3_handle *h = hns3_get_handle(netdev);
1092 
1093 	if (h->flags & HNAE3_SUPPORT_VF)
1094 		netdev->ethtool_ops = &hns3vf_ethtool_ops;
1095 	else
1096 		netdev->ethtool_ops = &hns3_ethtool_ops;
1097 }
1098