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 #include "hns3_ethtool.h" 11 12 /* tqp related stats */ 13 #define HNS3_TQP_STAT(_string, _member) { \ 14 .stats_string = _string, \ 15 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\ 16 offsetof(struct ring_stats, _member), \ 17 } 18 19 static const struct hns3_stats hns3_txq_stats[] = { 20 /* Tx per-queue statistics */ 21 HNS3_TQP_STAT("dropped", sw_err_cnt), 22 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt), 23 HNS3_TQP_STAT("packets", tx_pkts), 24 HNS3_TQP_STAT("bytes", tx_bytes), 25 HNS3_TQP_STAT("more", tx_more), 26 HNS3_TQP_STAT("push", tx_push), 27 HNS3_TQP_STAT("mem_doorbell", tx_mem_doorbell), 28 HNS3_TQP_STAT("wake", restart_queue), 29 HNS3_TQP_STAT("busy", tx_busy), 30 HNS3_TQP_STAT("copy", tx_copy), 31 HNS3_TQP_STAT("vlan_err", tx_vlan_err), 32 HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err), 33 HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err), 34 HNS3_TQP_STAT("tso_err", tx_tso_err), 35 HNS3_TQP_STAT("over_max_recursion", over_max_recursion), 36 HNS3_TQP_STAT("hw_limitation", hw_limitation), 37 HNS3_TQP_STAT("bounce", tx_bounce), 38 HNS3_TQP_STAT("spare_full", tx_spare_full), 39 HNS3_TQP_STAT("copy_bits_err", copy_bits_err), 40 HNS3_TQP_STAT("sgl", tx_sgl), 41 HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err), 42 HNS3_TQP_STAT("map_sg_err", map_sg_err), 43 }; 44 45 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats) 46 47 static const struct hns3_stats hns3_rxq_stats[] = { 48 /* Rx per-queue statistics */ 49 HNS3_TQP_STAT("dropped", sw_err_cnt), 50 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt), 51 HNS3_TQP_STAT("packets", rx_pkts), 52 HNS3_TQP_STAT("bytes", rx_bytes), 53 HNS3_TQP_STAT("errors", rx_err_cnt), 54 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt), 55 HNS3_TQP_STAT("err_pkt_len", err_pkt_len), 56 HNS3_TQP_STAT("err_bd_num", err_bd_num), 57 HNS3_TQP_STAT("l2_err", l2_err), 58 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err), 59 HNS3_TQP_STAT("csum_complete", csum_complete), 60 HNS3_TQP_STAT("multicast", rx_multicast), 61 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg), 62 HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err), 63 HNS3_TQP_STAT("frag_alloc", frag_alloc), 64 }; 65 66 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags) 67 68 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats) 69 70 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT) 71 72 #define HNS3_NIC_LB_TEST_PKT_NUM 1 73 #define HNS3_NIC_LB_TEST_RING_ID 0 74 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128 75 #define HNS3_NIC_LB_SETUP_USEC 10000 76 77 /* Nic loopback test err */ 78 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1 79 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2 80 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3 81 #define HNS3_NIC_LB_TEST_UNEXECUTED 4 82 83 static int hns3_get_sset_count(struct net_device *netdev, int stringset); 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 int ret; 90 91 if (!h->ae_algo->ops->set_loopback || 92 !h->ae_algo->ops->set_promisc_mode) 93 return -EOPNOTSUPP; 94 95 switch (loop) { 96 case HNAE3_LOOP_SERIAL_SERDES: 97 case HNAE3_LOOP_PARALLEL_SERDES: 98 case HNAE3_LOOP_APP: 99 case HNAE3_LOOP_PHY: 100 case HNAE3_LOOP_EXTERNAL: 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 117 return ret; 118 } 119 120 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode) 121 { 122 struct hnae3_handle *h = hns3_get_handle(ndev); 123 int ret; 124 125 ret = hns3_nic_reset_all_ring(h); 126 if (ret) 127 return ret; 128 129 ret = hns3_lp_setup(ndev, loop_mode, true); 130 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2); 131 132 return ret; 133 } 134 135 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode) 136 { 137 int ret; 138 139 ret = hns3_lp_setup(ndev, loop_mode, false); 140 if (ret) { 141 netdev_err(ndev, "lb_setup return error: %d\n", ret); 142 return ret; 143 } 144 145 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2); 146 147 return 0; 148 } 149 150 static void hns3_lp_setup_skb(struct sk_buff *skb) 151 { 152 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f 153 154 struct net_device *ndev = skb->dev; 155 struct hnae3_handle *handle; 156 struct hnae3_ae_dev *ae_dev; 157 unsigned char *packet; 158 struct ethhdr *ethh; 159 unsigned int i; 160 161 skb_reserve(skb, NET_IP_ALIGN); 162 ethh = skb_put(skb, sizeof(struct ethhdr)); 163 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE); 164 165 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN); 166 167 /* The dst mac addr of loopback packet is the same as the host' 168 * mac addr, the SSU component may loop back the packet to host 169 * before the packet reaches mac or serdes, which will defect 170 * the purpose of mac or serdes selftest. 171 */ 172 handle = hns3_get_handle(ndev); 173 ae_dev = pci_get_drvdata(handle->pdev); 174 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2) 175 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR; 176 eth_zero_addr(ethh->h_source); 177 ethh->h_proto = htons(ETH_P_ARP); 178 skb_reset_mac_header(skb); 179 180 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++) 181 packet[i] = (unsigned char)(i & 0xff); 182 } 183 184 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring, 185 struct sk_buff *skb) 186 { 187 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector; 188 unsigned char *packet = skb->data; 189 u32 len = skb_headlen(skb); 190 u32 i; 191 192 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE); 193 194 for (i = 0; i < len; i++) 195 if (packet[i] != (unsigned char)(i & 0xff)) 196 break; 197 198 /* The packet is correctly received */ 199 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE) 200 tqp_vector->rx_group.total_packets++; 201 else 202 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1, 203 skb->data, len, true); 204 205 dev_kfree_skb_any(skb); 206 } 207 208 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget) 209 { 210 struct hnae3_handle *h = priv->ae_handle; 211 struct hnae3_knic_private_info *kinfo; 212 u32 i, rcv_good_pkt_total = 0; 213 214 kinfo = &h->kinfo; 215 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) { 216 struct hns3_enet_ring *ring = &priv->ring[i]; 217 struct hns3_enet_ring_group *rx_group; 218 u64 pre_rx_pkt; 219 220 rx_group = &ring->tqp_vector->rx_group; 221 pre_rx_pkt = rx_group->total_packets; 222 223 preempt_disable(); 224 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data); 225 preempt_enable(); 226 227 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt); 228 rx_group->total_packets = pre_rx_pkt; 229 } 230 return rcv_good_pkt_total; 231 } 232 233 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid, 234 u32 end_ringid) 235 { 236 u32 i; 237 238 for (i = start_ringid; i <= end_ringid; i++) { 239 struct hns3_enet_ring *ring = &priv->ring[i]; 240 241 hns3_clean_tx_ring(ring, 0); 242 } 243 } 244 245 /** 246 * hns3_lp_run_test - run loopback test 247 * @ndev: net device 248 * @mode: loopback type 249 * 250 * Return: %0 for success or a NIC loopback test error code on failure 251 */ 252 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode) 253 { 254 struct hns3_nic_priv *priv = netdev_priv(ndev); 255 struct sk_buff *skb; 256 u32 i, good_cnt; 257 int ret_val = 0; 258 259 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN, 260 GFP_KERNEL); 261 if (!skb) 262 return HNS3_NIC_LB_TEST_NO_MEM_ERR; 263 264 skb->dev = ndev; 265 hns3_lp_setup_skb(skb); 266 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID; 267 268 good_cnt = 0; 269 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) { 270 netdev_tx_t tx_ret; 271 272 skb_get(skb); 273 tx_ret = hns3_nic_net_xmit(skb, ndev); 274 if (tx_ret == NETDEV_TX_OK) { 275 good_cnt++; 276 } else { 277 kfree_skb(skb); 278 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n", 279 tx_ret); 280 } 281 } 282 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) { 283 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR; 284 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n", 285 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM); 286 goto out; 287 } 288 289 /* Allow 200 milliseconds for packets to go from Tx to Rx */ 290 msleep(200); 291 292 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM); 293 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) { 294 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR; 295 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n", 296 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM); 297 } 298 299 out: 300 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID, 301 HNS3_NIC_LB_TEST_RING_ID); 302 303 kfree_skb(skb); 304 return ret_val; 305 } 306 307 static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2]) 308 { 309 st_param[HNAE3_LOOP_EXTERNAL][0] = HNAE3_LOOP_EXTERNAL; 310 st_param[HNAE3_LOOP_EXTERNAL][1] = 311 h->flags & HNAE3_SUPPORT_EXTERNAL_LOOPBACK; 312 313 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP; 314 st_param[HNAE3_LOOP_APP][1] = 315 h->flags & HNAE3_SUPPORT_APP_LOOPBACK; 316 317 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES; 318 st_param[HNAE3_LOOP_SERIAL_SERDES][1] = 319 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK; 320 321 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] = 322 HNAE3_LOOP_PARALLEL_SERDES; 323 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] = 324 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK; 325 326 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY; 327 st_param[HNAE3_LOOP_PHY][1] = 328 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK; 329 } 330 331 static void hns3_selftest_prepare(struct net_device *ndev, bool if_running) 332 { 333 struct hns3_nic_priv *priv = netdev_priv(ndev); 334 struct hnae3_handle *h = priv->ae_handle; 335 336 if (if_running) 337 ndev->netdev_ops->ndo_stop(ndev); 338 339 #if IS_ENABLED(CONFIG_VLAN_8021Q) 340 /* Disable the vlan filter for selftest does not support it */ 341 if (h->ae_algo->ops->enable_vlan_filter && 342 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) 343 h->ae_algo->ops->enable_vlan_filter(h, false); 344 #endif 345 346 /* Tell firmware to stop mac autoneg before loopback test start, 347 * otherwise loopback test may be failed when the port is still 348 * negotiating. 349 */ 350 if (h->ae_algo->ops->halt_autoneg) 351 h->ae_algo->ops->halt_autoneg(h, true); 352 353 set_bit(HNS3_NIC_STATE_TESTING, &priv->state); 354 } 355 356 static void hns3_selftest_restore(struct net_device *ndev, bool if_running) 357 { 358 struct hns3_nic_priv *priv = netdev_priv(ndev); 359 struct hnae3_handle *h = priv->ae_handle; 360 361 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state); 362 363 if (h->ae_algo->ops->halt_autoneg) 364 h->ae_algo->ops->halt_autoneg(h, false); 365 366 #if IS_ENABLED(CONFIG_VLAN_8021Q) 367 if (h->ae_algo->ops->enable_vlan_filter && 368 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) 369 h->ae_algo->ops->enable_vlan_filter(h, true); 370 #endif 371 372 if (if_running) 373 ndev->netdev_ops->ndo_open(ndev); 374 } 375 376 static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2], 377 struct ethtool_test *eth_test, u64 *data) 378 { 379 int test_index = HNAE3_LOOP_APP; 380 u32 i; 381 382 for (i = HNAE3_LOOP_APP; i < HNAE3_LOOP_NONE; i++) { 383 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0]; 384 385 if (!st_param[i][1]) 386 continue; 387 388 data[test_index] = hns3_lp_up(ndev, loop_type); 389 if (!data[test_index]) 390 data[test_index] = hns3_lp_run_test(ndev, loop_type); 391 392 hns3_lp_down(ndev, loop_type); 393 394 if (data[test_index]) 395 eth_test->flags |= ETH_TEST_FL_FAILED; 396 397 test_index++; 398 } 399 } 400 401 static void hns3_do_external_lb(struct net_device *ndev, 402 struct ethtool_test *eth_test, u64 *data) 403 { 404 data[HNAE3_LOOP_EXTERNAL] = hns3_lp_up(ndev, HNAE3_LOOP_EXTERNAL); 405 if (!data[HNAE3_LOOP_EXTERNAL]) 406 data[HNAE3_LOOP_EXTERNAL] = hns3_lp_run_test(ndev, HNAE3_LOOP_EXTERNAL); 407 hns3_lp_down(ndev, HNAE3_LOOP_EXTERNAL); 408 409 if (data[HNAE3_LOOP_EXTERNAL]) 410 eth_test->flags |= ETH_TEST_FL_FAILED; 411 412 eth_test->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE; 413 } 414 415 /** 416 * hns3_self_test - self test 417 * @ndev: net device 418 * @eth_test: test cmd 419 * @data: test result 420 */ 421 static void hns3_self_test(struct net_device *ndev, 422 struct ethtool_test *eth_test, u64 *data) 423 { 424 int cnt = hns3_get_sset_count(ndev, ETH_SS_TEST); 425 struct hns3_nic_priv *priv = netdev_priv(ndev); 426 struct hnae3_handle *h = priv->ae_handle; 427 int st_param[HNAE3_LOOP_NONE][2]; 428 bool if_running = netif_running(ndev); 429 int i; 430 431 /* initialize the loopback test result, avoid marking an unexcuted 432 * loopback test as PASS. 433 */ 434 for (i = 0; i < cnt; i++) 435 data[i] = HNS3_NIC_LB_TEST_UNEXECUTED; 436 437 if (hns3_nic_resetting(ndev)) { 438 netdev_err(ndev, "dev resetting!"); 439 goto failure; 440 } 441 442 if (!(eth_test->flags & ETH_TEST_FL_OFFLINE)) 443 goto failure; 444 445 if (netif_msg_ifdown(h)) 446 netdev_info(ndev, "self test start\n"); 447 448 hns3_set_selftest_param(h, st_param); 449 450 /* external loopback test requires that the link is up and the duplex is 451 * full, do external test first to reduce the whole test time 452 */ 453 if (eth_test->flags & ETH_TEST_FL_EXTERNAL_LB) { 454 hns3_external_lb_prepare(ndev, if_running); 455 hns3_do_external_lb(ndev, eth_test, data); 456 hns3_external_lb_restore(ndev, if_running); 457 } 458 459 hns3_selftest_prepare(ndev, if_running); 460 hns3_do_selftest(ndev, st_param, eth_test, data); 461 hns3_selftest_restore(ndev, if_running); 462 463 if (netif_msg_ifdown(h)) 464 netdev_info(ndev, "self test end\n"); 465 return; 466 467 failure: 468 eth_test->flags |= ETH_TEST_FL_FAILED; 469 } 470 471 static void hns3_update_limit_promisc_mode(struct net_device *netdev, 472 bool enable) 473 { 474 struct hnae3_handle *handle = hns3_get_handle(netdev); 475 476 if (enable) 477 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags); 478 else 479 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags); 480 481 hns3_request_update_promisc_mode(handle); 482 } 483 484 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = { 485 { "limit_promisc", hns3_update_limit_promisc_mode } 486 }; 487 488 static int hns3_get_sset_count(struct net_device *netdev, int stringset) 489 { 490 struct hnae3_handle *h = hns3_get_handle(netdev); 491 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 492 493 if (!ops->get_sset_count) 494 return -EOPNOTSUPP; 495 496 switch (stringset) { 497 case ETH_SS_STATS: 498 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) + 499 ops->get_sset_count(h, stringset)); 500 501 case ETH_SS_TEST: 502 return ops->get_sset_count(h, stringset); 503 504 case ETH_SS_PRIV_FLAGS: 505 return HNAE3_PFLAG_MAX; 506 507 default: 508 return -EOPNOTSUPP; 509 } 510 } 511 512 static void hns3_update_strings(u8 **data, const struct hns3_stats *stats, 513 u32 stat_count, u32 num_tqps, 514 const char *prefix) 515 { 516 u32 i, j; 517 518 for (i = 0; i < num_tqps; i++) 519 for (j = 0; j < stat_count; j++) 520 ethtool_sprintf(data, "%s%u_%s", prefix, i, 521 stats[j].stats_string); 522 } 523 524 static void hns3_get_strings_tqps(struct hnae3_handle *handle, u8 **data) 525 { 526 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 527 const char tx_prefix[] = "txq"; 528 const char rx_prefix[] = "rxq"; 529 530 /* get strings for Tx */ 531 hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT, 532 kinfo->num_tqps, tx_prefix); 533 534 /* get strings for Rx */ 535 hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT, 536 kinfo->num_tqps, rx_prefix); 537 } 538 539 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data) 540 { 541 struct hnae3_handle *h = hns3_get_handle(netdev); 542 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 543 int i; 544 545 if (!ops->get_strings) 546 return; 547 548 switch (stringset) { 549 case ETH_SS_STATS: 550 hns3_get_strings_tqps(h, &data); 551 ops->get_strings(h, stringset, &data); 552 break; 553 case ETH_SS_TEST: 554 ops->get_strings(h, stringset, &data); 555 break; 556 case ETH_SS_PRIV_FLAGS: 557 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) 558 ethtool_puts(&data, hns3_priv_flags[i].name); 559 break; 560 default: 561 break; 562 } 563 } 564 565 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data) 566 { 567 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 568 struct hns3_nic_priv *nic_priv = handle->priv; 569 struct hns3_enet_ring *ring; 570 u8 *stat; 571 int i, j; 572 573 /* get stats for Tx */ 574 for (i = 0; i < kinfo->num_tqps; i++) { 575 ring = &nic_priv->ring[i]; 576 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) { 577 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset; 578 *data++ = *(u64 *)stat; 579 } 580 } 581 582 /* get stats for Rx */ 583 for (i = 0; i < kinfo->num_tqps; i++) { 584 ring = &nic_priv->ring[i + kinfo->num_tqps]; 585 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) { 586 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset; 587 *data++ = *(u64 *)stat; 588 } 589 } 590 591 return data; 592 } 593 594 /* hns3_get_stats - get detail statistics. 595 * @netdev: net device 596 * @stats: statistics info. 597 * @data: statistics data. 598 */ 599 static void hns3_get_stats(struct net_device *netdev, 600 struct ethtool_stats *stats, u64 *data) 601 { 602 struct hnae3_handle *h = hns3_get_handle(netdev); 603 u64 *p = data; 604 605 if (hns3_nic_resetting(netdev)) { 606 netdev_err(netdev, "dev resetting, could not get stats\n"); 607 return; 608 } 609 610 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) { 611 netdev_err(netdev, "could not get any statistics\n"); 612 return; 613 } 614 615 h->ae_algo->ops->update_stats(h); 616 617 /* get per-queue stats */ 618 p = hns3_get_stats_tqps(h, p); 619 620 /* get MAC & other misc hardware stats */ 621 h->ae_algo->ops->get_stats(h, p); 622 } 623 624 static void hns3_get_drvinfo(struct net_device *netdev, 625 struct ethtool_drvinfo *drvinfo) 626 { 627 struct hns3_nic_priv *priv = netdev_priv(netdev); 628 struct hnae3_handle *h = priv->ae_handle; 629 u32 fw_version; 630 631 if (!h->ae_algo->ops->get_fw_version) { 632 netdev_err(netdev, "could not get fw version!\n"); 633 return; 634 } 635 636 strscpy(drvinfo->driver, dev_driver_string(&h->pdev->dev), 637 sizeof(drvinfo->driver)); 638 639 strscpy(drvinfo->bus_info, pci_name(h->pdev), 640 sizeof(drvinfo->bus_info)); 641 642 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h); 643 644 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), 645 "%lu.%lu.%lu.%lu", 646 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK, 647 HNAE3_FW_VERSION_BYTE3_SHIFT), 648 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK, 649 HNAE3_FW_VERSION_BYTE2_SHIFT), 650 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK, 651 HNAE3_FW_VERSION_BYTE1_SHIFT), 652 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK, 653 HNAE3_FW_VERSION_BYTE0_SHIFT)); 654 } 655 656 static u32 hns3_get_link(struct net_device *netdev) 657 { 658 struct hnae3_handle *h = hns3_get_handle(netdev); 659 660 if (h->ae_algo->ops->get_status) 661 return h->ae_algo->ops->get_status(h); 662 else 663 return 0; 664 } 665 666 static void hns3_get_ringparam(struct net_device *netdev, 667 struct ethtool_ringparam *param, 668 struct kernel_ethtool_ringparam *kernel_param, 669 struct netlink_ext_ack *extack) 670 { 671 struct hns3_nic_priv *priv = netdev_priv(netdev); 672 struct hnae3_handle *h = priv->ae_handle; 673 int rx_queue_index = h->kinfo.num_tqps; 674 675 if (hns3_nic_resetting(netdev) || !priv->ring) { 676 netdev_err(netdev, "failed to get ringparam value, due to dev resetting or uninited\n"); 677 return; 678 } 679 680 param->tx_max_pending = HNS3_RING_MAX_PENDING; 681 param->rx_max_pending = HNS3_RING_MAX_PENDING; 682 683 param->tx_pending = priv->ring[0].desc_num; 684 param->rx_pending = priv->ring[rx_queue_index].desc_num; 685 kernel_param->rx_buf_len = priv->ring[rx_queue_index].buf_size; 686 kernel_param->tx_push = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, 687 &priv->state); 688 } 689 690 static void hns3_get_pauseparam(struct net_device *netdev, 691 struct ethtool_pauseparam *param) 692 { 693 struct hnae3_handle *h = hns3_get_handle(netdev); 694 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 695 696 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps)) 697 return; 698 699 if (h->ae_algo->ops->get_pauseparam) 700 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg, 701 ¶m->rx_pause, ¶m->tx_pause); 702 } 703 704 static int hns3_set_pauseparam(struct net_device *netdev, 705 struct ethtool_pauseparam *param) 706 { 707 struct hnae3_handle *h = hns3_get_handle(netdev); 708 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 709 710 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps)) 711 return -EOPNOTSUPP; 712 713 netif_dbg(h, drv, netdev, 714 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n", 715 param->autoneg, param->rx_pause, param->tx_pause); 716 717 if (h->ae_algo->ops->set_pauseparam) 718 return h->ae_algo->ops->set_pauseparam(h, param->autoneg, 719 param->rx_pause, 720 param->tx_pause); 721 return -EOPNOTSUPP; 722 } 723 724 static void hns3_get_ksettings(struct hnae3_handle *h, 725 struct ethtool_link_ksettings *cmd) 726 { 727 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 728 729 /* 1.auto_neg & speed & duplex from cmd */ 730 if (ops->get_ksettings_an_result) 731 ops->get_ksettings_an_result(h, 732 &cmd->base.autoneg, 733 &cmd->base.speed, 734 &cmd->base.duplex, 735 &cmd->lanes); 736 737 /* 2.get link mode */ 738 if (ops->get_link_mode) 739 ops->get_link_mode(h, 740 cmd->link_modes.supported, 741 cmd->link_modes.advertising); 742 743 /* 3.mdix_ctrl&mdix get from phy reg */ 744 if (ops->get_mdix_mode) 745 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl, 746 &cmd->base.eth_tp_mdix); 747 } 748 749 static int hns3_get_link_ksettings(struct net_device *netdev, 750 struct ethtool_link_ksettings *cmd) 751 { 752 struct hnae3_handle *h = hns3_get_handle(netdev); 753 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 754 const struct hnae3_ae_ops *ops; 755 u8 module_type; 756 u8 media_type; 757 u8 link_stat; 758 759 ops = h->ae_algo->ops; 760 if (ops->get_media_type) 761 ops->get_media_type(h, &media_type, &module_type); 762 else 763 return -EOPNOTSUPP; 764 765 switch (media_type) { 766 case HNAE3_MEDIA_TYPE_NONE: 767 cmd->base.port = PORT_NONE; 768 hns3_get_ksettings(h, cmd); 769 break; 770 case HNAE3_MEDIA_TYPE_FIBER: 771 if (module_type == HNAE3_MODULE_TYPE_UNKNOWN) 772 cmd->base.port = PORT_OTHER; 773 else if (module_type == HNAE3_MODULE_TYPE_CR) 774 cmd->base.port = PORT_DA; 775 else 776 cmd->base.port = PORT_FIBRE; 777 778 hns3_get_ksettings(h, cmd); 779 break; 780 case HNAE3_MEDIA_TYPE_BACKPLANE: 781 cmd->base.port = PORT_NONE; 782 hns3_get_ksettings(h, cmd); 783 break; 784 case HNAE3_MEDIA_TYPE_COPPER: 785 cmd->base.port = PORT_TP; 786 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) && 787 ops->get_phy_link_ksettings) 788 ops->get_phy_link_ksettings(h, cmd); 789 else if (!netdev->phydev) 790 hns3_get_ksettings(h, cmd); 791 else 792 phy_ethtool_ksettings_get(netdev->phydev, cmd); 793 break; 794 default: 795 796 netdev_warn(netdev, "Unknown media type"); 797 return 0; 798 } 799 800 /* mdio_support */ 801 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22; 802 803 link_stat = hns3_get_link(netdev); 804 if (!link_stat) { 805 cmd->base.speed = SPEED_UNKNOWN; 806 cmd->base.duplex = DUPLEX_UNKNOWN; 807 } 808 809 return 0; 810 } 811 812 static int hns3_check_ksettings_param(const struct net_device *netdev, 813 const struct ethtool_link_ksettings *cmd) 814 { 815 struct hnae3_handle *handle = hns3_get_handle(netdev); 816 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 817 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN; 818 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN; 819 u32 lane_num; 820 u8 autoneg; 821 u32 speed; 822 u8 duplex; 823 int ret; 824 825 /* hw doesn't support use specified speed and duplex to negotiate, 826 * unnecessary to check them when autoneg on. 827 */ 828 if (cmd->base.autoneg) 829 return 0; 830 831 if (ops->get_ksettings_an_result) { 832 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex, &lane_num); 833 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed && 834 cmd->base.duplex == duplex && cmd->lanes == lane_num) 835 return 0; 836 } 837 838 if (ops->get_media_type) 839 ops->get_media_type(handle, &media_type, &module_type); 840 841 if (cmd->base.duplex == DUPLEX_HALF && 842 media_type != HNAE3_MEDIA_TYPE_COPPER) { 843 netdev_err(netdev, 844 "only copper port supports half duplex!"); 845 return -EINVAL; 846 } 847 848 if (ops->check_port_speed) { 849 ret = ops->check_port_speed(handle, cmd->base.speed); 850 if (ret) { 851 netdev_err(netdev, "unsupported speed\n"); 852 return ret; 853 } 854 } 855 856 return 0; 857 } 858 859 static int hns3_set_link_ksettings(struct net_device *netdev, 860 const struct ethtool_link_ksettings *cmd) 861 { 862 struct hnae3_handle *handle = hns3_get_handle(netdev); 863 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 864 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 865 int ret; 866 867 /* Chip don't support this mode. */ 868 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF) 869 return -EINVAL; 870 871 if (cmd->lanes && !hnae3_ae_dev_lane_num_supported(ae_dev)) 872 return -EOPNOTSUPP; 873 874 netif_dbg(handle, drv, netdev, 875 "set link(%s): autoneg=%u, speed=%u, duplex=%u, lanes=%u\n", 876 netdev->phydev ? "phy" : "mac", 877 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex, 878 cmd->lanes); 879 880 /* Only support ksettings_set for netdev with phy attached for now */ 881 if (netdev->phydev) { 882 if (cmd->base.speed == SPEED_1000 && 883 cmd->base.autoneg == AUTONEG_DISABLE) 884 return -EINVAL; 885 886 return phy_ethtool_ksettings_set(netdev->phydev, cmd); 887 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) && 888 ops->set_phy_link_ksettings) { 889 return ops->set_phy_link_ksettings(handle, cmd); 890 } 891 892 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2) 893 return -EOPNOTSUPP; 894 895 ret = hns3_check_ksettings_param(netdev, cmd); 896 if (ret) 897 return ret; 898 899 if (ops->set_autoneg) { 900 ret = ops->set_autoneg(handle, cmd->base.autoneg); 901 if (ret) 902 return ret; 903 } 904 905 /* hw doesn't support use specified speed and duplex to negotiate, 906 * ignore them when autoneg on. 907 */ 908 if (cmd->base.autoneg) { 909 netdev_info(netdev, 910 "autoneg is on, ignore the speed and duplex\n"); 911 return 0; 912 } 913 914 if (ops->cfg_mac_speed_dup_h) 915 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed, 916 cmd->base.duplex, (u8)(cmd->lanes)); 917 918 return ret; 919 } 920 921 static u32 hns3_get_rss_key_size(struct net_device *netdev) 922 { 923 struct hnae3_handle *h = hns3_get_handle(netdev); 924 925 if (!h->ae_algo->ops->get_rss_key_size) 926 return 0; 927 928 return h->ae_algo->ops->get_rss_key_size(h); 929 } 930 931 static u32 hns3_get_rss_indir_size(struct net_device *netdev) 932 { 933 struct hnae3_handle *h = hns3_get_handle(netdev); 934 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 935 936 return ae_dev->dev_specs.rss_ind_tbl_size; 937 } 938 939 static int hns3_get_rss(struct net_device *netdev, 940 struct ethtool_rxfh_param *rxfh) 941 { 942 struct hnae3_handle *h = hns3_get_handle(netdev); 943 944 if (!h->ae_algo->ops->get_rss) 945 return -EOPNOTSUPP; 946 947 return h->ae_algo->ops->get_rss(h, rxfh->indir, rxfh->key, 948 &rxfh->hfunc); 949 } 950 951 static int hns3_set_rss(struct net_device *netdev, 952 struct ethtool_rxfh_param *rxfh, 953 struct netlink_ext_ack *extack) 954 { 955 struct hnae3_handle *h = hns3_get_handle(netdev); 956 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 957 958 if (!h->ae_algo->ops->set_rss) 959 return -EOPNOTSUPP; 960 961 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 && 962 rxfh->hfunc != ETH_RSS_HASH_TOP) || 963 (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE && 964 rxfh->hfunc != ETH_RSS_HASH_TOP && 965 rxfh->hfunc != ETH_RSS_HASH_XOR)) { 966 netdev_err(netdev, "hash func not supported\n"); 967 return -EOPNOTSUPP; 968 } 969 970 if (!rxfh->indir) { 971 netdev_err(netdev, 972 "set rss failed for indir is empty\n"); 973 return -EOPNOTSUPP; 974 } 975 976 return h->ae_algo->ops->set_rss(h, rxfh->indir, rxfh->key, 977 rxfh->hfunc); 978 } 979 980 static int hns3_get_rxnfc(struct net_device *netdev, 981 struct ethtool_rxnfc *cmd, 982 u32 *rule_locs) 983 { 984 struct hnae3_handle *h = hns3_get_handle(netdev); 985 986 switch (cmd->cmd) { 987 case ETHTOOL_GRXRINGS: 988 cmd->data = h->kinfo.num_tqps; 989 return 0; 990 case ETHTOOL_GRXFH: 991 if (h->ae_algo->ops->get_rss_tuple) 992 return h->ae_algo->ops->get_rss_tuple(h, cmd); 993 return -EOPNOTSUPP; 994 case ETHTOOL_GRXCLSRLCNT: 995 if (h->ae_algo->ops->get_fd_rule_cnt) 996 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd); 997 return -EOPNOTSUPP; 998 case ETHTOOL_GRXCLSRULE: 999 if (h->ae_algo->ops->get_fd_rule_info) 1000 return h->ae_algo->ops->get_fd_rule_info(h, cmd); 1001 return -EOPNOTSUPP; 1002 case ETHTOOL_GRXCLSRLALL: 1003 if (h->ae_algo->ops->get_fd_all_rules) 1004 return h->ae_algo->ops->get_fd_all_rules(h, cmd, 1005 rule_locs); 1006 return -EOPNOTSUPP; 1007 default: 1008 return -EOPNOTSUPP; 1009 } 1010 } 1011 1012 static const struct hns3_reset_type_map hns3_reset_type[] = { 1013 {ETH_RESET_MGMT, HNAE3_IMP_RESET}, 1014 {ETH_RESET_ALL, HNAE3_GLOBAL_RESET}, 1015 {ETH_RESET_DEDICATED, HNAE3_FUNC_RESET}, 1016 }; 1017 1018 static const struct hns3_reset_type_map hns3vf_reset_type[] = { 1019 {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET}, 1020 }; 1021 1022 static int hns3_set_reset(struct net_device *netdev, u32 *flags) 1023 { 1024 enum hnae3_reset_type rst_type = HNAE3_NONE_RESET; 1025 struct hnae3_handle *h = hns3_get_handle(netdev); 1026 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 1027 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 1028 const struct hns3_reset_type_map *rst_type_map; 1029 enum ethtool_reset_flags rst_flags; 1030 u32 i, size; 1031 1032 if (ops->ae_dev_resetting && ops->ae_dev_resetting(h)) 1033 return -EBUSY; 1034 1035 if (!ops->set_default_reset_request || !ops->reset_event) 1036 return -EOPNOTSUPP; 1037 1038 if (h->flags & HNAE3_SUPPORT_VF) { 1039 rst_type_map = hns3vf_reset_type; 1040 size = ARRAY_SIZE(hns3vf_reset_type); 1041 } else { 1042 rst_type_map = hns3_reset_type; 1043 size = ARRAY_SIZE(hns3_reset_type); 1044 } 1045 1046 for (i = 0; i < size; i++) { 1047 if (rst_type_map[i].rst_flags == *flags) { 1048 rst_type = rst_type_map[i].rst_type; 1049 rst_flags = rst_type_map[i].rst_flags; 1050 break; 1051 } 1052 } 1053 1054 if (rst_type == HNAE3_NONE_RESET || 1055 (rst_type == HNAE3_IMP_RESET && 1056 ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2)) 1057 return -EOPNOTSUPP; 1058 1059 netdev_info(netdev, "Setting reset type %d\n", rst_type); 1060 1061 ops->set_default_reset_request(ae_dev, rst_type); 1062 1063 ops->reset_event(h->pdev, h); 1064 1065 *flags &= ~rst_flags; 1066 1067 return 0; 1068 } 1069 1070 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv, 1071 u32 tx_desc_num, u32 rx_desc_num) 1072 { 1073 struct hnae3_handle *h = priv->ae_handle; 1074 int i; 1075 1076 h->kinfo.num_tx_desc = tx_desc_num; 1077 h->kinfo.num_rx_desc = rx_desc_num; 1078 1079 for (i = 0; i < h->kinfo.num_tqps; i++) { 1080 priv->ring[i].desc_num = tx_desc_num; 1081 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num; 1082 } 1083 } 1084 1085 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv) 1086 { 1087 struct hnae3_handle *handle = priv->ae_handle; 1088 struct hns3_enet_ring *tmp_rings; 1089 int i; 1090 1091 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2, 1092 sizeof(struct hns3_enet_ring), GFP_KERNEL); 1093 if (!tmp_rings) 1094 return NULL; 1095 1096 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) { 1097 memcpy(&tmp_rings[i], &priv->ring[i], 1098 sizeof(struct hns3_enet_ring)); 1099 tmp_rings[i].skb = NULL; 1100 } 1101 1102 return tmp_rings; 1103 } 1104 1105 static int hns3_check_ringparam(struct net_device *ndev, 1106 struct ethtool_ringparam *param, 1107 struct kernel_ethtool_ringparam *kernel_param) 1108 { 1109 #define RX_BUF_LEN_2K 2048 1110 #define RX_BUF_LEN_4K 4096 1111 1112 struct hns3_nic_priv *priv = netdev_priv(ndev); 1113 1114 if (hns3_nic_resetting(ndev) || !priv->ring) { 1115 netdev_err(ndev, "failed to set ringparam value, due to dev resetting or uninited\n"); 1116 return -EBUSY; 1117 } 1118 1119 1120 if (param->rx_mini_pending || param->rx_jumbo_pending) 1121 return -EINVAL; 1122 1123 if (kernel_param->rx_buf_len != RX_BUF_LEN_2K && 1124 kernel_param->rx_buf_len != RX_BUF_LEN_4K) { 1125 netdev_err(ndev, "Rx buf len only support 2048 and 4096\n"); 1126 return -EINVAL; 1127 } 1128 1129 if (param->tx_pending > HNS3_RING_MAX_PENDING || 1130 param->tx_pending < HNS3_RING_MIN_PENDING || 1131 param->rx_pending > HNS3_RING_MAX_PENDING || 1132 param->rx_pending < HNS3_RING_MIN_PENDING) { 1133 netdev_err(ndev, "Queue depth out of range [%d-%d]\n", 1134 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING); 1135 return -EINVAL; 1136 } 1137 1138 return 0; 1139 } 1140 1141 static bool 1142 hns3_is_ringparam_changed(struct net_device *ndev, 1143 struct ethtool_ringparam *param, 1144 struct kernel_ethtool_ringparam *kernel_param, 1145 struct hns3_ring_param *old_ringparam, 1146 struct hns3_ring_param *new_ringparam) 1147 { 1148 struct hns3_nic_priv *priv = netdev_priv(ndev); 1149 struct hnae3_handle *h = priv->ae_handle; 1150 u16 queue_num = h->kinfo.num_tqps; 1151 1152 new_ringparam->tx_desc_num = ALIGN(param->tx_pending, 1153 HNS3_RING_BD_MULTIPLE); 1154 new_ringparam->rx_desc_num = ALIGN(param->rx_pending, 1155 HNS3_RING_BD_MULTIPLE); 1156 old_ringparam->tx_desc_num = priv->ring[0].desc_num; 1157 old_ringparam->rx_desc_num = priv->ring[queue_num].desc_num; 1158 old_ringparam->rx_buf_len = priv->ring[queue_num].buf_size; 1159 new_ringparam->rx_buf_len = kernel_param->rx_buf_len; 1160 1161 if (old_ringparam->tx_desc_num == new_ringparam->tx_desc_num && 1162 old_ringparam->rx_desc_num == new_ringparam->rx_desc_num && 1163 old_ringparam->rx_buf_len == new_ringparam->rx_buf_len) { 1164 netdev_info(ndev, "descriptor number and rx buffer length not changed\n"); 1165 return false; 1166 } 1167 1168 return true; 1169 } 1170 1171 static int hns3_change_rx_buf_len(struct net_device *ndev, u32 rx_buf_len) 1172 { 1173 struct hns3_nic_priv *priv = netdev_priv(ndev); 1174 struct hnae3_handle *h = priv->ae_handle; 1175 int i; 1176 1177 h->kinfo.rx_buf_len = rx_buf_len; 1178 1179 for (i = 0; i < h->kinfo.num_tqps; i++) { 1180 h->kinfo.tqp[i]->buf_size = rx_buf_len; 1181 priv->ring[i + h->kinfo.num_tqps].buf_size = rx_buf_len; 1182 } 1183 1184 return 0; 1185 } 1186 1187 static int hns3_set_tx_push(struct net_device *netdev, u32 tx_push) 1188 { 1189 struct hns3_nic_priv *priv = netdev_priv(netdev); 1190 struct hnae3_handle *h = hns3_get_handle(netdev); 1191 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 1192 u32 old_state = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state); 1193 1194 if (!test_bit(HNAE3_DEV_SUPPORT_TX_PUSH_B, ae_dev->caps) && tx_push) 1195 return -EOPNOTSUPP; 1196 1197 if (tx_push == old_state) 1198 return 0; 1199 1200 netdev_dbg(netdev, "Changing tx push from %s to %s\n", 1201 old_state ? "on" : "off", tx_push ? "on" : "off"); 1202 1203 if (tx_push) 1204 set_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state); 1205 else 1206 clear_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state); 1207 1208 return 0; 1209 } 1210 1211 static int hns3_set_ringparam(struct net_device *ndev, 1212 struct ethtool_ringparam *param, 1213 struct kernel_ethtool_ringparam *kernel_param, 1214 struct netlink_ext_ack *extack) 1215 { 1216 struct hns3_ring_param old_ringparam, new_ringparam; 1217 struct hns3_nic_priv *priv = netdev_priv(ndev); 1218 struct hnae3_handle *h = priv->ae_handle; 1219 struct hns3_enet_ring *tmp_rings; 1220 bool if_running = netif_running(ndev); 1221 int ret, i; 1222 1223 ret = hns3_check_ringparam(ndev, param, kernel_param); 1224 if (ret) 1225 return ret; 1226 1227 ret = hns3_set_tx_push(ndev, kernel_param->tx_push); 1228 if (ret) 1229 return ret; 1230 1231 if (!hns3_is_ringparam_changed(ndev, param, kernel_param, 1232 &old_ringparam, &new_ringparam)) 1233 return 0; 1234 1235 tmp_rings = hns3_backup_ringparam(priv); 1236 if (!tmp_rings) { 1237 netdev_err(ndev, "backup ring param failed by allocating memory fail\n"); 1238 return -ENOMEM; 1239 } 1240 1241 netdev_info(ndev, 1242 "Changing Tx/Rx ring depth from %u/%u to %u/%u, Changing rx buffer len from %u to %u\n", 1243 old_ringparam.tx_desc_num, old_ringparam.rx_desc_num, 1244 new_ringparam.tx_desc_num, new_ringparam.rx_desc_num, 1245 old_ringparam.rx_buf_len, new_ringparam.rx_buf_len); 1246 1247 if (if_running) 1248 ndev->netdev_ops->ndo_stop(ndev); 1249 1250 hns3_change_all_ring_bd_num(priv, new_ringparam.tx_desc_num, 1251 new_ringparam.rx_desc_num); 1252 hns3_change_rx_buf_len(ndev, new_ringparam.rx_buf_len); 1253 ret = hns3_init_all_ring(priv); 1254 if (ret) { 1255 netdev_err(ndev, "set ringparam fail, revert to old value(%d)\n", 1256 ret); 1257 1258 hns3_change_rx_buf_len(ndev, old_ringparam.rx_buf_len); 1259 hns3_change_all_ring_bd_num(priv, old_ringparam.tx_desc_num, 1260 old_ringparam.rx_desc_num); 1261 for (i = 0; i < h->kinfo.num_tqps * 2; i++) 1262 memcpy(&priv->ring[i], &tmp_rings[i], 1263 sizeof(struct hns3_enet_ring)); 1264 } else { 1265 for (i = 0; i < h->kinfo.num_tqps * 2; i++) 1266 hns3_fini_ring(&tmp_rings[i]); 1267 } 1268 1269 kfree(tmp_rings); 1270 1271 if (if_running) 1272 ret = ndev->netdev_ops->ndo_open(ndev); 1273 1274 return ret; 1275 } 1276 1277 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) 1278 { 1279 struct hnae3_handle *h = hns3_get_handle(netdev); 1280 1281 switch (cmd->cmd) { 1282 case ETHTOOL_SRXFH: 1283 if (h->ae_algo->ops->set_rss_tuple) 1284 return h->ae_algo->ops->set_rss_tuple(h, cmd); 1285 return -EOPNOTSUPP; 1286 case ETHTOOL_SRXCLSRLINS: 1287 if (h->ae_algo->ops->add_fd_entry) 1288 return h->ae_algo->ops->add_fd_entry(h, cmd); 1289 return -EOPNOTSUPP; 1290 case ETHTOOL_SRXCLSRLDEL: 1291 if (h->ae_algo->ops->del_fd_entry) 1292 return h->ae_algo->ops->del_fd_entry(h, cmd); 1293 return -EOPNOTSUPP; 1294 default: 1295 return -EOPNOTSUPP; 1296 } 1297 } 1298 1299 static int hns3_nway_reset(struct net_device *netdev) 1300 { 1301 struct hnae3_handle *handle = hns3_get_handle(netdev); 1302 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1303 struct phy_device *phy = netdev->phydev; 1304 int autoneg; 1305 1306 if (!netif_running(netdev)) 1307 return 0; 1308 1309 if (hns3_nic_resetting(netdev)) { 1310 netdev_err(netdev, "dev resetting!"); 1311 return -EBUSY; 1312 } 1313 1314 if (!ops->get_autoneg || !ops->restart_autoneg) 1315 return -EOPNOTSUPP; 1316 1317 autoneg = ops->get_autoneg(handle); 1318 if (autoneg != AUTONEG_ENABLE) { 1319 netdev_err(netdev, 1320 "Autoneg is off, don't support to restart it\n"); 1321 return -EINVAL; 1322 } 1323 1324 netif_dbg(handle, drv, netdev, 1325 "nway reset (using %s)\n", phy ? "phy" : "mac"); 1326 1327 if (phy) 1328 return genphy_restart_aneg(phy); 1329 1330 return ops->restart_autoneg(handle); 1331 } 1332 1333 static void hns3_get_channels(struct net_device *netdev, 1334 struct ethtool_channels *ch) 1335 { 1336 struct hnae3_handle *h = hns3_get_handle(netdev); 1337 1338 if (h->ae_algo->ops->get_channels) 1339 h->ae_algo->ops->get_channels(h, ch); 1340 } 1341 1342 static int hns3_get_coalesce(struct net_device *netdev, 1343 struct ethtool_coalesce *cmd, 1344 struct kernel_ethtool_coalesce *kernel_coal, 1345 struct netlink_ext_ack *extack) 1346 { 1347 struct hns3_nic_priv *priv = netdev_priv(netdev); 1348 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal; 1349 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal; 1350 struct hnae3_handle *h = priv->ae_handle; 1351 1352 if (hns3_nic_resetting(netdev)) 1353 return -EBUSY; 1354 1355 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable; 1356 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable; 1357 1358 cmd->tx_coalesce_usecs = tx_coal->int_gl; 1359 cmd->rx_coalesce_usecs = rx_coal->int_gl; 1360 1361 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting; 1362 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting; 1363 1364 cmd->tx_max_coalesced_frames = tx_coal->int_ql; 1365 cmd->rx_max_coalesced_frames = rx_coal->int_ql; 1366 1367 kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode == 1368 DIM_CQ_PERIOD_MODE_START_FROM_CQE); 1369 kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode == 1370 DIM_CQ_PERIOD_MODE_START_FROM_CQE); 1371 1372 return 0; 1373 } 1374 1375 static int hns3_check_gl_coalesce_para(struct net_device *netdev, 1376 struct ethtool_coalesce *cmd) 1377 { 1378 struct hnae3_handle *handle = hns3_get_handle(netdev); 1379 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1380 u32 rx_gl, tx_gl; 1381 1382 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) { 1383 netdev_err(netdev, 1384 "invalid rx-usecs value, rx-usecs range is 0-%u\n", 1385 ae_dev->dev_specs.max_int_gl); 1386 return -EINVAL; 1387 } 1388 1389 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) { 1390 netdev_err(netdev, 1391 "invalid tx-usecs value, tx-usecs range is 0-%u\n", 1392 ae_dev->dev_specs.max_int_gl); 1393 return -EINVAL; 1394 } 1395 1396 /* device version above V3(include V3), GL uses 1us unit, 1397 * so the round down is not needed. 1398 */ 1399 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3) 1400 return 0; 1401 1402 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs); 1403 if (rx_gl != cmd->rx_coalesce_usecs) { 1404 netdev_info(netdev, 1405 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n", 1406 cmd->rx_coalesce_usecs, rx_gl); 1407 } 1408 1409 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs); 1410 if (tx_gl != cmd->tx_coalesce_usecs) { 1411 netdev_info(netdev, 1412 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n", 1413 cmd->tx_coalesce_usecs, tx_gl); 1414 } 1415 1416 return 0; 1417 } 1418 1419 static int hns3_check_rl_coalesce_para(struct net_device *netdev, 1420 struct ethtool_coalesce *cmd) 1421 { 1422 u32 rl; 1423 1424 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) { 1425 netdev_err(netdev, 1426 "tx_usecs_high must be same as rx_usecs_high.\n"); 1427 return -EINVAL; 1428 } 1429 1430 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) { 1431 netdev_err(netdev, 1432 "Invalid usecs_high value, usecs_high range is 0-%d\n", 1433 HNS3_INT_RL_MAX); 1434 return -EINVAL; 1435 } 1436 1437 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high); 1438 if (rl != cmd->rx_coalesce_usecs_high) { 1439 netdev_info(netdev, 1440 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n", 1441 cmd->rx_coalesce_usecs_high, rl); 1442 } 1443 1444 return 0; 1445 } 1446 1447 static int hns3_check_ql_coalesce_param(struct net_device *netdev, 1448 struct ethtool_coalesce *cmd) 1449 { 1450 struct hnae3_handle *handle = hns3_get_handle(netdev); 1451 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1452 1453 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) && 1454 !ae_dev->dev_specs.int_ql_max) { 1455 netdev_err(netdev, "coalesced frames is not supported\n"); 1456 return -EOPNOTSUPP; 1457 } 1458 1459 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max || 1460 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) { 1461 netdev_err(netdev, 1462 "invalid coalesced_frames value, range is 0-%u\n", 1463 ae_dev->dev_specs.int_ql_max); 1464 return -ERANGE; 1465 } 1466 1467 return 0; 1468 } 1469 1470 static int 1471 hns3_check_cqe_coalesce_param(struct net_device *netdev, 1472 struct kernel_ethtool_coalesce *kernel_coal) 1473 { 1474 struct hnae3_handle *handle = hns3_get_handle(netdev); 1475 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1476 1477 if ((kernel_coal->use_cqe_mode_tx || kernel_coal->use_cqe_mode_rx) && 1478 !hnae3_ae_dev_cq_supported(ae_dev)) { 1479 netdev_err(netdev, "coalesced cqe mode is not supported\n"); 1480 return -EOPNOTSUPP; 1481 } 1482 1483 return 0; 1484 } 1485 1486 static int 1487 hns3_check_coalesce_para(struct net_device *netdev, 1488 struct ethtool_coalesce *cmd, 1489 struct kernel_ethtool_coalesce *kernel_coal) 1490 { 1491 int ret; 1492 1493 ret = hns3_check_cqe_coalesce_param(netdev, kernel_coal); 1494 if (ret) 1495 return ret; 1496 1497 ret = hns3_check_gl_coalesce_para(netdev, cmd); 1498 if (ret) { 1499 netdev_err(netdev, 1500 "Check gl coalesce param fail. ret = %d\n", ret); 1501 return ret; 1502 } 1503 1504 ret = hns3_check_rl_coalesce_para(netdev, cmd); 1505 if (ret) { 1506 netdev_err(netdev, 1507 "Check rl coalesce param fail. ret = %d\n", ret); 1508 return ret; 1509 } 1510 1511 return hns3_check_ql_coalesce_param(netdev, cmd); 1512 } 1513 1514 static void hns3_set_coalesce_per_queue(struct net_device *netdev, 1515 struct ethtool_coalesce *cmd, 1516 u32 queue) 1517 { 1518 struct hns3_enet_tqp_vector *tx_vector, *rx_vector; 1519 struct hns3_nic_priv *priv = netdev_priv(netdev); 1520 struct hnae3_handle *h = priv->ae_handle; 1521 int queue_num = h->kinfo.num_tqps; 1522 1523 tx_vector = priv->ring[queue].tqp_vector; 1524 rx_vector = priv->ring[queue_num + queue].tqp_vector; 1525 1526 tx_vector->tx_group.coal.adapt_enable = 1527 cmd->use_adaptive_tx_coalesce; 1528 rx_vector->rx_group.coal.adapt_enable = 1529 cmd->use_adaptive_rx_coalesce; 1530 1531 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs; 1532 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs; 1533 1534 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames; 1535 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames; 1536 1537 hns3_set_vector_coalesce_tx_gl(tx_vector, 1538 tx_vector->tx_group.coal.int_gl); 1539 hns3_set_vector_coalesce_rx_gl(rx_vector, 1540 rx_vector->rx_group.coal.int_gl); 1541 1542 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting); 1543 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting); 1544 1545 if (tx_vector->tx_group.coal.ql_enable) 1546 hns3_set_vector_coalesce_tx_ql(tx_vector, 1547 tx_vector->tx_group.coal.int_ql); 1548 if (rx_vector->rx_group.coal.ql_enable) 1549 hns3_set_vector_coalesce_rx_ql(rx_vector, 1550 rx_vector->rx_group.coal.int_ql); 1551 } 1552 1553 static int hns3_set_coalesce(struct net_device *netdev, 1554 struct ethtool_coalesce *cmd, 1555 struct kernel_ethtool_coalesce *kernel_coal, 1556 struct netlink_ext_ack *extack) 1557 { 1558 struct hnae3_handle *h = hns3_get_handle(netdev); 1559 struct hns3_nic_priv *priv = netdev_priv(netdev); 1560 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal; 1561 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal; 1562 u16 queue_num = h->kinfo.num_tqps; 1563 enum dim_cq_period_mode tx_mode; 1564 enum dim_cq_period_mode rx_mode; 1565 int ret; 1566 int i; 1567 1568 if (hns3_nic_resetting(netdev)) 1569 return -EBUSY; 1570 1571 ret = hns3_check_coalesce_para(netdev, cmd, kernel_coal); 1572 if (ret) 1573 return ret; 1574 1575 h->kinfo.int_rl_setting = 1576 hns3_rl_round_down(cmd->rx_coalesce_usecs_high); 1577 1578 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce; 1579 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce; 1580 1581 tx_coal->int_gl = cmd->tx_coalesce_usecs; 1582 rx_coal->int_gl = cmd->rx_coalesce_usecs; 1583 1584 tx_coal->int_ql = cmd->tx_max_coalesced_frames; 1585 rx_coal->int_ql = cmd->rx_max_coalesced_frames; 1586 1587 for (i = 0; i < queue_num; i++) 1588 hns3_set_coalesce_per_queue(netdev, cmd, i); 1589 1590 tx_mode = kernel_coal->use_cqe_mode_tx ? 1591 DIM_CQ_PERIOD_MODE_START_FROM_CQE : 1592 DIM_CQ_PERIOD_MODE_START_FROM_EQE; 1593 rx_mode = kernel_coal->use_cqe_mode_rx ? 1594 DIM_CQ_PERIOD_MODE_START_FROM_CQE : 1595 DIM_CQ_PERIOD_MODE_START_FROM_EQE; 1596 hns3_cq_period_mode_init(priv, tx_mode, rx_mode); 1597 1598 return 0; 1599 } 1600 1601 static int hns3_get_regs_len(struct net_device *netdev) 1602 { 1603 struct hnae3_handle *h = hns3_get_handle(netdev); 1604 1605 if (!h->ae_algo->ops->get_regs_len) 1606 return -EOPNOTSUPP; 1607 1608 return h->ae_algo->ops->get_regs_len(h); 1609 } 1610 1611 static void hns3_get_regs(struct net_device *netdev, 1612 struct ethtool_regs *cmd, void *data) 1613 { 1614 struct hnae3_handle *h = hns3_get_handle(netdev); 1615 1616 if (!h->ae_algo->ops->get_regs) 1617 return; 1618 1619 h->ae_algo->ops->get_regs(h, &cmd->version, data); 1620 } 1621 1622 static int hns3_set_phys_id(struct net_device *netdev, 1623 enum ethtool_phys_id_state state) 1624 { 1625 struct hnae3_handle *h = hns3_get_handle(netdev); 1626 1627 if (!h->ae_algo->ops->set_led_id) 1628 return -EOPNOTSUPP; 1629 1630 return h->ae_algo->ops->set_led_id(h, state); 1631 } 1632 1633 static u32 hns3_get_msglevel(struct net_device *netdev) 1634 { 1635 struct hnae3_handle *h = hns3_get_handle(netdev); 1636 1637 return h->msg_enable; 1638 } 1639 1640 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level) 1641 { 1642 struct hnae3_handle *h = hns3_get_handle(netdev); 1643 1644 h->msg_enable = msg_level; 1645 } 1646 1647 static void hns3_get_fec_stats(struct net_device *netdev, 1648 struct ethtool_fec_stats *fec_stats) 1649 { 1650 struct hnae3_handle *handle = hns3_get_handle(netdev); 1651 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1652 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1653 1654 if (!hnae3_ae_dev_fec_stats_supported(ae_dev) || !ops->get_fec_stats) 1655 return; 1656 1657 ops->get_fec_stats(handle, fec_stats); 1658 } 1659 1660 /* Translate local fec value into ethtool value. */ 1661 static unsigned int loc_to_eth_fec(u8 loc_fec) 1662 { 1663 u32 eth_fec = 0; 1664 1665 if (loc_fec & BIT(HNAE3_FEC_AUTO)) 1666 eth_fec |= ETHTOOL_FEC_AUTO; 1667 if (loc_fec & BIT(HNAE3_FEC_RS)) 1668 eth_fec |= ETHTOOL_FEC_RS; 1669 if (loc_fec & BIT(HNAE3_FEC_LLRS)) 1670 eth_fec |= ETHTOOL_FEC_LLRS; 1671 if (loc_fec & BIT(HNAE3_FEC_BASER)) 1672 eth_fec |= ETHTOOL_FEC_BASER; 1673 if (loc_fec & BIT(HNAE3_FEC_NONE)) 1674 eth_fec |= ETHTOOL_FEC_OFF; 1675 1676 return eth_fec; 1677 } 1678 1679 /* Translate ethtool fec value into local value. */ 1680 static unsigned int eth_to_loc_fec(unsigned int eth_fec) 1681 { 1682 u32 loc_fec = 0; 1683 1684 if (eth_fec & ETHTOOL_FEC_OFF) 1685 loc_fec |= BIT(HNAE3_FEC_NONE); 1686 if (eth_fec & ETHTOOL_FEC_AUTO) 1687 loc_fec |= BIT(HNAE3_FEC_AUTO); 1688 if (eth_fec & ETHTOOL_FEC_RS) 1689 loc_fec |= BIT(HNAE3_FEC_RS); 1690 if (eth_fec & ETHTOOL_FEC_LLRS) 1691 loc_fec |= BIT(HNAE3_FEC_LLRS); 1692 if (eth_fec & ETHTOOL_FEC_BASER) 1693 loc_fec |= BIT(HNAE3_FEC_BASER); 1694 1695 return loc_fec; 1696 } 1697 1698 static int hns3_get_fecparam(struct net_device *netdev, 1699 struct ethtool_fecparam *fec) 1700 { 1701 struct hnae3_handle *handle = hns3_get_handle(netdev); 1702 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1703 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1704 u8 fec_ability; 1705 u8 fec_mode; 1706 1707 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps)) 1708 return -EOPNOTSUPP; 1709 1710 if (!ops->get_fec) 1711 return -EOPNOTSUPP; 1712 1713 ops->get_fec(handle, &fec_ability, &fec_mode); 1714 1715 fec->fec = loc_to_eth_fec(fec_ability); 1716 fec->active_fec = loc_to_eth_fec(fec_mode); 1717 if (!fec->active_fec) 1718 fec->active_fec = ETHTOOL_FEC_OFF; 1719 1720 return 0; 1721 } 1722 1723 static int hns3_set_fecparam(struct net_device *netdev, 1724 struct ethtool_fecparam *fec) 1725 { 1726 struct hnae3_handle *handle = hns3_get_handle(netdev); 1727 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1728 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1729 u32 fec_mode; 1730 1731 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps)) 1732 return -EOPNOTSUPP; 1733 1734 if (!ops->set_fec) 1735 return -EOPNOTSUPP; 1736 fec_mode = eth_to_loc_fec(fec->fec); 1737 1738 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode); 1739 1740 return ops->set_fec(handle, fec_mode); 1741 } 1742 1743 static int hns3_get_module_info(struct net_device *netdev, 1744 struct ethtool_modinfo *modinfo) 1745 { 1746 #define HNS3_SFF_8636_V1_3 0x03 1747 1748 struct hnae3_handle *handle = hns3_get_handle(netdev); 1749 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1750 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1751 struct hns3_sfp_type sfp_type; 1752 int ret; 1753 1754 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 || 1755 !ops->get_module_eeprom) 1756 return -EOPNOTSUPP; 1757 1758 memset(&sfp_type, 0, sizeof(sfp_type)); 1759 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8), 1760 (u8 *)&sfp_type); 1761 if (ret) 1762 return ret; 1763 1764 switch (sfp_type.type) { 1765 case SFF8024_ID_SFP: 1766 modinfo->type = ETH_MODULE_SFF_8472; 1767 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 1768 break; 1769 case SFF8024_ID_QSFP_8438: 1770 modinfo->type = ETH_MODULE_SFF_8436; 1771 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN; 1772 break; 1773 case SFF8024_ID_QSFP_8436_8636: 1774 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) { 1775 modinfo->type = ETH_MODULE_SFF_8436; 1776 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN; 1777 } else { 1778 modinfo->type = ETH_MODULE_SFF_8636; 1779 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN; 1780 } 1781 break; 1782 case SFF8024_ID_QSFP28_8636: 1783 modinfo->type = ETH_MODULE_SFF_8636; 1784 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN; 1785 break; 1786 default: 1787 netdev_err(netdev, "Optical module unknown: %#x\n", 1788 sfp_type.type); 1789 return -EINVAL; 1790 } 1791 1792 return 0; 1793 } 1794 1795 static int hns3_get_module_eeprom(struct net_device *netdev, 1796 struct ethtool_eeprom *ee, u8 *data) 1797 { 1798 struct hnae3_handle *handle = hns3_get_handle(netdev); 1799 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1800 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1801 1802 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 || 1803 !ops->get_module_eeprom) 1804 return -EOPNOTSUPP; 1805 1806 if (!ee->len) 1807 return -EINVAL; 1808 1809 memset(data, 0, ee->len); 1810 1811 return ops->get_module_eeprom(handle, ee->offset, ee->len, data); 1812 } 1813 1814 static u32 hns3_get_priv_flags(struct net_device *netdev) 1815 { 1816 struct hnae3_handle *handle = hns3_get_handle(netdev); 1817 1818 return handle->priv_flags; 1819 } 1820 1821 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed) 1822 { 1823 u32 i; 1824 1825 for (i = 0; i < HNAE3_PFLAG_MAX; i++) 1826 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) { 1827 netdev_err(h->netdev, "%s is unsupported\n", 1828 hns3_priv_flags[i].name); 1829 return -EOPNOTSUPP; 1830 } 1831 1832 return 0; 1833 } 1834 1835 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags) 1836 { 1837 struct hnae3_handle *handle = hns3_get_handle(netdev); 1838 u32 changed = pflags ^ handle->priv_flags; 1839 int ret; 1840 u32 i; 1841 1842 ret = hns3_check_priv_flags(handle, changed); 1843 if (ret) 1844 return ret; 1845 1846 for (i = 0; i < HNAE3_PFLAG_MAX; i++) { 1847 if (changed & BIT(i)) { 1848 bool enable = !(handle->priv_flags & BIT(i)); 1849 1850 if (enable) 1851 handle->priv_flags |= BIT(i); 1852 else 1853 handle->priv_flags &= ~BIT(i); 1854 hns3_priv_flags[i].handler(netdev, enable); 1855 } 1856 } 1857 1858 return 0; 1859 } 1860 1861 static int hns3_get_tunable(struct net_device *netdev, 1862 const struct ethtool_tunable *tuna, 1863 void *data) 1864 { 1865 struct hns3_nic_priv *priv = netdev_priv(netdev); 1866 struct hnae3_handle *h = priv->ae_handle; 1867 int ret = 0; 1868 1869 switch (tuna->id) { 1870 case ETHTOOL_TX_COPYBREAK: 1871 /* all the tx rings have the same tx_copybreak */ 1872 *(u32 *)data = priv->tx_copybreak; 1873 break; 1874 case ETHTOOL_RX_COPYBREAK: 1875 *(u32 *)data = priv->rx_copybreak; 1876 break; 1877 case ETHTOOL_TX_COPYBREAK_BUF_SIZE: 1878 *(u32 *)data = h->kinfo.tx_spare_buf_size; 1879 break; 1880 default: 1881 ret = -EOPNOTSUPP; 1882 break; 1883 } 1884 1885 return ret; 1886 } 1887 1888 static int hns3_set_tx_spare_buf_size(struct net_device *netdev, 1889 u32 data) 1890 { 1891 struct hns3_nic_priv *priv = netdev_priv(netdev); 1892 struct hnae3_handle *h = priv->ae_handle; 1893 int ret; 1894 1895 h->kinfo.tx_spare_buf_size = data; 1896 1897 ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT); 1898 if (ret) 1899 return ret; 1900 1901 ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT); 1902 if (ret) 1903 return ret; 1904 1905 ret = hns3_reset_notify(h, HNAE3_INIT_CLIENT); 1906 if (ret) 1907 return ret; 1908 1909 ret = hns3_reset_notify(h, HNAE3_UP_CLIENT); 1910 if (ret) 1911 hns3_reset_notify(h, HNAE3_UNINIT_CLIENT); 1912 1913 return ret; 1914 } 1915 1916 static int hns3_set_tunable(struct net_device *netdev, 1917 const struct ethtool_tunable *tuna, 1918 const void *data) 1919 { 1920 struct hns3_nic_priv *priv = netdev_priv(netdev); 1921 u32 old_tx_spare_buf_size, new_tx_spare_buf_size; 1922 struct hnae3_handle *h = priv->ae_handle; 1923 int i, ret = 0; 1924 1925 if (hns3_nic_resetting(netdev) || !priv->ring) { 1926 netdev_err(netdev, "failed to set tunable value, dev resetting!"); 1927 return -EBUSY; 1928 } 1929 1930 switch (tuna->id) { 1931 case ETHTOOL_TX_COPYBREAK: 1932 priv->tx_copybreak = *(u32 *)data; 1933 1934 for (i = 0; i < h->kinfo.num_tqps; i++) 1935 priv->ring[i].tx_copybreak = priv->tx_copybreak; 1936 1937 break; 1938 case ETHTOOL_RX_COPYBREAK: 1939 priv->rx_copybreak = *(u32 *)data; 1940 1941 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++) 1942 priv->ring[i].rx_copybreak = priv->rx_copybreak; 1943 1944 break; 1945 case ETHTOOL_TX_COPYBREAK_BUF_SIZE: 1946 old_tx_spare_buf_size = h->kinfo.tx_spare_buf_size; 1947 new_tx_spare_buf_size = *(u32 *)data; 1948 netdev_info(netdev, "request to set tx spare buf size from %u to %u\n", 1949 old_tx_spare_buf_size, new_tx_spare_buf_size); 1950 ret = hns3_set_tx_spare_buf_size(netdev, new_tx_spare_buf_size); 1951 if (ret || 1952 (!priv->ring->tx_spare && new_tx_spare_buf_size != 0)) { 1953 int ret1; 1954 1955 netdev_warn(netdev, "change tx spare buf size fail, revert to old value\n"); 1956 ret1 = hns3_set_tx_spare_buf_size(netdev, 1957 old_tx_spare_buf_size); 1958 if (ret1) { 1959 netdev_err(netdev, "revert to old tx spare buf size fail\n"); 1960 return ret1; 1961 } 1962 1963 return ret; 1964 } 1965 1966 if (!priv->ring->tx_spare) 1967 netdev_info(netdev, "the active tx spare buf size is 0, disable tx spare buffer\n"); 1968 else 1969 netdev_info(netdev, "the active tx spare buf size is %u, due to page order\n", 1970 priv->ring->tx_spare->len); 1971 1972 break; 1973 default: 1974 ret = -EOPNOTSUPP; 1975 break; 1976 } 1977 1978 return ret; 1979 } 1980 1981 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \ 1982 ETHTOOL_COALESCE_USE_ADAPTIVE | \ 1983 ETHTOOL_COALESCE_RX_USECS_HIGH | \ 1984 ETHTOOL_COALESCE_TX_USECS_HIGH | \ 1985 ETHTOOL_COALESCE_MAX_FRAMES | \ 1986 ETHTOOL_COALESCE_USE_CQE) 1987 1988 #define HNS3_ETHTOOL_RING (ETHTOOL_RING_USE_RX_BUF_LEN | \ 1989 ETHTOOL_RING_USE_TX_PUSH) 1990 1991 static int hns3_get_ts_info(struct net_device *netdev, 1992 struct kernel_ethtool_ts_info *info) 1993 { 1994 struct hnae3_handle *handle = hns3_get_handle(netdev); 1995 1996 if (handle->ae_algo->ops->get_ts_info) 1997 return handle->ae_algo->ops->get_ts_info(handle, info); 1998 1999 return ethtool_op_get_ts_info(netdev, info); 2000 } 2001 2002 static const struct hns3_ethtool_link_ext_state_mapping 2003 hns3_link_ext_state_map[] = { 2004 {1, ETHTOOL_LINK_EXT_STATE_AUTONEG, 2005 ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD}, 2006 {2, ETHTOOL_LINK_EXT_STATE_AUTONEG, 2007 ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED}, 2008 2009 {256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE, 2010 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT}, 2011 {257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE, 2012 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY}, 2013 {512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE, 2014 ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT}, 2015 2016 {513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH, 2017 ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK}, 2018 {514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH, 2019 ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED}, 2020 {515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH, 2021 ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED}, 2022 2023 {768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY, 2024 ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS}, 2025 {769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY, 2026 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST}, 2027 {770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY, 2028 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS}, 2029 2030 {1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0}, 2031 {1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE, 2032 ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE}, 2033 2034 {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0}, 2035 }; 2036 2037 static int hns3_get_link_ext_state(struct net_device *netdev, 2038 struct ethtool_link_ext_state_info *info) 2039 { 2040 const struct hns3_ethtool_link_ext_state_mapping *map; 2041 struct hnae3_handle *h = hns3_get_handle(netdev); 2042 u32 status_code, i; 2043 int ret; 2044 2045 if (netif_carrier_ok(netdev)) 2046 return -ENODATA; 2047 2048 if (!h->ae_algo->ops->get_link_diagnosis_info) 2049 return -EOPNOTSUPP; 2050 2051 ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code); 2052 if (ret) 2053 return ret; 2054 2055 for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) { 2056 map = &hns3_link_ext_state_map[i]; 2057 if (map->status_code == status_code) { 2058 info->link_ext_state = map->link_ext_state; 2059 info->__link_ext_substate = map->link_ext_substate; 2060 return 0; 2061 } 2062 } 2063 2064 return -ENODATA; 2065 } 2066 2067 static void hns3_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) 2068 { 2069 struct hnae3_handle *handle = hns3_get_handle(netdev); 2070 const struct hnae3_ae_ops *ops = hns3_get_ops(handle); 2071 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle); 2072 2073 if (!hnae3_ae_dev_wol_supported(ae_dev)) 2074 return; 2075 2076 ops->get_wol(handle, wol); 2077 } 2078 2079 static int hns3_set_wol(struct net_device *netdev, 2080 struct ethtool_wolinfo *wol) 2081 { 2082 struct hnae3_handle *handle = hns3_get_handle(netdev); 2083 const struct hnae3_ae_ops *ops = hns3_get_ops(handle); 2084 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle); 2085 2086 if (!hnae3_ae_dev_wol_supported(ae_dev)) 2087 return -EOPNOTSUPP; 2088 2089 return ops->set_wol(handle, wol); 2090 } 2091 2092 static const struct ethtool_ops hns3vf_ethtool_ops = { 2093 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE, 2094 .supported_ring_params = HNS3_ETHTOOL_RING, 2095 .get_drvinfo = hns3_get_drvinfo, 2096 .get_ringparam = hns3_get_ringparam, 2097 .set_ringparam = hns3_set_ringparam, 2098 .get_strings = hns3_get_strings, 2099 .get_ethtool_stats = hns3_get_stats, 2100 .get_sset_count = hns3_get_sset_count, 2101 .get_rxnfc = hns3_get_rxnfc, 2102 .set_rxnfc = hns3_set_rxnfc, 2103 .get_rxfh_key_size = hns3_get_rss_key_size, 2104 .get_rxfh_indir_size = hns3_get_rss_indir_size, 2105 .get_rxfh = hns3_get_rss, 2106 .set_rxfh = hns3_set_rss, 2107 .get_link_ksettings = hns3_get_link_ksettings, 2108 .get_channels = hns3_get_channels, 2109 .set_channels = hns3_set_channels, 2110 .get_coalesce = hns3_get_coalesce, 2111 .set_coalesce = hns3_set_coalesce, 2112 .get_regs_len = hns3_get_regs_len, 2113 .get_regs = hns3_get_regs, 2114 .get_link = hns3_get_link, 2115 .get_msglevel = hns3_get_msglevel, 2116 .set_msglevel = hns3_set_msglevel, 2117 .get_priv_flags = hns3_get_priv_flags, 2118 .set_priv_flags = hns3_set_priv_flags, 2119 .get_tunable = hns3_get_tunable, 2120 .set_tunable = hns3_set_tunable, 2121 .reset = hns3_set_reset, 2122 }; 2123 2124 static const struct ethtool_ops hns3_ethtool_ops = { 2125 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE, 2126 .supported_ring_params = HNS3_ETHTOOL_RING, 2127 .cap_link_lanes_supported = true, 2128 .self_test = hns3_self_test, 2129 .get_drvinfo = hns3_get_drvinfo, 2130 .get_link = hns3_get_link, 2131 .get_ringparam = hns3_get_ringparam, 2132 .set_ringparam = hns3_set_ringparam, 2133 .get_pauseparam = hns3_get_pauseparam, 2134 .set_pauseparam = hns3_set_pauseparam, 2135 .get_strings = hns3_get_strings, 2136 .get_ethtool_stats = hns3_get_stats, 2137 .get_sset_count = hns3_get_sset_count, 2138 .get_rxnfc = hns3_get_rxnfc, 2139 .set_rxnfc = hns3_set_rxnfc, 2140 .get_rxfh_key_size = hns3_get_rss_key_size, 2141 .get_rxfh_indir_size = hns3_get_rss_indir_size, 2142 .get_rxfh = hns3_get_rss, 2143 .set_rxfh = hns3_set_rss, 2144 .get_link_ksettings = hns3_get_link_ksettings, 2145 .set_link_ksettings = hns3_set_link_ksettings, 2146 .nway_reset = hns3_nway_reset, 2147 .get_channels = hns3_get_channels, 2148 .set_channels = hns3_set_channels, 2149 .get_coalesce = hns3_get_coalesce, 2150 .set_coalesce = hns3_set_coalesce, 2151 .get_regs_len = hns3_get_regs_len, 2152 .get_regs = hns3_get_regs, 2153 .set_phys_id = hns3_set_phys_id, 2154 .get_msglevel = hns3_get_msglevel, 2155 .set_msglevel = hns3_set_msglevel, 2156 .get_fecparam = hns3_get_fecparam, 2157 .set_fecparam = hns3_set_fecparam, 2158 .get_fec_stats = hns3_get_fec_stats, 2159 .get_module_info = hns3_get_module_info, 2160 .get_module_eeprom = hns3_get_module_eeprom, 2161 .get_priv_flags = hns3_get_priv_flags, 2162 .set_priv_flags = hns3_set_priv_flags, 2163 .get_ts_info = hns3_get_ts_info, 2164 .get_tunable = hns3_get_tunable, 2165 .set_tunable = hns3_set_tunable, 2166 .reset = hns3_set_reset, 2167 .get_link_ext_state = hns3_get_link_ext_state, 2168 .get_wol = hns3_get_wol, 2169 .set_wol = hns3_set_wol, 2170 }; 2171 2172 void hns3_ethtool_set_ops(struct net_device *netdev) 2173 { 2174 struct hnae3_handle *h = hns3_get_handle(netdev); 2175 2176 if (h->flags & HNAE3_SUPPORT_VF) 2177 netdev->ethtool_ops = &hns3vf_ethtool_ops; 2178 else 2179 netdev->ethtool_ops = &hns3_ethtool_ops; 2180 } 2181