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),
51 #define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
53 static const struct hns3_stats hns3_rxq_stats[] = {
54 /* Rx per-queue statistics */
55 HNS3_TQP_STAT("dropped", sw_err_cnt),
56 HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
57 HNS3_TQP_STAT("packets", rx_pkts),
58 HNS3_TQP_STAT("bytes", rx_bytes),
59 HNS3_TQP_STAT("errors", rx_err_cnt),
60 HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
61 HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
62 HNS3_TQP_STAT("err_bd_num", err_bd_num),
63 HNS3_TQP_STAT("l2_err", l2_err),
64 HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
65 HNS3_TQP_STAT("csum_complete", csum_complete),
66 HNS3_TQP_STAT("multicast", rx_multicast),
67 HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
70 #define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)
72 #define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
74 #define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
76 #define HNS3_SELF_TEST_TYPE_NUM 4
77 #define HNS3_NIC_LB_TEST_PKT_NUM 1
78 #define HNS3_NIC_LB_TEST_RING_ID 0
79 #define HNS3_NIC_LB_TEST_PACKET_SIZE 128
80 #define HNS3_NIC_LB_SETUP_USEC 10000
82 /* Nic loopback test err */
83 #define HNS3_NIC_LB_TEST_NO_MEM_ERR 1
84 #define HNS3_NIC_LB_TEST_TX_CNT_ERR 2
85 #define HNS3_NIC_LB_TEST_RX_CNT_ERR 3
87 static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
89 struct hnae3_handle *h = hns3_get_handle(ndev);
90 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
91 bool vlan_filter_enable;
94 if (!h->ae_algo->ops->set_loopback ||
95 !h->ae_algo->ops->set_promisc_mode)
99 case HNAE3_LOOP_SERIAL_SERDES:
100 case HNAE3_LOOP_PARALLEL_SERDES:
103 ret = h->ae_algo->ops->set_loopback(h, loop, en);
110 if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
114 h->ae_algo->ops->set_promisc_mode(h, true, true);
116 /* recover promisc mode before loopback test */
117 hns3_request_update_promisc_mode(h);
118 vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
119 hns3_enable_vlan_filter(ndev, vlan_filter_enable);
125 static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
127 struct hnae3_handle *h = hns3_get_handle(ndev);
130 ret = hns3_nic_reset_all_ring(h);
134 ret = hns3_lp_setup(ndev, loop_mode, true);
135 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
140 static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
144 ret = hns3_lp_setup(ndev, loop_mode, false);
146 netdev_err(ndev, "lb_setup return error: %d\n", ret);
150 usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
155 static void hns3_lp_setup_skb(struct sk_buff *skb)
157 #define HNS3_NIC_LB_DST_MAC_ADDR 0x1f
159 struct net_device *ndev = skb->dev;
160 struct hnae3_handle *handle;
161 struct hnae3_ae_dev *ae_dev;
162 unsigned char *packet;
166 skb_reserve(skb, NET_IP_ALIGN);
167 ethh = skb_put(skb, sizeof(struct ethhdr));
168 packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
170 memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
172 /* The dst mac addr of loopback packet is the same as the host'
173 * mac addr, the SSU component may loop back the packet to host
174 * before the packet reaches mac or serdes, which will defect
175 * the purpose of mac or serdes selftest.
177 handle = hns3_get_handle(ndev);
178 ae_dev = pci_get_drvdata(handle->pdev);
179 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
180 ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
181 eth_zero_addr(ethh->h_source);
182 ethh->h_proto = htons(ETH_P_ARP);
183 skb_reset_mac_header(skb);
185 for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
186 packet[i] = (unsigned char)(i & 0xff);
189 static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
192 struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
193 unsigned char *packet = skb->data;
194 u32 len = skb_headlen(skb);
197 len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);
199 for (i = 0; i < len; i++)
200 if (packet[i] != (unsigned char)(i & 0xff))
203 /* The packet is correctly received */
204 if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
205 tqp_vector->rx_group.total_packets++;
207 print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
208 skb->data, len, true);
210 dev_kfree_skb_any(skb);
213 static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
215 struct hnae3_handle *h = priv->ae_handle;
216 struct hnae3_knic_private_info *kinfo;
217 u32 i, rcv_good_pkt_total = 0;
220 for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
221 struct hns3_enet_ring *ring = &priv->ring[i];
222 struct hns3_enet_ring_group *rx_group;
225 rx_group = &ring->tqp_vector->rx_group;
226 pre_rx_pkt = rx_group->total_packets;
229 hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
232 rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
233 rx_group->total_packets = pre_rx_pkt;
235 return rcv_good_pkt_total;
238 static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
239 u32 end_ringid, u32 budget)
243 for (i = start_ringid; i <= end_ringid; i++) {
244 struct hns3_enet_ring *ring = &priv->ring[i];
246 hns3_clean_tx_ring(ring, 0);
251 * hns3_lp_run_test - run loopback test
253 * @mode: loopback type
255 static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
257 struct hns3_nic_priv *priv = netdev_priv(ndev);
262 skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
265 return HNS3_NIC_LB_TEST_NO_MEM_ERR;
268 hns3_lp_setup_skb(skb);
269 skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
272 for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
276 tx_ret = hns3_nic_net_xmit(skb, ndev);
277 if (tx_ret == NETDEV_TX_OK) {
281 netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
285 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
286 ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
287 netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
288 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
292 /* Allow 200 milliseconds for packets to go from Tx to Rx */
295 good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
296 if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
297 ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
298 netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
299 mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
303 hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
304 HNS3_NIC_LB_TEST_RING_ID,
305 HNS3_NIC_LB_TEST_PKT_NUM);
312 * hns3_self_test - self test
314 * @eth_test: test cmd
317 static void hns3_self_test(struct net_device *ndev,
318 struct ethtool_test *eth_test, u64 *data)
320 struct hns3_nic_priv *priv = netdev_priv(ndev);
321 struct hnae3_handle *h = priv->ae_handle;
322 int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
323 bool if_running = netif_running(ndev);
327 if (hns3_nic_resetting(ndev)) {
328 netdev_err(ndev, "dev resetting!");
332 /* Only do offline selftest, or pass by default */
333 if (eth_test->flags != ETH_TEST_FL_OFFLINE)
336 netif_dbg(h, drv, ndev, "self test start");
338 st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
339 st_param[HNAE3_LOOP_APP][1] =
340 h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
342 st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
343 st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
344 h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
346 st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
347 HNAE3_LOOP_PARALLEL_SERDES;
348 st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
349 h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
351 st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
352 st_param[HNAE3_LOOP_PHY][1] =
353 h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
356 ndev->netdev_ops->ndo_stop(ndev);
358 #if IS_ENABLED(CONFIG_VLAN_8021Q)
359 /* Disable the vlan filter for selftest does not support it */
360 if (h->ae_algo->ops->enable_vlan_filter)
361 h->ae_algo->ops->enable_vlan_filter(h, false);
364 /* Tell firmware to stop mac autoneg before loopback test start,
365 * otherwise loopback test may be failed when the port is still
368 if (h->ae_algo->ops->halt_autoneg)
369 h->ae_algo->ops->halt_autoneg(h, true);
371 set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
373 for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
374 enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
379 data[test_index] = hns3_lp_up(ndev, loop_type);
380 if (!data[test_index])
381 data[test_index] = hns3_lp_run_test(ndev, loop_type);
383 hns3_lp_down(ndev, loop_type);
385 if (data[test_index])
386 eth_test->flags |= ETH_TEST_FL_FAILED;
391 clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
393 if (h->ae_algo->ops->halt_autoneg)
394 h->ae_algo->ops->halt_autoneg(h, false);
396 #if IS_ENABLED(CONFIG_VLAN_8021Q)
397 if (h->ae_algo->ops->enable_vlan_filter)
398 h->ae_algo->ops->enable_vlan_filter(h, true);
402 ndev->netdev_ops->ndo_open(ndev);
404 netif_dbg(h, drv, ndev, "self test end\n");
407 static void hns3_update_limit_promisc_mode(struct net_device *netdev,
410 struct hnae3_handle *handle = hns3_get_handle(netdev);
413 set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
415 clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
417 hns3_request_update_promisc_mode(handle);
420 static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
421 { "limit_promisc", hns3_update_limit_promisc_mode }
424 static int hns3_get_sset_count(struct net_device *netdev, int stringset)
426 struct hnae3_handle *h = hns3_get_handle(netdev);
427 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
429 if (!ops->get_sset_count)
434 return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
435 ops->get_sset_count(h, stringset));
438 return ops->get_sset_count(h, stringset);
440 case ETH_SS_PRIV_FLAGS:
441 return HNAE3_PFLAG_MAX;
448 static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
449 u32 stat_count, u32 num_tqps, const char *prefix)
451 #define MAX_PREFIX_SIZE (6 + 4)
456 for (i = 0; i < num_tqps; i++) {
457 for (j = 0; j < stat_count; j++) {
458 data[ETH_GSTRING_LEN - 1] = '\0';
460 /* first, prepend the prefix string */
461 n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
463 size_left = (ETH_GSTRING_LEN - 1) - n1;
465 /* now, concatenate the stats string to it */
466 strncat(data, stats[j].stats_string, size_left);
467 data += ETH_GSTRING_LEN;
474 static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
476 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
477 const char tx_prefix[] = "txq";
478 const char rx_prefix[] = "rxq";
480 /* get strings for Tx */
481 data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
482 kinfo->num_tqps, tx_prefix);
484 /* get strings for Rx */
485 data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
486 kinfo->num_tqps, rx_prefix);
491 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
493 struct hnae3_handle *h = hns3_get_handle(netdev);
494 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
495 char *buff = (char *)data;
498 if (!ops->get_strings)
503 buff = hns3_get_strings_tqps(h, buff);
504 ops->get_strings(h, stringset, (u8 *)buff);
507 ops->get_strings(h, stringset, data);
509 case ETH_SS_PRIV_FLAGS:
510 for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
511 snprintf(buff, ETH_GSTRING_LEN, "%s",
512 hns3_priv_flags[i].name);
513 buff += ETH_GSTRING_LEN;
521 static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
523 struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
524 struct hnae3_knic_private_info *kinfo = &handle->kinfo;
525 struct hns3_enet_ring *ring;
529 /* get stats for Tx */
530 for (i = 0; i < kinfo->num_tqps; i++) {
531 ring = &nic_priv->ring[i];
532 for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
533 stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
534 *data++ = *(u64 *)stat;
538 /* get stats for Rx */
539 for (i = 0; i < kinfo->num_tqps; i++) {
540 ring = &nic_priv->ring[i + kinfo->num_tqps];
541 for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
542 stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
543 *data++ = *(u64 *)stat;
550 /* hns3_get_stats - get detail statistics.
551 * @netdev: net device
552 * @stats: statistics info.
553 * @data: statistics data.
555 static void hns3_get_stats(struct net_device *netdev,
556 struct ethtool_stats *stats, u64 *data)
558 struct hnae3_handle *h = hns3_get_handle(netdev);
561 if (hns3_nic_resetting(netdev)) {
562 netdev_err(netdev, "dev resetting, could not get stats\n");
566 if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
567 netdev_err(netdev, "could not get any statistics\n");
571 h->ae_algo->ops->update_stats(h, &netdev->stats);
573 /* get per-queue stats */
574 p = hns3_get_stats_tqps(h, p);
576 /* get MAC & other misc hardware stats */
577 h->ae_algo->ops->get_stats(h, p);
580 static void hns3_get_drvinfo(struct net_device *netdev,
581 struct ethtool_drvinfo *drvinfo)
583 struct hns3_nic_priv *priv = netdev_priv(netdev);
584 struct hnae3_handle *h = priv->ae_handle;
587 if (!h->ae_algo->ops->get_fw_version) {
588 netdev_err(netdev, "could not get fw version!\n");
592 strncpy(drvinfo->driver, h->pdev->driver->name,
593 sizeof(drvinfo->driver));
594 drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
596 strncpy(drvinfo->bus_info, pci_name(h->pdev),
597 sizeof(drvinfo->bus_info));
598 drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
600 fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
602 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
604 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
605 HNAE3_FW_VERSION_BYTE3_SHIFT),
606 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
607 HNAE3_FW_VERSION_BYTE2_SHIFT),
608 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
609 HNAE3_FW_VERSION_BYTE1_SHIFT),
610 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
611 HNAE3_FW_VERSION_BYTE0_SHIFT));
614 static u32 hns3_get_link(struct net_device *netdev)
616 struct hnae3_handle *h = hns3_get_handle(netdev);
618 if (h->ae_algo->ops->get_status)
619 return h->ae_algo->ops->get_status(h);
624 static void hns3_get_ringparam(struct net_device *netdev,
625 struct ethtool_ringparam *param)
627 struct hns3_nic_priv *priv = netdev_priv(netdev);
628 struct hnae3_handle *h = priv->ae_handle;
629 int queue_num = h->kinfo.num_tqps;
631 if (hns3_nic_resetting(netdev)) {
632 netdev_err(netdev, "dev resetting!");
636 param->tx_max_pending = HNS3_RING_MAX_PENDING;
637 param->rx_max_pending = HNS3_RING_MAX_PENDING;
639 param->tx_pending = priv->ring[0].desc_num;
640 param->rx_pending = priv->ring[queue_num].desc_num;
643 static void hns3_get_pauseparam(struct net_device *netdev,
644 struct ethtool_pauseparam *param)
646 struct hnae3_handle *h = hns3_get_handle(netdev);
647 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
649 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
652 if (h->ae_algo->ops->get_pauseparam)
653 h->ae_algo->ops->get_pauseparam(h, ¶m->autoneg,
654 ¶m->rx_pause, ¶m->tx_pause);
657 static int hns3_set_pauseparam(struct net_device *netdev,
658 struct ethtool_pauseparam *param)
660 struct hnae3_handle *h = hns3_get_handle(netdev);
661 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
663 if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
666 netif_dbg(h, drv, netdev,
667 "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
668 param->autoneg, param->rx_pause, param->tx_pause);
670 if (h->ae_algo->ops->set_pauseparam)
671 return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
677 static void hns3_get_ksettings(struct hnae3_handle *h,
678 struct ethtool_link_ksettings *cmd)
680 const struct hnae3_ae_ops *ops = h->ae_algo->ops;
682 /* 1.auto_neg & speed & duplex from cmd */
683 if (ops->get_ksettings_an_result)
684 ops->get_ksettings_an_result(h,
689 /* 2.get link mode */
690 if (ops->get_link_mode)
691 ops->get_link_mode(h,
692 cmd->link_modes.supported,
693 cmd->link_modes.advertising);
695 /* 3.mdix_ctrl&mdix get from phy reg */
696 if (ops->get_mdix_mode)
697 ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
698 &cmd->base.eth_tp_mdix);
701 static int hns3_get_link_ksettings(struct net_device *netdev,
702 struct ethtool_link_ksettings *cmd)
704 struct hnae3_handle *h = hns3_get_handle(netdev);
705 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
706 const struct hnae3_ae_ops *ops;
711 ops = h->ae_algo->ops;
712 if (ops->get_media_type)
713 ops->get_media_type(h, &media_type, &module_type);
717 switch (media_type) {
718 case HNAE3_MEDIA_TYPE_NONE:
719 cmd->base.port = PORT_NONE;
720 hns3_get_ksettings(h, cmd);
722 case HNAE3_MEDIA_TYPE_FIBER:
723 if (module_type == HNAE3_MODULE_TYPE_CR)
724 cmd->base.port = PORT_DA;
726 cmd->base.port = PORT_FIBRE;
728 hns3_get_ksettings(h, cmd);
730 case HNAE3_MEDIA_TYPE_BACKPLANE:
731 cmd->base.port = PORT_NONE;
732 hns3_get_ksettings(h, cmd);
734 case HNAE3_MEDIA_TYPE_COPPER:
735 cmd->base.port = PORT_TP;
736 if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
737 ops->get_phy_link_ksettings)
738 ops->get_phy_link_ksettings(h, cmd);
739 else if (!netdev->phydev)
740 hns3_get_ksettings(h, cmd);
742 phy_ethtool_ksettings_get(netdev->phydev, cmd);
746 netdev_warn(netdev, "Unknown media type");
751 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
753 link_stat = hns3_get_link(netdev);
755 cmd->base.speed = SPEED_UNKNOWN;
756 cmd->base.duplex = DUPLEX_UNKNOWN;
762 static int hns3_check_ksettings_param(const struct net_device *netdev,
763 const struct ethtool_link_ksettings *cmd)
765 struct hnae3_handle *handle = hns3_get_handle(netdev);
766 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
767 u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
768 u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
774 /* hw doesn't support use specified speed and duplex to negotiate,
775 * unnecessary to check them when autoneg on.
777 if (cmd->base.autoneg)
780 if (ops->get_ksettings_an_result) {
781 ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
782 if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
783 cmd->base.duplex == duplex)
787 if (ops->get_media_type)
788 ops->get_media_type(handle, &media_type, &module_type);
790 if (cmd->base.duplex == DUPLEX_HALF &&
791 media_type != HNAE3_MEDIA_TYPE_COPPER) {
793 "only copper port supports half duplex!");
797 if (ops->check_port_speed) {
798 ret = ops->check_port_speed(handle, cmd->base.speed);
800 netdev_err(netdev, "unsupported speed\n");
808 static int hns3_set_link_ksettings(struct net_device *netdev,
809 const struct ethtool_link_ksettings *cmd)
811 struct hnae3_handle *handle = hns3_get_handle(netdev);
812 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
813 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
816 /* Chip don't support this mode. */
817 if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
820 netif_dbg(handle, drv, netdev,
821 "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
822 netdev->phydev ? "phy" : "mac",
823 cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
825 /* Only support ksettings_set for netdev with phy attached for now */
826 if (netdev->phydev) {
827 if (cmd->base.speed == SPEED_1000 &&
828 cmd->base.autoneg == AUTONEG_DISABLE)
831 return phy_ethtool_ksettings_set(netdev->phydev, cmd);
832 } else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
833 ops->set_phy_link_ksettings) {
834 return ops->set_phy_link_ksettings(handle, cmd);
837 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
840 ret = hns3_check_ksettings_param(netdev, cmd);
844 if (ops->set_autoneg) {
845 ret = ops->set_autoneg(handle, cmd->base.autoneg);
850 /* hw doesn't support use specified speed and duplex to negotiate,
851 * ignore them when autoneg on.
853 if (cmd->base.autoneg) {
855 "autoneg is on, ignore the speed and duplex\n");
859 if (ops->cfg_mac_speed_dup_h)
860 ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
866 static u32 hns3_get_rss_key_size(struct net_device *netdev)
868 struct hnae3_handle *h = hns3_get_handle(netdev);
870 if (!h->ae_algo->ops->get_rss_key_size)
873 return h->ae_algo->ops->get_rss_key_size(h);
876 static u32 hns3_get_rss_indir_size(struct net_device *netdev)
878 struct hnae3_handle *h = hns3_get_handle(netdev);
879 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
881 return ae_dev->dev_specs.rss_ind_tbl_size;
884 static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
887 struct hnae3_handle *h = hns3_get_handle(netdev);
889 if (!h->ae_algo->ops->get_rss)
892 return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
895 static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
896 const u8 *key, const u8 hfunc)
898 struct hnae3_handle *h = hns3_get_handle(netdev);
899 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
901 if (!h->ae_algo->ops->set_rss)
904 if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
905 hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
906 hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
907 netdev_err(netdev, "hash func not supported\n");
913 "set rss failed for indir is empty\n");
917 return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
920 static int hns3_get_rxnfc(struct net_device *netdev,
921 struct ethtool_rxnfc *cmd,
924 struct hnae3_handle *h = hns3_get_handle(netdev);
927 case ETHTOOL_GRXRINGS:
928 cmd->data = h->kinfo.num_tqps;
931 if (h->ae_algo->ops->get_rss_tuple)
932 return h->ae_algo->ops->get_rss_tuple(h, cmd);
934 case ETHTOOL_GRXCLSRLCNT:
935 if (h->ae_algo->ops->get_fd_rule_cnt)
936 return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
938 case ETHTOOL_GRXCLSRULE:
939 if (h->ae_algo->ops->get_fd_rule_info)
940 return h->ae_algo->ops->get_fd_rule_info(h, cmd);
942 case ETHTOOL_GRXCLSRLALL:
943 if (h->ae_algo->ops->get_fd_all_rules)
944 return h->ae_algo->ops->get_fd_all_rules(h, cmd,
952 static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
953 u32 tx_desc_num, u32 rx_desc_num)
955 struct hnae3_handle *h = priv->ae_handle;
958 h->kinfo.num_tx_desc = tx_desc_num;
959 h->kinfo.num_rx_desc = rx_desc_num;
961 for (i = 0; i < h->kinfo.num_tqps; i++) {
962 priv->ring[i].desc_num = tx_desc_num;
963 priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
967 static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
969 struct hnae3_handle *handle = priv->ae_handle;
970 struct hns3_enet_ring *tmp_rings;
973 tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
974 sizeof(struct hns3_enet_ring), GFP_KERNEL);
978 for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
979 memcpy(&tmp_rings[i], &priv->ring[i],
980 sizeof(struct hns3_enet_ring));
981 tmp_rings[i].skb = NULL;
987 static int hns3_check_ringparam(struct net_device *ndev,
988 struct ethtool_ringparam *param)
990 if (hns3_nic_resetting(ndev))
993 if (param->rx_mini_pending || param->rx_jumbo_pending)
996 if (param->tx_pending > HNS3_RING_MAX_PENDING ||
997 param->tx_pending < HNS3_RING_MIN_PENDING ||
998 param->rx_pending > HNS3_RING_MAX_PENDING ||
999 param->rx_pending < HNS3_RING_MIN_PENDING) {
1000 netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
1001 HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
1008 static int hns3_set_ringparam(struct net_device *ndev,
1009 struct ethtool_ringparam *param)
1011 struct hns3_nic_priv *priv = netdev_priv(ndev);
1012 struct hnae3_handle *h = priv->ae_handle;
1013 struct hns3_enet_ring *tmp_rings;
1014 bool if_running = netif_running(ndev);
1015 u32 old_tx_desc_num, new_tx_desc_num;
1016 u32 old_rx_desc_num, new_rx_desc_num;
1017 u16 queue_num = h->kinfo.num_tqps;
1020 ret = hns3_check_ringparam(ndev, param);
1024 /* Hardware requires that its descriptors must be multiple of eight */
1025 new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
1026 new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
1027 old_tx_desc_num = priv->ring[0].desc_num;
1028 old_rx_desc_num = priv->ring[queue_num].desc_num;
1029 if (old_tx_desc_num == new_tx_desc_num &&
1030 old_rx_desc_num == new_rx_desc_num)
1033 tmp_rings = hns3_backup_ringparam(priv);
1036 "backup ring param failed by allocating memory fail\n");
1041 "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
1042 old_tx_desc_num, old_rx_desc_num,
1043 new_tx_desc_num, new_rx_desc_num);
1046 ndev->netdev_ops->ndo_stop(ndev);
1048 hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
1049 ret = hns3_init_all_ring(priv);
1051 netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
1054 hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
1056 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1057 memcpy(&priv->ring[i], &tmp_rings[i],
1058 sizeof(struct hns3_enet_ring));
1060 for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1061 hns3_fini_ring(&tmp_rings[i]);
1067 ret = ndev->netdev_ops->ndo_open(ndev);
1072 static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1074 struct hnae3_handle *h = hns3_get_handle(netdev);
1078 if (h->ae_algo->ops->set_rss_tuple)
1079 return h->ae_algo->ops->set_rss_tuple(h, cmd);
1081 case ETHTOOL_SRXCLSRLINS:
1082 if (h->ae_algo->ops->add_fd_entry)
1083 return h->ae_algo->ops->add_fd_entry(h, cmd);
1085 case ETHTOOL_SRXCLSRLDEL:
1086 if (h->ae_algo->ops->del_fd_entry)
1087 return h->ae_algo->ops->del_fd_entry(h, cmd);
1094 static int hns3_nway_reset(struct net_device *netdev)
1096 struct hnae3_handle *handle = hns3_get_handle(netdev);
1097 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1098 struct phy_device *phy = netdev->phydev;
1101 if (!netif_running(netdev))
1104 if (hns3_nic_resetting(netdev)) {
1105 netdev_err(netdev, "dev resetting!");
1109 if (!ops->get_autoneg || !ops->restart_autoneg)
1112 autoneg = ops->get_autoneg(handle);
1113 if (autoneg != AUTONEG_ENABLE) {
1115 "Autoneg is off, don't support to restart it\n");
1119 netif_dbg(handle, drv, netdev,
1120 "nway reset (using %s)\n", phy ? "phy" : "mac");
1123 return genphy_restart_aneg(phy);
1125 return ops->restart_autoneg(handle);
1128 static void hns3_get_channels(struct net_device *netdev,
1129 struct ethtool_channels *ch)
1131 struct hnae3_handle *h = hns3_get_handle(netdev);
1133 if (h->ae_algo->ops->get_channels)
1134 h->ae_algo->ops->get_channels(h, ch);
1137 static int hns3_get_coalesce(struct net_device *netdev,
1138 struct ethtool_coalesce *cmd)
1140 struct hns3_nic_priv *priv = netdev_priv(netdev);
1141 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1142 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1143 struct hnae3_handle *h = priv->ae_handle;
1145 if (hns3_nic_resetting(netdev))
1148 cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
1149 cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1151 cmd->tx_coalesce_usecs = tx_coal->int_gl;
1152 cmd->rx_coalesce_usecs = rx_coal->int_gl;
1154 cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1155 cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1157 cmd->tx_max_coalesced_frames = tx_coal->int_ql;
1158 cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1163 static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1164 struct ethtool_coalesce *cmd)
1166 struct hnae3_handle *handle = hns3_get_handle(netdev);
1167 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1170 if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1172 "invalid rx-usecs value, rx-usecs range is 0-%u\n",
1173 ae_dev->dev_specs.max_int_gl);
1177 if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1179 "invalid tx-usecs value, tx-usecs range is 0-%u\n",
1180 ae_dev->dev_specs.max_int_gl);
1184 /* device version above V3(include V3), GL uses 1us unit,
1185 * so the round down is not needed.
1187 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
1190 rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1191 if (rx_gl != cmd->rx_coalesce_usecs) {
1193 "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1194 cmd->rx_coalesce_usecs, rx_gl);
1197 tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1198 if (tx_gl != cmd->tx_coalesce_usecs) {
1200 "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1201 cmd->tx_coalesce_usecs, tx_gl);
1207 static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1208 struct ethtool_coalesce *cmd)
1212 if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1214 "tx_usecs_high must be same as rx_usecs_high.\n");
1218 if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1220 "Invalid usecs_high value, usecs_high range is 0-%d\n",
1225 rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1226 if (rl != cmd->rx_coalesce_usecs_high) {
1228 "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1229 cmd->rx_coalesce_usecs_high, rl);
1235 static int hns3_check_ql_coalesce_param(struct net_device *netdev,
1236 struct ethtool_coalesce *cmd)
1238 struct hnae3_handle *handle = hns3_get_handle(netdev);
1239 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1241 if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
1242 !ae_dev->dev_specs.int_ql_max) {
1243 netdev_err(netdev, "coalesced frames is not supported\n");
1247 if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
1248 cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
1250 "invalid coalesced_frames value, range is 0-%u\n",
1251 ae_dev->dev_specs.int_ql_max);
1258 static int hns3_check_coalesce_para(struct net_device *netdev,
1259 struct ethtool_coalesce *cmd)
1263 ret = hns3_check_gl_coalesce_para(netdev, cmd);
1266 "Check gl coalesce param fail. ret = %d\n", ret);
1270 ret = hns3_check_rl_coalesce_para(netdev, cmd);
1273 "Check rl coalesce param fail. ret = %d\n", ret);
1277 return hns3_check_ql_coalesce_param(netdev, cmd);
1280 static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1281 struct ethtool_coalesce *cmd,
1284 struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1285 struct hns3_nic_priv *priv = netdev_priv(netdev);
1286 struct hnae3_handle *h = priv->ae_handle;
1287 int queue_num = h->kinfo.num_tqps;
1289 tx_vector = priv->ring[queue].tqp_vector;
1290 rx_vector = priv->ring[queue_num + queue].tqp_vector;
1292 tx_vector->tx_group.coal.adapt_enable =
1293 cmd->use_adaptive_tx_coalesce;
1294 rx_vector->rx_group.coal.adapt_enable =
1295 cmd->use_adaptive_rx_coalesce;
1297 tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1298 rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1300 tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
1301 rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;
1303 hns3_set_vector_coalesce_tx_gl(tx_vector,
1304 tx_vector->tx_group.coal.int_gl);
1305 hns3_set_vector_coalesce_rx_gl(rx_vector,
1306 rx_vector->rx_group.coal.int_gl);
1308 hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1309 hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1311 if (tx_vector->tx_group.coal.ql_enable)
1312 hns3_set_vector_coalesce_tx_ql(tx_vector,
1313 tx_vector->tx_group.coal.int_ql);
1314 if (rx_vector->rx_group.coal.ql_enable)
1315 hns3_set_vector_coalesce_rx_ql(rx_vector,
1316 rx_vector->rx_group.coal.int_ql);
1319 static int hns3_set_coalesce(struct net_device *netdev,
1320 struct ethtool_coalesce *cmd)
1322 struct hnae3_handle *h = hns3_get_handle(netdev);
1323 struct hns3_nic_priv *priv = netdev_priv(netdev);
1324 struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
1325 struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1326 u16 queue_num = h->kinfo.num_tqps;
1330 if (hns3_nic_resetting(netdev))
1333 ret = hns3_check_coalesce_para(netdev, cmd);
1337 h->kinfo.int_rl_setting =
1338 hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1340 tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
1341 rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;
1343 tx_coal->int_gl = cmd->tx_coalesce_usecs;
1344 rx_coal->int_gl = cmd->rx_coalesce_usecs;
1346 tx_coal->int_ql = cmd->tx_max_coalesced_frames;
1347 rx_coal->int_ql = cmd->rx_max_coalesced_frames;
1349 for (i = 0; i < queue_num; i++)
1350 hns3_set_coalesce_per_queue(netdev, cmd, i);
1355 static int hns3_get_regs_len(struct net_device *netdev)
1357 struct hnae3_handle *h = hns3_get_handle(netdev);
1359 if (!h->ae_algo->ops->get_regs_len)
1362 return h->ae_algo->ops->get_regs_len(h);
1365 static void hns3_get_regs(struct net_device *netdev,
1366 struct ethtool_regs *cmd, void *data)
1368 struct hnae3_handle *h = hns3_get_handle(netdev);
1370 if (!h->ae_algo->ops->get_regs)
1373 h->ae_algo->ops->get_regs(h, &cmd->version, data);
1376 static int hns3_set_phys_id(struct net_device *netdev,
1377 enum ethtool_phys_id_state state)
1379 struct hnae3_handle *h = hns3_get_handle(netdev);
1381 if (!h->ae_algo->ops->set_led_id)
1384 return h->ae_algo->ops->set_led_id(h, state);
1387 static u32 hns3_get_msglevel(struct net_device *netdev)
1389 struct hnae3_handle *h = hns3_get_handle(netdev);
1391 return h->msg_enable;
1394 static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1396 struct hnae3_handle *h = hns3_get_handle(netdev);
1398 h->msg_enable = msg_level;
1401 /* Translate local fec value into ethtool value. */
1402 static unsigned int loc_to_eth_fec(u8 loc_fec)
1406 if (loc_fec & BIT(HNAE3_FEC_AUTO))
1407 eth_fec |= ETHTOOL_FEC_AUTO;
1408 if (loc_fec & BIT(HNAE3_FEC_RS))
1409 eth_fec |= ETHTOOL_FEC_RS;
1410 if (loc_fec & BIT(HNAE3_FEC_BASER))
1411 eth_fec |= ETHTOOL_FEC_BASER;
1413 /* if nothing is set, then FEC is off */
1415 eth_fec = ETHTOOL_FEC_OFF;
1420 /* Translate ethtool fec value into local value. */
1421 static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1425 if (eth_fec & ETHTOOL_FEC_OFF)
1428 if (eth_fec & ETHTOOL_FEC_AUTO)
1429 loc_fec |= BIT(HNAE3_FEC_AUTO);
1430 if (eth_fec & ETHTOOL_FEC_RS)
1431 loc_fec |= BIT(HNAE3_FEC_RS);
1432 if (eth_fec & ETHTOOL_FEC_BASER)
1433 loc_fec |= BIT(HNAE3_FEC_BASER);
1438 static int hns3_get_fecparam(struct net_device *netdev,
1439 struct ethtool_fecparam *fec)
1441 struct hnae3_handle *handle = hns3_get_handle(netdev);
1442 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1443 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1447 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1453 ops->get_fec(handle, &fec_ability, &fec_mode);
1455 fec->fec = loc_to_eth_fec(fec_ability);
1456 fec->active_fec = loc_to_eth_fec(fec_mode);
1461 static int hns3_set_fecparam(struct net_device *netdev,
1462 struct ethtool_fecparam *fec)
1464 struct hnae3_handle *handle = hns3_get_handle(netdev);
1465 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1466 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1469 if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1474 fec_mode = eth_to_loc_fec(fec->fec);
1476 netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1478 return ops->set_fec(handle, fec_mode);
1481 static int hns3_get_module_info(struct net_device *netdev,
1482 struct ethtool_modinfo *modinfo)
1484 #define HNS3_SFF_8636_V1_3 0x03
1486 struct hnae3_handle *handle = hns3_get_handle(netdev);
1487 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1488 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1489 struct hns3_sfp_type sfp_type;
1492 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1493 !ops->get_module_eeprom)
1496 memset(&sfp_type, 0, sizeof(sfp_type));
1497 ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
1502 switch (sfp_type.type) {
1503 case SFF8024_ID_SFP:
1504 modinfo->type = ETH_MODULE_SFF_8472;
1505 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1507 case SFF8024_ID_QSFP_8438:
1508 modinfo->type = ETH_MODULE_SFF_8436;
1509 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1511 case SFF8024_ID_QSFP_8436_8636:
1512 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
1513 modinfo->type = ETH_MODULE_SFF_8436;
1514 modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1516 modinfo->type = ETH_MODULE_SFF_8636;
1517 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1520 case SFF8024_ID_QSFP28_8636:
1521 modinfo->type = ETH_MODULE_SFF_8636;
1522 modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1525 netdev_err(netdev, "Optical module unknown: %#x\n",
1533 static int hns3_get_module_eeprom(struct net_device *netdev,
1534 struct ethtool_eeprom *ee, u8 *data)
1536 struct hnae3_handle *handle = hns3_get_handle(netdev);
1537 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1538 const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1540 if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
1541 !ops->get_module_eeprom)
1547 memset(data, 0, ee->len);
1549 return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
1552 static u32 hns3_get_priv_flags(struct net_device *netdev)
1554 struct hnae3_handle *handle = hns3_get_handle(netdev);
1556 return handle->priv_flags;
1559 static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
1563 for (i = 0; i < HNAE3_PFLAG_MAX; i++)
1564 if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
1565 netdev_err(h->netdev, "%s is unsupported\n",
1566 hns3_priv_flags[i].name);
1573 static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
1575 struct hnae3_handle *handle = hns3_get_handle(netdev);
1576 u32 changed = pflags ^ handle->priv_flags;
1580 ret = hns3_check_priv_flags(handle, changed);
1584 for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
1585 if (changed & BIT(i)) {
1586 bool enable = !(handle->priv_flags & BIT(i));
1589 handle->priv_flags |= BIT(i);
1591 handle->priv_flags &= ~BIT(i);
1592 hns3_priv_flags[i].handler(netdev, enable);
1599 #define HNS3_ETHTOOL_COALESCE (ETHTOOL_COALESCE_USECS | \
1600 ETHTOOL_COALESCE_USE_ADAPTIVE | \
1601 ETHTOOL_COALESCE_RX_USECS_HIGH | \
1602 ETHTOOL_COALESCE_TX_USECS_HIGH | \
1603 ETHTOOL_COALESCE_MAX_FRAMES)
1605 static const struct ethtool_ops hns3vf_ethtool_ops = {
1606 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1607 .get_drvinfo = hns3_get_drvinfo,
1608 .get_ringparam = hns3_get_ringparam,
1609 .set_ringparam = hns3_set_ringparam,
1610 .get_strings = hns3_get_strings,
1611 .get_ethtool_stats = hns3_get_stats,
1612 .get_sset_count = hns3_get_sset_count,
1613 .get_rxnfc = hns3_get_rxnfc,
1614 .set_rxnfc = hns3_set_rxnfc,
1615 .get_rxfh_key_size = hns3_get_rss_key_size,
1616 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1617 .get_rxfh = hns3_get_rss,
1618 .set_rxfh = hns3_set_rss,
1619 .get_link_ksettings = hns3_get_link_ksettings,
1620 .get_channels = hns3_get_channels,
1621 .set_channels = hns3_set_channels,
1622 .get_coalesce = hns3_get_coalesce,
1623 .set_coalesce = hns3_set_coalesce,
1624 .get_regs_len = hns3_get_regs_len,
1625 .get_regs = hns3_get_regs,
1626 .get_link = hns3_get_link,
1627 .get_msglevel = hns3_get_msglevel,
1628 .set_msglevel = hns3_set_msglevel,
1629 .get_priv_flags = hns3_get_priv_flags,
1630 .set_priv_flags = hns3_set_priv_flags,
1633 static const struct ethtool_ops hns3_ethtool_ops = {
1634 .supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1635 .self_test = hns3_self_test,
1636 .get_drvinfo = hns3_get_drvinfo,
1637 .get_link = hns3_get_link,
1638 .get_ringparam = hns3_get_ringparam,
1639 .set_ringparam = hns3_set_ringparam,
1640 .get_pauseparam = hns3_get_pauseparam,
1641 .set_pauseparam = hns3_set_pauseparam,
1642 .get_strings = hns3_get_strings,
1643 .get_ethtool_stats = hns3_get_stats,
1644 .get_sset_count = hns3_get_sset_count,
1645 .get_rxnfc = hns3_get_rxnfc,
1646 .set_rxnfc = hns3_set_rxnfc,
1647 .get_rxfh_key_size = hns3_get_rss_key_size,
1648 .get_rxfh_indir_size = hns3_get_rss_indir_size,
1649 .get_rxfh = hns3_get_rss,
1650 .set_rxfh = hns3_set_rss,
1651 .get_link_ksettings = hns3_get_link_ksettings,
1652 .set_link_ksettings = hns3_set_link_ksettings,
1653 .nway_reset = hns3_nway_reset,
1654 .get_channels = hns3_get_channels,
1655 .set_channels = hns3_set_channels,
1656 .get_coalesce = hns3_get_coalesce,
1657 .set_coalesce = hns3_set_coalesce,
1658 .get_regs_len = hns3_get_regs_len,
1659 .get_regs = hns3_get_regs,
1660 .set_phys_id = hns3_set_phys_id,
1661 .get_msglevel = hns3_get_msglevel,
1662 .set_msglevel = hns3_set_msglevel,
1663 .get_fecparam = hns3_get_fecparam,
1664 .set_fecparam = hns3_set_fecparam,
1665 .get_module_info = hns3_get_module_info,
1666 .get_module_eeprom = hns3_get_module_eeprom,
1667 .get_priv_flags = hns3_get_priv_flags,
1668 .set_priv_flags = hns3_set_priv_flags,
1671 void hns3_ethtool_set_ops(struct net_device *netdev)
1673 struct hnae3_handle *h = hns3_get_handle(netdev);
1675 if (h->flags & HNAE3_SUPPORT_VF)
1676 netdev->ethtool_ops = &hns3vf_ethtool_ops;
1678 netdev->ethtool_ops = &hns3_ethtool_ops;