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 struct hns3_link_mode_mapping {
75 u32 ethtool_link_mode;
78 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
80 struct hnae3_handle *h = hns3_get_handle(ndev);
81 bool vlan_filter_enable;
84 if (!h->ae_algo->ops->set_loopback ||
85 !h->ae_algo->ops->set_promisc_mode)
89 case HNAE3_LOOP_SERIAL_SERDES:
90 case HNAE3_LOOP_PARALLEL_SERDES:
93 ret = h->ae_algo->ops->set_loopback(h, loop, en);
100 if (ret || h->pdev->revision >= 0x21)
104 h->ae_algo->ops->set_promisc_mode(h, true, true);
106 /* recover promisc mode before loopback test */
107 hns3_update_promisc_mode(ndev, h->netdev_flags);
108 vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
109 hns3_enable_vlan_filter(ndev, vlan_filter_enable);
115 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
117 struct hnae3_handle *h = hns3_get_handle(ndev);
120 ret = hns3_nic_reset_all_ring(h);
124 ret = hns3_lp_setup(ndev, loop_mode, true);
125 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
130 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
134 ret = hns3_lp_setup(ndev, loop_mode, false);
136 netdev_err(ndev, "lb_setup return error: %d\n", ret);
140 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
145 static void hns3_lp_setup_skb(struct sk_buff *skb)
147 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
149 struct net_device *ndev = skb->dev;
150 struct hnae3_handle *handle;
151 unsigned char *packet;
155 skb_reserve(skb, NET_IP_ALIGN);
156 ethh = skb_put(skb, sizeof(struct ethhdr));
157 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
159 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
161 /* The dst mac addr of loopback packet is the same as the host'
162 * mac addr, the SSU component may loop back the packet to host
163 * before the packet reaches mac or serdes, which will defect
164 * the purpose of mac or serdes selftest.
166 handle = hns3_get_handle(ndev);
167 if (handle->pdev->revision == 0x20)
168 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
169 eth_zero_addr(ethh->h_source);
170 ethh->h_proto = htons(ETH_P_ARP);
171 skb_reset_mac_header(skb);
173 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
174 packet[i] = (unsigned char)(i & 0xff);
177 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
180 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
181 unsigned char *packet = skb->data;
184 for (i = 0; i < skb->len; i++)
185 if (packet[i] != (unsigned char)(i & 0xff))
188 /* The packet is correctly received */
190 tqp_vector->rx_group.total_packets++;
192 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
193 skb->data, skb->len, true);
195 dev_kfree_skb_any(skb);
198 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
200 struct hnae3_handle *h = priv->ae_handle;
201 struct hnae3_knic_private_info *kinfo;
202 u32 i, rcv_good_pkt_total = 0;
205 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
206 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
207 struct hns3_enet_ring_group *rx_group;
210 rx_group = &ring->tqp_vector->rx_group;
211 pre_rx_pkt = rx_group->total_packets;
214 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
217 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
218 rx_group->total_packets = pre_rx_pkt;
220 return rcv_good_pkt_total;
223 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
224 u32 end_ringid, u32 budget)
228 for (i = start_ringid; i <= end_ringid; i++) {
229 struct hns3_enet_ring *ring = priv->ring_data[i].ring;
231 hns3_clean_tx_ring(ring);
236 * hns3_lp_run_test - run loopback test
238 * @mode: loopback type
240 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
242 struct hns3_nic_priv *priv = netdev_priv(ndev);
247 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
250 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
253 hns3_lp_setup_skb(skb);
254 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
257 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
261 tx_ret = hns3_nic_net_xmit(skb, ndev);
262 if (tx_ret == NETDEV_TX_OK) {
266 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
270 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
271 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
272 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
273 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
277 /* Allow 200 milliseconds for packets to go from Tx to Rx */
280 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
281 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
282 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
283 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
284 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
288 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
289 HNS3_NIC_LB_TEST_RING_ID,
290 HNS3_NIC_LB_TEST_PKT_NUM);
297 * hns3_nic_self_test - self test
299 * @eth_test: test cmd
302 static void hns3_self_test(struct net_device *ndev,
303 struct ethtool_test *eth_test, u64 *data)
305 struct hns3_nic_priv *priv = netdev_priv(ndev);
306 struct hnae3_handle *h = priv->ae_handle;
307 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
308 bool if_running = netif_running(ndev);
309 #if IS_ENABLED(CONFIG_VLAN_8021Q)
310 bool dis_vlan_filter;
315 if (hns3_nic_resetting(ndev)) {
316 netdev_err(ndev, "dev resetting!");
320 /* Only do offline selftest, or pass by default */
321 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
324 netif_dbg(h, drv, ndev, "self test start");
326 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
327 st_param[HNAE3_LOOP_APP][1] =
328 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
330 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
331 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
332 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
334 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
335 HNAE3_LOOP_PARALLEL_SERDES;
336 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
337 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
339 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
340 st_param[HNAE3_LOOP_PHY][1] =
341 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
344 ndev->netdev_ops->ndo_stop(ndev);
346 #if IS_ENABLED(CONFIG_VLAN_8021Q)
347 /* Disable the vlan filter for selftest does not support it */
348 dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
349 h->ae_algo->ops->enable_vlan_filter;
351 h->ae_algo->ops->enable_vlan_filter(h, false);
354 /* Tell firmware to stop mac autoneg before loopback test start,
355 * otherwise loopback test may be failed when the port is still
358 if (h->ae_algo->ops->halt_autoneg)
359 h->ae_algo->ops->halt_autoneg(h, true);
361 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
363 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
364 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
369 data[test_index] = hns3_lp_up(ndev, loop_type);
370 if (!data[test_index])
371 data[test_index] = hns3_lp_run_test(ndev, loop_type);
373 hns3_lp_down(ndev, loop_type);
375 if (data[test_index])
376 eth_test->flags |= ETH_TEST_FL_FAILED;
381 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
383 if (h->ae_algo->ops->halt_autoneg)
384 h->ae_algo->ops->halt_autoneg(h, false);
386 #if IS_ENABLED(CONFIG_VLAN_8021Q)
388 h->ae_algo->ops->enable_vlan_filter(h, true);
392 ndev->netdev_ops->ndo_open(ndev);
394 netif_dbg(h, drv, ndev, "self test end\n");
397 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
399 struct hnae3_handle *h = hns3_get_handle(netdev);
400 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
402 if (!ops->get_sset_count)
407 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
408 ops->get_sset_count(h, stringset));
411 return ops->get_sset_count(h, stringset);
418 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
419 u32 stat_count, u32 num_tqps, const char *prefix)
421 #define MAX_PREFIX_SIZE (6 + 4)
426 for (i = 0; i < num_tqps; i++) {
427 for (j = 0; j < stat_count; j++) {
428 data[ETH_GSTRING_LEN - 1] = '\0';
430 /* first, prepend the prefix string */
431 n1 = snprintf(data, MAX_PREFIX_SIZE, "%s%d_",
433 n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
434 size_left = (ETH_GSTRING_LEN - 1) - n1;
436 /* now, concatenate the stats string to it */
437 strncat(data, stats[j].stats_string, size_left);
438 data += ETH_GSTRING_LEN;
445 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
447 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
448 const char tx_prefix[] = "txq";
449 const char rx_prefix[] = "rxq";
451 /* get strings for Tx */
452 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
453 kinfo->num_tqps, tx_prefix);
455 /* get strings for Rx */
456 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
457 kinfo->num_tqps, rx_prefix);
462 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
464 struct hnae3_handle *h = hns3_get_handle(netdev);
465 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
466 char *buff = (char *)data;
468 if (!ops->get_strings)
473 buff = hns3_get_strings_tqps(h, buff);
474 ops->get_strings(h, stringset, (u8 *)buff);
477 ops->get_strings(h, stringset, data);
484 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
486 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
487 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
488 struct hns3_enet_ring *ring;
492 /* get stats for Tx */
493 for (i = 0; i < kinfo->num_tqps; i++) {
494 ring = nic_priv->ring_data[i].ring;
495 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
496 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
497 *data++ = *(u64 *)stat;
501 /* get stats for Rx */
502 for (i = 0; i < kinfo->num_tqps; i++) {
503 ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
504 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
505 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
506 *data++ = *(u64 *)stat;
513 /* hns3_get_stats - get detail statistics.
514 * @netdev: net device
515 * @stats: statistics info.
516 * @data: statistics data.
518 static void hns3_get_stats(struct net_device *netdev,
519 struct ethtool_stats *stats, u64 *data)
521 struct hnae3_handle *h = hns3_get_handle(netdev);
524 if (hns3_nic_resetting(netdev)) {
525 netdev_err(netdev, "dev resetting, could not get stats\n");
529 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
530 netdev_err(netdev, "could not get any statistics\n");
534 h->ae_algo->ops->update_stats(h, &netdev->stats);
536 /* get per-queue stats */
537 p = hns3_get_stats_tqps(h, p);
539 /* get MAC & other misc hardware stats */
540 h->ae_algo->ops->get_stats(h, p);
543 static void hns3_get_drvinfo(struct net_device *netdev,
544 struct ethtool_drvinfo *drvinfo)
546 struct hns3_nic_priv *priv = netdev_priv(netdev);
547 struct hnae3_handle *h = priv->ae_handle;
550 if (!h->ae_algo->ops->get_fw_version) {
551 netdev_err(netdev, "could not get fw version!\n");
555 strncpy(drvinfo->version, hns3_driver_version,
556 sizeof(drvinfo->version));
557 drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
559 strncpy(drvinfo->driver, h->pdev->driver->name,
560 sizeof(drvinfo->driver));
561 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
563 strncpy(drvinfo->bus_info, pci_name(h->pdev),
564 sizeof(drvinfo->bus_info));
565 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
567 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
569 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
571 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
572 HNAE3_FW_VERSION_BYTE3_SHIFT),
573 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
574 HNAE3_FW_VERSION_BYTE2_SHIFT),
575 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
576 HNAE3_FW_VERSION_BYTE1_SHIFT),
577 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
578 HNAE3_FW_VERSION_BYTE0_SHIFT));
581 static u32 hns3_get_link(struct net_device *netdev)
583 struct hnae3_handle *h = hns3_get_handle(netdev);
585 if (h->ae_algo->ops->get_status)
586 return h->ae_algo->ops->get_status(h);
591 static void hns3_get_ringparam(struct net_device *netdev,
592 struct ethtool_ringparam *param)
594 struct hns3_nic_priv *priv = netdev_priv(netdev);
595 struct hnae3_handle *h = priv->ae_handle;
596 int queue_num = h->kinfo.num_tqps;
598 if (hns3_nic_resetting(netdev)) {
599 netdev_err(netdev, "dev resetting!");
603 param->tx_max_pending = HNS3_RING_MAX_PENDING;
604 param->rx_max_pending = HNS3_RING_MAX_PENDING;
606 param->tx_pending = priv->ring_data[0].ring->desc_num;
607 param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
610 static void hns3_get_pauseparam(struct net_device *netdev,
611 struct ethtool_pauseparam *param)
613 struct hnae3_handle *h = hns3_get_handle(netdev);
615 if (h->ae_algo->ops->get_pauseparam)
616 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
617 ¶m->rx_pause, ¶m->tx_pause);
620 static int hns3_set_pauseparam(struct net_device *netdev,
621 struct ethtool_pauseparam *param)
623 struct hnae3_handle *h = hns3_get_handle(netdev);
625 netif_dbg(h, drv, netdev,
626 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
627 param->autoneg, param->rx_pause, param->tx_pause);
629 if (h->ae_algo->ops->set_pauseparam)
630 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
636 static void hns3_get_ksettings(struct hnae3_handle *h,
637 struct ethtool_link_ksettings *cmd)
639 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
641 /* 1.auto_neg & speed & duplex from cmd */
642 if (ops->get_ksettings_an_result)
643 ops->get_ksettings_an_result(h,
648 /* 2.get link mode */
649 if (ops->get_link_mode)
650 ops->get_link_mode(h,
651 cmd->link_modes.supported,
652 cmd->link_modes.advertising);
654 /* 3.mdix_ctrl&mdix get from phy reg */
655 if (ops->get_mdix_mode)
656 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
657 &cmd->base.eth_tp_mdix);
660 static int hns3_get_link_ksettings(struct net_device *netdev,
661 struct ethtool_link_ksettings *cmd)
663 struct hnae3_handle *h = hns3_get_handle(netdev);
664 const struct hnae3_ae_ops *ops;
669 ops = h->ae_algo->ops;
670 if (ops->get_media_type)
671 ops->get_media_type(h, &media_type, &module_type);
675 switch (media_type) {
676 case HNAE3_MEDIA_TYPE_NONE:
677 cmd->base.port = PORT_NONE;
678 hns3_get_ksettings(h, cmd);
680 case HNAE3_MEDIA_TYPE_FIBER:
681 if (module_type == HNAE3_MODULE_TYPE_CR)
682 cmd->base.port = PORT_DA;
684 cmd->base.port = PORT_FIBRE;
686 hns3_get_ksettings(h, cmd);
688 case HNAE3_MEDIA_TYPE_BACKPLANE:
689 cmd->base.port = PORT_NONE;
690 hns3_get_ksettings(h, cmd);
692 case HNAE3_MEDIA_TYPE_COPPER:
693 cmd->base.port = PORT_TP;
695 hns3_get_ksettings(h, cmd);
697 phy_ethtool_ksettings_get(netdev->phydev, cmd);
701 netdev_warn(netdev, "Unknown media type");
706 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
708 link_stat = hns3_get_link(netdev);
710 cmd->base.speed = SPEED_UNKNOWN;
711 cmd->base.duplex = DUPLEX_UNKNOWN;
717 static int hns3_check_ksettings_param(const struct net_device *netdev,
718 const struct ethtool_link_ksettings *cmd)
720 struct hnae3_handle *handle = hns3_get_handle(netdev);
721 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
722 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
723 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
729 /* hw doesn't support use specified speed and duplex to negotiate,
730 * unnecessary to check them when autoneg on.
732 if (cmd->base.autoneg)
735 if (ops->get_ksettings_an_result) {
736 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
737 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
738 cmd->base.duplex == duplex)
742 if (ops->get_media_type)
743 ops->get_media_type(handle, &media_type, &module_type);
745 if (cmd->base.duplex != DUPLEX_FULL &&
746 media_type != HNAE3_MEDIA_TYPE_COPPER) {
748 "only copper port supports half duplex!");
752 if (ops->check_port_speed) {
753 ret = ops->check_port_speed(handle, cmd->base.speed);
755 netdev_err(netdev, "unsupported speed\n");
763 static int hns3_set_link_ksettings(struct net_device *netdev,
764 const struct ethtool_link_ksettings *cmd)
766 struct hnae3_handle *handle = hns3_get_handle(netdev);
767 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
770 /* Chip don't support this mode. */
771 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
774 netif_dbg(handle, drv, netdev,
775 "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
776 netdev->phydev ? "phy" : "mac",
777 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
779 /* Only support ksettings_set for netdev with phy attached for now */
781 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
783 if (handle->pdev->revision == 0x20)
786 ret = hns3_check_ksettings_param(netdev, cmd);
790 if (ops->set_autoneg) {
791 ret = ops->set_autoneg(handle, cmd->base.autoneg);
796 /* hw doesn't support use specified speed and duplex to negotiate,
797 * ignore them when autoneg on.
799 if (cmd->base.autoneg) {
801 "autoneg is on, ignore the speed and duplex\n");
805 if (ops->cfg_mac_speed_dup_h)
806 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
812 static u32 hns3_get_rss_key_size(struct net_device *netdev)
814 struct hnae3_handle *h = hns3_get_handle(netdev);
816 if (!h->ae_algo->ops->get_rss_key_size)
819 return h->ae_algo->ops->get_rss_key_size(h);
822 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
824 struct hnae3_handle *h = hns3_get_handle(netdev);
826 if (!h->ae_algo->ops->get_rss_indir_size)
829 return h->ae_algo->ops->get_rss_indir_size(h);
832 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
835 struct hnae3_handle *h = hns3_get_handle(netdev);
837 if (!h->ae_algo->ops->get_rss)
840 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
843 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
844 const u8 *key, const u8 hfunc)
846 struct hnae3_handle *h = hns3_get_handle(netdev);
848 if (!h->ae_algo->ops->set_rss)
851 if ((h->pdev->revision == 0x20 &&
852 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
853 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
854 netdev_err(netdev, "hash func not supported\n");
860 "set rss failed for indir is empty\n");
864 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
867 static int hns3_get_rxnfc(struct net_device *netdev,
868 struct ethtool_rxnfc *cmd,
871 struct hnae3_handle *h = hns3_get_handle(netdev);
874 case ETHTOOL_GRXRINGS:
875 cmd->data = h->kinfo.num_tqps;
878 if (h->ae_algo->ops->get_rss_tuple)
879 return h->ae_algo->ops->get_rss_tuple(h, cmd);
881 case ETHTOOL_GRXCLSRLCNT:
882 if (h->ae_algo->ops->get_fd_rule_cnt)
883 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
885 case ETHTOOL_GRXCLSRULE:
886 if (h->ae_algo->ops->get_fd_rule_info)
887 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
889 case ETHTOOL_GRXCLSRLALL:
890 if (h->ae_algo->ops->get_fd_all_rules)
891 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
899 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
900 u32 tx_desc_num, u32 rx_desc_num)
902 struct hnae3_handle *h = priv->ae_handle;
905 h->kinfo.num_tx_desc = tx_desc_num;
906 h->kinfo.num_rx_desc = rx_desc_num;
908 for (i = 0; i < h->kinfo.num_tqps; i++) {
909 priv->ring_data[i].ring->desc_num = tx_desc_num;
910 priv->ring_data[i + h->kinfo.num_tqps].ring->desc_num =
915 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
917 struct hnae3_handle *handle = priv->ae_handle;
918 struct hns3_enet_ring *tmp_rings;
921 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
922 sizeof(struct hns3_enet_ring), GFP_KERNEL);
926 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
927 memcpy(&tmp_rings[i], priv->ring_data[i].ring,
928 sizeof(struct hns3_enet_ring));
929 tmp_rings[i].skb = NULL;
935 static int hns3_check_ringparam(struct net_device *ndev,
936 struct ethtool_ringparam *param)
938 if (hns3_nic_resetting(ndev))
941 if (param->rx_mini_pending || param->rx_jumbo_pending)
944 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
945 param->tx_pending < HNS3_RING_MIN_PENDING ||
946 param->rx_pending > HNS3_RING_MAX_PENDING ||
947 param->rx_pending < HNS3_RING_MIN_PENDING) {
948 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
949 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
956 static int hns3_set_ringparam(struct net_device *ndev,
957 struct ethtool_ringparam *param)
959 struct hns3_nic_priv *priv = netdev_priv(ndev);
960 struct hnae3_handle *h = priv->ae_handle;
961 struct hns3_enet_ring *tmp_rings;
962 bool if_running = netif_running(ndev);
963 u32 old_tx_desc_num, new_tx_desc_num;
964 u32 old_rx_desc_num, new_rx_desc_num;
965 u16 queue_num = h->kinfo.num_tqps;
968 ret = hns3_check_ringparam(ndev, param);
972 /* Hardware requires that its descriptors must be multiple of eight */
973 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
974 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
975 old_tx_desc_num = priv->ring_data[0].ring->desc_num;
976 old_rx_desc_num = priv->ring_data[queue_num].ring->desc_num;
977 if (old_tx_desc_num == new_tx_desc_num &&
978 old_rx_desc_num == new_rx_desc_num)
981 tmp_rings = hns3_backup_ringparam(priv);
984 "backup ring param failed by allocating memory fail\n");
989 "Changing Tx/Rx ring depth from %d/%d to %d/%d\n",
990 old_tx_desc_num, old_rx_desc_num,
991 new_tx_desc_num, new_rx_desc_num);
994 ndev->netdev_ops->ndo_stop(ndev);
996 hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
997 ret = hns3_init_all_ring(priv);
999 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1002 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1004 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1005 memcpy(priv->ring_data[i].ring, &tmp_rings[i],
1006 sizeof(struct hns3_enet_ring));
1008 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1009 hns3_fini_ring(&tmp_rings[i]);
1015 ret = ndev->netdev_ops->ndo_open(ndev);
1020 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1022 struct hnae3_handle *h = hns3_get_handle(netdev);
1026 if (h->ae_algo->ops->set_rss_tuple)
1027 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1029 case ETHTOOL_SRXCLSRLINS:
1030 if (h->ae_algo->ops->add_fd_entry)
1031 return h->ae_algo->ops->add_fd_entry(h, cmd);
1033 case ETHTOOL_SRXCLSRLDEL:
1034 if (h->ae_algo->ops->del_fd_entry)
1035 return h->ae_algo->ops->del_fd_entry(h, cmd);
1042 static int hns3_nway_reset(struct net_device *netdev)
1044 struct hnae3_handle *handle = hns3_get_handle(netdev);
1045 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1046 struct phy_device *phy = netdev->phydev;
1049 if (!netif_running(netdev))
1052 if (hns3_nic_resetting(netdev)) {
1053 netdev_err(netdev, "dev resetting!");
1057 if (!ops->get_autoneg || !ops->restart_autoneg)
1060 autoneg = ops->get_autoneg(handle);
1061 if (autoneg != AUTONEG_ENABLE) {
1063 "Autoneg is off, don't support to restart it\n");
1067 netif_dbg(handle, drv, netdev,
1068 "nway reset (using %s)\n", phy ? "phy" : "mac");
1071 return genphy_restart_aneg(phy);
1073 if (handle->pdev->revision == 0x20)
1076 return ops->restart_autoneg(handle);
1079 static void hns3_get_channels(struct net_device *netdev,
1080 struct ethtool_channels *ch)
1082 struct hnae3_handle *h = hns3_get_handle(netdev);
1084 if (h->ae_algo->ops->get_channels)
1085 h->ae_algo->ops->get_channels(h, ch);
1088 static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
1089 struct ethtool_coalesce *cmd)
1091 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1092 struct hns3_nic_priv *priv = netdev_priv(netdev);
1093 struct hnae3_handle *h = priv->ae_handle;
1094 u16 queue_num = h->kinfo.num_tqps;
1096 if (hns3_nic_resetting(netdev))
1099 if (queue >= queue_num) {
1101 "Invalid queue value %d! Queue max id=%d\n",
1102 queue, queue_num - 1);
1106 tx_vector = priv->ring_data[queue].ring->tqp_vector;
1107 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
1109 cmd->use_adaptive_tx_coalesce =
1110 tx_vector->tx_group.coal.gl_adapt_enable;
1111 cmd->use_adaptive_rx_coalesce =
1112 rx_vector->rx_group.coal.gl_adapt_enable;
1114 cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
1115 cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
1117 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1118 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1123 static int hns3_get_coalesce(struct net_device *netdev,
1124 struct ethtool_coalesce *cmd)
1126 return hns3_get_coalesce_per_queue(netdev, 0, cmd);
1129 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1130 struct ethtool_coalesce *cmd)
1134 if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
1136 "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
1141 if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
1143 "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
1148 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1149 if (rx_gl != cmd->rx_coalesce_usecs) {
1151 "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
1152 cmd->rx_coalesce_usecs, rx_gl);
1155 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1156 if (tx_gl != cmd->tx_coalesce_usecs) {
1158 "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
1159 cmd->tx_coalesce_usecs, tx_gl);
1165 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1166 struct ethtool_coalesce *cmd)
1170 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1172 "tx_usecs_high must be same as rx_usecs_high.\n");
1176 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1178 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1183 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1184 if (rl != cmd->rx_coalesce_usecs_high) {
1186 "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
1187 cmd->rx_coalesce_usecs_high, rl);
1193 static int hns3_check_coalesce_para(struct net_device *netdev,
1194 struct ethtool_coalesce *cmd)
1198 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1201 "Check gl coalesce param fail. ret = %d\n", ret);
1205 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1208 "Check rl coalesce param fail. ret = %d\n", ret);
1212 if (cmd->use_adaptive_tx_coalesce == 1 ||
1213 cmd->use_adaptive_rx_coalesce == 1) {
1215 "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
1216 cmd->use_adaptive_tx_coalesce,
1217 cmd->use_adaptive_rx_coalesce);
1223 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1224 struct ethtool_coalesce *cmd,
1227 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1228 struct hns3_nic_priv *priv = netdev_priv(netdev);
1229 struct hnae3_handle *h = priv->ae_handle;
1230 int queue_num = h->kinfo.num_tqps;
1232 tx_vector = priv->ring_data[queue].ring->tqp_vector;
1233 rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
1235 tx_vector->tx_group.coal.gl_adapt_enable =
1236 cmd->use_adaptive_tx_coalesce;
1237 rx_vector->rx_group.coal.gl_adapt_enable =
1238 cmd->use_adaptive_rx_coalesce;
1240 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1241 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1243 hns3_set_vector_coalesce_tx_gl(tx_vector,
1244 tx_vector->tx_group.coal.int_gl);
1245 hns3_set_vector_coalesce_rx_gl(rx_vector,
1246 rx_vector->rx_group.coal.int_gl);
1248 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1249 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1252 static int hns3_set_coalesce(struct net_device *netdev,
1253 struct ethtool_coalesce *cmd)
1255 struct hnae3_handle *h = hns3_get_handle(netdev);
1256 u16 queue_num = h->kinfo.num_tqps;
1260 if (hns3_nic_resetting(netdev))
1263 ret = hns3_check_coalesce_para(netdev, cmd);
1267 h->kinfo.int_rl_setting =
1268 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1270 for (i = 0; i < queue_num; i++)
1271 hns3_set_coalesce_per_queue(netdev, cmd, i);
1276 static int hns3_get_regs_len(struct net_device *netdev)
1278 struct hnae3_handle *h = hns3_get_handle(netdev);
1280 if (!h->ae_algo->ops->get_regs_len)
1283 return h->ae_algo->ops->get_regs_len(h);
1286 static void hns3_get_regs(struct net_device *netdev,
1287 struct ethtool_regs *cmd, void *data)
1289 struct hnae3_handle *h = hns3_get_handle(netdev);
1291 if (!h->ae_algo->ops->get_regs)
1294 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1297 static int hns3_set_phys_id(struct net_device *netdev,
1298 enum ethtool_phys_id_state state)
1300 struct hnae3_handle *h = hns3_get_handle(netdev);
1302 if (!h->ae_algo->ops->set_led_id)
1305 return h->ae_algo->ops->set_led_id(h, state);
1308 static u32 hns3_get_msglevel(struct net_device *netdev)
1310 struct hnae3_handle *h = hns3_get_handle(netdev);
1312 return h->msg_enable;
1315 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1317 struct hnae3_handle *h = hns3_get_handle(netdev);
1319 h->msg_enable = msg_level;
1322 /* Translate local fec value into ethtool value. */
1323 static unsigned int loc_to_eth_fec(u8 loc_fec)
1327 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1328 eth_fec |= ETHTOOL_FEC_AUTO;
1329 if (loc_fec & BIT(HNAE3_FEC_RS))
1330 eth_fec |= ETHTOOL_FEC_RS;
1331 if (loc_fec & BIT(HNAE3_FEC_BASER))
1332 eth_fec |= ETHTOOL_FEC_BASER;
1334 /* if nothing is set, then FEC is off */
1336 eth_fec = ETHTOOL_FEC_OFF;
1341 /* Translate ethtool fec value into local value. */
1342 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1346 if (eth_fec & ETHTOOL_FEC_OFF)
1349 if (eth_fec & ETHTOOL_FEC_AUTO)
1350 loc_fec |= BIT(HNAE3_FEC_AUTO);
1351 if (eth_fec & ETHTOOL_FEC_RS)
1352 loc_fec |= BIT(HNAE3_FEC_RS);
1353 if (eth_fec & ETHTOOL_FEC_BASER)
1354 loc_fec |= BIT(HNAE3_FEC_BASER);
1359 static int hns3_get_fecparam(struct net_device *netdev,
1360 struct ethtool_fecparam *fec)
1362 struct hnae3_handle *handle = hns3_get_handle(netdev);
1363 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1367 if (handle->pdev->revision == 0x20)
1373 ops->get_fec(handle, &fec_ability, &fec_mode);
1375 fec->fec = loc_to_eth_fec(fec_ability);
1376 fec->active_fec = loc_to_eth_fec(fec_mode);
1381 static int hns3_set_fecparam(struct net_device *netdev,
1382 struct ethtool_fecparam *fec)
1384 struct hnae3_handle *handle = hns3_get_handle(netdev);
1385 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1388 if (handle->pdev->revision == 0x20)
1393 fec_mode = eth_to_loc_fec(fec->fec);
1395 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1397 return ops->set_fec(handle, fec_mode);
1400 static const struct ethtool_ops hns3vf_ethtool_ops = {
1401 .get_drvinfo = hns3_get_drvinfo,
1402 .get_ringparam = hns3_get_ringparam,
1403 .set_ringparam = hns3_set_ringparam,
1404 .get_strings = hns3_get_strings,
1405 .get_ethtool_stats = hns3_get_stats,
1406 .get_sset_count = hns3_get_sset_count,
1407 .get_rxnfc = hns3_get_rxnfc,
1408 .set_rxnfc = hns3_set_rxnfc,
1409 .get_rxfh_key_size = hns3_get_rss_key_size,
1410 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1411 .get_rxfh = hns3_get_rss,
1412 .set_rxfh = hns3_set_rss,
1413 .get_link_ksettings = hns3_get_link_ksettings,
1414 .get_channels = hns3_get_channels,
1415 .set_channels = hns3_set_channels,
1416 .get_coalesce = hns3_get_coalesce,
1417 .set_coalesce = hns3_set_coalesce,
1418 .get_regs_len = hns3_get_regs_len,
1419 .get_regs = hns3_get_regs,
1420 .get_link = hns3_get_link,
1421 .get_msglevel = hns3_get_msglevel,
1422 .set_msglevel = hns3_set_msglevel,
1425 static const struct ethtool_ops hns3_ethtool_ops = {
1426 .self_test = hns3_self_test,
1427 .get_drvinfo = hns3_get_drvinfo,
1428 .get_link = hns3_get_link,
1429 .get_ringparam = hns3_get_ringparam,
1430 .set_ringparam = hns3_set_ringparam,
1431 .get_pauseparam = hns3_get_pauseparam,
1432 .set_pauseparam = hns3_set_pauseparam,
1433 .get_strings = hns3_get_strings,
1434 .get_ethtool_stats = hns3_get_stats,
1435 .get_sset_count = hns3_get_sset_count,
1436 .get_rxnfc = hns3_get_rxnfc,
1437 .set_rxnfc = hns3_set_rxnfc,
1438 .get_rxfh_key_size = hns3_get_rss_key_size,
1439 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1440 .get_rxfh = hns3_get_rss,
1441 .set_rxfh = hns3_set_rss,
1442 .get_link_ksettings = hns3_get_link_ksettings,
1443 .set_link_ksettings = hns3_set_link_ksettings,
1444 .nway_reset = hns3_nway_reset,
1445 .get_channels = hns3_get_channels,
1446 .set_channels = hns3_set_channels,
1447 .get_coalesce = hns3_get_coalesce,
1448 .set_coalesce = hns3_set_coalesce,
1449 .get_regs_len = hns3_get_regs_len,
1450 .get_regs = hns3_get_regs,
1451 .set_phys_id = hns3_set_phys_id,
1452 .get_msglevel = hns3_get_msglevel,
1453 .set_msglevel = hns3_set_msglevel,
1454 .get_fecparam = hns3_get_fecparam,
1455 .set_fecparam = hns3_set_fecparam,
1458 void hns3_ethtool_set_ops(struct net_device *netdev)
1460 struct hnae3_handle *h = hns3_get_handle(netdev);
1462 if (h->flags & HNAE3_SUPPORT_VF)
1463 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1465 netdev->ethtool_ops = &hns3_ethtool_ops;