• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2014-2015 Hisilicon Limited.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 
10 #include <linux/etherdevice.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include "hns_enet.h"
15 
16 #define HNS_PHY_PAGE_MDIX	0
17 #define HNS_PHY_PAGE_LED	3
18 #define HNS_PHY_PAGE_COPPER	0
19 
20 #define HNS_PHY_PAGE_REG	22	/* Page Selection Reg. */
21 #define HNS_PHY_CSC_REG		16	/* Copper Specific Control Register */
22 #define HNS_PHY_CSS_REG		17	/* Copper Specific Status Register */
23 #define HNS_LED_FC_REG		16	/* LED Function Control Reg. */
24 #define HNS_LED_PC_REG		17	/* LED Polarity Control Reg. */
25 
26 #define HNS_LED_FORCE_ON	9
27 #define HNS_LED_FORCE_OFF	8
28 
29 #define HNS_CHIP_VERSION 660
30 #define HNS_NET_STATS_CNT 26
31 
32 #define PHY_MDIX_CTRL_S		(5)
33 #define PHY_MDIX_CTRL_M		(3 << PHY_MDIX_CTRL_S)
34 
35 #define PHY_MDIX_STATUS_B	(6)
36 #define PHY_SPEED_DUP_RESOLVE_B	(11)
37 
38 /**
39  *hns_nic_get_link - get current link status
40  *@net_dev: net_device
41  *retuen 0 - success , negative --fail
42  */
hns_nic_get_link(struct net_device * net_dev)43 static u32 hns_nic_get_link(struct net_device *net_dev)
44 {
45 	struct hns_nic_priv *priv = netdev_priv(net_dev);
46 	u32 link_stat = priv->link;
47 	struct hnae_handle *h;
48 
49 	h = priv->ae_handle;
50 
51 	if (net_dev->phydev) {
52 		if (!genphy_read_status(net_dev->phydev))
53 			link_stat = net_dev->phydev->link;
54 		else
55 			link_stat = 0;
56 	}
57 
58 	if (h->dev && h->dev->ops && h->dev->ops->get_status)
59 		link_stat = link_stat && h->dev->ops->get_status(h);
60 	else
61 		link_stat = 0;
62 
63 	return link_stat;
64 }
65 
hns_get_mdix_mode(struct net_device * net_dev,struct ethtool_link_ksettings * cmd)66 static void hns_get_mdix_mode(struct net_device *net_dev,
67 			      struct ethtool_link_ksettings *cmd)
68 {
69 	int mdix_ctrl, mdix, retval, is_resolved;
70 	struct phy_device *phy_dev = net_dev->phydev;
71 
72 	if (!phy_dev || !phy_dev->mdio.bus) {
73 		cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
74 		cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
75 		return;
76 	}
77 
78 	phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_MDIX);
79 
80 	retval = phy_read(phy_dev, HNS_PHY_CSC_REG);
81 	mdix_ctrl = hnae_get_field(retval, PHY_MDIX_CTRL_M, PHY_MDIX_CTRL_S);
82 
83 	retval = phy_read(phy_dev, HNS_PHY_CSS_REG);
84 	mdix = hnae_get_bit(retval, PHY_MDIX_STATUS_B);
85 	is_resolved = hnae_get_bit(retval, PHY_SPEED_DUP_RESOLVE_B);
86 
87 	phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
88 
89 	switch (mdix_ctrl) {
90 	case 0x0:
91 		cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI;
92 		break;
93 	case 0x1:
94 		cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_X;
95 		break;
96 	case 0x3:
97 		cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_AUTO;
98 		break;
99 	default:
100 		cmd->base.eth_tp_mdix_ctrl = ETH_TP_MDI_INVALID;
101 		break;
102 	}
103 
104 	if (!is_resolved)
105 		cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
106 	else if (mdix)
107 		cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
108 	else
109 		cmd->base.eth_tp_mdix = ETH_TP_MDI;
110 }
111 
112 /**
113  *hns_nic_get_link_ksettings - implement ethtool get link ksettings
114  *@net_dev: net_device
115  *@cmd: ethtool_link_ksettings
116  *retuen 0 - success , negative --fail
117  */
hns_nic_get_link_ksettings(struct net_device * net_dev,struct ethtool_link_ksettings * cmd)118 static int hns_nic_get_link_ksettings(struct net_device *net_dev,
119 				      struct ethtool_link_ksettings *cmd)
120 {
121 	struct hns_nic_priv *priv = netdev_priv(net_dev);
122 	struct hnae_handle *h;
123 	u32 link_stat;
124 	int ret;
125 	u8 duplex;
126 	u16 speed;
127 	u32 supported, advertising;
128 
129 	if (!priv || !priv->ae_handle)
130 		return -ESRCH;
131 
132 	h = priv->ae_handle;
133 	if (!h->dev || !h->dev->ops || !h->dev->ops->get_info)
134 		return -ESRCH;
135 
136 	ret = h->dev->ops->get_info(h, NULL, &speed, &duplex);
137 	if (ret < 0) {
138 		netdev_err(net_dev, "%s get_info error!\n", __func__);
139 		return -EINVAL;
140 	}
141 
142 	ethtool_convert_link_mode_to_legacy_u32(&supported,
143 						cmd->link_modes.supported);
144 	ethtool_convert_link_mode_to_legacy_u32(&advertising,
145 						cmd->link_modes.advertising);
146 
147 	/* When there is no phy, autoneg is off. */
148 	cmd->base.autoneg = false;
149 	cmd->base.speed = speed;
150 	cmd->base.duplex = duplex;
151 
152 	if (net_dev->phydev)
153 		phy_ethtool_ksettings_get(net_dev->phydev, cmd);
154 
155 	link_stat = hns_nic_get_link(net_dev);
156 	if (!link_stat) {
157 		cmd->base.speed = (u32)SPEED_UNKNOWN;
158 		cmd->base.duplex = DUPLEX_UNKNOWN;
159 	}
160 
161 	if (cmd->base.autoneg)
162 		advertising |= ADVERTISED_Autoneg;
163 
164 	supported |= h->if_support;
165 	if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
166 		supported |= SUPPORTED_TP;
167 		advertising |= ADVERTISED_1000baseT_Full;
168 	} else if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
169 		supported |= SUPPORTED_FIBRE;
170 		advertising |= ADVERTISED_10000baseKR_Full;
171 	}
172 
173 	switch (h->media_type) {
174 	case HNAE_MEDIA_TYPE_FIBER:
175 		cmd->base.port = PORT_FIBRE;
176 		break;
177 	case HNAE_MEDIA_TYPE_COPPER:
178 		cmd->base.port = PORT_TP;
179 		break;
180 	case HNAE_MEDIA_TYPE_UNKNOWN:
181 	default:
182 		break;
183 	}
184 
185 	if (!(AE_IS_VER1(priv->enet_ver) && h->port_type == HNAE_PORT_DEBUG))
186 		supported |= SUPPORTED_Pause;
187 
188 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
189 						supported);
190 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
191 						advertising);
192 
193 	cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C45 | ETH_MDIO_SUPPORTS_C22;
194 	hns_get_mdix_mode(net_dev, cmd);
195 
196 	return 0;
197 }
198 
199 /**
200  *hns_nic_set_link_settings - implement ethtool set link ksettings
201  *@net_dev: net_device
202  *@cmd: ethtool_link_ksettings
203  *retuen 0 - success , negative --fail
204  */
hns_nic_set_link_ksettings(struct net_device * net_dev,const struct ethtool_link_ksettings * cmd)205 static int hns_nic_set_link_ksettings(struct net_device *net_dev,
206 				      const struct ethtool_link_ksettings *cmd)
207 {
208 	struct hns_nic_priv *priv = netdev_priv(net_dev);
209 	struct hnae_handle *h;
210 	u32 speed;
211 
212 	if (!netif_running(net_dev))
213 		return -ESRCH;
214 
215 	if (!priv || !priv->ae_handle || !priv->ae_handle->dev ||
216 	    !priv->ae_handle->dev->ops)
217 		return -ENODEV;
218 
219 	h = priv->ae_handle;
220 	speed = cmd->base.speed;
221 
222 	if (h->phy_if == PHY_INTERFACE_MODE_XGMII) {
223 		if (cmd->base.autoneg == AUTONEG_ENABLE ||
224 		    speed != SPEED_10000 ||
225 		    cmd->base.duplex != DUPLEX_FULL)
226 			return -EINVAL;
227 	} else if (h->phy_if == PHY_INTERFACE_MODE_SGMII) {
228 		if (!net_dev->phydev && cmd->base.autoneg == AUTONEG_ENABLE)
229 			return -EINVAL;
230 
231 		if (speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
232 			return -EINVAL;
233 		if (net_dev->phydev)
234 			return phy_ethtool_ksettings_set(net_dev->phydev, cmd);
235 
236 		if ((speed != SPEED_10 && speed != SPEED_100 &&
237 		     speed != SPEED_1000) || (cmd->base.duplex != DUPLEX_HALF &&
238 		     cmd->base.duplex != DUPLEX_FULL))
239 			return -EINVAL;
240 	} else {
241 		netdev_err(net_dev, "Not supported!");
242 		return -ENOTSUPP;
243 	}
244 
245 	if (h->dev->ops->adjust_link) {
246 		netif_carrier_off(net_dev);
247 		h->dev->ops->adjust_link(h, (int)speed, cmd->base.duplex);
248 		netif_carrier_on(net_dev);
249 		return 0;
250 	}
251 
252 	netdev_err(net_dev, "Not supported!");
253 	return -ENOTSUPP;
254 }
255 
256 static const char hns_nic_test_strs[][ETH_GSTRING_LEN] = {
257 	"Mac    Loopback test",
258 	"Serdes Loopback test",
259 	"Phy    Loopback test"
260 };
261 
hns_nic_config_phy_loopback(struct phy_device * phy_dev,u8 en)262 static int hns_nic_config_phy_loopback(struct phy_device *phy_dev, u8 en)
263 {
264 	int err;
265 
266 	if (en) {
267 		/* Doing phy loopback in offline state, phy resuming is
268 		 * needed to power up the device.
269 		 */
270 		err = phy_resume(phy_dev);
271 		if (err)
272 			goto out;
273 
274 		err = phy_loopback(phy_dev, true);
275 	} else {
276 		err = phy_loopback(phy_dev, false);
277 		if (err)
278 			goto out;
279 
280 		err = phy_suspend(phy_dev);
281 	}
282 
283 out:
284 	return err;
285 }
286 
__lb_setup(struct net_device * ndev,enum hnae_loop loop)287 static int __lb_setup(struct net_device *ndev,
288 		      enum hnae_loop loop)
289 {
290 	int ret = 0;
291 	struct hns_nic_priv *priv = netdev_priv(ndev);
292 	struct phy_device *phy_dev = ndev->phydev;
293 	struct hnae_handle *h = priv->ae_handle;
294 
295 	switch (loop) {
296 	case MAC_INTERNALLOOP_PHY:
297 		ret = hns_nic_config_phy_loopback(phy_dev, 0x1);
298 		if (!ret)
299 			ret = h->dev->ops->set_loopback(h, loop, 0x1);
300 		break;
301 	case MAC_INTERNALLOOP_MAC:
302 		if ((h->dev->ops->set_loopback) &&
303 		    (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII))
304 			ret = h->dev->ops->set_loopback(h, loop, 0x1);
305 		break;
306 	case MAC_INTERNALLOOP_SERDES:
307 		if (h->dev->ops->set_loopback)
308 			ret = h->dev->ops->set_loopback(h, loop, 0x1);
309 		break;
310 	case MAC_LOOP_PHY_NONE:
311 		ret = hns_nic_config_phy_loopback(phy_dev, 0x0);
312 	case MAC_LOOP_NONE:
313 		if (!ret && h->dev->ops->set_loopback) {
314 			if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
315 				ret = h->dev->ops->set_loopback(h,
316 					MAC_INTERNALLOOP_MAC, 0x0);
317 
318 			if (!ret)
319 				ret = h->dev->ops->set_loopback(h,
320 					MAC_INTERNALLOOP_SERDES, 0x0);
321 		}
322 		break;
323 	default:
324 		ret = -EINVAL;
325 		break;
326 	}
327 
328 	if (!ret) {
329 		if (loop == MAC_LOOP_NONE)
330 			h->dev->ops->set_promisc_mode(
331 				h, ndev->flags & IFF_PROMISC);
332 		else
333 			h->dev->ops->set_promisc_mode(h, 1);
334 	}
335 	return ret;
336 }
337 
__lb_up(struct net_device * ndev,enum hnae_loop loop_mode)338 static int __lb_up(struct net_device *ndev,
339 		   enum hnae_loop loop_mode)
340 {
341 #define NIC_LB_TEST_WAIT_PHY_LINK_TIME 300
342 	struct hns_nic_priv *priv = netdev_priv(ndev);
343 	struct hnae_handle *h = priv->ae_handle;
344 	int speed, duplex;
345 	int ret;
346 
347 	hns_nic_net_reset(ndev);
348 
349 	ret = __lb_setup(ndev, loop_mode);
350 	if (ret)
351 		return ret;
352 
353 	msleep(200);
354 
355 	ret = h->dev->ops->start ? h->dev->ops->start(h) : 0;
356 	if (ret)
357 		return ret;
358 
359 	/* link adjust duplex*/
360 	if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII)
361 		speed = 1000;
362 	else
363 		speed = 10000;
364 	duplex = 1;
365 
366 	h->dev->ops->adjust_link(h, speed, duplex);
367 
368 	/* wait adjust link done and phy ready */
369 	msleep(NIC_LB_TEST_WAIT_PHY_LINK_TIME);
370 
371 	return 0;
372 }
373 
__lb_other_process(struct hns_nic_ring_data * ring_data,struct sk_buff * skb)374 static void __lb_other_process(struct hns_nic_ring_data *ring_data,
375 			       struct sk_buff *skb)
376 {
377 	struct net_device *ndev;
378 	struct hns_nic_priv *priv;
379 	struct hnae_ring *ring;
380 	struct netdev_queue *dev_queue;
381 	struct sk_buff *new_skb;
382 	unsigned int frame_size;
383 	int check_ok;
384 	u32 i;
385 	char buff[33]; /* 32B data and the last character '\0' */
386 
387 	if (!ring_data) { /* Just for doing create frame*/
388 		ndev = skb->dev;
389 		priv = netdev_priv(ndev);
390 
391 		frame_size = skb->len;
392 		memset(skb->data, 0xFF, frame_size);
393 		if ((!AE_IS_VER1(priv->enet_ver)) &&
394 		    (priv->ae_handle->port_type == HNAE_PORT_SERVICE)) {
395 			memcpy(skb->data, ndev->dev_addr, 6);
396 			skb->data[5] += 0x1f;
397 		}
398 
399 		frame_size &= ~1ul;
400 		memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
401 		memset(&skb->data[frame_size / 2 + 10], 0xBE,
402 		       frame_size / 2 - 11);
403 		memset(&skb->data[frame_size / 2 + 12], 0xAF,
404 		       frame_size / 2 - 13);
405 		return;
406 	}
407 
408 	ring = ring_data->ring;
409 	ndev = ring_data->napi.dev;
410 	if (is_tx_ring(ring)) { /* for tx queue reset*/
411 		dev_queue = netdev_get_tx_queue(ndev, ring_data->queue_index);
412 		netdev_tx_reset_queue(dev_queue);
413 		return;
414 	}
415 
416 	frame_size = skb->len;
417 	frame_size &= ~1ul;
418 	/* for mutl buffer*/
419 	new_skb = skb_copy(skb, GFP_ATOMIC);
420 	dev_kfree_skb_any(skb);
421 	skb = new_skb;
422 
423 	check_ok = 0;
424 	if (*(skb->data + 10) == 0xFF) { /* for rx check frame*/
425 		if ((*(skb->data + frame_size / 2 + 10) == 0xBE) &&
426 		    (*(skb->data + frame_size / 2 + 12) == 0xAF))
427 			check_ok = 1;
428 	}
429 
430 	if (check_ok) {
431 		ndev->stats.rx_packets++;
432 		ndev->stats.rx_bytes += skb->len;
433 	} else {
434 		ndev->stats.rx_frame_errors++;
435 		for (i = 0; i < skb->len; i++) {
436 			snprintf(buff + i % 16 * 2, 3, /* tailing \0*/
437 				 "%02x", *(skb->data + i));
438 			if ((i % 16 == 15) || (i == skb->len - 1))
439 				pr_info("%s\n", buff);
440 		}
441 	}
442 	dev_kfree_skb_any(skb);
443 }
444 
__lb_clean_rings(struct hns_nic_priv * priv,int ringid0,int ringid1,int budget)445 static int __lb_clean_rings(struct hns_nic_priv *priv,
446 			    int ringid0, int ringid1, int budget)
447 {
448 	int i, ret;
449 	struct hns_nic_ring_data *ring_data;
450 	struct net_device *ndev = priv->netdev;
451 	unsigned long rx_packets = ndev->stats.rx_packets;
452 	unsigned long rx_bytes = ndev->stats.rx_bytes;
453 	unsigned long rx_frame_errors = ndev->stats.rx_frame_errors;
454 
455 	for (i = ringid0; i <= ringid1; i++) {
456 		ring_data = &priv->ring_data[i];
457 		(void)ring_data->poll_one(ring_data,
458 					  budget, __lb_other_process);
459 	}
460 	ret = (int)(ndev->stats.rx_packets - rx_packets);
461 	ndev->stats.rx_packets = rx_packets;
462 	ndev->stats.rx_bytes = rx_bytes;
463 	ndev->stats.rx_frame_errors = rx_frame_errors;
464 	return ret;
465 }
466 
467 /**
468  * nic_run_loopback_test -  run loopback test
469  * @nic_dev: net device
470  * @loopback_type: loopback type
471  */
__lb_run_test(struct net_device * ndev,enum hnae_loop loop_mode)472 static int __lb_run_test(struct net_device *ndev,
473 			 enum hnae_loop loop_mode)
474 {
475 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
476 #define NIC_LB_TEST_RING_ID 0
477 #define NIC_LB_TEST_FRAME_SIZE 128
478 /* nic loopback test err  */
479 #define NIC_LB_TEST_NO_MEM_ERR 1
480 #define NIC_LB_TEST_TX_CNT_ERR 2
481 #define NIC_LB_TEST_RX_CNT_ERR 3
482 #define NIC_LB_TEST_RX_PKG_ERR 4
483 	struct hns_nic_priv *priv = netdev_priv(ndev);
484 	struct hnae_handle *h = priv->ae_handle;
485 	int i, j, lc, good_cnt, ret_val = 0;
486 	unsigned int size;
487 	netdev_tx_t tx_ret_val;
488 	struct sk_buff *skb;
489 
490 	size = NIC_LB_TEST_FRAME_SIZE;
491 	/* allocate test skb */
492 	skb = alloc_skb(size, GFP_KERNEL);
493 	if (!skb)
494 		return NIC_LB_TEST_NO_MEM_ERR;
495 
496 	/* place data into test skb */
497 	(void)skb_put(skb, size);
498 	skb->dev = ndev;
499 	__lb_other_process(NULL, skb);
500 	skb->queue_mapping = NIC_LB_TEST_RING_ID;
501 
502 	lc = 1;
503 	for (j = 0; j < lc; j++) {
504 		/* reset count of good packets */
505 		good_cnt = 0;
506 		/* place 64 packets on the transmit queue*/
507 		for (i = 0; i < NIC_LB_TEST_PKT_NUM_PER_CYCLE; i++) {
508 			(void)skb_get(skb);
509 
510 			tx_ret_val = (netdev_tx_t)hns_nic_net_xmit_hw(
511 				ndev, skb,
512 				&tx_ring_data(priv, skb->queue_mapping));
513 			if (tx_ret_val == NETDEV_TX_OK)
514 				good_cnt++;
515 			else
516 				break;
517 		}
518 		if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
519 			ret_val = NIC_LB_TEST_TX_CNT_ERR;
520 			dev_err(priv->dev, "%s sent fail, cnt=0x%x, budget=0x%x\n",
521 				hns_nic_test_strs[loop_mode], good_cnt,
522 				NIC_LB_TEST_PKT_NUM_PER_CYCLE);
523 			break;
524 		}
525 
526 		/* allow 100 milliseconds for packets to go from Tx to Rx */
527 		msleep(100);
528 
529 		good_cnt = __lb_clean_rings(priv,
530 					    h->q_num, h->q_num * 2 - 1,
531 					    NIC_LB_TEST_PKT_NUM_PER_CYCLE);
532 		if (good_cnt != NIC_LB_TEST_PKT_NUM_PER_CYCLE) {
533 			ret_val = NIC_LB_TEST_RX_CNT_ERR;
534 			dev_err(priv->dev, "%s recv fail, cnt=0x%x, budget=0x%x\n",
535 				hns_nic_test_strs[loop_mode], good_cnt,
536 				NIC_LB_TEST_PKT_NUM_PER_CYCLE);
537 			break;
538 		}
539 		(void)__lb_clean_rings(priv,
540 				       NIC_LB_TEST_RING_ID, NIC_LB_TEST_RING_ID,
541 				       NIC_LB_TEST_PKT_NUM_PER_CYCLE);
542 	}
543 
544 	/* free the original skb */
545 	kfree_skb(skb);
546 
547 	return ret_val;
548 }
549 
__lb_down(struct net_device * ndev,enum hnae_loop loop)550 static int __lb_down(struct net_device *ndev, enum hnae_loop loop)
551 {
552 	struct hns_nic_priv *priv = netdev_priv(ndev);
553 	struct hnae_handle *h = priv->ae_handle;
554 	int ret;
555 
556 	if (loop == MAC_INTERNALLOOP_PHY)
557 		ret = __lb_setup(ndev, MAC_LOOP_PHY_NONE);
558 	else
559 		ret = __lb_setup(ndev, MAC_LOOP_NONE);
560 	if (ret)
561 		netdev_err(ndev, "%s: __lb_setup return error(%d)!\n",
562 			   __func__,
563 			   ret);
564 
565 	if (h->dev->ops->stop)
566 		h->dev->ops->stop(h);
567 
568 	usleep_range(10000, 20000);
569 	(void)__lb_clean_rings(priv, 0, h->q_num - 1, 256);
570 
571 	hns_nic_net_reset(ndev);
572 
573 	return 0;
574 }
575 
576 /**
577  * hns_nic_self_test - self test
578  * @dev: net device
579  * @eth_test: test cmd
580  * @data: test result
581  */
hns_nic_self_test(struct net_device * ndev,struct ethtool_test * eth_test,u64 * data)582 static void hns_nic_self_test(struct net_device *ndev,
583 			      struct ethtool_test *eth_test, u64 *data)
584 {
585 	struct hns_nic_priv *priv = netdev_priv(ndev);
586 	bool if_running = netif_running(ndev);
587 #define SELF_TEST_TPYE_NUM 3
588 	int st_param[SELF_TEST_TPYE_NUM][2];
589 	int i;
590 	int test_index = 0;
591 
592 	st_param[0][0] = MAC_INTERNALLOOP_MAC; /* XGE not supported lb */
593 	st_param[0][1] = (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII);
594 	st_param[1][0] = MAC_INTERNALLOOP_SERDES;
595 	st_param[1][1] = 1; /*serdes must exist*/
596 	st_param[2][0] = MAC_INTERNALLOOP_PHY; /* only supporte phy node*/
597 	st_param[2][1] = ((!!(priv->ae_handle->phy_dev)) &&
598 		(priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII));
599 
600 	if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
601 		set_bit(NIC_STATE_TESTING, &priv->state);
602 
603 		if (if_running)
604 			dev_close(ndev);
605 
606 		for (i = 0; i < SELF_TEST_TPYE_NUM; i++) {
607 			if (!st_param[i][1])
608 				continue;	/* NEXT testing */
609 
610 			data[test_index] = __lb_up(ndev,
611 				(enum hnae_loop)st_param[i][0]);
612 			if (!data[test_index]) {
613 				data[test_index] = __lb_run_test(
614 					ndev, (enum hnae_loop)st_param[i][0]);
615 				(void)__lb_down(ndev,
616 						(enum hnae_loop)st_param[i][0]);
617 			}
618 
619 			if (data[test_index])
620 				eth_test->flags |= ETH_TEST_FL_FAILED;
621 
622 			test_index++;
623 		}
624 
625 		hns_nic_net_reset(priv->netdev);
626 
627 		clear_bit(NIC_STATE_TESTING, &priv->state);
628 
629 		if (if_running)
630 			(void)dev_open(ndev);
631 	}
632 	/* Online tests aren't run; pass by default */
633 
634 	(void)msleep_interruptible(4 * 1000);
635 }
636 
637 /**
638  * hns_nic_get_drvinfo - get net driver info
639  * @dev: net device
640  * @drvinfo: driver info
641  */
hns_nic_get_drvinfo(struct net_device * net_dev,struct ethtool_drvinfo * drvinfo)642 static void hns_nic_get_drvinfo(struct net_device *net_dev,
643 				struct ethtool_drvinfo *drvinfo)
644 {
645 	struct hns_nic_priv *priv = netdev_priv(net_dev);
646 
647 	strncpy(drvinfo->version, HNAE_DRIVER_VERSION,
648 		sizeof(drvinfo->version));
649 	drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
650 
651 	strncpy(drvinfo->driver, HNAE_DRIVER_NAME, sizeof(drvinfo->driver));
652 	drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
653 
654 	strncpy(drvinfo->bus_info, priv->dev->bus->name,
655 		sizeof(drvinfo->bus_info));
656 	drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
657 
658 	strncpy(drvinfo->fw_version, "N/A", ETHTOOL_FWVERS_LEN);
659 	drvinfo->eedump_len = 0;
660 }
661 
662 /**
663  * hns_get_ringparam - get ring parameter
664  * @dev: net device
665  * @param: ethtool parameter
666  */
hns_get_ringparam(struct net_device * net_dev,struct ethtool_ringparam * param)667 void hns_get_ringparam(struct net_device *net_dev,
668 		       struct ethtool_ringparam *param)
669 {
670 	struct hns_nic_priv *priv = netdev_priv(net_dev);
671 	struct hnae_ae_ops *ops;
672 	struct hnae_queue *queue;
673 	u32 uplimit = 0;
674 
675 	queue = priv->ae_handle->qs[0];
676 	ops = priv->ae_handle->dev->ops;
677 
678 	if (ops->get_ring_bdnum_limit)
679 		ops->get_ring_bdnum_limit(queue, &uplimit);
680 
681 	param->rx_max_pending = uplimit;
682 	param->tx_max_pending = uplimit;
683 	param->rx_pending = queue->rx_ring.desc_num;
684 	param->tx_pending = queue->tx_ring.desc_num;
685 }
686 
687 /**
688  * hns_get_pauseparam - get pause parameter
689  * @dev: net device
690  * @param: pause parameter
691  */
hns_get_pauseparam(struct net_device * net_dev,struct ethtool_pauseparam * param)692 static void hns_get_pauseparam(struct net_device *net_dev,
693 			       struct ethtool_pauseparam *param)
694 {
695 	struct hns_nic_priv *priv = netdev_priv(net_dev);
696 	struct hnae_ae_ops *ops;
697 
698 	ops = priv->ae_handle->dev->ops;
699 
700 	if (ops->get_pauseparam)
701 		ops->get_pauseparam(priv->ae_handle, &param->autoneg,
702 					    &param->rx_pause, &param->tx_pause);
703 }
704 
705 /**
706  * hns_set_pauseparam - set pause parameter
707  * @dev: net device
708  * @param: pause parameter
709  *
710  * Return 0 on success, negative on failure
711  */
hns_set_pauseparam(struct net_device * net_dev,struct ethtool_pauseparam * param)712 static int hns_set_pauseparam(struct net_device *net_dev,
713 			      struct ethtool_pauseparam *param)
714 {
715 	struct hns_nic_priv *priv = netdev_priv(net_dev);
716 	struct hnae_handle *h;
717 	struct hnae_ae_ops *ops;
718 
719 	h = priv->ae_handle;
720 	ops = h->dev->ops;
721 
722 	if (!ops->set_pauseparam)
723 		return -ESRCH;
724 
725 	return ops->set_pauseparam(priv->ae_handle, param->autoneg,
726 				   param->rx_pause, param->tx_pause);
727 }
728 
729 /**
730  * hns_get_coalesce - get coalesce info.
731  * @dev: net device
732  * @ec: coalesce info.
733  *
734  * Return 0 on success, negative on failure.
735  */
hns_get_coalesce(struct net_device * net_dev,struct ethtool_coalesce * ec)736 static int hns_get_coalesce(struct net_device *net_dev,
737 			    struct ethtool_coalesce *ec)
738 {
739 	struct hns_nic_priv *priv = netdev_priv(net_dev);
740 	struct hnae_ae_ops *ops;
741 
742 	ops = priv->ae_handle->dev->ops;
743 
744 	ec->use_adaptive_rx_coalesce = priv->ae_handle->coal_adapt_en;
745 	ec->use_adaptive_tx_coalesce = priv->ae_handle->coal_adapt_en;
746 
747 	if ((!ops->get_coalesce_usecs) ||
748 	    (!ops->get_max_coalesced_frames))
749 		return -ESRCH;
750 
751 	ops->get_coalesce_usecs(priv->ae_handle,
752 					&ec->tx_coalesce_usecs,
753 					&ec->rx_coalesce_usecs);
754 
755 	ops->get_max_coalesced_frames(
756 		priv->ae_handle,
757 		&ec->tx_max_coalesced_frames,
758 		&ec->rx_max_coalesced_frames);
759 
760 	ops->get_coalesce_range(priv->ae_handle,
761 				&ec->tx_max_coalesced_frames_low,
762 				&ec->rx_max_coalesced_frames_low,
763 				&ec->tx_max_coalesced_frames_high,
764 				&ec->rx_max_coalesced_frames_high,
765 				&ec->tx_coalesce_usecs_low,
766 				&ec->rx_coalesce_usecs_low,
767 				&ec->tx_coalesce_usecs_high,
768 				&ec->rx_coalesce_usecs_high);
769 
770 	return 0;
771 }
772 
773 /**
774  * hns_set_coalesce - set coalesce info.
775  * @dev: net device
776  * @ec: coalesce info.
777  *
778  * Return 0 on success, negative on failure.
779  */
hns_set_coalesce(struct net_device * net_dev,struct ethtool_coalesce * ec)780 static int hns_set_coalesce(struct net_device *net_dev,
781 			    struct ethtool_coalesce *ec)
782 {
783 	struct hns_nic_priv *priv = netdev_priv(net_dev);
784 	struct hnae_ae_ops *ops;
785 	int rc1, rc2;
786 
787 	ops = priv->ae_handle->dev->ops;
788 
789 	if (ec->tx_coalesce_usecs != ec->rx_coalesce_usecs)
790 		return -EINVAL;
791 
792 	if ((!ops->set_coalesce_usecs) ||
793 	    (!ops->set_coalesce_frames))
794 		return -ESRCH;
795 
796 	if (ec->use_adaptive_rx_coalesce != priv->ae_handle->coal_adapt_en)
797 		priv->ae_handle->coal_adapt_en = ec->use_adaptive_rx_coalesce;
798 
799 	rc1 = ops->set_coalesce_usecs(priv->ae_handle,
800 				      ec->rx_coalesce_usecs);
801 
802 	rc2 = ops->set_coalesce_frames(priv->ae_handle,
803 				       ec->tx_max_coalesced_frames,
804 				       ec->rx_max_coalesced_frames);
805 
806 	if (rc1 || rc2)
807 		return -EINVAL;
808 
809 	return 0;
810 }
811 
812 /**
813  * hns_get_channels - get channel info.
814  * @dev: net device
815  * @ch: channel info.
816  */
hns_get_channels(struct net_device * net_dev,struct ethtool_channels * ch)817 void hns_get_channels(struct net_device *net_dev, struct ethtool_channels *ch)
818 {
819 	struct hns_nic_priv *priv = netdev_priv(net_dev);
820 
821 	ch->max_rx = priv->ae_handle->q_num;
822 	ch->max_tx = priv->ae_handle->q_num;
823 
824 	ch->rx_count = priv->ae_handle->q_num;
825 	ch->tx_count = priv->ae_handle->q_num;
826 }
827 
828 /**
829  * get_ethtool_stats - get detail statistics.
830  * @dev: net device
831  * @stats: statistics info.
832  * @data: statistics data.
833  */
hns_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)834 void hns_get_ethtool_stats(struct net_device *netdev,
835 			   struct ethtool_stats *stats, u64 *data)
836 {
837 	u64 *p = data;
838 	struct hns_nic_priv *priv = netdev_priv(netdev);
839 	struct hnae_handle *h = priv->ae_handle;
840 	const struct rtnl_link_stats64 *net_stats;
841 	struct rtnl_link_stats64 temp;
842 
843 	if (!h->dev->ops->get_stats || !h->dev->ops->update_stats) {
844 		netdev_err(netdev, "get_stats or update_stats is null!\n");
845 		return;
846 	}
847 
848 	h->dev->ops->update_stats(h, &netdev->stats);
849 
850 	net_stats = dev_get_stats(netdev, &temp);
851 
852 	/* get netdev statistics */
853 	p[0] = net_stats->rx_packets;
854 	p[1] = net_stats->tx_packets;
855 	p[2] = net_stats->rx_bytes;
856 	p[3] = net_stats->tx_bytes;
857 	p[4] = net_stats->rx_errors;
858 	p[5] = net_stats->tx_errors;
859 	p[6] = net_stats->rx_dropped;
860 	p[7] = net_stats->tx_dropped;
861 	p[8] = net_stats->multicast;
862 	p[9] = net_stats->collisions;
863 	p[10] = net_stats->rx_over_errors;
864 	p[11] = net_stats->rx_crc_errors;
865 	p[12] = net_stats->rx_frame_errors;
866 	p[13] = net_stats->rx_fifo_errors;
867 	p[14] = net_stats->rx_missed_errors;
868 	p[15] = net_stats->tx_aborted_errors;
869 	p[16] = net_stats->tx_carrier_errors;
870 	p[17] = net_stats->tx_fifo_errors;
871 	p[18] = net_stats->tx_heartbeat_errors;
872 	p[19] = net_stats->rx_length_errors;
873 	p[20] = net_stats->tx_window_errors;
874 	p[21] = net_stats->rx_compressed;
875 	p[22] = net_stats->tx_compressed;
876 
877 	p[23] = netdev->rx_dropped.counter;
878 	p[24] = netdev->tx_dropped.counter;
879 
880 	p[25] = priv->tx_timeout_count;
881 
882 	/* get driver statistics */
883 	h->dev->ops->get_stats(h, &p[26]);
884 }
885 
886 /**
887  * get_strings: Return a set of strings that describe the requested objects
888  * @dev: net device
889  * @stats: string set ID.
890  * @data: objects data.
891  */
hns_get_strings(struct net_device * netdev,u32 stringset,u8 * data)892 void hns_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
893 {
894 	struct hns_nic_priv *priv = netdev_priv(netdev);
895 	struct hnae_handle *h = priv->ae_handle;
896 	char *buff = (char *)data;
897 
898 	if (!h->dev->ops->get_strings) {
899 		netdev_err(netdev, "h->dev->ops->get_strings is null!\n");
900 		return;
901 	}
902 
903 	if (stringset == ETH_SS_TEST) {
904 		if (priv->ae_handle->phy_if != PHY_INTERFACE_MODE_XGMII) {
905 			memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_MAC],
906 			       ETH_GSTRING_LEN);
907 			buff += ETH_GSTRING_LEN;
908 		}
909 		memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_SERDES],
910 		       ETH_GSTRING_LEN);
911 		buff += ETH_GSTRING_LEN;
912 		if ((netdev->phydev) && (!netdev->phydev->is_c45))
913 			memcpy(buff, hns_nic_test_strs[MAC_INTERNALLOOP_PHY],
914 			       ETH_GSTRING_LEN);
915 
916 	} else {
917 		snprintf(buff, ETH_GSTRING_LEN, "rx_packets");
918 		buff = buff + ETH_GSTRING_LEN;
919 		snprintf(buff, ETH_GSTRING_LEN, "tx_packets");
920 		buff = buff + ETH_GSTRING_LEN;
921 		snprintf(buff, ETH_GSTRING_LEN, "rx_bytes");
922 		buff = buff + ETH_GSTRING_LEN;
923 		snprintf(buff, ETH_GSTRING_LEN, "tx_bytes");
924 		buff = buff + ETH_GSTRING_LEN;
925 		snprintf(buff, ETH_GSTRING_LEN, "rx_errors");
926 		buff = buff + ETH_GSTRING_LEN;
927 		snprintf(buff, ETH_GSTRING_LEN, "tx_errors");
928 		buff = buff + ETH_GSTRING_LEN;
929 		snprintf(buff, ETH_GSTRING_LEN, "rx_dropped");
930 		buff = buff + ETH_GSTRING_LEN;
931 		snprintf(buff, ETH_GSTRING_LEN, "tx_dropped");
932 		buff = buff + ETH_GSTRING_LEN;
933 		snprintf(buff, ETH_GSTRING_LEN, "multicast");
934 		buff = buff + ETH_GSTRING_LEN;
935 		snprintf(buff, ETH_GSTRING_LEN, "collisions");
936 		buff = buff + ETH_GSTRING_LEN;
937 		snprintf(buff, ETH_GSTRING_LEN, "rx_over_errors");
938 		buff = buff + ETH_GSTRING_LEN;
939 		snprintf(buff, ETH_GSTRING_LEN, "rx_crc_errors");
940 		buff = buff + ETH_GSTRING_LEN;
941 		snprintf(buff, ETH_GSTRING_LEN, "rx_frame_errors");
942 		buff = buff + ETH_GSTRING_LEN;
943 		snprintf(buff, ETH_GSTRING_LEN, "rx_fifo_errors");
944 		buff = buff + ETH_GSTRING_LEN;
945 		snprintf(buff, ETH_GSTRING_LEN, "rx_missed_errors");
946 		buff = buff + ETH_GSTRING_LEN;
947 		snprintf(buff, ETH_GSTRING_LEN, "tx_aborted_errors");
948 		buff = buff + ETH_GSTRING_LEN;
949 		snprintf(buff, ETH_GSTRING_LEN, "tx_carrier_errors");
950 		buff = buff + ETH_GSTRING_LEN;
951 		snprintf(buff, ETH_GSTRING_LEN, "tx_fifo_errors");
952 		buff = buff + ETH_GSTRING_LEN;
953 		snprintf(buff, ETH_GSTRING_LEN, "tx_heartbeat_errors");
954 		buff = buff + ETH_GSTRING_LEN;
955 		snprintf(buff, ETH_GSTRING_LEN, "rx_length_errors");
956 		buff = buff + ETH_GSTRING_LEN;
957 		snprintf(buff, ETH_GSTRING_LEN, "tx_window_errors");
958 		buff = buff + ETH_GSTRING_LEN;
959 		snprintf(buff, ETH_GSTRING_LEN, "rx_compressed");
960 		buff = buff + ETH_GSTRING_LEN;
961 		snprintf(buff, ETH_GSTRING_LEN, "tx_compressed");
962 		buff = buff + ETH_GSTRING_LEN;
963 		snprintf(buff, ETH_GSTRING_LEN, "netdev_rx_dropped");
964 		buff = buff + ETH_GSTRING_LEN;
965 		snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_dropped");
966 		buff = buff + ETH_GSTRING_LEN;
967 
968 		snprintf(buff, ETH_GSTRING_LEN, "netdev_tx_timeout");
969 		buff = buff + ETH_GSTRING_LEN;
970 
971 		h->dev->ops->get_strings(h, stringset, (u8 *)buff);
972 	}
973 }
974 
975 /**
976  * nic_get_sset_count - get string set count witch returned by nic_get_strings.
977  * @dev: net device
978  * @stringset: string set index, 0: self test string; 1: statistics string.
979  *
980  * Return string set count.
981  */
hns_get_sset_count(struct net_device * netdev,int stringset)982 int hns_get_sset_count(struct net_device *netdev, int stringset)
983 {
984 	struct hns_nic_priv *priv = netdev_priv(netdev);
985 	struct hnae_handle *h = priv->ae_handle;
986 	struct hnae_ae_ops *ops = h->dev->ops;
987 
988 	if (!ops->get_sset_count) {
989 		netdev_err(netdev, "get_sset_count is null!\n");
990 		return -EOPNOTSUPP;
991 	}
992 	if (stringset == ETH_SS_TEST) {
993 		u32 cnt = (sizeof(hns_nic_test_strs) / ETH_GSTRING_LEN);
994 
995 		if (priv->ae_handle->phy_if == PHY_INTERFACE_MODE_XGMII)
996 			cnt--;
997 
998 		if ((!netdev->phydev) || (netdev->phydev->is_c45))
999 			cnt--;
1000 
1001 		return cnt;
1002 	} else if (stringset == ETH_SS_STATS) {
1003 		return (HNS_NET_STATS_CNT + ops->get_sset_count(h, stringset));
1004 	} else {
1005 		return -EOPNOTSUPP;
1006 	}
1007 }
1008 
1009 /**
1010  * hns_phy_led_set - set phy LED status.
1011  * @dev: net device
1012  * @value: LED state.
1013  *
1014  * Return 0 on success, negative on failure.
1015  */
hns_phy_led_set(struct net_device * netdev,int value)1016 int hns_phy_led_set(struct net_device *netdev, int value)
1017 {
1018 	int retval;
1019 	struct phy_device *phy_dev = netdev->phydev;
1020 
1021 	retval = phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_LED);
1022 	retval |= phy_write(phy_dev, HNS_LED_FC_REG, value);
1023 	retval |= phy_write(phy_dev, HNS_PHY_PAGE_REG, HNS_PHY_PAGE_COPPER);
1024 	if (retval) {
1025 		netdev_err(netdev, "mdiobus_write fail !\n");
1026 		return retval;
1027 	}
1028 	return 0;
1029 }
1030 
1031 /**
1032  * nic_set_phys_id - set phy identify LED.
1033  * @dev: net device
1034  * @state: LED state.
1035  *
1036  * Return 0 on success, negative on failure.
1037  */
hns_set_phys_id(struct net_device * netdev,enum ethtool_phys_id_state state)1038 int hns_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1039 {
1040 	struct hns_nic_priv *priv = netdev_priv(netdev);
1041 	struct hnae_handle *h = priv->ae_handle;
1042 	struct phy_device *phy_dev = netdev->phydev;
1043 	int ret;
1044 
1045 	if (phy_dev)
1046 		switch (state) {
1047 		case ETHTOOL_ID_ACTIVE:
1048 			ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1049 					HNS_PHY_PAGE_LED);
1050 			if (ret)
1051 				return ret;
1052 
1053 			priv->phy_led_val = phy_read(phy_dev, HNS_LED_FC_REG);
1054 
1055 			ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1056 					HNS_PHY_PAGE_COPPER);
1057 			if (ret)
1058 				return ret;
1059 			return 2;
1060 		case ETHTOOL_ID_ON:
1061 			ret = hns_phy_led_set(netdev, HNS_LED_FORCE_ON);
1062 			if (ret)
1063 				return ret;
1064 			break;
1065 		case ETHTOOL_ID_OFF:
1066 			ret = hns_phy_led_set(netdev, HNS_LED_FORCE_OFF);
1067 			if (ret)
1068 				return ret;
1069 			break;
1070 		case ETHTOOL_ID_INACTIVE:
1071 			ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1072 					HNS_PHY_PAGE_LED);
1073 			if (ret)
1074 				return ret;
1075 
1076 			ret = phy_write(phy_dev, HNS_LED_FC_REG,
1077 					priv->phy_led_val);
1078 			if (ret)
1079 				return ret;
1080 
1081 			ret = phy_write(phy_dev, HNS_PHY_PAGE_REG,
1082 					HNS_PHY_PAGE_COPPER);
1083 			if (ret)
1084 				return ret;
1085 			break;
1086 		default:
1087 			return -EINVAL;
1088 		}
1089 	else
1090 		switch (state) {
1091 		case ETHTOOL_ID_ACTIVE:
1092 			return h->dev->ops->set_led_id(h, HNAE_LED_ACTIVE);
1093 		case ETHTOOL_ID_ON:
1094 			return h->dev->ops->set_led_id(h, HNAE_LED_ON);
1095 		case ETHTOOL_ID_OFF:
1096 			return h->dev->ops->set_led_id(h, HNAE_LED_OFF);
1097 		case ETHTOOL_ID_INACTIVE:
1098 			return h->dev->ops->set_led_id(h, HNAE_LED_INACTIVE);
1099 		default:
1100 			return -EINVAL;
1101 		}
1102 
1103 	return 0;
1104 }
1105 
1106 /**
1107  * hns_get_regs - get net device register
1108  * @dev: net device
1109  * @cmd: ethtool cmd
1110  * @date: register data
1111  */
hns_get_regs(struct net_device * net_dev,struct ethtool_regs * cmd,void * data)1112 void hns_get_regs(struct net_device *net_dev, struct ethtool_regs *cmd,
1113 		  void *data)
1114 {
1115 	struct hns_nic_priv *priv = netdev_priv(net_dev);
1116 	struct hnae_ae_ops *ops;
1117 
1118 	ops = priv->ae_handle->dev->ops;
1119 
1120 	cmd->version = HNS_CHIP_VERSION;
1121 	if (!ops->get_regs) {
1122 		netdev_err(net_dev, "ops->get_regs is null!\n");
1123 		return;
1124 	}
1125 	ops->get_regs(priv->ae_handle, data);
1126 }
1127 
1128 /**
1129  * nic_get_regs_len - get total register len.
1130  * @dev: net device
1131  *
1132  * Return total register len.
1133  */
hns_get_regs_len(struct net_device * net_dev)1134 static int hns_get_regs_len(struct net_device *net_dev)
1135 {
1136 	u32 reg_num;
1137 	struct hns_nic_priv *priv = netdev_priv(net_dev);
1138 	struct hnae_ae_ops *ops;
1139 
1140 	ops = priv->ae_handle->dev->ops;
1141 	if (!ops->get_regs_len) {
1142 		netdev_err(net_dev, "ops->get_regs_len is null!\n");
1143 		return -EOPNOTSUPP;
1144 	}
1145 
1146 	reg_num = ops->get_regs_len(priv->ae_handle);
1147 	if (reg_num > 0)
1148 		return reg_num * sizeof(u32);
1149 	else
1150 		return reg_num;	/* error code */
1151 }
1152 
1153 /**
1154  * hns_nic_nway_reset - nway reset
1155  * @dev: net device
1156  *
1157  * Return 0 on success, negative on failure
1158  */
hns_nic_nway_reset(struct net_device * netdev)1159 static int hns_nic_nway_reset(struct net_device *netdev)
1160 {
1161 	struct phy_device *phy = netdev->phydev;
1162 
1163 	if (!netif_running(netdev))
1164 		return 0;
1165 
1166 	if (!phy)
1167 		return -EOPNOTSUPP;
1168 
1169 	if (phy->autoneg != AUTONEG_ENABLE)
1170 		return -EINVAL;
1171 
1172 	return genphy_restart_aneg(phy);
1173 }
1174 
1175 static u32
hns_get_rss_key_size(struct net_device * netdev)1176 hns_get_rss_key_size(struct net_device *netdev)
1177 {
1178 	struct hns_nic_priv *priv = netdev_priv(netdev);
1179 	struct hnae_ae_ops *ops;
1180 
1181 	if (AE_IS_VER1(priv->enet_ver)) {
1182 		netdev_err(netdev,
1183 			   "RSS feature is not supported on this hardware\n");
1184 		return 0;
1185 	}
1186 
1187 	ops = priv->ae_handle->dev->ops;
1188 	return ops->get_rss_key_size(priv->ae_handle);
1189 }
1190 
1191 static u32
hns_get_rss_indir_size(struct net_device * netdev)1192 hns_get_rss_indir_size(struct net_device *netdev)
1193 {
1194 	struct hns_nic_priv *priv = netdev_priv(netdev);
1195 	struct hnae_ae_ops *ops;
1196 
1197 	if (AE_IS_VER1(priv->enet_ver)) {
1198 		netdev_err(netdev,
1199 			   "RSS feature is not supported on this hardware\n");
1200 		return 0;
1201 	}
1202 
1203 	ops = priv->ae_handle->dev->ops;
1204 	return ops->get_rss_indir_size(priv->ae_handle);
1205 }
1206 
1207 static int
hns_get_rss(struct net_device * netdev,u32 * indir,u8 * key,u8 * hfunc)1208 hns_get_rss(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc)
1209 {
1210 	struct hns_nic_priv *priv = netdev_priv(netdev);
1211 	struct hnae_ae_ops *ops;
1212 
1213 	if (AE_IS_VER1(priv->enet_ver)) {
1214 		netdev_err(netdev,
1215 			   "RSS feature is not supported on this hardware\n");
1216 		return -EOPNOTSUPP;
1217 	}
1218 
1219 	ops = priv->ae_handle->dev->ops;
1220 
1221 	if (!indir)
1222 		return 0;
1223 
1224 	return ops->get_rss(priv->ae_handle, indir, key, hfunc);
1225 }
1226 
1227 static int
hns_set_rss(struct net_device * netdev,const u32 * indir,const u8 * key,const u8 hfunc)1228 hns_set_rss(struct net_device *netdev, const u32 *indir, const u8 *key,
1229 	    const u8 hfunc)
1230 {
1231 	struct hns_nic_priv *priv = netdev_priv(netdev);
1232 	struct hnae_ae_ops *ops;
1233 
1234 	if (AE_IS_VER1(priv->enet_ver)) {
1235 		netdev_err(netdev,
1236 			   "RSS feature is not supported on this hardware\n");
1237 		return -EOPNOTSUPP;
1238 	}
1239 
1240 	ops = priv->ae_handle->dev->ops;
1241 
1242 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) {
1243 		netdev_err(netdev, "Invalid hfunc!\n");
1244 		return -EOPNOTSUPP;
1245 	}
1246 
1247 	return ops->set_rss(priv->ae_handle, indir, key, hfunc);
1248 }
1249 
hns_get_rxnfc(struct net_device * netdev,struct ethtool_rxnfc * cmd,u32 * rule_locs)1250 static int hns_get_rxnfc(struct net_device *netdev,
1251 			 struct ethtool_rxnfc *cmd,
1252 			 u32 *rule_locs)
1253 {
1254 	struct hns_nic_priv *priv = netdev_priv(netdev);
1255 
1256 	switch (cmd->cmd) {
1257 	case ETHTOOL_GRXRINGS:
1258 		cmd->data = priv->ae_handle->q_num;
1259 		break;
1260 	default:
1261 		return -EOPNOTSUPP;
1262 	}
1263 
1264 	return 0;
1265 }
1266 
1267 static const struct ethtool_ops hns_ethtool_ops = {
1268 	.get_drvinfo = hns_nic_get_drvinfo,
1269 	.get_link  = hns_nic_get_link,
1270 	.get_ringparam = hns_get_ringparam,
1271 	.get_pauseparam = hns_get_pauseparam,
1272 	.set_pauseparam = hns_set_pauseparam,
1273 	.get_coalesce = hns_get_coalesce,
1274 	.set_coalesce = hns_set_coalesce,
1275 	.get_channels = hns_get_channels,
1276 	.self_test = hns_nic_self_test,
1277 	.get_strings = hns_get_strings,
1278 	.get_sset_count = hns_get_sset_count,
1279 	.get_ethtool_stats = hns_get_ethtool_stats,
1280 	.set_phys_id = hns_set_phys_id,
1281 	.get_regs_len = hns_get_regs_len,
1282 	.get_regs = hns_get_regs,
1283 	.nway_reset = hns_nic_nway_reset,
1284 	.get_rxfh_key_size = hns_get_rss_key_size,
1285 	.get_rxfh_indir_size = hns_get_rss_indir_size,
1286 	.get_rxfh = hns_get_rss,
1287 	.set_rxfh = hns_set_rss,
1288 	.get_rxnfc = hns_get_rxnfc,
1289 	.get_link_ksettings  = hns_nic_get_link_ksettings,
1290 	.set_link_ksettings  = hns_nic_set_link_ksettings,
1291 };
1292 
hns_ethtool_set_ops(struct net_device * ndev)1293 void hns_ethtool_set_ops(struct net_device *ndev)
1294 {
1295 	ndev->ethtool_ops = &hns_ethtool_ops;
1296 }
1297