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),
32 HNS3_TQP_STAT("copy", tx_copy),
33 HNS3_TQP_STAT("vlan_err", tx_vlan_err),
34 HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
35 HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
36 HNS3_TQP_STAT("tso_err", tx_tso_err),
39 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
41 static const struct hns3_stats hns3_rxq_stats[] = {
42 /* Rx per-queue statistics */
43 HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
44 HNS3_TQP_STAT("dropped", sw_err_cnt),
45 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
46 HNS3_TQP_STAT("packets", rx_pkts),
47 HNS3_TQP_STAT("bytes", rx_bytes),
48 HNS3_TQP_STAT("errors", rx_err_cnt),
49 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
50 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
51 HNS3_TQP_STAT("err_bd_num", err_bd_num),
52 HNS3_TQP_STAT("l2_err", l2_err),
53 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
54 HNS3_TQP_STAT("multicast", rx_multicast),
55 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
58 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
60 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
62 #define HNS3_SELF_TEST_TYPE_NUM 4
63 #define HNS3_NIC_LB_TEST_PKT_NUM 1
64 #define HNS3_NIC_LB_TEST_RING_ID 0
65 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
66 #define HNS3_NIC_LB_SETUP_USEC 10000
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 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
75 struct hnae3_handle *h = hns3_get_handle(ndev);
76 bool vlan_filter_enable;
79 if (!h->ae_algo->ops->set_loopback ||
80 !h->ae_algo->ops->set_promisc_mode)
84 case HNAE3_LOOP_SERIAL_SERDES:
85 case HNAE3_LOOP_PARALLEL_SERDES:
88 ret = h->ae_algo->ops->set_loopback(h, loop, en);
95 if (ret || h->pdev->revision >= 0x21)
99 h->ae_algo->ops->set_promisc_mode(h, true, true);
101 /* recover promisc mode before loopback test */
102 hns3_update_promisc_mode(ndev, h->netdev_flags);
103 vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
104 hns3_enable_vlan_filter(ndev, vlan_filter_enable);
110 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
112 struct hnae3_handle *h = hns3_get_handle(ndev);
115 ret = hns3_nic_reset_all_ring(h);
119 ret = hns3_lp_setup(ndev, loop_mode, true);
120 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
125 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
129 ret = hns3_lp_setup(ndev, loop_mode, false);
131 netdev_err(ndev, "lb_setup return error: %d\n", ret);
135 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
140 static void hns3_lp_setup_skb(struct sk_buff *skb)
142 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
144 struct net_device *ndev = skb->dev;
145 struct hnae3_handle *handle;
146 unsigned char *packet;
150 skb_reserve(skb, NET_IP_ALIGN);
151 ethh = skb_put(skb, sizeof(struct ethhdr));
152 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
154 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
156 /* The dst mac addr of loopback packet is the same as the host'
157 * mac addr, the SSU component may loop back the packet to host
158 * before the packet reaches mac or serdes, which will defect
159 * the purpose of mac or serdes selftest.
161 handle = hns3_get_handle(ndev);
162 if (handle->pdev->revision == 0x20)
163 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
164 eth_zero_addr(ethh->h_source);
165 ethh->h_proto = htons(ETH_P_ARP);
166 skb_reset_mac_header(skb);
168 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
169 packet[i] = (unsigned char)(i & 0xff);
172 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
175 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
176 unsigned char *packet = skb->data;
179 for (i = 0; i < skb->len; i++)
180 if (packet[i] != (unsigned char)(i & 0xff))
183 /* The packet is correctly received */
185 tqp_vector->rx_group.total_packets++;
187 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
188 skb->data, skb->len, true);
190 dev_kfree_skb_any(skb);
193 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
195 struct hnae3_handle *h = priv->ae_handle;
196 struct hnae3_knic_private_info *kinfo;
197 u32 i, rcv_good_pkt_total = 0;
200 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
201 struct hns3_enet_ring *ring = &priv->ring[i];
202 struct hns3_enet_ring_group *rx_group;
205 rx_group = &ring->tqp_vector->rx_group;
206 pre_rx_pkt = rx_group->total_packets;
209 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
212 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
213 rx_group->total_packets = pre_rx_pkt;
215 return rcv_good_pkt_total;
218 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
219 u32 end_ringid, u32 budget)
223 for (i = start_ringid; i <= end_ringid; i++) {
224 struct hns3_enet_ring *ring = &priv->ring[i];
226 hns3_clean_tx_ring(ring);
231 * hns3_lp_run_test - run loopback test
233 * @mode: loopback type
235 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
237 struct hns3_nic_priv *priv = netdev_priv(ndev);
242 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
245 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
248 hns3_lp_setup_skb(skb);
249 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
252 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
256 tx_ret = hns3_nic_net_xmit(skb, ndev);
257 if (tx_ret == NETDEV_TX_OK) {
261 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
265 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
266 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
267 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
268 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
272 /* Allow 200 milliseconds for packets to go from Tx to Rx */
275 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
276 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
277 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
278 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
279 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
283 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
284 HNS3_NIC_LB_TEST_RING_ID,
285 HNS3_NIC_LB_TEST_PKT_NUM);
292 * hns3_nic_self_test - self test
294 * @eth_test: test cmd
297 static void hns3_self_test(struct net_device *ndev,
298 struct ethtool_test *eth_test, u64 *data)
300 struct hns3_nic_priv *priv = netdev_priv(ndev);
301 struct hnae3_handle *h = priv->ae_handle;
302 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
303 bool if_running = netif_running(ndev);
304 #if IS_ENABLED(CONFIG_VLAN_8021Q)
305 bool dis_vlan_filter;
310 if (hns3_nic_resetting(ndev)) {
311 netdev_err(ndev, "dev resetting!");
315 /* Only do offline selftest, or pass by default */
316 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
319 netif_dbg(h, drv, ndev, "self test start");
321 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
322 st_param[HNAE3_LOOP_APP][1] =
323 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
325 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
326 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
327 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
329 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
330 HNAE3_LOOP_PARALLEL_SERDES;
331 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
332 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
334 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
335 st_param[HNAE3_LOOP_PHY][1] =
336 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
339 ndev->netdev_ops->ndo_stop(ndev);
341 #if IS_ENABLED(CONFIG_VLAN_8021Q)
342 /* Disable the vlan filter for selftest does not support it */
343 dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
344 h->ae_algo->ops->enable_vlan_filter;
346 h->ae_algo->ops->enable_vlan_filter(h, false);
349 /* Tell firmware to stop mac autoneg before loopback test start,
350 * otherwise loopback test may be failed when the port is still
353 if (h->ae_algo->ops->halt_autoneg)
354 h->ae_algo->ops->halt_autoneg(h, true);
356 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
358 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
359 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
364 data[test_index] = hns3_lp_up(ndev, loop_type);
365 if (!data[test_index])
366 data[test_index] = hns3_lp_run_test(ndev, loop_type);
368 hns3_lp_down(ndev, loop_type);
370 if (data[test_index])
371 eth_test->flags |= ETH_TEST_FL_FAILED;
376 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
378 if (h->ae_algo->ops->halt_autoneg)
379 h->ae_algo->ops->halt_autoneg(h, false);
381 #if IS_ENABLED(CONFIG_VLAN_8021Q)
383 h->ae_algo->ops->enable_vlan_filter(h, true);
387 ndev->netdev_ops->ndo_open(ndev);
389 netif_dbg(h, drv, ndev, "self test end\n");
392 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
394 struct hnae3_handle *h = hns3_get_handle(netdev);
395 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
397 if (!ops->get_sset_count)
402 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
403 ops->get_sset_count(h, stringset));
406 return ops->get_sset_count(h, stringset);
413 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
414 u32 stat_count, u32 num_tqps, const char *prefix)
416 #define MAX_PREFIX_SIZE (6 + 4)
421 for (i = 0; i < num_tqps; i++) {
422 for (j = 0; j < stat_count; j++) {
423 data[ETH_GSTRING_LEN - 1] = '\0';
425 /* first, prepend the prefix string */
426 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s%d_",
428 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
429 size_left = (ETH_GSTRING_LEN - 1) - n1;
431 /* now, concatenate the stats string to it */
432 strncat(data, stats[j].stats_string, size_left);
433 data += ETH_GSTRING_LEN;
440 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
442 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
443 const char tx_prefix[] = "txq";
444 const char rx_prefix[] = "rxq";
446 /* get strings for Tx */
447 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
448 kinfo->num_tqps, tx_prefix);
450 /* get strings for Rx */
451 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
452 kinfo->num_tqps, rx_prefix);
457 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
459 struct hnae3_handle *h = hns3_get_handle(netdev);
460 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
461 char *buff = (char *)data;
463 if (!ops->get_strings)
468 buff = hns3_get_strings_tqps(h, buff);
469 ops->get_strings(h, stringset, (u8 *)buff);
472 ops->get_strings(h, stringset, data);
479 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
481 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
482 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
483 struct hns3_enet_ring *ring;
487 /* get stats for Tx */
488 for (i = 0; i < kinfo->num_tqps; i++) {
489 ring = &nic_priv->ring[i];
490 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
491 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
492 *data++ = *(u64 *)stat;
496 /* get stats for Rx */
497 for (i = 0; i < kinfo->num_tqps; i++) {
498 ring = &nic_priv->ring[i + kinfo->num_tqps];
499 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
500 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
501 *data++ = *(u64 *)stat;
508 /* hns3_get_stats - get detail statistics.
509 * @netdev: net device
510 * @stats: statistics info.
511 * @data: statistics data.
513 static void hns3_get_stats(struct net_device *netdev,
514 struct ethtool_stats *stats, u64 *data)
516 struct hnae3_handle *h = hns3_get_handle(netdev);
519 if (hns3_nic_resetting(netdev)) {
520 netdev_err(netdev, "dev resetting, could not get stats\n");
524 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
525 netdev_err(netdev, "could not get any statistics\n");
529 h->ae_algo->ops->update_stats(h, &netdev->stats);
531 /* get per-queue stats */
532 p = hns3_get_stats_tqps(h, p);
534 /* get MAC & other misc hardware stats */
535 h->ae_algo->ops->get_stats(h, p);
538 static void hns3_get_drvinfo(struct net_device *netdev,
539 struct ethtool_drvinfo *drvinfo)
541 struct hns3_nic_priv *priv = netdev_priv(netdev);
542 struct hnae3_handle *h = priv->ae_handle;
545 if (!h->ae_algo->ops->get_fw_version) {
546 netdev_err(netdev, "could not get fw version!\n");
550 strncpy(drvinfo->version, hns3_driver_version,
551 sizeof(drvinfo->version));
552 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
554 strncpy(drvinfo->driver, h->pdev->driver->name,
555 sizeof(drvinfo->driver));
556 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
558 strncpy(drvinfo->bus_info, pci_name(h->pdev),
559 sizeof(drvinfo->bus_info));
560 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
562 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
564 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
566 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
567 HNAE3_FW_VERSION_BYTE3_SHIFT),
568 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
569 HNAE3_FW_VERSION_BYTE2_SHIFT),
570 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
571 HNAE3_FW_VERSION_BYTE1_SHIFT),
572 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
573 HNAE3_FW_VERSION_BYTE0_SHIFT));
576 static u32 hns3_get_link(struct net_device *netdev)
578 struct hnae3_handle *h = hns3_get_handle(netdev);
580 if (h->ae_algo->ops->get_status)
581 return h->ae_algo->ops->get_status(h);
586 static void hns3_get_ringparam(struct net_device *netdev,
587 struct ethtool_ringparam *param)
589 struct hns3_nic_priv *priv = netdev_priv(netdev);
590 struct hnae3_handle *h = priv->ae_handle;
591 int queue_num = h->kinfo.num_tqps;
593 if (hns3_nic_resetting(netdev)) {
594 netdev_err(netdev, "dev resetting!");
598 param->tx_max_pending = HNS3_RING_MAX_PENDING;
599 param->rx_max_pending = HNS3_RING_MAX_PENDING;
601 param->tx_pending = priv->ring[0].desc_num;
602 param->rx_pending = priv->ring[queue_num].desc_num;
605 static void hns3_get_pauseparam(struct net_device *netdev,
606 struct ethtool_pauseparam *param)
608 struct hnae3_handle *h = hns3_get_handle(netdev);
610 if (h->ae_algo->ops->get_pauseparam)
611 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
612 ¶m->rx_pause, ¶m->tx_pause);
615 static int hns3_set_pauseparam(struct net_device *netdev,
616 struct ethtool_pauseparam *param)
618 struct hnae3_handle *h = hns3_get_handle(netdev);
620 netif_dbg(h, drv, netdev,
621 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
622 param->autoneg, param->rx_pause, param->tx_pause);
624 if (h->ae_algo->ops->set_pauseparam)
625 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
631 static void hns3_get_ksettings(struct hnae3_handle *h,
632 struct ethtool_link_ksettings *cmd)
634 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
636 /* 1.auto_neg & speed & duplex from cmd */
637 if (ops->get_ksettings_an_result)
638 ops->get_ksettings_an_result(h,
643 /* 2.get link mode */
644 if (ops->get_link_mode)
645 ops->get_link_mode(h,
646 cmd->link_modes.supported,
647 cmd->link_modes.advertising);
649 /* 3.mdix_ctrl&mdix get from phy reg */
650 if (ops->get_mdix_mode)
651 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
652 &cmd->base.eth_tp_mdix);
655 static int hns3_get_link_ksettings(struct net_device *netdev,
656 struct ethtool_link_ksettings *cmd)
658 struct hnae3_handle *h = hns3_get_handle(netdev);
659 const struct hnae3_ae_ops *ops;
664 ops = h->ae_algo->ops;
665 if (ops->get_media_type)
666 ops->get_media_type(h, &media_type, &module_type);
670 switch (media_type) {
671 case HNAE3_MEDIA_TYPE_NONE:
672 cmd->base.port = PORT_NONE;
673 hns3_get_ksettings(h, cmd);
675 case HNAE3_MEDIA_TYPE_FIBER:
676 if (module_type == HNAE3_MODULE_TYPE_CR)
677 cmd->base.port = PORT_DA;
679 cmd->base.port = PORT_FIBRE;
681 hns3_get_ksettings(h, cmd);
683 case HNAE3_MEDIA_TYPE_BACKPLANE:
684 cmd->base.port = PORT_NONE;
685 hns3_get_ksettings(h, cmd);
687 case HNAE3_MEDIA_TYPE_COPPER:
688 cmd->base.port = PORT_TP;
690 hns3_get_ksettings(h, cmd);
692 phy_ethtool_ksettings_get(netdev->phydev, cmd);
696 netdev_warn(netdev, "Unknown media type");
701 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
703 link_stat = hns3_get_link(netdev);
705 cmd->base.speed = SPEED_UNKNOWN;
706 cmd->base.duplex = DUPLEX_UNKNOWN;
712 static int hns3_check_ksettings_param(const struct net_device *netdev,
713 const struct ethtool_link_ksettings *cmd)
715 struct hnae3_handle *handle = hns3_get_handle(netdev);
716 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
717 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
718 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
724 /* hw doesn't support use specified speed and duplex to negotiate,
725 * unnecessary to check them when autoneg on.
727 if (cmd->base.autoneg)
730 if (ops->get_ksettings_an_result) {
731 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
732 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
733 cmd->base.duplex == duplex)
737 if (ops->get_media_type)
738 ops->get_media_type(handle, &media_type, &module_type);
740 if (cmd->base.duplex != DUPLEX_FULL &&
741 media_type != HNAE3_MEDIA_TYPE_COPPER) {
743 "only copper port supports half duplex!");
747 if (ops->check_port_speed) {
748 ret = ops->check_port_speed(handle, cmd->base.speed);
750 netdev_err(netdev, "unsupported speed\n");
758 static int hns3_set_link_ksettings(struct net_device *netdev,
759 const struct ethtool_link_ksettings *cmd)
761 struct hnae3_handle *handle = hns3_get_handle(netdev);
762 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
765 /* Chip don't support this mode. */
766 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
769 netif_dbg(handle, drv, netdev,
770 "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
771 netdev->phydev ? "phy" : "mac",
772 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
774 /* Only support ksettings_set for netdev with phy attached for now */
776 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
778 if (handle->pdev->revision == 0x20)
781 ret = hns3_check_ksettings_param(netdev, cmd);
785 if (ops->set_autoneg) {
786 ret = ops->set_autoneg(handle, cmd->base.autoneg);
791 /* hw doesn't support use specified speed and duplex to negotiate,
792 * ignore them when autoneg on.
794 if (cmd->base.autoneg) {
796 "autoneg is on, ignore the speed and duplex\n");
800 if (ops->cfg_mac_speed_dup_h)
801 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
807 static u32 hns3_get_rss_key_size(struct net_device *netdev)
809 struct hnae3_handle *h = hns3_get_handle(netdev);
811 if (!h->ae_algo->ops->get_rss_key_size)
814 return h->ae_algo->ops->get_rss_key_size(h);
817 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
819 struct hnae3_handle *h = hns3_get_handle(netdev);
821 if (!h->ae_algo->ops->get_rss_indir_size)
824 return h->ae_algo->ops->get_rss_indir_size(h);
827 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
830 struct hnae3_handle *h = hns3_get_handle(netdev);
832 if (!h->ae_algo->ops->get_rss)
835 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
838 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
839 const u8 *key, const u8 hfunc)
841 struct hnae3_handle *h = hns3_get_handle(netdev);
843 if (!h->ae_algo->ops->set_rss)
846 if ((h->pdev->revision == 0x20 &&
847 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
848 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
849 netdev_err(netdev, "hash func not supported\n");
855 "set rss failed for indir is empty\n");
859 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
862 static int hns3_get_rxnfc(struct net_device *netdev,
863 struct ethtool_rxnfc *cmd,
866 struct hnae3_handle *h = hns3_get_handle(netdev);
869 case ETHTOOL_GRXRINGS:
870 cmd->data = h->kinfo.num_tqps;
873 if (h->ae_algo->ops->get_rss_tuple)
874 return h->ae_algo->ops->get_rss_tuple(h, cmd);
876 case ETHTOOL_GRXCLSRLCNT:
877 if (h->ae_algo->ops->get_fd_rule_cnt)
878 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
880 case ETHTOOL_GRXCLSRULE:
881 if (h->ae_algo->ops->get_fd_rule_info)
882 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
884 case ETHTOOL_GRXCLSRLALL:
885 if (h->ae_algo->ops->get_fd_all_rules)
886 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
894 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
895 u32 tx_desc_num, u32 rx_desc_num)
897 struct hnae3_handle *h = priv->ae_handle;
900 h->kinfo.num_tx_desc = tx_desc_num;
901 h->kinfo.num_rx_desc = rx_desc_num;
903 for (i = 0; i < h->kinfo.num_tqps; i++) {
904 priv->ring[i].desc_num = tx_desc_num;
905 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
909 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
911 struct hnae3_handle *handle = priv->ae_handle;
912 struct hns3_enet_ring *tmp_rings;
915 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
916 sizeof(struct hns3_enet_ring), GFP_KERNEL);
920 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
921 memcpy(&tmp_rings[i], &priv->ring[i],
922 sizeof(struct hns3_enet_ring));
923 tmp_rings[i].skb = NULL;
929 static int hns3_check_ringparam(struct net_device *ndev,
930 struct ethtool_ringparam *param)
932 if (hns3_nic_resetting(ndev))
935 if (param->rx_mini_pending || param->rx_jumbo_pending)
938 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
939 param->tx_pending < HNS3_RING_MIN_PENDING ||
940 param->rx_pending > HNS3_RING_MAX_PENDING ||
941 param->rx_pending < HNS3_RING_MIN_PENDING) {
942 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
943 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
950 static int hns3_set_ringparam(struct net_device *ndev,
951 struct ethtool_ringparam *param)
953 struct hns3_nic_priv *priv = netdev_priv(ndev);
954 struct hnae3_handle *h = priv->ae_handle;
955 struct hns3_enet_ring *tmp_rings;
956 bool if_running = netif_running(ndev);
957 u32 old_tx_desc_num, new_tx_desc_num;
958 u32 old_rx_desc_num, new_rx_desc_num;
959 u16 queue_num = h->kinfo.num_tqps;
962 ret = hns3_check_ringparam(ndev, param);
966 /* Hardware requires that its descriptors must be multiple of eight */
967 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
968 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
969 old_tx_desc_num = priv->ring[0].desc_num;
970 old_rx_desc_num = priv->ring[queue_num].desc_num;
971 if (old_tx_desc_num == new_tx_desc_num &&
972 old_rx_desc_num == new_rx_desc_num)
975 tmp_rings = hns3_backup_ringparam(priv);
978 "backup ring param failed by allocating memory fail\n");
983 "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
984 old_tx_desc_num, old_rx_desc_num,
985 new_tx_desc_num, new_rx_desc_num);
988 ndev->netdev_ops->ndo_stop(ndev);
990 hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
991 ret = hns3_init_all_ring(priv);
993 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
996 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
998 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
999 memcpy(&priv->ring[i], &tmp_rings[i],
1000 sizeof(struct hns3_enet_ring));
1002 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1003 hns3_fini_ring(&tmp_rings[i]);
1009 ret = ndev->netdev_ops->ndo_open(ndev);
1014 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1016 struct hnae3_handle *h = hns3_get_handle(netdev);
1020 if (h->ae_algo->ops->set_rss_tuple)
1021 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1023 case ETHTOOL_SRXCLSRLINS:
1024 if (h->ae_algo->ops->add_fd_entry)
1025 return h->ae_algo->ops->add_fd_entry(h, cmd);
1027 case ETHTOOL_SRXCLSRLDEL:
1028 if (h->ae_algo->ops->del_fd_entry)
1029 return h->ae_algo->ops->del_fd_entry(h, cmd);
1036 static int hns3_nway_reset(struct net_device *netdev)
1038 struct hnae3_handle *handle = hns3_get_handle(netdev);
1039 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1040 struct phy_device *phy = netdev->phydev;
1043 if (!netif_running(netdev))
1046 if (hns3_nic_resetting(netdev)) {
1047 netdev_err(netdev, "dev resetting!");
1051 if (!ops->get_autoneg || !ops->restart_autoneg)
1054 autoneg = ops->get_autoneg(handle);
1055 if (autoneg != AUTONEG_ENABLE) {
1057 "Autoneg is off, don't support to restart it\n");
1061 netif_dbg(handle, drv, netdev,
1062 "nway reset (using %s)\n", phy ? "phy" : "mac");
1065 return genphy_restart_aneg(phy);
1067 if (handle->pdev->revision == 0x20)
1070 return ops->restart_autoneg(handle);
1073 static void hns3_get_channels(struct net_device *netdev,
1074 struct ethtool_channels *ch)
1076 struct hnae3_handle *h = hns3_get_handle(netdev);
1078 if (h->ae_algo->ops->get_channels)
1079 h->ae_algo->ops->get_channels(h, ch);
1082 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
1083 struct ethtool_coalesce *cmd)
1085 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1086 struct hns3_nic_priv *priv = netdev_priv(netdev);
1087 struct hnae3_handle *h = priv->ae_handle;
1088 u16 queue_num = h->kinfo.num_tqps;
1090 if (hns3_nic_resetting(netdev))
1093 if (queue >= queue_num) {
1095 "Invalid queue value %u! Queue max id=%u\n",
1096 queue, queue_num - 1);
1100 tx_vector = priv->ring[queue].tqp_vector;
1101 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1103 cmd->use_adaptive_tx_coalesce =
1104 tx_vector->tx_group.coal.gl_adapt_enable;
1105 cmd->use_adaptive_rx_coalesce =
1106 rx_vector->rx_group.coal.gl_adapt_enable;
1108 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
1109 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
1111 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1112 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1117 static int hns3_get_coalesce(struct net_device *netdev,
1118 struct ethtool_coalesce *cmd)
1120 return hns3_get_coalesce_per_queue(netdev, 0, cmd);
1123 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1124 struct ethtool_coalesce *cmd)
1128 if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
1130 "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
1135 if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
1137 "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
1142 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1143 if (rx_gl != cmd->rx_coalesce_usecs) {
1145 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1146 cmd->rx_coalesce_usecs, rx_gl);
1149 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1150 if (tx_gl != cmd->tx_coalesce_usecs) {
1152 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1153 cmd->tx_coalesce_usecs, tx_gl);
1159 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1160 struct ethtool_coalesce *cmd)
1164 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1166 "tx_usecs_high must be same as rx_usecs_high.\n");
1170 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1172 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1177 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1178 if (rl != cmd->rx_coalesce_usecs_high) {
1180 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1181 cmd->rx_coalesce_usecs_high, rl);
1187 static int hns3_check_coalesce_para(struct net_device *netdev,
1188 struct ethtool_coalesce *cmd)
1192 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1195 "Check gl coalesce param fail. ret = %d\n", ret);
1199 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1202 "Check rl coalesce param fail. ret = %d\n", ret);
1206 if (cmd->use_adaptive_tx_coalesce == 1 ||
1207 cmd->use_adaptive_rx_coalesce == 1) {
1209 "adaptive-tx=%u and adaptive-rx=%u, tx_usecs or rx_usecs will changed dynamically.\n",
1210 cmd->use_adaptive_tx_coalesce,
1211 cmd->use_adaptive_rx_coalesce);
1217 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1218 struct ethtool_coalesce *cmd,
1221 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1222 struct hns3_nic_priv *priv = netdev_priv(netdev);
1223 struct hnae3_handle *h = priv->ae_handle;
1224 int queue_num = h->kinfo.num_tqps;
1226 tx_vector = priv->ring[queue].tqp_vector;
1227 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1229 tx_vector->tx_group.coal.gl_adapt_enable =
1230 cmd->use_adaptive_tx_coalesce;
1231 rx_vector->rx_group.coal.gl_adapt_enable =
1232 cmd->use_adaptive_rx_coalesce;
1234 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1235 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1237 hns3_set_vector_coalesce_tx_gl(tx_vector,
1238 tx_vector->tx_group.coal.int_gl);
1239 hns3_set_vector_coalesce_rx_gl(rx_vector,
1240 rx_vector->rx_group.coal.int_gl);
1242 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1243 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1246 static int hns3_set_coalesce(struct net_device *netdev,
1247 struct ethtool_coalesce *cmd)
1249 struct hnae3_handle *h = hns3_get_handle(netdev);
1250 u16 queue_num = h->kinfo.num_tqps;
1254 if (hns3_nic_resetting(netdev))
1257 ret = hns3_check_coalesce_para(netdev, cmd);
1261 h->kinfo.int_rl_setting =
1262 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1264 for (i = 0; i < queue_num; i++)
1265 hns3_set_coalesce_per_queue(netdev, cmd, i);
1270 static int hns3_get_regs_len(struct net_device *netdev)
1272 struct hnae3_handle *h = hns3_get_handle(netdev);
1274 if (!h->ae_algo->ops->get_regs_len)
1277 return h->ae_algo->ops->get_regs_len(h);
1280 static void hns3_get_regs(struct net_device *netdev,
1281 struct ethtool_regs *cmd, void *data)
1283 struct hnae3_handle *h = hns3_get_handle(netdev);
1285 if (!h->ae_algo->ops->get_regs)
1288 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1291 static int hns3_set_phys_id(struct net_device *netdev,
1292 enum ethtool_phys_id_state state)
1294 struct hnae3_handle *h = hns3_get_handle(netdev);
1296 if (!h->ae_algo->ops->set_led_id)
1299 return h->ae_algo->ops->set_led_id(h, state);
1302 static u32 hns3_get_msglevel(struct net_device *netdev)
1304 struct hnae3_handle *h = hns3_get_handle(netdev);
1306 return h->msg_enable;
1309 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1311 struct hnae3_handle *h = hns3_get_handle(netdev);
1313 h->msg_enable = msg_level;
1316 /* Translate local fec value into ethtool value. */
1317 static unsigned int loc_to_eth_fec(u8 loc_fec)
1321 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1322 eth_fec |= ETHTOOL_FEC_AUTO;
1323 if (loc_fec & BIT(HNAE3_FEC_RS))
1324 eth_fec |= ETHTOOL_FEC_RS;
1325 if (loc_fec & BIT(HNAE3_FEC_BASER))
1326 eth_fec |= ETHTOOL_FEC_BASER;
1328 /* if nothing is set, then FEC is off */
1330 eth_fec = ETHTOOL_FEC_OFF;
1335 /* Translate ethtool fec value into local value. */
1336 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1340 if (eth_fec & ETHTOOL_FEC_OFF)
1343 if (eth_fec & ETHTOOL_FEC_AUTO)
1344 loc_fec |= BIT(HNAE3_FEC_AUTO);
1345 if (eth_fec & ETHTOOL_FEC_RS)
1346 loc_fec |= BIT(HNAE3_FEC_RS);
1347 if (eth_fec & ETHTOOL_FEC_BASER)
1348 loc_fec |= BIT(HNAE3_FEC_BASER);
1353 static int hns3_get_fecparam(struct net_device *netdev,
1354 struct ethtool_fecparam *fec)
1356 struct hnae3_handle *handle = hns3_get_handle(netdev);
1357 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1361 if (handle->pdev->revision == 0x20)
1367 ops->get_fec(handle, &fec_ability, &fec_mode);
1369 fec->fec = loc_to_eth_fec(fec_ability);
1370 fec->active_fec = loc_to_eth_fec(fec_mode);
1375 static int hns3_set_fecparam(struct net_device *netdev,
1376 struct ethtool_fecparam *fec)
1378 struct hnae3_handle *handle = hns3_get_handle(netdev);
1379 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1382 if (handle->pdev->revision == 0x20)
1387 fec_mode = eth_to_loc_fec(fec->fec);
1389 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1391 return ops->set_fec(handle, fec_mode);
1394 static const struct ethtool_ops hns3vf_ethtool_ops = {
1395 .get_drvinfo = hns3_get_drvinfo,
1396 .get_ringparam = hns3_get_ringparam,
1397 .set_ringparam = hns3_set_ringparam,
1398 .get_strings = hns3_get_strings,
1399 .get_ethtool_stats = hns3_get_stats,
1400 .get_sset_count = hns3_get_sset_count,
1401 .get_rxnfc = hns3_get_rxnfc,
1402 .set_rxnfc = hns3_set_rxnfc,
1403 .get_rxfh_key_size = hns3_get_rss_key_size,
1404 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1405 .get_rxfh = hns3_get_rss,
1406 .set_rxfh = hns3_set_rss,
1407 .get_link_ksettings = hns3_get_link_ksettings,
1408 .get_channels = hns3_get_channels,
1409 .set_channels = hns3_set_channels,
1410 .get_coalesce = hns3_get_coalesce,
1411 .set_coalesce = hns3_set_coalesce,
1412 .get_regs_len = hns3_get_regs_len,
1413 .get_regs = hns3_get_regs,
1414 .get_link = hns3_get_link,
1415 .get_msglevel = hns3_get_msglevel,
1416 .set_msglevel = hns3_set_msglevel,
1419 static const struct ethtool_ops hns3_ethtool_ops = {
1420 .self_test = hns3_self_test,
1421 .get_drvinfo = hns3_get_drvinfo,
1422 .get_link = hns3_get_link,
1423 .get_ringparam = hns3_get_ringparam,
1424 .set_ringparam = hns3_set_ringparam,
1425 .get_pauseparam = hns3_get_pauseparam,
1426 .set_pauseparam = hns3_set_pauseparam,
1427 .get_strings = hns3_get_strings,
1428 .get_ethtool_stats = hns3_get_stats,
1429 .get_sset_count = hns3_get_sset_count,
1430 .get_rxnfc = hns3_get_rxnfc,
1431 .set_rxnfc = hns3_set_rxnfc,
1432 .get_rxfh_key_size = hns3_get_rss_key_size,
1433 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1434 .get_rxfh = hns3_get_rss,
1435 .set_rxfh = hns3_set_rss,
1436 .get_link_ksettings = hns3_get_link_ksettings,
1437 .set_link_ksettings = hns3_set_link_ksettings,
1438 .nway_reset = hns3_nway_reset,
1439 .get_channels = hns3_get_channels,
1440 .set_channels = hns3_set_channels,
1441 .get_coalesce = hns3_get_coalesce,
1442 .set_coalesce = hns3_set_coalesce,
1443 .get_regs_len = hns3_get_regs_len,
1444 .get_regs = hns3_get_regs,
1445 .set_phys_id = hns3_set_phys_id,
1446 .get_msglevel = hns3_get_msglevel,
1447 .set_msglevel = hns3_set_msglevel,
1448 .get_fecparam = hns3_get_fecparam,
1449 .set_fecparam = hns3_set_fecparam,
1452 void hns3_ethtool_set_ops(struct net_device *netdev)
1454 struct hnae3_handle *h = hns3_get_handle(netdev);
1456 if (h->flags & HNAE3_SUPPORT_VF)
1457 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1459 netdev->ethtool_ops = &hns3_ethtool_ops;