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