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