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 #include <linux/sfp.h> 8 9 #include "hns3_enet.h" 10 11 struct hns3_stats { 12 char stats_string[ETH_GSTRING_LEN]; 13 int stats_offset; 14 }; 15 16 struct hns3_sfp_type { 17 u8 type; 18 u8 ext_type; 19 }; 20 21 struct hns3_pflag_desc { 22 char name[ETH_GSTRING_LEN]; 23 void (*handler)(struct net_device *netdev, bool enable); 24 }; 25 26 /* tqp related stats */ 27 #define HNS3_TQP_STAT(_string, _member) { \ 28 .stats_string = _string, \ 29 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\ 30 offsetof(struct ring_stats, _member), \ 31 } 32 33 static const struct hns3_stats hns3_txq_stats[] = { 34 /* Tx per-queue statistics */ 35 HNS3_TQP_STAT("dropped", sw_err_cnt), 36 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt), 37 HNS3_TQP_STAT("packets", tx_pkts), 38 HNS3_TQP_STAT("bytes", tx_bytes), 39 HNS3_TQP_STAT("more", tx_more), 40 HNS3_TQP_STAT("wake", restart_queue), 41 HNS3_TQP_STAT("busy", tx_busy), 42 HNS3_TQP_STAT("copy", tx_copy), 43 HNS3_TQP_STAT("vlan_err", tx_vlan_err), 44 HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err), 45 HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err), 46 HNS3_TQP_STAT("tso_err", tx_tso_err), 47 }; 48 49 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats) 50 51 static const struct hns3_stats hns3_rxq_stats[] = { 52 /* Rx per-queue statistics */ 53 HNS3_TQP_STAT("dropped", sw_err_cnt), 54 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt), 55 HNS3_TQP_STAT("packets", rx_pkts), 56 HNS3_TQP_STAT("bytes", rx_bytes), 57 HNS3_TQP_STAT("errors", rx_err_cnt), 58 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt), 59 HNS3_TQP_STAT("err_pkt_len", err_pkt_len), 60 HNS3_TQP_STAT("err_bd_num", err_bd_num), 61 HNS3_TQP_STAT("l2_err", l2_err), 62 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err), 63 HNS3_TQP_STAT("csum_complete", csum_complete), 64 HNS3_TQP_STAT("multicast", rx_multicast), 65 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg), 66 }; 67 68 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags) 69 70 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats) 71 72 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT) 73 74 #define HNS3_SELF_TEST_TYPE_NUM 4 75 #define HNS3_NIC_LB_TEST_PKT_NUM 1 76 #define HNS3_NIC_LB_TEST_RING_ID 0 77 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128 78 #define HNS3_NIC_LB_SETUP_USEC 10000 79 80 /* Nic loopback test err */ 81 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1 82 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2 83 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3 84 85 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en) 86 { 87 struct hnae3_handle *h = hns3_get_handle(ndev); 88 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 89 bool vlan_filter_enable; 90 int ret; 91 92 if (!h->ae_algo->ops->set_loopback || 93 !h->ae_algo->ops->set_promisc_mode) 94 return -EOPNOTSUPP; 95 96 switch (loop) { 97 case HNAE3_LOOP_SERIAL_SERDES: 98 case HNAE3_LOOP_PARALLEL_SERDES: 99 case HNAE3_LOOP_APP: 100 case HNAE3_LOOP_PHY: 101 ret = h->ae_algo->ops->set_loopback(h, loop, en); 102 break; 103 default: 104 ret = -ENOTSUPP; 105 break; 106 } 107 108 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) 109 return ret; 110 111 if (en) { 112 h->ae_algo->ops->set_promisc_mode(h, true, true); 113 } else { 114 /* recover promisc mode before loopback test */ 115 hns3_request_update_promisc_mode(h); 116 vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true; 117 hns3_enable_vlan_filter(ndev, vlan_filter_enable); 118 } 119 120 return ret; 121 } 122 123 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode) 124 { 125 struct hnae3_handle *h = hns3_get_handle(ndev); 126 int ret; 127 128 ret = hns3_nic_reset_all_ring(h); 129 if (ret) 130 return ret; 131 132 ret = hns3_lp_setup(ndev, loop_mode, true); 133 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2); 134 135 return ret; 136 } 137 138 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode) 139 { 140 int ret; 141 142 ret = hns3_lp_setup(ndev, loop_mode, false); 143 if (ret) { 144 netdev_err(ndev, "lb_setup return error: %d\n", ret); 145 return ret; 146 } 147 148 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2); 149 150 return 0; 151 } 152 153 static void hns3_lp_setup_skb(struct sk_buff *skb) 154 { 155 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f 156 157 struct net_device *ndev = skb->dev; 158 struct hnae3_handle *handle; 159 struct hnae3_ae_dev *ae_dev; 160 unsigned char *packet; 161 struct ethhdr *ethh; 162 unsigned int i; 163 164 skb_reserve(skb, NET_IP_ALIGN); 165 ethh = skb_put(skb, sizeof(struct ethhdr)); 166 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE); 167 168 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN); 169 170 /* The dst mac addr of loopback packet is the same as the host' 171 * mac addr, the SSU component may loop back the packet to host 172 * before the packet reaches mac or serdes, which will defect 173 * the purpose of mac or serdes selftest. 174 */ 175 handle = hns3_get_handle(ndev); 176 ae_dev = pci_get_drvdata(handle->pdev); 177 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2) 178 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR; 179 eth_zero_addr(ethh->h_source); 180 ethh->h_proto = htons(ETH_P_ARP); 181 skb_reset_mac_header(skb); 182 183 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++) 184 packet[i] = (unsigned char)(i & 0xff); 185 } 186 187 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring, 188 struct sk_buff *skb) 189 { 190 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector; 191 unsigned char *packet = skb->data; 192 u32 len = skb_headlen(skb); 193 u32 i; 194 195 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE); 196 197 for (i = 0; i < len; i++) 198 if (packet[i] != (unsigned char)(i & 0xff)) 199 break; 200 201 /* The packet is correctly received */ 202 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE) 203 tqp_vector->rx_group.total_packets++; 204 else 205 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1, 206 skb->data, len, true); 207 208 dev_kfree_skb_any(skb); 209 } 210 211 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget) 212 { 213 struct hnae3_handle *h = priv->ae_handle; 214 struct hnae3_knic_private_info *kinfo; 215 u32 i, rcv_good_pkt_total = 0; 216 217 kinfo = &h->kinfo; 218 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) { 219 struct hns3_enet_ring *ring = &priv->ring[i]; 220 struct hns3_enet_ring_group *rx_group; 221 u64 pre_rx_pkt; 222 223 rx_group = &ring->tqp_vector->rx_group; 224 pre_rx_pkt = rx_group->total_packets; 225 226 preempt_disable(); 227 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data); 228 preempt_enable(); 229 230 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt); 231 rx_group->total_packets = pre_rx_pkt; 232 } 233 return rcv_good_pkt_total; 234 } 235 236 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid, 237 u32 end_ringid, u32 budget) 238 { 239 u32 i; 240 241 for (i = start_ringid; i <= end_ringid; i++) { 242 struct hns3_enet_ring *ring = &priv->ring[i]; 243 244 hns3_clean_tx_ring(ring, 0); 245 } 246 } 247 248 /** 249 * hns3_lp_run_test - run loopback test 250 * @ndev: net device 251 * @mode: loopback type 252 */ 253 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode) 254 { 255 struct hns3_nic_priv *priv = netdev_priv(ndev); 256 struct sk_buff *skb; 257 u32 i, good_cnt; 258 int ret_val = 0; 259 260 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN, 261 GFP_KERNEL); 262 if (!skb) 263 return HNS3_NIC_LB_TEST_NO_MEM_ERR; 264 265 skb->dev = ndev; 266 hns3_lp_setup_skb(skb); 267 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID; 268 269 good_cnt = 0; 270 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) { 271 netdev_tx_t tx_ret; 272 273 skb_get(skb); 274 tx_ret = hns3_nic_net_xmit(skb, ndev); 275 if (tx_ret == NETDEV_TX_OK) { 276 good_cnt++; 277 } else { 278 kfree_skb(skb); 279 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n", 280 tx_ret); 281 } 282 } 283 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) { 284 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR; 285 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n", 286 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM); 287 goto out; 288 } 289 290 /* Allow 200 milliseconds for packets to go from Tx to Rx */ 291 msleep(200); 292 293 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM); 294 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) { 295 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR; 296 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n", 297 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM); 298 } 299 300 out: 301 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID, 302 HNS3_NIC_LB_TEST_RING_ID, 303 HNS3_NIC_LB_TEST_PKT_NUM); 304 305 kfree_skb(skb); 306 return ret_val; 307 } 308 309 /** 310 * hns3_nic_self_test - self test 311 * @ndev: net device 312 * @eth_test: test cmd 313 * @data: test result 314 */ 315 static void hns3_self_test(struct net_device *ndev, 316 struct ethtool_test *eth_test, u64 *data) 317 { 318 struct hns3_nic_priv *priv = netdev_priv(ndev); 319 struct hnae3_handle *h = priv->ae_handle; 320 int st_param[HNS3_SELF_TEST_TYPE_NUM][2]; 321 bool if_running = netif_running(ndev); 322 int test_index = 0; 323 u32 i; 324 325 if (hns3_nic_resetting(ndev)) { 326 netdev_err(ndev, "dev resetting!"); 327 return; 328 } 329 330 /* Only do offline selftest, or pass by default */ 331 if (eth_test->flags != ETH_TEST_FL_OFFLINE) 332 return; 333 334 netif_dbg(h, drv, ndev, "self test start"); 335 336 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP; 337 st_param[HNAE3_LOOP_APP][1] = 338 h->flags & HNAE3_SUPPORT_APP_LOOPBACK; 339 340 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES; 341 st_param[HNAE3_LOOP_SERIAL_SERDES][1] = 342 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK; 343 344 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] = 345 HNAE3_LOOP_PARALLEL_SERDES; 346 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] = 347 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK; 348 349 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY; 350 st_param[HNAE3_LOOP_PHY][1] = 351 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK; 352 353 if (if_running) 354 ndev->netdev_ops->ndo_stop(ndev); 355 356 #if IS_ENABLED(CONFIG_VLAN_8021Q) 357 /* Disable the vlan filter for selftest does not support it */ 358 if (h->ae_algo->ops->enable_vlan_filter) 359 h->ae_algo->ops->enable_vlan_filter(h, false); 360 #endif 361 362 /* Tell firmware to stop mac autoneg before loopback test start, 363 * otherwise loopback test may be failed when the port is still 364 * negotiating. 365 */ 366 if (h->ae_algo->ops->halt_autoneg) 367 h->ae_algo->ops->halt_autoneg(h, true); 368 369 set_bit(HNS3_NIC_STATE_TESTING, &priv->state); 370 371 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) { 372 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0]; 373 374 if (!st_param[i][1]) 375 continue; 376 377 data[test_index] = hns3_lp_up(ndev, loop_type); 378 if (!data[test_index]) 379 data[test_index] = hns3_lp_run_test(ndev, loop_type); 380 381 hns3_lp_down(ndev, loop_type); 382 383 if (data[test_index]) 384 eth_test->flags |= ETH_TEST_FL_FAILED; 385 386 test_index++; 387 } 388 389 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state); 390 391 if (h->ae_algo->ops->halt_autoneg) 392 h->ae_algo->ops->halt_autoneg(h, false); 393 394 #if IS_ENABLED(CONFIG_VLAN_8021Q) 395 if (h->ae_algo->ops->enable_vlan_filter) 396 h->ae_algo->ops->enable_vlan_filter(h, true); 397 #endif 398 399 if (if_running) 400 ndev->netdev_ops->ndo_open(ndev); 401 402 netif_dbg(h, drv, ndev, "self test end\n"); 403 } 404 405 static void hns3_update_limit_promisc_mode(struct net_device *netdev, 406 bool enable) 407 { 408 struct hnae3_handle *handle = hns3_get_handle(netdev); 409 410 if (enable) 411 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags); 412 else 413 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags); 414 415 hns3_request_update_promisc_mode(handle); 416 } 417 418 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = { 419 { "limit_promisc", hns3_update_limit_promisc_mode } 420 }; 421 422 static int hns3_get_sset_count(struct net_device *netdev, int stringset) 423 { 424 struct hnae3_handle *h = hns3_get_handle(netdev); 425 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 426 427 if (!ops->get_sset_count) 428 return -EOPNOTSUPP; 429 430 switch (stringset) { 431 case ETH_SS_STATS: 432 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) + 433 ops->get_sset_count(h, stringset)); 434 435 case ETH_SS_TEST: 436 return ops->get_sset_count(h, stringset); 437 438 case ETH_SS_PRIV_FLAGS: 439 return HNAE3_PFLAG_MAX; 440 441 default: 442 return -EOPNOTSUPP; 443 } 444 } 445 446 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats, 447 u32 stat_count, u32 num_tqps, const char *prefix) 448 { 449 #define MAX_PREFIX_SIZE (6 + 4) 450 u32 size_left; 451 u32 i, j; 452 u32 n1; 453 454 for (i = 0; i < num_tqps; i++) { 455 for (j = 0; j < stat_count; j++) { 456 data[ETH_GSTRING_LEN - 1] = '\0'; 457 458 /* first, prepend the prefix string */ 459 n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_", 460 prefix, i); 461 size_left = (ETH_GSTRING_LEN - 1) - n1; 462 463 /* now, concatenate the stats string to it */ 464 strncat(data, stats[j].stats_string, size_left); 465 data += ETH_GSTRING_LEN; 466 } 467 } 468 469 return data; 470 } 471 472 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data) 473 { 474 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 475 const char tx_prefix[] = "txq"; 476 const char rx_prefix[] = "rxq"; 477 478 /* get strings for Tx */ 479 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT, 480 kinfo->num_tqps, tx_prefix); 481 482 /* get strings for Rx */ 483 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT, 484 kinfo->num_tqps, rx_prefix); 485 486 return data; 487 } 488 489 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data) 490 { 491 struct hnae3_handle *h = hns3_get_handle(netdev); 492 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 493 char *buff = (char *)data; 494 int i; 495 496 if (!ops->get_strings) 497 return; 498 499 switch (stringset) { 500 case ETH_SS_STATS: 501 buff = hns3_get_strings_tqps(h, buff); 502 ops->get_strings(h, stringset, (u8 *)buff); 503 break; 504 case ETH_SS_TEST: 505 ops->get_strings(h, stringset, data); 506 break; 507 case ETH_SS_PRIV_FLAGS: 508 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) { 509 snprintf(buff, ETH_GSTRING_LEN, "%s", 510 hns3_priv_flags[i].name); 511 buff += ETH_GSTRING_LEN; 512 } 513 break; 514 default: 515 break; 516 } 517 } 518 519 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data) 520 { 521 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv; 522 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 523 struct hns3_enet_ring *ring; 524 u8 *stat; 525 int i, j; 526 527 /* get stats for Tx */ 528 for (i = 0; i < kinfo->num_tqps; i++) { 529 ring = &nic_priv->ring[i]; 530 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) { 531 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset; 532 *data++ = *(u64 *)stat; 533 } 534 } 535 536 /* get stats for Rx */ 537 for (i = 0; i < kinfo->num_tqps; i++) { 538 ring = &nic_priv->ring[i + kinfo->num_tqps]; 539 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) { 540 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset; 541 *data++ = *(u64 *)stat; 542 } 543 } 544 545 return data; 546 } 547 548 /* hns3_get_stats - get detail statistics. 549 * @netdev: net device 550 * @stats: statistics info. 551 * @data: statistics data. 552 */ 553 static void hns3_get_stats(struct net_device *netdev, 554 struct ethtool_stats *stats, u64 *data) 555 { 556 struct hnae3_handle *h = hns3_get_handle(netdev); 557 u64 *p = data; 558 559 if (hns3_nic_resetting(netdev)) { 560 netdev_err(netdev, "dev resetting, could not get stats\n"); 561 return; 562 } 563 564 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) { 565 netdev_err(netdev, "could not get any statistics\n"); 566 return; 567 } 568 569 h->ae_algo->ops->update_stats(h, &netdev->stats); 570 571 /* get per-queue stats */ 572 p = hns3_get_stats_tqps(h, p); 573 574 /* get MAC & other misc hardware stats */ 575 h->ae_algo->ops->get_stats(h, p); 576 } 577 578 static void hns3_get_drvinfo(struct net_device *netdev, 579 struct ethtool_drvinfo *drvinfo) 580 { 581 struct hns3_nic_priv *priv = netdev_priv(netdev); 582 struct hnae3_handle *h = priv->ae_handle; 583 u32 fw_version; 584 585 if (!h->ae_algo->ops->get_fw_version) { 586 netdev_err(netdev, "could not get fw version!\n"); 587 return; 588 } 589 590 strncpy(drvinfo->driver, h->pdev->driver->name, 591 sizeof(drvinfo->driver)); 592 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0'; 593 594 strncpy(drvinfo->bus_info, pci_name(h->pdev), 595 sizeof(drvinfo->bus_info)); 596 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0'; 597 598 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h); 599 600 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), 601 "%lu.%lu.%lu.%lu", 602 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK, 603 HNAE3_FW_VERSION_BYTE3_SHIFT), 604 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK, 605 HNAE3_FW_VERSION_BYTE2_SHIFT), 606 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK, 607 HNAE3_FW_VERSION_BYTE1_SHIFT), 608 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK, 609 HNAE3_FW_VERSION_BYTE0_SHIFT)); 610 } 611 612 static u32 hns3_get_link(struct net_device *netdev) 613 { 614 struct hnae3_handle *h = hns3_get_handle(netdev); 615 616 if (h->ae_algo->ops->get_status) 617 return h->ae_algo->ops->get_status(h); 618 else 619 return 0; 620 } 621 622 static void hns3_get_ringparam(struct net_device *netdev, 623 struct ethtool_ringparam *param) 624 { 625 struct hns3_nic_priv *priv = netdev_priv(netdev); 626 struct hnae3_handle *h = priv->ae_handle; 627 int queue_num = h->kinfo.num_tqps; 628 629 if (hns3_nic_resetting(netdev)) { 630 netdev_err(netdev, "dev resetting!"); 631 return; 632 } 633 634 param->tx_max_pending = HNS3_RING_MAX_PENDING; 635 param->rx_max_pending = HNS3_RING_MAX_PENDING; 636 637 param->tx_pending = priv->ring[0].desc_num; 638 param->rx_pending = priv->ring[queue_num].desc_num; 639 } 640 641 static void hns3_get_pauseparam(struct net_device *netdev, 642 struct ethtool_pauseparam *param) 643 { 644 struct hnae3_handle *h = hns3_get_handle(netdev); 645 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 646 647 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps)) 648 return; 649 650 if (h->ae_algo->ops->get_pauseparam) 651 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg, 652 ¶m->rx_pause, ¶m->tx_pause); 653 } 654 655 static int hns3_set_pauseparam(struct net_device *netdev, 656 struct ethtool_pauseparam *param) 657 { 658 struct hnae3_handle *h = hns3_get_handle(netdev); 659 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 660 661 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps)) 662 return -EOPNOTSUPP; 663 664 netif_dbg(h, drv, netdev, 665 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n", 666 param->autoneg, param->rx_pause, param->tx_pause); 667 668 if (h->ae_algo->ops->set_pauseparam) 669 return h->ae_algo->ops->set_pauseparam(h, param->autoneg, 670 param->rx_pause, 671 param->tx_pause); 672 return -EOPNOTSUPP; 673 } 674 675 static void hns3_get_ksettings(struct hnae3_handle *h, 676 struct ethtool_link_ksettings *cmd) 677 { 678 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 679 680 /* 1.auto_neg & speed & duplex from cmd */ 681 if (ops->get_ksettings_an_result) 682 ops->get_ksettings_an_result(h, 683 &cmd->base.autoneg, 684 &cmd->base.speed, 685 &cmd->base.duplex); 686 687 /* 2.get link mode */ 688 if (ops->get_link_mode) 689 ops->get_link_mode(h, 690 cmd->link_modes.supported, 691 cmd->link_modes.advertising); 692 693 /* 3.mdix_ctrl&mdix get from phy reg */ 694 if (ops->get_mdix_mode) 695 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl, 696 &cmd->base.eth_tp_mdix); 697 } 698 699 static int hns3_get_link_ksettings(struct net_device *netdev, 700 struct ethtool_link_ksettings *cmd) 701 { 702 struct hnae3_handle *h = hns3_get_handle(netdev); 703 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 704 const struct hnae3_ae_ops *ops; 705 u8 module_type; 706 u8 media_type; 707 u8 link_stat; 708 709 ops = h->ae_algo->ops; 710 if (ops->get_media_type) 711 ops->get_media_type(h, &media_type, &module_type); 712 else 713 return -EOPNOTSUPP; 714 715 switch (media_type) { 716 case HNAE3_MEDIA_TYPE_NONE: 717 cmd->base.port = PORT_NONE; 718 hns3_get_ksettings(h, cmd); 719 break; 720 case HNAE3_MEDIA_TYPE_FIBER: 721 if (module_type == HNAE3_MODULE_TYPE_CR) 722 cmd->base.port = PORT_DA; 723 else 724 cmd->base.port = PORT_FIBRE; 725 726 hns3_get_ksettings(h, cmd); 727 break; 728 case HNAE3_MEDIA_TYPE_BACKPLANE: 729 cmd->base.port = PORT_NONE; 730 hns3_get_ksettings(h, cmd); 731 break; 732 case HNAE3_MEDIA_TYPE_COPPER: 733 cmd->base.port = PORT_TP; 734 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) && 735 ops->get_phy_link_ksettings) 736 ops->get_phy_link_ksettings(h, cmd); 737 else if (!netdev->phydev) 738 hns3_get_ksettings(h, cmd); 739 else 740 phy_ethtool_ksettings_get(netdev->phydev, cmd); 741 break; 742 default: 743 744 netdev_warn(netdev, "Unknown media type"); 745 return 0; 746 } 747 748 /* mdio_support */ 749 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22; 750 751 link_stat = hns3_get_link(netdev); 752 if (!link_stat) { 753 cmd->base.speed = SPEED_UNKNOWN; 754 cmd->base.duplex = DUPLEX_UNKNOWN; 755 } 756 757 return 0; 758 } 759 760 static int hns3_check_ksettings_param(const struct net_device *netdev, 761 const struct ethtool_link_ksettings *cmd) 762 { 763 struct hnae3_handle *handle = hns3_get_handle(netdev); 764 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 765 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN; 766 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN; 767 u8 autoneg; 768 u32 speed; 769 u8 duplex; 770 int ret; 771 772 /* hw doesn't support use specified speed and duplex to negotiate, 773 * unnecessary to check them when autoneg on. 774 */ 775 if (cmd->base.autoneg) 776 return 0; 777 778 if (ops->get_ksettings_an_result) { 779 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex); 780 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed && 781 cmd->base.duplex == duplex) 782 return 0; 783 } 784 785 if (ops->get_media_type) 786 ops->get_media_type(handle, &media_type, &module_type); 787 788 if (cmd->base.duplex == DUPLEX_HALF && 789 media_type != HNAE3_MEDIA_TYPE_COPPER) { 790 netdev_err(netdev, 791 "only copper port supports half duplex!"); 792 return -EINVAL; 793 } 794 795 if (ops->check_port_speed) { 796 ret = ops->check_port_speed(handle, cmd->base.speed); 797 if (ret) { 798 netdev_err(netdev, "unsupported speed\n"); 799 return ret; 800 } 801 } 802 803 return 0; 804 } 805 806 static int hns3_set_link_ksettings(struct net_device *netdev, 807 const struct ethtool_link_ksettings *cmd) 808 { 809 struct hnae3_handle *handle = hns3_get_handle(netdev); 810 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 811 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 812 int ret; 813 814 /* Chip don't support this mode. */ 815 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF) 816 return -EINVAL; 817 818 netif_dbg(handle, drv, netdev, 819 "set link(%s): autoneg=%u, speed=%u, duplex=%u\n", 820 netdev->phydev ? "phy" : "mac", 821 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex); 822 823 /* Only support ksettings_set for netdev with phy attached for now */ 824 if (netdev->phydev) { 825 if (cmd->base.speed == SPEED_1000 && 826 cmd->base.autoneg == AUTONEG_DISABLE) 827 return -EINVAL; 828 829 return phy_ethtool_ksettings_set(netdev->phydev, cmd); 830 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) && 831 ops->set_phy_link_ksettings) { 832 return ops->set_phy_link_ksettings(handle, cmd); 833 } 834 835 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2) 836 return -EOPNOTSUPP; 837 838 ret = hns3_check_ksettings_param(netdev, cmd); 839 if (ret) 840 return ret; 841 842 if (ops->set_autoneg) { 843 ret = ops->set_autoneg(handle, cmd->base.autoneg); 844 if (ret) 845 return ret; 846 } 847 848 /* hw doesn't support use specified speed and duplex to negotiate, 849 * ignore them when autoneg on. 850 */ 851 if (cmd->base.autoneg) { 852 netdev_info(netdev, 853 "autoneg is on, ignore the speed and duplex\n"); 854 return 0; 855 } 856 857 if (ops->cfg_mac_speed_dup_h) 858 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed, 859 cmd->base.duplex); 860 861 return ret; 862 } 863 864 static u32 hns3_get_rss_key_size(struct net_device *netdev) 865 { 866 struct hnae3_handle *h = hns3_get_handle(netdev); 867 868 if (!h->ae_algo->ops->get_rss_key_size) 869 return 0; 870 871 return h->ae_algo->ops->get_rss_key_size(h); 872 } 873 874 static u32 hns3_get_rss_indir_size(struct net_device *netdev) 875 { 876 struct hnae3_handle *h = hns3_get_handle(netdev); 877 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 878 879 return ae_dev->dev_specs.rss_ind_tbl_size; 880 } 881 882 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key, 883 u8 *hfunc) 884 { 885 struct hnae3_handle *h = hns3_get_handle(netdev); 886 887 if (!h->ae_algo->ops->get_rss) 888 return -EOPNOTSUPP; 889 890 return h->ae_algo->ops->get_rss(h, indir, key, hfunc); 891 } 892 893 static int hns3_set_rss(struct net_device *netdev, const u32 *indir, 894 const u8 *key, const u8 hfunc) 895 { 896 struct hnae3_handle *h = hns3_get_handle(netdev); 897 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 898 899 if (!h->ae_algo->ops->set_rss) 900 return -EOPNOTSUPP; 901 902 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 && 903 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE && 904 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) { 905 netdev_err(netdev, "hash func not supported\n"); 906 return -EOPNOTSUPP; 907 } 908 909 if (!indir) { 910 netdev_err(netdev, 911 "set rss failed for indir is empty\n"); 912 return -EOPNOTSUPP; 913 } 914 915 return h->ae_algo->ops->set_rss(h, indir, key, hfunc); 916 } 917 918 static int hns3_get_rxnfc(struct net_device *netdev, 919 struct ethtool_rxnfc *cmd, 920 u32 *rule_locs) 921 { 922 struct hnae3_handle *h = hns3_get_handle(netdev); 923 924 switch (cmd->cmd) { 925 case ETHTOOL_GRXRINGS: 926 cmd->data = h->kinfo.num_tqps; 927 return 0; 928 case ETHTOOL_GRXFH: 929 if (h->ae_algo->ops->get_rss_tuple) 930 return h->ae_algo->ops->get_rss_tuple(h, cmd); 931 return -EOPNOTSUPP; 932 case ETHTOOL_GRXCLSRLCNT: 933 if (h->ae_algo->ops->get_fd_rule_cnt) 934 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd); 935 return -EOPNOTSUPP; 936 case ETHTOOL_GRXCLSRULE: 937 if (h->ae_algo->ops->get_fd_rule_info) 938 return h->ae_algo->ops->get_fd_rule_info(h, cmd); 939 return -EOPNOTSUPP; 940 case ETHTOOL_GRXCLSRLALL: 941 if (h->ae_algo->ops->get_fd_all_rules) 942 return h->ae_algo->ops->get_fd_all_rules(h, cmd, 943 rule_locs); 944 return -EOPNOTSUPP; 945 default: 946 return -EOPNOTSUPP; 947 } 948 } 949 950 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv, 951 u32 tx_desc_num, u32 rx_desc_num) 952 { 953 struct hnae3_handle *h = priv->ae_handle; 954 int i; 955 956 h->kinfo.num_tx_desc = tx_desc_num; 957 h->kinfo.num_rx_desc = rx_desc_num; 958 959 for (i = 0; i < h->kinfo.num_tqps; i++) { 960 priv->ring[i].desc_num = tx_desc_num; 961 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num; 962 } 963 } 964 965 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv) 966 { 967 struct hnae3_handle *handle = priv->ae_handle; 968 struct hns3_enet_ring *tmp_rings; 969 int i; 970 971 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2, 972 sizeof(struct hns3_enet_ring), GFP_KERNEL); 973 if (!tmp_rings) 974 return NULL; 975 976 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) { 977 memcpy(&tmp_rings[i], &priv->ring[i], 978 sizeof(struct hns3_enet_ring)); 979 tmp_rings[i].skb = NULL; 980 } 981 982 return tmp_rings; 983 } 984 985 static int hns3_check_ringparam(struct net_device *ndev, 986 struct ethtool_ringparam *param) 987 { 988 if (hns3_nic_resetting(ndev)) 989 return -EBUSY; 990 991 if (param->rx_mini_pending || param->rx_jumbo_pending) 992 return -EINVAL; 993 994 if (param->tx_pending > HNS3_RING_MAX_PENDING || 995 param->tx_pending < HNS3_RING_MIN_PENDING || 996 param->rx_pending > HNS3_RING_MAX_PENDING || 997 param->rx_pending < HNS3_RING_MIN_PENDING) { 998 netdev_err(ndev, "Queue depth out of range [%d-%d]\n", 999 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING); 1000 return -EINVAL; 1001 } 1002 1003 return 0; 1004 } 1005 1006 static int hns3_set_ringparam(struct net_device *ndev, 1007 struct ethtool_ringparam *param) 1008 { 1009 struct hns3_nic_priv *priv = netdev_priv(ndev); 1010 struct hnae3_handle *h = priv->ae_handle; 1011 struct hns3_enet_ring *tmp_rings; 1012 bool if_running = netif_running(ndev); 1013 u32 old_tx_desc_num, new_tx_desc_num; 1014 u32 old_rx_desc_num, new_rx_desc_num; 1015 u16 queue_num = h->kinfo.num_tqps; 1016 int ret, i; 1017 1018 ret = hns3_check_ringparam(ndev, param); 1019 if (ret) 1020 return ret; 1021 1022 /* Hardware requires that its descriptors must be multiple of eight */ 1023 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE); 1024 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE); 1025 old_tx_desc_num = priv->ring[0].desc_num; 1026 old_rx_desc_num = priv->ring[queue_num].desc_num; 1027 if (old_tx_desc_num == new_tx_desc_num && 1028 old_rx_desc_num == new_rx_desc_num) 1029 return 0; 1030 1031 tmp_rings = hns3_backup_ringparam(priv); 1032 if (!tmp_rings) { 1033 netdev_err(ndev, 1034 "backup ring param failed by allocating memory fail\n"); 1035 return -ENOMEM; 1036 } 1037 1038 netdev_info(ndev, 1039 "Changing Tx/Rx ring depth from %u/%u to %u/%u\n", 1040 old_tx_desc_num, old_rx_desc_num, 1041 new_tx_desc_num, new_rx_desc_num); 1042 1043 if (if_running) 1044 ndev->netdev_ops->ndo_stop(ndev); 1045 1046 hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num); 1047 ret = hns3_init_all_ring(priv); 1048 if (ret) { 1049 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n", 1050 ret); 1051 1052 hns3_change_all_ring_bd_num(priv, old_tx_desc_num, 1053 old_rx_desc_num); 1054 for (i = 0; i < h->kinfo.num_tqps * 2; i++) 1055 memcpy(&priv->ring[i], &tmp_rings[i], 1056 sizeof(struct hns3_enet_ring)); 1057 } else { 1058 for (i = 0; i < h->kinfo.num_tqps * 2; i++) 1059 hns3_fini_ring(&tmp_rings[i]); 1060 } 1061 1062 kfree(tmp_rings); 1063 1064 if (if_running) 1065 ret = ndev->netdev_ops->ndo_open(ndev); 1066 1067 return ret; 1068 } 1069 1070 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) 1071 { 1072 struct hnae3_handle *h = hns3_get_handle(netdev); 1073 1074 switch (cmd->cmd) { 1075 case ETHTOOL_SRXFH: 1076 if (h->ae_algo->ops->set_rss_tuple) 1077 return h->ae_algo->ops->set_rss_tuple(h, cmd); 1078 return -EOPNOTSUPP; 1079 case ETHTOOL_SRXCLSRLINS: 1080 if (h->ae_algo->ops->add_fd_entry) 1081 return h->ae_algo->ops->add_fd_entry(h, cmd); 1082 return -EOPNOTSUPP; 1083 case ETHTOOL_SRXCLSRLDEL: 1084 if (h->ae_algo->ops->del_fd_entry) 1085 return h->ae_algo->ops->del_fd_entry(h, cmd); 1086 return -EOPNOTSUPP; 1087 default: 1088 return -EOPNOTSUPP; 1089 } 1090 } 1091 1092 static int hns3_nway_reset(struct net_device *netdev) 1093 { 1094 struct hnae3_handle *handle = hns3_get_handle(netdev); 1095 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1096 struct phy_device *phy = netdev->phydev; 1097 int autoneg; 1098 1099 if (!netif_running(netdev)) 1100 return 0; 1101 1102 if (hns3_nic_resetting(netdev)) { 1103 netdev_err(netdev, "dev resetting!"); 1104 return -EBUSY; 1105 } 1106 1107 if (!ops->get_autoneg || !ops->restart_autoneg) 1108 return -EOPNOTSUPP; 1109 1110 autoneg = ops->get_autoneg(handle); 1111 if (autoneg != AUTONEG_ENABLE) { 1112 netdev_err(netdev, 1113 "Autoneg is off, don't support to restart it\n"); 1114 return -EINVAL; 1115 } 1116 1117 netif_dbg(handle, drv, netdev, 1118 "nway reset (using %s)\n", phy ? "phy" : "mac"); 1119 1120 if (phy) 1121 return genphy_restart_aneg(phy); 1122 1123 return ops->restart_autoneg(handle); 1124 } 1125 1126 static void hns3_get_channels(struct net_device *netdev, 1127 struct ethtool_channels *ch) 1128 { 1129 struct hnae3_handle *h = hns3_get_handle(netdev); 1130 1131 if (h->ae_algo->ops->get_channels) 1132 h->ae_algo->ops->get_channels(h, ch); 1133 } 1134 1135 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue, 1136 struct ethtool_coalesce *cmd) 1137 { 1138 struct hns3_enet_tqp_vector *tx_vector, *rx_vector; 1139 struct hns3_nic_priv *priv = netdev_priv(netdev); 1140 struct hnae3_handle *h = priv->ae_handle; 1141 u16 queue_num = h->kinfo.num_tqps; 1142 1143 if (hns3_nic_resetting(netdev)) 1144 return -EBUSY; 1145 1146 if (queue >= queue_num) { 1147 netdev_err(netdev, 1148 "Invalid queue value %u! Queue max id=%u\n", 1149 queue, queue_num - 1); 1150 return -EINVAL; 1151 } 1152 1153 tx_vector = priv->ring[queue].tqp_vector; 1154 rx_vector = priv->ring[queue_num + queue].tqp_vector; 1155 1156 cmd->use_adaptive_tx_coalesce = 1157 tx_vector->tx_group.coal.adapt_enable; 1158 cmd->use_adaptive_rx_coalesce = 1159 rx_vector->rx_group.coal.adapt_enable; 1160 1161 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl; 1162 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl; 1163 1164 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting; 1165 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting; 1166 1167 cmd->tx_max_coalesced_frames = tx_vector->tx_group.coal.int_ql; 1168 cmd->rx_max_coalesced_frames = rx_vector->rx_group.coal.int_ql; 1169 1170 return 0; 1171 } 1172 1173 static int hns3_get_coalesce(struct net_device *netdev, 1174 struct ethtool_coalesce *cmd) 1175 { 1176 return hns3_get_coalesce_per_queue(netdev, 0, cmd); 1177 } 1178 1179 static int hns3_check_gl_coalesce_para(struct net_device *netdev, 1180 struct ethtool_coalesce *cmd) 1181 { 1182 struct hnae3_handle *handle = hns3_get_handle(netdev); 1183 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1184 u32 rx_gl, tx_gl; 1185 1186 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) { 1187 netdev_err(netdev, 1188 "invalid rx-usecs value, rx-usecs range is 0-%u\n", 1189 ae_dev->dev_specs.max_int_gl); 1190 return -EINVAL; 1191 } 1192 1193 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) { 1194 netdev_err(netdev, 1195 "invalid tx-usecs value, tx-usecs range is 0-%u\n", 1196 ae_dev->dev_specs.max_int_gl); 1197 return -EINVAL; 1198 } 1199 1200 /* device version above V3(include V3), GL uses 1us unit, 1201 * so the round down is not needed. 1202 */ 1203 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3) 1204 return 0; 1205 1206 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs); 1207 if (rx_gl != cmd->rx_coalesce_usecs) { 1208 netdev_info(netdev, 1209 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n", 1210 cmd->rx_coalesce_usecs, rx_gl); 1211 } 1212 1213 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs); 1214 if (tx_gl != cmd->tx_coalesce_usecs) { 1215 netdev_info(netdev, 1216 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n", 1217 cmd->tx_coalesce_usecs, tx_gl); 1218 } 1219 1220 return 0; 1221 } 1222 1223 static int hns3_check_rl_coalesce_para(struct net_device *netdev, 1224 struct ethtool_coalesce *cmd) 1225 { 1226 u32 rl; 1227 1228 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) { 1229 netdev_err(netdev, 1230 "tx_usecs_high must be same as rx_usecs_high.\n"); 1231 return -EINVAL; 1232 } 1233 1234 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) { 1235 netdev_err(netdev, 1236 "Invalid usecs_high value, usecs_high range is 0-%d\n", 1237 HNS3_INT_RL_MAX); 1238 return -EINVAL; 1239 } 1240 1241 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high); 1242 if (rl != cmd->rx_coalesce_usecs_high) { 1243 netdev_info(netdev, 1244 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n", 1245 cmd->rx_coalesce_usecs_high, rl); 1246 } 1247 1248 return 0; 1249 } 1250 1251 static int hns3_check_ql_coalesce_param(struct net_device *netdev, 1252 struct ethtool_coalesce *cmd) 1253 { 1254 struct hnae3_handle *handle = hns3_get_handle(netdev); 1255 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1256 1257 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) && 1258 !ae_dev->dev_specs.int_ql_max) { 1259 netdev_err(netdev, "coalesced frames is not supported\n"); 1260 return -EOPNOTSUPP; 1261 } 1262 1263 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max || 1264 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) { 1265 netdev_err(netdev, 1266 "invalid coalesced_frames value, range is 0-%u\n", 1267 ae_dev->dev_specs.int_ql_max); 1268 return -ERANGE; 1269 } 1270 1271 return 0; 1272 } 1273 1274 static int hns3_check_coalesce_para(struct net_device *netdev, 1275 struct ethtool_coalesce *cmd) 1276 { 1277 int ret; 1278 1279 ret = hns3_check_gl_coalesce_para(netdev, cmd); 1280 if (ret) { 1281 netdev_err(netdev, 1282 "Check gl coalesce param fail. ret = %d\n", ret); 1283 return ret; 1284 } 1285 1286 ret = hns3_check_rl_coalesce_para(netdev, cmd); 1287 if (ret) { 1288 netdev_err(netdev, 1289 "Check rl coalesce param fail. ret = %d\n", ret); 1290 return ret; 1291 } 1292 1293 ret = hns3_check_ql_coalesce_param(netdev, cmd); 1294 if (ret) 1295 return ret; 1296 1297 if (cmd->use_adaptive_tx_coalesce == 1 || 1298 cmd->use_adaptive_rx_coalesce == 1) { 1299 netdev_info(netdev, 1300 "adaptive-tx=%u and adaptive-rx=%u, tx_usecs or rx_usecs will changed dynamically.\n", 1301 cmd->use_adaptive_tx_coalesce, 1302 cmd->use_adaptive_rx_coalesce); 1303 } 1304 1305 return 0; 1306 } 1307 1308 static void hns3_set_coalesce_per_queue(struct net_device *netdev, 1309 struct ethtool_coalesce *cmd, 1310 u32 queue) 1311 { 1312 struct hns3_enet_tqp_vector *tx_vector, *rx_vector; 1313 struct hns3_nic_priv *priv = netdev_priv(netdev); 1314 struct hnae3_handle *h = priv->ae_handle; 1315 int queue_num = h->kinfo.num_tqps; 1316 1317 tx_vector = priv->ring[queue].tqp_vector; 1318 rx_vector = priv->ring[queue_num + queue].tqp_vector; 1319 1320 tx_vector->tx_group.coal.adapt_enable = 1321 cmd->use_adaptive_tx_coalesce; 1322 rx_vector->rx_group.coal.adapt_enable = 1323 cmd->use_adaptive_rx_coalesce; 1324 1325 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs; 1326 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs; 1327 1328 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames; 1329 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames; 1330 1331 hns3_set_vector_coalesce_tx_gl(tx_vector, 1332 tx_vector->tx_group.coal.int_gl); 1333 hns3_set_vector_coalesce_rx_gl(rx_vector, 1334 rx_vector->rx_group.coal.int_gl); 1335 1336 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting); 1337 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting); 1338 1339 if (tx_vector->tx_group.coal.ql_enable) 1340 hns3_set_vector_coalesce_tx_ql(tx_vector, 1341 tx_vector->tx_group.coal.int_ql); 1342 if (rx_vector->rx_group.coal.ql_enable) 1343 hns3_set_vector_coalesce_rx_ql(rx_vector, 1344 rx_vector->rx_group.coal.int_ql); 1345 } 1346 1347 static int hns3_set_coalesce(struct net_device *netdev, 1348 struct ethtool_coalesce *cmd) 1349 { 1350 struct hnae3_handle *h = hns3_get_handle(netdev); 1351 u16 queue_num = h->kinfo.num_tqps; 1352 int ret; 1353 int i; 1354 1355 if (hns3_nic_resetting(netdev)) 1356 return -EBUSY; 1357 1358 ret = hns3_check_coalesce_para(netdev, cmd); 1359 if (ret) 1360 return ret; 1361 1362 h->kinfo.int_rl_setting = 1363 hns3_rl_round_down(cmd->rx_coalesce_usecs_high); 1364 1365 for (i = 0; i < queue_num; i++) 1366 hns3_set_coalesce_per_queue(netdev, cmd, i); 1367 1368 return 0; 1369 } 1370 1371 static int hns3_get_regs_len(struct net_device *netdev) 1372 { 1373 struct hnae3_handle *h = hns3_get_handle(netdev); 1374 1375 if (!h->ae_algo->ops->get_regs_len) 1376 return -EOPNOTSUPP; 1377 1378 return h->ae_algo->ops->get_regs_len(h); 1379 } 1380 1381 static void hns3_get_regs(struct net_device *netdev, 1382 struct ethtool_regs *cmd, void *data) 1383 { 1384 struct hnae3_handle *h = hns3_get_handle(netdev); 1385 1386 if (!h->ae_algo->ops->get_regs) 1387 return; 1388 1389 h->ae_algo->ops->get_regs(h, &cmd->version, data); 1390 } 1391 1392 static int hns3_set_phys_id(struct net_device *netdev, 1393 enum ethtool_phys_id_state state) 1394 { 1395 struct hnae3_handle *h = hns3_get_handle(netdev); 1396 1397 if (!h->ae_algo->ops->set_led_id) 1398 return -EOPNOTSUPP; 1399 1400 return h->ae_algo->ops->set_led_id(h, state); 1401 } 1402 1403 static u32 hns3_get_msglevel(struct net_device *netdev) 1404 { 1405 struct hnae3_handle *h = hns3_get_handle(netdev); 1406 1407 return h->msg_enable; 1408 } 1409 1410 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level) 1411 { 1412 struct hnae3_handle *h = hns3_get_handle(netdev); 1413 1414 h->msg_enable = msg_level; 1415 } 1416 1417 /* Translate local fec value into ethtool value. */ 1418 static unsigned int loc_to_eth_fec(u8 loc_fec) 1419 { 1420 u32 eth_fec = 0; 1421 1422 if (loc_fec & BIT(HNAE3_FEC_AUTO)) 1423 eth_fec |= ETHTOOL_FEC_AUTO; 1424 if (loc_fec & BIT(HNAE3_FEC_RS)) 1425 eth_fec |= ETHTOOL_FEC_RS; 1426 if (loc_fec & BIT(HNAE3_FEC_BASER)) 1427 eth_fec |= ETHTOOL_FEC_BASER; 1428 1429 /* if nothing is set, then FEC is off */ 1430 if (!eth_fec) 1431 eth_fec = ETHTOOL_FEC_OFF; 1432 1433 return eth_fec; 1434 } 1435 1436 /* Translate ethtool fec value into local value. */ 1437 static unsigned int eth_to_loc_fec(unsigned int eth_fec) 1438 { 1439 u32 loc_fec = 0; 1440 1441 if (eth_fec & ETHTOOL_FEC_OFF) 1442 return loc_fec; 1443 1444 if (eth_fec & ETHTOOL_FEC_AUTO) 1445 loc_fec |= BIT(HNAE3_FEC_AUTO); 1446 if (eth_fec & ETHTOOL_FEC_RS) 1447 loc_fec |= BIT(HNAE3_FEC_RS); 1448 if (eth_fec & ETHTOOL_FEC_BASER) 1449 loc_fec |= BIT(HNAE3_FEC_BASER); 1450 1451 return loc_fec; 1452 } 1453 1454 static int hns3_get_fecparam(struct net_device *netdev, 1455 struct ethtool_fecparam *fec) 1456 { 1457 struct hnae3_handle *handle = hns3_get_handle(netdev); 1458 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1459 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1460 u8 fec_ability; 1461 u8 fec_mode; 1462 1463 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps)) 1464 return -EOPNOTSUPP; 1465 1466 if (!ops->get_fec) 1467 return -EOPNOTSUPP; 1468 1469 ops->get_fec(handle, &fec_ability, &fec_mode); 1470 1471 fec->fec = loc_to_eth_fec(fec_ability); 1472 fec->active_fec = loc_to_eth_fec(fec_mode); 1473 1474 return 0; 1475 } 1476 1477 static int hns3_set_fecparam(struct net_device *netdev, 1478 struct ethtool_fecparam *fec) 1479 { 1480 struct hnae3_handle *handle = hns3_get_handle(netdev); 1481 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1482 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1483 u32 fec_mode; 1484 1485 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps)) 1486 return -EOPNOTSUPP; 1487 1488 if (!ops->set_fec) 1489 return -EOPNOTSUPP; 1490 fec_mode = eth_to_loc_fec(fec->fec); 1491 1492 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode); 1493 1494 return ops->set_fec(handle, fec_mode); 1495 } 1496 1497 static int hns3_get_module_info(struct net_device *netdev, 1498 struct ethtool_modinfo *modinfo) 1499 { 1500 #define HNS3_SFF_8636_V1_3 0x03 1501 1502 struct hnae3_handle *handle = hns3_get_handle(netdev); 1503 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1504 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1505 struct hns3_sfp_type sfp_type; 1506 int ret; 1507 1508 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 || 1509 !ops->get_module_eeprom) 1510 return -EOPNOTSUPP; 1511 1512 memset(&sfp_type, 0, sizeof(sfp_type)); 1513 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8), 1514 (u8 *)&sfp_type); 1515 if (ret) 1516 return ret; 1517 1518 switch (sfp_type.type) { 1519 case SFF8024_ID_SFP: 1520 modinfo->type = ETH_MODULE_SFF_8472; 1521 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 1522 break; 1523 case SFF8024_ID_QSFP_8438: 1524 modinfo->type = ETH_MODULE_SFF_8436; 1525 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN; 1526 break; 1527 case SFF8024_ID_QSFP_8436_8636: 1528 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) { 1529 modinfo->type = ETH_MODULE_SFF_8436; 1530 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN; 1531 } else { 1532 modinfo->type = ETH_MODULE_SFF_8636; 1533 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN; 1534 } 1535 break; 1536 case SFF8024_ID_QSFP28_8636: 1537 modinfo->type = ETH_MODULE_SFF_8636; 1538 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN; 1539 break; 1540 default: 1541 netdev_err(netdev, "Optical module unknown: %#x\n", 1542 sfp_type.type); 1543 return -EINVAL; 1544 } 1545 1546 return 0; 1547 } 1548 1549 static int hns3_get_module_eeprom(struct net_device *netdev, 1550 struct ethtool_eeprom *ee, u8 *data) 1551 { 1552 struct hnae3_handle *handle = hns3_get_handle(netdev); 1553 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1554 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1555 1556 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 || 1557 !ops->get_module_eeprom) 1558 return -EOPNOTSUPP; 1559 1560 if (!ee->len) 1561 return -EINVAL; 1562 1563 memset(data, 0, ee->len); 1564 1565 return ops->get_module_eeprom(handle, ee->offset, ee->len, data); 1566 } 1567 1568 static u32 hns3_get_priv_flags(struct net_device *netdev) 1569 { 1570 struct hnae3_handle *handle = hns3_get_handle(netdev); 1571 1572 return handle->priv_flags; 1573 } 1574 1575 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed) 1576 { 1577 u32 i; 1578 1579 for (i = 0; i < HNAE3_PFLAG_MAX; i++) 1580 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) { 1581 netdev_err(h->netdev, "%s is unsupported\n", 1582 hns3_priv_flags[i].name); 1583 return -EOPNOTSUPP; 1584 } 1585 1586 return 0; 1587 } 1588 1589 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags) 1590 { 1591 struct hnae3_handle *handle = hns3_get_handle(netdev); 1592 u32 changed = pflags ^ handle->priv_flags; 1593 int ret; 1594 u32 i; 1595 1596 ret = hns3_check_priv_flags(handle, changed); 1597 if (ret) 1598 return ret; 1599 1600 for (i = 0; i < HNAE3_PFLAG_MAX; i++) { 1601 if (changed & BIT(i)) { 1602 bool enable = !(handle->priv_flags & BIT(i)); 1603 1604 if (enable) 1605 handle->priv_flags |= BIT(i); 1606 else 1607 handle->priv_flags &= ~BIT(i); 1608 hns3_priv_flags[i].handler(netdev, enable); 1609 } 1610 } 1611 1612 return 0; 1613 } 1614 1615 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \ 1616 ETHTOOL_COALESCE_USE_ADAPTIVE | \ 1617 ETHTOOL_COALESCE_RX_USECS_HIGH | \ 1618 ETHTOOL_COALESCE_TX_USECS_HIGH | \ 1619 ETHTOOL_COALESCE_MAX_FRAMES) 1620 1621 static const struct ethtool_ops hns3vf_ethtool_ops = { 1622 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE, 1623 .get_drvinfo = hns3_get_drvinfo, 1624 .get_ringparam = hns3_get_ringparam, 1625 .set_ringparam = hns3_set_ringparam, 1626 .get_strings = hns3_get_strings, 1627 .get_ethtool_stats = hns3_get_stats, 1628 .get_sset_count = hns3_get_sset_count, 1629 .get_rxnfc = hns3_get_rxnfc, 1630 .set_rxnfc = hns3_set_rxnfc, 1631 .get_rxfh_key_size = hns3_get_rss_key_size, 1632 .get_rxfh_indir_size = hns3_get_rss_indir_size, 1633 .get_rxfh = hns3_get_rss, 1634 .set_rxfh = hns3_set_rss, 1635 .get_link_ksettings = hns3_get_link_ksettings, 1636 .get_channels = hns3_get_channels, 1637 .set_channels = hns3_set_channels, 1638 .get_coalesce = hns3_get_coalesce, 1639 .set_coalesce = hns3_set_coalesce, 1640 .get_regs_len = hns3_get_regs_len, 1641 .get_regs = hns3_get_regs, 1642 .get_link = hns3_get_link, 1643 .get_msglevel = hns3_get_msglevel, 1644 .set_msglevel = hns3_set_msglevel, 1645 .get_priv_flags = hns3_get_priv_flags, 1646 .set_priv_flags = hns3_set_priv_flags, 1647 }; 1648 1649 static const struct ethtool_ops hns3_ethtool_ops = { 1650 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE, 1651 .self_test = hns3_self_test, 1652 .get_drvinfo = hns3_get_drvinfo, 1653 .get_link = hns3_get_link, 1654 .get_ringparam = hns3_get_ringparam, 1655 .set_ringparam = hns3_set_ringparam, 1656 .get_pauseparam = hns3_get_pauseparam, 1657 .set_pauseparam = hns3_set_pauseparam, 1658 .get_strings = hns3_get_strings, 1659 .get_ethtool_stats = hns3_get_stats, 1660 .get_sset_count = hns3_get_sset_count, 1661 .get_rxnfc = hns3_get_rxnfc, 1662 .set_rxnfc = hns3_set_rxnfc, 1663 .get_rxfh_key_size = hns3_get_rss_key_size, 1664 .get_rxfh_indir_size = hns3_get_rss_indir_size, 1665 .get_rxfh = hns3_get_rss, 1666 .set_rxfh = hns3_set_rss, 1667 .get_link_ksettings = hns3_get_link_ksettings, 1668 .set_link_ksettings = hns3_set_link_ksettings, 1669 .nway_reset = hns3_nway_reset, 1670 .get_channels = hns3_get_channels, 1671 .set_channels = hns3_set_channels, 1672 .get_coalesce = hns3_get_coalesce, 1673 .set_coalesce = hns3_set_coalesce, 1674 .get_regs_len = hns3_get_regs_len, 1675 .get_regs = hns3_get_regs, 1676 .set_phys_id = hns3_set_phys_id, 1677 .get_msglevel = hns3_get_msglevel, 1678 .set_msglevel = hns3_set_msglevel, 1679 .get_fecparam = hns3_get_fecparam, 1680 .set_fecparam = hns3_set_fecparam, 1681 .get_module_info = hns3_get_module_info, 1682 .get_module_eeprom = hns3_get_module_eeprom, 1683 .get_priv_flags = hns3_get_priv_flags, 1684 .set_priv_flags = hns3_set_priv_flags, 1685 }; 1686 1687 void hns3_ethtool_set_ops(struct net_device *netdev) 1688 { 1689 struct hnae3_handle *h = hns3_get_handle(netdev); 1690 1691 if (h->flags & HNAE3_SUPPORT_VF) 1692 netdev->ethtool_ops = &hns3vf_ethtool_ops; 1693 else 1694 netdev->ethtool_ops = &hns3_ethtool_ops; 1695 } 1696