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

#include "hclge_main.h"
#include "hclge_mbx.h"
#include "hnae3.h"

8 9 10
#define CREATE_TRACE_POINTS
#include "hclge_trace.h"

11 12 13 14 15
static u16 hclge_errno_to_resp(int errno)
{
	return abs(errno);
}

16 17 18 19 20 21 22 23 24
/* hclge_gen_resp_to_vf: used to generate a synchronous response to VF when PF
 * receives a mailbox message from VF.
 * @vport: pointer to struct hclge_vport
 * @vf_to_pf_req: pointer to hclge_mbx_vf_to_pf_cmd of the original mailbox
 *		  message
 * @resp_status: indicate to VF whether its request success(0) or failed.
 */
static int hclge_gen_resp_to_vf(struct hclge_vport *vport,
				struct hclge_mbx_vf_to_pf_cmd *vf_to_pf_req,
25
				struct hclge_respond_to_vf_msg *resp_msg)
26 27 28 29 30
{
	struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf;
	struct hclge_dev *hdev = vport->back;
	enum hclge_cmd_status status;
	struct hclge_desc desc;
31
	u16 resp;
32 33 34

	resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data;

35
	if (resp_msg->len > HCLGE_MBX_MAX_RESP_DATA_SIZE) {
36
		dev_err(&hdev->pdev->dev,
37
			"PF fail to gen resp to VF len %u exceeds max len %u\n",
38
			resp_msg->len,
39
			HCLGE_MBX_MAX_RESP_DATA_SIZE);
40
		/* If resp_msg->len is too long, set the value to max length
41 42
		 * and return the msg to VF
		 */
43
		resp_msg->len = HCLGE_MBX_MAX_RESP_DATA_SIZE;
44 45 46 47 48 49 50
	}

	hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false);

	resp_pf_to_vf->dest_vfid = vf_to_pf_req->mbx_src_vfid;
	resp_pf_to_vf->msg_len = vf_to_pf_req->msg_len;

51 52 53
	resp_pf_to_vf->msg.code = HCLGE_MBX_PF_VF_RESP;
	resp_pf_to_vf->msg.vf_mbx_msg_code = vf_to_pf_req->msg.code;
	resp_pf_to_vf->msg.vf_mbx_msg_subcode = vf_to_pf_req->msg.subcode;
54
	resp = hclge_errno_to_resp(resp_msg->status);
55
	if (resp < SHRT_MAX) {
56
		resp_pf_to_vf->msg.resp_status = resp;
57 58
	} else {
		dev_warn(&hdev->pdev->dev,
59
			 "failed to send response to VF, response status %u is out-of-bound\n",
60
			 resp);
61
		resp_pf_to_vf->msg.resp_status = EIO;
62
	}
63

64 65 66
	if (resp_msg->len > 0)
		memcpy(resp_pf_to_vf->msg.resp_data, resp_msg->data,
		       resp_msg->len);
67 68 69 70

	status = hclge_cmd_send(&hdev->hw, &desc, 1);
	if (status)
		dev_err(&hdev->pdev->dev,
71 72 73
			"failed to send response to VF, status: %d, vfid: %u, code: %u, subcode: %u.\n",
			status, vf_to_pf_req->mbx_src_vfid,
			vf_to_pf_req->msg.code, vf_to_pf_req->msg.subcode);
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

	return status;
}

static int hclge_send_mbx_msg(struct hclge_vport *vport, u8 *msg, u16 msg_len,
			      u16 mbx_opcode, u8 dest_vfid)
{
	struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf;
	struct hclge_dev *hdev = vport->back;
	enum hclge_cmd_status status;
	struct hclge_desc desc;

	resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data;

	hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false);

	resp_pf_to_vf->dest_vfid = dest_vfid;
	resp_pf_to_vf->msg_len = msg_len;
92
	resp_pf_to_vf->msg.code = mbx_opcode;
93

94
	memcpy(&resp_pf_to_vf->msg.vf_mbx_msg_code, msg, msg_len);
95

96 97
	trace_hclge_pf_mbx_send(hdev, resp_pf_to_vf);

98 99 100
	status = hclge_cmd_send(&hdev->hw, &desc, 1);
	if (status)
		dev_err(&hdev->pdev->dev,
101 102
			"failed to send mailbox to VF, status: %d, vfid: %u, opcode: %u\n",
			status, dest_vfid, mbx_opcode);
103 104 105 106

	return status;
}

107
int hclge_inform_reset_assert_to_vf(struct hclge_vport *vport)
108
{
109
	struct hclge_dev *hdev = vport->back;
110
	u16 reset_type;
111 112 113
	u8 msg_data[2];
	u8 dest_vfid;

114 115
	BUILD_BUG_ON(HNAE3_MAX_RESET > U16_MAX);

116 117
	dest_vfid = (u8)vport->vport_id;

118 119
	if (hdev->reset_type == HNAE3_FUNC_RESET)
		reset_type = HNAE3_VF_PF_FUNC_RESET;
120 121
	else if (hdev->reset_type == HNAE3_FLR_RESET)
		reset_type = HNAE3_VF_FULL_RESET;
122
	else
123
		reset_type = HNAE3_VF_FUNC_RESET;
124 125 126

	memcpy(&msg_data[0], &reset_type, sizeof(u16));

127
	/* send this requested info to VF */
128
	return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
129 130 131
				  HCLGE_MBX_ASSERTING_RESET, dest_vfid);
}

132 133 134 135 136 137 138 139
static void hclge_free_vector_ring_chain(struct hnae3_ring_chain_node *head)
{
	struct hnae3_ring_chain_node *chain_tmp, *chain;

	chain = head->next;

	while (chain) {
		chain_tmp = chain->next;
140
		kfree_sensitive(chain);
141 142 143 144
		chain = chain_tmp;
	}
}

145 146
/* hclge_get_ring_chain_from_mbx: get ring type & tqp id & int_gl idx
 * from mailbox message
147 148 149 150 151
 * msg[0]: opcode
 * msg[1]: <not relevant to this function>
 * msg[2]: ring_num
 * msg[3]: first ring type (TX|RX)
 * msg[4]: first tqp id
152 153
 * msg[5]: first int_gl idx
 * msg[6] ~ msg[14]: other ring type, tqp id and int_gl idx
154 155 156 157 158 159 160
 */
static int hclge_get_ring_chain_from_mbx(
			struct hclge_mbx_vf_to_pf_cmd *req,
			struct hnae3_ring_chain_node *ring_chain,
			struct hclge_vport *vport)
{
	struct hnae3_ring_chain_node *cur_chain, *new_chain;
161
	struct hclge_dev *hdev = vport->back;
162
	int ring_num;
163
	int i;
164

165
	ring_num = req->msg.ring_num;
166

167
	if (ring_num > HCLGE_MBX_MAX_RING_CHAIN_PARAM_NUM)
168 169
		return -ENOMEM;

170 171 172 173 174 175 176 177 178
	for (i = 0; i < ring_num; i++) {
		if (req->msg.param[i].tqp_index >= vport->nic.kinfo.rss_size) {
			dev_err(&hdev->pdev->dev, "tqp index(%u) is out of range(0-%u)\n",
				req->msg.param[i].tqp_index,
				vport->nic.kinfo.rss_size - 1);
			return -EINVAL;
		}
	}

179
	hnae3_set_bit(ring_chain->flag, HNAE3_RING_TYPE_B,
180
		      req->msg.param[0].ring_type);
181
	ring_chain->tqp_index =
182
		hclge_get_queue_id(vport->nic.kinfo.tqp
183
				   [req->msg.param[0].tqp_index]);
184
	hnae3_set_field(ring_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
185
			HNAE3_RING_GL_IDX_S, req->msg.param[0].int_gl_index);
186 187 188 189 190 191 192 193

	cur_chain = ring_chain;

	for (i = 1; i < ring_num; i++) {
		new_chain = kzalloc(sizeof(*new_chain), GFP_KERNEL);
		if (!new_chain)
			goto err;

P
Peng Li 已提交
194
		hnae3_set_bit(new_chain->flag, HNAE3_RING_TYPE_B,
195
			      req->msg.param[i].ring_type);
196 197 198

		new_chain->tqp_index =
		hclge_get_queue_id(vport->nic.kinfo.tqp
199
			[req->msg.param[i].tqp_index]);
200

201 202
		hnae3_set_field(new_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
				HNAE3_RING_GL_IDX_S,
203
				req->msg.param[i].int_gl_index);
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218
		cur_chain->next = new_chain;
		cur_chain = new_chain;
	}

	return 0;
err:
	hclge_free_vector_ring_chain(ring_chain);
	return -ENOMEM;
}

static int hclge_map_unmap_ring_to_vf_vector(struct hclge_vport *vport, bool en,
					     struct hclge_mbx_vf_to_pf_cmd *req)
{
	struct hnae3_ring_chain_node ring_chain;
219
	int vector_id = req->msg.vector_id;
220 221 222 223 224 225 226 227 228 229 230
	int ret;

	memset(&ring_chain, 0, sizeof(ring_chain));
	ret = hclge_get_ring_chain_from_mbx(req, &ring_chain, vport);
	if (ret)
		return ret;

	ret = hclge_bind_ring_with_vector(vport, vector_id, en, &ring_chain);

	hclge_free_vector_ring_chain(&ring_chain);

231
	return ret;
232 233
}

234 235
static void hclge_set_vf_promisc_mode(struct hclge_vport *vport,
				      struct hclge_mbx_vf_to_pf_cmd *req)
236
{
237
	struct hnae3_handle *handle = &vport->nic;
238
	struct hclge_dev *hdev = vport->back;
239

240 241 242
	vport->vf_info.request_uc_en = req->msg.en_uc;
	vport->vf_info.request_mc_en = req->msg.en_mc;
	vport->vf_info.request_bc_en = req->msg.en_bc;
243

244 245 246 247 248 249
	if (req->msg.en_limit_promisc)
		set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
	else
		clear_bit(HNAE3_PFLAG_LIMIT_PROMISC,
			  &handle->priv_flags);

250 251
	set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
	hclge_task_schedule(hdev, 0);
252 253 254
}

static int hclge_set_vf_uc_mac_addr(struct hclge_vport *vport,
255
				    struct hclge_mbx_vf_to_pf_cmd *mbx_req)
256
{
257 258 259
#define HCLGE_MBX_VF_OLD_MAC_ADDR_OFFSET	6

	const u8 *mac_addr = (const u8 *)(mbx_req->msg.data);
260 261 262
	struct hclge_dev *hdev = vport->back;
	int status;

263 264 265
	if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_MODIFY) {
		const u8 *old_addr = (const u8 *)
		(&mbx_req->msg.data[HCLGE_MBX_VF_OLD_MAC_ADDR_OFFSET]);
266

267 268 269 270
		/* If VF MAC has been configured by the host then it
		 * cannot be overridden by the MAC specified by the VM.
		 */
		if (!is_zero_ether_addr(vport->vf_info.mac) &&
271 272
		    !ether_addr_equal(mac_addr, vport->vf_info.mac))
			return -EPERM;
273

274 275
		if (!is_valid_ether_addr(mac_addr))
			return -EINVAL;
276

277 278 279 280 281
		spin_lock_bh(&vport->mac_list_lock);
		status = hclge_update_mac_node_for_dev_addr(vport, old_addr,
							    mac_addr);
		spin_unlock_bh(&vport->mac_list_lock);
		hclge_task_schedule(hdev, 0);
282
	} else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_ADD) {
283 284
		status = hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD,
					       HCLGE_MAC_ADDR_UC, mac_addr);
285
	} else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_REMOVE) {
286 287
		status = hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL,
					       HCLGE_MAC_ADDR_UC, mac_addr);
288 289
	} else {
		dev_err(&hdev->pdev->dev,
290
			"failed to set unicast mac addr, unknown subcode %u\n",
291
			mbx_req->msg.subcode);
292 293 294
		return -EIO;
	}

295
	return status;
296 297 298
}

static int hclge_set_vf_mc_mac_addr(struct hclge_vport *vport,
299
				    struct hclge_mbx_vf_to_pf_cmd *mbx_req)
300
{
301
	const u8 *mac_addr = (const u8 *)(mbx_req->msg.data);
302 303
	struct hclge_dev *hdev = vport->back;

304
	if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_MC_ADD) {
305 306
		hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD,
				      HCLGE_MAC_ADDR_MC, mac_addr);
307
	} else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_MC_REMOVE) {
308 309
		hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL,
				      HCLGE_MAC_ADDR_MC, mac_addr);
310 311
	} else {
		dev_err(&hdev->pdev->dev,
312
			"failed to set mcast mac addr, unknown subcode %u\n",
313
			mbx_req->msg.subcode);
314 315 316
		return -EIO;
	}

317
	return 0;
318 319
}

320 321 322 323 324 325 326 327 328 329 330 331 332 333
int hclge_push_vf_port_base_vlan_info(struct hclge_vport *vport, u8 vfid,
				      u16 state, u16 vlan_tag, u16 qos,
				      u16 vlan_proto)
{
#define MSG_DATA_SIZE	8

	u8 msg_data[MSG_DATA_SIZE];

	memcpy(&msg_data[0], &state, sizeof(u16));
	memcpy(&msg_data[2], &vlan_proto, sizeof(u16));
	memcpy(&msg_data[4], &qos, sizeof(u16));
	memcpy(&msg_data[6], &vlan_tag, sizeof(u16));

	return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
334
				  HCLGE_MBX_PUSH_VLAN_INFO, vfid);
335 336
}

337
static int hclge_set_vf_vlan_cfg(struct hclge_vport *vport,
338 339
				 struct hclge_mbx_vf_to_pf_cmd *mbx_req,
				 struct hclge_respond_to_vf_msg *resp_msg)
340
{
341 342 343
#define HCLGE_MBX_VLAN_STATE_OFFSET	0
#define HCLGE_MBX_VLAN_INFO_OFFSET	2

344
	struct hclge_vf_vlan_cfg *msg_cmd;
345 346
	int status = 0;

347
	msg_cmd = (struct hclge_vf_vlan_cfg *)&mbx_req->msg;
348
	if (msg_cmd->subcode == HCLGE_MBX_VLAN_FILTER) {
349
		struct hnae3_handle *handle = &vport->nic;
350 351 352
		u16 vlan, proto;
		bool is_kill;

353 354 355
		is_kill = !!msg_cmd->is_kill;
		vlan =  msg_cmd->vlan;
		proto =  msg_cmd->proto;
356 357
		status = hclge_set_vlan_filter(handle, cpu_to_be16(proto),
					       vlan, is_kill);
358
	} else if (msg_cmd->subcode == HCLGE_MBX_VLAN_RX_OFF_CFG) {
359
		struct hnae3_handle *handle = &vport->nic;
360
		bool en = msg_cmd->is_kill ? true : false;
361 362

		status = hclge_en_hw_strip_rxvtag(handle, en);
363
	} else if (msg_cmd->subcode == HCLGE_MBX_PORT_BASE_VLAN_CFG) {
364 365 366
		struct hclge_vlan_info *vlan_info;
		u16 *state;

367 368 369
		state = (u16 *)&mbx_req->msg.data[HCLGE_MBX_VLAN_STATE_OFFSET];
		vlan_info = (struct hclge_vlan_info *)
			&mbx_req->msg.data[HCLGE_MBX_VLAN_INFO_OFFSET];
370 371
		status = hclge_update_port_base_vlan_cfg(vport, *state,
							 vlan_info);
372
	} else if (msg_cmd->subcode == HCLGE_MBX_GET_PORT_BASE_VLAN_STATE) {
373 374 375 376 377 378 379 380 381 382
		struct hnae3_ae_dev *ae_dev = pci_get_drvdata(vport->nic.pdev);
		/* vf does not need to know about the port based VLAN state
		 * on device HNAE3_DEVICE_VERSION_V3. So always return disable
		 * on device HNAE3_DEVICE_VERSION_V3 if vf queries the port
		 * based VLAN state.
		 */
		resp_msg->data[0] =
			ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3 ?
			HNAE3_PORT_BASE_VLAN_DISABLE :
			vport->port_base_vlan_cfg.state;
383
		resp_msg->len = sizeof(u8);
384 385 386 387 388
	}

	return status;
}

389
static int hclge_set_vf_alive(struct hclge_vport *vport,
390
			      struct hclge_mbx_vf_to_pf_cmd *mbx_req)
391
{
392
	bool alive = !!mbx_req->msg.data[0];
393 394 395 396 397 398 399 400 401 402
	int ret = 0;

	if (alive)
		ret = hclge_vport_start(vport);
	else
		hclge_vport_stop(vport);

	return ret;
}

403 404
static void hclge_get_vf_tcinfo(struct hclge_vport *vport,
				struct hclge_respond_to_vf_msg *resp_msg)
405
{
406
	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
407
	unsigned int i;
408

409
	for (i = 0; i < kinfo->tc_info.num_tc; i++)
410
		resp_msg->data[0] |= BIT(i);
411

412
	resp_msg->len = sizeof(u8);
413 414
}

415 416
static void hclge_get_vf_queue_info(struct hclge_vport *vport,
				    struct hclge_respond_to_vf_msg *resp_msg)
417
{
418
#define HCLGE_TQPS_RSS_INFO_LEN		6
419 420 421 422
#define HCLGE_TQPS_ALLOC_OFFSET		0
#define HCLGE_TQPS_RSS_SIZE_OFFSET	2
#define HCLGE_TQPS_RX_BUFFER_LEN_OFFSET	4

423 424 425
	struct hclge_dev *hdev = vport->back;

	/* get the queue related info */
426 427 428 429 430 431 432
	memcpy(&resp_msg->data[HCLGE_TQPS_ALLOC_OFFSET],
	       &vport->alloc_tqps, sizeof(u16));
	memcpy(&resp_msg->data[HCLGE_TQPS_RSS_SIZE_OFFSET],
	       &vport->nic.kinfo.rss_size, sizeof(u16));
	memcpy(&resp_msg->data[HCLGE_TQPS_RX_BUFFER_LEN_OFFSET],
	       &hdev->rx_buf_len, sizeof(u16));
	resp_msg->len = HCLGE_TQPS_RSS_INFO_LEN;
433 434
}

435 436
static void hclge_get_vf_mac_addr(struct hclge_vport *vport,
				  struct hclge_respond_to_vf_msg *resp_msg)
437
{
438 439
	ether_addr_copy(resp_msg->data, vport->vf_info.mac);
	resp_msg->len = ETH_ALEN;
440 441
}

442 443
static void hclge_get_vf_queue_depth(struct hclge_vport *vport,
				     struct hclge_respond_to_vf_msg *resp_msg)
444 445
{
#define HCLGE_TQPS_DEPTH_INFO_LEN	4
446 447 448
#define HCLGE_TQPS_NUM_TX_DESC_OFFSET	0
#define HCLGE_TQPS_NUM_RX_DESC_OFFSET	2

449 450 451
	struct hclge_dev *hdev = vport->back;

	/* get the queue depth info */
452 453 454 455 456
	memcpy(&resp_msg->data[HCLGE_TQPS_NUM_TX_DESC_OFFSET],
	       &hdev->num_tx_desc, sizeof(u16));
	memcpy(&resp_msg->data[HCLGE_TQPS_NUM_RX_DESC_OFFSET],
	       &hdev->num_rx_desc, sizeof(u16));
	resp_msg->len = HCLGE_TQPS_DEPTH_INFO_LEN;
457 458
}

459 460
static void hclge_get_vf_media_type(struct hclge_vport *vport,
				    struct hclge_respond_to_vf_msg *resp_msg)
461
{
462 463 464 465
#define HCLGE_VF_MEDIA_TYPE_OFFSET	0
#define HCLGE_VF_MODULE_TYPE_OFFSET	1
#define HCLGE_VF_MEDIA_TYPE_LENGTH	2

466 467
	struct hclge_dev *hdev = vport->back;

468 469 470 471 472
	resp_msg->data[HCLGE_VF_MEDIA_TYPE_OFFSET] =
		hdev->hw.mac.media_type;
	resp_msg->data[HCLGE_VF_MODULE_TYPE_OFFSET] =
		hdev->hw.mac.module_type;
	resp_msg->len = HCLGE_VF_MEDIA_TYPE_LENGTH;
473 474
}

475
int hclge_push_vf_link_status(struct hclge_vport *vport)
476
{
477 478 479
#define HCLGE_VF_LINK_STATE_UP		1U
#define HCLGE_VF_LINK_STATE_DOWN	0U

480 481
	struct hclge_dev *hdev = vport->back;
	u16 link_status;
482
	u8 msg_data[9];
483
	u16 duplex;
484 485

	/* mac.link can only be 0 or 1 */
486 487 488 489 490 491 492 493 494 495 496 497 498
	switch (vport->vf_info.link_state) {
	case IFLA_VF_LINK_STATE_ENABLE:
		link_status = HCLGE_VF_LINK_STATE_UP;
		break;
	case IFLA_VF_LINK_STATE_DISABLE:
		link_status = HCLGE_VF_LINK_STATE_DOWN;
		break;
	case IFLA_VF_LINK_STATE_AUTO:
	default:
		link_status = (u16)hdev->hw.mac.link;
		break;
	}

499
	duplex = hdev->hw.mac.duplex;
500
	memcpy(&msg_data[0], &link_status, sizeof(u16));
501 502
	memcpy(&msg_data[2], &hdev->hw.mac.speed, sizeof(u32));
	memcpy(&msg_data[6], &duplex, sizeof(u16));
503
	msg_data[8] = HCLGE_MBX_PUSH_LINK_STATUS_EN;
504 505

	/* send this requested info to VF */
506
	return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
507
				  HCLGE_MBX_LINK_STAT_CHANGE, vport->vport_id);
508 509
}

510 511 512 513 514 515 516 517
static void hclge_get_link_mode(struct hclge_vport *vport,
				struct hclge_mbx_vf_to_pf_cmd *mbx_req)
{
#define HCLGE_SUPPORTED   1
	struct hclge_dev *hdev = vport->back;
	unsigned long advertising;
	unsigned long supported;
	unsigned long send_data;
518
	u8 msg_data[10] = {};
519 520 521 522 523
	u8 dest_vfid;

	advertising = hdev->hw.mac.advertising[0];
	supported = hdev->hw.mac.supported[0];
	dest_vfid = mbx_req->mbx_src_vfid;
524
	msg_data[0] = mbx_req->msg.data[0];
525 526 527 528 529 530 531 532

	send_data = msg_data[0] == HCLGE_SUPPORTED ? supported : advertising;

	memcpy(&msg_data[2], &send_data, sizeof(unsigned long));
	hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
			   HCLGE_MBX_LINK_STAT_MODE, dest_vfid);
}

533 534 535
static int hclge_mbx_reset_vf_queue(struct hclge_vport *vport,
				    struct hclge_mbx_vf_to_pf_cmd *mbx_req,
				    struct hclge_respond_to_vf_msg *resp_msg)
536
{
537 538 539
#define HCLGE_RESET_ALL_QUEUE_DONE	1U
	struct hnae3_handle *handle = &vport->nic;
	struct hclge_dev *hdev = vport->back;
540
	u16 queue_id;
541
	int ret;
542

543
	memcpy(&queue_id, mbx_req->msg.data, sizeof(queue_id));
544 545
	resp_msg->data[0] = HCLGE_RESET_ALL_QUEUE_DONE;
	resp_msg->len = sizeof(u8);
546

547 548 549 550 551 552 553 554 555 556 557 558
	/* pf will reset vf's all queues at a time. So it is unnecessary
	 * to reset queues if queue_id > 0, just return success.
	 */
	if (queue_id > 0)
		return 0;

	ret = hclge_reset_tqp(handle);
	if (ret)
		dev_err(&hdev->pdev->dev, "failed to reset vf %u queue, ret = %d\n",
			vport->vport_id - HCLGE_VF_VPORT_START_NUM, ret);

	return ret;
559 560
}

561
static int hclge_reset_vf(struct hclge_vport *vport)
562 563 564
{
	struct hclge_dev *hdev = vport->back;

565
	dev_warn(&hdev->pdev->dev, "PF received VF reset request from VF %u!",
566
		 vport->vport_id);
567

568
	return hclge_func_reset_cmd(hdev, vport->vport_id);
569 570
}

571
static void hclge_vf_keep_alive(struct hclge_vport *vport)
572 573 574 575
{
	vport->last_active_jiffies = jiffies;
}

576 577 578 579 580
static int hclge_set_vf_mtu(struct hclge_vport *vport,
			    struct hclge_mbx_vf_to_pf_cmd *mbx_req)
{
	u32 mtu;

581
	memcpy(&mtu, mbx_req->msg.data, sizeof(mtu));
582

583
	return hclge_set_vport_mtu(vport, mtu);
584 585
}

586 587 588
static void hclge_get_queue_id_in_pf(struct hclge_vport *vport,
				     struct hclge_mbx_vf_to_pf_cmd *mbx_req,
				     struct hclge_respond_to_vf_msg *resp_msg)
589 590 591
{
	u16 queue_id, qid_in_pf;

592
	memcpy(&queue_id, mbx_req->msg.data, sizeof(queue_id));
593
	qid_in_pf = hclge_covert_handle_qid_global(&vport->nic, queue_id);
594 595
	memcpy(resp_msg->data, &qid_in_pf, sizeof(qid_in_pf));
	resp_msg->len = sizeof(qid_in_pf);
596 597
}

598 599 600
static void hclge_get_rss_key(struct hclge_vport *vport,
			      struct hclge_mbx_vf_to_pf_cmd *mbx_req,
			      struct hclge_respond_to_vf_msg *resp_msg)
J
Jian Shen 已提交
601 602 603 604 605
{
#define HCLGE_RSS_MBX_RESP_LEN	8
	struct hclge_dev *hdev = vport->back;
	u8 index;

606
	index = mbx_req->msg.data[0];
J
Jian Shen 已提交
607

608 609 610 611 612 613 614 615 616 617 618
	/* Check the query index of rss_hash_key from VF, make sure no
	 * more than the size of rss_hash_key.
	 */
	if (((index + 1) * HCLGE_RSS_MBX_RESP_LEN) >
	      sizeof(vport[0].rss_hash_key)) {
		dev_warn(&hdev->pdev->dev,
			 "failed to get the rss hash key, the index(%u) invalid !\n",
			 index);
		return;
	}

619
	memcpy(resp_msg->data,
J
Jian Shen 已提交
620 621
	       &hdev->vport[0].rss_hash_key[index * HCLGE_RSS_MBX_RESP_LEN],
	       HCLGE_RSS_MBX_RESP_LEN);
622
	resp_msg->len = HCLGE_RSS_MBX_RESP_LEN;
J
Jian Shen 已提交
623 624
}

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
static void hclge_link_fail_parse(struct hclge_dev *hdev, u8 link_fail_code)
{
	switch (link_fail_code) {
	case HCLGE_LF_REF_CLOCK_LOST:
		dev_warn(&hdev->pdev->dev, "Reference clock lost!\n");
		break;
	case HCLGE_LF_XSFP_TX_DISABLE:
		dev_warn(&hdev->pdev->dev, "SFP tx is disabled!\n");
		break;
	case HCLGE_LF_XSFP_ABSENT:
		dev_warn(&hdev->pdev->dev, "SFP is absent!\n");
		break;
	default:
		break;
	}
}

static void hclge_handle_link_change_event(struct hclge_dev *hdev,
					   struct hclge_mbx_vf_to_pf_cmd *req)
{
	hclge_task_schedule(hdev, 0);

647 648
	if (!req->msg.subcode)
		hclge_link_fail_parse(hdev, req->msg.data[0]);
649 650
}

651 652 653 654 655 656 657
static bool hclge_cmd_crq_empty(struct hclge_hw *hw)
{
	u32 tail = hclge_read_dev(hw, HCLGE_NIC_CRQ_TAIL_REG);

	return tail == hw->cmq.crq.next_to_use;
}

658 659 660 661 662 663 664 665 666
static void hclge_handle_ncsi_error(struct hclge_dev *hdev)
{
	struct hnae3_ae_dev *ae_dev = hdev->ae_dev;

	ae_dev->ops->set_default_reset_request(ae_dev, HNAE3_GLOBAL_RESET);
	dev_warn(&hdev->pdev->dev, "requesting reset due to NCSI error\n");
	ae_dev->ops->reset_event(hdev->pdev, NULL);
}

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
static void hclge_handle_vf_tbl(struct hclge_vport *vport,
				struct hclge_mbx_vf_to_pf_cmd *mbx_req)
{
	struct hclge_dev *hdev = vport->back;
	struct hclge_vf_vlan_cfg *msg_cmd;

	msg_cmd = (struct hclge_vf_vlan_cfg *)&mbx_req->msg;
	if (msg_cmd->subcode == HCLGE_MBX_VPORT_LIST_CLEAR) {
		hclge_rm_vport_all_mac_table(vport, true, HCLGE_MAC_ADDR_UC);
		hclge_rm_vport_all_mac_table(vport, true, HCLGE_MAC_ADDR_MC);
		hclge_rm_vport_all_vlan_table(vport, true);
	} else {
		dev_warn(&hdev->pdev->dev, "Invalid cmd(%u)\n",
			 msg_cmd->subcode);
	}
}

684 685 686
void hclge_mbx_handler(struct hclge_dev *hdev)
{
	struct hclge_cmq_ring *crq = &hdev->hw.cmq.crq;
687
	struct hclge_respond_to_vf_msg resp_msg;
688 689 690
	struct hclge_mbx_vf_to_pf_cmd *req;
	struct hclge_vport *vport;
	struct hclge_desc *desc;
691
	bool is_del = false;
692
	unsigned int flag;
693
	int ret = 0;
694 695

	/* handle all the mailbox requests in the queue */
696
	while (!hclge_cmd_crq_empty(&hdev->hw)) {
697 698 699 700 701 702
		if (test_bit(HCLGE_STATE_CMD_DISABLE, &hdev->state)) {
			dev_warn(&hdev->pdev->dev,
				 "command queue needs re-initializing\n");
			return;
		}

703 704 705
		desc = &crq->desc[crq->next_to_use];
		req = (struct hclge_mbx_vf_to_pf_cmd *)desc->data;

706
		flag = le16_to_cpu(crq->desc[crq->next_to_use].flag);
P
Peng Li 已提交
707
		if (unlikely(!hnae3_get_bit(flag, HCLGE_CMDQ_RX_OUTVLD_B))) {
708
			dev_warn(&hdev->pdev->dev,
709
				 "dropped invalid mailbox message, code = %u\n",
710
				 req->msg.code);
711 712 713 714 715 716 717

			/* dropping/not processing this invalid message */
			crq->desc[crq->next_to_use].flag = 0;
			hclge_mbx_ring_ptr_move_crq(crq);
			continue;
		}

718 719
		vport = &hdev->vport[req->mbx_src_vfid];

720 721
		trace_hclge_pf_mbx_get(hdev, req);

722 723 724
		/* clear the resp_msg before processing every mailbox message */
		memset(&resp_msg, 0, sizeof(resp_msg));

725
		switch (req->msg.code) {
726 727 728 729 730 731 732 733
		case HCLGE_MBX_MAP_RING_TO_VECTOR:
			ret = hclge_map_unmap_ring_to_vf_vector(vport, true,
								req);
			break;
		case HCLGE_MBX_UNMAP_RING_TO_VECTOR:
			ret = hclge_map_unmap_ring_to_vf_vector(vport, false,
								req);
			break;
734
		case HCLGE_MBX_SET_PROMISC_MODE:
735
			hclge_set_vf_promisc_mode(vport, req);
736 737
			break;
		case HCLGE_MBX_SET_UNICAST:
738
			ret = hclge_set_vf_uc_mac_addr(vport, req);
739 740 741 742 743 744
			if (ret)
				dev_err(&hdev->pdev->dev,
					"PF fail(%d) to set VF UC MAC Addr\n",
					ret);
			break;
		case HCLGE_MBX_SET_MULTICAST:
745
			ret = hclge_set_vf_mc_mac_addr(vport, req);
746 747 748 749 750 751
			if (ret)
				dev_err(&hdev->pdev->dev,
					"PF fail(%d) to set VF MC MAC Addr\n",
					ret);
			break;
		case HCLGE_MBX_SET_VLAN:
752
			ret = hclge_set_vf_vlan_cfg(vport, req, &resp_msg);
753 754 755 756 757
			if (ret)
				dev_err(&hdev->pdev->dev,
					"PF failed(%d) to config VF's VLAN\n",
					ret);
			break;
758
		case HCLGE_MBX_SET_ALIVE:
759
			ret = hclge_set_vf_alive(vport, req);
760 761 762 763 764
			if (ret)
				dev_err(&hdev->pdev->dev,
					"PF failed(%d) to set VF's ALIVE\n",
					ret);
			break;
765
		case HCLGE_MBX_GET_QINFO:
766
			hclge_get_vf_queue_info(vport, &resp_msg);
767
			break;
768
		case HCLGE_MBX_GET_QDEPTH:
769
			hclge_get_vf_queue_depth(vport, &resp_msg);
770
			break;
771
		case HCLGE_MBX_GET_TCINFO:
772
			hclge_get_vf_tcinfo(vport, &resp_msg);
773 774
			break;
		case HCLGE_MBX_GET_LINK_STATUS:
775
			ret = hclge_push_vf_link_status(vport);
776 777
			if (ret)
				dev_err(&hdev->pdev->dev,
778
					"failed to inform link stat to VF, ret = %d\n",
779 780
					ret);
			break;
781
		case HCLGE_MBX_QUEUE_RESET:
782
			ret = hclge_mbx_reset_vf_queue(vport, req, &resp_msg);
783
			break;
784
		case HCLGE_MBX_RESET:
785
			ret = hclge_reset_vf(vport);
786
			break;
787
		case HCLGE_MBX_KEEP_ALIVE:
788
			hclge_vf_keep_alive(vport);
789
			break;
790 791 792 793 794 795
		case HCLGE_MBX_SET_MTU:
			ret = hclge_set_vf_mtu(vport, req);
			if (ret)
				dev_err(&hdev->pdev->dev,
					"VF fail(%d) to set mtu\n", ret);
			break;
796
		case HCLGE_MBX_GET_QID_IN_PF:
797
			hclge_get_queue_id_in_pf(vport, req, &resp_msg);
798
			break;
J
Jian Shen 已提交
799
		case HCLGE_MBX_GET_RSS_KEY:
800
			hclge_get_rss_key(vport, req, &resp_msg);
J
Jian Shen 已提交
801
			break;
802 803 804
		case HCLGE_MBX_GET_LINK_MODE:
			hclge_get_link_mode(vport, req);
			break;
805
		case HCLGE_MBX_GET_VF_FLR_STATUS:
806
		case HCLGE_MBX_VF_UNINIT:
807 808
			is_del = req->msg.code == HCLGE_MBX_VF_UNINIT;
			hclge_rm_vport_all_mac_table(vport, is_del,
809
						     HCLGE_MAC_ADDR_UC);
810
			hclge_rm_vport_all_mac_table(vport, is_del,
811
						     HCLGE_MAC_ADDR_MC);
812
			hclge_rm_vport_all_vlan_table(vport, is_del);
813
			break;
814
		case HCLGE_MBX_GET_MEDIA_TYPE:
815
			hclge_get_vf_media_type(vport, &resp_msg);
816
			break;
817 818 819
		case HCLGE_MBX_PUSH_LINK_STATUS:
			hclge_handle_link_change_event(hdev, req);
			break;
820
		case HCLGE_MBX_GET_MAC_ADDR:
821
			hclge_get_vf_mac_addr(vport, &resp_msg);
822
			break;
823 824 825
		case HCLGE_MBX_NCSI_ERROR:
			hclge_handle_ncsi_error(hdev);
			break;
826 827 828
		case HCLGE_MBX_HANDLE_VF_TBL:
			hclge_handle_vf_tbl(vport, req);
			break;
829 830
		default:
			dev_err(&hdev->pdev->dev,
831
				"un-supported mailbox message, code = %u\n",
832
				req->msg.code);
833 834
			break;
		}
835 836 837 838 839 840 841 842

		/* PF driver should not reply IMP */
		if (hnae3_get_bit(req->mbx_need_resp, HCLGE_MBX_NEED_RESP_B) &&
		    req->msg.code < HCLGE_MBX_GET_VF_FLR_STATUS) {
			resp_msg.status = ret;
			hclge_gen_resp_to_vf(vport, req, &resp_msg);
		}

843
		crq->desc[crq->next_to_use].flag = 0;
844
		hclge_mbx_ring_ptr_move_crq(crq);
845 846 847

		/* reinitialize ret after complete the mbx message processing */
		ret = 0;
848 849 850 851 852
	}

	/* Write back CMDQ_RQ header pointer, M7 need this pointer */
	hclge_write_dev(&hdev->hw, HCLGE_NIC_CRQ_HEAD_REG, crq->next_to_use);
}