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 82 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en) 83 { 84 struct hnae3_handle *h = hns3_get_handle(ndev); 85 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 86 int ret; 87 88 if (!h->ae_algo->ops->set_loopback || 89 !h->ae_algo->ops->set_promisc_mode) 90 return -EOPNOTSUPP; 91 92 switch (loop) { 93 case HNAE3_LOOP_SERIAL_SERDES: 94 case HNAE3_LOOP_PARALLEL_SERDES: 95 case HNAE3_LOOP_APP: 96 case HNAE3_LOOP_PHY: 97 case HNAE3_LOOP_EXTERNAL: 98 ret = h->ae_algo->ops->set_loopback(h, loop, en); 99 break; 100 default: 101 ret = -ENOTSUPP; 102 break; 103 } 104 105 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) 106 return ret; 107 108 if (en) 109 h->ae_algo->ops->set_promisc_mode(h, true, true); 110 else 111 /* recover promisc mode before loopback test */ 112 hns3_request_update_promisc_mode(h); 113 114 return ret; 115 } 116 117 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode) 118 { 119 struct hnae3_handle *h = hns3_get_handle(ndev); 120 int ret; 121 122 ret = hns3_nic_reset_all_ring(h); 123 if (ret) 124 return ret; 125 126 ret = hns3_lp_setup(ndev, loop_mode, true); 127 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2); 128 129 return ret; 130 } 131 132 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode) 133 { 134 int ret; 135 136 ret = hns3_lp_setup(ndev, loop_mode, false); 137 if (ret) { 138 netdev_err(ndev, "lb_setup return error: %d\n", ret); 139 return ret; 140 } 141 142 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2); 143 144 return 0; 145 } 146 147 static void hns3_lp_setup_skb(struct sk_buff *skb) 148 { 149 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f 150 151 struct net_device *ndev = skb->dev; 152 struct hnae3_handle *handle; 153 struct hnae3_ae_dev *ae_dev; 154 unsigned char *packet; 155 struct ethhdr *ethh; 156 unsigned int i; 157 158 skb_reserve(skb, NET_IP_ALIGN); 159 ethh = skb_put(skb, sizeof(struct ethhdr)); 160 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE); 161 162 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN); 163 164 /* The dst mac addr of loopback packet is the same as the host' 165 * mac addr, the SSU component may loop back the packet to host 166 * before the packet reaches mac or serdes, which will defect 167 * the purpose of mac or serdes selftest. 168 */ 169 handle = hns3_get_handle(ndev); 170 ae_dev = pci_get_drvdata(handle->pdev); 171 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2) 172 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR; 173 eth_zero_addr(ethh->h_source); 174 ethh->h_proto = htons(ETH_P_ARP); 175 skb_reset_mac_header(skb); 176 177 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++) 178 packet[i] = (unsigned char)(i & 0xff); 179 } 180 181 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring, 182 struct sk_buff *skb) 183 { 184 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector; 185 unsigned char *packet = skb->data; 186 u32 len = skb_headlen(skb); 187 u32 i; 188 189 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE); 190 191 for (i = 0; i < len; i++) 192 if (packet[i] != (unsigned char)(i & 0xff)) 193 break; 194 195 /* The packet is correctly received */ 196 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE) 197 tqp_vector->rx_group.total_packets++; 198 else 199 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1, 200 skb->data, len, true); 201 202 dev_kfree_skb_any(skb); 203 } 204 205 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget) 206 { 207 struct hnae3_handle *h = priv->ae_handle; 208 struct hnae3_knic_private_info *kinfo; 209 u32 i, rcv_good_pkt_total = 0; 210 211 kinfo = &h->kinfo; 212 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) { 213 struct hns3_enet_ring *ring = &priv->ring[i]; 214 struct hns3_enet_ring_group *rx_group; 215 u64 pre_rx_pkt; 216 217 rx_group = &ring->tqp_vector->rx_group; 218 pre_rx_pkt = rx_group->total_packets; 219 220 preempt_disable(); 221 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data); 222 preempt_enable(); 223 224 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt); 225 rx_group->total_packets = pre_rx_pkt; 226 } 227 return rcv_good_pkt_total; 228 } 229 230 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid, 231 u32 end_ringid) 232 { 233 u32 i; 234 235 for (i = start_ringid; i <= end_ringid; i++) { 236 struct hns3_enet_ring *ring = &priv->ring[i]; 237 238 hns3_clean_tx_ring(ring, 0); 239 } 240 } 241 242 /** 243 * hns3_lp_run_test - run loopback test 244 * @ndev: net device 245 * @mode: loopback type 246 * 247 * Return: %0 for success or a NIC loopback test error code on failure 248 */ 249 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode) 250 { 251 struct hns3_nic_priv *priv = netdev_priv(ndev); 252 struct sk_buff *skb; 253 u32 i, good_cnt; 254 int ret_val = 0; 255 256 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN, 257 GFP_KERNEL); 258 if (!skb) 259 return HNS3_NIC_LB_TEST_NO_MEM_ERR; 260 261 skb->dev = ndev; 262 hns3_lp_setup_skb(skb); 263 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID; 264 265 good_cnt = 0; 266 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) { 267 netdev_tx_t tx_ret; 268 269 skb_get(skb); 270 tx_ret = hns3_nic_net_xmit(skb, ndev); 271 if (tx_ret == NETDEV_TX_OK) { 272 good_cnt++; 273 } else { 274 kfree_skb(skb); 275 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n", 276 tx_ret); 277 } 278 } 279 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) { 280 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR; 281 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n", 282 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM); 283 goto out; 284 } 285 286 /* Allow 200 milliseconds for packets to go from Tx to Rx */ 287 msleep(200); 288 289 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM); 290 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) { 291 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR; 292 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n", 293 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM); 294 } 295 296 out: 297 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID, 298 HNS3_NIC_LB_TEST_RING_ID); 299 300 kfree_skb(skb); 301 return ret_val; 302 } 303 304 static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2]) 305 { 306 st_param[HNAE3_LOOP_EXTERNAL][0] = HNAE3_LOOP_EXTERNAL; 307 st_param[HNAE3_LOOP_EXTERNAL][1] = 308 h->flags & HNAE3_SUPPORT_EXTERNAL_LOOPBACK; 309 310 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP; 311 st_param[HNAE3_LOOP_APP][1] = 312 h->flags & HNAE3_SUPPORT_APP_LOOPBACK; 313 314 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES; 315 st_param[HNAE3_LOOP_SERIAL_SERDES][1] = 316 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK; 317 318 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] = 319 HNAE3_LOOP_PARALLEL_SERDES; 320 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] = 321 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK; 322 323 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY; 324 st_param[HNAE3_LOOP_PHY][1] = 325 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK; 326 } 327 328 static void hns3_selftest_prepare(struct net_device *ndev, bool if_running) 329 { 330 struct hns3_nic_priv *priv = netdev_priv(ndev); 331 struct hnae3_handle *h = priv->ae_handle; 332 333 if (if_running) 334 ndev->netdev_ops->ndo_stop(ndev); 335 336 #if IS_ENABLED(CONFIG_VLAN_8021Q) 337 /* Disable the vlan filter for selftest does not support it */ 338 if (h->ae_algo->ops->enable_vlan_filter && 339 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) 340 h->ae_algo->ops->enable_vlan_filter(h, false); 341 #endif 342 343 /* Tell firmware to stop mac autoneg before loopback test start, 344 * otherwise loopback test may be failed when the port is still 345 * negotiating. 346 */ 347 if (h->ae_algo->ops->halt_autoneg) 348 h->ae_algo->ops->halt_autoneg(h, true); 349 350 set_bit(HNS3_NIC_STATE_TESTING, &priv->state); 351 } 352 353 static void hns3_selftest_restore(struct net_device *ndev, bool if_running) 354 { 355 struct hns3_nic_priv *priv = netdev_priv(ndev); 356 struct hnae3_handle *h = priv->ae_handle; 357 358 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state); 359 360 if (h->ae_algo->ops->halt_autoneg) 361 h->ae_algo->ops->halt_autoneg(h, false); 362 363 #if IS_ENABLED(CONFIG_VLAN_8021Q) 364 if (h->ae_algo->ops->enable_vlan_filter && 365 ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) 366 h->ae_algo->ops->enable_vlan_filter(h, true); 367 #endif 368 369 if (if_running) 370 ndev->netdev_ops->ndo_open(ndev); 371 } 372 373 static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2], 374 struct ethtool_test *eth_test, u64 *data) 375 { 376 int test_index = HNAE3_LOOP_APP; 377 u32 i; 378 379 for (i = HNAE3_LOOP_APP; i < HNAE3_LOOP_NONE; i++) { 380 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0]; 381 382 if (!st_param[i][1]) 383 continue; 384 385 data[test_index] = hns3_lp_up(ndev, loop_type); 386 if (!data[test_index]) 387 data[test_index] = hns3_lp_run_test(ndev, loop_type); 388 389 hns3_lp_down(ndev, loop_type); 390 391 if (data[test_index]) 392 eth_test->flags |= ETH_TEST_FL_FAILED; 393 394 test_index++; 395 } 396 } 397 398 static void hns3_do_external_lb(struct net_device *ndev, 399 struct ethtool_test *eth_test, u64 *data) 400 { 401 data[HNAE3_LOOP_EXTERNAL] = hns3_lp_up(ndev, HNAE3_LOOP_EXTERNAL); 402 if (!data[HNAE3_LOOP_EXTERNAL]) 403 data[HNAE3_LOOP_EXTERNAL] = hns3_lp_run_test(ndev, HNAE3_LOOP_EXTERNAL); 404 hns3_lp_down(ndev, HNAE3_LOOP_EXTERNAL); 405 406 if (data[HNAE3_LOOP_EXTERNAL]) 407 eth_test->flags |= ETH_TEST_FL_FAILED; 408 409 eth_test->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE; 410 } 411 412 /** 413 * hns3_self_test - self test 414 * @ndev: net device 415 * @eth_test: test cmd 416 * @data: test result 417 */ 418 static void hns3_self_test(struct net_device *ndev, 419 struct ethtool_test *eth_test, u64 *data) 420 { 421 struct hns3_nic_priv *priv = netdev_priv(ndev); 422 struct hnae3_handle *h = priv->ae_handle; 423 int st_param[HNAE3_LOOP_NONE][2]; 424 bool if_running = netif_running(ndev); 425 426 if (hns3_nic_resetting(ndev)) { 427 netdev_err(ndev, "dev resetting!"); 428 return; 429 } 430 431 if (!(eth_test->flags & ETH_TEST_FL_OFFLINE)) 432 return; 433 434 if (netif_msg_ifdown(h)) 435 netdev_info(ndev, "self test start\n"); 436 437 hns3_set_selftest_param(h, st_param); 438 439 /* external loopback test requires that the link is up and the duplex is 440 * full, do external test first to reduce the whole test time 441 */ 442 if (eth_test->flags & ETH_TEST_FL_EXTERNAL_LB) { 443 hns3_external_lb_prepare(ndev, if_running); 444 hns3_do_external_lb(ndev, eth_test, data); 445 hns3_external_lb_restore(ndev, if_running); 446 } 447 448 hns3_selftest_prepare(ndev, if_running); 449 hns3_do_selftest(ndev, st_param, eth_test, data); 450 hns3_selftest_restore(ndev, if_running); 451 452 if (netif_msg_ifdown(h)) 453 netdev_info(ndev, "self test end\n"); 454 } 455 456 static void hns3_update_limit_promisc_mode(struct net_device *netdev, 457 bool enable) 458 { 459 struct hnae3_handle *handle = hns3_get_handle(netdev); 460 461 if (enable) 462 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags); 463 else 464 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags); 465 466 hns3_request_update_promisc_mode(handle); 467 } 468 469 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = { 470 { "limit_promisc", hns3_update_limit_promisc_mode } 471 }; 472 473 static int hns3_get_sset_count(struct net_device *netdev, int stringset) 474 { 475 struct hnae3_handle *h = hns3_get_handle(netdev); 476 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 477 478 if (!ops->get_sset_count) 479 return -EOPNOTSUPP; 480 481 switch (stringset) { 482 case ETH_SS_STATS: 483 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) + 484 ops->get_sset_count(h, stringset)); 485 486 case ETH_SS_TEST: 487 return ops->get_sset_count(h, stringset); 488 489 case ETH_SS_PRIV_FLAGS: 490 return HNAE3_PFLAG_MAX; 491 492 default: 493 return -EOPNOTSUPP; 494 } 495 } 496 497 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats, 498 u32 stat_count, u32 num_tqps, const char *prefix) 499 { 500 #define MAX_PREFIX_SIZE (6 + 4) 501 u32 size_left; 502 u32 i, j; 503 u32 n1; 504 505 for (i = 0; i < num_tqps; i++) { 506 for (j = 0; j < stat_count; j++) { 507 data[ETH_GSTRING_LEN - 1] = '\0'; 508 509 /* first, prepend the prefix string */ 510 n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_", 511 prefix, i); 512 size_left = (ETH_GSTRING_LEN - 1) - n1; 513 514 /* now, concatenate the stats string to it */ 515 strncat(data, stats[j].stats_string, size_left); 516 data += ETH_GSTRING_LEN; 517 } 518 } 519 520 return data; 521 } 522 523 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data) 524 { 525 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 526 const char tx_prefix[] = "txq"; 527 const char rx_prefix[] = "rxq"; 528 529 /* get strings for Tx */ 530 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT, 531 kinfo->num_tqps, tx_prefix); 532 533 /* get strings for Rx */ 534 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT, 535 kinfo->num_tqps, rx_prefix); 536 537 return data; 538 } 539 540 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data) 541 { 542 struct hnae3_handle *h = hns3_get_handle(netdev); 543 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 544 char *buff = (char *)data; 545 int i; 546 547 if (!ops->get_strings) 548 return; 549 550 switch (stringset) { 551 case ETH_SS_STATS: 552 buff = hns3_get_strings_tqps(h, buff); 553 ops->get_strings(h, stringset, (u8 *)buff); 554 break; 555 case ETH_SS_TEST: 556 ops->get_strings(h, stringset, data); 557 break; 558 case ETH_SS_PRIV_FLAGS: 559 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) { 560 snprintf(buff, ETH_GSTRING_LEN, "%s", 561 hns3_priv_flags[i].name); 562 buff += ETH_GSTRING_LEN; 563 } 564 break; 565 default: 566 break; 567 } 568 } 569 570 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data) 571 { 572 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 573 struct hns3_nic_priv *nic_priv = handle->priv; 574 struct hns3_enet_ring *ring; 575 u8 *stat; 576 int i, j; 577 578 /* get stats for Tx */ 579 for (i = 0; i < kinfo->num_tqps; i++) { 580 ring = &nic_priv->ring[i]; 581 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) { 582 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset; 583 *data++ = *(u64 *)stat; 584 } 585 } 586 587 /* get stats for Rx */ 588 for (i = 0; i < kinfo->num_tqps; i++) { 589 ring = &nic_priv->ring[i + kinfo->num_tqps]; 590 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) { 591 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset; 592 *data++ = *(u64 *)stat; 593 } 594 } 595 596 return data; 597 } 598 599 /* hns3_get_stats - get detail statistics. 600 * @netdev: net device 601 * @stats: statistics info. 602 * @data: statistics data. 603 */ 604 static void hns3_get_stats(struct net_device *netdev, 605 struct ethtool_stats *stats, u64 *data) 606 { 607 struct hnae3_handle *h = hns3_get_handle(netdev); 608 u64 *p = data; 609 610 if (hns3_nic_resetting(netdev)) { 611 netdev_err(netdev, "dev resetting, could not get stats\n"); 612 return; 613 } 614 615 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) { 616 netdev_err(netdev, "could not get any statistics\n"); 617 return; 618 } 619 620 h->ae_algo->ops->update_stats(h); 621 622 /* get per-queue stats */ 623 p = hns3_get_stats_tqps(h, p); 624 625 /* get MAC & other misc hardware stats */ 626 h->ae_algo->ops->get_stats(h, p); 627 } 628 629 static void hns3_get_drvinfo(struct net_device *netdev, 630 struct ethtool_drvinfo *drvinfo) 631 { 632 struct hns3_nic_priv *priv = netdev_priv(netdev); 633 struct hnae3_handle *h = priv->ae_handle; 634 u32 fw_version; 635 636 if (!h->ae_algo->ops->get_fw_version) { 637 netdev_err(netdev, "could not get fw version!\n"); 638 return; 639 } 640 641 strscpy(drvinfo->driver, dev_driver_string(&h->pdev->dev), 642 sizeof(drvinfo->driver)); 643 644 strscpy(drvinfo->bus_info, pci_name(h->pdev), 645 sizeof(drvinfo->bus_info)); 646 647 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h); 648 649 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), 650 "%lu.%lu.%lu.%lu", 651 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK, 652 HNAE3_FW_VERSION_BYTE3_SHIFT), 653 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK, 654 HNAE3_FW_VERSION_BYTE2_SHIFT), 655 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK, 656 HNAE3_FW_VERSION_BYTE1_SHIFT), 657 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK, 658 HNAE3_FW_VERSION_BYTE0_SHIFT)); 659 } 660 661 static u32 hns3_get_link(struct net_device *netdev) 662 { 663 struct hnae3_handle *h = hns3_get_handle(netdev); 664 665 if (h->ae_algo->ops->get_status) 666 return h->ae_algo->ops->get_status(h); 667 else 668 return 0; 669 } 670 671 static void hns3_get_ringparam(struct net_device *netdev, 672 struct ethtool_ringparam *param, 673 struct kernel_ethtool_ringparam *kernel_param, 674 struct netlink_ext_ack *extack) 675 { 676 struct hns3_nic_priv *priv = netdev_priv(netdev); 677 struct hnae3_handle *h = priv->ae_handle; 678 int rx_queue_index = h->kinfo.num_tqps; 679 680 if (hns3_nic_resetting(netdev) || !priv->ring) { 681 netdev_err(netdev, "failed to get ringparam value, due to dev resetting or uninited\n"); 682 return; 683 } 684 685 param->tx_max_pending = HNS3_RING_MAX_PENDING; 686 param->rx_max_pending = HNS3_RING_MAX_PENDING; 687 688 param->tx_pending = priv->ring[0].desc_num; 689 param->rx_pending = priv->ring[rx_queue_index].desc_num; 690 kernel_param->rx_buf_len = priv->ring[rx_queue_index].buf_size; 691 kernel_param->tx_push = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, 692 &priv->state); 693 } 694 695 static void hns3_get_pauseparam(struct net_device *netdev, 696 struct ethtool_pauseparam *param) 697 { 698 struct hnae3_handle *h = hns3_get_handle(netdev); 699 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 700 701 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps)) 702 return; 703 704 if (h->ae_algo->ops->get_pauseparam) 705 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg, 706 ¶m->rx_pause, ¶m->tx_pause); 707 } 708 709 static int hns3_set_pauseparam(struct net_device *netdev, 710 struct ethtool_pauseparam *param) 711 { 712 struct hnae3_handle *h = hns3_get_handle(netdev); 713 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 714 715 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps)) 716 return -EOPNOTSUPP; 717 718 netif_dbg(h, drv, netdev, 719 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n", 720 param->autoneg, param->rx_pause, param->tx_pause); 721 722 if (h->ae_algo->ops->set_pauseparam) 723 return h->ae_algo->ops->set_pauseparam(h, param->autoneg, 724 param->rx_pause, 725 param->tx_pause); 726 return -EOPNOTSUPP; 727 } 728 729 static void hns3_get_ksettings(struct hnae3_handle *h, 730 struct ethtool_link_ksettings *cmd) 731 { 732 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 733 734 /* 1.auto_neg & speed & duplex from cmd */ 735 if (ops->get_ksettings_an_result) 736 ops->get_ksettings_an_result(h, 737 &cmd->base.autoneg, 738 &cmd->base.speed, 739 &cmd->base.duplex, 740 &cmd->lanes); 741 742 /* 2.get link mode */ 743 if (ops->get_link_mode) 744 ops->get_link_mode(h, 745 cmd->link_modes.supported, 746 cmd->link_modes.advertising); 747 748 /* 3.mdix_ctrl&mdix get from phy reg */ 749 if (ops->get_mdix_mode) 750 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl, 751 &cmd->base.eth_tp_mdix); 752 } 753 754 static int hns3_get_link_ksettings(struct net_device *netdev, 755 struct ethtool_link_ksettings *cmd) 756 { 757 struct hnae3_handle *h = hns3_get_handle(netdev); 758 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 759 const struct hnae3_ae_ops *ops; 760 u8 module_type; 761 u8 media_type; 762 u8 link_stat; 763 764 ops = h->ae_algo->ops; 765 if (ops->get_media_type) 766 ops->get_media_type(h, &media_type, &module_type); 767 else 768 return -EOPNOTSUPP; 769 770 switch (media_type) { 771 case HNAE3_MEDIA_TYPE_NONE: 772 cmd->base.port = PORT_NONE; 773 hns3_get_ksettings(h, cmd); 774 break; 775 case HNAE3_MEDIA_TYPE_FIBER: 776 if (module_type == HNAE3_MODULE_TYPE_UNKNOWN) 777 cmd->base.port = PORT_OTHER; 778 else if (module_type == HNAE3_MODULE_TYPE_CR) 779 cmd->base.port = PORT_DA; 780 else 781 cmd->base.port = PORT_FIBRE; 782 783 hns3_get_ksettings(h, cmd); 784 break; 785 case HNAE3_MEDIA_TYPE_BACKPLANE: 786 cmd->base.port = PORT_NONE; 787 hns3_get_ksettings(h, cmd); 788 break; 789 case HNAE3_MEDIA_TYPE_COPPER: 790 cmd->base.port = PORT_TP; 791 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) && 792 ops->get_phy_link_ksettings) 793 ops->get_phy_link_ksettings(h, cmd); 794 else if (!netdev->phydev) 795 hns3_get_ksettings(h, cmd); 796 else 797 phy_ethtool_ksettings_get(netdev->phydev, cmd); 798 break; 799 default: 800 801 netdev_warn(netdev, "Unknown media type"); 802 return 0; 803 } 804 805 /* mdio_support */ 806 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22; 807 808 link_stat = hns3_get_link(netdev); 809 if (!link_stat) { 810 cmd->base.speed = SPEED_UNKNOWN; 811 cmd->base.duplex = DUPLEX_UNKNOWN; 812 } 813 814 return 0; 815 } 816 817 static int hns3_check_ksettings_param(const struct net_device *netdev, 818 const struct ethtool_link_ksettings *cmd) 819 { 820 struct hnae3_handle *handle = hns3_get_handle(netdev); 821 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 822 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN; 823 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN; 824 u32 lane_num; 825 u8 autoneg; 826 u32 speed; 827 u8 duplex; 828 int ret; 829 830 /* hw doesn't support use specified speed and duplex to negotiate, 831 * unnecessary to check them when autoneg on. 832 */ 833 if (cmd->base.autoneg) 834 return 0; 835 836 if (ops->get_ksettings_an_result) { 837 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex, &lane_num); 838 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed && 839 cmd->base.duplex == duplex && cmd->lanes == lane_num) 840 return 0; 841 } 842 843 if (ops->get_media_type) 844 ops->get_media_type(handle, &media_type, &module_type); 845 846 if (cmd->base.duplex == DUPLEX_HALF && 847 media_type != HNAE3_MEDIA_TYPE_COPPER) { 848 netdev_err(netdev, 849 "only copper port supports half duplex!"); 850 return -EINVAL; 851 } 852 853 if (ops->check_port_speed) { 854 ret = ops->check_port_speed(handle, cmd->base.speed); 855 if (ret) { 856 netdev_err(netdev, "unsupported speed\n"); 857 return ret; 858 } 859 } 860 861 return 0; 862 } 863 864 static int hns3_set_link_ksettings(struct net_device *netdev, 865 const struct ethtool_link_ksettings *cmd) 866 { 867 struct hnae3_handle *handle = hns3_get_handle(netdev); 868 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 869 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 870 int ret; 871 872 /* Chip don't support this mode. */ 873 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF) 874 return -EINVAL; 875 876 if (cmd->lanes && !hnae3_ae_dev_lane_num_supported(ae_dev)) 877 return -EOPNOTSUPP; 878 879 netif_dbg(handle, drv, netdev, 880 "set link(%s): autoneg=%u, speed=%u, duplex=%u, lanes=%u\n", 881 netdev->phydev ? "phy" : "mac", 882 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex, 883 cmd->lanes); 884 885 /* Only support ksettings_set for netdev with phy attached for now */ 886 if (netdev->phydev) { 887 if (cmd->base.speed == SPEED_1000 && 888 cmd->base.autoneg == AUTONEG_DISABLE) 889 return -EINVAL; 890 891 return phy_ethtool_ksettings_set(netdev->phydev, cmd); 892 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) && 893 ops->set_phy_link_ksettings) { 894 return ops->set_phy_link_ksettings(handle, cmd); 895 } 896 897 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2) 898 return -EOPNOTSUPP; 899 900 ret = hns3_check_ksettings_param(netdev, cmd); 901 if (ret) 902 return ret; 903 904 if (ops->set_autoneg) { 905 ret = ops->set_autoneg(handle, cmd->base.autoneg); 906 if (ret) 907 return ret; 908 } 909 910 /* hw doesn't support use specified speed and duplex to negotiate, 911 * ignore them when autoneg on. 912 */ 913 if (cmd->base.autoneg) { 914 netdev_info(netdev, 915 "autoneg is on, ignore the speed and duplex\n"); 916 return 0; 917 } 918 919 if (ops->cfg_mac_speed_dup_h) 920 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed, 921 cmd->base.duplex, (u8)(cmd->lanes)); 922 923 return ret; 924 } 925 926 static u32 hns3_get_rss_key_size(struct net_device *netdev) 927 { 928 struct hnae3_handle *h = hns3_get_handle(netdev); 929 930 if (!h->ae_algo->ops->get_rss_key_size) 931 return 0; 932 933 return h->ae_algo->ops->get_rss_key_size(h); 934 } 935 936 static u32 hns3_get_rss_indir_size(struct net_device *netdev) 937 { 938 struct hnae3_handle *h = hns3_get_handle(netdev); 939 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 940 941 return ae_dev->dev_specs.rss_ind_tbl_size; 942 } 943 944 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key, 945 u8 *hfunc) 946 { 947 struct hnae3_handle *h = hns3_get_handle(netdev); 948 949 if (!h->ae_algo->ops->get_rss) 950 return -EOPNOTSUPP; 951 952 return h->ae_algo->ops->get_rss(h, indir, key, hfunc); 953 } 954 955 static int hns3_set_rss(struct net_device *netdev, const u32 *indir, 956 const u8 *key, const u8 hfunc) 957 { 958 struct hnae3_handle *h = hns3_get_handle(netdev); 959 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 960 961 if (!h->ae_algo->ops->set_rss) 962 return -EOPNOTSUPP; 963 964 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 && 965 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE && 966 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) { 967 netdev_err(netdev, "hash func not supported\n"); 968 return -EOPNOTSUPP; 969 } 970 971 if (!indir) { 972 netdev_err(netdev, 973 "set rss failed for indir is empty\n"); 974 return -EOPNOTSUPP; 975 } 976 977 return h->ae_algo->ops->set_rss(h, indir, key, 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 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