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);
76 if (!h->ae_algo->ops->set_loopback ||
77 !h->ae_algo->ops->set_promisc_mode)
81 case HNAE3_LOOP_SERIAL_SERDES:
82 case HNAE3_LOOP_PARALLEL_SERDES:
84 ret = h->ae_algo->ops->set_loopback(h, loop, en);
94 h->ae_algo->ops->set_promisc_mode(h, en, en);
99 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
101 struct hnae3_handle *h = hns3_get_handle(ndev);
104 ret = hns3_nic_reset_all_ring(h);
108 ret = hns3_lp_setup(ndev, loop_mode, true);
109 usleep_range(10000, 20000);
114 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
118 ret = hns3_lp_setup(ndev, loop_mode, false);
120 netdev_err(ndev, "lb_setup return error: %d\n", ret);
124 usleep_range(10000, 20000);
129 static void hns3_lp_setup_skb(struct sk_buff *skb)
131 struct net_device *ndev = skb->dev;
132 unsigned char *packet;
136 skb_reserve(skb, NET_IP_ALIGN);
137 ethh = skb_put(skb, sizeof(struct ethhdr));
138 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
140 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
141 ethh->h_dest[5] += 0x1f;
142 eth_zero_addr(ethh->h_source);
143 ethh->h_proto = htons(ETH_P_ARP);
144 skb_reset_mac_header(skb);
146 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
147 packet[i] = (unsigned char)(i & 0xff);
150 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
153 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
154 unsigned char *packet = skb->data;
157 for (i = 0; i < skb->len; i++)
158 if (packet[i] != (unsigned char)(i & 0xff))
161 /* The packet is correctly received */
163 tqp_vector->rx_group.total_packets++;
165 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
166 skb->data, skb->len, true);
168 dev_kfree_skb_any(skb);
171 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
173 struct hnae3_handle *h = priv->ae_handle;
174 struct hnae3_knic_private_info *kinfo;
175 u32 i, rcv_good_pkt_total = 0;
178 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
179 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
180 struct hns3_enet_ring_group *rx_group;
183 rx_group = &ring->tqp_vector->rx_group;
184 pre_rx_pkt = rx_group->total_packets;
187 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
190 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
191 rx_group->total_packets = pre_rx_pkt;
193 return rcv_good_pkt_total;
196 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
197 u32 end_ringid, u32 budget)
201 for (i = start_ringid; i <= end_ringid; i++) {
202 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
204 hns3_clean_tx_ring(ring);
209 * hns3_lp_run_test - run loopback test
211 * @mode: loopback type
213 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
215 struct hns3_nic_priv *priv = netdev_priv(ndev);
220 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
223 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
226 hns3_lp_setup_skb(skb);
227 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
230 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
234 tx_ret = hns3_nic_net_xmit(skb, ndev);
235 if (tx_ret == NETDEV_TX_OK)
238 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
241 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
242 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
243 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
244 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
248 /* Allow 200 milliseconds for packets to go from Tx to Rx */
251 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
252 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
253 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
254 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
255 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
259 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
260 HNS3_NIC_LB_TEST_RING_ID,
261 HNS3_NIC_LB_TEST_PKT_NUM);
268 * hns3_nic_self_test - self test
270 * @eth_test: test cmd
273 static void hns3_self_test(struct net_device *ndev,
274 struct ethtool_test *eth_test, u64 *data)
276 struct hns3_nic_priv *priv = netdev_priv(ndev);
277 struct hnae3_handle *h = priv->ae_handle;
278 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
279 bool if_running = netif_running(ndev);
280 #if IS_ENABLED(CONFIG_VLAN_8021Q)
281 bool dis_vlan_filter;
286 /* Only do offline selftest, or pass by default */
287 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
290 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
291 st_param[HNAE3_LOOP_APP][1] =
292 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
294 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
295 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
296 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
298 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
299 HNAE3_LOOP_PARALLEL_SERDES;
300 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
301 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
304 ndev->netdev_ops->ndo_stop(ndev);
306 #if IS_ENABLED(CONFIG_VLAN_8021Q)
307 /* Disable the vlan filter for selftest does not support it */
308 dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
309 h->ae_algo->ops->enable_vlan_filter;
311 h->ae_algo->ops->enable_vlan_filter(h, false);
314 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
316 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
317 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
322 data[test_index] = hns3_lp_up(ndev, loop_type);
323 if (!data[test_index]) {
324 data[test_index] = hns3_lp_run_test(ndev, loop_type);
325 hns3_lp_down(ndev, loop_type);
328 if (data[test_index])
329 eth_test->flags |= ETH_TEST_FL_FAILED;
334 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
336 #if IS_ENABLED(CONFIG_VLAN_8021Q)
338 h->ae_algo->ops->enable_vlan_filter(h, true);
342 ndev->netdev_ops->ndo_open(ndev);
345 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
347 struct hnae3_handle *h = hns3_get_handle(netdev);
348 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
350 if (!ops->get_sset_count)
355 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
356 ops->get_sset_count(h, stringset));
359 return ops->get_sset_count(h, stringset);
366 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
367 u32 stat_count, u32 num_tqps, const char *prefix)
369 #define MAX_PREFIX_SIZE (6 + 4)
374 for (i = 0; i < num_tqps; i++) {
375 for (j = 0; j < stat_count; j++) {
376 data[ETH_GSTRING_LEN - 1] = '\0';
378 /* first, prepend the prefix string */
379 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s%d_",
381 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
382 size_left = (ETH_GSTRING_LEN - 1) - n1;
384 /* now, concatenate the stats string to it */
385 strncat(data, stats[j].stats_string, size_left);
386 data += ETH_GSTRING_LEN;
393 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
395 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
396 const char tx_prefix[] = "txq";
397 const char rx_prefix[] = "rxq";
399 /* get strings for Tx */
400 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
401 kinfo->num_tqps, tx_prefix);
403 /* get strings for Rx */
404 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
405 kinfo->num_tqps, rx_prefix);
410 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
412 struct hnae3_handle *h = hns3_get_handle(netdev);
413 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
414 char *buff = (char *)data;
416 if (!ops->get_strings)
421 buff = hns3_get_strings_tqps(h, buff);
422 h->ae_algo->ops->get_strings(h, stringset, (u8 *)buff);
425 ops->get_strings(h, stringset, data);
432 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
434 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
435 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
436 struct hns3_enet_ring *ring;
440 /* get stats for Tx */
441 for (i = 0; i < kinfo->num_tqps; i++) {
442 ring = nic_priv->ring_data[i].ring;
443 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
444 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
445 *data++ = *(u64 *)stat;
449 /* get stats for Rx */
450 for (i = 0; i < kinfo->num_tqps; i++) {
451 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
452 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
453 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
454 *data++ = *(u64 *)stat;
461 /* hns3_get_stats - get detail statistics.
462 * @netdev: net device
463 * @stats: statistics info.
464 * @data: statistics data.
466 static void hns3_get_stats(struct net_device *netdev,
467 struct ethtool_stats *stats, u64 *data)
469 struct hnae3_handle *h = hns3_get_handle(netdev);
472 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
473 netdev_err(netdev, "could not get any statistics\n");
477 h->ae_algo->ops->update_stats(h, &netdev->stats);
479 /* get per-queue stats */
480 p = hns3_get_stats_tqps(h, p);
482 /* get MAC & other misc hardware stats */
483 h->ae_algo->ops->get_stats(h, p);
486 static void hns3_get_drvinfo(struct net_device *netdev,
487 struct ethtool_drvinfo *drvinfo)
489 struct hns3_nic_priv *priv = netdev_priv(netdev);
490 struct hnae3_handle *h = priv->ae_handle;
492 strncpy(drvinfo->version, hns3_driver_version,
493 sizeof(drvinfo->version));
494 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
496 strncpy(drvinfo->driver, h->pdev->driver->name,
497 sizeof(drvinfo->driver));
498 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
500 strncpy(drvinfo->bus_info, pci_name(h->pdev),
501 sizeof(drvinfo->bus_info));
502 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
504 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "0x%08x",
505 priv->ae_handle->ae_algo->ops->get_fw_version(h));
508 static u32 hns3_get_link(struct net_device *netdev)
510 struct hnae3_handle *h = hns3_get_handle(netdev);
512 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_status)
513 return h->ae_algo->ops->get_status(h);
518 static void hns3_get_ringparam(struct net_device *netdev,
519 struct ethtool_ringparam *param)
521 struct hns3_nic_priv *priv = netdev_priv(netdev);
522 struct hnae3_handle *h = priv->ae_handle;
523 int queue_num = h->kinfo.num_tqps;
525 param->tx_max_pending = HNS3_RING_MAX_PENDING;
526 param->rx_max_pending = HNS3_RING_MAX_PENDING;
528 param->tx_pending = priv->ring_data[0].ring->desc_num;
529 param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
532 static void hns3_get_pauseparam(struct net_device *netdev,
533 struct ethtool_pauseparam *param)
535 struct hnae3_handle *h = hns3_get_handle(netdev);
537 if (h->ae_algo && h->ae_algo->ops && h->ae_algo->ops->get_pauseparam)
538 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
539 ¶m->rx_pause, ¶m->tx_pause);
542 static int hns3_set_pauseparam(struct net_device *netdev,
543 struct ethtool_pauseparam *param)
545 struct hnae3_handle *h = hns3_get_handle(netdev);
547 if (h->ae_algo->ops->set_pauseparam)
548 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
554 static void hns3_get_ksettings(struct hnae3_handle *h,
555 struct ethtool_link_ksettings *cmd)
557 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
559 /* 1.auto_neg & speed & duplex from cmd */
560 if (ops->get_ksettings_an_result)
561 ops->get_ksettings_an_result(h,
567 if (ops->get_link_mode)
568 ops->get_link_mode(h,
569 cmd->link_modes.supported,
570 cmd->link_modes.advertising);
572 /* 3.mdix_ctrl&mdix get from phy reg */
573 if (ops->get_mdix_mode)
574 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
575 &cmd->base.eth_tp_mdix);
578 static int hns3_get_link_ksettings(struct net_device *netdev,
579 struct ethtool_link_ksettings *cmd)
581 struct hnae3_handle *h = hns3_get_handle(netdev);
582 const struct hnae3_ae_ops *ops;
586 if (!h->ae_algo || !h->ae_algo->ops)
589 ops = h->ae_algo->ops;
590 if (ops->get_media_type)
591 ops->get_media_type(h, &media_type);
595 switch (media_type) {
596 case HNAE3_MEDIA_TYPE_NONE:
597 cmd->base.port = PORT_NONE;
598 hns3_get_ksettings(h, cmd);
600 case HNAE3_MEDIA_TYPE_FIBER:
601 cmd->base.port = PORT_FIBRE;
602 hns3_get_ksettings(h, cmd);
604 case HNAE3_MEDIA_TYPE_COPPER:
608 cmd->base.port = PORT_TP;
609 phy_ethtool_ksettings_get(netdev->phydev, cmd);
614 netdev_warn(netdev, "Unknown media type");
619 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
621 link_stat = hns3_get_link(netdev);
623 cmd->base.speed = SPEED_UNKNOWN;
624 cmd->base.duplex = DUPLEX_UNKNOWN;
630 static int hns3_set_link_ksettings(struct net_device *netdev,
631 const struct ethtool_link_ksettings *cmd)
633 /* Only support ksettings_set for netdev with phy attached for now */
635 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
640 static u32 hns3_get_rss_key_size(struct net_device *netdev)
642 struct hnae3_handle *h = hns3_get_handle(netdev);
644 if (!h->ae_algo || !h->ae_algo->ops ||
645 !h->ae_algo->ops->get_rss_key_size)
648 return h->ae_algo->ops->get_rss_key_size(h);
651 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
653 struct hnae3_handle *h = hns3_get_handle(netdev);
655 if (!h->ae_algo || !h->ae_algo->ops ||
656 !h->ae_algo->ops->get_rss_indir_size)
659 return h->ae_algo->ops->get_rss_indir_size(h);
662 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
665 struct hnae3_handle *h = hns3_get_handle(netdev);
667 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->get_rss)
670 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
673 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
674 const u8 *key, const u8 hfunc)
676 struct hnae3_handle *h = hns3_get_handle(netdev);
678 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_rss)
681 /* currently we only support Toeplitz hash */
682 if ((hfunc != ETH_RSS_HASH_NO_CHANGE) && (hfunc != ETH_RSS_HASH_TOP)) {
684 "hash func not supported (only Toeplitz hash)\n");
689 "set rss failed for indir is empty\n");
693 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
696 static int hns3_get_rxnfc(struct net_device *netdev,
697 struct ethtool_rxnfc *cmd,
700 struct hnae3_handle *h = hns3_get_handle(netdev);
702 if (!h->ae_algo || !h->ae_algo->ops)
706 case ETHTOOL_GRXRINGS:
707 cmd->data = h->kinfo.num_tqps;
710 if (h->ae_algo->ops->get_rss_tuple)
711 return h->ae_algo->ops->get_rss_tuple(h, cmd);
713 case ETHTOOL_GRXCLSRLCNT:
714 if (h->ae_algo->ops->get_fd_rule_cnt)
715 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
717 case ETHTOOL_GRXCLSRULE:
718 if (h->ae_algo->ops->get_fd_rule_info)
719 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
721 case ETHTOOL_GRXCLSRLALL:
722 if (h->ae_algo->ops->get_fd_all_rules)
723 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
731 static int hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
734 struct hnae3_handle *h = priv->ae_handle;
737 h->kinfo.num_desc = new_desc_num;
739 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
740 priv->ring_data[i].ring->desc_num = new_desc_num;
742 return hns3_init_all_ring(priv);
745 static int hns3_set_ringparam(struct net_device *ndev,
746 struct ethtool_ringparam *param)
748 struct hns3_nic_priv *priv = netdev_priv(ndev);
749 struct hnae3_handle *h = priv->ae_handle;
750 bool if_running = netif_running(ndev);
751 u32 old_desc_num, new_desc_num;
754 if (param->rx_mini_pending || param->rx_jumbo_pending)
757 if (param->tx_pending != param->rx_pending) {
759 "Descriptors of tx and rx must be equal");
763 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
764 param->tx_pending < HNS3_RING_MIN_PENDING) {
766 "Descriptors requested (Tx/Rx: %d) out of range [%d-%d]\n",
767 param->tx_pending, HNS3_RING_MIN_PENDING,
768 HNS3_RING_MAX_PENDING);
772 new_desc_num = param->tx_pending;
774 /* Hardware requires that its descriptors must be multiple of eight */
775 new_desc_num = ALIGN(new_desc_num, HNS3_RING_BD_MULTIPLE);
776 old_desc_num = h->kinfo.num_desc;
777 if (old_desc_num == new_desc_num)
781 "Changing descriptor count from %d to %d.\n",
782 old_desc_num, new_desc_num);
787 ret = hns3_uninit_all_ring(priv);
791 ret = hns3_change_all_ring_bd_num(priv, new_desc_num);
793 ret = hns3_change_all_ring_bd_num(priv, old_desc_num);
796 "Revert to old bd num fail, ret=%d.\n", ret);
802 ret = dev_open(ndev);
807 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
809 struct hnae3_handle *h = hns3_get_handle(netdev);
811 if (!h->ae_algo || !h->ae_algo->ops)
816 if (h->ae_algo->ops->set_rss_tuple)
817 return h->ae_algo->ops->set_rss_tuple(h, cmd);
819 case ETHTOOL_SRXCLSRLINS:
820 if (h->ae_algo->ops->add_fd_entry)
821 return h->ae_algo->ops->add_fd_entry(h, cmd);
823 case ETHTOOL_SRXCLSRLDEL:
824 if (h->ae_algo->ops->del_fd_entry)
825 return h->ae_algo->ops->del_fd_entry(h, cmd);
832 static int hns3_nway_reset(struct net_device *netdev)
834 struct phy_device *phy = netdev->phydev;
836 if (!netif_running(netdev))
839 /* Only support nway_reset for netdev with phy attached for now */
843 if (phy->autoneg != AUTONEG_ENABLE)
846 return genphy_restart_aneg(phy);
849 static void hns3_get_channels(struct net_device *netdev,
850 struct ethtool_channels *ch)
852 struct hnae3_handle *h = hns3_get_handle(netdev);
854 if (h->ae_algo->ops->get_channels)
855 h->ae_algo->ops->get_channels(h, ch);
858 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
859 struct ethtool_coalesce *cmd)
861 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
862 struct hns3_nic_priv *priv = netdev_priv(netdev);
863 struct hnae3_handle *h = priv->ae_handle;
864 u16 queue_num = h->kinfo.num_tqps;
866 if (queue >= queue_num) {
868 "Invalid queue value %d! Queue max id=%d\n",
869 queue, queue_num - 1);
873 tx_vector = priv->ring_data[queue].ring->tqp_vector;
874 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
876 cmd->use_adaptive_tx_coalesce =
877 tx_vector->tx_group.coal.gl_adapt_enable;
878 cmd->use_adaptive_rx_coalesce =
879 rx_vector->rx_group.coal.gl_adapt_enable;
881 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
882 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
884 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
885 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
890 static int hns3_get_coalesce(struct net_device *netdev,
891 struct ethtool_coalesce *cmd)
893 return hns3_get_coalesce_per_queue(netdev, 0, cmd);
896 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
897 struct ethtool_coalesce *cmd)
901 if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
903 "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
908 if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
910 "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
915 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
916 if (rx_gl != cmd->rx_coalesce_usecs) {
918 "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
919 cmd->rx_coalesce_usecs, rx_gl);
922 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
923 if (tx_gl != cmd->tx_coalesce_usecs) {
925 "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
926 cmd->tx_coalesce_usecs, tx_gl);
932 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
933 struct ethtool_coalesce *cmd)
937 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
939 "tx_usecs_high must be same as rx_usecs_high.\n");
943 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
945 "Invalid usecs_high value, usecs_high range is 0-%d\n",
950 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
951 if (rl != cmd->rx_coalesce_usecs_high) {
953 "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
954 cmd->rx_coalesce_usecs_high, rl);
960 static int hns3_check_coalesce_para(struct net_device *netdev,
961 struct ethtool_coalesce *cmd)
965 ret = hns3_check_gl_coalesce_para(netdev, cmd);
968 "Check gl coalesce param fail. ret = %d\n", ret);
972 ret = hns3_check_rl_coalesce_para(netdev, cmd);
975 "Check rl coalesce param fail. ret = %d\n", ret);
979 if (cmd->use_adaptive_tx_coalesce == 1 ||
980 cmd->use_adaptive_rx_coalesce == 1) {
982 "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
983 cmd->use_adaptive_tx_coalesce,
984 cmd->use_adaptive_rx_coalesce);
990 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
991 struct ethtool_coalesce *cmd,
994 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
995 struct hns3_nic_priv *priv = netdev_priv(netdev);
996 struct hnae3_handle *h = priv->ae_handle;
997 int queue_num = h->kinfo.num_tqps;
999 tx_vector = priv->ring_data[queue].ring->tqp_vector;
1000 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
1002 tx_vector->tx_group.coal.gl_adapt_enable =
1003 cmd->use_adaptive_tx_coalesce;
1004 rx_vector->rx_group.coal.gl_adapt_enable =
1005 cmd->use_adaptive_rx_coalesce;
1007 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1008 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1010 hns3_set_vector_coalesce_tx_gl(tx_vector,
1011 tx_vector->tx_group.coal.int_gl);
1012 hns3_set_vector_coalesce_rx_gl(rx_vector,
1013 rx_vector->rx_group.coal.int_gl);
1015 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1016 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1019 static int hns3_set_coalesce(struct net_device *netdev,
1020 struct ethtool_coalesce *cmd)
1022 struct hnae3_handle *h = hns3_get_handle(netdev);
1023 u16 queue_num = h->kinfo.num_tqps;
1027 ret = hns3_check_coalesce_para(netdev, cmd);
1031 h->kinfo.int_rl_setting =
1032 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1034 for (i = 0; i < queue_num; i++)
1035 hns3_set_coalesce_per_queue(netdev, cmd, i);
1040 static int hns3_get_regs_len(struct net_device *netdev)
1042 struct hnae3_handle *h = hns3_get_handle(netdev);
1044 if (!h->ae_algo->ops->get_regs_len)
1047 return h->ae_algo->ops->get_regs_len(h);
1050 static void hns3_get_regs(struct net_device *netdev,
1051 struct ethtool_regs *cmd, void *data)
1053 struct hnae3_handle *h = hns3_get_handle(netdev);
1055 if (!h->ae_algo->ops->get_regs)
1058 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1061 static int hns3_set_phys_id(struct net_device *netdev,
1062 enum ethtool_phys_id_state state)
1064 struct hnae3_handle *h = hns3_get_handle(netdev);
1066 if (!h->ae_algo || !h->ae_algo->ops || !h->ae_algo->ops->set_led_id)
1069 return h->ae_algo->ops->set_led_id(h, state);
1072 static const struct ethtool_ops hns3vf_ethtool_ops = {
1073 .get_drvinfo = hns3_get_drvinfo,
1074 .get_ringparam = hns3_get_ringparam,
1075 .set_ringparam = hns3_set_ringparam,
1076 .get_strings = hns3_get_strings,
1077 .get_ethtool_stats = hns3_get_stats,
1078 .get_sset_count = hns3_get_sset_count,
1079 .get_rxnfc = hns3_get_rxnfc,
1080 .get_rxfh_key_size = hns3_get_rss_key_size,
1081 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1082 .get_rxfh = hns3_get_rss,
1083 .set_rxfh = hns3_set_rss,
1084 .get_link_ksettings = hns3_get_link_ksettings,
1085 .get_channels = hns3_get_channels,
1086 .get_coalesce = hns3_get_coalesce,
1087 .set_coalesce = hns3_set_coalesce,
1088 .get_link = hns3_get_link,
1091 static const struct ethtool_ops hns3_ethtool_ops = {
1092 .self_test = hns3_self_test,
1093 .get_drvinfo = hns3_get_drvinfo,
1094 .get_link = hns3_get_link,
1095 .get_ringparam = hns3_get_ringparam,
1096 .set_ringparam = hns3_set_ringparam,
1097 .get_pauseparam = hns3_get_pauseparam,
1098 .set_pauseparam = hns3_set_pauseparam,
1099 .get_strings = hns3_get_strings,
1100 .get_ethtool_stats = hns3_get_stats,
1101 .get_sset_count = hns3_get_sset_count,
1102 .get_rxnfc = hns3_get_rxnfc,
1103 .set_rxnfc = hns3_set_rxnfc,
1104 .get_rxfh_key_size = hns3_get_rss_key_size,
1105 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1106 .get_rxfh = hns3_get_rss,
1107 .set_rxfh = hns3_set_rss,
1108 .get_link_ksettings = hns3_get_link_ksettings,
1109 .set_link_ksettings = hns3_set_link_ksettings,
1110 .nway_reset = hns3_nway_reset,
1111 .get_channels = hns3_get_channels,
1112 .set_channels = hns3_set_channels,
1113 .get_coalesce = hns3_get_coalesce,
1114 .set_coalesce = hns3_set_coalesce,
1115 .get_regs_len = hns3_get_regs_len,
1116 .get_regs = hns3_get_regs,
1117 .set_phys_id = hns3_set_phys_id,
1120 void hns3_ethtool_set_ops(struct net_device *netdev)
1122 struct hnae3_handle *h = hns3_get_handle(netdev);
1124 if (h->flags & HNAE3_SUPPORT_VF)
1125 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1127 netdev->ethtool_ops = &hns3_ethtool_ops;