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