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 
12 #include "hinic_hw_dev.h"
13 #include "hinic_dev.h"
14 #include "hinic_hw_mbox.h"
15 #include "hinic_hw_cmdq.h"
16 #include "hinic_port.h"
17 #include "hinic_sriov.h"
18 
19 static unsigned char set_vf_link_state;
20 module_param(set_vf_link_state, byte, 0444);
21 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.");
22 
23 #define HINIC_VLAN_PRIORITY_SHIFT 13
24 #define HINIC_ADD_VLAN_IN_MAC 0x8000
25 #define HINIC_TX_RATE_TABLE_FULL 12
26 
27 static int hinic_set_mac(struct hinic_hwdev *hwdev, const u8 *mac_addr,
28 			 u16 vlan_id, u16 func_id)
29 {
30 	struct hinic_port_mac_cmd mac_info = {0};
31 	u16 out_size = sizeof(mac_info);
32 	int err;
33 
34 	mac_info.func_idx = func_id;
35 	mac_info.vlan_id = vlan_id;
36 	memcpy(mac_info.mac, mac_addr, ETH_ALEN);
37 
38 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_SET_MAC, &mac_info,
39 				 sizeof(mac_info), &mac_info, &out_size);
40 	if (err || out_size != sizeof(mac_info) ||
41 	    (mac_info.status && mac_info.status != HINIC_PF_SET_VF_ALREADY &&
42 	    mac_info.status != HINIC_MGMT_STATUS_EXIST)) {
43 		dev_err(&hwdev->func_to_io.hwif->pdev->dev, "Failed to change MAC, ret = %d\n",
44 			mac_info.status);
45 		return -EFAULT;
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 struct vf_cmd_msg_handle nic_vf_cmd_msg_handler[] = {
433 	{HINIC_PORT_CMD_VF_REGISTER, hinic_register_vf_msg_handler},
434 	{HINIC_PORT_CMD_VF_UNREGISTER, hinic_unregister_vf_msg_handler},
435 	{HINIC_PORT_CMD_CHANGE_MTU, hinic_change_vf_mtu_msg_handler},
436 	{HINIC_PORT_CMD_GET_MAC, hinic_get_vf_mac_msg_handler},
437 	{HINIC_PORT_CMD_SET_MAC, hinic_set_vf_mac_msg_handler},
438 	{HINIC_PORT_CMD_DEL_MAC, hinic_del_vf_mac_msg_handler},
439 	{HINIC_PORT_CMD_GET_LINK_STATE, hinic_get_vf_link_status_msg_handler},
440 };
441 
442 #define CHECK_IPSU_15BIT	0X8000
443 
444 static
445 struct hinic_sriov_info *hinic_get_sriov_info_by_pcidev(struct pci_dev *pdev)
446 {
447 	struct net_device *netdev = pci_get_drvdata(pdev);
448 	struct hinic_dev *nic_dev = netdev_priv(netdev);
449 
450 	return &nic_dev->sriov_info;
451 }
452 
453 static int hinic_check_mac_info(u8 status, u16 vlan_id)
454 {
455 	if ((status && status != HINIC_MGMT_STATUS_EXIST &&
456 	     status != HINIC_PF_SET_VF_ALREADY) ||
457 	    (vlan_id & CHECK_IPSU_15BIT &&
458 	     status == HINIC_MGMT_STATUS_EXIST))
459 		return -EINVAL;
460 
461 	return 0;
462 }
463 
464 #define HINIC_VLAN_ID_MASK	0x7FFF
465 
466 static int hinic_update_mac(struct hinic_hwdev *hwdev, u8 *old_mac,
467 			    u8 *new_mac, u16 vlan_id, u16 func_id)
468 {
469 	struct hinic_port_mac_update mac_info = {0};
470 	u16 out_size = sizeof(mac_info);
471 	int err;
472 
473 	if (!hwdev || !old_mac || !new_mac)
474 		return -EINVAL;
475 
476 	if ((vlan_id & HINIC_VLAN_ID_MASK) >= VLAN_N_VID) {
477 		dev_err(&hwdev->hwif->pdev->dev, "Invalid VLAN number: %d\n",
478 			(vlan_id & HINIC_VLAN_ID_MASK));
479 		return -EINVAL;
480 	}
481 
482 	mac_info.func_id = func_id;
483 	mac_info.vlan_id = vlan_id;
484 	memcpy(mac_info.old_mac, old_mac, ETH_ALEN);
485 	memcpy(mac_info.new_mac, new_mac, ETH_ALEN);
486 
487 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_UPDATE_MAC, &mac_info,
488 				 sizeof(mac_info), &mac_info, &out_size);
489 
490 	if (err || !out_size ||
491 	    hinic_check_mac_info(mac_info.status, mac_info.vlan_id)) {
492 		dev_err(&hwdev->hwif->pdev->dev,
493 			"Failed to update MAC, err: %d, status: 0x%x, out size: 0x%x\n",
494 			err, mac_info.status, out_size);
495 		return -EINVAL;
496 	}
497 
498 	if (mac_info.status == HINIC_PF_SET_VF_ALREADY) {
499 		dev_warn(&hwdev->hwif->pdev->dev,
500 			 "PF has already set VF MAC. Ignore update operation\n");
501 		return HINIC_PF_SET_VF_ALREADY;
502 	}
503 
504 	if (mac_info.status == HINIC_MGMT_STATUS_EXIST)
505 		dev_warn(&hwdev->hwif->pdev->dev, "MAC is repeated. Ignore update operation\n");
506 
507 	return 0;
508 }
509 
510 static void hinic_get_vf_config(struct hinic_hwdev *hwdev, u16 vf_id,
511 				struct ifla_vf_info *ivi)
512 {
513 	struct vf_data_storage *vfinfo;
514 
515 	vfinfo = hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
516 
517 	ivi->vf = HW_VF_ID_TO_OS(vf_id);
518 	memcpy(ivi->mac, vfinfo->vf_mac_addr, ETH_ALEN);
519 	ivi->vlan = vfinfo->pf_vlan;
520 	ivi->qos = vfinfo->pf_qos;
521 	ivi->spoofchk = vfinfo->spoofchk;
522 	ivi->trusted = vfinfo->trust;
523 	ivi->max_tx_rate = vfinfo->max_rate;
524 	ivi->min_tx_rate = vfinfo->min_rate;
525 
526 	if (!vfinfo->link_forced)
527 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
528 	else if (vfinfo->link_up)
529 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
530 	else
531 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
532 }
533 
534 int hinic_ndo_get_vf_config(struct net_device *netdev,
535 			    int vf, struct ifla_vf_info *ivi)
536 {
537 	struct hinic_dev *nic_dev = netdev_priv(netdev);
538 	struct hinic_sriov_info *sriov_info;
539 
540 	sriov_info = &nic_dev->sriov_info;
541 	if (vf >= sriov_info->num_vfs)
542 		return -EINVAL;
543 
544 	hinic_get_vf_config(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), ivi);
545 
546 	return 0;
547 }
548 
549 static int hinic_set_vf_mac(struct hinic_hwdev *hwdev, int vf,
550 			    unsigned char *mac_addr)
551 {
552 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
553 	struct vf_data_storage *vf_info;
554 	u16 func_id;
555 	int err;
556 
557 	vf_info = nic_io->vf_infos + HW_VF_ID_TO_OS(vf);
558 
559 	/* duplicate request, so just return success */
560 	if (vf_info->pf_set_mac &&
561 	    !memcmp(vf_info->vf_mac_addr, mac_addr, ETH_ALEN))
562 		return 0;
563 
564 	vf_info->pf_set_mac = true;
565 
566 	func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf;
567 	err = hinic_update_mac(hwdev, vf_info->vf_mac_addr,
568 			       mac_addr, 0, func_id);
569 	if (err) {
570 		vf_info->pf_set_mac = false;
571 		return err;
572 	}
573 
574 	memcpy(vf_info->vf_mac_addr, mac_addr, ETH_ALEN);
575 
576 	return 0;
577 }
578 
579 int hinic_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
580 {
581 	struct hinic_dev *nic_dev = netdev_priv(netdev);
582 	struct hinic_sriov_info *sriov_info;
583 	int err;
584 
585 	sriov_info = &nic_dev->sriov_info;
586 	if (!is_valid_ether_addr(mac) || vf >= sriov_info->num_vfs)
587 		return -EINVAL;
588 
589 	err = hinic_set_vf_mac(sriov_info->hwdev, OS_VF_ID_TO_HW(vf), mac);
590 	if (err)
591 		return err;
592 
593 	netif_info(nic_dev, drv, netdev, "Setting MAC %pM on VF %d\n", mac, vf);
594 	netif_info(nic_dev, drv, netdev, "Reload the VF driver to make this change effective.");
595 
596 	return 0;
597 }
598 
599 static int hinic_add_vf_vlan(struct hinic_hwdev *hwdev, int vf_id,
600 			     u16 vlan, u8 qos)
601 {
602 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
603 	int err;
604 
605 	err = hinic_set_vf_vlan(hwdev, true, vlan, qos, vf_id);
606 	if (err)
607 		return err;
608 
609 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = vlan;
610 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = qos;
611 
612 	dev_info(&hwdev->hwif->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
613 		 vlan, qos, HW_VF_ID_TO_OS(vf_id));
614 	return 0;
615 }
616 
617 static int hinic_kill_vf_vlan(struct hinic_hwdev *hwdev, int vf_id)
618 {
619 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
620 	int err;
621 
622 	err = hinic_set_vf_vlan(hwdev, false,
623 				nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan,
624 				nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos,
625 				vf_id);
626 	if (err)
627 		return err;
628 
629 	dev_info(&hwdev->hwif->pdev->dev, "Remove VLAN %d on VF %d\n",
630 		 nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan,
631 		 HW_VF_ID_TO_OS(vf_id));
632 
633 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_vlan = 0;
634 	nic_io->vf_infos[HW_VF_ID_TO_OS(vf_id)].pf_qos = 0;
635 
636 	return 0;
637 }
638 
639 static int hinic_update_mac_vlan(struct hinic_dev *nic_dev, u16 old_vlan,
640 				 u16 new_vlan, int vf_id)
641 {
642 	struct vf_data_storage *vf_info;
643 	u16 vlan_id;
644 	int err;
645 
646 	if (!nic_dev || old_vlan >= VLAN_N_VID || new_vlan >= VLAN_N_VID)
647 		return -EINVAL;
648 
649 	vf_info = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
650 	if (!vf_info->pf_set_mac)
651 		return 0;
652 
653 	vlan_id = old_vlan;
654 	if (vlan_id)
655 		vlan_id |= HINIC_ADD_VLAN_IN_MAC;
656 
657 	err = hinic_port_del_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
658 	if (err) {
659 		dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to delete VF %d MAC %pM vlan %d\n",
660 			HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, old_vlan);
661 		return err;
662 	}
663 
664 	vlan_id = new_vlan;
665 	if (vlan_id)
666 		vlan_id |= HINIC_ADD_VLAN_IN_MAC;
667 
668 	err = hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
669 	if (err) {
670 		dev_err(&nic_dev->hwdev->hwif->pdev->dev, "Failed to add VF %d MAC %pM vlan %d\n",
671 			HW_VF_ID_TO_OS(vf_id), vf_info->vf_mac_addr, new_vlan);
672 		goto out;
673 	}
674 
675 	return 0;
676 
677 out:
678 	vlan_id = old_vlan;
679 	if (vlan_id)
680 		vlan_id |= HINIC_ADD_VLAN_IN_MAC;
681 	hinic_port_add_mac(nic_dev, vf_info->vf_mac_addr, vlan_id);
682 
683 	return err;
684 }
685 
686 static int set_hw_vf_vlan(struct hinic_dev *nic_dev,
687 			  u16 cur_vlanprio, int vf, u16 vlan, u8 qos)
688 {
689 	u16 old_vlan = cur_vlanprio & VLAN_VID_MASK;
690 	int err = 0;
691 
692 	if (vlan || qos) {
693 		if (cur_vlanprio) {
694 			err = hinic_kill_vf_vlan(nic_dev->hwdev,
695 						 OS_VF_ID_TO_HW(vf));
696 			if (err) {
697 				dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d old vlan %d\n",
698 					vf, old_vlan);
699 				goto out;
700 			}
701 		}
702 		err = hinic_add_vf_vlan(nic_dev->hwdev,
703 					OS_VF_ID_TO_HW(vf), vlan, qos);
704 		if (err) {
705 			dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to add vf %d new vlan %d\n",
706 				vf, vlan);
707 			goto out;
708 		}
709 	} else {
710 		err = hinic_kill_vf_vlan(nic_dev->hwdev, OS_VF_ID_TO_HW(vf));
711 		if (err) {
712 			dev_err(&nic_dev->sriov_info.pdev->dev, "Failed to delete vf %d vlan %d\n",
713 				vf, old_vlan);
714 			goto out;
715 		}
716 	}
717 
718 	err = hinic_update_mac_vlan(nic_dev, old_vlan, vlan,
719 				    OS_VF_ID_TO_HW(vf));
720 
721 out:
722 	return err;
723 }
724 
725 int hinic_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
726 			  __be16 vlan_proto)
727 {
728 	struct hinic_dev *nic_dev = netdev_priv(netdev);
729 	struct hinic_sriov_info *sriov_info;
730 	u16 vlanprio, cur_vlanprio;
731 
732 	sriov_info = &nic_dev->sriov_info;
733 	if (vf >= sriov_info->num_vfs || vlan > 4095 || qos > 7)
734 		return -EINVAL;
735 	if (vlan_proto != htons(ETH_P_8021Q))
736 		return -EPROTONOSUPPORT;
737 	vlanprio = vlan | qos << HINIC_VLAN_PRIORITY_SHIFT;
738 	cur_vlanprio = hinic_vf_info_vlanprio(nic_dev->hwdev,
739 					      OS_VF_ID_TO_HW(vf));
740 	/* duplicate request, so just return success */
741 	if (vlanprio == cur_vlanprio)
742 		return 0;
743 
744 	return set_hw_vf_vlan(nic_dev, cur_vlanprio, vf, vlan, qos);
745 }
746 
747 static int hinic_set_vf_trust(struct hinic_hwdev *hwdev, u16 vf_id,
748 			      bool trust)
749 {
750 	struct vf_data_storage *vf_infos;
751 	struct hinic_func_to_io *nic_io;
752 
753 	if (!hwdev)
754 		return -EINVAL;
755 
756 	nic_io = &hwdev->func_to_io;
757 	vf_infos = nic_io->vf_infos;
758 	vf_infos[vf_id].trust = trust;
759 
760 	return 0;
761 }
762 
763 int hinic_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
764 {
765 	struct hinic_dev *adapter = netdev_priv(netdev);
766 	struct hinic_sriov_info *sriov_info;
767 	struct hinic_func_to_io *nic_io;
768 	bool cur_trust;
769 	int err;
770 
771 	sriov_info = &adapter->sriov_info;
772 	nic_io = &adapter->hwdev->func_to_io;
773 
774 	if (vf >= sriov_info->num_vfs)
775 		return -EINVAL;
776 
777 	cur_trust = nic_io->vf_infos[vf].trust;
778 	/* same request, so just return success */
779 	if ((setting && cur_trust) || (!setting && !cur_trust))
780 		return 0;
781 
782 	err = hinic_set_vf_trust(adapter->hwdev, vf, setting);
783 	if (!err)
784 		dev_info(&sriov_info->pdev->dev, "Set VF %d trusted %s succeed\n",
785 			 vf, setting ? "on" : "off");
786 	else
787 		dev_err(&sriov_info->pdev->dev, "Failed set VF %d trusted %s\n",
788 			vf, setting ? "on" : "off");
789 
790 	return err;
791 }
792 
793 int hinic_ndo_set_vf_bw(struct net_device *netdev,
794 			int vf, int min_tx_rate, int max_tx_rate)
795 {
796 	u32 speeds[] = {SPEED_10, SPEED_100, SPEED_1000, SPEED_10000,
797 			SPEED_25000, SPEED_40000, SPEED_100000};
798 	struct hinic_dev *nic_dev = netdev_priv(netdev);
799 	struct hinic_port_cap port_cap = { 0 };
800 	enum hinic_port_link_state link_state;
801 	int err;
802 
803 	if (vf >= nic_dev->sriov_info.num_vfs) {
804 		netif_err(nic_dev, drv, netdev, "VF number must be less than %d\n",
805 			  nic_dev->sriov_info.num_vfs);
806 		return -EINVAL;
807 	}
808 
809 	if (max_tx_rate < min_tx_rate) {
810 		netif_err(nic_dev, drv, netdev, "Max rate %d must be greater than or equal to min rate %d\n",
811 			  max_tx_rate, min_tx_rate);
812 		return -EINVAL;
813 	}
814 
815 	err = hinic_port_link_state(nic_dev, &link_state);
816 	if (err) {
817 		netif_err(nic_dev, drv, netdev,
818 			  "Get link status failed when setting vf tx rate\n");
819 		return -EIO;
820 	}
821 
822 	if (link_state == HINIC_LINK_STATE_DOWN) {
823 		netif_err(nic_dev, drv, netdev,
824 			  "Link status must be up when setting vf tx rate\n");
825 		return -EPERM;
826 	}
827 
828 	err = hinic_port_get_cap(nic_dev, &port_cap);
829 	if (err || port_cap.speed > LINK_SPEED_100GB)
830 		return -EIO;
831 
832 	/* rate limit cannot be less than 0 and greater than link speed */
833 	if (max_tx_rate < 0 || max_tx_rate > speeds[port_cap.speed]) {
834 		netif_err(nic_dev, drv, netdev, "Max tx rate must be in [0 - %d]\n",
835 			  speeds[port_cap.speed]);
836 		return -EINVAL;
837 	}
838 
839 	err = hinic_set_vf_tx_rate(nic_dev->hwdev, OS_VF_ID_TO_HW(vf),
840 				   max_tx_rate, min_tx_rate);
841 	if (err) {
842 		netif_err(nic_dev, drv, netdev,
843 			  "Unable to set VF %d max rate %d min rate %d%s\n",
844 			  vf, max_tx_rate, min_tx_rate,
845 			  err == HINIC_TX_RATE_TABLE_FULL ?
846 			  ", tx rate profile is full" : "");
847 		return -EIO;
848 	}
849 
850 	netif_info(nic_dev, drv, netdev,
851 		   "Set VF %d max tx rate %d min tx rate %d successfully\n",
852 		   vf, max_tx_rate, min_tx_rate);
853 
854 	return 0;
855 }
856 
857 static int hinic_set_vf_spoofchk(struct hinic_hwdev *hwdev, u16 vf_id,
858 				 bool spoofchk)
859 {
860 	struct hinic_spoofchk_set spoofchk_cfg = {0};
861 	struct vf_data_storage *vf_infos = NULL;
862 	u16 out_size = sizeof(spoofchk_cfg);
863 	int err;
864 
865 	if (!hwdev)
866 		return -EINVAL;
867 
868 	vf_infos = hwdev->func_to_io.vf_infos;
869 
870 	spoofchk_cfg.func_id = hinic_glb_pf_vf_offset(hwdev->hwif) + vf_id;
871 	spoofchk_cfg.state = spoofchk ? 1 : 0;
872 	err = hinic_port_msg_cmd(hwdev, HINIC_PORT_CMD_ENABLE_SPOOFCHK,
873 				 &spoofchk_cfg, sizeof(spoofchk_cfg),
874 				 &spoofchk_cfg, &out_size);
875 	if (spoofchk_cfg.status == HINIC_MGMT_CMD_UNSUPPORTED) {
876 		err = HINIC_MGMT_CMD_UNSUPPORTED;
877 	} else if (err || !out_size || spoofchk_cfg.status) {
878 		dev_err(&hwdev->hwif->pdev->dev, "Failed to set VF(%d) spoofchk, err: %d, status: 0x%x, out size: 0x%x\n",
879 			HW_VF_ID_TO_OS(vf_id), err, spoofchk_cfg.status,
880 			out_size);
881 		err = -EIO;
882 	}
883 
884 	vf_infos[HW_VF_ID_TO_OS(vf_id)].spoofchk = spoofchk;
885 
886 	return err;
887 }
888 
889 int hinic_ndo_set_vf_spoofchk(struct net_device *netdev, int vf, bool setting)
890 {
891 	struct hinic_dev *nic_dev = netdev_priv(netdev);
892 	struct hinic_sriov_info *sriov_info;
893 	bool cur_spoofchk;
894 	int err;
895 
896 	sriov_info = &nic_dev->sriov_info;
897 	if (vf >= sriov_info->num_vfs)
898 		return -EINVAL;
899 
900 	cur_spoofchk = nic_dev->hwdev->func_to_io.vf_infos[vf].spoofchk;
901 
902 	/* same request, so just return success */
903 	if ((setting && cur_spoofchk) || (!setting && !cur_spoofchk))
904 		return 0;
905 
906 	err = hinic_set_vf_spoofchk(sriov_info->hwdev,
907 				    OS_VF_ID_TO_HW(vf), setting);
908 
909 	if (!err) {
910 		netif_info(nic_dev, drv, netdev, "Set VF %d spoofchk %s successfully\n",
911 			   vf, setting ? "on" : "off");
912 	} else if (err == HINIC_MGMT_CMD_UNSUPPORTED) {
913 		netif_err(nic_dev, drv, netdev,
914 			  "Current firmware doesn't support to set vf spoofchk, need to upgrade latest firmware version\n");
915 		err = -EOPNOTSUPP;
916 	}
917 
918 	return err;
919 }
920 
921 static int hinic_set_vf_link_state(struct hinic_hwdev *hwdev, u16 vf_id,
922 				   int link)
923 {
924 	struct hinic_func_to_io *nic_io = &hwdev->func_to_io;
925 	struct vf_data_storage *vf_infos = nic_io->vf_infos;
926 	u8 link_status = 0;
927 
928 	switch (link) {
929 	case HINIC_IFLA_VF_LINK_STATE_AUTO:
930 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = false;
931 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = nic_io->link_status ?
932 			true : false;
933 		link_status = nic_io->link_status;
934 		break;
935 	case HINIC_IFLA_VF_LINK_STATE_ENABLE:
936 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
937 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = true;
938 		link_status = HINIC_LINK_UP;
939 		break;
940 	case HINIC_IFLA_VF_LINK_STATE_DISABLE:
941 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_forced = true;
942 		vf_infos[HW_VF_ID_TO_OS(vf_id)].link_up = false;
943 		link_status = HINIC_LINK_DOWN;
944 		break;
945 	default:
946 		return -EINVAL;
947 	}
948 
949 	/* Notify the VF of its new link state */
950 	hinic_notify_vf_link_status(hwdev, vf_id, link_status);
951 
952 	return 0;
953 }
954 
955 int hinic_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
956 {
957 	struct hinic_dev *nic_dev = netdev_priv(netdev);
958 	struct hinic_sriov_info *sriov_info;
959 
960 	sriov_info = &nic_dev->sriov_info;
961 
962 	if (vf_id >= sriov_info->num_vfs) {
963 		netif_err(nic_dev, drv, netdev,
964 			  "Invalid VF Identifier %d\n", vf_id);
965 		return -EINVAL;
966 	}
967 
968 	return hinic_set_vf_link_state(sriov_info->hwdev,
969 				      OS_VF_ID_TO_HW(vf_id), link);
970 }
971 
972 /* pf receive message from vf */
973 static int nic_pf_mbox_handler(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
974 			       u16 in_size, void *buf_out, u16 *out_size)
975 {
976 	struct vf_cmd_msg_handle *vf_msg_handle;
977 	struct hinic_hwdev *dev = hwdev;
978 	struct hinic_func_to_io *nic_io;
979 	struct hinic_pfhwdev *pfhwdev;
980 	int err = 0;
981 	u32 i;
982 
983 	if (!hwdev)
984 		return -EFAULT;
985 
986 	pfhwdev = container_of(dev, struct hinic_pfhwdev, hwdev);
987 	nic_io = &dev->func_to_io;
988 	for (i = 0; i < ARRAY_SIZE(nic_vf_cmd_msg_handler); i++) {
989 		vf_msg_handle = &nic_vf_cmd_msg_handler[i];
990 		if (cmd == vf_msg_handle->cmd &&
991 		    vf_msg_handle->cmd_msg_handler) {
992 			err = vf_msg_handle->cmd_msg_handler(hwdev, vf_id,
993 							     buf_in, in_size,
994 							     buf_out,
995 							     out_size);
996 			break;
997 		}
998 	}
999 	if (i == ARRAY_SIZE(nic_vf_cmd_msg_handler))
1000 		err = hinic_msg_to_mgmt(&pfhwdev->pf_to_mgmt, HINIC_MOD_L2NIC,
1001 					cmd, buf_in, in_size, buf_out,
1002 					out_size, HINIC_MGMT_MSG_SYNC);
1003 
1004 	if (err &&  err != HINIC_MBOX_PF_BUSY_ACTIVE_FW)
1005 		dev_err(&nic_io->hwif->pdev->dev, "PF receive VF L2NIC cmd: %d process error, err:%d\n",
1006 			cmd, err);
1007 	return err;
1008 }
1009 
1010 static int cfg_mbx_pf_proc_vf_msg(void *hwdev, u16 vf_id, u8 cmd, void *buf_in,
1011 				  u16 in_size, void *buf_out, u16 *out_size)
1012 {
1013 	struct hinic_dev_cap *dev_cap = buf_out;
1014 	struct hinic_hwdev *dev = hwdev;
1015 	struct hinic_cap *cap;
1016 
1017 	cap = &dev->nic_cap;
1018 	memset(dev_cap, 0, sizeof(*dev_cap));
1019 
1020 	dev_cap->max_vf = cap->max_vf;
1021 	dev_cap->max_sqs = cap->max_vf_qps;
1022 	dev_cap->max_rqs = cap->max_vf_qps;
1023 
1024 	*out_size = sizeof(*dev_cap);
1025 
1026 	return 0;
1027 }
1028 
1029 static int hinic_init_vf_infos(struct hinic_func_to_io *nic_io, u16 vf_id)
1030 {
1031 	struct vf_data_storage *vf_infos = nic_io->vf_infos;
1032 
1033 	if (set_vf_link_state > HINIC_IFLA_VF_LINK_STATE_DISABLE) {
1034 		dev_warn(&nic_io->hwif->pdev->dev, "Module Parameter set_vf_link_state value %d is out of range, resetting to %d\n",
1035 			 set_vf_link_state, HINIC_IFLA_VF_LINK_STATE_AUTO);
1036 		set_vf_link_state = HINIC_IFLA_VF_LINK_STATE_AUTO;
1037 	}
1038 
1039 	switch (set_vf_link_state) {
1040 	case HINIC_IFLA_VF_LINK_STATE_AUTO:
1041 		vf_infos[vf_id].link_forced = false;
1042 		break;
1043 	case HINIC_IFLA_VF_LINK_STATE_ENABLE:
1044 		vf_infos[vf_id].link_forced = true;
1045 		vf_infos[vf_id].link_up = true;
1046 		break;
1047 	case HINIC_IFLA_VF_LINK_STATE_DISABLE:
1048 		vf_infos[vf_id].link_forced = true;
1049 		vf_infos[vf_id].link_up = false;
1050 		break;
1051 	default:
1052 		dev_err(&nic_io->hwif->pdev->dev, "Invalid input parameter set_vf_link_state: %d\n",
1053 			set_vf_link_state);
1054 		return -EINVAL;
1055 	}
1056 
1057 	return 0;
1058 }
1059 
1060 static void hinic_clear_vf_infos(struct hinic_dev *nic_dev, u16 vf_id)
1061 {
1062 	struct vf_data_storage *vf_infos;
1063 	u16 func_id;
1064 
1065 	func_id = hinic_glb_pf_vf_offset(nic_dev->hwdev->hwif) + vf_id;
1066 	vf_infos = nic_dev->hwdev->func_to_io.vf_infos + HW_VF_ID_TO_OS(vf_id);
1067 	if (vf_infos->pf_set_mac)
1068 		hinic_port_del_mac(nic_dev, vf_infos->vf_mac_addr, 0);
1069 
1070 	if (hinic_vf_info_vlanprio(nic_dev->hwdev, vf_id))
1071 		hinic_kill_vf_vlan(nic_dev->hwdev, vf_id);
1072 
1073 	if (vf_infos->max_rate)
1074 		hinic_set_vf_tx_rate(nic_dev->hwdev, vf_id, 0, 0);
1075 
1076 	if (vf_infos->spoofchk)
1077 		hinic_set_vf_spoofchk(nic_dev->hwdev, vf_id, false);
1078 
1079 	if (vf_infos->trust)
1080 		hinic_set_vf_trust(nic_dev->hwdev, vf_id, false);
1081 
1082 	memset(vf_infos, 0, sizeof(*vf_infos));
1083 	/* set vf_infos to default */
1084 	hinic_init_vf_infos(&nic_dev->hwdev->func_to_io, HW_VF_ID_TO_OS(vf_id));
1085 }
1086 
1087 static int hinic_deinit_vf_hw(struct hinic_sriov_info *sriov_info,
1088 			      u16 start_vf_id, u16 end_vf_id)
1089 {
1090 	struct hinic_dev *nic_dev;
1091 	u16 func_idx, idx;
1092 
1093 	nic_dev = container_of(sriov_info, struct hinic_dev, sriov_info);
1094 
1095 	for (idx = start_vf_id; idx <= end_vf_id; idx++) {
1096 		func_idx = hinic_glb_pf_vf_offset(nic_dev->hwdev->hwif) + idx;
1097 		hinic_set_wq_page_size(nic_dev->hwdev, func_idx,
1098 				       HINIC_HW_WQ_PAGE_SIZE);
1099 		hinic_clear_vf_infos(nic_dev, idx);
1100 	}
1101 
1102 	return 0;
1103 }
1104 
1105 int hinic_vf_func_init(struct hinic_hwdev *hwdev)
1106 {
1107 	struct hinic_register_vf register_info = {0};
1108 	u16 out_size = sizeof(register_info);
1109 	struct hinic_func_to_io *nic_io;
1110 	int err = 0;
1111 	u32 size, i;
1112 
1113 	nic_io = &hwdev->func_to_io;
1114 
1115 	if (HINIC_IS_VF(hwdev->hwif)) {
1116 		err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1117 				       HINIC_PORT_CMD_VF_REGISTER,
1118 				       &register_info, sizeof(register_info),
1119 				       &register_info, &out_size, 0);
1120 		if (err || register_info.status || !out_size) {
1121 			dev_err(&hwdev->hwif->pdev->dev,
1122 				"Failed to register VF, err: %d, status: 0x%x, out size: 0x%x\n",
1123 				err, register_info.status, out_size);
1124 			hinic_unregister_vf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1125 			return -EIO;
1126 		}
1127 	} else {
1128 		err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_CFGM,
1129 						cfg_mbx_pf_proc_vf_msg);
1130 		if (err) {
1131 			dev_err(&hwdev->hwif->pdev->dev,
1132 				"Register PF mailbox callback failed\n");
1133 			return err;
1134 		}
1135 		nic_io->max_vfs = hwdev->nic_cap.max_vf;
1136 		size = sizeof(*nic_io->vf_infos) * nic_io->max_vfs;
1137 		if (size != 0) {
1138 			nic_io->vf_infos = kzalloc(size, GFP_KERNEL);
1139 			if (!nic_io->vf_infos) {
1140 				err = -ENOMEM;
1141 				goto out_free_nic_io;
1142 			}
1143 
1144 			for (i = 0; i < nic_io->max_vfs; i++) {
1145 				err = hinic_init_vf_infos(nic_io, i);
1146 				if (err)
1147 					goto err_init_vf_infos;
1148 			}
1149 
1150 			err = hinic_register_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC,
1151 							nic_pf_mbox_handler);
1152 			if (err)
1153 				goto err_register_pf_mbox_cb;
1154 		}
1155 	}
1156 
1157 	return 0;
1158 
1159 err_register_pf_mbox_cb:
1160 err_init_vf_infos:
1161 	kfree(nic_io->vf_infos);
1162 out_free_nic_io:
1163 	return err;
1164 }
1165 
1166 void hinic_vf_func_free(struct hinic_hwdev *hwdev)
1167 {
1168 	struct hinic_register_vf unregister = {0};
1169 	u16 out_size = sizeof(unregister);
1170 	int err;
1171 
1172 	if (HINIC_IS_VF(hwdev->hwif)) {
1173 		err = hinic_mbox_to_pf(hwdev, HINIC_MOD_L2NIC,
1174 				       HINIC_PORT_CMD_VF_UNREGISTER,
1175 				       &unregister, sizeof(unregister),
1176 				       &unregister, &out_size, 0);
1177 		if (err || !out_size || unregister.status)
1178 			dev_err(&hwdev->hwif->pdev->dev, "Failed to unregister VF, err: %d, status: 0x%x, out_size: 0x%x\n",
1179 				err, unregister.status, out_size);
1180 	} else {
1181 		if (hwdev->func_to_io.vf_infos) {
1182 			hinic_unregister_pf_mbox_cb(hwdev, HINIC_MOD_L2NIC);
1183 			kfree(hwdev->func_to_io.vf_infos);
1184 		}
1185 	}
1186 }
1187 
1188 static int hinic_init_vf_hw(struct hinic_hwdev *hwdev, u16 start_vf_id,
1189 			    u16 end_vf_id)
1190 {
1191 	u16 i, func_idx;
1192 	int err;
1193 
1194 	/* vf use 256K as default wq page size, and can't change it */
1195 	for (i = start_vf_id; i <= end_vf_id; i++) {
1196 		func_idx = hinic_glb_pf_vf_offset(hwdev->hwif) + i;
1197 		err = hinic_set_wq_page_size(hwdev, func_idx,
1198 					     HINIC_DEFAULT_WQ_PAGE_SIZE);
1199 		if (err)
1200 			return err;
1201 	}
1202 
1203 	return 0;
1204 }
1205 
1206 int hinic_pci_sriov_disable(struct pci_dev *pdev)
1207 {
1208 	struct hinic_sriov_info *sriov_info;
1209 	u16 tmp_vfs;
1210 
1211 	sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1212 	/* if SR-IOV is already disabled then nothing will be done */
1213 	if (!sriov_info->sriov_enabled)
1214 		return 0;
1215 
1216 	set_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1217 
1218 	/* If our VFs are assigned we cannot shut down SR-IOV
1219 	 * without causing issues, so just leave the hardware
1220 	 * available but disabled
1221 	 */
1222 	if (pci_vfs_assigned(sriov_info->pdev)) {
1223 		clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1224 		dev_warn(&pdev->dev, "Unloading driver while VFs are assigned - VFs will not be deallocated\n");
1225 		return -EPERM;
1226 	}
1227 	sriov_info->sriov_enabled = false;
1228 
1229 	/* disable iov and allow time for transactions to clear */
1230 	pci_disable_sriov(sriov_info->pdev);
1231 
1232 	tmp_vfs = (u16)sriov_info->num_vfs;
1233 	sriov_info->num_vfs = 0;
1234 	hinic_deinit_vf_hw(sriov_info, OS_VF_ID_TO_HW(0),
1235 			   OS_VF_ID_TO_HW(tmp_vfs - 1));
1236 
1237 	clear_bit(HINIC_SRIOV_DISABLE, &sriov_info->state);
1238 
1239 	return 0;
1240 }
1241 
1242 int hinic_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1243 {
1244 	struct hinic_sriov_info *sriov_info;
1245 	int err;
1246 
1247 	sriov_info = hinic_get_sriov_info_by_pcidev(pdev);
1248 
1249 	if (test_and_set_bit(HINIC_SRIOV_ENABLE, &sriov_info->state)) {
1250 		dev_err(&pdev->dev,
1251 			"SR-IOV enable in process, please wait, num_vfs %d\n",
1252 			num_vfs);
1253 		return -EPERM;
1254 	}
1255 
1256 	err = hinic_init_vf_hw(sriov_info->hwdev, OS_VF_ID_TO_HW(0),
1257 			       OS_VF_ID_TO_HW((u16)num_vfs - 1));
1258 	if (err) {
1259 		dev_err(&sriov_info->pdev->dev,
1260 			"Failed to init vf in hardware before enable sriov, error %d\n",
1261 			err);
1262 		clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1263 		return err;
1264 	}
1265 
1266 	err = pci_enable_sriov(sriov_info->pdev, num_vfs);
1267 	if (err) {
1268 		dev_err(&pdev->dev,
1269 			"Failed to enable SR-IOV, error %d\n", err);
1270 		clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1271 		return err;
1272 	}
1273 
1274 	sriov_info->sriov_enabled = true;
1275 	sriov_info->num_vfs = num_vfs;
1276 	clear_bit(HINIC_SRIOV_ENABLE, &sriov_info->state);
1277 
1278 	return num_vfs;
1279 }
1280 
1281 int hinic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1282 {
1283 	struct hinic_sriov_info *sriov_info;
1284 
1285 	sriov_info = hinic_get_sriov_info_by_pcidev(dev);
1286 
1287 	if (test_bit(HINIC_FUNC_REMOVE, &sriov_info->state))
1288 		return -EBUSY;
1289 
1290 	if (!num_vfs)
1291 		return hinic_pci_sriov_disable(dev);
1292 	else
1293 		return hinic_pci_sriov_enable(dev, num_vfs);
1294 }
1295