1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2014-2015 Hisilicon Limited.
6 #include <linux/etherdevice.h>
7 #include <linux/interrupt.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
12 #define HNS_PHY_PAGE_MDIX 0
13 #define HNS_PHY_PAGE_LED 3
14 #define HNS_PHY_PAGE_COPPER 0
16 #define HNS_PHY_PAGE_REG 22 /* Page Selection Reg. */
17 #define HNS_PHY_CSC_REG 16 /* Copper Specific Control Register */
18 #define HNS_PHY_CSS_REG 17 /* Copper Specific Status Register */
19 #define HNS_LED_FC_REG 16 /* LED Function Control Reg. */
21 #define HNS_LED_FORCE_ON 9
22 #define HNS_LED_FORCE_OFF 8
24 #define HNS_CHIP_VERSION 660
25 #define HNS_NET_STATS_CNT 26
27 #define PHY_MDIX_CTRL_S (5)
28 #define PHY_MDIX_CTRL_M (3 << PHY_MDIX_CTRL_S)
30 #define PHY_MDIX_STATUS_B (6)
31 #define PHY_SPEED_DUP_RESOLVE_B (11)
34 *hns_nic_get_link - get current link status
36 *retuen 0 - success , negative --fail
38 static u32
hns_nic_get_link(struct net_device
*net_dev
)
40 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
41 u32 link_stat
= priv
->link
;
42 struct hnae_handle
*h
;
46 if (net_dev
->phydev
) {
47 if (!genphy_read_status(net_dev
->phydev
))
48 link_stat
= net_dev
->phydev
->link
;
53 if (h
->dev
&& h
->dev
->ops
&& h
->dev
->ops
->get_status
)
54 link_stat
= link_stat
&& h
->dev
->ops
->get_status(h
);
61 static void hns_get_mdix_mode(struct net_device
*net_dev
,
62 struct ethtool_link_ksettings
*cmd
)
64 int mdix_ctrl
, mdix
, retval
, is_resolved
;
65 struct phy_device
*phy_dev
= net_dev
->phydev
;
67 if (!phy_dev
|| !phy_dev
->mdio
.bus
) {
68 cmd
->base
.eth_tp_mdix_ctrl
= ETH_TP_MDI_INVALID
;
69 cmd
->base
.eth_tp_mdix
= ETH_TP_MDI_INVALID
;
73 phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_MDIX
);
75 retval
= phy_read(phy_dev
, HNS_PHY_CSC_REG
);
76 mdix_ctrl
= hnae_get_field(retval
, PHY_MDIX_CTRL_M
, PHY_MDIX_CTRL_S
);
78 retval
= phy_read(phy_dev
, HNS_PHY_CSS_REG
);
79 mdix
= hnae_get_bit(retval
, PHY_MDIX_STATUS_B
);
80 is_resolved
= hnae_get_bit(retval
, PHY_SPEED_DUP_RESOLVE_B
);
82 phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_COPPER
);
86 cmd
->base
.eth_tp_mdix_ctrl
= ETH_TP_MDI
;
89 cmd
->base
.eth_tp_mdix_ctrl
= ETH_TP_MDI_X
;
92 cmd
->base
.eth_tp_mdix_ctrl
= ETH_TP_MDI_AUTO
;
95 cmd
->base
.eth_tp_mdix_ctrl
= ETH_TP_MDI_INVALID
;
100 cmd
->base
.eth_tp_mdix
= ETH_TP_MDI_INVALID
;
102 cmd
->base
.eth_tp_mdix
= ETH_TP_MDI_X
;
104 cmd
->base
.eth_tp_mdix
= ETH_TP_MDI
;
108 *hns_nic_get_link_ksettings - implement ethtool get link ksettings
109 *@net_dev: net_device
110 *@cmd: ethtool_link_ksettings
111 *retuen 0 - success , negative --fail
113 static int hns_nic_get_link_ksettings(struct net_device
*net_dev
,
114 struct ethtool_link_ksettings
*cmd
)
116 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
117 struct hnae_handle
*h
;
122 u32 supported
, advertising
;
124 if (!priv
|| !priv
->ae_handle
)
128 if (!h
->dev
|| !h
->dev
->ops
|| !h
->dev
->ops
->get_info
)
131 ret
= h
->dev
->ops
->get_info(h
, NULL
, &speed
, &duplex
);
133 netdev_err(net_dev
, "%s get_info error!\n", __func__
);
137 ethtool_convert_link_mode_to_legacy_u32(&supported
,
138 cmd
->link_modes
.supported
);
139 ethtool_convert_link_mode_to_legacy_u32(&advertising
,
140 cmd
->link_modes
.advertising
);
142 /* When there is no phy, autoneg is off. */
143 cmd
->base
.autoneg
= false;
144 cmd
->base
.speed
= speed
;
145 cmd
->base
.duplex
= duplex
;
148 phy_ethtool_ksettings_get(net_dev
->phydev
, cmd
);
150 link_stat
= hns_nic_get_link(net_dev
);
152 cmd
->base
.speed
= (u32
)SPEED_UNKNOWN
;
153 cmd
->base
.duplex
= DUPLEX_UNKNOWN
;
156 if (cmd
->base
.autoneg
)
157 advertising
|= ADVERTISED_Autoneg
;
159 supported
|= h
->if_support
;
160 if (h
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
161 supported
|= SUPPORTED_TP
;
162 advertising
|= ADVERTISED_1000baseT_Full
;
163 } else if (h
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
164 supported
|= SUPPORTED_FIBRE
;
165 advertising
|= ADVERTISED_10000baseKR_Full
;
168 switch (h
->media_type
) {
169 case HNAE_MEDIA_TYPE_FIBER
:
170 cmd
->base
.port
= PORT_FIBRE
;
172 case HNAE_MEDIA_TYPE_COPPER
:
173 cmd
->base
.port
= PORT_TP
;
175 case HNAE_MEDIA_TYPE_UNKNOWN
:
180 if (!(AE_IS_VER1(priv
->enet_ver
) && h
->port_type
== HNAE_PORT_DEBUG
))
181 supported
|= SUPPORTED_Pause
;
183 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.supported
,
185 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.advertising
,
188 cmd
->base
.mdio_support
= ETH_MDIO_SUPPORTS_C45
| ETH_MDIO_SUPPORTS_C22
;
189 hns_get_mdix_mode(net_dev
, cmd
);
195 *hns_nic_set_link_ksettings - implement ethtool set link ksettings
196 *@net_dev: net_device
197 *@cmd: ethtool_link_ksettings
198 *retuen 0 - success , negative --fail
200 static int hns_nic_set_link_ksettings(struct net_device
*net_dev
,
201 const struct ethtool_link_ksettings
*cmd
)
203 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
204 struct hnae_handle
*h
;
207 if (!netif_running(net_dev
))
210 if (!priv
|| !priv
->ae_handle
|| !priv
->ae_handle
->dev
||
211 !priv
->ae_handle
->dev
->ops
)
215 speed
= cmd
->base
.speed
;
217 if (h
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
218 if (cmd
->base
.autoneg
== AUTONEG_ENABLE
||
219 speed
!= SPEED_10000
||
220 cmd
->base
.duplex
!= DUPLEX_FULL
)
222 } else if (h
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
223 if (!net_dev
->phydev
&& cmd
->base
.autoneg
== AUTONEG_ENABLE
)
226 if (speed
== SPEED_1000
&& cmd
->base
.duplex
== DUPLEX_HALF
)
229 return phy_ethtool_ksettings_set(net_dev
->phydev
, cmd
);
231 if ((speed
!= SPEED_10
&& speed
!= SPEED_100
&&
232 speed
!= SPEED_1000
) || (cmd
->base
.duplex
!= DUPLEX_HALF
&&
233 cmd
->base
.duplex
!= DUPLEX_FULL
))
236 netdev_err(net_dev
, "Not supported!");
240 if (h
->dev
->ops
->adjust_link
) {
241 netif_carrier_off(net_dev
);
242 h
->dev
->ops
->adjust_link(h
, (int)speed
, cmd
->base
.duplex
);
243 netif_carrier_on(net_dev
);
247 netdev_err(net_dev
, "Not supported!");
251 static const char hns_nic_test_strs
[][ETH_GSTRING_LEN
] = {
253 "Serdes Loopback test",
257 static int hns_nic_config_phy_loopback(struct phy_device
*phy_dev
, u8 en
)
262 /* Doing phy loopback in offline state, phy resuming is
263 * needed to power up the device.
265 err
= phy_resume(phy_dev
);
269 err
= phy_loopback(phy_dev
, true);
271 err
= phy_loopback(phy_dev
, false);
275 err
= phy_suspend(phy_dev
);
282 static int __lb_setup(struct net_device
*ndev
,
286 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
287 struct phy_device
*phy_dev
= ndev
->phydev
;
288 struct hnae_handle
*h
= priv
->ae_handle
;
291 case MAC_INTERNALLOOP_PHY
:
292 ret
= hns_nic_config_phy_loopback(phy_dev
, 0x1);
294 ret
= h
->dev
->ops
->set_loopback(h
, loop
, 0x1);
296 case MAC_INTERNALLOOP_MAC
:
297 if ((h
->dev
->ops
->set_loopback
) &&
298 (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
))
299 ret
= h
->dev
->ops
->set_loopback(h
, loop
, 0x1);
301 case MAC_INTERNALLOOP_SERDES
:
302 if (h
->dev
->ops
->set_loopback
)
303 ret
= h
->dev
->ops
->set_loopback(h
, loop
, 0x1);
305 case MAC_LOOP_PHY_NONE
:
306 ret
= hns_nic_config_phy_loopback(phy_dev
, 0x0);
309 if (!ret
&& h
->dev
->ops
->set_loopback
) {
310 if (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
)
311 ret
= h
->dev
->ops
->set_loopback(h
,
312 MAC_INTERNALLOOP_MAC
, 0x0);
315 ret
= h
->dev
->ops
->set_loopback(h
,
316 MAC_INTERNALLOOP_SERDES
, 0x0);
325 if (loop
== MAC_LOOP_NONE
)
326 h
->dev
->ops
->set_promisc_mode(
327 h
, ndev
->flags
& IFF_PROMISC
);
329 h
->dev
->ops
->set_promisc_mode(h
, 1);
334 static int __lb_up(struct net_device
*ndev
,
335 enum hnae_loop loop_mode
)
337 #define NIC_LB_TEST_WAIT_PHY_LINK_TIME 300
338 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
339 struct hnae_handle
*h
= priv
->ae_handle
;
343 hns_nic_net_reset(ndev
);
345 ret
= __lb_setup(ndev
, loop_mode
);
351 ret
= h
->dev
->ops
->start
? h
->dev
->ops
->start(h
) : 0;
355 /* link adjust duplex*/
356 if (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
)
362 h
->dev
->ops
->adjust_link(h
, speed
, duplex
);
364 /* wait adjust link done and phy ready */
365 msleep(NIC_LB_TEST_WAIT_PHY_LINK_TIME
);
370 static void __lb_other_process(struct hns_nic_ring_data
*ring_data
,
373 struct net_device
*ndev
;
374 struct hns_nic_priv
*priv
;
375 struct hnae_ring
*ring
;
376 struct netdev_queue
*dev_queue
;
377 struct sk_buff
*new_skb
;
378 unsigned int frame_size
;
381 char buff
[33]; /* 32B data and the last character '\0' */
383 if (!ring_data
) { /* Just for doing create frame*/
385 priv
= netdev_priv(ndev
);
387 frame_size
= skb
->len
;
388 memset(skb
->data
, 0xFF, frame_size
);
389 if ((!AE_IS_VER1(priv
->enet_ver
)) &&
390 (priv
->ae_handle
->port_type
== HNAE_PORT_SERVICE
)) {
391 memcpy(skb
->data
, ndev
->dev_addr
, 6);
392 skb
->data
[5] += 0x1f;
396 memset(&skb
->data
[frame_size
/ 2], 0xAA, frame_size
/ 2 - 1);
397 memset(&skb
->data
[frame_size
/ 2 + 10], 0xBE,
398 frame_size
/ 2 - 11);
399 memset(&skb
->data
[frame_size
/ 2 + 12], 0xAF,
400 frame_size
/ 2 - 13);
404 ring
= ring_data
->ring
;
405 ndev
= ring_data
->napi
.dev
;
406 if (is_tx_ring(ring
)) { /* for tx queue reset*/
407 dev_queue
= netdev_get_tx_queue(ndev
, ring_data
->queue_index
);
408 netdev_tx_reset_queue(dev_queue
);
412 frame_size
= skb
->len
;
415 new_skb
= skb_copy(skb
, GFP_ATOMIC
);
416 dev_kfree_skb_any(skb
);
418 netdev_err(ndev
, "skb alloc failed\n");
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))
431 ndev
->stats
.rx_packets
++;
432 ndev
->stats
.rx_bytes
+= skb
->len
;
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
);
442 dev_kfree_skb_any(skb
);
445 static int __lb_clean_rings(struct hns_nic_priv
*priv
,
446 int ringid0
, int ringid1
, int budget
)
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
;
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
);
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
;
468 * __lb_run_test - run loopback test
470 * @loop_mode: loopback mode
472 static int __lb_run_test(struct net_device
*ndev
,
473 enum hnae_loop loop_mode
)
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
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;
487 netdev_tx_t tx_ret_val
;
490 size
= NIC_LB_TEST_FRAME_SIZE
;
491 /* allocate test skb */
492 skb
= alloc_skb(size
, GFP_KERNEL
);
494 return NIC_LB_TEST_NO_MEM_ERR
;
496 /* place data into test skb */
497 (void)skb_put(skb
, size
);
499 __lb_other_process(NULL
, skb
);
500 skb
->queue_mapping
= NIC_LB_TEST_RING_ID
;
503 for (j
= 0; j
< lc
; j
++) {
504 /* reset count of good packets */
506 /* place 64 packets on the transmit queue*/
507 for (i
= 0; i
< NIC_LB_TEST_PKT_NUM_PER_CYCLE
; i
++) {
510 tx_ret_val
= (netdev_tx_t
)hns_nic_net_xmit_hw(
512 &tx_ring_data(priv
, skb
->queue_mapping
));
513 if (tx_ret_val
== NETDEV_TX_OK
)
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
);
526 /* allow 100 milliseconds for packets to go from Tx to Rx */
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
);
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
);
544 /* free the original skb */
550 static int __lb_down(struct net_device
*ndev
, enum hnae_loop loop
)
552 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
553 struct hnae_handle
*h
= priv
->ae_handle
;
556 if (loop
== MAC_INTERNALLOOP_PHY
)
557 ret
= __lb_setup(ndev
, MAC_LOOP_PHY_NONE
);
559 ret
= __lb_setup(ndev
, MAC_LOOP_NONE
);
561 netdev_err(ndev
, "%s: __lb_setup return error(%d)!\n",
565 if (h
->dev
->ops
->stop
)
566 h
->dev
->ops
->stop(h
);
568 usleep_range(10000, 20000);
569 (void)__lb_clean_rings(priv
, 0, h
->q_num
- 1, 256);
571 hns_nic_net_reset(ndev
);
577 * hns_nic_self_test - self test
579 * @eth_test: test cmd
582 static void hns_nic_self_test(struct net_device
*ndev
,
583 struct ethtool_test
*eth_test
, u64
*data
)
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];
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
));
600 if (eth_test
->flags
== ETH_TEST_FL_OFFLINE
) {
601 set_bit(NIC_STATE_TESTING
, &priv
->state
);
606 for (i
= 0; i
< SELF_TEST_TPYE_NUM
; i
++) {
608 continue; /* NEXT testing */
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]);
619 if (data
[test_index
])
620 eth_test
->flags
|= ETH_TEST_FL_FAILED
;
625 hns_nic_net_reset(priv
->netdev
);
627 clear_bit(NIC_STATE_TESTING
, &priv
->state
);
630 (void)dev_open(ndev
, NULL
);
632 /* Online tests aren't run; pass by default */
634 (void)msleep_interruptible(4 * 1000);
638 * hns_nic_get_drvinfo - get net driver info
639 * @net_dev: net device
640 * @drvinfo: driver info
642 static void hns_nic_get_drvinfo(struct net_device
*net_dev
,
643 struct ethtool_drvinfo
*drvinfo
)
645 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
647 strscpy(drvinfo
->version
, HNAE_DRIVER_VERSION
,
648 sizeof(drvinfo
->version
));
650 strscpy(drvinfo
->driver
, HNAE_DRIVER_NAME
, sizeof(drvinfo
->driver
));
652 strscpy(drvinfo
->bus_info
, priv
->dev
->bus
->name
,
653 sizeof(drvinfo
->bus_info
));
655 strscpy(drvinfo
->fw_version
, "N/A", ETHTOOL_FWVERS_LEN
);
656 drvinfo
->eedump_len
= 0;
660 * hns_get_ringparam - get ring parameter
661 * @net_dev: net device
662 * @param: ethtool parameter
663 * @kernel_param: ethtool external parameter
664 * @extack: netlink extended ACK report struct
666 static void hns_get_ringparam(struct net_device
*net_dev
,
667 struct ethtool_ringparam
*param
,
668 struct kernel_ethtool_ringparam
*kernel_param
,
669 struct netlink_ext_ack
*extack
)
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
690 * @net_dev: net device
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
708 * @net_dev: net device
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.
732 * @net_dev: net device
733 * @ec: coalesce info.
734 * @kernel_coal: ethtool CQE mode setting structure
735 * @extack: extack for reporting error messages
737 * Return 0 on success, negative on failure.
739 static int hns_get_coalesce(struct net_device
*net_dev
,
740 struct ethtool_coalesce
*ec
,
741 struct kernel_ethtool_coalesce
*kernel_coal
,
742 struct netlink_ext_ack
*extack
)
744 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
745 struct hnae_ae_ops
*ops
;
747 ops
= priv
->ae_handle
->dev
->ops
;
749 ec
->use_adaptive_rx_coalesce
= priv
->ae_handle
->coal_adapt_en
;
750 ec
->use_adaptive_tx_coalesce
= priv
->ae_handle
->coal_adapt_en
;
752 if ((!ops
->get_coalesce_usecs
) ||
753 (!ops
->get_max_coalesced_frames
))
756 ops
->get_coalesce_usecs(priv
->ae_handle
,
757 &ec
->tx_coalesce_usecs
,
758 &ec
->rx_coalesce_usecs
);
760 ops
->get_max_coalesced_frames(
762 &ec
->tx_max_coalesced_frames
,
763 &ec
->rx_max_coalesced_frames
);
765 ops
->get_coalesce_range(priv
->ae_handle
,
766 &ec
->tx_max_coalesced_frames_low
,
767 &ec
->rx_max_coalesced_frames_low
,
768 &ec
->tx_max_coalesced_frames_high
,
769 &ec
->rx_max_coalesced_frames_high
,
770 &ec
->tx_coalesce_usecs_low
,
771 &ec
->rx_coalesce_usecs_low
,
772 &ec
->tx_coalesce_usecs_high
,
773 &ec
->rx_coalesce_usecs_high
);
779 * hns_set_coalesce - set coalesce info.
780 * @net_dev: net device
781 * @ec: coalesce info.
782 * @kernel_coal: ethtool CQE mode setting structure
783 * @extack: extack for reporting error messages
785 * Return 0 on success, negative on failure.
787 static int hns_set_coalesce(struct net_device
*net_dev
,
788 struct ethtool_coalesce
*ec
,
789 struct kernel_ethtool_coalesce
*kernel_coal
,
790 struct netlink_ext_ack
*extack
)
792 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
793 struct hnae_ae_ops
*ops
;
796 ops
= priv
->ae_handle
->dev
->ops
;
798 if (ec
->tx_coalesce_usecs
!= ec
->rx_coalesce_usecs
)
801 if ((!ops
->set_coalesce_usecs
) ||
802 (!ops
->set_coalesce_frames
))
805 if (ec
->use_adaptive_rx_coalesce
!= priv
->ae_handle
->coal_adapt_en
)
806 priv
->ae_handle
->coal_adapt_en
= ec
->use_adaptive_rx_coalesce
;
808 rc1
= ops
->set_coalesce_usecs(priv
->ae_handle
,
809 ec
->rx_coalesce_usecs
);
811 rc2
= ops
->set_coalesce_frames(priv
->ae_handle
,
812 ec
->tx_max_coalesced_frames
,
813 ec
->rx_max_coalesced_frames
);
822 * hns_get_channels - get channel info.
823 * @net_dev: net device
827 hns_get_channels(struct net_device
*net_dev
, struct ethtool_channels
*ch
)
829 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
831 ch
->max_rx
= priv
->ae_handle
->q_num
;
832 ch
->max_tx
= priv
->ae_handle
->q_num
;
834 ch
->rx_count
= priv
->ae_handle
->q_num
;
835 ch
->tx_count
= priv
->ae_handle
->q_num
;
839 * hns_get_ethtool_stats - get detail statistics.
840 * @netdev: net device
841 * @stats: statistics info.
842 * @data: statistics data.
844 static void hns_get_ethtool_stats(struct net_device
*netdev
,
845 struct ethtool_stats
*stats
, u64
*data
)
848 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
849 struct hnae_handle
*h
= priv
->ae_handle
;
850 const struct rtnl_link_stats64
*net_stats
;
851 struct rtnl_link_stats64 temp
;
853 if (!h
->dev
->ops
->get_stats
|| !h
->dev
->ops
->update_stats
) {
854 netdev_err(netdev
, "get_stats or update_stats is null!\n");
858 h
->dev
->ops
->update_stats(h
, &netdev
->stats
);
860 net_stats
= dev_get_stats(netdev
, &temp
);
862 /* get netdev statistics */
863 p
[0] = net_stats
->rx_packets
;
864 p
[1] = net_stats
->tx_packets
;
865 p
[2] = net_stats
->rx_bytes
;
866 p
[3] = net_stats
->tx_bytes
;
867 p
[4] = net_stats
->rx_errors
;
868 p
[5] = net_stats
->tx_errors
;
869 p
[6] = net_stats
->rx_dropped
;
870 p
[7] = net_stats
->tx_dropped
;
871 p
[8] = net_stats
->multicast
;
872 p
[9] = net_stats
->collisions
;
873 p
[10] = net_stats
->rx_over_errors
;
874 p
[11] = net_stats
->rx_crc_errors
;
875 p
[12] = net_stats
->rx_frame_errors
;
876 p
[13] = net_stats
->rx_fifo_errors
;
877 p
[14] = net_stats
->rx_missed_errors
;
878 p
[15] = net_stats
->tx_aborted_errors
;
879 p
[16] = net_stats
->tx_carrier_errors
;
880 p
[17] = net_stats
->tx_fifo_errors
;
881 p
[18] = net_stats
->tx_heartbeat_errors
;
882 p
[19] = net_stats
->rx_length_errors
;
883 p
[20] = net_stats
->tx_window_errors
;
884 p
[21] = net_stats
->rx_compressed
;
885 p
[22] = net_stats
->tx_compressed
;
887 p
[23] = 0; /* was netdev->rx_dropped.counter */
888 p
[24] = 0; /* was netdev->tx_dropped.counter */
890 p
[25] = priv
->tx_timeout_count
;
892 /* get driver statistics */
893 h
->dev
->ops
->get_stats(h
, &p
[26]);
897 * hns_get_strings: Return a set of strings that describe the requested objects
898 * @netdev: net device
899 * @stringset: string set ID.
900 * @data: objects data.
902 static void hns_get_strings(struct net_device
*netdev
, u32 stringset
, u8
*data
)
904 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
905 struct hnae_handle
*h
= priv
->ae_handle
;
907 if (!h
->dev
->ops
->get_strings
) {
908 netdev_err(netdev
, "h->dev->ops->get_strings is null!\n");
912 if (stringset
== ETH_SS_TEST
) {
913 if (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
)
915 hns_nic_test_strs
[MAC_INTERNALLOOP_MAC
]);
916 ethtool_puts(&data
, hns_nic_test_strs
[MAC_INTERNALLOOP_SERDES
]);
917 if ((netdev
->phydev
) && (!netdev
->phydev
->is_c45
))
919 hns_nic_test_strs
[MAC_INTERNALLOOP_PHY
]);
922 ethtool_puts(&data
, "rx_packets");
923 ethtool_puts(&data
, "tx_packets");
924 ethtool_puts(&data
, "rx_bytes");
925 ethtool_puts(&data
, "tx_bytes");
926 ethtool_puts(&data
, "rx_errors");
927 ethtool_puts(&data
, "tx_errors");
928 ethtool_puts(&data
, "rx_dropped");
929 ethtool_puts(&data
, "tx_dropped");
930 ethtool_puts(&data
, "multicast");
931 ethtool_puts(&data
, "collisions");
932 ethtool_puts(&data
, "rx_over_errors");
933 ethtool_puts(&data
, "rx_crc_errors");
934 ethtool_puts(&data
, "rx_frame_errors");
935 ethtool_puts(&data
, "rx_fifo_errors");
936 ethtool_puts(&data
, "rx_missed_errors");
937 ethtool_puts(&data
, "tx_aborted_errors");
938 ethtool_puts(&data
, "tx_carrier_errors");
939 ethtool_puts(&data
, "tx_fifo_errors");
940 ethtool_puts(&data
, "tx_heartbeat_errors");
941 ethtool_puts(&data
, "rx_length_errors");
942 ethtool_puts(&data
, "tx_window_errors");
943 ethtool_puts(&data
, "rx_compressed");
944 ethtool_puts(&data
, "tx_compressed");
945 ethtool_puts(&data
, "netdev_rx_dropped");
946 ethtool_puts(&data
, "netdev_tx_dropped");
948 ethtool_puts(&data
, "netdev_tx_timeout");
950 h
->dev
->ops
->get_strings(h
, stringset
, &data
);
955 * hns_get_sset_count - get string set count returned by nic_get_strings
956 * @netdev: net device
957 * @stringset: string set index, 0: self test string; 1: statistics string.
959 * Return string set count.
961 static int hns_get_sset_count(struct net_device
*netdev
, int stringset
)
963 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
964 struct hnae_handle
*h
= priv
->ae_handle
;
965 struct hnae_ae_ops
*ops
= h
->dev
->ops
;
967 if (!ops
->get_sset_count
) {
968 netdev_err(netdev
, "get_sset_count is null!\n");
971 if (stringset
== ETH_SS_TEST
) {
972 u32 cnt
= ARRAY_SIZE(hns_nic_test_strs
);
974 if (priv
->ae_handle
->phy_if
== PHY_INTERFACE_MODE_XGMII
)
977 if ((!netdev
->phydev
) || (netdev
->phydev
->is_c45
))
981 } else if (stringset
== ETH_SS_STATS
) {
982 return (HNS_NET_STATS_CNT
+ ops
->get_sset_count(h
, stringset
));
989 * hns_phy_led_set - set phy LED status.
990 * @netdev: net device
993 * Return 0 on success, negative on failure.
995 static int hns_phy_led_set(struct net_device
*netdev
, int value
)
998 struct phy_device
*phy_dev
= netdev
->phydev
;
1000 retval
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_LED
);
1001 retval
|= phy_write(phy_dev
, HNS_LED_FC_REG
, value
);
1002 retval
|= phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_COPPER
);
1004 netdev_err(netdev
, "mdiobus_write fail !\n");
1011 * hns_set_phys_id - set phy identify LED.
1012 * @netdev: net device
1013 * @state: LED state.
1015 * Return 0 on success, negative on failure.
1018 hns_set_phys_id(struct net_device
*netdev
, enum ethtool_phys_id_state state
)
1020 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1021 struct hnae_handle
*h
= priv
->ae_handle
;
1022 struct phy_device
*phy_dev
= netdev
->phydev
;
1027 case ETHTOOL_ID_ACTIVE
:
1028 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1033 priv
->phy_led_val
= phy_read(phy_dev
, HNS_LED_FC_REG
);
1035 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1036 HNS_PHY_PAGE_COPPER
);
1041 ret
= hns_phy_led_set(netdev
, HNS_LED_FORCE_ON
);
1045 case ETHTOOL_ID_OFF
:
1046 ret
= hns_phy_led_set(netdev
, HNS_LED_FORCE_OFF
);
1050 case ETHTOOL_ID_INACTIVE
:
1051 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1056 ret
= phy_write(phy_dev
, HNS_LED_FC_REG
,
1061 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1062 HNS_PHY_PAGE_COPPER
);
1071 case ETHTOOL_ID_ACTIVE
:
1072 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_ACTIVE
);
1074 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_ON
);
1075 case ETHTOOL_ID_OFF
:
1076 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_OFF
);
1077 case ETHTOOL_ID_INACTIVE
:
1078 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_INACTIVE
);
1087 * hns_get_regs - get net device register
1088 * @net_dev: net device
1090 * @data: register data
1092 static void hns_get_regs(struct net_device
*net_dev
, struct ethtool_regs
*cmd
,
1095 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
1096 struct hnae_ae_ops
*ops
;
1098 ops
= priv
->ae_handle
->dev
->ops
;
1100 cmd
->version
= HNS_CHIP_VERSION
;
1101 if (!ops
->get_regs
) {
1102 netdev_err(net_dev
, "ops->get_regs is null!\n");
1105 ops
->get_regs(priv
->ae_handle
, data
);
1109 * hns_get_regs_len - get total register len.
1110 * @net_dev: net device
1112 * Return total register len.
1114 static int hns_get_regs_len(struct net_device
*net_dev
)
1117 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
1118 struct hnae_ae_ops
*ops
;
1120 ops
= priv
->ae_handle
->dev
->ops
;
1121 if (!ops
->get_regs_len
) {
1122 netdev_err(net_dev
, "ops->get_regs_len is null!\n");
1126 reg_num
= ops
->get_regs_len(priv
->ae_handle
);
1128 return reg_num
* sizeof(u32
);
1130 return reg_num
; /* error code */
1134 * hns_nic_nway_reset - nway reset
1135 * @netdev: net device
1137 * Return 0 on success, negative on failure
1139 static int hns_nic_nway_reset(struct net_device
*netdev
)
1141 struct phy_device
*phy
= netdev
->phydev
;
1143 if (!netif_running(netdev
))
1149 if (phy
->autoneg
!= AUTONEG_ENABLE
)
1152 return genphy_restart_aneg(phy
);
1156 hns_get_rss_key_size(struct net_device
*netdev
)
1158 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1159 struct hnae_ae_ops
*ops
;
1161 if (AE_IS_VER1(priv
->enet_ver
)) {
1163 "RSS feature is not supported on this hardware\n");
1167 ops
= priv
->ae_handle
->dev
->ops
;
1168 return ops
->get_rss_key_size(priv
->ae_handle
);
1172 hns_get_rss_indir_size(struct net_device
*netdev
)
1174 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1175 struct hnae_ae_ops
*ops
;
1177 if (AE_IS_VER1(priv
->enet_ver
)) {
1179 "RSS feature is not supported on this hardware\n");
1183 ops
= priv
->ae_handle
->dev
->ops
;
1184 return ops
->get_rss_indir_size(priv
->ae_handle
);
1188 hns_get_rss(struct net_device
*netdev
, struct ethtool_rxfh_param
*rxfh
)
1190 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1191 struct hnae_ae_ops
*ops
;
1193 if (AE_IS_VER1(priv
->enet_ver
)) {
1195 "RSS feature is not supported on this hardware\n");
1199 ops
= priv
->ae_handle
->dev
->ops
;
1204 return ops
->get_rss(priv
->ae_handle
,
1205 rxfh
->indir
, rxfh
->key
, &rxfh
->hfunc
);
1209 hns_set_rss(struct net_device
*netdev
, struct ethtool_rxfh_param
*rxfh
,
1210 struct netlink_ext_ack
*extack
)
1212 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1213 struct hnae_ae_ops
*ops
;
1215 if (AE_IS_VER1(priv
->enet_ver
)) {
1217 "RSS feature is not supported on this hardware\n");
1221 ops
= priv
->ae_handle
->dev
->ops
;
1223 if (rxfh
->hfunc
!= ETH_RSS_HASH_NO_CHANGE
&&
1224 rxfh
->hfunc
!= ETH_RSS_HASH_TOP
) {
1225 netdev_err(netdev
, "Invalid hfunc!\n");
1229 return ops
->set_rss(priv
->ae_handle
,
1230 rxfh
->indir
, rxfh
->key
, rxfh
->hfunc
);
1233 static int hns_get_rxnfc(struct net_device
*netdev
,
1234 struct ethtool_rxnfc
*cmd
,
1237 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1240 case ETHTOOL_GRXRINGS
:
1241 cmd
->data
= priv
->ae_handle
->q_num
;
1250 static const struct ethtool_ops hns_ethtool_ops
= {
1251 .supported_coalesce_params
= ETHTOOL_COALESCE_USECS
|
1252 ETHTOOL_COALESCE_MAX_FRAMES
|
1253 ETHTOOL_COALESCE_USE_ADAPTIVE
|
1254 ETHTOOL_COALESCE_USECS_LOW_HIGH
|
1255 ETHTOOL_COALESCE_MAX_FRAMES_LOW_HIGH
,
1256 .get_drvinfo
= hns_nic_get_drvinfo
,
1257 .get_link
= hns_nic_get_link
,
1258 .get_ringparam
= hns_get_ringparam
,
1259 .get_pauseparam
= hns_get_pauseparam
,
1260 .set_pauseparam
= hns_set_pauseparam
,
1261 .get_coalesce
= hns_get_coalesce
,
1262 .set_coalesce
= hns_set_coalesce
,
1263 .get_channels
= hns_get_channels
,
1264 .self_test
= hns_nic_self_test
,
1265 .get_strings
= hns_get_strings
,
1266 .get_sset_count
= hns_get_sset_count
,
1267 .get_ethtool_stats
= hns_get_ethtool_stats
,
1268 .set_phys_id
= hns_set_phys_id
,
1269 .get_regs_len
= hns_get_regs_len
,
1270 .get_regs
= hns_get_regs
,
1271 .nway_reset
= hns_nic_nway_reset
,
1272 .get_rxfh_key_size
= hns_get_rss_key_size
,
1273 .get_rxfh_indir_size
= hns_get_rss_indir_size
,
1274 .get_rxfh
= hns_get_rss
,
1275 .set_rxfh
= hns_set_rss
,
1276 .get_rxnfc
= hns_get_rxnfc
,
1277 .get_link_ksettings
= hns_nic_get_link_ksettings
,
1278 .set_link_ksettings
= hns_nic_set_link_ksettings
,
1281 void hns_ethtool_set_ops(struct net_device
*ndev
)
1283 ndev
->ethtool_ops
= &hns_ethtool_ops
;