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