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