Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
   1// SPDX-License-Identifier: GPL-2.0+
   2// Copyright (c) 2016-2017 Hisilicon Limited.
   3
   4#include <linux/etherdevice.h>
   5#include <linux/string.h>
   6#include <linux/phy.h>
   7
   8#include "hns3_enet.h"
   9
  10struct hns3_stats {
  11	char stats_string[ETH_GSTRING_LEN];
  12	int stats_offset;
  13};
  14
  15/* tqp related stats */
  16#define HNS3_TQP_STAT(_string, _member)	{			\
  17	.stats_string = _string,				\
  18	.stats_offset = offsetof(struct hns3_enet_ring, stats) +\
  19			offsetof(struct ring_stats, _member),   \
  20}
  21
  22static const struct hns3_stats hns3_txq_stats[] = {
  23	/* Tx per-queue statistics */
  24	HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
  25	HNS3_TQP_STAT("dropped", sw_err_cnt),
  26	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
  27	HNS3_TQP_STAT("packets", tx_pkts),
  28	HNS3_TQP_STAT("bytes", tx_bytes),
  29	HNS3_TQP_STAT("errors", tx_err_cnt),
  30	HNS3_TQP_STAT("wake", restart_queue),
  31	HNS3_TQP_STAT("busy", tx_busy),
  32	HNS3_TQP_STAT("copy", tx_copy),
  33	HNS3_TQP_STAT("vlan_err", tx_vlan_err),
  34	HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
  35	HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
  36	HNS3_TQP_STAT("tso_err", tx_tso_err),
  37};
  38
  39#define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)
  40
  41static const struct hns3_stats hns3_rxq_stats[] = {
  42	/* Rx per-queue statistics */
  43	HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
  44	HNS3_TQP_STAT("dropped", sw_err_cnt),
  45	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
  46	HNS3_TQP_STAT("packets", rx_pkts),
  47	HNS3_TQP_STAT("bytes", rx_bytes),
  48	HNS3_TQP_STAT("errors", rx_err_cnt),
  49	HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
  50	HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
  51	HNS3_TQP_STAT("err_bd_num", err_bd_num),
  52	HNS3_TQP_STAT("l2_err", l2_err),
  53	HNS3_TQP_STAT("l3l4_csum_err", l3l4_csum_err),
  54	HNS3_TQP_STAT("multicast", rx_multicast),
  55	HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
  56};
  57
  58#define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)
  59
  60#define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)
  61
  62#define HNS3_SELF_TEST_TYPE_NUM         4
  63#define HNS3_NIC_LB_TEST_PKT_NUM	1
  64#define HNS3_NIC_LB_TEST_RING_ID	0
  65#define HNS3_NIC_LB_TEST_PACKET_SIZE	128
  66#define HNS3_NIC_LB_SETUP_USEC		10000
  67
  68/* Nic loopback test err  */
  69#define HNS3_NIC_LB_TEST_NO_MEM_ERR	1
  70#define HNS3_NIC_LB_TEST_TX_CNT_ERR	2
  71#define HNS3_NIC_LB_TEST_RX_CNT_ERR	3
  72
  73static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
  74{
  75	struct hnae3_handle *h = hns3_get_handle(ndev);
  76	bool vlan_filter_enable;
  77	int ret;
  78
  79	if (!h->ae_algo->ops->set_loopback ||
  80	    !h->ae_algo->ops->set_promisc_mode)
  81		return -EOPNOTSUPP;
  82
  83	switch (loop) {
  84	case HNAE3_LOOP_SERIAL_SERDES:
  85	case HNAE3_LOOP_PARALLEL_SERDES:
  86	case HNAE3_LOOP_APP:
  87	case HNAE3_LOOP_PHY:
  88		ret = h->ae_algo->ops->set_loopback(h, loop, en);
  89		break;
  90	default:
  91		ret = -ENOTSUPP;
  92		break;
  93	}
  94
  95	if (ret || h->pdev->revision >= 0x21)
  96		return ret;
  97
  98	if (en) {
  99		h->ae_algo->ops->set_promisc_mode(h, true, true);
 100	} else {
 101		/* recover promisc mode before loopback test */
 102		hns3_update_promisc_mode(ndev, h->netdev_flags);
 103		vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
 104		hns3_enable_vlan_filter(ndev, vlan_filter_enable);
 105	}
 106
 107	return ret;
 108}
 109
 110static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
 111{
 112	struct hnae3_handle *h = hns3_get_handle(ndev);
 113	int ret;
 114
 115	ret = hns3_nic_reset_all_ring(h);
 116	if (ret)
 117		return ret;
 118
 119	ret = hns3_lp_setup(ndev, loop_mode, true);
 120	usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
 121
 122	return ret;
 123}
 124
 125static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
 126{
 127	int ret;
 128
 129	ret = hns3_lp_setup(ndev, loop_mode, false);
 130	if (ret) {
 131		netdev_err(ndev, "lb_setup return error: %d\n", ret);
 132		return ret;
 133	}
 134
 135	usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
 136
 137	return 0;
 138}
 139
 140static void hns3_lp_setup_skb(struct sk_buff *skb)
 141{
 142#define	HNS3_NIC_LB_DST_MAC_ADDR	0x1f
 143
 144	struct net_device *ndev = skb->dev;
 145	struct hnae3_handle *handle;
 146	unsigned char *packet;
 147	struct ethhdr *ethh;
 148	unsigned int i;
 149
 150	skb_reserve(skb, NET_IP_ALIGN);
 151	ethh = skb_put(skb, sizeof(struct ethhdr));
 152	packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);
 153
 154	memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
 155
 156	/* The dst mac addr of loopback packet is the same as the host'
 157	 * mac addr, the SSU component may loop back the packet to host
 158	 * before the packet reaches mac or serdes, which will defect
 159	 * the purpose of mac or serdes selftest.
 160	 */
 161	handle = hns3_get_handle(ndev);
 162	if (handle->pdev->revision == 0x20)
 163		ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
 164	eth_zero_addr(ethh->h_source);
 165	ethh->h_proto = htons(ETH_P_ARP);
 166	skb_reset_mac_header(skb);
 167
 168	for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
 169		packet[i] = (unsigned char)(i & 0xff);
 170}
 171
 172static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
 173				   struct sk_buff *skb)
 174{
 175	struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
 176	unsigned char *packet = skb->data;
 177	u32 i;
 178
 179	for (i = 0; i < skb->len; i++)
 180		if (packet[i] != (unsigned char)(i & 0xff))
 181			break;
 182
 183	/* The packet is correctly received */
 184	if (i == skb->len)
 185		tqp_vector->rx_group.total_packets++;
 186	else
 187		print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
 188			       skb->data, skb->len, true);
 189
 190	dev_kfree_skb_any(skb);
 191}
 192
 193static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
 194{
 195	struct hnae3_handle *h = priv->ae_handle;
 196	struct hnae3_knic_private_info *kinfo;
 197	u32 i, rcv_good_pkt_total = 0;
 198
 199	kinfo = &h->kinfo;
 200	for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
 201		struct hns3_enet_ring *ring = priv->ring_data[i].ring;
 202		struct hns3_enet_ring_group *rx_group;
 203		u64 pre_rx_pkt;
 204
 205		rx_group = &ring->tqp_vector->rx_group;
 206		pre_rx_pkt = rx_group->total_packets;
 207
 208		preempt_disable();
 209		hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
 210		preempt_enable();
 211
 212		rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
 213		rx_group->total_packets = pre_rx_pkt;
 214	}
 215	return rcv_good_pkt_total;
 216}
 217
 218static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
 219				  u32 end_ringid, u32 budget)
 220{
 221	u32 i;
 222
 223	for (i = start_ringid; i <= end_ringid; i++) {
 224		struct hns3_enet_ring *ring = priv->ring_data[i].ring;
 225
 226		hns3_clean_tx_ring(ring);
 227	}
 228}
 229
 230/**
 231 * hns3_lp_run_test -  run loopback test
 232 * @ndev: net device
 233 * @mode: loopback type
 234 */
 235static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
 236{
 237	struct hns3_nic_priv *priv = netdev_priv(ndev);
 238	struct sk_buff *skb;
 239	u32 i, good_cnt;
 240	int ret_val = 0;
 241
 242	skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
 243			GFP_KERNEL);
 244	if (!skb)
 245		return HNS3_NIC_LB_TEST_NO_MEM_ERR;
 246
 247	skb->dev = ndev;
 248	hns3_lp_setup_skb(skb);
 249	skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;
 250
 251	good_cnt = 0;
 252	for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
 253		netdev_tx_t tx_ret;
 254
 255		skb_get(skb);
 256		tx_ret = hns3_nic_net_xmit(skb, ndev);
 257		if (tx_ret == NETDEV_TX_OK) {
 258			good_cnt++;
 259		} else {
 260			kfree_skb(skb);
 261			netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
 262				   tx_ret);
 263		}
 264	}
 265	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
 266		ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
 267		netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
 268			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
 269		goto out;
 270	}
 271
 272	/* Allow 200 milliseconds for packets to go from Tx to Rx */
 273	msleep(200);
 274
 275	good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
 276	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
 277		ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
 278		netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
 279			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
 280	}
 281
 282out:
 283	hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
 284			      HNS3_NIC_LB_TEST_RING_ID,
 285			      HNS3_NIC_LB_TEST_PKT_NUM);
 286
 287	kfree_skb(skb);
 288	return ret_val;
 289}
 290
 291/**
 292 * hns3_nic_self_test - self test
 293 * @ndev: net device
 294 * @eth_test: test cmd
 295 * @data: test result
 296 */
 297static void hns3_self_test(struct net_device *ndev,
 298			   struct ethtool_test *eth_test, u64 *data)
 299{
 300	struct hns3_nic_priv *priv = netdev_priv(ndev);
 301	struct hnae3_handle *h = priv->ae_handle;
 302	int st_param[HNS3_SELF_TEST_TYPE_NUM][2];
 303	bool if_running = netif_running(ndev);
 304#if IS_ENABLED(CONFIG_VLAN_8021Q)
 305	bool dis_vlan_filter;
 306#endif
 307	int test_index = 0;
 308	u32 i;
 309
 310	if (hns3_nic_resetting(ndev)) {
 311		netdev_err(ndev, "dev resetting!");
 312		return;
 313	}
 314
 315	/* Only do offline selftest, or pass by default */
 316	if (eth_test->flags != ETH_TEST_FL_OFFLINE)
 317		return;
 318
 319	netif_dbg(h, drv, ndev, "self test start");
 320
 321	st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
 322	st_param[HNAE3_LOOP_APP][1] =
 323			h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
 324
 325	st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
 326	st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
 327			h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
 328
 329	st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
 330			HNAE3_LOOP_PARALLEL_SERDES;
 331	st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
 332			h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
 333
 334	st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
 335	st_param[HNAE3_LOOP_PHY][1] =
 336			h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
 337
 338	if (if_running)
 339		ndev->netdev_ops->ndo_stop(ndev);
 340
 341#if IS_ENABLED(CONFIG_VLAN_8021Q)
 342	/* Disable the vlan filter for selftest does not support it */
 343	dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
 344				h->ae_algo->ops->enable_vlan_filter;
 345	if (dis_vlan_filter)
 346		h->ae_algo->ops->enable_vlan_filter(h, false);
 347#endif
 348
 349	/* Tell firmware to stop mac autoneg before loopback test start,
 350	 * otherwise loopback test may be failed when the port is still
 351	 * negotiating.
 352	 */
 353	if (h->ae_algo->ops->halt_autoneg)
 354		h->ae_algo->ops->halt_autoneg(h, true);
 355
 356	set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
 357
 358	for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
 359		enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];
 360
 361		if (!st_param[i][1])
 362			continue;
 363
 364		data[test_index] = hns3_lp_up(ndev, loop_type);
 365		if (!data[test_index])
 366			data[test_index] = hns3_lp_run_test(ndev, loop_type);
 367
 368		hns3_lp_down(ndev, loop_type);
 369
 370		if (data[test_index])
 371			eth_test->flags |= ETH_TEST_FL_FAILED;
 372
 373		test_index++;
 374	}
 375
 376	clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);
 377
 378	if (h->ae_algo->ops->halt_autoneg)
 379		h->ae_algo->ops->halt_autoneg(h, false);
 380
 381#if IS_ENABLED(CONFIG_VLAN_8021Q)
 382	if (dis_vlan_filter)
 383		h->ae_algo->ops->enable_vlan_filter(h, true);
 384#endif
 385
 386	if (if_running)
 387		ndev->netdev_ops->ndo_open(ndev);
 388
 389	netif_dbg(h, drv, ndev, "self test end\n");
 390}
 391
 392static int hns3_get_sset_count(struct net_device *netdev, int stringset)
 393{
 394	struct hnae3_handle *h = hns3_get_handle(netdev);
 395	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 396
 397	if (!ops->get_sset_count)
 398		return -EOPNOTSUPP;
 399
 400	switch (stringset) {
 401	case ETH_SS_STATS:
 402		return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
 403			ops->get_sset_count(h, stringset));
 404
 405	case ETH_SS_TEST:
 406		return ops->get_sset_count(h, stringset);
 407
 408	default:
 409		return -EOPNOTSUPP;
 410	}
 411}
 412
 413static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
 414		u32 stat_count, u32 num_tqps, const char *prefix)
 415{
 416#define MAX_PREFIX_SIZE (6 + 4)
 417	u32 size_left;
 418	u32 i, j;
 419	u32 n1;
 420
 421	for (i = 0; i < num_tqps; i++) {
 422		for (j = 0; j < stat_count; j++) {
 423			data[ETH_GSTRING_LEN - 1] = '\0';
 424
 425			/* first, prepend the prefix string */
 426			n1 = snprintf(data, MAX_PREFIX_SIZE, "%s%d_",
 427				      prefix, i);
 428			n1 = min_t(uint, n1, MAX_PREFIX_SIZE - 1);
 429			size_left = (ETH_GSTRING_LEN - 1) - n1;
 430
 431			/* now, concatenate the stats string to it */
 432			strncat(data, stats[j].stats_string, size_left);
 433			data += ETH_GSTRING_LEN;
 434		}
 435	}
 436
 437	return data;
 438}
 439
 440static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
 441{
 442	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
 443	const char tx_prefix[] = "txq";
 444	const char rx_prefix[] = "rxq";
 445
 446	/* get strings for Tx */
 447	data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
 448				   kinfo->num_tqps, tx_prefix);
 449
 450	/* get strings for Rx */
 451	data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
 452				   kinfo->num_tqps, rx_prefix);
 453
 454	return data;
 455}
 456
 457static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
 458{
 459	struct hnae3_handle *h = hns3_get_handle(netdev);
 460	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 461	char *buff = (char *)data;
 462
 463	if (!ops->get_strings)
 464		return;
 465
 466	switch (stringset) {
 467	case ETH_SS_STATS:
 468		buff = hns3_get_strings_tqps(h, buff);
 469		ops->get_strings(h, stringset, (u8 *)buff);
 470		break;
 471	case ETH_SS_TEST:
 472		ops->get_strings(h, stringset, data);
 473		break;
 474	default:
 475		break;
 476	}
 477}
 478
 479static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
 480{
 481	struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
 482	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
 483	struct hns3_enet_ring *ring;
 484	u8 *stat;
 485	int i, j;
 486
 487	/* get stats for Tx */
 488	for (i = 0; i < kinfo->num_tqps; i++) {
 489		ring = nic_priv->ring_data[i].ring;
 490		for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
 491			stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
 492			*data++ = *(u64 *)stat;
 493		}
 494	}
 495
 496	/* get stats for Rx */
 497	for (i = 0; i < kinfo->num_tqps; i++) {
 498		ring = nic_priv->ring_data[i + kinfo->num_tqps].ring;
 499		for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
 500			stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
 501			*data++ = *(u64 *)stat;
 502		}
 503	}
 504
 505	return data;
 506}
 507
 508/* hns3_get_stats - get detail statistics.
 509 * @netdev: net device
 510 * @stats: statistics info.
 511 * @data: statistics data.
 512 */
 513static void hns3_get_stats(struct net_device *netdev,
 514			   struct ethtool_stats *stats, u64 *data)
 515{
 516	struct hnae3_handle *h = hns3_get_handle(netdev);
 517	u64 *p = data;
 518
 519	if (hns3_nic_resetting(netdev)) {
 520		netdev_err(netdev, "dev resetting, could not get stats\n");
 521		return;
 522	}
 523
 524	if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
 525		netdev_err(netdev, "could not get any statistics\n");
 526		return;
 527	}
 528
 529	h->ae_algo->ops->update_stats(h, &netdev->stats);
 530
 531	/* get per-queue stats */
 532	p = hns3_get_stats_tqps(h, p);
 533
 534	/* get MAC & other misc hardware stats */
 535	h->ae_algo->ops->get_stats(h, p);
 536}
 537
 538static void hns3_get_drvinfo(struct net_device *netdev,
 539			     struct ethtool_drvinfo *drvinfo)
 540{
 541	struct hns3_nic_priv *priv = netdev_priv(netdev);
 542	struct hnae3_handle *h = priv->ae_handle;
 543	u32 fw_version;
 544
 545	if (!h->ae_algo->ops->get_fw_version) {
 546		netdev_err(netdev, "could not get fw version!\n");
 547		return;
 548	}
 549
 550	strncpy(drvinfo->version, hns3_driver_version,
 551		sizeof(drvinfo->version));
 552	drvinfo->version[sizeof(drvinfo->version) - 1] = '\0';
 553
 554	strncpy(drvinfo->driver, h->pdev->driver->name,
 555		sizeof(drvinfo->driver));
 556	drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';
 557
 558	strncpy(drvinfo->bus_info, pci_name(h->pdev),
 559		sizeof(drvinfo->bus_info));
 560	drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';
 561
 562	fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);
 563
 564	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
 565		 "%lu.%lu.%lu.%lu",
 566		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
 567				 HNAE3_FW_VERSION_BYTE3_SHIFT),
 568		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
 569				 HNAE3_FW_VERSION_BYTE2_SHIFT),
 570		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
 571				 HNAE3_FW_VERSION_BYTE1_SHIFT),
 572		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
 573				 HNAE3_FW_VERSION_BYTE0_SHIFT));
 574}
 575
 576static u32 hns3_get_link(struct net_device *netdev)
 577{
 578	struct hnae3_handle *h = hns3_get_handle(netdev);
 579
 580	if (h->ae_algo->ops->get_status)
 581		return h->ae_algo->ops->get_status(h);
 582	else
 583		return 0;
 584}
 585
 586static void hns3_get_ringparam(struct net_device *netdev,
 587			       struct ethtool_ringparam *param)
 588{
 589	struct hns3_nic_priv *priv = netdev_priv(netdev);
 590	struct hnae3_handle *h = priv->ae_handle;
 591	int queue_num = h->kinfo.num_tqps;
 592
 593	if (hns3_nic_resetting(netdev)) {
 594		netdev_err(netdev, "dev resetting!");
 595		return;
 596	}
 597
 598	param->tx_max_pending = HNS3_RING_MAX_PENDING;
 599	param->rx_max_pending = HNS3_RING_MAX_PENDING;
 600
 601	param->tx_pending = priv->ring_data[0].ring->desc_num;
 602	param->rx_pending = priv->ring_data[queue_num].ring->desc_num;
 603}
 604
 605static void hns3_get_pauseparam(struct net_device *netdev,
 606				struct ethtool_pauseparam *param)
 607{
 608	struct hnae3_handle *h = hns3_get_handle(netdev);
 609
 610	if (h->ae_algo->ops->get_pauseparam)
 611		h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
 612			&param->rx_pause, &param->tx_pause);
 613}
 614
 615static int hns3_set_pauseparam(struct net_device *netdev,
 616			       struct ethtool_pauseparam *param)
 617{
 618	struct hnae3_handle *h = hns3_get_handle(netdev);
 619
 620	netif_dbg(h, drv, netdev,
 621		  "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
 622		  param->autoneg, param->rx_pause, param->tx_pause);
 623
 624	if (h->ae_algo->ops->set_pauseparam)
 625		return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
 626						       param->rx_pause,
 627						       param->tx_pause);
 628	return -EOPNOTSUPP;
 629}
 630
 631static void hns3_get_ksettings(struct hnae3_handle *h,
 632			       struct ethtool_link_ksettings *cmd)
 633{
 634	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
 635
 636	/* 1.auto_neg & speed & duplex from cmd */
 637	if (ops->get_ksettings_an_result)
 638		ops->get_ksettings_an_result(h,
 639					     &cmd->base.autoneg,
 640					     &cmd->base.speed,
 641					     &cmd->base.duplex);
 642
 643	/* 2.get link mode */
 644	if (ops->get_link_mode)
 645		ops->get_link_mode(h,
 646				   cmd->link_modes.supported,
 647				   cmd->link_modes.advertising);
 648
 649	/* 3.mdix_ctrl&mdix get from phy reg */
 650	if (ops->get_mdix_mode)
 651		ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
 652				   &cmd->base.eth_tp_mdix);
 653}
 654
 655static int hns3_get_link_ksettings(struct net_device *netdev,
 656				   struct ethtool_link_ksettings *cmd)
 657{
 658	struct hnae3_handle *h = hns3_get_handle(netdev);
 659	const struct hnae3_ae_ops *ops;
 660	u8 module_type;
 661	u8 media_type;
 662	u8 link_stat;
 663
 664	ops = h->ae_algo->ops;
 665	if (ops->get_media_type)
 666		ops->get_media_type(h, &media_type, &module_type);
 667	else
 668		return -EOPNOTSUPP;
 669
 670	switch (media_type) {
 671	case HNAE3_MEDIA_TYPE_NONE:
 672		cmd->base.port = PORT_NONE;
 673		hns3_get_ksettings(h, cmd);
 674		break;
 675	case HNAE3_MEDIA_TYPE_FIBER:
 676		if (module_type == HNAE3_MODULE_TYPE_CR)
 677			cmd->base.port = PORT_DA;
 678		else
 679			cmd->base.port = PORT_FIBRE;
 680
 681		hns3_get_ksettings(h, cmd);
 682		break;
 683	case HNAE3_MEDIA_TYPE_BACKPLANE:
 684		cmd->base.port = PORT_NONE;
 685		hns3_get_ksettings(h, cmd);
 686		break;
 687	case HNAE3_MEDIA_TYPE_COPPER:
 688		cmd->base.port = PORT_TP;
 689		if (!netdev->phydev)
 690			hns3_get_ksettings(h, cmd);
 691		else
 692			phy_ethtool_ksettings_get(netdev->phydev, cmd);
 693		break;
 694	default:
 695
 696		netdev_warn(netdev, "Unknown media type");
 697		return 0;
 698	}
 699
 700	/* mdio_support */
 701	cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
 702
 703	link_stat = hns3_get_link(netdev);
 704	if (!link_stat) {
 705		cmd->base.speed = SPEED_UNKNOWN;
 706		cmd->base.duplex = DUPLEX_UNKNOWN;
 707	}
 708
 709	return 0;
 710}
 711
 712static int hns3_check_ksettings_param(const struct net_device *netdev,
 713				      const struct ethtool_link_ksettings *cmd)
 714{
 715	struct hnae3_handle *handle = hns3_get_handle(netdev);
 716	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
 717	u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
 718	u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
 719	u8 autoneg;
 720	u32 speed;
 721	u8 duplex;
 722	int ret;
 723
 724	/* hw doesn't support use specified speed and duplex to negotiate,
 725	 * unnecessary to check them when autoneg on.
 726	 */
 727	if (cmd->base.autoneg)
 728		return 0;
 729
 730	if (ops->get_ksettings_an_result) {
 731		ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
 732		if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
 733		    cmd->base.duplex == duplex)
 734			return 0;
 735	}
 736
 737	if (ops->get_media_type)
 738		ops->get_media_type(handle, &media_type, &module_type);
 739
 740	if (cmd->base.duplex != DUPLEX_FULL &&
 741	    media_type != HNAE3_MEDIA_TYPE_COPPER) {
 742		netdev_err(netdev,
 743			   "only copper port supports half duplex!");
 744		return -EINVAL;
 745	}
 746
 747	if (ops->check_port_speed) {
 748		ret = ops->check_port_speed(handle, cmd->base.speed);
 749		if (ret) {
 750			netdev_err(netdev, "unsupported speed\n");
 751			return ret;
 752		}
 753	}
 754
 755	return 0;
 756}
 757
 758static int hns3_set_link_ksettings(struct net_device *netdev,
 759				   const struct ethtool_link_ksettings *cmd)
 760{
 761	struct hnae3_handle *handle = hns3_get_handle(netdev);
 762	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
 763	int ret;
 764
 765	/* Chip don't support this mode. */
 766	if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
 767		return -EINVAL;
 768
 769	netif_dbg(handle, drv, netdev,
 770		  "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
 771		  netdev->phydev ? "phy" : "mac",
 772		  cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);
 773
 774	/* Only support ksettings_set for netdev with phy attached for now */
 775	if (netdev->phydev)
 776		return phy_ethtool_ksettings_set(netdev->phydev, cmd);
 777
 778	if (handle->pdev->revision == 0x20)
 779		return -EOPNOTSUPP;
 780
 781	ret = hns3_check_ksettings_param(netdev, cmd);
 782	if (ret)
 783		return ret;
 784
 785	if (ops->set_autoneg) {
 786		ret = ops->set_autoneg(handle, cmd->base.autoneg);
 787		if (ret)
 788			return ret;
 789	}
 790
 791	/* hw doesn't support use specified speed and duplex to negotiate,
 792	 * ignore them when autoneg on.
 793	 */
 794	if (cmd->base.autoneg) {
 795		netdev_info(netdev,
 796			    "autoneg is on, ignore the speed and duplex\n");
 797		return 0;
 798	}
 799
 800	if (ops->cfg_mac_speed_dup_h)
 801		ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
 802					       cmd->base.duplex);
 803
 804	return ret;
 805}
 806
 807static u32 hns3_get_rss_key_size(struct net_device *netdev)
 808{
 809	struct hnae3_handle *h = hns3_get_handle(netdev);
 810
 811	if (!h->ae_algo->ops->get_rss_key_size)
 812		return 0;
 813
 814	return h->ae_algo->ops->get_rss_key_size(h);
 815}
 816
 817static u32 hns3_get_rss_indir_size(struct net_device *netdev)
 818{
 819	struct hnae3_handle *h = hns3_get_handle(netdev);
 820
 821	if (!h->ae_algo->ops->get_rss_indir_size)
 822		return 0;
 823
 824	return h->ae_algo->ops->get_rss_indir_size(h);
 825}
 826
 827static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
 828			u8 *hfunc)
 829{
 830	struct hnae3_handle *h = hns3_get_handle(netdev);
 831
 832	if (!h->ae_algo->ops->get_rss)
 833		return -EOPNOTSUPP;
 834
 835	return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
 836}
 837
 838static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
 839			const u8 *key, const u8 hfunc)
 840{
 841	struct hnae3_handle *h = hns3_get_handle(netdev);
 842
 843	if (!h->ae_algo->ops->set_rss)
 844		return -EOPNOTSUPP;
 845
 846	if ((h->pdev->revision == 0x20 &&
 847	     hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
 848	     hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
 849		netdev_err(netdev, "hash func not supported\n");
 850		return -EOPNOTSUPP;
 851	}
 852
 853	if (!indir) {
 854		netdev_err(netdev,
 855			   "set rss failed for indir is empty\n");
 856		return -EOPNOTSUPP;
 857	}
 858
 859	return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
 860}
 861
 862static int hns3_get_rxnfc(struct net_device *netdev,
 863			  struct ethtool_rxnfc *cmd,
 864			  u32 *rule_locs)
 865{
 866	struct hnae3_handle *h = hns3_get_handle(netdev);
 867
 868	switch (cmd->cmd) {
 869	case ETHTOOL_GRXRINGS:
 870		cmd->data = h->kinfo.num_tqps;
 871		return 0;
 872	case ETHTOOL_GRXFH:
 873		if (h->ae_algo->ops->get_rss_tuple)
 874			return h->ae_algo->ops->get_rss_tuple(h, cmd);
 875		return -EOPNOTSUPP;
 876	case ETHTOOL_GRXCLSRLCNT:
 877		if (h->ae_algo->ops->get_fd_rule_cnt)
 878			return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
 879		return -EOPNOTSUPP;
 880	case ETHTOOL_GRXCLSRULE:
 881		if (h->ae_algo->ops->get_fd_rule_info)
 882			return h->ae_algo->ops->get_fd_rule_info(h, cmd);
 883		return -EOPNOTSUPP;
 884	case ETHTOOL_GRXCLSRLALL:
 885		if (h->ae_algo->ops->get_fd_all_rules)
 886			return h->ae_algo->ops->get_fd_all_rules(h, cmd,
 887								 rule_locs);
 888		return -EOPNOTSUPP;
 889	default:
 890		return -EOPNOTSUPP;
 891	}
 892}
 893
 894static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
 895					u32 tx_desc_num, u32 rx_desc_num)
 896{
 897	struct hnae3_handle *h = priv->ae_handle;
 898	int i;
 899
 900	h->kinfo.num_tx_desc = tx_desc_num;
 901	h->kinfo.num_rx_desc = rx_desc_num;
 902
 903	for (i = 0; i < h->kinfo.num_tqps; i++) {
 904		priv->ring_data[i].ring->desc_num = tx_desc_num;
 905		priv->ring_data[i + h->kinfo.num_tqps].ring->desc_num =
 906			rx_desc_num;
 907	}
 908}
 909
 910static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
 911{
 912	struct hnae3_handle *handle = priv->ae_handle;
 913	struct hns3_enet_ring *tmp_rings;
 914	int i;
 915
 916	tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
 917			    sizeof(struct hns3_enet_ring), GFP_KERNEL);
 918	if (!tmp_rings)
 919		return NULL;
 920
 921	for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
 922		memcpy(&tmp_rings[i], priv->ring_data[i].ring,
 923		       sizeof(struct hns3_enet_ring));
 924		tmp_rings[i].skb = NULL;
 925	}
 926
 927	return tmp_rings;
 928}
 929
 930static int hns3_check_ringparam(struct net_device *ndev,
 931				struct ethtool_ringparam *param)
 932{
 933	if (hns3_nic_resetting(ndev))
 934		return -EBUSY;
 935
 936	if (param->rx_mini_pending || param->rx_jumbo_pending)
 937		return -EINVAL;
 938
 939	if (param->tx_pending > HNS3_RING_MAX_PENDING ||
 940	    param->tx_pending < HNS3_RING_MIN_PENDING ||
 941	    param->rx_pending > HNS3_RING_MAX_PENDING ||
 942	    param->rx_pending < HNS3_RING_MIN_PENDING) {
 943		netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
 944			   HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
 945		return -EINVAL;
 946	}
 947
 948	return 0;
 949}
 950
 951static int hns3_set_ringparam(struct net_device *ndev,
 952			      struct ethtool_ringparam *param)
 953{
 954	struct hns3_nic_priv *priv = netdev_priv(ndev);
 955	struct hnae3_handle *h = priv->ae_handle;
 956	struct hns3_enet_ring *tmp_rings;
 957	bool if_running = netif_running(ndev);
 958	u32 old_tx_desc_num, new_tx_desc_num;
 959	u32 old_rx_desc_num, new_rx_desc_num;
 960	u16 queue_num = h->kinfo.num_tqps;
 961	int ret, i;
 962
 963	ret = hns3_check_ringparam(ndev, param);
 964	if (ret)
 965		return ret;
 966
 967	/* Hardware requires that its descriptors must be multiple of eight */
 968	new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
 969	new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
 970	old_tx_desc_num = priv->ring_data[0].ring->desc_num;
 971	old_rx_desc_num = priv->ring_data[queue_num].ring->desc_num;
 972	if (old_tx_desc_num == new_tx_desc_num &&
 973	    old_rx_desc_num == new_rx_desc_num)
 974		return 0;
 975
 976	tmp_rings = hns3_backup_ringparam(priv);
 977	if (!tmp_rings) {
 978		netdev_err(ndev,
 979			   "backup ring param failed by allocating memory fail\n");
 980		return -ENOMEM;
 981	}
 982
 983	netdev_info(ndev,
 984		    "Changing Tx/Rx ring depth from %d/%d to %d/%d\n",
 985		    old_tx_desc_num, old_rx_desc_num,
 986		    new_tx_desc_num, new_rx_desc_num);
 987
 988	if (if_running)
 989		ndev->netdev_ops->ndo_stop(ndev);
 990
 991	hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
 992	ret = hns3_init_all_ring(priv);
 993	if (ret) {
 994		netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
 995			   ret);
 996
 997		hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
 998					    old_rx_desc_num);
 999		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1000			memcpy(priv->ring_data[i].ring, &tmp_rings[i],
1001			       sizeof(struct hns3_enet_ring));
1002	} else {
1003		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1004			hns3_fini_ring(&tmp_rings[i]);
1005	}
1006
1007	kfree(tmp_rings);
1008
1009	if (if_running)
1010		ret = ndev->netdev_ops->ndo_open(ndev);
1011
1012	return ret;
1013}
1014
1015static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1016{
1017	struct hnae3_handle *h = hns3_get_handle(netdev);
1018
1019	switch (cmd->cmd) {
1020	case ETHTOOL_SRXFH:
1021		if (h->ae_algo->ops->set_rss_tuple)
1022			return h->ae_algo->ops->set_rss_tuple(h, cmd);
1023		return -EOPNOTSUPP;
1024	case ETHTOOL_SRXCLSRLINS:
1025		if (h->ae_algo->ops->add_fd_entry)
1026			return h->ae_algo->ops->add_fd_entry(h, cmd);
1027		return -EOPNOTSUPP;
1028	case ETHTOOL_SRXCLSRLDEL:
1029		if (h->ae_algo->ops->del_fd_entry)
1030			return h->ae_algo->ops->del_fd_entry(h, cmd);
1031		return -EOPNOTSUPP;
1032	default:
1033		return -EOPNOTSUPP;
1034	}
1035}
1036
1037static int hns3_nway_reset(struct net_device *netdev)
1038{
1039	struct hnae3_handle *handle = hns3_get_handle(netdev);
1040	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1041	struct phy_device *phy = netdev->phydev;
1042	int autoneg;
1043
1044	if (!netif_running(netdev))
1045		return 0;
1046
1047	if (hns3_nic_resetting(netdev)) {
1048		netdev_err(netdev, "dev resetting!");
1049		return -EBUSY;
1050	}
1051
1052	if (!ops->get_autoneg || !ops->restart_autoneg)
1053		return -EOPNOTSUPP;
1054
1055	autoneg = ops->get_autoneg(handle);
1056	if (autoneg != AUTONEG_ENABLE) {
1057		netdev_err(netdev,
1058			   "Autoneg is off, don't support to restart it\n");
1059		return -EINVAL;
1060	}
1061
1062	netif_dbg(handle, drv, netdev,
1063		  "nway reset (using %s)\n", phy ? "phy" : "mac");
1064
1065	if (phy)
1066		return genphy_restart_aneg(phy);
1067
1068	if (handle->pdev->revision == 0x20)
1069		return -EOPNOTSUPP;
1070
1071	return ops->restart_autoneg(handle);
1072}
1073
1074static void hns3_get_channels(struct net_device *netdev,
1075			      struct ethtool_channels *ch)
1076{
1077	struct hnae3_handle *h = hns3_get_handle(netdev);
1078
1079	if (h->ae_algo->ops->get_channels)
1080		h->ae_algo->ops->get_channels(h, ch);
1081}
1082
1083static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
1084				       struct ethtool_coalesce *cmd)
1085{
1086	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1087	struct hns3_nic_priv *priv = netdev_priv(netdev);
1088	struct hnae3_handle *h = priv->ae_handle;
1089	u16 queue_num = h->kinfo.num_tqps;
1090
1091	if (hns3_nic_resetting(netdev))
1092		return -EBUSY;
1093
1094	if (queue >= queue_num) {
1095		netdev_err(netdev,
1096			   "Invalid queue value %d! Queue max id=%d\n",
1097			   queue, queue_num - 1);
1098		return -EINVAL;
1099	}
1100
1101	tx_vector = priv->ring_data[queue].ring->tqp_vector;
1102	rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
1103
1104	cmd->use_adaptive_tx_coalesce =
1105			tx_vector->tx_group.coal.gl_adapt_enable;
1106	cmd->use_adaptive_rx_coalesce =
1107			rx_vector->rx_group.coal.gl_adapt_enable;
1108
1109	cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
1110	cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
1111
1112	cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1113	cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;
1114
1115	return 0;
1116}
1117
1118static int hns3_get_coalesce(struct net_device *netdev,
1119			     struct ethtool_coalesce *cmd)
1120{
1121	return hns3_get_coalesce_per_queue(netdev, 0, cmd);
1122}
1123
1124static int hns3_check_gl_coalesce_para(struct net_device *netdev,
1125				       struct ethtool_coalesce *cmd)
1126{
1127	u32 rx_gl, tx_gl;
1128
1129	if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
1130		netdev_err(netdev,
1131			   "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
1132			   HNS3_INT_GL_MAX);
1133		return -EINVAL;
1134	}
1135
1136	if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
1137		netdev_err(netdev,
1138			   "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
1139			   HNS3_INT_GL_MAX);
1140		return -EINVAL;
1141	}
1142
1143	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
1144	if (rx_gl != cmd->rx_coalesce_usecs) {
1145		netdev_info(netdev,
1146			    "rx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
1147			    cmd->rx_coalesce_usecs, rx_gl);
1148	}
1149
1150	tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
1151	if (tx_gl != cmd->tx_coalesce_usecs) {
1152		netdev_info(netdev,
1153			    "tx_usecs(%d) rounded down to %d, because it must be multiple of 2.\n",
1154			    cmd->tx_coalesce_usecs, tx_gl);
1155	}
1156
1157	return 0;
1158}
1159
1160static int hns3_check_rl_coalesce_para(struct net_device *netdev,
1161				       struct ethtool_coalesce *cmd)
1162{
1163	u32 rl;
1164
1165	if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
1166		netdev_err(netdev,
1167			   "tx_usecs_high must be same as rx_usecs_high.\n");
1168		return -EINVAL;
1169	}
1170
1171	if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
1172		netdev_err(netdev,
1173			   "Invalid usecs_high value, usecs_high range is 0-%d\n",
1174			   HNS3_INT_RL_MAX);
1175		return -EINVAL;
1176	}
1177
1178	rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1179	if (rl != cmd->rx_coalesce_usecs_high) {
1180		netdev_info(netdev,
1181			    "usecs_high(%d) rounded down to %d, because it must be multiple of 4.\n",
1182			    cmd->rx_coalesce_usecs_high, rl);
1183	}
1184
1185	return 0;
1186}
1187
1188static int hns3_check_coalesce_para(struct net_device *netdev,
1189				    struct ethtool_coalesce *cmd)
1190{
1191	int ret;
1192
1193	ret = hns3_check_gl_coalesce_para(netdev, cmd);
1194	if (ret) {
1195		netdev_err(netdev,
1196			   "Check gl coalesce param fail. ret = %d\n", ret);
1197		return ret;
1198	}
1199
1200	ret = hns3_check_rl_coalesce_para(netdev, cmd);
1201	if (ret) {
1202		netdev_err(netdev,
1203			   "Check rl coalesce param fail. ret = %d\n", ret);
1204		return ret;
1205	}
1206
1207	if (cmd->use_adaptive_tx_coalesce == 1 ||
1208	    cmd->use_adaptive_rx_coalesce == 1) {
1209		netdev_info(netdev,
1210			    "adaptive-tx=%d and adaptive-rx=%d, tx_usecs or rx_usecs will changed dynamically.\n",
1211			    cmd->use_adaptive_tx_coalesce,
1212			    cmd->use_adaptive_rx_coalesce);
1213	}
1214
1215	return 0;
1216}
1217
1218static void hns3_set_coalesce_per_queue(struct net_device *netdev,
1219					struct ethtool_coalesce *cmd,
1220					u32 queue)
1221{
1222	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
1223	struct hns3_nic_priv *priv = netdev_priv(netdev);
1224	struct hnae3_handle *h = priv->ae_handle;
1225	int queue_num = h->kinfo.num_tqps;
1226
1227	tx_vector = priv->ring_data[queue].ring->tqp_vector;
1228	rx_vector = priv->ring_data[queue_num + queue].ring->tqp_vector;
1229
1230	tx_vector->tx_group.coal.gl_adapt_enable =
1231				cmd->use_adaptive_tx_coalesce;
1232	rx_vector->rx_group.coal.gl_adapt_enable =
1233				cmd->use_adaptive_rx_coalesce;
1234
1235	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
1236	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1237
1238	hns3_set_vector_coalesce_tx_gl(tx_vector,
1239				       tx_vector->tx_group.coal.int_gl);
1240	hns3_set_vector_coalesce_rx_gl(rx_vector,
1241				       rx_vector->rx_group.coal.int_gl);
1242
1243	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
1244	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1245}
1246
1247static int hns3_set_coalesce(struct net_device *netdev,
1248			     struct ethtool_coalesce *cmd)
1249{
1250	struct hnae3_handle *h = hns3_get_handle(netdev);
1251	u16 queue_num = h->kinfo.num_tqps;
1252	int ret;
1253	int i;
1254
1255	if (hns3_nic_resetting(netdev))
1256		return -EBUSY;
1257
1258	ret = hns3_check_coalesce_para(netdev, cmd);
1259	if (ret)
1260		return ret;
1261
1262	h->kinfo.int_rl_setting =
1263		hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
1264
1265	for (i = 0; i < queue_num; i++)
1266		hns3_set_coalesce_per_queue(netdev, cmd, i);
1267
1268	return 0;
1269}
1270
1271static int hns3_get_regs_len(struct net_device *netdev)
1272{
1273	struct hnae3_handle *h = hns3_get_handle(netdev);
1274
1275	if (!h->ae_algo->ops->get_regs_len)
1276		return -EOPNOTSUPP;
1277
1278	return h->ae_algo->ops->get_regs_len(h);
1279}
1280
1281static void hns3_get_regs(struct net_device *netdev,
1282			  struct ethtool_regs *cmd, void *data)
1283{
1284	struct hnae3_handle *h = hns3_get_handle(netdev);
1285
1286	if (!h->ae_algo->ops->get_regs)
1287		return;
1288
1289	h->ae_algo->ops->get_regs(h, &cmd->version, data);
1290}
1291
1292static int hns3_set_phys_id(struct net_device *netdev,
1293			    enum ethtool_phys_id_state state)
1294{
1295	struct hnae3_handle *h = hns3_get_handle(netdev);
1296
1297	if (!h->ae_algo->ops->set_led_id)
1298		return -EOPNOTSUPP;
1299
1300	return h->ae_algo->ops->set_led_id(h, state);
1301}
1302
1303static u32 hns3_get_msglevel(struct net_device *netdev)
1304{
1305	struct hnae3_handle *h = hns3_get_handle(netdev);
1306
1307	return h->msg_enable;
1308}
1309
1310static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
1311{
1312	struct hnae3_handle *h = hns3_get_handle(netdev);
1313
1314	h->msg_enable = msg_level;
1315}
1316
1317/* Translate local fec value into ethtool value. */
1318static unsigned int loc_to_eth_fec(u8 loc_fec)
1319{
1320	u32 eth_fec = 0;
1321
1322	if (loc_fec & BIT(HNAE3_FEC_AUTO))
1323		eth_fec |= ETHTOOL_FEC_AUTO;
1324	if (loc_fec & BIT(HNAE3_FEC_RS))
1325		eth_fec |= ETHTOOL_FEC_RS;
1326	if (loc_fec & BIT(HNAE3_FEC_BASER))
1327		eth_fec |= ETHTOOL_FEC_BASER;
1328
1329	/* if nothing is set, then FEC is off */
1330	if (!eth_fec)
1331		eth_fec = ETHTOOL_FEC_OFF;
1332
1333	return eth_fec;
1334}
1335
1336/* Translate ethtool fec value into local value. */
1337static unsigned int eth_to_loc_fec(unsigned int eth_fec)
1338{
1339	u32 loc_fec = 0;
1340
1341	if (eth_fec & ETHTOOL_FEC_OFF)
1342		return loc_fec;
1343
1344	if (eth_fec & ETHTOOL_FEC_AUTO)
1345		loc_fec |= BIT(HNAE3_FEC_AUTO);
1346	if (eth_fec & ETHTOOL_FEC_RS)
1347		loc_fec |= BIT(HNAE3_FEC_RS);
1348	if (eth_fec & ETHTOOL_FEC_BASER)
1349		loc_fec |= BIT(HNAE3_FEC_BASER);
1350
1351	return loc_fec;
1352}
1353
1354static int hns3_get_fecparam(struct net_device *netdev,
1355			     struct ethtool_fecparam *fec)
1356{
1357	struct hnae3_handle *handle = hns3_get_handle(netdev);
1358	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1359	u8 fec_ability;
1360	u8 fec_mode;
1361
1362	if (handle->pdev->revision == 0x20)
1363		return -EOPNOTSUPP;
1364
1365	if (!ops->get_fec)
1366		return -EOPNOTSUPP;
1367
1368	ops->get_fec(handle, &fec_ability, &fec_mode);
1369
1370	fec->fec = loc_to_eth_fec(fec_ability);
1371	fec->active_fec = loc_to_eth_fec(fec_mode);
1372
1373	return 0;
1374}
1375
1376static int hns3_set_fecparam(struct net_device *netdev,
1377			     struct ethtool_fecparam *fec)
1378{
1379	struct hnae3_handle *handle = hns3_get_handle(netdev);
1380	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1381	u32 fec_mode;
1382
1383	if (handle->pdev->revision == 0x20)
1384		return -EOPNOTSUPP;
1385
1386	if (!ops->set_fec)
1387		return -EOPNOTSUPP;
1388	fec_mode = eth_to_loc_fec(fec->fec);
1389
1390	netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);
1391
1392	return ops->set_fec(handle, fec_mode);
1393}
1394
1395static const struct ethtool_ops hns3vf_ethtool_ops = {
1396	.get_drvinfo = hns3_get_drvinfo,
1397	.get_ringparam = hns3_get_ringparam,
1398	.set_ringparam = hns3_set_ringparam,
1399	.get_strings = hns3_get_strings,
1400	.get_ethtool_stats = hns3_get_stats,
1401	.get_sset_count = hns3_get_sset_count,
1402	.get_rxnfc = hns3_get_rxnfc,
1403	.set_rxnfc = hns3_set_rxnfc,
1404	.get_rxfh_key_size = hns3_get_rss_key_size,
1405	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1406	.get_rxfh = hns3_get_rss,
1407	.set_rxfh = hns3_set_rss,
1408	.get_link_ksettings = hns3_get_link_ksettings,
1409	.get_channels = hns3_get_channels,
1410	.set_channels = hns3_set_channels,
1411	.get_coalesce = hns3_get_coalesce,
1412	.set_coalesce = hns3_set_coalesce,
1413	.get_regs_len = hns3_get_regs_len,
1414	.get_regs = hns3_get_regs,
1415	.get_link = hns3_get_link,
1416	.get_msglevel = hns3_get_msglevel,
1417	.set_msglevel = hns3_set_msglevel,
1418};
1419
1420static const struct ethtool_ops hns3_ethtool_ops = {
1421	.self_test = hns3_self_test,
1422	.get_drvinfo = hns3_get_drvinfo,
1423	.get_link = hns3_get_link,
1424	.get_ringparam = hns3_get_ringparam,
1425	.set_ringparam = hns3_set_ringparam,
1426	.get_pauseparam = hns3_get_pauseparam,
1427	.set_pauseparam = hns3_set_pauseparam,
1428	.get_strings = hns3_get_strings,
1429	.get_ethtool_stats = hns3_get_stats,
1430	.get_sset_count = hns3_get_sset_count,
1431	.get_rxnfc = hns3_get_rxnfc,
1432	.set_rxnfc = hns3_set_rxnfc,
1433	.get_rxfh_key_size = hns3_get_rss_key_size,
1434	.get_rxfh_indir_size = hns3_get_rss_indir_size,
1435	.get_rxfh = hns3_get_rss,
1436	.set_rxfh = hns3_set_rss,
1437	.get_link_ksettings = hns3_get_link_ksettings,
1438	.set_link_ksettings = hns3_set_link_ksettings,
1439	.nway_reset = hns3_nway_reset,
1440	.get_channels = hns3_get_channels,
1441	.set_channels = hns3_set_channels,
1442	.get_coalesce = hns3_get_coalesce,
1443	.set_coalesce = hns3_set_coalesce,
1444	.get_regs_len = hns3_get_regs_len,
1445	.get_regs = hns3_get_regs,
1446	.set_phys_id = hns3_set_phys_id,
1447	.get_msglevel = hns3_get_msglevel,
1448	.set_msglevel = hns3_set_msglevel,
1449	.get_fecparam = hns3_get_fecparam,
1450	.set_fecparam = hns3_set_fecparam,
1451};
1452
1453void hns3_ethtool_set_ops(struct net_device *netdev)
1454{
1455	struct hnae3_handle *h = hns3_get_handle(netdev);
1456
1457	if (h->flags & HNAE3_SUPPORT_VF)
1458		netdev->ethtool_ops = &hns3vf_ethtool_ops;
1459	else
1460		netdev->ethtool_ops = &hns3_ethtool_ops;
1461}