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