2 * Copyright (c) 2016~2017 Hisilicon Limited.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/etherdevice.h>
11 #include <linux/string.h>
12 #include <linux/phy.h>
14 #include "hns3_enet.h"
17 char stats_string[ETH_GSTRING_LEN];
22 /* tqp related stats */
23 #define HNS3_TQP_STAT(_string, _member) { \
24 .stats_string = _string, \
25 .stats_size = FIELD_SIZEOF(struct ring_stats, _member), \
26 .stats_offset = offsetof(struct hns3_enet_ring, stats), \
29 static const struct hns3_stats hns3_txq_stats[] = {
30 /* Tx per-queue statistics */
31 HNS3_TQP_STAT("tx_io_err_cnt", io_err_cnt),
32 HNS3_TQP_STAT("tx_sw_err_cnt", sw_err_cnt),
33 HNS3_TQP_STAT("tx_seg_pkt_cnt", seg_pkt_cnt),
34 HNS3_TQP_STAT("tx_pkts", tx_pkts),
35 HNS3_TQP_STAT("tx_bytes", tx_bytes),
36 HNS3_TQP_STAT("tx_err_cnt", tx_err_cnt),
37 HNS3_TQP_STAT("tx_restart_queue", restart_queue),
38 HNS3_TQP_STAT("tx_busy", tx_busy),
41 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
43 static const struct hns3_stats hns3_rxq_stats[] = {
44 /* Rx per-queue statistics */
45 HNS3_TQP_STAT("rx_io_err_cnt", io_err_cnt),
46 HNS3_TQP_STAT("rx_sw_err_cnt", sw_err_cnt),
47 HNS3_TQP_STAT("rx_seg_pkt_cnt", seg_pkt_cnt),
48 HNS3_TQP_STAT("rx_pkts", rx_pkts),
49 HNS3_TQP_STAT("rx_bytes", rx_bytes),
50 HNS3_TQP_STAT("rx_err_cnt", rx_err_cnt),
51 HNS3_TQP_STAT("rx_reuse_pg_cnt", reuse_pg_cnt),
52 HNS3_TQP_STAT("rx_err_pkt_len", err_pkt_len),
53 HNS3_TQP_STAT("rx_non_vld_descs", non_vld_descs),
54 HNS3_TQP_STAT("rx_err_bd_num", err_bd_num),
55 HNS3_TQP_STAT("rx_l2_err", l2_err),
56 HNS3_TQP_STAT("rx_l3l4_csum_err", l3l4_csum_err),
59 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
61 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
63 #define HNS3_SELF_TEST_TPYE_NUM 1
64 #define HNS3_NIC_LB_TEST_PKT_NUM 1
65 #define HNS3_NIC_LB_TEST_RING_ID 0
66 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
68 /* Nic loopback test err */
69 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
70 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
71 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
73 struct hns3_link_mode_mapping {
75 u32 ethtool_link_mode;
78 static const struct hns3_link_mode_mapping hns3_lm_map[] = {
79 {HNS3_LM_FIBRE_BIT, ETHTOOL_LINK_MODE_FIBRE_BIT},
80 {HNS3_LM_AUTONEG_BIT, ETHTOOL_LINK_MODE_Autoneg_BIT},
81 {HNS3_LM_TP_BIT, ETHTOOL_LINK_MODE_TP_BIT},
82 {HNS3_LM_PAUSE_BIT, ETHTOOL_LINK_MODE_Pause_BIT},
83 {HNS3_LM_BACKPLANE_BIT, ETHTOOL_LINK_MODE_Backplane_BIT},
84 {HNS3_LM_10BASET_HALF_BIT, ETHTOOL_LINK_MODE_10baseT_Half_BIT},
85 {HNS3_LM_10BASET_FULL_BIT, ETHTOOL_LINK_MODE_10baseT_Full_BIT},
86 {HNS3_LM_100BASET_HALF_BIT, ETHTOOL_LINK_MODE_100baseT_Half_BIT},
87 {HNS3_LM_100BASET_FULL_BIT, ETHTOOL_LINK_MODE_100baseT_Full_BIT},
88 {HNS3_LM_1000BASET_FULL_BIT, ETHTOOL_LINK_MODE_1000baseT_Full_BIT},
91 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop)
93 struct hnae3_handle *h = hns3_get_handle(ndev);
96 if (!h->ae_algo->ops->set_loopback ||
97 !h->ae_algo->ops->set_promisc_mode)
101 case HNAE3_MAC_INTER_LOOP_MAC:
102 ret = h->ae_algo->ops->set_loopback(h, loop, true);
104 case HNAE3_MAC_LOOP_NONE:
105 ret = h->ae_algo->ops->set_loopback(h,
106 HNAE3_MAC_INTER_LOOP_MAC, false);
116 if (loop == HNAE3_MAC_LOOP_NONE)
117 h->ae_algo->ops->set_promisc_mode(h, ndev->flags & IFF_PROMISC);
119 h->ae_algo->ops->set_promisc_mode(h, 1);
124 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
126 struct hnae3_handle *h = hns3_get_handle(ndev);
129 if (!h->ae_algo->ops->start)
132 ret = h->ae_algo->ops->start(h);
135 "hns3_lb_up ae start return error: %d\n", ret);
139 ret = hns3_lp_setup(ndev, loop_mode);
140 usleep_range(10000, 20000);
145 static int hns3_lp_down(struct net_device *ndev)
147 struct hnae3_handle *h = hns3_get_handle(ndev);
150 if (!h->ae_algo->ops->stop)
153 ret = hns3_lp_setup(ndev, HNAE3_MAC_LOOP_NONE);
155 netdev_err(ndev, "lb_setup return error: %d\n", ret);
159 h->ae_algo->ops->stop(h);
160 usleep_range(10000, 20000);
165 static void hns3_lp_setup_skb(struct sk_buff *skb)
167 struct net_device *ndev = skb->dev;
168 unsigned char *packet;
172 skb_reserve(skb, NET_IP_ALIGN);
173 ethh = skb_put(skb, sizeof(struct ethhdr));
174 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
176 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
177 eth_zero_addr(ethh->h_source);
178 ethh->h_proto = htons(ETH_P_ARP);
179 skb_reset_mac_header(skb);
181 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
182 packet[i] = (unsigned char)(i & 0xff);
185 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
188 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
189 unsigned char *packet = skb->data;
192 for (i = 0; i < skb->len; i++)
193 if (packet[i] != (unsigned char)(i & 0xff))
196 /* The packet is correctly received */
198 tqp_vector->rx_group.total_packets++;
200 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
201 skb->data, skb->len, true);
203 dev_kfree_skb_any(skb);
206 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
208 struct hnae3_handle *h = priv->ae_handle;
209 struct hnae3_knic_private_info *kinfo;
210 u32 i, rcv_good_pkt_total = 0;
213 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
214 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
215 struct hns3_enet_ring_group *rx_group;
218 rx_group = &ring->tqp_vector->rx_group;
219 pre_rx_pkt = rx_group->total_packets;
221 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
223 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
224 rx_group->total_packets = pre_rx_pkt;
226 return rcv_good_pkt_total;
229 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
230 u32 end_ringid, u32 budget)
234 for (i = start_ringid; i <= end_ringid; i++) {
235 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
237 hns3_clean_tx_ring(ring, budget);
242 * hns3_lp_run_test - run loopback test
244 * @mode: loopback type
246 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
248 struct hns3_nic_priv *priv = netdev_priv(ndev);
253 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
256 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
259 hns3_lp_setup_skb(skb);
260 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
263 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
267 tx_ret = hns3_nic_net_xmit(skb, ndev);
268 if (tx_ret == NETDEV_TX_OK)
271 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
274 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
275 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
276 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
277 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
281 /* Allow 200 milliseconds for packets to go from Tx to Rx */
284 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
285 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
286 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
287 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
288 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
292 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
293 HNS3_NIC_LB_TEST_RING_ID,
294 HNS3_NIC_LB_TEST_PKT_NUM);
301 * hns3_nic_self_test - self test
303 * @eth_test: test cmd
306 static void hns3_self_test(struct net_device *ndev,
307 struct ethtool_test *eth_test, u64 *data)
309 struct hns3_nic_priv *priv = netdev_priv(ndev);
310 struct hnae3_handle *h = priv->ae_handle;
311 int st_param[HNS3_SELF_TEST_TPYE_NUM][2];
312 bool if_running = netif_running(ndev);
316 /* Only do offline selftest, or pass by default */
317 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
320 st_param[HNAE3_MAC_INTER_LOOP_MAC][0] = HNAE3_MAC_INTER_LOOP_MAC;
321 st_param[HNAE3_MAC_INTER_LOOP_MAC][1] =
322 h->flags & HNAE3_SUPPORT_MAC_LOOPBACK;
327 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
329 for (i = 0; i < HNS3_SELF_TEST_TPYE_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);
341 if (data[test_index])
342 eth_test->flags |= ETH_TEST_FL_FAILED;
347 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
353 static void hns3_driv_to_eth_caps(u32 caps, struct ethtool_link_ksettings *cmd,
358 for (i = 0; i < ARRAY_SIZE(hns3_lm_map); i++) {
359 if (!(caps & hns3_lm_map[i].hns3_link_mode))
363 __set_bit(hns3_lm_map[i].ethtool_link_mode,
364 cmd->link_modes.advertising);
366 __set_bit(hns3_lm_map[i].ethtool_link_mode,
367 cmd->link_modes.supported);
371 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
373 struct hnae3_handle *h = hns3_get_handle(netdev);
374 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
376 if (!ops->get_sset_count)
381 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
382 ops->get_sset_count(h, stringset));
385 return ops->get_sset_count(h, stringset);
391 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
392 u32 stat_count, u32 num_tqps)
394 #define MAX_PREFIX_SIZE (8 + 4)
399 for (i = 0; i < num_tqps; i++) {
400 for (j = 0; j < stat_count; j++) {
401 data[ETH_GSTRING_LEN - 1] = '\0';
403 /* first, prepend the prefix string */
404 n1 = snprintf(data, MAX_PREFIX_SIZE, "rcb_q%d_", i);
405 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
406 size_left = (ETH_GSTRING_LEN - 1) - n1;
408 /* now, concatenate the stats string to it */
409 strncat(data, stats[j].stats_string, size_left);
410 data += ETH_GSTRING_LEN;
417 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
419 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
421 /* get strings for Tx */
422 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
425 /* get strings for Rx */
426 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
432 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
434 struct hnae3_handle *h = hns3_get_handle(netdev);
435 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
436 char *buff = (char *)data;
438 if (!ops->get_strings)
443 buff = hns3_get_strings_tqps(h, buff);
444 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
447 ops->get_strings(h, stringset, data);
452 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
454 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
455 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
456 struct hns3_enet_ring *ring;
460 /* get stats for Tx */
461 for (i = 0; i < kinfo->num_tqps; i++) {
462 ring = nic_priv->ring_data[i].ring;
463 for (i = 0; i < HNS3_TXQ_STATS_COUNT; i++) {
464 stat = (u8 *)ring + hns3_txq_stats[i].stats_offset;
465 *data++ = *(u64 *)stat;
469 /* get stats for Rx */
470 for (i = 0; i < kinfo->num_tqps; i++) {
471 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
472 for (i = 0; i < HNS3_RXQ_STATS_COUNT; i++) {
473 stat = (u8 *)ring + hns3_rxq_stats[i].stats_offset;
474 *data++ = *(u64 *)stat;
481 /* hns3_get_stats - get detail statistics.
482 * @netdev: net device
483 * @stats: statistics info.
484 * @data: statistics data.
486 static void hns3_get_stats(struct net_device *netdev,
487 struct ethtool_stats *stats, u64 *data)
489 struct hnae3_handle *h = hns3_get_handle(netdev);
492 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
493 netdev_err(netdev, "could not get any statistics\n");
497 h->ae_algo->ops->update_stats(h, &netdev->stats);
499 /* get per-queue stats */
500 p = hns3_get_stats_tqps(h, p);
502 /* get MAC & other misc hardware stats */
503 h->ae_algo->ops->get_stats(h, p);
506 static void hns3_get_drvinfo(struct net_device *netdev,
507 struct ethtool_drvinfo *drvinfo)
509 struct hns3_nic_priv *priv = netdev_priv(netdev);
510 struct hnae3_handle *h = priv->ae_handle;
512 strncpy(drvinfo->version, hns3_driver_version,
513 sizeof(drvinfo->version));
514 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
516 strncpy(drvinfo->driver, h->pdev->driver->name,
517 sizeof(drvinfo->driver));
518 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
520 strncpy(drvinfo->bus_info, pci_name(h->pdev),
521 sizeof(drvinfo->bus_info));
522 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
524 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
525 priv->ae_handle->ae_algo->ops->get_fw_version(h));
528 static u32 hns3_get_link(struct net_device *netdev)
530 struct hnae3_handle *h = hns3_get_handle(netdev);
532 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
533 return h->ae_algo->ops->get_status(h);
538 static void hns3_get_ringparam(struct net_device *netdev,
539 struct ethtool_ringparam *param)
541 struct hns3_nic_priv *priv = netdev_priv(netdev);
542 struct hnae3_handle *h = priv->ae_handle;
543 int queue_num = h->kinfo.num_tqps;
545 param->tx_max_pending = HNS3_RING_MAX_PENDING;
546 param->rx_max_pending = HNS3_RING_MAX_PENDING;
548 param->tx_pending = priv->ring_data[0].ring->desc_num;
549 param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
552 static void hns3_get_pauseparam(struct net_device *netdev,
553 struct ethtool_pauseparam *param)
555 struct hnae3_handle *h = hns3_get_handle(netdev);
557 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
558 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
559 ¶m->rx_pause, ¶m->tx_pause);
562 static int hns3_set_pauseparam(struct net_device *netdev,
563 struct ethtool_pauseparam *param)
565 struct hnae3_handle *h = hns3_get_handle(netdev);
567 if (h->ae_algo->ops->set_pauseparam)
568 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
574 static int hns3_get_link_ksettings(struct net_device *netdev,
575 struct ethtool_link_ksettings *cmd)
577 struct hnae3_handle *h = hns3_get_handle(netdev);
578 u32 flowctrl_adv = 0;
581 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
584 if (!h->ae_algo || !h->ae_algo->ops)
587 /* 1.auto_neg & speed & duplex from cmd */
589 phy_ethtool_ksettings_get(netdev->phydev, cmd);
590 else if (h->ae_algo->ops->get_ksettings_an_result)
591 h->ae_algo->ops->get_ksettings_an_result(h,
598 link_stat = hns3_get_link(netdev);
600 cmd->base.speed = SPEED_UNKNOWN;
601 cmd->base.duplex = DUPLEX_UNKNOWN;
604 /* 2.media_type get from bios parameter block */
605 if (h->ae_algo->ops->get_media_type) {
606 h->ae_algo->ops->get_media_type(h, &media_type);
608 switch (media_type) {
609 case HNAE3_MEDIA_TYPE_FIBER:
610 cmd->base.port = PORT_FIBRE;
611 supported_caps = HNS3_LM_FIBRE_BIT |
612 HNS3_LM_AUTONEG_BIT |
614 HNS3_LM_1000BASET_FULL_BIT;
616 advertised_caps = supported_caps;
618 case HNAE3_MEDIA_TYPE_COPPER:
619 cmd->base.port = PORT_TP;
620 supported_caps = HNS3_LM_TP_BIT |
621 HNS3_LM_AUTONEG_BIT |
623 HNS3_LM_1000BASET_FULL_BIT |
624 HNS3_LM_100BASET_FULL_BIT |
625 HNS3_LM_100BASET_HALF_BIT |
626 HNS3_LM_10BASET_FULL_BIT |
627 HNS3_LM_10BASET_HALF_BIT;
628 advertised_caps = supported_caps;
630 case HNAE3_MEDIA_TYPE_BACKPLANE:
631 cmd->base.port = PORT_NONE;
632 supported_caps = HNS3_LM_BACKPLANE_BIT |
634 HNS3_LM_AUTONEG_BIT |
635 HNS3_LM_1000BASET_FULL_BIT |
636 HNS3_LM_100BASET_FULL_BIT |
637 HNS3_LM_100BASET_HALF_BIT |
638 HNS3_LM_10BASET_FULL_BIT |
639 HNS3_LM_10BASET_HALF_BIT;
641 advertised_caps = supported_caps;
643 case HNAE3_MEDIA_TYPE_UNKNOWN:
645 cmd->base.port = PORT_OTHER;
651 if (!cmd->base.autoneg)
652 advertised_caps &= ~HNS3_LM_AUTONEG_BIT;
654 advertised_caps &= ~HNS3_LM_PAUSE_BIT;
656 /* now, map driver link modes to ethtool link modes */
657 hns3_driv_to_eth_caps(supported_caps, cmd, false);
658 hns3_driv_to_eth_caps(advertised_caps, cmd, true);
661 /* 3.mdix_ctrl&mdix get from phy reg */
662 if (h->ae_algo->ops->get_mdix_mode)
663 h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
664 &cmd->base.eth_tp_mdix);
666 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
668 /* 5.get flow control setttings */
669 if (h->ae_algo->ops->get_flowctrl_adv)
670 h->ae_algo->ops->get_flowctrl_adv(h, &flowctrl_adv);
672 if (flowctrl_adv & ADVERTISED_Pause)
673 ethtool_link_ksettings_add_link_mode(cmd, advertising,
676 if (flowctrl_adv & ADVERTISED_Asym_Pause)
677 ethtool_link_ksettings_add_link_mode(cmd, advertising,
683 static int hns3_set_link_ksettings(struct net_device *netdev,
684 const struct ethtool_link_ksettings *cmd)
686 /* Only support ksettings_set for netdev with phy attached for now */
688 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
693 static u32 hns3_get_rss_key_size(struct net_device *netdev)
695 struct hnae3_handle *h = hns3_get_handle(netdev);
697 if (!h->ae_algo || !h->ae_algo->ops ||
698 !h->ae_algo->ops->get_rss_key_size)
701 return h->ae_algo->ops->get_rss_key_size(h);
704 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
706 struct hnae3_handle *h = hns3_get_handle(netdev);
708 if (!h->ae_algo || !h->ae_algo->ops ||
709 !h->ae_algo->ops->get_rss_indir_size)
712 return h->ae_algo->ops->get_rss_indir_size(h);
715 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
718 struct hnae3_handle *h = hns3_get_handle(netdev);
720 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
723 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
726 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
727 const u8 *key, const u8 hfunc)
729 struct hnae3_handle *h = hns3_get_handle(netdev);
731 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
734 /* currently we only support Toeplitz hash */
735 if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
737 "hash func not supported (only Toeplitz hash)\n");
742 "set rss failed for indir is empty\n");
746 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
749 static int hns3_get_rxnfc(struct net_device *netdev,
750 struct ethtool_rxnfc *cmd,
753 struct hnae3_handle *h = hns3_get_handle(netdev);
755 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss_tuple)
759 case ETHTOOL_GRXRINGS:
760 cmd->data = h->kinfo.rss_size;
763 return h->ae_algo->ops->get_rss_tuple(h, cmd);
771 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
774 struct hnae3_handle *h = priv->ae_handle;
777 h->kinfo.num_desc = new_desc_num;
779 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
780 priv->ring_data[i].ring->desc_num = new_desc_num;
782 return hns3_init_all_ring(priv);
785 static int hns3_set_ringparam(struct net_device *ndev,
786 struct ethtool_ringparam *param)
788 struct hns3_nic_priv *priv = netdev_priv(ndev);
789 struct hnae3_handle *h = priv->ae_handle;
790 bool if_running = netif_running(ndev);
791 u32 old_desc_num, new_desc_num;
794 if (param->rx_mini_pending || param->rx_jumbo_pending)
797 if (param->tx_pending != param->rx_pending) {
799 "Descriptors of tx and rx must be equal");
803 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
804 param->tx_pending < HNS3_RING_MIN_PENDING) {
806 "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
807 param->tx_pending, HNS3_RING_MIN_PENDING,
808 HNS3_RING_MAX_PENDING);
812 new_desc_num = param->tx_pending;
814 /* Hardware requires that its descriptors must be multiple of eight */
815 new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
816 old_desc_num = h->kinfo.num_desc;
817 if (old_desc_num == new_desc_num)
821 "Changing descriptor count from %d to %d.\n",
822 old_desc_num, new_desc_num);
827 ret = hns3_uninit_all_ring(priv);
831 ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
833 ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
836 "Revert to old bd num fail, ret=%d.\n", ret);
842 ret = dev_open(ndev);
847 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
849 struct hnae3_handle *h = hns3_get_handle(netdev);
851 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss_tuple)
856 return h->ae_algo->ops->set_rss_tuple(h, cmd);
862 static int hns3_nway_reset(struct net_device *netdev)
864 struct phy_device *phy = netdev->phydev;
866 if (!netif_running(netdev))
869 /* Only support nway_reset for netdev with phy attached for now */
873 if (phy->autoneg != AUTONEG_ENABLE)
876 return genphy_restart_aneg(phy);
879 static void hns3_get_channels(struct net_device *netdev,
880 struct ethtool_channels *ch)
882 struct hnae3_handle *h = hns3_get_handle(netdev);
884 if (h->ae_algo->ops->get_channels)
885 h->ae_algo->ops->get_channels(h, ch);
888 static const struct ethtool_ops hns3vf_ethtool_ops = {
889 .get_drvinfo = hns3_get_drvinfo,
890 .get_ringparam = hns3_get_ringparam,
891 .set_ringparam = hns3_set_ringparam,
892 .get_strings = hns3_get_strings,
893 .get_ethtool_stats = hns3_get_stats,
894 .get_sset_count = hns3_get_sset_count,
895 .get_rxnfc = hns3_get_rxnfc,
896 .get_rxfh_key_size = hns3_get_rss_key_size,
897 .get_rxfh_indir_size = hns3_get_rss_indir_size,
898 .get_rxfh = hns3_get_rss,
899 .set_rxfh = hns3_set_rss,
900 .get_link_ksettings = hns3_get_link_ksettings,
903 static const struct ethtool_ops hns3_ethtool_ops = {
904 .self_test = hns3_self_test,
905 .get_drvinfo = hns3_get_drvinfo,
906 .get_link = hns3_get_link,
907 .get_ringparam = hns3_get_ringparam,
908 .set_ringparam = hns3_set_ringparam,
909 .get_pauseparam = hns3_get_pauseparam,
910 .set_pauseparam = hns3_set_pauseparam,
911 .get_strings = hns3_get_strings,
912 .get_ethtool_stats = hns3_get_stats,
913 .get_sset_count = hns3_get_sset_count,
914 .get_rxnfc = hns3_get_rxnfc,
915 .set_rxnfc = hns3_set_rxnfc,
916 .get_rxfh_key_size = hns3_get_rss_key_size,
917 .get_rxfh_indir_size = hns3_get_rss_indir_size,
918 .get_rxfh = hns3_get_rss,
919 .set_rxfh = hns3_set_rss,
920 .get_link_ksettings = hns3_get_link_ksettings,
921 .set_link_ksettings = hns3_set_link_ksettings,
922 .nway_reset = hns3_nway_reset,
923 .get_channels = hns3_get_channels,
924 .set_channels = hns3_set_channels,
927 void hns3_ethtool_set_ops(struct net_device *netdev)
929 struct hnae3_handle *h = hns3_get_handle(netdev);
931 if (h->flags & HNAE3_SUPPORT_VF)
932 netdev->ethtool_ops = &hns3vf_ethtool_ops;
934 netdev->ethtool_ops = &hns3_ethtool_ops;