hns3_ethtool.c 58.2 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
#include <linux/sfp.h>
8 9

#include "hns3_enet.h"
10
#include "hns3_ethtool.h"
11

12 13 14
/* tqp related stats */
#define HNS3_TQP_STAT(_string, _member)	{			\
	.stats_string = _string,				\
15 16
	.stats_offset = offsetof(struct hns3_enet_ring, stats) +\
			offsetof(struct ring_stats, _member),   \
17
}
18 19 20

static const struct hns3_stats hns3_txq_stats[] = {
	/* Tx per-queue statistics */
21
	HNS3_TQP_STAT("dropped", sw_err_cnt),
22 23 24
	HNS3_TQP_STAT("seg_pkt_cnt", seg_pkt_cnt),
	HNS3_TQP_STAT("packets", tx_pkts),
	HNS3_TQP_STAT("bytes", tx_bytes),
25
	HNS3_TQP_STAT("more", tx_more),
26 27
	HNS3_TQP_STAT("push", tx_push),
	HNS3_TQP_STAT("mem_doorbell", tx_mem_doorbell),
28 29
	HNS3_TQP_STAT("wake", restart_queue),
	HNS3_TQP_STAT("busy", tx_busy),
30
	HNS3_TQP_STAT("copy", tx_copy),
31 32 33 34
	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),
35 36
	HNS3_TQP_STAT("over_max_recursion", over_max_recursion),
	HNS3_TQP_STAT("hw_limitation", hw_limitation),
37 38 39
	HNS3_TQP_STAT("bounce", tx_bounce),
	HNS3_TQP_STAT("spare_full", tx_spare_full),
	HNS3_TQP_STAT("copy_bits_err", copy_bits_err),
40 41 42
	HNS3_TQP_STAT("sgl", tx_sgl),
	HNS3_TQP_STAT("skb2sgl_err", skb2sgl_err),
	HNS3_TQP_STAT("map_sg_err", map_sg_err),
43 44 45 46 47 48
};

#define HNS3_TXQ_STATS_COUNT ARRAY_SIZE(hns3_txq_stats)

static const struct hns3_stats hns3_rxq_stats[] = {
	/* Rx per-queue statistics */
49
	HNS3_TQP_STAT("dropped", sw_err_cnt),
50 51 52 53 54 55 56 57 58
	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),
59
	HNS3_TQP_STAT("csum_complete", csum_complete),
60
	HNS3_TQP_STAT("multicast", rx_multicast),
61
	HNS3_TQP_STAT("non_reuse_pg", non_reuse_pg),
62 63
	HNS3_TQP_STAT("frag_alloc_err", frag_alloc_err),
	HNS3_TQP_STAT("frag_alloc", frag_alloc),
64 65
};

66 67
#define HNS3_PRIV_FLAGS_LEN ARRAY_SIZE(hns3_priv_flags)

68 69 70 71
#define HNS3_RXQ_STATS_COUNT ARRAY_SIZE(hns3_rxq_stats)

#define HNS3_TQP_STATS_COUNT (HNS3_TXQ_STATS_COUNT + HNS3_RXQ_STATS_COUNT)

72 73 74
#define HNS3_NIC_LB_TEST_PKT_NUM	1
#define HNS3_NIC_LB_TEST_RING_ID	0
#define HNS3_NIC_LB_TEST_PACKET_SIZE	128
75
#define HNS3_NIC_LB_SETUP_USEC		10000
76 77 78 79 80 81

/* 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

82
static int hns3_lp_setup(struct net_device *ndev, enum hnae3_loop loop, bool en)
83 84
{
	struct hnae3_handle *h = hns3_get_handle(ndev);
85
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
86 87 88 89 90 91 92
	int ret;

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

	switch (loop) {
93 94
	case HNAE3_LOOP_SERIAL_SERDES:
	case HNAE3_LOOP_PARALLEL_SERDES:
95
	case HNAE3_LOOP_APP:
Y
Yufeng Mo 已提交
96
	case HNAE3_LOOP_PHY:
97
	case HNAE3_LOOP_EXTERNAL:
98
		ret = h->ae_algo->ops->set_loopback(h, loop, en);
99 100 101 102 103 104
		break;
	default:
		ret = -ENOTSUPP;
		break;
	}

105
	if (ret || ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
106 107
		return ret;

108
	if (en)
109
		h->ae_algo->ops->set_promisc_mode(h, true, true);
110
	else
111
		/* recover promisc mode before loopback test */
112
		hns3_request_update_promisc_mode(h);
113 114 115 116 117 118 119 120 121

	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;

122 123 124 125
	ret = hns3_nic_reset_all_ring(h);
	if (ret)
		return ret;

126
	ret = hns3_lp_setup(ndev, loop_mode, true);
127
	usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
128

129
	return ret;
130 131
}

132
static int hns3_lp_down(struct net_device *ndev, enum hnae3_loop loop_mode)
133 134 135
{
	int ret;

136
	ret = hns3_lp_setup(ndev, loop_mode, false);
137 138 139 140 141
	if (ret) {
		netdev_err(ndev, "lb_setup return error: %d\n", ret);
		return ret;
	}

142
	usleep_range(HNS3_NIC_LB_SETUP_USEC, HNS3_NIC_LB_SETUP_USEC * 2);
143 144 145 146 147 148

	return 0;
}

static void hns3_lp_setup_skb(struct sk_buff *skb)
{
149 150
#define	HNS3_NIC_LB_DST_MAC_ADDR	0x1f

151
	struct net_device *ndev = skb->dev;
152
	struct hnae3_handle *handle;
153
	struct hnae3_ae_dev *ae_dev;
154 155 156 157 158 159 160 161 162
	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);
163 164 165 166 167 168

	/* 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.
	 */
169
	handle = hns3_get_handle(ndev);
170 171
	ae_dev = pci_get_drvdata(handle->pdev);
	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
172
		ethh->h_dest[5] += HNS3_NIC_LB_DST_MAC_ADDR;
173 174 175 176 177 178 179 180 181 182 183 184 185
	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;
186
	u32 len = skb_headlen(skb);
187 188
	u32 i;

189 190 191
	len = min_t(u32, len, HNS3_NIC_LB_TEST_PACKET_SIZE);

	for (i = 0; i < len; i++)
192 193 194 195
		if (packet[i] != (unsigned char)(i & 0xff))
			break;

	/* The packet is correctly received */
196
	if (i == HNS3_NIC_LB_TEST_PACKET_SIZE)
197 198 199
		tqp_vector->rx_group.total_packets++;
	else
		print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET, 16, 1,
200
			       skb->data, len, true);
201 202 203 204 205 206 207 208 209 210 211 212

	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++) {
213
		struct hns3_enet_ring *ring = &priv->ring[i];
214 215 216 217 218 219
		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;

220
		preempt_disable();
221
		hns3_clean_rx_ring(ring, budget, hns3_lb_check_skb_data);
222
		preempt_enable();
223 224 225 226 227 228 229 230

		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,
231
				  u32 end_ringid)
232 233 234 235
{
	u32 i;

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

238
		hns3_clean_tx_ring(ring, 0);
239 240 241 242
	}
}

/**
243
 * hns3_lp_run_test - run loopback test
244 245
 * @ndev: net device
 * @mode: loopback type
246 247
 *
 * Return: %0 for success or a NIC loopback test error code on failure
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
 */
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);
271
		if (tx_ret == NETDEV_TX_OK) {
272
			good_cnt++;
273 274
		} else {
			kfree_skb(skb);
275 276
			netdev_err(ndev, "hns3_lb_run_test xmit failed: %d\n",
				   tx_ret);
277
		}
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	}
	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,
298
			      HNS3_NIC_LB_TEST_RING_ID);
299 300 301 302 303

	kfree_skb(skb);
	return ret_val;
}

304
static void hns3_set_selftest_param(struct hnae3_handle *h, int (*st_param)[2])
305
{
306 307 308 309
	st_param[HNAE3_LOOP_EXTERNAL][0] = HNAE3_LOOP_EXTERNAL;
	st_param[HNAE3_LOOP_EXTERNAL][1] =
			h->flags & HNAE3_SUPPORT_EXTERNAL_LOOPBACK;

310 311 312
	st_param[HNAE3_LOOP_APP][0] = HNAE3_LOOP_APP;
	st_param[HNAE3_LOOP_APP][1] =
			h->flags & HNAE3_SUPPORT_APP_LOOPBACK;
313

314 315 316 317 318 319 320 321
	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;
322

Y
Yufeng Mo 已提交
323 324 325
	st_param[HNAE3_LOOP_PHY][0] = HNAE3_LOOP_PHY;
	st_param[HNAE3_LOOP_PHY][1] =
			h->flags & HNAE3_SUPPORT_PHY_LOOPBACK;
326 327
}

328
static void hns3_selftest_prepare(struct net_device *ndev, bool if_running)
329 330 331 332
{
	struct hns3_nic_priv *priv = netdev_priv(ndev);
	struct hnae3_handle *h = priv->ae_handle;

333
	if (if_running)
334
		ndev->netdev_ops->ndo_stop(ndev);
335

336 337
#if IS_ENABLED(CONFIG_VLAN_8021Q)
	/* Disable the vlan filter for selftest does not support it */
338 339
	if (h->ae_algo->ops->enable_vlan_filter &&
	    ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
340 341 342
		h->ae_algo->ops->enable_vlan_filter(h, false);
#endif

343 344 345 346 347 348 349
	/* 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);

350
	set_bit(HNS3_NIC_STATE_TESTING, &priv->state);
351 352 353 354 355 356 357 358 359 360 361 362 363
}

static void hns3_selftest_restore(struct net_device *ndev, bool if_running)
{
	struct hns3_nic_priv *priv = netdev_priv(ndev);
	struct hnae3_handle *h = priv->ae_handle;

	clear_bit(HNS3_NIC_STATE_TESTING, &priv->state);

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

#if IS_ENABLED(CONFIG_VLAN_8021Q)
364 365
	if (h->ae_algo->ops->enable_vlan_filter &&
	    ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
366 367 368 369 370 371 372 373 374 375
		h->ae_algo->ops->enable_vlan_filter(h, true);
#endif

	if (if_running)
		ndev->netdev_ops->ndo_open(ndev);
}

static void hns3_do_selftest(struct net_device *ndev, int (*st_param)[2],
			     struct ethtool_test *eth_test, u64 *data)
{
376
	int test_index = HNAE3_LOOP_APP;
377
	u32 i;
378

379
	for (i = HNAE3_LOOP_APP; i < HNAE3_LOOP_NONE; i++) {
380 381 382 383 384 385
		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);
386
		if (!data[test_index])
387
			data[test_index] = hns3_lp_run_test(ndev, loop_type);
388 389

		hns3_lp_down(ndev, loop_type);
390 391 392 393 394 395

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

		test_index++;
	}
396
}
397

398 399 400 401 402 403 404 405 406 407 408 409 410 411
static void hns3_do_external_lb(struct net_device *ndev,
				struct ethtool_test *eth_test, u64 *data)
{
	data[HNAE3_LOOP_EXTERNAL] = hns3_lp_up(ndev, HNAE3_LOOP_EXTERNAL);
	if (!data[HNAE3_LOOP_EXTERNAL])
		data[HNAE3_LOOP_EXTERNAL] = hns3_lp_run_test(ndev, HNAE3_LOOP_EXTERNAL);
	hns3_lp_down(ndev, HNAE3_LOOP_EXTERNAL);

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

	eth_test->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
}

412
/**
H
Huazhong Tan 已提交
413
 * hns3_self_test - self test
414 415 416 417 418 419 420
 * @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)
{
421 422 423
	struct hns3_nic_priv *priv = netdev_priv(ndev);
	struct hnae3_handle *h = priv->ae_handle;
	int st_param[HNAE3_LOOP_NONE][2];
424
	bool if_running = netif_running(ndev);
425

426 427 428 429
	if (hns3_nic_resetting(ndev)) {
		netdev_err(ndev, "dev resetting!");
		return;
	}
430

431
	if (!(eth_test->flags & ETH_TEST_FL_OFFLINE))
432
		return;
433

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	if (netif_msg_ifdown(h))
		netdev_info(ndev, "self test start\n");

	hns3_set_selftest_param(h, st_param);

	/* external loopback test requires that the link is up and the duplex is
	 * full, do external test first to reduce the whole test time
	 */
	if (eth_test->flags & ETH_TEST_FL_EXTERNAL_LB) {
		hns3_external_lb_prepare(ndev, if_running);
		hns3_do_external_lb(ndev, eth_test, data);
		hns3_external_lb_restore(ndev, if_running);
	}

	hns3_selftest_prepare(ndev, if_running);
449 450
	hns3_do_selftest(ndev, st_param, eth_test, data);
	hns3_selftest_restore(ndev, if_running);
451 452 453

	if (netif_msg_ifdown(h))
		netdev_info(ndev, "self test end\n");
454 455
}

456 457 458 459 460 461 462 463 464 465 466 467 468
static void hns3_update_limit_promisc_mode(struct net_device *netdev,
					   bool enable)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);

	if (enable)
		set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
	else
		clear_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);

	hns3_request_update_promisc_mode(handle);
}

469 470 471 472 473 474 475 476 477 478
static void hns3_update_fd_qb_state(struct net_device *netdev, bool enable)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);

	if (!handle->ae_algo->ops->request_flush_qb_config)
		return;

	handle->ae_algo->ops->request_flush_qb_config(handle);
}

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
static void hns3_update_state(struct net_device *netdev,
			      enum hns3_nic_state state, bool enable)
{
	struct hns3_nic_priv *priv = netdev_priv(netdev);

	if (enable)
		set_bit(state, &priv->state);
	else
		clear_bit(state, &priv->state);
}

static void hns3_update_push_state(struct net_device *netdev, bool enable)
{
	hns3_update_state(netdev, HNS3_NIC_STATE_TX_PUSH_ENABLE, enable);
}

495
static const struct hns3_pflag_desc hns3_priv_flags[HNAE3_PFLAG_MAX] = {
496
	{ "limit_promisc",	hns3_update_limit_promisc_mode },
497 498
	{ "tx_push_enable",	hns3_update_push_state },
	{ "qb_enable",		hns3_update_fd_qb_state },
499 500
};

501 502
static int hns3_get_sset_count(struct net_device *netdev, int stringset)
{
503
	struct hnae3_handle *h = hns3_get_handle(netdev);
504 505 506 507 508 509 510 511 512 513 514 515 516
	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);

517 518 519
	case ETH_SS_PRIV_FLAGS:
		return HNAE3_PFLAG_MAX;

520 521 522
	default:
		return -EOPNOTSUPP;
	}
523 524 525
}

static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats,
526
		u32 stat_count, u32 num_tqps, const char *prefix)
527
{
528
#define MAX_PREFIX_SIZE (6 + 4)
529 530 531 532 533 534 535 536 537
	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 */
538
			n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_",
539
				       prefix, i);
540 541 542 543 544 545 546 547 548 549 550 551 552 553
			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;
554 555
	const char tx_prefix[] = "txq";
	const char rx_prefix[] = "rxq";
556 557 558

	/* get strings for Tx */
	data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT,
559
				   kinfo->num_tqps, tx_prefix);
560 561 562

	/* get strings for Rx */
	data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT,
563
				   kinfo->num_tqps, rx_prefix);
564 565 566 567 568 569

	return data;
}

static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
{
570
	struct hnae3_handle *h = hns3_get_handle(netdev);
571 572
	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
	char *buff = (char *)data;
573
	int i;
574 575 576 577 578 579 580

	if (!ops->get_strings)
		return;

	switch (stringset) {
	case ETH_SS_STATS:
		buff = hns3_get_strings_tqps(h, buff);
581
		ops->get_strings(h, stringset, (u8 *)buff);
582 583 584 585
		break;
	case ETH_SS_TEST:
		ops->get_strings(h, stringset, data);
		break;
586 587 588 589 590 591 592
	case ETH_SS_PRIV_FLAGS:
		for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) {
			snprintf(buff, ETH_GSTRING_LEN, "%s",
				 hns3_priv_flags[i].name);
			buff += ETH_GSTRING_LEN;
		}
		break;
593 594
	default:
		break;
595 596 597 598 599 600 601 602 603
	}
}

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;
604
	int i, j;
605 606 607

	/* get stats for Tx */
	for (i = 0; i < kinfo->num_tqps; i++) {
608
		ring = &nic_priv->ring[i];
609 610
		for (j = 0; j < HNS3_TXQ_STATS_COUNT; j++) {
			stat = (u8 *)ring + hns3_txq_stats[j].stats_offset;
611 612 613 614 615 616
			*data++ = *(u64 *)stat;
		}
	}

	/* get stats for Rx */
	for (i = 0; i < kinfo->num_tqps; i++) {
617
		ring = &nic_priv->ring[i + kinfo->num_tqps];
618 619
		for (j = 0; j < HNS3_RXQ_STATS_COUNT; j++) {
			stat = (u8 *)ring + hns3_rxq_stats[j].stats_offset;
620 621 622 623 624 625 626 627 628 629 630 631
			*data++ = *(u64 *)stat;
		}
	}

	return data;
}

/* hns3_get_stats - get detail statistics.
 * @netdev: net device
 * @stats: statistics info.
 * @data: statistics data.
 */
632 633
static void hns3_get_stats(struct net_device *netdev,
			   struct ethtool_stats *stats, u64 *data)
634
{
635
	struct hnae3_handle *h = hns3_get_handle(netdev);
636 637
	u64 *p = data;

638 639 640 641 642
	if (hns3_nic_resetting(netdev)) {
		netdev_err(netdev, "dev resetting, could not get stats\n");
		return;
	}

643 644 645 646 647
	if (!h->ae_algo->ops->get_stats || !h->ae_algo->ops->update_stats) {
		netdev_err(netdev, "could not get any statistics\n");
		return;
	}

648
	h->ae_algo->ops->update_stats(h);
649 650 651 652 653 654 655 656 657 658 659 660 661

	/* 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;
662
	u32 fw_version;
663

664 665 666 667 668
	if (!h->ae_algo->ops->get_fw_version) {
		netdev_err(netdev, "could not get fw version!\n");
		return;
	}

669
	strncpy(drvinfo->driver, dev_driver_string(&h->pdev->dev),
670 671 672 673 674 675 676
		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';

677 678 679 680 681 682 683 684 685 686 687 688
	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));
689 690 691 692
}

static u32 hns3_get_link(struct net_device *netdev)
{
693
	struct hnae3_handle *h = hns3_get_handle(netdev);
694

695
	if (h->ae_algo->ops->get_status)
696 697 698 699 700 701
		return h->ae_algo->ops->get_status(h);
	else
		return 0;
}

static void hns3_get_ringparam(struct net_device *netdev,
702 703 704
			       struct ethtool_ringparam *param,
			       struct kernel_ethtool_ringparam *kernel_param,
			       struct netlink_ext_ack *extack)
705 706
{
	struct hns3_nic_priv *priv = netdev_priv(netdev);
707
	struct hnae3_handle *h = priv->ae_handle;
708
	int rx_queue_index = h->kinfo.num_tqps;
709

710 711
	if (hns3_nic_resetting(netdev) || !priv->ring) {
		netdev_err(netdev, "failed to get ringparam value, due to dev resetting or uninited\n");
712 713 714
		return;
	}

715 716 717
	param->tx_max_pending = HNS3_RING_MAX_PENDING;
	param->rx_max_pending = HNS3_RING_MAX_PENDING;

718
	param->tx_pending = priv->ring[0].desc_num;
719 720
	param->rx_pending = priv->ring[rx_queue_index].desc_num;
	kernel_param->rx_buf_len = priv->ring[rx_queue_index].buf_size;
721 722 723 724 725
}

static void hns3_get_pauseparam(struct net_device *netdev,
				struct ethtool_pauseparam *param)
{
726
	struct hnae3_handle *h = hns3_get_handle(netdev);
727 728 729 730
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);

	if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
		return;
731

732
	if (h->ae_algo->ops->get_pauseparam)
733 734 735 736
		h->ae_algo->ops->get_pauseparam(h, &param->autoneg,
			&param->rx_pause, &param->tx_pause);
}

737 738 739 740
static int hns3_set_pauseparam(struct net_device *netdev,
			       struct ethtool_pauseparam *param)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);
741 742 743 744
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);

	if (!test_bit(HNAE3_DEV_SUPPORT_PAUSE_B, ae_dev->caps))
		return -EOPNOTSUPP;
745

746 747 748 749
	netif_dbg(h, drv, netdev,
		  "set pauseparam: autoneg=%u, rx:%u, tx:%u\n",
		  param->autoneg, param->rx_pause, param->tx_pause);

750 751 752 753 754 755 756
	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;
}

757 758 759 760 761 762 763 764 765 766 767 768
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 已提交
769
	/* 2.get link mode */
770 771 772 773 774 775 776 777 778 779 780
	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);
}

781 782 783
static int hns3_get_link_ksettings(struct net_device *netdev,
				   struct ethtool_link_ksettings *cmd)
{
784
	struct hnae3_handle *h = hns3_get_handle(netdev);
785
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
786
	const struct hnae3_ae_ops *ops;
787
	u8 module_type;
788
	u8 media_type;
789 790
	u8 link_stat;

791
	ops = h->ae_algo->ops;
792
	if (ops->get_media_type)
793
		ops->get_media_type(h, &media_type, &module_type);
794 795 796
	else
		return -EOPNOTSUPP;

797 798 799 800 801 802
	switch (media_type) {
	case HNAE3_MEDIA_TYPE_NONE:
		cmd->base.port = PORT_NONE;
		hns3_get_ksettings(h, cmd);
		break;
	case HNAE3_MEDIA_TYPE_FIBER:
803 804 805 806 807 808 809 810 811
		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;
812
		hns3_get_ksettings(h, cmd);
813
		break;
814 815
	case HNAE3_MEDIA_TYPE_COPPER:
		cmd->base.port = PORT_TP;
816 817 818 819
		if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
		    ops->get_phy_link_ksettings)
			ops->get_phy_link_ksettings(h, cmd);
		else if (!netdev->phydev)
820 821 822
			hns3_get_ksettings(h, cmd);
		else
			phy_ethtool_ksettings_get(netdev->phydev, cmd);
823 824
		break;
	default:
825 826

		netdev_warn(netdev, "Unknown media type");
827 828 829
		return 0;
	}

830 831
	/* mdio_support */
	cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
832 833 834 835 836

	link_stat = hns3_get_link(netdev);
	if (!link_stat) {
		cmd->base.speed = SPEED_UNKNOWN;
		cmd->base.duplex = DUPLEX_UNKNOWN;
837 838 839 840 841
	}

	return 0;
}

842
static int hns3_check_ksettings_param(const struct net_device *netdev,
843 844 845 846 847 848 849 850 851 852 853
				      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;

854 855 856 857 858 859
	/* hw doesn't support use specified speed and duplex to negotiate,
	 * unnecessary to check them when autoneg on.
	 */
	if (cmd->base.autoneg)
		return 0;

860 861 862 863 864 865 866 867 868 869
	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);

870
	if (cmd->base.duplex == DUPLEX_HALF &&
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
	    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;
}

888 889 890
static int hns3_set_link_ksettings(struct net_device *netdev,
				   const struct ethtool_link_ksettings *cmd)
{
891
	struct hnae3_handle *handle = hns3_get_handle(netdev);
892
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
893
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
894
	int ret;
895 896

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

900 901 902 903 904
	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);

905
	/* Only support ksettings_set for netdev with phy attached for now */
906 907 908 909 910
	if (netdev->phydev) {
		if (cmd->base.speed == SPEED_1000 &&
		    cmd->base.autoneg == AUTONEG_DISABLE)
			return -EINVAL;

911
		return phy_ethtool_ksettings_set(netdev->phydev, cmd);
912 913 914
	} else if (test_bit(HNAE3_DEV_SUPPORT_PHY_IMP_B, ae_dev->caps) &&
		   ops->set_phy_link_ksettings) {
		return ops->set_phy_link_ksettings(handle, cmd);
915
	}
916

917
	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
918 919 920 921 922 923 924 925 926 927 928 929
		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;
	}

930 931 932 933 934 935 936 937 938
	/* 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;
	}

939 940 941 942 943
	if (ops->cfg_mac_speed_dup_h)
		ret = ops->cfg_mac_speed_dup_h(handle, cmd->base.speed,
					       cmd->base.duplex);

	return ret;
944 945
}

946 947
static u32 hns3_get_rss_key_size(struct net_device *netdev)
{
948
	struct hnae3_handle *h = hns3_get_handle(netdev);
949

950
	if (!h->ae_algo->ops->get_rss_key_size)
951
		return 0;
952 953 954 955 956 957

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

static u32 hns3_get_rss_indir_size(struct net_device *netdev)
{
958
	struct hnae3_handle *h = hns3_get_handle(netdev);
959
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
960

961
	return ae_dev->dev_specs.rss_ind_tbl_size;
962 963 964 965 966
}

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

969
	if (!h->ae_algo->ops->get_rss)
970 971 972 973 974 975 976 977
		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)
{
978
	struct hnae3_handle *h = hns3_get_handle(netdev);
979
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
980

981
	if (!h->ae_algo->ops->set_rss)
982 983
		return -EOPNOTSUPP;

984
	if ((ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 &&
985 986 987
	     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");
988 989
		return -EOPNOTSUPP;
	}
990

991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	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)
{
1004
	struct hnae3_handle *h = hns3_get_handle(netdev);
1005 1006 1007

	switch (cmd->cmd) {
	case ETHTOOL_GRXRINGS:
1008 1009
		cmd->data = h->kinfo.num_tqps;
		return 0;
L
Lipeng 已提交
1010
	case ETHTOOL_GRXFH:
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
		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;
1027 1028 1029 1030 1031
	default:
		return -EOPNOTSUPP;
	}
}

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
static const struct hns3_reset_type_map hns3_reset_type[] = {
	{ETH_RESET_MGMT, HNAE3_IMP_RESET},
	{ETH_RESET_ALL, HNAE3_GLOBAL_RESET},
	{ETH_RESET_DEDICATED, HNAE3_FUNC_RESET},
};

static const struct hns3_reset_type_map hns3vf_reset_type[] = {
	{ETH_RESET_DEDICATED, HNAE3_VF_FUNC_RESET},
};

static int hns3_set_reset(struct net_device *netdev, u32 *flags)
{
	enum hnae3_reset_type rst_type = HNAE3_NONE_RESET;
	struct hnae3_handle *h = hns3_get_handle(netdev);
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev);
	const struct hnae3_ae_ops *ops = h->ae_algo->ops;
	const struct hns3_reset_type_map *rst_type_map;
1049
	enum ethtool_reset_flags rst_flags;
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	u32 i, size;

	if (ops->ae_dev_resetting && ops->ae_dev_resetting(h))
		return -EBUSY;

	if (!ops->set_default_reset_request || !ops->reset_event)
		return -EOPNOTSUPP;

	if (h->flags & HNAE3_SUPPORT_VF) {
		rst_type_map = hns3vf_reset_type;
		size = ARRAY_SIZE(hns3vf_reset_type);
	} else {
		rst_type_map = hns3_reset_type;
		size = ARRAY_SIZE(hns3_reset_type);
	}

	for (i = 0; i < size; i++) {
		if (rst_type_map[i].rst_flags == *flags) {
			rst_type = rst_type_map[i].rst_type;
1069
			rst_flags = rst_type_map[i].rst_flags;
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
			break;
		}
	}

	if (rst_type == HNAE3_NONE_RESET ||
	    (rst_type == HNAE3_IMP_RESET &&
	     ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2))
		return -EOPNOTSUPP;

	netdev_info(netdev, "Setting reset type %d\n", rst_type);

	ops->set_default_reset_request(ae_dev, rst_type);

	ops->reset_event(h->pdev, h);

1085 1086
	*flags &= ~rst_flags;

1087 1088 1089
	return 0;
}

1090 1091
static void hns3_change_all_ring_bd_num(struct hns3_nic_priv *priv,
					u32 tx_desc_num, u32 rx_desc_num)
L
Lipeng 已提交
1092 1093 1094 1095
{
	struct hnae3_handle *h = priv->ae_handle;
	int i;

1096 1097
	h->kinfo.num_tx_desc = tx_desc_num;
	h->kinfo.num_rx_desc = rx_desc_num;
L
Lipeng 已提交
1098

1099
	for (i = 0; i < h->kinfo.num_tqps; i++) {
1100 1101
		priv->ring[i].desc_num = tx_desc_num;
		priv->ring[i + h->kinfo.num_tqps].desc_num = rx_desc_num;
1102
	}
L
Lipeng 已提交
1103 1104
}

1105
static struct hns3_enet_ring *hns3_backup_ringparam(struct hns3_nic_priv *priv)
L
Lipeng 已提交
1106
{
1107 1108 1109
	struct hnae3_handle *handle = priv->ae_handle;
	struct hns3_enet_ring *tmp_rings;
	int i;
L
Lipeng 已提交
1110

1111 1112 1113 1114 1115
	tmp_rings = kcalloc(handle->kinfo.num_tqps * 2,
			    sizeof(struct hns3_enet_ring), GFP_KERNEL);
	if (!tmp_rings)
		return NULL;

1116
	for (i = 0; i < handle->kinfo.num_tqps * 2; i++) {
1117
		memcpy(&tmp_rings[i], &priv->ring[i],
1118
		       sizeof(struct hns3_enet_ring));
1119 1120
		tmp_rings[i].skb = NULL;
	}
1121 1122 1123 1124 1125

	return tmp_rings;
}

static int hns3_check_ringparam(struct net_device *ndev,
1126 1127
				struct ethtool_ringparam *param,
				struct kernel_ethtool_ringparam *kernel_param)
1128
{
1129 1130
#define RX_BUF_LEN_2K 2048
#define RX_BUF_LEN_4K 4096
1131 1132 1133 1134 1135

	struct hns3_nic_priv *priv = netdev_priv(ndev);

	if (hns3_nic_resetting(ndev) || !priv->ring) {
		netdev_err(ndev, "failed to set ringparam value, due to dev resetting or uninited\n");
1136
		return -EBUSY;
1137 1138
	}

1139

L
Lipeng 已提交
1140 1141 1142
	if (param->rx_mini_pending || param->rx_jumbo_pending)
		return -EINVAL;

1143 1144 1145 1146 1147 1148
	if (kernel_param->rx_buf_len != RX_BUF_LEN_2K &&
	    kernel_param->rx_buf_len != RX_BUF_LEN_4K) {
		netdev_err(ndev, "Rx buf len only support 2048 and 4096\n");
		return -EINVAL;
	}

L
Lipeng 已提交
1149
	if (param->tx_pending > HNS3_RING_MAX_PENDING ||
1150 1151 1152 1153 1154
	    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 已提交
1155 1156 1157
		return -EINVAL;
	}

1158 1159 1160
	return 0;
}

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
static bool
hns3_is_ringparam_changed(struct net_device *ndev,
			  struct ethtool_ringparam *param,
			  struct kernel_ethtool_ringparam *kernel_param,
			  struct hns3_ring_param *old_ringparam,
			  struct hns3_ring_param *new_ringparam)
{
	struct hns3_nic_priv *priv = netdev_priv(ndev);
	struct hnae3_handle *h = priv->ae_handle;
	u16 queue_num = h->kinfo.num_tqps;

	new_ringparam->tx_desc_num = ALIGN(param->tx_pending,
					   HNS3_RING_BD_MULTIPLE);
	new_ringparam->rx_desc_num = ALIGN(param->rx_pending,
					   HNS3_RING_BD_MULTIPLE);
	old_ringparam->tx_desc_num = priv->ring[0].desc_num;
	old_ringparam->rx_desc_num = priv->ring[queue_num].desc_num;
	old_ringparam->rx_buf_len = priv->ring[queue_num].buf_size;
	new_ringparam->rx_buf_len = kernel_param->rx_buf_len;

	if (old_ringparam->tx_desc_num == new_ringparam->tx_desc_num &&
	    old_ringparam->rx_desc_num == new_ringparam->rx_desc_num &&
	    old_ringparam->rx_buf_len == new_ringparam->rx_buf_len) {
1184
		netdev_info(ndev, "descriptor number and rx buffer length not changed\n");
1185 1186 1187 1188 1189 1190
		return false;
	}

	return true;
}

1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
static int hns3_change_rx_buf_len(struct net_device *ndev, u32 rx_buf_len)
{
	struct hns3_nic_priv *priv = netdev_priv(ndev);
	struct hnae3_handle *h = priv->ae_handle;
	int i;

	h->kinfo.rx_buf_len = rx_buf_len;

	for (i = 0; i < h->kinfo.num_tqps; i++) {
		h->kinfo.tqp[i]->buf_size = rx_buf_len;
		priv->ring[i + h->kinfo.num_tqps].buf_size = rx_buf_len;
	}

	return 0;
}

1207
static int hns3_set_ringparam(struct net_device *ndev,
1208 1209 1210
			      struct ethtool_ringparam *param,
			      struct kernel_ethtool_ringparam *kernel_param,
			      struct netlink_ext_ack *extack)
1211
{
1212
	struct hns3_ring_param old_ringparam, new_ringparam;
1213 1214 1215 1216 1217 1218
	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);
	int ret, i;

1219
	ret = hns3_check_ringparam(ndev, param, kernel_param);
1220 1221 1222
	if (ret)
		return ret;

1223 1224
	if (!hns3_is_ringparam_changed(ndev, param, kernel_param,
				       &old_ringparam, &new_ringparam))
L
Lipeng 已提交
1225 1226
		return 0;

1227 1228
	tmp_rings = hns3_backup_ringparam(priv);
	if (!tmp_rings) {
1229
		netdev_err(ndev, "backup ring param failed by allocating memory fail\n");
1230 1231 1232
		return -ENOMEM;
	}

L
Lipeng 已提交
1233
	netdev_info(ndev,
1234 1235 1236 1237
		    "Changing Tx/Rx ring depth from %u/%u to %u/%u, Changing rx buffer len from %u to %u\n",
		    old_ringparam.tx_desc_num, old_ringparam.rx_desc_num,
		    new_ringparam.tx_desc_num, new_ringparam.rx_desc_num,
		    old_ringparam.rx_buf_len, new_ringparam.rx_buf_len);
L
Lipeng 已提交
1238 1239

	if (if_running)
1240
		ndev->netdev_ops->ndo_stop(ndev);
L
Lipeng 已提交
1241

1242 1243 1244
	hns3_change_all_ring_bd_num(priv, new_ringparam.tx_desc_num,
				    new_ringparam.rx_desc_num);
	hns3_change_rx_buf_len(ndev, new_ringparam.rx_buf_len);
1245
	ret = hns3_init_all_ring(priv);
L
Lipeng 已提交
1246
	if (ret) {
1247
		netdev_err(ndev, "set ringparam fail, revert to old value(%d)\n",
1248 1249
			   ret);

1250 1251 1252
		hns3_change_rx_buf_len(ndev, old_ringparam.rx_buf_len);
		hns3_change_all_ring_bd_num(priv, old_ringparam.tx_desc_num,
					    old_ringparam.rx_desc_num);
1253
		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
1254
			memcpy(&priv->ring[i], &tmp_rings[i],
1255 1256 1257 1258
			       sizeof(struct hns3_enet_ring));
	} else {
		for (i = 0; i < h->kinfo.num_tqps * 2; i++)
			hns3_fini_ring(&tmp_rings[i]);
L
Lipeng 已提交
1259 1260
	}

1261 1262
	kfree(tmp_rings);

L
Lipeng 已提交
1263
	if (if_running)
1264
		ret = ndev->netdev_ops->ndo_open(ndev);
L
Lipeng 已提交
1265 1266 1267 1268

	return ret;
}

L
Lipeng 已提交
1269 1270 1271 1272 1273 1274
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:
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
		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 已提交
1286 1287 1288 1289 1290
	default:
		return -EOPNOTSUPP;
	}
}

1291 1292
static int hns3_nway_reset(struct net_device *netdev)
{
1293 1294
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
1295
	struct phy_device *phy = netdev->phydev;
1296
	int autoneg;
1297 1298 1299 1300

	if (!netif_running(netdev))
		return 0;

1301 1302 1303 1304 1305 1306
	if (hns3_nic_resetting(netdev)) {
		netdev_err(netdev, "dev resetting!");
		return -EBUSY;
	}

	if (!ops->get_autoneg || !ops->restart_autoneg)
1307 1308
		return -EOPNOTSUPP;

1309 1310 1311 1312
	autoneg = ops->get_autoneg(handle);
	if (autoneg != AUTONEG_ENABLE) {
		netdev_err(netdev,
			   "Autoneg is off, don't support to restart it\n");
1313
		return -EINVAL;
1314 1315
	}

1316 1317 1318
	netif_dbg(handle, drv, netdev,
		  "nway reset (using %s)\n", phy ? "phy" : "mac");

1319 1320 1321 1322
	if (phy)
		return genphy_restart_aneg(phy);

	return ops->restart_autoneg(handle);
1323 1324
}

1325 1326
static void hns3_get_channels(struct net_device *netdev,
			      struct ethtool_channels *ch)
1327 1328 1329 1330 1331 1332 1333
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

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

1334
static int hns3_get_coalesce(struct net_device *netdev,
1335 1336 1337
			     struct ethtool_coalesce *cmd,
			     struct kernel_ethtool_coalesce *kernel_coal,
			     struct netlink_ext_ack *extack)
1338 1339
{
	struct hns3_nic_priv *priv = netdev_priv(netdev);
1340 1341
	struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
	struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1342 1343
	struct hnae3_handle *h = priv->ae_handle;

1344 1345 1346
	if (hns3_nic_resetting(netdev))
		return -EBUSY;

1347 1348
	cmd->use_adaptive_tx_coalesce = tx_coal->adapt_enable;
	cmd->use_adaptive_rx_coalesce = rx_coal->adapt_enable;
1349

1350 1351
	cmd->tx_coalesce_usecs = tx_coal->int_gl;
	cmd->rx_coalesce_usecs = rx_coal->int_gl;
1352 1353 1354 1355

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

1356 1357
	cmd->tx_max_coalesced_frames = tx_coal->int_ql;
	cmd->rx_max_coalesced_frames = rx_coal->int_ql;
1358

1359 1360 1361 1362 1363
	kernel_coal->use_cqe_mode_tx = (priv->tx_cqe_mode ==
					DIM_CQ_PERIOD_MODE_START_FROM_CQE);
	kernel_coal->use_cqe_mode_rx = (priv->rx_cqe_mode ==
					DIM_CQ_PERIOD_MODE_START_FROM_CQE);

1364 1365 1366
	return 0;
}

1367 1368 1369
static int hns3_check_gl_coalesce_para(struct net_device *netdev,
				       struct ethtool_coalesce *cmd)
{
1370 1371
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1372 1373
	u32 rx_gl, tx_gl;

1374
	if (cmd->rx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1375
		netdev_err(netdev,
1376 1377
			   "invalid rx-usecs value, rx-usecs range is 0-%u\n",
			   ae_dev->dev_specs.max_int_gl);
1378 1379 1380
		return -EINVAL;
	}

1381
	if (cmd->tx_coalesce_usecs > ae_dev->dev_specs.max_int_gl) {
1382
		netdev_err(netdev,
1383 1384
			   "invalid tx-usecs value, tx-usecs range is 0-%u\n",
			   ae_dev->dev_specs.max_int_gl);
1385 1386 1387
		return -EINVAL;
	}

1388 1389 1390 1391 1392 1393
	/* device version above V3(include V3), GL uses 1us unit,
	 * so the round down is not needed.
	 */
	if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3)
		return 0;

1394 1395 1396
	rx_gl = hns3_gl_round_down(cmd->rx_coalesce_usecs);
	if (rx_gl != cmd->rx_coalesce_usecs) {
		netdev_info(netdev,
1397
			    "rx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1398 1399 1400 1401 1402 1403
			    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,
1404
			    "tx_usecs(%u) rounded down to %u, because it must be multiple of 2.\n",
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
			    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,
1432
			    "usecs_high(%u) rounded down to %u, because it must be multiple of 4.\n",
1433 1434 1435 1436 1437 1438
			    cmd->rx_coalesce_usecs_high, rl);
	}

	return 0;
}

1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
static int hns3_check_ql_coalesce_param(struct net_device *netdev,
					struct ethtool_coalesce *cmd)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);

	if ((cmd->tx_max_coalesced_frames || cmd->rx_max_coalesced_frames) &&
	    !ae_dev->dev_specs.int_ql_max) {
		netdev_err(netdev, "coalesced frames is not supported\n");
		return -EOPNOTSUPP;
	}

	if (cmd->tx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max ||
	    cmd->rx_max_coalesced_frames > ae_dev->dev_specs.int_ql_max) {
		netdev_err(netdev,
			   "invalid coalesced_frames value, range is 0-%u\n",
			   ae_dev->dev_specs.int_ql_max);
		return -ERANGE;
	}

	return 0;
}

1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
static int
hns3_check_cqe_coalesce_param(struct net_device *netdev,
			      struct kernel_ethtool_coalesce *kernel_coal)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);

	if ((kernel_coal->use_cqe_mode_tx || kernel_coal->use_cqe_mode_rx) &&
	    !hnae3_ae_dev_cq_supported(ae_dev)) {
		netdev_err(netdev, "coalesced cqe mode is not supported\n");
		return -EOPNOTSUPP;
	}

	return 0;
}

static int
hns3_check_coalesce_para(struct net_device *netdev,
			 struct ethtool_coalesce *cmd,
			 struct kernel_ethtool_coalesce *kernel_coal)
1482 1483 1484
{
	int ret;

1485 1486 1487 1488
	ret = hns3_check_cqe_coalesce_param(netdev, kernel_coal);
	if (ret)
		return ret;

1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	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;
	}

1503
	return hns3_check_ql_coalesce_param(netdev, cmd);
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
}

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;

1515 1516
	tx_vector = priv->ring[queue].tqp_vector;
	rx_vector = priv->ring[queue_num + queue].tqp_vector;
1517

1518
	tx_vector->tx_group.coal.adapt_enable =
1519
				cmd->use_adaptive_tx_coalesce;
1520
	rx_vector->rx_group.coal.adapt_enable =
1521
				cmd->use_adaptive_rx_coalesce;
1522

1523 1524
	tx_vector->tx_group.coal.int_gl = cmd->tx_coalesce_usecs;
	rx_vector->rx_group.coal.int_gl = cmd->rx_coalesce_usecs;
1525

1526 1527 1528
	tx_vector->tx_group.coal.int_ql = cmd->tx_max_coalesced_frames;
	rx_vector->rx_group.coal.int_ql = cmd->rx_max_coalesced_frames;

1529 1530 1531 1532
	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);
1533 1534 1535

	hns3_set_vector_coalesce_rl(tx_vector, h->kinfo.int_rl_setting);
	hns3_set_vector_coalesce_rl(rx_vector, h->kinfo.int_rl_setting);
1536 1537 1538 1539 1540 1541 1542

	if (tx_vector->tx_group.coal.ql_enable)
		hns3_set_vector_coalesce_tx_ql(tx_vector,
					       tx_vector->tx_group.coal.int_ql);
	if (rx_vector->rx_group.coal.ql_enable)
		hns3_set_vector_coalesce_rx_ql(rx_vector,
					       rx_vector->rx_group.coal.int_ql);
1543 1544 1545
}

static int hns3_set_coalesce(struct net_device *netdev,
1546 1547 1548
			     struct ethtool_coalesce *cmd,
			     struct kernel_ethtool_coalesce *kernel_coal,
			     struct netlink_ext_ack *extack)
1549 1550
{
	struct hnae3_handle *h = hns3_get_handle(netdev);
1551 1552 1553
	struct hns3_nic_priv *priv = netdev_priv(netdev);
	struct hns3_enet_coalesce *tx_coal = &priv->tx_coal;
	struct hns3_enet_coalesce *rx_coal = &priv->rx_coal;
1554
	u16 queue_num = h->kinfo.num_tqps;
1555 1556
	enum dim_cq_period_mode tx_mode;
	enum dim_cq_period_mode rx_mode;
1557 1558 1559
	int ret;
	int i;

1560 1561 1562
	if (hns3_nic_resetting(netdev))
		return -EBUSY;

1563
	ret = hns3_check_coalesce_para(netdev, cmd, kernel_coal);
1564 1565 1566 1567 1568 1569
	if (ret)
		return ret;

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

1570 1571 1572 1573 1574 1575 1576 1577 1578
	tx_coal->adapt_enable = cmd->use_adaptive_tx_coalesce;
	rx_coal->adapt_enable = cmd->use_adaptive_rx_coalesce;

	tx_coal->int_gl = cmd->tx_coalesce_usecs;
	rx_coal->int_gl = cmd->rx_coalesce_usecs;

	tx_coal->int_ql = cmd->tx_max_coalesced_frames;
	rx_coal->int_ql = cmd->rx_max_coalesced_frames;

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

1582 1583 1584 1585 1586 1587 1588 1589
	tx_mode = kernel_coal->use_cqe_mode_tx ?
		  DIM_CQ_PERIOD_MODE_START_FROM_CQE :
		  DIM_CQ_PERIOD_MODE_START_FROM_EQE;
	rx_mode = kernel_coal->use_cqe_mode_rx ?
		  DIM_CQ_PERIOD_MODE_START_FROM_CQE :
		  DIM_CQ_PERIOD_MODE_START_FROM_EQE;
	hns3_cq_period_mode_init(priv, tx_mode, rx_mode);

1590 1591 1592
	return 0;
}

1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
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);
}

1614 1615 1616 1617 1618
static int hns3_set_phys_id(struct net_device *netdev,
			    enum ethtool_phys_id_state state)
{
	struct hnae3_handle *h = hns3_get_handle(netdev);

1619
	if (!h->ae_algo->ops->set_led_id)
1620 1621 1622 1623 1624
		return -EOPNOTSUPP;

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

1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
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;
}

1639 1640 1641 1642 1643 1644 1645 1646 1647
/* 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;
1648 1649
	if (loc_fec & BIT(HNAE3_FEC_LLRS))
		eth_fec |= ETHTOOL_FEC_LLRS;
1650 1651
	if (loc_fec & BIT(HNAE3_FEC_BASER))
		eth_fec |= ETHTOOL_FEC_BASER;
1652 1653
	if (loc_fec & BIT(HNAE3_FEC_NONE))
		eth_fec |= ETHTOOL_FEC_OFF;
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663

	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)
1664
		loc_fec |= BIT(HNAE3_FEC_NONE);
1665 1666 1667 1668
	if (eth_fec & ETHTOOL_FEC_AUTO)
		loc_fec |= BIT(HNAE3_FEC_AUTO);
	if (eth_fec & ETHTOOL_FEC_RS)
		loc_fec |= BIT(HNAE3_FEC_RS);
1669 1670
	if (eth_fec & ETHTOOL_FEC_LLRS)
		loc_fec |= BIT(HNAE3_FEC_LLRS);
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
	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);
1681
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1682 1683 1684 1685
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
	u8 fec_ability;
	u8 fec_mode;

1686
	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1687 1688 1689 1690 1691 1692 1693 1694 1695
		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);
1696 1697
	if (!fec->active_fec)
		fec->active_fec = ETHTOOL_FEC_OFF;
1698 1699 1700 1701 1702 1703 1704 1705

	return 0;
}

static int hns3_set_fecparam(struct net_device *netdev,
			     struct ethtool_fecparam *fec)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
1706
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1707 1708 1709
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
	u32 fec_mode;

1710
	if (!test_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps))
1711 1712 1713 1714 1715
		return -EOPNOTSUPP;

	if (!ops->set_fec)
		return -EOPNOTSUPP;
	fec_mode = eth_to_loc_fec(fec->fec);
1716 1717 1718

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

1719 1720 1721
	return ops->set_fec(handle, fec_mode);
}

1722 1723 1724 1725 1726 1727
static int hns3_get_module_info(struct net_device *netdev,
				struct ethtool_modinfo *modinfo)
{
#define HNS3_SFF_8636_V1_3 0x03

	struct hnae3_handle *handle = hns3_get_handle(netdev);
1728
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1729 1730 1731 1732
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
	struct hns3_sfp_type sfp_type;
	int ret;

1733 1734
	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
	    !ops->get_module_eeprom)
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
		return -EOPNOTSUPP;

	memset(&sfp_type, 0, sizeof(sfp_type));
	ret = ops->get_module_eeprom(handle, 0, sizeof(sfp_type) / sizeof(u8),
				     (u8 *)&sfp_type);
	if (ret)
		return ret;

	switch (sfp_type.type) {
	case SFF8024_ID_SFP:
		modinfo->type = ETH_MODULE_SFF_8472;
		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
		break;
	case SFF8024_ID_QSFP_8438:
		modinfo->type = ETH_MODULE_SFF_8436;
		modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
		break;
	case SFF8024_ID_QSFP_8436_8636:
		if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
			modinfo->type = ETH_MODULE_SFF_8436;
			modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
		} else {
			modinfo->type = ETH_MODULE_SFF_8636;
			modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
		}
		break;
	case SFF8024_ID_QSFP28_8636:
		modinfo->type = ETH_MODULE_SFF_8636;
		modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
		break;
	default:
		netdev_err(netdev, "Optical module unknown: %#x\n",
			   sfp_type.type);
		return -EINVAL;
	}

	return 0;
}

static int hns3_get_module_eeprom(struct net_device *netdev,
				  struct ethtool_eeprom *ee, u8 *data)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
1778
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
1779 1780
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;

1781 1782
	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2 ||
	    !ops->get_module_eeprom)
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
		return -EOPNOTSUPP;

	if (!ee->len)
		return -EINVAL;

	memset(data, 0, ee->len);

	return ops->get_module_eeprom(handle, ee->offset, ee->len, data);
}

1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
static u32 hns3_get_priv_flags(struct net_device *netdev)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);

	return handle->priv_flags;
}

static int hns3_check_priv_flags(struct hnae3_handle *h, u32 changed)
{
	u32 i;

	for (i = 0; i < HNAE3_PFLAG_MAX; i++)
		if ((changed & BIT(i)) && !test_bit(i, &h->supported_pflags)) {
			netdev_err(h->netdev, "%s is unsupported\n",
				   hns3_priv_flags[i].name);
			return -EOPNOTSUPP;
		}

	return 0;
}

static int hns3_set_priv_flags(struct net_device *netdev, u32 pflags)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
	u32 changed = pflags ^ handle->priv_flags;
	int ret;
	u32 i;

	ret = hns3_check_priv_flags(handle, changed);
	if (ret)
		return ret;

	for (i = 0; i < HNAE3_PFLAG_MAX; i++) {
		if (changed & BIT(i)) {
			bool enable = !(handle->priv_flags & BIT(i));

			if (enable)
				handle->priv_flags |= BIT(i);
			else
				handle->priv_flags &= ~BIT(i);
			hns3_priv_flags[i].handler(netdev, enable);
		}
	}

	return 0;
}

1840 1841 1842 1843 1844
static int hns3_get_tunable(struct net_device *netdev,
			    const struct ethtool_tunable *tuna,
			    void *data)
{
	struct hns3_nic_priv *priv = netdev_priv(netdev);
1845
	struct hnae3_handle *h = priv->ae_handle;
1846 1847 1848 1849 1850 1851 1852
	int ret = 0;

	switch (tuna->id) {
	case ETHTOOL_TX_COPYBREAK:
		/* all the tx rings have the same tx_copybreak */
		*(u32 *)data = priv->tx_copybreak;
		break;
1853 1854 1855
	case ETHTOOL_RX_COPYBREAK:
		*(u32 *)data = priv->rx_copybreak;
		break;
1856 1857 1858
	case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
		*(u32 *)data = h->kinfo.tx_spare_buf_size;
		break;
1859 1860 1861 1862 1863 1864 1865 1866
	default:
		ret = -EOPNOTSUPP;
		break;
	}

	return ret;
}

1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
static int hns3_set_tx_spare_buf_size(struct net_device *netdev,
				      u32 data)
{
	struct hns3_nic_priv *priv = netdev_priv(netdev);
	struct hnae3_handle *h = priv->ae_handle;
	int ret;

	h->kinfo.tx_spare_buf_size = data;

	ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT);
	if (ret)
		return ret;

	ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);
	if (ret)
		return ret;

	ret = hns3_reset_notify(h, HNAE3_INIT_CLIENT);
	if (ret)
		return ret;

	ret = hns3_reset_notify(h, HNAE3_UP_CLIENT);
	if (ret)
		hns3_reset_notify(h, HNAE3_UNINIT_CLIENT);

	return ret;
}

1895 1896 1897 1898 1899
static int hns3_set_tunable(struct net_device *netdev,
			    const struct ethtool_tunable *tuna,
			    const void *data)
{
	struct hns3_nic_priv *priv = netdev_priv(netdev);
1900
	u32 old_tx_spare_buf_size, new_tx_spare_buf_size;
1901 1902 1903
	struct hnae3_handle *h = priv->ae_handle;
	int i, ret = 0;

1904 1905 1906 1907 1908
	if (hns3_nic_resetting(netdev) || !priv->ring) {
		netdev_err(netdev, "failed to set tunable value, dev resetting!");
		return -EBUSY;
	}

1909 1910 1911 1912 1913 1914 1915
	switch (tuna->id) {
	case ETHTOOL_TX_COPYBREAK:
		priv->tx_copybreak = *(u32 *)data;

		for (i = 0; i < h->kinfo.num_tqps; i++)
			priv->ring[i].tx_copybreak = priv->tx_copybreak;

1916 1917 1918 1919 1920 1921 1922
		break;
	case ETHTOOL_RX_COPYBREAK:
		priv->rx_copybreak = *(u32 *)data;

		for (i = h->kinfo.num_tqps; i < h->kinfo.num_tqps * 2; i++)
			priv->ring[i].rx_copybreak = priv->rx_copybreak;

1923 1924 1925 1926
		break;
	case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
		old_tx_spare_buf_size = h->kinfo.tx_spare_buf_size;
		new_tx_spare_buf_size = *(u32 *)data;
1927 1928
		netdev_info(netdev, "request to set tx spare buf size from %u to %u\n",
			    old_tx_spare_buf_size, new_tx_spare_buf_size);
1929
		ret = hns3_set_tx_spare_buf_size(netdev, new_tx_spare_buf_size);
1930 1931
		if (ret ||
		    (!priv->ring->tx_spare && new_tx_spare_buf_size != 0)) {
1932 1933
			int ret1;

1934
			netdev_warn(netdev, "change tx spare buf size fail, revert to old value\n");
1935 1936 1937
			ret1 = hns3_set_tx_spare_buf_size(netdev,
							  old_tx_spare_buf_size);
			if (ret1) {
1938
				netdev_err(netdev, "revert to old tx spare buf size fail\n");
1939 1940 1941 1942 1943
				return ret1;
			}

			return ret;
		}
1944

1945 1946 1947 1948 1949
		if (!priv->ring->tx_spare)
			netdev_info(netdev, "the active tx spare buf size is 0, disable tx spare buffer\n");
		else
			netdev_info(netdev, "the active tx spare buf size is %u, due to page order\n",
				    priv->ring->tx_spare->len);
1950

1951 1952 1953 1954 1955 1956 1957 1958 1959
		break;
	default:
		ret = -EOPNOTSUPP;
		break;
	}

	return ret;
}

1960 1961 1962
#define HNS3_ETHTOOL_COALESCE	(ETHTOOL_COALESCE_USECS |		\
				 ETHTOOL_COALESCE_USE_ADAPTIVE |	\
				 ETHTOOL_COALESCE_RX_USECS_HIGH |	\
1963
				 ETHTOOL_COALESCE_TX_USECS_HIGH |	\
1964 1965
				 ETHTOOL_COALESCE_MAX_FRAMES |		\
				 ETHTOOL_COALESCE_USE_CQE)
1966

1967 1968
#define HNS3_ETHTOOL_RING	ETHTOOL_RING_USE_RX_BUF_LEN

H
Huazhong Tan 已提交
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
static int hns3_get_ts_info(struct net_device *netdev,
			    struct ethtool_ts_info *info)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);

	if (handle->ae_algo->ops->get_ts_info)
		return handle->ae_algo->ops->get_ts_info(handle, info);

	return ethtool_op_get_ts_info(netdev, info);
}

1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
static const struct hns3_ethtool_link_ext_state_mapping
hns3_link_ext_state_map[] = {
	{1, ETHTOOL_LINK_EXT_STATE_AUTONEG,
		ETHTOOL_LINK_EXT_SUBSTATE_AN_NO_HCD},
	{2, ETHTOOL_LINK_EXT_STATE_AUTONEG,
		ETHTOOL_LINK_EXT_SUBSTATE_AN_ACK_NOT_RECEIVED},

	{256, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
		ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_INHIBIT_TIMEOUT},
	{257, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
		ETHTOOL_LINK_EXT_SUBSTATE_LT_KR_LINK_PARTNER_DID_NOT_SET_RECEIVER_READY},
	{512, ETHTOOL_LINK_EXT_STATE_LINK_TRAINING_FAILURE,
		ETHTOOL_LINK_EXT_SUBSTATE_LT_REMOTE_FAULT},

	{513, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
		ETHTOOL_LINK_EXT_SUBSTATE_LLM_PCS_DID_NOT_ACQUIRE_BLOCK_LOCK},
	{514, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
		ETHTOOL_LINK_EXT_SUBSTATE_LLM_FC_FEC_IS_NOT_LOCKED},
	{515, ETHTOOL_LINK_EXT_STATE_LINK_LOGICAL_MISMATCH,
		ETHTOOL_LINK_EXT_SUBSTATE_LLM_RS_FEC_IS_NOT_LOCKED},

	{768, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
		ETHTOOL_LINK_EXT_SUBSTATE_BSI_LARGE_NUMBER_OF_PHYSICAL_ERRORS},
	{769, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
		ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_REFERENCE_CLOCK_LOST},
	{770, ETHTOOL_LINK_EXT_STATE_BAD_SIGNAL_INTEGRITY,
		ETHTOOL_LINK_EXT_SUBSTATE_BSI_SERDES_ALOS},

	{1024, ETHTOOL_LINK_EXT_STATE_NO_CABLE, 0},
	{1025, ETHTOOL_LINK_EXT_STATE_CABLE_ISSUE,
		ETHTOOL_LINK_EXT_SUBSTATE_CI_UNSUPPORTED_CABLE},

	{1026, ETHTOOL_LINK_EXT_STATE_EEPROM_ISSUE, 0},
};

static int hns3_get_link_ext_state(struct net_device *netdev,
				   struct ethtool_link_ext_state_info *info)
{
	const struct hns3_ethtool_link_ext_state_mapping *map;
	struct hnae3_handle *h = hns3_get_handle(netdev);
	u32 status_code, i;
	int ret;

	if (netif_carrier_ok(netdev))
		return -ENODATA;

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

	ret = h->ae_algo->ops->get_link_diagnosis_info(h, &status_code);
	if (ret)
		return ret;

	for (i = 0; i < ARRAY_SIZE(hns3_link_ext_state_map); i++) {
		map = &hns3_link_ext_state_map[i];
		if (map->status_code == status_code) {
			info->link_ext_state = map->link_ext_state;
			info->__link_ext_substate = map->link_ext_substate;
			return 0;
		}
	}

	return -ENODATA;
}

2045 2046 2047
static void hns3_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
2048 2049
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
2050

2051
	if (!hnae3_ae_dev_wol_supported(ae_dev) || !ops->get_wol)
2052 2053 2054 2055 2056 2057 2058 2059 2060
		return;

	ops->get_wol(handle, wol);
}

static int hns3_set_wol(struct net_device *netdev,
			struct ethtool_wolinfo *wol)
{
	struct hnae3_handle *handle = hns3_get_handle(netdev);
2061 2062
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
	const struct hnae3_ae_ops *ops = handle->ae_algo->ops;
2063

2064
	if (!hnae3_ae_dev_wol_supported(ae_dev) || !ops->set_wol)
2065 2066 2067 2068 2069
		return -EOPNOTSUPP;

	return ops->set_wol(handle, wol);
}

2070
static const struct ethtool_ops hns3vf_ethtool_ops = {
2071
	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
2072
	.supported_ring_params = HNS3_ETHTOOL_RING,
2073 2074 2075 2076 2077 2078 2079
	.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,
2080
	.set_rxnfc = hns3_set_rxnfc,
2081 2082 2083 2084 2085
	.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,
2086
	.get_channels = hns3_get_channels,
2087
	.set_channels = hns3_set_channels,
2088 2089
	.get_coalesce = hns3_get_coalesce,
	.set_coalesce = hns3_set_coalesce,
2090 2091
	.get_regs_len = hns3_get_regs_len,
	.get_regs = hns3_get_regs,
2092
	.get_link = hns3_get_link,
2093 2094
	.get_msglevel = hns3_get_msglevel,
	.set_msglevel = hns3_set_msglevel,
2095 2096
	.get_priv_flags = hns3_get_priv_flags,
	.set_priv_flags = hns3_set_priv_flags,
2097 2098
	.get_tunable = hns3_get_tunable,
	.set_tunable = hns3_set_tunable,
2099
	.reset = hns3_set_reset,
2100 2101
};

2102
static const struct ethtool_ops hns3_ethtool_ops = {
2103
	.supported_coalesce_params = HNS3_ETHTOOL_COALESCE,
2104
	.supported_ring_params = HNS3_ETHTOOL_RING,
2105
	.self_test = hns3_self_test,
2106 2107 2108
	.get_drvinfo = hns3_get_drvinfo,
	.get_link = hns3_get_link,
	.get_ringparam = hns3_get_ringparam,
L
Lipeng 已提交
2109
	.set_ringparam = hns3_set_ringparam,
2110
	.get_pauseparam = hns3_get_pauseparam,
2111
	.set_pauseparam = hns3_set_pauseparam,
2112 2113 2114 2115
	.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 已提交
2116
	.set_rxnfc = hns3_set_rxnfc,
2117 2118 2119 2120 2121
	.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,
2122
	.set_link_ksettings = hns3_set_link_ksettings,
2123
	.nway_reset = hns3_nway_reset,
2124
	.get_channels = hns3_get_channels,
2125
	.set_channels = hns3_set_channels,
2126
	.get_coalesce = hns3_get_coalesce,
2127
	.set_coalesce = hns3_set_coalesce,
2128 2129
	.get_regs_len = hns3_get_regs_len,
	.get_regs = hns3_get_regs,
2130
	.set_phys_id = hns3_set_phys_id,
2131 2132
	.get_msglevel = hns3_get_msglevel,
	.set_msglevel = hns3_set_msglevel,
2133 2134
	.get_fecparam = hns3_get_fecparam,
	.set_fecparam = hns3_set_fecparam,
2135 2136
	.get_module_info = hns3_get_module_info,
	.get_module_eeprom = hns3_get_module_eeprom,
2137 2138
	.get_priv_flags = hns3_get_priv_flags,
	.set_priv_flags = hns3_set_priv_flags,
H
Huazhong Tan 已提交
2139
	.get_ts_info = hns3_get_ts_info,
2140 2141
	.get_tunable = hns3_get_tunable,
	.set_tunable = hns3_set_tunable,
2142
	.reset = hns3_set_reset,
2143
	.get_link_ext_state = hns3_get_link_ext_state,
2144 2145
	.get_wol = hns3_get_wol,
	.set_wol = hns3_set_wol,
2146 2147 2148 2149
};

void hns3_ethtool_set_ops(struct net_device *netdev)
{
2150 2151 2152 2153 2154 2155
	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;
2156
}