2 * Copyright (c) 2014-2015 Hisilicon Limited.
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.
10 #include <linux/etherdevice.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
16 #define HNS_PHY_PAGE_MDIX 0
17 #define HNS_PHY_PAGE_LED 3
18 #define HNS_PHY_PAGE_COPPER 0
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. */
26 #define HNS_LED_FORCE_ON 9
27 #define HNS_LED_FORCE_OFF 8
29 #define HNS_CHIP_VERSION 660
30 #define HNS_NET_STATS_CNT 26
32 #define PHY_MDIX_CTRL_S (5)
33 #define PHY_MDIX_CTRL_M (3 << PHY_MDIX_CTRL_S)
35 #define PHY_MDIX_STATUS_B (6)
36 #define PHY_SPEED_DUP_RESOLVE_B (11)
39 *hns_nic_get_link - get current link status
41 *retuen 0 - success , negative --fail
43 static u32
hns_nic_get_link(struct net_device
*net_dev
)
45 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
46 u32 link_stat
= priv
->link
;
47 struct hnae_handle
*h
;
52 if (!genphy_update_link(priv
->phy
))
53 link_stat
= priv
->phy
->link
;
58 if (h
->dev
&& h
->dev
->ops
&& h
->dev
->ops
->get_status
)
59 link_stat
= link_stat
&& h
->dev
->ops
->get_status(h
);
66 static void hns_get_mdix_mode(struct net_device
*net_dev
,
67 struct ethtool_cmd
*cmd
)
69 int mdix_ctrl
, mdix
, retval
, is_resolved
;
70 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
71 struct phy_device
*phy_dev
= priv
->phy
;
73 if (!phy_dev
|| !phy_dev
->mdio
.bus
) {
74 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI_INVALID
;
75 cmd
->eth_tp_mdix
= ETH_TP_MDI_INVALID
;
79 phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_MDIX
);
81 retval
= phy_read(phy_dev
, HNS_PHY_CSC_REG
);
82 mdix_ctrl
= hnae_get_field(retval
, PHY_MDIX_CTRL_M
, PHY_MDIX_CTRL_S
);
84 retval
= phy_read(phy_dev
, HNS_PHY_CSS_REG
);
85 mdix
= hnae_get_bit(retval
, PHY_MDIX_STATUS_B
);
86 is_resolved
= hnae_get_bit(retval
, PHY_SPEED_DUP_RESOLVE_B
);
88 phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_COPPER
);
92 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI
;
95 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI_X
;
98 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI_AUTO
;
101 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI_INVALID
;
106 cmd
->eth_tp_mdix
= ETH_TP_MDI_INVALID
;
108 cmd
->eth_tp_mdix
= ETH_TP_MDI_X
;
110 cmd
->eth_tp_mdix
= ETH_TP_MDI
;
114 *hns_nic_get_settings - implement ethtool get settings
115 *@net_dev: net_device
117 *retuen 0 - success , negative --fail
119 static int hns_nic_get_settings(struct net_device
*net_dev
,
120 struct ethtool_cmd
*cmd
)
122 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
123 struct hnae_handle
*h
;
129 if (!priv
|| !priv
->ae_handle
)
133 if (!h
->dev
|| !h
->dev
->ops
|| !h
->dev
->ops
->get_info
)
136 ret
= h
->dev
->ops
->get_info(h
, NULL
, &speed
, &duplex
);
138 netdev_err(net_dev
, "%s get_info error!\n", __func__
);
142 /* When there is no phy, autoneg is off. */
143 cmd
->autoneg
= false;
144 ethtool_cmd_speed_set(cmd
, speed
);
145 cmd
->duplex
= duplex
;
148 (void)phy_ethtool_gset(priv
->phy
, cmd
);
150 link_stat
= hns_nic_get_link(net_dev
);
152 ethtool_cmd_speed_set(cmd
, (u32
)SPEED_UNKNOWN
);
153 cmd
->duplex
= DUPLEX_UNKNOWN
;
157 cmd
->advertising
|= ADVERTISED_Autoneg
;
159 cmd
->supported
|= h
->if_support
;
160 if (h
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
161 cmd
->supported
|= SUPPORTED_TP
;
162 cmd
->advertising
|= ADVERTISED_1000baseT_Full
;
163 } else if (h
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
164 cmd
->supported
|= SUPPORTED_FIBRE
;
165 cmd
->advertising
|= ADVERTISED_10000baseKR_Full
;
168 if (h
->port_type
== HNAE_PORT_SERVICE
) {
169 cmd
->port
= PORT_FIBRE
;
170 cmd
->supported
|= SUPPORTED_Pause
;
175 cmd
->transceiver
= XCVR_EXTERNAL
;
176 cmd
->mdio_support
= (ETH_MDIO_SUPPORTS_C45
| ETH_MDIO_SUPPORTS_C22
);
177 hns_get_mdix_mode(net_dev
, cmd
);
183 *hns_nic_set_settings - implement ethtool set settings
184 *@net_dev: net_device
186 *retuen 0 - success , negative --fail
188 static int hns_nic_set_settings(struct net_device
*net_dev
,
189 struct ethtool_cmd
*cmd
)
191 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
192 struct hnae_handle
*h
;
195 if (!netif_running(net_dev
))
198 if (!priv
|| !priv
->ae_handle
|| !priv
->ae_handle
->dev
||
199 !priv
->ae_handle
->dev
->ops
)
203 speed
= ethtool_cmd_speed(cmd
);
205 if (h
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
206 if (cmd
->autoneg
== AUTONEG_ENABLE
|| speed
!= SPEED_10000
||
207 cmd
->duplex
!= DUPLEX_FULL
)
209 } else if (h
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
210 if (!priv
->phy
&& cmd
->autoneg
== AUTONEG_ENABLE
)
213 if (speed
== SPEED_1000
&& cmd
->duplex
== DUPLEX_HALF
)
216 return phy_ethtool_sset(priv
->phy
, cmd
);
218 if ((speed
!= SPEED_10
&& speed
!= SPEED_100
&&
219 speed
!= SPEED_1000
) || (cmd
->duplex
!= DUPLEX_HALF
&&
220 cmd
->duplex
!= DUPLEX_FULL
))
223 netdev_err(net_dev
, "Not supported!");
227 if (h
->dev
->ops
->adjust_link
) {
228 h
->dev
->ops
->adjust_link(h
, (int)speed
, cmd
->duplex
);
232 netdev_err(net_dev
, "Not supported!");
236 static const char hns_nic_test_strs
[][ETH_GSTRING_LEN
] = {
238 "Serdes Loopback test",
242 static int hns_nic_config_phy_loopback(struct phy_device
*phy_dev
, u8 en
)
244 #define COPPER_CONTROL_REG 0
245 #define PHY_LOOP_BACK BIT(14)
248 if (phy_dev
->is_c45
) /* c45 branch adding for XGE PHY */
253 phy_write(phy_dev
, HNS_PHY_PAGE_REG
, 2);
254 phy_write(phy_dev
, 21, 0x1046);
256 phy_write(phy_dev
, 9, 0x1F00);
258 phy_write(phy_dev
, 0, 0x9140);
259 /* If autoneg disabled,two soft-reset operations */
260 phy_write(phy_dev
, 0, 0x9140);
261 phy_write(phy_dev
, 22, 0xFA);
263 /* Default is 0x0400 */
264 phy_write(phy_dev
, 1, 0x418);
266 /* Force 1000M Link, Default is 0x0200 */
267 phy_write(phy_dev
, 7, 0x20C);
268 phy_write(phy_dev
, 22, 0);
270 /* Enable MAC loop-back */
271 val
= phy_read(phy_dev
, COPPER_CONTROL_REG
);
272 val
|= PHY_LOOP_BACK
;
273 phy_write(phy_dev
, COPPER_CONTROL_REG
, val
);
275 phy_write(phy_dev
, 22, 0xFA);
276 phy_write(phy_dev
, 1, 0x400);
277 phy_write(phy_dev
, 7, 0x200);
278 phy_write(phy_dev
, 22, 0);
280 val
= phy_read(phy_dev
, COPPER_CONTROL_REG
);
281 val
&= ~PHY_LOOP_BACK
;
282 phy_write(phy_dev
, COPPER_CONTROL_REG
, val
);
287 static int __lb_setup(struct net_device
*ndev
,
291 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
292 struct phy_device
*phy_dev
= priv
->phy
;
293 struct hnae_handle
*h
= priv
->ae_handle
;
296 case MAC_INTERNALLOOP_PHY
:
297 if ((phy_dev
) && (!phy_dev
->is_c45
)) {
298 ret
= hns_nic_config_phy_loopback(phy_dev
, 0x1);
299 ret
|= h
->dev
->ops
->set_loopback(h
, loop
, 0x1);
302 case MAC_INTERNALLOOP_MAC
:
303 if ((h
->dev
->ops
->set_loopback
) &&
304 (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
))
305 ret
= h
->dev
->ops
->set_loopback(h
, loop
, 0x1);
307 case MAC_INTERNALLOOP_SERDES
:
308 if (h
->dev
->ops
->set_loopback
)
309 ret
= h
->dev
->ops
->set_loopback(h
, loop
, 0x1);
312 if ((phy_dev
) && (!phy_dev
->is_c45
))
313 ret
|= hns_nic_config_phy_loopback(phy_dev
, 0x0);
315 if (h
->dev
->ops
->set_loopback
) {
316 if (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
)
317 ret
|= h
->dev
->ops
->set_loopback(h
,
318 MAC_INTERNALLOOP_MAC
, 0x0);
320 ret
|= h
->dev
->ops
->set_loopback(h
,
321 MAC_INTERNALLOOP_SERDES
, 0x0);
332 static int __lb_up(struct net_device
*ndev
,
333 enum hnae_loop loop_mode
)
335 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
336 struct hnae_handle
*h
= priv
->ae_handle
;
340 hns_nic_net_reset(ndev
);
343 phy_disconnect(priv
->phy
);
346 ret
= hns_nic_init_phy(ndev
, h
);
351 ret
= __lb_setup(ndev
, loop_mode
);
357 ret
= h
->dev
->ops
->start
? h
->dev
->ops
->start(h
) : 0;
362 phy_start(priv
->phy
);
364 /* link adjust duplex*/
365 if (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
)
371 h
->dev
->ops
->adjust_link(h
, speed
, duplex
);
376 static void __lb_other_process(struct hns_nic_ring_data
*ring_data
,
379 struct net_device
*ndev
;
380 struct hns_nic_priv
*priv
;
381 struct hnae_ring
*ring
;
382 struct netdev_queue
*dev_queue
;
383 struct sk_buff
*new_skb
;
384 unsigned int frame_size
;
387 char buff
[33]; /* 32B data and the last character '\0' */
389 if (!ring_data
) { /* Just for doing create frame*/
391 priv
= netdev_priv(ndev
);
393 frame_size
= skb
->len
;
394 memset(skb
->data
, 0xFF, frame_size
);
395 if ((!AE_IS_VER1(priv
->enet_ver
)) &&
396 (priv
->ae_handle
->port_type
== HNAE_PORT_SERVICE
)) {
397 memcpy(skb
->data
, ndev
->dev_addr
, 6);
398 skb
->data
[5] += 0x1f;
402 memset(&skb
->data
[frame_size
/ 2], 0xAA, frame_size
/ 2 - 1);
403 memset(&skb
->data
[frame_size
/ 2 + 10], 0xBE,
404 frame_size
/ 2 - 11);
405 memset(&skb
->data
[frame_size
/ 2 + 12], 0xAF,
406 frame_size
/ 2 - 13);
410 ring
= ring_data
->ring
;
411 ndev
= ring_data
->napi
.dev
;
412 if (is_tx_ring(ring
)) { /* for tx queue reset*/
413 dev_queue
= netdev_get_tx_queue(ndev
, ring_data
->queue_index
);
414 netdev_tx_reset_queue(dev_queue
);
418 frame_size
= skb
->len
;
421 new_skb
= skb_copy(skb
, GFP_ATOMIC
);
422 dev_kfree_skb_any(skb
);
426 if (*(skb
->data
+ 10) == 0xFF) { /* for rx check frame*/
427 if ((*(skb
->data
+ frame_size
/ 2 + 10) == 0xBE) &&
428 (*(skb
->data
+ frame_size
/ 2 + 12) == 0xAF))
433 ndev
->stats
.rx_packets
++;
434 ndev
->stats
.rx_bytes
+= skb
->len
;
436 ndev
->stats
.rx_frame_errors
++;
437 for (i
= 0; i
< skb
->len
; i
++) {
438 snprintf(buff
+ i
% 16 * 2, 3, /* tailing \0*/
439 "%02x", *(skb
->data
+ i
));
440 if ((i
% 16 == 15) || (i
== skb
->len
- 1))
441 pr_info("%s\n", buff
);
444 dev_kfree_skb_any(skb
);
447 static int __lb_clean_rings(struct hns_nic_priv
*priv
,
448 int ringid0
, int ringid1
, int budget
)
451 struct hns_nic_ring_data
*ring_data
;
452 struct net_device
*ndev
= priv
->netdev
;
453 unsigned long rx_packets
= ndev
->stats
.rx_packets
;
454 unsigned long rx_bytes
= ndev
->stats
.rx_bytes
;
455 unsigned long rx_frame_errors
= ndev
->stats
.rx_frame_errors
;
457 for (i
= ringid0
; i
<= ringid1
; i
++) {
458 ring_data
= &priv
->ring_data
[i
];
459 (void)ring_data
->poll_one(ring_data
,
460 budget
, __lb_other_process
);
462 ret
= (int)(ndev
->stats
.rx_packets
- rx_packets
);
463 ndev
->stats
.rx_packets
= rx_packets
;
464 ndev
->stats
.rx_bytes
= rx_bytes
;
465 ndev
->stats
.rx_frame_errors
= rx_frame_errors
;
470 * nic_run_loopback_test - run loopback test
471 * @nic_dev: net device
472 * @loopback_type: loopback type
474 static int __lb_run_test(struct net_device
*ndev
,
475 enum hnae_loop loop_mode
)
477 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
478 #define NIC_LB_TEST_RING_ID 0
479 #define NIC_LB_TEST_FRAME_SIZE 128
480 /* nic loopback test err */
481 #define NIC_LB_TEST_NO_MEM_ERR 1
482 #define NIC_LB_TEST_TX_CNT_ERR 2
483 #define NIC_LB_TEST_RX_CNT_ERR 3
484 #define NIC_LB_TEST_RX_PKG_ERR 4
485 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
486 struct hnae_handle
*h
= priv
->ae_handle
;
487 int i
, j
, lc
, good_cnt
, ret_val
= 0;
489 netdev_tx_t tx_ret_val
;
492 size
= NIC_LB_TEST_FRAME_SIZE
;
493 /* allocate test skb */
494 skb
= alloc_skb(size
, GFP_KERNEL
);
496 return NIC_LB_TEST_NO_MEM_ERR
;
498 /* place data into test skb */
499 (void)skb_put(skb
, size
);
501 __lb_other_process(NULL
, skb
);
502 skb
->queue_mapping
= NIC_LB_TEST_RING_ID
;
505 for (j
= 0; j
< lc
; j
++) {
506 /* reset count of good packets */
508 /* place 64 packets on the transmit queue*/
509 for (i
= 0; i
< NIC_LB_TEST_PKT_NUM_PER_CYCLE
; i
++) {
512 tx_ret_val
= (netdev_tx_t
)hns_nic_net_xmit_hw(
514 &tx_ring_data(priv
, skb
->queue_mapping
));
515 if (tx_ret_val
== NETDEV_TX_OK
)
520 if (good_cnt
!= NIC_LB_TEST_PKT_NUM_PER_CYCLE
) {
521 ret_val
= NIC_LB_TEST_TX_CNT_ERR
;
522 dev_err(priv
->dev
, "%s sent fail, cnt=0x%x, budget=0x%x\n",
523 hns_nic_test_strs
[loop_mode
], good_cnt
,
524 NIC_LB_TEST_PKT_NUM_PER_CYCLE
);
528 /* allow 100 milliseconds for packets to go from Tx to Rx */
531 good_cnt
= __lb_clean_rings(priv
,
532 h
->q_num
, h
->q_num
* 2 - 1,
533 NIC_LB_TEST_PKT_NUM_PER_CYCLE
);
534 if (good_cnt
!= NIC_LB_TEST_PKT_NUM_PER_CYCLE
) {
535 ret_val
= NIC_LB_TEST_RX_CNT_ERR
;
536 dev_err(priv
->dev
, "%s recv fail, cnt=0x%x, budget=0x%x\n",
537 hns_nic_test_strs
[loop_mode
], good_cnt
,
538 NIC_LB_TEST_PKT_NUM_PER_CYCLE
);
541 (void)__lb_clean_rings(priv
,
542 NIC_LB_TEST_RING_ID
, NIC_LB_TEST_RING_ID
,
543 NIC_LB_TEST_PKT_NUM_PER_CYCLE
);
546 /* free the original skb */
552 static int __lb_down(struct net_device
*ndev
)
554 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
555 struct hnae_handle
*h
= priv
->ae_handle
;
558 ret
= __lb_setup(ndev
, MAC_LOOP_NONE
);
560 netdev_err(ndev
, "%s: __lb_setup return error(%d)!\n",
567 if (h
->dev
->ops
->stop
)
568 h
->dev
->ops
->stop(h
);
570 usleep_range(10000, 20000);
571 (void)__lb_clean_rings(priv
, 0, h
->q_num
- 1, 256);
573 hns_nic_net_reset(ndev
);
579 * hns_nic_self_test - self test
581 * @eth_test: test cmd
584 static void hns_nic_self_test(struct net_device
*ndev
,
585 struct ethtool_test
*eth_test
, u64
*data
)
587 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
588 bool if_running
= netif_running(ndev
);
589 #define SELF_TEST_TPYE_NUM 3
590 int st_param
[SELF_TEST_TPYE_NUM
][2];
594 st_param
[0][0] = MAC_INTERNALLOOP_MAC
; /* XGE not supported lb */
595 st_param
[0][1] = (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
);
596 st_param
[1][0] = MAC_INTERNALLOOP_SERDES
;
597 st_param
[1][1] = 1; /*serdes must exist*/
598 st_param
[2][0] = MAC_INTERNALLOOP_PHY
; /* only supporte phy node*/
599 st_param
[2][1] = ((!!(priv
->ae_handle
->phy_node
)) &&
600 (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
));
602 if (eth_test
->flags
== ETH_TEST_FL_OFFLINE
) {
603 set_bit(NIC_STATE_TESTING
, &priv
->state
);
606 (void)dev_close(ndev
);
608 for (i
= 0; i
< SELF_TEST_TPYE_NUM
; i
++) {
610 continue; /* NEXT testing */
612 data
[test_index
] = __lb_up(ndev
,
613 (enum hnae_loop
)st_param
[i
][0]);
614 if (!data
[test_index
]) {
615 data
[test_index
] = __lb_run_test(
616 ndev
, (enum hnae_loop
)st_param
[i
][0]);
617 (void)__lb_down(ndev
);
620 if (data
[test_index
])
621 eth_test
->flags
|= ETH_TEST_FL_FAILED
;
626 hns_nic_net_reset(priv
->netdev
);
628 clear_bit(NIC_STATE_TESTING
, &priv
->state
);
631 (void)dev_open(ndev
);
633 /* Online tests aren't run; pass by default */
635 (void)msleep_interruptible(4 * 1000);
639 * hns_nic_get_drvinfo - get net driver info
641 * @drvinfo: driver info
643 static void hns_nic_get_drvinfo(struct net_device
*net_dev
,
644 struct ethtool_drvinfo
*drvinfo
)
646 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
648 strncpy(drvinfo
->version
, HNAE_DRIVER_VERSION
,
649 sizeof(drvinfo
->version
));
650 drvinfo
->version
[sizeof(drvinfo
->version
) - 1] = '\0';
652 strncpy(drvinfo
->driver
, HNAE_DRIVER_NAME
, sizeof(drvinfo
->driver
));
653 drvinfo
->driver
[sizeof(drvinfo
->driver
) - 1] = '\0';
655 strncpy(drvinfo
->bus_info
, priv
->dev
->bus
->name
,
656 sizeof(drvinfo
->bus_info
));
657 drvinfo
->bus_info
[ETHTOOL_BUSINFO_LEN
- 1] = '\0';
659 strncpy(drvinfo
->fw_version
, "N/A", ETHTOOL_FWVERS_LEN
);
660 drvinfo
->eedump_len
= 0;
664 * hns_get_ringparam - get ring parameter
666 * @param: ethtool parameter
668 void hns_get_ringparam(struct net_device
*net_dev
,
669 struct ethtool_ringparam
*param
)
671 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
672 struct hnae_ae_ops
*ops
;
673 struct hnae_queue
*queue
;
676 queue
= priv
->ae_handle
->qs
[0];
677 ops
= priv
->ae_handle
->dev
->ops
;
679 if (ops
->get_ring_bdnum_limit
)
680 ops
->get_ring_bdnum_limit(queue
, &uplimit
);
682 param
->rx_max_pending
= uplimit
;
683 param
->tx_max_pending
= uplimit
;
684 param
->rx_pending
= queue
->rx_ring
.desc_num
;
685 param
->tx_pending
= queue
->tx_ring
.desc_num
;
689 * hns_get_pauseparam - get pause parameter
691 * @param: pause parameter
693 static void hns_get_pauseparam(struct net_device
*net_dev
,
694 struct ethtool_pauseparam
*param
)
696 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
697 struct hnae_ae_ops
*ops
;
699 ops
= priv
->ae_handle
->dev
->ops
;
701 if (ops
->get_pauseparam
)
702 ops
->get_pauseparam(priv
->ae_handle
, ¶m
->autoneg
,
703 ¶m
->rx_pause
, ¶m
->tx_pause
);
707 * hns_set_pauseparam - set pause parameter
709 * @param: pause parameter
711 * Return 0 on success, negative on failure
713 static int hns_set_pauseparam(struct net_device
*net_dev
,
714 struct ethtool_pauseparam
*param
)
716 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
717 struct hnae_handle
*h
;
718 struct hnae_ae_ops
*ops
;
723 if (!ops
->set_pauseparam
)
726 return ops
->set_pauseparam(priv
->ae_handle
, param
->autoneg
,
727 param
->rx_pause
, param
->tx_pause
);
731 * hns_get_coalesce - get coalesce info.
733 * @ec: coalesce info.
735 * Return 0 on success, negative on failure.
737 static int hns_get_coalesce(struct net_device
*net_dev
,
738 struct ethtool_coalesce
*ec
)
740 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
741 struct hnae_ae_ops
*ops
;
743 ops
= priv
->ae_handle
->dev
->ops
;
745 ec
->use_adaptive_rx_coalesce
= 1;
746 ec
->use_adaptive_tx_coalesce
= 1;
748 if ((!ops
->get_coalesce_usecs
) ||
749 (!ops
->get_rx_max_coalesced_frames
))
752 ops
->get_coalesce_usecs(priv
->ae_handle
,
753 &ec
->tx_coalesce_usecs
,
754 &ec
->rx_coalesce_usecs
);
756 ops
->get_rx_max_coalesced_frames(
758 &ec
->tx_max_coalesced_frames
,
759 &ec
->rx_max_coalesced_frames
);
765 * hns_set_coalesce - set coalesce info.
767 * @ec: coalesce info.
769 * Return 0 on success, negative on failure.
771 static int hns_set_coalesce(struct net_device
*net_dev
,
772 struct ethtool_coalesce
*ec
)
774 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
775 struct hnae_ae_ops
*ops
;
778 ops
= priv
->ae_handle
->dev
->ops
;
780 if (ec
->tx_coalesce_usecs
!= ec
->rx_coalesce_usecs
)
783 if (ec
->rx_max_coalesced_frames
!= ec
->tx_max_coalesced_frames
)
786 if ((!ops
->set_coalesce_usecs
) ||
787 (!ops
->set_coalesce_frames
))
790 ret
= ops
->set_coalesce_usecs(priv
->ae_handle
,
791 ec
->rx_coalesce_usecs
);
795 ret
= ops
->set_coalesce_frames(
797 ec
->rx_max_coalesced_frames
);
803 * hns_get_channels - get channel info.
807 void hns_get_channels(struct net_device
*net_dev
, struct ethtool_channels
*ch
)
809 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
811 ch
->max_rx
= priv
->ae_handle
->q_num
;
812 ch
->max_tx
= priv
->ae_handle
->q_num
;
814 ch
->rx_count
= priv
->ae_handle
->q_num
;
815 ch
->tx_count
= priv
->ae_handle
->q_num
;
819 * get_ethtool_stats - get detail statistics.
821 * @stats: statistics info.
822 * @data: statistics data.
824 void hns_get_ethtool_stats(struct net_device
*netdev
,
825 struct ethtool_stats
*stats
, u64
*data
)
828 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
829 struct hnae_handle
*h
= priv
->ae_handle
;
830 const struct rtnl_link_stats64
*net_stats
;
831 struct rtnl_link_stats64 temp
;
833 if (!h
->dev
->ops
->get_stats
|| !h
->dev
->ops
->update_stats
) {
834 netdev_err(netdev
, "get_stats or update_stats is null!\n");
838 h
->dev
->ops
->update_stats(h
, &netdev
->stats
);
840 net_stats
= dev_get_stats(netdev
, &temp
);
842 /* get netdev statistics */
843 p
[0] = net_stats
->rx_packets
;
844 p
[1] = net_stats
->tx_packets
;
845 p
[2] = net_stats
->rx_bytes
;
846 p
[3] = net_stats
->tx_bytes
;
847 p
[4] = net_stats
->rx_errors
;
848 p
[5] = net_stats
->tx_errors
;
849 p
[6] = net_stats
->rx_dropped
;
850 p
[7] = net_stats
->tx_dropped
;
851 p
[8] = net_stats
->multicast
;
852 p
[9] = net_stats
->collisions
;
853 p
[10] = net_stats
->rx_over_errors
;
854 p
[11] = net_stats
->rx_crc_errors
;
855 p
[12] = net_stats
->rx_frame_errors
;
856 p
[13] = net_stats
->rx_fifo_errors
;
857 p
[14] = net_stats
->rx_missed_errors
;
858 p
[15] = net_stats
->tx_aborted_errors
;
859 p
[16] = net_stats
->tx_carrier_errors
;
860 p
[17] = net_stats
->tx_fifo_errors
;
861 p
[18] = net_stats
->tx_heartbeat_errors
;
862 p
[19] = net_stats
->rx_length_errors
;
863 p
[20] = net_stats
->tx_window_errors
;
864 p
[21] = net_stats
->rx_compressed
;
865 p
[22] = net_stats
->tx_compressed
;
867 p
[23] = netdev
->rx_dropped
.counter
;
868 p
[24] = netdev
->tx_dropped
.counter
;
870 p
[25] = priv
->tx_timeout_count
;
872 /* get driver statistics */
873 h
->dev
->ops
->get_stats(h
, &p
[26]);
877 * get_strings: Return a set of strings that describe the requested objects
879 * @stats: string set ID.
880 * @data: objects data.
882 void hns_get_strings(struct net_device
*netdev
, u32 stringset
, u8
*data
)
884 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
885 struct hnae_handle
*h
= priv
->ae_handle
;
886 char *buff
= (char *)data
;
888 if (!h
->dev
->ops
->get_strings
) {
889 netdev_err(netdev
, "h->dev->ops->get_strings is null!\n");
893 if (stringset
== ETH_SS_TEST
) {
894 if (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
) {
895 memcpy(buff
, hns_nic_test_strs
[MAC_INTERNALLOOP_MAC
],
897 buff
+= ETH_GSTRING_LEN
;
899 memcpy(buff
, hns_nic_test_strs
[MAC_INTERNALLOOP_SERDES
],
901 buff
+= ETH_GSTRING_LEN
;
902 if ((priv
->phy
) && (!priv
->phy
->is_c45
))
903 memcpy(buff
, hns_nic_test_strs
[MAC_INTERNALLOOP_PHY
],
907 snprintf(buff
, ETH_GSTRING_LEN
, "rx_packets");
908 buff
= buff
+ ETH_GSTRING_LEN
;
909 snprintf(buff
, ETH_GSTRING_LEN
, "tx_packets");
910 buff
= buff
+ ETH_GSTRING_LEN
;
911 snprintf(buff
, ETH_GSTRING_LEN
, "rx_bytes");
912 buff
= buff
+ ETH_GSTRING_LEN
;
913 snprintf(buff
, ETH_GSTRING_LEN
, "tx_bytes");
914 buff
= buff
+ ETH_GSTRING_LEN
;
915 snprintf(buff
, ETH_GSTRING_LEN
, "rx_errors");
916 buff
= buff
+ ETH_GSTRING_LEN
;
917 snprintf(buff
, ETH_GSTRING_LEN
, "tx_errors");
918 buff
= buff
+ ETH_GSTRING_LEN
;
919 snprintf(buff
, ETH_GSTRING_LEN
, "rx_dropped");
920 buff
= buff
+ ETH_GSTRING_LEN
;
921 snprintf(buff
, ETH_GSTRING_LEN
, "tx_dropped");
922 buff
= buff
+ ETH_GSTRING_LEN
;
923 snprintf(buff
, ETH_GSTRING_LEN
, "multicast");
924 buff
= buff
+ ETH_GSTRING_LEN
;
925 snprintf(buff
, ETH_GSTRING_LEN
, "collisions");
926 buff
= buff
+ ETH_GSTRING_LEN
;
927 snprintf(buff
, ETH_GSTRING_LEN
, "rx_over_errors");
928 buff
= buff
+ ETH_GSTRING_LEN
;
929 snprintf(buff
, ETH_GSTRING_LEN
, "rx_crc_errors");
930 buff
= buff
+ ETH_GSTRING_LEN
;
931 snprintf(buff
, ETH_GSTRING_LEN
, "rx_frame_errors");
932 buff
= buff
+ ETH_GSTRING_LEN
;
933 snprintf(buff
, ETH_GSTRING_LEN
, "rx_fifo_errors");
934 buff
= buff
+ ETH_GSTRING_LEN
;
935 snprintf(buff
, ETH_GSTRING_LEN
, "rx_missed_errors");
936 buff
= buff
+ ETH_GSTRING_LEN
;
937 snprintf(buff
, ETH_GSTRING_LEN
, "tx_aborted_errors");
938 buff
= buff
+ ETH_GSTRING_LEN
;
939 snprintf(buff
, ETH_GSTRING_LEN
, "tx_carrier_errors");
940 buff
= buff
+ ETH_GSTRING_LEN
;
941 snprintf(buff
, ETH_GSTRING_LEN
, "tx_fifo_errors");
942 buff
= buff
+ ETH_GSTRING_LEN
;
943 snprintf(buff
, ETH_GSTRING_LEN
, "tx_heartbeat_errors");
944 buff
= buff
+ ETH_GSTRING_LEN
;
945 snprintf(buff
, ETH_GSTRING_LEN
, "rx_length_errors");
946 buff
= buff
+ ETH_GSTRING_LEN
;
947 snprintf(buff
, ETH_GSTRING_LEN
, "tx_window_errors");
948 buff
= buff
+ ETH_GSTRING_LEN
;
949 snprintf(buff
, ETH_GSTRING_LEN
, "rx_compressed");
950 buff
= buff
+ ETH_GSTRING_LEN
;
951 snprintf(buff
, ETH_GSTRING_LEN
, "tx_compressed");
952 buff
= buff
+ ETH_GSTRING_LEN
;
953 snprintf(buff
, ETH_GSTRING_LEN
, "netdev_rx_dropped");
954 buff
= buff
+ ETH_GSTRING_LEN
;
955 snprintf(buff
, ETH_GSTRING_LEN
, "netdev_tx_dropped");
956 buff
= buff
+ ETH_GSTRING_LEN
;
958 snprintf(buff
, ETH_GSTRING_LEN
, "netdev_tx_timeout");
959 buff
= buff
+ ETH_GSTRING_LEN
;
961 h
->dev
->ops
->get_strings(h
, stringset
, (u8
*)buff
);
966 * nic_get_sset_count - get string set count witch returned by nic_get_strings.
968 * @stringset: string set index, 0: self test string; 1: statistics string.
970 * Return string set count.
972 int hns_get_sset_count(struct net_device
*netdev
, int stringset
)
974 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
975 struct hnae_handle
*h
= priv
->ae_handle
;
976 struct hnae_ae_ops
*ops
= h
->dev
->ops
;
978 if (!ops
->get_sset_count
) {
979 netdev_err(netdev
, "get_sset_count is null!\n");
982 if (stringset
== ETH_SS_TEST
) {
983 u32 cnt
= (sizeof(hns_nic_test_strs
) / ETH_GSTRING_LEN
);
985 if (priv
->ae_handle
->phy_if
== PHY_INTERFACE_MODE_XGMII
)
988 if ((!priv
->phy
) || (priv
->phy
->is_c45
))
993 return (HNS_NET_STATS_CNT
+ ops
->get_sset_count(h
, stringset
));
998 * hns_phy_led_set - set phy LED status.
1000 * @value: LED state.
1002 * Return 0 on success, negative on failure.
1004 int hns_phy_led_set(struct net_device
*netdev
, int value
)
1007 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1008 struct phy_device
*phy_dev
= priv
->phy
;
1010 retval
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_LED
);
1011 retval
|= phy_write(phy_dev
, HNS_LED_FC_REG
, value
);
1012 retval
|= phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_COPPER
);
1014 netdev_err(netdev
, "mdiobus_write fail !\n");
1021 * nic_set_phys_id - set phy identify LED.
1023 * @state: LED state.
1025 * Return 0 on success, negative on failure.
1027 int hns_set_phys_id(struct net_device
*netdev
, enum ethtool_phys_id_state state
)
1029 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1030 struct hnae_handle
*h
= priv
->ae_handle
;
1031 struct phy_device
*phy_dev
= priv
->phy
;
1036 case ETHTOOL_ID_ACTIVE
:
1037 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1042 priv
->phy_led_val
= phy_read(phy_dev
, HNS_LED_FC_REG
);
1044 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1045 HNS_PHY_PAGE_COPPER
);
1050 ret
= hns_phy_led_set(netdev
, HNS_LED_FORCE_ON
);
1054 case ETHTOOL_ID_OFF
:
1055 ret
= hns_phy_led_set(netdev
, HNS_LED_FORCE_OFF
);
1059 case ETHTOOL_ID_INACTIVE
:
1060 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1065 ret
= phy_write(phy_dev
, HNS_LED_FC_REG
,
1070 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1071 HNS_PHY_PAGE_COPPER
);
1080 case ETHTOOL_ID_ACTIVE
:
1081 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_ACTIVE
);
1083 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_ON
);
1084 case ETHTOOL_ID_OFF
:
1085 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_OFF
);
1086 case ETHTOOL_ID_INACTIVE
:
1087 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_INACTIVE
);
1096 * hns_get_regs - get net device register
1099 * @date: register data
1101 void hns_get_regs(struct net_device
*net_dev
, struct ethtool_regs
*cmd
,
1104 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
1105 struct hnae_ae_ops
*ops
;
1107 ops
= priv
->ae_handle
->dev
->ops
;
1109 cmd
->version
= HNS_CHIP_VERSION
;
1110 if (!ops
->get_regs
) {
1111 netdev_err(net_dev
, "ops->get_regs is null!\n");
1114 ops
->get_regs(priv
->ae_handle
, data
);
1118 * nic_get_regs_len - get total register len.
1121 * Return total register len.
1123 static int hns_get_regs_len(struct net_device
*net_dev
)
1126 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
1127 struct hnae_ae_ops
*ops
;
1129 ops
= priv
->ae_handle
->dev
->ops
;
1130 if (!ops
->get_regs_len
) {
1131 netdev_err(net_dev
, "ops->get_regs_len is null!\n");
1135 reg_num
= ops
->get_regs_len(priv
->ae_handle
);
1137 return reg_num
* sizeof(u32
);
1139 return reg_num
; /* error code */
1143 * hns_nic_nway_reset - nway reset
1146 * Return 0 on success, negative on failure
1148 static int hns_nic_nway_reset(struct net_device
*netdev
)
1151 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1152 struct phy_device
*phy
= priv
->phy
;
1154 if (netif_running(netdev
)) {
1156 ret
= genphy_restart_aneg(phy
);
1163 hns_get_rss_key_size(struct net_device
*netdev
)
1165 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1166 struct hnae_ae_ops
*ops
;
1168 if (AE_IS_VER1(priv
->enet_ver
)) {
1170 "RSS feature is not supported on this hardware\n");
1174 ops
= priv
->ae_handle
->dev
->ops
;
1175 return ops
->get_rss_key_size(priv
->ae_handle
);
1179 hns_get_rss_indir_size(struct net_device
*netdev
)
1181 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1182 struct hnae_ae_ops
*ops
;
1184 if (AE_IS_VER1(priv
->enet_ver
)) {
1186 "RSS feature is not supported on this hardware\n");
1190 ops
= priv
->ae_handle
->dev
->ops
;
1191 return ops
->get_rss_indir_size(priv
->ae_handle
);
1195 hns_get_rss(struct net_device
*netdev
, u32
*indir
, u8
*key
, u8
*hfunc
)
1197 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1198 struct hnae_ae_ops
*ops
;
1200 if (AE_IS_VER1(priv
->enet_ver
)) {
1202 "RSS feature is not supported on this hardware\n");
1206 ops
= priv
->ae_handle
->dev
->ops
;
1211 return ops
->get_rss(priv
->ae_handle
, indir
, key
, hfunc
);
1215 hns_set_rss(struct net_device
*netdev
, const u32
*indir
, const u8
*key
,
1218 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1219 struct hnae_ae_ops
*ops
;
1221 if (AE_IS_VER1(priv
->enet_ver
)) {
1223 "RSS feature is not supported on this hardware\n");
1227 ops
= priv
->ae_handle
->dev
->ops
;
1229 /* currently hfunc can only be Toeplitz hash */
1231 (hfunc
!= ETH_RSS_HASH_NO_CHANGE
&& hfunc
!= ETH_RSS_HASH_TOP
))
1236 return ops
->set_rss(priv
->ae_handle
, indir
, key
, hfunc
);
1239 static int hns_get_rxnfc(struct net_device
*netdev
,
1240 struct ethtool_rxnfc
*cmd
,
1243 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1246 case ETHTOOL_GRXRINGS
:
1247 cmd
->data
= priv
->ae_handle
->q_num
;
1256 static struct ethtool_ops hns_ethtool_ops
= {
1257 .get_drvinfo
= hns_nic_get_drvinfo
,
1258 .get_link
= hns_nic_get_link
,
1259 .get_settings
= hns_nic_get_settings
,
1260 .set_settings
= hns_nic_set_settings
,
1261 .get_ringparam
= hns_get_ringparam
,
1262 .get_pauseparam
= hns_get_pauseparam
,
1263 .set_pauseparam
= hns_set_pauseparam
,
1264 .get_coalesce
= hns_get_coalesce
,
1265 .set_coalesce
= hns_set_coalesce
,
1266 .get_channels
= hns_get_channels
,
1267 .self_test
= hns_nic_self_test
,
1268 .get_strings
= hns_get_strings
,
1269 .get_sset_count
= hns_get_sset_count
,
1270 .get_ethtool_stats
= hns_get_ethtool_stats
,
1271 .set_phys_id
= hns_set_phys_id
,
1272 .get_regs_len
= hns_get_regs_len
,
1273 .get_regs
= hns_get_regs
,
1274 .nway_reset
= hns_nic_nway_reset
,
1275 .get_rxfh_key_size
= hns_get_rss_key_size
,
1276 .get_rxfh_indir_size
= hns_get_rss_indir_size
,
1277 .get_rxfh
= hns_get_rss
,
1278 .set_rxfh
= hns_set_rss
,
1279 .get_rxnfc
= hns_get_rxnfc
,
1282 void hns_ethtool_set_ops(struct net_device
*ndev
)
1284 ndev
->ethtool_ops
= &hns_ethtool_ops
;