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