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 hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv; 573 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 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_CR) 777 cmd->base.port = PORT_DA; 778 else 779 cmd->base.port = PORT_FIBRE; 780 781 hns3_get_ksettings(h, cmd); 782 break; 783 case HNAE3_MEDIA_TYPE_BACKPLANE: 784 cmd->base.port = PORT_NONE; 785 hns3_get_ksettings(h, cmd); 786 break; 787 case HNAE3_MEDIA_TYPE_COPPER: 788 cmd->base.port = PORT_TP; 789 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) && 790 ops->get_phy_link_ksettings) 791 ops->get_phy_link_ksettings(h, cmd); 792 else if (!netdev->phydev) 793 hns3_get_ksettings(h, cmd); 794 else 795 phy_ethtool_ksettings_get(netdev->phydev, cmd); 796 break; 797 default: 798 799 netdev_warn(netdev, "Unknown media type"); 800 return 0; 801 } 802 803 /* mdio_support */ 804 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22; 805 806 link_stat = hns3_get_link(netdev); 807 if (!link_stat) { 808 cmd->base.speed = SPEED_UNKNOWN; 809 cmd->base.duplex = DUPLEX_UNKNOWN; 810 } 811 812 return 0; 813 } 814 815 static int hns3_check_ksettings_param(const struct net_device *netdev, 816 const struct ethtool_link_ksettings *cmd) 817 { 818 struct hnae3_handle *handle = hns3_get_handle(netdev); 819 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 820 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN; 821 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN; 822 u32 lane_num; 823 u8 autoneg; 824 u32 speed; 825 u8 duplex; 826 int ret; 827 828 /* hw doesn't support use specified speed and duplex to negotiate, 829 * unnecessary to check them when autoneg on. 830 */ 831 if (cmd->base.autoneg) 832 return 0; 833 834 if (ops->get_ksettings_an_result) { 835 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex, &lane_num); 836 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed && 837 cmd->base.duplex == duplex && cmd->lanes == lane_num) 838 return 0; 839 } 840 841 if (ops->get_media_type) 842 ops->get_media_type(handle, &media_type, &module_type); 843 844 if (cmd->base.duplex == DUPLEX_HALF && 845 media_type != HNAE3_MEDIA_TYPE_COPPER) { 846 netdev_err(netdev, 847 "only copper port supports half duplex!"); 848 return -EINVAL; 849 } 850 851 if (ops->check_port_speed) { 852 ret = ops->check_port_speed(handle, cmd->base.speed); 853 if (ret) { 854 netdev_err(netdev, "unsupported speed\n"); 855 return ret; 856 } 857 } 858 859 return 0; 860 } 861 862 static int hns3_set_link_ksettings(struct net_device *netdev, 863 const struct ethtool_link_ksettings *cmd) 864 { 865 struct hnae3_handle *handle = hns3_get_handle(netdev); 866 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 867 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 868 int ret; 869 870 /* Chip don't support this mode. */ 871 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF) 872 return -EINVAL; 873 874 if (cmd->lanes && !hnae3_ae_dev_lane_num_supported(ae_dev)) 875 return -EOPNOTSUPP; 876 877 netif_dbg(handle, drv, netdev, 878 "set link(%s): autoneg=%u, speed=%u, duplex=%u, lanes=%u\n", 879 netdev->phydev ? "phy" : "mac", 880 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex, 881 cmd->lanes); 882 883 /* Only support ksettings_set for netdev with phy attached for now */ 884 if (netdev->phydev) { 885 if (cmd->base.speed == SPEED_1000 && 886 cmd->base.autoneg == AUTONEG_DISABLE) 887 return -EINVAL; 888 889 return phy_ethtool_ksettings_set(netdev->phydev, cmd); 890 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) && 891 ops->set_phy_link_ksettings) { 892 return ops->set_phy_link_ksettings(handle, cmd); 893 } 894 895 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2) 896 return -EOPNOTSUPP; 897 898 ret = hns3_check_ksettings_param(netdev, cmd); 899 if (ret) 900 return ret; 901 902 if (ops->set_autoneg) { 903 ret = ops->set_autoneg(handle, cmd->base.autoneg); 904 if (ret) 905 return ret; 906 } 907 908 /* hw doesn't support use specified speed and duplex to negotiate, 909 * ignore them when autoneg on. 910 */ 911 if (cmd->base.autoneg) { 912 netdev_info(netdev, 913 "autoneg is on, ignore the speed and duplex\n"); 914 return 0; 915 } 916 917 if (ops->cfg_mac_speed_dup_h) 918 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed, 919 cmd->base.duplex, (u8)(cmd->lanes)); 920 921 return ret; 922 } 923 924 static u32 hns3_get_rss_key_size(struct net_device *netdev) 925 { 926 struct hnae3_handle *h = hns3_get_handle(netdev); 927 928 if (!h->ae_algo->ops->get_rss_key_size) 929 return 0; 930 931 return h->ae_algo->ops->get_rss_key_size(h); 932 } 933 934 static u32 hns3_get_rss_indir_size(struct net_device *netdev) 935 { 936 struct hnae3_handle *h = hns3_get_handle(netdev); 937 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 938 939 return ae_dev->dev_specs.rss_ind_tbl_size; 940 } 941 942 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key, 943 u8 *hfunc) 944 { 945 struct hnae3_handle *h = hns3_get_handle(netdev); 946 947 if (!h->ae_algo->ops->get_rss) 948 return -EOPNOTSUPP; 949 950 return h->ae_algo->ops->get_rss(h, indir, key, hfunc); 951 } 952 953 static int hns3_set_rss(struct net_device *netdev, const u32 *indir, 954 const u8 *key, const u8 hfunc) 955 { 956 struct hnae3_handle *h = hns3_get_handle(netdev); 957 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 958 959 if (!h->ae_algo->ops->set_rss) 960 return -EOPNOTSUPP; 961 962 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 && 963 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE && 964 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) { 965 netdev_err(netdev, "hash func not supported\n"); 966 return -EOPNOTSUPP; 967 } 968 969 if (!indir) { 970 netdev_err(netdev, 971 "set rss failed for indir is empty\n"); 972 return -EOPNOTSUPP; 973 } 974 975 return h->ae_algo->ops->set_rss(h, indir, key, hfunc); 976 } 977 978 static int hns3_get_rxnfc(struct net_device *netdev, 979 struct ethtool_rxnfc *cmd, 980 u32 *rule_locs) 981 { 982 struct hnae3_handle *h = hns3_get_handle(netdev); 983 984 switch (cmd->cmd) { 985 case ETHTOOL_GRXRINGS: 986 cmd->data = h->kinfo.num_tqps; 987 return 0; 988 case ETHTOOL_GRXFH: 989 if (h->ae_algo->ops->get_rss_tuple) 990 return h->ae_algo->ops->get_rss_tuple(h, cmd); 991 return -EOPNOTSUPP; 992 case ETHTOOL_GRXCLSRLCNT: 993 if (h->ae_algo->ops->get_fd_rule_cnt) 994 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd); 995 return -EOPNOTSUPP; 996 case ETHTOOL_GRXCLSRULE: 997 if (h->ae_algo->ops->get_fd_rule_info) 998 return h->ae_algo->ops->get_fd_rule_info(h, cmd); 999 return -EOPNOTSUPP; 1000 case ETHTOOL_GRXCLSRLALL: 1001 if (h->ae_algo->ops->get_fd_all_rules) 1002 return h->ae_algo->ops->get_fd_all_rules(h, cmd, 1003 rule_locs); 1004 return -EOPNOTSUPP; 1005 default: 1006 return -EOPNOTSUPP; 1007 } 1008 } 1009 1010 static const struct hns3_reset_type_map hns3_reset_type[] = { 1011 {ETH_RESET_MGMT, HNAE3_IMP_RESET}, 1012 {ETH_RESET_ALL, HNAE3_GLOBAL_RESET}, 1013 {ETH_RESET_DEDICATED, HNAE3_FUNC_RESET}, 1014 }; 1015 1016 static const struct hns3_reset_type_map hns3vf_reset_type[] = { 1017 {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET}, 1018 }; 1019 1020 static int hns3_set_reset(struct net_device *netdev, u32 *flags) 1021 { 1022 enum hnae3_reset_type rst_type = HNAE3_NONE_RESET; 1023 struct hnae3_handle *h = hns3_get_handle(netdev); 1024 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 1025 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 1026 const struct hns3_reset_type_map *rst_type_map; 1027 enum ethtool_reset_flags rst_flags; 1028 u32 i, size; 1029 1030 if (ops->ae_dev_resetting && ops->ae_dev_resetting(h)) 1031 return -EBUSY; 1032 1033 if (!ops->set_default_reset_request || !ops->reset_event) 1034 return -EOPNOTSUPP; 1035 1036 if (h->flags & HNAE3_SUPPORT_VF) { 1037 rst_type_map = hns3vf_reset_type; 1038 size = ARRAY_SIZE(hns3vf_reset_type); 1039 } else { 1040 rst_type_map = hns3_reset_type; 1041 size = ARRAY_SIZE(hns3_reset_type); 1042 } 1043 1044 for (i = 0; i < size; i++) { 1045 if (rst_type_map[i].rst_flags == *flags) { 1046 rst_type = rst_type_map[i].rst_type; 1047 rst_flags = rst_type_map[i].rst_flags; 1048 break; 1049 } 1050 } 1051 1052 if (rst_type == HNAE3_NONE_RESET || 1053 (rst_type == HNAE3_IMP_RESET && 1054 ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2)) 1055 return -EOPNOTSUPP; 1056 1057 netdev_info(netdev, "Setting reset type %d\n", rst_type); 1058 1059 ops->set_default_reset_request(ae_dev, rst_type); 1060 1061 ops->reset_event(h->pdev, h); 1062 1063 *flags &= ~rst_flags; 1064 1065 return 0; 1066 } 1067 1068 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv, 1069 u32 tx_desc_num, u32 rx_desc_num) 1070 { 1071 struct hnae3_handle *h = priv->ae_handle; 1072 int i; 1073 1074 h->kinfo.num_tx_desc = tx_desc_num; 1075 h->kinfo.num_rx_desc = rx_desc_num; 1076 1077 for (i = 0; i < h->kinfo.num_tqps; i++) { 1078 priv->ring[i].desc_num = tx_desc_num; 1079 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num; 1080 } 1081 } 1082 1083 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv) 1084 { 1085 struct hnae3_handle *handle = priv->ae_handle; 1086 struct hns3_enet_ring *tmp_rings; 1087 int i; 1088 1089 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2, 1090 sizeof(struct hns3_enet_ring), GFP_KERNEL); 1091 if (!tmp_rings) 1092 return NULL; 1093 1094 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) { 1095 memcpy(&tmp_rings[i], &priv->ring[i], 1096 sizeof(struct hns3_enet_ring)); 1097 tmp_rings[i].skb = NULL; 1098 } 1099 1100 return tmp_rings; 1101 } 1102 1103 static int hns3_check_ringparam(struct net_device *ndev, 1104 struct ethtool_ringparam *param, 1105 struct kernel_ethtool_ringparam *kernel_param) 1106 { 1107 #define RX_BUF_LEN_2K 2048 1108 #define RX_BUF_LEN_4K 4096 1109 1110 struct hns3_nic_priv *priv = netdev_priv(ndev); 1111 1112 if (hns3_nic_resetting(ndev) || !priv->ring) { 1113 netdev_err(ndev, "failed to set ringparam value, due to dev resetting or uninited\n"); 1114 return -EBUSY; 1115 } 1116 1117 1118 if (param->rx_mini_pending || param->rx_jumbo_pending) 1119 return -EINVAL; 1120 1121 if (kernel_param->rx_buf_len != RX_BUF_LEN_2K && 1122 kernel_param->rx_buf_len != RX_BUF_LEN_4K) { 1123 netdev_err(ndev, "Rx buf len only support 2048 and 4096\n"); 1124 return -EINVAL; 1125 } 1126 1127 if (param->tx_pending > HNS3_RING_MAX_PENDING || 1128 param->tx_pending < HNS3_RING_MIN_PENDING || 1129 param->rx_pending > HNS3_RING_MAX_PENDING || 1130 param->rx_pending < HNS3_RING_MIN_PENDING) { 1131 netdev_err(ndev, "Queue depth out of range [%d-%d]\n", 1132 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING); 1133 return -EINVAL; 1134 } 1135 1136 return 0; 1137 } 1138 1139 static bool 1140 hns3_is_ringparam_changed(struct net_device *ndev, 1141 struct ethtool_ringparam *param, 1142 struct kernel_ethtool_ringparam *kernel_param, 1143 struct hns3_ring_param *old_ringparam, 1144 struct hns3_ring_param *new_ringparam) 1145 { 1146 struct hns3_nic_priv *priv = netdev_priv(ndev); 1147 struct hnae3_handle *h = priv->ae_handle; 1148 u16 queue_num = h->kinfo.num_tqps; 1149 1150 new_ringparam->tx_desc_num = ALIGN(param->tx_pending, 1151 HNS3_RING_BD_MULTIPLE); 1152 new_ringparam->rx_desc_num = ALIGN(param->rx_pending, 1153 HNS3_RING_BD_MULTIPLE); 1154 old_ringparam->tx_desc_num = priv->ring[0].desc_num; 1155 old_ringparam->rx_desc_num = priv->ring[queue_num].desc_num; 1156 old_ringparam->rx_buf_len = priv->ring[queue_num].buf_size; 1157 new_ringparam->rx_buf_len = kernel_param->rx_buf_len; 1158 1159 if (old_ringparam->tx_desc_num == new_ringparam->tx_desc_num && 1160 old_ringparam->rx_desc_num == new_ringparam->rx_desc_num && 1161 old_ringparam->rx_buf_len == new_ringparam->rx_buf_len) { 1162 netdev_info(ndev, "descriptor number and rx buffer length not changed\n"); 1163 return false; 1164 } 1165 1166 return true; 1167 } 1168 1169 static int hns3_change_rx_buf_len(struct net_device *ndev, u32 rx_buf_len) 1170 { 1171 struct hns3_nic_priv *priv = netdev_priv(ndev); 1172 struct hnae3_handle *h = priv->ae_handle; 1173 int i; 1174 1175 h->kinfo.rx_buf_len = rx_buf_len; 1176 1177 for (i = 0; i < h->kinfo.num_tqps; i++) { 1178 h->kinfo.tqp[i]->buf_size = rx_buf_len; 1179 priv->ring[i + h->kinfo.num_tqps].buf_size = rx_buf_len; 1180 } 1181 1182 return 0; 1183 } 1184 1185 static int hns3_set_tx_push(struct net_device *netdev, u32 tx_push) 1186 { 1187 struct hns3_nic_priv *priv = netdev_priv(netdev); 1188 struct hnae3_handle *h = hns3_get_handle(netdev); 1189 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 1190 u32 old_state = test_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state); 1191 1192 if (!test_bit(HNAE3_DEV_SUPPORT_TX_PUSH_B, ae_dev->caps) && tx_push) 1193 return -EOPNOTSUPP; 1194 1195 if (tx_push == old_state) 1196 return 0; 1197 1198 netdev_dbg(netdev, "Changing tx push from %s to %s\n", 1199 old_state ? "on" : "off", tx_push ? "on" : "off"); 1200 1201 if (tx_push) 1202 set_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state); 1203 else 1204 clear_bit(HNS3_NIC_STATE_TX_PUSH_ENABLE, &priv->state); 1205 1206 return 0; 1207 } 1208 1209 static int hns3_set_ringparam(struct net_device *ndev, 1210 struct ethtool_ringparam *param, 1211 struct kernel_ethtool_ringparam *kernel_param, 1212 struct netlink_ext_ack *extack) 1213 { 1214 struct hns3_ring_param old_ringparam, new_ringparam; 1215 struct hns3_nic_priv *priv = netdev_priv(ndev); 1216 struct hnae3_handle *h = priv->ae_handle; 1217 struct hns3_enet_ring *tmp_rings; 1218 bool if_running = netif_running(ndev); 1219 int ret, i; 1220 1221 ret = hns3_check_ringparam(ndev, param, kernel_param); 1222 if (ret) 1223 return ret; 1224 1225 ret = hns3_set_tx_push(ndev, kernel_param->tx_push); 1226 if (ret) 1227 return ret; 1228 1229 if (!hns3_is_ringparam_changed(ndev, param, kernel_param, 1230 &old_ringparam, &new_ringparam)) 1231 return 0; 1232 1233 tmp_rings = hns3_backup_ringparam(priv); 1234 if (!tmp_rings) { 1235 netdev_err(ndev, "backup ring param failed by allocating memory fail\n"); 1236 return -ENOMEM; 1237 } 1238 1239 netdev_info(ndev, 1240 "Changing Tx/Rx ring depth from %u/%u to %u/%u, Changing rx buffer len from %u to %u\n", 1241 old_ringparam.tx_desc_num, old_ringparam.rx_desc_num, 1242 new_ringparam.tx_desc_num, new_ringparam.rx_desc_num, 1243 old_ringparam.rx_buf_len, new_ringparam.rx_buf_len); 1244 1245 if (if_running) 1246 ndev->netdev_ops->ndo_stop(ndev); 1247 1248 hns3_change_all_ring_bd_num(priv, new_ringparam.tx_desc_num, 1249 new_ringparam.rx_desc_num); 1250 hns3_change_rx_buf_len(ndev, new_ringparam.rx_buf_len); 1251 ret = hns3_init_all_ring(priv); 1252 if (ret) { 1253 netdev_err(ndev, "set ringparam fail, revert to old value(%d)\n", 1254 ret); 1255 1256 hns3_change_rx_buf_len(ndev, old_ringparam.rx_buf_len); 1257 hns3_change_all_ring_bd_num(priv, old_ringparam.tx_desc_num, 1258 old_ringparam.rx_desc_num); 1259 for (i = 0; i < h->kinfo.num_tqps * 2; i++) 1260 memcpy(&priv->ring[i], &tmp_rings[i], 1261 sizeof(struct hns3_enet_ring)); 1262 } else { 1263 for (i = 0; i < h->kinfo.num_tqps * 2; i++) 1264 hns3_fini_ring(&tmp_rings[i]); 1265 } 1266 1267 kfree(tmp_rings); 1268 1269 if (if_running) 1270 ret = ndev->netdev_ops->ndo_open(ndev); 1271 1272 return ret; 1273 } 1274 1275 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) 1276 { 1277 struct hnae3_handle *h = hns3_get_handle(netdev); 1278 1279 switch (cmd->cmd) { 1280 case ETHTOOL_SRXFH: 1281 if (h->ae_algo->ops->set_rss_tuple) 1282 return h->ae_algo->ops->set_rss_tuple(h, cmd); 1283 return -EOPNOTSUPP; 1284 case ETHTOOL_SRXCLSRLINS: 1285 if (h->ae_algo->ops->add_fd_entry) 1286 return h->ae_algo->ops->add_fd_entry(h, cmd); 1287 return -EOPNOTSUPP; 1288 case ETHTOOL_SRXCLSRLDEL: 1289 if (h->ae_algo->ops->del_fd_entry) 1290 return h->ae_algo->ops->del_fd_entry(h, cmd); 1291 return -EOPNOTSUPP; 1292 default: 1293 return -EOPNOTSUPP; 1294 } 1295 } 1296 1297 static int hns3_nway_reset(struct net_device *netdev) 1298 { 1299 struct hnae3_handle *handle = hns3_get_handle(netdev); 1300 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1301 struct phy_device *phy = netdev->phydev; 1302 int autoneg; 1303 1304 if (!netif_running(netdev)) 1305 return 0; 1306 1307 if (hns3_nic_resetting(netdev)) { 1308 netdev_err(netdev, "dev resetting!"); 1309 return -EBUSY; 1310 } 1311 1312 if (!ops->get_autoneg || !ops->restart_autoneg) 1313 return -EOPNOTSUPP; 1314 1315 autoneg = ops->get_autoneg(handle); 1316 if (autoneg != AUTONEG_ENABLE) { 1317 netdev_err(netdev, 1318 "Autoneg is off, don't support to restart it\n"); 1319 return -EINVAL; 1320 } 1321 1322 netif_dbg(handle, drv, netdev, 1323 "nway reset (using %s)\n", phy ? "phy" : "mac"); 1324 1325 if (phy) 1326 return genphy_restart_aneg(phy); 1327 1328 return ops->restart_autoneg(handle); 1329 } 1330 1331 static void hns3_get_channels(struct net_device *netdev, 1332 struct ethtool_channels *ch) 1333 { 1334 struct hnae3_handle *h = hns3_get_handle(netdev); 1335 1336 if (h->ae_algo->ops->get_channels) 1337 h->ae_algo->ops->get_channels(h, ch); 1338 } 1339 1340 static int hns3_get_coalesce(struct net_device *netdev, 1341 struct ethtool_coalesce *cmd, 1342 struct kernel_ethtool_coalesce *kernel_coal, 1343 struct netlink_ext_ack *extack) 1344 { 1345 struct hns3_nic_priv *priv = netdev_priv(netdev); 1346 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal; 1347 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal; 1348 struct hnae3_handle *h = priv->ae_handle; 1349 1350 if (hns3_nic_resetting(netdev)) 1351 return -EBUSY; 1352 1353 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable; 1354 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable; 1355 1356 cmd->tx_coalesce_usecs = tx_coal->int_gl; 1357 cmd->rx_coalesce_usecs = rx_coal->int_gl; 1358 1359 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting; 1360 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting; 1361 1362 cmd->tx_max_coalesced_frames = tx_coal->int_ql; 1363 cmd->rx_max_coalesced_frames = rx_coal->int_ql; 1364 1365 kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode == 1366 DIM_CQ_PERIOD_MODE_START_FROM_CQE); 1367 kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode == 1368 DIM_CQ_PERIOD_MODE_START_FROM_CQE); 1369 1370 return 0; 1371 } 1372 1373 static int hns3_check_gl_coalesce_para(struct net_device *netdev, 1374 struct ethtool_coalesce *cmd) 1375 { 1376 struct hnae3_handle *handle = hns3_get_handle(netdev); 1377 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1378 u32 rx_gl, tx_gl; 1379 1380 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) { 1381 netdev_err(netdev, 1382 "invalid rx-usecs value, rx-usecs range is 0-%u\n", 1383 ae_dev->dev_specs.max_int_gl); 1384 return -EINVAL; 1385 } 1386 1387 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) { 1388 netdev_err(netdev, 1389 "invalid tx-usecs value, tx-usecs range is 0-%u\n", 1390 ae_dev->dev_specs.max_int_gl); 1391 return -EINVAL; 1392 } 1393 1394 /* device version above V3(include V3), GL uses 1us unit, 1395 * so the round down is not needed. 1396 */ 1397 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3) 1398 return 0; 1399 1400 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs); 1401 if (rx_gl != cmd->rx_coalesce_usecs) { 1402 netdev_info(netdev, 1403 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n", 1404 cmd->rx_coalesce_usecs, rx_gl); 1405 } 1406 1407 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs); 1408 if (tx_gl != cmd->tx_coalesce_usecs) { 1409 netdev_info(netdev, 1410 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n", 1411 cmd->tx_coalesce_usecs, tx_gl); 1412 } 1413 1414 return 0; 1415 } 1416 1417 static int hns3_check_rl_coalesce_para(struct net_device *netdev, 1418 struct ethtool_coalesce *cmd) 1419 { 1420 u32 rl; 1421 1422 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) { 1423 netdev_err(netdev, 1424 "tx_usecs_high must be same as rx_usecs_high.\n"); 1425 return -EINVAL; 1426 } 1427 1428 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) { 1429 netdev_err(netdev, 1430 "Invalid usecs_high value, usecs_high range is 0-%d\n", 1431 HNS3_INT_RL_MAX); 1432 return -EINVAL; 1433 } 1434 1435 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high); 1436 if (rl != cmd->rx_coalesce_usecs_high) { 1437 netdev_info(netdev, 1438 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n", 1439 cmd->rx_coalesce_usecs_high, rl); 1440 } 1441 1442 return 0; 1443 } 1444 1445 static int hns3_check_ql_coalesce_param(struct net_device *netdev, 1446 struct ethtool_coalesce *cmd) 1447 { 1448 struct hnae3_handle *handle = hns3_get_handle(netdev); 1449 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1450 1451 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) && 1452 !ae_dev->dev_specs.int_ql_max) { 1453 netdev_err(netdev, "coalesced frames is not supported\n"); 1454 return -EOPNOTSUPP; 1455 } 1456 1457 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max || 1458 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) { 1459 netdev_err(netdev, 1460 "invalid coalesced_frames value, range is 0-%u\n", 1461 ae_dev->dev_specs.int_ql_max); 1462 return -ERANGE; 1463 } 1464 1465 return 0; 1466 } 1467 1468 static int 1469 hns3_check_cqe_coalesce_param(struct net_device *netdev, 1470 struct kernel_ethtool_coalesce *kernel_coal) 1471 { 1472 struct hnae3_handle *handle = hns3_get_handle(netdev); 1473 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1474 1475 if ((kernel_coal->use_cqe_mode_tx || kernel_coal->use_cqe_mode_rx) && 1476 !hnae3_ae_dev_cq_supported(ae_dev)) { 1477 netdev_err(netdev, "coalesced cqe mode is not supported\n"); 1478 return -EOPNOTSUPP; 1479 } 1480 1481 return 0; 1482 } 1483 1484 static int 1485 hns3_check_coalesce_para(struct net_device *netdev, 1486 struct ethtool_coalesce *cmd, 1487 struct kernel_ethtool_coalesce *kernel_coal) 1488 { 1489 int ret; 1490 1491 ret = hns3_check_cqe_coalesce_param(netdev, kernel_coal); 1492 if (ret) 1493 return ret; 1494 1495 ret = hns3_check_gl_coalesce_para(netdev, cmd); 1496 if (ret) { 1497 netdev_err(netdev, 1498 "Check gl coalesce param fail. ret = %d\n", ret); 1499 return ret; 1500 } 1501 1502 ret = hns3_check_rl_coalesce_para(netdev, cmd); 1503 if (ret) { 1504 netdev_err(netdev, 1505 "Check rl coalesce param fail. ret = %d\n", ret); 1506 return ret; 1507 } 1508 1509 return hns3_check_ql_coalesce_param(netdev, cmd); 1510 } 1511 1512 static void hns3_set_coalesce_per_queue(struct net_device *netdev, 1513 struct ethtool_coalesce *cmd, 1514 u32 queue) 1515 { 1516 struct hns3_enet_tqp_vector *tx_vector, *rx_vector; 1517 struct hns3_nic_priv *priv = netdev_priv(netdev); 1518 struct hnae3_handle *h = priv->ae_handle; 1519 int queue_num = h->kinfo.num_tqps; 1520 1521 tx_vector = priv->ring[queue].tqp_vector; 1522 rx_vector = priv->ring[queue_num + queue].tqp_vector; 1523 1524 tx_vector->tx_group.coal.adapt_enable = 1525 cmd->use_adaptive_tx_coalesce; 1526 rx_vector->rx_group.coal.adapt_enable = 1527 cmd->use_adaptive_rx_coalesce; 1528 1529 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs; 1530 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs; 1531 1532 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames; 1533 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames; 1534 1535 hns3_set_vector_coalesce_tx_gl(tx_vector, 1536 tx_vector->tx_group.coal.int_gl); 1537 hns3_set_vector_coalesce_rx_gl(rx_vector, 1538 rx_vector->rx_group.coal.int_gl); 1539 1540 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting); 1541 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting); 1542 1543 if (tx_vector->tx_group.coal.ql_enable) 1544 hns3_set_vector_coalesce_tx_ql(tx_vector, 1545 tx_vector->tx_group.coal.int_ql); 1546 if (rx_vector->rx_group.coal.ql_enable) 1547 hns3_set_vector_coalesce_rx_ql(rx_vector, 1548 rx_vector->rx_group.coal.int_ql); 1549 } 1550 1551 static int hns3_set_coalesce(struct net_device *netdev, 1552 struct ethtool_coalesce *cmd, 1553 struct kernel_ethtool_coalesce *kernel_coal, 1554 struct netlink_ext_ack *extack) 1555 { 1556 struct hnae3_handle *h = hns3_get_handle(netdev); 1557 struct hns3_nic_priv *priv = netdev_priv(netdev); 1558 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal; 1559 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal; 1560 u16 queue_num = h->kinfo.num_tqps; 1561 enum dim_cq_period_mode tx_mode; 1562 enum dim_cq_period_mode rx_mode; 1563 int ret; 1564 int i; 1565 1566 if (hns3_nic_resetting(netdev)) 1567 return -EBUSY; 1568 1569 ret = hns3_check_coalesce_para(netdev, cmd, kernel_coal); 1570 if (ret) 1571 return ret; 1572 1573 h->kinfo.int_rl_setting = 1574 hns3_rl_round_down(cmd->rx_coalesce_usecs_high); 1575 1576 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce; 1577 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce; 1578 1579 tx_coal->int_gl = cmd->tx_coalesce_usecs; 1580 rx_coal->int_gl = cmd->rx_coalesce_usecs; 1581 1582 tx_coal->int_ql = cmd->tx_max_coalesced_frames; 1583 rx_coal->int_ql = cmd->rx_max_coalesced_frames; 1584 1585 for (i = 0; i < queue_num; i++) 1586 hns3_set_coalesce_per_queue(netdev, cmd, i); 1587 1588 tx_mode = kernel_coal->use_cqe_mode_tx ? 1589 DIM_CQ_PERIOD_MODE_START_FROM_CQE : 1590 DIM_CQ_PERIOD_MODE_START_FROM_EQE; 1591 rx_mode = kernel_coal->use_cqe_mode_rx ? 1592 DIM_CQ_PERIOD_MODE_START_FROM_CQE : 1593 DIM_CQ_PERIOD_MODE_START_FROM_EQE; 1594 hns3_cq_period_mode_init(priv, tx_mode, rx_mode); 1595 1596 return 0; 1597 } 1598 1599 static int hns3_get_regs_len(struct net_device *netdev) 1600 { 1601 struct hnae3_handle *h = hns3_get_handle(netdev); 1602 1603 if (!h->ae_algo->ops->get_regs_len) 1604 return -EOPNOTSUPP; 1605 1606 return h->ae_algo->ops->get_regs_len(h); 1607 } 1608 1609 static void hns3_get_regs(struct net_device *netdev, 1610 struct ethtool_regs *cmd, void *data) 1611 { 1612 struct hnae3_handle *h = hns3_get_handle(netdev); 1613 1614 if (!h->ae_algo->ops->get_regs) 1615 return; 1616 1617 h->ae_algo->ops->get_regs(h, &cmd->version, data); 1618 } 1619 1620 static int hns3_set_phys_id(struct net_device *netdev, 1621 enum ethtool_phys_id_state state) 1622 { 1623 struct hnae3_handle *h = hns3_get_handle(netdev); 1624 1625 if (!h->ae_algo->ops->set_led_id) 1626 return -EOPNOTSUPP; 1627 1628 return h->ae_algo->ops->set_led_id(h, state); 1629 } 1630 1631 static u32 hns3_get_msglevel(struct net_device *netdev) 1632 { 1633 struct hnae3_handle *h = hns3_get_handle(netdev); 1634 1635 return h->msg_enable; 1636 } 1637 1638 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level) 1639 { 1640 struct hnae3_handle *h = hns3_get_handle(netdev); 1641 1642 h->msg_enable = msg_level; 1643 } 1644 1645 static void hns3_get_fec_stats(struct net_device *netdev, 1646 struct ethtool_fec_stats *fec_stats) 1647 { 1648 struct hnae3_handle *handle = hns3_get_handle(netdev); 1649 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1650 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1651 1652 if (!hnae3_ae_dev_fec_stats_supported(ae_dev) || !ops->get_fec_stats) 1653 return; 1654 1655 ops->get_fec_stats(handle, fec_stats); 1656 } 1657 1658 /* Translate local fec value into ethtool value. */ 1659 static unsigned int loc_to_eth_fec(u8 loc_fec) 1660 { 1661 u32 eth_fec = 0; 1662 1663 if (loc_fec & BIT(HNAE3_FEC_AUTO)) 1664 eth_fec |= ETHTOOL_FEC_AUTO; 1665 if (loc_fec & BIT(HNAE3_FEC_RS)) 1666 eth_fec |= ETHTOOL_FEC_RS; 1667 if (loc_fec & BIT(HNAE3_FEC_LLRS)) 1668 eth_fec |= ETHTOOL_FEC_LLRS; 1669 if (loc_fec & BIT(HNAE3_FEC_BASER)) 1670 eth_fec |= ETHTOOL_FEC_BASER; 1671 if (loc_fec & BIT(HNAE3_FEC_NONE)) 1672 eth_fec |= ETHTOOL_FEC_OFF; 1673 1674 return eth_fec; 1675 } 1676 1677 /* Translate ethtool fec value into local value. */ 1678 static unsigned int eth_to_loc_fec(unsigned int eth_fec) 1679 { 1680 u32 loc_fec = 0; 1681 1682 if (eth_fec & ETHTOOL_FEC_OFF) 1683 loc_fec |= BIT(HNAE3_FEC_NONE); 1684 if (eth_fec & ETHTOOL_FEC_AUTO) 1685 loc_fec |= BIT(HNAE3_FEC_AUTO); 1686 if (eth_fec & ETHTOOL_FEC_RS) 1687 loc_fec |= BIT(HNAE3_FEC_RS); 1688 if (eth_fec & ETHTOOL_FEC_LLRS) 1689 loc_fec |= BIT(HNAE3_FEC_LLRS); 1690 if (eth_fec & ETHTOOL_FEC_BASER) 1691 loc_fec |= BIT(HNAE3_FEC_BASER); 1692 1693 return loc_fec; 1694 } 1695 1696 static int hns3_get_fecparam(struct net_device *netdev, 1697 struct ethtool_fecparam *fec) 1698 { 1699 struct hnae3_handle *handle = hns3_get_handle(netdev); 1700 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1701 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1702 u8 fec_ability; 1703 u8 fec_mode; 1704 1705 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps)) 1706 return -EOPNOTSUPP; 1707 1708 if (!ops->get_fec) 1709 return -EOPNOTSUPP; 1710 1711 ops->get_fec(handle, &fec_ability, &fec_mode); 1712 1713 fec->fec = loc_to_eth_fec(fec_ability); 1714 fec->active_fec = loc_to_eth_fec(fec_mode); 1715 if (!fec->active_fec) 1716 fec->active_fec = ETHTOOL_FEC_OFF; 1717 1718 return 0; 1719 } 1720 1721 static int hns3_set_fecparam(struct net_device *netdev, 1722 struct ethtool_fecparam *fec) 1723 { 1724 struct hnae3_handle *handle = hns3_get_handle(netdev); 1725 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1726 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1727 u32 fec_mode; 1728 1729 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps)) 1730 return -EOPNOTSUPP; 1731 1732 if (!ops->set_fec) 1733 return -EOPNOTSUPP; 1734 fec_mode = eth_to_loc_fec(fec->fec); 1735 1736 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode); 1737 1738 return ops->set_fec(handle, fec_mode); 1739 } 1740 1741 static int hns3_get_module_info(struct net_device *netdev, 1742 struct ethtool_modinfo *modinfo) 1743 { 1744 #define HNS3_SFF_8636_V1_3 0x03 1745 1746 struct hnae3_handle *handle = hns3_get_handle(netdev); 1747 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1748 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1749 struct hns3_sfp_type sfp_type; 1750 int ret; 1751 1752 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 || 1753 !ops->get_module_eeprom) 1754 return -EOPNOTSUPP; 1755 1756 memset(&sfp_type, 0, sizeof(sfp_type)); 1757 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8), 1758 (u8 *)&sfp_type); 1759 if (ret) 1760 return ret; 1761 1762 switch (sfp_type.type) { 1763 case SFF8024_ID_SFP: 1764 modinfo->type = ETH_MODULE_SFF_8472; 1765 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 1766 break; 1767 case SFF8024_ID_QSFP_8438: 1768 modinfo->type = ETH_MODULE_SFF_8436; 1769 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN; 1770 break; 1771 case SFF8024_ID_QSFP_8436_8636: 1772 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) { 1773 modinfo->type = ETH_MODULE_SFF_8436; 1774 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN; 1775 } else { 1776 modinfo->type = ETH_MODULE_SFF_8636; 1777 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN; 1778 } 1779 break; 1780 case SFF8024_ID_QSFP28_8636: 1781 modinfo->type = ETH_MODULE_SFF_8636; 1782 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN; 1783 break; 1784 default: 1785 netdev_err(netdev, "Optical module unknown: %#x\n", 1786 sfp_type.type); 1787 return -EINVAL; 1788 } 1789 1790 return 0; 1791 } 1792 1793 static int hns3_get_module_eeprom(struct net_device *netdev, 1794 struct ethtool_eeprom *ee, u8 *data) 1795 { 1796 struct hnae3_handle *handle = hns3_get_handle(netdev); 1797 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev); 1798 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 1799 1800 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 || 1801 !ops->get_module_eeprom) 1802 return -EOPNOTSUPP; 1803 1804 if (!ee->len) 1805 return -EINVAL; 1806 1807 memset(data, 0, ee->len); 1808 1809 return ops->get_module_eeprom(handle, ee->offset, ee->len, data); 1810 } 1811 1812 static u32 hns3_get_priv_flags(struct net_device *netdev) 1813 { 1814 struct hnae3_handle *handle = hns3_get_handle(netdev); 1815 1816 return handle->priv_flags; 1817 } 1818 1819 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed) 1820 { 1821 u32 i; 1822 1823 for (i = 0; i < HNAE3_PFLAG_MAX; i++) 1824 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) { 1825 netdev_err(h->netdev, "%s is unsupported\n", 1826 hns3_priv_flags[i].name); 1827 return -EOPNOTSUPP; 1828 } 1829 1830 return 0; 1831 } 1832 1833 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags) 1834 { 1835 struct hnae3_handle *handle = hns3_get_handle(netdev); 1836 u32 changed = pflags ^ handle->priv_flags; 1837 int ret; 1838 u32 i; 1839 1840 ret = hns3_check_priv_flags(handle, changed); 1841 if (ret) 1842 return ret; 1843 1844 for (i = 0; i < HNAE3_PFLAG_MAX; i++) { 1845 if (changed & BIT(i)) { 1846 bool enable = !(handle->priv_flags & BIT(i)); 1847 1848 if (enable) 1849 handle->priv_flags |= BIT(i); 1850 else 1851 handle->priv_flags &= ~BIT(i); 1852 hns3_priv_flags[i].handler(netdev, enable); 1853 } 1854 } 1855 1856 return 0; 1857 } 1858 1859 static int hns3_get_tunable(struct net_device *netdev, 1860 const struct ethtool_tunable *tuna, 1861 void *data) 1862 { 1863 struct hns3_nic_priv *priv = netdev_priv(netdev); 1864 struct hnae3_handle *h = priv->ae_handle; 1865 int ret = 0; 1866 1867 switch (tuna->id) { 1868 case ETHTOOL_TX_COPYBREAK: 1869 /* all the tx rings have the same tx_copybreak */ 1870 *(u32 *)data = priv->tx_copybreak; 1871 break; 1872 case ETHTOOL_RX_COPYBREAK: 1873 *(u32 *)data = priv->rx_copybreak; 1874 break; 1875 case ETHTOOL_TX_COPYBREAK_BUF_SIZE: 1876 *(u32 *)data = h->kinfo.tx_spare_buf_size; 1877 break; 1878 default: 1879 ret = -EOPNOTSUPP; 1880 break; 1881 } 1882 1883 return ret; 1884 } 1885 1886 static int hns3_set_tx_spare_buf_size(struct net_device *netdev, 1887 u32 data) 1888 { 1889 struct hns3_nic_priv *priv = netdev_priv(netdev); 1890 struct hnae3_handle *h = priv->ae_handle; 1891 int ret; 1892 1893 h->kinfo.tx_spare_buf_size = data; 1894 1895 ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT); 1896 if (ret) 1897 return ret; 1898 1899 ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT); 1900 if (ret) 1901 return ret; 1902 1903 ret = hns3_reset_notify(h, HNAE3_INIT_CLIENT); 1904 if (ret) 1905 return ret; 1906 1907 ret = hns3_reset_notify(h, HNAE3_UP_CLIENT); 1908 if (ret) 1909 hns3_reset_notify(h, HNAE3_UNINIT_CLIENT); 1910 1911 return ret; 1912 } 1913 1914 static int hns3_set_tunable(struct net_device *netdev, 1915 const struct ethtool_tunable *tuna, 1916 const void *data) 1917 { 1918 struct hns3_nic_priv *priv = netdev_priv(netdev); 1919 u32 old_tx_spare_buf_size, new_tx_spare_buf_size; 1920 struct hnae3_handle *h = priv->ae_handle; 1921 int i, ret = 0; 1922 1923 if (hns3_nic_resetting(netdev) || !priv->ring) { 1924 netdev_err(netdev, "failed to set tunable value, dev resetting!"); 1925 return -EBUSY; 1926 } 1927 1928 switch (tuna->id) { 1929 case ETHTOOL_TX_COPYBREAK: 1930 priv->tx_copybreak = *(u32 *)data; 1931 1932 for (i = 0; i < h->kinfo.num_tqps; i++) 1933 priv->ring[i].tx_copybreak = priv->tx_copybreak; 1934 1935 break; 1936 case ETHTOOL_RX_COPYBREAK: 1937 priv->rx_copybreak = *(u32 *)data; 1938 1939 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++) 1940 priv->ring[i].rx_copybreak = priv->rx_copybreak; 1941 1942 break; 1943 case ETHTOOL_TX_COPYBREAK_BUF_SIZE: 1944 old_tx_spare_buf_size = h->kinfo.tx_spare_buf_size; 1945 new_tx_spare_buf_size = *(u32 *)data; 1946 netdev_info(netdev, "request to set tx spare buf size from %u to %u\n", 1947 old_tx_spare_buf_size, new_tx_spare_buf_size); 1948 ret = hns3_set_tx_spare_buf_size(netdev, new_tx_spare_buf_size); 1949 if (ret || 1950 (!priv->ring->tx_spare && new_tx_spare_buf_size != 0)) { 1951 int ret1; 1952 1953 netdev_warn(netdev, "change tx spare buf size fail, revert to old value\n"); 1954 ret1 = hns3_set_tx_spare_buf_size(netdev, 1955 old_tx_spare_buf_size); 1956 if (ret1) { 1957 netdev_err(netdev, "revert to old tx spare buf size fail\n"); 1958 return ret1; 1959 } 1960 1961 return ret; 1962 } 1963 1964 if (!priv->ring->tx_spare) 1965 netdev_info(netdev, "the active tx spare buf size is 0, disable tx spare buffer\n"); 1966 else 1967 netdev_info(netdev, "the active tx spare buf size is %u, due to page order\n", 1968 priv->ring->tx_spare->len); 1969 1970 break; 1971 default: 1972 ret = -EOPNOTSUPP; 1973 break; 1974 } 1975 1976 return ret; 1977 } 1978 1979 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \ 1980 ETHTOOL_COALESCE_USE_ADAPTIVE | \ 1981 ETHTOOL_COALESCE_RX_USECS_HIGH | \ 1982 ETHTOOL_COALESCE_TX_USECS_HIGH | \ 1983 ETHTOOL_COALESCE_MAX_FRAMES | \ 1984 ETHTOOL_COALESCE_USE_CQE) 1985 1986 #define HNS3_ETHTOOL_RING (ETHTOOL_RING_USE_RX_BUF_LEN | \ 1987 ETHTOOL_RING_USE_TX_PUSH) 1988 1989 static int hns3_get_ts_info(struct net_device *netdev, 1990 struct ethtool_ts_info *info) 1991 { 1992 struct hnae3_handle *handle = hns3_get_handle(netdev); 1993 1994 if (handle->ae_algo->ops->get_ts_info) 1995 return handle->ae_algo->ops->get_ts_info(handle, info); 1996 1997 return ethtool_op_get_ts_info(netdev, info); 1998 } 1999 2000 static const struct hns3_ethtool_link_ext_state_mapping 2001 hns3_link_ext_state_map[] = { 2002 {1, ETHTOOL_LINK_EXT_STATE_AUTONEG, 2003 ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD}, 2004 {2, ETHTOOL_LINK_EXT_STATE_AUTONEG, 2005 ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED}, 2006 2007 {256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE, 2008 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT}, 2009 {257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE, 2010 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY}, 2011 {512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE, 2012 ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT}, 2013 2014 {513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH, 2015 ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK}, 2016 {514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH, 2017 ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED}, 2018 {515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH, 2019 ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED}, 2020 2021 {768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY, 2022 ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS}, 2023 {769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY, 2024 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST}, 2025 {770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY, 2026 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS}, 2027 2028 {1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0}, 2029 {1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE, 2030 ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE}, 2031 2032 {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0}, 2033 }; 2034 2035 static int hns3_get_link_ext_state(struct net_device *netdev, 2036 struct ethtool_link_ext_state_info *info) 2037 { 2038 const struct hns3_ethtool_link_ext_state_mapping *map; 2039 struct hnae3_handle *h = hns3_get_handle(netdev); 2040 u32 status_code, i; 2041 int ret; 2042 2043 if (netif_carrier_ok(netdev)) 2044 return -ENODATA; 2045 2046 if (!h->ae_algo->ops->get_link_diagnosis_info) 2047 return -EOPNOTSUPP; 2048 2049 ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code); 2050 if (ret) 2051 return ret; 2052 2053 for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) { 2054 map = &hns3_link_ext_state_map[i]; 2055 if (map->status_code == status_code) { 2056 info->link_ext_state = map->link_ext_state; 2057 info->__link_ext_substate = map->link_ext_substate; 2058 return 0; 2059 } 2060 } 2061 2062 return -ENODATA; 2063 } 2064 2065 static void hns3_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol) 2066 { 2067 struct hnae3_handle *handle = hns3_get_handle(netdev); 2068 const struct hnae3_ae_ops *ops = hns3_get_ops(handle); 2069 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle); 2070 2071 if (!hnae3_ae_dev_wol_supported(ae_dev)) 2072 return; 2073 2074 ops->get_wol(handle, wol); 2075 } 2076 2077 static int hns3_set_wol(struct net_device *netdev, 2078 struct ethtool_wolinfo *wol) 2079 { 2080 struct hnae3_handle *handle = hns3_get_handle(netdev); 2081 const struct hnae3_ae_ops *ops = hns3_get_ops(handle); 2082 struct hnae3_ae_dev *ae_dev = hns3_get_ae_dev(handle); 2083 2084 if (!hnae3_ae_dev_wol_supported(ae_dev)) 2085 return -EOPNOTSUPP; 2086 2087 return ops->set_wol(handle, wol); 2088 } 2089 2090 static const struct ethtool_ops hns3vf_ethtool_ops = { 2091 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE, 2092 .supported_ring_params = HNS3_ETHTOOL_RING, 2093 .get_drvinfo = hns3_get_drvinfo, 2094 .get_ringparam = hns3_get_ringparam, 2095 .set_ringparam = hns3_set_ringparam, 2096 .get_strings = hns3_get_strings, 2097 .get_ethtool_stats = hns3_get_stats, 2098 .get_sset_count = hns3_get_sset_count, 2099 .get_rxnfc = hns3_get_rxnfc, 2100 .set_rxnfc = hns3_set_rxnfc, 2101 .get_rxfh_key_size = hns3_get_rss_key_size, 2102 .get_rxfh_indir_size = hns3_get_rss_indir_size, 2103 .get_rxfh = hns3_get_rss, 2104 .set_rxfh = hns3_set_rss, 2105 .get_link_ksettings = hns3_get_link_ksettings, 2106 .get_channels = hns3_get_channels, 2107 .set_channels = hns3_set_channels, 2108 .get_coalesce = hns3_get_coalesce, 2109 .set_coalesce = hns3_set_coalesce, 2110 .get_regs_len = hns3_get_regs_len, 2111 .get_regs = hns3_get_regs, 2112 .get_link = hns3_get_link, 2113 .get_msglevel = hns3_get_msglevel, 2114 .set_msglevel = hns3_set_msglevel, 2115 .get_priv_flags = hns3_get_priv_flags, 2116 .set_priv_flags = hns3_set_priv_flags, 2117 .get_tunable = hns3_get_tunable, 2118 .set_tunable = hns3_set_tunable, 2119 .reset = hns3_set_reset, 2120 }; 2121 2122 static const struct ethtool_ops hns3_ethtool_ops = { 2123 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE, 2124 .supported_ring_params = HNS3_ETHTOOL_RING, 2125 .cap_link_lanes_supported = true, 2126 .self_test = hns3_self_test, 2127 .get_drvinfo = hns3_get_drvinfo, 2128 .get_link = hns3_get_link, 2129 .get_ringparam = hns3_get_ringparam, 2130 .set_ringparam = hns3_set_ringparam, 2131 .get_pauseparam = hns3_get_pauseparam, 2132 .set_pauseparam = hns3_set_pauseparam, 2133 .get_strings = hns3_get_strings, 2134 .get_ethtool_stats = hns3_get_stats, 2135 .get_sset_count = hns3_get_sset_count, 2136 .get_rxnfc = hns3_get_rxnfc, 2137 .set_rxnfc = hns3_set_rxnfc, 2138 .get_rxfh_key_size = hns3_get_rss_key_size, 2139 .get_rxfh_indir_size = hns3_get_rss_indir_size, 2140 .get_rxfh = hns3_get_rss, 2141 .set_rxfh = hns3_set_rss, 2142 .get_link_ksettings = hns3_get_link_ksettings, 2143 .set_link_ksettings = hns3_set_link_ksettings, 2144 .nway_reset = hns3_nway_reset, 2145 .get_channels = hns3_get_channels, 2146 .set_channels = hns3_set_channels, 2147 .get_coalesce = hns3_get_coalesce, 2148 .set_coalesce = hns3_set_coalesce, 2149 .get_regs_len = hns3_get_regs_len, 2150 .get_regs = hns3_get_regs, 2151 .set_phys_id = hns3_set_phys_id, 2152 .get_msglevel = hns3_get_msglevel, 2153 .set_msglevel = hns3_set_msglevel, 2154 .get_fecparam = hns3_get_fecparam, 2155 .set_fecparam = hns3_set_fecparam, 2156 .get_fec_stats = hns3_get_fec_stats, 2157 .get_module_info = hns3_get_module_info, 2158 .get_module_eeprom = hns3_get_module_eeprom, 2159 .get_priv_flags = hns3_get_priv_flags, 2160 .set_priv_flags = hns3_set_priv_flags, 2161 .get_ts_info = hns3_get_ts_info, 2162 .get_tunable = hns3_get_tunable, 2163 .set_tunable = hns3_set_tunable, 2164 .reset = hns3_set_reset, 2165 .get_link_ext_state = hns3_get_link_ext_state, 2166 .get_wol = hns3_get_wol, 2167 .set_wol = hns3_set_wol, 2168 }; 2169 2170 void hns3_ethtool_set_ops(struct net_device *netdev) 2171 { 2172 struct hnae3_handle *h = hns3_get_handle(netdev); 2173 2174 if (h->flags & HNAE3_SUPPORT_VF) 2175 netdev->ethtool_ops = &hns3vf_ethtool_ops; 2176 else 2177 netdev->ethtool_ops = &hns3_ethtool_ops; 2178 } 2179