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 	err = hinic_port_link_state(nic_dev, &link_state);
856 	if (err) {
857 		netif_err(nic_dev, drv, netdev,
858 			  "Get link status failed when setting vf tx rate\n");
859 		return -EIO;
860 	}
861 
862 	if (link_state == HINIC_LINK_STATE_DOWN) {
863 		netif_err(nic_dev, drv, netdev,
864 			  "Link status must be up when setting vf tx rate\n");
865 		return -EPERM;
866 	}
867 
868 	err = hinic_port_get_cap(nic_dev, &port_cap);
869 	if (err || port_cap.speed > LINK_SPEED_100GB)
870 		return -EIO;
871 
872 	/* rate limit cannot be less than 0 and greater than link speed */
873 	if (max_tx_rate < 0 || max_tx_rate > speeds[port_cap.speed]) {
874 		netif_err(nic_dev, drv, netdev, "Max tx rate must be in [0 - %d]\n",
875 			  speeds[port_cap.speed]);
876 		return -EINVAL;
877 	}
878 
879 	err = hinic_set_vf_tx_rate(nic_dev->hwdev, OS_VF_ID_TO_HW(vf),
880 				   max_tx_rate, min_tx_rate);
881 	if (err) {
882 		netif_err(nic_dev, drv, netdev,
883 			  "Unable to set VF %d max rate %d min rate %d%s\n",
884 			  vf, max_tx_rate, min_tx_rate,
885 			  err == HINIC_TX_RATE_TABLE_FULL ?
886 			  ", tx rate profile is full" : "");
887 		return -EIO;
888 	}
889 
890 	netif_info(nic_dev, drv, netdev,
891 		   "Set VF %d max tx rate %d min tx rate %d successfully\n",
892 		   vf, max_tx_rate, min_tx_rate);
893 
894 	return 0;
895 }
896 
897 static int hinic_set_vf_spoofchk(struct hinic_hwdev *hwdev, u16 vf_id,
898 				 bool spoofchk)
899 {
900 	struct hinic_spoofchk_set spoofchk_cfg = {0};
901 	struct vf_data_storage *vf_infos = NULL;
902 	u16 out_size = sizeof(spoofchk_cfg);
903 	int err;
904 
905 	if (!hwdev)
906 		return -EINVAL;
907 
908 	vf_infos = hwdev->func_to_io.vf_infos;
909 
910 	spoofchk_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
911 	spoofchk_cfg.state = spoofchk ? 1 : 0;
912 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_ENABLE_SPOOFCHK,
913 				 &spoofchk_cfg, sizeof(spoofchk_cfg),
914 				 &spoofchk_cfg, &out_size);
915 	if (spoofchk_cfg.status == HINIC_MGMT_CMD_UNSUPPORTED) {
916 		err = HINIC_MGMT_CMD_UNSUPPORTED;
917 	} else if (err || !out_size || spoofchk_cfg.status) {
918 		dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) spoofchk, err: %d, status: 0x%x, out size: 0x%x\n",
919 			HW_VF_ID_TO_OS(vf_id), err, spoofchk_cfg.status,
920 			out_size);
921 		err = -EIO;
922 	}
923 
924 	vf_infos[HW_VF_ID_TO_OS(vf_id)].spoofchk = spoofchk;
925 
926 	return err;
927 }
928 
929 int hinic_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
930 {
931 	struct hinic_dev *nic_dev = netdev_priv(netdev);
932 	struct hinic_sriov_info *sriov_info;
933 	bool cur_spoofchk;
934 	int err;
935 
936 	sriov_info = &nic_dev->sriov_info;
937 	if (vf >= sriov_info->num_vfs)
938 		return -EINVAL;
939 
940 	cur_spoofchk = nic_dev->hwdev->func_to_io.vf_infos[vf].spoofchk;
941 
942 	/* same request, so just return success */
943 	if ((setting && cur_spoofchk) || (!setting && !cur_spoofchk))
944 		return 0;
945 
946 	err = hinic_set_vf_spoofchk(sriov_info->hwdev,
947 				    OS_VF_ID_TO_HW(vf), setting);
948 	if (!err) {
949 		netif_info(nic_dev, drv, netdev, "Set VF %d spoofchk %s successfully\n",
950 			   vf, setting ? "on" : "off");
951 	} else if (err == HINIC_MGMT_CMD_UNSUPPORTED) {
952 		netif_err(nic_dev, drv, netdev,
953 			  "Current firmware doesn't support to set vf spoofchk, need to upgrade latest firmware version\n");
954 		err = -EOPNOTSUPP;
955 	}
956 
957 	return err;
958 }
959 
960 static int hinic_set_vf_link_state(struct hinic_hwdev *hwdev, u16 vf_id,
961 				   int link)
962 {
963 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
964 	struct vf_data_storage *vf_infos = nic_io->vf_infos;
965 	u8 link_status = 0;
966 
967 	switch (link) {
968 	case HINIC_IFLA_VF_LINK_STATE_AUTO:
969 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = false;
970 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = nic_io->link_status ?
971 			true : false;
972 		link_status = nic_io->link_status;
973 		break;
974 	case HINIC_IFLA_VF_LINK_STATE_ENABLE:
975 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
976 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = true;
977 		link_status = HINIC_LINK_UP;
978 		break;
979 	case HINIC_IFLA_VF_LINK_STATE_DISABLE:
980 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
981 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = false;
982 		link_status = HINIC_LINK_DOWN;
983 		break;
984 	default:
985 		return -EINVAL;
986 	}
987 
988 	/* Notify the VF of its new link state */
989 	hinic_notify_vf_link_status(hwdev, vf_id, link_status);
990 
991 	return 0;
992 }
993 
994 int hinic_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
995 {
996 	struct hinic_dev *nic_dev = netdev_priv(netdev);
997 	struct hinic_sriov_info *sriov_info;
998 
999 	sriov_info = &nic_dev->sriov_info;
1000 
1001 	if (vf_id >= sriov_info->num_vfs) {
1002 		netif_err(nic_dev, drv, netdev,
1003 			  "Invalid VF Identifier %d\n", vf_id);
1004 		return -EINVAL;
1005 	}
1006 
1007 	return hinic_set_vf_link_state(sriov_info->hwdev,
1008 				      OS_VF_ID_TO_HW(vf_id), link);
1009 }
1010 
1011 /* pf receive message from vf */
1012 static int nic_pf_mbox_handler(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1013 			       u16 in_size, void *buf_out, u16 *out_size)
1014 {
1015 	u8 size = ARRAY_SIZE(nic_cmd_support_vf);
1016 	struct vf_cmd_msg_handle *vf_msg_handle;
1017 	struct hinic_hwdev *dev = hwdev;
1018 	struct hinic_func_to_io *nic_io;
1019 	struct hinic_pfhwdev *pfhwdev;
1020 	int err = 0;
1021 	u32 i;
1022 
1023 	if (!hwdev)
1024 		return -EINVAL;
1025 
1026 	if (!hinic_mbox_check_cmd_valid(hwdev, nic_cmd_support_vf, vf_id, cmd,
1027 					buf_in, in_size, size)) {
1028 		dev_err(&dev->hwif->pdev->dev,
1029 			"PF Receive VF nic cmd: 0x%x, mbox len: 0x%x is invalid\n",
1030 			cmd, in_size);
1031 		return HINIC_MBOX_VF_CMD_ERROR;
1032 	}
1033 
1034 	pfhwdev = container_of(dev, struct hinic_pfhwdev, hwdev);
1035 	nic_io = &dev->func_to_io;
1036 	for (i = 0; i < ARRAY_SIZE(nic_vf_cmd_msg_handler); i++) {
1037 		vf_msg_handle = &nic_vf_cmd_msg_handler[i];
1038 		if (cmd == vf_msg_handle->cmd &&
1039 		    vf_msg_handle->cmd_msg_handler) {
1040 			err = vf_msg_handle->cmd_msg_handler(hwdev, vf_id,
1041 							     buf_in, in_size,
1042 							     buf_out,
1043 							     out_size);
1044 			break;
1045 		}
1046 	}
1047 	if (i == ARRAY_SIZE(nic_vf_cmd_msg_handler))
1048 		err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC,
1049 					cmd, buf_in, in_size, buf_out,
1050 					out_size, HINIC_MGMT_MSG_SYNC);
1051 
1052 	if (err &&  err != HINIC_MBOX_PF_BUSY_ACTIVE_FW)
1053 		dev_err(&nic_io->hwif->pdev->dev, "PF receive VF L2NIC cmd: %d process error, err:%d\n",
1054 			cmd, err);
1055 	return err;
1056 }
1057 
1058 static int cfg_mbx_pf_proc_vf_msg(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1059 				  u16 in_size, void *buf_out, u16 *out_size)
1060 {
1061 	struct hinic_dev_cap *dev_cap = buf_out;
1062 	struct hinic_hwdev *dev = hwdev;
1063 	struct hinic_cap *cap;
1064 
1065 	cap = &dev->nic_cap;
1066 	memset(dev_cap, 0, sizeof(*dev_cap));
1067 
1068 	dev_cap->max_vf = cap->max_vf;
1069 	dev_cap->max_sqs = cap->max_vf_qps;
1070 	dev_cap->max_rqs = cap->max_vf_qps;
1071 	dev_cap->port_id = dev->port_id;
1072 
1073 	*out_size = sizeof(*dev_cap);
1074 
1075 	return 0;
1076 }
1077 
1078 static int hinic_init_vf_infos(struct hinic_func_to_io *nic_io, u16 vf_id)
1079 {
1080 	struct vf_data_storage *vf_infos = nic_io->vf_infos;
1081 
1082 	if (set_vf_link_state > HINIC_IFLA_VF_LINK_STATE_DISABLE) {
1083 		dev_warn(&nic_io->hwif->pdev->dev, "Module Parameter set_vf_link_state value %d is out of range, resetting to %d\n",
1084 			 set_vf_link_state, HINIC_IFLA_VF_LINK_STATE_AUTO);
1085 		set_vf_link_state = HINIC_IFLA_VF_LINK_STATE_AUTO;
1086 	}
1087 
1088 	switch (set_vf_link_state) {
1089 	case HINIC_IFLA_VF_LINK_STATE_AUTO:
1090 		vf_infos[vf_id].link_forced = false;
1091 		break;
1092 	case HINIC_IFLA_VF_LINK_STATE_ENABLE:
1093 		vf_infos[vf_id].link_forced = true;
1094 		vf_infos[vf_id].link_up = true;
1095 		break;
1096 	case HINIC_IFLA_VF_LINK_STATE_DISABLE:
1097 		vf_infos[vf_id].link_forced = true;
1098 		vf_infos[vf_id].link_up = false;
1099 		break;
1100 	default:
1101 		dev_err(&nic_io->hwif->pdev->dev, "Invalid input parameter set_vf_link_state: %d\n",
1102 			set_vf_link_state);
1103 		return -EINVAL;
1104 	}
1105 
1106 	return 0;
1107 }
1108 
1109 static void hinic_clear_vf_infos(struct hinic_dev *nic_dev, u16 vf_id)
1110 {
1111 	struct vf_data_storage *vf_infos;
1112 
1113 	vf_infos = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
1114 	if (vf_infos->pf_set_mac)
1115 		hinic_port_del_mac(nic_dev, vf_infos->vf_mac_addr, 0);
1116 
1117 	if (hinic_vf_info_vlanprio(nic_dev->hwdev, vf_id))
1118 		hinic_kill_vf_vlan(nic_dev->hwdev, vf_id);
1119 
1120 	if (vf_infos->max_rate)
1121 		hinic_set_vf_tx_rate(nic_dev->hwdev, vf_id, 0, 0);
1122 
1123 	if (vf_infos->spoofchk)
1124 		hinic_set_vf_spoofchk(nic_dev->hwdev, vf_id, false);
1125 
1126 	if (vf_infos->trust)
1127 		hinic_set_vf_trust(nic_dev->hwdev, vf_id, false);
1128 
1129 	memset(vf_infos, 0, sizeof(*vf_infos));
1130 	/* set vf_infos to default */
1131 	hinic_init_vf_infos(&nic_dev->hwdev->func_to_io, HW_VF_ID_TO_OS(vf_id));
1132 }
1133 
1134 static int hinic_deinit_vf_hw(struct hinic_sriov_info *sriov_info,
1135 			      u16 start_vf_id, u16 end_vf_id)
1136 {
1137 	struct hinic_dev *nic_dev;
1138 	u16 func_idx, idx;
1139 
1140 	nic_dev = container_of(sriov_info, struct hinic_dev, sriov_info);
1141 
1142 	for (idx = start_vf_id; idx <= end_vf_id; idx++) {
1143 		func_idx = hinic_glb_pf_vf_offset(nic_dev->hwdev->hwif) + idx;
1144 		hinic_set_wq_page_size(nic_dev->hwdev, func_idx,
1145 				       HINIC_HW_WQ_PAGE_SIZE);
1146 		hinic_clear_vf_infos(nic_dev, idx);
1147 	}
1148 
1149 	return 0;
1150 }
1151 
1152 int hinic_vf_func_init(struct hinic_hwdev *hwdev)
1153 {
1154 	struct hinic_register_vf register_info = {0};
1155 	u16 out_size = sizeof(register_info);
1156 	struct hinic_func_to_io *nic_io;
1157 	int err = 0;
1158 	u32 size, i;
1159 
1160 	err = hinic_vf_mbox_random_id_init(hwdev);
1161 	if (err) {
1162 		dev_err(&hwdev->hwif->pdev->dev, "Failed to init vf mbox random id, err: %d\n",
1163 			err);
1164 		return err;
1165 	}
1166 
1167 	nic_io = &hwdev->func_to_io;
1168 
1169 	if (HINIC_IS_VF(hwdev->hwif)) {
1170 		err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1171 				       HINIC_PORT_CMD_VF_REGISTER,
1172 				       &register_info, sizeof(register_info),
1173 				       &register_info, &out_size, 0);
1174 		if (err || register_info.status || !out_size) {
1175 			dev_err(&hwdev->hwif->pdev->dev,
1176 				"Failed to register VF, err: %d, status: 0x%x, out size: 0x%x\n",
1177 				err, register_info.status, out_size);
1178 			hinic_unregister_vf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1179 			return -EIO;
1180 		}
1181 	} else {
1182 		err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_CFGM,
1183 						cfg_mbx_pf_proc_vf_msg);
1184 		if (err) {
1185 			dev_err(&hwdev->hwif->pdev->dev,
1186 				"Register PF mailbox callback failed\n");
1187 			return err;
1188 		}
1189 		nic_io->max_vfs = hwdev->nic_cap.max_vf;
1190 		size = sizeof(*nic_io->vf_infos) * nic_io->max_vfs;
1191 		if (size != 0) {
1192 			nic_io->vf_infos = kzalloc(size, GFP_KERNEL);
1193 			if (!nic_io->vf_infos) {
1194 				err = -ENOMEM;
1195 				goto out_free_nic_io;
1196 			}
1197 
1198 			for (i = 0; i < nic_io->max_vfs; i++) {
1199 				err = hinic_init_vf_infos(nic_io, i);
1200 				if (err)
1201 					goto err_init_vf_infos;
1202 			}
1203 
1204 			err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC,
1205 							nic_pf_mbox_handler);
1206 			if (err)
1207 				goto err_register_pf_mbox_cb;
1208 		}
1209 	}
1210 
1211 	return 0;
1212 
1213 err_register_pf_mbox_cb:
1214 err_init_vf_infos:
1215 	kfree(nic_io->vf_infos);
1216 out_free_nic_io:
1217 	return err;
1218 }
1219 
1220 void hinic_vf_func_free(struct hinic_hwdev *hwdev)
1221 {
1222 	struct hinic_register_vf unregister = {0};
1223 	u16 out_size = sizeof(unregister);
1224 	int err;
1225 
1226 	if (HINIC_IS_VF(hwdev->hwif)) {
1227 		err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1228 				       HINIC_PORT_CMD_VF_UNREGISTER,
1229 				       &unregister, sizeof(unregister),
1230 				       &unregister, &out_size, 0);
1231 		if (err || !out_size || unregister.status)
1232 			dev_err(&hwdev->hwif->pdev->dev, "Failed to unregister VF, err: %d, status: 0x%x, out_size: 0x%x\n",
1233 				err, unregister.status, out_size);
1234 	} else {
1235 		if (hwdev->func_to_io.vf_infos) {
1236 			hinic_unregister_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1237 			kfree(hwdev->func_to_io.vf_infos);
1238 		}
1239 	}
1240 }
1241 
1242 static int hinic_init_vf_hw(struct hinic_hwdev *hwdev, u16 start_vf_id,
1243 			    u16 end_vf_id)
1244 {
1245 	u16 i, func_idx;
1246 	int err;
1247 
1248 	/* vf use 256K as default wq page size, and can't change it */
1249 	for (i = start_vf_id; i <= end_vf_id; i++) {
1250 		func_idx = hinic_glb_pf_vf_offset(hwdev->hwif) + i;
1251 		err = hinic_set_wq_page_size(hwdev, func_idx,
1252 					     HINIC_DEFAULT_WQ_PAGE_SIZE);
1253 		if (err)
1254 			return err;
1255 	}
1256 
1257 	return 0;
1258 }
1259 
1260 int hinic_pci_sriov_disable(struct pci_dev *pdev)
1261 {
1262 	struct hinic_sriov_info *sriov_info;
1263 	u16 tmp_vfs;
1264 
1265 	sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1266 	/* if SR-IOV is already disabled then nothing will be done */
1267 	if (!sriov_info->sriov_enabled)
1268 		return 0;
1269 
1270 	set_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1271 
1272 	/* If our VFs are assigned we cannot shut down SR-IOV
1273 	 * without causing issues, so just leave the hardware
1274 	 * available but disabled
1275 	 */
1276 	if (pci_vfs_assigned(sriov_info->pdev)) {
1277 		clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1278 		dev_warn(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
1279 		return -EPERM;
1280 	}
1281 	sriov_info->sriov_enabled = false;
1282 
1283 	/* disable iov and allow time for transactions to clear */
1284 	pci_disable_sriov(sriov_info->pdev);
1285 
1286 	tmp_vfs = (u16)sriov_info->num_vfs;
1287 	sriov_info->num_vfs = 0;
1288 	hinic_deinit_vf_hw(sriov_info, OS_VF_ID_TO_HW(0),
1289 			   OS_VF_ID_TO_HW(tmp_vfs - 1));
1290 
1291 	clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1292 
1293 	return 0;
1294 }
1295 
1296 int hinic_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1297 {
1298 	struct hinic_sriov_info *sriov_info;
1299 	int err;
1300 
1301 	sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1302 
1303 	if (test_and_set_bit(HINIC_SRIOV_ENABLE, &sriov_info->state)) {
1304 		dev_err(&pdev->dev,
1305 			"SR-IOV enable in process, please wait, num_vfs %d\n",
1306 			num_vfs);
1307 		return -EPERM;
1308 	}
1309 
1310 	err = hinic_init_vf_hw(sriov_info->hwdev, OS_VF_ID_TO_HW(0),
1311 			       OS_VF_ID_TO_HW((u16)num_vfs - 1));
1312 	if (err) {
1313 		dev_err(&sriov_info->pdev->dev,
1314 			"Failed to init vf in hardware before enable sriov, error %d\n",
1315 			err);
1316 		clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1317 		return err;
1318 	}
1319 
1320 	err = pci_enable_sriov(sriov_info->pdev, num_vfs);
1321 	if (err) {
1322 		dev_err(&pdev->dev,
1323 			"Failed to enable SR-IOV, error %d\n", err);
1324 		clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1325 		return err;
1326 	}
1327 
1328 	sriov_info->sriov_enabled = true;
1329 	sriov_info->num_vfs = num_vfs;
1330 	clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1331 
1332 	return num_vfs;
1333 }
1334 
1335 int hinic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1336 {
1337 	struct hinic_sriov_info *sriov_info;
1338 
1339 	sriov_info = hinic_get_sriov_info_by_pcidev(dev);
1340 
1341 	if (test_bit(HINIC_FUNC_REMOVE, &sriov_info->state))
1342 		return -EBUSY;
1343 
1344 	if (!num_vfs)
1345 		return hinic_pci_sriov_disable(dev);
1346 	else
1347 		return hinic_pci_sriov_enable(dev, num_vfs);
1348 }
1349