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