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