1 /* 2 * Copyright (c) 2016~2017 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/string.h> 12 #include <linux/phy.h> 13 14 #include "hns3_enet.h" 15 16 struct hns3_stats { 17 char stats_string[ETH_GSTRING_LEN]; 18 int stats_offset; 19 }; 20 21 /* tqp related stats */ 22 #define HNS3_TQP_STAT(_string, _member) { \ 23 .stats_string = _string, \ 24 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\ 25 offsetof(struct ring_stats, _member), \ 26 } 27 28 static const struct hns3_stats hns3_txq_stats[] = { 29 /* Tx per-queue statistics */ 30 HNS3_TQP_STAT("io_err_cnt", io_err_cnt), 31 HNS3_TQP_STAT("tx_dropped", sw_err_cnt), 32 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt), 33 HNS3_TQP_STAT("packets", tx_pkts), 34 HNS3_TQP_STAT("bytes", tx_bytes), 35 HNS3_TQP_STAT("errors", tx_err_cnt), 36 HNS3_TQP_STAT("tx_wake", restart_queue), 37 HNS3_TQP_STAT("tx_busy", tx_busy), 38 }; 39 40 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats) 41 42 static const struct hns3_stats hns3_rxq_stats[] = { 43 /* Rx per-queue statistics */ 44 HNS3_TQP_STAT("io_err_cnt", io_err_cnt), 45 HNS3_TQP_STAT("rx_dropped", sw_err_cnt), 46 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt), 47 HNS3_TQP_STAT("packets", rx_pkts), 48 HNS3_TQP_STAT("bytes", rx_bytes), 49 HNS3_TQP_STAT("errors", rx_err_cnt), 50 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt), 51 HNS3_TQP_STAT("err_pkt_len", err_pkt_len), 52 HNS3_TQP_STAT("non_vld_descs", non_vld_descs), 53 HNS3_TQP_STAT("err_bd_num", err_bd_num), 54 HNS3_TQP_STAT("l2_err", l2_err), 55 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err), 56 }; 57 58 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats) 59 60 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT) 61 62 #define HNS3_SELF_TEST_TPYE_NUM 1 63 #define HNS3_NIC_LB_TEST_PKT_NUM 1 64 #define HNS3_NIC_LB_TEST_RING_ID 0 65 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128 66 67 /* Nic loopback test err */ 68 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1 69 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2 70 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3 71 72 struct hns3_link_mode_mapping { 73 u32 hns3_link_mode; 74 u32 ethtool_link_mode; 75 }; 76 77 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en) 78 { 79 struct hnae3_handle *h = hns3_get_handle(ndev); 80 int ret; 81 82 if (!h->ae_algo->ops->set_loopback || 83 !h->ae_algo->ops->set_promisc_mode) 84 return -EOPNOTSUPP; 85 86 switch (loop) { 87 case HNAE3_MAC_INTER_LOOP_MAC: 88 ret = h->ae_algo->ops->set_loopback(h, loop, en); 89 break; 90 default: 91 ret = -ENOTSUPP; 92 break; 93 } 94 95 if (ret) 96 return ret; 97 98 h->ae_algo->ops->set_promisc_mode(h, en, en); 99 100 return ret; 101 } 102 103 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode) 104 { 105 struct hnae3_handle *h = hns3_get_handle(ndev); 106 int ret; 107 108 if (!h->ae_algo->ops->start) 109 return -EOPNOTSUPP; 110 111 ret = hns3_nic_reset_all_ring(h); 112 if (ret) 113 return ret; 114 115 ret = h->ae_algo->ops->start(h); 116 if (ret) { 117 netdev_err(ndev, 118 "hns3_lb_up ae start return error: %d\n", ret); 119 return ret; 120 } 121 122 ret = hns3_lp_setup(ndev, loop_mode, true); 123 usleep_range(10000, 20000); 124 125 return ret; 126 } 127 128 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode) 129 { 130 struct hnae3_handle *h = hns3_get_handle(ndev); 131 int ret; 132 133 if (!h->ae_algo->ops->stop) 134 return -EOPNOTSUPP; 135 136 ret = hns3_lp_setup(ndev, loop_mode, false); 137 if (ret) { 138 netdev_err(ndev, "lb_setup return error: %d\n", ret); 139 return ret; 140 } 141 142 h->ae_algo->ops->stop(h); 143 usleep_range(10000, 20000); 144 145 return 0; 146 } 147 148 static void hns3_lp_setup_skb(struct sk_buff *skb) 149 { 150 struct net_device *ndev = skb->dev; 151 unsigned char *packet; 152 struct ethhdr *ethh; 153 unsigned int i; 154 155 skb_reserve(skb, NET_IP_ALIGN); 156 ethh = skb_put(skb, sizeof(struct ethhdr)); 157 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE); 158 159 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN); 160 eth_zero_addr(ethh->h_source); 161 ethh->h_proto = htons(ETH_P_ARP); 162 skb_reset_mac_header(skb); 163 164 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++) 165 packet[i] = (unsigned char)(i & 0xff); 166 } 167 168 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring, 169 struct sk_buff *skb) 170 { 171 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector; 172 unsigned char *packet = skb->data; 173 u32 i; 174 175 for (i = 0; i < skb->len; i++) 176 if (packet[i] != (unsigned char)(i & 0xff)) 177 break; 178 179 /* The packet is correctly received */ 180 if (i == skb->len) 181 tqp_vector->rx_group.total_packets++; 182 else 183 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1, 184 skb->data, skb->len, true); 185 186 dev_kfree_skb_any(skb); 187 } 188 189 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget) 190 { 191 struct hnae3_handle *h = priv->ae_handle; 192 struct hnae3_knic_private_info *kinfo; 193 u32 i, rcv_good_pkt_total = 0; 194 195 kinfo = &h->kinfo; 196 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) { 197 struct hns3_enet_ring *ring = priv->ring_data[i].ring; 198 struct hns3_enet_ring_group *rx_group; 199 u64 pre_rx_pkt; 200 201 rx_group = &ring->tqp_vector->rx_group; 202 pre_rx_pkt = rx_group->total_packets; 203 204 preempt_disable(); 205 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data); 206 preempt_enable(); 207 208 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt); 209 rx_group->total_packets = pre_rx_pkt; 210 } 211 return rcv_good_pkt_total; 212 } 213 214 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid, 215 u32 end_ringid, u32 budget) 216 { 217 u32 i; 218 219 for (i = start_ringid; i <= end_ringid; i++) { 220 struct hns3_enet_ring *ring = priv->ring_data[i].ring; 221 222 hns3_clean_tx_ring(ring, budget); 223 } 224 } 225 226 /** 227 * hns3_lp_run_test - run loopback test 228 * @ndev: net device 229 * @mode: loopback type 230 */ 231 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode) 232 { 233 struct hns3_nic_priv *priv = netdev_priv(ndev); 234 struct sk_buff *skb; 235 u32 i, good_cnt; 236 int ret_val = 0; 237 238 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN, 239 GFP_KERNEL); 240 if (!skb) 241 return HNS3_NIC_LB_TEST_NO_MEM_ERR; 242 243 skb->dev = ndev; 244 hns3_lp_setup_skb(skb); 245 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID; 246 247 good_cnt = 0; 248 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) { 249 netdev_tx_t tx_ret; 250 251 skb_get(skb); 252 tx_ret = hns3_nic_net_xmit(skb, ndev); 253 if (tx_ret == NETDEV_TX_OK) 254 good_cnt++; 255 else 256 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n", 257 tx_ret); 258 } 259 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) { 260 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR; 261 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n", 262 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM); 263 goto out; 264 } 265 266 /* Allow 200 milliseconds for packets to go from Tx to Rx */ 267 msleep(200); 268 269 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM); 270 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) { 271 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR; 272 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n", 273 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM); 274 } 275 276 out: 277 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID, 278 HNS3_NIC_LB_TEST_RING_ID, 279 HNS3_NIC_LB_TEST_PKT_NUM); 280 281 kfree_skb(skb); 282 return ret_val; 283 } 284 285 /** 286 * hns3_nic_self_test - self test 287 * @ndev: net device 288 * @eth_test: test cmd 289 * @data: test result 290 */ 291 static void hns3_self_test(struct net_device *ndev, 292 struct ethtool_test *eth_test, u64 *data) 293 { 294 struct hns3_nic_priv *priv = netdev_priv(ndev); 295 struct hnae3_handle *h = priv->ae_handle; 296 int st_param[HNS3_SELF_TEST_TPYE_NUM][2]; 297 bool if_running = netif_running(ndev); 298 #if IS_ENABLED(CONFIG_VLAN_8021Q) 299 bool dis_vlan_filter; 300 #endif 301 int test_index = 0; 302 u32 i; 303 304 /* Only do offline selftest, or pass by default */ 305 if (eth_test->flags != ETH_TEST_FL_OFFLINE) 306 return; 307 308 st_param[HNAE3_MAC_INTER_LOOP_MAC][0] = HNAE3_MAC_INTER_LOOP_MAC; 309 st_param[HNAE3_MAC_INTER_LOOP_MAC][1] = 310 h->flags & HNAE3_SUPPORT_MAC_LOOPBACK; 311 312 if (if_running) 313 dev_close(ndev); 314 315 #if IS_ENABLED(CONFIG_VLAN_8021Q) 316 /* Disable the vlan filter for selftest does not support it */ 317 dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) && 318 h->ae_algo->ops->enable_vlan_filter; 319 if (dis_vlan_filter) 320 h->ae_algo->ops->enable_vlan_filter(h, false); 321 #endif 322 323 set_bit(HNS3_NIC_STATE_TESTING, &priv->state); 324 325 for (i = 0; i < HNS3_SELF_TEST_TPYE_NUM; i++) { 326 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0]; 327 328 if (!st_param[i][1]) 329 continue; 330 331 data[test_index] = hns3_lp_up(ndev, loop_type); 332 if (!data[test_index]) { 333 data[test_index] = hns3_lp_run_test(ndev, loop_type); 334 hns3_lp_down(ndev, loop_type); 335 } 336 337 if (data[test_index]) 338 eth_test->flags |= ETH_TEST_FL_FAILED; 339 340 test_index++; 341 } 342 343 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state); 344 345 #if IS_ENABLED(CONFIG_VLAN_8021Q) 346 if (dis_vlan_filter) 347 h->ae_algo->ops->enable_vlan_filter(h, true); 348 #endif 349 350 if (if_running) 351 dev_open(ndev); 352 } 353 354 static int hns3_get_sset_count(struct net_device *netdev, int stringset) 355 { 356 struct hnae3_handle *h = hns3_get_handle(netdev); 357 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 358 359 if (!ops->get_sset_count) 360 return -EOPNOTSUPP; 361 362 switch (stringset) { 363 case ETH_SS_STATS: 364 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) + 365 ops->get_sset_count(h, stringset)); 366 367 case ETH_SS_TEST: 368 return ops->get_sset_count(h, stringset); 369 } 370 371 return 0; 372 } 373 374 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats, 375 u32 stat_count, u32 num_tqps, const char *prefix) 376 { 377 #define MAX_PREFIX_SIZE (6 + 4) 378 u32 size_left; 379 u32 i, j; 380 u32 n1; 381 382 for (i = 0; i < num_tqps; i++) { 383 for (j = 0; j < stat_count; j++) { 384 data[ETH_GSTRING_LEN - 1] = '\0'; 385 386 /* first, prepend the prefix string */ 387 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s#%d_", 388 prefix, i); 389 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1); 390 size_left = (ETH_GSTRING_LEN - 1) - n1; 391 392 /* now, concatenate the stats string to it */ 393 strncat(data, stats[j].stats_string, size_left); 394 data += ETH_GSTRING_LEN; 395 } 396 } 397 398 return data; 399 } 400 401 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data) 402 { 403 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 404 const char tx_prefix[] = "txq"; 405 const char rx_prefix[] = "rxq"; 406 407 /* get strings for Tx */ 408 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT, 409 kinfo->num_tqps, tx_prefix); 410 411 /* get strings for Rx */ 412 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT, 413 kinfo->num_tqps, rx_prefix); 414 415 return data; 416 } 417 418 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data) 419 { 420 struct hnae3_handle *h = hns3_get_handle(netdev); 421 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 422 char *buff = (char *)data; 423 424 if (!ops->get_strings) 425 return; 426 427 switch (stringset) { 428 case ETH_SS_STATS: 429 buff = hns3_get_strings_tqps(h, buff); 430 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff); 431 break; 432 case ETH_SS_TEST: 433 ops->get_strings(h, stringset, data); 434 break; 435 } 436 } 437 438 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data) 439 { 440 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv; 441 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 442 struct hns3_enet_ring *ring; 443 u8 *stat; 444 int i, j; 445 446 /* get stats for Tx */ 447 for (i = 0; i < kinfo->num_tqps; i++) { 448 ring = nic_priv->ring_data[i].ring; 449 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) { 450 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset; 451 *data++ = *(u64 *)stat; 452 } 453 } 454 455 /* get stats for Rx */ 456 for (i = 0; i < kinfo->num_tqps; i++) { 457 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring; 458 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) { 459 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset; 460 *data++ = *(u64 *)stat; 461 } 462 } 463 464 return data; 465 } 466 467 /* hns3_get_stats - get detail statistics. 468 * @netdev: net device 469 * @stats: statistics info. 470 * @data: statistics data. 471 */ 472 static void hns3_get_stats(struct net_device *netdev, 473 struct ethtool_stats *stats, u64 *data) 474 { 475 struct hnae3_handle *h = hns3_get_handle(netdev); 476 u64 *p = data; 477 478 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) { 479 netdev_err(netdev, "could not get any statistics\n"); 480 return; 481 } 482 483 h->ae_algo->ops->update_stats(h, &netdev->stats); 484 485 /* get per-queue stats */ 486 p = hns3_get_stats_tqps(h, p); 487 488 /* get MAC & other misc hardware stats */ 489 h->ae_algo->ops->get_stats(h, p); 490 } 491 492 static void hns3_get_drvinfo(struct net_device *netdev, 493 struct ethtool_drvinfo *drvinfo) 494 { 495 struct hns3_nic_priv *priv = netdev_priv(netdev); 496 struct hnae3_handle *h = priv->ae_handle; 497 498 strncpy(drvinfo->version, hns3_driver_version, 499 sizeof(drvinfo->version)); 500 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0'; 501 502 strncpy(drvinfo->driver, h->pdev->driver->name, 503 sizeof(drvinfo->driver)); 504 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0'; 505 506 strncpy(drvinfo->bus_info, pci_name(h->pdev), 507 sizeof(drvinfo->bus_info)); 508 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0'; 509 510 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x", 511 priv->ae_handle->ae_algo->ops->get_fw_version(h)); 512 } 513 514 static u32 hns3_get_link(struct net_device *netdev) 515 { 516 struct hnae3_handle *h = hns3_get_handle(netdev); 517 518 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status) 519 return h->ae_algo->ops->get_status(h); 520 else 521 return 0; 522 } 523 524 static void hns3_get_ringparam(struct net_device *netdev, 525 struct ethtool_ringparam *param) 526 { 527 struct hns3_nic_priv *priv = netdev_priv(netdev); 528 struct hnae3_handle *h = priv->ae_handle; 529 int queue_num = h->kinfo.num_tqps; 530 531 param->tx_max_pending = HNS3_RING_MAX_PENDING; 532 param->rx_max_pending = HNS3_RING_MAX_PENDING; 533 534 param->tx_pending = priv->ring_data[0].ring->desc_num; 535 param->rx_pending = priv->ring_data[queue_num].ring->desc_num; 536 } 537 538 static void hns3_get_pauseparam(struct net_device *netdev, 539 struct ethtool_pauseparam *param) 540 { 541 struct hnae3_handle *h = hns3_get_handle(netdev); 542 543 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam) 544 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg, 545 ¶m->rx_pause, ¶m->tx_pause); 546 } 547 548 static int hns3_set_pauseparam(struct net_device *netdev, 549 struct ethtool_pauseparam *param) 550 { 551 struct hnae3_handle *h = hns3_get_handle(netdev); 552 553 if (h->ae_algo->ops->set_pauseparam) 554 return h->ae_algo->ops->set_pauseparam(h, param->autoneg, 555 param->rx_pause, 556 param->tx_pause); 557 return -EOPNOTSUPP; 558 } 559 560 static int hns3_get_link_ksettings(struct net_device *netdev, 561 struct ethtool_link_ksettings *cmd) 562 { 563 struct hnae3_handle *h = hns3_get_handle(netdev); 564 u32 flowctrl_adv = 0; 565 u8 link_stat; 566 567 if (!h->ae_algo || !h->ae_algo->ops) 568 return -EOPNOTSUPP; 569 570 /* 1.auto_neg & speed & duplex from cmd */ 571 if (netdev->phydev) { 572 phy_ethtool_ksettings_get(netdev->phydev, cmd); 573 574 return 0; 575 } 576 577 if (h->ae_algo->ops->get_ksettings_an_result) 578 h->ae_algo->ops->get_ksettings_an_result(h, 579 &cmd->base.autoneg, 580 &cmd->base.speed, 581 &cmd->base.duplex); 582 else 583 return -EOPNOTSUPP; 584 585 link_stat = hns3_get_link(netdev); 586 if (!link_stat) { 587 cmd->base.speed = SPEED_UNKNOWN; 588 cmd->base.duplex = DUPLEX_UNKNOWN; 589 } 590 591 /* 2.get link mode and port type*/ 592 if (h->ae_algo->ops->get_link_mode) 593 h->ae_algo->ops->get_link_mode(h, 594 cmd->link_modes.supported, 595 cmd->link_modes.advertising); 596 597 cmd->base.port = PORT_NONE; 598 if (h->ae_algo->ops->get_port_type) 599 h->ae_algo->ops->get_port_type(h, 600 &cmd->base.port); 601 602 /* 3.mdix_ctrl&mdix get from phy reg */ 603 if (h->ae_algo->ops->get_mdix_mode) 604 h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl, 605 &cmd->base.eth_tp_mdix); 606 /* 4.mdio_support */ 607 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22; 608 609 /* 5.get flow control setttings */ 610 if (h->ae_algo->ops->get_flowctrl_adv) 611 h->ae_algo->ops->get_flowctrl_adv(h, &flowctrl_adv); 612 613 if (flowctrl_adv & ADVERTISED_Pause) 614 ethtool_link_ksettings_add_link_mode(cmd, advertising, 615 Pause); 616 617 if (flowctrl_adv & ADVERTISED_Asym_Pause) 618 ethtool_link_ksettings_add_link_mode(cmd, advertising, 619 Asym_Pause); 620 621 return 0; 622 } 623 624 static int hns3_set_link_ksettings(struct net_device *netdev, 625 const struct ethtool_link_ksettings *cmd) 626 { 627 /* Only support ksettings_set for netdev with phy attached for now */ 628 if (netdev->phydev) 629 return phy_ethtool_ksettings_set(netdev->phydev, cmd); 630 631 return -EOPNOTSUPP; 632 } 633 634 static u32 hns3_get_rss_key_size(struct net_device *netdev) 635 { 636 struct hnae3_handle *h = hns3_get_handle(netdev); 637 638 if (!h->ae_algo || !h->ae_algo->ops || 639 !h->ae_algo->ops->get_rss_key_size) 640 return 0; 641 642 return h->ae_algo->ops->get_rss_key_size(h); 643 } 644 645 static u32 hns3_get_rss_indir_size(struct net_device *netdev) 646 { 647 struct hnae3_handle *h = hns3_get_handle(netdev); 648 649 if (!h->ae_algo || !h->ae_algo->ops || 650 !h->ae_algo->ops->get_rss_indir_size) 651 return 0; 652 653 return h->ae_algo->ops->get_rss_indir_size(h); 654 } 655 656 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key, 657 u8 *hfunc) 658 { 659 struct hnae3_handle *h = hns3_get_handle(netdev); 660 661 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss) 662 return -EOPNOTSUPP; 663 664 return h->ae_algo->ops->get_rss(h, indir, key, hfunc); 665 } 666 667 static int hns3_set_rss(struct net_device *netdev, const u32 *indir, 668 const u8 *key, const u8 hfunc) 669 { 670 struct hnae3_handle *h = hns3_get_handle(netdev); 671 672 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss) 673 return -EOPNOTSUPP; 674 675 /* currently we only support Toeplitz hash */ 676 if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) { 677 netdev_err(netdev, 678 "hash func not supported (only Toeplitz hash)\n"); 679 return -EOPNOTSUPP; 680 } 681 if (!indir) { 682 netdev_err(netdev, 683 "set rss failed for indir is empty\n"); 684 return -EOPNOTSUPP; 685 } 686 687 return h->ae_algo->ops->set_rss(h, indir, key, hfunc); 688 } 689 690 static int hns3_get_rxnfc(struct net_device *netdev, 691 struct ethtool_rxnfc *cmd, 692 u32 *rule_locs) 693 { 694 struct hnae3_handle *h = hns3_get_handle(netdev); 695 696 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss_tuple) 697 return -EOPNOTSUPP; 698 699 switch (cmd->cmd) { 700 case ETHTOOL_GRXRINGS: 701 cmd->data = h->kinfo.rss_size; 702 break; 703 case ETHTOOL_GRXFH: 704 return h->ae_algo->ops->get_rss_tuple(h, cmd); 705 default: 706 return -EOPNOTSUPP; 707 } 708 709 return 0; 710 } 711 712 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv, 713 u32 new_desc_num) 714 { 715 struct hnae3_handle *h = priv->ae_handle; 716 int i; 717 718 h->kinfo.num_desc = new_desc_num; 719 720 for (i = 0; i < h->kinfo.num_tqps * 2; i++) 721 priv->ring_data[i].ring->desc_num = new_desc_num; 722 723 return hns3_init_all_ring(priv); 724 } 725 726 static int hns3_set_ringparam(struct net_device *ndev, 727 struct ethtool_ringparam *param) 728 { 729 struct hns3_nic_priv *priv = netdev_priv(ndev); 730 struct hnae3_handle *h = priv->ae_handle; 731 bool if_running = netif_running(ndev); 732 u32 old_desc_num, new_desc_num; 733 int ret; 734 735 if (param->rx_mini_pending || param->rx_jumbo_pending) 736 return -EINVAL; 737 738 if (param->tx_pending != param->rx_pending) { 739 netdev_err(ndev, 740 "Descriptors of tx and rx must be equal"); 741 return -EINVAL; 742 } 743 744 if (param->tx_pending > HNS3_RING_MAX_PENDING || 745 param->tx_pending < HNS3_RING_MIN_PENDING) { 746 netdev_err(ndev, 747 "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n", 748 param->tx_pending, HNS3_RING_MIN_PENDING, 749 HNS3_RING_MAX_PENDING); 750 return -EINVAL; 751 } 752 753 new_desc_num = param->tx_pending; 754 755 /* Hardware requires that its descriptors must be multiple of eight */ 756 new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE); 757 old_desc_num = h->kinfo.num_desc; 758 if (old_desc_num == new_desc_num) 759 return 0; 760 761 netdev_info(ndev, 762 "Changing descriptor count from %d to %d.\n", 763 old_desc_num, new_desc_num); 764 765 if (if_running) 766 dev_close(ndev); 767 768 ret = hns3_uninit_all_ring(priv); 769 if (ret) 770 return ret; 771 772 ret = hns3_change_all_ring_bd_num(priv, new_desc_num); 773 if (ret) { 774 ret = hns3_change_all_ring_bd_num(priv, old_desc_num); 775 if (ret) { 776 netdev_err(ndev, 777 "Revert to old bd num fail, ret=%d.\n", ret); 778 return ret; 779 } 780 } 781 782 if (if_running) 783 ret = dev_open(ndev); 784 785 return ret; 786 } 787 788 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) 789 { 790 struct hnae3_handle *h = hns3_get_handle(netdev); 791 792 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss_tuple) 793 return -EOPNOTSUPP; 794 795 switch (cmd->cmd) { 796 case ETHTOOL_SRXFH: 797 return h->ae_algo->ops->set_rss_tuple(h, cmd); 798 default: 799 return -EOPNOTSUPP; 800 } 801 } 802 803 static int hns3_nway_reset(struct net_device *netdev) 804 { 805 struct phy_device *phy = netdev->phydev; 806 807 if (!netif_running(netdev)) 808 return 0; 809 810 /* Only support nway_reset for netdev with phy attached for now */ 811 if (!phy) 812 return -EOPNOTSUPP; 813 814 if (phy->autoneg != AUTONEG_ENABLE) 815 return -EINVAL; 816 817 return genphy_restart_aneg(phy); 818 } 819 820 static void hns3_get_channels(struct net_device *netdev, 821 struct ethtool_channels *ch) 822 { 823 struct hnae3_handle *h = hns3_get_handle(netdev); 824 825 if (h->ae_algo->ops->get_channels) 826 h->ae_algo->ops->get_channels(h, ch); 827 } 828 829 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue, 830 struct ethtool_coalesce *cmd) 831 { 832 struct hns3_enet_tqp_vector *tx_vector, *rx_vector; 833 struct hns3_nic_priv *priv = netdev_priv(netdev); 834 struct hnae3_handle *h = priv->ae_handle; 835 u16 queue_num = h->kinfo.num_tqps; 836 837 if (queue >= queue_num) { 838 netdev_err(netdev, 839 "Invalid queue value %d! Queue max id=%d\n", 840 queue, queue_num - 1); 841 return -EINVAL; 842 } 843 844 tx_vector = priv->ring_data[queue].ring->tqp_vector; 845 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector; 846 847 cmd->use_adaptive_tx_coalesce = 848 tx_vector->tx_group.coal.gl_adapt_enable; 849 cmd->use_adaptive_rx_coalesce = 850 rx_vector->rx_group.coal.gl_adapt_enable; 851 852 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl; 853 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl; 854 855 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting; 856 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting; 857 858 return 0; 859 } 860 861 static int hns3_get_coalesce(struct net_device *netdev, 862 struct ethtool_coalesce *cmd) 863 { 864 return hns3_get_coalesce_per_queue(netdev, 0, cmd); 865 } 866 867 static int hns3_check_gl_coalesce_para(struct net_device *netdev, 868 struct ethtool_coalesce *cmd) 869 { 870 u32 rx_gl, tx_gl; 871 872 if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) { 873 netdev_err(netdev, 874 "Invalid rx-usecs value, rx-usecs range is 0-%d\n", 875 HNS3_INT_GL_MAX); 876 return -EINVAL; 877 } 878 879 if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) { 880 netdev_err(netdev, 881 "Invalid tx-usecs value, tx-usecs range is 0-%d\n", 882 HNS3_INT_GL_MAX); 883 return -EINVAL; 884 } 885 886 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs); 887 if (rx_gl != cmd->rx_coalesce_usecs) { 888 netdev_info(netdev, 889 "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n", 890 cmd->rx_coalesce_usecs, rx_gl); 891 } 892 893 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs); 894 if (tx_gl != cmd->tx_coalesce_usecs) { 895 netdev_info(netdev, 896 "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n", 897 cmd->tx_coalesce_usecs, tx_gl); 898 } 899 900 return 0; 901 } 902 903 static int hns3_check_rl_coalesce_para(struct net_device *netdev, 904 struct ethtool_coalesce *cmd) 905 { 906 u32 rl; 907 908 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) { 909 netdev_err(netdev, 910 "tx_usecs_high must be same as rx_usecs_high.\n"); 911 return -EINVAL; 912 } 913 914 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) { 915 netdev_err(netdev, 916 "Invalid usecs_high value, usecs_high range is 0-%d\n", 917 HNS3_INT_RL_MAX); 918 return -EINVAL; 919 } 920 921 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high); 922 if (rl != cmd->rx_coalesce_usecs_high) { 923 netdev_info(netdev, 924 "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n", 925 cmd->rx_coalesce_usecs_high, rl); 926 } 927 928 return 0; 929 } 930 931 static int hns3_check_coalesce_para(struct net_device *netdev, 932 struct ethtool_coalesce *cmd) 933 { 934 int ret; 935 936 ret = hns3_check_gl_coalesce_para(netdev, cmd); 937 if (ret) { 938 netdev_err(netdev, 939 "Check gl coalesce param fail. ret = %d\n", ret); 940 return ret; 941 } 942 943 ret = hns3_check_rl_coalesce_para(netdev, cmd); 944 if (ret) { 945 netdev_err(netdev, 946 "Check rl coalesce param fail. ret = %d\n", ret); 947 return ret; 948 } 949 950 if (cmd->use_adaptive_tx_coalesce == 1 || 951 cmd->use_adaptive_rx_coalesce == 1) { 952 netdev_info(netdev, 953 "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n", 954 cmd->use_adaptive_tx_coalesce, 955 cmd->use_adaptive_rx_coalesce); 956 } 957 958 return 0; 959 } 960 961 static void hns3_set_coalesce_per_queue(struct net_device *netdev, 962 struct ethtool_coalesce *cmd, 963 u32 queue) 964 { 965 struct hns3_enet_tqp_vector *tx_vector, *rx_vector; 966 struct hns3_nic_priv *priv = netdev_priv(netdev); 967 struct hnae3_handle *h = priv->ae_handle; 968 int queue_num = h->kinfo.num_tqps; 969 970 tx_vector = priv->ring_data[queue].ring->tqp_vector; 971 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector; 972 973 tx_vector->tx_group.coal.gl_adapt_enable = 974 cmd->use_adaptive_tx_coalesce; 975 rx_vector->rx_group.coal.gl_adapt_enable = 976 cmd->use_adaptive_rx_coalesce; 977 978 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs; 979 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs; 980 981 hns3_set_vector_coalesce_tx_gl(tx_vector, 982 tx_vector->tx_group.coal.int_gl); 983 hns3_set_vector_coalesce_rx_gl(rx_vector, 984 rx_vector->rx_group.coal.int_gl); 985 986 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting); 987 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting); 988 } 989 990 static int hns3_set_coalesce(struct net_device *netdev, 991 struct ethtool_coalesce *cmd) 992 { 993 struct hnae3_handle *h = hns3_get_handle(netdev); 994 u16 queue_num = h->kinfo.num_tqps; 995 int ret; 996 int i; 997 998 ret = hns3_check_coalesce_para(netdev, cmd); 999 if (ret) 1000 return ret; 1001 1002 h->kinfo.int_rl_setting = 1003 hns3_rl_round_down(cmd->rx_coalesce_usecs_high); 1004 1005 for (i = 0; i < queue_num; i++) 1006 hns3_set_coalesce_per_queue(netdev, cmd, i); 1007 1008 return 0; 1009 } 1010 1011 static int hns3_get_regs_len(struct net_device *netdev) 1012 { 1013 struct hnae3_handle *h = hns3_get_handle(netdev); 1014 1015 if (!h->ae_algo->ops->get_regs_len) 1016 return -EOPNOTSUPP; 1017 1018 return h->ae_algo->ops->get_regs_len(h); 1019 } 1020 1021 static void hns3_get_regs(struct net_device *netdev, 1022 struct ethtool_regs *cmd, void *data) 1023 { 1024 struct hnae3_handle *h = hns3_get_handle(netdev); 1025 1026 if (!h->ae_algo->ops->get_regs) 1027 return; 1028 1029 h->ae_algo->ops->get_regs(h, &cmd->version, data); 1030 } 1031 1032 static int hns3_set_phys_id(struct net_device *netdev, 1033 enum ethtool_phys_id_state state) 1034 { 1035 struct hnae3_handle *h = hns3_get_handle(netdev); 1036 1037 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id) 1038 return -EOPNOTSUPP; 1039 1040 return h->ae_algo->ops->set_led_id(h, state); 1041 } 1042 1043 static const struct ethtool_ops hns3vf_ethtool_ops = { 1044 .get_drvinfo = hns3_get_drvinfo, 1045 .get_ringparam = hns3_get_ringparam, 1046 .set_ringparam = hns3_set_ringparam, 1047 .get_strings = hns3_get_strings, 1048 .get_ethtool_stats = hns3_get_stats, 1049 .get_sset_count = hns3_get_sset_count, 1050 .get_rxnfc = hns3_get_rxnfc, 1051 .get_rxfh_key_size = hns3_get_rss_key_size, 1052 .get_rxfh_indir_size = hns3_get_rss_indir_size, 1053 .get_rxfh = hns3_get_rss, 1054 .set_rxfh = hns3_set_rss, 1055 .get_link_ksettings = hns3_get_link_ksettings, 1056 .get_channels = hns3_get_channels, 1057 .get_coalesce = hns3_get_coalesce, 1058 .set_coalesce = hns3_set_coalesce, 1059 .get_link = hns3_get_link, 1060 }; 1061 1062 static const struct ethtool_ops hns3_ethtool_ops = { 1063 .self_test = hns3_self_test, 1064 .get_drvinfo = hns3_get_drvinfo, 1065 .get_link = hns3_get_link, 1066 .get_ringparam = hns3_get_ringparam, 1067 .set_ringparam = hns3_set_ringparam, 1068 .get_pauseparam = hns3_get_pauseparam, 1069 .set_pauseparam = hns3_set_pauseparam, 1070 .get_strings = hns3_get_strings, 1071 .get_ethtool_stats = hns3_get_stats, 1072 .get_sset_count = hns3_get_sset_count, 1073 .get_rxnfc = hns3_get_rxnfc, 1074 .set_rxnfc = hns3_set_rxnfc, 1075 .get_rxfh_key_size = hns3_get_rss_key_size, 1076 .get_rxfh_indir_size = hns3_get_rss_indir_size, 1077 .get_rxfh = hns3_get_rss, 1078 .set_rxfh = hns3_set_rss, 1079 .get_link_ksettings = hns3_get_link_ksettings, 1080 .set_link_ksettings = hns3_set_link_ksettings, 1081 .nway_reset = hns3_nway_reset, 1082 .get_channels = hns3_get_channels, 1083 .set_channels = hns3_set_channels, 1084 .get_coalesce = hns3_get_coalesce, 1085 .set_coalesce = hns3_set_coalesce, 1086 .get_regs_len = hns3_get_regs_len, 1087 .get_regs = hns3_get_regs, 1088 .set_phys_id = hns3_set_phys_id, 1089 }; 1090 1091 void hns3_ethtool_set_ops(struct net_device *netdev) 1092 { 1093 struct hnae3_handle *h = hns3_get_handle(netdev); 1094 1095 if (h->flags & HNAE3_SUPPORT_VF) 1096 netdev->ethtool_ops = &hns3vf_ethtool_ops; 1097 else 1098 netdev->ethtool_ops = &hns3_ethtool_ops; 1099 } 1100