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 eth_zero_addr(ethh->h_source);
141 ethh->h_proto = htons(ETH_P_ARP);
142 skb_reset_mac_header(skb);
144 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
145 packet[i] = (unsigned char)(i & 0xff);
148 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
151 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
152 unsigned char *packet = skb->data;
155 for (i = 0; i < skb->len; i++)
156 if (packet[i] != (unsigned char)(i & 0xff))
159 /* The packet is correctly received */
161 tqp_vector->rx_group.total_packets++;
163 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
164 skb->data, skb->len, true);
166 dev_kfree_skb_any(skb);
169 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
171 struct hnae3_handle *h = priv->ae_handle;
172 struct hnae3_knic_private_info *kinfo;
173 u32 i, rcv_good_pkt_total = 0;
176 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
177 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
178 struct hns3_enet_ring_group *rx_group;
181 rx_group = &ring->tqp_vector->rx_group;
182 pre_rx_pkt = rx_group->total_packets;
185 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
188 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
189 rx_group->total_packets = pre_rx_pkt;
191 return rcv_good_pkt_total;
194 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
195 u32 end_ringid, u32 budget)
199 for (i = start_ringid; i <= end_ringid; i++) {
200 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
202 hns3_clean_tx_ring(ring, budget);
207 * hns3_lp_run_test - run loopback test
209 * @mode: loopback type
211 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
213 struct hns3_nic_priv *priv = netdev_priv(ndev);
218 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
221 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
224 hns3_lp_setup_skb(skb);
225 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
228 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
232 tx_ret = hns3_nic_net_xmit(skb, ndev);
233 if (tx_ret == NETDEV_TX_OK)
236 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
239 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
240 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
241 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
242 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
246 /* Allow 200 milliseconds for packets to go from Tx to Rx */
249 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
250 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
251 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
252 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
253 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
257 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
258 HNS3_NIC_LB_TEST_RING_ID,
259 HNS3_NIC_LB_TEST_PKT_NUM);
266 * hns3_nic_self_test - self test
268 * @eth_test: test cmd
271 static void hns3_self_test(struct net_device *ndev,
272 struct ethtool_test *eth_test, u64 *data)
274 struct hns3_nic_priv *priv = netdev_priv(ndev);
275 struct hnae3_handle *h = priv->ae_handle;
276 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
277 bool if_running = netif_running(ndev);
278 #if IS_ENABLED(CONFIG_VLAN_8021Q)
279 bool dis_vlan_filter;
284 /* Only do offline selftest, or pass by default */
285 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
288 st_param[HNAE3_MAC_INTER_LOOP_MAC][0] = HNAE3_MAC_INTER_LOOP_MAC;
289 st_param[HNAE3_MAC_INTER_LOOP_MAC][1] =
290 h->flags & HNAE3_SUPPORT_MAC_LOOPBACK;
292 st_param[HNAE3_MAC_INTER_LOOP_SERDES][0] = HNAE3_MAC_INTER_LOOP_SERDES;
293 st_param[HNAE3_MAC_INTER_LOOP_SERDES][1] =
294 h->flags & HNAE3_SUPPORT_SERDES_LOOPBACK;
299 #if IS_ENABLED(CONFIG_VLAN_8021Q)
300 /* Disable the vlan filter for selftest does not support it */
301 dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
302 h->ae_algo->ops->enable_vlan_filter;
304 h->ae_algo->ops->enable_vlan_filter(h, false);
307 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
309 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
310 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
315 data[test_index] = hns3_lp_up(ndev, loop_type);
316 if (!data[test_index]) {
317 data[test_index] = hns3_lp_run_test(ndev, loop_type);
318 hns3_lp_down(ndev, loop_type);
321 if (data[test_index])
322 eth_test->flags |= ETH_TEST_FL_FAILED;
327 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
329 #if IS_ENABLED(CONFIG_VLAN_8021Q)
331 h->ae_algo->ops->enable_vlan_filter(h, true);
338 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
340 struct hnae3_handle *h = hns3_get_handle(netdev);
341 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
343 if (!ops->get_sset_count)
348 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
349 ops->get_sset_count(h, stringset));
352 return ops->get_sset_count(h, stringset);
358 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
359 u32 stat_count, u32 num_tqps, const char *prefix)
361 #define MAX_PREFIX_SIZE (6 + 4)
366 for (i = 0; i < num_tqps; i++) {
367 for (j = 0; j < stat_count; j++) {
368 data[ETH_GSTRING_LEN - 1] = '\0';
370 /* first, prepend the prefix string */
371 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s#%d_",
373 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
374 size_left = (ETH_GSTRING_LEN - 1) - n1;
376 /* now, concatenate the stats string to it */
377 strncat(data, stats[j].stats_string, size_left);
378 data += ETH_GSTRING_LEN;
385 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
387 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
388 const char tx_prefix[] = "txq";
389 const char rx_prefix[] = "rxq";
391 /* get strings for Tx */
392 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
393 kinfo->num_tqps, tx_prefix);
395 /* get strings for Rx */
396 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
397 kinfo->num_tqps, rx_prefix);
402 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
404 struct hnae3_handle *h = hns3_get_handle(netdev);
405 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
406 char *buff = (char *)data;
408 if (!ops->get_strings)
413 buff = hns3_get_strings_tqps(h, buff);
414 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
417 ops->get_strings(h, stringset, data);
422 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
424 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
425 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
426 struct hns3_enet_ring *ring;
430 /* get stats for Tx */
431 for (i = 0; i < kinfo->num_tqps; i++) {
432 ring = nic_priv->ring_data[i].ring;
433 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
434 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
435 *data++ = *(u64 *)stat;
439 /* get stats for Rx */
440 for (i = 0; i < kinfo->num_tqps; i++) {
441 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
442 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
443 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
444 *data++ = *(u64 *)stat;
451 /* hns3_get_stats - get detail statistics.
452 * @netdev: net device
453 * @stats: statistics info.
454 * @data: statistics data.
456 static void hns3_get_stats(struct net_device *netdev,
457 struct ethtool_stats *stats, u64 *data)
459 struct hnae3_handle *h = hns3_get_handle(netdev);
462 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
463 netdev_err(netdev, "could not get any statistics\n");
467 h->ae_algo->ops->update_stats(h, &netdev->stats);
469 /* get per-queue stats */
470 p = hns3_get_stats_tqps(h, p);
472 /* get MAC & other misc hardware stats */
473 h->ae_algo->ops->get_stats(h, p);
476 static void hns3_get_drvinfo(struct net_device *netdev,
477 struct ethtool_drvinfo *drvinfo)
479 struct hns3_nic_priv *priv = netdev_priv(netdev);
480 struct hnae3_handle *h = priv->ae_handle;
482 strncpy(drvinfo->version, hns3_driver_version,
483 sizeof(drvinfo->version));
484 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
486 strncpy(drvinfo->driver, h->pdev->driver->name,
487 sizeof(drvinfo->driver));
488 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
490 strncpy(drvinfo->bus_info, pci_name(h->pdev),
491 sizeof(drvinfo->bus_info));
492 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
494 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
495 priv->ae_handle->ae_algo->ops->get_fw_version(h));
498 static u32 hns3_get_link(struct net_device *netdev)
500 struct hnae3_handle *h = hns3_get_handle(netdev);
502 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
503 return h->ae_algo->ops->get_status(h);
508 static void hns3_get_ringparam(struct net_device *netdev,
509 struct ethtool_ringparam *param)
511 struct hns3_nic_priv *priv = netdev_priv(netdev);
512 struct hnae3_handle *h = priv->ae_handle;
513 int queue_num = h->kinfo.num_tqps;
515 param->tx_max_pending = HNS3_RING_MAX_PENDING;
516 param->rx_max_pending = HNS3_RING_MAX_PENDING;
518 param->tx_pending = priv->ring_data[0].ring->desc_num;
519 param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
522 static void hns3_get_pauseparam(struct net_device *netdev,
523 struct ethtool_pauseparam *param)
525 struct hnae3_handle *h = hns3_get_handle(netdev);
527 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
528 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
529 ¶m->rx_pause, ¶m->tx_pause);
532 static int hns3_set_pauseparam(struct net_device *netdev,
533 struct ethtool_pauseparam *param)
535 struct hnae3_handle *h = hns3_get_handle(netdev);
537 if (h->ae_algo->ops->set_pauseparam)
538 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
544 static int hns3_get_link_ksettings(struct net_device *netdev,
545 struct ethtool_link_ksettings *cmd)
547 struct hnae3_handle *h = hns3_get_handle(netdev);
548 u32 flowctrl_adv = 0;
551 if (!h->ae_algo || !h->ae_algo->ops)
554 /* 1.auto_neg & speed & duplex from cmd */
555 if (netdev->phydev) {
556 phy_ethtool_ksettings_get(netdev->phydev, cmd);
561 if (h->ae_algo->ops->get_ksettings_an_result)
562 h->ae_algo->ops->get_ksettings_an_result(h,
569 link_stat = hns3_get_link(netdev);
571 cmd->base.speed = SPEED_UNKNOWN;
572 cmd->base.duplex = DUPLEX_UNKNOWN;
575 /* 2.get link mode and port type*/
576 if (h->ae_algo->ops->get_link_mode)
577 h->ae_algo->ops->get_link_mode(h,
578 cmd->link_modes.supported,
579 cmd->link_modes.advertising);
581 cmd->base.port = PORT_NONE;
582 if (h->ae_algo->ops->get_port_type)
583 h->ae_algo->ops->get_port_type(h,
586 /* 3.mdix_ctrl&mdix get from phy reg */
587 if (h->ae_algo->ops->get_mdix_mode)
588 h->ae_algo->ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
589 &cmd->base.eth_tp_mdix);
591 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
593 /* 5.get flow control setttings */
594 if (h->ae_algo->ops->get_flowctrl_adv)
595 h->ae_algo->ops->get_flowctrl_adv(h, &flowctrl_adv);
597 if (flowctrl_adv & ADVERTISED_Pause)
598 ethtool_link_ksettings_add_link_mode(cmd, advertising,
601 if (flowctrl_adv & ADVERTISED_Asym_Pause)
602 ethtool_link_ksettings_add_link_mode(cmd, advertising,
608 static int hns3_set_link_ksettings(struct net_device *netdev,
609 const struct ethtool_link_ksettings *cmd)
611 /* Only support ksettings_set for netdev with phy attached for now */
613 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
618 static u32 hns3_get_rss_key_size(struct net_device *netdev)
620 struct hnae3_handle *h = hns3_get_handle(netdev);
622 if (!h->ae_algo || !h->ae_algo->ops ||
623 !h->ae_algo->ops->get_rss_key_size)
626 return h->ae_algo->ops->get_rss_key_size(h);
629 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
631 struct hnae3_handle *h = hns3_get_handle(netdev);
633 if (!h->ae_algo || !h->ae_algo->ops ||
634 !h->ae_algo->ops->get_rss_indir_size)
637 return h->ae_algo->ops->get_rss_indir_size(h);
640 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
643 struct hnae3_handle *h = hns3_get_handle(netdev);
645 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
648 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
651 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
652 const u8 *key, const u8 hfunc)
654 struct hnae3_handle *h = hns3_get_handle(netdev);
656 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
659 /* currently we only support Toeplitz hash */
660 if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
662 "hash func not supported (only Toeplitz hash)\n");
667 "set rss failed for indir is empty\n");
671 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
674 static int hns3_get_rxnfc(struct net_device *netdev,
675 struct ethtool_rxnfc *cmd,
678 struct hnae3_handle *h = hns3_get_handle(netdev);
680 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss_tuple)
684 case ETHTOOL_GRXRINGS:
685 cmd->data = h->kinfo.rss_size;
688 return h->ae_algo->ops->get_rss_tuple(h, cmd);
696 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
699 struct hnae3_handle *h = priv->ae_handle;
702 h->kinfo.num_desc = new_desc_num;
704 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
705 priv->ring_data[i].ring->desc_num = new_desc_num;
707 return hns3_init_all_ring(priv);
710 static int hns3_set_ringparam(struct net_device *ndev,
711 struct ethtool_ringparam *param)
713 struct hns3_nic_priv *priv = netdev_priv(ndev);
714 struct hnae3_handle *h = priv->ae_handle;
715 bool if_running = netif_running(ndev);
716 u32 old_desc_num, new_desc_num;
719 if (param->rx_mini_pending || param->rx_jumbo_pending)
722 if (param->tx_pending != param->rx_pending) {
724 "Descriptors of tx and rx must be equal");
728 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
729 param->tx_pending < HNS3_RING_MIN_PENDING) {
731 "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
732 param->tx_pending, HNS3_RING_MIN_PENDING,
733 HNS3_RING_MAX_PENDING);
737 new_desc_num = param->tx_pending;
739 /* Hardware requires that its descriptors must be multiple of eight */
740 new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
741 old_desc_num = h->kinfo.num_desc;
742 if (old_desc_num == new_desc_num)
746 "Changing descriptor count from %d to %d.\n",
747 old_desc_num, new_desc_num);
752 ret = hns3_uninit_all_ring(priv);
756 ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
758 ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
761 "Revert to old bd num fail, ret=%d.\n", ret);
767 ret = dev_open(ndev);
772 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
774 struct hnae3_handle *h = hns3_get_handle(netdev);
776 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss_tuple)
781 return h->ae_algo->ops->set_rss_tuple(h, cmd);
787 static int hns3_nway_reset(struct net_device *netdev)
789 struct phy_device *phy = netdev->phydev;
791 if (!netif_running(netdev))
794 /* Only support nway_reset for netdev with phy attached for now */
798 if (phy->autoneg != AUTONEG_ENABLE)
801 return genphy_restart_aneg(phy);
804 static void hns3_get_channels(struct net_device *netdev,
805 struct ethtool_channels *ch)
807 struct hnae3_handle *h = hns3_get_handle(netdev);
809 if (h->ae_algo->ops->get_channels)
810 h->ae_algo->ops->get_channels(h, ch);
813 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
814 struct ethtool_coalesce *cmd)
816 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
817 struct hns3_nic_priv *priv = netdev_priv(netdev);
818 struct hnae3_handle *h = priv->ae_handle;
819 u16 queue_num = h->kinfo.num_tqps;
821 if (queue >= queue_num) {
823 "Invalid queue value %d! Queue max id=%d\n",
824 queue, queue_num - 1);
828 tx_vector = priv->ring_data[queue].ring->tqp_vector;
829 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
831 cmd->use_adaptive_tx_coalesce =
832 tx_vector->tx_group.coal.gl_adapt_enable;
833 cmd->use_adaptive_rx_coalesce =
834 rx_vector->rx_group.coal.gl_adapt_enable;
836 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
837 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
839 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
840 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
845 static int hns3_get_coalesce(struct net_device *netdev,
846 struct ethtool_coalesce *cmd)
848 return hns3_get_coalesce_per_queue(netdev, 0, cmd);
851 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
852 struct ethtool_coalesce *cmd)
856 if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
858 "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
863 if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
865 "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
870 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
871 if (rx_gl != cmd->rx_coalesce_usecs) {
873 "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
874 cmd->rx_coalesce_usecs, rx_gl);
877 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
878 if (tx_gl != cmd->tx_coalesce_usecs) {
880 "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
881 cmd->tx_coalesce_usecs, tx_gl);
887 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
888 struct ethtool_coalesce *cmd)
892 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
894 "tx_usecs_high must be same as rx_usecs_high.\n");
898 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
900 "Invalid usecs_high value, usecs_high range is 0-%d\n",
905 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
906 if (rl != cmd->rx_coalesce_usecs_high) {
908 "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
909 cmd->rx_coalesce_usecs_high, rl);
915 static int hns3_check_coalesce_para(struct net_device *netdev,
916 struct ethtool_coalesce *cmd)
920 ret = hns3_check_gl_coalesce_para(netdev, cmd);
923 "Check gl coalesce param fail. ret = %d\n", ret);
927 ret = hns3_check_rl_coalesce_para(netdev, cmd);
930 "Check rl coalesce param fail. ret = %d\n", ret);
934 if (cmd->use_adaptive_tx_coalesce == 1 ||
935 cmd->use_adaptive_rx_coalesce == 1) {
937 "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
938 cmd->use_adaptive_tx_coalesce,
939 cmd->use_adaptive_rx_coalesce);
945 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
946 struct ethtool_coalesce *cmd,
949 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
950 struct hns3_nic_priv *priv = netdev_priv(netdev);
951 struct hnae3_handle *h = priv->ae_handle;
952 int queue_num = h->kinfo.num_tqps;
954 tx_vector = priv->ring_data[queue].ring->tqp_vector;
955 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
957 tx_vector->tx_group.coal.gl_adapt_enable =
958 cmd->use_adaptive_tx_coalesce;
959 rx_vector->rx_group.coal.gl_adapt_enable =
960 cmd->use_adaptive_rx_coalesce;
962 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
963 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
965 hns3_set_vector_coalesce_tx_gl(tx_vector,
966 tx_vector->tx_group.coal.int_gl);
967 hns3_set_vector_coalesce_rx_gl(rx_vector,
968 rx_vector->rx_group.coal.int_gl);
970 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
971 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
974 static int hns3_set_coalesce(struct net_device *netdev,
975 struct ethtool_coalesce *cmd)
977 struct hnae3_handle *h = hns3_get_handle(netdev);
978 u16 queue_num = h->kinfo.num_tqps;
982 ret = hns3_check_coalesce_para(netdev, cmd);
986 h->kinfo.int_rl_setting =
987 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
989 for (i = 0; i < queue_num; i++)
990 hns3_set_coalesce_per_queue(netdev, cmd, i);
995 static int hns3_get_regs_len(struct net_device *netdev)
997 struct hnae3_handle *h = hns3_get_handle(netdev);
999 if (!h->ae_algo->ops->get_regs_len)
1002 return h->ae_algo->ops->get_regs_len(h);
1005 static void hns3_get_regs(struct net_device *netdev,
1006 struct ethtool_regs *cmd, void *data)
1008 struct hnae3_handle *h = hns3_get_handle(netdev);
1010 if (!h->ae_algo->ops->get_regs)
1013 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1016 static int hns3_set_phys_id(struct net_device *netdev,
1017 enum ethtool_phys_id_state state)
1019 struct hnae3_handle *h = hns3_get_handle(netdev);
1021 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id)
1024 return h->ae_algo->ops->set_led_id(h, state);
1027 static const struct ethtool_ops hns3vf_ethtool_ops = {
1028 .get_drvinfo = hns3_get_drvinfo,
1029 .get_ringparam = hns3_get_ringparam,
1030 .set_ringparam = hns3_set_ringparam,
1031 .get_strings = hns3_get_strings,
1032 .get_ethtool_stats = hns3_get_stats,
1033 .get_sset_count = hns3_get_sset_count,
1034 .get_rxnfc = hns3_get_rxnfc,
1035 .get_rxfh_key_size = hns3_get_rss_key_size,
1036 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1037 .get_rxfh = hns3_get_rss,
1038 .set_rxfh = hns3_set_rss,
1039 .get_link_ksettings = hns3_get_link_ksettings,
1040 .get_channels = hns3_get_channels,
1041 .get_coalesce = hns3_get_coalesce,
1042 .set_coalesce = hns3_set_coalesce,
1043 .get_link = hns3_get_link,
1046 static const struct ethtool_ops hns3_ethtool_ops = {
1047 .self_test = hns3_self_test,
1048 .get_drvinfo = hns3_get_drvinfo,
1049 .get_link = hns3_get_link,
1050 .get_ringparam = hns3_get_ringparam,
1051 .set_ringparam = hns3_set_ringparam,
1052 .get_pauseparam = hns3_get_pauseparam,
1053 .set_pauseparam = hns3_set_pauseparam,
1054 .get_strings = hns3_get_strings,
1055 .get_ethtool_stats = hns3_get_stats,
1056 .get_sset_count = hns3_get_sset_count,
1057 .get_rxnfc = hns3_get_rxnfc,
1058 .set_rxnfc = hns3_set_rxnfc,
1059 .get_rxfh_key_size = hns3_get_rss_key_size,
1060 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1061 .get_rxfh = hns3_get_rss,
1062 .set_rxfh = hns3_set_rss,
1063 .get_link_ksettings = hns3_get_link_ksettings,
1064 .set_link_ksettings = hns3_set_link_ksettings,
1065 .nway_reset = hns3_nway_reset,
1066 .get_channels = hns3_get_channels,
1067 .set_channels = hns3_set_channels,
1068 .get_coalesce = hns3_get_coalesce,
1069 .set_coalesce = hns3_set_coalesce,
1070 .get_regs_len = hns3_get_regs_len,
1071 .get_regs = hns3_get_regs,
1072 .set_phys_id = hns3_set_phys_id,
1075 void hns3_ethtool_set_ops(struct net_device *netdev)
1077 struct hnae3_handle *h = hns3_get_handle(netdev);
1079 if (h->flags & HNAE3_SUPPORT_VF)
1080 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1082 netdev->ethtool_ops = &hns3_ethtool_ops;