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
;
49 assert(priv
&& priv
->ae_handle
);
53 if (!genphy_update_link(priv
->phy
))
54 link_stat
= priv
->phy
->link
;
59 if (h
->dev
&& h
->dev
->ops
&& h
->dev
->ops
->get_status
)
60 link_stat
= link_stat
&& h
->dev
->ops
->get_status(h
);
67 static void hns_get_mdix_mode(struct net_device
*net_dev
,
68 struct ethtool_cmd
*cmd
)
70 int mdix_ctrl
, mdix
, retval
, is_resolved
;
71 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
72 struct phy_device
*phy_dev
= priv
->phy
;
74 if (!phy_dev
|| !phy_dev
->mdio
.bus
) {
75 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI_INVALID
;
76 cmd
->eth_tp_mdix
= ETH_TP_MDI_INVALID
;
80 phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_MDIX
);
82 retval
= phy_read(phy_dev
, HNS_PHY_CSC_REG
);
83 mdix_ctrl
= hnae_get_field(retval
, PHY_MDIX_CTRL_M
, PHY_MDIX_CTRL_S
);
85 retval
= phy_read(phy_dev
, HNS_PHY_CSS_REG
);
86 mdix
= hnae_get_bit(retval
, PHY_MDIX_STATUS_B
);
87 is_resolved
= hnae_get_bit(retval
, PHY_SPEED_DUP_RESOLVE_B
);
89 phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_COPPER
);
93 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI
;
96 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI_X
;
99 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI_AUTO
;
102 cmd
->eth_tp_mdix_ctrl
= ETH_TP_MDI_INVALID
;
107 cmd
->eth_tp_mdix
= ETH_TP_MDI_INVALID
;
109 cmd
->eth_tp_mdix
= ETH_TP_MDI_X
;
111 cmd
->eth_tp_mdix
= ETH_TP_MDI
;
115 *hns_nic_get_settings - implement ethtool get settings
116 *@net_dev: net_device
118 *retuen 0 - success , negative --fail
120 static int hns_nic_get_settings(struct net_device
*net_dev
,
121 struct ethtool_cmd
*cmd
)
123 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
124 struct hnae_handle
*h
;
130 if (!priv
|| !priv
->ae_handle
)
134 if (!h
->dev
|| !h
->dev
->ops
|| !h
->dev
->ops
->get_info
)
137 ret
= h
->dev
->ops
->get_info(h
, NULL
, &speed
, &duplex
);
139 netdev_err(net_dev
, "%s get_info error!\n", __func__
);
143 /* When there is no phy, autoneg is off. */
144 cmd
->autoneg
= false;
145 ethtool_cmd_speed_set(cmd
, speed
);
146 cmd
->duplex
= duplex
;
149 (void)phy_ethtool_gset(priv
->phy
, cmd
);
151 link_stat
= hns_nic_get_link(net_dev
);
153 ethtool_cmd_speed_set(cmd
, (u32
)SPEED_UNKNOWN
);
154 cmd
->duplex
= DUPLEX_UNKNOWN
;
158 cmd
->advertising
|= ADVERTISED_Autoneg
;
160 cmd
->supported
|= h
->if_support
;
161 if (h
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
162 cmd
->supported
|= SUPPORTED_TP
;
163 cmd
->advertising
|= ADVERTISED_1000baseT_Full
;
164 } else if (h
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
165 cmd
->supported
|= SUPPORTED_FIBRE
;
166 cmd
->advertising
|= ADVERTISED_10000baseKR_Full
;
169 if (h
->port_type
== HNAE_PORT_SERVICE
) {
170 cmd
->port
= PORT_FIBRE
;
171 cmd
->supported
|= SUPPORTED_Pause
;
176 cmd
->transceiver
= XCVR_EXTERNAL
;
177 cmd
->mdio_support
= (ETH_MDIO_SUPPORTS_C45
| ETH_MDIO_SUPPORTS_C22
);
178 hns_get_mdix_mode(net_dev
, cmd
);
184 *hns_nic_set_settings - implement ethtool set settings
185 *@net_dev: net_device
187 *retuen 0 - success , negative --fail
189 static int hns_nic_set_settings(struct net_device
*net_dev
,
190 struct ethtool_cmd
*cmd
)
192 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
193 struct hnae_handle
*h
;
196 if (!netif_running(net_dev
))
199 if (!priv
|| !priv
->ae_handle
|| !priv
->ae_handle
->dev
||
200 !priv
->ae_handle
->dev
->ops
)
204 speed
= ethtool_cmd_speed(cmd
);
206 if (h
->phy_if
== PHY_INTERFACE_MODE_XGMII
) {
207 if (cmd
->autoneg
== AUTONEG_ENABLE
|| speed
!= SPEED_10000
||
208 cmd
->duplex
!= DUPLEX_FULL
)
210 } else if (h
->phy_if
== PHY_INTERFACE_MODE_SGMII
) {
211 if (!priv
->phy
&& cmd
->autoneg
== AUTONEG_ENABLE
)
214 if (speed
== SPEED_1000
&& cmd
->duplex
== DUPLEX_HALF
)
217 return phy_ethtool_sset(priv
->phy
, cmd
);
219 if ((speed
!= SPEED_10
&& speed
!= SPEED_100
&&
220 speed
!= SPEED_1000
) || (cmd
->duplex
!= DUPLEX_HALF
&&
221 cmd
->duplex
!= DUPLEX_FULL
))
224 netdev_err(net_dev
, "Not supported!");
228 if (h
->dev
->ops
->adjust_link
) {
229 h
->dev
->ops
->adjust_link(h
, (int)speed
, cmd
->duplex
);
233 netdev_err(net_dev
, "Not supported!");
237 static const char hns_nic_test_strs
[][ETH_GSTRING_LEN
] = {
239 "Serdes Loopback test",
243 static int hns_nic_config_phy_loopback(struct phy_device
*phy_dev
, u8 en
)
245 #define COPPER_CONTROL_REG 0
246 #define PHY_LOOP_BACK BIT(14)
249 if (phy_dev
->is_c45
) /* c45 branch adding for XGE PHY */
254 phy_write(phy_dev
, HNS_PHY_PAGE_REG
, 2);
255 phy_write(phy_dev
, 21, 0x1046);
257 phy_write(phy_dev
, 9, 0x1F00);
259 phy_write(phy_dev
, 0, 0x9140);
260 /* If autoneg disabled,two soft-reset operations */
261 phy_write(phy_dev
, 0, 0x9140);
262 phy_write(phy_dev
, 22, 0xFA);
264 /* Default is 0x0400 */
265 phy_write(phy_dev
, 1, 0x418);
267 /* Force 1000M Link, Default is 0x0200 */
268 phy_write(phy_dev
, 7, 0x20C);
269 phy_write(phy_dev
, 22, 0);
271 /* Enable MAC loop-back */
272 val
= phy_read(phy_dev
, COPPER_CONTROL_REG
);
273 val
|= PHY_LOOP_BACK
;
274 phy_write(phy_dev
, COPPER_CONTROL_REG
, val
);
276 phy_write(phy_dev
, 22, 0xFA);
277 phy_write(phy_dev
, 1, 0x400);
278 phy_write(phy_dev
, 7, 0x200);
279 phy_write(phy_dev
, 22, 0);
281 val
= phy_read(phy_dev
, COPPER_CONTROL_REG
);
282 val
&= ~PHY_LOOP_BACK
;
283 phy_write(phy_dev
, COPPER_CONTROL_REG
, val
);
288 static int __lb_setup(struct net_device
*ndev
,
292 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
293 struct phy_device
*phy_dev
= priv
->phy
;
294 struct hnae_handle
*h
= priv
->ae_handle
;
297 case MAC_INTERNALLOOP_PHY
:
298 if ((phy_dev
) && (!phy_dev
->is_c45
)) {
299 ret
= hns_nic_config_phy_loopback(phy_dev
, 0x1);
300 ret
|= h
->dev
->ops
->set_loopback(h
, loop
, 0x1);
303 case MAC_INTERNALLOOP_MAC
:
304 if ((h
->dev
->ops
->set_loopback
) &&
305 (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
))
306 ret
= h
->dev
->ops
->set_loopback(h
, loop
, 0x1);
308 case MAC_INTERNALLOOP_SERDES
:
309 if (h
->dev
->ops
->set_loopback
)
310 ret
= h
->dev
->ops
->set_loopback(h
, loop
, 0x1);
313 if ((phy_dev
) && (!phy_dev
->is_c45
))
314 ret
|= hns_nic_config_phy_loopback(phy_dev
, 0x0);
316 if (h
->dev
->ops
->set_loopback
) {
317 if (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
)
318 ret
|= h
->dev
->ops
->set_loopback(h
,
319 MAC_INTERNALLOOP_MAC
, 0x0);
321 ret
|= h
->dev
->ops
->set_loopback(h
,
322 MAC_INTERNALLOOP_SERDES
, 0x0);
333 static int __lb_up(struct net_device
*ndev
,
334 enum hnae_loop loop_mode
)
336 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
337 struct hnae_handle
*h
= priv
->ae_handle
;
341 hns_nic_net_reset(ndev
);
344 phy_disconnect(priv
->phy
);
347 ret
= hns_nic_init_phy(ndev
, h
);
352 ret
= __lb_setup(ndev
, loop_mode
);
358 ret
= h
->dev
->ops
->start
? h
->dev
->ops
->start(h
) : 0;
363 phy_start(priv
->phy
);
365 /* link adjust duplex*/
366 if (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
)
372 h
->dev
->ops
->adjust_link(h
, speed
, duplex
);
377 static void __lb_other_process(struct hns_nic_ring_data
*ring_data
,
380 struct net_device
*ndev
;
381 struct hns_nic_priv
*priv
;
382 struct hnae_ring
*ring
;
383 struct netdev_queue
*dev_queue
;
384 struct sk_buff
*new_skb
;
385 unsigned int frame_size
;
388 char buff
[33]; /* 32B data and the last character '\0' */
390 if (!ring_data
) { /* Just for doing create frame*/
392 priv
= netdev_priv(ndev
);
394 frame_size
= skb
->len
;
395 memset(skb
->data
, 0xFF, frame_size
);
396 if ((!AE_IS_VER1(priv
->enet_ver
)) &&
397 (priv
->ae_handle
->port_type
== HNAE_PORT_SERVICE
)) {
398 memcpy(skb
->data
, ndev
->dev_addr
, 6);
399 skb
->data
[5] += 0x1f;
403 memset(&skb
->data
[frame_size
/ 2], 0xAA, frame_size
/ 2 - 1);
404 memset(&skb
->data
[frame_size
/ 2 + 10], 0xBE,
405 frame_size
/ 2 - 11);
406 memset(&skb
->data
[frame_size
/ 2 + 12], 0xAF,
407 frame_size
/ 2 - 13);
411 ring
= ring_data
->ring
;
412 ndev
= ring_data
->napi
.dev
;
413 if (is_tx_ring(ring
)) { /* for tx queue reset*/
414 dev_queue
= netdev_get_tx_queue(ndev
, ring_data
->queue_index
);
415 netdev_tx_reset_queue(dev_queue
);
419 frame_size
= skb
->len
;
422 new_skb
= skb_copy(skb
, GFP_ATOMIC
);
423 dev_kfree_skb_any(skb
);
427 if (*(skb
->data
+ 10) == 0xFF) { /* for rx check frame*/
428 if ((*(skb
->data
+ frame_size
/ 2 + 10) == 0xBE) &&
429 (*(skb
->data
+ frame_size
/ 2 + 12) == 0xAF))
434 ndev
->stats
.rx_packets
++;
435 ndev
->stats
.rx_bytes
+= skb
->len
;
437 ndev
->stats
.rx_frame_errors
++;
438 for (i
= 0; i
< skb
->len
; i
++) {
439 snprintf(buff
+ i
% 16 * 2, 3, /* tailing \0*/
440 "%02x", *(skb
->data
+ i
));
441 if ((i
% 16 == 15) || (i
== skb
->len
- 1))
442 pr_info("%s\n", buff
);
445 dev_kfree_skb_any(skb
);
448 static int __lb_clean_rings(struct hns_nic_priv
*priv
,
449 int ringid0
, int ringid1
, int budget
)
452 struct hns_nic_ring_data
*ring_data
;
453 struct net_device
*ndev
= priv
->netdev
;
454 unsigned long rx_packets
= ndev
->stats
.rx_packets
;
455 unsigned long rx_bytes
= ndev
->stats
.rx_bytes
;
456 unsigned long rx_frame_errors
= ndev
->stats
.rx_frame_errors
;
458 for (i
= ringid0
; i
<= ringid1
; i
++) {
459 ring_data
= &priv
->ring_data
[i
];
460 (void)ring_data
->poll_one(ring_data
,
461 budget
, __lb_other_process
);
463 ret
= (int)(ndev
->stats
.rx_packets
- rx_packets
);
464 ndev
->stats
.rx_packets
= rx_packets
;
465 ndev
->stats
.rx_bytes
= rx_bytes
;
466 ndev
->stats
.rx_frame_errors
= rx_frame_errors
;
471 * nic_run_loopback_test - run loopback test
472 * @nic_dev: net device
473 * @loopback_type: loopback type
475 static int __lb_run_test(struct net_device
*ndev
,
476 enum hnae_loop loop_mode
)
478 #define NIC_LB_TEST_PKT_NUM_PER_CYCLE 1
479 #define NIC_LB_TEST_RING_ID 0
480 #define NIC_LB_TEST_FRAME_SIZE 128
481 /* nic loopback test err */
482 #define NIC_LB_TEST_NO_MEM_ERR 1
483 #define NIC_LB_TEST_TX_CNT_ERR 2
484 #define NIC_LB_TEST_RX_CNT_ERR 3
485 #define NIC_LB_TEST_RX_PKG_ERR 4
486 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
487 struct hnae_handle
*h
= priv
->ae_handle
;
488 int i
, j
, lc
, good_cnt
, ret_val
= 0;
490 netdev_tx_t tx_ret_val
;
493 size
= NIC_LB_TEST_FRAME_SIZE
;
494 /* allocate test skb */
495 skb
= alloc_skb(size
, GFP_KERNEL
);
497 return NIC_LB_TEST_NO_MEM_ERR
;
499 /* place data into test skb */
500 (void)skb_put(skb
, size
);
502 __lb_other_process(NULL
, skb
);
503 skb
->queue_mapping
= NIC_LB_TEST_RING_ID
;
506 for (j
= 0; j
< lc
; j
++) {
507 /* reset count of good packets */
509 /* place 64 packets on the transmit queue*/
510 for (i
= 0; i
< NIC_LB_TEST_PKT_NUM_PER_CYCLE
; i
++) {
513 tx_ret_val
= (netdev_tx_t
)hns_nic_net_xmit_hw(
515 &tx_ring_data(priv
, skb
->queue_mapping
));
516 if (tx_ret_val
== NETDEV_TX_OK
)
521 if (good_cnt
!= NIC_LB_TEST_PKT_NUM_PER_CYCLE
) {
522 ret_val
= NIC_LB_TEST_TX_CNT_ERR
;
523 dev_err(priv
->dev
, "%s sent fail, cnt=0x%x, budget=0x%x\n",
524 hns_nic_test_strs
[loop_mode
], good_cnt
,
525 NIC_LB_TEST_PKT_NUM_PER_CYCLE
);
529 /* allow 100 milliseconds for packets to go from Tx to Rx */
532 good_cnt
= __lb_clean_rings(priv
,
533 h
->q_num
, h
->q_num
* 2 - 1,
534 NIC_LB_TEST_PKT_NUM_PER_CYCLE
);
535 if (good_cnt
!= NIC_LB_TEST_PKT_NUM_PER_CYCLE
) {
536 ret_val
= NIC_LB_TEST_RX_CNT_ERR
;
537 dev_err(priv
->dev
, "%s recv fail, cnt=0x%x, budget=0x%x\n",
538 hns_nic_test_strs
[loop_mode
], good_cnt
,
539 NIC_LB_TEST_PKT_NUM_PER_CYCLE
);
542 (void)__lb_clean_rings(priv
,
543 NIC_LB_TEST_RING_ID
, NIC_LB_TEST_RING_ID
,
544 NIC_LB_TEST_PKT_NUM_PER_CYCLE
);
547 /* free the original skb */
553 static int __lb_down(struct net_device
*ndev
)
555 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
556 struct hnae_handle
*h
= priv
->ae_handle
;
559 ret
= __lb_setup(ndev
, MAC_LOOP_NONE
);
561 netdev_err(ndev
, "%s: __lb_setup return error(%d)!\n",
568 if (h
->dev
->ops
->stop
)
569 h
->dev
->ops
->stop(h
);
571 usleep_range(10000, 20000);
572 (void)__lb_clean_rings(priv
, 0, h
->q_num
- 1, 256);
574 hns_nic_net_reset(ndev
);
580 * hns_nic_self_test - self test
582 * @eth_test: test cmd
585 static void hns_nic_self_test(struct net_device
*ndev
,
586 struct ethtool_test
*eth_test
, u64
*data
)
588 struct hns_nic_priv
*priv
= netdev_priv(ndev
);
589 bool if_running
= netif_running(ndev
);
590 #define SELF_TEST_TPYE_NUM 3
591 int st_param
[SELF_TEST_TPYE_NUM
][2];
595 st_param
[0][0] = MAC_INTERNALLOOP_MAC
; /* XGE not supported lb */
596 st_param
[0][1] = (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
);
597 st_param
[1][0] = MAC_INTERNALLOOP_SERDES
;
598 st_param
[1][1] = 1; /*serdes must exist*/
599 st_param
[2][0] = MAC_INTERNALLOOP_PHY
; /* only supporte phy node*/
600 st_param
[2][1] = ((!!(priv
->ae_handle
->phy_node
)) &&
601 (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
));
603 if (eth_test
->flags
== ETH_TEST_FL_OFFLINE
) {
604 set_bit(NIC_STATE_TESTING
, &priv
->state
);
607 (void)dev_close(ndev
);
609 for (i
= 0; i
< SELF_TEST_TPYE_NUM
; i
++) {
611 continue; /* NEXT testing */
613 data
[test_index
] = __lb_up(ndev
,
614 (enum hnae_loop
)st_param
[i
][0]);
615 if (!data
[test_index
]) {
616 data
[test_index
] = __lb_run_test(
617 ndev
, (enum hnae_loop
)st_param
[i
][0]);
618 (void)__lb_down(ndev
);
621 if (data
[test_index
])
622 eth_test
->flags
|= ETH_TEST_FL_FAILED
;
627 hns_nic_net_reset(priv
->netdev
);
629 clear_bit(NIC_STATE_TESTING
, &priv
->state
);
632 (void)dev_open(ndev
);
634 /* Online tests aren't run; pass by default */
636 (void)msleep_interruptible(4 * 1000);
640 * hns_nic_get_drvinfo - get net driver info
642 * @drvinfo: driver info
644 static void hns_nic_get_drvinfo(struct net_device
*net_dev
,
645 struct ethtool_drvinfo
*drvinfo
)
647 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
651 strncpy(drvinfo
->version
, HNAE_DRIVER_VERSION
,
652 sizeof(drvinfo
->version
));
653 drvinfo
->version
[sizeof(drvinfo
->version
) - 1] = '\0';
655 strncpy(drvinfo
->driver
, HNAE_DRIVER_NAME
, sizeof(drvinfo
->driver
));
656 drvinfo
->driver
[sizeof(drvinfo
->driver
) - 1] = '\0';
658 strncpy(drvinfo
->bus_info
, priv
->dev
->bus
->name
,
659 sizeof(drvinfo
->bus_info
));
660 drvinfo
->bus_info
[ETHTOOL_BUSINFO_LEN
- 1] = '\0';
662 strncpy(drvinfo
->fw_version
, "N/A", ETHTOOL_FWVERS_LEN
);
663 drvinfo
->eedump_len
= 0;
667 * hns_get_ringparam - get ring parameter
669 * @param: ethtool parameter
671 void hns_get_ringparam(struct net_device
*net_dev
,
672 struct ethtool_ringparam
*param
)
674 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
675 struct hnae_ae_ops
*ops
;
676 struct hnae_queue
*queue
;
679 queue
= priv
->ae_handle
->qs
[0];
680 ops
= priv
->ae_handle
->dev
->ops
;
682 if (ops
->get_ring_bdnum_limit
)
683 ops
->get_ring_bdnum_limit(queue
, &uplimit
);
685 param
->rx_max_pending
= uplimit
;
686 param
->tx_max_pending
= uplimit
;
687 param
->rx_pending
= queue
->rx_ring
.desc_num
;
688 param
->tx_pending
= queue
->tx_ring
.desc_num
;
692 * hns_get_pauseparam - get pause parameter
694 * @param: pause parameter
696 static void hns_get_pauseparam(struct net_device
*net_dev
,
697 struct ethtool_pauseparam
*param
)
699 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
700 struct hnae_ae_ops
*ops
;
702 ops
= priv
->ae_handle
->dev
->ops
;
704 if (ops
->get_pauseparam
)
705 ops
->get_pauseparam(priv
->ae_handle
, ¶m
->autoneg
,
706 ¶m
->rx_pause
, ¶m
->tx_pause
);
710 * hns_set_pauseparam - set pause parameter
712 * @param: pause parameter
714 * Return 0 on success, negative on failure
716 static int hns_set_pauseparam(struct net_device
*net_dev
,
717 struct ethtool_pauseparam
*param
)
719 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
720 struct hnae_handle
*h
;
721 struct hnae_ae_ops
*ops
;
723 assert(priv
|| priv
->ae_handle
);
728 if (!ops
->set_pauseparam
)
731 return ops
->set_pauseparam(priv
->ae_handle
, param
->autoneg
,
732 param
->rx_pause
, param
->tx_pause
);
736 * hns_get_coalesce - get coalesce info.
738 * @ec: coalesce info.
740 * Return 0 on success, negative on failure.
742 static int hns_get_coalesce(struct net_device
*net_dev
,
743 struct ethtool_coalesce
*ec
)
745 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
746 struct hnae_ae_ops
*ops
;
748 ops
= priv
->ae_handle
->dev
->ops
;
750 ec
->use_adaptive_rx_coalesce
= 1;
751 ec
->use_adaptive_tx_coalesce
= 1;
753 if ((!ops
->get_coalesce_usecs
) ||
754 (!ops
->get_rx_max_coalesced_frames
))
757 ops
->get_coalesce_usecs(priv
->ae_handle
,
758 &ec
->tx_coalesce_usecs
,
759 &ec
->rx_coalesce_usecs
);
761 ops
->get_rx_max_coalesced_frames(
763 &ec
->tx_max_coalesced_frames
,
764 &ec
->rx_max_coalesced_frames
);
770 * hns_set_coalesce - set coalesce info.
772 * @ec: coalesce info.
774 * Return 0 on success, negative on failure.
776 static int hns_set_coalesce(struct net_device
*net_dev
,
777 struct ethtool_coalesce
*ec
)
779 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
780 struct hnae_ae_ops
*ops
;
783 assert(priv
|| priv
->ae_handle
);
785 ops
= priv
->ae_handle
->dev
->ops
;
787 if (ec
->tx_coalesce_usecs
!= ec
->rx_coalesce_usecs
)
790 if (ec
->rx_max_coalesced_frames
!= ec
->tx_max_coalesced_frames
)
793 if ((!ops
->set_coalesce_usecs
) ||
794 (!ops
->set_coalesce_frames
))
797 ops
->set_coalesce_usecs(priv
->ae_handle
,
798 ec
->rx_coalesce_usecs
);
800 ret
= ops
->set_coalesce_frames(
802 ec
->rx_max_coalesced_frames
);
808 * hns_get_channels - get channel info.
812 void hns_get_channels(struct net_device
*net_dev
, struct ethtool_channels
*ch
)
814 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
816 ch
->max_rx
= priv
->ae_handle
->q_num
;
817 ch
->max_tx
= priv
->ae_handle
->q_num
;
819 ch
->rx_count
= priv
->ae_handle
->q_num
;
820 ch
->tx_count
= priv
->ae_handle
->q_num
;
824 * get_ethtool_stats - get detail statistics.
826 * @stats: statistics info.
827 * @data: statistics data.
829 void hns_get_ethtool_stats(struct net_device
*netdev
,
830 struct ethtool_stats
*stats
, u64
*data
)
833 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
834 struct hnae_handle
*h
= priv
->ae_handle
;
835 const struct rtnl_link_stats64
*net_stats
;
836 struct rtnl_link_stats64 temp
;
838 if (!h
->dev
->ops
->get_stats
|| !h
->dev
->ops
->update_stats
) {
839 netdev_err(netdev
, "get_stats or update_stats is null!\n");
843 h
->dev
->ops
->update_stats(h
, &netdev
->stats
);
845 net_stats
= dev_get_stats(netdev
, &temp
);
847 /* get netdev statistics */
848 p
[0] = net_stats
->rx_packets
;
849 p
[1] = net_stats
->tx_packets
;
850 p
[2] = net_stats
->rx_bytes
;
851 p
[3] = net_stats
->tx_bytes
;
852 p
[4] = net_stats
->rx_errors
;
853 p
[5] = net_stats
->tx_errors
;
854 p
[6] = net_stats
->rx_dropped
;
855 p
[7] = net_stats
->tx_dropped
;
856 p
[8] = net_stats
->multicast
;
857 p
[9] = net_stats
->collisions
;
858 p
[10] = net_stats
->rx_over_errors
;
859 p
[11] = net_stats
->rx_crc_errors
;
860 p
[12] = net_stats
->rx_frame_errors
;
861 p
[13] = net_stats
->rx_fifo_errors
;
862 p
[14] = net_stats
->rx_missed_errors
;
863 p
[15] = net_stats
->tx_aborted_errors
;
864 p
[16] = net_stats
->tx_carrier_errors
;
865 p
[17] = net_stats
->tx_fifo_errors
;
866 p
[18] = net_stats
->tx_heartbeat_errors
;
867 p
[19] = net_stats
->rx_length_errors
;
868 p
[20] = net_stats
->tx_window_errors
;
869 p
[21] = net_stats
->rx_compressed
;
870 p
[22] = net_stats
->tx_compressed
;
872 p
[23] = netdev
->rx_dropped
.counter
;
873 p
[24] = netdev
->tx_dropped
.counter
;
875 p
[25] = priv
->tx_timeout_count
;
877 /* get driver statistics */
878 h
->dev
->ops
->get_stats(h
, &p
[26]);
882 * get_strings: Return a set of strings that describe the requested objects
884 * @stats: string set ID.
885 * @data: objects data.
887 void hns_get_strings(struct net_device
*netdev
, u32 stringset
, u8
*data
)
889 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
890 struct hnae_handle
*h
= priv
->ae_handle
;
891 char *buff
= (char *)data
;
893 if (!h
->dev
->ops
->get_strings
) {
894 netdev_err(netdev
, "h->dev->ops->get_strings is null!\n");
898 if (stringset
== ETH_SS_TEST
) {
899 if (priv
->ae_handle
->phy_if
!= PHY_INTERFACE_MODE_XGMII
) {
900 memcpy(buff
, hns_nic_test_strs
[MAC_INTERNALLOOP_MAC
],
902 buff
+= ETH_GSTRING_LEN
;
904 memcpy(buff
, hns_nic_test_strs
[MAC_INTERNALLOOP_SERDES
],
906 buff
+= ETH_GSTRING_LEN
;
907 if ((priv
->phy
) && (!priv
->phy
->is_c45
))
908 memcpy(buff
, hns_nic_test_strs
[MAC_INTERNALLOOP_PHY
],
912 snprintf(buff
, ETH_GSTRING_LEN
, "rx_packets");
913 buff
= buff
+ ETH_GSTRING_LEN
;
914 snprintf(buff
, ETH_GSTRING_LEN
, "tx_packets");
915 buff
= buff
+ ETH_GSTRING_LEN
;
916 snprintf(buff
, ETH_GSTRING_LEN
, "rx_bytes");
917 buff
= buff
+ ETH_GSTRING_LEN
;
918 snprintf(buff
, ETH_GSTRING_LEN
, "tx_bytes");
919 buff
= buff
+ ETH_GSTRING_LEN
;
920 snprintf(buff
, ETH_GSTRING_LEN
, "rx_errors");
921 buff
= buff
+ ETH_GSTRING_LEN
;
922 snprintf(buff
, ETH_GSTRING_LEN
, "tx_errors");
923 buff
= buff
+ ETH_GSTRING_LEN
;
924 snprintf(buff
, ETH_GSTRING_LEN
, "rx_dropped");
925 buff
= buff
+ ETH_GSTRING_LEN
;
926 snprintf(buff
, ETH_GSTRING_LEN
, "tx_dropped");
927 buff
= buff
+ ETH_GSTRING_LEN
;
928 snprintf(buff
, ETH_GSTRING_LEN
, "multicast");
929 buff
= buff
+ ETH_GSTRING_LEN
;
930 snprintf(buff
, ETH_GSTRING_LEN
, "collisions");
931 buff
= buff
+ ETH_GSTRING_LEN
;
932 snprintf(buff
, ETH_GSTRING_LEN
, "rx_over_errors");
933 buff
= buff
+ ETH_GSTRING_LEN
;
934 snprintf(buff
, ETH_GSTRING_LEN
, "rx_crc_errors");
935 buff
= buff
+ ETH_GSTRING_LEN
;
936 snprintf(buff
, ETH_GSTRING_LEN
, "rx_frame_errors");
937 buff
= buff
+ ETH_GSTRING_LEN
;
938 snprintf(buff
, ETH_GSTRING_LEN
, "rx_fifo_errors");
939 buff
= buff
+ ETH_GSTRING_LEN
;
940 snprintf(buff
, ETH_GSTRING_LEN
, "rx_missed_errors");
941 buff
= buff
+ ETH_GSTRING_LEN
;
942 snprintf(buff
, ETH_GSTRING_LEN
, "tx_aborted_errors");
943 buff
= buff
+ ETH_GSTRING_LEN
;
944 snprintf(buff
, ETH_GSTRING_LEN
, "tx_carrier_errors");
945 buff
= buff
+ ETH_GSTRING_LEN
;
946 snprintf(buff
, ETH_GSTRING_LEN
, "tx_fifo_errors");
947 buff
= buff
+ ETH_GSTRING_LEN
;
948 snprintf(buff
, ETH_GSTRING_LEN
, "tx_heartbeat_errors");
949 buff
= buff
+ ETH_GSTRING_LEN
;
950 snprintf(buff
, ETH_GSTRING_LEN
, "rx_length_errors");
951 buff
= buff
+ ETH_GSTRING_LEN
;
952 snprintf(buff
, ETH_GSTRING_LEN
, "tx_window_errors");
953 buff
= buff
+ ETH_GSTRING_LEN
;
954 snprintf(buff
, ETH_GSTRING_LEN
, "rx_compressed");
955 buff
= buff
+ ETH_GSTRING_LEN
;
956 snprintf(buff
, ETH_GSTRING_LEN
, "tx_compressed");
957 buff
= buff
+ ETH_GSTRING_LEN
;
958 snprintf(buff
, ETH_GSTRING_LEN
, "netdev_rx_dropped");
959 buff
= buff
+ ETH_GSTRING_LEN
;
960 snprintf(buff
, ETH_GSTRING_LEN
, "netdev_tx_dropped");
961 buff
= buff
+ ETH_GSTRING_LEN
;
963 snprintf(buff
, ETH_GSTRING_LEN
, "netdev_tx_timeout");
964 buff
= buff
+ ETH_GSTRING_LEN
;
966 h
->dev
->ops
->get_strings(h
, stringset
, (u8
*)buff
);
971 * nic_get_sset_count - get string set count witch returned by nic_get_strings.
973 * @stringset: string set index, 0: self test string; 1: statistics string.
975 * Return string set count.
977 int hns_get_sset_count(struct net_device
*netdev
, int stringset
)
979 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
980 struct hnae_handle
*h
= priv
->ae_handle
;
981 struct hnae_ae_ops
*ops
= h
->dev
->ops
;
983 if (!ops
->get_sset_count
) {
984 netdev_err(netdev
, "get_sset_count is null!\n");
987 if (stringset
== ETH_SS_TEST
) {
988 u32 cnt
= (sizeof(hns_nic_test_strs
) / ETH_GSTRING_LEN
);
990 if (priv
->ae_handle
->phy_if
== PHY_INTERFACE_MODE_XGMII
)
993 if ((!priv
->phy
) || (priv
->phy
->is_c45
))
998 return (HNS_NET_STATS_CNT
+ ops
->get_sset_count(h
, stringset
));
1003 * hns_phy_led_set - set phy LED status.
1005 * @value: LED state.
1007 * Return 0 on success, negative on failure.
1009 int hns_phy_led_set(struct net_device
*netdev
, int value
)
1012 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1013 struct phy_device
*phy_dev
= priv
->phy
;
1015 retval
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_LED
);
1016 retval
= phy_write(phy_dev
, HNS_LED_FC_REG
, value
);
1017 retval
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
, HNS_PHY_PAGE_COPPER
);
1019 netdev_err(netdev
, "mdiobus_write fail !\n");
1026 * nic_set_phys_id - set phy identify LED.
1028 * @state: LED state.
1030 * Return 0 on success, negative on failure.
1032 int hns_set_phys_id(struct net_device
*netdev
, enum ethtool_phys_id_state state
)
1034 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1035 struct hnae_handle
*h
= priv
->ae_handle
;
1036 struct phy_device
*phy_dev
= priv
->phy
;
1041 case ETHTOOL_ID_ACTIVE
:
1042 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1047 priv
->phy_led_val
= phy_read(phy_dev
, HNS_LED_FC_REG
);
1049 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1050 HNS_PHY_PAGE_COPPER
);
1055 ret
= hns_phy_led_set(netdev
, HNS_LED_FORCE_ON
);
1059 case ETHTOOL_ID_OFF
:
1060 ret
= hns_phy_led_set(netdev
, HNS_LED_FORCE_OFF
);
1064 case ETHTOOL_ID_INACTIVE
:
1065 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1070 ret
= phy_write(phy_dev
, HNS_LED_FC_REG
,
1075 ret
= phy_write(phy_dev
, HNS_PHY_PAGE_REG
,
1076 HNS_PHY_PAGE_COPPER
);
1085 case ETHTOOL_ID_ACTIVE
:
1086 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_ACTIVE
);
1088 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_ON
);
1089 case ETHTOOL_ID_OFF
:
1090 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_OFF
);
1091 case ETHTOOL_ID_INACTIVE
:
1092 return h
->dev
->ops
->set_led_id(h
, HNAE_LED_INACTIVE
);
1101 * hns_get_regs - get net device register
1104 * @date: register data
1106 void hns_get_regs(struct net_device
*net_dev
, struct ethtool_regs
*cmd
,
1109 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
1110 struct hnae_ae_ops
*ops
;
1112 assert(priv
|| priv
->ae_handle
);
1114 ops
= priv
->ae_handle
->dev
->ops
;
1116 cmd
->version
= HNS_CHIP_VERSION
;
1117 if (!ops
->get_regs
) {
1118 netdev_err(net_dev
, "ops->get_regs is null!\n");
1121 ops
->get_regs(priv
->ae_handle
, data
);
1125 * nic_get_regs_len - get total register len.
1128 * Return total register len.
1130 static int hns_get_regs_len(struct net_device
*net_dev
)
1133 struct hns_nic_priv
*priv
= netdev_priv(net_dev
);
1134 struct hnae_ae_ops
*ops
;
1136 assert(priv
|| priv
->ae_handle
);
1138 ops
= priv
->ae_handle
->dev
->ops
;
1139 if (!ops
->get_regs_len
) {
1140 netdev_err(net_dev
, "ops->get_regs_len is null!\n");
1144 reg_num
= ops
->get_regs_len(priv
->ae_handle
);
1146 return reg_num
* sizeof(u32
);
1148 return reg_num
; /* error code */
1152 * hns_nic_nway_reset - nway reset
1155 * Return 0 on success, negative on failure
1157 static int hns_nic_nway_reset(struct net_device
*netdev
)
1160 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1161 struct phy_device
*phy
= priv
->phy
;
1163 if (netif_running(netdev
)) {
1165 ret
= genphy_restart_aneg(phy
);
1172 hns_get_rss_key_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_key_size(priv
->ae_handle
);
1188 hns_get_rss_indir_size(struct net_device
*netdev
)
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
;
1200 return ops
->get_rss_indir_size(priv
->ae_handle
);
1204 hns_get_rss(struct net_device
*netdev
, u32
*indir
, u8
*key
, u8
*hfunc
)
1206 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1207 struct hnae_ae_ops
*ops
;
1209 if (AE_IS_VER1(priv
->enet_ver
)) {
1211 "RSS feature is not supported on this hardware\n");
1215 ops
= priv
->ae_handle
->dev
->ops
;
1220 return ops
->get_rss(priv
->ae_handle
, indir
, key
, hfunc
);
1224 hns_set_rss(struct net_device
*netdev
, const u32
*indir
, const u8
*key
,
1227 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1228 struct hnae_ae_ops
*ops
;
1230 if (AE_IS_VER1(priv
->enet_ver
)) {
1232 "RSS feature is not supported on this hardware\n");
1236 ops
= priv
->ae_handle
->dev
->ops
;
1238 /* currently hfunc can only be Toeplitz hash */
1240 (hfunc
!= ETH_RSS_HASH_NO_CHANGE
&& hfunc
!= ETH_RSS_HASH_TOP
))
1245 return ops
->set_rss(priv
->ae_handle
, indir
, key
, hfunc
);
1248 static int hns_get_rxnfc(struct net_device
*netdev
,
1249 struct ethtool_rxnfc
*cmd
,
1252 struct hns_nic_priv
*priv
= netdev_priv(netdev
);
1255 case ETHTOOL_GRXRINGS
:
1256 cmd
->data
= priv
->ae_handle
->q_num
;
1265 static struct ethtool_ops hns_ethtool_ops
= {
1266 .get_drvinfo
= hns_nic_get_drvinfo
,
1267 .get_link
= hns_nic_get_link
,
1268 .get_settings
= hns_nic_get_settings
,
1269 .set_settings
= hns_nic_set_settings
,
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
,
1291 void hns_ethtool_set_ops(struct net_device
*ndev
)
1293 ndev
->ethtool_ops
= &hns_ethtool_ops
;