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