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