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

#include <linux/etherdevice.h>
5
#include <linux/iopoll.h>
6
#include <net/rtnetlink.h>
7 8 9 10
#include "hclgevf_cmd.h"
#include "hclgevf_main.h"
#include "hclge_mbx.h"
#include "hnae3.h"
11
#include "hclgevf_devlink.h"
12 13 14

#define HCLGEVF_NAME	"hclgevf"

15 16
#define HCLGEVF_RESET_MAX_FAIL_CNT	5

17
static int hclgevf_reset_hdev(struct hclgevf_dev *hdev);
18 19 20
static void hclgevf_task_schedule(struct hclgevf_dev *hdev,
				  unsigned long delay);

21 22
static struct hnae3_ae_algo ae_algovf;

23 24
static struct workqueue_struct *hclgevf_wq;

25
static const struct pci_device_id ae_algovf_pci_tbl[] = {
26 27 28
	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_VF), 0},
	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_RDMA_DCB_PFC_VF),
	 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS},
29 30 31 32
	/* required last entry */
	{0, }
};

33 34 35 36 37 38 39 40
static const u8 hclgevf_hash_key[] = {
	0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
	0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
	0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
	0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
	0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA
};

41 42
MODULE_DEVICE_TABLE(pci, ae_algovf_pci_tbl);

43 44 45 46 47 48 49 50 51 52
static const u32 cmdq_reg_addr_list[] = {HCLGEVF_NIC_CSQ_BASEADDR_L_REG,
					 HCLGEVF_NIC_CSQ_BASEADDR_H_REG,
					 HCLGEVF_NIC_CSQ_DEPTH_REG,
					 HCLGEVF_NIC_CSQ_TAIL_REG,
					 HCLGEVF_NIC_CSQ_HEAD_REG,
					 HCLGEVF_NIC_CRQ_BASEADDR_L_REG,
					 HCLGEVF_NIC_CRQ_BASEADDR_H_REG,
					 HCLGEVF_NIC_CRQ_DEPTH_REG,
					 HCLGEVF_NIC_CRQ_TAIL_REG,
					 HCLGEVF_NIC_CRQ_HEAD_REG,
53
					 HCLGEVF_VECTOR0_CMDQ_SRC_REG,
54
					 HCLGEVF_VECTOR0_CMDQ_STATE_REG,
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
					 HCLGEVF_CMDQ_INTR_EN_REG,
					 HCLGEVF_CMDQ_INTR_GEN_REG};

static const u32 common_reg_addr_list[] = {HCLGEVF_MISC_VECTOR_REG_BASE,
					   HCLGEVF_RST_ING,
					   HCLGEVF_GRO_EN_REG};

static const u32 ring_reg_addr_list[] = {HCLGEVF_RING_RX_ADDR_L_REG,
					 HCLGEVF_RING_RX_ADDR_H_REG,
					 HCLGEVF_RING_RX_BD_NUM_REG,
					 HCLGEVF_RING_RX_BD_LENGTH_REG,
					 HCLGEVF_RING_RX_MERGE_EN_REG,
					 HCLGEVF_RING_RX_TAIL_REG,
					 HCLGEVF_RING_RX_HEAD_REG,
					 HCLGEVF_RING_RX_FBD_NUM_REG,
					 HCLGEVF_RING_RX_OFFSET_REG,
					 HCLGEVF_RING_RX_FBD_OFFSET_REG,
					 HCLGEVF_RING_RX_STASH_REG,
					 HCLGEVF_RING_RX_BD_ERR_REG,
					 HCLGEVF_RING_TX_ADDR_L_REG,
					 HCLGEVF_RING_TX_ADDR_H_REG,
					 HCLGEVF_RING_TX_BD_NUM_REG,
					 HCLGEVF_RING_TX_PRIORITY_REG,
					 HCLGEVF_RING_TX_TC_REG,
					 HCLGEVF_RING_TX_MERGE_EN_REG,
					 HCLGEVF_RING_TX_TAIL_REG,
					 HCLGEVF_RING_TX_HEAD_REG,
					 HCLGEVF_RING_TX_FBD_NUM_REG,
					 HCLGEVF_RING_TX_OFFSET_REG,
					 HCLGEVF_RING_TX_EBD_NUM_REG,
					 HCLGEVF_RING_TX_EBD_OFFSET_REG,
					 HCLGEVF_RING_TX_BD_ERR_REG,
					 HCLGEVF_RING_EN_REG};

static const u32 tqp_intr_reg_addr_list[] = {HCLGEVF_TQP_INTR_CTRL_REG,
					     HCLGEVF_TQP_INTR_GL0_REG,
					     HCLGEVF_TQP_INTR_GL1_REG,
					     HCLGEVF_TQP_INTR_GL2_REG,
					     HCLGEVF_TQP_INTR_RL_REG};

95
static struct hclgevf_dev *hclgevf_ae_get_hdev(struct hnae3_handle *handle)
96
{
97 98 99 100 101 102
	if (!handle->client)
		return container_of(handle, struct hclgevf_dev, nic);
	else if (handle->client->type == HNAE3_CLIENT_ROCE)
		return container_of(handle, struct hclgevf_dev, roce);
	else
		return container_of(handle, struct hclgevf_dev, nic);
103 104 105 106
}

static int hclgevf_tqps_update_stats(struct hnae3_handle *handle)
{
107
	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
108 109 110 111 112 113
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	struct hclgevf_desc desc;
	struct hclgevf_tqp *tqp;
	int status;
	int i;

114 115
	for (i = 0; i < kinfo->num_tqps; i++) {
		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
116 117 118 119 120 121 122 123 124 125 126 127 128
		hclgevf_cmd_setup_basic_desc(&desc,
					     HCLGEVF_OPC_QUERY_RX_STATUS,
					     true);

		desc.data[0] = cpu_to_le32(tqp->index & 0x1ff);
		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
		if (status) {
			dev_err(&hdev->pdev->dev,
				"Query tqp stat fail, status = %d,queue = %d\n",
				status,	i);
			return status;
		}
		tqp->tqp_stats.rcb_rx_ring_pktnum_rcd +=
129
			le32_to_cpu(desc.data[1]);
130 131 132 133 134 135 136 137 138 139 140 141 142

		hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_TX_STATUS,
					     true);

		desc.data[0] = cpu_to_le32(tqp->index & 0x1ff);
		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
		if (status) {
			dev_err(&hdev->pdev->dev,
				"Query tqp stat fail, status = %d,queue = %d\n",
				status, i);
			return status;
		}
		tqp->tqp_stats.rcb_tx_ring_pktnum_rcd +=
143
			le32_to_cpu(desc.data[1]);
144 145 146 147 148 149 150 151 152 153 154 155
	}

	return 0;
}

static u64 *hclgevf_tqps_get_stats(struct hnae3_handle *handle, u64 *data)
{
	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
	struct hclgevf_tqp *tqp;
	u64 *buff = data;
	int i;

156 157
	for (i = 0; i < kinfo->num_tqps; i++) {
		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
158 159 160
		*buff++ = tqp->tqp_stats.rcb_tx_ring_pktnum_rcd;
	}
	for (i = 0; i < kinfo->num_tqps; i++) {
161
		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
162 163 164 165 166 167 168 169
		*buff++ = tqp->tqp_stats.rcb_rx_ring_pktnum_rcd;
	}

	return buff;
}

static int hclgevf_tqps_get_sset_count(struct hnae3_handle *handle, int strset)
{
170
	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
171

172
	return kinfo->num_tqps * 2;
173 174 175 176
}

static u8 *hclgevf_tqps_get_strings(struct hnae3_handle *handle, u8 *data)
{
177
	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
178
	u8 *buff = data;
179
	int i;
180

181 182 183
	for (i = 0; i < kinfo->num_tqps; i++) {
		struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i],
						       struct hclgevf_tqp, q);
184
		snprintf(buff, ETH_GSTRING_LEN, "txq%u_pktnum_rcd",
185 186 187 188
			 tqp->index);
		buff += ETH_GSTRING_LEN;
	}

189 190 191
	for (i = 0; i < kinfo->num_tqps; i++) {
		struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i],
						       struct hclgevf_tqp, q);
192
		snprintf(buff, ETH_GSTRING_LEN, "rxq%u_pktnum_rcd",
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
			 tqp->index);
		buff += ETH_GSTRING_LEN;
	}

	return buff;
}

static void hclgevf_update_stats(struct hnae3_handle *handle,
				 struct net_device_stats *net_stats)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	int status;

	status = hclgevf_tqps_update_stats(handle);
	if (status)
		dev_err(&hdev->pdev->dev,
			"VF update of TQPS stats fail, status = %d.\n",
			status);
}

static int hclgevf_get_sset_count(struct hnae3_handle *handle, int strset)
{
	if (strset == ETH_SS_TEST)
		return -EOPNOTSUPP;
	else if (strset == ETH_SS_STATS)
		return hclgevf_tqps_get_sset_count(handle, strset);

	return 0;
}

static void hclgevf_get_strings(struct hnae3_handle *handle, u32 strset,
				u8 *data)
{
	u8 *p = (char *)data;

	if (strset == ETH_SS_STATS)
		p = hclgevf_tqps_get_strings(handle, p);
}

static void hclgevf_get_stats(struct hnae3_handle *handle, u64 *data)
{
	hclgevf_tqps_get_stats(handle, data);
}

237 238 239 240 241 242 243 244 245 246
static void hclgevf_build_send_msg(struct hclge_vf_to_pf_msg *msg, u8 code,
				   u8 subcode)
{
	if (msg) {
		memset(msg, 0, sizeof(struct hclge_vf_to_pf_msg));
		msg->code = code;
		msg->subcode = subcode;
	}
}

247
static int hclgevf_get_basic_info(struct hclgevf_dev *hdev)
248
{
249 250 251
	struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
	u8 resp_msg[HCLGE_MBX_MAX_RESP_DATA_SIZE];
	struct hclge_basic_info *basic_info;
252
	struct hclge_vf_to_pf_msg send_msg;
253
	unsigned long caps;
254 255
	int status;

256 257
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_BASIC_INFO, 0);
	status = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
258
				      sizeof(resp_msg));
259 260
	if (status) {
		dev_err(&hdev->pdev->dev,
261
			"failed to get basic info from pf, ret = %d", status);
262 263 264
		return status;
	}

265 266 267 268 269 270 271
	basic_info = (struct hclge_basic_info *)resp_msg;

	hdev->hw_tc_map = basic_info->hw_tc_map;
	hdev->mbx_api_version = basic_info->mbx_api_version;
	caps = basic_info->pf_caps;
	if (test_bit(HNAE3_PF_SUPPORT_VLAN_FLTR_MDF_B, &caps))
		set_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, ae_dev->caps);
272 273 274 275

	return 0;
}

276 277 278
static int hclgevf_get_port_base_vlan_filter_state(struct hclgevf_dev *hdev)
{
	struct hnae3_handle *nic = &hdev->nic;
279
	struct hclge_vf_to_pf_msg send_msg;
280 281 282
	u8 resp_msg;
	int ret;

283 284 285 286
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
			       HCLGE_MBX_GET_PORT_BASE_VLAN_STATE);
	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, &resp_msg,
				   sizeof(u8));
287 288 289 290 291 292 293 294 295 296 297 298
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"VF request to get port based vlan state failed %d",
			ret);
		return ret;
	}

	nic->port_base_vlan_state = resp_msg;

	return 0;
}

299
static int hclgevf_get_queue_info(struct hclgevf_dev *hdev)
300
{
301
#define HCLGEVF_TQPS_RSS_INFO_LEN	6
302 303 304 305
#define HCLGEVF_TQPS_ALLOC_OFFSET	0
#define HCLGEVF_TQPS_RSS_SIZE_OFFSET	2
#define HCLGEVF_TQPS_RX_BUFFER_LEN_OFFSET	4

306
	u8 resp_msg[HCLGEVF_TQPS_RSS_INFO_LEN];
307
	struct hclge_vf_to_pf_msg send_msg;
308 309
	int status;

310 311
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_QINFO, 0);
	status = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
312 313 314 315 316 317 318 319
				      HCLGEVF_TQPS_RSS_INFO_LEN);
	if (status) {
		dev_err(&hdev->pdev->dev,
			"VF request to get tqp info from PF failed %d",
			status);
		return status;
	}

320 321 322 323 324 325
	memcpy(&hdev->num_tqps, &resp_msg[HCLGEVF_TQPS_ALLOC_OFFSET],
	       sizeof(u16));
	memcpy(&hdev->rss_size_max, &resp_msg[HCLGEVF_TQPS_RSS_SIZE_OFFSET],
	       sizeof(u16));
	memcpy(&hdev->rx_buf_len, &resp_msg[HCLGEVF_TQPS_RX_BUFFER_LEN_OFFSET],
	       sizeof(u16));
326 327 328 329 330 331 332

	return 0;
}

static int hclgevf_get_queue_depth(struct hclgevf_dev *hdev)
{
#define HCLGEVF_TQPS_DEPTH_INFO_LEN	4
333 334 335
#define HCLGEVF_TQPS_NUM_TX_DESC_OFFSET	0
#define HCLGEVF_TQPS_NUM_RX_DESC_OFFSET	2

336
	u8 resp_msg[HCLGEVF_TQPS_DEPTH_INFO_LEN];
337
	struct hclge_vf_to_pf_msg send_msg;
338 339
	int ret;

340 341
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_QDEPTH, 0);
	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
342 343 344 345 346 347 348 349
				   HCLGEVF_TQPS_DEPTH_INFO_LEN);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"VF request to get tqp depth info from PF failed %d",
			ret);
		return ret;
	}

350 351 352 353
	memcpy(&hdev->num_tx_desc, &resp_msg[HCLGEVF_TQPS_NUM_TX_DESC_OFFSET],
	       sizeof(u16));
	memcpy(&hdev->num_rx_desc, &resp_msg[HCLGEVF_TQPS_NUM_RX_DESC_OFFSET],
	       sizeof(u16));
354 355 356 357

	return 0;
}

358 359 360
static u16 hclgevf_get_qid_global(struct hnae3_handle *handle, u16 queue_id)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
361
	struct hclge_vf_to_pf_msg send_msg;
362
	u16 qid_in_pf = 0;
363
	u8 resp_data[2];
364 365
	int ret;

366 367 368
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_QID_IN_PF, 0);
	memcpy(send_msg.data, &queue_id, sizeof(queue_id));
	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_data,
369
				   sizeof(resp_data));
370 371 372 373 374 375
	if (!ret)
		qid_in_pf = *(u16 *)resp_data;

	return qid_in_pf;
}

376 377
static int hclgevf_get_pf_media_type(struct hclgevf_dev *hdev)
{
378
	struct hclge_vf_to_pf_msg send_msg;
379
	u8 resp_msg[2];
380 381
	int ret;

382 383 384
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_MEDIA_TYPE, 0);
	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
				   sizeof(resp_msg));
385 386 387 388 389 390 391
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"VF request to get the pf port media type failed %d",
			ret);
		return ret;
	}

392 393
	hdev->hw.mac.media_type = resp_msg[0];
	hdev->hw.mac.module_type = resp_msg[1];
394 395 396 397

	return 0;
}

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
static int hclgevf_alloc_tqps(struct hclgevf_dev *hdev)
{
	struct hclgevf_tqp *tqp;
	int i;

	hdev->htqp = devm_kcalloc(&hdev->pdev->dev, hdev->num_tqps,
				  sizeof(struct hclgevf_tqp), GFP_KERNEL);
	if (!hdev->htqp)
		return -ENOMEM;

	tqp = hdev->htqp;

	for (i = 0; i < hdev->num_tqps; i++) {
		tqp->dev = &hdev->pdev->dev;
		tqp->index = i;

		tqp->q.ae_algo = &ae_algovf;
		tqp->q.buf_size = hdev->rx_buf_len;
416 417
		tqp->q.tx_desc_num = hdev->num_tx_desc;
		tqp->q.rx_desc_num = hdev->num_rx_desc;
418 419 420 421 422 423 424 425 426 427 428 429 430 431

		/* need an extended offset to configure queues >=
		 * HCLGEVF_TQP_MAX_SIZE_DEV_V2.
		 */
		if (i < HCLGEVF_TQP_MAX_SIZE_DEV_V2)
			tqp->q.io_base = hdev->hw.io_base +
					 HCLGEVF_TQP_REG_OFFSET +
					 i * HCLGEVF_TQP_REG_SIZE;
		else
			tqp->q.io_base = hdev->hw.io_base +
					 HCLGEVF_TQP_REG_OFFSET +
					 HCLGEVF_TQP_EXT_REG_OFFSET +
					 (i - HCLGEVF_TQP_MAX_SIZE_DEV_V2) *
					 HCLGEVF_TQP_REG_SIZE;
432 433 434 435 436 437 438 439 440 441 442 443

		tqp++;
	}

	return 0;
}

static int hclgevf_knic_setup(struct hclgevf_dev *hdev)
{
	struct hnae3_handle *nic = &hdev->nic;
	struct hnae3_knic_private_info *kinfo;
	u16 new_tqps = hdev->num_tqps;
444
	unsigned int i;
445
	u8 num_tc = 0;
446 447

	kinfo = &nic->kinfo;
448 449
	kinfo->num_tx_desc = hdev->num_tx_desc;
	kinfo->num_rx_desc = hdev->num_rx_desc;
450 451 452
	kinfo->rx_buf_len = hdev->rx_buf_len;
	for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++)
		if (hdev->hw_tc_map & BIT(i))
453
			num_tc++;
454

455 456 457 458
	num_tc = num_tc ? num_tc : 1;
	kinfo->tc_info.num_tc = num_tc;
	kinfo->rss_size = min_t(u16, hdev->rss_size_max, new_tqps / num_tc);
	new_tqps = kinfo->rss_size * num_tc;
459 460 461 462 463 464 465 466 467 468 469 470 471
	kinfo->num_tqps = min(new_tqps, hdev->num_tqps);

	kinfo->tqp = devm_kcalloc(&hdev->pdev->dev, kinfo->num_tqps,
				  sizeof(struct hnae3_queue *), GFP_KERNEL);
	if (!kinfo->tqp)
		return -ENOMEM;

	for (i = 0; i < kinfo->num_tqps; i++) {
		hdev->htqp[i].q.handle = &hdev->nic;
		hdev->htqp[i].q.tqp_index = i;
		kinfo->tqp[i] = &hdev->htqp[i].q;
	}

472 473 474 475
	/* after init the max rss_size and tqps, adjust the default tqp numbers
	 * and rss size with the actual vector numbers
	 */
	kinfo->num_tqps = min_t(u16, hdev->num_nic_msix - 1, kinfo->num_tqps);
476
	kinfo->rss_size = min_t(u16, kinfo->num_tqps / num_tc,
477 478
				kinfo->rss_size);

479 480 481 482 483
	return 0;
}

static void hclgevf_request_link_info(struct hclgevf_dev *hdev)
{
484
	struct hclge_vf_to_pf_msg send_msg;
485 486
	int status;

487 488
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_LINK_STATUS, 0);
	status = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
489 490 491 492 493 494 495
	if (status)
		dev_err(&hdev->pdev->dev,
			"VF failed to fetch link status(%d) from PF", status);
}

void hclgevf_update_link_status(struct hclgevf_dev *hdev, int link_state)
{
496
	struct hnae3_handle *rhandle = &hdev->roce;
497
	struct hnae3_handle *handle = &hdev->nic;
498
	struct hnae3_client *rclient;
499 500
	struct hnae3_client *client;

501 502 503
	if (test_and_set_bit(HCLGEVF_STATE_LINK_UPDATING, &hdev->state))
		return;

504
	client = handle->client;
505
	rclient = hdev->roce_client;
506

507 508
	link_state =
		test_bit(HCLGEVF_STATE_DOWN, &hdev->state) ? 0 : link_state;
509
	if (link_state != hdev->hw.mac.link) {
510
		hdev->hw.mac.link = link_state;
511
		client->ops->link_status_change(handle, !!link_state);
512 513
		if (rclient && rclient->ops->link_status_change)
			rclient->ops->link_status_change(rhandle, !!link_state);
514
	}
515 516

	clear_bit(HCLGEVF_STATE_LINK_UPDATING, &hdev->state);
517 518
}

519
static void hclgevf_update_link_mode(struct hclgevf_dev *hdev)
520
{
521 522 523 524
#define HCLGEVF_ADVERTISING	0
#define HCLGEVF_SUPPORTED	1

	struct hclge_vf_to_pf_msg send_msg;
525

526 527 528 529 530
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_LINK_MODE, 0);
	send_msg.data[0] = HCLGEVF_ADVERTISING;
	hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
	send_msg.data[0] = HCLGEVF_SUPPORTED;
	hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
531 532
}

533 534 535 536 537 538 539 540
static int hclgevf_set_handle_info(struct hclgevf_dev *hdev)
{
	struct hnae3_handle *nic = &hdev->nic;
	int ret;

	nic->ae_algo = &ae_algovf;
	nic->pdev = hdev->pdev;
	nic->numa_node_mask = hdev->numa_node_mask;
541
	nic->flags |= HNAE3_SUPPORT_VF;
542
	nic->kinfo.io_base = hdev->hw.io_base;
543 544 545 546 547 548 549 550 551 552

	ret = hclgevf_knic_setup(hdev);
	if (ret)
		dev_err(&hdev->pdev->dev, "VF knic setup failed %d\n",
			ret);
	return ret;
}

static void hclgevf_free_vector(struct hclgevf_dev *hdev, int vector_id)
{
553 554 555 556 557 558
	if (hdev->vector_status[vector_id] == HCLGEVF_INVALID_VPORT) {
		dev_warn(&hdev->pdev->dev,
			 "vector(vector_id %d) has been freed.\n", vector_id);
		return;
	}

559 560 561 562 563 564 565 566 567 568 569 570 571
	hdev->vector_status[vector_id] = HCLGEVF_INVALID_VPORT;
	hdev->num_msi_left += 1;
	hdev->num_msi_used -= 1;
}

static int hclgevf_get_vector(struct hnae3_handle *handle, u16 vector_num,
			      struct hnae3_vector_info *vector_info)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	struct hnae3_vector_info *vector = vector_info;
	int alloc = 0;
	int i, j;

572
	vector_num = min_t(u16, hdev->num_nic_msix - 1, vector_num);
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
	vector_num = min(hdev->num_msi_left, vector_num);

	for (j = 0; j < vector_num; j++) {
		for (i = HCLGEVF_MISC_VECTOR_NUM + 1; i < hdev->num_msi; i++) {
			if (hdev->vector_status[i] == HCLGEVF_INVALID_VPORT) {
				vector->vector = pci_irq_vector(hdev->pdev, i);
				vector->io_addr = hdev->hw.io_base +
					HCLGEVF_VECTOR_REG_BASE +
					(i - 1) * HCLGEVF_VECTOR_REG_OFFSET;
				hdev->vector_status[i] = 0;
				hdev->vector_irq[i] = vector->vector;

				vector++;
				alloc++;

				break;
			}
		}
	}
	hdev->num_msi_left -= alloc;
	hdev->num_msi_used += alloc;

	return alloc;
}

static int hclgevf_get_vector_index(struct hclgevf_dev *hdev, int vector)
{
	int i;

	for (i = 0; i < hdev->num_msi; i++)
		if (vector == hdev->vector_irq[i])
			return i;

	return -EINVAL;
}

609 610 611 612
static int hclgevf_set_rss_algo_key(struct hclgevf_dev *hdev,
				    const u8 hfunc, const u8 *key)
{
	struct hclgevf_rss_config_cmd *req;
613
	unsigned int key_offset = 0;
614
	struct hclgevf_desc desc;
615
	int key_counts;
616 617 618
	int key_size;
	int ret;

619
	key_counts = HCLGEVF_RSS_KEY_SIZE;
620 621
	req = (struct hclgevf_rss_config_cmd *)desc.data;

622
	while (key_counts) {
623 624 625 626 627 628 629 630
		hclgevf_cmd_setup_basic_desc(&desc,
					     HCLGEVF_OPC_RSS_GENERIC_CONFIG,
					     false);

		req->hash_config |= (hfunc & HCLGEVF_RSS_HASH_ALGO_MASK);
		req->hash_config |=
			(key_offset << HCLGEVF_RSS_HASH_KEY_OFFSET_B);

631
		key_size = min(HCLGEVF_RSS_HASH_KEY_NUM, key_counts);
632 633 634
		memcpy(req->hash_key,
		       key + key_offset * HCLGEVF_RSS_HASH_KEY_NUM, key_size);

635 636
		key_counts -= key_size;
		key_offset++;
637 638 639 640 641 642 643 644 645 646 647 648
		ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
		if (ret) {
			dev_err(&hdev->pdev->dev,
				"Configure RSS config fail, status = %d\n",
				ret);
			return ret;
		}
	}

	return 0;
}

649 650 651 652 653 654 655 656 657 658
static u32 hclgevf_get_rss_key_size(struct hnae3_handle *handle)
{
	return HCLGEVF_RSS_KEY_SIZE;
}

static int hclgevf_set_rss_indir_table(struct hclgevf_dev *hdev)
{
	const u8 *indir = hdev->rss_cfg.rss_indirection_tbl;
	struct hclgevf_rss_indirection_table_cmd *req;
	struct hclgevf_desc desc;
659
	int rss_cfg_tbl_num;
660 661 662 663
	int status;
	int i, j;

	req = (struct hclgevf_rss_indirection_table_cmd *)desc.data;
664 665
	rss_cfg_tbl_num = hdev->ae_dev->dev_specs.rss_ind_tbl_size /
			  HCLGEVF_RSS_CFG_TBL_SIZE;
666

667
	for (i = 0; i < rss_cfg_tbl_num; i++) {
668 669
		hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INDIR_TABLE,
					     false);
670 671 672
		req->start_table_index =
			cpu_to_le16(i * HCLGEVF_RSS_CFG_TBL_SIZE);
		req->rss_set_bitmap = cpu_to_le16(HCLGEVF_RSS_SET_BITMAP_MSK);
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
		for (j = 0; j < HCLGEVF_RSS_CFG_TBL_SIZE; j++)
			req->rss_result[j] =
				indir[i * HCLGEVF_RSS_CFG_TBL_SIZE + j];

		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
		if (status) {
			dev_err(&hdev->pdev->dev,
				"VF failed(=%d) to set RSS indirection table\n",
				status);
			return status;
		}
	}

	return 0;
}

static int hclgevf_set_rss_tc_mode(struct hclgevf_dev *hdev,  u16 rss_size)
{
	struct hclgevf_rss_tc_mode_cmd *req;
	u16 tc_offset[HCLGEVF_MAX_TC_NUM];
	u16 tc_valid[HCLGEVF_MAX_TC_NUM];
	u16 tc_size[HCLGEVF_MAX_TC_NUM];
	struct hclgevf_desc desc;
	u16 roundup_size;
697
	unsigned int i;
698
	int status;
699 700 701 702 703 704 705 706 707 708 709 710 711 712

	req = (struct hclgevf_rss_tc_mode_cmd *)desc.data;

	roundup_size = roundup_pow_of_two(rss_size);
	roundup_size = ilog2(roundup_size);

	for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) {
		tc_valid[i] = !!(hdev->hw_tc_map & BIT(i));
		tc_size[i] = roundup_size;
		tc_offset[i] = rss_size * i;
	}

	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_TC_MODE, false);
	for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) {
713 714 715
		u16 mode = 0;

		hnae3_set_bit(mode, HCLGEVF_RSS_TC_VALID_B,
P
Peng Li 已提交
716
			      (tc_valid[i] & 0x1));
717
		hnae3_set_field(mode, HCLGEVF_RSS_TC_SIZE_M,
P
Peng Li 已提交
718
				HCLGEVF_RSS_TC_SIZE_S, tc_size[i]);
719 720 721
		hnae3_set_bit(mode, HCLGEVF_RSS_TC_SIZE_MSB_B,
			      tc_size[i] >> HCLGEVF_RSS_TC_SIZE_MSB_OFFSET &
			      0x1);
722
		hnae3_set_field(mode, HCLGEVF_RSS_TC_OFFSET_M,
P
Peng Li 已提交
723
				HCLGEVF_RSS_TC_OFFSET_S, tc_offset[i]);
724 725

		req->rss_tc_mode[i] = cpu_to_le16(mode);
726 727 728 729 730 731 732 733 734
	}
	status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
	if (status)
		dev_err(&hdev->pdev->dev,
			"VF failed(=%d) to set rss tc mode\n", status);

	return status;
}

J
Jian Shen 已提交
735 736 737 738 739 740
/* for revision 0x20, vf shared the same rss config with pf */
static int hclgevf_get_rss_hash_key(struct hclgevf_dev *hdev)
{
#define HCLGEVF_RSS_MBX_RESP_LEN	8
	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
	u8 resp_msg[HCLGEVF_RSS_MBX_RESP_LEN];
741
	struct hclge_vf_to_pf_msg send_msg;
J
Jian Shen 已提交
742 743 744 745
	u16 msg_num, hash_key_index;
	u8 index;
	int ret;

746
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_RSS_KEY, 0);
J
Jian Shen 已提交
747 748 749
	msg_num = (HCLGEVF_RSS_KEY_SIZE + HCLGEVF_RSS_MBX_RESP_LEN - 1) /
			HCLGEVF_RSS_MBX_RESP_LEN;
	for (index = 0; index < msg_num; index++) {
750 751
		send_msg.data[0] = index;
		ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
J
Jian Shen 已提交
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
					   HCLGEVF_RSS_MBX_RESP_LEN);
		if (ret) {
			dev_err(&hdev->pdev->dev,
				"VF get rss hash key from PF failed, ret=%d",
				ret);
			return ret;
		}

		hash_key_index = HCLGEVF_RSS_MBX_RESP_LEN * index;
		if (index == msg_num - 1)
			memcpy(&rss_cfg->rss_hash_key[hash_key_index],
			       &resp_msg[0],
			       HCLGEVF_RSS_KEY_SIZE - hash_key_index);
		else
			memcpy(&rss_cfg->rss_hash_key[hash_key_index],
			       &resp_msg[0], HCLGEVF_RSS_MBX_RESP_LEN);
	}

	return 0;
}

773 774
static int hclgevf_get_rss(struct hnae3_handle *handle, u32 *indir, u8 *key,
			   u8 *hfunc)
775 776
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
777
	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
J
Jian Shen 已提交
778
	int i, ret;
779

780
	if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
781 782 783 784 785 786 787 788 789 790 791 792 793
		/* Get hash algorithm */
		if (hfunc) {
			switch (rss_cfg->hash_algo) {
			case HCLGEVF_RSS_HASH_ALGO_TOEPLITZ:
				*hfunc = ETH_RSS_HASH_TOP;
				break;
			case HCLGEVF_RSS_HASH_ALGO_SIMPLE:
				*hfunc = ETH_RSS_HASH_XOR;
				break;
			default:
				*hfunc = ETH_RSS_HASH_UNKNOWN;
				break;
			}
794 795
		}

796
		/* Get the RSS Key required by the user */
797
		if (key)
798 799
			memcpy(key, rss_cfg->rss_hash_key,
			       HCLGEVF_RSS_KEY_SIZE);
J
Jian Shen 已提交
800 801 802 803 804 805 806 807 808 809
	} else {
		if (hfunc)
			*hfunc = ETH_RSS_HASH_TOP;
		if (key) {
			ret = hclgevf_get_rss_hash_key(hdev);
			if (ret)
				return ret;
			memcpy(key, rss_cfg->rss_hash_key,
			       HCLGEVF_RSS_KEY_SIZE);
		}
810 811 812
	}

	if (indir)
813
		for (i = 0; i < hdev->ae_dev->dev_specs.rss_ind_tbl_size; i++)
814 815
			indir[i] = rss_cfg->rss_indirection_tbl[i];

816
	return 0;
817 818
}

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
static int hclgevf_parse_rss_hfunc(struct hclgevf_dev *hdev, const u8 hfunc,
				   u8 *hash_algo)
{
	switch (hfunc) {
	case ETH_RSS_HASH_TOP:
		*hash_algo = HCLGEVF_RSS_HASH_ALGO_TOEPLITZ;
		return 0;
	case ETH_RSS_HASH_XOR:
		*hash_algo = HCLGEVF_RSS_HASH_ALGO_SIMPLE;
		return 0;
	case ETH_RSS_HASH_NO_CHANGE:
		*hash_algo = hdev->rss_cfg.hash_algo;
		return 0;
	default:
		return -EINVAL;
	}
}

837
static int hclgevf_set_rss(struct hnae3_handle *handle, const u32 *indir,
838
			   const u8 *key, const u8 hfunc)
839 840 841
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
842
	u8 hash_algo;
843 844
	int ret, i;

845
	if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
846 847 848 849
		ret = hclgevf_parse_rss_hfunc(hdev, hfunc, &hash_algo);
		if (ret)
			return ret;

850 851
		/* Set the RSS Hash Key if specififed by the user */
		if (key) {
852 853 854 855
			ret = hclgevf_set_rss_algo_key(hdev, hash_algo, key);
			if (ret) {
				dev_err(&hdev->pdev->dev,
					"invalid hfunc type %u\n", hfunc);
856
				return ret;
857
			}
858 859 860 861

			/* Update the shadow RSS key with user specified qids */
			memcpy(rss_cfg->rss_hash_key, key,
			       HCLGEVF_RSS_KEY_SIZE);
862 863 864 865 866
		} else {
			ret = hclgevf_set_rss_algo_key(hdev, hash_algo,
						       rss_cfg->rss_hash_key);
			if (ret)
				return ret;
867
		}
868
		rss_cfg->hash_algo = hash_algo;
869
	}
870 871

	/* update the shadow RSS table with user specified qids */
872
	for (i = 0; i < hdev->ae_dev->dev_specs.rss_ind_tbl_size; i++)
873 874 875 876 877 878
		rss_cfg->rss_indirection_tbl[i] = indir[i];

	/* update the hardware */
	return hclgevf_set_rss_indir_table(hdev);
}

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
static u8 hclgevf_get_rss_hash_bits(struct ethtool_rxnfc *nfc)
{
	u8 hash_sets = nfc->data & RXH_L4_B_0_1 ? HCLGEVF_S_PORT_BIT : 0;

	if (nfc->data & RXH_L4_B_2_3)
		hash_sets |= HCLGEVF_D_PORT_BIT;
	else
		hash_sets &= ~HCLGEVF_D_PORT_BIT;

	if (nfc->data & RXH_IP_SRC)
		hash_sets |= HCLGEVF_S_IP_BIT;
	else
		hash_sets &= ~HCLGEVF_S_IP_BIT;

	if (nfc->data & RXH_IP_DST)
		hash_sets |= HCLGEVF_D_IP_BIT;
	else
		hash_sets &= ~HCLGEVF_D_IP_BIT;

	if (nfc->flow_type == SCTP_V4_FLOW || nfc->flow_type == SCTP_V6_FLOW)
		hash_sets |= HCLGEVF_V_TAG_BIT;

	return hash_sets;
}

904 905 906
static int hclgevf_init_rss_tuple_cmd(struct hnae3_handle *handle,
				      struct ethtool_rxnfc *nfc,
				      struct hclgevf_rss_input_tuple_cmd *req)
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
	u8 tuple_sets;

	req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
	req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en;
	req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
	req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en;
	req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
	req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en;
	req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
	req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en;

	tuple_sets = hclgevf_get_rss_hash_bits(nfc);
	switch (nfc->flow_type) {
	case TCP_V4_FLOW:
		req->ipv4_tcp_en = tuple_sets;
		break;
	case TCP_V6_FLOW:
		req->ipv6_tcp_en = tuple_sets;
		break;
	case UDP_V4_FLOW:
		req->ipv4_udp_en = tuple_sets;
		break;
	case UDP_V6_FLOW:
		req->ipv6_udp_en = tuple_sets;
		break;
	case SCTP_V4_FLOW:
		req->ipv4_sctp_en = tuple_sets;
		break;
	case SCTP_V6_FLOW:
939 940
		if (hdev->ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2 &&
		    (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)))
941 942 943 944 945 946 947 948 949 950 951 952 953 954
			return -EINVAL;

		req->ipv6_sctp_en = tuple_sets;
		break;
	case IPV4_FLOW:
		req->ipv4_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
		break;
	case IPV6_FLOW:
		req->ipv6_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
		break;
	default:
		return -EINVAL;
	}

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
	return 0;
}

static int hclgevf_set_rss_tuple(struct hnae3_handle *handle,
				 struct ethtool_rxnfc *nfc)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
	struct hclgevf_rss_input_tuple_cmd *req;
	struct hclgevf_desc desc;
	int ret;

	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
		return -EOPNOTSUPP;

	if (nfc->data &
	    ~(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3))
		return -EINVAL;

	req = (struct hclgevf_rss_input_tuple_cmd *)desc.data;
	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false);

	ret = hclgevf_init_rss_tuple_cmd(handle, nfc, req);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"failed to init rss tuple cmd, ret = %d\n", ret);
		return ret;
	}

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"Set rss tuple fail, status = %d\n", ret);
		return ret;
	}

	rss_cfg->rss_tuple_sets.ipv4_tcp_en = req->ipv4_tcp_en;
	rss_cfg->rss_tuple_sets.ipv4_udp_en = req->ipv4_udp_en;
	rss_cfg->rss_tuple_sets.ipv4_sctp_en = req->ipv4_sctp_en;
	rss_cfg->rss_tuple_sets.ipv4_fragment_en = req->ipv4_fragment_en;
	rss_cfg->rss_tuple_sets.ipv6_tcp_en = req->ipv6_tcp_en;
	rss_cfg->rss_tuple_sets.ipv6_udp_en = req->ipv6_udp_en;
	rss_cfg->rss_tuple_sets.ipv6_sctp_en = req->ipv6_sctp_en;
	rss_cfg->rss_tuple_sets.ipv6_fragment_en = req->ipv6_fragment_en;
	return 0;
}

1002 1003
static int hclgevf_get_rss_tuple_by_flow_type(struct hclgevf_dev *hdev,
					      int flow_type, u8 *tuple_sets)
1004
{
1005
	switch (flow_type) {
1006
	case TCP_V4_FLOW:
1007
		*tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv4_tcp_en;
1008 1009
		break;
	case UDP_V4_FLOW:
1010
		*tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv4_udp_en;
1011 1012
		break;
	case TCP_V6_FLOW:
1013
		*tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv6_tcp_en;
1014 1015
		break;
	case UDP_V6_FLOW:
1016
		*tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv6_udp_en;
1017 1018
		break;
	case SCTP_V4_FLOW:
1019
		*tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv4_sctp_en;
1020 1021
		break;
	case SCTP_V6_FLOW:
1022
		*tuple_sets = hdev->rss_cfg.rss_tuple_sets.ipv6_sctp_en;
1023 1024 1025
		break;
	case IPV4_FLOW:
	case IPV6_FLOW:
1026
		*tuple_sets = HCLGEVF_S_IP_BIT | HCLGEVF_D_IP_BIT;
1027 1028 1029 1030 1031
		break;
	default:
		return -EINVAL;
	}

1032 1033 1034 1035 1036 1037
	return 0;
}

static u64 hclgevf_convert_rss_tuple(u8 tuple_sets)
{
	u64 tuple_data = 0;
1038 1039

	if (tuple_sets & HCLGEVF_D_PORT_BIT)
1040
		tuple_data |= RXH_L4_B_2_3;
1041
	if (tuple_sets & HCLGEVF_S_PORT_BIT)
1042
		tuple_data |= RXH_L4_B_0_1;
1043
	if (tuple_sets & HCLGEVF_D_IP_BIT)
1044
		tuple_data |= RXH_IP_DST;
1045
	if (tuple_sets & HCLGEVF_S_IP_BIT)
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
		tuple_data |= RXH_IP_SRC;

	return tuple_data;
}

static int hclgevf_get_rss_tuple(struct hnae3_handle *handle,
				 struct ethtool_rxnfc *nfc)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	u8 tuple_sets;
	int ret;

	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
		return -EOPNOTSUPP;

	nfc->data = 0;

	ret = hclgevf_get_rss_tuple_by_flow_type(hdev, nfc->flow_type,
						 &tuple_sets);
	if (ret || !tuple_sets)
		return ret;

	nfc->data = hclgevf_convert_rss_tuple(tuple_sets);
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

	return 0;
}

static int hclgevf_set_rss_input_tuple(struct hclgevf_dev *hdev,
				       struct hclgevf_rss_cfg *rss_cfg)
{
	struct hclgevf_rss_input_tuple_cmd *req;
	struct hclgevf_desc desc;
	int ret;

	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false);

	req = (struct hclgevf_rss_input_tuple_cmd *)desc.data;

	req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
	req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en;
	req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
	req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en;
	req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
	req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en;
	req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
	req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en;

	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
	if (ret)
		dev_err(&hdev->pdev->dev,
			"Configure rss input fail, status = %d\n", ret);
	return ret;
}

1100 1101 1102 1103 1104 1105 1106 1107 1108
static int hclgevf_get_tc_size(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;

	return rss_cfg->rss_size;
}

static int hclgevf_bind_ring_to_vector(struct hnae3_handle *handle, bool en,
1109
				       int vector_id,
1110 1111 1112
				       struct hnae3_ring_chain_node *ring_chain)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1113
	struct hclge_vf_to_pf_msg send_msg;
1114 1115
	struct hnae3_ring_chain_node *node;
	int status;
1116
	int i = 0;
1117

1118 1119
	memset(&send_msg, 0, sizeof(send_msg));
	send_msg.code = en ? HCLGE_MBX_MAP_RING_TO_VECTOR :
1120
		HCLGE_MBX_UNMAP_RING_TO_VECTOR;
1121
	send_msg.vector_id = vector_id;
1122 1123

	for (node = ring_chain; node; node = node->next) {
1124
		send_msg.param[i].ring_type =
P
Peng Li 已提交
1125
				hnae3_get_bit(node->flag, HNAE3_RING_TYPE_B);
1126 1127 1128 1129 1130 1131

		send_msg.param[i].tqp_index = node->tqp_index;
		send_msg.param[i].int_gl_index =
					hnae3_get_field(node->int_gl_idx,
							HNAE3_RING_GL_IDX_M,
							HNAE3_RING_GL_IDX_S);
1132 1133

		i++;
1134 1135
		if (i == HCLGE_MBX_MAX_RING_CHAIN_PARAM_NUM || !node->next) {
			send_msg.ring_num = i;
1136

1137 1138
			status = hclgevf_send_mbx_msg(hdev, &send_msg, false,
						      NULL, 0);
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
			if (status) {
				dev_err(&hdev->pdev->dev,
					"Map TQP fail, status is %d.\n",
					status);
				return status;
			}
			i = 0;
		}
	}

	return 0;
}

static int hclgevf_map_ring_to_vector(struct hnae3_handle *handle, int vector,
				      struct hnae3_ring_chain_node *ring_chain)
{
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	int vector_id;

	vector_id = hclgevf_get_vector_index(hdev, vector);
	if (vector_id < 0) {
		dev_err(&handle->pdev->dev,
			"Get vector index fail. ret =%d\n", vector_id);
		return vector_id;
	}

	return hclgevf_bind_ring_to_vector(handle, true, vector_id, ring_chain);
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
}

static int hclgevf_unmap_ring_from_vector(
				struct hnae3_handle *handle,
				int vector,
				struct hnae3_ring_chain_node *ring_chain)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	int ret, vector_id;

1176 1177 1178
	if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state))
		return 0;

1179 1180 1181 1182 1183 1184 1185
	vector_id = hclgevf_get_vector_index(hdev, vector);
	if (vector_id < 0) {
		dev_err(&handle->pdev->dev,
			"Get vector index fail. ret =%d\n", vector_id);
		return vector_id;
	}

1186
	ret = hclgevf_bind_ring_to_vector(handle, false, vector_id, ring_chain);
1187
	if (ret)
1188 1189 1190 1191
		dev_err(&handle->pdev->dev,
			"Unmap ring from vector fail. vector=%d, ret =%d\n",
			vector_id,
			ret);
1192 1193 1194 1195 1196 1197 1198

	return ret;
}

static int hclgevf_put_vector(struct hnae3_handle *handle, int vector)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1199 1200 1201 1202 1203 1204 1205 1206 1207
	int vector_id;

	vector_id = hclgevf_get_vector_index(hdev, vector);
	if (vector_id < 0) {
		dev_err(&handle->pdev->dev,
			"hclgevf_put_vector get vector index fail. ret =%d\n",
			vector_id);
		return vector_id;
	}
1208

1209
	hclgevf_free_vector(hdev, vector_id);
1210 1211 1212 1213

	return 0;
}

1214
static int hclgevf_cmd_set_promisc_mode(struct hclgevf_dev *hdev,
1215
					bool en_uc_pmc, bool en_mc_pmc,
1216
					bool en_bc_pmc)
1217
{
1218
	struct hnae3_handle *handle = &hdev->nic;
1219
	struct hclge_vf_to_pf_msg send_msg;
1220
	int ret;
1221

1222 1223 1224 1225 1226
	memset(&send_msg, 0, sizeof(send_msg));
	send_msg.code = HCLGE_MBX_SET_PROMISC_MODE;
	send_msg.en_bc = en_bc_pmc ? 1 : 0;
	send_msg.en_uc = en_uc_pmc ? 1 : 0;
	send_msg.en_mc = en_mc_pmc ? 1 : 0;
1227 1228
	send_msg.en_limit_promisc = test_bit(HNAE3_PFLAG_LIMIT_PROMISC,
					     &handle->priv_flags) ? 1 : 0;
1229 1230

	ret = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
1231
	if (ret)
1232
		dev_err(&hdev->pdev->dev,
1233
			"Set promisc mode fail, status is %d.\n", ret);
1234

1235
	return ret;
1236 1237
}

1238 1239
static int hclgevf_set_promisc_mode(struct hnae3_handle *handle, bool en_uc_pmc,
				    bool en_mc_pmc)
1240
{
1241 1242 1243
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	bool en_bc_pmc;

1244
	en_bc_pmc = hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2;
1245 1246 1247

	return hclgevf_cmd_set_promisc_mode(hdev, en_uc_pmc, en_mc_pmc,
					    en_bc_pmc);
1248 1249
}

1250 1251 1252 1253 1254
static void hclgevf_request_update_promisc_mode(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	set_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state);
1255
	hclgevf_task_schedule(hdev, 0);
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
}

static void hclgevf_sync_promisc_mode(struct hclgevf_dev *hdev)
{
	struct hnae3_handle *handle = &hdev->nic;
	bool en_uc_pmc = handle->netdev_flags & HNAE3_UPE;
	bool en_mc_pmc = handle->netdev_flags & HNAE3_MPE;
	int ret;

	if (test_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state)) {
		ret = hclgevf_set_promisc_mode(handle, en_uc_pmc, en_mc_pmc);
		if (!ret)
			clear_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state);
	}
}

1272 1273
static int hclgevf_tqp_enable_cmd_send(struct hclgevf_dev *hdev, u16 tqp_id,
				       u16 stream_id, bool enable)
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
{
	struct hclgevf_cfg_com_tqp_queue_cmd *req;
	struct hclgevf_desc desc;

	req = (struct hclgevf_cfg_com_tqp_queue_cmd *)desc.data;

	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_CFG_COM_TQP_QUEUE,
				     false);
	req->tqp_id = cpu_to_le16(tqp_id & HCLGEVF_RING_ID_MASK);
	req->stream_id = cpu_to_le16(stream_id);
1284 1285
	if (enable)
		req->enable |= 1U << HCLGEVF_TQP_ENABLE_B;
1286

1287 1288
	return hclgevf_cmd_send(&hdev->hw, &desc, 1);
}
1289

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
static int hclgevf_tqp_enable(struct hnae3_handle *handle, bool enable)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	int ret;
	u16 i;

	for (i = 0; i < handle->kinfo.num_tqps; i++) {
		ret = hclgevf_tqp_enable_cmd_send(hdev, i, 0, enable);
		if (ret)
			return ret;
	}

	return 0;
1303 1304 1305 1306
}

static void hclgevf_reset_tqp_stats(struct hnae3_handle *handle)
{
1307
	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
1308 1309 1310
	struct hclgevf_tqp *tqp;
	int i;

1311 1312
	for (i = 0; i < kinfo->num_tqps; i++) {
		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
1313 1314 1315 1316
		memset(&tqp->tqp_stats, 0, sizeof(tqp->tqp_stats));
	}
}

1317 1318
static int hclgevf_get_host_mac_addr(struct hclgevf_dev *hdev, u8 *p)
{
1319
	struct hclge_vf_to_pf_msg send_msg;
1320 1321 1322
	u8 host_mac[ETH_ALEN];
	int status;

1323 1324 1325
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_MAC_ADDR, 0);
	status = hclgevf_send_mbx_msg(hdev, &send_msg, true, host_mac,
				      ETH_ALEN);
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	if (status) {
		dev_err(&hdev->pdev->dev,
			"fail to get VF MAC from host %d", status);
		return status;
	}

	ether_addr_copy(p, host_mac);

	return 0;
}

1337 1338 1339
static void hclgevf_get_mac_addr(struct hnae3_handle *handle, u8 *p)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1340
	u8 host_mac_addr[ETH_ALEN];
1341

1342 1343 1344 1345 1346 1347 1348 1349
	if (hclgevf_get_host_mac_addr(hdev, host_mac_addr))
		return;

	hdev->has_pf_mac = !is_zero_ether_addr(host_mac_addr);
	if (hdev->has_pf_mac)
		ether_addr_copy(p, host_mac_addr);
	else
		ether_addr_copy(p, hdev->hw.mac.mac_addr);
1350 1351
}

1352
static int hclgevf_set_mac_addr(struct hnae3_handle *handle, const void *p,
1353
				bool is_first)
1354 1355 1356
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	u8 *old_mac_addr = (u8 *)hdev->hw.mac.mac_addr;
1357
	struct hclge_vf_to_pf_msg send_msg;
1358 1359 1360
	u8 *new_mac_addr = (u8 *)p;
	int status;

1361
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_UNICAST, 0);
1362
	send_msg.subcode = HCLGE_MBX_MAC_VLAN_UC_MODIFY;
1363
	ether_addr_copy(send_msg.data, new_mac_addr);
1364 1365 1366 1367
	if (is_first && !hdev->has_pf_mac)
		eth_zero_addr(&send_msg.data[ETH_ALEN]);
	else
		ether_addr_copy(&send_msg.data[ETH_ALEN], old_mac_addr);
1368
	status = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1369 1370 1371 1372 1373 1374
	if (!status)
		ether_addr_copy(hdev->hw.mac.mac_addr, new_mac_addr);

	return status;
}

1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
static struct hclgevf_mac_addr_node *
hclgevf_find_mac_node(struct list_head *list, const u8 *mac_addr)
{
	struct hclgevf_mac_addr_node *mac_node, *tmp;

	list_for_each_entry_safe(mac_node, tmp, list, node)
		if (ether_addr_equal(mac_addr, mac_node->mac_addr))
			return mac_node;

	return NULL;
}

static void hclgevf_update_mac_node(struct hclgevf_mac_addr_node *mac_node,
				    enum HCLGEVF_MAC_NODE_STATE state)
{
	switch (state) {
	/* from set_rx_mode or tmp_add_list */
	case HCLGEVF_MAC_TO_ADD:
		if (mac_node->state == HCLGEVF_MAC_TO_DEL)
			mac_node->state = HCLGEVF_MAC_ACTIVE;
		break;
	/* only from set_rx_mode */
	case HCLGEVF_MAC_TO_DEL:
		if (mac_node->state == HCLGEVF_MAC_TO_ADD) {
			list_del(&mac_node->node);
			kfree(mac_node);
		} else {
			mac_node->state = HCLGEVF_MAC_TO_DEL;
		}
		break;
	/* only from tmp_add_list, the mac_node->state won't be
	 * HCLGEVF_MAC_ACTIVE
	 */
	case HCLGEVF_MAC_ACTIVE:
		if (mac_node->state == HCLGEVF_MAC_TO_ADD)
			mac_node->state = HCLGEVF_MAC_ACTIVE;
		break;
	}
}

static int hclgevf_update_mac_list(struct hnae3_handle *handle,
				   enum HCLGEVF_MAC_NODE_STATE state,
				   enum HCLGEVF_MAC_ADDR_TYPE mac_type,
				   const unsigned char *addr)
1419 1420
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1421 1422
	struct hclgevf_mac_addr_node *mac_node;
	struct list_head *list;
1423

1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
	list = (mac_type == HCLGEVF_MAC_ADDR_UC) ?
	       &hdev->mac_table.uc_mac_list : &hdev->mac_table.mc_mac_list;

	spin_lock_bh(&hdev->mac_table.mac_list_lock);

	/* if the mac addr is already in the mac list, no need to add a new
	 * one into it, just check the mac addr state, convert it to a new
	 * new state, or just remove it, or do nothing.
	 */
	mac_node = hclgevf_find_mac_node(list, addr);
	if (mac_node) {
		hclgevf_update_mac_node(mac_node, state);
		spin_unlock_bh(&hdev->mac_table.mac_list_lock);
		return 0;
	}
	/* if this address is never added, unnecessary to delete */
	if (state == HCLGEVF_MAC_TO_DEL) {
		spin_unlock_bh(&hdev->mac_table.mac_list_lock);
		return -ENOENT;
	}

	mac_node = kzalloc(sizeof(*mac_node), GFP_ATOMIC);
	if (!mac_node) {
		spin_unlock_bh(&hdev->mac_table.mac_list_lock);
		return -ENOMEM;
	}

	mac_node->state = state;
	ether_addr_copy(mac_node->mac_addr, addr);
	list_add_tail(&mac_node->node, list);

	spin_unlock_bh(&hdev->mac_table.mac_list_lock);
	return 0;
}

static int hclgevf_add_uc_addr(struct hnae3_handle *handle,
			       const unsigned char *addr)
{
	return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_ADD,
				       HCLGEVF_MAC_ADDR_UC, addr);
1464 1465 1466 1467 1468
}

static int hclgevf_rm_uc_addr(struct hnae3_handle *handle,
			      const unsigned char *addr)
{
1469 1470
	return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_DEL,
				       HCLGEVF_MAC_ADDR_UC, addr);
1471 1472 1473 1474 1475
}

static int hclgevf_add_mc_addr(struct hnae3_handle *handle,
			       const unsigned char *addr)
{
1476 1477
	return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_ADD,
				       HCLGEVF_MAC_ADDR_MC, addr);
1478 1479 1480 1481 1482
}

static int hclgevf_rm_mc_addr(struct hnae3_handle *handle,
			      const unsigned char *addr)
{
1483 1484 1485 1486 1487 1488 1489 1490
	return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_DEL,
				       HCLGEVF_MAC_ADDR_MC, addr);
}

static int hclgevf_add_del_mac_addr(struct hclgevf_dev *hdev,
				    struct hclgevf_mac_addr_node *mac_node,
				    enum HCLGEVF_MAC_ADDR_TYPE mac_type)
{
1491
	struct hclge_vf_to_pf_msg send_msg;
1492
	u8 code, subcode;
1493

1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
	if (mac_type == HCLGEVF_MAC_ADDR_UC) {
		code = HCLGE_MBX_SET_UNICAST;
		if (mac_node->state == HCLGEVF_MAC_TO_ADD)
			subcode = HCLGE_MBX_MAC_VLAN_UC_ADD;
		else
			subcode = HCLGE_MBX_MAC_VLAN_UC_REMOVE;
	} else {
		code = HCLGE_MBX_SET_MULTICAST;
		if (mac_node->state == HCLGEVF_MAC_TO_ADD)
			subcode = HCLGE_MBX_MAC_VLAN_MC_ADD;
		else
			subcode = HCLGE_MBX_MAC_VLAN_MC_REMOVE;
	}

	hclgevf_build_send_msg(&send_msg, code, subcode);
	ether_addr_copy(send_msg.data, mac_node->mac_addr);
1510
	return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
1511 1512
}

1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
static void hclgevf_config_mac_list(struct hclgevf_dev *hdev,
				    struct list_head *list,
				    enum HCLGEVF_MAC_ADDR_TYPE mac_type)
{
	struct hclgevf_mac_addr_node *mac_node, *tmp;
	int ret;

	list_for_each_entry_safe(mac_node, tmp, list, node) {
		ret = hclgevf_add_del_mac_addr(hdev, mac_node, mac_type);
		if  (ret) {
			dev_err(&hdev->pdev->dev,
				"failed to configure mac %pM, state = %d, ret = %d\n",
				mac_node->mac_addr, mac_node->state, ret);
			return;
		}
		if (mac_node->state == HCLGEVF_MAC_TO_ADD) {
			mac_node->state = HCLGEVF_MAC_ACTIVE;
		} else {
			list_del(&mac_node->node);
			kfree(mac_node);
		}
	}
}

static void hclgevf_sync_from_add_list(struct list_head *add_list,
				       struct list_head *mac_list)
{
	struct hclgevf_mac_addr_node *mac_node, *tmp, *new_node;

	list_for_each_entry_safe(mac_node, tmp, add_list, node) {
		/* if the mac address from tmp_add_list is not in the
		 * uc/mc_mac_list, it means have received a TO_DEL request
		 * during the time window of sending mac config request to PF
		 * If mac_node state is ACTIVE, then change its state to TO_DEL,
		 * then it will be removed at next time. If is TO_ADD, it means
		 * send TO_ADD request failed, so just remove the mac node.
		 */
		new_node = hclgevf_find_mac_node(mac_list, mac_node->mac_addr);
		if (new_node) {
			hclgevf_update_mac_node(new_node, mac_node->state);
			list_del(&mac_node->node);
			kfree(mac_node);
		} else if (mac_node->state == HCLGEVF_MAC_ACTIVE) {
			mac_node->state = HCLGEVF_MAC_TO_DEL;
1557
			list_move_tail(&mac_node->node, mac_list);
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
		} else {
			list_del(&mac_node->node);
			kfree(mac_node);
		}
	}
}

static void hclgevf_sync_from_del_list(struct list_head *del_list,
				       struct list_head *mac_list)
{
	struct hclgevf_mac_addr_node *mac_node, *tmp, *new_node;

	list_for_each_entry_safe(mac_node, tmp, del_list, node) {
		new_node = hclgevf_find_mac_node(mac_list, mac_node->mac_addr);
		if (new_node) {
			/* If the mac addr is exist in the mac list, it means
			 * received a new request TO_ADD during the time window
			 * of sending mac addr configurrequest to PF, so just
			 * change the mac state to ACTIVE.
			 */
			new_node->state = HCLGEVF_MAC_ACTIVE;
			list_del(&mac_node->node);
			kfree(mac_node);
		} else {
1582
			list_move_tail(&mac_node->node, mac_list);
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
		}
	}
}

static void hclgevf_clear_list(struct list_head *list)
{
	struct hclgevf_mac_addr_node *mac_node, *tmp;

	list_for_each_entry_safe(mac_node, tmp, list, node) {
		list_del(&mac_node->node);
		kfree(mac_node);
	}
}

static void hclgevf_sync_mac_list(struct hclgevf_dev *hdev,
				  enum HCLGEVF_MAC_ADDR_TYPE mac_type)
{
	struct hclgevf_mac_addr_node *mac_node, *tmp, *new_node;
	struct list_head tmp_add_list, tmp_del_list;
	struct list_head *list;

	INIT_LIST_HEAD(&tmp_add_list);
	INIT_LIST_HEAD(&tmp_del_list);

	/* move the mac addr to the tmp_add_list and tmp_del_list, then
	 * we can add/delete these mac addr outside the spin lock
	 */
	list = (mac_type == HCLGEVF_MAC_ADDR_UC) ?
		&hdev->mac_table.uc_mac_list : &hdev->mac_table.mc_mac_list;

	spin_lock_bh(&hdev->mac_table.mac_list_lock);

	list_for_each_entry_safe(mac_node, tmp, list, node) {
		switch (mac_node->state) {
		case HCLGEVF_MAC_TO_DEL:
1618
			list_move_tail(&mac_node->node, &tmp_del_list);
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
			break;
		case HCLGEVF_MAC_TO_ADD:
			new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
			if (!new_node)
				goto stop_traverse;

			ether_addr_copy(new_node->mac_addr, mac_node->mac_addr);
			new_node->state = mac_node->state;
			list_add_tail(&new_node->node, &tmp_add_list);
			break;
		default:
			break;
		}
	}

stop_traverse:
	spin_unlock_bh(&hdev->mac_table.mac_list_lock);

	/* delete first, in order to get max mac table space for adding */
	hclgevf_config_mac_list(hdev, &tmp_del_list, mac_type);
	hclgevf_config_mac_list(hdev, &tmp_add_list, mac_type);

	/* if some mac addresses were added/deleted fail, move back to the
	 * mac_list, and retry at next time.
	 */
	spin_lock_bh(&hdev->mac_table.mac_list_lock);

	hclgevf_sync_from_del_list(&tmp_del_list, list);
	hclgevf_sync_from_add_list(&tmp_add_list, list);

	spin_unlock_bh(&hdev->mac_table.mac_list_lock);
}

static void hclgevf_sync_mac_table(struct hclgevf_dev *hdev)
{
	hclgevf_sync_mac_list(hdev, HCLGEVF_MAC_ADDR_UC);
	hclgevf_sync_mac_list(hdev, HCLGEVF_MAC_ADDR_MC);
}

static void hclgevf_uninit_mac_list(struct hclgevf_dev *hdev)
{
	spin_lock_bh(&hdev->mac_table.mac_list_lock);

	hclgevf_clear_list(&hdev->mac_table.uc_mac_list);
	hclgevf_clear_list(&hdev->mac_table.mc_mac_list);

	spin_unlock_bh(&hdev->mac_table.mac_list_lock);
}

1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
static int hclgevf_enable_vlan_filter(struct hnae3_handle *handle, bool enable)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
	struct hclge_vf_to_pf_msg send_msg;

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

	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
			       HCLGE_MBX_ENABLE_VLAN_FILTER);
	send_msg.data[0] = enable ? 1 : 0;

	return hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
}

1684 1685 1686 1687
static int hclgevf_set_vlan_filter(struct hnae3_handle *handle,
				   __be16 proto, u16 vlan_id,
				   bool is_kill)
{
1688 1689 1690 1691
#define HCLGEVF_VLAN_MBX_IS_KILL_OFFSET	0
#define HCLGEVF_VLAN_MBX_VLAN_ID_OFFSET	1
#define HCLGEVF_VLAN_MBX_PROTO_OFFSET	3

1692
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1693
	struct hclge_vf_to_pf_msg send_msg;
1694
	int ret;
1695

1696
	if (vlan_id > HCLGEVF_MAX_VLAN_ID)
1697 1698 1699 1700 1701
		return -EINVAL;

	if (proto != htons(ETH_P_8021Q))
		return -EPROTONOSUPPORT;

1702 1703
	/* When device is resetting or reset failed, firmware is unable to
	 * handle mailbox. Just record the vlan id, and remove it after
1704 1705
	 * reset finished.
	 */
1706 1707
	if ((test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state) ||
	     test_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state)) && is_kill) {
1708 1709 1710 1711
		set_bit(vlan_id, hdev->vlan_del_fail_bmap);
		return -EBUSY;
	}

1712 1713 1714 1715 1716 1717 1718
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
			       HCLGE_MBX_VLAN_FILTER);
	send_msg.data[HCLGEVF_VLAN_MBX_IS_KILL_OFFSET] = is_kill;
	memcpy(&send_msg.data[HCLGEVF_VLAN_MBX_VLAN_ID_OFFSET], &vlan_id,
	       sizeof(vlan_id));
	memcpy(&send_msg.data[HCLGEVF_VLAN_MBX_PROTO_OFFSET], &proto,
	       sizeof(proto));
G
Guojia Liao 已提交
1719
	/* when remove hw vlan filter failed, record the vlan id,
1720 1721 1722
	 * and try to remove it from hw later, to be consistence
	 * with stack.
	 */
1723
	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
	if (is_kill && ret)
		set_bit(vlan_id, hdev->vlan_del_fail_bmap);

	return ret;
}

static void hclgevf_sync_vlan_filter(struct hclgevf_dev *hdev)
{
#define HCLGEVF_MAX_SYNC_COUNT	60
	struct hnae3_handle *handle = &hdev->nic;
	int ret, sync_cnt = 0;
	u16 vlan_id;

	vlan_id = find_first_bit(hdev->vlan_del_fail_bmap, VLAN_N_VID);
	while (vlan_id != VLAN_N_VID) {
		ret = hclgevf_set_vlan_filter(handle, htons(ETH_P_8021Q),
					      vlan_id, true);
		if (ret)
			return;

		clear_bit(vlan_id, hdev->vlan_del_fail_bmap);
		sync_cnt++;
		if (sync_cnt >= HCLGEVF_MAX_SYNC_COUNT)
			return;

		vlan_id = find_first_bit(hdev->vlan_del_fail_bmap, VLAN_N_VID);
	}
1751 1752
}

1753 1754 1755
static int hclgevf_en_hw_strip_rxvtag(struct hnae3_handle *handle, bool enable)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1756
	struct hclge_vf_to_pf_msg send_msg;
1757

1758 1759 1760 1761
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
			       HCLGE_MBX_VLAN_RX_OFF_CFG);
	send_msg.data[0] = enable ? 1 : 0;
	return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
1762 1763
}

1764
static int hclgevf_reset_tqp(struct hnae3_handle *handle)
1765
{
1766
#define HCLGEVF_RESET_ALL_QUEUE_DONE	1U
1767
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1768
	struct hclge_vf_to_pf_msg send_msg;
1769
	u8 return_status = 0;
1770
	int ret;
1771
	u16 i;
1772

1773
	/* disable vf queue before send queue reset msg to PF */
1774 1775 1776 1777
	ret = hclgevf_tqp_enable(handle, false);
	if (ret) {
		dev_err(&hdev->pdev->dev, "failed to disable tqp, ret = %d\n",
			ret);
1778
		return ret;
1779
	}
1780

1781
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_QUEUE_RESET, 0);
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, &return_status,
				   sizeof(return_status));
	if (ret || return_status == HCLGEVF_RESET_ALL_QUEUE_DONE)
		return ret;

	for (i = 1; i < handle->kinfo.num_tqps; i++) {
		hclgevf_build_send_msg(&send_msg, HCLGE_MBX_QUEUE_RESET, 0);
		memcpy(send_msg.data, &i, sizeof(i));
		ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
		if (ret)
			return ret;
	}

	return 0;
1797 1798
}

1799 1800 1801
static int hclgevf_set_mtu(struct hnae3_handle *handle, int new_mtu)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1802
	struct hclge_vf_to_pf_msg send_msg;
1803

1804 1805 1806
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_MTU, 0);
	memcpy(send_msg.data, &new_mtu, sizeof(new_mtu));
	return hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1807 1808
}

1809 1810 1811 1812 1813
static int hclgevf_notify_client(struct hclgevf_dev *hdev,
				 enum hnae3_reset_notify_type type)
{
	struct hnae3_client *client = hdev->nic_client;
	struct hnae3_handle *handle = &hdev->nic;
1814
	int ret;
1815

1816 1817 1818 1819
	if (!test_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state) ||
	    !client)
		return 0;

1820 1821 1822
	if (!client->ops->reset_notify)
		return -EOPNOTSUPP;

1823 1824 1825 1826 1827 1828
	ret = client->ops->reset_notify(handle, type);
	if (ret)
		dev_err(&hdev->pdev->dev, "notify nic client failed %d(%d)\n",
			type, ret);

	return ret;
1829 1830
}

1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
static int hclgevf_notify_roce_client(struct hclgevf_dev *hdev,
				      enum hnae3_reset_notify_type type)
{
	struct hnae3_client *client = hdev->roce_client;
	struct hnae3_handle *handle = &hdev->roce;
	int ret;

	if (!test_bit(HCLGEVF_STATE_ROCE_REGISTERED, &hdev->state) || !client)
		return 0;

	if (!client->ops->reset_notify)
		return -EOPNOTSUPP;

	ret = client->ops->reset_notify(handle, type);
	if (ret)
		dev_err(&hdev->pdev->dev, "notify roce client failed %d(%d)",
			type, ret);
	return ret;
}

1851 1852
static int hclgevf_reset_wait(struct hclgevf_dev *hdev)
{
1853 1854 1855 1856 1857 1858 1859
#define HCLGEVF_RESET_WAIT_US	20000
#define HCLGEVF_RESET_WAIT_CNT	2000
#define HCLGEVF_RESET_WAIT_TIMEOUT_US	\
	(HCLGEVF_RESET_WAIT_US * HCLGEVF_RESET_WAIT_CNT)

	u32 val;
	int ret;
1860

1861
	if (hdev->reset_type == HNAE3_VF_RESET)
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
		ret = readl_poll_timeout(hdev->hw.io_base +
					 HCLGEVF_VF_RST_ING, val,
					 !(val & HCLGEVF_VF_RST_ING_BIT),
					 HCLGEVF_RESET_WAIT_US,
					 HCLGEVF_RESET_WAIT_TIMEOUT_US);
	else
		ret = readl_poll_timeout(hdev->hw.io_base +
					 HCLGEVF_RST_ING, val,
					 !(val & HCLGEVF_RST_ING_BITS),
					 HCLGEVF_RESET_WAIT_US,
					 HCLGEVF_RESET_WAIT_TIMEOUT_US);
1873 1874

	/* hardware completion status should be available by this time */
1875 1876
	if (ret) {
		dev_err(&hdev->pdev->dev,
1877
			"couldn't get reset done status from h/w, timeout!\n");
1878
		return ret;
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
	}

	/* we will wait a bit more to let reset of the stack to complete. This
	 * might happen in case reset assertion was made by PF. Yes, this also
	 * means we might end up waiting bit more even for VF reset.
	 */
	msleep(5000);

	return 0;
}

1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
static void hclgevf_reset_handshake(struct hclgevf_dev *hdev, bool enable)
{
	u32 reg_val;

	reg_val = hclgevf_read_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG);
	if (enable)
		reg_val |= HCLGEVF_NIC_SW_RST_RDY;
	else
		reg_val &= ~HCLGEVF_NIC_SW_RST_RDY;

	hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG,
			  reg_val);
}

1904 1905
static int hclgevf_reset_stack(struct hclgevf_dev *hdev)
{
1906 1907
	int ret;

1908
	/* uninitialize the nic client */
1909 1910 1911
	ret = hclgevf_notify_client(hdev, HNAE3_UNINIT_CLIENT);
	if (ret)
		return ret;
1912

1913
	/* re-initialize the hclge device */
1914
	ret = hclgevf_reset_hdev(hdev);
1915 1916 1917 1918 1919
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"hclge device re-init failed, VF is disabled!\n");
		return ret;
	}
1920 1921

	/* bring up the nic client again */
1922 1923 1924
	ret = hclgevf_notify_client(hdev, HNAE3_INIT_CLIENT);
	if (ret)
		return ret;
1925

1926 1927 1928
	/* clear handshake status with IMP */
	hclgevf_reset_handshake(hdev, false);

1929 1930
	/* bring up the nic to enable TX/RX again */
	return hclgevf_notify_client(hdev, HNAE3_UP_CLIENT);
1931 1932
}

1933 1934
static int hclgevf_reset_prepare_wait(struct hclgevf_dev *hdev)
{
1935 1936
#define HCLGEVF_RESET_SYNC_TIME 100

1937
	if (hdev->reset_type == HNAE3_VF_FUNC_RESET) {
1938 1939 1940
		struct hclge_vf_to_pf_msg send_msg;
		int ret;

1941 1942
		hclgevf_build_send_msg(&send_msg, HCLGE_MBX_RESET, 0);
		ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1943 1944 1945 1946 1947
		if (ret) {
			dev_err(&hdev->pdev->dev,
				"failed to assert VF reset, ret = %d\n", ret);
			return ret;
		}
1948
		hdev->rst_stats.vf_func_rst_cnt++;
1949 1950
	}

1951
	set_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state);
1952 1953
	/* inform hardware that preparatory work is done */
	msleep(HCLGEVF_RESET_SYNC_TIME);
1954
	hclgevf_reset_handshake(hdev, true);
1955 1956
	dev_info(&hdev->pdev->dev, "prepare reset(%d) wait done\n",
		 hdev->reset_type);
1957

1958
	return 0;
1959 1960
}

1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
static void hclgevf_dump_rst_info(struct hclgevf_dev *hdev)
{
	dev_info(&hdev->pdev->dev, "VF function reset count: %u\n",
		 hdev->rst_stats.vf_func_rst_cnt);
	dev_info(&hdev->pdev->dev, "FLR reset count: %u\n",
		 hdev->rst_stats.flr_rst_cnt);
	dev_info(&hdev->pdev->dev, "VF reset count: %u\n",
		 hdev->rst_stats.vf_rst_cnt);
	dev_info(&hdev->pdev->dev, "reset done count: %u\n",
		 hdev->rst_stats.rst_done_cnt);
	dev_info(&hdev->pdev->dev, "HW reset done count: %u\n",
		 hdev->rst_stats.hw_rst_done_cnt);
	dev_info(&hdev->pdev->dev, "reset count: %u\n",
		 hdev->rst_stats.rst_cnt);
	dev_info(&hdev->pdev->dev, "reset fail count: %u\n",
		 hdev->rst_stats.rst_fail_cnt);
	dev_info(&hdev->pdev->dev, "vector0 interrupt enable status: 0x%x\n",
		 hclgevf_read_dev(&hdev->hw, HCLGEVF_MISC_VECTOR_REG_BASE));
	dev_info(&hdev->pdev->dev, "vector0 interrupt status: 0x%x\n",
1980
		 hclgevf_read_dev(&hdev->hw, HCLGEVF_VECTOR0_CMDQ_STATE_REG));
1981
	dev_info(&hdev->pdev->dev, "handshake status: 0x%x\n",
1982
		 hclgevf_read_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG));
1983 1984 1985 1986 1987
	dev_info(&hdev->pdev->dev, "function reset status: 0x%x\n",
		 hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING));
	dev_info(&hdev->pdev->dev, "hdev state: 0x%lx\n", hdev->state);
}

1988 1989
static void hclgevf_reset_err_handle(struct hclgevf_dev *hdev)
{
1990 1991
	/* recover handshake status with IMP when reset fail */
	hclgevf_reset_handshake(hdev, true);
1992
	hdev->rst_stats.rst_fail_cnt++;
1993
	dev_err(&hdev->pdev->dev, "failed to reset VF(%u)\n",
1994 1995 1996 1997 1998 1999 2000 2001
		hdev->rst_stats.rst_fail_cnt);

	if (hdev->rst_stats.rst_fail_cnt < HCLGEVF_RESET_MAX_FAIL_CNT)
		set_bit(hdev->reset_type, &hdev->reset_pending);

	if (hclgevf_is_reset_pending(hdev)) {
		set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
		hclgevf_reset_task_schedule(hdev);
2002
	} else {
2003
		set_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state);
2004
		hclgevf_dump_rst_info(hdev);
2005 2006 2007
	}
}

2008
static int hclgevf_reset_prepare(struct hclgevf_dev *hdev)
2009 2010 2011
{
	int ret;

2012
	hdev->rst_stats.rst_cnt++;
2013

2014 2015 2016 2017 2018
	/* perform reset of the stack & ae device for a client */
	ret = hclgevf_notify_roce_client(hdev, HNAE3_DOWN_CLIENT);
	if (ret)
		return ret;

2019
	rtnl_lock();
2020
	/* bring down the nic to stop any ongoing TX/RX */
2021
	ret = hclgevf_notify_client(hdev, HNAE3_DOWN_CLIENT);
2022
	rtnl_unlock();
2023
	if (ret)
2024
		return ret;
2025

2026 2027 2028 2029 2030 2031
	return hclgevf_reset_prepare_wait(hdev);
}

static int hclgevf_reset_rebuild(struct hclgevf_dev *hdev)
{
	int ret;
2032

2033
	hdev->rst_stats.hw_rst_done_cnt++;
2034 2035 2036
	ret = hclgevf_notify_roce_client(hdev, HNAE3_UNINIT_CLIENT);
	if (ret)
		return ret;
2037

2038
	rtnl_lock();
G
Guojia Liao 已提交
2039
	/* now, re-initialize the nic client and ae device */
2040
	ret = hclgevf_reset_stack(hdev);
2041
	rtnl_unlock();
2042
	if (ret) {
2043
		dev_err(&hdev->pdev->dev, "failed to reset VF stack\n");
2044
		return ret;
2045
	}
2046

2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
	ret = hclgevf_notify_roce_client(hdev, HNAE3_INIT_CLIENT);
	/* ignore RoCE notify error if it fails HCLGEVF_RESET_MAX_FAIL_CNT - 1
	 * times
	 */
	if (ret &&
	    hdev->rst_stats.rst_fail_cnt < HCLGEVF_RESET_MAX_FAIL_CNT - 1)
		return ret;

	ret = hclgevf_notify_roce_client(hdev, HNAE3_UP_CLIENT);
	if (ret)
		return ret;

2059
	hdev->last_reset_time = jiffies;
2060
	hdev->rst_stats.rst_done_cnt++;
2061
	hdev->rst_stats.rst_fail_cnt = 0;
2062
	clear_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state);
2063

2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
	return 0;
}

static void hclgevf_reset(struct hclgevf_dev *hdev)
{
	if (hclgevf_reset_prepare(hdev))
		goto err_reset;

	/* check if VF could successfully fetch the hardware reset completion
	 * status from the hardware
	 */
	if (hclgevf_reset_wait(hdev)) {
		/* can't do much in this situation, will disable VF */
		dev_err(&hdev->pdev->dev,
			"failed to fetch H/W reset completion status\n");
		goto err_reset;
	}

	if (hclgevf_reset_rebuild(hdev))
		goto err_reset;

	return;

2087
err_reset:
2088
	hclgevf_reset_err_handle(hdev);
2089 2090
}

2091 2092 2093 2094 2095
static enum hnae3_reset_type hclgevf_get_reset_level(struct hclgevf_dev *hdev,
						     unsigned long *addr)
{
	enum hnae3_reset_type rst_level = HNAE3_NONE_RESET;

2096
	/* return the highest priority reset level amongst all */
2097 2098 2099 2100 2101 2102
	if (test_bit(HNAE3_VF_RESET, addr)) {
		rst_level = HNAE3_VF_RESET;
		clear_bit(HNAE3_VF_RESET, addr);
		clear_bit(HNAE3_VF_PF_FUNC_RESET, addr);
		clear_bit(HNAE3_VF_FUNC_RESET, addr);
	} else if (test_bit(HNAE3_VF_FULL_RESET, addr)) {
2103 2104 2105
		rst_level = HNAE3_VF_FULL_RESET;
		clear_bit(HNAE3_VF_FULL_RESET, addr);
		clear_bit(HNAE3_VF_FUNC_RESET, addr);
2106 2107 2108 2109
	} else if (test_bit(HNAE3_VF_PF_FUNC_RESET, addr)) {
		rst_level = HNAE3_VF_PF_FUNC_RESET;
		clear_bit(HNAE3_VF_PF_FUNC_RESET, addr);
		clear_bit(HNAE3_VF_FUNC_RESET, addr);
2110 2111 2112
	} else if (test_bit(HNAE3_VF_FUNC_RESET, addr)) {
		rst_level = HNAE3_VF_FUNC_RESET;
		clear_bit(HNAE3_VF_FUNC_RESET, addr);
2113 2114 2115
	} else if (test_bit(HNAE3_FLR_RESET, addr)) {
		rst_level = HNAE3_FLR_RESET;
		clear_bit(HNAE3_FLR_RESET, addr);
2116 2117 2118 2119 2120
	}

	return rst_level;
}

2121 2122
static void hclgevf_reset_event(struct pci_dev *pdev,
				struct hnae3_handle *handle)
2123
{
2124 2125
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
	struct hclgevf_dev *hdev = ae_dev->priv;
2126 2127 2128

	dev_info(&hdev->pdev->dev, "received reset request from VF enet\n");

2129
	if (hdev->default_reset_request)
2130
		hdev->reset_level =
2131 2132 2133
			hclgevf_get_reset_level(hdev,
						&hdev->default_reset_request);
	else
2134
		hdev->reset_level = HNAE3_VF_FUNC_RESET;
2135

2136 2137 2138
	/* reset of this VF requested */
	set_bit(HCLGEVF_RESET_REQUESTED, &hdev->reset_state);
	hclgevf_reset_task_schedule(hdev);
2139

2140
	hdev->last_reset_time = jiffies;
2141 2142
}

2143 2144 2145 2146 2147 2148 2149 2150
static void hclgevf_set_def_reset_request(struct hnae3_ae_dev *ae_dev,
					  enum hnae3_reset_type rst_type)
{
	struct hclgevf_dev *hdev = ae_dev->priv;

	set_bit(rst_type, &hdev->default_reset_request);
}

2151 2152 2153 2154 2155
static void hclgevf_enable_vector(struct hclgevf_misc_vector *vector, bool en)
{
	writel(en ? 1 : 0, vector->addr);
}

2156 2157
static void hclgevf_reset_prepare_general(struct hnae3_ae_dev *ae_dev,
					  enum hnae3_reset_type rst_type)
2158
{
2159 2160
#define HCLGEVF_RESET_RETRY_WAIT_MS	500
#define HCLGEVF_RESET_RETRY_CNT		5
2161

2162
	struct hclgevf_dev *hdev = ae_dev->priv;
2163 2164
	int retry_cnt = 0;
	int ret;
2165

2166 2167 2168
retry:
	down(&hdev->reset_sem);
	set_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2169
	hdev->reset_type = rst_type;
2170 2171
	ret = hclgevf_reset_prepare(hdev);
	if (ret) {
2172
		dev_err(&hdev->pdev->dev, "fail to prepare to reset, ret=%d\n",
2173 2174
			ret);
		if (hdev->reset_pending ||
2175
		    retry_cnt++ < HCLGEVF_RESET_RETRY_CNT) {
2176 2177 2178 2179 2180
			dev_err(&hdev->pdev->dev,
				"reset_pending:0x%lx, retry_cnt:%d\n",
				hdev->reset_pending, retry_cnt);
			clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
			up(&hdev->reset_sem);
2181
			msleep(HCLGEVF_RESET_RETRY_WAIT_MS);
2182 2183 2184
			goto retry;
		}
	}
2185

2186
	/* disable misc vector before reset done */
2187
	hclgevf_enable_vector(&hdev->misc_vector, false);
2188 2189 2190

	if (hdev->reset_type == HNAE3_FLR_RESET)
		hdev->rst_stats.flr_rst_cnt++;
2191
}
2192

2193
static void hclgevf_reset_done(struct hnae3_ae_dev *ae_dev)
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
{
	struct hclgevf_dev *hdev = ae_dev->priv;
	int ret;

	hclgevf_enable_vector(&hdev->misc_vector, true);

	ret = hclgevf_reset_rebuild(hdev);
	if (ret)
		dev_warn(&hdev->pdev->dev, "fail to rebuild, ret=%d\n",
			 ret);

	hdev->reset_type = HNAE3_NONE_RESET;
	clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
	up(&hdev->reset_sem);
2208 2209
}

2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
static u32 hclgevf_get_fw_version(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	return hdev->fw_version;
}

static void hclgevf_get_misc_vector(struct hclgevf_dev *hdev)
{
	struct hclgevf_misc_vector *vector = &hdev->misc_vector;

	vector->vector_irq = pci_irq_vector(hdev->pdev,
					    HCLGEVF_MISC_VECTOR_NUM);
	vector->addr = hdev->hw.io_base + HCLGEVF_MISC_VECTOR_REG_BASE;
	/* vector status always valid for Vector 0 */
	hdev->vector_status[HCLGEVF_MISC_VECTOR_NUM] = 0;
	hdev->vector_irq[HCLGEVF_MISC_VECTOR_NUM] = vector->vector_irq;

	hdev->num_msi_left -= 1;
	hdev->num_msi_used += 1;
}

2232 2233
void hclgevf_reset_task_schedule(struct hclgevf_dev *hdev)
{
2234
	if (!test_bit(HCLGEVF_STATE_REMOVING, &hdev->state) &&
2235
	    test_bit(HCLGEVF_STATE_SERVICE_INITED, &hdev->state) &&
2236 2237
	    !test_and_set_bit(HCLGEVF_STATE_RST_SERVICE_SCHED,
			      &hdev->state))
2238
		mod_delayed_work(hclgevf_wq, &hdev->service_task, 0);
2239 2240
}

2241
void hclgevf_mbx_task_schedule(struct hclgevf_dev *hdev)
2242
{
2243 2244 2245
	if (!test_bit(HCLGEVF_STATE_REMOVING, &hdev->state) &&
	    !test_and_set_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED,
			      &hdev->state))
2246
		mod_delayed_work(hclgevf_wq, &hdev->service_task, 0);
2247 2248
}

2249 2250
static void hclgevf_task_schedule(struct hclgevf_dev *hdev,
				  unsigned long delay)
2251
{
2252 2253
	if (!test_bit(HCLGEVF_STATE_REMOVING, &hdev->state) &&
	    !test_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state))
2254
		mod_delayed_work(hclgevf_wq, &hdev->service_task, delay);
2255 2256
}

2257
static void hclgevf_reset_service_task(struct hclgevf_dev *hdev)
2258
{
2259 2260
#define	HCLGEVF_MAX_RESET_ATTEMPTS_CNT	3

2261
	if (!test_and_clear_bit(HCLGEVF_STATE_RST_SERVICE_SCHED, &hdev->state))
2262 2263
		return;

2264 2265
	down(&hdev->reset_sem);
	set_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2266

2267 2268
	if (test_and_clear_bit(HCLGEVF_RESET_PENDING,
			       &hdev->reset_state)) {
2269
		/* PF has intimated that it is about to reset the hardware.
2270 2271 2272
		 * We now have to poll & check if hardware has actually
		 * completed the reset sequence. On hardware reset completion,
		 * VF needs to reset the client and ae device.
2273 2274 2275
		 */
		hdev->reset_attempts = 0;

2276
		hdev->last_reset_time = jiffies;
2277 2278 2279
		hdev->reset_type =
			hclgevf_get_reset_level(hdev, &hdev->reset_pending);
		if (hdev->reset_type != HNAE3_NONE_RESET)
2280
			hclgevf_reset(hdev);
2281 2282 2283
	} else if (test_and_clear_bit(HCLGEVF_RESET_REQUESTED,
				      &hdev->reset_state)) {
		/* we could be here when either of below happens:
2284
		 * 1. reset was initiated due to watchdog timeout caused by
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
		 *    a. IMP was earlier reset and our TX got choked down and
		 *       which resulted in watchdog reacting and inducing VF
		 *       reset. This also means our cmdq would be unreliable.
		 *    b. problem in TX due to other lower layer(example link
		 *       layer not functioning properly etc.)
		 * 2. VF reset might have been initiated due to some config
		 *    change.
		 *
		 * NOTE: Theres no clear way to detect above cases than to react
		 * to the response of PF for this reset request. PF will ack the
		 * 1b and 2. cases but we will not get any intimation about 1a
		 * from PF as cmdq would be in unreliable state i.e. mailbox
		 * communication between PF and VF would be broken.
G
Guojia Liao 已提交
2298 2299
		 *
		 * if we are never geting into pending state it means either:
2300 2301 2302 2303 2304 2305
		 * 1. PF is not receiving our request which could be due to IMP
		 *    reset
		 * 2. PF is screwed
		 * We cannot do much for 2. but to check first we can try reset
		 * our PCIe + stack and see if it alleviates the problem.
		 */
2306
		if (hdev->reset_attempts > HCLGEVF_MAX_RESET_ATTEMPTS_CNT) {
2307
			/* prepare for full reset of stack + pcie interface */
2308
			set_bit(HNAE3_VF_FULL_RESET, &hdev->reset_pending);
2309 2310 2311 2312 2313 2314

			/* "defer" schedule the reset task again */
			set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
		} else {
			hdev->reset_attempts++;

2315 2316
			set_bit(hdev->reset_level, &hdev->reset_pending);
			set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
2317
		}
2318
		hclgevf_reset_task_schedule(hdev);
2319
	}
2320

2321
	hdev->reset_type = HNAE3_NONE_RESET;
2322
	clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2323
	up(&hdev->reset_sem);
2324 2325
}

2326
static void hclgevf_mailbox_service_task(struct hclgevf_dev *hdev)
2327
{
2328 2329
	if (!test_and_clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state))
		return;
2330 2331 2332 2333

	if (test_and_set_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state))
		return;

2334
	hclgevf_mbx_async_handler(hdev);
2335 2336 2337 2338

	clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state);
}

2339
static void hclgevf_keep_alive(struct hclgevf_dev *hdev)
2340
{
2341
	struct hclge_vf_to_pf_msg send_msg;
2342 2343
	int ret;

2344
	if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state))
2345 2346
		return;

2347 2348
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_KEEP_ALIVE, 0);
	ret = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
2349 2350 2351 2352 2353
	if (ret)
		dev_err(&hdev->pdev->dev,
			"VF sends keep alive cmd failed(=%d)\n", ret);
}

2354
static void hclgevf_periodic_service_task(struct hclgevf_dev *hdev)
2355
{
2356 2357
	unsigned long delta = round_jiffies_relative(HZ);
	struct hnae3_handle *handle = &hdev->nic;
2358

2359 2360 2361
	if (test_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state))
		return;

2362 2363
	if (time_is_after_jiffies(hdev->last_serv_processed + HZ)) {
		delta = jiffies - hdev->last_serv_processed;
2364

2365 2366 2367 2368
		if (delta < round_jiffies_relative(HZ)) {
			delta = round_jiffies_relative(HZ) - delta;
			goto out;
		}
2369
	}
2370

2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
	hdev->serv_processed_cnt++;
	if (!(hdev->serv_processed_cnt % HCLGEVF_KEEP_ALIVE_TASK_INTERVAL))
		hclgevf_keep_alive(hdev);

	if (test_bit(HCLGEVF_STATE_DOWN, &hdev->state)) {
		hdev->last_serv_processed = jiffies;
		goto out;
	}

	if (!(hdev->serv_processed_cnt % HCLGEVF_STATS_TIMER_INTERVAL))
		hclgevf_tqps_update_stats(handle);

2383 2384
	/* VF does not need to request link status when this bit is set, because
	 * PF will push its link status to VFs when link status changed.
2385
	 */
2386 2387
	if (!test_bit(HCLGEVF_STATE_PF_PUSH_LINK_STATUS, &hdev->state))
		hclgevf_request_link_info(hdev);
2388

2389 2390
	hclgevf_update_link_mode(hdev);

2391 2392
	hclgevf_sync_vlan_filter(hdev);

2393 2394
	hclgevf_sync_mac_table(hdev);

2395 2396
	hclgevf_sync_promisc_mode(hdev);

2397
	hdev->last_serv_processed = jiffies;
2398

2399 2400 2401 2402 2403 2404 2405 2406
out:
	hclgevf_task_schedule(hdev, delta);
}

static void hclgevf_service_task(struct work_struct *work)
{
	struct hclgevf_dev *hdev = container_of(work, struct hclgevf_dev,
						service_task.work);
2407

2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
	hclgevf_reset_service_task(hdev);
	hclgevf_mailbox_service_task(hdev);
	hclgevf_periodic_service_task(hdev);

	/* Handle reset and mbx again in case periodical task delays the
	 * handling by calling hclgevf_task_schedule() in
	 * hclgevf_periodic_service_task()
	 */
	hclgevf_reset_service_task(hdev);
	hclgevf_mailbox_service_task(hdev);
2418 2419 2420 2421 2422 2423 2424
}

static void hclgevf_clear_event_cause(struct hclgevf_dev *hdev, u32 regclr)
{
	hclgevf_write_dev(&hdev->hw, HCLGEVF_VECTOR0_CMDQ_SRC_REG, regclr);
}

2425 2426
static enum hclgevf_evt_cause hclgevf_check_evt_cause(struct hclgevf_dev *hdev,
						      u32 *clearval)
2427
{
2428
	u32 val, cmdq_stat_reg, rst_ing_reg;
2429 2430

	/* fetch the events from their corresponding regs */
2431
	cmdq_stat_reg = hclgevf_read_dev(&hdev->hw,
2432
					 HCLGEVF_VECTOR0_CMDQ_STATE_REG);
2433
	if (BIT(HCLGEVF_VECTOR0_RST_INT_B) & cmdq_stat_reg) {
2434 2435 2436 2437 2438
		rst_ing_reg = hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING);
		dev_info(&hdev->pdev->dev,
			 "receive reset interrupt 0x%x!\n", rst_ing_reg);
		set_bit(HNAE3_VF_RESET, &hdev->reset_pending);
		set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
2439
		set_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state);
2440
		*clearval = ~(1U << HCLGEVF_VECTOR0_RST_INT_B);
2441
		hdev->rst_stats.vf_rst_cnt++;
2442 2443 2444 2445 2446 2447
		/* set up VF hardware reset status, its PF will clear
		 * this status when PF has initialized done.
		 */
		val = hclgevf_read_dev(&hdev->hw, HCLGEVF_VF_RST_ING);
		hclgevf_write_dev(&hdev->hw, HCLGEVF_VF_RST_ING,
				  val | HCLGEVF_VF_RST_ING_BIT);
2448 2449 2450
		return HCLGEVF_VECTOR0_EVENT_RST;
	}

2451
	/* check for vector0 mailbox(=CMDQ RX) event source */
2452 2453 2454 2455 2456 2457 2458 2459
	if (BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B) & cmdq_stat_reg) {
		/* for revision 0x21, clearing interrupt is writing bit 0
		 * to the clear register, writing bit 1 means to keep the
		 * old value.
		 * for revision 0x20, the clear register is a read & write
		 * register, so we should just write 0 to the bit we are
		 * handling, and keep other bits as cmdq_stat_reg.
		 */
2460
		if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2)
2461 2462 2463 2464 2465
			*clearval = ~(1U << HCLGEVF_VECTOR0_RX_CMDQ_INT_B);
		else
			*clearval = cmdq_stat_reg &
				    ~BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B);

2466
		return HCLGEVF_VECTOR0_EVENT_MBX;
2467 2468
	}

2469 2470 2471 2472
	/* print other vector0 event source */
	dev_info(&hdev->pdev->dev,
		 "vector 0 interrupt from unknown source, cmdq_src = %#x\n",
		 cmdq_stat_reg);
2473

2474
	return HCLGEVF_VECTOR0_EVENT_OTHER;
2475 2476 2477 2478
}

static irqreturn_t hclgevf_misc_irq_handle(int irq, void *data)
{
2479
	enum hclgevf_evt_cause event_cause;
2480 2481 2482 2483
	struct hclgevf_dev *hdev = data;
	u32 clearval;

	hclgevf_enable_vector(&hdev->misc_vector, false);
2484
	event_cause = hclgevf_check_evt_cause(hdev, &clearval);
2485 2486
	if (event_cause != HCLGEVF_VECTOR0_EVENT_OTHER)
		hclgevf_clear_event_cause(hdev, clearval);
2487

2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
	switch (event_cause) {
	case HCLGEVF_VECTOR0_EVENT_RST:
		hclgevf_reset_task_schedule(hdev);
		break;
	case HCLGEVF_VECTOR0_EVENT_MBX:
		hclgevf_mbx_handler(hdev);
		break;
	default:
		break;
	}
2498

2499
	if (event_cause != HCLGEVF_VECTOR0_EVENT_OTHER)
2500
		hclgevf_enable_vector(&hdev->misc_vector, true);
2501 2502 2503 2504 2505 2506 2507 2508

	return IRQ_HANDLED;
}

static int hclgevf_configure(struct hclgevf_dev *hdev)
{
	int ret;

2509 2510
	hdev->gro_en = true;

2511 2512 2513 2514
	ret = hclgevf_get_basic_info(hdev);
	if (ret)
		return ret;

2515 2516 2517 2518 2519
	/* get current port based vlan state from PF */
	ret = hclgevf_get_port_base_vlan_filter_state(hdev);
	if (ret)
		return ret;

2520
	/* get queue configuration from PF */
2521
	ret = hclgevf_get_queue_info(hdev);
2522 2523
	if (ret)
		return ret;
2524 2525 2526 2527 2528 2529

	/* get queue depth info from PF */
	ret = hclgevf_get_queue_depth(hdev);
	if (ret)
		return ret;

2530
	return hclgevf_get_pf_media_type(hdev);
2531 2532
}

2533 2534 2535
static int hclgevf_alloc_hdev(struct hnae3_ae_dev *ae_dev)
{
	struct pci_dev *pdev = ae_dev->pdev;
2536
	struct hclgevf_dev *hdev;
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548

	hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
	if (!hdev)
		return -ENOMEM;

	hdev->pdev = pdev;
	hdev->ae_dev = ae_dev;
	ae_dev->priv = hdev;

	return 0;
}

2549 2550 2551 2552 2553
static int hclgevf_init_roce_base_info(struct hclgevf_dev *hdev)
{
	struct hnae3_handle *roce = &hdev->roce;
	struct hnae3_handle *nic = &hdev->nic;

2554
	roce->rinfo.num_vectors = hdev->num_roce_msix;
2555 2556 2557 2558 2559

	if (hdev->num_msi_left < roce->rinfo.num_vectors ||
	    hdev->num_msi_left == 0)
		return -EINVAL;

2560
	roce->rinfo.base_vector = hdev->roce_base_msix_offset;
2561 2562 2563

	roce->rinfo.netdev = nic->kinfo.netdev;
	roce->rinfo.roce_io_base = hdev->hw.io_base;
2564
	roce->rinfo.roce_mem_base = hdev->hw.mem_base;
2565 2566 2567 2568 2569 2570 2571 2572

	roce->pdev = nic->pdev;
	roce->ae_algo = nic->ae_algo;
	roce->numa_node_mask = nic->numa_node_mask;

	return 0;
}

2573
static int hclgevf_config_gro(struct hclgevf_dev *hdev)
2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
{
	struct hclgevf_cfg_gro_status_cmd *req;
	struct hclgevf_desc desc;
	int ret;

	if (!hnae3_dev_gro_supported(hdev))
		return 0;

	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_GRO_GENERIC_CONFIG,
				     false);
	req = (struct hclgevf_cfg_gro_status_cmd *)desc.data;

2586
	req->gro_en = hdev->gro_en ? 1 : 0;
2587 2588 2589 2590 2591 2592 2593 2594 2595

	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
	if (ret)
		dev_err(&hdev->pdev->dev,
			"VF GRO hardware config cmd failed, ret = %d.\n", ret);

	return ret;
}

2596
static int hclgevf_rss_init_cfg(struct hclgevf_dev *hdev)
2597
{
2598
	u16 rss_ind_tbl_size = hdev->ae_dev->dev_specs.rss_ind_tbl_size;
2599
	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
2600
	struct hclgevf_rss_tuple_cfg *tuple_sets;
2601
	u32 i;
2602

2603
	rss_cfg->hash_algo = HCLGEVF_RSS_HASH_ALGO_TOEPLITZ;
2604
	rss_cfg->rss_size = hdev->nic.kinfo.rss_size;
2605
	tuple_sets = &rss_cfg->rss_tuple_sets;
2606
	if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
2607 2608
		u8 *rss_ind_tbl;

2609
		rss_cfg->hash_algo = HCLGEVF_RSS_HASH_ALGO_SIMPLE;
2610 2611 2612 2613 2614 2615 2616

		rss_ind_tbl = devm_kcalloc(&hdev->pdev->dev, rss_ind_tbl_size,
					   sizeof(*rss_ind_tbl), GFP_KERNEL);
		if (!rss_ind_tbl)
			return -ENOMEM;

		rss_cfg->rss_indirection_tbl = rss_ind_tbl;
2617 2618
		memcpy(rss_cfg->rss_hash_key, hclgevf_hash_key,
		       HCLGEVF_RSS_KEY_SIZE);
2619

2620 2621 2622 2623 2624 2625
		tuple_sets->ipv4_tcp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
		tuple_sets->ipv4_udp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
		tuple_sets->ipv4_sctp_en = HCLGEVF_RSS_INPUT_TUPLE_SCTP;
		tuple_sets->ipv4_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
		tuple_sets->ipv6_tcp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
		tuple_sets->ipv6_udp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2626 2627 2628 2629
		tuple_sets->ipv6_sctp_en =
			hdev->ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2 ?
					HCLGEVF_RSS_INPUT_TUPLE_SCTP_NO_PORT :
					HCLGEVF_RSS_INPUT_TUPLE_SCTP;
2630 2631 2632 2633
		tuple_sets->ipv6_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
	}

	/* Initialize RSS indirect table */
2634
	for (i = 0; i < rss_ind_tbl_size; i++)
2635
		rss_cfg->rss_indirection_tbl[i] = i % rss_cfg->rss_size;
2636 2637

	return 0;
2638 2639 2640 2641 2642 2643 2644
}

static int hclgevf_rss_init_hw(struct hclgevf_dev *hdev)
{
	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
	int ret;

2645
	if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
2646 2647 2648 2649
		ret = hclgevf_set_rss_algo_key(hdev, rss_cfg->hash_algo,
					       rss_cfg->rss_hash_key);
		if (ret)
			return ret;
2650 2651 2652 2653

		ret = hclgevf_set_rss_input_tuple(hdev, rss_cfg);
		if (ret)
			return ret;
2654 2655
	}

2656 2657 2658 2659
	ret = hclgevf_set_rss_indir_table(hdev);
	if (ret)
		return ret;

2660
	return hclgevf_set_rss_tc_mode(hdev, rss_cfg->rss_size);
2661 2662 2663 2664
}

static int hclgevf_init_vlan_config(struct hclgevf_dev *hdev)
{
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
	struct hnae3_handle *nic = &hdev->nic;
	int ret;

	ret = hclgevf_en_hw_strip_rxvtag(nic, true);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"failed to enable rx vlan offload, ret = %d\n", ret);
		return ret;
	}

2675 2676 2677 2678
	return hclgevf_set_vlan_filter(&hdev->nic, htons(ETH_P_8021Q), 0,
				       false);
}

2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
static void hclgevf_flush_link_update(struct hclgevf_dev *hdev)
{
#define HCLGEVF_FLUSH_LINK_TIMEOUT	100000

	unsigned long last = hdev->serv_processed_cnt;
	int i = 0;

	while (test_bit(HCLGEVF_STATE_LINK_UPDATING, &hdev->state) &&
	       i++ < HCLGEVF_FLUSH_LINK_TIMEOUT &&
	       last == hdev->serv_processed_cnt)
		usleep_range(1, 1);
}

2692 2693 2694 2695 2696
static void hclgevf_set_timer_task(struct hnae3_handle *handle, bool enable)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	if (enable) {
2697
		hclgevf_task_schedule(hdev, 0);
2698
	} else {
2699
		set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2700 2701 2702 2703

		/* flush memory to make sure DOWN is seen by service task */
		smp_mb__before_atomic();
		hclgevf_flush_link_update(hdev);
2704 2705 2706
	}
}

2707 2708 2709 2710
static int hclgevf_ae_start(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

2711
	clear_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2712
	clear_bit(HCLGEVF_STATE_PF_PUSH_LINK_STATUS, &hdev->state);
2713

2714 2715 2716 2717
	hclgevf_reset_tqp_stats(handle);

	hclgevf_request_link_info(hdev);

2718 2719
	hclgevf_update_link_mode(hdev);

2720 2721 2722 2723 2724 2725 2726
	return 0;
}

static void hclgevf_ae_stop(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

2727 2728
	set_bit(HCLGEVF_STATE_DOWN, &hdev->state);

2729
	if (hdev->reset_type != HNAE3_VF_RESET)
2730
		hclgevf_reset_tqp(handle);
2731

2732
	hclgevf_reset_tqp_stats(handle);
2733
	hclgevf_update_link_status(hdev, 0);
2734 2735
}

2736 2737
static int hclgevf_set_alive(struct hnae3_handle *handle, bool alive)
{
2738 2739 2740
#define HCLGEVF_STATE_ALIVE	1
#define HCLGEVF_STATE_NOT_ALIVE	0

2741
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2742
	struct hclge_vf_to_pf_msg send_msg;
2743

2744 2745 2746 2747
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_ALIVE, 0);
	send_msg.data[0] = alive ? HCLGEVF_STATE_ALIVE :
				HCLGEVF_STATE_NOT_ALIVE;
	return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
2748 2749 2750 2751
}

static int hclgevf_client_start(struct hnae3_handle *handle)
{
2752
	return hclgevf_set_alive(handle, true);
2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
}

static void hclgevf_client_stop(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	int ret;

	ret = hclgevf_set_alive(handle, false);
	if (ret)
		dev_warn(&hdev->pdev->dev,
			 "%s failed %d\n", __func__, ret);
}

2766 2767 2768 2769
static void hclgevf_state_init(struct hclgevf_dev *hdev)
{
	clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state);
	clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state);
2770
	clear_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state);
2771

2772
	INIT_DELAYED_WORK(&hdev->service_task, hclgevf_service_task);
2773

2774
	mutex_init(&hdev->mbx_resp.mbx_mutex);
2775
	sema_init(&hdev->reset_sem, 1);
2776

2777 2778 2779 2780
	spin_lock_init(&hdev->mac_table.mac_list_lock);
	INIT_LIST_HEAD(&hdev->mac_table.uc_mac_list);
	INIT_LIST_HEAD(&hdev->mac_table.mc_mac_list);

2781 2782 2783 2784 2785 2786 2787
	/* bring the device down */
	set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
}

static void hclgevf_state_uninit(struct hclgevf_dev *hdev)
{
	set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2788
	set_bit(HCLGEVF_STATE_REMOVING, &hdev->state);
2789

2790 2791
	if (hdev->service_task.work.func)
		cancel_delayed_work_sync(&hdev->service_task);
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801

	mutex_destroy(&hdev->mbx_resp.mbx_mutex);
}

static int hclgevf_init_msi(struct hclgevf_dev *hdev)
{
	struct pci_dev *pdev = hdev->pdev;
	int vectors;
	int i;

2802
	if (hnae3_dev_roce_supported(hdev))
2803 2804 2805 2806 2807
		vectors = pci_alloc_irq_vectors(pdev,
						hdev->roce_base_msix_offset + 1,
						hdev->num_msi,
						PCI_IRQ_MSIX);
	else
2808 2809
		vectors = pci_alloc_irq_vectors(pdev, HNAE3_MIN_VECTOR_NUM,
						hdev->num_msi,
2810
						PCI_IRQ_MSI | PCI_IRQ_MSIX);
2811 2812 2813 2814 2815 2816 2817 2818 2819

	if (vectors < 0) {
		dev_err(&pdev->dev,
			"failed(%d) to allocate MSI/MSI-X vectors\n",
			vectors);
		return vectors;
	}
	if (vectors < hdev->num_msi)
		dev_warn(&hdev->pdev->dev,
2820
			 "requested %u MSI/MSI-X, but allocated %d MSI/MSI-X\n",
2821 2822 2823 2824
			 hdev->num_msi, vectors);

	hdev->num_msi = vectors;
	hdev->num_msi_left = vectors;
2825

2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
	hdev->vector_status = devm_kcalloc(&pdev->dev, hdev->num_msi,
					   sizeof(u16), GFP_KERNEL);
	if (!hdev->vector_status) {
		pci_free_irq_vectors(pdev);
		return -ENOMEM;
	}

	for (i = 0; i < hdev->num_msi; i++)
		hdev->vector_status[i] = HCLGEVF_INVALID_VPORT;

	hdev->vector_irq = devm_kcalloc(&pdev->dev, hdev->num_msi,
					sizeof(int), GFP_KERNEL);
	if (!hdev->vector_irq) {
2839
		devm_kfree(&pdev->dev, hdev->vector_status);
2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
		pci_free_irq_vectors(pdev);
		return -ENOMEM;
	}

	return 0;
}

static void hclgevf_uninit_msi(struct hclgevf_dev *hdev)
{
	struct pci_dev *pdev = hdev->pdev;

2851 2852
	devm_kfree(&pdev->dev, hdev->vector_status);
	devm_kfree(&pdev->dev, hdev->vector_irq);
2853 2854 2855 2856 2857
	pci_free_irq_vectors(pdev);
}

static int hclgevf_misc_irq_init(struct hclgevf_dev *hdev)
{
2858
	int ret;
2859 2860 2861

	hclgevf_get_misc_vector(hdev);

2862 2863
	snprintf(hdev->misc_vector.name, HNAE3_INT_NAME_LEN, "%s-misc-%s",
		 HCLGEVF_NAME, pci_name(hdev->pdev));
2864
	ret = request_irq(hdev->misc_vector.vector_irq, hclgevf_misc_irq_handle,
2865
			  0, hdev->misc_vector.name, hdev);
2866 2867 2868 2869 2870 2871
	if (ret) {
		dev_err(&hdev->pdev->dev, "VF failed to request misc irq(%d)\n",
			hdev->misc_vector.vector_irq);
		return ret;
	}

2872 2873
	hclgevf_clear_event_cause(hdev, 0);

2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
	/* enable misc. vector(vector 0) */
	hclgevf_enable_vector(&hdev->misc_vector, true);

	return ret;
}

static void hclgevf_misc_irq_uninit(struct hclgevf_dev *hdev)
{
	/* disable misc vector(vector 0) */
	hclgevf_enable_vector(&hdev->misc_vector, false);
2884
	synchronize_irq(hdev->misc_vector.vector_irq);
2885 2886 2887 2888
	free_irq(hdev->misc_vector.vector_irq, hdev);
	hclgevf_free_vector(hdev, 0);
}

2889 2890 2891 2892 2893 2894
static void hclgevf_info_show(struct hclgevf_dev *hdev)
{
	struct device *dev = &hdev->pdev->dev;

	dev_info(dev, "VF info begin:\n");

2895 2896 2897 2898 2899 2900
	dev_info(dev, "Task queue pairs numbers: %u\n", hdev->num_tqps);
	dev_info(dev, "Desc num per TX queue: %u\n", hdev->num_tx_desc);
	dev_info(dev, "Desc num per RX queue: %u\n", hdev->num_rx_desc);
	dev_info(dev, "Numbers of vports: %u\n", hdev->num_alloc_vport);
	dev_info(dev, "HW tc map: 0x%x\n", hdev->hw_tc_map);
	dev_info(dev, "PF media type of this VF: %u\n",
2901 2902 2903 2904 2905
		 hdev->hw.mac.media_type);

	dev_info(dev, "VF info end.\n");
}

2906 2907 2908 2909
static int hclgevf_init_nic_client_instance(struct hnae3_ae_dev *ae_dev,
					    struct hnae3_client *client)
{
	struct hclgevf_dev *hdev = ae_dev->priv;
2910
	int rst_cnt = hdev->rst_stats.rst_cnt;
2911 2912 2913 2914 2915 2916 2917
	int ret;

	ret = client->ops->init_instance(&hdev->nic);
	if (ret)
		return ret;

	set_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state);
2918 2919 2920 2921 2922 2923 2924 2925
	if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state) ||
	    rst_cnt != hdev->rst_stats.rst_cnt) {
		clear_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state);

		client->ops->uninit_instance(&hdev->nic, 0);
		return -EBUSY;
	}

2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951
	hnae3_set_client_init_flag(client, ae_dev, 1);

	if (netif_msg_drv(&hdev->nic))
		hclgevf_info_show(hdev);

	return 0;
}

static int hclgevf_init_roce_client_instance(struct hnae3_ae_dev *ae_dev,
					     struct hnae3_client *client)
{
	struct hclgevf_dev *hdev = ae_dev->priv;
	int ret;

	if (!hnae3_dev_roce_supported(hdev) || !hdev->roce_client ||
	    !hdev->nic_client)
		return 0;

	ret = hclgevf_init_roce_base_info(hdev);
	if (ret)
		return ret;

	ret = client->ops->init_instance(&hdev->roce);
	if (ret)
		return ret;

2952
	set_bit(HCLGEVF_STATE_ROCE_REGISTERED, &hdev->state);
2953 2954 2955 2956 2957
	hnae3_set_client_init_flag(client, ae_dev, 1);

	return 0;
}

2958 2959
static int hclgevf_init_client_instance(struct hnae3_client *client,
					struct hnae3_ae_dev *ae_dev)
2960
{
2961
	struct hclgevf_dev *hdev = ae_dev->priv;
2962 2963 2964 2965 2966 2967 2968
	int ret;

	switch (client->type) {
	case HNAE3_CLIENT_KNIC:
		hdev->nic_client = client;
		hdev->nic.client = client;

2969
		ret = hclgevf_init_nic_client_instance(ae_dev, client);
2970
		if (ret)
2971
			goto clear_nic;
2972

2973 2974 2975 2976
		ret = hclgevf_init_roce_client_instance(ae_dev,
							hdev->roce_client);
		if (ret)
			goto clear_roce;
2977

2978 2979
		break;
	case HNAE3_CLIENT_ROCE:
2980 2981 2982 2983
		if (hnae3_dev_roce_supported(hdev)) {
			hdev->roce_client = client;
			hdev->roce.client = client;
		}
2984

2985 2986 2987
		ret = hclgevf_init_roce_client_instance(ae_dev, client);
		if (ret)
			goto clear_roce;
2988

2989 2990 2991
		break;
	default:
		return -EINVAL;
2992 2993 2994
	}

	return 0;
2995 2996 2997 2998 2999 3000 3001 3002 3003

clear_nic:
	hdev->nic_client = NULL;
	hdev->nic.client = NULL;
	return ret;
clear_roce:
	hdev->roce_client = NULL;
	hdev->roce.client = NULL;
	return ret;
3004 3005
}

3006 3007
static void hclgevf_uninit_client_instance(struct hnae3_client *client,
					   struct hnae3_ae_dev *ae_dev)
3008
{
3009 3010
	struct hclgevf_dev *hdev = ae_dev->priv;

3011
	/* un-init roce, if it exists */
3012
	if (hdev->roce_client) {
3013
		clear_bit(HCLGEVF_STATE_ROCE_REGISTERED, &hdev->state);
3014
		hdev->roce_client->ops->uninit_instance(&hdev->roce, 0);
3015 3016 3017
		hdev->roce_client = NULL;
		hdev->roce.client = NULL;
	}
3018 3019

	/* un-init nic/unic, if this was not called by roce client */
3020 3021
	if (client->ops->uninit_instance && hdev->nic_client &&
	    client->type != HNAE3_CLIENT_ROCE) {
3022 3023
		clear_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state);

3024
		client->ops->uninit_instance(&hdev->nic, 0);
3025 3026 3027
		hdev->nic_client = NULL;
		hdev->nic.client = NULL;
	}
3028 3029
}

3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045
static int hclgevf_dev_mem_map(struct hclgevf_dev *hdev)
{
#define HCLGEVF_MEM_BAR		4

	struct pci_dev *pdev = hdev->pdev;
	struct hclgevf_hw *hw = &hdev->hw;

	/* for device does not have device memory, return directly */
	if (!(pci_select_bars(pdev, IORESOURCE_MEM) & BIT(HCLGEVF_MEM_BAR)))
		return 0;

	hw->mem_base = devm_ioremap_wc(&pdev->dev,
				       pci_resource_start(pdev,
							  HCLGEVF_MEM_BAR),
				       pci_resource_len(pdev, HCLGEVF_MEM_BAR));
	if (!hw->mem_base) {
3046
		dev_err(&pdev->dev, "failed to map device memory\n");
3047 3048 3049 3050 3051 3052
		return -EFAULT;
	}

	return 0;
}

3053 3054 3055 3056 3057 3058 3059 3060 3061
static int hclgevf_pci_init(struct hclgevf_dev *hdev)
{
	struct pci_dev *pdev = hdev->pdev;
	struct hclgevf_hw *hw;
	int ret;

	ret = pci_enable_device(pdev);
	if (ret) {
		dev_err(&pdev->dev, "failed to enable PCI device\n");
3062
		return ret;
3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079
	}

	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
	if (ret) {
		dev_err(&pdev->dev, "can't set consistent PCI DMA, exiting");
		goto err_disable_device;
	}

	ret = pci_request_regions(pdev, HCLGEVF_DRIVER_NAME);
	if (ret) {
		dev_err(&pdev->dev, "PCI request regions failed %d\n", ret);
		goto err_disable_device;
	}

	pci_set_master(pdev);
	hw = &hdev->hw;
	hw->hdev = hdev;
P
Peng Li 已提交
3080
	hw->io_base = pci_iomap(pdev, 2, 0);
3081 3082 3083 3084 3085 3086
	if (!hw->io_base) {
		dev_err(&pdev->dev, "can't map configuration register space\n");
		ret = -ENOMEM;
		goto err_clr_master;
	}

3087 3088 3089 3090
	ret = hclgevf_dev_mem_map(hdev);
	if (ret)
		goto err_unmap_io_base;

3091 3092
	return 0;

3093 3094
err_unmap_io_base:
	pci_iounmap(pdev, hdev->hw.io_base);
3095 3096 3097 3098 3099
err_clr_master:
	pci_clear_master(pdev);
	pci_release_regions(pdev);
err_disable_device:
	pci_disable_device(pdev);
3100

3101 3102 3103 3104 3105 3106 3107
	return ret;
}

static void hclgevf_pci_uninit(struct hclgevf_dev *hdev)
{
	struct pci_dev *pdev = hdev->pdev;

3108 3109 3110
	if (hdev->hw.mem_base)
		devm_iounmap(&pdev->dev, hdev->hw.mem_base);

3111 3112 3113 3114 3115 3116
	pci_iounmap(pdev, hdev->hw.io_base);
	pci_clear_master(pdev);
	pci_release_regions(pdev);
	pci_disable_device(pdev);
}

3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
static int hclgevf_query_vf_resource(struct hclgevf_dev *hdev)
{
	struct hclgevf_query_res_cmd *req;
	struct hclgevf_desc desc;
	int ret;

	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_VF_RSRC, true);
	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"query vf resource failed, ret = %d.\n", ret);
		return ret;
	}

	req = (struct hclgevf_query_res_cmd *)desc.data;

3133
	if (hnae3_dev_roce_supported(hdev)) {
3134
		hdev->roce_base_msix_offset =
3135
		hnae3_get_field(le16_to_cpu(req->msixcap_localid_ba_rocee),
3136 3137 3138
				HCLGEVF_MSIX_OFT_ROCEE_M,
				HCLGEVF_MSIX_OFT_ROCEE_S);
		hdev->num_roce_msix =
3139
		hnae3_get_field(le16_to_cpu(req->vf_intr_vector_number),
3140 3141
				HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S);

3142 3143 3144
		/* nic's msix numbers is always equals to the roce's. */
		hdev->num_nic_msix = hdev->num_roce_msix;

3145 3146 3147 3148 3149 3150 3151
		/* VF should have NIC vectors and Roce vectors, NIC vectors
		 * are queued before Roce vectors. The offset is fixed to 64.
		 */
		hdev->num_msi = hdev->num_roce_msix +
				hdev->roce_base_msix_offset;
	} else {
		hdev->num_msi =
3152
		hnae3_get_field(le16_to_cpu(req->vf_intr_vector_number),
3153
				HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S);
3154 3155 3156 3157 3158 3159 3160 3161 3162

		hdev->num_nic_msix = hdev->num_msi;
	}

	if (hdev->num_nic_msix < HNAE3_MIN_VECTOR_NUM) {
		dev_err(&hdev->pdev->dev,
			"Just %u msi resources, not enough for vf(min:2).\n",
			hdev->num_nic_msix);
		return -EINVAL;
3163 3164 3165 3166 3167
	}

	return 0;
}

3168 3169 3170 3171 3172 3173 3174 3175 3176 3177
static void hclgevf_set_default_dev_specs(struct hclgevf_dev *hdev)
{
#define HCLGEVF_MAX_NON_TSO_BD_NUM			8U

	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);

	ae_dev->dev_specs.max_non_tso_bd_num =
					HCLGEVF_MAX_NON_TSO_BD_NUM;
	ae_dev->dev_specs.rss_ind_tbl_size = HCLGEVF_RSS_IND_TBL_SIZE;
	ae_dev->dev_specs.rss_key_size = HCLGEVF_RSS_KEY_SIZE;
3178
	ae_dev->dev_specs.max_int_gl = HCLGEVF_DEF_MAX_INT_GL;
3179
	ae_dev->dev_specs.max_frm_size = HCLGEVF_MAC_MAX_FRAME;
3180 3181 3182 3183 3184 3185 3186
}

static void hclgevf_parse_dev_specs(struct hclgevf_dev *hdev,
				    struct hclgevf_desc *desc)
{
	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
	struct hclgevf_dev_specs_0_cmd *req0;
3187
	struct hclgevf_dev_specs_1_cmd *req1;
3188 3189

	req0 = (struct hclgevf_dev_specs_0_cmd *)desc[0].data;
3190
	req1 = (struct hclgevf_dev_specs_1_cmd *)desc[1].data;
3191 3192 3193 3194

	ae_dev->dev_specs.max_non_tso_bd_num = req0->max_non_tso_bd_num;
	ae_dev->dev_specs.rss_ind_tbl_size =
					le16_to_cpu(req0->rss_ind_tbl_size);
3195
	ae_dev->dev_specs.int_ql_max = le16_to_cpu(req0->int_ql_max);
3196
	ae_dev->dev_specs.rss_key_size = le16_to_cpu(req0->rss_key_size);
3197
	ae_dev->dev_specs.max_int_gl = le16_to_cpu(req1->max_int_gl);
3198
	ae_dev->dev_specs.max_frm_size = le16_to_cpu(req1->max_frm_size);
3199 3200
}

3201 3202 3203 3204 3205 3206 3207 3208 3209 3210
static void hclgevf_check_dev_specs(struct hclgevf_dev *hdev)
{
	struct hnae3_dev_specs *dev_specs = &hdev->ae_dev->dev_specs;

	if (!dev_specs->max_non_tso_bd_num)
		dev_specs->max_non_tso_bd_num = HCLGEVF_MAX_NON_TSO_BD_NUM;
	if (!dev_specs->rss_ind_tbl_size)
		dev_specs->rss_ind_tbl_size = HCLGEVF_RSS_IND_TBL_SIZE;
	if (!dev_specs->rss_key_size)
		dev_specs->rss_key_size = HCLGEVF_RSS_KEY_SIZE;
3211 3212
	if (!dev_specs->max_int_gl)
		dev_specs->max_int_gl = HCLGEVF_DEF_MAX_INT_GL;
3213 3214
	if (!dev_specs->max_frm_size)
		dev_specs->max_frm_size = HCLGEVF_MAC_MAX_FRAME;
3215 3216
}

3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
static int hclgevf_query_dev_specs(struct hclgevf_dev *hdev)
{
	struct hclgevf_desc desc[HCLGEVF_QUERY_DEV_SPECS_BD_NUM];
	int ret;
	int i;

	/* set default specifications as devices lower than version V3 do not
	 * support querying specifications from firmware.
	 */
	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V3) {
		hclgevf_set_default_dev_specs(hdev);
		return 0;
	}

	for (i = 0; i < HCLGEVF_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
		hclgevf_cmd_setup_basic_desc(&desc[i],
					     HCLGEVF_OPC_QUERY_DEV_SPECS, true);
		desc[i].flag |= cpu_to_le16(HCLGEVF_CMD_FLAG_NEXT);
	}
	hclgevf_cmd_setup_basic_desc(&desc[i], HCLGEVF_OPC_QUERY_DEV_SPECS,
				     true);

	ret = hclgevf_cmd_send(&hdev->hw, desc, HCLGEVF_QUERY_DEV_SPECS_BD_NUM);
	if (ret)
		return ret;

	hclgevf_parse_dev_specs(hdev, desc);
3244
	hclgevf_check_dev_specs(hdev);
3245 3246 3247 3248

	return 0;
}

3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283
static int hclgevf_pci_reset(struct hclgevf_dev *hdev)
{
	struct pci_dev *pdev = hdev->pdev;
	int ret = 0;

	if (hdev->reset_type == HNAE3_VF_FULL_RESET &&
	    test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) {
		hclgevf_misc_irq_uninit(hdev);
		hclgevf_uninit_msi(hdev);
		clear_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
	}

	if (!test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) {
		pci_set_master(pdev);
		ret = hclgevf_init_msi(hdev);
		if (ret) {
			dev_err(&pdev->dev,
				"failed(%d) to init MSI/MSI-X\n", ret);
			return ret;
		}

		ret = hclgevf_misc_irq_init(hdev);
		if (ret) {
			hclgevf_uninit_msi(hdev);
			dev_err(&pdev->dev, "failed(%d) to init Misc IRQ(vector0)\n",
				ret);
			return ret;
		}

		set_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
	}

	return ret;
}

3284 3285 3286 3287 3288 3289 3290 3291 3292
static int hclgevf_clear_vport_list(struct hclgevf_dev *hdev)
{
	struct hclge_vf_to_pf_msg send_msg;

	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_HANDLE_VF_TBL,
			       HCLGE_MBX_VPORT_LIST_CLEAR);
	return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
}

3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304
static void hclgevf_init_rxd_adv_layout(struct hclgevf_dev *hdev)
{
	if (hnae3_ae_dev_rxd_adv_layout_supported(hdev->ae_dev))
		hclgevf_write_dev(&hdev->hw, HCLGEVF_RXD_ADV_LAYOUT_EN_REG, 1);
}

static void hclgevf_uninit_rxd_adv_layout(struct hclgevf_dev *hdev)
{
	if (hnae3_ae_dev_rxd_adv_layout_supported(hdev->ae_dev))
		hclgevf_write_dev(&hdev->hw, HCLGEVF_RXD_ADV_LAYOUT_EN_REG, 0);
}

3305
static int hclgevf_reset_hdev(struct hclgevf_dev *hdev)
3306
{
3307
	struct pci_dev *pdev = hdev->pdev;
3308 3309
	int ret;

3310 3311 3312 3313 3314 3315
	ret = hclgevf_pci_reset(hdev);
	if (ret) {
		dev_err(&pdev->dev, "pci reset failed %d\n", ret);
		return ret;
	}

3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328
	ret = hclgevf_cmd_init(hdev);
	if (ret) {
		dev_err(&pdev->dev, "cmd failed %d\n", ret);
		return ret;
	}

	ret = hclgevf_rss_init_hw(hdev);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"failed(%d) to initialize RSS\n", ret);
		return ret;
	}

3329
	ret = hclgevf_config_gro(hdev);
3330 3331 3332
	if (ret)
		return ret;

3333 3334 3335 3336 3337
	ret = hclgevf_init_vlan_config(hdev);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"failed(%d) to initialize VLAN config\n", ret);
		return ret;
3338
	}
3339

3340 3341
	set_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state);

3342 3343
	hclgevf_init_rxd_adv_layout(hdev);

3344 3345 3346 3347 3348 3349 3350 3351 3352 3353
	dev_info(&hdev->pdev->dev, "Reset done\n");

	return 0;
}

static int hclgevf_init_hdev(struct hclgevf_dev *hdev)
{
	struct pci_dev *pdev = hdev->pdev;
	int ret;

3354
	ret = hclgevf_pci_init(hdev);
3355
	if (ret)
3356 3357
		return ret;

3358 3359 3360 3361
	ret = hclgevf_devlink_init(hdev);
	if (ret)
		goto err_devlink_init;

3362
	ret = hclgevf_cmd_queue_init(hdev);
3363
	if (ret)
3364 3365
		goto err_cmd_queue_init;

3366 3367 3368 3369 3370 3371
	ret = hclgevf_cmd_init(hdev);
	if (ret)
		goto err_cmd_init;

	/* Get vf resource */
	ret = hclgevf_query_vf_resource(hdev);
3372
	if (ret)
3373
		goto err_cmd_init;
3374

3375 3376 3377 3378 3379 3380 3381
	ret = hclgevf_query_dev_specs(hdev);
	if (ret) {
		dev_err(&pdev->dev,
			"failed to query dev specifications, ret = %d\n", ret);
		goto err_cmd_init;
	}

3382 3383 3384
	ret = hclgevf_init_msi(hdev);
	if (ret) {
		dev_err(&pdev->dev, "failed(%d) to init MSI/MSI-X\n", ret);
3385
		goto err_cmd_init;
3386 3387 3388
	}

	hclgevf_state_init(hdev);
3389
	hdev->reset_level = HNAE3_VF_FUNC_RESET;
3390
	hdev->reset_type = HNAE3_NONE_RESET;
3391 3392

	ret = hclgevf_misc_irq_init(hdev);
3393
	if (ret)
3394 3395
		goto err_misc_irq_init;

3396 3397
	set_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);

3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410
	ret = hclgevf_configure(hdev);
	if (ret) {
		dev_err(&pdev->dev, "failed(%d) to fetch configuration\n", ret);
		goto err_config;
	}

	ret = hclgevf_alloc_tqps(hdev);
	if (ret) {
		dev_err(&pdev->dev, "failed(%d) to allocate TQPs\n", ret);
		goto err_config;
	}

	ret = hclgevf_set_handle_info(hdev);
3411
	if (ret)
3412 3413
		goto err_config;

3414
	ret = hclgevf_config_gro(hdev);
3415 3416 3417
	if (ret)
		goto err_config;

3418
	/* Initialize RSS for this VF */
3419 3420 3421 3422 3423 3424
	ret = hclgevf_rss_init_cfg(hdev);
	if (ret) {
		dev_err(&pdev->dev, "failed to init rss cfg, ret = %d\n", ret);
		goto err_config;
	}

3425 3426 3427 3428 3429 3430 3431
	ret = hclgevf_rss_init_hw(hdev);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"failed(%d) to initialize RSS\n", ret);
		goto err_config;
	}

3432 3433 3434 3435 3436 3437 3438 3439 3440
	/* ensure vf tbl list as empty before init*/
	ret = hclgevf_clear_vport_list(hdev);
	if (ret) {
		dev_err(&pdev->dev,
			"failed to clear tbl list configuration, ret = %d.\n",
			ret);
		goto err_config;
	}

3441 3442 3443 3444 3445 3446 3447
	ret = hclgevf_init_vlan_config(hdev);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"failed(%d) to initialize VLAN config\n", ret);
		goto err_config;
	}

3448 3449
	hclgevf_init_rxd_adv_layout(hdev);

3450 3451
	set_bit(HCLGEVF_STATE_SERVICE_INITED, &hdev->state);

3452
	hdev->last_reset_time = jiffies;
3453 3454
	dev_info(&hdev->pdev->dev, "finished initializing %s driver\n",
		 HCLGEVF_DRIVER_NAME);
3455

3456 3457
	hclgevf_task_schedule(hdev, round_jiffies_relative(HZ));

3458 3459 3460 3461 3462 3463 3464
	return 0;

err_config:
	hclgevf_misc_irq_uninit(hdev);
err_misc_irq_init:
	hclgevf_state_uninit(hdev);
	hclgevf_uninit_msi(hdev);
3465
err_cmd_init:
3466 3467
	hclgevf_cmd_uninit(hdev);
err_cmd_queue_init:
3468 3469
	hclgevf_devlink_uninit(hdev);
err_devlink_init:
3470
	hclgevf_pci_uninit(hdev);
3471
	clear_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
3472 3473 3474
	return ret;
}

3475
static void hclgevf_uninit_hdev(struct hclgevf_dev *hdev)
3476
{
3477 3478
	struct hclge_vf_to_pf_msg send_msg;

3479
	hclgevf_state_uninit(hdev);
3480
	hclgevf_uninit_rxd_adv_layout(hdev);
3481

3482 3483
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_VF_UNINIT, 0);
	hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
3484

3485 3486 3487 3488 3489
	if (test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) {
		hclgevf_misc_irq_uninit(hdev);
		hclgevf_uninit_msi(hdev);
	}

3490
	hclgevf_cmd_uninit(hdev);
3491
	hclgevf_devlink_uninit(hdev);
3492
	hclgevf_pci_uninit(hdev);
3493
	hclgevf_uninit_mac_list(hdev);
3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
}

static int hclgevf_init_ae_dev(struct hnae3_ae_dev *ae_dev)
{
	struct pci_dev *pdev = ae_dev->pdev;
	int ret;

	ret = hclgevf_alloc_hdev(ae_dev);
	if (ret) {
		dev_err(&pdev->dev, "hclge device allocation failed\n");
		return ret;
	}

	ret = hclgevf_init_hdev(ae_dev->priv);
3508
	if (ret) {
3509
		dev_err(&pdev->dev, "hclge device initialization failed\n");
3510 3511
		return ret;
	}
3512

3513
	return 0;
3514 3515 3516 3517 3518 3519 3520
}

static void hclgevf_uninit_ae_dev(struct hnae3_ae_dev *ae_dev)
{
	struct hclgevf_dev *hdev = ae_dev->priv;

	hclgevf_uninit_hdev(hdev);
3521 3522 3523
	ae_dev->priv = NULL;
}

3524 3525 3526 3527 3528
static u32 hclgevf_get_max_channels(struct hclgevf_dev *hdev)
{
	struct hnae3_handle *nic = &hdev->nic;
	struct hnae3_knic_private_info *kinfo = &nic->kinfo;

3529
	return min_t(u32, hdev->rss_size_max,
3530
		     hdev->num_tqps / kinfo->tc_info.num_tc);
3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
}

/**
 * hclgevf_get_channels - Get the current channels enabled and max supported.
 * @handle: hardware information for network interface
 * @ch: ethtool channels structure
 *
 * We don't support separate tx and rx queues as channels. The other count
 * represents how many queues are being used for control. max_combined counts
 * how many queue pairs we can support. They may not be mapped 1 to 1 with
 * q_vectors since we support a lot more queue pairs than q_vectors.
 **/
static void hclgevf_get_channels(struct hnae3_handle *handle,
				 struct ethtool_channels *ch)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	ch->max_combined = hclgevf_get_max_channels(hdev);
	ch->other_count = 0;
	ch->max_other = 0;
3551
	ch->combined_count = handle->kinfo.rss_size;
3552 3553
}

3554
static void hclgevf_get_tqps_and_rss_info(struct hnae3_handle *handle,
3555
					  u16 *alloc_tqps, u16 *max_rss_size)
3556 3557 3558
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

3559
	*alloc_tqps = hdev->num_tqps;
3560 3561 3562
	*max_rss_size = hdev->rss_size_max;
}

3563 3564 3565 3566 3567 3568 3569 3570 3571 3572
static void hclgevf_update_rss_size(struct hnae3_handle *handle,
				    u32 new_tqps_num)
{
	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	u16 max_rss_size;

	kinfo->req_rss_size = new_tqps_num;

	max_rss_size = min_t(u16, hdev->rss_size_max,
3573
			     hdev->num_tqps / kinfo->tc_info.num_tc);
3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584

	/* Use the user's configuration when it is not larger than
	 * max_rss_size, otherwise, use the maximum specification value.
	 */
	if (kinfo->req_rss_size != kinfo->rss_size && kinfo->req_rss_size &&
	    kinfo->req_rss_size <= max_rss_size)
		kinfo->rss_size = kinfo->req_rss_size;
	else if (kinfo->rss_size > max_rss_size ||
		 (!kinfo->req_rss_size && kinfo->rss_size < max_rss_size))
		kinfo->rss_size = max_rss_size;

3585
	kinfo->num_tqps = kinfo->tc_info.num_tc * kinfo->rss_size;
3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604
}

static int hclgevf_set_channels(struct hnae3_handle *handle, u32 new_tqps_num,
				bool rxfh_configured)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
	u16 cur_rss_size = kinfo->rss_size;
	u16 cur_tqps = kinfo->num_tqps;
	u32 *rss_indir;
	unsigned int i;
	int ret;

	hclgevf_update_rss_size(handle, new_tqps_num);

	ret = hclgevf_set_rss_tc_mode(hdev, kinfo->rss_size);
	if (ret)
		return ret;

3605
	/* RSS indirection table has been configured by user */
3606 3607 3608 3609
	if (rxfh_configured)
		goto out;

	/* Reinitializes the rss indirect table according to the new RSS size */
3610 3611
	rss_indir = kcalloc(hdev->ae_dev->dev_specs.rss_ind_tbl_size,
			    sizeof(u32), GFP_KERNEL);
3612 3613 3614
	if (!rss_indir)
		return -ENOMEM;

3615
	for (i = 0; i < hdev->ae_dev->dev_specs.rss_ind_tbl_size; i++)
3616 3617
		rss_indir[i] = i % kinfo->rss_size;

3618 3619
	hdev->rss_cfg.rss_size = kinfo->rss_size;

3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631
	ret = hclgevf_set_rss(handle, rss_indir, NULL, 0);
	if (ret)
		dev_err(&hdev->pdev->dev, "set rss indir table fail, ret=%d\n",
			ret);

	kfree(rss_indir);

out:
	if (!ret)
		dev_info(&hdev->pdev->dev,
			 "Channels changed, rss_size from %u to %u, tqps from %u to %u",
			 cur_rss_size, kinfo->rss_size,
3632
			 cur_tqps, kinfo->rss_size * kinfo->tc_info.num_tc);
3633 3634 3635 3636

	return ret;
}

3637 3638 3639 3640 3641 3642 3643
static int hclgevf_get_status(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	return hdev->hw.mac.link;
}

3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664
static void hclgevf_get_ksettings_an_result(struct hnae3_handle *handle,
					    u8 *auto_neg, u32 *speed,
					    u8 *duplex)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	if (speed)
		*speed = hdev->hw.mac.speed;
	if (duplex)
		*duplex = hdev->hw.mac.duplex;
	if (auto_neg)
		*auto_neg = AUTONEG_DISABLE;
}

void hclgevf_update_speed_duplex(struct hclgevf_dev *hdev, u32 speed,
				 u8 duplex)
{
	hdev->hw.mac.speed = speed;
	hdev->hw.mac.duplex = duplex;
}

3665
static int hclgevf_gro_en(struct hnae3_handle *handle, bool enable)
3666 3667
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3668 3669
	bool gro_en_old = hdev->gro_en;
	int ret;
3670

3671 3672 3673 3674 3675 3676
	hdev->gro_en = enable;
	ret = hclgevf_config_gro(hdev);
	if (ret)
		hdev->gro_en = gro_en_old;

	return ret;
3677 3678
}

3679 3680
static void hclgevf_get_media_type(struct hnae3_handle *handle, u8 *media_type,
				   u8 *module_type)
3681 3682
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3683

3684 3685
	if (media_type)
		*media_type = hdev->hw.mac.media_type;
3686 3687 3688

	if (module_type)
		*module_type = hdev->hw.mac.module_type;
3689 3690
}

3691 3692 3693 3694
static bool hclgevf_get_hw_reset_stat(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

3695
	return !!hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING);
3696 3697
}

3698 3699 3700 3701 3702 3703 3704
static bool hclgevf_get_cmdq_stat(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	return test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state);
}

3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
static bool hclgevf_ae_dev_resetting(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	return test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
}

static unsigned long hclgevf_ae_dev_reset_cnt(struct hnae3_handle *handle)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

3716
	return hdev->rst_stats.hw_rst_done_cnt;
3717 3718
}

3719 3720 3721 3722 3723 3724 3725 3726 3727 3728
static void hclgevf_get_link_mode(struct hnae3_handle *handle,
				  unsigned long *supported,
				  unsigned long *advertising)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	*supported = hdev->hw.mac.supported;
	*advertising = hdev->hw.mac.advertising;
}

3729
#define MAX_SEPARATE_NUM	4
3730
#define SEPARATOR_VALUE		0xFDFCFBFA
3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794
#define REG_NUM_PER_LINE	4
#define REG_LEN_PER_LINE	(REG_NUM_PER_LINE * sizeof(u32))

static int hclgevf_get_regs_len(struct hnae3_handle *handle)
{
	int cmdq_lines, common_lines, ring_lines, tqp_intr_lines;
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);

	cmdq_lines = sizeof(cmdq_reg_addr_list) / REG_LEN_PER_LINE + 1;
	common_lines = sizeof(common_reg_addr_list) / REG_LEN_PER_LINE + 1;
	ring_lines = sizeof(ring_reg_addr_list) / REG_LEN_PER_LINE + 1;
	tqp_intr_lines = sizeof(tqp_intr_reg_addr_list) / REG_LEN_PER_LINE + 1;

	return (cmdq_lines + common_lines + ring_lines * hdev->num_tqps +
		tqp_intr_lines * (hdev->num_msi_used - 1)) * REG_LEN_PER_LINE;
}

static void hclgevf_get_regs(struct hnae3_handle *handle, u32 *version,
			     void *data)
{
	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
	int i, j, reg_um, separator_num;
	u32 *reg = data;

	*version = hdev->fw_version;

	/* fetching per-VF registers values from VF PCIe register space */
	reg_um = sizeof(cmdq_reg_addr_list) / sizeof(u32);
	separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
	for (i = 0; i < reg_um; i++)
		*reg++ = hclgevf_read_dev(&hdev->hw, cmdq_reg_addr_list[i]);
	for (i = 0; i < separator_num; i++)
		*reg++ = SEPARATOR_VALUE;

	reg_um = sizeof(common_reg_addr_list) / sizeof(u32);
	separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
	for (i = 0; i < reg_um; i++)
		*reg++ = hclgevf_read_dev(&hdev->hw, common_reg_addr_list[i]);
	for (i = 0; i < separator_num; i++)
		*reg++ = SEPARATOR_VALUE;

	reg_um = sizeof(ring_reg_addr_list) / sizeof(u32);
	separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
	for (j = 0; j < hdev->num_tqps; j++) {
		for (i = 0; i < reg_um; i++)
			*reg++ = hclgevf_read_dev(&hdev->hw,
						  ring_reg_addr_list[i] +
						  0x200 * j);
		for (i = 0; i < separator_num; i++)
			*reg++ = SEPARATOR_VALUE;
	}

	reg_um = sizeof(tqp_intr_reg_addr_list) / sizeof(u32);
	separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
	for (j = 0; j < hdev->num_msi_used - 1; j++) {
		for (i = 0; i < reg_um; i++)
			*reg++ = hclgevf_read_dev(&hdev->hw,
						  tqp_intr_reg_addr_list[i] +
						  4 * j);
		for (i = 0; i < separator_num; i++)
			*reg++ = SEPARATOR_VALUE;
	}
}

3795 3796 3797 3798
void hclgevf_update_port_base_vlan_info(struct hclgevf_dev *hdev, u16 state,
					u8 *port_base_vlan_info, u8 data_size)
{
	struct hnae3_handle *nic = &hdev->nic;
3799
	struct hclge_vf_to_pf_msg send_msg;
3800
	int ret;
3801 3802

	rtnl_lock();
3803

3804 3805
	if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state) ||
	    test_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state)) {
3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
		dev_warn(&hdev->pdev->dev,
			 "is resetting when updating port based vlan info\n");
		rtnl_unlock();
		return;
	}

	ret = hclgevf_notify_client(hdev, HNAE3_DOWN_CLIENT);
	if (ret) {
		rtnl_unlock();
		return;
	}
3817 3818

	/* send msg to PF and wait update port based vlan info */
3819 3820 3821
	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
			       HCLGE_MBX_PORT_BASE_VLAN_CFG);
	memcpy(send_msg.data, port_base_vlan_info, data_size);
3822 3823 3824 3825 3826 3827 3828
	ret = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
	if (!ret) {
		if (state == HNAE3_PORT_BASE_VLAN_DISABLE)
			nic->port_base_vlan_state = state;
		else
			nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_ENABLE;
	}
3829 3830 3831 3832 3833

	hclgevf_notify_client(hdev, HNAE3_UP_CLIENT);
	rtnl_unlock();
}

3834 3835 3836
static const struct hnae3_ae_ops hclgevf_ops = {
	.init_ae_dev = hclgevf_init_ae_dev,
	.uninit_ae_dev = hclgevf_uninit_ae_dev,
3837 3838
	.reset_prepare = hclgevf_reset_prepare_general,
	.reset_done = hclgevf_reset_done,
3839 3840
	.init_client_instance = hclgevf_init_client_instance,
	.uninit_client_instance = hclgevf_uninit_client_instance,
3841 3842
	.start = hclgevf_ae_start,
	.stop = hclgevf_ae_stop,
3843 3844
	.client_start = hclgevf_client_start,
	.client_stop = hclgevf_client_stop,
3845 3846 3847
	.map_ring_to_vector = hclgevf_map_ring_to_vector,
	.unmap_ring_from_vector = hclgevf_unmap_ring_from_vector,
	.get_vector = hclgevf_get_vector,
3848
	.put_vector = hclgevf_put_vector,
3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862
	.reset_queue = hclgevf_reset_tqp,
	.get_mac_addr = hclgevf_get_mac_addr,
	.set_mac_addr = hclgevf_set_mac_addr,
	.add_uc_addr = hclgevf_add_uc_addr,
	.rm_uc_addr = hclgevf_rm_uc_addr,
	.add_mc_addr = hclgevf_add_mc_addr,
	.rm_mc_addr = hclgevf_rm_mc_addr,
	.get_stats = hclgevf_get_stats,
	.update_stats = hclgevf_update_stats,
	.get_strings = hclgevf_get_strings,
	.get_sset_count = hclgevf_get_sset_count,
	.get_rss_key_size = hclgevf_get_rss_key_size,
	.get_rss = hclgevf_get_rss,
	.set_rss = hclgevf_set_rss,
3863 3864
	.get_rss_tuple = hclgevf_get_rss_tuple,
	.set_rss_tuple = hclgevf_set_rss_tuple,
3865 3866 3867
	.get_tc_size = hclgevf_get_tc_size,
	.get_fw_version = hclgevf_get_fw_version,
	.set_vlan_filter = hclgevf_set_vlan_filter,
3868
	.enable_vlan_filter = hclgevf_enable_vlan_filter,
3869
	.enable_hw_strip_rxvtag = hclgevf_en_hw_strip_rxvtag,
3870
	.reset_event = hclgevf_reset_event,
3871
	.set_default_reset_request = hclgevf_set_def_reset_request,
3872
	.set_channels = hclgevf_set_channels,
3873
	.get_channels = hclgevf_get_channels,
3874
	.get_tqps_and_rss_info = hclgevf_get_tqps_and_rss_info,
3875 3876
	.get_regs_len = hclgevf_get_regs_len,
	.get_regs = hclgevf_get_regs,
3877
	.get_status = hclgevf_get_status,
3878
	.get_ksettings_an_result = hclgevf_get_ksettings_an_result,
3879
	.get_media_type = hclgevf_get_media_type,
3880 3881 3882
	.get_hw_reset_stat = hclgevf_get_hw_reset_stat,
	.ae_dev_resetting = hclgevf_ae_dev_resetting,
	.ae_dev_reset_cnt = hclgevf_ae_dev_reset_cnt,
3883
	.set_gro_en = hclgevf_gro_en,
3884
	.set_mtu = hclgevf_set_mtu,
3885
	.get_global_queue_id = hclgevf_get_qid_global,
3886
	.set_timer_task = hclgevf_set_timer_task,
3887
	.get_link_mode = hclgevf_get_link_mode,
3888
	.set_promisc_mode = hclgevf_set_promisc_mode,
3889
	.request_update_promisc_mode = hclgevf_request_update_promisc_mode,
3890
	.get_cmdq_stat = hclgevf_get_cmdq_stat,
3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901
};

static struct hnae3_ae_algo ae_algovf = {
	.ops = &hclgevf_ops,
	.pdev_id_table = ae_algovf_pci_tbl,
};

static int hclgevf_init(void)
{
	pr_info("%s is initializing\n", HCLGEVF_NAME);

3902
	hclgevf_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, HCLGEVF_NAME);
3903 3904 3905 3906 3907
	if (!hclgevf_wq) {
		pr_err("%s: failed to create workqueue\n", HCLGEVF_NAME);
		return -ENOMEM;
	}

3908 3909 3910
	hnae3_register_ae_algo(&ae_algovf);

	return 0;
3911 3912 3913 3914 3915
}

static void hclgevf_exit(void)
{
	hnae3_unregister_ae_algo(&ae_algovf);
3916
	destroy_workqueue(hclgevf_wq);
3917 3918 3919 3920 3921 3922 3923 3924
}
module_init(hclgevf_init);
module_exit(hclgevf_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
MODULE_DESCRIPTION("HCLGEVF Driver");
MODULE_VERSION(HCLGEVF_MOD_VERSION);