hns3_ethtool.c 37.5 KB
Newer Older
1 2
// SPDX-License-Identifier: GPL-2.0+
// Copyright (c) 2016-2017 Hisilicon Limited.
3 4 5

#include <linux/etherdevice.h>
#include <linux/string.h>
6
#include <linux/phy.h>
7 8 9 10 11 12 13 14 15 16 17

#include "hns3_enet.h"

struct hns3_stats {
	char stats_string[ETH_GSTRING_LEN];
	int stats_offset;
};

/* tqp related stats */
#define HNS3_TQP_STAT(_string, _member)	{			\
	.stats_string = _string,				\
18 19
	.stats_offset = offsetof(struct hns3_enet_ring, stats) +\
			offsetof(struct ring_stats, _member),   \
20
}
21 22 23

static const struct hns3_stats hns3_txq_stats[] = {
	/* Tx per-queue statistics */
24
	HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
25
	HNS3_TQP_STAT("dropped", sw_err_cnt),
26 27 28 29
	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
	HNS3_TQP_STAT("packets", tx_pkts),
	HNS3_TQP_STAT("bytes", tx_bytes),
	HNS3_TQP_STAT("errors", tx_err_cnt),
30 31
	HNS3_TQP_STAT("wake", restart_queue),
	HNS3_TQP_STAT("busy", tx_busy),
32
	HNS3_TQP_STAT("copy", tx_copy),
33 34 35 36
	HNS3_TQP_STAT("vlan_err", tx_vlan_err),
	HNS3_TQP_STAT("l4_proto_err", tx_l4_proto_err),
	HNS3_TQP_STAT("l2l3l4_err", tx_l2l3l4_err),
	HNS3_TQP_STAT("tso_err", tx_tso_err),
37 38 39 40 41 42
};

#define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)

static const struct hns3_stats hns3_rxq_stats[] = {
	/* Rx per-queue statistics */
43
	HNS3_TQP_STAT("io_err_cnt", io_err_cnt),
44
	HNS3_TQP_STAT("dropped", sw_err_cnt),
45 46 47 48 49 50 51 52 53
	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
	HNS3_TQP_STAT("packets", rx_pkts),
	HNS3_TQP_STAT("bytes", rx_bytes),
	HNS3_TQP_STAT("errors", rx_err_cnt),
	HNS3_TQP_STAT("reuse_pg_cnt", reuse_pg_cnt),
	HNS3_TQP_STAT("err_pkt_len", err_pkt_len),
	HNS3_TQP_STAT("err_bd_num", err_bd_num),
	HNS3_TQP_STAT("l2_err", l2_err),
	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 59 60 61
};

#define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)

#define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)

Y
Yufeng Mo 已提交
62
#define HNS3_SELF_TEST_TYPE_NUM         4
63 64 65
#define HNS3_NIC_LB_TEST_PKT_NUM	1
#define HNS3_NIC_LB_TEST_RING_ID	0
#define HNS3_NIC_LB_TEST_PACKET_SIZE	128
66
#define HNS3_NIC_LB_SETUP_USEC		10000
67 68 69 70 71 72

/* Nic loopback test err  */
#define HNS3_NIC_LB_TEST_NO_MEM_ERR	1
#define HNS3_NIC_LB_TEST_TX_CNT_ERR	2
#define HNS3_NIC_LB_TEST_RX_CNT_ERR	3

73
static 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 78 79 80 81 82 83
	int ret;

	if (!h->ae_algo->ops->set_loopback ||
	    !h->ae_algo->ops->set_promisc_mode)
		return -EOPNOTSUPP;

	switch (loop) {
84 85
	case HNAE3_LOOP_SERIAL_SERDES:
	case HNAE3_LOOP_PARALLEL_SERDES:
86
	case HNAE3_LOOP_APP:
Y
Yufeng Mo 已提交
87
	case HNAE3_LOOP_PHY:
88
		ret = h->ae_algo->ops->set_loopback(h, loop, en);
89 90 91 92 93 94
		break;
	default:
		ret = -ENOTSUPP;
		break;
	}

95
	if (ret || h->pdev->revision >= 0x21)
96 97
		return ret;

98 99 100 101
	if (en) {
		h->ae_algo->ops->set_promisc_mode(h, true, true);
	} else {
		/* recover promisc mode before loopback test */
102
		hns3_request_update_promisc_mode(h);
103 104 105
		vlan_filter_enable = ndev->flags & IFF_PROMISC ? false : true;
		hns3_enable_vlan_filter(ndev, vlan_filter_enable);
	}
106 107 108 109 110 111 112 113 114

	return ret;
}

static int hns3_lp_up(struct net_device *ndev, enum hnae3_loop loop_mode)
{
	struct hnae3_handle *h = hns3_get_handle(ndev);
	int ret;

115 116 117 118
	ret = hns3_nic_reset_all_ring(h);
	if (ret)
		return ret;

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
}

125
static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
126 127 128
{
	int ret;

129
	ret = hns3_lp_setup(ndev, loop_mode, false);
130 131 132 133 134
	if (ret) {
		netdev_err(ndev, "lb_setup return error: %d\n", ret);
		return ret;
	}

135
	usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
136 137 138 139 140 141

	return 0;
}

static void hns3_lp_setup_skb(struct sk_buff *skb)
{
142 143
#define	HNS3_NIC_LB_DST_MAC_ADDR	0x1f

144
	struct net_device *ndev = skb->dev;
145
	struct hnae3_handle *handle;
146 147 148 149 150 151 152 153 154
	unsigned char *packet;
	struct ethhdr *ethh;
	unsigned int i;

	skb_reserve(skb, NET_IP_ALIGN);
	ethh = skb_put(skb, sizeof(struct ethhdr));
	packet = skb_put(skb, HNS3_NIC_LB_TEST_PACKET_SIZE);

	memcpy(ethh->h_dest, ndev->dev_addr, ETH_ALEN);
155 156 157 158 159 160

	/* The dst mac addr of loopback packet is the same as the host'
	 * mac addr, the SSU component may loop back the packet to host
	 * before the packet reaches mac or serdes, which will defect
	 * the purpose of mac or serdes selftest.
	 */
161 162 163
	handle = hns3_get_handle(ndev);
	if (handle->pdev->revision == 0x20)
		ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
	eth_zero_addr(ethh->h_source);
	ethh->h_proto = htons(ETH_P_ARP);
	skb_reset_mac_header(skb);

	for (i = 0; i < HNS3_NIC_LB_TEST_PACKET_SIZE; i++)
		packet[i] = (unsigned char)(i & 0xff);
}

static void hns3_lb_check_skb_data(struct hns3_enet_ring *ring,
				   struct sk_buff *skb)
{
	struct hns3_enet_tqp_vector *tqp_vector = ring->tqp_vector;
	unsigned char *packet = skb->data;
	u32 i;

	for (i = 0; i < skb->len; i++)
		if (packet[i] != (unsigned char)(i & 0xff))
			break;

	/* The packet is correctly received */
	if (i == skb->len)
		tqp_vector->rx_group.total_packets++;
	else
		print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
			       skb->data, skb->len, true);

	dev_kfree_skb_any(skb);
}

static u32 hns3_lb_check_rx_ring(struct hns3_nic_priv *priv, u32 budget)
{
	struct hnae3_handle *h = priv->ae_handle;
	struct hnae3_knic_private_info *kinfo;
	u32 i, rcv_good_pkt_total = 0;

	kinfo = &h->kinfo;
	for (i = kinfo->num_tqps; i < kinfo->num_tqps * 2; i++) {
201
		struct hns3_enet_ring *ring = &priv->ring[i];
202 203 204 205 206 207
		struct hns3_enet_ring_group *rx_group;
		u64 pre_rx_pkt;

		rx_group = &ring->tqp_vector->rx_group;
		pre_rx_pkt = rx_group->total_packets;

208
		preempt_disable();
209
		hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
210
		preempt_enable();
211 212 213 214 215 216 217 218 219 220 221 222 223

		rcv_good_pkt_total += (rx_group->total_packets - pre_rx_pkt);
		rx_group->total_packets = pre_rx_pkt;
	}
	return rcv_good_pkt_total;
}

static void hns3_lb_clear_tx_ring(struct hns3_nic_priv *priv, u32 start_ringid,
				  u32 end_ringid, u32 budget)
{
	u32 i;

	for (i = start_ringid; i <= end_ringid; i++) {
224
		struct hns3_enet_ring *ring = &priv->ring[i];
225

226
		hns3_clean_tx_ring(ring);
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	}
}

/**
 * hns3_lp_run_test -  run loopback test
 * @ndev: net device
 * @mode: loopback type
 */
static int hns3_lp_run_test(struct net_device *ndev, enum hnae3_loop mode)
{
	struct hns3_nic_priv *priv = netdev_priv(ndev);
	struct sk_buff *skb;
	u32 i, good_cnt;
	int ret_val = 0;

	skb = alloc_skb(HNS3_NIC_LB_TEST_PACKET_SIZE + ETH_HLEN + NET_IP_ALIGN,
			GFP_KERNEL);
	if (!skb)
		return HNS3_NIC_LB_TEST_NO_MEM_ERR;

	skb->dev = ndev;
	hns3_lp_setup_skb(skb);
	skb->queue_mapping = HNS3_NIC_LB_TEST_RING_ID;

	good_cnt = 0;
	for (i = 0; i < HNS3_NIC_LB_TEST_PKT_NUM; i++) {
		netdev_tx_t tx_ret;

		skb_get(skb);
		tx_ret = hns3_nic_net_xmit(skb, ndev);
257
		if (tx_ret == NETDEV_TX_OK) {
258
			good_cnt++;
259 260
		} else {
			kfree_skb(skb);
261 262
			netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
				   tx_ret);
263
		}
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	}
	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
		ret_val = HNS3_NIC_LB_TEST_TX_CNT_ERR;
		netdev_err(ndev, "mode %d sent fail, cnt=0x%x, budget=0x%x\n",
			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
		goto out;
	}

	/* Allow 200 milliseconds for packets to go from Tx to Rx */
	msleep(200);

	good_cnt = hns3_lb_check_rx_ring(priv, HNS3_NIC_LB_TEST_PKT_NUM);
	if (good_cnt != HNS3_NIC_LB_TEST_PKT_NUM) {
		ret_val = HNS3_NIC_LB_TEST_RX_CNT_ERR;
		netdev_err(ndev, "mode %d recv fail, cnt=0x%x, budget=0x%x\n",
			   mode, good_cnt, HNS3_NIC_LB_TEST_PKT_NUM);
	}

out:
	hns3_lb_clear_tx_ring(priv, HNS3_NIC_LB_TEST_RING_ID,
			      HNS3_NIC_LB_TEST_RING_ID,
			      HNS3_NIC_LB_TEST_PKT_NUM);

	kfree_skb(skb);
	return ret_val;
}

/**
 * hns3_nic_self_test - self test
 * @ndev: net device
 * @eth_test: test cmd
 * @data: test result
 */
static void hns3_self_test(struct net_device *ndev,
			   struct ethtool_test *eth_test, u64 *data)
{
	struct hns3_nic_priv *priv = netdev_priv(ndev);
	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 305 306
#if IS_ENABLED(CONFIG_VLAN_8021Q)
	bool dis_vlan_filter;
#endif
307 308 309
	int test_index = 0;
	u32 i;

310 311 312 313 314
	if (hns3_nic_resetting(ndev)) {
		netdev_err(ndev, "dev resetting!");
		return;
	}

315 316 317 318
	/* Only do offline selftest, or pass by default */
	if (eth_test->flags != ETH_TEST_FL_OFFLINE)
		return;

319 320
	netif_dbg(h, drv, ndev, "self test start");

321 322 323
	st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
	st_param[HNAE3_LOOP_APP][1] =
			h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
324

325 326 327 328 329 330 331 332
	st_param[HNAE3_LOOP_SERIAL_SERDES][0] = HNAE3_LOOP_SERIAL_SERDES;
	st_param[HNAE3_LOOP_SERIAL_SERDES][1] =
			h->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;

	st_param[HNAE3_LOOP_PARALLEL_SERDES][0] =
			HNAE3_LOOP_PARALLEL_SERDES;
	st_param[HNAE3_LOOP_PARALLEL_SERDES][1] =
			h->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
333

Y
Yufeng Mo 已提交
334 335 336 337
	st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
	st_param[HNAE3_LOOP_PHY][1] =
			h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;

338
	if (if_running)
339
		ndev->netdev_ops->ndo_stop(ndev);
340

341 342 343 344 345 346 347 348
#if IS_ENABLED(CONFIG_VLAN_8021Q)
	/* Disable the vlan filter for selftest does not support it */
	dis_vlan_filter = (ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
				h->ae_algo->ops->enable_vlan_filter;
	if (dis_vlan_filter)
		h->ae_algo->ops->enable_vlan_filter(h, false);
#endif

349 350 351 352 353 354 355
	/* Tell firmware to stop mac autoneg before loopback test start,
	 * otherwise loopback test may be failed when the port is still
	 * negotiating.
	 */
	if (h->ae_algo->ops->halt_autoneg)
		h->ae_algo->ops->halt_autoneg(h, true);

356 357
	set_bit(HNS3_NIC_STATE_TESTING, &priv->state);

358
	for (i = 0; i < HNS3_SELF_TEST_TYPE_NUM; i++) {
359 360 361 362 363 364
		enum hnae3_loop loop_type = (enum hnae3_loop)st_param[i][0];

		if (!st_param[i][1])
			continue;

		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 371 372 373 374 375 376 377

		if (data[test_index])
			eth_test->flags |= ETH_TEST_FL_FAILED;

		test_index++;
	}

	clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);

378 379 380
	if (h->ae_algo->ops->halt_autoneg)
		h->ae_algo->ops->halt_autoneg(h, false);

381 382 383 384 385
#if IS_ENABLED(CONFIG_VLAN_8021Q)
	if (dis_vlan_filter)
		h->ae_algo->ops->enable_vlan_filter(h, true);
#endif

386
	if (if_running)
387
		ndev->netdev_ops->ndo_open(ndev);
388 389

	netif_dbg(h, drv, ndev, "self test end\n");
390 391
}

392 393
static int hns3_get_sset_count(struct net_device *netdev, int stringset)
{
394
	struct hnae3_handle *h = hns3_get_handle(netdev);
395 396 397 398 399 400 401 402 403 404 405 406 407
	const struct hnae3_ae_ops *ops = h->ae_algo->ops;

	if (!ops->get_sset_count)
		return -EOPNOTSUPP;

	switch (stringset) {
	case ETH_SS_STATS:
		return ((HNS3_TQP_STATS_COUNT * h->kinfo.num_tqps) +
			ops->get_sset_count(h, stringset));

	case ETH_SS_TEST:
		return ops->get_sset_count(h, stringset);

408 409 410
	default:
		return -EOPNOTSUPP;
	}
411 412 413
}

static 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 418 419 420 421 422 423 424 425
	u32 size_left;
	u32 i, j;
	u32 n1;

	for (i = 0; i < num_tqps; i++) {
		for (j = 0; j < stat_count; j++) {
			data[ETH_GSTRING_LEN - 1] = '\0';

			/* first, prepend the prefix string */
426 427
			n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%d_",
				       prefix, i);
428 429 430 431 432 433 434 435 436 437 438 439 440 441
			size_left = (ETH_GSTRING_LEN - 1) - n1;

			/* now, concatenate the stats string to it */
			strncat(data, stats[j].stats_string, size_left);
			data += ETH_GSTRING_LEN;
		}
	}

	return data;
}

static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data)
{
	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
442 443
	const char tx_prefix[] = "txq";
	const char rx_prefix[] = "rxq";
444 445 446

	/* get strings for Tx */
	data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
447
				   kinfo->num_tqps, tx_prefix);
448 449 450

	/* get strings for Rx */
	data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
451
				   kinfo->num_tqps, rx_prefix);
452 453 454 455 456 457

	return data;
}

static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
{
458
	struct hnae3_handle *h = hns3_get_handle(netdev);
459 460 461 462 463 464 465 466 467
	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
	char *buff = (char *)data;

	if (!ops->get_strings)
		return;

	switch (stringset) {
	case ETH_SS_STATS:
		buff = hns3_get_strings_tqps(h, buff);
468
		ops->get_strings(h, stringset, (u8 *)buff);
469 470 471 472
		break;
	case ETH_SS_TEST:
		ops->get_strings(h, stringset, data);
		break;
473 474
	default:
		break;
475 476 477 478 479 480 481 482 483
	}
}

static u64 *hns3_get_stats_tqps(struct hnae3_handle *handle, u64 *data)
{
	struct hns3_nic_priv *nic_priv = (struct hns3_nic_priv *)handle->priv;
	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
	struct hns3_enet_ring *ring;
	u8 *stat;
484
	int i, j;
485 486 487

	/* get stats for Tx */
	for (i = 0; i < kinfo->num_tqps; i++) {
488
		ring = &nic_priv->ring[i];
489 490
		for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
			stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
491 492 493 494 495 496
			*data++ = *(u64 *)stat;
		}
	}

	/* get stats for Rx */
	for (i = 0; i < kinfo->num_tqps; i++) {
497
		ring = &nic_priv->ring[i + kinfo->num_tqps];
498 499
		for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
			stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
500 501 502 503 504 505 506 507 508 509 510 511
			*data++ = *(u64 *)stat;
		}
	}

	return data;
}

/* hns3_get_stats - get detail statistics.
 * @netdev: net device
 * @stats: statistics info.
 * @data: statistics data.
 */
512 513
static void hns3_get_stats(struct net_device *netdev,
			   struct ethtool_stats *stats, u64 *data)
514
{
515
	struct hnae3_handle *h = hns3_get_handle(netdev);
516 517
	u64 *p = data;

518 519 520 521 522
	if (hns3_nic_resetting(netdev)) {
		netdev_err(netdev, "dev resetting, could not get stats\n");
		return;
	}

523 524 525 526 527
	if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
		netdev_err(netdev, "could not get any statistics\n");
		return;
	}

528
	h->ae_algo->ops->update_stats(h, &netdev->stats);
529 530 531 532 533 534 535 536 537 538 539 540 541

	/* get per-queue stats */
	p = hns3_get_stats_tqps(h, p);

	/* get MAC & other misc hardware stats */
	h->ae_algo->ops->get_stats(h, p);
}

static void hns3_get_drvinfo(struct net_device *netdev,
			     struct ethtool_drvinfo *drvinfo)
{
	struct hns3_nic_priv *priv = netdev_priv(netdev);
	struct hnae3_handle *h = priv->ae_handle;
542
	u32 fw_version;
543

544 545 546 547 548
	if (!h->ae_algo->ops->get_fw_version) {
		netdev_err(netdev, "could not get fw version!\n");
		return;
	}

549 550 551 552 553 554 555 556
	strncpy(drvinfo->driver, h->pdev->driver->name,
		sizeof(drvinfo->driver));
	drvinfo->driver[sizeof(drvinfo->driver) - 1] = '\0';

	strncpy(drvinfo->bus_info, pci_name(h->pdev),
		sizeof(drvinfo->bus_info));
	drvinfo->bus_info[ETHTOOL_BUSINFO_LEN - 1] = '\0';

557 558 559 560 561 562 563 564 565 566 567 568
	fw_version = priv->ae_handle->ae_algo->ops->get_fw_version(h);

	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
		 "%lu.%lu.%lu.%lu",
		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE3_MASK,
				 HNAE3_FW_VERSION_BYTE3_SHIFT),
		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE2_MASK,
				 HNAE3_FW_VERSION_BYTE2_SHIFT),
		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE1_MASK,
				 HNAE3_FW_VERSION_BYTE1_SHIFT),
		 hnae3_get_field(fw_version, HNAE3_FW_VERSION_BYTE0_MASK,
				 HNAE3_FW_VERSION_BYTE0_SHIFT));
569 570 571 572
}

static u32 hns3_get_link(struct net_device *netdev)
{
573
	struct hnae3_handle *h = hns3_get_handle(netdev);
574

575
	if (h->ae_algo->ops->get_status)
576 577 578 579 580 581 582 583 584
		return h->ae_algo->ops->get_status(h);
	else
		return 0;
}

static void hns3_get_ringparam(struct net_device *netdev,
			       struct ethtool_ringparam *param)
{
	struct hns3_nic_priv *priv = netdev_priv(netdev);
585 586
	struct hnae3_handle *h = priv->ae_handle;
	int queue_num = h->kinfo.num_tqps;
587

588 589 590 591 592
	if (hns3_nic_resetting(netdev)) {
		netdev_err(netdev, "dev resetting!");
		return;
	}

593 594 595
	param->tx_max_pending = HNS3_RING_MAX_PENDING;
	param->rx_max_pending = HNS3_RING_MAX_PENDING;

596 597
	param->tx_pending = priv->ring[0].desc_num;
	param->rx_pending = priv->ring[queue_num].desc_num;
598 599 600 601 602
}

static void hns3_get_pauseparam(struct net_device *netdev,
				struct ethtool_pauseparam *param)
{
603
	struct hnae3_handle *h = hns3_get_handle(netdev);
604

605
	if (h->ae_algo->ops->get_pauseparam)
606 607 608 609
		h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
			&param->rx_pause, &param->tx_pause);
}

610 611 612 613 614
static int hns3_set_pauseparam(struct net_device *netdev,
			       struct ethtool_pauseparam *param)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

615 616 617 618
	netif_dbg(h, drv, netdev,
		  "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
		  param->autoneg, param->rx_pause, param->tx_pause);

619 620 621 622 623 624 625
	if (h->ae_algo->ops->set_pauseparam)
		return h->ae_algo->ops->set_pauseparam(h, param->autoneg,
						       param->rx_pause,
						       param->tx_pause);
	return -EOPNOTSUPP;
}

626 627 628 629 630 631 632 633 634 635 636 637
static void hns3_get_ksettings(struct hnae3_handle *h,
			       struct ethtool_link_ksettings *cmd)
{
	const struct hnae3_ae_ops *ops = h->ae_algo->ops;

	/* 1.auto_neg & speed & duplex from cmd */
	if (ops->get_ksettings_an_result)
		ops->get_ksettings_an_result(h,
					     &cmd->base.autoneg,
					     &cmd->base.speed,
					     &cmd->base.duplex);

G
Guojia Liao 已提交
638
	/* 2.get link mode */
639 640 641 642 643 644 645 646 647 648 649
	if (ops->get_link_mode)
		ops->get_link_mode(h,
				   cmd->link_modes.supported,
				   cmd->link_modes.advertising);

	/* 3.mdix_ctrl&mdix get from phy reg */
	if (ops->get_mdix_mode)
		ops->get_mdix_mode(h, &cmd->base.eth_tp_mdix_ctrl,
				   &cmd->base.eth_tp_mdix);
}

650 651 652
static int hns3_get_link_ksettings(struct net_device *netdev,
				   struct ethtool_link_ksettings *cmd)
{
653
	struct hnae3_handle *h = hns3_get_handle(netdev);
654
	const struct hnae3_ae_ops *ops;
655
	u8 module_type;
656
	u8 media_type;
657 658
	u8 link_stat;

659
	ops = h->ae_algo->ops;
660
	if (ops->get_media_type)
661
		ops->get_media_type(h, &media_type, &module_type);
662 663 664
	else
		return -EOPNOTSUPP;

665 666 667 668 669 670
	switch (media_type) {
	case HNAE3_MEDIA_TYPE_NONE:
		cmd->base.port = PORT_NONE;
		hns3_get_ksettings(h, cmd);
		break;
	case HNAE3_MEDIA_TYPE_FIBER:
671 672 673 674 675 676 677 678 679
		if (module_type == HNAE3_MODULE_TYPE_CR)
			cmd->base.port = PORT_DA;
		else
			cmd->base.port = PORT_FIBRE;

		hns3_get_ksettings(h, cmd);
		break;
	case HNAE3_MEDIA_TYPE_BACKPLANE:
		cmd->base.port = PORT_NONE;
680
		hns3_get_ksettings(h, cmd);
681
		break;
682 683
	case HNAE3_MEDIA_TYPE_COPPER:
		cmd->base.port = PORT_TP;
684 685 686 687
		if (!netdev->phydev)
			hns3_get_ksettings(h, cmd);
		else
			phy_ethtool_ksettings_get(netdev->phydev, cmd);
688 689
		break;
	default:
690 691

		netdev_warn(netdev, "Unknown media type");
692 693 694
		return 0;
	}

695 696
	/* mdio_support */
	cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
697 698 699 700 701

	link_stat = hns3_get_link(netdev);
	if (!link_stat) {
		cmd->base.speed = SPEED_UNKNOWN;
		cmd->base.duplex = DUPLEX_UNKNOWN;
702 703 704 705 706
	}

	return 0;
}

707
static int hns3_check_ksettings_param(const struct net_device *netdev,
708 709 710 711 712 713 714 715 716 717 718
				      const struct ethtool_link_ksettings *cmd)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
	u8 module_type = HNAE3_MODULE_TYPE_UNKNOWN;
	u8 media_type = HNAE3_MEDIA_TYPE_UNKNOWN;
	u8 autoneg;
	u32 speed;
	u8 duplex;
	int ret;

719 720 721 722 723 724
	/* hw doesn't support use specified speed and duplex to negotiate,
	 * unnecessary to check them when autoneg on.
	 */
	if (cmd->base.autoneg)
		return 0;

725 726 727 728 729 730 731 732 733 734
	if (ops->get_ksettings_an_result) {
		ops->get_ksettings_an_result(handle, &autoneg, &speed, &duplex);
		if (cmd->base.autoneg == autoneg && cmd->base.speed == speed &&
		    cmd->base.duplex == duplex)
			return 0;
	}

	if (ops->get_media_type)
		ops->get_media_type(handle, &media_type, &module_type);

735
	if (cmd->base.duplex == DUPLEX_HALF &&
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	    media_type != HNAE3_MEDIA_TYPE_COPPER) {
		netdev_err(netdev,
			   "only copper port supports half duplex!");
		return -EINVAL;
	}

	if (ops->check_port_speed) {
		ret = ops->check_port_speed(handle, cmd->base.speed);
		if (ret) {
			netdev_err(netdev, "unsupported speed\n");
			return ret;
		}
	}

	return 0;
}

753 754 755
static int hns3_set_link_ksettings(struct net_device *netdev,
				   const struct ethtool_link_ksettings *cmd)
{
756 757
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
758
	int ret;
759 760

	/* Chip don't support this mode. */
761 762 763
	if (cmd->base.speed == SPEED_1000 && cmd->base.duplex == DUPLEX_HALF)
		return -EINVAL;

764 765 766 767 768
	netif_dbg(handle, drv, netdev,
		  "set link(%s): autoneg=%u, speed=%u, duplex=%u\n",
		  netdev->phydev ? "phy" : "mac",
		  cmd->base.autoneg, cmd->base.speed, cmd->base.duplex);

769 770 771 772
	/* Only support ksettings_set for netdev with phy attached for now */
	if (netdev->phydev)
		return phy_ethtool_ksettings_set(netdev->phydev, cmd);

773 774 775 776 777 778 779 780 781 782 783 784 785
	if (handle->pdev->revision == 0x20)
		return -EOPNOTSUPP;

	ret = hns3_check_ksettings_param(netdev, cmd);
	if (ret)
		return ret;

	if (ops->set_autoneg) {
		ret = ops->set_autoneg(handle, cmd->base.autoneg);
		if (ret)
			return ret;
	}

786 787 788 789 790 791 792 793 794
	/* hw doesn't support use specified speed and duplex to negotiate,
	 * ignore them when autoneg on.
	 */
	if (cmd->base.autoneg) {
		netdev_info(netdev,
			    "autoneg is on, ignore the speed and duplex\n");
		return 0;
	}

795 796 797 798 799
	if (ops->cfg_mac_speed_dup_h)
		ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
					       cmd->base.duplex);

	return ret;
800 801
}

802 803
static u32 hns3_get_rss_key_size(struct net_device *netdev)
{
804
	struct hnae3_handle *h = hns3_get_handle(netdev);
805

806
	if (!h->ae_algo->ops->get_rss_key_size)
807
		return 0;
808 809 810 811 812 813

	return h->ae_algo->ops->get_rss_key_size(h);
}

static u32 hns3_get_rss_indir_size(struct net_device *netdev)
{
814
	struct hnae3_handle *h = hns3_get_handle(netdev);
815

816
	if (!h->ae_algo->ops->get_rss_indir_size)
817
		return 0;
818 819 820 821 822 823 824

	return h->ae_algo->ops->get_rss_indir_size(h);
}

static int hns3_get_rss(struct net_device *netdev, u32 *indir, u8 *key,
			u8 *hfunc)
{
825
	struct hnae3_handle *h = hns3_get_handle(netdev);
826

827
	if (!h->ae_algo->ops->get_rss)
828 829 830 831 832 833 834 835
		return -EOPNOTSUPP;

	return h->ae_algo->ops->get_rss(h, indir, key, hfunc);
}

static int hns3_set_rss(struct net_device *netdev, const u32 *indir,
			const u8 *key, const u8 hfunc)
{
836
	struct hnae3_handle *h = hns3_get_handle(netdev);
837

838
	if (!h->ae_algo->ops->set_rss)
839 840
		return -EOPNOTSUPP;

841 842 843 844
	if ((h->pdev->revision == 0x20 &&
	     hfunc != ETH_RSS_HASH_TOP) || (hfunc != ETH_RSS_HASH_NO_CHANGE &&
	     hfunc != ETH_RSS_HASH_TOP && hfunc != ETH_RSS_HASH_XOR)) {
		netdev_err(netdev, "hash func not supported\n");
845 846
		return -EOPNOTSUPP;
	}
847

848 849 850 851 852 853 854 855 856 857 858 859 860
	if (!indir) {
		netdev_err(netdev,
			   "set rss failed for indir is empty\n");
		return -EOPNOTSUPP;
	}

	return h->ae_algo->ops->set_rss(h, indir, key, hfunc);
}

static int hns3_get_rxnfc(struct net_device *netdev,
			  struct ethtool_rxnfc *cmd,
			  u32 *rule_locs)
{
861
	struct hnae3_handle *h = hns3_get_handle(netdev);
862 863 864

	switch (cmd->cmd) {
	case ETHTOOL_GRXRINGS:
865 866
		cmd->data = h->kinfo.num_tqps;
		return 0;
L
Lipeng 已提交
867
	case ETHTOOL_GRXFH:
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
		if (h->ae_algo->ops->get_rss_tuple)
			return h->ae_algo->ops->get_rss_tuple(h, cmd);
		return -EOPNOTSUPP;
	case ETHTOOL_GRXCLSRLCNT:
		if (h->ae_algo->ops->get_fd_rule_cnt)
			return h->ae_algo->ops->get_fd_rule_cnt(h, cmd);
		return -EOPNOTSUPP;
	case ETHTOOL_GRXCLSRULE:
		if (h->ae_algo->ops->get_fd_rule_info)
			return h->ae_algo->ops->get_fd_rule_info(h, cmd);
		return -EOPNOTSUPP;
	case ETHTOOL_GRXCLSRLALL:
		if (h->ae_algo->ops->get_fd_all_rules)
			return h->ae_algo->ops->get_fd_all_rules(h, cmd,
								 rule_locs);
		return -EOPNOTSUPP;
884 885 886 887 888
	default:
		return -EOPNOTSUPP;
	}
}

889 890
static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
					u32 tx_desc_num, u32 rx_desc_num)
L
Lipeng 已提交
891 892 893 894
{
	struct hnae3_handle *h = priv->ae_handle;
	int i;

895 896
	h->kinfo.num_tx_desc = tx_desc_num;
	h->kinfo.num_rx_desc = rx_desc_num;
L
Lipeng 已提交
897

898
	for (i = 0; i < h->kinfo.num_tqps; i++) {
899 900
		priv->ring[i].desc_num = tx_desc_num;
		priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
901
	}
L
Lipeng 已提交
902 903
}

904
static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
L
Lipeng 已提交
905
{
906 907 908
	struct hnae3_handle *handle = priv->ae_handle;
	struct hns3_enet_ring *tmp_rings;
	int i;
L
Lipeng 已提交
909

910 911 912 913 914
	tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
			    sizeof(struct hns3_enet_ring), GFP_KERNEL);
	if (!tmp_rings)
		return NULL;

915
	for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
916
		memcpy(&tmp_rings[i], &priv->ring[i],
917
		       sizeof(struct hns3_enet_ring));
918 919
		tmp_rings[i].skb = NULL;
	}
920 921 922 923 924 925 926

	return tmp_rings;
}

static int hns3_check_ringparam(struct net_device *ndev,
				struct ethtool_ringparam *param)
{
927 928 929
	if (hns3_nic_resetting(ndev))
		return -EBUSY;

L
Lipeng 已提交
930 931 932 933
	if (param->rx_mini_pending || param->rx_jumbo_pending)
		return -EINVAL;

	if (param->tx_pending > HNS3_RING_MAX_PENDING ||
934 935 936 937 938
	    param->tx_pending < HNS3_RING_MIN_PENDING ||
	    param->rx_pending > HNS3_RING_MAX_PENDING ||
	    param->rx_pending < HNS3_RING_MIN_PENDING) {
		netdev_err(ndev, "Queue depth out of range [%d-%d]\n",
			   HNS3_RING_MIN_PENDING, HNS3_RING_MAX_PENDING);
L
Lipeng 已提交
939 940 941
		return -EINVAL;
	}

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
	return 0;
}

static int hns3_set_ringparam(struct net_device *ndev,
			      struct ethtool_ringparam *param)
{
	struct hns3_nic_priv *priv = netdev_priv(ndev);
	struct hnae3_handle *h = priv->ae_handle;
	struct hns3_enet_ring *tmp_rings;
	bool if_running = netif_running(ndev);
	u32 old_tx_desc_num, new_tx_desc_num;
	u32 old_rx_desc_num, new_rx_desc_num;
	u16 queue_num = h->kinfo.num_tqps;
	int ret, i;

	ret = hns3_check_ringparam(ndev, param);
	if (ret)
		return ret;

L
Lipeng 已提交
961
	/* Hardware requires that its descriptors must be multiple of eight */
962 963
	new_tx_desc_num = ALIGN(param->tx_pending, HNS3_RING_BD_MULTIPLE);
	new_rx_desc_num = ALIGN(param->rx_pending, HNS3_RING_BD_MULTIPLE);
964 965
	old_tx_desc_num = priv->ring[0].desc_num;
	old_rx_desc_num = priv->ring[queue_num].desc_num;
966 967
	if (old_tx_desc_num == new_tx_desc_num &&
	    old_rx_desc_num == new_rx_desc_num)
L
Lipeng 已提交
968 969
		return 0;

970 971 972 973 974 975 976
	tmp_rings = hns3_backup_ringparam(priv);
	if (!tmp_rings) {
		netdev_err(ndev,
			   "backup ring param failed by allocating memory fail\n");
		return -ENOMEM;
	}

L
Lipeng 已提交
977
	netdev_info(ndev,
978
		    "Changing Tx/Rx ring depth from %u/%u to %u/%u\n",
979 980
		    old_tx_desc_num, old_rx_desc_num,
		    new_tx_desc_num, new_rx_desc_num);
L
Lipeng 已提交
981 982

	if (if_running)
983
		ndev->netdev_ops->ndo_stop(ndev);
L
Lipeng 已提交
984

985 986
	hns3_change_all_ring_bd_num(priv, new_tx_desc_num, new_rx_desc_num);
	ret = hns3_init_all_ring(priv);
L
Lipeng 已提交
987
	if (ret) {
988 989 990 991 992 993
		netdev_err(ndev, "Change bd num fail, revert to old value(%d)\n",
			   ret);

		hns3_change_all_ring_bd_num(priv, old_tx_desc_num,
					    old_rx_desc_num);
		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
994
			memcpy(&priv->ring[i], &tmp_rings[i],
995 996 997 998
			       sizeof(struct hns3_enet_ring));
	} else {
		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
			hns3_fini_ring(&tmp_rings[i]);
L
Lipeng 已提交
999 1000
	}

1001 1002
	kfree(tmp_rings);

L
Lipeng 已提交
1003
	if (if_running)
1004
		ret = ndev->netdev_ops->ndo_open(ndev);
L
Lipeng 已提交
1005 1006 1007 1008

	return ret;
}

L
Lipeng 已提交
1009 1010 1011 1012 1013 1014
static int hns3_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

	switch (cmd->cmd) {
	case ETHTOOL_SRXFH:
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
		if (h->ae_algo->ops->set_rss_tuple)
			return h->ae_algo->ops->set_rss_tuple(h, cmd);
		return -EOPNOTSUPP;
	case ETHTOOL_SRXCLSRLINS:
		if (h->ae_algo->ops->add_fd_entry)
			return h->ae_algo->ops->add_fd_entry(h, cmd);
		return -EOPNOTSUPP;
	case ETHTOOL_SRXCLSRLDEL:
		if (h->ae_algo->ops->del_fd_entry)
			return h->ae_algo->ops->del_fd_entry(h, cmd);
		return -EOPNOTSUPP;
L
Lipeng 已提交
1026 1027 1028 1029 1030
	default:
		return -EOPNOTSUPP;
	}
}

1031 1032
static int hns3_nway_reset(struct net_device *netdev)
{
1033 1034
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1035
	struct phy_device *phy = netdev->phydev;
1036
	int autoneg;
1037 1038 1039 1040

	if (!netif_running(netdev))
		return 0;

1041 1042 1043 1044 1045 1046
	if (hns3_nic_resetting(netdev)) {
		netdev_err(netdev, "dev resetting!");
		return -EBUSY;
	}

	if (!ops->get_autoneg || !ops->restart_autoneg)
1047 1048
		return -EOPNOTSUPP;

1049 1050 1051 1052
	autoneg = ops->get_autoneg(handle);
	if (autoneg != AUTONEG_ENABLE) {
		netdev_err(netdev,
			   "Autoneg is off, don't support to restart it\n");
1053
		return -EINVAL;
1054 1055
	}

1056 1057 1058
	netif_dbg(handle, drv, netdev,
		  "nway reset (using %s)\n", phy ? "phy" : "mac");

1059 1060 1061 1062 1063
	if (phy)
		return genphy_restart_aneg(phy);

	if (handle->pdev->revision == 0x20)
		return -EOPNOTSUPP;
1064

1065
	return ops->restart_autoneg(handle);
1066 1067
}

1068 1069
static void hns3_get_channels(struct net_device *netdev,
			      struct ethtool_channels *ch)
1070 1071 1072 1073 1074 1075 1076
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

	if (h->ae_algo->ops->get_channels)
		h->ae_algo->ops->get_channels(h, ch);
}

1077 1078 1079 1080 1081 1082 1083 1084
static int hns3_get_coalesce_per_queue(struct net_device *netdev, u32 queue,
				       struct ethtool_coalesce *cmd)
{
	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
	struct hns3_nic_priv *priv = netdev_priv(netdev);
	struct hnae3_handle *h = priv->ae_handle;
	u16 queue_num = h->kinfo.num_tqps;

1085 1086 1087
	if (hns3_nic_resetting(netdev))
		return -EBUSY;

1088 1089
	if (queue >= queue_num) {
		netdev_err(netdev,
1090
			   "Invalid queue value %u! Queue max id=%u\n",
1091 1092 1093 1094
			   queue, queue_num - 1);
		return -EINVAL;
	}

1095 1096
	tx_vector = priv->ring[queue].tqp_vector;
	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1097

1098 1099 1100 1101
	cmd->use_adaptive_tx_coalesce =
			tx_vector->tx_group.coal.gl_adapt_enable;
	cmd->use_adaptive_rx_coalesce =
			rx_vector->rx_group.coal.gl_adapt_enable;
1102

1103 1104
	cmd->tx_coalesce_usecs = tx_vector->tx_group.coal.int_gl;
	cmd->rx_coalesce_usecs = rx_vector->rx_group.coal.int_gl;
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

	cmd->tx_coalesce_usecs_high = h->kinfo.int_rl_setting;
	cmd->rx_coalesce_usecs_high = h->kinfo.int_rl_setting;

	return 0;
}

static int hns3_get_coalesce(struct net_device *netdev,
			     struct ethtool_coalesce *cmd)
{
	return hns3_get_coalesce_per_queue(netdev, 0, cmd);
}

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
static int hns3_check_gl_coalesce_para(struct net_device *netdev,
				       struct ethtool_coalesce *cmd)
{
	u32 rx_gl, tx_gl;

	if (cmd->rx_coalesce_usecs > HNS3_INT_GL_MAX) {
		netdev_err(netdev,
			   "Invalid rx-usecs value, rx-usecs range is 0-%d\n",
			   HNS3_INT_GL_MAX);
		return -EINVAL;
	}

	if (cmd->tx_coalesce_usecs > HNS3_INT_GL_MAX) {
		netdev_err(netdev,
			   "Invalid tx-usecs value, tx-usecs range is 0-%d\n",
			   HNS3_INT_GL_MAX);
		return -EINVAL;
	}

	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
	if (rx_gl != cmd->rx_coalesce_usecs) {
		netdev_info(netdev,
1140
			    "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1141 1142 1143 1144 1145 1146
			    cmd->rx_coalesce_usecs, rx_gl);
	}

	tx_gl = hns3_gl_round_down(cmd->tx_coalesce_usecs);
	if (tx_gl != cmd->tx_coalesce_usecs) {
		netdev_info(netdev,
1147
			    "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
			    cmd->tx_coalesce_usecs, tx_gl);
	}

	return 0;
}

static int hns3_check_rl_coalesce_para(struct net_device *netdev,
				       struct ethtool_coalesce *cmd)
{
	u32 rl;

	if (cmd->tx_coalesce_usecs_high != cmd->rx_coalesce_usecs_high) {
		netdev_err(netdev,
			   "tx_usecs_high must be same as rx_usecs_high.\n");
		return -EINVAL;
	}

	if (cmd->rx_coalesce_usecs_high > HNS3_INT_RL_MAX) {
		netdev_err(netdev,
			   "Invalid usecs_high value, usecs_high range is 0-%d\n",
			   HNS3_INT_RL_MAX);
		return -EINVAL;
	}

	rl = hns3_rl_round_down(cmd->rx_coalesce_usecs_high);
	if (rl != cmd->rx_coalesce_usecs_high) {
		netdev_info(netdev,
1175
			    "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
			    cmd->rx_coalesce_usecs_high, rl);
	}

	return 0;
}

static int hns3_check_coalesce_para(struct net_device *netdev,
				    struct ethtool_coalesce *cmd)
{
	int ret;

	ret = hns3_check_gl_coalesce_para(netdev, cmd);
	if (ret) {
		netdev_err(netdev,
			   "Check gl coalesce param fail. ret = %d\n", ret);
		return ret;
	}

	ret = hns3_check_rl_coalesce_para(netdev, cmd);
	if (ret) {
		netdev_err(netdev,
			   "Check rl coalesce param fail. ret = %d\n", ret);
		return ret;
	}

	if (cmd->use_adaptive_tx_coalesce == 1 ||
	    cmd->use_adaptive_rx_coalesce == 1) {
		netdev_info(netdev,
1204
			    "adaptive-tx=%u and adaptive-rx=%u, tx_usecs or rx_usecs will changed dynamically.\n",
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
			    cmd->use_adaptive_tx_coalesce,
			    cmd->use_adaptive_rx_coalesce);
	}

	return 0;
}

static void hns3_set_coalesce_per_queue(struct net_device *netdev,
					struct ethtool_coalesce *cmd,
					u32 queue)
{
	struct hns3_enet_tqp_vector *tx_vector, *rx_vector;
	struct hns3_nic_priv *priv = netdev_priv(netdev);
	struct hnae3_handle *h = priv->ae_handle;
	int queue_num = h->kinfo.num_tqps;

1221 1222
	tx_vector = priv->ring[queue].tqp_vector;
	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1223

1224 1225 1226 1227
	tx_vector->tx_group.coal.gl_adapt_enable =
				cmd->use_adaptive_tx_coalesce;
	rx_vector->rx_group.coal.gl_adapt_enable =
				cmd->use_adaptive_rx_coalesce;
1228

1229 1230
	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1231

1232 1233 1234 1235
	hns3_set_vector_coalesce_tx_gl(tx_vector,
				       tx_vector->tx_group.coal.int_gl);
	hns3_set_vector_coalesce_rx_gl(rx_vector,
				       rx_vector->rx_group.coal.int_gl);
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
}

static int hns3_set_coalesce(struct net_device *netdev,
			     struct ethtool_coalesce *cmd)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);
	u16 queue_num = h->kinfo.num_tqps;
	int ret;
	int i;

1249 1250 1251
	if (hns3_nic_resetting(netdev))
		return -EBUSY;

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	ret = hns3_check_coalesce_para(netdev, cmd);
	if (ret)
		return ret;

	h->kinfo.int_rl_setting =
		hns3_rl_round_down(cmd->rx_coalesce_usecs_high);

	for (i = 0; i < queue_num; i++)
		hns3_set_coalesce_per_queue(netdev, cmd, i);

	return 0;
}

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
static int hns3_get_regs_len(struct net_device *netdev)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

	if (!h->ae_algo->ops->get_regs_len)
		return -EOPNOTSUPP;

	return h->ae_algo->ops->get_regs_len(h);
}

static void hns3_get_regs(struct net_device *netdev,
			  struct ethtool_regs *cmd, void *data)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

	if (!h->ae_algo->ops->get_regs)
		return;

	h->ae_algo->ops->get_regs(h, &cmd->version, data);
}

1286 1287 1288 1289 1290
static int hns3_set_phys_id(struct net_device *netdev,
			    enum ethtool_phys_id_state state)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

1291
	if (!h->ae_algo->ops->set_led_id)
1292 1293 1294 1295 1296
		return -EOPNOTSUPP;

	return h->ae_algo->ops->set_led_id(h, state);
}

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
static u32 hns3_get_msglevel(struct net_device *netdev)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

	return h->msg_enable;
}

static void hns3_set_msglevel(struct net_device *netdev, u32 msg_level)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

	h->msg_enable = msg_level;
}

1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
/* Translate local fec value into ethtool value. */
static unsigned int loc_to_eth_fec(u8 loc_fec)
{
	u32 eth_fec = 0;

	if (loc_fec & BIT(HNAE3_FEC_AUTO))
		eth_fec |= ETHTOOL_FEC_AUTO;
	if (loc_fec & BIT(HNAE3_FEC_RS))
		eth_fec |= ETHTOOL_FEC_RS;
	if (loc_fec & BIT(HNAE3_FEC_BASER))
		eth_fec |= ETHTOOL_FEC_BASER;

	/* if nothing is set, then FEC is off */
	if (!eth_fec)
		eth_fec = ETHTOOL_FEC_OFF;

	return eth_fec;
}

/* Translate ethtool fec value into local value. */
static unsigned int eth_to_loc_fec(unsigned int eth_fec)
{
	u32 loc_fec = 0;

	if (eth_fec & ETHTOOL_FEC_OFF)
		return loc_fec;

	if (eth_fec & ETHTOOL_FEC_AUTO)
		loc_fec |= BIT(HNAE3_FEC_AUTO);
	if (eth_fec & ETHTOOL_FEC_RS)
		loc_fec |= BIT(HNAE3_FEC_RS);
	if (eth_fec & ETHTOOL_FEC_BASER)
		loc_fec |= BIT(HNAE3_FEC_BASER);

	return loc_fec;
}

static int hns3_get_fecparam(struct net_device *netdev,
			     struct ethtool_fecparam *fec)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
	u8 fec_ability;
	u8 fec_mode;

	if (handle->pdev->revision == 0x20)
		return -EOPNOTSUPP;

	if (!ops->get_fec)
		return -EOPNOTSUPP;

	ops->get_fec(handle, &fec_ability, &fec_mode);

	fec->fec = loc_to_eth_fec(fec_ability);
	fec->active_fec = loc_to_eth_fec(fec_mode);

	return 0;
}

static int hns3_set_fecparam(struct net_device *netdev,
			     struct ethtool_fecparam *fec)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
	u32 fec_mode;

	if (handle->pdev->revision == 0x20)
		return -EOPNOTSUPP;

	if (!ops->set_fec)
		return -EOPNOTSUPP;
	fec_mode = eth_to_loc_fec(fec->fec);
1383 1384 1385

	netif_dbg(handle, drv, netdev, "set fecparam: mode=%u\n", fec_mode);

1386 1387 1388
	return ops->set_fec(handle, fec_mode);
}

1389 1390 1391 1392 1393
#define HNS3_ETHTOOL_COALESCE	(ETHTOOL_COALESCE_USECS |		\
				 ETHTOOL_COALESCE_USE_ADAPTIVE |	\
				 ETHTOOL_COALESCE_RX_USECS_HIGH |	\
				 ETHTOOL_COALESCE_TX_USECS_HIGH)

1394
static const struct ethtool_ops hns3vf_ethtool_ops = {
1395
	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1396 1397 1398 1399 1400 1401 1402
	.get_drvinfo = hns3_get_drvinfo,
	.get_ringparam = hns3_get_ringparam,
	.set_ringparam = hns3_set_ringparam,
	.get_strings = hns3_get_strings,
	.get_ethtool_stats = hns3_get_stats,
	.get_sset_count = hns3_get_sset_count,
	.get_rxnfc = hns3_get_rxnfc,
1403
	.set_rxnfc = hns3_set_rxnfc,
1404 1405 1406 1407 1408
	.get_rxfh_key_size = hns3_get_rss_key_size,
	.get_rxfh_indir_size = hns3_get_rss_indir_size,
	.get_rxfh = hns3_get_rss,
	.set_rxfh = hns3_set_rss,
	.get_link_ksettings = hns3_get_link_ksettings,
1409
	.get_channels = hns3_get_channels,
1410
	.set_channels = hns3_set_channels,
1411 1412
	.get_coalesce = hns3_get_coalesce,
	.set_coalesce = hns3_set_coalesce,
1413 1414
	.get_regs_len = hns3_get_regs_len,
	.get_regs = hns3_get_regs,
1415
	.get_link = hns3_get_link,
1416 1417
	.get_msglevel = hns3_get_msglevel,
	.set_msglevel = hns3_set_msglevel,
1418 1419
};

1420
static const struct ethtool_ops hns3_ethtool_ops = {
1421
	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
1422
	.self_test = hns3_self_test,
1423 1424 1425
	.get_drvinfo = hns3_get_drvinfo,
	.get_link = hns3_get_link,
	.get_ringparam = hns3_get_ringparam,
L
Lipeng 已提交
1426
	.set_ringparam = hns3_set_ringparam,
1427
	.get_pauseparam = hns3_get_pauseparam,
1428
	.set_pauseparam = hns3_set_pauseparam,
1429 1430 1431 1432
	.get_strings = hns3_get_strings,
	.get_ethtool_stats = hns3_get_stats,
	.get_sset_count = hns3_get_sset_count,
	.get_rxnfc = hns3_get_rxnfc,
L
Lipeng 已提交
1433
	.set_rxnfc = hns3_set_rxnfc,
1434 1435 1436 1437 1438
	.get_rxfh_key_size = hns3_get_rss_key_size,
	.get_rxfh_indir_size = hns3_get_rss_indir_size,
	.get_rxfh = hns3_get_rss,
	.set_rxfh = hns3_set_rss,
	.get_link_ksettings = hns3_get_link_ksettings,
1439
	.set_link_ksettings = hns3_set_link_ksettings,
1440
	.nway_reset = hns3_nway_reset,
1441
	.get_channels = hns3_get_channels,
1442
	.set_channels = hns3_set_channels,
1443
	.get_coalesce = hns3_get_coalesce,
1444
	.set_coalesce = hns3_set_coalesce,
1445 1446
	.get_regs_len = hns3_get_regs_len,
	.get_regs = hns3_get_regs,
1447
	.set_phys_id = hns3_set_phys_id,
1448 1449
	.get_msglevel = hns3_get_msglevel,
	.set_msglevel = hns3_set_msglevel,
1450 1451
	.get_fecparam = hns3_get_fecparam,
	.set_fecparam = hns3_set_fecparam,
1452 1453 1454 1455
};

void hns3_ethtool_set_ops(struct net_device *netdev)
{
1456 1457 1458 1459 1460 1461
	struct hnae3_handle *h = hns3_get_handle(netdev);

	if (h->flags & HNAE3_SUPPORT_VF)
		netdev->ethtool_ops = &hns3vf_ethtool_ops;
	else
		netdev->ethtool_ops = &hns3_ethtool_ops;
1462
}