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("tx_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("tx_wake", restart_queue),
31 HNS3_TQP_STAT("tx_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("rx_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 2
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);
76 if (!h->ae_algo->ops->set_loopback ||
77 !h->ae_algo->ops->set_promisc_mode)
81 case HNAE3_MAC_INTER_LOOP_SERDES:
82 case HNAE3_MAC_INTER_LOOP_MAC:
83 ret = h->ae_algo->ops->set_loopback(h, loop, en);
93 h->ae_algo->ops->set_promisc_mode(h, en, en);
98 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
100 struct hnae3_handle *h = hns3_get_handle(ndev);
103 ret = hns3_nic_reset_all_ring(h);
107 ret = hns3_lp_setup(ndev, loop_mode, true);
108 usleep_range(10000, 20000);
113 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
117 ret = hns3_lp_setup(ndev, loop_mode, false);
119 netdev_err(ndev, "lb_setup return error: %d\n", ret);
123 usleep_range(10000, 20000);
128 static void hns3_lp_setup_skb(struct sk_buff *skb)
130 struct net_device *ndev = skb->dev;
131 unsigned char *packet;
135 skb_reserve(skb, NET_IP_ALIGN);
136 ethh = skb_put(skb, sizeof(struct ethhdr));
137 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
139 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
140 ethh->h_dest[5] += 0x1f;
141 eth_zero_addr(ethh->h_source);
142 ethh->h_proto = htons(ETH_P_ARP);
143 skb_reset_mac_header(skb);
145 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
146 packet[i] = (unsigned char)(i & 0xff);
149 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
152 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
153 unsigned char *packet = skb->data;
156 for (i = 0; i < skb->len; i++)
157 if (packet[i] != (unsigned char)(i & 0xff))
160 /* The packet is correctly received */
162 tqp_vector->rx_group.total_packets++;
164 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
165 skb->data, skb->len, true);
167 dev_kfree_skb_any(skb);
170 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
172 struct hnae3_handle *h = priv->ae_handle;
173 struct hnae3_knic_private_info *kinfo;
174 u32 i, rcv_good_pkt_total = 0;
177 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
178 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
179 struct hns3_enet_ring_group *rx_group;
182 rx_group = &ring->tqp_vector->rx_group;
183 pre_rx_pkt = rx_group->total_packets;
186 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
189 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
190 rx_group->total_packets = pre_rx_pkt;
192 return rcv_good_pkt_total;
195 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
196 u32 end_ringid, u32 budget)
200 for (i = start_ringid; i <= end_ringid; i++) {
201 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
203 hns3_clean_tx_ring(ring, budget);
208 * hns3_lp_run_test - run loopback test
210 * @mode: loopback type
212 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
214 struct hns3_nic_priv *priv = netdev_priv(ndev);
219 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
222 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
225 hns3_lp_setup_skb(skb);
226 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
229 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
233 tx_ret = hns3_nic_net_xmit(skb, ndev);
234 if (tx_ret == NETDEV_TX_OK)
237 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
240 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
241 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
242 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
243 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
247 /* Allow 200 milliseconds for packets to go from Tx to Rx */
250 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
251 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
252 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
253 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
254 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
258 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
259 HNS3_NIC_LB_TEST_RING_ID,
260 HNS3_NIC_LB_TEST_PKT_NUM);
267 * hns3_nic_self_test - self test
269 * @eth_test: test cmd
272 static void hns3_self_test(struct net_device *ndev,
273 struct ethtool_test *eth_test, u64 *data)
275 struct hns3_nic_priv *priv = netdev_priv(ndev);
276 struct hnae3_handle *h = priv->ae_handle;
277 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
278 bool if_running = netif_running(ndev);
279 #if IS_ENABLED(CONFIG_VLAN_8021Q)
280 bool dis_vlan_filter;
285 /* Only do offline selftest, or pass by default */
286 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
289 st_param[HNAE3_MAC_INTER_LOOP_MAC][0] = HNAE3_MAC_INTER_LOOP_MAC;
290 st_param[HNAE3_MAC_INTER_LOOP_MAC][1] =
291 h->flags & HNAE3_SUPPORT_MAC_LOOPBACK;
293 st_param[HNAE3_MAC_INTER_LOOP_SERDES][0] = HNAE3_MAC_INTER_LOOP_SERDES;
294 st_param[HNAE3_MAC_INTER_LOOP_SERDES][1] =
295 h->flags & HNAE3_SUPPORT_SERDES_LOOPBACK;
298 ndev->netdev_ops->ndo_stop(ndev);
300 #if IS_ENABLED(CONFIG_VLAN_8021Q)
301 /* Disable the vlan filter for selftest does not support it */
302 dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
303 h->ae_algo->ops->enable_vlan_filter;
305 h->ae_algo->ops->enable_vlan_filter(h, false);
308 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
310 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
311 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
316 data[test_index] = hns3_lp_up(ndev, loop_type);
317 if (!data[test_index]) {
318 data[test_index] = hns3_lp_run_test(ndev, loop_type);
319 hns3_lp_down(ndev, loop_type);
322 if (data[test_index])
323 eth_test->flags |= ETH_TEST_FL_FAILED;
328 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
330 #if IS_ENABLED(CONFIG_VLAN_8021Q)
332 h->ae_algo->ops->enable_vlan_filter(h, true);
336 ndev->netdev_ops->ndo_open(ndev);
339 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
341 struct hnae3_handle *h = hns3_get_handle(netdev);
342 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
344 if (!ops->get_sset_count)
349 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
350 ops->get_sset_count(h, stringset));
353 return ops->get_sset_count(h, stringset);
359 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
360 u32 stat_count, u32 num_tqps, const char *prefix)
362 #define MAX_PREFIX_SIZE (6 + 4)
367 for (i = 0; i < num_tqps; i++) {
368 for (j = 0; j < stat_count; j++) {
369 data[ETH_GSTRING_LEN - 1] = '\0';
371 /* first, prepend the prefix string */
372 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s#%d_",
374 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
375 size_left = (ETH_GSTRING_LEN - 1) - n1;
377 /* now, concatenate the stats string to it */
378 strncat(data, stats[j].stats_string, size_left);
379 data += ETH_GSTRING_LEN;
386 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
388 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
389 const char tx_prefix[] = "txq";
390 const char rx_prefix[] = "rxq";
392 /* get strings for Tx */
393 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
394 kinfo->num_tqps, tx_prefix);
396 /* get strings for Rx */
397 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
398 kinfo->num_tqps, rx_prefix);
403 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
405 struct hnae3_handle *h = hns3_get_handle(netdev);
406 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
407 char *buff = (char *)data;
409 if (!ops->get_strings)
414 buff = hns3_get_strings_tqps(h, buff);
415 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
418 ops->get_strings(h, stringset, data);
423 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
425 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
426 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
427 struct hns3_enet_ring *ring;
431 /* get stats for Tx */
432 for (i = 0; i < kinfo->num_tqps; i++) {
433 ring = nic_priv->ring_data[i].ring;
434 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
435 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
436 *data++ = *(u64 *)stat;
440 /* get stats for Rx */
441 for (i = 0; i < kinfo->num_tqps; i++) {
442 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
443 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
444 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
445 *data++ = *(u64 *)stat;
452 /* hns3_get_stats - get detail statistics.
453 * @netdev: net device
454 * @stats: statistics info.
455 * @data: statistics data.
457 static void hns3_get_stats(struct net_device *netdev,
458 struct ethtool_stats *stats, u64 *data)
460 struct hnae3_handle *h = hns3_get_handle(netdev);
463 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
464 netdev_err(netdev, "could not get any statistics\n");
468 h->ae_algo->ops->update_stats(h, &netdev->stats);
470 /* get per-queue stats */
471 p = hns3_get_stats_tqps(h, p);
473 /* get MAC & other misc hardware stats */
474 h->ae_algo->ops->get_stats(h, p);
477 static void hns3_get_drvinfo(struct net_device *netdev,
478 struct ethtool_drvinfo *drvinfo)
480 struct hns3_nic_priv *priv = netdev_priv(netdev);
481 struct hnae3_handle *h = priv->ae_handle;
483 strncpy(drvinfo->version, hns3_driver_version,
484 sizeof(drvinfo->version));
485 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
487 strncpy(drvinfo->driver, h->pdev->driver->name,
488 sizeof(drvinfo->driver));
489 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
491 strncpy(drvinfo->bus_info, pci_name(h->pdev),
492 sizeof(drvinfo->bus_info));
493 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
495 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
496 priv->ae_handle->ae_algo->ops->get_fw_version(h));
499 static u32 hns3_get_link(struct net_device *netdev)
501 struct hnae3_handle *h = hns3_get_handle(netdev);
503 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
504 return h->ae_algo->ops->get_status(h);
509 static void hns3_get_ringparam(struct net_device *netdev,
510 struct ethtool_ringparam *param)
512 struct hns3_nic_priv *priv = netdev_priv(netdev);
513 struct hnae3_handle *h = priv->ae_handle;
514 int queue_num = h->kinfo.num_tqps;
516 param->tx_max_pending = HNS3_RING_MAX_PENDING;
517 param->rx_max_pending = HNS3_RING_MAX_PENDING;
519 param->tx_pending = priv->ring_data[0].ring->desc_num;
520 param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
523 static void hns3_get_pauseparam(struct net_device *netdev,
524 struct ethtool_pauseparam *param)
526 struct hnae3_handle *h = hns3_get_handle(netdev);
528 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
529 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
530 ¶m->rx_pause, ¶m->tx_pause);
533 static int hns3_set_pauseparam(struct net_device *netdev,
534 struct ethtool_pauseparam *param)
536 struct hnae3_handle *h = hns3_get_handle(netdev);
538 if (h->ae_algo->ops->set_pauseparam)
539 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
545 static int hns3_get_link_ksettings(struct net_device *netdev,
546 struct ethtool_link_ksettings *cmd)
548 struct hnae3_handle *h = hns3_get_handle(netdev);
549 u32 flowctrl_adv = 0;
552 if (!h->ae_algo || !h->ae_algo->ops)
555 /* 1.auto_neg & speed & duplex from cmd */
556 if (netdev->phydev) {
557 phy_ethtool_ksettings_get(netdev->phydev, cmd);
562 if (h->ae_algo->ops->get_ksettings_an_result)
563 h->ae_algo->ops->get_ksettings_an_result(h,
570 link_stat = hns3_get_link(netdev);
572 cmd->base.speed = SPEED_UNKNOWN;
573 cmd->base.duplex = DUPLEX_UNKNOWN;
576 /* 2.get link mode and port type*/
577 if (h->ae_algo->ops->get_link_mode)
578 h->ae_algo->ops->get_link_mode(h,
579 cmd->link_modes.supported,
580 cmd->link_modes.advertising);
582 cmd->base.port = PORT_NONE;
583 if (h->ae_algo->ops->get_port_type)
584 h->ae_algo->ops->get_port_type(h,
587 /* 3.mdix_ctrl&mdix get from phy reg */
588 if (h->ae_algo->ops->get_mdix_mode)
589 h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
590 &cmd->base.eth_tp_mdix);
592 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
594 /* 5.get flow control setttings */
595 if (h->ae_algo->ops->get_flowctrl_adv)
596 h->ae_algo->ops->get_flowctrl_adv(h, &flowctrl_adv);
598 if (flowctrl_adv & ADVERTISED_Pause)
599 ethtool_link_ksettings_add_link_mode(cmd, advertising,
602 if (flowctrl_adv & ADVERTISED_Asym_Pause)
603 ethtool_link_ksettings_add_link_mode(cmd, advertising,
609 static int hns3_set_link_ksettings(struct net_device *netdev,
610 const struct ethtool_link_ksettings *cmd)
612 /* Only support ksettings_set for netdev with phy attached for now */
614 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
619 static u32 hns3_get_rss_key_size(struct net_device *netdev)
621 struct hnae3_handle *h = hns3_get_handle(netdev);
623 if (!h->ae_algo || !h->ae_algo->ops ||
624 !h->ae_algo->ops->get_rss_key_size)
627 return h->ae_algo->ops->get_rss_key_size(h);
630 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
632 struct hnae3_handle *h = hns3_get_handle(netdev);
634 if (!h->ae_algo || !h->ae_algo->ops ||
635 !h->ae_algo->ops->get_rss_indir_size)
638 return h->ae_algo->ops->get_rss_indir_size(h);
641 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
644 struct hnae3_handle *h = hns3_get_handle(netdev);
646 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
649 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
652 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
653 const u8 *key, const u8 hfunc)
655 struct hnae3_handle *h = hns3_get_handle(netdev);
657 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
660 /* currently we only support Toeplitz hash */
661 if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
663 "hash func not supported (only Toeplitz hash)\n");
668 "set rss failed for indir is empty\n");
672 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
675 static int hns3_get_rxnfc(struct net_device *netdev,
676 struct ethtool_rxnfc *cmd,
679 struct hnae3_handle *h = hns3_get_handle(netdev);
681 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss_tuple)
685 case ETHTOOL_GRXRINGS:
686 cmd->data = h->kinfo.rss_size;
689 return h->ae_algo->ops->get_rss_tuple(h, cmd);
697 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
700 struct hnae3_handle *h = priv->ae_handle;
703 h->kinfo.num_desc = new_desc_num;
705 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
706 priv->ring_data[i].ring->desc_num = new_desc_num;
708 return hns3_init_all_ring(priv);
711 static int hns3_set_ringparam(struct net_device *ndev,
712 struct ethtool_ringparam *param)
714 struct hns3_nic_priv *priv = netdev_priv(ndev);
715 struct hnae3_handle *h = priv->ae_handle;
716 bool if_running = netif_running(ndev);
717 u32 old_desc_num, new_desc_num;
720 if (param->rx_mini_pending || param->rx_jumbo_pending)
723 if (param->tx_pending != param->rx_pending) {
725 "Descriptors of tx and rx must be equal");
729 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
730 param->tx_pending < HNS3_RING_MIN_PENDING) {
732 "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
733 param->tx_pending, HNS3_RING_MIN_PENDING,
734 HNS3_RING_MAX_PENDING);
738 new_desc_num = param->tx_pending;
740 /* Hardware requires that its descriptors must be multiple of eight */
741 new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
742 old_desc_num = h->kinfo.num_desc;
743 if (old_desc_num == new_desc_num)
747 "Changing descriptor count from %d to %d.\n",
748 old_desc_num, new_desc_num);
753 ret = hns3_uninit_all_ring(priv);
757 ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
759 ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
762 "Revert to old bd num fail, ret=%d.\n", ret);
768 ret = dev_open(ndev);
773 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
775 struct hnae3_handle *h = hns3_get_handle(netdev);
777 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss_tuple)
782 return h->ae_algo->ops->set_rss_tuple(h, cmd);
788 static int hns3_nway_reset(struct net_device *netdev)
790 struct phy_device *phy = netdev->phydev;
792 if (!netif_running(netdev))
795 /* Only support nway_reset for netdev with phy attached for now */
799 if (phy->autoneg != AUTONEG_ENABLE)
802 return genphy_restart_aneg(phy);
805 static void hns3_get_channels(struct net_device *netdev,
806 struct ethtool_channels *ch)
808 struct hnae3_handle *h = hns3_get_handle(netdev);
810 if (h->ae_algo->ops->get_channels)
811 h->ae_algo->ops->get_channels(h, ch);
814 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
815 struct ethtool_coalesce *cmd)
817 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
818 struct hns3_nic_priv *priv = netdev_priv(netdev);
819 struct hnae3_handle *h = priv->ae_handle;
820 u16 queue_num = h->kinfo.num_tqps;
822 if (queue >= queue_num) {
824 "Invalid queue value %d! Queue max id=%d\n",
825 queue, queue_num - 1);
829 tx_vector = priv->ring_data[queue].ring->tqp_vector;
830 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
832 cmd->use_adaptive_tx_coalesce =
833 tx_vector->tx_group.coal.gl_adapt_enable;
834 cmd->use_adaptive_rx_coalesce =
835 rx_vector->rx_group.coal.gl_adapt_enable;
837 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
838 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
840 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
841 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
846 static int hns3_get_coalesce(struct net_device *netdev,
847 struct ethtool_coalesce *cmd)
849 return hns3_get_coalesce_per_queue(netdev, 0, cmd);
852 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
853 struct ethtool_coalesce *cmd)
857 if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
859 "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
864 if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
866 "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
871 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
872 if (rx_gl != cmd->rx_coalesce_usecs) {
874 "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
875 cmd->rx_coalesce_usecs, rx_gl);
878 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
879 if (tx_gl != cmd->tx_coalesce_usecs) {
881 "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
882 cmd->tx_coalesce_usecs, tx_gl);
888 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
889 struct ethtool_coalesce *cmd)
893 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
895 "tx_usecs_high must be same as rx_usecs_high.\n");
899 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
901 "Invalid usecs_high value, usecs_high range is 0-%d\n",
906 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
907 if (rl != cmd->rx_coalesce_usecs_high) {
909 "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
910 cmd->rx_coalesce_usecs_high, rl);
916 static int hns3_check_coalesce_para(struct net_device *netdev,
917 struct ethtool_coalesce *cmd)
921 ret = hns3_check_gl_coalesce_para(netdev, cmd);
924 "Check gl coalesce param fail. ret = %d\n", ret);
928 ret = hns3_check_rl_coalesce_para(netdev, cmd);
931 "Check rl coalesce param fail. ret = %d\n", ret);
935 if (cmd->use_adaptive_tx_coalesce == 1 ||
936 cmd->use_adaptive_rx_coalesce == 1) {
938 "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
939 cmd->use_adaptive_tx_coalesce,
940 cmd->use_adaptive_rx_coalesce);
946 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
947 struct ethtool_coalesce *cmd,
950 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
951 struct hns3_nic_priv *priv = netdev_priv(netdev);
952 struct hnae3_handle *h = priv->ae_handle;
953 int queue_num = h->kinfo.num_tqps;
955 tx_vector = priv->ring_data[queue].ring->tqp_vector;
956 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
958 tx_vector->tx_group.coal.gl_adapt_enable =
959 cmd->use_adaptive_tx_coalesce;
960 rx_vector->rx_group.coal.gl_adapt_enable =
961 cmd->use_adaptive_rx_coalesce;
963 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
964 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
966 hns3_set_vector_coalesce_tx_gl(tx_vector,
967 tx_vector->tx_group.coal.int_gl);
968 hns3_set_vector_coalesce_rx_gl(rx_vector,
969 rx_vector->rx_group.coal.int_gl);
971 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
972 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
975 static int hns3_set_coalesce(struct net_device *netdev,
976 struct ethtool_coalesce *cmd)
978 struct hnae3_handle *h = hns3_get_handle(netdev);
979 u16 queue_num = h->kinfo.num_tqps;
983 ret = hns3_check_coalesce_para(netdev, cmd);
987 h->kinfo.int_rl_setting =
988 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
990 for (i = 0; i < queue_num; i++)
991 hns3_set_coalesce_per_queue(netdev, cmd, i);
996 static int hns3_get_regs_len(struct net_device *netdev)
998 struct hnae3_handle *h = hns3_get_handle(netdev);
1000 if (!h->ae_algo->ops->get_regs_len)
1003 return h->ae_algo->ops->get_regs_len(h);
1006 static void hns3_get_regs(struct net_device *netdev,
1007 struct ethtool_regs *cmd, void *data)
1009 struct hnae3_handle *h = hns3_get_handle(netdev);
1011 if (!h->ae_algo->ops->get_regs)
1014 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1017 static int hns3_set_phys_id(struct net_device *netdev,
1018 enum ethtool_phys_id_state state)
1020 struct hnae3_handle *h = hns3_get_handle(netdev);
1022 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id)
1025 return h->ae_algo->ops->set_led_id(h, state);
1028 static const struct ethtool_ops hns3vf_ethtool_ops = {
1029 .get_drvinfo = hns3_get_drvinfo,
1030 .get_ringparam = hns3_get_ringparam,
1031 .set_ringparam = hns3_set_ringparam,
1032 .get_strings = hns3_get_strings,
1033 .get_ethtool_stats = hns3_get_stats,
1034 .get_sset_count = hns3_get_sset_count,
1035 .get_rxnfc = hns3_get_rxnfc,
1036 .get_rxfh_key_size = hns3_get_rss_key_size,
1037 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1038 .get_rxfh = hns3_get_rss,
1039 .set_rxfh = hns3_set_rss,
1040 .get_link_ksettings = hns3_get_link_ksettings,
1041 .get_channels = hns3_get_channels,
1042 .get_coalesce = hns3_get_coalesce,
1043 .set_coalesce = hns3_set_coalesce,
1044 .get_link = hns3_get_link,
1047 static const struct ethtool_ops hns3_ethtool_ops = {
1048 .self_test = hns3_self_test,
1049 .get_drvinfo = hns3_get_drvinfo,
1050 .get_link = hns3_get_link,
1051 .get_ringparam = hns3_get_ringparam,
1052 .set_ringparam = hns3_set_ringparam,
1053 .get_pauseparam = hns3_get_pauseparam,
1054 .set_pauseparam = hns3_set_pauseparam,
1055 .get_strings = hns3_get_strings,
1056 .get_ethtool_stats = hns3_get_stats,
1057 .get_sset_count = hns3_get_sset_count,
1058 .get_rxnfc = hns3_get_rxnfc,
1059 .set_rxnfc = hns3_set_rxnfc,
1060 .get_rxfh_key_size = hns3_get_rss_key_size,
1061 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1062 .get_rxfh = hns3_get_rss,
1063 .set_rxfh = hns3_set_rss,
1064 .get_link_ksettings = hns3_get_link_ksettings,
1065 .set_link_ksettings = hns3_set_link_ksettings,
1066 .nway_reset = hns3_nway_reset,
1067 .get_channels = hns3_get_channels,
1068 .set_channels = hns3_set_channels,
1069 .get_coalesce = hns3_get_coalesce,
1070 .set_coalesce = hns3_set_coalesce,
1071 .get_regs_len = hns3_get_regs_len,
1072 .get_regs = hns3_get_regs,
1073 .set_phys_id = hns3_set_phys_id,
1076 void hns3_ethtool_set_ops(struct net_device *netdev)
1078 struct hnae3_handle *h = hns3_get_handle(netdev);
1080 if (h->flags & HNAE3_SUPPORT_VF)
1081 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1083 netdev->ethtool_ops = &hns3_ethtool_ops;