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