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