1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
4 #include <linux/etherdevice.h>
5 #include <linux/string.h>
11 char stats_string[ETH_GSTRING_LEN];
15 /* tqp related stats */
16 #define HNS3_TQP_STAT(_string, _member) { \
17 .stats_string = _string, \
18 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
19 offsetof(struct ring_stats, _member), \
22 static const struct hns3_stats hns3_txq_stats[] = {
23 /* Tx per-queue statistics */
24 HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
25 HNS3_TQP_STAT("dropped", sw_err_cnt),
26 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
27 HNS3_TQP_STAT("packets", tx_pkts),
28 HNS3_TQP_STAT("bytes", tx_bytes),
29 HNS3_TQP_STAT("errors", tx_err_cnt),
30 HNS3_TQP_STAT("wake", restart_queue),
31 HNS3_TQP_STAT("busy", tx_busy),
34 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
36 static const struct hns3_stats hns3_rxq_stats[] = {
37 /* Rx per-queue statistics */
38 HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
39 HNS3_TQP_STAT("dropped", sw_err_cnt),
40 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
41 HNS3_TQP_STAT("packets", rx_pkts),
42 HNS3_TQP_STAT("bytes", rx_bytes),
43 HNS3_TQP_STAT("errors", rx_err_cnt),
44 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
45 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
46 HNS3_TQP_STAT("non_vld_descs", non_vld_descs),
47 HNS3_TQP_STAT("err_bd_num", err_bd_num),
48 HNS3_TQP_STAT("l2_err", l2_err),
49 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
50 HNS3_TQP_STAT("multicast", rx_multicast),
53 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
55 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
57 #define HNS3_SELF_TEST_TYPE_NUM 3
58 #define HNS3_NIC_LB_TEST_PKT_NUM 1
59 #define HNS3_NIC_LB_TEST_RING_ID 0
60 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
62 /* Nic loopback test err */
63 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
64 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
65 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
67 struct hns3_link_mode_mapping {
69 u32 ethtool_link_mode;
72 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
74 struct hnae3_handle *h = hns3_get_handle(ndev);
75 bool vlan_filter_enable;
78 if (!h->ae_algo->ops->set_loopback ||
79 !h->ae_algo->ops->set_promisc_mode)
83 case HNAE3_LOOP_SERIAL_SERDES:
84 case HNAE3_LOOP_PARALLEL_SERDES:
86 ret = h->ae_algo->ops->set_loopback(h, loop, en);
97 h->ae_algo->ops->set_promisc_mode(h, true, true);
99 /* recover promisc mode before loopback test */
100 hns3_update_promisc_mode(ndev, h->netdev_flags);
101 vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
102 hns3_enable_vlan_filter(ndev, vlan_filter_enable);
108 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
110 struct hnae3_handle *h = hns3_get_handle(ndev);
113 ret = hns3_nic_reset_all_ring(h);
117 ret = hns3_lp_setup(ndev, loop_mode, true);
118 usleep_range(10000, 20000);
123 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
127 ret = hns3_lp_setup(ndev, loop_mode, false);
129 netdev_err(ndev, "lb_setup return error: %d\n", ret);
133 usleep_range(10000, 20000);
138 static void hns3_lp_setup_skb(struct sk_buff *skb)
140 struct net_device *ndev = skb->dev;
141 unsigned char *packet;
145 skb_reserve(skb, NET_IP_ALIGN);
146 ethh = skb_put(skb, sizeof(struct ethhdr));
147 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
149 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
150 ethh->h_dest[5] += 0x1f;
151 eth_zero_addr(ethh->h_source);
152 ethh->h_proto = htons(ETH_P_ARP);
153 skb_reset_mac_header(skb);
155 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
156 packet[i] = (unsigned char)(i & 0xff);
159 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
162 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
163 unsigned char *packet = skb->data;
166 for (i = 0; i < skb->len; i++)
167 if (packet[i] != (unsigned char)(i & 0xff))
170 /* The packet is correctly received */
172 tqp_vector->rx_group.total_packets++;
174 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
175 skb->data, skb->len, true);
177 dev_kfree_skb_any(skb);
180 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
182 struct hnae3_handle *h = priv->ae_handle;
183 struct hnae3_knic_private_info *kinfo;
184 u32 i, rcv_good_pkt_total = 0;
187 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
188 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
189 struct hns3_enet_ring_group *rx_group;
192 rx_group = &ring->tqp_vector->rx_group;
193 pre_rx_pkt = rx_group->total_packets;
196 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
199 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
200 rx_group->total_packets = pre_rx_pkt;
202 return rcv_good_pkt_total;
205 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
206 u32 end_ringid, u32 budget)
210 for (i = start_ringid; i <= end_ringid; i++) {
211 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
213 hns3_clean_tx_ring(ring);
218 * hns3_lp_run_test - run loopback test
220 * @mode: loopback type
222 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
224 struct hns3_nic_priv *priv = netdev_priv(ndev);
229 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
232 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
235 hns3_lp_setup_skb(skb);
236 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
239 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
243 tx_ret = hns3_nic_net_xmit(skb, ndev);
244 if (tx_ret == NETDEV_TX_OK)
247 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
250 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
251 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
252 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
253 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
257 /* Allow 200 milliseconds for packets to go from Tx to Rx */
260 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
261 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
262 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
263 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
264 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
268 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
269 HNS3_NIC_LB_TEST_RING_ID,
270 HNS3_NIC_LB_TEST_PKT_NUM);
277 * hns3_nic_self_test - self test
279 * @eth_test: test cmd
282 static void hns3_self_test(struct net_device *ndev,
283 struct ethtool_test *eth_test, u64 *data)
285 struct hns3_nic_priv *priv = netdev_priv(ndev);
286 struct hnae3_handle *h = priv->ae_handle;
287 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
288 bool if_running = netif_running(ndev);
289 #if IS_ENABLED(CONFIG_VLAN_8021Q)
290 bool dis_vlan_filter;
295 if (hns3_nic_resetting(ndev)) {
296 netdev_err(ndev, "dev resetting!");
300 /* Only do offline selftest, or pass by default */
301 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
304 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
305 st_param[HNAE3_LOOP_APP][1] =
306 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
308 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
309 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
310 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
312 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
313 HNAE3_LOOP_PARALLEL_SERDES;
314 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
315 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
318 ndev->netdev_ops->ndo_stop(ndev);
320 #if IS_ENABLED(CONFIG_VLAN_8021Q)
321 /* Disable the vlan filter for selftest does not support it */
322 dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
323 h->ae_algo->ops->enable_vlan_filter;
325 h->ae_algo->ops->enable_vlan_filter(h, false);
328 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
330 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
331 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
336 data[test_index] = hns3_lp_up(ndev, loop_type);
337 if (!data[test_index])
338 data[test_index] = hns3_lp_run_test(ndev, loop_type);
340 hns3_lp_down(ndev, loop_type);
342 if (data[test_index])
343 eth_test->flags |= ETH_TEST_FL_FAILED;
348 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
350 #if IS_ENABLED(CONFIG_VLAN_8021Q)
352 h->ae_algo->ops->enable_vlan_filter(h, true);
356 ndev->netdev_ops->ndo_open(ndev);
359 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
361 struct hnae3_handle *h = hns3_get_handle(netdev);
362 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
364 if (!ops->get_sset_count)
369 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
370 ops->get_sset_count(h, stringset));
373 return ops->get_sset_count(h, stringset);
380 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
381 u32 stat_count, u32 num_tqps, const char *prefix)
383 #define MAX_PREFIX_SIZE (6 + 4)
388 for (i = 0; i < num_tqps; i++) {
389 for (j = 0; j < stat_count; j++) {
390 data[ETH_GSTRING_LEN - 1] = '\0';
392 /* first, prepend the prefix string */
393 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s%d_",
395 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
396 size_left = (ETH_GSTRING_LEN - 1) - n1;
398 /* now, concatenate the stats string to it */
399 strncat(data, stats[j].stats_string, size_left);
400 data += ETH_GSTRING_LEN;
407 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
409 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
410 const char tx_prefix[] = "txq";
411 const char rx_prefix[] = "rxq";
413 /* get strings for Tx */
414 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
415 kinfo->num_tqps, tx_prefix);
417 /* get strings for Rx */
418 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
419 kinfo->num_tqps, rx_prefix);
424 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
426 struct hnae3_handle *h = hns3_get_handle(netdev);
427 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
428 char *buff = (char *)data;
430 if (!ops->get_strings)
435 buff = hns3_get_strings_tqps(h, buff);
436 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
439 ops->get_strings(h, stringset, data);
446 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
448 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
449 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
450 struct hns3_enet_ring *ring;
454 /* get stats for Tx */
455 for (i = 0; i < kinfo->num_tqps; i++) {
456 ring = nic_priv->ring_data[i].ring;
457 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
458 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
459 *data++ = *(u64 *)stat;
463 /* get stats for Rx */
464 for (i = 0; i < kinfo->num_tqps; i++) {
465 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
466 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
467 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
468 *data++ = *(u64 *)stat;
475 /* hns3_get_stats - get detail statistics.
476 * @netdev: net device
477 * @stats: statistics info.
478 * @data: statistics data.
480 static void hns3_get_stats(struct net_device *netdev,
481 struct ethtool_stats *stats, u64 *data)
483 struct hnae3_handle *h = hns3_get_handle(netdev);
486 if (hns3_nic_resetting(netdev)) {
487 netdev_err(netdev, "dev resetting, could not get stats\n");
491 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
492 netdev_err(netdev, "could not get any statistics\n");
496 h->ae_algo->ops->update_stats(h, &netdev->stats);
498 /* get per-queue stats */
499 p = hns3_get_stats_tqps(h, p);
501 /* get MAC & other misc hardware stats */
502 h->ae_algo->ops->get_stats(h, p);
505 static void hns3_get_drvinfo(struct net_device *netdev,
506 struct ethtool_drvinfo *drvinfo)
508 struct hns3_nic_priv *priv = netdev_priv(netdev);
509 struct hnae3_handle *h = priv->ae_handle;
511 strncpy(drvinfo->version, hns3_driver_version,
512 sizeof(drvinfo->version));
513 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
515 strncpy(drvinfo->driver, h->pdev->driver->name,
516 sizeof(drvinfo->driver));
517 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
519 strncpy(drvinfo->bus_info, pci_name(h->pdev),
520 sizeof(drvinfo->bus_info));
521 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
523 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
524 priv->ae_handle->ae_algo->ops->get_fw_version(h));
527 static u32 hns3_get_link(struct net_device *netdev)
529 struct hnae3_handle *h = hns3_get_handle(netdev);
531 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
532 return h->ae_algo->ops->get_status(h);
537 static void hns3_get_ringparam(struct net_device *netdev,
538 struct ethtool_ringparam *param)
540 struct hns3_nic_priv *priv = netdev_priv(netdev);
541 struct hnae3_handle *h = priv->ae_handle;
542 int queue_num = h->kinfo.num_tqps;
544 if (hns3_nic_resetting(netdev)) {
545 netdev_err(netdev, "dev resetting!");
549 param->tx_max_pending = HNS3_RING_MAX_PENDING;
550 param->rx_max_pending = HNS3_RING_MAX_PENDING;
552 param->tx_pending = priv->ring_data[0].ring->desc_num;
553 param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
556 static void hns3_get_pauseparam(struct net_device *netdev,
557 struct ethtool_pauseparam *param)
559 struct hnae3_handle *h = hns3_get_handle(netdev);
561 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
562 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
563 ¶m->rx_pause, ¶m->tx_pause);
566 static int hns3_set_pauseparam(struct net_device *netdev,
567 struct ethtool_pauseparam *param)
569 struct hnae3_handle *h = hns3_get_handle(netdev);
571 if (h->ae_algo->ops->set_pauseparam)
572 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
578 static void hns3_get_ksettings(struct hnae3_handle *h,
579 struct ethtool_link_ksettings *cmd)
581 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
583 /* 1.auto_neg & speed & duplex from cmd */
584 if (ops->get_ksettings_an_result)
585 ops->get_ksettings_an_result(h,
591 if (ops->get_link_mode)
592 ops->get_link_mode(h,
593 cmd->link_modes.supported,
594 cmd->link_modes.advertising);
596 /* 3.mdix_ctrl&mdix get from phy reg */
597 if (ops->get_mdix_mode)
598 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
599 &cmd->base.eth_tp_mdix);
602 static int hns3_get_link_ksettings(struct net_device *netdev,
603 struct ethtool_link_ksettings *cmd)
605 struct hnae3_handle *h = hns3_get_handle(netdev);
606 const struct hnae3_ae_ops *ops;
610 if (!h->ae_algo || !h->ae_algo->ops)
613 ops = h->ae_algo->ops;
614 if (ops->get_media_type)
615 ops->get_media_type(h, &media_type);
619 switch (media_type) {
620 case HNAE3_MEDIA_TYPE_NONE:
621 cmd->base.port = PORT_NONE;
622 hns3_get_ksettings(h, cmd);
624 case HNAE3_MEDIA_TYPE_FIBER:
625 cmd->base.port = PORT_FIBRE;
626 hns3_get_ksettings(h, cmd);
628 case HNAE3_MEDIA_TYPE_COPPER:
629 cmd->base.port = PORT_TP;
631 hns3_get_ksettings(h, cmd);
633 phy_ethtool_ksettings_get(netdev->phydev, cmd);
637 netdev_warn(netdev, "Unknown media type");
642 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
644 link_stat = hns3_get_link(netdev);
646 cmd->base.speed = SPEED_UNKNOWN;
647 cmd->base.duplex = DUPLEX_UNKNOWN;
653 static int hns3_set_link_ksettings(struct net_device *netdev,
654 const struct ethtool_link_ksettings *cmd)
656 /* Chip doesn't support this mode. */
657 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
660 /* Only support ksettings_set for netdev with phy attached for now */
662 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
667 static u32 hns3_get_rss_key_size(struct net_device *netdev)
669 struct hnae3_handle *h = hns3_get_handle(netdev);
671 if (!h->ae_algo || !h->ae_algo->ops ||
672 !h->ae_algo->ops->get_rss_key_size)
675 return h->ae_algo->ops->get_rss_key_size(h);
678 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
680 struct hnae3_handle *h = hns3_get_handle(netdev);
682 if (!h->ae_algo || !h->ae_algo->ops ||
683 !h->ae_algo->ops->get_rss_indir_size)
686 return h->ae_algo->ops->get_rss_indir_size(h);
689 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
692 struct hnae3_handle *h = hns3_get_handle(netdev);
694 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
697 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
700 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
701 const u8 *key, const u8 hfunc)
703 struct hnae3_handle *h = hns3_get_handle(netdev);
705 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
708 if ((h->pdev->revision == 0x20 &&
709 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
710 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
711 netdev_err(netdev, "hash func not supported\n");
717 "set rss failed for indir is empty\n");
721 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
724 static int hns3_get_rxnfc(struct net_device *netdev,
725 struct ethtool_rxnfc *cmd,
728 struct hnae3_handle *h = hns3_get_handle(netdev);
730 if (!h->ae_algo || !h->ae_algo->ops)
734 case ETHTOOL_GRXRINGS:
735 cmd->data = h->kinfo.num_tqps;
738 if (h->ae_algo->ops->get_rss_tuple)
739 return h->ae_algo->ops->get_rss_tuple(h, cmd);
741 case ETHTOOL_GRXCLSRLCNT:
742 if (h->ae_algo->ops->get_fd_rule_cnt)
743 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
745 case ETHTOOL_GRXCLSRULE:
746 if (h->ae_algo->ops->get_fd_rule_info)
747 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
749 case ETHTOOL_GRXCLSRLALL:
750 if (h->ae_algo->ops->get_fd_all_rules)
751 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
759 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
760 u32 tx_desc_num, u32 rx_desc_num)
762 struct hnae3_handle *h = priv->ae_handle;
765 h->kinfo.num_tx_desc = tx_desc_num;
766 h->kinfo.num_rx_desc = rx_desc_num;
768 for (i = 0; i < h->kinfo.num_tqps; i++) {
769 priv->ring_data[i].ring->desc_num = tx_desc_num;
770 priv->ring_data[i + h->kinfo.num_tqps].ring->desc_num =
774 return hns3_init_all_ring(priv);
777 static int hns3_set_ringparam(struct net_device *ndev,
778 struct ethtool_ringparam *param)
780 struct hns3_nic_priv *priv = netdev_priv(ndev);
781 struct hnae3_handle *h = priv->ae_handle;
782 bool if_running = netif_running(ndev);
783 u32 old_tx_desc_num, new_tx_desc_num;
784 u32 old_rx_desc_num, new_rx_desc_num;
785 int queue_num = h->kinfo.num_tqps;
788 if (hns3_nic_resetting(ndev))
791 if (param->rx_mini_pending || param->rx_jumbo_pending)
794 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
795 param->tx_pending < HNS3_RING_MIN_PENDING ||
796 param->rx_pending > HNS3_RING_MAX_PENDING ||
797 param->rx_pending < HNS3_RING_MIN_PENDING) {
798 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
799 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
803 /* Hardware requires that its descriptors must be multiple of eight */
804 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
805 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
806 old_tx_desc_num = priv->ring_data[0].ring->desc_num;
807 old_rx_desc_num = priv->ring_data[queue_num].ring->desc_num;
808 if (old_tx_desc_num == new_tx_desc_num &&
809 old_rx_desc_num == new_rx_desc_num)
813 "Changing Tx/Rx ring depth from %d/%d to %d/%d\n",
814 old_tx_desc_num, old_rx_desc_num,
815 new_tx_desc_num, new_rx_desc_num);
818 ndev->netdev_ops->ndo_stop(ndev);
820 ret = hns3_uninit_all_ring(priv);
824 ret = hns3_change_all_ring_bd_num(priv, new_tx_desc_num,
827 ret = hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
831 "Revert to old bd num fail, ret=%d.\n", ret);
837 ret = ndev->netdev_ops->ndo_open(ndev);
842 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
844 struct hnae3_handle *h = hns3_get_handle(netdev);
846 if (!h->ae_algo || !h->ae_algo->ops)
851 if (h->ae_algo->ops->set_rss_tuple)
852 return h->ae_algo->ops->set_rss_tuple(h, cmd);
854 case ETHTOOL_SRXCLSRLINS:
855 if (h->ae_algo->ops->add_fd_entry)
856 return h->ae_algo->ops->add_fd_entry(h, cmd);
858 case ETHTOOL_SRXCLSRLDEL:
859 if (h->ae_algo->ops->del_fd_entry)
860 return h->ae_algo->ops->del_fd_entry(h, cmd);
867 static int hns3_nway_reset(struct net_device *netdev)
869 struct phy_device *phy = netdev->phydev;
871 if (!netif_running(netdev))
874 /* Only support nway_reset for netdev with phy attached for now */
878 if (phy->autoneg != AUTONEG_ENABLE)
881 return genphy_restart_aneg(phy);
884 static void hns3_get_channels(struct net_device *netdev,
885 struct ethtool_channels *ch)
887 struct hnae3_handle *h = hns3_get_handle(netdev);
889 if (h->ae_algo->ops->get_channels)
890 h->ae_algo->ops->get_channels(h, ch);
893 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
894 struct ethtool_coalesce *cmd)
896 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
897 struct hns3_nic_priv *priv = netdev_priv(netdev);
898 struct hnae3_handle *h = priv->ae_handle;
899 u16 queue_num = h->kinfo.num_tqps;
901 if (hns3_nic_resetting(netdev))
904 if (queue >= queue_num) {
906 "Invalid queue value %d! Queue max id=%d\n",
907 queue, queue_num - 1);
911 tx_vector = priv->ring_data[queue].ring->tqp_vector;
912 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
914 cmd->use_adaptive_tx_coalesce =
915 tx_vector->tx_group.coal.gl_adapt_enable;
916 cmd->use_adaptive_rx_coalesce =
917 rx_vector->rx_group.coal.gl_adapt_enable;
919 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
920 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
922 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
923 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
928 static int hns3_get_coalesce(struct net_device *netdev,
929 struct ethtool_coalesce *cmd)
931 return hns3_get_coalesce_per_queue(netdev, 0, cmd);
934 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
935 struct ethtool_coalesce *cmd)
939 if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
941 "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
946 if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
948 "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
953 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
954 if (rx_gl != cmd->rx_coalesce_usecs) {
956 "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
957 cmd->rx_coalesce_usecs, rx_gl);
960 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
961 if (tx_gl != cmd->tx_coalesce_usecs) {
963 "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
964 cmd->tx_coalesce_usecs, tx_gl);
970 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
971 struct ethtool_coalesce *cmd)
975 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
977 "tx_usecs_high must be same as rx_usecs_high.\n");
981 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
983 "Invalid usecs_high value, usecs_high range is 0-%d\n",
988 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
989 if (rl != cmd->rx_coalesce_usecs_high) {
991 "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
992 cmd->rx_coalesce_usecs_high, rl);
998 static int hns3_check_coalesce_para(struct net_device *netdev,
999 struct ethtool_coalesce *cmd)
1003 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1006 "Check gl coalesce param fail. ret = %d\n", ret);
1010 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1013 "Check rl coalesce param fail. ret = %d\n", ret);
1017 if (cmd->use_adaptive_tx_coalesce == 1 ||
1018 cmd->use_adaptive_rx_coalesce == 1) {
1020 "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
1021 cmd->use_adaptive_tx_coalesce,
1022 cmd->use_adaptive_rx_coalesce);
1028 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1029 struct ethtool_coalesce *cmd,
1032 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1033 struct hns3_nic_priv *priv = netdev_priv(netdev);
1034 struct hnae3_handle *h = priv->ae_handle;
1035 int queue_num = h->kinfo.num_tqps;
1037 tx_vector = priv->ring_data[queue].ring->tqp_vector;
1038 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
1040 tx_vector->tx_group.coal.gl_adapt_enable =
1041 cmd->use_adaptive_tx_coalesce;
1042 rx_vector->rx_group.coal.gl_adapt_enable =
1043 cmd->use_adaptive_rx_coalesce;
1045 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1046 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1048 hns3_set_vector_coalesce_tx_gl(tx_vector,
1049 tx_vector->tx_group.coal.int_gl);
1050 hns3_set_vector_coalesce_rx_gl(rx_vector,
1051 rx_vector->rx_group.coal.int_gl);
1053 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1054 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1057 static int hns3_set_coalesce(struct net_device *netdev,
1058 struct ethtool_coalesce *cmd)
1060 struct hnae3_handle *h = hns3_get_handle(netdev);
1061 u16 queue_num = h->kinfo.num_tqps;
1065 if (hns3_nic_resetting(netdev))
1068 ret = hns3_check_coalesce_para(netdev, cmd);
1072 h->kinfo.int_rl_setting =
1073 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1075 for (i = 0; i < queue_num; i++)
1076 hns3_set_coalesce_per_queue(netdev, cmd, i);
1081 static int hns3_get_regs_len(struct net_device *netdev)
1083 struct hnae3_handle *h = hns3_get_handle(netdev);
1085 if (!h->ae_algo->ops->get_regs_len)
1088 return h->ae_algo->ops->get_regs_len(h);
1091 static void hns3_get_regs(struct net_device *netdev,
1092 struct ethtool_regs *cmd, void *data)
1094 struct hnae3_handle *h = hns3_get_handle(netdev);
1096 if (!h->ae_algo->ops->get_regs)
1099 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1102 static int hns3_set_phys_id(struct net_device *netdev,
1103 enum ethtool_phys_id_state state)
1105 struct hnae3_handle *h = hns3_get_handle(netdev);
1107 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id)
1110 return h->ae_algo->ops->set_led_id(h, state);
1113 static u32 hns3_get_msglevel(struct net_device *netdev)
1115 struct hnae3_handle *h = hns3_get_handle(netdev);
1117 return h->msg_enable;
1120 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1122 struct hnae3_handle *h = hns3_get_handle(netdev);
1124 h->msg_enable = msg_level;
1127 static const struct ethtool_ops hns3vf_ethtool_ops = {
1128 .get_drvinfo = hns3_get_drvinfo,
1129 .get_ringparam = hns3_get_ringparam,
1130 .set_ringparam = hns3_set_ringparam,
1131 .get_strings = hns3_get_strings,
1132 .get_ethtool_stats = hns3_get_stats,
1133 .get_sset_count = hns3_get_sset_count,
1134 .get_rxnfc = hns3_get_rxnfc,
1135 .set_rxnfc = hns3_set_rxnfc,
1136 .get_rxfh_key_size = hns3_get_rss_key_size,
1137 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1138 .get_rxfh = hns3_get_rss,
1139 .set_rxfh = hns3_set_rss,
1140 .get_link_ksettings = hns3_get_link_ksettings,
1141 .get_channels = hns3_get_channels,
1142 .get_coalesce = hns3_get_coalesce,
1143 .set_coalesce = hns3_set_coalesce,
1144 .get_regs_len = hns3_get_regs_len,
1145 .get_regs = hns3_get_regs,
1146 .get_link = hns3_get_link,
1147 .get_msglevel = hns3_get_msglevel,
1148 .set_msglevel = hns3_set_msglevel,
1151 static const struct ethtool_ops hns3_ethtool_ops = {
1152 .self_test = hns3_self_test,
1153 .get_drvinfo = hns3_get_drvinfo,
1154 .get_link = hns3_get_link,
1155 .get_ringparam = hns3_get_ringparam,
1156 .set_ringparam = hns3_set_ringparam,
1157 .get_pauseparam = hns3_get_pauseparam,
1158 .set_pauseparam = hns3_set_pauseparam,
1159 .get_strings = hns3_get_strings,
1160 .get_ethtool_stats = hns3_get_stats,
1161 .get_sset_count = hns3_get_sset_count,
1162 .get_rxnfc = hns3_get_rxnfc,
1163 .set_rxnfc = hns3_set_rxnfc,
1164 .get_rxfh_key_size = hns3_get_rss_key_size,
1165 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1166 .get_rxfh = hns3_get_rss,
1167 .set_rxfh = hns3_set_rss,
1168 .get_link_ksettings = hns3_get_link_ksettings,
1169 .set_link_ksettings = hns3_set_link_ksettings,
1170 .nway_reset = hns3_nway_reset,
1171 .get_channels = hns3_get_channels,
1172 .set_channels = hns3_set_channels,
1173 .get_coalesce = hns3_get_coalesce,
1174 .set_coalesce = hns3_set_coalesce,
1175 .get_regs_len = hns3_get_regs_len,
1176 .get_regs = hns3_get_regs,
1177 .set_phys_id = hns3_set_phys_id,
1178 .get_msglevel = hns3_get_msglevel,
1179 .set_msglevel = hns3_set_msglevel,
1182 void hns3_ethtool_set_ops(struct net_device *netdev)
1184 struct hnae3_handle *h = hns3_get_handle(netdev);
1186 if (h->flags & HNAE3_SUPPORT_VF)
1187 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1189 netdev->ethtool_ops = &hns3_ethtool_ops;