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 #include "hns3_ethtool.h"
11 
12 /* tqp related stats */
13 #define HNS3_TQP_STAT(_string, _member)	{			\
14 	.stats_string = _string,				\
15 	.stats_offset = offsetof(struct hns3_enet_ring, stats) +\
16 			offsetof(struct ring_stats, _member),   \
17 }
18 
19 static const struct hns3_stats hns3_txq_stats[] = {
20 	/* Tx per-queue statistics */
21 	HNS3_TQP_STAT("dropped", sw_err_cnt),
22 	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
23 	HNS3_TQP_STAT("packets", tx_pkts),
24 	HNS3_TQP_STAT("bytes", tx_bytes),
25 	HNS3_TQP_STAT("more", tx_more),
26 	HNS3_TQP_STAT("wake", restart_queue),
27 	HNS3_TQP_STAT("busy", tx_busy),
28 	HNS3_TQP_STAT("copy", tx_copy),
29 	HNS3_TQP_STAT("vlan_err", tx_vlan_err),
30 	HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
31 	HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
32 	HNS3_TQP_STAT("tso_err", tx_tso_err),
33 	HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
34 	HNS3_TQP_STAT("hw_limitation", hw_limitation),
35 	HNS3_TQP_STAT("bounce", tx_bounce),
36 	HNS3_TQP_STAT("spare_full", tx_spare_full),
37 	HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
38 	HNS3_TQP_STAT("sgl", tx_sgl),
39 	HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
40 	HNS3_TQP_STAT("map_sg_err", map_sg_err),
41 };
42 
43 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
44 
45 static const struct hns3_stats hns3_rxq_stats[] = {
46 	/* Rx per-queue statistics */
47 	HNS3_TQP_STAT("dropped", sw_err_cnt),
48 	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
49 	HNS3_TQP_STAT("packets", rx_pkts),
50 	HNS3_TQP_STAT("bytes", rx_bytes),
51 	HNS3_TQP_STAT("errors", rx_err_cnt),
52 	HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
53 	HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
54 	HNS3_TQP_STAT("err_bd_num", err_bd_num),
55 	HNS3_TQP_STAT("l2_err", l2_err),
56 	HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
57 	HNS3_TQP_STAT("csum_complete", csum_complete),
58 	HNS3_TQP_STAT("multicast", rx_multicast),
59 	HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
60 	HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
61 	HNS3_TQP_STAT("frag_alloc", frag_alloc),
62 };
63 
64 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
65 
66 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
67 
68 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
69 
70 #define HNS3_SELF_TEST_TYPE_NUM         4
71 #define HNS3_NIC_LB_TEST_PKT_NUM	1
72 #define HNS3_NIC_LB_TEST_RING_ID	0
73 #define HNS3_NIC_LB_TEST_PACKET_SIZE	128
74 #define HNS3_NIC_LB_SETUP_USEC		10000
75 
76 /* Nic loopback test err  */
77 #define HNS3_NIC_LB_TEST_NO_MEM_ERR	1
78 #define HNS3_NIC_LB_TEST_TX_CNT_ERR	2
79 #define HNS3_NIC_LB_TEST_RX_CNT_ERR	3
80 
81 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
82 {
83 	struct hnae3_handle *h = hns3_get_handle(ndev);
84 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
85 	int ret;
86 
87 	if (!h->ae_algo->ops->set_loopback ||
88 	    !h->ae_algo->ops->set_promisc_mode)
89 		return -EOPNOTSUPP;
90 
91 	switch (loop) {
92 	case HNAE3_LOOP_SERIAL_SERDES:
93 	case HNAE3_LOOP_PARALLEL_SERDES:
94 	case HNAE3_LOOP_APP:
95 	case HNAE3_LOOP_PHY:
96 		ret = h->ae_algo->ops->set_loopback(h, loop, en);
97 		break;
98 	default:
99 		ret = -ENOTSUPP;
100 		break;
101 	}
102 
103 	if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
104 		return ret;
105 
106 	if (en)
107 		h->ae_algo->ops->set_promisc_mode(h, true, true);
108 	else
109 		/* recover promisc mode before loopback test */
110 		hns3_request_update_promisc_mode(h);
111 
112 	return ret;
113 }
114 
115 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
116 {
117 	struct hnae3_handle *h = hns3_get_handle(ndev);
118 	int ret;
119 
120 	ret = hns3_nic_reset_all_ring(h);
121 	if (ret)
122 		return ret;
123 
124 	ret = hns3_lp_setup(ndev, loop_mode, true);
125 	usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
126 
127 	return ret;
128 }
129 
130 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
131 {
132 	int ret;
133 
134 	ret = hns3_lp_setup(ndev, loop_mode, false);
135 	if (ret) {
136 		netdev_err(ndev, "lb_setup return error: %d\n", ret);
137 		return ret;
138 	}
139 
140 	usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
141 
142 	return 0;
143 }
144 
145 static void hns3_lp_setup_skb(struct sk_buff *skb)
146 {
147 #define	HNS3_NIC_LB_DST_MAC_ADDR	0x1f
148 
149 	struct net_device *ndev = skb->dev;
150 	struct hnae3_handle *handle;
151 	struct hnae3_ae_dev *ae_dev;
152 	unsigned char *packet;
153 	struct ethhdr *ethh;
154 	unsigned int i;
155 
156 	skb_reserve(skb, NET_IP_ALIGN);
157 	ethh = skb_put(skb, sizeof(struct ethhdr));
158 	packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
159 
160 	memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
161 
162 	/* The dst mac addr of loopback packet is the same as the host'
163 	 * mac addr, the SSU component may loop back the packet to host
164 	 * before the packet reaches mac or serdes, which will defect
165 	 * the purpose of mac or serdes selftest.
166 	 */
167 	handle = hns3_get_handle(ndev);
168 	ae_dev = pci_get_drvdata(handle->pdev);
169 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
170 		ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
171 	eth_zero_addr(ethh->h_source);
172 	ethh->h_proto = htons(ETH_P_ARP);
173 	skb_reset_mac_header(skb);
174 
175 	for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
176 		packet[i] = (unsigned char)(i & 0xff);
177 }
178 
179 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
180 				   struct sk_buff *skb)
181 {
182 	struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
183 	unsigned char *packet = skb->data;
184 	u32 len = skb_headlen(skb);
185 	u32 i;
186 
187 	len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
188 
189 	for (i = 0; i < len; i++)
190 		if (packet[i] != (unsigned char)(i & 0xff))
191 			break;
192 
193 	/* The packet is correctly received */
194 	if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
195 		tqp_vector->rx_group.total_packets++;
196 	else
197 		print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
198 			       skb->data, len, true);
199 
200 	dev_kfree_skb_any(skb);
201 }
202 
203 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
204 {
205 	struct hnae3_handle *h = priv->ae_handle;
206 	struct hnae3_knic_private_info *kinfo;
207 	u32 i, rcv_good_pkt_total = 0;
208 
209 	kinfo = &h->kinfo;
210 	for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
211 		struct hns3_enet_ring *ring = &priv->ring[i];
212 		struct hns3_enet_ring_group *rx_group;
213 		u64 pre_rx_pkt;
214 
215 		rx_group = &ring->tqp_vector->rx_group;
216 		pre_rx_pkt = rx_group->total_packets;
217 
218 		preempt_disable();
219 		hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
220 		preempt_enable();
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[i];
235 
236 		hns3_clean_tx_ring(ring, 0);
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 			kfree_skb(skb);
271 			netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
272 				   tx_ret);
273 		}
274 	}
275 	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
276 		ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
277 		netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
278 			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
279 		goto out;
280 	}
281 
282 	/* Allow 200 milliseconds for packets to go from Tx to Rx */
283 	msleep(200);
284 
285 	good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
286 	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
287 		ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
288 		netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
289 			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
290 	}
291 
292 out:
293 	hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
294 			      HNS3_NIC_LB_TEST_RING_ID,
295 			      HNS3_NIC_LB_TEST_PKT_NUM);
296 
297 	kfree_skb(skb);
298 	return ret_val;
299 }
300 
301 static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2])
302 {
303 	st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
304 	st_param[HNAE3_LOOP_APP][1] =
305 			h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
306 
307 	st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
308 	st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
309 			h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
310 
311 	st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
312 			HNAE3_LOOP_PARALLEL_SERDES;
313 	st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
314 			h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
315 
316 	st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
317 	st_param[HNAE3_LOOP_PHY][1] =
318 			h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
319 }
320 
321 static void hns3_selftest_prepare(struct net_device *ndev,
322 				  bool if_running, int (*st_param)[2])
323 {
324 	struct hns3_nic_priv *priv = netdev_priv(ndev);
325 	struct hnae3_handle *h = priv->ae_handle;
326 
327 	if (netif_msg_ifdown(h))
328 		netdev_info(ndev, "self test start\n");
329 
330 	hns3_set_selftest_param(h, st_param);
331 
332 	if (if_running)
333 		ndev->netdev_ops->ndo_stop(ndev);
334 
335 #if IS_ENABLED(CONFIG_VLAN_8021Q)
336 	/* Disable the vlan filter for selftest does not support it */
337 	if (h->ae_algo->ops->enable_vlan_filter)
338 		h->ae_algo->ops->enable_vlan_filter(h, false);
339 #endif
340 
341 	/* Tell firmware to stop mac autoneg before loopback test start,
342 	 * otherwise loopback test may be failed when the port is still
343 	 * negotiating.
344 	 */
345 	if (h->ae_algo->ops->halt_autoneg)
346 		h->ae_algo->ops->halt_autoneg(h, true);
347 
348 	set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
349 }
350 
351 static void hns3_selftest_restore(struct net_device *ndev, bool if_running)
352 {
353 	struct hns3_nic_priv *priv = netdev_priv(ndev);
354 	struct hnae3_handle *h = priv->ae_handle;
355 
356 	clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
357 
358 	if (h->ae_algo->ops->halt_autoneg)
359 		h->ae_algo->ops->halt_autoneg(h, false);
360 
361 #if IS_ENABLED(CONFIG_VLAN_8021Q)
362 	if (h->ae_algo->ops->enable_vlan_filter)
363 		h->ae_algo->ops->enable_vlan_filter(h, true);
364 #endif
365 
366 	if (if_running)
367 		ndev->netdev_ops->ndo_open(ndev);
368 
369 	if (netif_msg_ifdown(h))
370 		netdev_info(ndev, "self test end\n");
371 }
372 
373 static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2],
374 			     struct ethtool_test *eth_test, u64 *data)
375 {
376 	int test_index = 0;
377 	u32 i;
378 
379 	for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
380 		enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
381 
382 		if (!st_param[i][1])
383 			continue;
384 
385 		data[test_index] = hns3_lp_up(ndev, loop_type);
386 		if (!data[test_index])
387 			data[test_index] = hns3_lp_run_test(ndev, loop_type);
388 
389 		hns3_lp_down(ndev, loop_type);
390 
391 		if (data[test_index])
392 			eth_test->flags |= ETH_TEST_FL_FAILED;
393 
394 		test_index++;
395 	}
396 }
397 
398 /**
399  * hns3_nic_self_test - self test
400  * @ndev: net device
401  * @eth_test: test cmd
402  * @data: test result
403  */
404 static void hns3_self_test(struct net_device *ndev,
405 			   struct ethtool_test *eth_test, u64 *data)
406 {
407 	int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
408 	bool if_running = netif_running(ndev);
409 
410 	if (hns3_nic_resetting(ndev)) {
411 		netdev_err(ndev, "dev resetting!");
412 		return;
413 	}
414 
415 	/* Only do offline selftest, or pass by default */
416 	if (eth_test->flags != ETH_TEST_FL_OFFLINE)
417 		return;
418 
419 	hns3_selftest_prepare(ndev, if_running, st_param);
420 	hns3_do_selftest(ndev, st_param, eth_test, data);
421 	hns3_selftest_restore(ndev, if_running);
422 }
423 
424 static void hns3_update_limit_promisc_mode(struct net_device *netdev,
425 					   bool enable)
426 {
427 	struct hnae3_handle *handle = hns3_get_handle(netdev);
428 
429 	if (enable)
430 		set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
431 	else
432 		clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
433 
434 	hns3_request_update_promisc_mode(handle);
435 }
436 
437 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
438 	{ "limit_promisc",	hns3_update_limit_promisc_mode }
439 };
440 
441 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
442 {
443 	struct hnae3_handle *h = hns3_get_handle(netdev);
444 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
445 
446 	if (!ops->get_sset_count)
447 		return -EOPNOTSUPP;
448 
449 	switch (stringset) {
450 	case ETH_SS_STATS:
451 		return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
452 			ops->get_sset_count(h, stringset));
453 
454 	case ETH_SS_TEST:
455 		return ops->get_sset_count(h, stringset);
456 
457 	case ETH_SS_PRIV_FLAGS:
458 		return HNAE3_PFLAG_MAX;
459 
460 	default:
461 		return -EOPNOTSUPP;
462 	}
463 }
464 
465 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
466 		u32 stat_count, u32 num_tqps, const char *prefix)
467 {
468 #define MAX_PREFIX_SIZE (6 + 4)
469 	u32 size_left;
470 	u32 i, j;
471 	u32 n1;
472 
473 	for (i = 0; i < num_tqps; i++) {
474 		for (j = 0; j < stat_count; j++) {
475 			data[ETH_GSTRING_LEN - 1] = '\0';
476 
477 			/* first, prepend the prefix string */
478 			n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
479 				       prefix, i);
480 			size_left = (ETH_GSTRING_LEN - 1) - n1;
481 
482 			/* now, concatenate the stats string to it */
483 			strncat(data, stats[j].stats_string, size_left);
484 			data += ETH_GSTRING_LEN;
485 		}
486 	}
487 
488 	return data;
489 }
490 
491 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
492 {
493 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
494 	const char tx_prefix[] = "txq";
495 	const char rx_prefix[] = "rxq";
496 
497 	/* get strings for Tx */
498 	data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
499 				   kinfo->num_tqps, tx_prefix);
500 
501 	/* get strings for Rx */
502 	data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
503 				   kinfo->num_tqps, rx_prefix);
504 
505 	return data;
506 }
507 
508 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
509 {
510 	struct hnae3_handle *h = hns3_get_handle(netdev);
511 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
512 	char *buff = (char *)data;
513 	int i;
514 
515 	if (!ops->get_strings)
516 		return;
517 
518 	switch (stringset) {
519 	case ETH_SS_STATS:
520 		buff = hns3_get_strings_tqps(h, buff);
521 		ops->get_strings(h, stringset, (u8 *)buff);
522 		break;
523 	case ETH_SS_TEST:
524 		ops->get_strings(h, stringset, data);
525 		break;
526 	case ETH_SS_PRIV_FLAGS:
527 		for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
528 			snprintf(buff, ETH_GSTRING_LEN, "%s",
529 				 hns3_priv_flags[i].name);
530 			buff += ETH_GSTRING_LEN;
531 		}
532 		break;
533 	default:
534 		break;
535 	}
536 }
537 
538 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
539 {
540 	struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
541 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
542 	struct hns3_enet_ring *ring;
543 	u8 *stat;
544 	int i, j;
545 
546 	/* get stats for Tx */
547 	for (i = 0; i < kinfo->num_tqps; i++) {
548 		ring = &nic_priv->ring[i];
549 		for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
550 			stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
551 			*data++ = *(u64 *)stat;
552 		}
553 	}
554 
555 	/* get stats for Rx */
556 	for (i = 0; i < kinfo->num_tqps; i++) {
557 		ring = &nic_priv->ring[i + kinfo->num_tqps];
558 		for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
559 			stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
560 			*data++ = *(u64 *)stat;
561 		}
562 	}
563 
564 	return data;
565 }
566 
567 /* hns3_get_stats - get detail statistics.
568  * @netdev: net device
569  * @stats: statistics info.
570  * @data: statistics data.
571  */
572 static void hns3_get_stats(struct net_device *netdev,
573 			   struct ethtool_stats *stats, u64 *data)
574 {
575 	struct hnae3_handle *h = hns3_get_handle(netdev);
576 	u64 *p = data;
577 
578 	if (hns3_nic_resetting(netdev)) {
579 		netdev_err(netdev, "dev resetting, could not get stats\n");
580 		return;
581 	}
582 
583 	if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
584 		netdev_err(netdev, "could not get any statistics\n");
585 		return;
586 	}
587 
588 	h->ae_algo->ops->update_stats(h, &netdev->stats);
589 
590 	/* get per-queue stats */
591 	p = hns3_get_stats_tqps(h, p);
592 
593 	/* get MAC & other misc hardware stats */
594 	h->ae_algo->ops->get_stats(h, p);
595 }
596 
597 static void hns3_get_drvinfo(struct net_device *netdev,
598 			     struct ethtool_drvinfo *drvinfo)
599 {
600 	struct hns3_nic_priv *priv = netdev_priv(netdev);
601 	struct hnae3_handle *h = priv->ae_handle;
602 	u32 fw_version;
603 
604 	if (!h->ae_algo->ops->get_fw_version) {
605 		netdev_err(netdev, "could not get fw version!\n");
606 		return;
607 	}
608 
609 	strncpy(drvinfo->driver, h->pdev->driver->name,
610 		sizeof(drvinfo->driver));
611 	drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
612 
613 	strncpy(drvinfo->bus_info, pci_name(h->pdev),
614 		sizeof(drvinfo->bus_info));
615 	drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
616 
617 	fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
618 
619 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
620 		 "%lu.%lu.%lu.%lu",
621 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
622 				 HNAE3_FW_VERSION_BYTE3_SHIFT),
623 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
624 				 HNAE3_FW_VERSION_BYTE2_SHIFT),
625 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
626 				 HNAE3_FW_VERSION_BYTE1_SHIFT),
627 		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
628 				 HNAE3_FW_VERSION_BYTE0_SHIFT));
629 }
630 
631 static u32 hns3_get_link(struct net_device *netdev)
632 {
633 	struct hnae3_handle *h = hns3_get_handle(netdev);
634 
635 	if (h->ae_algo->ops->get_status)
636 		return h->ae_algo->ops->get_status(h);
637 	else
638 		return 0;
639 }
640 
641 static void hns3_get_ringparam(struct net_device *netdev,
642 			       struct ethtool_ringparam *param)
643 {
644 	struct hns3_nic_priv *priv = netdev_priv(netdev);
645 	struct hnae3_handle *h = priv->ae_handle;
646 	int queue_num = h->kinfo.num_tqps;
647 
648 	if (hns3_nic_resetting(netdev)) {
649 		netdev_err(netdev, "dev resetting!");
650 		return;
651 	}
652 
653 	param->tx_max_pending = HNS3_RING_MAX_PENDING;
654 	param->rx_max_pending = HNS3_RING_MAX_PENDING;
655 
656 	param->tx_pending = priv->ring[0].desc_num;
657 	param->rx_pending = priv->ring[queue_num].desc_num;
658 }
659 
660 static void hns3_get_pauseparam(struct net_device *netdev,
661 				struct ethtool_pauseparam *param)
662 {
663 	struct hnae3_handle *h = hns3_get_handle(netdev);
664 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
665 
666 	if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
667 		return;
668 
669 	if (h->ae_algo->ops->get_pauseparam)
670 		h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
671 			&param->rx_pause, &param->tx_pause);
672 }
673 
674 static int hns3_set_pauseparam(struct net_device *netdev,
675 			       struct ethtool_pauseparam *param)
676 {
677 	struct hnae3_handle *h = hns3_get_handle(netdev);
678 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
679 
680 	if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
681 		return -EOPNOTSUPP;
682 
683 	netif_dbg(h, drv, netdev,
684 		  "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
685 		  param->autoneg, param->rx_pause, param->tx_pause);
686 
687 	if (h->ae_algo->ops->set_pauseparam)
688 		return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
689 						       param->rx_pause,
690 						       param->tx_pause);
691 	return -EOPNOTSUPP;
692 }
693 
694 static void hns3_get_ksettings(struct hnae3_handle *h,
695 			       struct ethtool_link_ksettings *cmd)
696 {
697 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
698 
699 	/* 1.auto_neg & speed & duplex from cmd */
700 	if (ops->get_ksettings_an_result)
701 		ops->get_ksettings_an_result(h,
702 					     &cmd->base.autoneg,
703 					     &cmd->base.speed,
704 					     &cmd->base.duplex);
705 
706 	/* 2.get link mode */
707 	if (ops->get_link_mode)
708 		ops->get_link_mode(h,
709 				   cmd->link_modes.supported,
710 				   cmd->link_modes.advertising);
711 
712 	/* 3.mdix_ctrl&mdix get from phy reg */
713 	if (ops->get_mdix_mode)
714 		ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
715 				   &cmd->base.eth_tp_mdix);
716 }
717 
718 static int hns3_get_link_ksettings(struct net_device *netdev,
719 				   struct ethtool_link_ksettings *cmd)
720 {
721 	struct hnae3_handle *h = hns3_get_handle(netdev);
722 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
723 	const struct hnae3_ae_ops *ops;
724 	u8 module_type;
725 	u8 media_type;
726 	u8 link_stat;
727 
728 	ops = h->ae_algo->ops;
729 	if (ops->get_media_type)
730 		ops->get_media_type(h, &media_type, &module_type);
731 	else
732 		return -EOPNOTSUPP;
733 
734 	switch (media_type) {
735 	case HNAE3_MEDIA_TYPE_NONE:
736 		cmd->base.port = PORT_NONE;
737 		hns3_get_ksettings(h, cmd);
738 		break;
739 	case HNAE3_MEDIA_TYPE_FIBER:
740 		if (module_type == HNAE3_MODULE_TYPE_CR)
741 			cmd->base.port = PORT_DA;
742 		else
743 			cmd->base.port = PORT_FIBRE;
744 
745 		hns3_get_ksettings(h, cmd);
746 		break;
747 	case HNAE3_MEDIA_TYPE_BACKPLANE:
748 		cmd->base.port = PORT_NONE;
749 		hns3_get_ksettings(h, cmd);
750 		break;
751 	case HNAE3_MEDIA_TYPE_COPPER:
752 		cmd->base.port = PORT_TP;
753 		if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
754 		    ops->get_phy_link_ksettings)
755 			ops->get_phy_link_ksettings(h, cmd);
756 		else if (!netdev->phydev)
757 			hns3_get_ksettings(h, cmd);
758 		else
759 			phy_ethtool_ksettings_get(netdev->phydev, cmd);
760 		break;
761 	default:
762 
763 		netdev_warn(netdev, "Unknown media type");
764 		return 0;
765 	}
766 
767 	/* mdio_support */
768 	cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
769 
770 	link_stat = hns3_get_link(netdev);
771 	if (!link_stat) {
772 		cmd->base.speed = SPEED_UNKNOWN;
773 		cmd->base.duplex = DUPLEX_UNKNOWN;
774 	}
775 
776 	return 0;
777 }
778 
779 static int hns3_check_ksettings_param(const struct net_device *netdev,
780 				      const struct ethtool_link_ksettings *cmd)
781 {
782 	struct hnae3_handle *handle = hns3_get_handle(netdev);
783 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
784 	u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
785 	u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
786 	u8 autoneg;
787 	u32 speed;
788 	u8 duplex;
789 	int ret;
790 
791 	/* hw doesn't support use specified speed and duplex to negotiate,
792 	 * unnecessary to check them when autoneg on.
793 	 */
794 	if (cmd->base.autoneg)
795 		return 0;
796 
797 	if (ops->get_ksettings_an_result) {
798 		ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
799 		if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
800 		    cmd->base.duplex == duplex)
801 			return 0;
802 	}
803 
804 	if (ops->get_media_type)
805 		ops->get_media_type(handle, &media_type, &module_type);
806 
807 	if (cmd->base.duplex == DUPLEX_HALF &&
808 	    media_type != HNAE3_MEDIA_TYPE_COPPER) {
809 		netdev_err(netdev,
810 			   "only copper port supports half duplex!");
811 		return -EINVAL;
812 	}
813 
814 	if (ops->check_port_speed) {
815 		ret = ops->check_port_speed(handle, cmd->base.speed);
816 		if (ret) {
817 			netdev_err(netdev, "unsupported speed\n");
818 			return ret;
819 		}
820 	}
821 
822 	return 0;
823 }
824 
825 static int hns3_set_link_ksettings(struct net_device *netdev,
826 				   const struct ethtool_link_ksettings *cmd)
827 {
828 	struct hnae3_handle *handle = hns3_get_handle(netdev);
829 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
830 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
831 	int ret;
832 
833 	/* Chip don't support this mode. */
834 	if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
835 		return -EINVAL;
836 
837 	netif_dbg(handle, drv, netdev,
838 		  "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
839 		  netdev->phydev ? "phy" : "mac",
840 		  cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
841 
842 	/* Only support ksettings_set for netdev with phy attached for now */
843 	if (netdev->phydev) {
844 		if (cmd->base.speed == SPEED_1000 &&
845 		    cmd->base.autoneg == AUTONEG_DISABLE)
846 			return -EINVAL;
847 
848 		return phy_ethtool_ksettings_set(netdev->phydev, cmd);
849 	} else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
850 		   ops->set_phy_link_ksettings) {
851 		return ops->set_phy_link_ksettings(handle, cmd);
852 	}
853 
854 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
855 		return -EOPNOTSUPP;
856 
857 	ret = hns3_check_ksettings_param(netdev, cmd);
858 	if (ret)
859 		return ret;
860 
861 	if (ops->set_autoneg) {
862 		ret = ops->set_autoneg(handle, cmd->base.autoneg);
863 		if (ret)
864 			return ret;
865 	}
866 
867 	/* hw doesn't support use specified speed and duplex to negotiate,
868 	 * ignore them when autoneg on.
869 	 */
870 	if (cmd->base.autoneg) {
871 		netdev_info(netdev,
872 			    "autoneg is on, ignore the speed and duplex\n");
873 		return 0;
874 	}
875 
876 	if (ops->cfg_mac_speed_dup_h)
877 		ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
878 					       cmd->base.duplex);
879 
880 	return ret;
881 }
882 
883 static u32 hns3_get_rss_key_size(struct net_device *netdev)
884 {
885 	struct hnae3_handle *h = hns3_get_handle(netdev);
886 
887 	if (!h->ae_algo->ops->get_rss_key_size)
888 		return 0;
889 
890 	return h->ae_algo->ops->get_rss_key_size(h);
891 }
892 
893 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
894 {
895 	struct hnae3_handle *h = hns3_get_handle(netdev);
896 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
897 
898 	return ae_dev->dev_specs.rss_ind_tbl_size;
899 }
900 
901 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
902 			u8 *hfunc)
903 {
904 	struct hnae3_handle *h = hns3_get_handle(netdev);
905 
906 	if (!h->ae_algo->ops->get_rss)
907 		return -EOPNOTSUPP;
908 
909 	return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
910 }
911 
912 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
913 			const u8 *key, const u8 hfunc)
914 {
915 	struct hnae3_handle *h = hns3_get_handle(netdev);
916 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
917 
918 	if (!h->ae_algo->ops->set_rss)
919 		return -EOPNOTSUPP;
920 
921 	if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
922 	     hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
923 	     hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
924 		netdev_err(netdev, "hash func not supported\n");
925 		return -EOPNOTSUPP;
926 	}
927 
928 	if (!indir) {
929 		netdev_err(netdev,
930 			   "set rss failed for indir is empty\n");
931 		return -EOPNOTSUPP;
932 	}
933 
934 	return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
935 }
936 
937 static int hns3_get_rxnfc(struct net_device *netdev,
938 			  struct ethtool_rxnfc *cmd,
939 			  u32 *rule_locs)
940 {
941 	struct hnae3_handle *h = hns3_get_handle(netdev);
942 
943 	switch (cmd->cmd) {
944 	case ETHTOOL_GRXRINGS:
945 		cmd->data = h->kinfo.num_tqps;
946 		return 0;
947 	case ETHTOOL_GRXFH:
948 		if (h->ae_algo->ops->get_rss_tuple)
949 			return h->ae_algo->ops->get_rss_tuple(h, cmd);
950 		return -EOPNOTSUPP;
951 	case ETHTOOL_GRXCLSRLCNT:
952 		if (h->ae_algo->ops->get_fd_rule_cnt)
953 			return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
954 		return -EOPNOTSUPP;
955 	case ETHTOOL_GRXCLSRULE:
956 		if (h->ae_algo->ops->get_fd_rule_info)
957 			return h->ae_algo->ops->get_fd_rule_info(h, cmd);
958 		return -EOPNOTSUPP;
959 	case ETHTOOL_GRXCLSRLALL:
960 		if (h->ae_algo->ops->get_fd_all_rules)
961 			return h->ae_algo->ops->get_fd_all_rules(h, cmd,
962 								 rule_locs);
963 		return -EOPNOTSUPP;
964 	default:
965 		return -EOPNOTSUPP;
966 	}
967 }
968 
969 static const struct hns3_reset_type_map hns3_reset_type[] = {
970 	{ETH_RESET_MGMT, HNAE3_IMP_RESET},
971 	{ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
972 	{ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
973 };
974 
975 static const struct hns3_reset_type_map hns3vf_reset_type[] = {
976 	{ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
977 };
978 
979 static int hns3_set_reset(struct net_device *netdev, u32 *flags)
980 {
981 	enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
982 	struct hnae3_handle *h = hns3_get_handle(netdev);
983 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
984 	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
985 	const struct hns3_reset_type_map *rst_type_map;
986 	u32 i, size;
987 
988 	if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
989 		return -EBUSY;
990 
991 	if (!ops->set_default_reset_request || !ops->reset_event)
992 		return -EOPNOTSUPP;
993 
994 	if (h->flags & HNAE3_SUPPORT_VF) {
995 		rst_type_map = hns3vf_reset_type;
996 		size = ARRAY_SIZE(hns3vf_reset_type);
997 	} else {
998 		rst_type_map = hns3_reset_type;
999 		size = ARRAY_SIZE(hns3_reset_type);
1000 	}
1001 
1002 	for (i = 0; i < size; i++) {
1003 		if (rst_type_map[i].rst_flags == *flags) {
1004 			rst_type = rst_type_map[i].rst_type;
1005 			break;
1006 		}
1007 	}
1008 
1009 	if (rst_type == HNAE3_NONE_RESET ||
1010 	    (rst_type == HNAE3_IMP_RESET &&
1011 	     ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
1012 		return -EOPNOTSUPP;
1013 
1014 	netdev_info(netdev, "Setting reset type %d\n", rst_type);
1015 
1016 	ops->set_default_reset_request(ae_dev, rst_type);
1017 
1018 	ops->reset_event(h->pdev, h);
1019 
1020 	return 0;
1021 }
1022 
1023 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
1024 					u32 tx_desc_num, u32 rx_desc_num)
1025 {
1026 	struct hnae3_handle *h = priv->ae_handle;
1027 	int i;
1028 
1029 	h->kinfo.num_tx_desc = tx_desc_num;
1030 	h->kinfo.num_rx_desc = rx_desc_num;
1031 
1032 	for (i = 0; i < h->kinfo.num_tqps; i++) {
1033 		priv->ring[i].desc_num = tx_desc_num;
1034 		priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1035 	}
1036 }
1037 
1038 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1039 {
1040 	struct hnae3_handle *handle = priv->ae_handle;
1041 	struct hns3_enet_ring *tmp_rings;
1042 	int i;
1043 
1044 	tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
1045 			    sizeof(struct hns3_enet_ring), GFP_KERNEL);
1046 	if (!tmp_rings)
1047 		return NULL;
1048 
1049 	for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1050 		memcpy(&tmp_rings[i], &priv->ring[i],
1051 		       sizeof(struct hns3_enet_ring));
1052 		tmp_rings[i].skb = NULL;
1053 	}
1054 
1055 	return tmp_rings;
1056 }
1057 
1058 static int hns3_check_ringparam(struct net_device *ndev,
1059 				struct ethtool_ringparam *param)
1060 {
1061 	if (hns3_nic_resetting(ndev))
1062 		return -EBUSY;
1063 
1064 	if (param->rx_mini_pending || param->rx_jumbo_pending)
1065 		return -EINVAL;
1066 
1067 	if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1068 	    param->tx_pending < HNS3_RING_MIN_PENDING ||
1069 	    param->rx_pending > HNS3_RING_MAX_PENDING ||
1070 	    param->rx_pending < HNS3_RING_MIN_PENDING) {
1071 		netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1072 			   HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1073 		return -EINVAL;
1074 	}
1075 
1076 	return 0;
1077 }
1078 
1079 static int hns3_set_ringparam(struct net_device *ndev,
1080 			      struct ethtool_ringparam *param)
1081 {
1082 	struct hns3_nic_priv *priv = netdev_priv(ndev);
1083 	struct hnae3_handle *h = priv->ae_handle;
1084 	struct hns3_enet_ring *tmp_rings;
1085 	bool if_running = netif_running(ndev);
1086 	u32 old_tx_desc_num, new_tx_desc_num;
1087 	u32 old_rx_desc_num, new_rx_desc_num;
1088 	u16 queue_num = h->kinfo.num_tqps;
1089 	int ret, i;
1090 
1091 	ret = hns3_check_ringparam(ndev, param);
1092 	if (ret)
1093 		return ret;
1094 
1095 	/* Hardware requires that its descriptors must be multiple of eight */
1096 	new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1097 	new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1098 	old_tx_desc_num = priv->ring[0].desc_num;
1099 	old_rx_desc_num = priv->ring[queue_num].desc_num;
1100 	if (old_tx_desc_num == new_tx_desc_num &&
1101 	    old_rx_desc_num == new_rx_desc_num)
1102 		return 0;
1103 
1104 	tmp_rings = hns3_backup_ringparam(priv);
1105 	if (!tmp_rings) {
1106 		netdev_err(ndev,
1107 			   "backup ring param failed by allocating memory fail\n");
1108 		return -ENOMEM;
1109 	}
1110 
1111 	netdev_info(ndev,
1112 		    "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
1113 		    old_tx_desc_num, old_rx_desc_num,
1114 		    new_tx_desc_num, new_rx_desc_num);
1115 
1116 	if (if_running)
1117 		ndev->netdev_ops->ndo_stop(ndev);
1118 
1119 	hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1120 	ret = hns3_init_all_ring(priv);
1121 	if (ret) {
1122 		netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1123 			   ret);
1124 
1125 		hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1126 					    old_rx_desc_num);
1127 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1128 			memcpy(&priv->ring[i], &tmp_rings[i],
1129 			       sizeof(struct hns3_enet_ring));
1130 	} else {
1131 		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1132 			hns3_fini_ring(&tmp_rings[i]);
1133 	}
1134 
1135 	kfree(tmp_rings);
1136 
1137 	if (if_running)
1138 		ret = ndev->netdev_ops->ndo_open(ndev);
1139 
1140 	return ret;
1141 }
1142 
1143 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1144 {
1145 	struct hnae3_handle *h = hns3_get_handle(netdev);
1146 
1147 	switch (cmd->cmd) {
1148 	case ETHTOOL_SRXFH:
1149 		if (h->ae_algo->ops->set_rss_tuple)
1150 			return h->ae_algo->ops->set_rss_tuple(h, cmd);
1151 		return -EOPNOTSUPP;
1152 	case ETHTOOL_SRXCLSRLINS:
1153 		if (h->ae_algo->ops->add_fd_entry)
1154 			return h->ae_algo->ops->add_fd_entry(h, cmd);
1155 		return -EOPNOTSUPP;
1156 	case ETHTOOL_SRXCLSRLDEL:
1157 		if (h->ae_algo->ops->del_fd_entry)
1158 			return h->ae_algo->ops->del_fd_entry(h, cmd);
1159 		return -EOPNOTSUPP;
1160 	default:
1161 		return -EOPNOTSUPP;
1162 	}
1163 }
1164 
1165 static int hns3_nway_reset(struct net_device *netdev)
1166 {
1167 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1168 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1169 	struct phy_device *phy = netdev->phydev;
1170 	int autoneg;
1171 
1172 	if (!netif_running(netdev))
1173 		return 0;
1174 
1175 	if (hns3_nic_resetting(netdev)) {
1176 		netdev_err(netdev, "dev resetting!");
1177 		return -EBUSY;
1178 	}
1179 
1180 	if (!ops->get_autoneg || !ops->restart_autoneg)
1181 		return -EOPNOTSUPP;
1182 
1183 	autoneg = ops->get_autoneg(handle);
1184 	if (autoneg != AUTONEG_ENABLE) {
1185 		netdev_err(netdev,
1186 			   "Autoneg is off, don't support to restart it\n");
1187 		return -EINVAL;
1188 	}
1189 
1190 	netif_dbg(handle, drv, netdev,
1191 		  "nway reset (using %s)\n", phy ? "phy" : "mac");
1192 
1193 	if (phy)
1194 		return genphy_restart_aneg(phy);
1195 
1196 	return ops->restart_autoneg(handle);
1197 }
1198 
1199 static void hns3_get_channels(struct net_device *netdev,
1200 			      struct ethtool_channels *ch)
1201 {
1202 	struct hnae3_handle *h = hns3_get_handle(netdev);
1203 
1204 	if (h->ae_algo->ops->get_channels)
1205 		h->ae_algo->ops->get_channels(h, ch);
1206 }
1207 
1208 static int hns3_get_coalesce(struct net_device *netdev,
1209 			     struct ethtool_coalesce *cmd,
1210 			     struct kernel_ethtool_coalesce *kernel_coal,
1211 			     struct netlink_ext_ack *extack)
1212 {
1213 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1214 	struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1215 	struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1216 	struct hnae3_handle *h = priv->ae_handle;
1217 
1218 	if (hns3_nic_resetting(netdev))
1219 		return -EBUSY;
1220 
1221 	cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1222 	cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1223 
1224 	cmd->tx_coalesce_usecs = tx_coal->int_gl;
1225 	cmd->rx_coalesce_usecs = rx_coal->int_gl;
1226 
1227 	cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1228 	cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1229 
1230 	cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1231 	cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1232 
1233 	kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode ==
1234 					DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1235 	kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode ==
1236 					DIM_CQ_PERIOD_MODE_START_FROM_CQE);
1237 
1238 	return 0;
1239 }
1240 
1241 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1242 				       struct ethtool_coalesce *cmd)
1243 {
1244 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1245 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1246 	u32 rx_gl, tx_gl;
1247 
1248 	if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1249 		netdev_err(netdev,
1250 			   "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1251 			   ae_dev->dev_specs.max_int_gl);
1252 		return -EINVAL;
1253 	}
1254 
1255 	if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1256 		netdev_err(netdev,
1257 			   "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1258 			   ae_dev->dev_specs.max_int_gl);
1259 		return -EINVAL;
1260 	}
1261 
1262 	/* device version above V3(include V3), GL uses 1us unit,
1263 	 * so the round down is not needed.
1264 	 */
1265 	if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1266 		return 0;
1267 
1268 	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1269 	if (rx_gl != cmd->rx_coalesce_usecs) {
1270 		netdev_info(netdev,
1271 			    "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1272 			    cmd->rx_coalesce_usecs, rx_gl);
1273 	}
1274 
1275 	tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1276 	if (tx_gl != cmd->tx_coalesce_usecs) {
1277 		netdev_info(netdev,
1278 			    "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1279 			    cmd->tx_coalesce_usecs, tx_gl);
1280 	}
1281 
1282 	return 0;
1283 }
1284 
1285 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1286 				       struct ethtool_coalesce *cmd)
1287 {
1288 	u32 rl;
1289 
1290 	if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1291 		netdev_err(netdev,
1292 			   "tx_usecs_high must be same as rx_usecs_high.\n");
1293 		return -EINVAL;
1294 	}
1295 
1296 	if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1297 		netdev_err(netdev,
1298 			   "Invalid usecs_high value, usecs_high range is 0-%d\n",
1299 			   HNS3_INT_RL_MAX);
1300 		return -EINVAL;
1301 	}
1302 
1303 	rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1304 	if (rl != cmd->rx_coalesce_usecs_high) {
1305 		netdev_info(netdev,
1306 			    "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1307 			    cmd->rx_coalesce_usecs_high, rl);
1308 	}
1309 
1310 	return 0;
1311 }
1312 
1313 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1314 					struct ethtool_coalesce *cmd)
1315 {
1316 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1317 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1318 
1319 	if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1320 	    !ae_dev->dev_specs.int_ql_max) {
1321 		netdev_err(netdev, "coalesced frames is not supported\n");
1322 		return -EOPNOTSUPP;
1323 	}
1324 
1325 	if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1326 	    cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1327 		netdev_err(netdev,
1328 			   "invalid coalesced_frames value, range is 0-%u\n",
1329 			   ae_dev->dev_specs.int_ql_max);
1330 		return -ERANGE;
1331 	}
1332 
1333 	return 0;
1334 }
1335 
1336 static int hns3_check_coalesce_para(struct net_device *netdev,
1337 				    struct ethtool_coalesce *cmd)
1338 {
1339 	int ret;
1340 
1341 	ret = hns3_check_gl_coalesce_para(netdev, cmd);
1342 	if (ret) {
1343 		netdev_err(netdev,
1344 			   "Check gl coalesce param fail. ret = %d\n", ret);
1345 		return ret;
1346 	}
1347 
1348 	ret = hns3_check_rl_coalesce_para(netdev, cmd);
1349 	if (ret) {
1350 		netdev_err(netdev,
1351 			   "Check rl coalesce param fail. ret = %d\n", ret);
1352 		return ret;
1353 	}
1354 
1355 	return hns3_check_ql_coalesce_param(netdev, cmd);
1356 }
1357 
1358 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1359 					struct ethtool_coalesce *cmd,
1360 					u32 queue)
1361 {
1362 	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1363 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1364 	struct hnae3_handle *h = priv->ae_handle;
1365 	int queue_num = h->kinfo.num_tqps;
1366 
1367 	tx_vector = priv->ring[queue].tqp_vector;
1368 	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1369 
1370 	tx_vector->tx_group.coal.adapt_enable =
1371 				cmd->use_adaptive_tx_coalesce;
1372 	rx_vector->rx_group.coal.adapt_enable =
1373 				cmd->use_adaptive_rx_coalesce;
1374 
1375 	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1376 	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1377 
1378 	tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1379 	rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1380 
1381 	hns3_set_vector_coalesce_tx_gl(tx_vector,
1382 				       tx_vector->tx_group.coal.int_gl);
1383 	hns3_set_vector_coalesce_rx_gl(rx_vector,
1384 				       rx_vector->rx_group.coal.int_gl);
1385 
1386 	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1387 	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1388 
1389 	if (tx_vector->tx_group.coal.ql_enable)
1390 		hns3_set_vector_coalesce_tx_ql(tx_vector,
1391 					       tx_vector->tx_group.coal.int_ql);
1392 	if (rx_vector->rx_group.coal.ql_enable)
1393 		hns3_set_vector_coalesce_rx_ql(rx_vector,
1394 					       rx_vector->rx_group.coal.int_ql);
1395 }
1396 
1397 static int hns3_set_coalesce(struct net_device *netdev,
1398 			     struct ethtool_coalesce *cmd,
1399 			     struct kernel_ethtool_coalesce *kernel_coal,
1400 			     struct netlink_ext_ack *extack)
1401 {
1402 	struct hnae3_handle *h = hns3_get_handle(netdev);
1403 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1404 	struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1405 	struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1406 	u16 queue_num = h->kinfo.num_tqps;
1407 	enum dim_cq_period_mode tx_mode;
1408 	enum dim_cq_period_mode rx_mode;
1409 	int ret;
1410 	int i;
1411 
1412 	if (hns3_nic_resetting(netdev))
1413 		return -EBUSY;
1414 
1415 	ret = hns3_check_coalesce_para(netdev, cmd);
1416 	if (ret)
1417 		return ret;
1418 
1419 	h->kinfo.int_rl_setting =
1420 		hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1421 
1422 	tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1423 	rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1424 
1425 	tx_coal->int_gl = cmd->tx_coalesce_usecs;
1426 	rx_coal->int_gl = cmd->rx_coalesce_usecs;
1427 
1428 	tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1429 	rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1430 
1431 	for (i = 0; i < queue_num; i++)
1432 		hns3_set_coalesce_per_queue(netdev, cmd, i);
1433 
1434 	tx_mode = kernel_coal->use_cqe_mode_tx ?
1435 		  DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1436 		  DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1437 	rx_mode = kernel_coal->use_cqe_mode_rx ?
1438 		  DIM_CQ_PERIOD_MODE_START_FROM_CQE :
1439 		  DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1440 	hns3_cq_period_mode_init(priv, tx_mode, rx_mode);
1441 
1442 	return 0;
1443 }
1444 
1445 static int hns3_get_regs_len(struct net_device *netdev)
1446 {
1447 	struct hnae3_handle *h = hns3_get_handle(netdev);
1448 
1449 	if (!h->ae_algo->ops->get_regs_len)
1450 		return -EOPNOTSUPP;
1451 
1452 	return h->ae_algo->ops->get_regs_len(h);
1453 }
1454 
1455 static void hns3_get_regs(struct net_device *netdev,
1456 			  struct ethtool_regs *cmd, void *data)
1457 {
1458 	struct hnae3_handle *h = hns3_get_handle(netdev);
1459 
1460 	if (!h->ae_algo->ops->get_regs)
1461 		return;
1462 
1463 	h->ae_algo->ops->get_regs(h, &cmd->version, data);
1464 }
1465 
1466 static int hns3_set_phys_id(struct net_device *netdev,
1467 			    enum ethtool_phys_id_state state)
1468 {
1469 	struct hnae3_handle *h = hns3_get_handle(netdev);
1470 
1471 	if (!h->ae_algo->ops->set_led_id)
1472 		return -EOPNOTSUPP;
1473 
1474 	return h->ae_algo->ops->set_led_id(h, state);
1475 }
1476 
1477 static u32 hns3_get_msglevel(struct net_device *netdev)
1478 {
1479 	struct hnae3_handle *h = hns3_get_handle(netdev);
1480 
1481 	return h->msg_enable;
1482 }
1483 
1484 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1485 {
1486 	struct hnae3_handle *h = hns3_get_handle(netdev);
1487 
1488 	h->msg_enable = msg_level;
1489 }
1490 
1491 /* Translate local fec value into ethtool value. */
1492 static unsigned int loc_to_eth_fec(u8 loc_fec)
1493 {
1494 	u32 eth_fec = 0;
1495 
1496 	if (loc_fec & BIT(HNAE3_FEC_AUTO))
1497 		eth_fec |= ETHTOOL_FEC_AUTO;
1498 	if (loc_fec & BIT(HNAE3_FEC_RS))
1499 		eth_fec |= ETHTOOL_FEC_RS;
1500 	if (loc_fec & BIT(HNAE3_FEC_BASER))
1501 		eth_fec |= ETHTOOL_FEC_BASER;
1502 
1503 	/* if nothing is set, then FEC is off */
1504 	if (!eth_fec)
1505 		eth_fec = ETHTOOL_FEC_OFF;
1506 
1507 	return eth_fec;
1508 }
1509 
1510 /* Translate ethtool fec value into local value. */
1511 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1512 {
1513 	u32 loc_fec = 0;
1514 
1515 	if (eth_fec & ETHTOOL_FEC_OFF)
1516 		return loc_fec;
1517 
1518 	if (eth_fec & ETHTOOL_FEC_AUTO)
1519 		loc_fec |= BIT(HNAE3_FEC_AUTO);
1520 	if (eth_fec & ETHTOOL_FEC_RS)
1521 		loc_fec |= BIT(HNAE3_FEC_RS);
1522 	if (eth_fec & ETHTOOL_FEC_BASER)
1523 		loc_fec |= BIT(HNAE3_FEC_BASER);
1524 
1525 	return loc_fec;
1526 }
1527 
1528 static int hns3_get_fecparam(struct net_device *netdev,
1529 			     struct ethtool_fecparam *fec)
1530 {
1531 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1532 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1533 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1534 	u8 fec_ability;
1535 	u8 fec_mode;
1536 
1537 	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1538 		return -EOPNOTSUPP;
1539 
1540 	if (!ops->get_fec)
1541 		return -EOPNOTSUPP;
1542 
1543 	ops->get_fec(handle, &fec_ability, &fec_mode);
1544 
1545 	fec->fec = loc_to_eth_fec(fec_ability);
1546 	fec->active_fec = loc_to_eth_fec(fec_mode);
1547 
1548 	return 0;
1549 }
1550 
1551 static int hns3_set_fecparam(struct net_device *netdev,
1552 			     struct ethtool_fecparam *fec)
1553 {
1554 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1555 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1556 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1557 	u32 fec_mode;
1558 
1559 	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1560 		return -EOPNOTSUPP;
1561 
1562 	if (!ops->set_fec)
1563 		return -EOPNOTSUPP;
1564 	fec_mode = eth_to_loc_fec(fec->fec);
1565 
1566 	netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1567 
1568 	return ops->set_fec(handle, fec_mode);
1569 }
1570 
1571 static int hns3_get_module_info(struct net_device *netdev,
1572 				struct ethtool_modinfo *modinfo)
1573 {
1574 #define HNS3_SFF_8636_V1_3 0x03
1575 
1576 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1577 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1578 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1579 	struct hns3_sfp_type sfp_type;
1580 	int ret;
1581 
1582 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1583 	    !ops->get_module_eeprom)
1584 		return -EOPNOTSUPP;
1585 
1586 	memset(&sfp_type, 0, sizeof(sfp_type));
1587 	ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1588 				     (u8 *)&sfp_type);
1589 	if (ret)
1590 		return ret;
1591 
1592 	switch (sfp_type.type) {
1593 	case SFF8024_ID_SFP:
1594 		modinfo->type = ETH_MODULE_SFF_8472;
1595 		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1596 		break;
1597 	case SFF8024_ID_QSFP_8438:
1598 		modinfo->type = ETH_MODULE_SFF_8436;
1599 		modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1600 		break;
1601 	case SFF8024_ID_QSFP_8436_8636:
1602 		if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1603 			modinfo->type = ETH_MODULE_SFF_8436;
1604 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1605 		} else {
1606 			modinfo->type = ETH_MODULE_SFF_8636;
1607 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1608 		}
1609 		break;
1610 	case SFF8024_ID_QSFP28_8636:
1611 		modinfo->type = ETH_MODULE_SFF_8636;
1612 		modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1613 		break;
1614 	default:
1615 		netdev_err(netdev, "Optical module unknown: %#x\n",
1616 			   sfp_type.type);
1617 		return -EINVAL;
1618 	}
1619 
1620 	return 0;
1621 }
1622 
1623 static int hns3_get_module_eeprom(struct net_device *netdev,
1624 				  struct ethtool_eeprom *ee, u8 *data)
1625 {
1626 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1627 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1628 	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1629 
1630 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1631 	    !ops->get_module_eeprom)
1632 		return -EOPNOTSUPP;
1633 
1634 	if (!ee->len)
1635 		return -EINVAL;
1636 
1637 	memset(data, 0, ee->len);
1638 
1639 	return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1640 }
1641 
1642 static u32 hns3_get_priv_flags(struct net_device *netdev)
1643 {
1644 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1645 
1646 	return handle->priv_flags;
1647 }
1648 
1649 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1650 {
1651 	u32 i;
1652 
1653 	for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1654 		if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1655 			netdev_err(h->netdev, "%s is unsupported\n",
1656 				   hns3_priv_flags[i].name);
1657 			return -EOPNOTSUPP;
1658 		}
1659 
1660 	return 0;
1661 }
1662 
1663 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1664 {
1665 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1666 	u32 changed = pflags ^ handle->priv_flags;
1667 	int ret;
1668 	u32 i;
1669 
1670 	ret = hns3_check_priv_flags(handle, changed);
1671 	if (ret)
1672 		return ret;
1673 
1674 	for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1675 		if (changed & BIT(i)) {
1676 			bool enable = !(handle->priv_flags & BIT(i));
1677 
1678 			if (enable)
1679 				handle->priv_flags |= BIT(i);
1680 			else
1681 				handle->priv_flags &= ~BIT(i);
1682 			hns3_priv_flags[i].handler(netdev, enable);
1683 		}
1684 	}
1685 
1686 	return 0;
1687 }
1688 
1689 static int hns3_get_tunable(struct net_device *netdev,
1690 			    const struct ethtool_tunable *tuna,
1691 			    void *data)
1692 {
1693 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1694 	int ret = 0;
1695 
1696 	switch (tuna->id) {
1697 	case ETHTOOL_TX_COPYBREAK:
1698 		/* all the tx rings have the same tx_copybreak */
1699 		*(u32 *)data = priv->tx_copybreak;
1700 		break;
1701 	case ETHTOOL_RX_COPYBREAK:
1702 		*(u32 *)data = priv->rx_copybreak;
1703 		break;
1704 	default:
1705 		ret = -EOPNOTSUPP;
1706 		break;
1707 	}
1708 
1709 	return ret;
1710 }
1711 
1712 static int hns3_set_tunable(struct net_device *netdev,
1713 			    const struct ethtool_tunable *tuna,
1714 			    const void *data)
1715 {
1716 	struct hns3_nic_priv *priv = netdev_priv(netdev);
1717 	struct hnae3_handle *h = priv->ae_handle;
1718 	int i, ret = 0;
1719 
1720 	switch (tuna->id) {
1721 	case ETHTOOL_TX_COPYBREAK:
1722 		priv->tx_copybreak = *(u32 *)data;
1723 
1724 		for (i = 0; i < h->kinfo.num_tqps; i++)
1725 			priv->ring[i].tx_copybreak = priv->tx_copybreak;
1726 
1727 		break;
1728 	case ETHTOOL_RX_COPYBREAK:
1729 		priv->rx_copybreak = *(u32 *)data;
1730 
1731 		for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1732 			priv->ring[i].rx_copybreak = priv->rx_copybreak;
1733 
1734 		break;
1735 	default:
1736 		ret = -EOPNOTSUPP;
1737 		break;
1738 	}
1739 
1740 	return ret;
1741 }
1742 
1743 #define HNS3_ETHTOOL_COALESCE	(ETHTOOL_COALESCE_USECS |		\
1744 				 ETHTOOL_COALESCE_USE_ADAPTIVE |	\
1745 				 ETHTOOL_COALESCE_RX_USECS_HIGH |	\
1746 				 ETHTOOL_COALESCE_TX_USECS_HIGH |	\
1747 				 ETHTOOL_COALESCE_MAX_FRAMES |		\
1748 				 ETHTOOL_COALESCE_USE_CQE)
1749 
1750 static int hns3_get_ts_info(struct net_device *netdev,
1751 			    struct ethtool_ts_info *info)
1752 {
1753 	struct hnae3_handle *handle = hns3_get_handle(netdev);
1754 
1755 	if (handle->ae_algo->ops->get_ts_info)
1756 		return handle->ae_algo->ops->get_ts_info(handle, info);
1757 
1758 	return ethtool_op_get_ts_info(netdev, info);
1759 }
1760 
1761 static const struct hns3_ethtool_link_ext_state_mapping
1762 hns3_link_ext_state_map[] = {
1763 	{1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1764 		ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
1765 	{2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1766 		ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},
1767 
1768 	{256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1769 		ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
1770 	{257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1771 		ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
1772 	{512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1773 		ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},
1774 
1775 	{513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1776 		ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
1777 	{514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1778 		ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
1779 	{515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1780 		ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},
1781 
1782 	{768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1783 		ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
1784 	{769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1785 		ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
1786 	{770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1787 		ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},
1788 
1789 	{1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
1790 	{1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
1791 		ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},
1792 
1793 	{1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
1794 };
1795 
1796 static int hns3_get_link_ext_state(struct net_device *netdev,
1797 				   struct ethtool_link_ext_state_info *info)
1798 {
1799 	const struct hns3_ethtool_link_ext_state_mapping *map;
1800 	struct hnae3_handle *h = hns3_get_handle(netdev);
1801 	u32 status_code, i;
1802 	int ret;
1803 
1804 	if (netif_carrier_ok(netdev))
1805 		return -ENODATA;
1806 
1807 	if (!h->ae_algo->ops->get_link_diagnosis_info)
1808 		return -EOPNOTSUPP;
1809 
1810 	ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
1811 	if (ret)
1812 		return ret;
1813 
1814 	for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
1815 		map = &hns3_link_ext_state_map[i];
1816 		if (map->status_code == status_code) {
1817 			info->link_ext_state = map->link_ext_state;
1818 			info->__link_ext_substate = map->link_ext_substate;
1819 			return 0;
1820 		}
1821 	}
1822 
1823 	return -ENODATA;
1824 }
1825 
1826 static const struct ethtool_ops hns3vf_ethtool_ops = {
1827 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1828 	.get_drvinfo = hns3_get_drvinfo,
1829 	.get_ringparam = hns3_get_ringparam,
1830 	.set_ringparam = hns3_set_ringparam,
1831 	.get_strings = hns3_get_strings,
1832 	.get_ethtool_stats = hns3_get_stats,
1833 	.get_sset_count = hns3_get_sset_count,
1834 	.get_rxnfc = hns3_get_rxnfc,
1835 	.set_rxnfc = hns3_set_rxnfc,
1836 	.get_rxfh_key_size = hns3_get_rss_key_size,
1837 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1838 	.get_rxfh = hns3_get_rss,
1839 	.set_rxfh = hns3_set_rss,
1840 	.get_link_ksettings = hns3_get_link_ksettings,
1841 	.get_channels = hns3_get_channels,
1842 	.set_channels = hns3_set_channels,
1843 	.get_coalesce = hns3_get_coalesce,
1844 	.set_coalesce = hns3_set_coalesce,
1845 	.get_regs_len = hns3_get_regs_len,
1846 	.get_regs = hns3_get_regs,
1847 	.get_link = hns3_get_link,
1848 	.get_msglevel = hns3_get_msglevel,
1849 	.set_msglevel = hns3_set_msglevel,
1850 	.get_priv_flags = hns3_get_priv_flags,
1851 	.set_priv_flags = hns3_set_priv_flags,
1852 	.get_tunable = hns3_get_tunable,
1853 	.set_tunable = hns3_set_tunable,
1854 	.reset = hns3_set_reset,
1855 };
1856 
1857 static const struct ethtool_ops hns3_ethtool_ops = {
1858 	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1859 	.self_test = hns3_self_test,
1860 	.get_drvinfo = hns3_get_drvinfo,
1861 	.get_link = hns3_get_link,
1862 	.get_ringparam = hns3_get_ringparam,
1863 	.set_ringparam = hns3_set_ringparam,
1864 	.get_pauseparam = hns3_get_pauseparam,
1865 	.set_pauseparam = hns3_set_pauseparam,
1866 	.get_strings = hns3_get_strings,
1867 	.get_ethtool_stats = hns3_get_stats,
1868 	.get_sset_count = hns3_get_sset_count,
1869 	.get_rxnfc = hns3_get_rxnfc,
1870 	.set_rxnfc = hns3_set_rxnfc,
1871 	.get_rxfh_key_size = hns3_get_rss_key_size,
1872 	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1873 	.get_rxfh = hns3_get_rss,
1874 	.set_rxfh = hns3_set_rss,
1875 	.get_link_ksettings = hns3_get_link_ksettings,
1876 	.set_link_ksettings = hns3_set_link_ksettings,
1877 	.nway_reset = hns3_nway_reset,
1878 	.get_channels = hns3_get_channels,
1879 	.set_channels = hns3_set_channels,
1880 	.get_coalesce = hns3_get_coalesce,
1881 	.set_coalesce = hns3_set_coalesce,
1882 	.get_regs_len = hns3_get_regs_len,
1883 	.get_regs = hns3_get_regs,
1884 	.set_phys_id = hns3_set_phys_id,
1885 	.get_msglevel = hns3_get_msglevel,
1886 	.set_msglevel = hns3_set_msglevel,
1887 	.get_fecparam = hns3_get_fecparam,
1888 	.set_fecparam = hns3_set_fecparam,
1889 	.get_module_info = hns3_get_module_info,
1890 	.get_module_eeprom = hns3_get_module_eeprom,
1891 	.get_priv_flags = hns3_get_priv_flags,
1892 	.set_priv_flags = hns3_set_priv_flags,
1893 	.get_ts_info = hns3_get_ts_info,
1894 	.get_tunable = hns3_get_tunable,
1895 	.set_tunable = hns3_set_tunable,
1896 	.reset = hns3_set_reset,
1897 	.get_link_ext_state = hns3_get_link_ext_state,
1898 };
1899 
1900 void hns3_ethtool_set_ops(struct net_device *netdev)
1901 {
1902 	struct hnae3_handle *h = hns3_get_handle(netdev);
1903 
1904 	if (h->flags & HNAE3_SUPPORT_VF)
1905 		netdev->ethtool_ops = &hns3vf_ethtool_ops;
1906 	else
1907 		netdev->ethtool_ops = &hns3_ethtool_ops;
1908 }
1909