xref: /linux/drivers/net/ethernet/huawei/hinic/hinic_sriov.c (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Huawei HiNIC PCI Express Linux driver
3  * Copyright(c) 2017 Huawei Technologies Co., Ltd
4  */
5 
6 #include <linux/pci.h>
7 #include <linux/if_vlan.h>
8 #include <linux/interrupt.h>
9 #include <linux/etherdevice.h>
10 #include <linux/netdevice.h>
11 #include <linux/module.h>
12 
13 #include "hinic_hw_dev.h"
14 #include "hinic_dev.h"
15 #include "hinic_hw_mbox.h"
16 #include "hinic_hw_cmdq.h"
17 #include "hinic_port.h"
18 #include "hinic_sriov.h"
19 
20 static unsigned char set_vf_link_state;
21 module_param(set_vf_link_state, byte, 0444);
22 MODULE_PARM_DESC(set_vf_link_state, "Set vf link state, 0 represents link auto, 1 represents link always up, 2 represents link always down. - default is 0.");
23 
24 #define HINIC_VLAN_PRIORITY_SHIFT 13
25 #define HINIC_ADD_VLAN_IN_MAC 0x8000
26 #define HINIC_TX_RATE_TABLE_FULL 12
27 
28 static int hinic_set_mac(struct hinic_hwdev *hwdev, const u8 *mac_addr,
29 			 u16 vlan_id, u16 func_id)
30 {
31 	struct hinic_port_mac_cmd mac_info = {0};
32 	u16 out_size = sizeof(mac_info);
33 	int err;
34 
35 	mac_info.func_idx = func_id;
36 	mac_info.vlan_id = vlan_id;
37 	memcpy(mac_info.mac, mac_addr, ETH_ALEN);
38 
39 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_MAC, &mac_info,
40 				 sizeof(mac_info), &mac_info, &out_size);
41 	if (err || out_size != sizeof(mac_info) ||
42 	    (mac_info.status && mac_info.status != HINIC_MGMT_STATUS_EXIST)) {
43 		dev_err(&hwdev->func_to_io.hwif->pdev->dev, "Failed to set MAC, err: %d, status: 0x%x, out size: 0x%x\n",
44 			err, mac_info.status, out_size);
45 		return -EIO;
46 	}
47 
48 	return 0;
49 }
50 
51 static void hinic_notify_vf_link_status(struct hinic_hwdev *hwdev, u16 vf_id,
52 					u8 link_status)
53 {
54 	struct vf_data_storage *vf_infos = hwdev->func_to_io.vf_infos;
55 	struct hinic_port_link_status link = {0};
56 	u16 out_size = sizeof(link);
57 	int err;
58 
59 	if (vf_infos[HW_VF_ID_TO_OS(vf_id)].registered) {
60 		link.link = link_status;
61 		link.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
62 		err = hinic_mbox_to_vf(hwdev, HINIC_MOD_L2NIC,
63 				       vf_id, HINIC_PORT_CMD_LINK_STATUS_REPORT,
64 				       &link, sizeof(link),
65 				       &link, &out_size, 0);
66 		if (err || !out_size || link.status)
67 			dev_err(&hwdev->hwif->pdev->dev,
68 				"Send link change event to VF %d failed, err: %d, status: 0x%x, out_size: 0x%x\n",
69 				HW_VF_ID_TO_OS(vf_id), err,
70 				link.status, out_size);
71 	}
72 }
73 
74 /* send link change event mbox msg to active vfs under the pf */
75 void hinic_notify_all_vfs_link_changed(struct hinic_hwdev *hwdev,
76 				       u8 link_status)
77 {
78 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
79 	u16 i;
80 
81 	nic_io->link_status = link_status;
82 	for (i = 1; i <= nic_io->max_vfs; i++) {
83 		if (!nic_io->vf_infos[HW_VF_ID_TO_OS(i)].link_forced)
84 			hinic_notify_vf_link_status(hwdev, i,  link_status);
85 	}
86 }
87 
88 static u16 hinic_vf_info_vlanprio(struct hinic_hwdev *hwdev, int vf_id)
89 {
90 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
91 	u16 pf_vlan, vlanprio;
92 	u8 pf_qos;
93 
94 	pf_vlan = nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan;
95 	pf_qos = nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos;
96 	vlanprio = pf_vlan | pf_qos << HINIC_VLAN_PRIORITY_SHIFT;
97 
98 	return vlanprio;
99 }
100 
101 static int hinic_set_vf_vlan(struct hinic_hwdev *hwdev, bool add, u16 vid,
102 			     u8 qos, int vf_id)
103 {
104 	struct hinic_vf_vlan_config vf_vlan = {0};
105 	u16 out_size = sizeof(vf_vlan);
106 	int err;
107 	u8 cmd;
108 
109 	/* VLAN 0 is a special case, don't allow it to be removed */
110 	if (!vid && !add)
111 		return 0;
112 
113 	vf_vlan.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
114 	vf_vlan.vlan_id = vid;
115 	vf_vlan.qos = qos;
116 
117 	if (add)
118 		cmd = HINIC_PORT_CMD_SET_VF_VLAN;
119 	else
120 		cmd = HINIC_PORT_CMD_CLR_VF_VLAN;
121 
122 	err = hinic_port_msg_cmd(hwdev, cmd, &vf_vlan,
123 				 sizeof(vf_vlan), &vf_vlan, &out_size);
124 	if (err || !out_size || vf_vlan.status) {
125 		dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF %d vlan, err: %d, status: 0x%x, out size: 0x%x\n",
126 			HW_VF_ID_TO_OS(vf_id), err, vf_vlan.status, out_size);
127 		return -EFAULT;
128 	}
129 
130 	return 0;
131 }
132 
133 static int hinic_set_vf_tx_rate_max_min(struct hinic_hwdev *hwdev, u16 vf_id,
134 					u32 max_rate, u32 min_rate)
135 {
136 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
137 	struct hinic_tx_rate_cfg_max_min rate_cfg = {0};
138 	u16 out_size = sizeof(rate_cfg);
139 	int err;
140 
141 	rate_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
142 	rate_cfg.max_rate = max_rate;
143 	rate_cfg.min_rate = min_rate;
144 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_VF_MAX_MIN_RATE,
145 				 &rate_cfg, sizeof(rate_cfg), &rate_cfg,
146 				 &out_size);
147 	if ((rate_cfg.status != HINIC_MGMT_CMD_UNSUPPORTED &&
148 	     rate_cfg.status) || err || !out_size) {
149 		dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) max rate(%d), min rate(%d), err: %d, status: 0x%x, out size: 0x%x\n",
150 			HW_VF_ID_TO_OS(vf_id), max_rate, min_rate, err,
151 			rate_cfg.status, out_size);
152 		return -EIO;
153 	}
154 
155 	if (!rate_cfg.status) {
156 		nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].max_rate = max_rate;
157 		nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].min_rate = min_rate;
158 	}
159 
160 	return rate_cfg.status;
161 }
162 
163 static int hinic_set_vf_rate_limit(struct hinic_hwdev *hwdev, u16 vf_id,
164 				   u32 tx_rate)
165 {
166 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
167 	struct hinic_tx_rate_cfg rate_cfg = {0};
168 	u16 out_size = sizeof(rate_cfg);
169 	int err;
170 
171 	rate_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
172 	rate_cfg.tx_rate = tx_rate;
173 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_VF_RATE,
174 				 &rate_cfg, sizeof(rate_cfg), &rate_cfg,
175 				 &out_size);
176 	if (err || !out_size || rate_cfg.status) {
177 		dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) rate(%d), err: %d, status: 0x%x, out size: 0x%x\n",
178 			HW_VF_ID_TO_OS(vf_id), tx_rate, err, rate_cfg.status,
179 			out_size);
180 		if (rate_cfg.status)
181 			return rate_cfg.status;
182 
183 		return -EIO;
184 	}
185 
186 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].max_rate = tx_rate;
187 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].min_rate = 0;
188 
189 	return 0;
190 }
191 
192 static int hinic_set_vf_tx_rate(struct hinic_hwdev *hwdev, u16 vf_id,
193 				u32 max_rate, u32 min_rate)
194 {
195 	int err;
196 
197 	err = hinic_set_vf_tx_rate_max_min(hwdev, vf_id, max_rate, min_rate);
198 	if (err != HINIC_MGMT_CMD_UNSUPPORTED)
199 		return err;
200 
201 	if (min_rate) {
202 		dev_err(&hwdev->hwif->pdev->dev, "Current firmware doesn't support to set min tx rate\n");
203 		return -EOPNOTSUPP;
204 	}
205 
206 	dev_info(&hwdev->hwif->pdev->dev, "Current firmware doesn't support to set min tx rate, force min_tx_rate = max_tx_rate\n");
207 
208 	return hinic_set_vf_rate_limit(hwdev, vf_id, max_rate);
209 }
210 
211 static int hinic_init_vf_config(struct hinic_hwdev *hwdev, u16 vf_id)
212 {
213 	struct vf_data_storage *vf_info;
214 	u16 func_id, vlan_id;
215 	int err = 0;
216 
217 	vf_info = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
218 	if (vf_info->pf_set_mac) {
219 		func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
220 
221 		vlan_id = 0;
222 
223 		err = hinic_set_mac(hwdev, vf_info->vf_mac_addr, vlan_id,
224 				    func_id);
225 		if (err) {
226 			dev_err(&hwdev->func_to_io.hwif->pdev->dev, "Failed to set VF %d MAC\n",
227 				HW_VF_ID_TO_OS(vf_id));
228 			return err;
229 		}
230 	}
231 
232 	if (hinic_vf_info_vlanprio(hwdev, vf_id)) {
233 		err = hinic_set_vf_vlan(hwdev, true, vf_info->pf_vlan,
234 					vf_info->pf_qos, vf_id);
235 		if (err) {
236 			dev_err(&hwdev->hwif->pdev->dev, "Failed to add VF %d VLAN_QOS\n",
237 				HW_VF_ID_TO_OS(vf_id));
238 			return err;
239 		}
240 	}
241 
242 	if (vf_info->max_rate) {
243 		err = hinic_set_vf_tx_rate(hwdev, vf_id, vf_info->max_rate,
244 					   vf_info->min_rate);
245 		if (err) {
246 			dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF %d max rate: %d, min rate: %d\n",
247 				HW_VF_ID_TO_OS(vf_id), vf_info->max_rate,
248 				vf_info->min_rate);
249 			return err;
250 		}
251 	}
252 
253 	return 0;
254 }
255 
256 static int hinic_register_vf_msg_handler(void *hwdev, u16 vf_id,
257 					 void *buf_in, u16 in_size,
258 					 void *buf_out, u16 *out_size)
259 {
260 	struct hinic_register_vf *register_info = buf_out;
261 	struct hinic_hwdev *hw_dev = hwdev;
262 	struct hinic_func_to_io *nic_io;
263 	int err;
264 
265 	nic_io = &hw_dev->func_to_io;
266 	if (vf_id > nic_io->max_vfs) {
267 		dev_err(&hw_dev->hwif->pdev->dev, "Register VF id %d exceed limit[0-%d]\n",
268 			HW_VF_ID_TO_OS(vf_id), HW_VF_ID_TO_OS(nic_io->max_vfs));
269 		register_info->status = EFAULT;
270 		return -EFAULT;
271 	}
272 
273 	*out_size = sizeof(*register_info);
274 	err = hinic_init_vf_config(hw_dev, vf_id);
275 	if (err) {
276 		register_info->status = EFAULT;
277 		return err;
278 	}
279 
280 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = true;
281 
282 	return 0;
283 }
284 
285 static int hinic_unregister_vf_msg_handler(void *hwdev, u16 vf_id,
286 					   void *buf_in, u16 in_size,
287 					   void *buf_out, u16 *out_size)
288 {
289 	struct hinic_hwdev *hw_dev = hwdev;
290 	struct hinic_func_to_io *nic_io;
291 
292 	nic_io = &hw_dev->func_to_io;
293 	*out_size = 0;
294 	if (vf_id > nic_io->max_vfs)
295 		return 0;
296 
297 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].registered = false;
298 
299 	return 0;
300 }
301 
302 static int hinic_change_vf_mtu_msg_handler(void *hwdev, u16 vf_id,
303 					   void *buf_in, u16 in_size,
304 					   void *buf_out, u16 *out_size)
305 {
306 	struct hinic_hwdev *hw_dev = hwdev;
307 	int err;
308 
309 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_CHANGE_MTU, buf_in,
310 				 in_size, buf_out, out_size);
311 	if (err) {
312 		dev_err(&hw_dev->hwif->pdev->dev, "Failed to set VF %u mtu\n",
313 			vf_id);
314 		return err;
315 	}
316 
317 	return 0;
318 }
319 
320 static int hinic_get_vf_mac_msg_handler(void *hwdev, u16 vf_id,
321 					void *buf_in, u16 in_size,
322 					void *buf_out, u16 *out_size)
323 {
324 	struct hinic_port_mac_cmd *mac_info = buf_out;
325 	struct hinic_hwdev *dev = hwdev;
326 	struct hinic_func_to_io *nic_io;
327 	struct vf_data_storage *vf_info;
328 
329 	nic_io = &dev->func_to_io;
330 	vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
331 
332 	memcpy(mac_info->mac, vf_info->vf_mac_addr, ETH_ALEN);
333 	mac_info->status = 0;
334 	*out_size = sizeof(*mac_info);
335 
336 	return 0;
337 }
338 
339 static int hinic_set_vf_mac_msg_handler(void *hwdev, u16 vf_id,
340 					void *buf_in, u16 in_size,
341 					void *buf_out, u16 *out_size)
342 {
343 	struct hinic_port_mac_cmd *mac_out = buf_out;
344 	struct hinic_port_mac_cmd *mac_in = buf_in;
345 	struct hinic_hwdev *hw_dev = hwdev;
346 	struct hinic_func_to_io *nic_io;
347 	struct vf_data_storage *vf_info;
348 	int err;
349 
350 	nic_io =  &hw_dev->func_to_io;
351 	vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
352 	if (vf_info->pf_set_mac && !(vf_info->trust) &&
353 	    is_valid_ether_addr(mac_in->mac)) {
354 		dev_warn(&hw_dev->hwif->pdev->dev, "PF has already set VF %d MAC address\n",
355 			 HW_VF_ID_TO_OS(vf_id));
356 		mac_out->status = HINIC_PF_SET_VF_ALREADY;
357 		*out_size = sizeof(*mac_out);
358 		return 0;
359 	}
360 
361 	err = hinic_port_msg_cmd(hw_dev, HINIC_PORT_CMD_SET_MAC, buf_in,
362 				 in_size, buf_out, out_size);
363 	if ((err &&  err != HINIC_MBOX_PF_BUSY_ACTIVE_FW) || !(*out_size)) {
364 		dev_err(&hw_dev->hwif->pdev->dev,
365 			"Failed to set VF %d MAC address, err: %d, status: 0x%x, out size: 0x%x\n",
366 			HW_VF_ID_TO_OS(vf_id), err, mac_out->status, *out_size);
367 		return -EFAULT;
368 	}
369 
370 	return err;
371 }
372 
373 static int hinic_del_vf_mac_msg_handler(void *hwdev, u16 vf_id,
374 					void *buf_in, u16 in_size,
375 					void *buf_out, u16 *out_size)
376 {
377 	struct hinic_port_mac_cmd *mac_out = buf_out;
378 	struct hinic_port_mac_cmd *mac_in = buf_in;
379 	struct hinic_hwdev *hw_dev = hwdev;
380 	struct hinic_func_to_io *nic_io;
381 	struct vf_data_storage *vf_info;
382 	int err;
383 
384 	nic_io = &hw_dev->func_to_io;
385 	vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf_id);
386 	if (vf_info->pf_set_mac && is_valid_ether_addr(mac_in->mac) &&
387 	    !memcmp(vf_info->vf_mac_addr, mac_in->mac, ETH_ALEN)) {
388 		dev_warn(&hw_dev->hwif->pdev->dev, "PF has already set VF mac.\n");
389 		mac_out->status = HINIC_PF_SET_VF_ALREADY;
390 		*out_size = sizeof(*mac_out);
391 		return 0;
392 	}
393 
394 	err = hinic_port_msg_cmd(hw_dev, HINIC_PORT_CMD_DEL_MAC, buf_in,
395 				 in_size, buf_out, out_size);
396 	if ((err && err != HINIC_MBOX_PF_BUSY_ACTIVE_FW) || !(*out_size)) {
397 		dev_err(&hw_dev->hwif->pdev->dev, "Failed to delete VF %d MAC, err: %d, status: 0x%x, out size: 0x%x\n",
398 			HW_VF_ID_TO_OS(vf_id), err, mac_out->status, *out_size);
399 		return -EFAULT;
400 	}
401 
402 	return err;
403 }
404 
405 static int hinic_get_vf_link_status_msg_handler(void *hwdev, u16 vf_id,
406 						void *buf_in, u16 in_size,
407 						void *buf_out, u16 *out_size)
408 {
409 	struct hinic_port_link_cmd *get_link = buf_out;
410 	struct hinic_hwdev *hw_dev = hwdev;
411 	struct vf_data_storage *vf_infos;
412 	struct hinic_func_to_io *nic_io;
413 	bool link_forced, link_up;
414 
415 	nic_io = &hw_dev->func_to_io;
416 	vf_infos = nic_io->vf_infos;
417 	link_forced = vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced;
418 	link_up = vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up;
419 
420 	if (link_forced)
421 		get_link->state = link_up ?
422 			HINIC_LINK_STATE_UP : HINIC_LINK_STATE_DOWN;
423 	else
424 		get_link->state = nic_io->link_status;
425 
426 	get_link->status = 0;
427 	*out_size = sizeof(*get_link);
428 
429 	return 0;
430 }
431 
432 static bool check_func_table(struct hinic_hwdev *hwdev, u16 func_idx,
433 			     void *buf_in, u16 in_size)
434 {
435 	struct hinic_cmd_fw_ctxt *function_table = buf_in;
436 
437 	if (!hinic_mbox_check_func_id_8B(hwdev, func_idx, buf_in, in_size) ||
438 	    !function_table->rx_buf_sz)
439 		return false;
440 
441 	return true;
442 }
443 
444 static struct vf_cmd_msg_handle nic_vf_cmd_msg_handler[] = {
445 	{HINIC_PORT_CMD_VF_REGISTER, hinic_register_vf_msg_handler},
446 	{HINIC_PORT_CMD_VF_UNREGISTER, hinic_unregister_vf_msg_handler},
447 	{HINIC_PORT_CMD_CHANGE_MTU, hinic_change_vf_mtu_msg_handler},
448 	{HINIC_PORT_CMD_GET_MAC, hinic_get_vf_mac_msg_handler},
449 	{HINIC_PORT_CMD_SET_MAC, hinic_set_vf_mac_msg_handler},
450 	{HINIC_PORT_CMD_DEL_MAC, hinic_del_vf_mac_msg_handler},
451 	{HINIC_PORT_CMD_GET_LINK_STATE, hinic_get_vf_link_status_msg_handler},
452 };
453 
454 static struct vf_cmd_check_handle nic_cmd_support_vf[] = {
455 	{HINIC_PORT_CMD_VF_REGISTER, NULL},
456 	{HINIC_PORT_CMD_VF_UNREGISTER, NULL},
457 	{HINIC_PORT_CMD_CHANGE_MTU, hinic_mbox_check_func_id_8B},
458 	{HINIC_PORT_CMD_ADD_VLAN, hinic_mbox_check_func_id_8B},
459 	{HINIC_PORT_CMD_DEL_VLAN, hinic_mbox_check_func_id_8B},
460 	{HINIC_PORT_CMD_SET_MAC, hinic_mbox_check_func_id_8B},
461 	{HINIC_PORT_CMD_GET_MAC, hinic_mbox_check_func_id_8B},
462 	{HINIC_PORT_CMD_DEL_MAC, hinic_mbox_check_func_id_8B},
463 	{HINIC_PORT_CMD_SET_RX_MODE, hinic_mbox_check_func_id_8B},
464 	{HINIC_PORT_CMD_GET_PAUSE_INFO, hinic_mbox_check_func_id_8B},
465 	{HINIC_PORT_CMD_GET_LINK_STATE, hinic_mbox_check_func_id_8B},
466 	{HINIC_PORT_CMD_SET_LRO, hinic_mbox_check_func_id_8B},
467 	{HINIC_PORT_CMD_SET_RX_CSUM, hinic_mbox_check_func_id_8B},
468 	{HINIC_PORT_CMD_SET_RX_VLAN_OFFLOAD, hinic_mbox_check_func_id_8B},
469 	{HINIC_PORT_CMD_GET_VPORT_STAT, hinic_mbox_check_func_id_8B},
470 	{HINIC_PORT_CMD_CLEAN_VPORT_STAT, hinic_mbox_check_func_id_8B},
471 	{HINIC_PORT_CMD_GET_RSS_TEMPLATE_INDIR_TBL,
472 	 hinic_mbox_check_func_id_8B},
473 	{HINIC_PORT_CMD_SET_RSS_TEMPLATE_TBL, hinic_mbox_check_func_id_8B},
474 	{HINIC_PORT_CMD_GET_RSS_TEMPLATE_TBL, hinic_mbox_check_func_id_8B},
475 	{HINIC_PORT_CMD_SET_RSS_HASH_ENGINE, hinic_mbox_check_func_id_8B},
476 	{HINIC_PORT_CMD_GET_RSS_HASH_ENGINE, hinic_mbox_check_func_id_8B},
477 	{HINIC_PORT_CMD_GET_RSS_CTX_TBL, hinic_mbox_check_func_id_8B},
478 	{HINIC_PORT_CMD_SET_RSS_CTX_TBL, hinic_mbox_check_func_id_8B},
479 	{HINIC_PORT_CMD_RSS_TEMP_MGR, hinic_mbox_check_func_id_8B},
480 	{HINIC_PORT_CMD_RSS_CFG, hinic_mbox_check_func_id_8B},
481 	{HINIC_PORT_CMD_FWCTXT_INIT, check_func_table},
482 	{HINIC_PORT_CMD_GET_MGMT_VERSION, NULL},
483 	{HINIC_PORT_CMD_SET_FUNC_STATE, hinic_mbox_check_func_id_8B},
484 	{HINIC_PORT_CMD_GET_GLOBAL_QPN, hinic_mbox_check_func_id_8B},
485 	{HINIC_PORT_CMD_SET_TSO, hinic_mbox_check_func_id_8B},
486 	{HINIC_PORT_CMD_SET_RQ_IQ_MAP, hinic_mbox_check_func_id_8B},
487 	{HINIC_PORT_CMD_LINK_STATUS_REPORT, hinic_mbox_check_func_id_8B},
488 	{HINIC_PORT_CMD_UPDATE_MAC, hinic_mbox_check_func_id_8B},
489 	{HINIC_PORT_CMD_GET_CAP, hinic_mbox_check_func_id_8B},
490 	{HINIC_PORT_CMD_GET_LINK_MODE, hinic_mbox_check_func_id_8B},
491 };
492 
493 #define CHECK_IPSU_15BIT	0X8000
494 
495 static
496 struct hinic_sriov_info *hinic_get_sriov_info_by_pcidev(struct pci_dev *pdev)
497 {
498 	struct net_device *netdev = pci_get_drvdata(pdev);
499 	struct hinic_dev *nic_dev = netdev_priv(netdev);
500 
501 	return &nic_dev->sriov_info;
502 }
503 
504 static int hinic_check_mac_info(u8 status, u16 vlan_id)
505 {
506 	if ((status && status != HINIC_MGMT_STATUS_EXIST) ||
507 	    (vlan_id & CHECK_IPSU_15BIT &&
508 	     status == HINIC_MGMT_STATUS_EXIST))
509 		return -EINVAL;
510 
511 	return 0;
512 }
513 
514 #define HINIC_VLAN_ID_MASK	0x7FFF
515 
516 static int hinic_update_mac(struct hinic_hwdev *hwdev, u8 *old_mac,
517 			    u8 *new_mac, u16 vlan_id, u16 func_id)
518 {
519 	struct hinic_port_mac_update mac_info = {0};
520 	u16 out_size = sizeof(mac_info);
521 	int err;
522 
523 	if (!hwdev || !old_mac || !new_mac)
524 		return -EINVAL;
525 
526 	if ((vlan_id & HINIC_VLAN_ID_MASK) >= VLAN_N_VID) {
527 		dev_err(&hwdev->hwif->pdev->dev, "Invalid VLAN number: %d\n",
528 			(vlan_id & HINIC_VLAN_ID_MASK));
529 		return -EINVAL;
530 	}
531 
532 	mac_info.func_id = func_id;
533 	mac_info.vlan_id = vlan_id;
534 	memcpy(mac_info.old_mac, old_mac, ETH_ALEN);
535 	memcpy(mac_info.new_mac, new_mac, ETH_ALEN);
536 
537 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_UPDATE_MAC, &mac_info,
538 				 sizeof(mac_info), &mac_info, &out_size);
539 
540 	if (err || !out_size ||
541 	    hinic_check_mac_info(mac_info.status, mac_info.vlan_id)) {
542 		dev_err(&hwdev->hwif->pdev->dev,
543 			"Failed to update MAC, err: %d, status: 0x%x, out size: 0x%x\n",
544 			err, mac_info.status, out_size);
545 		return -EINVAL;
546 	}
547 
548 	if (mac_info.status == HINIC_MGMT_STATUS_EXIST)
549 		dev_warn(&hwdev->hwif->pdev->dev, "MAC is repeated. Ignore update operation\n");
550 
551 	return 0;
552 }
553 
554 static void hinic_get_vf_config(struct hinic_hwdev *hwdev, u16 vf_id,
555 				struct ifla_vf_info *ivi)
556 {
557 	struct vf_data_storage *vfinfo;
558 
559 	vfinfo = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
560 
561 	ivi->vf = HW_VF_ID_TO_OS(vf_id);
562 	memcpy(ivi->mac, vfinfo->vf_mac_addr, ETH_ALEN);
563 	ivi->vlan = vfinfo->pf_vlan;
564 	ivi->qos = vfinfo->pf_qos;
565 	ivi->spoofchk = vfinfo->spoofchk;
566 	ivi->trusted = vfinfo->trust;
567 	ivi->max_tx_rate = vfinfo->max_rate;
568 	ivi->min_tx_rate = vfinfo->min_rate;
569 
570 	if (!vfinfo->link_forced)
571 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
572 	else if (vfinfo->link_up)
573 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
574 	else
575 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
576 }
577 
578 int hinic_ndo_get_vf_config(struct net_device *netdev,
579 			    int vf, struct ifla_vf_info *ivi)
580 {
581 	struct hinic_dev *nic_dev = netdev_priv(netdev);
582 	struct hinic_sriov_info *sriov_info;
583 
584 	sriov_info = &nic_dev->sriov_info;
585 	if (vf >= sriov_info->num_vfs)
586 		return -EINVAL;
587 
588 	hinic_get_vf_config(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), ivi);
589 
590 	return 0;
591 }
592 
593 static int hinic_set_vf_mac(struct hinic_hwdev *hwdev, int vf,
594 			    unsigned char *mac_addr)
595 {
596 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
597 	struct vf_data_storage *vf_info;
598 	u16 func_id;
599 	int err;
600 
601 	vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf);
602 
603 	/* duplicate request, so just return success */
604 	if (vf_info->pf_set_mac &&
605 	    !memcmp(vf_info->vf_mac_addr, mac_addr, ETH_ALEN))
606 		return 0;
607 
608 	vf_info->pf_set_mac = true;
609 
610 	func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf;
611 	err = hinic_update_mac(hwdev, vf_info->vf_mac_addr,
612 			       mac_addr, 0, func_id);
613 	if (err) {
614 		vf_info->pf_set_mac = false;
615 		return err;
616 	}
617 
618 	memcpy(vf_info->vf_mac_addr, mac_addr, ETH_ALEN);
619 
620 	return 0;
621 }
622 
623 int hinic_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
624 {
625 	struct hinic_dev *nic_dev = netdev_priv(netdev);
626 	struct hinic_sriov_info *sriov_info;
627 	int err;
628 
629 	sriov_info = &nic_dev->sriov_info;
630 	if (!is_valid_ether_addr(mac) || vf >= sriov_info->num_vfs)
631 		return -EINVAL;
632 
633 	err = hinic_set_vf_mac(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), mac);
634 	if (err)
635 		return err;
636 
637 	netif_info(nic_dev, drv, netdev, "Setting MAC %pM on VF %d\n", mac, vf);
638 	netif_info(nic_dev, drv, netdev, "Reload the VF driver to make this change effective.");
639 
640 	return 0;
641 }
642 
643 static int hinic_add_vf_vlan(struct hinic_hwdev *hwdev, int vf_id,
644 			     u16 vlan, u8 qos)
645 {
646 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
647 	int err;
648 
649 	err = hinic_set_vf_vlan(hwdev, true, vlan, qos, vf_id);
650 	if (err)
651 		return err;
652 
653 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = vlan;
654 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = qos;
655 
656 	dev_info(&hwdev->hwif->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
657 		 vlan, qos, HW_VF_ID_TO_OS(vf_id));
658 	return 0;
659 }
660 
661 static int hinic_kill_vf_vlan(struct hinic_hwdev *hwdev, int vf_id)
662 {
663 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
664 	int err;
665 
666 	err = hinic_set_vf_vlan(hwdev, false,
667 				nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan,
668 				nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos,
669 				vf_id);
670 	if (err)
671 		return err;
672 
673 	dev_info(&hwdev->hwif->pdev->dev, "Remove VLAN %d on VF %d\n",
674 		 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan,
675 		 HW_VF_ID_TO_OS(vf_id));
676 
677 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = 0;
678 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = 0;
679 
680 	return 0;
681 }
682 
683 static int hinic_update_mac_vlan(struct hinic_dev *nic_dev, u16 old_vlan,
684 				 u16 new_vlan, int vf_id)
685 {
686 	struct vf_data_storage *vf_info;
687 	u16 vlan_id;
688 	int err;
689 
690 	if (!nic_dev || old_vlan >= VLAN_N_VID || new_vlan >= VLAN_N_VID)
691 		return -EINVAL;
692 
693 	vf_info = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
694 	if (!vf_info->pf_set_mac)
695 		return 0;
696 
697 	vlan_id = old_vlan;
698 	if (vlan_id)
699 		vlan_id |= HINIC_ADD_VLAN_IN_MAC;
700 
701 	err = hinic_port_del_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
702 	if (err) {
703 		dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to delete VF %d MAC %pM vlan %d\n",
704 			HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, old_vlan);
705 		return err;
706 	}
707 
708 	vlan_id = new_vlan;
709 	if (vlan_id)
710 		vlan_id |= HINIC_ADD_VLAN_IN_MAC;
711 
712 	err = hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
713 	if (err) {
714 		dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to add VF %d MAC %pM vlan %d\n",
715 			HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, new_vlan);
716 		goto out;
717 	}
718 
719 	return 0;
720 
721 out:
722 	vlan_id = old_vlan;
723 	if (vlan_id)
724 		vlan_id |= HINIC_ADD_VLAN_IN_MAC;
725 	hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
726 
727 	return err;
728 }
729 
730 static int set_hw_vf_vlan(struct hinic_dev *nic_dev,
731 			  u16 cur_vlanprio, int vf, u16 vlan, u8 qos)
732 {
733 	u16 old_vlan = cur_vlanprio & VLAN_VID_MASK;
734 	int err = 0;
735 
736 	if (vlan || qos) {
737 		if (cur_vlanprio) {
738 			err = hinic_kill_vf_vlan(nic_dev->hwdev,
739 						 OS_VF_ID_TO_HW(vf));
740 			if (err) {
741 				dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d old vlan %d\n",
742 					vf, old_vlan);
743 				goto out;
744 			}
745 		}
746 		err = hinic_add_vf_vlan(nic_dev->hwdev,
747 					OS_VF_ID_TO_HW(vf), vlan, qos);
748 		if (err) {
749 			dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to add vf %d new vlan %d\n",
750 				vf, vlan);
751 			goto out;
752 		}
753 	} else {
754 		err = hinic_kill_vf_vlan(nic_dev->hwdev, OS_VF_ID_TO_HW(vf));
755 		if (err) {
756 			dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d vlan %d\n",
757 				vf, old_vlan);
758 			goto out;
759 		}
760 	}
761 
762 	err = hinic_update_mac_vlan(nic_dev, old_vlan, vlan,
763 				    OS_VF_ID_TO_HW(vf));
764 
765 out:
766 	return err;
767 }
768 
769 int hinic_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
770 			  __be16 vlan_proto)
771 {
772 	struct hinic_dev *nic_dev = netdev_priv(netdev);
773 	struct hinic_sriov_info *sriov_info;
774 	u16 vlanprio, cur_vlanprio;
775 
776 	sriov_info = &nic_dev->sriov_info;
777 	if (vf >= sriov_info->num_vfs || vlan > 4095 || qos > 7)
778 		return -EINVAL;
779 	if (vlan_proto != htons(ETH_P_8021Q))
780 		return -EPROTONOSUPPORT;
781 	vlanprio = vlan | qos << HINIC_VLAN_PRIORITY_SHIFT;
782 	cur_vlanprio = hinic_vf_info_vlanprio(nic_dev->hwdev,
783 					      OS_VF_ID_TO_HW(vf));
784 	/* duplicate request, so just return success */
785 	if (vlanprio == cur_vlanprio)
786 		return 0;
787 
788 	return set_hw_vf_vlan(nic_dev, cur_vlanprio, vf, vlan, qos);
789 }
790 
791 static int hinic_set_vf_trust(struct hinic_hwdev *hwdev, u16 vf_id,
792 			      bool trust)
793 {
794 	struct vf_data_storage *vf_infos;
795 	struct hinic_func_to_io *nic_io;
796 
797 	if (!hwdev)
798 		return -EINVAL;
799 
800 	nic_io = &hwdev->func_to_io;
801 	vf_infos = nic_io->vf_infos;
802 	vf_infos[vf_id].trust = trust;
803 
804 	return 0;
805 }
806 
807 int hinic_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
808 {
809 	struct hinic_dev *adapter = netdev_priv(netdev);
810 	struct hinic_sriov_info *sriov_info;
811 	struct hinic_func_to_io *nic_io;
812 	bool cur_trust;
813 	int err;
814 
815 	sriov_info = &adapter->sriov_info;
816 	nic_io = &adapter->hwdev->func_to_io;
817 
818 	if (vf >= sriov_info->num_vfs)
819 		return -EINVAL;
820 
821 	cur_trust = nic_io->vf_infos[vf].trust;
822 	/* same request, so just return success */
823 	if ((setting && cur_trust) || (!setting && !cur_trust))
824 		return 0;
825 
826 	err = hinic_set_vf_trust(adapter->hwdev, vf, setting);
827 	if (!err)
828 		dev_info(&sriov_info->pdev->dev, "Set VF %d trusted %s succeed\n",
829 			 vf, setting ? "on" : "off");
830 	else
831 		dev_err(&sriov_info->pdev->dev, "Failed set VF %d trusted %s\n",
832 			vf, setting ? "on" : "off");
833 
834 	return err;
835 }
836 
837 int hinic_ndo_set_vf_bw(struct net_device *netdev,
838 			int vf, int min_tx_rate, int max_tx_rate)
839 {
840 	static const u32 speeds[] = {
841 		SPEED_10, SPEED_100, SPEED_1000, SPEED_10000,
842 		SPEED_25000, SPEED_40000, SPEED_100000
843 	};
844 	struct hinic_dev *nic_dev = netdev_priv(netdev);
845 	struct hinic_port_cap port_cap = { 0 };
846 	enum hinic_port_link_state link_state;
847 	int err;
848 
849 	if (vf >= nic_dev->sriov_info.num_vfs) {
850 		netif_err(nic_dev, drv, netdev, "VF number must be less than %d\n",
851 			  nic_dev->sriov_info.num_vfs);
852 		return -EINVAL;
853 	}
854 
855 	if (max_tx_rate < min_tx_rate) {
856 		netif_err(nic_dev, drv, netdev, "Max rate %d must be greater than or equal to min rate %d\n",
857 			  max_tx_rate, min_tx_rate);
858 		return -EINVAL;
859 	}
860 
861 	err = hinic_port_link_state(nic_dev, &link_state);
862 	if (err) {
863 		netif_err(nic_dev, drv, netdev,
864 			  "Get link status failed when setting vf tx rate\n");
865 		return -EIO;
866 	}
867 
868 	if (link_state == HINIC_LINK_STATE_DOWN) {
869 		netif_err(nic_dev, drv, netdev,
870 			  "Link status must be up when setting vf tx rate\n");
871 		return -EPERM;
872 	}
873 
874 	err = hinic_port_get_cap(nic_dev, &port_cap);
875 	if (err || port_cap.speed > LINK_SPEED_100GB)
876 		return -EIO;
877 
878 	/* rate limit cannot be less than 0 and greater than link speed */
879 	if (max_tx_rate < 0 || max_tx_rate > speeds[port_cap.speed]) {
880 		netif_err(nic_dev, drv, netdev, "Max tx rate must be in [0 - %d]\n",
881 			  speeds[port_cap.speed]);
882 		return -EINVAL;
883 	}
884 
885 	err = hinic_set_vf_tx_rate(nic_dev->hwdev, OS_VF_ID_TO_HW(vf),
886 				   max_tx_rate, min_tx_rate);
887 	if (err) {
888 		netif_err(nic_dev, drv, netdev,
889 			  "Unable to set VF %d max rate %d min rate %d%s\n",
890 			  vf, max_tx_rate, min_tx_rate,
891 			  err == HINIC_TX_RATE_TABLE_FULL ?
892 			  ", tx rate profile is full" : "");
893 		return -EIO;
894 	}
895 
896 	netif_info(nic_dev, drv, netdev,
897 		   "Set VF %d max tx rate %d min tx rate %d successfully\n",
898 		   vf, max_tx_rate, min_tx_rate);
899 
900 	return 0;
901 }
902 
903 static int hinic_set_vf_spoofchk(struct hinic_hwdev *hwdev, u16 vf_id,
904 				 bool spoofchk)
905 {
906 	struct hinic_spoofchk_set spoofchk_cfg = {0};
907 	struct vf_data_storage *vf_infos = NULL;
908 	u16 out_size = sizeof(spoofchk_cfg);
909 	int err;
910 
911 	if (!hwdev)
912 		return -EINVAL;
913 
914 	vf_infos = hwdev->func_to_io.vf_infos;
915 
916 	spoofchk_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
917 	spoofchk_cfg.state = spoofchk ? 1 : 0;
918 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_ENABLE_SPOOFCHK,
919 				 &spoofchk_cfg, sizeof(spoofchk_cfg),
920 				 &spoofchk_cfg, &out_size);
921 	if (spoofchk_cfg.status == HINIC_MGMT_CMD_UNSUPPORTED) {
922 		err = HINIC_MGMT_CMD_UNSUPPORTED;
923 	} else if (err || !out_size || spoofchk_cfg.status) {
924 		dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) spoofchk, err: %d, status: 0x%x, out size: 0x%x\n",
925 			HW_VF_ID_TO_OS(vf_id), err, spoofchk_cfg.status,
926 			out_size);
927 		err = -EIO;
928 	}
929 
930 	vf_infos[HW_VF_ID_TO_OS(vf_id)].spoofchk = spoofchk;
931 
932 	return err;
933 }
934 
935 int hinic_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
936 {
937 	struct hinic_dev *nic_dev = netdev_priv(netdev);
938 	struct hinic_sriov_info *sriov_info;
939 	bool cur_spoofchk;
940 	int err;
941 
942 	sriov_info = &nic_dev->sriov_info;
943 	if (vf >= sriov_info->num_vfs)
944 		return -EINVAL;
945 
946 	cur_spoofchk = nic_dev->hwdev->func_to_io.vf_infos[vf].spoofchk;
947 
948 	/* same request, so just return success */
949 	if ((setting && cur_spoofchk) || (!setting && !cur_spoofchk))
950 		return 0;
951 
952 	err = hinic_set_vf_spoofchk(sriov_info->hwdev,
953 				    OS_VF_ID_TO_HW(vf), setting);
954 	if (!err) {
955 		netif_info(nic_dev, drv, netdev, "Set VF %d spoofchk %s successfully\n",
956 			   vf, setting ? "on" : "off");
957 	} else if (err == HINIC_MGMT_CMD_UNSUPPORTED) {
958 		netif_err(nic_dev, drv, netdev,
959 			  "Current firmware doesn't support to set vf spoofchk, need to upgrade latest firmware version\n");
960 		err = -EOPNOTSUPP;
961 	}
962 
963 	return err;
964 }
965 
966 static int hinic_set_vf_link_state(struct hinic_hwdev *hwdev, u16 vf_id,
967 				   int link)
968 {
969 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
970 	struct vf_data_storage *vf_infos = nic_io->vf_infos;
971 	u8 link_status = 0;
972 
973 	switch (link) {
974 	case HINIC_IFLA_VF_LINK_STATE_AUTO:
975 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = false;
976 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = nic_io->link_status ?
977 			true : false;
978 		link_status = nic_io->link_status;
979 		break;
980 	case HINIC_IFLA_VF_LINK_STATE_ENABLE:
981 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
982 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = true;
983 		link_status = HINIC_LINK_UP;
984 		break;
985 	case HINIC_IFLA_VF_LINK_STATE_DISABLE:
986 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
987 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = false;
988 		link_status = HINIC_LINK_DOWN;
989 		break;
990 	default:
991 		return -EINVAL;
992 	}
993 
994 	/* Notify the VF of its new link state */
995 	hinic_notify_vf_link_status(hwdev, vf_id, link_status);
996 
997 	return 0;
998 }
999 
1000 int hinic_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
1001 {
1002 	struct hinic_dev *nic_dev = netdev_priv(netdev);
1003 	struct hinic_sriov_info *sriov_info;
1004 
1005 	sriov_info = &nic_dev->sriov_info;
1006 
1007 	if (vf_id >= sriov_info->num_vfs) {
1008 		netif_err(nic_dev, drv, netdev,
1009 			  "Invalid VF Identifier %d\n", vf_id);
1010 		return -EINVAL;
1011 	}
1012 
1013 	return hinic_set_vf_link_state(sriov_info->hwdev,
1014 				      OS_VF_ID_TO_HW(vf_id), link);
1015 }
1016 
1017 /* pf receive message from vf */
1018 static int nic_pf_mbox_handler(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1019 			       u16 in_size, void *buf_out, u16 *out_size)
1020 {
1021 	u8 size = ARRAY_SIZE(nic_cmd_support_vf);
1022 	struct vf_cmd_msg_handle *vf_msg_handle;
1023 	struct hinic_hwdev *dev = hwdev;
1024 	struct hinic_func_to_io *nic_io;
1025 	struct hinic_pfhwdev *pfhwdev;
1026 	int err = 0;
1027 	u32 i;
1028 
1029 	if (!hwdev)
1030 		return -EINVAL;
1031 
1032 	if (!hinic_mbox_check_cmd_valid(hwdev, nic_cmd_support_vf, vf_id, cmd,
1033 					buf_in, in_size, size)) {
1034 		dev_err(&dev->hwif->pdev->dev,
1035 			"PF Receive VF nic cmd: 0x%x, mbox len: 0x%x is invalid\n",
1036 			cmd, in_size);
1037 		return HINIC_MBOX_VF_CMD_ERROR;
1038 	}
1039 
1040 	pfhwdev = container_of(dev, struct hinic_pfhwdev, hwdev);
1041 	nic_io = &dev->func_to_io;
1042 	for (i = 0; i < ARRAY_SIZE(nic_vf_cmd_msg_handler); i++) {
1043 		vf_msg_handle = &nic_vf_cmd_msg_handler[i];
1044 		if (cmd == vf_msg_handle->cmd &&
1045 		    vf_msg_handle->cmd_msg_handler) {
1046 			err = vf_msg_handle->cmd_msg_handler(hwdev, vf_id,
1047 							     buf_in, in_size,
1048 							     buf_out,
1049 							     out_size);
1050 			break;
1051 		}
1052 	}
1053 	if (i == ARRAY_SIZE(nic_vf_cmd_msg_handler))
1054 		err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC,
1055 					cmd, buf_in, in_size, buf_out,
1056 					out_size, HINIC_MGMT_MSG_SYNC);
1057 
1058 	if (err &&  err != HINIC_MBOX_PF_BUSY_ACTIVE_FW)
1059 		dev_err(&nic_io->hwif->pdev->dev, "PF receive VF L2NIC cmd: %d process error, err:%d\n",
1060 			cmd, err);
1061 	return err;
1062 }
1063 
1064 static int cfg_mbx_pf_proc_vf_msg(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1065 				  u16 in_size, void *buf_out, u16 *out_size)
1066 {
1067 	struct hinic_dev_cap *dev_cap = buf_out;
1068 	struct hinic_hwdev *dev = hwdev;
1069 	struct hinic_cap *cap;
1070 
1071 	cap = &dev->nic_cap;
1072 	memset(dev_cap, 0, sizeof(*dev_cap));
1073 
1074 	dev_cap->max_vf = cap->max_vf;
1075 	dev_cap->max_sqs = cap->max_vf_qps;
1076 	dev_cap->max_rqs = cap->max_vf_qps;
1077 	dev_cap->port_id = dev->port_id;
1078 
1079 	*out_size = sizeof(*dev_cap);
1080 
1081 	return 0;
1082 }
1083 
1084 static int hinic_init_vf_infos(struct hinic_func_to_io *nic_io, u16 vf_id)
1085 {
1086 	struct vf_data_storage *vf_infos = nic_io->vf_infos;
1087 
1088 	if (set_vf_link_state > HINIC_IFLA_VF_LINK_STATE_DISABLE) {
1089 		dev_warn(&nic_io->hwif->pdev->dev, "Module Parameter set_vf_link_state value %d is out of range, resetting to %d\n",
1090 			 set_vf_link_state, HINIC_IFLA_VF_LINK_STATE_AUTO);
1091 		set_vf_link_state = HINIC_IFLA_VF_LINK_STATE_AUTO;
1092 	}
1093 
1094 	switch (set_vf_link_state) {
1095 	case HINIC_IFLA_VF_LINK_STATE_AUTO:
1096 		vf_infos[vf_id].link_forced = false;
1097 		break;
1098 	case HINIC_IFLA_VF_LINK_STATE_ENABLE:
1099 		vf_infos[vf_id].link_forced = true;
1100 		vf_infos[vf_id].link_up = true;
1101 		break;
1102 	case HINIC_IFLA_VF_LINK_STATE_DISABLE:
1103 		vf_infos[vf_id].link_forced = true;
1104 		vf_infos[vf_id].link_up = false;
1105 		break;
1106 	default:
1107 		dev_err(&nic_io->hwif->pdev->dev, "Invalid input parameter set_vf_link_state: %d\n",
1108 			set_vf_link_state);
1109 		return -EINVAL;
1110 	}
1111 
1112 	return 0;
1113 }
1114 
1115 static void hinic_clear_vf_infos(struct hinic_dev *nic_dev, u16 vf_id)
1116 {
1117 	struct vf_data_storage *vf_infos;
1118 
1119 	vf_infos = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
1120 	if (vf_infos->pf_set_mac)
1121 		hinic_port_del_mac(nic_dev, vf_infos->vf_mac_addr, 0);
1122 
1123 	if (hinic_vf_info_vlanprio(nic_dev->hwdev, vf_id))
1124 		hinic_kill_vf_vlan(nic_dev->hwdev, vf_id);
1125 
1126 	if (vf_infos->max_rate)
1127 		hinic_set_vf_tx_rate(nic_dev->hwdev, vf_id, 0, 0);
1128 
1129 	if (vf_infos->spoofchk)
1130 		hinic_set_vf_spoofchk(nic_dev->hwdev, vf_id, false);
1131 
1132 	if (vf_infos->trust)
1133 		hinic_set_vf_trust(nic_dev->hwdev, vf_id, false);
1134 
1135 	memset(vf_infos, 0, sizeof(*vf_infos));
1136 	/* set vf_infos to default */
1137 	hinic_init_vf_infos(&nic_dev->hwdev->func_to_io, HW_VF_ID_TO_OS(vf_id));
1138 }
1139 
1140 static int hinic_deinit_vf_hw(struct hinic_sriov_info *sriov_info,
1141 			      u16 start_vf_id, u16 end_vf_id)
1142 {
1143 	struct hinic_dev *nic_dev;
1144 	u16 func_idx, idx;
1145 
1146 	nic_dev = container_of(sriov_info, struct hinic_dev, sriov_info);
1147 
1148 	for (idx = start_vf_id; idx <= end_vf_id; idx++) {
1149 		func_idx = hinic_glb_pf_vf_offset(nic_dev->hwdev->hwif) + idx;
1150 		hinic_set_wq_page_size(nic_dev->hwdev, func_idx,
1151 				       HINIC_HW_WQ_PAGE_SIZE);
1152 		hinic_clear_vf_infos(nic_dev, idx);
1153 	}
1154 
1155 	return 0;
1156 }
1157 
1158 int hinic_vf_func_init(struct hinic_hwdev *hwdev)
1159 {
1160 	struct hinic_register_vf register_info = {0};
1161 	u16 out_size = sizeof(register_info);
1162 	struct hinic_func_to_io *nic_io;
1163 	int err = 0;
1164 	u32 size, i;
1165 
1166 	err = hinic_vf_mbox_random_id_init(hwdev);
1167 	if (err) {
1168 		dev_err(&hwdev->hwif->pdev->dev, "Failed to init vf mbox random id, err: %d\n",
1169 			err);
1170 		return err;
1171 	}
1172 
1173 	nic_io = &hwdev->func_to_io;
1174 
1175 	if (HINIC_IS_VF(hwdev->hwif)) {
1176 		err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1177 				       HINIC_PORT_CMD_VF_REGISTER,
1178 				       &register_info, sizeof(register_info),
1179 				       &register_info, &out_size, 0);
1180 		if (err || register_info.status || !out_size) {
1181 			dev_err(&hwdev->hwif->pdev->dev,
1182 				"Failed to register VF, err: %d, status: 0x%x, out size: 0x%x\n",
1183 				err, register_info.status, out_size);
1184 			hinic_unregister_vf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1185 			return -EIO;
1186 		}
1187 	} else {
1188 		err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_CFGM,
1189 						cfg_mbx_pf_proc_vf_msg);
1190 		if (err) {
1191 			dev_err(&hwdev->hwif->pdev->dev,
1192 				"Register PF mailbox callback failed\n");
1193 			return err;
1194 		}
1195 		nic_io->max_vfs = hwdev->nic_cap.max_vf;
1196 		size = sizeof(*nic_io->vf_infos) * nic_io->max_vfs;
1197 		if (size != 0) {
1198 			nic_io->vf_infos = kzalloc(size, GFP_KERNEL);
1199 			if (!nic_io->vf_infos) {
1200 				err = -ENOMEM;
1201 				goto out_free_nic_io;
1202 			}
1203 
1204 			for (i = 0; i < nic_io->max_vfs; i++) {
1205 				err = hinic_init_vf_infos(nic_io, i);
1206 				if (err)
1207 					goto err_init_vf_infos;
1208 			}
1209 
1210 			err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC,
1211 							nic_pf_mbox_handler);
1212 			if (err)
1213 				goto err_register_pf_mbox_cb;
1214 		}
1215 	}
1216 
1217 	return 0;
1218 
1219 err_register_pf_mbox_cb:
1220 err_init_vf_infos:
1221 	kfree(nic_io->vf_infos);
1222 out_free_nic_io:
1223 	return err;
1224 }
1225 
1226 void hinic_vf_func_free(struct hinic_hwdev *hwdev)
1227 {
1228 	struct hinic_register_vf unregister = {0};
1229 	u16 out_size = sizeof(unregister);
1230 	int err;
1231 
1232 	if (HINIC_IS_VF(hwdev->hwif)) {
1233 		err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1234 				       HINIC_PORT_CMD_VF_UNREGISTER,
1235 				       &unregister, sizeof(unregister),
1236 				       &unregister, &out_size, 0);
1237 		if (err || !out_size || unregister.status)
1238 			dev_err(&hwdev->hwif->pdev->dev, "Failed to unregister VF, err: %d, status: 0x%x, out_size: 0x%x\n",
1239 				err, unregister.status, out_size);
1240 	} else {
1241 		if (hwdev->func_to_io.vf_infos) {
1242 			hinic_unregister_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1243 			kfree(hwdev->func_to_io.vf_infos);
1244 		}
1245 	}
1246 }
1247 
1248 static int hinic_init_vf_hw(struct hinic_hwdev *hwdev, u16 start_vf_id,
1249 			    u16 end_vf_id)
1250 {
1251 	u16 i, func_idx;
1252 	int err;
1253 
1254 	/* vf use 256K as default wq page size, and can't change it */
1255 	for (i = start_vf_id; i <= end_vf_id; i++) {
1256 		func_idx = hinic_glb_pf_vf_offset(hwdev->hwif) + i;
1257 		err = hinic_set_wq_page_size(hwdev, func_idx,
1258 					     HINIC_DEFAULT_WQ_PAGE_SIZE);
1259 		if (err)
1260 			return err;
1261 	}
1262 
1263 	return 0;
1264 }
1265 
1266 int hinic_pci_sriov_disable(struct pci_dev *pdev)
1267 {
1268 	struct hinic_sriov_info *sriov_info;
1269 	u16 tmp_vfs;
1270 
1271 	sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1272 	/* if SR-IOV is already disabled then nothing will be done */
1273 	if (!sriov_info->sriov_enabled)
1274 		return 0;
1275 
1276 	set_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1277 
1278 	/* If our VFs are assigned we cannot shut down SR-IOV
1279 	 * without causing issues, so just leave the hardware
1280 	 * available but disabled
1281 	 */
1282 	if (pci_vfs_assigned(sriov_info->pdev)) {
1283 		clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1284 		dev_warn(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
1285 		return -EPERM;
1286 	}
1287 	sriov_info->sriov_enabled = false;
1288 
1289 	/* disable iov and allow time for transactions to clear */
1290 	pci_disable_sriov(sriov_info->pdev);
1291 
1292 	tmp_vfs = (u16)sriov_info->num_vfs;
1293 	sriov_info->num_vfs = 0;
1294 	hinic_deinit_vf_hw(sriov_info, OS_VF_ID_TO_HW(0),
1295 			   OS_VF_ID_TO_HW(tmp_vfs - 1));
1296 
1297 	clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1298 
1299 	return 0;
1300 }
1301 
1302 int hinic_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1303 {
1304 	struct hinic_sriov_info *sriov_info;
1305 	int err;
1306 
1307 	sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1308 
1309 	if (test_and_set_bit(HINIC_SRIOV_ENABLE, &sriov_info->state)) {
1310 		dev_err(&pdev->dev,
1311 			"SR-IOV enable in process, please wait, num_vfs %d\n",
1312 			num_vfs);
1313 		return -EPERM;
1314 	}
1315 
1316 	err = hinic_init_vf_hw(sriov_info->hwdev, OS_VF_ID_TO_HW(0),
1317 			       OS_VF_ID_TO_HW((u16)num_vfs - 1));
1318 	if (err) {
1319 		dev_err(&sriov_info->pdev->dev,
1320 			"Failed to init vf in hardware before enable sriov, error %d\n",
1321 			err);
1322 		clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1323 		return err;
1324 	}
1325 
1326 	err = pci_enable_sriov(sriov_info->pdev, num_vfs);
1327 	if (err) {
1328 		dev_err(&pdev->dev,
1329 			"Failed to enable SR-IOV, error %d\n", err);
1330 		clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1331 		return err;
1332 	}
1333 
1334 	sriov_info->sriov_enabled = true;
1335 	sriov_info->num_vfs = num_vfs;
1336 	clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1337 
1338 	return num_vfs;
1339 }
1340 
1341 int hinic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1342 {
1343 	struct hinic_sriov_info *sriov_info;
1344 
1345 	sriov_info = hinic_get_sriov_info_by_pcidev(dev);
1346 
1347 	if (test_bit(HINIC_FUNC_REMOVE, &sriov_info->state))
1348 		return -EBUSY;
1349 
1350 	if (!num_vfs)
1351 		return hinic_pci_sriov_disable(dev);
1352 	else
1353 		return hinic_pci_sriov_enable(dev, num_vfs);
1354 }
1355