1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic qlcnic NIC Driver
4  * Copyright (c) 2009-2013 QLogic Corporation
5  */
6 
7 #include <linux/types.h>
8 
9 #include "qlcnic_sriov.h"
10 #include "qlcnic.h"
11 
12 #define QLCNIC_SRIOV_VF_MAX_MAC 7
13 #define QLC_VF_MIN_TX_RATE	100
14 #define QLC_VF_MAX_TX_RATE	9999
15 #define QLC_MAC_OPCODE_MASK	0x7
16 #define QLC_VF_FLOOD_BIT	BIT_16
17 #define QLC_FLOOD_MODE		0x5
18 #define QLC_SRIOV_ALLOW_VLAN0	BIT_19
19 #define QLC_INTR_COAL_TYPE_MASK	0x7
20 
21 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *, u8);
22 
23 struct qlcnic_sriov_cmd_handler {
24 	int (*fn) (struct qlcnic_bc_trans *, struct qlcnic_cmd_args *);
25 };
26 
27 struct qlcnic_sriov_fw_cmd_handler {
28 	u32 cmd;
29 	int (*fn) (struct qlcnic_bc_trans *, struct qlcnic_cmd_args *);
30 };
31 
32 static int qlcnic_sriov_pf_set_vport_info(struct qlcnic_adapter *adapter,
33 					  struct qlcnic_info *npar_info,
34 					  u16 vport_id)
35 {
36 	struct qlcnic_cmd_args cmd;
37 	int err;
38 
39 	if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO))
40 		return -ENOMEM;
41 
42 	cmd.req.arg[1] = (vport_id << 16) | 0x1;
43 	cmd.req.arg[2] = npar_info->bit_offsets;
44 	cmd.req.arg[2] |= npar_info->min_tx_bw << 16;
45 	cmd.req.arg[3] = npar_info->max_tx_bw | (npar_info->max_tx_ques << 16);
46 	cmd.req.arg[4] = npar_info->max_tx_mac_filters;
47 	cmd.req.arg[4] |= npar_info->max_rx_mcast_mac_filters << 16;
48 	cmd.req.arg[5] = npar_info->max_rx_ucast_mac_filters |
49 			 (npar_info->max_rx_ip_addr << 16);
50 	cmd.req.arg[6] = npar_info->max_rx_lro_flow |
51 			 (npar_info->max_rx_status_rings << 16);
52 	cmd.req.arg[7] = npar_info->max_rx_buf_rings |
53 			 (npar_info->max_rx_ques << 16);
54 	cmd.req.arg[8] = npar_info->max_tx_vlan_keys;
55 	cmd.req.arg[8] |= npar_info->max_local_ipv6_addrs << 16;
56 	cmd.req.arg[9] = npar_info->max_remote_ipv6_addrs;
57 
58 	err = qlcnic_issue_cmd(adapter, &cmd);
59 	if (err)
60 		dev_err(&adapter->pdev->dev,
61 			"Failed to set vport info, err=%d\n", err);
62 
63 	qlcnic_free_mbx_args(&cmd);
64 	return err;
65 }
66 
67 static int qlcnic_sriov_pf_cal_res_limit(struct qlcnic_adapter *adapter,
68 					 struct qlcnic_info *info, u16 func)
69 {
70 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
71 	struct qlcnic_resources *res = &sriov->ff_max;
72 	u16 num_macs = sriov->num_allowed_vlans + 1;
73 	int ret = -EIO, vpid, id;
74 	struct qlcnic_vport *vp;
75 	u32 num_vfs, max, temp;
76 
77 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func);
78 	if (vpid < 0)
79 		return -EINVAL;
80 
81 	num_vfs = sriov->num_vfs;
82 	max = num_vfs + 1;
83 	info->bit_offsets = 0xffff;
84 	info->max_tx_ques = res->num_tx_queues / max;
85 
86 	if (qlcnic_83xx_pf_check(adapter))
87 		num_macs = QLCNIC_83XX_SRIOV_VF_MAX_MAC;
88 
89 	info->max_rx_mcast_mac_filters = res->num_rx_mcast_mac_filters;
90 
91 	if (adapter->ahw->pci_func == func) {
92 		info->min_tx_bw = 0;
93 		info->max_tx_bw = MAX_BW;
94 
95 		temp = res->num_rx_ucast_mac_filters - num_macs * num_vfs;
96 		info->max_rx_ucast_mac_filters = temp;
97 		temp = res->num_tx_mac_filters - num_macs * num_vfs;
98 		info->max_tx_mac_filters = temp;
99 		temp = num_macs * num_vfs * QLCNIC_SRIOV_VF_MAX_MAC;
100 		temp = res->num_rx_mcast_mac_filters - temp;
101 		info->max_rx_mcast_mac_filters = temp;
102 
103 		info->max_tx_ques = res->num_tx_queues - sriov->num_vfs;
104 	} else {
105 		id = qlcnic_sriov_func_to_index(adapter, func);
106 		if (id < 0)
107 			return id;
108 		vp = sriov->vf_info[id].vp;
109 		info->min_tx_bw = vp->min_tx_bw;
110 		info->max_tx_bw = vp->max_tx_bw;
111 
112 		info->max_rx_ucast_mac_filters = num_macs;
113 		info->max_tx_mac_filters = num_macs;
114 		temp = num_macs * QLCNIC_SRIOV_VF_MAX_MAC;
115 		info->max_rx_mcast_mac_filters = temp;
116 
117 		info->max_tx_ques = QLCNIC_SINGLE_RING;
118 	}
119 
120 	info->max_rx_ip_addr = res->num_destip / max;
121 	info->max_rx_status_rings = res->num_rx_status_rings / max;
122 	info->max_rx_buf_rings = res->num_rx_buf_rings / max;
123 	info->max_rx_ques = res->num_rx_queues / max;
124 	info->max_rx_lro_flow = res->num_lro_flows_supported / max;
125 	info->max_tx_vlan_keys = res->num_txvlan_keys;
126 	info->max_local_ipv6_addrs = res->max_local_ipv6_addrs;
127 	info->max_remote_ipv6_addrs = res->max_remote_ipv6_addrs;
128 
129 	ret = qlcnic_sriov_pf_set_vport_info(adapter, info, vpid);
130 	if (ret)
131 		return ret;
132 
133 	return 0;
134 }
135 
136 static void qlcnic_sriov_pf_set_ff_max_res(struct qlcnic_adapter *adapter,
137 					   struct qlcnic_info *info)
138 {
139 	struct qlcnic_resources *ff_max = &adapter->ahw->sriov->ff_max;
140 
141 	ff_max->num_tx_mac_filters = info->max_tx_mac_filters;
142 	ff_max->num_rx_ucast_mac_filters = info->max_rx_ucast_mac_filters;
143 	ff_max->num_rx_mcast_mac_filters = info->max_rx_mcast_mac_filters;
144 	ff_max->num_txvlan_keys = info->max_tx_vlan_keys;
145 	ff_max->num_rx_queues = info->max_rx_ques;
146 	ff_max->num_tx_queues = info->max_tx_ques;
147 	ff_max->num_lro_flows_supported = info->max_rx_lro_flow;
148 	ff_max->num_destip = info->max_rx_ip_addr;
149 	ff_max->num_rx_buf_rings = info->max_rx_buf_rings;
150 	ff_max->num_rx_status_rings = info->max_rx_status_rings;
151 	ff_max->max_remote_ipv6_addrs = info->max_remote_ipv6_addrs;
152 	ff_max->max_local_ipv6_addrs = info->max_local_ipv6_addrs;
153 }
154 
155 static void qlcnic_sriov_set_vf_max_vlan(struct qlcnic_adapter *adapter,
156 					 struct qlcnic_info *npar_info)
157 {
158 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
159 	int temp, total_fn;
160 
161 	temp = npar_info->max_rx_mcast_mac_filters;
162 	total_fn = sriov->num_vfs + 1;
163 
164 	temp = temp / (QLCNIC_SRIOV_VF_MAX_MAC * total_fn);
165 	sriov->num_allowed_vlans = temp - 1;
166 
167 	if (qlcnic_83xx_pf_check(adapter))
168 		sriov->num_allowed_vlans = 1;
169 
170 	netdev_info(adapter->netdev, "Max Guest VLANs supported per VF = %d\n",
171 		    sriov->num_allowed_vlans);
172 }
173 
174 static int qlcnic_sriov_get_pf_info(struct qlcnic_adapter *adapter,
175 				    struct qlcnic_info *npar_info)
176 {
177 	int err;
178 	struct qlcnic_cmd_args cmd;
179 
180 	if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_GET_NIC_INFO))
181 		return -ENOMEM;
182 
183 	cmd.req.arg[1] = 0x2;
184 	err = qlcnic_issue_cmd(adapter, &cmd);
185 	if (err) {
186 		dev_err(&adapter->pdev->dev,
187 			"Failed to get PF info, err=%d\n", err);
188 		goto out;
189 	}
190 
191 	npar_info->total_pf = cmd.rsp.arg[2] & 0xff;
192 	npar_info->total_rss_engines = (cmd.rsp.arg[2] >> 8) & 0xff;
193 	npar_info->max_vports = MSW(cmd.rsp.arg[2]);
194 	npar_info->max_tx_ques =  LSW(cmd.rsp.arg[3]);
195 	npar_info->max_tx_mac_filters = MSW(cmd.rsp.arg[3]);
196 	npar_info->max_rx_mcast_mac_filters = LSW(cmd.rsp.arg[4]);
197 	npar_info->max_rx_ucast_mac_filters = MSW(cmd.rsp.arg[4]);
198 	npar_info->max_rx_ip_addr = LSW(cmd.rsp.arg[5]);
199 	npar_info->max_rx_lro_flow = MSW(cmd.rsp.arg[5]);
200 	npar_info->max_rx_status_rings = LSW(cmd.rsp.arg[6]);
201 	npar_info->max_rx_buf_rings = MSW(cmd.rsp.arg[6]);
202 	npar_info->max_rx_ques = LSW(cmd.rsp.arg[7]);
203 	npar_info->max_tx_vlan_keys = MSW(cmd.rsp.arg[7]);
204 	npar_info->max_local_ipv6_addrs = LSW(cmd.rsp.arg[8]);
205 	npar_info->max_remote_ipv6_addrs = MSW(cmd.rsp.arg[8]);
206 
207 	qlcnic_sriov_set_vf_max_vlan(adapter, npar_info);
208 	qlcnic_sriov_pf_set_ff_max_res(adapter, npar_info);
209 	dev_info(&adapter->pdev->dev,
210 		 "\n\ttotal_pf: %d,\n"
211 		 "\n\ttotal_rss_engines: %d max_vports: %d max_tx_ques %d,\n"
212 		 "\tmax_tx_mac_filters: %d max_rx_mcast_mac_filters: %d,\n"
213 		 "\tmax_rx_ucast_mac_filters: 0x%x, max_rx_ip_addr: %d,\n"
214 		 "\tmax_rx_lro_flow: %d max_rx_status_rings: %d,\n"
215 		 "\tmax_rx_buf_rings: %d, max_rx_ques: %d, max_tx_vlan_keys %d\n"
216 		 "\tmax_local_ipv6_addrs: %d, max_remote_ipv6_addrs: %d\n",
217 		 npar_info->total_pf, npar_info->total_rss_engines,
218 		 npar_info->max_vports, npar_info->max_tx_ques,
219 		 npar_info->max_tx_mac_filters,
220 		 npar_info->max_rx_mcast_mac_filters,
221 		 npar_info->max_rx_ucast_mac_filters, npar_info->max_rx_ip_addr,
222 		 npar_info->max_rx_lro_flow, npar_info->max_rx_status_rings,
223 		 npar_info->max_rx_buf_rings, npar_info->max_rx_ques,
224 		 npar_info->max_tx_vlan_keys, npar_info->max_local_ipv6_addrs,
225 		 npar_info->max_remote_ipv6_addrs);
226 
227 out:
228 	qlcnic_free_mbx_args(&cmd);
229 	return err;
230 }
231 
232 static void qlcnic_sriov_pf_reset_vport_handle(struct qlcnic_adapter *adapter,
233 					       u8 func)
234 {
235 	struct qlcnic_sriov  *sriov = adapter->ahw->sriov;
236 	struct qlcnic_vport *vp;
237 	int index;
238 
239 	if (adapter->ahw->pci_func == func) {
240 		sriov->vp_handle = 0;
241 	} else {
242 		index = qlcnic_sriov_func_to_index(adapter, func);
243 		if (index < 0)
244 			return;
245 		vp = sriov->vf_info[index].vp;
246 		vp->handle = 0;
247 	}
248 }
249 
250 static void qlcnic_sriov_pf_set_vport_handle(struct qlcnic_adapter *adapter,
251 					     u16 vport_handle, u8 func)
252 {
253 	struct qlcnic_sriov  *sriov = adapter->ahw->sriov;
254 	struct qlcnic_vport *vp;
255 	int index;
256 
257 	if (adapter->ahw->pci_func == func) {
258 		sriov->vp_handle = vport_handle;
259 	} else {
260 		index = qlcnic_sriov_func_to_index(adapter, func);
261 		if (index < 0)
262 			return;
263 		vp = sriov->vf_info[index].vp;
264 		vp->handle = vport_handle;
265 	}
266 }
267 
268 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *adapter,
269 					    u8 func)
270 {
271 	struct qlcnic_sriov  *sriov = adapter->ahw->sriov;
272 	struct qlcnic_vf_info *vf_info;
273 	int index;
274 
275 	if (adapter->ahw->pci_func == func) {
276 		return sriov->vp_handle;
277 	} else {
278 		index = qlcnic_sriov_func_to_index(adapter, func);
279 		if (index >= 0) {
280 			vf_info = &sriov->vf_info[index];
281 			return vf_info->vp->handle;
282 		}
283 	}
284 
285 	return -EINVAL;
286 }
287 
288 static int qlcnic_sriov_pf_config_vport(struct qlcnic_adapter *adapter,
289 					u8 flag, u16 func)
290 {
291 	struct qlcnic_cmd_args cmd;
292 	int ret;
293 	int vpid;
294 
295 	if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_CONFIG_VPORT))
296 		return -ENOMEM;
297 
298 	if (flag) {
299 		cmd.req.arg[3] = func << 8;
300 	} else {
301 		vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func);
302 		if (vpid < 0) {
303 			ret = -EINVAL;
304 			goto out;
305 		}
306 		cmd.req.arg[3] = ((vpid & 0xffff) << 8) | 1;
307 	}
308 
309 	ret = qlcnic_issue_cmd(adapter, &cmd);
310 	if (ret) {
311 		dev_err(&adapter->pdev->dev,
312 			"Failed %s vport, err %d for func 0x%x\n",
313 			(flag ? "enable" : "disable"), ret, func);
314 		goto out;
315 	}
316 
317 	if (flag) {
318 		vpid = cmd.rsp.arg[2] & 0xffff;
319 		qlcnic_sriov_pf_set_vport_handle(adapter, vpid, func);
320 	} else {
321 		qlcnic_sriov_pf_reset_vport_handle(adapter, func);
322 	}
323 
324 out:
325 	qlcnic_free_mbx_args(&cmd);
326 	return ret;
327 }
328 
329 static int qlcnic_sriov_pf_cfg_vlan_filtering(struct qlcnic_adapter *adapter,
330 					      u8 enable)
331 {
332 	struct qlcnic_cmd_args cmd;
333 	int err;
334 
335 	err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO);
336 	if (err)
337 		return err;
338 
339 	cmd.req.arg[1] = 0x4;
340 	if (enable) {
341 		adapter->flags |= QLCNIC_VLAN_FILTERING;
342 		cmd.req.arg[1] |= BIT_16;
343 		if (qlcnic_84xx_check(adapter))
344 			cmd.req.arg[1] |= QLC_SRIOV_ALLOW_VLAN0;
345 	} else {
346 		adapter->flags &= ~QLCNIC_VLAN_FILTERING;
347 	}
348 
349 	err = qlcnic_issue_cmd(adapter, &cmd);
350 	if (err)
351 		dev_err(&adapter->pdev->dev,
352 			"Failed to configure VLAN filtering, err=%d\n", err);
353 
354 	qlcnic_free_mbx_args(&cmd);
355 	return err;
356 }
357 
358 /* On configuring VF flood bit, PFD will receive traffic from all VFs */
359 static int qlcnic_sriov_pf_cfg_flood(struct qlcnic_adapter *adapter)
360 {
361 	struct qlcnic_cmd_args cmd;
362 	int err;
363 
364 	err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO);
365 	if (err)
366 		return err;
367 
368 	cmd.req.arg[1] = QLC_FLOOD_MODE | QLC_VF_FLOOD_BIT;
369 
370 	err = qlcnic_issue_cmd(adapter, &cmd);
371 	if (err)
372 		dev_err(&adapter->pdev->dev,
373 			"Failed to configure VF Flood bit on PF, err=%d\n",
374 			err);
375 
376 	qlcnic_free_mbx_args(&cmd);
377 	return err;
378 }
379 
380 static int qlcnic_sriov_pf_cfg_eswitch(struct qlcnic_adapter *adapter,
381 				       u8 func, u8 enable)
382 {
383 	struct qlcnic_cmd_args cmd;
384 	int err = -EIO;
385 
386 	if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_TOGGLE_ESWITCH))
387 		return -ENOMEM;
388 
389 	cmd.req.arg[0] |= (3 << 29);
390 	cmd.req.arg[1] = ((func & 0xf) << 2) | BIT_6 | BIT_1;
391 	if (enable)
392 		cmd.req.arg[1] |= BIT_0;
393 
394 	err = qlcnic_issue_cmd(adapter, &cmd);
395 
396 	if (err != QLCNIC_RCODE_SUCCESS) {
397 		dev_err(&adapter->pdev->dev,
398 			"Failed to enable sriov eswitch%d\n", err);
399 		err = -EIO;
400 	}
401 
402 	qlcnic_free_mbx_args(&cmd);
403 	return err;
404 }
405 
406 static void qlcnic_sriov_pf_del_flr_queue(struct qlcnic_adapter *adapter)
407 {
408 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
409 	struct qlcnic_back_channel *bc = &sriov->bc;
410 	int i;
411 
412 	for (i = 0; i < sriov->num_vfs; i++)
413 		cancel_work_sync(&sriov->vf_info[i].flr_work);
414 
415 	destroy_workqueue(bc->bc_flr_wq);
416 }
417 
418 static int qlcnic_sriov_pf_create_flr_queue(struct qlcnic_adapter *adapter)
419 {
420 	struct qlcnic_back_channel *bc = &adapter->ahw->sriov->bc;
421 	struct workqueue_struct *wq;
422 
423 	wq = create_singlethread_workqueue("qlcnic-flr");
424 	if (wq == NULL) {
425 		dev_err(&adapter->pdev->dev, "Cannot create FLR workqueue\n");
426 		return -ENOMEM;
427 	}
428 
429 	bc->bc_flr_wq =  wq;
430 	return 0;
431 }
432 
433 void qlcnic_sriov_pf_cleanup(struct qlcnic_adapter *adapter)
434 {
435 	u8 func = adapter->ahw->pci_func;
436 
437 	if (!qlcnic_sriov_enable_check(adapter))
438 		return;
439 
440 	qlcnic_sriov_pf_del_flr_queue(adapter);
441 	qlcnic_sriov_cfg_bc_intr(adapter, 0);
442 	qlcnic_sriov_pf_config_vport(adapter, 0, func);
443 	qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0);
444 	qlcnic_sriov_pf_cfg_vlan_filtering(adapter, 0);
445 	__qlcnic_sriov_cleanup(adapter);
446 	adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
447 	clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
448 }
449 
450 void qlcnic_sriov_pf_disable(struct qlcnic_adapter *adapter)
451 {
452 	if (!qlcnic_sriov_pf_check(adapter))
453 		return;
454 
455 	if (!qlcnic_sriov_enable_check(adapter))
456 		return;
457 
458 	pci_disable_sriov(adapter->pdev);
459 	netdev_info(adapter->netdev,
460 		    "SR-IOV is disabled successfully on port %d\n",
461 		    adapter->portnum);
462 }
463 
464 static int qlcnic_pci_sriov_disable(struct qlcnic_adapter *adapter)
465 {
466 	struct net_device *netdev = adapter->netdev;
467 
468 	if (pci_vfs_assigned(adapter->pdev)) {
469 		netdev_err(adapter->netdev,
470 			   "SR-IOV VFs belonging to port %d are assigned to VMs. SR-IOV can not be disabled on this port\n",
471 			   adapter->portnum);
472 		netdev_info(adapter->netdev,
473 			    "Please detach SR-IOV VFs belonging to port %d from VMs, and then try to disable SR-IOV on this port\n",
474 			    adapter->portnum);
475 		return -EPERM;
476 	}
477 
478 	qlcnic_sriov_pf_disable(adapter);
479 
480 	rtnl_lock();
481 	if (netif_running(netdev))
482 		__qlcnic_down(adapter, netdev);
483 
484 	qlcnic_sriov_free_vlans(adapter);
485 
486 	qlcnic_sriov_pf_cleanup(adapter);
487 
488 	/* After disabling SRIOV re-init the driver in default mode
489 	   configure opmode based on op_mode of function
490 	 */
491 	if (qlcnic_83xx_configure_opmode(adapter)) {
492 		rtnl_unlock();
493 		return -EIO;
494 	}
495 
496 	if (netif_running(netdev))
497 		__qlcnic_up(adapter, netdev);
498 
499 	rtnl_unlock();
500 	return 0;
501 }
502 
503 static int qlcnic_sriov_pf_init(struct qlcnic_adapter *adapter)
504 {
505 	struct qlcnic_hardware_context *ahw = adapter->ahw;
506 	struct qlcnic_info nic_info, pf_info, vp_info;
507 	int err;
508 	u8 func = ahw->pci_func;
509 
510 	if (!qlcnic_sriov_enable_check(adapter))
511 		return 0;
512 
513 	err = qlcnic_sriov_pf_cfg_vlan_filtering(adapter, 1);
514 	if (err)
515 		return err;
516 
517 	if (qlcnic_84xx_check(adapter)) {
518 		err = qlcnic_sriov_pf_cfg_flood(adapter);
519 		if (err)
520 			goto disable_vlan_filtering;
521 	}
522 
523 	err = qlcnic_sriov_pf_cfg_eswitch(adapter, func, 1);
524 	if (err)
525 		goto disable_vlan_filtering;
526 
527 	err = qlcnic_sriov_pf_config_vport(adapter, 1, func);
528 	if (err)
529 		goto disable_eswitch;
530 
531 	err = qlcnic_sriov_get_pf_info(adapter, &pf_info);
532 	if (err)
533 		goto delete_vport;
534 
535 	err = qlcnic_get_nic_info(adapter, &nic_info, func);
536 	if (err)
537 		goto delete_vport;
538 
539 	err = qlcnic_sriov_pf_cal_res_limit(adapter, &vp_info, func);
540 	if (err)
541 		goto delete_vport;
542 
543 	err = qlcnic_sriov_cfg_bc_intr(adapter, 1);
544 	if (err)
545 		goto delete_vport;
546 
547 	ahw->physical_port = (u8) nic_info.phys_port;
548 	ahw->switch_mode = nic_info.switch_mode;
549 	ahw->max_mtu = nic_info.max_mtu;
550 	ahw->capabilities = nic_info.capabilities;
551 	ahw->nic_mode = QLC_83XX_SRIOV_MODE;
552 	return err;
553 
554 delete_vport:
555 	qlcnic_sriov_pf_config_vport(adapter, 0, func);
556 
557 disable_eswitch:
558 	qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0);
559 
560 disable_vlan_filtering:
561 	qlcnic_sriov_pf_cfg_vlan_filtering(adapter, 0);
562 
563 	return err;
564 }
565 
566 static int qlcnic_sriov_pf_enable(struct qlcnic_adapter *adapter, int num_vfs)
567 {
568 	int err;
569 
570 	if (!qlcnic_sriov_enable_check(adapter))
571 		return 0;
572 
573 	err = pci_enable_sriov(adapter->pdev, num_vfs);
574 	if (err)
575 		qlcnic_sriov_pf_cleanup(adapter);
576 
577 	return err;
578 }
579 
580 static int __qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter,
581 				     int num_vfs)
582 {
583 	int err = 0;
584 
585 	set_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
586 	adapter->ahw->op_mode = QLCNIC_SRIOV_PF_FUNC;
587 
588 	err = qlcnic_sriov_init(adapter, num_vfs);
589 	if (err)
590 		goto clear_op_mode;
591 
592 	err = qlcnic_sriov_pf_create_flr_queue(adapter);
593 	if (err)
594 		goto sriov_cleanup;
595 
596 	err = qlcnic_sriov_pf_init(adapter);
597 	if (err)
598 		goto del_flr_queue;
599 
600 	err = qlcnic_sriov_alloc_vlans(adapter);
601 	if (err)
602 		goto del_flr_queue;
603 
604 	return err;
605 
606 del_flr_queue:
607 	qlcnic_sriov_pf_del_flr_queue(adapter);
608 
609 sriov_cleanup:
610 	__qlcnic_sriov_cleanup(adapter);
611 
612 clear_op_mode:
613 	clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
614 	adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
615 	return err;
616 }
617 
618 static int qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter, int num_vfs)
619 {
620 	struct net_device *netdev = adapter->netdev;
621 	int err;
622 
623 	if (!(adapter->flags & QLCNIC_MSIX_ENABLED)) {
624 		netdev_err(netdev,
625 			   "SR-IOV cannot be enabled, when legacy interrupts are enabled\n");
626 		return -EIO;
627 	}
628 
629 	rtnl_lock();
630 	if (netif_running(netdev))
631 		__qlcnic_down(adapter, netdev);
632 
633 	err = __qlcnic_pci_sriov_enable(adapter, num_vfs);
634 	if (err)
635 		goto error;
636 
637 	if (netif_running(netdev))
638 		__qlcnic_up(adapter, netdev);
639 
640 	rtnl_unlock();
641 	err = qlcnic_sriov_pf_enable(adapter, num_vfs);
642 	if (!err) {
643 		netdev_info(netdev,
644 			    "SR-IOV is enabled successfully on port %d\n",
645 			    adapter->portnum);
646 		/* Return number of vfs enabled */
647 		return num_vfs;
648 	}
649 
650 	rtnl_lock();
651 	if (netif_running(netdev))
652 		__qlcnic_down(adapter, netdev);
653 
654 error:
655 	if (!qlcnic_83xx_configure_opmode(adapter)) {
656 		if (netif_running(netdev))
657 			__qlcnic_up(adapter, netdev);
658 	}
659 
660 	rtnl_unlock();
661 	netdev_info(netdev, "Failed to enable SR-IOV on port %d\n",
662 		    adapter->portnum);
663 
664 	return err;
665 }
666 
667 int qlcnic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
668 {
669 	struct qlcnic_adapter *adapter = pci_get_drvdata(dev);
670 	int err;
671 
672 	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
673 		return -EBUSY;
674 
675 	if (num_vfs == 0)
676 		err = qlcnic_pci_sriov_disable(adapter);
677 	else
678 		err = qlcnic_pci_sriov_enable(adapter, num_vfs);
679 
680 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
681 	return err;
682 }
683 
684 static int qlcnic_sriov_set_vf_acl(struct qlcnic_adapter *adapter, u8 func)
685 {
686 	struct qlcnic_cmd_args cmd;
687 	struct qlcnic_vport *vp;
688 	int err, id;
689 	u8 *mac;
690 
691 	id = qlcnic_sriov_func_to_index(adapter, func);
692 	if (id < 0)
693 		return id;
694 
695 	vp = adapter->ahw->sriov->vf_info[id].vp;
696 	err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO);
697 	if (err)
698 		return err;
699 
700 	cmd.req.arg[1] = 0x3 | func << 16;
701 	if (vp->spoofchk == true) {
702 		mac = vp->mac;
703 		cmd.req.arg[2] |= BIT_1 | BIT_3 | BIT_8;
704 		cmd.req.arg[4] = mac[5] | mac[4] << 8 | mac[3] << 16 |
705 				 mac[2] << 24;
706 		cmd.req.arg[5] = mac[1] | mac[0] << 8;
707 	}
708 
709 	if (vp->vlan_mode == QLC_PVID_MODE) {
710 		cmd.req.arg[2] |= BIT_6;
711 		cmd.req.arg[3] |= vp->pvid << 8;
712 	}
713 
714 	err = qlcnic_issue_cmd(adapter, &cmd);
715 	if (err)
716 		dev_err(&adapter->pdev->dev, "Failed to set ACL, err=%d\n",
717 			err);
718 
719 	qlcnic_free_mbx_args(&cmd);
720 	return err;
721 }
722 
723 static int qlcnic_sriov_set_vf_vport_info(struct qlcnic_adapter *adapter,
724 					  u16 func)
725 {
726 	struct qlcnic_info defvp_info;
727 	int err;
728 
729 	err = qlcnic_sriov_pf_cal_res_limit(adapter, &defvp_info, func);
730 	if (err)
731 		return -EIO;
732 
733 	err = qlcnic_sriov_set_vf_acl(adapter, func);
734 	if (err)
735 		return err;
736 
737 	return 0;
738 }
739 
740 static int qlcnic_sriov_pf_channel_cfg_cmd(struct qlcnic_bc_trans *trans,
741 					   struct qlcnic_cmd_args *cmd)
742 {
743 	struct qlcnic_vf_info *vf = trans->vf;
744 	struct qlcnic_vport *vp = vf->vp;
745 	struct qlcnic_adapter *adapter;
746 	struct qlcnic_sriov *sriov;
747 	u16 func = vf->pci_func;
748 	size_t size;
749 	int err;
750 
751 	adapter = vf->adapter;
752 	sriov = adapter->ahw->sriov;
753 
754 	if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) {
755 		err = qlcnic_sriov_pf_config_vport(adapter, 1, func);
756 		if (!err) {
757 			err = qlcnic_sriov_set_vf_vport_info(adapter, func);
758 			if (err)
759 				qlcnic_sriov_pf_config_vport(adapter, 0, func);
760 		}
761 	} else {
762 		if (vp->vlan_mode == QLC_GUEST_VLAN_MODE) {
763 			size = sizeof(*vf->sriov_vlans);
764 			size = size * sriov->num_allowed_vlans;
765 			memset(vf->sriov_vlans, 0, size);
766 		}
767 
768 		err = qlcnic_sriov_pf_config_vport(adapter, 0, func);
769 	}
770 
771 	if (err)
772 		goto err_out;
773 
774 	cmd->rsp.arg[0] |= (1 << 25);
775 
776 	if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT)
777 		set_bit(QLC_BC_VF_STATE, &vf->state);
778 	else
779 		clear_bit(QLC_BC_VF_STATE, &vf->state);
780 
781 	return err;
782 
783 err_out:
784 	cmd->rsp.arg[0] |= (2 << 25);
785 	return err;
786 }
787 
788 static int qlcnic_sriov_cfg_vf_def_mac(struct qlcnic_adapter *adapter,
789 				       struct qlcnic_vf_info *vf,
790 				       u16 vlan, u8 op)
791 {
792 	struct qlcnic_cmd_args *cmd;
793 	struct qlcnic_macvlan_mbx mv;
794 	struct qlcnic_vport *vp;
795 	u8 *addr;
796 	int err;
797 	u32 *buf;
798 	int vpid;
799 
800 	vp = vf->vp;
801 
802 	cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
803 	if (!cmd)
804 		return -ENOMEM;
805 
806 	err = qlcnic_alloc_mbx_args(cmd, adapter, QLCNIC_CMD_CONFIG_MAC_VLAN);
807 	if (err)
808 		goto free_cmd;
809 
810 	cmd->type = QLC_83XX_MBX_CMD_NO_WAIT;
811 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func);
812 	if (vpid < 0) {
813 		err = -EINVAL;
814 		goto free_args;
815 	}
816 
817 	if (vlan)
818 		op = ((op == QLCNIC_MAC_ADD || op == QLCNIC_MAC_VLAN_ADD) ?
819 		      QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_VLAN_DEL);
820 
821 	cmd->req.arg[1] = op | (1 << 8) | (3 << 6);
822 	cmd->req.arg[1] |= ((vpid & 0xffff) << 16) | BIT_31;
823 
824 	addr = vp->mac;
825 	mv.vlan = vlan;
826 	mv.mac_addr0 = addr[0];
827 	mv.mac_addr1 = addr[1];
828 	mv.mac_addr2 = addr[2];
829 	mv.mac_addr3 = addr[3];
830 	mv.mac_addr4 = addr[4];
831 	mv.mac_addr5 = addr[5];
832 	buf = &cmd->req.arg[2];
833 	memcpy(buf, &mv, sizeof(struct qlcnic_macvlan_mbx));
834 
835 	err = qlcnic_issue_cmd(adapter, cmd);
836 
837 	if (!err)
838 		return err;
839 
840 free_args:
841 	qlcnic_free_mbx_args(cmd);
842 free_cmd:
843 	kfree(cmd);
844 	return err;
845 }
846 
847 static int qlcnic_sriov_validate_create_rx_ctx(struct qlcnic_cmd_args *cmd)
848 {
849 	if ((cmd->req.arg[0] >> 29) != 0x3)
850 		return -EINVAL;
851 
852 	return 0;
853 }
854 
855 static void qlcnic_83xx_cfg_default_mac_vlan(struct qlcnic_adapter *adapter,
856 					     struct qlcnic_vf_info *vf,
857 					     int opcode)
858 {
859 	struct qlcnic_sriov *sriov;
860 	u16 vlan;
861 	int i;
862 
863 	sriov = adapter->ahw->sriov;
864 
865 	spin_lock_bh(&vf->vlan_list_lock);
866 	if (vf->num_vlan) {
867 		for (i = 0; i < sriov->num_allowed_vlans; i++) {
868 			vlan = vf->sriov_vlans[i];
869 			if (vlan)
870 				qlcnic_sriov_cfg_vf_def_mac(adapter, vf, vlan,
871 							    opcode);
872 		}
873 	}
874 	spin_unlock_bh(&vf->vlan_list_lock);
875 
876 	if (vf->vp->vlan_mode != QLC_PVID_MODE) {
877 		if (qlcnic_83xx_pf_check(adapter) &&
878 		    qlcnic_sriov_check_any_vlan(vf))
879 			return;
880 		qlcnic_sriov_cfg_vf_def_mac(adapter, vf, 0, opcode);
881 	}
882 }
883 
884 static int qlcnic_sriov_pf_create_rx_ctx_cmd(struct qlcnic_bc_trans *tran,
885 					     struct qlcnic_cmd_args *cmd)
886 {
887 	struct qlcnic_vf_info *vf = tran->vf;
888 	struct qlcnic_adapter *adapter = vf->adapter;
889 	struct qlcnic_rcv_mbx_out *mbx_out;
890 	int err;
891 
892 	err = qlcnic_sriov_validate_create_rx_ctx(cmd);
893 	if (err) {
894 		cmd->rsp.arg[0] |= (0x6 << 25);
895 		return err;
896 	}
897 
898 	cmd->req.arg[6] = vf->vp->handle;
899 	err = qlcnic_issue_cmd(adapter, cmd);
900 
901 	if (!err) {
902 		mbx_out = (struct qlcnic_rcv_mbx_out *)&cmd->rsp.arg[1];
903 		vf->rx_ctx_id = mbx_out->ctx_id;
904 		qlcnic_83xx_cfg_default_mac_vlan(adapter, vf, QLCNIC_MAC_ADD);
905 	} else {
906 		vf->rx_ctx_id = 0;
907 	}
908 
909 	return err;
910 }
911 
912 static int qlcnic_sriov_pf_mac_address_cmd(struct qlcnic_bc_trans *trans,
913 					   struct qlcnic_cmd_args *cmd)
914 {
915 	struct qlcnic_vf_info *vf = trans->vf;
916 	u8 type, *mac;
917 
918 	type = cmd->req.arg[1];
919 	switch (type) {
920 	case QLCNIC_SET_STATION_MAC:
921 	case QLCNIC_SET_FAC_DEF_MAC:
922 		cmd->rsp.arg[0] = (2 << 25);
923 		break;
924 	case QLCNIC_GET_CURRENT_MAC:
925 		cmd->rsp.arg[0] = (1 << 25);
926 		mac = vf->vp->mac;
927 		cmd->rsp.arg[2] = mac[1] | ((mac[0] << 8) & 0xff00);
928 		cmd->rsp.arg[1] = mac[5] | ((mac[4] << 8) & 0xff00) |
929 				  ((mac[3]) << 16 & 0xff0000) |
930 				  ((mac[2]) << 24 & 0xff000000);
931 	}
932 
933 	return 0;
934 }
935 
936 static int qlcnic_sriov_validate_create_tx_ctx(struct qlcnic_cmd_args *cmd)
937 {
938 	if ((cmd->req.arg[0] >> 29) != 0x3)
939 		return -EINVAL;
940 
941 	return 0;
942 }
943 
944 static int qlcnic_sriov_pf_create_tx_ctx_cmd(struct qlcnic_bc_trans *trans,
945 					     struct qlcnic_cmd_args *cmd)
946 {
947 	struct qlcnic_vf_info *vf = trans->vf;
948 	struct qlcnic_adapter *adapter = vf->adapter;
949 	struct qlcnic_tx_mbx_out *mbx_out;
950 	int err;
951 
952 	err = qlcnic_sriov_validate_create_tx_ctx(cmd);
953 	if (err) {
954 		cmd->rsp.arg[0] |= (0x6 << 25);
955 		return err;
956 	}
957 
958 	cmd->req.arg[5] |= vf->vp->handle << 16;
959 	err = qlcnic_issue_cmd(adapter, cmd);
960 	if (!err) {
961 		mbx_out = (struct qlcnic_tx_mbx_out *)&cmd->rsp.arg[2];
962 		vf->tx_ctx_id = mbx_out->ctx_id;
963 	} else {
964 		vf->tx_ctx_id = 0;
965 	}
966 
967 	return err;
968 }
969 
970 static int qlcnic_sriov_validate_del_rx_ctx(struct qlcnic_vf_info *vf,
971 					    struct qlcnic_cmd_args *cmd)
972 {
973 	if ((cmd->req.arg[0] >> 29) != 0x3)
974 		return -EINVAL;
975 
976 	if ((cmd->req.arg[1] & 0xffff) != vf->rx_ctx_id)
977 		return -EINVAL;
978 
979 	return 0;
980 }
981 
982 static int qlcnic_sriov_pf_del_rx_ctx_cmd(struct qlcnic_bc_trans *trans,
983 					  struct qlcnic_cmd_args *cmd)
984 {
985 	struct qlcnic_vf_info *vf = trans->vf;
986 	struct qlcnic_adapter *adapter = vf->adapter;
987 	int err;
988 
989 	err = qlcnic_sriov_validate_del_rx_ctx(vf, cmd);
990 	if (err) {
991 		cmd->rsp.arg[0] |= (0x6 << 25);
992 		return err;
993 	}
994 
995 	qlcnic_83xx_cfg_default_mac_vlan(adapter, vf, QLCNIC_MAC_DEL);
996 	cmd->req.arg[1] |= vf->vp->handle << 16;
997 	err = qlcnic_issue_cmd(adapter, cmd);
998 
999 	if (!err)
1000 		vf->rx_ctx_id = 0;
1001 
1002 	return err;
1003 }
1004 
1005 static int qlcnic_sriov_validate_del_tx_ctx(struct qlcnic_vf_info *vf,
1006 					    struct qlcnic_cmd_args *cmd)
1007 {
1008 	if ((cmd->req.arg[0] >> 29) != 0x3)
1009 		return -EINVAL;
1010 
1011 	if ((cmd->req.arg[1] & 0xffff) != vf->tx_ctx_id)
1012 		return -EINVAL;
1013 
1014 	return 0;
1015 }
1016 
1017 static int qlcnic_sriov_pf_del_tx_ctx_cmd(struct qlcnic_bc_trans *trans,
1018 					  struct qlcnic_cmd_args *cmd)
1019 {
1020 	struct qlcnic_vf_info *vf = trans->vf;
1021 	struct qlcnic_adapter *adapter = vf->adapter;
1022 	int err;
1023 
1024 	err = qlcnic_sriov_validate_del_tx_ctx(vf, cmd);
1025 	if (err) {
1026 		cmd->rsp.arg[0] |= (0x6 << 25);
1027 		return err;
1028 	}
1029 
1030 	cmd->req.arg[1] |= vf->vp->handle << 16;
1031 	err = qlcnic_issue_cmd(adapter, cmd);
1032 
1033 	if (!err)
1034 		vf->tx_ctx_id = 0;
1035 
1036 	return err;
1037 }
1038 
1039 static int qlcnic_sriov_validate_cfg_lro(struct qlcnic_vf_info *vf,
1040 					 struct qlcnic_cmd_args *cmd)
1041 {
1042 	if ((cmd->req.arg[1] >> 16) != vf->rx_ctx_id)
1043 		return -EINVAL;
1044 
1045 	return 0;
1046 }
1047 
1048 static int qlcnic_sriov_pf_cfg_lro_cmd(struct qlcnic_bc_trans *trans,
1049 				       struct qlcnic_cmd_args *cmd)
1050 {
1051 	struct qlcnic_vf_info *vf = trans->vf;
1052 	struct qlcnic_adapter *adapter = vf->adapter;
1053 	int err;
1054 
1055 	err = qlcnic_sriov_validate_cfg_lro(vf, cmd);
1056 	if (err) {
1057 		cmd->rsp.arg[0] |= (0x6 << 25);
1058 		return err;
1059 	}
1060 
1061 	err = qlcnic_issue_cmd(adapter, cmd);
1062 	return err;
1063 }
1064 
1065 static int qlcnic_sriov_pf_cfg_ip_cmd(struct qlcnic_bc_trans *trans,
1066 				      struct qlcnic_cmd_args *cmd)
1067 {
1068 	struct qlcnic_vf_info *vf = trans->vf;
1069 	struct qlcnic_adapter *adapter = vf->adapter;
1070 	int err;
1071 
1072 	cmd->req.arg[1] |= vf->vp->handle << 16;
1073 	cmd->req.arg[1] |= BIT_31;
1074 
1075 	err = qlcnic_issue_cmd(adapter, cmd);
1076 	return err;
1077 }
1078 
1079 static int qlcnic_sriov_validate_cfg_intrpt(struct qlcnic_vf_info *vf,
1080 					    struct qlcnic_cmd_args *cmd)
1081 {
1082 	if (((cmd->req.arg[1] >> 8) & 0xff) != vf->pci_func)
1083 		return -EINVAL;
1084 
1085 	if (!(cmd->req.arg[1] & BIT_16))
1086 		return -EINVAL;
1087 
1088 	if ((cmd->req.arg[1] & 0xff) != 0x1)
1089 		return -EINVAL;
1090 
1091 	return 0;
1092 }
1093 
1094 static int qlcnic_sriov_pf_cfg_intrpt_cmd(struct qlcnic_bc_trans *trans,
1095 					  struct qlcnic_cmd_args *cmd)
1096 {
1097 	struct qlcnic_vf_info *vf = trans->vf;
1098 	struct qlcnic_adapter *adapter = vf->adapter;
1099 	int err;
1100 
1101 	err = qlcnic_sriov_validate_cfg_intrpt(vf, cmd);
1102 	if (err)
1103 		cmd->rsp.arg[0] |= (0x6 << 25);
1104 	else
1105 		err = qlcnic_issue_cmd(adapter, cmd);
1106 
1107 	return err;
1108 }
1109 
1110 static int qlcnic_sriov_validate_mtu(struct qlcnic_adapter *adapter,
1111 				     struct qlcnic_vf_info *vf,
1112 				     struct qlcnic_cmd_args *cmd)
1113 {
1114 	if (cmd->req.arg[1] != vf->rx_ctx_id)
1115 		return -EINVAL;
1116 
1117 	if (cmd->req.arg[2] > adapter->ahw->max_mtu)
1118 		return -EINVAL;
1119 
1120 	return 0;
1121 }
1122 
1123 static int qlcnic_sriov_pf_set_mtu_cmd(struct qlcnic_bc_trans *trans,
1124 				       struct qlcnic_cmd_args *cmd)
1125 {
1126 	struct qlcnic_vf_info *vf = trans->vf;
1127 	struct qlcnic_adapter *adapter = vf->adapter;
1128 	int err;
1129 
1130 	err = qlcnic_sriov_validate_mtu(adapter, vf, cmd);
1131 	if (err)
1132 		cmd->rsp.arg[0] |= (0x6 << 25);
1133 	else
1134 		err = qlcnic_issue_cmd(adapter, cmd);
1135 
1136 	return err;
1137 }
1138 
1139 static int qlcnic_sriov_validate_get_nic_info(struct qlcnic_vf_info *vf,
1140 					      struct qlcnic_cmd_args *cmd)
1141 {
1142 	if (cmd->req.arg[1] & BIT_31) {
1143 		if (((cmd->req.arg[1] >> 16) & 0x7fff) != vf->pci_func)
1144 			return -EINVAL;
1145 	} else {
1146 		cmd->req.arg[1] |= vf->vp->handle << 16;
1147 	}
1148 
1149 	return 0;
1150 }
1151 
1152 static int qlcnic_sriov_pf_get_nic_info_cmd(struct qlcnic_bc_trans *trans,
1153 					    struct qlcnic_cmd_args *cmd)
1154 {
1155 	struct qlcnic_vf_info *vf = trans->vf;
1156 	struct qlcnic_adapter *adapter = vf->adapter;
1157 	int err;
1158 
1159 	err = qlcnic_sriov_validate_get_nic_info(vf, cmd);
1160 	if (err) {
1161 		cmd->rsp.arg[0] |= (0x6 << 25);
1162 		return err;
1163 	}
1164 
1165 	err = qlcnic_issue_cmd(adapter, cmd);
1166 	return err;
1167 }
1168 
1169 static int qlcnic_sriov_validate_cfg_rss(struct qlcnic_vf_info *vf,
1170 					 struct qlcnic_cmd_args *cmd)
1171 {
1172 	if (cmd->req.arg[1] != vf->rx_ctx_id)
1173 		return -EINVAL;
1174 
1175 	return 0;
1176 }
1177 
1178 static int qlcnic_sriov_pf_cfg_rss_cmd(struct qlcnic_bc_trans *trans,
1179 				       struct qlcnic_cmd_args *cmd)
1180 {
1181 	struct qlcnic_vf_info *vf = trans->vf;
1182 	struct qlcnic_adapter *adapter = vf->adapter;
1183 	int err;
1184 
1185 	err = qlcnic_sriov_validate_cfg_rss(vf, cmd);
1186 	if (err)
1187 		cmd->rsp.arg[0] |= (0x6 << 25);
1188 	else
1189 		err = qlcnic_issue_cmd(adapter, cmd);
1190 
1191 	return err;
1192 }
1193 
1194 static int qlcnic_sriov_validate_cfg_intrcoal(struct qlcnic_adapter *adapter,
1195 					      struct qlcnic_vf_info *vf,
1196 					      struct qlcnic_cmd_args *cmd)
1197 {
1198 	struct qlcnic_nic_intr_coalesce *coal = &adapter->ahw->coal;
1199 	u16 ctx_id, pkts, time;
1200 	int err = -EINVAL;
1201 	u8 type;
1202 
1203 	type = cmd->req.arg[1] & QLC_INTR_COAL_TYPE_MASK;
1204 	ctx_id = cmd->req.arg[1] >> 16;
1205 	pkts = cmd->req.arg[2] & 0xffff;
1206 	time = cmd->req.arg[2] >> 16;
1207 
1208 	switch (type) {
1209 	case QLCNIC_INTR_COAL_TYPE_RX:
1210 		if (ctx_id != vf->rx_ctx_id || pkts > coal->rx_packets ||
1211 		    time < coal->rx_time_us)
1212 			goto err_label;
1213 		break;
1214 	case QLCNIC_INTR_COAL_TYPE_TX:
1215 		if (ctx_id != vf->tx_ctx_id || pkts > coal->tx_packets ||
1216 		    time < coal->tx_time_us)
1217 			goto err_label;
1218 		break;
1219 	default:
1220 		netdev_err(adapter->netdev, "Invalid coalescing type 0x%x received\n",
1221 			   type);
1222 		return err;
1223 	}
1224 
1225 	return 0;
1226 
1227 err_label:
1228 	netdev_err(adapter->netdev, "Expected: rx_ctx_id 0x%x rx_packets 0x%x rx_time_us 0x%x tx_ctx_id 0x%x tx_packets 0x%x tx_time_us 0x%x\n",
1229 		   vf->rx_ctx_id, coal->rx_packets, coal->rx_time_us,
1230 		   vf->tx_ctx_id, coal->tx_packets, coal->tx_time_us);
1231 	netdev_err(adapter->netdev, "Received: ctx_id 0x%x packets 0x%x time_us 0x%x type 0x%x\n",
1232 		   ctx_id, pkts, time, type);
1233 
1234 	return err;
1235 }
1236 
1237 static int qlcnic_sriov_pf_cfg_intrcoal_cmd(struct qlcnic_bc_trans *tran,
1238 					    struct qlcnic_cmd_args *cmd)
1239 {
1240 	struct qlcnic_vf_info *vf = tran->vf;
1241 	struct qlcnic_adapter *adapter = vf->adapter;
1242 	int err;
1243 
1244 	err = qlcnic_sriov_validate_cfg_intrcoal(adapter, vf, cmd);
1245 	if (err) {
1246 		cmd->rsp.arg[0] |= (0x6 << 25);
1247 		return err;
1248 	}
1249 
1250 	err = qlcnic_issue_cmd(adapter, cmd);
1251 	return err;
1252 }
1253 
1254 static int qlcnic_sriov_validate_cfg_macvlan(struct qlcnic_adapter *adapter,
1255 					     struct qlcnic_vf_info *vf,
1256 					     struct qlcnic_cmd_args *cmd)
1257 {
1258 	struct qlcnic_vport *vp = vf->vp;
1259 	u8 op, new_op;
1260 
1261 	if (!(cmd->req.arg[1] & BIT_8))
1262 		return -EINVAL;
1263 
1264 	cmd->req.arg[1] |= (vf->vp->handle << 16);
1265 	cmd->req.arg[1] |= BIT_31;
1266 
1267 	if (vp->vlan_mode == QLC_PVID_MODE) {
1268 		op = cmd->req.arg[1] & 0x7;
1269 		cmd->req.arg[1] &= ~0x7;
1270 		new_op = (op == QLCNIC_MAC_ADD || op == QLCNIC_MAC_VLAN_ADD) ?
1271 			 QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_VLAN_DEL;
1272 		cmd->req.arg[3] |= vp->pvid << 16;
1273 		cmd->req.arg[1] |= new_op;
1274 	}
1275 
1276 	return 0;
1277 }
1278 
1279 static int qlcnic_sriov_pf_cfg_macvlan_cmd(struct qlcnic_bc_trans *trans,
1280 					   struct qlcnic_cmd_args *cmd)
1281 {
1282 	struct qlcnic_vf_info *vf = trans->vf;
1283 	struct qlcnic_adapter *adapter = vf->adapter;
1284 	int err;
1285 
1286 	err = qlcnic_sriov_validate_cfg_macvlan(adapter, vf, cmd);
1287 	if (err) {
1288 		cmd->rsp.arg[0] |= (0x6 << 25);
1289 		return err;
1290 	}
1291 
1292 	err = qlcnic_issue_cmd(adapter, cmd);
1293 	return err;
1294 }
1295 
1296 static int qlcnic_sriov_validate_linkevent(struct qlcnic_vf_info *vf,
1297 					   struct qlcnic_cmd_args *cmd)
1298 {
1299 	if ((cmd->req.arg[1] >> 16) != vf->rx_ctx_id)
1300 		return -EINVAL;
1301 
1302 	return 0;
1303 }
1304 
1305 static int qlcnic_sriov_pf_linkevent_cmd(struct qlcnic_bc_trans *trans,
1306 					 struct qlcnic_cmd_args *cmd)
1307 {
1308 	struct qlcnic_vf_info *vf = trans->vf;
1309 	struct qlcnic_adapter *adapter = vf->adapter;
1310 	int err;
1311 
1312 	err = qlcnic_sriov_validate_linkevent(vf, cmd);
1313 	if (err) {
1314 		cmd->rsp.arg[0] |= (0x6 << 25);
1315 		return err;
1316 	}
1317 
1318 	err = qlcnic_issue_cmd(adapter, cmd);
1319 	return err;
1320 }
1321 
1322 static int qlcnic_sriov_pf_cfg_promisc_cmd(struct qlcnic_bc_trans *trans,
1323 					   struct qlcnic_cmd_args *cmd)
1324 {
1325 	struct qlcnic_vf_info *vf = trans->vf;
1326 	struct qlcnic_adapter *adapter = vf->adapter;
1327 	int err;
1328 
1329 	cmd->req.arg[1] |= vf->vp->handle << 16;
1330 	cmd->req.arg[1] |= BIT_31;
1331 	err = qlcnic_issue_cmd(adapter, cmd);
1332 	return err;
1333 }
1334 
1335 static int qlcnic_sriov_pf_get_acl_cmd(struct qlcnic_bc_trans *trans,
1336 				       struct qlcnic_cmd_args *cmd)
1337 {
1338 	struct qlcnic_vf_info *vf = trans->vf;
1339 	struct qlcnic_vport *vp = vf->vp;
1340 	u8 mode = vp->vlan_mode;
1341 	struct qlcnic_adapter *adapter;
1342 	struct qlcnic_sriov *sriov;
1343 
1344 	adapter = vf->adapter;
1345 	sriov = adapter->ahw->sriov;
1346 
1347 	cmd->rsp.arg[0] |= 1 << 25;
1348 
1349 	/* For 84xx adapter in case of PVID , PFD should send vlan mode as
1350 	 * QLC_NO_VLAN_MODE to VFD which is zero in mailbox response
1351 	 */
1352 	if (qlcnic_84xx_check(adapter) && mode == QLC_PVID_MODE)
1353 		return 0;
1354 
1355 	switch (mode) {
1356 	case QLC_GUEST_VLAN_MODE:
1357 		cmd->rsp.arg[1] = mode | 1 << 8;
1358 		cmd->rsp.arg[2] = sriov->num_allowed_vlans << 16;
1359 		break;
1360 	case QLC_PVID_MODE:
1361 		cmd->rsp.arg[1] = mode | 1 << 8 | vp->pvid << 16;
1362 		break;
1363 	}
1364 
1365 	return 0;
1366 }
1367 
1368 static int qlcnic_sriov_pf_del_guest_vlan(struct qlcnic_adapter *adapter,
1369 					  struct qlcnic_vf_info *vf,
1370 					  struct qlcnic_cmd_args *cmd)
1371 {
1372 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1373 	u16 vlan;
1374 
1375 	if (!qlcnic_sriov_check_any_vlan(vf))
1376 		return -EINVAL;
1377 
1378 	vlan = cmd->req.arg[1] >> 16;
1379 	if (!vf->rx_ctx_id) {
1380 		qlcnic_sriov_del_vlan_id(sriov, vf, vlan);
1381 		return 0;
1382 	}
1383 
1384 	qlcnic_sriov_cfg_vf_def_mac(adapter, vf, vlan, QLCNIC_MAC_DEL);
1385 	qlcnic_sriov_del_vlan_id(sriov, vf, vlan);
1386 
1387 	if (qlcnic_83xx_pf_check(adapter))
1388 		qlcnic_sriov_cfg_vf_def_mac(adapter, vf,
1389 					    0, QLCNIC_MAC_ADD);
1390 	return 0;
1391 }
1392 
1393 static int qlcnic_sriov_pf_add_guest_vlan(struct qlcnic_adapter *adapter,
1394 					  struct qlcnic_vf_info *vf,
1395 					  struct qlcnic_cmd_args *cmd)
1396 {
1397 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1398 	int err = -EIO;
1399 	u16 vlan;
1400 
1401 	if (qlcnic_83xx_pf_check(adapter) && qlcnic_sriov_check_any_vlan(vf))
1402 		return err;
1403 
1404 	vlan = cmd->req.arg[1] >> 16;
1405 
1406 	if (!vf->rx_ctx_id) {
1407 		qlcnic_sriov_add_vlan_id(sriov, vf, vlan);
1408 		return 0;
1409 	}
1410 
1411 	if (qlcnic_83xx_pf_check(adapter)) {
1412 		err = qlcnic_sriov_cfg_vf_def_mac(adapter, vf, 0,
1413 						  QLCNIC_MAC_DEL);
1414 		if (err)
1415 			return err;
1416 	}
1417 
1418 	err = qlcnic_sriov_cfg_vf_def_mac(adapter, vf, vlan, QLCNIC_MAC_ADD);
1419 
1420 	if (err) {
1421 		if (qlcnic_83xx_pf_check(adapter))
1422 			qlcnic_sriov_cfg_vf_def_mac(adapter, vf, 0,
1423 						    QLCNIC_MAC_ADD);
1424 		return err;
1425 	}
1426 
1427 	qlcnic_sriov_add_vlan_id(sriov, vf, vlan);
1428 	return err;
1429 }
1430 
1431 static int qlcnic_sriov_pf_cfg_guest_vlan_cmd(struct qlcnic_bc_trans *tran,
1432 					      struct qlcnic_cmd_args *cmd)
1433 {
1434 	struct qlcnic_vf_info  *vf = tran->vf;
1435 	struct qlcnic_adapter *adapter =  vf->adapter;
1436 	struct qlcnic_vport *vp = vf->vp;
1437 	int err = -EIO;
1438 	u8 op;
1439 
1440 	if (vp->vlan_mode != QLC_GUEST_VLAN_MODE) {
1441 		cmd->rsp.arg[0] |= 2 << 25;
1442 		return err;
1443 	}
1444 
1445 	op = cmd->req.arg[1] & 0xf;
1446 
1447 	if (op)
1448 		err = qlcnic_sriov_pf_add_guest_vlan(adapter, vf, cmd);
1449 	else
1450 		err = qlcnic_sriov_pf_del_guest_vlan(adapter, vf, cmd);
1451 
1452 	cmd->rsp.arg[0] |= err ? 2 << 25 : 1 << 25;
1453 	return err;
1454 }
1455 
1456 static const int qlcnic_pf_passthru_supp_cmds[] = {
1457 	QLCNIC_CMD_GET_STATISTICS,
1458 	QLCNIC_CMD_GET_PORT_CONFIG,
1459 	QLCNIC_CMD_GET_LINK_STATUS,
1460 	QLCNIC_CMD_INIT_NIC_FUNC,
1461 	QLCNIC_CMD_STOP_NIC_FUNC,
1462 };
1463 
1464 static const struct qlcnic_sriov_cmd_handler qlcnic_pf_bc_cmd_hdlr[] = {
1465 	[QLCNIC_BC_CMD_CHANNEL_INIT] = {&qlcnic_sriov_pf_channel_cfg_cmd},
1466 	[QLCNIC_BC_CMD_CHANNEL_TERM] = {&qlcnic_sriov_pf_channel_cfg_cmd},
1467 	[QLCNIC_BC_CMD_GET_ACL]	= {&qlcnic_sriov_pf_get_acl_cmd},
1468 	[QLCNIC_BC_CMD_CFG_GUEST_VLAN]	= {&qlcnic_sriov_pf_cfg_guest_vlan_cmd},
1469 };
1470 
1471 static const struct qlcnic_sriov_fw_cmd_handler qlcnic_pf_fw_cmd_hdlr[] = {
1472 	{QLCNIC_CMD_CREATE_RX_CTX, qlcnic_sriov_pf_create_rx_ctx_cmd},
1473 	{QLCNIC_CMD_CREATE_TX_CTX, qlcnic_sriov_pf_create_tx_ctx_cmd},
1474 	{QLCNIC_CMD_MAC_ADDRESS, qlcnic_sriov_pf_mac_address_cmd},
1475 	{QLCNIC_CMD_DESTROY_RX_CTX, qlcnic_sriov_pf_del_rx_ctx_cmd},
1476 	{QLCNIC_CMD_DESTROY_TX_CTX, qlcnic_sriov_pf_del_tx_ctx_cmd},
1477 	{QLCNIC_CMD_CONFIGURE_HW_LRO, qlcnic_sriov_pf_cfg_lro_cmd},
1478 	{QLCNIC_CMD_CONFIGURE_IP_ADDR, qlcnic_sriov_pf_cfg_ip_cmd},
1479 	{QLCNIC_CMD_CONFIG_INTRPT, qlcnic_sriov_pf_cfg_intrpt_cmd},
1480 	{QLCNIC_CMD_SET_MTU, qlcnic_sriov_pf_set_mtu_cmd},
1481 	{QLCNIC_CMD_GET_NIC_INFO, qlcnic_sriov_pf_get_nic_info_cmd},
1482 	{QLCNIC_CMD_CONFIGURE_RSS, qlcnic_sriov_pf_cfg_rss_cmd},
1483 	{QLCNIC_CMD_CONFIG_INTR_COAL, qlcnic_sriov_pf_cfg_intrcoal_cmd},
1484 	{QLCNIC_CMD_CONFIG_MAC_VLAN, qlcnic_sriov_pf_cfg_macvlan_cmd},
1485 	{QLCNIC_CMD_GET_LINK_EVENT, qlcnic_sriov_pf_linkevent_cmd},
1486 	{QLCNIC_CMD_CONFIGURE_MAC_RX_MODE, qlcnic_sriov_pf_cfg_promisc_cmd},
1487 };
1488 
1489 void qlcnic_sriov_pf_process_bc_cmd(struct qlcnic_adapter *adapter,
1490 				    struct qlcnic_bc_trans *trans,
1491 				    struct qlcnic_cmd_args *cmd)
1492 {
1493 	u8 size, cmd_op;
1494 
1495 	cmd_op = trans->req_hdr->cmd_op;
1496 
1497 	if (trans->req_hdr->op_type == QLC_BC_CMD) {
1498 		size = ARRAY_SIZE(qlcnic_pf_bc_cmd_hdlr);
1499 		if (cmd_op < size) {
1500 			qlcnic_pf_bc_cmd_hdlr[cmd_op].fn(trans, cmd);
1501 			return;
1502 		}
1503 	} else {
1504 		int i;
1505 		size = ARRAY_SIZE(qlcnic_pf_fw_cmd_hdlr);
1506 		for (i = 0; i < size; i++) {
1507 			if (cmd_op == qlcnic_pf_fw_cmd_hdlr[i].cmd) {
1508 				qlcnic_pf_fw_cmd_hdlr[i].fn(trans, cmd);
1509 				return;
1510 			}
1511 		}
1512 
1513 		size = ARRAY_SIZE(qlcnic_pf_passthru_supp_cmds);
1514 		for (i = 0; i < size; i++) {
1515 			if (cmd_op == qlcnic_pf_passthru_supp_cmds[i]) {
1516 				qlcnic_issue_cmd(adapter, cmd);
1517 				return;
1518 			}
1519 		}
1520 	}
1521 
1522 	cmd->rsp.arg[0] |= (0x9 << 25);
1523 }
1524 
1525 void qlcnic_pf_set_interface_id_create_rx_ctx(struct qlcnic_adapter *adapter,
1526 					     u32 *int_id)
1527 {
1528 	u16 vpid;
1529 
1530 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1531 						adapter->ahw->pci_func);
1532 	*int_id |= vpid;
1533 }
1534 
1535 void qlcnic_pf_set_interface_id_del_rx_ctx(struct qlcnic_adapter *adapter,
1536 					   u32 *int_id)
1537 {
1538 	u16 vpid;
1539 
1540 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1541 						adapter->ahw->pci_func);
1542 	*int_id |= vpid << 16;
1543 }
1544 
1545 void qlcnic_pf_set_interface_id_create_tx_ctx(struct qlcnic_adapter *adapter,
1546 					      u32 *int_id)
1547 {
1548 	int vpid;
1549 
1550 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1551 						adapter->ahw->pci_func);
1552 	*int_id |= vpid << 16;
1553 }
1554 
1555 void qlcnic_pf_set_interface_id_del_tx_ctx(struct qlcnic_adapter *adapter,
1556 					   u32 *int_id)
1557 {
1558 	u16 vpid;
1559 
1560 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1561 						adapter->ahw->pci_func);
1562 	*int_id |= vpid << 16;
1563 }
1564 
1565 void qlcnic_pf_set_interface_id_promisc(struct qlcnic_adapter *adapter,
1566 					u32 *int_id)
1567 {
1568 	u16 vpid;
1569 
1570 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1571 						adapter->ahw->pci_func);
1572 	*int_id |= (vpid << 16) | BIT_31;
1573 }
1574 
1575 void qlcnic_pf_set_interface_id_ipaddr(struct qlcnic_adapter *adapter,
1576 				       u32 *int_id)
1577 {
1578 	u16 vpid;
1579 
1580 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1581 						adapter->ahw->pci_func);
1582 	*int_id |= (vpid << 16) | BIT_31;
1583 }
1584 
1585 void qlcnic_pf_set_interface_id_macaddr(struct qlcnic_adapter *adapter,
1586 					u32 *int_id)
1587 {
1588 	u16 vpid;
1589 
1590 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1591 						adapter->ahw->pci_func);
1592 	*int_id |= (vpid << 16) | BIT_31;
1593 }
1594 
1595 static void qlcnic_sriov_del_rx_ctx(struct qlcnic_adapter *adapter,
1596 				    struct qlcnic_vf_info *vf)
1597 {
1598 	struct qlcnic_cmd_args cmd;
1599 	int vpid;
1600 
1601 	if (!vf->rx_ctx_id)
1602 		return;
1603 
1604 	if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_DESTROY_RX_CTX))
1605 		return;
1606 
1607 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func);
1608 	if (vpid >= 0) {
1609 		cmd.req.arg[1] = vf->rx_ctx_id | (vpid & 0xffff) << 16;
1610 		if (qlcnic_issue_cmd(adapter, &cmd))
1611 			dev_err(&adapter->pdev->dev,
1612 				"Failed to delete Tx ctx in firmware for func 0x%x\n",
1613 				vf->pci_func);
1614 		else
1615 			vf->rx_ctx_id = 0;
1616 	}
1617 
1618 	qlcnic_free_mbx_args(&cmd);
1619 }
1620 
1621 static void qlcnic_sriov_del_tx_ctx(struct qlcnic_adapter *adapter,
1622 				    struct qlcnic_vf_info *vf)
1623 {
1624 	struct qlcnic_cmd_args cmd;
1625 	int vpid;
1626 
1627 	if (!vf->tx_ctx_id)
1628 		return;
1629 
1630 	if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_DESTROY_TX_CTX))
1631 		return;
1632 
1633 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func);
1634 	if (vpid >= 0) {
1635 		cmd.req.arg[1] |= vf->tx_ctx_id | (vpid & 0xffff) << 16;
1636 		if (qlcnic_issue_cmd(adapter, &cmd))
1637 			dev_err(&adapter->pdev->dev,
1638 				"Failed to delete Tx ctx in firmware for func 0x%x\n",
1639 				vf->pci_func);
1640 		else
1641 			vf->tx_ctx_id = 0;
1642 	}
1643 
1644 	qlcnic_free_mbx_args(&cmd);
1645 }
1646 
1647 static int qlcnic_sriov_add_act_list_irqsave(struct qlcnic_sriov *sriov,
1648 					     struct qlcnic_vf_info *vf,
1649 					     struct qlcnic_bc_trans *trans)
1650 {
1651 	struct qlcnic_trans_list *t_list = &vf->rcv_act;
1652 	unsigned long flag;
1653 
1654 	spin_lock_irqsave(&t_list->lock, flag);
1655 
1656 	__qlcnic_sriov_add_act_list(sriov, vf, trans);
1657 
1658 	spin_unlock_irqrestore(&t_list->lock, flag);
1659 	return 0;
1660 }
1661 
1662 static void __qlcnic_sriov_process_flr(struct qlcnic_vf_info *vf)
1663 {
1664 	struct qlcnic_adapter *adapter = vf->adapter;
1665 
1666 	qlcnic_sriov_cleanup_list(&vf->rcv_pend);
1667 	cancel_work_sync(&vf->trans_work);
1668 	qlcnic_sriov_cleanup_list(&vf->rcv_act);
1669 
1670 	if (test_bit(QLC_BC_VF_SOFT_FLR, &vf->state)) {
1671 		qlcnic_sriov_del_tx_ctx(adapter, vf);
1672 		qlcnic_sriov_del_rx_ctx(adapter, vf);
1673 	}
1674 
1675 	qlcnic_sriov_pf_config_vport(adapter, 0, vf->pci_func);
1676 
1677 	clear_bit(QLC_BC_VF_FLR, &vf->state);
1678 	if (test_bit(QLC_BC_VF_SOFT_FLR, &vf->state)) {
1679 		qlcnic_sriov_add_act_list_irqsave(adapter->ahw->sriov, vf,
1680 						  vf->flr_trans);
1681 		clear_bit(QLC_BC_VF_SOFT_FLR, &vf->state);
1682 		vf->flr_trans = NULL;
1683 	}
1684 }
1685 
1686 static void qlcnic_sriov_pf_process_flr(struct work_struct *work)
1687 {
1688 	struct qlcnic_vf_info *vf;
1689 
1690 	vf = container_of(work, struct qlcnic_vf_info, flr_work);
1691 	__qlcnic_sriov_process_flr(vf);
1692 	return;
1693 }
1694 
1695 static void qlcnic_sriov_schedule_flr(struct qlcnic_sriov *sriov,
1696 				      struct qlcnic_vf_info *vf,
1697 				      work_func_t func)
1698 {
1699 	if (test_bit(__QLCNIC_RESETTING, &vf->adapter->state))
1700 		return;
1701 
1702 	INIT_WORK(&vf->flr_work, func);
1703 	queue_work(sriov->bc.bc_flr_wq, &vf->flr_work);
1704 }
1705 
1706 static void qlcnic_sriov_handle_soft_flr(struct qlcnic_adapter *adapter,
1707 					 struct qlcnic_bc_trans *trans,
1708 					 struct qlcnic_vf_info *vf)
1709 {
1710 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1711 
1712 	set_bit(QLC_BC_VF_FLR, &vf->state);
1713 	clear_bit(QLC_BC_VF_STATE, &vf->state);
1714 	set_bit(QLC_BC_VF_SOFT_FLR, &vf->state);
1715 	vf->flr_trans = trans;
1716 	qlcnic_sriov_schedule_flr(sriov, vf, qlcnic_sriov_pf_process_flr);
1717 	netdev_info(adapter->netdev, "Software FLR for PCI func %d\n",
1718 		    vf->pci_func);
1719 }
1720 
1721 bool qlcnic_sriov_soft_flr_check(struct qlcnic_adapter *adapter,
1722 				 struct qlcnic_bc_trans *trans,
1723 				 struct qlcnic_vf_info *vf)
1724 {
1725 	struct qlcnic_bc_hdr *hdr = trans->req_hdr;
1726 
1727 	if ((hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) &&
1728 	    (hdr->op_type == QLC_BC_CMD) &&
1729 	     test_bit(QLC_BC_VF_STATE, &vf->state)) {
1730 		qlcnic_sriov_handle_soft_flr(adapter, trans, vf);
1731 		return true;
1732 	}
1733 
1734 	return false;
1735 }
1736 
1737 void qlcnic_sriov_pf_handle_flr(struct qlcnic_sriov *sriov,
1738 				struct qlcnic_vf_info *vf)
1739 {
1740 	struct net_device *dev = vf->adapter->netdev;
1741 	struct qlcnic_vport *vp = vf->vp;
1742 
1743 	if (!test_and_clear_bit(QLC_BC_VF_STATE, &vf->state)) {
1744 		clear_bit(QLC_BC_VF_FLR, &vf->state);
1745 		return;
1746 	}
1747 
1748 	if (test_and_set_bit(QLC_BC_VF_FLR, &vf->state)) {
1749 		netdev_info(dev, "FLR for PCI func %d in progress\n",
1750 			    vf->pci_func);
1751 		return;
1752 	}
1753 
1754 	if (vp->vlan_mode == QLC_GUEST_VLAN_MODE)
1755 		memset(vf->sriov_vlans, 0,
1756 		       sizeof(*vf->sriov_vlans) * sriov->num_allowed_vlans);
1757 
1758 	qlcnic_sriov_schedule_flr(sriov, vf, qlcnic_sriov_pf_process_flr);
1759 	netdev_info(dev, "FLR received for PCI func %d\n", vf->pci_func);
1760 }
1761 
1762 void qlcnic_sriov_pf_reset(struct qlcnic_adapter *adapter)
1763 {
1764 	struct qlcnic_hardware_context *ahw = adapter->ahw;
1765 	struct qlcnic_sriov *sriov = ahw->sriov;
1766 	struct qlcnic_vf_info *vf;
1767 	u16 num_vfs = sriov->num_vfs;
1768 	int i;
1769 
1770 	for (i = 0; i < num_vfs; i++) {
1771 		vf = &sriov->vf_info[i];
1772 		vf->rx_ctx_id = 0;
1773 		vf->tx_ctx_id = 0;
1774 		cancel_work_sync(&vf->flr_work);
1775 		__qlcnic_sriov_process_flr(vf);
1776 		clear_bit(QLC_BC_VF_STATE, &vf->state);
1777 	}
1778 
1779 	qlcnic_sriov_pf_reset_vport_handle(adapter, ahw->pci_func);
1780 	QLCWRX(ahw, QLCNIC_MBX_INTR_ENBL, (ahw->num_msix - 1) << 8);
1781 }
1782 
1783 int qlcnic_sriov_pf_reinit(struct qlcnic_adapter *adapter)
1784 {
1785 	struct qlcnic_hardware_context *ahw = adapter->ahw;
1786 	int err;
1787 
1788 	if (!qlcnic_sriov_enable_check(adapter))
1789 		return 0;
1790 
1791 	ahw->op_mode = QLCNIC_SRIOV_PF_FUNC;
1792 
1793 	err = qlcnic_sriov_pf_init(adapter);
1794 	if (err)
1795 		return err;
1796 
1797 	dev_info(&adapter->pdev->dev, "%s: op_mode %d\n",
1798 		 __func__, ahw->op_mode);
1799 	return err;
1800 }
1801 
1802 int qlcnic_sriov_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1803 {
1804 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1805 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1806 	int i, num_vfs;
1807 	struct qlcnic_vf_info *vf_info;
1808 	u8 *curr_mac;
1809 
1810 	if (!qlcnic_sriov_pf_check(adapter))
1811 		return -EOPNOTSUPP;
1812 
1813 	num_vfs = sriov->num_vfs;
1814 
1815 	if (!is_valid_ether_addr(mac) || vf >= num_vfs)
1816 		return -EINVAL;
1817 
1818 	if (ether_addr_equal(adapter->mac_addr, mac)) {
1819 		netdev_err(netdev, "MAC address is already in use by the PF\n");
1820 		return -EINVAL;
1821 	}
1822 
1823 	for (i = 0; i < num_vfs; i++) {
1824 		vf_info = &sriov->vf_info[i];
1825 		if (ether_addr_equal(vf_info->vp->mac, mac)) {
1826 			netdev_err(netdev,
1827 				   "MAC address is already in use by VF %d\n",
1828 				   i);
1829 			return -EINVAL;
1830 		}
1831 	}
1832 
1833 	vf_info = &sriov->vf_info[vf];
1834 	curr_mac = vf_info->vp->mac;
1835 
1836 	if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
1837 		netdev_err(netdev,
1838 			   "MAC address change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n",
1839 			   vf);
1840 		return -EOPNOTSUPP;
1841 	}
1842 
1843 	memcpy(curr_mac, mac, netdev->addr_len);
1844 	netdev_info(netdev, "MAC Address %pM  is configured for VF %d\n",
1845 		    mac, vf);
1846 	return 0;
1847 }
1848 
1849 int qlcnic_sriov_set_vf_tx_rate(struct net_device *netdev, int vf,
1850 				int min_tx_rate, int max_tx_rate)
1851 {
1852 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1853 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1854 	struct qlcnic_vf_info *vf_info;
1855 	struct qlcnic_info nic_info;
1856 	struct qlcnic_vport *vp;
1857 	u16 vpid;
1858 
1859 	if (!qlcnic_sriov_pf_check(adapter))
1860 		return -EOPNOTSUPP;
1861 
1862 	if (vf >= sriov->num_vfs)
1863 		return -EINVAL;
1864 
1865 	vf_info = &sriov->vf_info[vf];
1866 	vp = vf_info->vp;
1867 	vpid = vp->handle;
1868 
1869 	if (!min_tx_rate)
1870 		min_tx_rate = QLC_VF_MIN_TX_RATE;
1871 
1872 	if (max_tx_rate &&
1873 	    (max_tx_rate >= 10000 || max_tx_rate < min_tx_rate)) {
1874 		netdev_err(netdev,
1875 			   "Invalid max Tx rate, allowed range is [%d - %d]",
1876 			   min_tx_rate, QLC_VF_MAX_TX_RATE);
1877 		return -EINVAL;
1878 	}
1879 
1880 	if (!max_tx_rate)
1881 		max_tx_rate = 10000;
1882 
1883 	if (min_tx_rate &&
1884 	    (min_tx_rate > max_tx_rate || min_tx_rate < QLC_VF_MIN_TX_RATE)) {
1885 		netdev_err(netdev,
1886 			   "Invalid min Tx rate, allowed range is [%d - %d]",
1887 			   QLC_VF_MIN_TX_RATE, max_tx_rate);
1888 		return -EINVAL;
1889 	}
1890 
1891 	if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
1892 		if (qlcnic_sriov_get_vf_vport_info(adapter, &nic_info, vpid))
1893 			return -EIO;
1894 
1895 		nic_info.max_tx_bw = max_tx_rate / 100;
1896 		nic_info.min_tx_bw = min_tx_rate / 100;
1897 		nic_info.bit_offsets = BIT_0;
1898 
1899 		if (qlcnic_sriov_pf_set_vport_info(adapter, &nic_info, vpid))
1900 			return -EIO;
1901 	}
1902 
1903 	vp->max_tx_bw = max_tx_rate / 100;
1904 	netdev_info(netdev,
1905 		    "Setting Max Tx rate %d (Mbps), %d %% of PF bandwidth, for VF %d\n",
1906 		    max_tx_rate, vp->max_tx_bw, vf);
1907 	vp->min_tx_bw = min_tx_rate / 100;
1908 	netdev_info(netdev,
1909 		    "Setting Min Tx rate %d (Mbps), %d %% of PF bandwidth, for VF %d\n",
1910 		    min_tx_rate, vp->min_tx_bw, vf);
1911 	return 0;
1912 }
1913 
1914 int qlcnic_sriov_set_vf_vlan(struct net_device *netdev, int vf,
1915 			     u16 vlan, u8 qos, __be16 vlan_proto)
1916 {
1917 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1918 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1919 	struct qlcnic_vf_info *vf_info;
1920 	struct qlcnic_vport *vp;
1921 
1922 	if (!qlcnic_sriov_pf_check(adapter))
1923 		return -EOPNOTSUPP;
1924 
1925 	if (vf >= sriov->num_vfs || qos > 7)
1926 		return -EINVAL;
1927 
1928 	if (vlan_proto != htons(ETH_P_8021Q))
1929 		return -EPROTONOSUPPORT;
1930 
1931 	if (vlan > MAX_VLAN_ID) {
1932 		netdev_err(netdev,
1933 			   "Invalid VLAN ID, allowed range is [0 - %d]\n",
1934 			   MAX_VLAN_ID);
1935 		return -EINVAL;
1936 	}
1937 
1938 	vf_info = &sriov->vf_info[vf];
1939 	vp = vf_info->vp;
1940 	if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
1941 		netdev_err(netdev,
1942 			   "VLAN change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n",
1943 			   vf);
1944 		return -EOPNOTSUPP;
1945 	}
1946 
1947 	memset(vf_info->sriov_vlans, 0,
1948 	       sizeof(*vf_info->sriov_vlans) * sriov->num_allowed_vlans);
1949 
1950 	switch (vlan) {
1951 	case 4095:
1952 		vp->vlan_mode = QLC_GUEST_VLAN_MODE;
1953 		break;
1954 	case 0:
1955 		vp->vlan_mode = QLC_NO_VLAN_MODE;
1956 		vp->qos = 0;
1957 		break;
1958 	default:
1959 		vp->vlan_mode = QLC_PVID_MODE;
1960 		qlcnic_sriov_add_vlan_id(sriov, vf_info, vlan);
1961 		vp->qos = qos;
1962 		vp->pvid = vlan;
1963 	}
1964 
1965 	netdev_info(netdev, "Setting VLAN %d, QoS %d, for VF %d\n",
1966 		    vlan, qos, vf);
1967 	return 0;
1968 }
1969 
1970 static __u32 qlcnic_sriov_get_vf_vlan(struct qlcnic_adapter *adapter,
1971 				      struct qlcnic_vport *vp, int vf)
1972 {
1973 	__u32 vlan = 0;
1974 
1975 	switch (vp->vlan_mode) {
1976 	case QLC_PVID_MODE:
1977 		vlan = vp->pvid;
1978 		break;
1979 	case QLC_GUEST_VLAN_MODE:
1980 		vlan = MAX_VLAN_ID;
1981 		break;
1982 	case QLC_NO_VLAN_MODE:
1983 		vlan = 0;
1984 		break;
1985 	default:
1986 		netdev_info(adapter->netdev, "Invalid VLAN mode = %d for VF %d\n",
1987 			    vp->vlan_mode, vf);
1988 	}
1989 
1990 	return vlan;
1991 }
1992 
1993 int qlcnic_sriov_get_vf_config(struct net_device *netdev,
1994 			       int vf, struct ifla_vf_info *ivi)
1995 {
1996 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1997 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1998 	struct qlcnic_vport *vp;
1999 
2000 	if (!qlcnic_sriov_pf_check(adapter))
2001 		return -EOPNOTSUPP;
2002 
2003 	if (vf >= sriov->num_vfs)
2004 		return -EINVAL;
2005 
2006 	vp = sriov->vf_info[vf].vp;
2007 	memcpy(&ivi->mac, vp->mac, ETH_ALEN);
2008 	ivi->vlan = qlcnic_sriov_get_vf_vlan(adapter, vp, vf);
2009 	ivi->qos = vp->qos;
2010 	ivi->spoofchk = vp->spoofchk;
2011 	if (vp->max_tx_bw == MAX_BW)
2012 		ivi->max_tx_rate = 0;
2013 	else
2014 		ivi->max_tx_rate = vp->max_tx_bw * 100;
2015 	if (vp->min_tx_bw == MIN_BW)
2016 		ivi->min_tx_rate = 0;
2017 	else
2018 		ivi->min_tx_rate = vp->min_tx_bw * 100;
2019 
2020 	ivi->vf = vf;
2021 	return 0;
2022 }
2023 
2024 int qlcnic_sriov_set_vf_spoofchk(struct net_device *netdev, int vf, bool chk)
2025 {
2026 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2027 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
2028 	struct qlcnic_vf_info *vf_info;
2029 	struct qlcnic_vport *vp;
2030 
2031 	if (!qlcnic_sriov_pf_check(adapter))
2032 		return -EOPNOTSUPP;
2033 
2034 	if (vf >= sriov->num_vfs)
2035 		return -EINVAL;
2036 
2037 	vf_info = &sriov->vf_info[vf];
2038 	vp = vf_info->vp;
2039 	if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
2040 		netdev_err(netdev,
2041 			   "Spoof check change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n",
2042 			   vf);
2043 		return -EOPNOTSUPP;
2044 	}
2045 
2046 	vp->spoofchk = chk;
2047 	return 0;
2048 }
2049