1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
4 #include <linux/etherdevice.h>
5 #include <linux/string.h>
10 #include "hns3_ethtool.h"
12 /* tqp related stats */
13 #define HNS3_TQP_STAT(_string, _member) { \
14 .stats_string = _string, \
15 .stats_offset = offsetof(struct hns3_enet_ring, stats) +\
16 offsetof(struct ring_stats, _member), \
19 static const struct hns3_stats hns3_txq_stats[] = {
20 /* Tx per-queue statistics */
21 HNS3_TQP_STAT("dropped", sw_err_cnt),
22 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
23 HNS3_TQP_STAT("packets", tx_pkts),
24 HNS3_TQP_STAT("bytes", tx_bytes),
25 HNS3_TQP_STAT("more", tx_more),
26 HNS3_TQP_STAT("wake", restart_queue),
27 HNS3_TQP_STAT("busy", tx_busy),
28 HNS3_TQP_STAT("copy", tx_copy),
29 HNS3_TQP_STAT("vlan_err", tx_vlan_err),
30 HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
31 HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
32 HNS3_TQP_STAT("tso_err", tx_tso_err),
33 HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
34 HNS3_TQP_STAT("hw_limitation", hw_limitation),
35 HNS3_TQP_STAT("bounce", tx_bounce),
36 HNS3_TQP_STAT("spare_full", tx_spare_full),
37 HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
38 HNS3_TQP_STAT("sgl", tx_sgl),
39 HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
40 HNS3_TQP_STAT("map_sg_err", map_sg_err),
43 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
45 static const struct hns3_stats hns3_rxq_stats[] = {
46 /* Rx per-queue statistics */
47 HNS3_TQP_STAT("dropped", sw_err_cnt),
48 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
49 HNS3_TQP_STAT("packets", rx_pkts),
50 HNS3_TQP_STAT("bytes", rx_bytes),
51 HNS3_TQP_STAT("errors", rx_err_cnt),
52 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
53 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
54 HNS3_TQP_STAT("err_bd_num", err_bd_num),
55 HNS3_TQP_STAT("l2_err", l2_err),
56 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
57 HNS3_TQP_STAT("csum_complete", csum_complete),
58 HNS3_TQP_STAT("multicast", rx_multicast),
59 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
60 HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
61 HNS3_TQP_STAT("frag_alloc", frag_alloc),
64 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
66 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
68 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
70 #define HNS3_SELF_TEST_TYPE_NUM 4
71 #define HNS3_NIC_LB_TEST_PKT_NUM 1
72 #define HNS3_NIC_LB_TEST_RING_ID 0
73 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
74 #define HNS3_NIC_LB_SETUP_USEC 10000
76 /* Nic loopback test err */
77 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
78 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
79 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
81 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
83 struct hnae3_handle *h = hns3_get_handle(ndev);
84 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
87 if (!h->ae_algo->ops->set_loopback ||
88 !h->ae_algo->ops->set_promisc_mode)
92 case HNAE3_LOOP_SERIAL_SERDES:
93 case HNAE3_LOOP_PARALLEL_SERDES:
96 ret = h->ae_algo->ops->set_loopback(h, loop, en);
103 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
107 h->ae_algo->ops->set_promisc_mode(h, true, true);
109 /* recover promisc mode before loopback test */
110 hns3_request_update_promisc_mode(h);
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 struct hnae3_ae_dev *ae_dev;
152 unsigned char *packet;
156 skb_reserve(skb, NET_IP_ALIGN);
157 ethh = skb_put(skb, sizeof(struct ethhdr));
158 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
160 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
162 /* The dst mac addr of loopback packet is the same as the host'
163 * mac addr, the SSU component may loop back the packet to host
164 * before the packet reaches mac or serdes, which will defect
165 * the purpose of mac or serdes selftest.
167 handle = hns3_get_handle(ndev);
168 ae_dev = pci_get_drvdata(handle->pdev);
169 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
170 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
171 eth_zero_addr(ethh->h_source);
172 ethh->h_proto = htons(ETH_P_ARP);
173 skb_reset_mac_header(skb);
175 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
176 packet[i] = (unsigned char)(i & 0xff);
179 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
182 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
183 unsigned char *packet = skb->data;
184 u32 len = skb_headlen(skb);
187 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
189 for (i = 0; i < len; i++)
190 if (packet[i] != (unsigned char)(i & 0xff))
193 /* The packet is correctly received */
194 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
195 tqp_vector->rx_group.total_packets++;
197 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
198 skb->data, len, true);
200 dev_kfree_skb_any(skb);
203 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
205 struct hnae3_handle *h = priv->ae_handle;
206 struct hnae3_knic_private_info *kinfo;
207 u32 i, rcv_good_pkt_total = 0;
210 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
211 struct hns3_enet_ring *ring = &priv->ring[i];
212 struct hns3_enet_ring_group *rx_group;
215 rx_group = &ring->tqp_vector->rx_group;
216 pre_rx_pkt = rx_group->total_packets;
219 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
222 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
223 rx_group->total_packets = pre_rx_pkt;
225 return rcv_good_pkt_total;
228 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
229 u32 end_ringid, u32 budget)
233 for (i = start_ringid; i <= end_ringid; i++) {
234 struct hns3_enet_ring *ring = &priv->ring[i];
236 hns3_clean_tx_ring(ring, 0);
241 * hns3_lp_run_test - run loopback test
243 * @mode: loopback type
245 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
247 struct hns3_nic_priv *priv = netdev_priv(ndev);
252 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
255 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
258 hns3_lp_setup_skb(skb);
259 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
262 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
266 tx_ret = hns3_nic_net_xmit(skb, ndev);
267 if (tx_ret == NETDEV_TX_OK) {
271 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
275 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
276 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
277 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
278 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
282 /* Allow 200 milliseconds for packets to go from Tx to Rx */
285 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
286 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
287 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
288 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
289 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
293 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
294 HNS3_NIC_LB_TEST_RING_ID,
295 HNS3_NIC_LB_TEST_PKT_NUM);
302 * hns3_self_test - self test
304 * @eth_test: test cmd
307 static void hns3_self_test(struct net_device *ndev,
308 struct ethtool_test *eth_test, u64 *data)
310 struct hns3_nic_priv *priv = netdev_priv(ndev);
311 struct hnae3_handle *h = priv->ae_handle;
312 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
313 bool if_running = netif_running(ndev);
317 if (hns3_nic_resetting(ndev)) {
318 netdev_err(ndev, "dev resetting!");
322 /* Only do offline selftest, or pass by default */
323 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
326 netif_dbg(h, drv, ndev, "self test start");
328 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
329 st_param[HNAE3_LOOP_APP][1] =
330 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
332 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
333 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
334 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
336 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
337 HNAE3_LOOP_PARALLEL_SERDES;
338 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
339 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
341 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
342 st_param[HNAE3_LOOP_PHY][1] =
343 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
346 ndev->netdev_ops->ndo_stop(ndev);
348 #if IS_ENABLED(CONFIG_VLAN_8021Q)
349 /* Disable the vlan filter for selftest does not support it */
350 if (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)
387 if (h->ae_algo->ops->enable_vlan_filter)
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 void hns3_update_limit_promisc_mode(struct net_device *netdev,
400 struct hnae3_handle *handle = hns3_get_handle(netdev);
403 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
405 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
407 hns3_request_update_promisc_mode(handle);
410 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
411 { "limit_promisc", hns3_update_limit_promisc_mode }
414 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
416 struct hnae3_handle *h = hns3_get_handle(netdev);
417 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
419 if (!ops->get_sset_count)
424 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
425 ops->get_sset_count(h, stringset));
428 return ops->get_sset_count(h, stringset);
430 case ETH_SS_PRIV_FLAGS:
431 return HNAE3_PFLAG_MAX;
438 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
439 u32 stat_count, u32 num_tqps, const char *prefix)
441 #define MAX_PREFIX_SIZE (6 + 4)
446 for (i = 0; i < num_tqps; i++) {
447 for (j = 0; j < stat_count; j++) {
448 data[ETH_GSTRING_LEN - 1] = '\0';
450 /* first, prepend the prefix string */
451 n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
453 size_left = (ETH_GSTRING_LEN - 1) - n1;
455 /* now, concatenate the stats string to it */
456 strncat(data, stats[j].stats_string, size_left);
457 data += ETH_GSTRING_LEN;
464 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
466 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
467 const char tx_prefix[] = "txq";
468 const char rx_prefix[] = "rxq";
470 /* get strings for Tx */
471 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
472 kinfo->num_tqps, tx_prefix);
474 /* get strings for Rx */
475 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
476 kinfo->num_tqps, rx_prefix);
481 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
483 struct hnae3_handle *h = hns3_get_handle(netdev);
484 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
485 char *buff = (char *)data;
488 if (!ops->get_strings)
493 buff = hns3_get_strings_tqps(h, buff);
494 ops->get_strings(h, stringset, (u8 *)buff);
497 ops->get_strings(h, stringset, data);
499 case ETH_SS_PRIV_FLAGS:
500 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
501 snprintf(buff, ETH_GSTRING_LEN, "%s",
502 hns3_priv_flags[i].name);
503 buff += ETH_GSTRING_LEN;
511 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
513 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
514 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
515 struct hns3_enet_ring *ring;
519 /* get stats for Tx */
520 for (i = 0; i < kinfo->num_tqps; i++) {
521 ring = &nic_priv->ring[i];
522 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
523 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
524 *data++ = *(u64 *)stat;
528 /* get stats for Rx */
529 for (i = 0; i < kinfo->num_tqps; i++) {
530 ring = &nic_priv->ring[i + kinfo->num_tqps];
531 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
532 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
533 *data++ = *(u64 *)stat;
540 /* hns3_get_stats - get detail statistics.
541 * @netdev: net device
542 * @stats: statistics info.
543 * @data: statistics data.
545 static void hns3_get_stats(struct net_device *netdev,
546 struct ethtool_stats *stats, u64 *data)
548 struct hnae3_handle *h = hns3_get_handle(netdev);
551 if (hns3_nic_resetting(netdev)) {
552 netdev_err(netdev, "dev resetting, could not get stats\n");
556 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
557 netdev_err(netdev, "could not get any statistics\n");
561 h->ae_algo->ops->update_stats(h, &netdev->stats);
563 /* get per-queue stats */
564 p = hns3_get_stats_tqps(h, p);
566 /* get MAC & other misc hardware stats */
567 h->ae_algo->ops->get_stats(h, p);
570 static void hns3_get_drvinfo(struct net_device *netdev,
571 struct ethtool_drvinfo *drvinfo)
573 struct hns3_nic_priv *priv = netdev_priv(netdev);
574 struct hnae3_handle *h = priv->ae_handle;
577 if (!h->ae_algo->ops->get_fw_version) {
578 netdev_err(netdev, "could not get fw version!\n");
582 strncpy(drvinfo->driver, h->pdev->driver->name,
583 sizeof(drvinfo->driver));
584 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
586 strncpy(drvinfo->bus_info, pci_name(h->pdev),
587 sizeof(drvinfo->bus_info));
588 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
590 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
592 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
594 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
595 HNAE3_FW_VERSION_BYTE3_SHIFT),
596 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
597 HNAE3_FW_VERSION_BYTE2_SHIFT),
598 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
599 HNAE3_FW_VERSION_BYTE1_SHIFT),
600 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
601 HNAE3_FW_VERSION_BYTE0_SHIFT));
604 static u32 hns3_get_link(struct net_device *netdev)
606 struct hnae3_handle *h = hns3_get_handle(netdev);
608 if (h->ae_algo->ops->get_status)
609 return h->ae_algo->ops->get_status(h);
614 static void hns3_get_ringparam(struct net_device *netdev,
615 struct ethtool_ringparam *param)
617 struct hns3_nic_priv *priv = netdev_priv(netdev);
618 struct hnae3_handle *h = priv->ae_handle;
619 int queue_num = h->kinfo.num_tqps;
621 if (hns3_nic_resetting(netdev)) {
622 netdev_err(netdev, "dev resetting!");
626 param->tx_max_pending = HNS3_RING_MAX_PENDING;
627 param->rx_max_pending = HNS3_RING_MAX_PENDING;
629 param->tx_pending = priv->ring[0].desc_num;
630 param->rx_pending = priv->ring[queue_num].desc_num;
633 static void hns3_get_pauseparam(struct net_device *netdev,
634 struct ethtool_pauseparam *param)
636 struct hnae3_handle *h = hns3_get_handle(netdev);
637 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
639 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
642 if (h->ae_algo->ops->get_pauseparam)
643 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
644 ¶m->rx_pause, ¶m->tx_pause);
647 static int hns3_set_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 netif_dbg(h, drv, netdev,
657 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
658 param->autoneg, param->rx_pause, param->tx_pause);
660 if (h->ae_algo->ops->set_pauseparam)
661 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
667 static void hns3_get_ksettings(struct hnae3_handle *h,
668 struct ethtool_link_ksettings *cmd)
670 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
672 /* 1.auto_neg & speed & duplex from cmd */
673 if (ops->get_ksettings_an_result)
674 ops->get_ksettings_an_result(h,
679 /* 2.get link mode */
680 if (ops->get_link_mode)
681 ops->get_link_mode(h,
682 cmd->link_modes.supported,
683 cmd->link_modes.advertising);
685 /* 3.mdix_ctrl&mdix get from phy reg */
686 if (ops->get_mdix_mode)
687 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
688 &cmd->base.eth_tp_mdix);
691 static int hns3_get_link_ksettings(struct net_device *netdev,
692 struct ethtool_link_ksettings *cmd)
694 struct hnae3_handle *h = hns3_get_handle(netdev);
695 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
696 const struct hnae3_ae_ops *ops;
701 ops = h->ae_algo->ops;
702 if (ops->get_media_type)
703 ops->get_media_type(h, &media_type, &module_type);
707 switch (media_type) {
708 case HNAE3_MEDIA_TYPE_NONE:
709 cmd->base.port = PORT_NONE;
710 hns3_get_ksettings(h, cmd);
712 case HNAE3_MEDIA_TYPE_FIBER:
713 if (module_type == HNAE3_MODULE_TYPE_CR)
714 cmd->base.port = PORT_DA;
716 cmd->base.port = PORT_FIBRE;
718 hns3_get_ksettings(h, cmd);
720 case HNAE3_MEDIA_TYPE_BACKPLANE:
721 cmd->base.port = PORT_NONE;
722 hns3_get_ksettings(h, cmd);
724 case HNAE3_MEDIA_TYPE_COPPER:
725 cmd->base.port = PORT_TP;
726 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
727 ops->get_phy_link_ksettings)
728 ops->get_phy_link_ksettings(h, cmd);
729 else if (!netdev->phydev)
730 hns3_get_ksettings(h, cmd);
732 phy_ethtool_ksettings_get(netdev->phydev, cmd);
736 netdev_warn(netdev, "Unknown media type");
741 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
743 link_stat = hns3_get_link(netdev);
745 cmd->base.speed = SPEED_UNKNOWN;
746 cmd->base.duplex = DUPLEX_UNKNOWN;
752 static int hns3_check_ksettings_param(const struct net_device *netdev,
753 const struct ethtool_link_ksettings *cmd)
755 struct hnae3_handle *handle = hns3_get_handle(netdev);
756 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
757 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
758 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
764 /* hw doesn't support use specified speed and duplex to negotiate,
765 * unnecessary to check them when autoneg on.
767 if (cmd->base.autoneg)
770 if (ops->get_ksettings_an_result) {
771 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
772 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
773 cmd->base.duplex == duplex)
777 if (ops->get_media_type)
778 ops->get_media_type(handle, &media_type, &module_type);
780 if (cmd->base.duplex == DUPLEX_HALF &&
781 media_type != HNAE3_MEDIA_TYPE_COPPER) {
783 "only copper port supports half duplex!");
787 if (ops->check_port_speed) {
788 ret = ops->check_port_speed(handle, cmd->base.speed);
790 netdev_err(netdev, "unsupported speed\n");
798 static int hns3_set_link_ksettings(struct net_device *netdev,
799 const struct ethtool_link_ksettings *cmd)
801 struct hnae3_handle *handle = hns3_get_handle(netdev);
802 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
803 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
806 /* Chip don't support this mode. */
807 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
810 netif_dbg(handle, drv, netdev,
811 "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
812 netdev->phydev ? "phy" : "mac",
813 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
815 /* Only support ksettings_set for netdev with phy attached for now */
816 if (netdev->phydev) {
817 if (cmd->base.speed == SPEED_1000 &&
818 cmd->base.autoneg == AUTONEG_DISABLE)
821 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
822 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
823 ops->set_phy_link_ksettings) {
824 return ops->set_phy_link_ksettings(handle, cmd);
827 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
830 ret = hns3_check_ksettings_param(netdev, cmd);
834 if (ops->set_autoneg) {
835 ret = ops->set_autoneg(handle, cmd->base.autoneg);
840 /* hw doesn't support use specified speed and duplex to negotiate,
841 * ignore them when autoneg on.
843 if (cmd->base.autoneg) {
845 "autoneg is on, ignore the speed and duplex\n");
849 if (ops->cfg_mac_speed_dup_h)
850 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
856 static u32 hns3_get_rss_key_size(struct net_device *netdev)
858 struct hnae3_handle *h = hns3_get_handle(netdev);
860 if (!h->ae_algo->ops->get_rss_key_size)
863 return h->ae_algo->ops->get_rss_key_size(h);
866 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
868 struct hnae3_handle *h = hns3_get_handle(netdev);
869 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
871 return ae_dev->dev_specs.rss_ind_tbl_size;
874 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
877 struct hnae3_handle *h = hns3_get_handle(netdev);
879 if (!h->ae_algo->ops->get_rss)
882 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
885 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
886 const u8 *key, const u8 hfunc)
888 struct hnae3_handle *h = hns3_get_handle(netdev);
889 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
891 if (!h->ae_algo->ops->set_rss)
894 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
895 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
896 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
897 netdev_err(netdev, "hash func not supported\n");
903 "set rss failed for indir is empty\n");
907 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
910 static int hns3_get_rxnfc(struct net_device *netdev,
911 struct ethtool_rxnfc *cmd,
914 struct hnae3_handle *h = hns3_get_handle(netdev);
917 case ETHTOOL_GRXRINGS:
918 cmd->data = h->kinfo.num_tqps;
921 if (h->ae_algo->ops->get_rss_tuple)
922 return h->ae_algo->ops->get_rss_tuple(h, cmd);
924 case ETHTOOL_GRXCLSRLCNT:
925 if (h->ae_algo->ops->get_fd_rule_cnt)
926 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
928 case ETHTOOL_GRXCLSRULE:
929 if (h->ae_algo->ops->get_fd_rule_info)
930 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
932 case ETHTOOL_GRXCLSRLALL:
933 if (h->ae_algo->ops->get_fd_all_rules)
934 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
942 static const struct hns3_reset_type_map hns3_reset_type[] = {
943 {ETH_RESET_MGMT, HNAE3_IMP_RESET},
944 {ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
945 {ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
948 static const struct hns3_reset_type_map hns3vf_reset_type[] = {
949 {ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
952 static int hns3_set_reset(struct net_device *netdev, u32 *flags)
954 enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
955 struct hnae3_handle *h = hns3_get_handle(netdev);
956 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
957 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
958 const struct hns3_reset_type_map *rst_type_map;
961 if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
964 if (!ops->set_default_reset_request || !ops->reset_event)
967 if (h->flags & HNAE3_SUPPORT_VF) {
968 rst_type_map = hns3vf_reset_type;
969 size = ARRAY_SIZE(hns3vf_reset_type);
971 rst_type_map = hns3_reset_type;
972 size = ARRAY_SIZE(hns3_reset_type);
975 for (i = 0; i < size; i++) {
976 if (rst_type_map[i].rst_flags == *flags) {
977 rst_type = rst_type_map[i].rst_type;
982 if (rst_type == HNAE3_NONE_RESET ||
983 (rst_type == HNAE3_IMP_RESET &&
984 ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
987 netdev_info(netdev, "Setting reset type %d\n", rst_type);
989 ops->set_default_reset_request(ae_dev, rst_type);
991 ops->reset_event(h->pdev, h);
996 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
997 u32 tx_desc_num, u32 rx_desc_num)
999 struct hnae3_handle *h = priv->ae_handle;
1002 h->kinfo.num_tx_desc = tx_desc_num;
1003 h->kinfo.num_rx_desc = rx_desc_num;
1005 for (i = 0; i < h->kinfo.num_tqps; i++) {
1006 priv->ring[i].desc_num = tx_desc_num;
1007 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1011 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
1013 struct hnae3_handle *handle = priv->ae_handle;
1014 struct hns3_enet_ring *tmp_rings;
1017 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
1018 sizeof(struct hns3_enet_ring), GFP_KERNEL);
1022 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1023 memcpy(&tmp_rings[i], &priv->ring[i],
1024 sizeof(struct hns3_enet_ring));
1025 tmp_rings[i].skb = NULL;
1031 static int hns3_check_ringparam(struct net_device *ndev,
1032 struct ethtool_ringparam *param)
1034 if (hns3_nic_resetting(ndev))
1037 if (param->rx_mini_pending || param->rx_jumbo_pending)
1040 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1041 param->tx_pending < HNS3_RING_MIN_PENDING ||
1042 param->rx_pending > HNS3_RING_MAX_PENDING ||
1043 param->rx_pending < HNS3_RING_MIN_PENDING) {
1044 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1045 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1052 static int hns3_set_ringparam(struct net_device *ndev,
1053 struct ethtool_ringparam *param)
1055 struct hns3_nic_priv *priv = netdev_priv(ndev);
1056 struct hnae3_handle *h = priv->ae_handle;
1057 struct hns3_enet_ring *tmp_rings;
1058 bool if_running = netif_running(ndev);
1059 u32 old_tx_desc_num, new_tx_desc_num;
1060 u32 old_rx_desc_num, new_rx_desc_num;
1061 u16 queue_num = h->kinfo.num_tqps;
1064 ret = hns3_check_ringparam(ndev, param);
1068 /* Hardware requires that its descriptors must be multiple of eight */
1069 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1070 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1071 old_tx_desc_num = priv->ring[0].desc_num;
1072 old_rx_desc_num = priv->ring[queue_num].desc_num;
1073 if (old_tx_desc_num == new_tx_desc_num &&
1074 old_rx_desc_num == new_rx_desc_num)
1077 tmp_rings = hns3_backup_ringparam(priv);
1080 "backup ring param failed by allocating memory fail\n");
1085 "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
1086 old_tx_desc_num, old_rx_desc_num,
1087 new_tx_desc_num, new_rx_desc_num);
1090 ndev->netdev_ops->ndo_stop(ndev);
1092 hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1093 ret = hns3_init_all_ring(priv);
1095 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1098 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1100 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1101 memcpy(&priv->ring[i], &tmp_rings[i],
1102 sizeof(struct hns3_enet_ring));
1104 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1105 hns3_fini_ring(&tmp_rings[i]);
1111 ret = ndev->netdev_ops->ndo_open(ndev);
1116 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1118 struct hnae3_handle *h = hns3_get_handle(netdev);
1122 if (h->ae_algo->ops->set_rss_tuple)
1123 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1125 case ETHTOOL_SRXCLSRLINS:
1126 if (h->ae_algo->ops->add_fd_entry)
1127 return h->ae_algo->ops->add_fd_entry(h, cmd);
1129 case ETHTOOL_SRXCLSRLDEL:
1130 if (h->ae_algo->ops->del_fd_entry)
1131 return h->ae_algo->ops->del_fd_entry(h, cmd);
1138 static int hns3_nway_reset(struct net_device *netdev)
1140 struct hnae3_handle *handle = hns3_get_handle(netdev);
1141 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1142 struct phy_device *phy = netdev->phydev;
1145 if (!netif_running(netdev))
1148 if (hns3_nic_resetting(netdev)) {
1149 netdev_err(netdev, "dev resetting!");
1153 if (!ops->get_autoneg || !ops->restart_autoneg)
1156 autoneg = ops->get_autoneg(handle);
1157 if (autoneg != AUTONEG_ENABLE) {
1159 "Autoneg is off, don't support to restart it\n");
1163 netif_dbg(handle, drv, netdev,
1164 "nway reset (using %s)\n", phy ? "phy" : "mac");
1167 return genphy_restart_aneg(phy);
1169 return ops->restart_autoneg(handle);
1172 static void hns3_get_channels(struct net_device *netdev,
1173 struct ethtool_channels *ch)
1175 struct hnae3_handle *h = hns3_get_handle(netdev);
1177 if (h->ae_algo->ops->get_channels)
1178 h->ae_algo->ops->get_channels(h, ch);
1181 static int hns3_get_coalesce(struct net_device *netdev,
1182 struct ethtool_coalesce *cmd,
1183 struct kernel_ethtool_coalesce *kernel_coal,
1184 struct netlink_ext_ack *extack)
1186 struct hns3_nic_priv *priv = netdev_priv(netdev);
1187 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1188 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1189 struct hnae3_handle *h = priv->ae_handle;
1191 if (hns3_nic_resetting(netdev))
1194 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1195 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1197 cmd->tx_coalesce_usecs = tx_coal->int_gl;
1198 cmd->rx_coalesce_usecs = rx_coal->int_gl;
1200 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1201 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1203 cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1204 cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1209 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1210 struct ethtool_coalesce *cmd)
1212 struct hnae3_handle *handle = hns3_get_handle(netdev);
1213 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1216 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1218 "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1219 ae_dev->dev_specs.max_int_gl);
1223 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1225 "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1226 ae_dev->dev_specs.max_int_gl);
1230 /* device version above V3(include V3), GL uses 1us unit,
1231 * so the round down is not needed.
1233 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1236 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1237 if (rx_gl != cmd->rx_coalesce_usecs) {
1239 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1240 cmd->rx_coalesce_usecs, rx_gl);
1243 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1244 if (tx_gl != cmd->tx_coalesce_usecs) {
1246 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1247 cmd->tx_coalesce_usecs, tx_gl);
1253 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1254 struct ethtool_coalesce *cmd)
1258 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1260 "tx_usecs_high must be same as rx_usecs_high.\n");
1264 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1266 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1271 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1272 if (rl != cmd->rx_coalesce_usecs_high) {
1274 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1275 cmd->rx_coalesce_usecs_high, rl);
1281 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1282 struct ethtool_coalesce *cmd)
1284 struct hnae3_handle *handle = hns3_get_handle(netdev);
1285 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1287 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1288 !ae_dev->dev_specs.int_ql_max) {
1289 netdev_err(netdev, "coalesced frames is not supported\n");
1293 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1294 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1296 "invalid coalesced_frames value, range is 0-%u\n",
1297 ae_dev->dev_specs.int_ql_max);
1304 static int hns3_check_coalesce_para(struct net_device *netdev,
1305 struct ethtool_coalesce *cmd)
1309 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1312 "Check gl coalesce param fail. ret = %d\n", ret);
1316 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1319 "Check rl coalesce param fail. ret = %d\n", ret);
1323 return hns3_check_ql_coalesce_param(netdev, cmd);
1326 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1327 struct ethtool_coalesce *cmd,
1330 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1331 struct hns3_nic_priv *priv = netdev_priv(netdev);
1332 struct hnae3_handle *h = priv->ae_handle;
1333 int queue_num = h->kinfo.num_tqps;
1335 tx_vector = priv->ring[queue].tqp_vector;
1336 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1338 tx_vector->tx_group.coal.adapt_enable =
1339 cmd->use_adaptive_tx_coalesce;
1340 rx_vector->rx_group.coal.adapt_enable =
1341 cmd->use_adaptive_rx_coalesce;
1343 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1344 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1346 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1347 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1349 hns3_set_vector_coalesce_tx_gl(tx_vector,
1350 tx_vector->tx_group.coal.int_gl);
1351 hns3_set_vector_coalesce_rx_gl(rx_vector,
1352 rx_vector->rx_group.coal.int_gl);
1354 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1355 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1357 if (tx_vector->tx_group.coal.ql_enable)
1358 hns3_set_vector_coalesce_tx_ql(tx_vector,
1359 tx_vector->tx_group.coal.int_ql);
1360 if (rx_vector->rx_group.coal.ql_enable)
1361 hns3_set_vector_coalesce_rx_ql(rx_vector,
1362 rx_vector->rx_group.coal.int_ql);
1365 static int hns3_set_coalesce(struct net_device *netdev,
1366 struct ethtool_coalesce *cmd,
1367 struct kernel_ethtool_coalesce *kernel_coal,
1368 struct netlink_ext_ack *extack)
1370 struct hnae3_handle *h = hns3_get_handle(netdev);
1371 struct hns3_nic_priv *priv = netdev_priv(netdev);
1372 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1373 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1374 u16 queue_num = h->kinfo.num_tqps;
1378 if (hns3_nic_resetting(netdev))
1381 ret = hns3_check_coalesce_para(netdev, cmd);
1385 h->kinfo.int_rl_setting =
1386 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1388 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1389 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1391 tx_coal->int_gl = cmd->tx_coalesce_usecs;
1392 rx_coal->int_gl = cmd->rx_coalesce_usecs;
1394 tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1395 rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1397 for (i = 0; i < queue_num; i++)
1398 hns3_set_coalesce_per_queue(netdev, cmd, i);
1403 static int hns3_get_regs_len(struct net_device *netdev)
1405 struct hnae3_handle *h = hns3_get_handle(netdev);
1407 if (!h->ae_algo->ops->get_regs_len)
1410 return h->ae_algo->ops->get_regs_len(h);
1413 static void hns3_get_regs(struct net_device *netdev,
1414 struct ethtool_regs *cmd, void *data)
1416 struct hnae3_handle *h = hns3_get_handle(netdev);
1418 if (!h->ae_algo->ops->get_regs)
1421 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1424 static int hns3_set_phys_id(struct net_device *netdev,
1425 enum ethtool_phys_id_state state)
1427 struct hnae3_handle *h = hns3_get_handle(netdev);
1429 if (!h->ae_algo->ops->set_led_id)
1432 return h->ae_algo->ops->set_led_id(h, state);
1435 static u32 hns3_get_msglevel(struct net_device *netdev)
1437 struct hnae3_handle *h = hns3_get_handle(netdev);
1439 return h->msg_enable;
1442 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1444 struct hnae3_handle *h = hns3_get_handle(netdev);
1446 h->msg_enable = msg_level;
1449 /* Translate local fec value into ethtool value. */
1450 static unsigned int loc_to_eth_fec(u8 loc_fec)
1454 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1455 eth_fec |= ETHTOOL_FEC_AUTO;
1456 if (loc_fec & BIT(HNAE3_FEC_RS))
1457 eth_fec |= ETHTOOL_FEC_RS;
1458 if (loc_fec & BIT(HNAE3_FEC_BASER))
1459 eth_fec |= ETHTOOL_FEC_BASER;
1461 /* if nothing is set, then FEC is off */
1463 eth_fec = ETHTOOL_FEC_OFF;
1468 /* Translate ethtool fec value into local value. */
1469 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1473 if (eth_fec & ETHTOOL_FEC_OFF)
1476 if (eth_fec & ETHTOOL_FEC_AUTO)
1477 loc_fec |= BIT(HNAE3_FEC_AUTO);
1478 if (eth_fec & ETHTOOL_FEC_RS)
1479 loc_fec |= BIT(HNAE3_FEC_RS);
1480 if (eth_fec & ETHTOOL_FEC_BASER)
1481 loc_fec |= BIT(HNAE3_FEC_BASER);
1486 static int hns3_get_fecparam(struct net_device *netdev,
1487 struct ethtool_fecparam *fec)
1489 struct hnae3_handle *handle = hns3_get_handle(netdev);
1490 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1491 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1495 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1501 ops->get_fec(handle, &fec_ability, &fec_mode);
1503 fec->fec = loc_to_eth_fec(fec_ability);
1504 fec->active_fec = loc_to_eth_fec(fec_mode);
1509 static int hns3_set_fecparam(struct net_device *netdev,
1510 struct ethtool_fecparam *fec)
1512 struct hnae3_handle *handle = hns3_get_handle(netdev);
1513 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1514 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1517 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1522 fec_mode = eth_to_loc_fec(fec->fec);
1524 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1526 return ops->set_fec(handle, fec_mode);
1529 static int hns3_get_module_info(struct net_device *netdev,
1530 struct ethtool_modinfo *modinfo)
1532 #define HNS3_SFF_8636_V1_3 0x03
1534 struct hnae3_handle *handle = hns3_get_handle(netdev);
1535 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1536 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1537 struct hns3_sfp_type sfp_type;
1540 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1541 !ops->get_module_eeprom)
1544 memset(&sfp_type, 0, sizeof(sfp_type));
1545 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1550 switch (sfp_type.type) {
1551 case SFF8024_ID_SFP:
1552 modinfo->type = ETH_MODULE_SFF_8472;
1553 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1555 case SFF8024_ID_QSFP_8438:
1556 modinfo->type = ETH_MODULE_SFF_8436;
1557 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1559 case SFF8024_ID_QSFP_8436_8636:
1560 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1561 modinfo->type = ETH_MODULE_SFF_8436;
1562 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1564 modinfo->type = ETH_MODULE_SFF_8636;
1565 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1568 case SFF8024_ID_QSFP28_8636:
1569 modinfo->type = ETH_MODULE_SFF_8636;
1570 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1573 netdev_err(netdev, "Optical module unknown: %#x\n",
1581 static int hns3_get_module_eeprom(struct net_device *netdev,
1582 struct ethtool_eeprom *ee, u8 *data)
1584 struct hnae3_handle *handle = hns3_get_handle(netdev);
1585 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1586 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1588 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1589 !ops->get_module_eeprom)
1595 memset(data, 0, ee->len);
1597 return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1600 static u32 hns3_get_priv_flags(struct net_device *netdev)
1602 struct hnae3_handle *handle = hns3_get_handle(netdev);
1604 return handle->priv_flags;
1607 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1611 for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1612 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1613 netdev_err(h->netdev, "%s is unsupported\n",
1614 hns3_priv_flags[i].name);
1621 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1623 struct hnae3_handle *handle = hns3_get_handle(netdev);
1624 u32 changed = pflags ^ handle->priv_flags;
1628 ret = hns3_check_priv_flags(handle, changed);
1632 for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1633 if (changed & BIT(i)) {
1634 bool enable = !(handle->priv_flags & BIT(i));
1637 handle->priv_flags |= BIT(i);
1639 handle->priv_flags &= ~BIT(i);
1640 hns3_priv_flags[i].handler(netdev, enable);
1647 static int hns3_get_tunable(struct net_device *netdev,
1648 const struct ethtool_tunable *tuna,
1651 struct hns3_nic_priv *priv = netdev_priv(netdev);
1655 case ETHTOOL_TX_COPYBREAK:
1656 /* all the tx rings have the same tx_copybreak */
1657 *(u32 *)data = priv->tx_copybreak;
1659 case ETHTOOL_RX_COPYBREAK:
1660 *(u32 *)data = priv->rx_copybreak;
1670 static int hns3_set_tunable(struct net_device *netdev,
1671 const struct ethtool_tunable *tuna,
1674 struct hns3_nic_priv *priv = netdev_priv(netdev);
1675 struct hnae3_handle *h = priv->ae_handle;
1679 case ETHTOOL_TX_COPYBREAK:
1680 priv->tx_copybreak = *(u32 *)data;
1682 for (i = 0; i < h->kinfo.num_tqps; i++)
1683 priv->ring[i].tx_copybreak = priv->tx_copybreak;
1686 case ETHTOOL_RX_COPYBREAK:
1687 priv->rx_copybreak = *(u32 *)data;
1689 for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
1690 priv->ring[i].rx_copybreak = priv->rx_copybreak;
1701 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \
1702 ETHTOOL_COALESCE_USE_ADAPTIVE | \
1703 ETHTOOL_COALESCE_RX_USECS_HIGH | \
1704 ETHTOOL_COALESCE_TX_USECS_HIGH | \
1705 ETHTOOL_COALESCE_MAX_FRAMES)
1707 static int hns3_get_ts_info(struct net_device *netdev,
1708 struct ethtool_ts_info *info)
1710 struct hnae3_handle *handle = hns3_get_handle(netdev);
1712 if (handle->ae_algo->ops->get_ts_info)
1713 return handle->ae_algo->ops->get_ts_info(handle, info);
1715 return ethtool_op_get_ts_info(netdev, info);
1718 static const struct hns3_ethtool_link_ext_state_mapping
1719 hns3_link_ext_state_map[] = {
1720 {1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1721 ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
1722 {2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
1723 ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},
1725 {256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1726 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
1727 {257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1728 ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
1729 {512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
1730 ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},
1732 {513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1733 ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
1734 {514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1735 ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
1736 {515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
1737 ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},
1739 {768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1740 ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
1741 {769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1742 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
1743 {770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
1744 ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},
1746 {1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
1747 {1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
1748 ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},
1750 {1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
1753 static int hns3_get_link_ext_state(struct net_device *netdev,
1754 struct ethtool_link_ext_state_info *info)
1756 const struct hns3_ethtool_link_ext_state_mapping *map;
1757 struct hnae3_handle *h = hns3_get_handle(netdev);
1761 if (netif_carrier_ok(netdev))
1764 if (!h->ae_algo->ops->get_link_diagnosis_info)
1767 ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
1771 for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
1772 map = &hns3_link_ext_state_map[i];
1773 if (map->status_code == status_code) {
1774 info->link_ext_state = map->link_ext_state;
1775 info->__link_ext_substate = map->link_ext_substate;
1783 static const struct ethtool_ops hns3vf_ethtool_ops = {
1784 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1785 .get_drvinfo = hns3_get_drvinfo,
1786 .get_ringparam = hns3_get_ringparam,
1787 .set_ringparam = hns3_set_ringparam,
1788 .get_strings = hns3_get_strings,
1789 .get_ethtool_stats = hns3_get_stats,
1790 .get_sset_count = hns3_get_sset_count,
1791 .get_rxnfc = hns3_get_rxnfc,
1792 .set_rxnfc = hns3_set_rxnfc,
1793 .get_rxfh_key_size = hns3_get_rss_key_size,
1794 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1795 .get_rxfh = hns3_get_rss,
1796 .set_rxfh = hns3_set_rss,
1797 .get_link_ksettings = hns3_get_link_ksettings,
1798 .get_channels = hns3_get_channels,
1799 .set_channels = hns3_set_channels,
1800 .get_coalesce = hns3_get_coalesce,
1801 .set_coalesce = hns3_set_coalesce,
1802 .get_regs_len = hns3_get_regs_len,
1803 .get_regs = hns3_get_regs,
1804 .get_link = hns3_get_link,
1805 .get_msglevel = hns3_get_msglevel,
1806 .set_msglevel = hns3_set_msglevel,
1807 .get_priv_flags = hns3_get_priv_flags,
1808 .set_priv_flags = hns3_set_priv_flags,
1809 .get_tunable = hns3_get_tunable,
1810 .set_tunable = hns3_set_tunable,
1811 .reset = hns3_set_reset,
1814 static const struct ethtool_ops hns3_ethtool_ops = {
1815 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1816 .self_test = hns3_self_test,
1817 .get_drvinfo = hns3_get_drvinfo,
1818 .get_link = hns3_get_link,
1819 .get_ringparam = hns3_get_ringparam,
1820 .set_ringparam = hns3_set_ringparam,
1821 .get_pauseparam = hns3_get_pauseparam,
1822 .set_pauseparam = hns3_set_pauseparam,
1823 .get_strings = hns3_get_strings,
1824 .get_ethtool_stats = hns3_get_stats,
1825 .get_sset_count = hns3_get_sset_count,
1826 .get_rxnfc = hns3_get_rxnfc,
1827 .set_rxnfc = hns3_set_rxnfc,
1828 .get_rxfh_key_size = hns3_get_rss_key_size,
1829 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1830 .get_rxfh = hns3_get_rss,
1831 .set_rxfh = hns3_set_rss,
1832 .get_link_ksettings = hns3_get_link_ksettings,
1833 .set_link_ksettings = hns3_set_link_ksettings,
1834 .nway_reset = hns3_nway_reset,
1835 .get_channels = hns3_get_channels,
1836 .set_channels = hns3_set_channels,
1837 .get_coalesce = hns3_get_coalesce,
1838 .set_coalesce = hns3_set_coalesce,
1839 .get_regs_len = hns3_get_regs_len,
1840 .get_regs = hns3_get_regs,
1841 .set_phys_id = hns3_set_phys_id,
1842 .get_msglevel = hns3_get_msglevel,
1843 .set_msglevel = hns3_set_msglevel,
1844 .get_fecparam = hns3_get_fecparam,
1845 .set_fecparam = hns3_set_fecparam,
1846 .get_module_info = hns3_get_module_info,
1847 .get_module_eeprom = hns3_get_module_eeprom,
1848 .get_priv_flags = hns3_get_priv_flags,
1849 .set_priv_flags = hns3_set_priv_flags,
1850 .get_ts_info = hns3_get_ts_info,
1851 .get_tunable = hns3_get_tunable,
1852 .set_tunable = hns3_set_tunable,
1853 .reset = hns3_set_reset,
1854 .get_link_ext_state = hns3_get_link_ext_state,
1857 void hns3_ethtool_set_ops(struct net_device *netdev)
1859 struct hnae3_handle *h = hns3_get_handle(netdev);
1861 if (h->flags & HNAE3_SUPPORT_VF)
1862 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1864 netdev->ethtool_ops = &hns3_ethtool_ops;