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