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),
52 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
54 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
56 #define HNS3_SELF_TEST_TYPE_NUM 3
57 #define HNS3_NIC_LB_TEST_PKT_NUM 1
58 #define HNS3_NIC_LB_TEST_RING_ID 0
59 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
61 /* Nic loopback test err */
62 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
63 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
64 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
66 struct hns3_link_mode_mapping {
68 u32 ethtool_link_mode;
71 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
73 struct hnae3_handle *h = hns3_get_handle(ndev);
74 bool vlan_filter_enable;
77 if (!h->ae_algo->ops->set_loopback ||
78 !h->ae_algo->ops->set_promisc_mode)
82 case HNAE3_LOOP_SERIAL_SERDES:
83 case HNAE3_LOOP_PARALLEL_SERDES:
85 ret = h->ae_algo->ops->set_loopback(h, loop, en);
96 h->ae_algo->ops->set_promisc_mode(h, true, true);
98 /* recover promisc mode before loopback test */
99 hns3_update_promisc_mode(ndev, h->netdev_flags);
100 vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
101 hns3_enable_vlan_filter(ndev, vlan_filter_enable);
107 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
109 struct hnae3_handle *h = hns3_get_handle(ndev);
112 ret = hns3_nic_reset_all_ring(h);
116 ret = hns3_lp_setup(ndev, loop_mode, true);
117 usleep_range(10000, 20000);
122 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
126 ret = hns3_lp_setup(ndev, loop_mode, false);
128 netdev_err(ndev, "lb_setup return error: %d\n", ret);
132 usleep_range(10000, 20000);
137 static void hns3_lp_setup_skb(struct sk_buff *skb)
139 struct net_device *ndev = skb->dev;
140 unsigned char *packet;
144 skb_reserve(skb, NET_IP_ALIGN);
145 ethh = skb_put(skb, sizeof(struct ethhdr));
146 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
148 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
149 ethh->h_dest[5] += 0x1f;
150 eth_zero_addr(ethh->h_source);
151 ethh->h_proto = htons(ETH_P_ARP);
152 skb_reset_mac_header(skb);
154 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
155 packet[i] = (unsigned char)(i & 0xff);
158 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
161 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
162 unsigned char *packet = skb->data;
165 for (i = 0; i < skb->len; i++)
166 if (packet[i] != (unsigned char)(i & 0xff))
169 /* The packet is correctly received */
171 tqp_vector->rx_group.total_packets++;
173 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
174 skb->data, skb->len, true);
176 dev_kfree_skb_any(skb);
179 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
181 struct hnae3_handle *h = priv->ae_handle;
182 struct hnae3_knic_private_info *kinfo;
183 u32 i, rcv_good_pkt_total = 0;
186 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
187 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
188 struct hns3_enet_ring_group *rx_group;
191 rx_group = &ring->tqp_vector->rx_group;
192 pre_rx_pkt = rx_group->total_packets;
195 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
198 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
199 rx_group->total_packets = pre_rx_pkt;
201 return rcv_good_pkt_total;
204 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
205 u32 end_ringid, u32 budget)
209 for (i = start_ringid; i <= end_ringid; i++) {
210 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
212 hns3_clean_tx_ring(ring);
217 * hns3_lp_run_test - run loopback test
219 * @mode: loopback type
221 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
223 struct hns3_nic_priv *priv = netdev_priv(ndev);
228 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
231 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
234 hns3_lp_setup_skb(skb);
235 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
238 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
242 tx_ret = hns3_nic_net_xmit(skb, ndev);
243 if (tx_ret == NETDEV_TX_OK)
246 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
249 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
250 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
251 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
252 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
256 /* Allow 200 milliseconds for packets to go from Tx to Rx */
259 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
260 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
261 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
262 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
263 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
267 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
268 HNS3_NIC_LB_TEST_RING_ID,
269 HNS3_NIC_LB_TEST_PKT_NUM);
276 * hns3_nic_self_test - self test
278 * @eth_test: test cmd
281 static void hns3_self_test(struct net_device *ndev,
282 struct ethtool_test *eth_test, u64 *data)
284 struct hns3_nic_priv *priv = netdev_priv(ndev);
285 struct hnae3_handle *h = priv->ae_handle;
286 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
287 bool if_running = netif_running(ndev);
288 #if IS_ENABLED(CONFIG_VLAN_8021Q)
289 bool dis_vlan_filter;
294 if (hns3_nic_resetting(ndev)) {
295 netdev_err(ndev, "dev resetting!");
299 /* Only do offline selftest, or pass by default */
300 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
303 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
304 st_param[HNAE3_LOOP_APP][1] =
305 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
307 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
308 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
309 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
311 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
312 HNAE3_LOOP_PARALLEL_SERDES;
313 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
314 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
317 ndev->netdev_ops->ndo_stop(ndev);
319 #if IS_ENABLED(CONFIG_VLAN_8021Q)
320 /* Disable the vlan filter for selftest does not support it */
321 dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
322 h->ae_algo->ops->enable_vlan_filter;
324 h->ae_algo->ops->enable_vlan_filter(h, false);
327 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
329 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
330 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
335 data[test_index] = hns3_lp_up(ndev, loop_type);
336 if (!data[test_index]) {
337 data[test_index] = hns3_lp_run_test(ndev, loop_type);
338 hns3_lp_down(ndev, loop_type);
341 if (data[test_index])
342 eth_test->flags |= ETH_TEST_FL_FAILED;
347 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
349 #if IS_ENABLED(CONFIG_VLAN_8021Q)
351 h->ae_algo->ops->enable_vlan_filter(h, true);
355 ndev->netdev_ops->ndo_open(ndev);
358 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
360 struct hnae3_handle *h = hns3_get_handle(netdev);
361 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
363 if (!ops->get_sset_count)
368 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
369 ops->get_sset_count(h, stringset));
372 return ops->get_sset_count(h, stringset);
379 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
380 u32 stat_count, u32 num_tqps, const char *prefix)
382 #define MAX_PREFIX_SIZE (6 + 4)
387 for (i = 0; i < num_tqps; i++) {
388 for (j = 0; j < stat_count; j++) {
389 data[ETH_GSTRING_LEN - 1] = '\0';
391 /* first, prepend the prefix string */
392 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s%d_",
394 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
395 size_left = (ETH_GSTRING_LEN - 1) - n1;
397 /* now, concatenate the stats string to it */
398 strncat(data, stats[j].stats_string, size_left);
399 data += ETH_GSTRING_LEN;
406 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
408 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
409 const char tx_prefix[] = "txq";
410 const char rx_prefix[] = "rxq";
412 /* get strings for Tx */
413 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
414 kinfo->num_tqps, tx_prefix);
416 /* get strings for Rx */
417 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
418 kinfo->num_tqps, rx_prefix);
423 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
425 struct hnae3_handle *h = hns3_get_handle(netdev);
426 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
427 char *buff = (char *)data;
429 if (!ops->get_strings)
434 buff = hns3_get_strings_tqps(h, buff);
435 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
438 ops->get_strings(h, stringset, data);
445 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
447 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
448 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
449 struct hns3_enet_ring *ring;
453 /* get stats for Tx */
454 for (i = 0; i < kinfo->num_tqps; i++) {
455 ring = nic_priv->ring_data[i].ring;
456 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
457 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
458 *data++ = *(u64 *)stat;
462 /* get stats for Rx */
463 for (i = 0; i < kinfo->num_tqps; i++) {
464 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
465 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
466 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
467 *data++ = *(u64 *)stat;
474 /* hns3_get_stats - get detail statistics.
475 * @netdev: net device
476 * @stats: statistics info.
477 * @data: statistics data.
479 static void hns3_get_stats(struct net_device *netdev,
480 struct ethtool_stats *stats, u64 *data)
482 struct hnae3_handle *h = hns3_get_handle(netdev);
485 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
486 netdev_err(netdev, "could not get any statistics\n");
490 h->ae_algo->ops->update_stats(h, &netdev->stats);
492 /* get per-queue stats */
493 p = hns3_get_stats_tqps(h, p);
495 /* get MAC & other misc hardware stats */
496 h->ae_algo->ops->get_stats(h, p);
499 static void hns3_get_drvinfo(struct net_device *netdev,
500 struct ethtool_drvinfo *drvinfo)
502 struct hns3_nic_priv *priv = netdev_priv(netdev);
503 struct hnae3_handle *h = priv->ae_handle;
505 strncpy(drvinfo->version, hns3_driver_version,
506 sizeof(drvinfo->version));
507 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
509 strncpy(drvinfo->driver, h->pdev->driver->name,
510 sizeof(drvinfo->driver));
511 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
513 strncpy(drvinfo->bus_info, pci_name(h->pdev),
514 sizeof(drvinfo->bus_info));
515 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
517 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
518 priv->ae_handle->ae_algo->ops->get_fw_version(h));
521 static u32 hns3_get_link(struct net_device *netdev)
523 struct hnae3_handle *h = hns3_get_handle(netdev);
525 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
526 return h->ae_algo->ops->get_status(h);
531 static void hns3_get_ringparam(struct net_device *netdev,
532 struct ethtool_ringparam *param)
534 struct hns3_nic_priv *priv = netdev_priv(netdev);
535 struct hnae3_handle *h = priv->ae_handle;
536 int queue_num = h->kinfo.num_tqps;
538 if (hns3_nic_resetting(netdev)) {
539 netdev_err(netdev, "dev resetting!");
543 param->tx_max_pending = HNS3_RING_MAX_PENDING;
544 param->rx_max_pending = HNS3_RING_MAX_PENDING;
546 param->tx_pending = priv->ring_data[0].ring->desc_num;
547 param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
550 static void hns3_get_pauseparam(struct net_device *netdev,
551 struct ethtool_pauseparam *param)
553 struct hnae3_handle *h = hns3_get_handle(netdev);
555 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
556 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
557 ¶m->rx_pause, ¶m->tx_pause);
560 static int hns3_set_pauseparam(struct net_device *netdev,
561 struct ethtool_pauseparam *param)
563 struct hnae3_handle *h = hns3_get_handle(netdev);
565 if (h->ae_algo->ops->set_pauseparam)
566 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
572 static void hns3_get_ksettings(struct hnae3_handle *h,
573 struct ethtool_link_ksettings *cmd)
575 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
577 /* 1.auto_neg & speed & duplex from cmd */
578 if (ops->get_ksettings_an_result)
579 ops->get_ksettings_an_result(h,
585 if (ops->get_link_mode)
586 ops->get_link_mode(h,
587 cmd->link_modes.supported,
588 cmd->link_modes.advertising);
590 /* 3.mdix_ctrl&mdix get from phy reg */
591 if (ops->get_mdix_mode)
592 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
593 &cmd->base.eth_tp_mdix);
596 static int hns3_get_link_ksettings(struct net_device *netdev,
597 struct ethtool_link_ksettings *cmd)
599 struct hnae3_handle *h = hns3_get_handle(netdev);
600 const struct hnae3_ae_ops *ops;
604 if (!h->ae_algo || !h->ae_algo->ops)
607 ops = h->ae_algo->ops;
608 if (ops->get_media_type)
609 ops->get_media_type(h, &media_type);
613 switch (media_type) {
614 case HNAE3_MEDIA_TYPE_NONE:
615 cmd->base.port = PORT_NONE;
616 hns3_get_ksettings(h, cmd);
618 case HNAE3_MEDIA_TYPE_FIBER:
619 cmd->base.port = PORT_FIBRE;
620 hns3_get_ksettings(h, cmd);
622 case HNAE3_MEDIA_TYPE_COPPER:
626 cmd->base.port = PORT_TP;
627 phy_ethtool_ksettings_get(netdev->phydev, cmd);
632 netdev_warn(netdev, "Unknown media type");
637 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
639 link_stat = hns3_get_link(netdev);
641 cmd->base.speed = SPEED_UNKNOWN;
642 cmd->base.duplex = DUPLEX_UNKNOWN;
648 static int hns3_set_link_ksettings(struct net_device *netdev,
649 const struct ethtool_link_ksettings *cmd)
651 /* Only support ksettings_set for netdev with phy attached for now */
653 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
658 static u32 hns3_get_rss_key_size(struct net_device *netdev)
660 struct hnae3_handle *h = hns3_get_handle(netdev);
662 if (!h->ae_algo || !h->ae_algo->ops ||
663 !h->ae_algo->ops->get_rss_key_size)
666 return h->ae_algo->ops->get_rss_key_size(h);
669 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
671 struct hnae3_handle *h = hns3_get_handle(netdev);
673 if (!h->ae_algo || !h->ae_algo->ops ||
674 !h->ae_algo->ops->get_rss_indir_size)
677 return h->ae_algo->ops->get_rss_indir_size(h);
680 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
683 struct hnae3_handle *h = hns3_get_handle(netdev);
685 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
688 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
691 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
692 const u8 *key, const u8 hfunc)
694 struct hnae3_handle *h = hns3_get_handle(netdev);
696 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
699 if ((h->pdev->revision == 0x20 &&
700 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
701 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
702 netdev_err(netdev, "hash func not supported\n");
708 "set rss failed for indir is empty\n");
712 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
715 static int hns3_get_rxnfc(struct net_device *netdev,
716 struct ethtool_rxnfc *cmd,
719 struct hnae3_handle *h = hns3_get_handle(netdev);
721 if (!h->ae_algo || !h->ae_algo->ops)
725 case ETHTOOL_GRXRINGS:
726 cmd->data = h->kinfo.num_tqps;
729 if (h->ae_algo->ops->get_rss_tuple)
730 return h->ae_algo->ops->get_rss_tuple(h, cmd);
732 case ETHTOOL_GRXCLSRLCNT:
733 if (h->ae_algo->ops->get_fd_rule_cnt)
734 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
736 case ETHTOOL_GRXCLSRULE:
737 if (h->ae_algo->ops->get_fd_rule_info)
738 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
740 case ETHTOOL_GRXCLSRLALL:
741 if (h->ae_algo->ops->get_fd_all_rules)
742 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
750 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
753 struct hnae3_handle *h = priv->ae_handle;
756 h->kinfo.num_desc = new_desc_num;
758 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
759 priv->ring_data[i].ring->desc_num = new_desc_num;
761 return hns3_init_all_ring(priv);
764 static int hns3_set_ringparam(struct net_device *ndev,
765 struct ethtool_ringparam *param)
767 struct hns3_nic_priv *priv = netdev_priv(ndev);
768 struct hnae3_handle *h = priv->ae_handle;
769 bool if_running = netif_running(ndev);
770 u32 old_desc_num, new_desc_num;
773 if (hns3_nic_resetting(ndev))
776 if (param->rx_mini_pending || param->rx_jumbo_pending)
779 if (param->tx_pending != param->rx_pending) {
781 "Descriptors of tx and rx must be equal");
785 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
786 param->tx_pending < HNS3_RING_MIN_PENDING) {
788 "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
789 param->tx_pending, HNS3_RING_MIN_PENDING,
790 HNS3_RING_MAX_PENDING);
794 new_desc_num = param->tx_pending;
796 /* Hardware requires that its descriptors must be multiple of eight */
797 new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
798 old_desc_num = h->kinfo.num_desc;
799 if (old_desc_num == new_desc_num)
803 "Changing descriptor count from %d to %d.\n",
804 old_desc_num, new_desc_num);
809 ret = hns3_uninit_all_ring(priv);
813 ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
815 ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
818 "Revert to old bd num fail, ret=%d.\n", ret);
824 ret = dev_open(ndev, NULL);
829 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
831 struct hnae3_handle *h = hns3_get_handle(netdev);
833 if (!h->ae_algo || !h->ae_algo->ops)
838 if (h->ae_algo->ops->set_rss_tuple)
839 return h->ae_algo->ops->set_rss_tuple(h, cmd);
841 case ETHTOOL_SRXCLSRLINS:
842 if (h->ae_algo->ops->add_fd_entry)
843 return h->ae_algo->ops->add_fd_entry(h, cmd);
845 case ETHTOOL_SRXCLSRLDEL:
846 if (h->ae_algo->ops->del_fd_entry)
847 return h->ae_algo->ops->del_fd_entry(h, cmd);
854 static int hns3_nway_reset(struct net_device *netdev)
856 struct phy_device *phy = netdev->phydev;
858 if (!netif_running(netdev))
861 /* Only support nway_reset for netdev with phy attached for now */
865 if (phy->autoneg != AUTONEG_ENABLE)
868 return genphy_restart_aneg(phy);
871 static void hns3_get_channels(struct net_device *netdev,
872 struct ethtool_channels *ch)
874 struct hnae3_handle *h = hns3_get_handle(netdev);
876 if (h->ae_algo->ops->get_channels)
877 h->ae_algo->ops->get_channels(h, ch);
880 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
881 struct ethtool_coalesce *cmd)
883 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
884 struct hns3_nic_priv *priv = netdev_priv(netdev);
885 struct hnae3_handle *h = priv->ae_handle;
886 u16 queue_num = h->kinfo.num_tqps;
888 if (hns3_nic_resetting(netdev))
891 if (queue >= queue_num) {
893 "Invalid queue value %d! Queue max id=%d\n",
894 queue, queue_num - 1);
898 tx_vector = priv->ring_data[queue].ring->tqp_vector;
899 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
901 cmd->use_adaptive_tx_coalesce =
902 tx_vector->tx_group.coal.gl_adapt_enable;
903 cmd->use_adaptive_rx_coalesce =
904 rx_vector->rx_group.coal.gl_adapt_enable;
906 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
907 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
909 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
910 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
915 static int hns3_get_coalesce(struct net_device *netdev,
916 struct ethtool_coalesce *cmd)
918 return hns3_get_coalesce_per_queue(netdev, 0, cmd);
921 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
922 struct ethtool_coalesce *cmd)
926 if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
928 "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
933 if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
935 "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
940 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
941 if (rx_gl != cmd->rx_coalesce_usecs) {
943 "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
944 cmd->rx_coalesce_usecs, rx_gl);
947 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
948 if (tx_gl != cmd->tx_coalesce_usecs) {
950 "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
951 cmd->tx_coalesce_usecs, tx_gl);
957 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
958 struct ethtool_coalesce *cmd)
962 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
964 "tx_usecs_high must be same as rx_usecs_high.\n");
968 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
970 "Invalid usecs_high value, usecs_high range is 0-%d\n",
975 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
976 if (rl != cmd->rx_coalesce_usecs_high) {
978 "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
979 cmd->rx_coalesce_usecs_high, rl);
985 static int hns3_check_coalesce_para(struct net_device *netdev,
986 struct ethtool_coalesce *cmd)
990 ret = hns3_check_gl_coalesce_para(netdev, cmd);
993 "Check gl coalesce param fail. ret = %d\n", ret);
997 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1000 "Check rl coalesce param fail. ret = %d\n", ret);
1004 if (cmd->use_adaptive_tx_coalesce == 1 ||
1005 cmd->use_adaptive_rx_coalesce == 1) {
1007 "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
1008 cmd->use_adaptive_tx_coalesce,
1009 cmd->use_adaptive_rx_coalesce);
1015 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1016 struct ethtool_coalesce *cmd,
1019 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1020 struct hns3_nic_priv *priv = netdev_priv(netdev);
1021 struct hnae3_handle *h = priv->ae_handle;
1022 int queue_num = h->kinfo.num_tqps;
1024 tx_vector = priv->ring_data[queue].ring->tqp_vector;
1025 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
1027 tx_vector->tx_group.coal.gl_adapt_enable =
1028 cmd->use_adaptive_tx_coalesce;
1029 rx_vector->rx_group.coal.gl_adapt_enable =
1030 cmd->use_adaptive_rx_coalesce;
1032 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1033 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1035 hns3_set_vector_coalesce_tx_gl(tx_vector,
1036 tx_vector->tx_group.coal.int_gl);
1037 hns3_set_vector_coalesce_rx_gl(rx_vector,
1038 rx_vector->rx_group.coal.int_gl);
1040 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1041 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1044 static int hns3_set_coalesce(struct net_device *netdev,
1045 struct ethtool_coalesce *cmd)
1047 struct hnae3_handle *h = hns3_get_handle(netdev);
1048 u16 queue_num = h->kinfo.num_tqps;
1052 if (hns3_nic_resetting(netdev))
1055 ret = hns3_check_coalesce_para(netdev, cmd);
1059 h->kinfo.int_rl_setting =
1060 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1062 for (i = 0; i < queue_num; i++)
1063 hns3_set_coalesce_per_queue(netdev, cmd, i);
1068 static int hns3_get_regs_len(struct net_device *netdev)
1070 struct hnae3_handle *h = hns3_get_handle(netdev);
1072 if (!h->ae_algo->ops->get_regs_len)
1075 return h->ae_algo->ops->get_regs_len(h);
1078 static void hns3_get_regs(struct net_device *netdev,
1079 struct ethtool_regs *cmd, void *data)
1081 struct hnae3_handle *h = hns3_get_handle(netdev);
1083 if (!h->ae_algo->ops->get_regs)
1086 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1089 static int hns3_set_phys_id(struct net_device *netdev,
1090 enum ethtool_phys_id_state state)
1092 struct hnae3_handle *h = hns3_get_handle(netdev);
1094 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id)
1097 return h->ae_algo->ops->set_led_id(h, state);
1100 static const struct ethtool_ops hns3vf_ethtool_ops = {
1101 .get_drvinfo = hns3_get_drvinfo,
1102 .get_ringparam = hns3_get_ringparam,
1103 .set_ringparam = hns3_set_ringparam,
1104 .get_strings = hns3_get_strings,
1105 .get_ethtool_stats = hns3_get_stats,
1106 .get_sset_count = hns3_get_sset_count,
1107 .get_rxnfc = hns3_get_rxnfc,
1108 .set_rxnfc = hns3_set_rxnfc,
1109 .get_rxfh_key_size = hns3_get_rss_key_size,
1110 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1111 .get_rxfh = hns3_get_rss,
1112 .set_rxfh = hns3_set_rss,
1113 .get_link_ksettings = hns3_get_link_ksettings,
1114 .get_channels = hns3_get_channels,
1115 .get_coalesce = hns3_get_coalesce,
1116 .set_coalesce = hns3_set_coalesce,
1117 .get_link = hns3_get_link,
1120 static const struct ethtool_ops hns3_ethtool_ops = {
1121 .self_test = hns3_self_test,
1122 .get_drvinfo = hns3_get_drvinfo,
1123 .get_link = hns3_get_link,
1124 .get_ringparam = hns3_get_ringparam,
1125 .set_ringparam = hns3_set_ringparam,
1126 .get_pauseparam = hns3_get_pauseparam,
1127 .set_pauseparam = hns3_set_pauseparam,
1128 .get_strings = hns3_get_strings,
1129 .get_ethtool_stats = hns3_get_stats,
1130 .get_sset_count = hns3_get_sset_count,
1131 .get_rxnfc = hns3_get_rxnfc,
1132 .set_rxnfc = hns3_set_rxnfc,
1133 .get_rxfh_key_size = hns3_get_rss_key_size,
1134 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1135 .get_rxfh = hns3_get_rss,
1136 .set_rxfh = hns3_set_rss,
1137 .get_link_ksettings = hns3_get_link_ksettings,
1138 .set_link_ksettings = hns3_set_link_ksettings,
1139 .nway_reset = hns3_nway_reset,
1140 .get_channels = hns3_get_channels,
1141 .set_channels = hns3_set_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 .set_phys_id = hns3_set_phys_id,
1149 void hns3_ethtool_set_ops(struct net_device *netdev)
1151 struct hnae3_handle *h = hns3_get_handle(netdev);
1153 if (h->flags & HNAE3_SUPPORT_VF)
1154 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1156 netdev->ethtool_ops = &hns3_ethtool_ops;