1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
4 #include <linux/etherdevice.h>
5 #include <linux/string.h>
12 char stats_string[ETH_GSTRING_LEN];
16 struct hns3_sfp_type {
21 struct hns3_pflag_desc {
22 char name[ETH_GSTRING_LEN];
23 void (*handler)(struct net_device *netdev, bool enable);
26 /* tqp related stats */
27 #define HNS3_TQP_STAT(_string, _member) { \
28 .stats_string = _string, \
29 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
30 offsetof(struct ring_stats, _member), \
33 static const struct hns3_stats hns3_txq_stats[] = {
34 /* Tx per-queue statistics */
35 HNS3_TQP_STAT("dropped", sw_err_cnt),
36 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
37 HNS3_TQP_STAT("packets", tx_pkts),
38 HNS3_TQP_STAT("bytes", tx_bytes),
39 HNS3_TQP_STAT("more", tx_more),
40 HNS3_TQP_STAT("wake", restart_queue),
41 HNS3_TQP_STAT("busy", tx_busy),
42 HNS3_TQP_STAT("copy", tx_copy),
43 HNS3_TQP_STAT("vlan_err", tx_vlan_err),
44 HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
45 HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
46 HNS3_TQP_STAT("tso_err", tx_tso_err),
47 HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
48 HNS3_TQP_STAT("hw_limitation", hw_limitation),
49 HNS3_TQP_STAT("bounce", tx_bounce),
50 HNS3_TQP_STAT("spare_full", tx_spare_full),
51 HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
52 HNS3_TQP_STAT("sgl", tx_sgl),
53 HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
54 HNS3_TQP_STAT("map_sg_err", map_sg_err),
57 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
59 static const struct hns3_stats hns3_rxq_stats[] = {
60 /* Rx per-queue statistics */
61 HNS3_TQP_STAT("dropped", sw_err_cnt),
62 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
63 HNS3_TQP_STAT("packets", rx_pkts),
64 HNS3_TQP_STAT("bytes", rx_bytes),
65 HNS3_TQP_STAT("errors", rx_err_cnt),
66 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
67 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
68 HNS3_TQP_STAT("err_bd_num", err_bd_num),
69 HNS3_TQP_STAT("l2_err", l2_err),
70 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
71 HNS3_TQP_STAT("csum_complete", csum_complete),
72 HNS3_TQP_STAT("multicast", rx_multicast),
73 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
74 HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
75 HNS3_TQP_STAT("frag_alloc", frag_alloc),
78 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
80 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
82 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
84 #define HNS3_SELF_TEST_TYPE_NUM 4
85 #define HNS3_NIC_LB_TEST_PKT_NUM 1
86 #define HNS3_NIC_LB_TEST_RING_ID 0
87 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
88 #define HNS3_NIC_LB_SETUP_USEC 10000
90 /* Nic loopback test err */
91 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
92 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
93 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
95 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
97 struct hnae3_handle *h = hns3_get_handle(ndev);
98 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
101 if (!h->ae_algo->ops->set_loopback ||
102 !h->ae_algo->ops->set_promisc_mode)
106 case HNAE3_LOOP_SERIAL_SERDES:
107 case HNAE3_LOOP_PARALLEL_SERDES:
110 ret = h->ae_algo->ops->set_loopback(h, loop, en);
117 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
121 h->ae_algo->ops->set_promisc_mode(h, true, true);
123 /* recover promisc mode before loopback test */
124 hns3_request_update_promisc_mode(h);
129 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
131 struct hnae3_handle *h = hns3_get_handle(ndev);
134 ret = hns3_nic_reset_all_ring(h);
138 ret = hns3_lp_setup(ndev, loop_mode, true);
139 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
144 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
148 ret = hns3_lp_setup(ndev, loop_mode, false);
150 netdev_err(ndev, "lb_setup return error: %d\n", ret);
154 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
159 static void hns3_lp_setup_skb(struct sk_buff *skb)
161 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
163 struct net_device *ndev = skb->dev;
164 struct hnae3_handle *handle;
165 struct hnae3_ae_dev *ae_dev;
166 unsigned char *packet;
170 skb_reserve(skb, NET_IP_ALIGN);
171 ethh = skb_put(skb, sizeof(struct ethhdr));
172 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
174 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
176 /* The dst mac addr of loopback packet is the same as the host'
177 * mac addr, the SSU component may loop back the packet to host
178 * before the packet reaches mac or serdes, which will defect
179 * the purpose of mac or serdes selftest.
181 handle = hns3_get_handle(ndev);
182 ae_dev = pci_get_drvdata(handle->pdev);
183 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
184 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
185 eth_zero_addr(ethh->h_source);
186 ethh->h_proto = htons(ETH_P_ARP);
187 skb_reset_mac_header(skb);
189 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
190 packet[i] = (unsigned char)(i & 0xff);
193 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
196 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
197 unsigned char *packet = skb->data;
198 u32 len = skb_headlen(skb);
201 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
203 for (i = 0; i < len; i++)
204 if (packet[i] != (unsigned char)(i & 0xff))
207 /* The packet is correctly received */
208 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
209 tqp_vector->rx_group.total_packets++;
211 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
212 skb->data, len, true);
214 dev_kfree_skb_any(skb);
217 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
219 struct hnae3_handle *h = priv->ae_handle;
220 struct hnae3_knic_private_info *kinfo;
221 u32 i, rcv_good_pkt_total = 0;
224 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
225 struct hns3_enet_ring *ring = &priv->ring[i];
226 struct hns3_enet_ring_group *rx_group;
229 rx_group = &ring->tqp_vector->rx_group;
230 pre_rx_pkt = rx_group->total_packets;
233 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
236 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
237 rx_group->total_packets = pre_rx_pkt;
239 return rcv_good_pkt_total;
242 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
243 u32 end_ringid, u32 budget)
247 for (i = start_ringid; i <= end_ringid; i++) {
248 struct hns3_enet_ring *ring = &priv->ring[i];
250 hns3_clean_tx_ring(ring, 0);
255 * hns3_lp_run_test - run loopback test
257 * @mode: loopback type
259 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
261 struct hns3_nic_priv *priv = netdev_priv(ndev);
266 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
269 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
272 hns3_lp_setup_skb(skb);
273 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
276 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
280 tx_ret = hns3_nic_net_xmit(skb, ndev);
281 if (tx_ret == NETDEV_TX_OK) {
285 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
289 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
290 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
291 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
292 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
296 /* Allow 200 milliseconds for packets to go from Tx to Rx */
299 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
300 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
301 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
302 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
303 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
307 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
308 HNS3_NIC_LB_TEST_RING_ID,
309 HNS3_NIC_LB_TEST_PKT_NUM);
316 * hns3_self_test - self test
318 * @eth_test: test cmd
321 static void hns3_self_test(struct net_device *ndev,
322 struct ethtool_test *eth_test, u64 *data)
324 struct hns3_nic_priv *priv = netdev_priv(ndev);
325 struct hnae3_handle *h = priv->ae_handle;
326 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
327 bool if_running = netif_running(ndev);
331 if (hns3_nic_resetting(ndev)) {
332 netdev_err(ndev, "dev resetting!");
336 /* Only do offline selftest, or pass by default */
337 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
340 netif_dbg(h, drv, ndev, "self test start");
342 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
343 st_param[HNAE3_LOOP_APP][1] =
344 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
346 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
347 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
348 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
350 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
351 HNAE3_LOOP_PARALLEL_SERDES;
352 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
353 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
355 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
356 st_param[HNAE3_LOOP_PHY][1] =
357 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
360 ndev->netdev_ops->ndo_stop(ndev);
362 #if IS_ENABLED(CONFIG_VLAN_8021Q)
363 /* Disable the vlan filter for selftest does not support it */
364 if (h->ae_algo->ops->enable_vlan_filter)
365 h->ae_algo->ops->enable_vlan_filter(h, false);
368 /* Tell firmware to stop mac autoneg before loopback test start,
369 * otherwise loopback test may be failed when the port is still
372 if (h->ae_algo->ops->halt_autoneg)
373 h->ae_algo->ops->halt_autoneg(h, true);
375 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
377 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
378 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
383 data[test_index] = hns3_lp_up(ndev, loop_type);
384 if (!data[test_index])
385 data[test_index] = hns3_lp_run_test(ndev, loop_type);
387 hns3_lp_down(ndev, loop_type);
389 if (data[test_index])
390 eth_test->flags |= ETH_TEST_FL_FAILED;
395 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
397 if (h->ae_algo->ops->halt_autoneg)
398 h->ae_algo->ops->halt_autoneg(h, false);
400 #if IS_ENABLED(CONFIG_VLAN_8021Q)
401 if (h->ae_algo->ops->enable_vlan_filter)
402 h->ae_algo->ops->enable_vlan_filter(h, true);
406 ndev->netdev_ops->ndo_open(ndev);
408 netif_dbg(h, drv, ndev, "self test end\n");
411 static void hns3_update_limit_promisc_mode(struct net_device *netdev,
414 struct hnae3_handle *handle = hns3_get_handle(netdev);
417 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
419 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
421 hns3_request_update_promisc_mode(handle);
424 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
425 { "limit_promisc", hns3_update_limit_promisc_mode }
428 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
430 struct hnae3_handle *h = hns3_get_handle(netdev);
431 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
433 if (!ops->get_sset_count)
438 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
439 ops->get_sset_count(h, stringset));
442 return ops->get_sset_count(h, stringset);
444 case ETH_SS_PRIV_FLAGS:
445 return HNAE3_PFLAG_MAX;
452 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
453 u32 stat_count, u32 num_tqps, const char *prefix)
455 #define MAX_PREFIX_SIZE (6 + 4)
460 for (i = 0; i < num_tqps; i++) {
461 for (j = 0; j < stat_count; j++) {
462 data[ETH_GSTRING_LEN - 1] = '\0';
464 /* first, prepend the prefix string */
465 n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
467 size_left = (ETH_GSTRING_LEN - 1) - n1;
469 /* now, concatenate the stats string to it */
470 strncat(data, stats[j].stats_string, size_left);
471 data += ETH_GSTRING_LEN;
478 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
480 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
481 const char tx_prefix[] = "txq";
482 const char rx_prefix[] = "rxq";
484 /* get strings for Tx */
485 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
486 kinfo->num_tqps, tx_prefix);
488 /* get strings for Rx */
489 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
490 kinfo->num_tqps, rx_prefix);
495 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
497 struct hnae3_handle *h = hns3_get_handle(netdev);
498 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
499 char *buff = (char *)data;
502 if (!ops->get_strings)
507 buff = hns3_get_strings_tqps(h, buff);
508 ops->get_strings(h, stringset, (u8 *)buff);
511 ops->get_strings(h, stringset, data);
513 case ETH_SS_PRIV_FLAGS:
514 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
515 snprintf(buff, ETH_GSTRING_LEN, "%s",
516 hns3_priv_flags[i].name);
517 buff += ETH_GSTRING_LEN;
525 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
527 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
528 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
529 struct hns3_enet_ring *ring;
533 /* get stats for Tx */
534 for (i = 0; i < kinfo->num_tqps; i++) {
535 ring = &nic_priv->ring[i];
536 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
537 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
538 *data++ = *(u64 *)stat;
542 /* get stats for Rx */
543 for (i = 0; i < kinfo->num_tqps; i++) {
544 ring = &nic_priv->ring[i + kinfo->num_tqps];
545 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
546 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
547 *data++ = *(u64 *)stat;
554 /* hns3_get_stats - get detail statistics.
555 * @netdev: net device
556 * @stats: statistics info.
557 * @data: statistics data.
559 static void hns3_get_stats(struct net_device *netdev,
560 struct ethtool_stats *stats, u64 *data)
562 struct hnae3_handle *h = hns3_get_handle(netdev);
565 if (hns3_nic_resetting(netdev)) {
566 netdev_err(netdev, "dev resetting, could not get stats\n");
570 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
571 netdev_err(netdev, "could not get any statistics\n");
575 h->ae_algo->ops->update_stats(h, &netdev->stats);
577 /* get per-queue stats */
578 p = hns3_get_stats_tqps(h, p);
580 /* get MAC & other misc hardware stats */
581 h->ae_algo->ops->get_stats(h, p);
584 static void hns3_get_drvinfo(struct net_device *netdev,
585 struct ethtool_drvinfo *drvinfo)
587 struct hns3_nic_priv *priv = netdev_priv(netdev);
588 struct hnae3_handle *h = priv->ae_handle;
591 if (!h->ae_algo->ops->get_fw_version) {
592 netdev_err(netdev, "could not get fw version!\n");
596 strncpy(drvinfo->driver, h->pdev->driver->name,
597 sizeof(drvinfo->driver));
598 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
600 strncpy(drvinfo->bus_info, pci_name(h->pdev),
601 sizeof(drvinfo->bus_info));
602 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
604 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
606 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
608 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
609 HNAE3_FW_VERSION_BYTE3_SHIFT),
610 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
611 HNAE3_FW_VERSION_BYTE2_SHIFT),
612 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
613 HNAE3_FW_VERSION_BYTE1_SHIFT),
614 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
615 HNAE3_FW_VERSION_BYTE0_SHIFT));
618 static u32 hns3_get_link(struct net_device *netdev)
620 struct hnae3_handle *h = hns3_get_handle(netdev);
622 if (h->ae_algo->ops->get_status)
623 return h->ae_algo->ops->get_status(h);
628 static void hns3_get_ringparam(struct net_device *netdev,
629 struct ethtool_ringparam *param)
631 struct hns3_nic_priv *priv = netdev_priv(netdev);
632 struct hnae3_handle *h = priv->ae_handle;
633 int queue_num = h->kinfo.num_tqps;
635 if (hns3_nic_resetting(netdev)) {
636 netdev_err(netdev, "dev resetting!");
640 param->tx_max_pending = HNS3_RING_MAX_PENDING;
641 param->rx_max_pending = HNS3_RING_MAX_PENDING;
643 param->tx_pending = priv->ring[0].desc_num;
644 param->rx_pending = priv->ring[queue_num].desc_num;
647 static void hns3_get_pauseparam(struct net_device *netdev,
648 struct ethtool_pauseparam *param)
650 struct hnae3_handle *h = hns3_get_handle(netdev);
651 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
653 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
656 if (h->ae_algo->ops->get_pauseparam)
657 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
658 ¶m->rx_pause, ¶m->tx_pause);
661 static int hns3_set_pauseparam(struct net_device *netdev,
662 struct ethtool_pauseparam *param)
664 struct hnae3_handle *h = hns3_get_handle(netdev);
665 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
667 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
670 netif_dbg(h, drv, netdev,
671 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
672 param->autoneg, param->rx_pause, param->tx_pause);
674 if (h->ae_algo->ops->set_pauseparam)
675 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
681 static void hns3_get_ksettings(struct hnae3_handle *h,
682 struct ethtool_link_ksettings *cmd)
684 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
686 /* 1.auto_neg & speed & duplex from cmd */
687 if (ops->get_ksettings_an_result)
688 ops->get_ksettings_an_result(h,
693 /* 2.get link mode */
694 if (ops->get_link_mode)
695 ops->get_link_mode(h,
696 cmd->link_modes.supported,
697 cmd->link_modes.advertising);
699 /* 3.mdix_ctrl&mdix get from phy reg */
700 if (ops->get_mdix_mode)
701 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
702 &cmd->base.eth_tp_mdix);
705 static int hns3_get_link_ksettings(struct net_device *netdev,
706 struct ethtool_link_ksettings *cmd)
708 struct hnae3_handle *h = hns3_get_handle(netdev);
709 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
710 const struct hnae3_ae_ops *ops;
715 ops = h->ae_algo->ops;
716 if (ops->get_media_type)
717 ops->get_media_type(h, &media_type, &module_type);
721 switch (media_type) {
722 case HNAE3_MEDIA_TYPE_NONE:
723 cmd->base.port = PORT_NONE;
724 hns3_get_ksettings(h, cmd);
726 case HNAE3_MEDIA_TYPE_FIBER:
727 if (module_type == HNAE3_MODULE_TYPE_CR)
728 cmd->base.port = PORT_DA;
730 cmd->base.port = PORT_FIBRE;
732 hns3_get_ksettings(h, cmd);
734 case HNAE3_MEDIA_TYPE_BACKPLANE:
735 cmd->base.port = PORT_NONE;
736 hns3_get_ksettings(h, cmd);
738 case HNAE3_MEDIA_TYPE_COPPER:
739 cmd->base.port = PORT_TP;
740 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
741 ops->get_phy_link_ksettings)
742 ops->get_phy_link_ksettings(h, cmd);
743 else if (!netdev->phydev)
744 hns3_get_ksettings(h, cmd);
746 phy_ethtool_ksettings_get(netdev->phydev, cmd);
750 netdev_warn(netdev, "Unknown media type");
755 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
757 link_stat = hns3_get_link(netdev);
759 cmd->base.speed = SPEED_UNKNOWN;
760 cmd->base.duplex = DUPLEX_UNKNOWN;
766 static int hns3_check_ksettings_param(const struct net_device *netdev,
767 const struct ethtool_link_ksettings *cmd)
769 struct hnae3_handle *handle = hns3_get_handle(netdev);
770 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
771 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
772 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
778 /* hw doesn't support use specified speed and duplex to negotiate,
779 * unnecessary to check them when autoneg on.
781 if (cmd->base.autoneg)
784 if (ops->get_ksettings_an_result) {
785 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
786 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
787 cmd->base.duplex == duplex)
791 if (ops->get_media_type)
792 ops->get_media_type(handle, &media_type, &module_type);
794 if (cmd->base.duplex == DUPLEX_HALF &&
795 media_type != HNAE3_MEDIA_TYPE_COPPER) {
797 "only copper port supports half duplex!");
801 if (ops->check_port_speed) {
802 ret = ops->check_port_speed(handle, cmd->base.speed);
804 netdev_err(netdev, "unsupported speed\n");
812 static int hns3_set_link_ksettings(struct net_device *netdev,
813 const struct ethtool_link_ksettings *cmd)
815 struct hnae3_handle *handle = hns3_get_handle(netdev);
816 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
817 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
820 /* Chip don't support this mode. */
821 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
824 netif_dbg(handle, drv, netdev,
825 "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
826 netdev->phydev ? "phy" : "mac",
827 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
829 /* Only support ksettings_set for netdev with phy attached for now */
830 if (netdev->phydev) {
831 if (cmd->base.speed == SPEED_1000 &&
832 cmd->base.autoneg == AUTONEG_DISABLE)
835 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
836 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
837 ops->set_phy_link_ksettings) {
838 return ops->set_phy_link_ksettings(handle, cmd);
841 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
844 ret = hns3_check_ksettings_param(netdev, cmd);
848 if (ops->set_autoneg) {
849 ret = ops->set_autoneg(handle, cmd->base.autoneg);
854 /* hw doesn't support use specified speed and duplex to negotiate,
855 * ignore them when autoneg on.
857 if (cmd->base.autoneg) {
859 "autoneg is on, ignore the speed and duplex\n");
863 if (ops->cfg_mac_speed_dup_h)
864 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
870 static u32 hns3_get_rss_key_size(struct net_device *netdev)
872 struct hnae3_handle *h = hns3_get_handle(netdev);
874 if (!h->ae_algo->ops->get_rss_key_size)
877 return h->ae_algo->ops->get_rss_key_size(h);
880 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
882 struct hnae3_handle *h = hns3_get_handle(netdev);
883 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
885 return ae_dev->dev_specs.rss_ind_tbl_size;
888 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
891 struct hnae3_handle *h = hns3_get_handle(netdev);
893 if (!h->ae_algo->ops->get_rss)
896 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
899 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
900 const u8 *key, const u8 hfunc)
902 struct hnae3_handle *h = hns3_get_handle(netdev);
903 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
905 if (!h->ae_algo->ops->set_rss)
908 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
909 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
910 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
911 netdev_err(netdev, "hash func not supported\n");
917 "set rss failed for indir is empty\n");
921 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
924 static int hns3_get_rxnfc(struct net_device *netdev,
925 struct ethtool_rxnfc *cmd,
928 struct hnae3_handle *h = hns3_get_handle(netdev);
931 case ETHTOOL_GRXRINGS:
932 cmd->data = h->kinfo.num_tqps;
935 if (h->ae_algo->ops->get_rss_tuple)
936 return h->ae_algo->ops->get_rss_tuple(h, cmd);
938 case ETHTOOL_GRXCLSRLCNT:
939 if (h->ae_algo->ops->get_fd_rule_cnt)
940 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
942 case ETHTOOL_GRXCLSRULE:
943 if (h->ae_algo->ops->get_fd_rule_info)
944 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
946 case ETHTOOL_GRXCLSRLALL:
947 if (h->ae_algo->ops->get_fd_all_rules)
948 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
956 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
957 u32 tx_desc_num, u32 rx_desc_num)
959 struct hnae3_handle *h = priv->ae_handle;
962 h->kinfo.num_tx_desc = tx_desc_num;
963 h->kinfo.num_rx_desc = rx_desc_num;
965 for (i = 0; i < h->kinfo.num_tqps; i++) {
966 priv->ring[i].desc_num = tx_desc_num;
967 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
971 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
973 struct hnae3_handle *handle = priv->ae_handle;
974 struct hns3_enet_ring *tmp_rings;
977 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
978 sizeof(struct hns3_enet_ring), GFP_KERNEL);
982 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
983 memcpy(&tmp_rings[i], &priv->ring[i],
984 sizeof(struct hns3_enet_ring));
985 tmp_rings[i].skb = NULL;
991 static int hns3_check_ringparam(struct net_device *ndev,
992 struct ethtool_ringparam *param)
994 if (hns3_nic_resetting(ndev))
997 if (param->rx_mini_pending || param->rx_jumbo_pending)
1000 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1001 param->tx_pending < HNS3_RING_MIN_PENDING ||
1002 param->rx_pending > HNS3_RING_MAX_PENDING ||
1003 param->rx_pending < HNS3_RING_MIN_PENDING) {
1004 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1005 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1012 static int hns3_set_ringparam(struct net_device *ndev,
1013 struct ethtool_ringparam *param)
1015 struct hns3_nic_priv *priv = netdev_priv(ndev);
1016 struct hnae3_handle *h = priv->ae_handle;
1017 struct hns3_enet_ring *tmp_rings;
1018 bool if_running = netif_running(ndev);
1019 u32 old_tx_desc_num, new_tx_desc_num;
1020 u32 old_rx_desc_num, new_rx_desc_num;
1021 u16 queue_num = h->kinfo.num_tqps;
1024 ret = hns3_check_ringparam(ndev, param);
1028 /* Hardware requires that its descriptors must be multiple of eight */
1029 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1030 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1031 old_tx_desc_num = priv->ring[0].desc_num;
1032 old_rx_desc_num = priv->ring[queue_num].desc_num;
1033 if (old_tx_desc_num == new_tx_desc_num &&
1034 old_rx_desc_num == new_rx_desc_num)
1037 tmp_rings = hns3_backup_ringparam(priv);
1040 "backup ring param failed by allocating memory fail\n");
1045 "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
1046 old_tx_desc_num, old_rx_desc_num,
1047 new_tx_desc_num, new_rx_desc_num);
1050 ndev->netdev_ops->ndo_stop(ndev);
1052 hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1053 ret = hns3_init_all_ring(priv);
1055 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1058 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1060 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1061 memcpy(&priv->ring[i], &tmp_rings[i],
1062 sizeof(struct hns3_enet_ring));
1064 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1065 hns3_fini_ring(&tmp_rings[i]);
1071 ret = ndev->netdev_ops->ndo_open(ndev);
1076 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1078 struct hnae3_handle *h = hns3_get_handle(netdev);
1082 if (h->ae_algo->ops->set_rss_tuple)
1083 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1085 case ETHTOOL_SRXCLSRLINS:
1086 if (h->ae_algo->ops->add_fd_entry)
1087 return h->ae_algo->ops->add_fd_entry(h, cmd);
1089 case ETHTOOL_SRXCLSRLDEL:
1090 if (h->ae_algo->ops->del_fd_entry)
1091 return h->ae_algo->ops->del_fd_entry(h, cmd);
1098 static int hns3_nway_reset(struct net_device *netdev)
1100 struct hnae3_handle *handle = hns3_get_handle(netdev);
1101 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1102 struct phy_device *phy = netdev->phydev;
1105 if (!netif_running(netdev))
1108 if (hns3_nic_resetting(netdev)) {
1109 netdev_err(netdev, "dev resetting!");
1113 if (!ops->get_autoneg || !ops->restart_autoneg)
1116 autoneg = ops->get_autoneg(handle);
1117 if (autoneg != AUTONEG_ENABLE) {
1119 "Autoneg is off, don't support to restart it\n");
1123 netif_dbg(handle, drv, netdev,
1124 "nway reset (using %s)\n", phy ? "phy" : "mac");
1127 return genphy_restart_aneg(phy);
1129 return ops->restart_autoneg(handle);
1132 static void hns3_get_channels(struct net_device *netdev,
1133 struct ethtool_channels *ch)
1135 struct hnae3_handle *h = hns3_get_handle(netdev);
1137 if (h->ae_algo->ops->get_channels)
1138 h->ae_algo->ops->get_channels(h, ch);
1141 static int hns3_get_coalesce(struct net_device *netdev,
1142 struct ethtool_coalesce *cmd)
1144 struct hns3_nic_priv *priv = netdev_priv(netdev);
1145 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1146 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1147 struct hnae3_handle *h = priv->ae_handle;
1149 if (hns3_nic_resetting(netdev))
1152 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1153 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1155 cmd->tx_coalesce_usecs = tx_coal->int_gl;
1156 cmd->rx_coalesce_usecs = rx_coal->int_gl;
1158 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1159 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1161 cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1162 cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1167 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1168 struct ethtool_coalesce *cmd)
1170 struct hnae3_handle *handle = hns3_get_handle(netdev);
1171 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1174 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1176 "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1177 ae_dev->dev_specs.max_int_gl);
1181 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1183 "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1184 ae_dev->dev_specs.max_int_gl);
1188 /* device version above V3(include V3), GL uses 1us unit,
1189 * so the round down is not needed.
1191 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1194 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1195 if (rx_gl != cmd->rx_coalesce_usecs) {
1197 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1198 cmd->rx_coalesce_usecs, rx_gl);
1201 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1202 if (tx_gl != cmd->tx_coalesce_usecs) {
1204 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1205 cmd->tx_coalesce_usecs, tx_gl);
1211 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1212 struct ethtool_coalesce *cmd)
1216 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1218 "tx_usecs_high must be same as rx_usecs_high.\n");
1222 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1224 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1229 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1230 if (rl != cmd->rx_coalesce_usecs_high) {
1232 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1233 cmd->rx_coalesce_usecs_high, rl);
1239 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1240 struct ethtool_coalesce *cmd)
1242 struct hnae3_handle *handle = hns3_get_handle(netdev);
1243 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1245 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1246 !ae_dev->dev_specs.int_ql_max) {
1247 netdev_err(netdev, "coalesced frames is not supported\n");
1251 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1252 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1254 "invalid coalesced_frames value, range is 0-%u\n",
1255 ae_dev->dev_specs.int_ql_max);
1262 static int hns3_check_coalesce_para(struct net_device *netdev,
1263 struct ethtool_coalesce *cmd)
1267 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1270 "Check gl coalesce param fail. ret = %d\n", ret);
1274 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1277 "Check rl coalesce param fail. ret = %d\n", ret);
1281 return hns3_check_ql_coalesce_param(netdev, cmd);
1284 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1285 struct ethtool_coalesce *cmd,
1288 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1289 struct hns3_nic_priv *priv = netdev_priv(netdev);
1290 struct hnae3_handle *h = priv->ae_handle;
1291 int queue_num = h->kinfo.num_tqps;
1293 tx_vector = priv->ring[queue].tqp_vector;
1294 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1296 tx_vector->tx_group.coal.adapt_enable =
1297 cmd->use_adaptive_tx_coalesce;
1298 rx_vector->rx_group.coal.adapt_enable =
1299 cmd->use_adaptive_rx_coalesce;
1301 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1302 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1304 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1305 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1307 hns3_set_vector_coalesce_tx_gl(tx_vector,
1308 tx_vector->tx_group.coal.int_gl);
1309 hns3_set_vector_coalesce_rx_gl(rx_vector,
1310 rx_vector->rx_group.coal.int_gl);
1312 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1313 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1315 if (tx_vector->tx_group.coal.ql_enable)
1316 hns3_set_vector_coalesce_tx_ql(tx_vector,
1317 tx_vector->tx_group.coal.int_ql);
1318 if (rx_vector->rx_group.coal.ql_enable)
1319 hns3_set_vector_coalesce_rx_ql(rx_vector,
1320 rx_vector->rx_group.coal.int_ql);
1323 static int hns3_set_coalesce(struct net_device *netdev,
1324 struct ethtool_coalesce *cmd)
1326 struct hnae3_handle *h = hns3_get_handle(netdev);
1327 struct hns3_nic_priv *priv = netdev_priv(netdev);
1328 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1329 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1330 u16 queue_num = h->kinfo.num_tqps;
1334 if (hns3_nic_resetting(netdev))
1337 ret = hns3_check_coalesce_para(netdev, cmd);
1341 h->kinfo.int_rl_setting =
1342 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1344 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1345 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1347 tx_coal->int_gl = cmd->tx_coalesce_usecs;
1348 rx_coal->int_gl = cmd->rx_coalesce_usecs;
1350 tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1351 rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1353 for (i = 0; i < queue_num; i++)
1354 hns3_set_coalesce_per_queue(netdev, cmd, i);
1359 static int hns3_get_regs_len(struct net_device *netdev)
1361 struct hnae3_handle *h = hns3_get_handle(netdev);
1363 if (!h->ae_algo->ops->get_regs_len)
1366 return h->ae_algo->ops->get_regs_len(h);
1369 static void hns3_get_regs(struct net_device *netdev,
1370 struct ethtool_regs *cmd, void *data)
1372 struct hnae3_handle *h = hns3_get_handle(netdev);
1374 if (!h->ae_algo->ops->get_regs)
1377 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1380 static int hns3_set_phys_id(struct net_device *netdev,
1381 enum ethtool_phys_id_state state)
1383 struct hnae3_handle *h = hns3_get_handle(netdev);
1385 if (!h->ae_algo->ops->set_led_id)
1388 return h->ae_algo->ops->set_led_id(h, state);
1391 static u32 hns3_get_msglevel(struct net_device *netdev)
1393 struct hnae3_handle *h = hns3_get_handle(netdev);
1395 return h->msg_enable;
1398 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1400 struct hnae3_handle *h = hns3_get_handle(netdev);
1402 h->msg_enable = msg_level;
1405 /* Translate local fec value into ethtool value. */
1406 static unsigned int loc_to_eth_fec(u8 loc_fec)
1410 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1411 eth_fec |= ETHTOOL_FEC_AUTO;
1412 if (loc_fec & BIT(HNAE3_FEC_RS))
1413 eth_fec |= ETHTOOL_FEC_RS;
1414 if (loc_fec & BIT(HNAE3_FEC_BASER))
1415 eth_fec |= ETHTOOL_FEC_BASER;
1417 /* if nothing is set, then FEC is off */
1419 eth_fec = ETHTOOL_FEC_OFF;
1424 /* Translate ethtool fec value into local value. */
1425 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1429 if (eth_fec & ETHTOOL_FEC_OFF)
1432 if (eth_fec & ETHTOOL_FEC_AUTO)
1433 loc_fec |= BIT(HNAE3_FEC_AUTO);
1434 if (eth_fec & ETHTOOL_FEC_RS)
1435 loc_fec |= BIT(HNAE3_FEC_RS);
1436 if (eth_fec & ETHTOOL_FEC_BASER)
1437 loc_fec |= BIT(HNAE3_FEC_BASER);
1442 static int hns3_get_fecparam(struct net_device *netdev,
1443 struct ethtool_fecparam *fec)
1445 struct hnae3_handle *handle = hns3_get_handle(netdev);
1446 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1447 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1451 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1457 ops->get_fec(handle, &fec_ability, &fec_mode);
1459 fec->fec = loc_to_eth_fec(fec_ability);
1460 fec->active_fec = loc_to_eth_fec(fec_mode);
1465 static int hns3_set_fecparam(struct net_device *netdev,
1466 struct ethtool_fecparam *fec)
1468 struct hnae3_handle *handle = hns3_get_handle(netdev);
1469 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1470 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1473 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1478 fec_mode = eth_to_loc_fec(fec->fec);
1480 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1482 return ops->set_fec(handle, fec_mode);
1485 static int hns3_get_module_info(struct net_device *netdev,
1486 struct ethtool_modinfo *modinfo)
1488 #define HNS3_SFF_8636_V1_3 0x03
1490 struct hnae3_handle *handle = hns3_get_handle(netdev);
1491 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1492 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1493 struct hns3_sfp_type sfp_type;
1496 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1497 !ops->get_module_eeprom)
1500 memset(&sfp_type, 0, sizeof(sfp_type));
1501 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1506 switch (sfp_type.type) {
1507 case SFF8024_ID_SFP:
1508 modinfo->type = ETH_MODULE_SFF_8472;
1509 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1511 case SFF8024_ID_QSFP_8438:
1512 modinfo->type = ETH_MODULE_SFF_8436;
1513 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1515 case SFF8024_ID_QSFP_8436_8636:
1516 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1517 modinfo->type = ETH_MODULE_SFF_8436;
1518 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1520 modinfo->type = ETH_MODULE_SFF_8636;
1521 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1524 case SFF8024_ID_QSFP28_8636:
1525 modinfo->type = ETH_MODULE_SFF_8636;
1526 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1529 netdev_err(netdev, "Optical module unknown: %#x\n",
1537 static int hns3_get_module_eeprom(struct net_device *netdev,
1538 struct ethtool_eeprom *ee, u8 *data)
1540 struct hnae3_handle *handle = hns3_get_handle(netdev);
1541 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1542 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1544 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1545 !ops->get_module_eeprom)
1551 memset(data, 0, ee->len);
1553 return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1556 static u32 hns3_get_priv_flags(struct net_device *netdev)
1558 struct hnae3_handle *handle = hns3_get_handle(netdev);
1560 return handle->priv_flags;
1563 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1567 for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1568 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1569 netdev_err(h->netdev, "%s is unsupported\n",
1570 hns3_priv_flags[i].name);
1577 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1579 struct hnae3_handle *handle = hns3_get_handle(netdev);
1580 u32 changed = pflags ^ handle->priv_flags;
1584 ret = hns3_check_priv_flags(handle, changed);
1588 for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1589 if (changed & BIT(i)) {
1590 bool enable = !(handle->priv_flags & BIT(i));
1593 handle->priv_flags |= BIT(i);
1595 handle->priv_flags &= ~BIT(i);
1596 hns3_priv_flags[i].handler(netdev, enable);
1603 static int hns3_get_tunable(struct net_device *netdev,
1604 const struct ethtool_tunable *tuna,
1607 struct hns3_nic_priv *priv = netdev_priv(netdev);
1611 case ETHTOOL_TX_COPYBREAK:
1612 /* all the tx rings have the same tx_copybreak */
1613 *(u32 *)data = priv->tx_copybreak;
1615 case ETHTOOL_RX_COPYBREAK:
1616 *(u32 *)data = priv->rx_copybreak;
1626 static int hns3_set_tunable(struct net_device *netdev,
1627 const struct ethtool_tunable *tuna,
1630 struct hns3_nic_priv *priv = netdev_priv(netdev);
1631 struct hnae3_handle *h = priv->ae_handle;
1635 case ETHTOOL_TX_COPYBREAK:
1636 priv->tx_copybreak = *(u32 *)data;
1638 for (i = 0; i < h->kinfo.num_tqps; i++)
1639 priv->ring[i].tx_copybreak = priv->tx_copybreak;
1642 case ETHTOOL_RX_COPYBREAK:
1643 priv->rx_copybreak = *(u32 *)data;
1645 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1646 priv->ring[i].rx_copybreak = priv->rx_copybreak;
1657 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \
1658 ETHTOOL_COALESCE_USE_ADAPTIVE | \
1659 ETHTOOL_COALESCE_RX_USECS_HIGH | \
1660 ETHTOOL_COALESCE_TX_USECS_HIGH | \
1661 ETHTOOL_COALESCE_MAX_FRAMES)
1663 static int hns3_get_ts_info(struct net_device *netdev,
1664 struct ethtool_ts_info *info)
1666 struct hnae3_handle *handle = hns3_get_handle(netdev);
1668 if (handle->ae_algo->ops->get_ts_info)
1669 return handle->ae_algo->ops->get_ts_info(handle, info);
1671 return ethtool_op_get_ts_info(netdev, info);
1674 static const struct ethtool_ops hns3vf_ethtool_ops = {
1675 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1676 .get_drvinfo = hns3_get_drvinfo,
1677 .get_ringparam = hns3_get_ringparam,
1678 .set_ringparam = hns3_set_ringparam,
1679 .get_strings = hns3_get_strings,
1680 .get_ethtool_stats = hns3_get_stats,
1681 .get_sset_count = hns3_get_sset_count,
1682 .get_rxnfc = hns3_get_rxnfc,
1683 .set_rxnfc = hns3_set_rxnfc,
1684 .get_rxfh_key_size = hns3_get_rss_key_size,
1685 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1686 .get_rxfh = hns3_get_rss,
1687 .set_rxfh = hns3_set_rss,
1688 .get_link_ksettings = hns3_get_link_ksettings,
1689 .get_channels = hns3_get_channels,
1690 .set_channels = hns3_set_channels,
1691 .get_coalesce = hns3_get_coalesce,
1692 .set_coalesce = hns3_set_coalesce,
1693 .get_regs_len = hns3_get_regs_len,
1694 .get_regs = hns3_get_regs,
1695 .get_link = hns3_get_link,
1696 .get_msglevel = hns3_get_msglevel,
1697 .set_msglevel = hns3_set_msglevel,
1698 .get_priv_flags = hns3_get_priv_flags,
1699 .set_priv_flags = hns3_set_priv_flags,
1700 .get_tunable = hns3_get_tunable,
1701 .set_tunable = hns3_set_tunable,
1704 static const struct ethtool_ops hns3_ethtool_ops = {
1705 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1706 .self_test = hns3_self_test,
1707 .get_drvinfo = hns3_get_drvinfo,
1708 .get_link = hns3_get_link,
1709 .get_ringparam = hns3_get_ringparam,
1710 .set_ringparam = hns3_set_ringparam,
1711 .get_pauseparam = hns3_get_pauseparam,
1712 .set_pauseparam = hns3_set_pauseparam,
1713 .get_strings = hns3_get_strings,
1714 .get_ethtool_stats = hns3_get_stats,
1715 .get_sset_count = hns3_get_sset_count,
1716 .get_rxnfc = hns3_get_rxnfc,
1717 .set_rxnfc = hns3_set_rxnfc,
1718 .get_rxfh_key_size = hns3_get_rss_key_size,
1719 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1720 .get_rxfh = hns3_get_rss,
1721 .set_rxfh = hns3_set_rss,
1722 .get_link_ksettings = hns3_get_link_ksettings,
1723 .set_link_ksettings = hns3_set_link_ksettings,
1724 .nway_reset = hns3_nway_reset,
1725 .get_channels = hns3_get_channels,
1726 .set_channels = hns3_set_channels,
1727 .get_coalesce = hns3_get_coalesce,
1728 .set_coalesce = hns3_set_coalesce,
1729 .get_regs_len = hns3_get_regs_len,
1730 .get_regs = hns3_get_regs,
1731 .set_phys_id = hns3_set_phys_id,
1732 .get_msglevel = hns3_get_msglevel,
1733 .set_msglevel = hns3_set_msglevel,
1734 .get_fecparam = hns3_get_fecparam,
1735 .set_fecparam = hns3_set_fecparam,
1736 .get_module_info = hns3_get_module_info,
1737 .get_module_eeprom = hns3_get_module_eeprom,
1738 .get_priv_flags = hns3_get_priv_flags,
1739 .set_priv_flags = hns3_set_priv_flags,
1740 .get_ts_info = hns3_get_ts_info,
1741 .get_tunable = hns3_get_tunable,
1742 .set_tunable = hns3_set_tunable,
1745 void hns3_ethtool_set_ops(struct net_device *netdev)
1747 struct hnae3_handle *h = hns3_get_handle(netdev);
1749 if (h->flags & HNAE3_SUPPORT_VF)
1750 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1752 netdev->ethtool_ops = &hns3_ethtool_ops;