xref: /linux/drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_pf.c (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
1 /*
2  * QLogic qlcnic NIC Driver
3  * Copyright (c) 2009-2013 QLogic Corporation
4  *
5  * See LICENSE.qlcnic for copyright and licensing details.
6  */
7 
8 #include "qlcnic_sriov.h"
9 #include "qlcnic.h"
10 #include <linux/types.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 	qlcnic_sriov_alloc_vlans(adapter);
601 
602 	return err;
603 
604 del_flr_queue:
605 	qlcnic_sriov_pf_del_flr_queue(adapter);
606 
607 sriov_cleanup:
608 	__qlcnic_sriov_cleanup(adapter);
609 
610 clear_op_mode:
611 	clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
612 	adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
613 	return err;
614 }
615 
616 static int qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter, int num_vfs)
617 {
618 	struct net_device *netdev = adapter->netdev;
619 	int err;
620 
621 	if (!(adapter->flags & QLCNIC_MSIX_ENABLED)) {
622 		netdev_err(netdev,
623 			   "SR-IOV cannot be enabled, when legacy interrupts are enabled\n");
624 		return -EIO;
625 	}
626 
627 	rtnl_lock();
628 	if (netif_running(netdev))
629 		__qlcnic_down(adapter, netdev);
630 
631 	err = __qlcnic_pci_sriov_enable(adapter, num_vfs);
632 	if (err)
633 		goto error;
634 
635 	if (netif_running(netdev))
636 		__qlcnic_up(adapter, netdev);
637 
638 	rtnl_unlock();
639 	err = qlcnic_sriov_pf_enable(adapter, num_vfs);
640 	if (!err) {
641 		netdev_info(netdev,
642 			    "SR-IOV is enabled successfully on port %d\n",
643 			    adapter->portnum);
644 		/* Return number of vfs enabled */
645 		return num_vfs;
646 	}
647 
648 	rtnl_lock();
649 	if (netif_running(netdev))
650 		__qlcnic_down(adapter, netdev);
651 
652 error:
653 	if (!qlcnic_83xx_configure_opmode(adapter)) {
654 		if (netif_running(netdev))
655 			__qlcnic_up(adapter, netdev);
656 	}
657 
658 	rtnl_unlock();
659 	netdev_info(netdev, "Failed to enable SR-IOV on port %d\n",
660 		    adapter->portnum);
661 
662 	return err;
663 }
664 
665 int qlcnic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
666 {
667 	struct qlcnic_adapter *adapter = pci_get_drvdata(dev);
668 	int err;
669 
670 	if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
671 		return -EBUSY;
672 
673 	if (num_vfs == 0)
674 		err = qlcnic_pci_sriov_disable(adapter);
675 	else
676 		err = qlcnic_pci_sriov_enable(adapter, num_vfs);
677 
678 	clear_bit(__QLCNIC_RESETTING, &adapter->state);
679 	return err;
680 }
681 
682 static int qlcnic_sriov_set_vf_acl(struct qlcnic_adapter *adapter, u8 func)
683 {
684 	struct qlcnic_cmd_args cmd;
685 	struct qlcnic_vport *vp;
686 	int err, id;
687 	u8 *mac;
688 
689 	id = qlcnic_sriov_func_to_index(adapter, func);
690 	if (id < 0)
691 		return id;
692 
693 	vp = adapter->ahw->sriov->vf_info[id].vp;
694 	err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO);
695 	if (err)
696 		return err;
697 
698 	cmd.req.arg[1] = 0x3 | func << 16;
699 	if (vp->spoofchk == true) {
700 		mac = vp->mac;
701 		cmd.req.arg[2] |= BIT_1 | BIT_3 | BIT_8;
702 		cmd.req.arg[4] = mac[5] | mac[4] << 8 | mac[3] << 16 |
703 				 mac[2] << 24;
704 		cmd.req.arg[5] = mac[1] | mac[0] << 8;
705 	}
706 
707 	if (vp->vlan_mode == QLC_PVID_MODE) {
708 		cmd.req.arg[2] |= BIT_6;
709 		cmd.req.arg[3] |= vp->pvid << 8;
710 	}
711 
712 	err = qlcnic_issue_cmd(adapter, &cmd);
713 	if (err)
714 		dev_err(&adapter->pdev->dev, "Failed to set ACL, err=%d\n",
715 			err);
716 
717 	qlcnic_free_mbx_args(&cmd);
718 	return err;
719 }
720 
721 static int qlcnic_sriov_set_vf_vport_info(struct qlcnic_adapter *adapter,
722 					  u16 func)
723 {
724 	struct qlcnic_info defvp_info;
725 	int err;
726 
727 	err = qlcnic_sriov_pf_cal_res_limit(adapter, &defvp_info, func);
728 	if (err)
729 		return -EIO;
730 
731 	err = qlcnic_sriov_set_vf_acl(adapter, func);
732 	if (err)
733 		return err;
734 
735 	return 0;
736 }
737 
738 static int qlcnic_sriov_pf_channel_cfg_cmd(struct qlcnic_bc_trans *trans,
739 					   struct qlcnic_cmd_args *cmd)
740 {
741 	struct qlcnic_vf_info *vf = trans->vf;
742 	struct qlcnic_vport *vp = vf->vp;
743 	struct qlcnic_adapter *adapter;
744 	struct qlcnic_sriov *sriov;
745 	u16 func = vf->pci_func;
746 	size_t size;
747 	int err;
748 
749 	adapter = vf->adapter;
750 	sriov = adapter->ahw->sriov;
751 
752 	if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) {
753 		err = qlcnic_sriov_pf_config_vport(adapter, 1, func);
754 		if (!err) {
755 			err = qlcnic_sriov_set_vf_vport_info(adapter, func);
756 			if (err)
757 				qlcnic_sriov_pf_config_vport(adapter, 0, func);
758 		}
759 	} else {
760 		if (vp->vlan_mode == QLC_GUEST_VLAN_MODE) {
761 			size = sizeof(*vf->sriov_vlans);
762 			size = size * sriov->num_allowed_vlans;
763 			memset(vf->sriov_vlans, 0, size);
764 		}
765 
766 		err = qlcnic_sriov_pf_config_vport(adapter, 0, func);
767 	}
768 
769 	if (err)
770 		goto err_out;
771 
772 	cmd->rsp.arg[0] |= (1 << 25);
773 
774 	if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT)
775 		set_bit(QLC_BC_VF_STATE, &vf->state);
776 	else
777 		clear_bit(QLC_BC_VF_STATE, &vf->state);
778 
779 	return err;
780 
781 err_out:
782 	cmd->rsp.arg[0] |= (2 << 25);
783 	return err;
784 }
785 
786 static int qlcnic_sriov_cfg_vf_def_mac(struct qlcnic_adapter *adapter,
787 				       struct qlcnic_vf_info *vf,
788 				       u16 vlan, u8 op)
789 {
790 	struct qlcnic_cmd_args *cmd;
791 	struct qlcnic_macvlan_mbx mv;
792 	struct qlcnic_vport *vp;
793 	u8 *addr;
794 	int err;
795 	u32 *buf;
796 	int vpid;
797 
798 	vp = vf->vp;
799 
800 	cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
801 	if (!cmd)
802 		return -ENOMEM;
803 
804 	err = qlcnic_alloc_mbx_args(cmd, adapter, QLCNIC_CMD_CONFIG_MAC_VLAN);
805 	if (err)
806 		goto free_cmd;
807 
808 	cmd->type = QLC_83XX_MBX_CMD_NO_WAIT;
809 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func);
810 	if (vpid < 0) {
811 		err = -EINVAL;
812 		goto free_args;
813 	}
814 
815 	if (vlan)
816 		op = ((op == QLCNIC_MAC_ADD || op == QLCNIC_MAC_VLAN_ADD) ?
817 		      QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_VLAN_DEL);
818 
819 	cmd->req.arg[1] = op | (1 << 8) | (3 << 6);
820 	cmd->req.arg[1] |= ((vpid & 0xffff) << 16) | BIT_31;
821 
822 	addr = vp->mac;
823 	mv.vlan = vlan;
824 	mv.mac_addr0 = addr[0];
825 	mv.mac_addr1 = addr[1];
826 	mv.mac_addr2 = addr[2];
827 	mv.mac_addr3 = addr[3];
828 	mv.mac_addr4 = addr[4];
829 	mv.mac_addr5 = addr[5];
830 	buf = &cmd->req.arg[2];
831 	memcpy(buf, &mv, sizeof(struct qlcnic_macvlan_mbx));
832 
833 	err = qlcnic_issue_cmd(adapter, cmd);
834 
835 	if (!err)
836 		return err;
837 
838 free_args:
839 	qlcnic_free_mbx_args(cmd);
840 free_cmd:
841 	kfree(cmd);
842 	return err;
843 }
844 
845 static int qlcnic_sriov_validate_create_rx_ctx(struct qlcnic_cmd_args *cmd)
846 {
847 	if ((cmd->req.arg[0] >> 29) != 0x3)
848 		return -EINVAL;
849 
850 	return 0;
851 }
852 
853 static void qlcnic_83xx_cfg_default_mac_vlan(struct qlcnic_adapter *adapter,
854 					     struct qlcnic_vf_info *vf,
855 					     int opcode)
856 {
857 	struct qlcnic_sriov *sriov;
858 	u16 vlan;
859 	int i;
860 
861 	sriov = adapter->ahw->sriov;
862 
863 	spin_lock_bh(&vf->vlan_list_lock);
864 	if (vf->num_vlan) {
865 		for (i = 0; i < sriov->num_allowed_vlans; i++) {
866 			vlan = vf->sriov_vlans[i];
867 			if (vlan)
868 				qlcnic_sriov_cfg_vf_def_mac(adapter, vf, vlan,
869 							    opcode);
870 		}
871 	}
872 	spin_unlock_bh(&vf->vlan_list_lock);
873 
874 	if (vf->vp->vlan_mode != QLC_PVID_MODE) {
875 		if (qlcnic_83xx_pf_check(adapter) &&
876 		    qlcnic_sriov_check_any_vlan(vf))
877 			return;
878 		qlcnic_sriov_cfg_vf_def_mac(adapter, vf, 0, opcode);
879 	}
880 }
881 
882 static int qlcnic_sriov_pf_create_rx_ctx_cmd(struct qlcnic_bc_trans *tran,
883 					     struct qlcnic_cmd_args *cmd)
884 {
885 	struct qlcnic_vf_info *vf = tran->vf;
886 	struct qlcnic_adapter *adapter = vf->adapter;
887 	struct qlcnic_rcv_mbx_out *mbx_out;
888 	int err;
889 
890 	err = qlcnic_sriov_validate_create_rx_ctx(cmd);
891 	if (err) {
892 		cmd->rsp.arg[0] |= (0x6 << 25);
893 		return err;
894 	}
895 
896 	cmd->req.arg[6] = vf->vp->handle;
897 	err = qlcnic_issue_cmd(adapter, cmd);
898 
899 	if (!err) {
900 		mbx_out = (struct qlcnic_rcv_mbx_out *)&cmd->rsp.arg[1];
901 		vf->rx_ctx_id = mbx_out->ctx_id;
902 		qlcnic_83xx_cfg_default_mac_vlan(adapter, vf, QLCNIC_MAC_ADD);
903 	} else {
904 		vf->rx_ctx_id = 0;
905 	}
906 
907 	return err;
908 }
909 
910 static int qlcnic_sriov_pf_mac_address_cmd(struct qlcnic_bc_trans *trans,
911 					   struct qlcnic_cmd_args *cmd)
912 {
913 	struct qlcnic_vf_info *vf = trans->vf;
914 	u8 type, *mac;
915 
916 	type = cmd->req.arg[1];
917 	switch (type) {
918 	case QLCNIC_SET_STATION_MAC:
919 	case QLCNIC_SET_FAC_DEF_MAC:
920 		cmd->rsp.arg[0] = (2 << 25);
921 		break;
922 	case QLCNIC_GET_CURRENT_MAC:
923 		cmd->rsp.arg[0] = (1 << 25);
924 		mac = vf->vp->mac;
925 		cmd->rsp.arg[2] = mac[1] | ((mac[0] << 8) & 0xff00);
926 		cmd->rsp.arg[1] = mac[5] | ((mac[4] << 8) & 0xff00) |
927 				  ((mac[3]) << 16 & 0xff0000) |
928 				  ((mac[2]) << 24 & 0xff000000);
929 	}
930 
931 	return 0;
932 }
933 
934 static int qlcnic_sriov_validate_create_tx_ctx(struct qlcnic_cmd_args *cmd)
935 {
936 	if ((cmd->req.arg[0] >> 29) != 0x3)
937 		return -EINVAL;
938 
939 	return 0;
940 }
941 
942 static int qlcnic_sriov_pf_create_tx_ctx_cmd(struct qlcnic_bc_trans *trans,
943 					     struct qlcnic_cmd_args *cmd)
944 {
945 	struct qlcnic_vf_info *vf = trans->vf;
946 	struct qlcnic_adapter *adapter = vf->adapter;
947 	struct qlcnic_tx_mbx_out *mbx_out;
948 	int err;
949 
950 	err = qlcnic_sriov_validate_create_tx_ctx(cmd);
951 	if (err) {
952 		cmd->rsp.arg[0] |= (0x6 << 25);
953 		return err;
954 	}
955 
956 	cmd->req.arg[5] |= vf->vp->handle << 16;
957 	err = qlcnic_issue_cmd(adapter, cmd);
958 	if (!err) {
959 		mbx_out = (struct qlcnic_tx_mbx_out *)&cmd->rsp.arg[2];
960 		vf->tx_ctx_id = mbx_out->ctx_id;
961 	} else {
962 		vf->tx_ctx_id = 0;
963 	}
964 
965 	return err;
966 }
967 
968 static int qlcnic_sriov_validate_del_rx_ctx(struct qlcnic_vf_info *vf,
969 					    struct qlcnic_cmd_args *cmd)
970 {
971 	if ((cmd->req.arg[0] >> 29) != 0x3)
972 		return -EINVAL;
973 
974 	if ((cmd->req.arg[1] & 0xffff) != vf->rx_ctx_id)
975 		return -EINVAL;
976 
977 	return 0;
978 }
979 
980 static int qlcnic_sriov_pf_del_rx_ctx_cmd(struct qlcnic_bc_trans *trans,
981 					  struct qlcnic_cmd_args *cmd)
982 {
983 	struct qlcnic_vf_info *vf = trans->vf;
984 	struct qlcnic_adapter *adapter = vf->adapter;
985 	int err;
986 
987 	err = qlcnic_sriov_validate_del_rx_ctx(vf, cmd);
988 	if (err) {
989 		cmd->rsp.arg[0] |= (0x6 << 25);
990 		return err;
991 	}
992 
993 	qlcnic_83xx_cfg_default_mac_vlan(adapter, vf, QLCNIC_MAC_DEL);
994 	cmd->req.arg[1] |= vf->vp->handle << 16;
995 	err = qlcnic_issue_cmd(adapter, cmd);
996 
997 	if (!err)
998 		vf->rx_ctx_id = 0;
999 
1000 	return err;
1001 }
1002 
1003 static int qlcnic_sriov_validate_del_tx_ctx(struct qlcnic_vf_info *vf,
1004 					    struct qlcnic_cmd_args *cmd)
1005 {
1006 	if ((cmd->req.arg[0] >> 29) != 0x3)
1007 		return -EINVAL;
1008 
1009 	if ((cmd->req.arg[1] & 0xffff) != vf->tx_ctx_id)
1010 		return -EINVAL;
1011 
1012 	return 0;
1013 }
1014 
1015 static int qlcnic_sriov_pf_del_tx_ctx_cmd(struct qlcnic_bc_trans *trans,
1016 					  struct qlcnic_cmd_args *cmd)
1017 {
1018 	struct qlcnic_vf_info *vf = trans->vf;
1019 	struct qlcnic_adapter *adapter = vf->adapter;
1020 	int err;
1021 
1022 	err = qlcnic_sriov_validate_del_tx_ctx(vf, cmd);
1023 	if (err) {
1024 		cmd->rsp.arg[0] |= (0x6 << 25);
1025 		return err;
1026 	}
1027 
1028 	cmd->req.arg[1] |= vf->vp->handle << 16;
1029 	err = qlcnic_issue_cmd(adapter, cmd);
1030 
1031 	if (!err)
1032 		vf->tx_ctx_id = 0;
1033 
1034 	return err;
1035 }
1036 
1037 static int qlcnic_sriov_validate_cfg_lro(struct qlcnic_vf_info *vf,
1038 					 struct qlcnic_cmd_args *cmd)
1039 {
1040 	if ((cmd->req.arg[1] >> 16) != vf->rx_ctx_id)
1041 		return -EINVAL;
1042 
1043 	return 0;
1044 }
1045 
1046 static int qlcnic_sriov_pf_cfg_lro_cmd(struct qlcnic_bc_trans *trans,
1047 				       struct qlcnic_cmd_args *cmd)
1048 {
1049 	struct qlcnic_vf_info *vf = trans->vf;
1050 	struct qlcnic_adapter *adapter = vf->adapter;
1051 	int err;
1052 
1053 	err = qlcnic_sriov_validate_cfg_lro(vf, cmd);
1054 	if (err) {
1055 		cmd->rsp.arg[0] |= (0x6 << 25);
1056 		return err;
1057 	}
1058 
1059 	err = qlcnic_issue_cmd(adapter, cmd);
1060 	return err;
1061 }
1062 
1063 static int qlcnic_sriov_pf_cfg_ip_cmd(struct qlcnic_bc_trans *trans,
1064 				      struct qlcnic_cmd_args *cmd)
1065 {
1066 	struct qlcnic_vf_info *vf = trans->vf;
1067 	struct qlcnic_adapter *adapter = vf->adapter;
1068 	int err = -EIO;
1069 	u8 op;
1070 
1071 	op =  cmd->req.arg[1] & 0xff;
1072 
1073 	cmd->req.arg[1] |= vf->vp->handle << 16;
1074 	cmd->req.arg[1] |= BIT_31;
1075 
1076 	err = qlcnic_issue_cmd(adapter, cmd);
1077 	return err;
1078 }
1079 
1080 static int qlcnic_sriov_validate_cfg_intrpt(struct qlcnic_vf_info *vf,
1081 					    struct qlcnic_cmd_args *cmd)
1082 {
1083 	if (((cmd->req.arg[1] >> 8) & 0xff) != vf->pci_func)
1084 		return -EINVAL;
1085 
1086 	if (!(cmd->req.arg[1] & BIT_16))
1087 		return -EINVAL;
1088 
1089 	if ((cmd->req.arg[1] & 0xff) != 0x1)
1090 		return -EINVAL;
1091 
1092 	return 0;
1093 }
1094 
1095 static int qlcnic_sriov_pf_cfg_intrpt_cmd(struct qlcnic_bc_trans *trans,
1096 					  struct qlcnic_cmd_args *cmd)
1097 {
1098 	struct qlcnic_vf_info *vf = trans->vf;
1099 	struct qlcnic_adapter *adapter = vf->adapter;
1100 	int err;
1101 
1102 	err = qlcnic_sriov_validate_cfg_intrpt(vf, cmd);
1103 	if (err)
1104 		cmd->rsp.arg[0] |= (0x6 << 25);
1105 	else
1106 		err = qlcnic_issue_cmd(adapter, cmd);
1107 
1108 	return err;
1109 }
1110 
1111 static int qlcnic_sriov_validate_mtu(struct qlcnic_adapter *adapter,
1112 				     struct qlcnic_vf_info *vf,
1113 				     struct qlcnic_cmd_args *cmd)
1114 {
1115 	if (cmd->req.arg[1] != vf->rx_ctx_id)
1116 		return -EINVAL;
1117 
1118 	if (cmd->req.arg[2] > adapter->ahw->max_mtu)
1119 		return -EINVAL;
1120 
1121 	return 0;
1122 }
1123 
1124 static int qlcnic_sriov_pf_set_mtu_cmd(struct qlcnic_bc_trans *trans,
1125 				       struct qlcnic_cmd_args *cmd)
1126 {
1127 	struct qlcnic_vf_info *vf = trans->vf;
1128 	struct qlcnic_adapter *adapter = vf->adapter;
1129 	int err;
1130 
1131 	err = qlcnic_sriov_validate_mtu(adapter, vf, cmd);
1132 	if (err)
1133 		cmd->rsp.arg[0] |= (0x6 << 25);
1134 	else
1135 		err = qlcnic_issue_cmd(adapter, cmd);
1136 
1137 	return err;
1138 }
1139 
1140 static int qlcnic_sriov_validate_get_nic_info(struct qlcnic_vf_info *vf,
1141 					      struct qlcnic_cmd_args *cmd)
1142 {
1143 	if (cmd->req.arg[1] & BIT_31) {
1144 		if (((cmd->req.arg[1] >> 16) & 0x7fff) != vf->pci_func)
1145 			return -EINVAL;
1146 	} else {
1147 		cmd->req.arg[1] |= vf->vp->handle << 16;
1148 	}
1149 
1150 	return 0;
1151 }
1152 
1153 static int qlcnic_sriov_pf_get_nic_info_cmd(struct qlcnic_bc_trans *trans,
1154 					    struct qlcnic_cmd_args *cmd)
1155 {
1156 	struct qlcnic_vf_info *vf = trans->vf;
1157 	struct qlcnic_adapter *adapter = vf->adapter;
1158 	int err;
1159 
1160 	err = qlcnic_sriov_validate_get_nic_info(vf, cmd);
1161 	if (err) {
1162 		cmd->rsp.arg[0] |= (0x6 << 25);
1163 		return err;
1164 	}
1165 
1166 	err = qlcnic_issue_cmd(adapter, cmd);
1167 	return err;
1168 }
1169 
1170 static int qlcnic_sriov_validate_cfg_rss(struct qlcnic_vf_info *vf,
1171 					 struct qlcnic_cmd_args *cmd)
1172 {
1173 	if (cmd->req.arg[1] != vf->rx_ctx_id)
1174 		return -EINVAL;
1175 
1176 	return 0;
1177 }
1178 
1179 static int qlcnic_sriov_pf_cfg_rss_cmd(struct qlcnic_bc_trans *trans,
1180 				       struct qlcnic_cmd_args *cmd)
1181 {
1182 	struct qlcnic_vf_info *vf = trans->vf;
1183 	struct qlcnic_adapter *adapter = vf->adapter;
1184 	int err;
1185 
1186 	err = qlcnic_sriov_validate_cfg_rss(vf, cmd);
1187 	if (err)
1188 		cmd->rsp.arg[0] |= (0x6 << 25);
1189 	else
1190 		err = qlcnic_issue_cmd(adapter, cmd);
1191 
1192 	return err;
1193 }
1194 
1195 static int qlcnic_sriov_validate_cfg_intrcoal(struct qlcnic_adapter *adapter,
1196 					      struct qlcnic_vf_info *vf,
1197 					      struct qlcnic_cmd_args *cmd)
1198 {
1199 	struct qlcnic_nic_intr_coalesce *coal = &adapter->ahw->coal;
1200 	u16 ctx_id, pkts, time;
1201 	int err = -EINVAL;
1202 	u8 type;
1203 
1204 	type = cmd->req.arg[1] & QLC_INTR_COAL_TYPE_MASK;
1205 	ctx_id = cmd->req.arg[1] >> 16;
1206 	pkts = cmd->req.arg[2] & 0xffff;
1207 	time = cmd->req.arg[2] >> 16;
1208 
1209 	switch (type) {
1210 	case QLCNIC_INTR_COAL_TYPE_RX:
1211 		if (ctx_id != vf->rx_ctx_id || pkts > coal->rx_packets ||
1212 		    time < coal->rx_time_us)
1213 			goto err_label;
1214 		break;
1215 	case QLCNIC_INTR_COAL_TYPE_TX:
1216 		if (ctx_id != vf->tx_ctx_id || pkts > coal->tx_packets ||
1217 		    time < coal->tx_time_us)
1218 			goto err_label;
1219 		break;
1220 	default:
1221 		netdev_err(adapter->netdev, "Invalid coalescing type 0x%x received\n",
1222 			   type);
1223 		return err;
1224 	}
1225 
1226 	return 0;
1227 
1228 err_label:
1229 	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",
1230 		   vf->rx_ctx_id, coal->rx_packets, coal->rx_time_us,
1231 		   vf->tx_ctx_id, coal->tx_packets, coal->tx_time_us);
1232 	netdev_err(adapter->netdev, "Received: ctx_id 0x%x packets 0x%x time_us 0x%x type 0x%x\n",
1233 		   ctx_id, pkts, time, type);
1234 
1235 	return err;
1236 }
1237 
1238 static int qlcnic_sriov_pf_cfg_intrcoal_cmd(struct qlcnic_bc_trans *tran,
1239 					    struct qlcnic_cmd_args *cmd)
1240 {
1241 	struct qlcnic_vf_info *vf = tran->vf;
1242 	struct qlcnic_adapter *adapter = vf->adapter;
1243 	int err;
1244 
1245 	err = qlcnic_sriov_validate_cfg_intrcoal(adapter, vf, cmd);
1246 	if (err) {
1247 		cmd->rsp.arg[0] |= (0x6 << 25);
1248 		return err;
1249 	}
1250 
1251 	err = qlcnic_issue_cmd(adapter, cmd);
1252 	return err;
1253 }
1254 
1255 static int qlcnic_sriov_validate_cfg_macvlan(struct qlcnic_adapter *adapter,
1256 					     struct qlcnic_vf_info *vf,
1257 					     struct qlcnic_cmd_args *cmd)
1258 {
1259 	struct qlcnic_vport *vp = vf->vp;
1260 	u8 op, new_op;
1261 
1262 	if (!(cmd->req.arg[1] & BIT_8))
1263 		return -EINVAL;
1264 
1265 	cmd->req.arg[1] |= (vf->vp->handle << 16);
1266 	cmd->req.arg[1] |= BIT_31;
1267 
1268 	if (vp->vlan_mode == QLC_PVID_MODE) {
1269 		op = cmd->req.arg[1] & 0x7;
1270 		cmd->req.arg[1] &= ~0x7;
1271 		new_op = (op == QLCNIC_MAC_ADD || op == QLCNIC_MAC_VLAN_ADD) ?
1272 			 QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_VLAN_DEL;
1273 		cmd->req.arg[3] |= vp->pvid << 16;
1274 		cmd->req.arg[1] |= new_op;
1275 	}
1276 
1277 	return 0;
1278 }
1279 
1280 static int qlcnic_sriov_pf_cfg_macvlan_cmd(struct qlcnic_bc_trans *trans,
1281 					   struct qlcnic_cmd_args *cmd)
1282 {
1283 	struct qlcnic_vf_info *vf = trans->vf;
1284 	struct qlcnic_adapter *adapter = vf->adapter;
1285 	int err;
1286 
1287 	err = qlcnic_sriov_validate_cfg_macvlan(adapter, vf, cmd);
1288 	if (err) {
1289 		cmd->rsp.arg[0] |= (0x6 << 25);
1290 		return err;
1291 	}
1292 
1293 	err = qlcnic_issue_cmd(adapter, cmd);
1294 	return err;
1295 }
1296 
1297 static int qlcnic_sriov_validate_linkevent(struct qlcnic_vf_info *vf,
1298 					   struct qlcnic_cmd_args *cmd)
1299 {
1300 	if ((cmd->req.arg[1] >> 16) != vf->rx_ctx_id)
1301 		return -EINVAL;
1302 
1303 	return 0;
1304 }
1305 
1306 static int qlcnic_sriov_pf_linkevent_cmd(struct qlcnic_bc_trans *trans,
1307 					 struct qlcnic_cmd_args *cmd)
1308 {
1309 	struct qlcnic_vf_info *vf = trans->vf;
1310 	struct qlcnic_adapter *adapter = vf->adapter;
1311 	int err;
1312 
1313 	err = qlcnic_sriov_validate_linkevent(vf, cmd);
1314 	if (err) {
1315 		cmd->rsp.arg[0] |= (0x6 << 25);
1316 		return err;
1317 	}
1318 
1319 	err = qlcnic_issue_cmd(adapter, cmd);
1320 	return err;
1321 }
1322 
1323 static int qlcnic_sriov_pf_cfg_promisc_cmd(struct qlcnic_bc_trans *trans,
1324 					   struct qlcnic_cmd_args *cmd)
1325 {
1326 	struct qlcnic_vf_info *vf = trans->vf;
1327 	struct qlcnic_adapter *adapter = vf->adapter;
1328 	int err;
1329 
1330 	cmd->req.arg[1] |= vf->vp->handle << 16;
1331 	cmd->req.arg[1] |= BIT_31;
1332 	err = qlcnic_issue_cmd(adapter, cmd);
1333 	return err;
1334 }
1335 
1336 static int qlcnic_sriov_pf_get_acl_cmd(struct qlcnic_bc_trans *trans,
1337 				       struct qlcnic_cmd_args *cmd)
1338 {
1339 	struct qlcnic_vf_info *vf = trans->vf;
1340 	struct qlcnic_vport *vp = vf->vp;
1341 	u8 cmd_op, mode = vp->vlan_mode;
1342 	struct qlcnic_adapter *adapter;
1343 	struct qlcnic_sriov *sriov;
1344 
1345 	adapter = vf->adapter;
1346 	sriov = adapter->ahw->sriov;
1347 
1348 	cmd_op = trans->req_hdr->cmd_op;
1349 	cmd->rsp.arg[0] |= 1 << 25;
1350 
1351 	/* For 84xx adapter in case of PVID , PFD should send vlan mode as
1352 	 * QLC_NO_VLAN_MODE to VFD which is zero in mailbox response
1353 	 */
1354 	if (qlcnic_84xx_check(adapter) && mode == QLC_PVID_MODE)
1355 		return 0;
1356 
1357 	switch (mode) {
1358 	case QLC_GUEST_VLAN_MODE:
1359 		cmd->rsp.arg[1] = mode | 1 << 8;
1360 		cmd->rsp.arg[2] = sriov->num_allowed_vlans << 16;
1361 		break;
1362 	case QLC_PVID_MODE:
1363 		cmd->rsp.arg[1] = mode | 1 << 8 | vp->pvid << 16;
1364 		break;
1365 	}
1366 
1367 	return 0;
1368 }
1369 
1370 static int qlcnic_sriov_pf_del_guest_vlan(struct qlcnic_adapter *adapter,
1371 					  struct qlcnic_vf_info *vf,
1372 					  struct qlcnic_cmd_args *cmd)
1373 {
1374 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1375 	u16 vlan;
1376 
1377 	if (!qlcnic_sriov_check_any_vlan(vf))
1378 		return -EINVAL;
1379 
1380 	vlan = cmd->req.arg[1] >> 16;
1381 	if (!vf->rx_ctx_id) {
1382 		qlcnic_sriov_del_vlan_id(sriov, vf, vlan);
1383 		return 0;
1384 	}
1385 
1386 	qlcnic_sriov_cfg_vf_def_mac(adapter, vf, vlan, QLCNIC_MAC_DEL);
1387 	qlcnic_sriov_del_vlan_id(sriov, vf, vlan);
1388 
1389 	if (qlcnic_83xx_pf_check(adapter))
1390 		qlcnic_sriov_cfg_vf_def_mac(adapter, vf,
1391 					    0, QLCNIC_MAC_ADD);
1392 	return 0;
1393 }
1394 
1395 static int qlcnic_sriov_pf_add_guest_vlan(struct qlcnic_adapter *adapter,
1396 					  struct qlcnic_vf_info *vf,
1397 					  struct qlcnic_cmd_args *cmd)
1398 {
1399 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1400 	int err = -EIO;
1401 	u16 vlan;
1402 
1403 	if (qlcnic_83xx_pf_check(adapter) && qlcnic_sriov_check_any_vlan(vf))
1404 		return err;
1405 
1406 	vlan = cmd->req.arg[1] >> 16;
1407 
1408 	if (!vf->rx_ctx_id) {
1409 		qlcnic_sriov_add_vlan_id(sriov, vf, vlan);
1410 		return 0;
1411 	}
1412 
1413 	if (qlcnic_83xx_pf_check(adapter)) {
1414 		err = qlcnic_sriov_cfg_vf_def_mac(adapter, vf, 0,
1415 						  QLCNIC_MAC_DEL);
1416 		if (err)
1417 			return err;
1418 	}
1419 
1420 	err = qlcnic_sriov_cfg_vf_def_mac(adapter, vf, vlan, QLCNIC_MAC_ADD);
1421 
1422 	if (err) {
1423 		if (qlcnic_83xx_pf_check(adapter))
1424 			qlcnic_sriov_cfg_vf_def_mac(adapter, vf, 0,
1425 						    QLCNIC_MAC_ADD);
1426 		return err;
1427 	}
1428 
1429 	qlcnic_sriov_add_vlan_id(sriov, vf, vlan);
1430 	return err;
1431 }
1432 
1433 static int qlcnic_sriov_pf_cfg_guest_vlan_cmd(struct qlcnic_bc_trans *tran,
1434 					      struct qlcnic_cmd_args *cmd)
1435 {
1436 	struct qlcnic_vf_info  *vf = tran->vf;
1437 	struct qlcnic_adapter *adapter =  vf->adapter;
1438 	struct qlcnic_vport *vp = vf->vp;
1439 	int err = -EIO;
1440 	u8 op;
1441 
1442 	if (vp->vlan_mode != QLC_GUEST_VLAN_MODE) {
1443 		cmd->rsp.arg[0] |= 2 << 25;
1444 		return err;
1445 	}
1446 
1447 	op = cmd->req.arg[1] & 0xf;
1448 
1449 	if (op)
1450 		err = qlcnic_sriov_pf_add_guest_vlan(adapter, vf, cmd);
1451 	else
1452 		err = qlcnic_sriov_pf_del_guest_vlan(adapter, vf, cmd);
1453 
1454 	cmd->rsp.arg[0] |= err ? 2 << 25 : 1 << 25;
1455 	return err;
1456 }
1457 
1458 static const int qlcnic_pf_passthru_supp_cmds[] = {
1459 	QLCNIC_CMD_GET_STATISTICS,
1460 	QLCNIC_CMD_GET_PORT_CONFIG,
1461 	QLCNIC_CMD_GET_LINK_STATUS,
1462 	QLCNIC_CMD_INIT_NIC_FUNC,
1463 	QLCNIC_CMD_STOP_NIC_FUNC,
1464 };
1465 
1466 static const struct qlcnic_sriov_cmd_handler qlcnic_pf_bc_cmd_hdlr[] = {
1467 	[QLCNIC_BC_CMD_CHANNEL_INIT] = {&qlcnic_sriov_pf_channel_cfg_cmd},
1468 	[QLCNIC_BC_CMD_CHANNEL_TERM] = {&qlcnic_sriov_pf_channel_cfg_cmd},
1469 	[QLCNIC_BC_CMD_GET_ACL]	= {&qlcnic_sriov_pf_get_acl_cmd},
1470 	[QLCNIC_BC_CMD_CFG_GUEST_VLAN]	= {&qlcnic_sriov_pf_cfg_guest_vlan_cmd},
1471 };
1472 
1473 static const struct qlcnic_sriov_fw_cmd_handler qlcnic_pf_fw_cmd_hdlr[] = {
1474 	{QLCNIC_CMD_CREATE_RX_CTX, qlcnic_sriov_pf_create_rx_ctx_cmd},
1475 	{QLCNIC_CMD_CREATE_TX_CTX, qlcnic_sriov_pf_create_tx_ctx_cmd},
1476 	{QLCNIC_CMD_MAC_ADDRESS, qlcnic_sriov_pf_mac_address_cmd},
1477 	{QLCNIC_CMD_DESTROY_RX_CTX, qlcnic_sriov_pf_del_rx_ctx_cmd},
1478 	{QLCNIC_CMD_DESTROY_TX_CTX, qlcnic_sriov_pf_del_tx_ctx_cmd},
1479 	{QLCNIC_CMD_CONFIGURE_HW_LRO, qlcnic_sriov_pf_cfg_lro_cmd},
1480 	{QLCNIC_CMD_CONFIGURE_IP_ADDR, qlcnic_sriov_pf_cfg_ip_cmd},
1481 	{QLCNIC_CMD_CONFIG_INTRPT, qlcnic_sriov_pf_cfg_intrpt_cmd},
1482 	{QLCNIC_CMD_SET_MTU, qlcnic_sriov_pf_set_mtu_cmd},
1483 	{QLCNIC_CMD_GET_NIC_INFO, qlcnic_sriov_pf_get_nic_info_cmd},
1484 	{QLCNIC_CMD_CONFIGURE_RSS, qlcnic_sriov_pf_cfg_rss_cmd},
1485 	{QLCNIC_CMD_CONFIG_INTR_COAL, qlcnic_sriov_pf_cfg_intrcoal_cmd},
1486 	{QLCNIC_CMD_CONFIG_MAC_VLAN, qlcnic_sriov_pf_cfg_macvlan_cmd},
1487 	{QLCNIC_CMD_GET_LINK_EVENT, qlcnic_sriov_pf_linkevent_cmd},
1488 	{QLCNIC_CMD_CONFIGURE_MAC_RX_MODE, qlcnic_sriov_pf_cfg_promisc_cmd},
1489 };
1490 
1491 void qlcnic_sriov_pf_process_bc_cmd(struct qlcnic_adapter *adapter,
1492 				    struct qlcnic_bc_trans *trans,
1493 				    struct qlcnic_cmd_args *cmd)
1494 {
1495 	u8 size, cmd_op;
1496 
1497 	cmd_op = trans->req_hdr->cmd_op;
1498 
1499 	if (trans->req_hdr->op_type == QLC_BC_CMD) {
1500 		size = ARRAY_SIZE(qlcnic_pf_bc_cmd_hdlr);
1501 		if (cmd_op < size) {
1502 			qlcnic_pf_bc_cmd_hdlr[cmd_op].fn(trans, cmd);
1503 			return;
1504 		}
1505 	} else {
1506 		int i;
1507 		size = ARRAY_SIZE(qlcnic_pf_fw_cmd_hdlr);
1508 		for (i = 0; i < size; i++) {
1509 			if (cmd_op == qlcnic_pf_fw_cmd_hdlr[i].cmd) {
1510 				qlcnic_pf_fw_cmd_hdlr[i].fn(trans, cmd);
1511 				return;
1512 			}
1513 		}
1514 
1515 		size = ARRAY_SIZE(qlcnic_pf_passthru_supp_cmds);
1516 		for (i = 0; i < size; i++) {
1517 			if (cmd_op == qlcnic_pf_passthru_supp_cmds[i]) {
1518 				qlcnic_issue_cmd(adapter, cmd);
1519 				return;
1520 			}
1521 		}
1522 	}
1523 
1524 	cmd->rsp.arg[0] |= (0x9 << 25);
1525 }
1526 
1527 void qlcnic_pf_set_interface_id_create_rx_ctx(struct qlcnic_adapter *adapter,
1528 					     u32 *int_id)
1529 {
1530 	u16 vpid;
1531 
1532 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1533 						adapter->ahw->pci_func);
1534 	*int_id |= vpid;
1535 }
1536 
1537 void qlcnic_pf_set_interface_id_del_rx_ctx(struct qlcnic_adapter *adapter,
1538 					   u32 *int_id)
1539 {
1540 	u16 vpid;
1541 
1542 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1543 						adapter->ahw->pci_func);
1544 	*int_id |= vpid << 16;
1545 }
1546 
1547 void qlcnic_pf_set_interface_id_create_tx_ctx(struct qlcnic_adapter *adapter,
1548 					      u32 *int_id)
1549 {
1550 	int vpid;
1551 
1552 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1553 						adapter->ahw->pci_func);
1554 	*int_id |= vpid << 16;
1555 }
1556 
1557 void qlcnic_pf_set_interface_id_del_tx_ctx(struct qlcnic_adapter *adapter,
1558 					   u32 *int_id)
1559 {
1560 	u16 vpid;
1561 
1562 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1563 						adapter->ahw->pci_func);
1564 	*int_id |= vpid << 16;
1565 }
1566 
1567 void qlcnic_pf_set_interface_id_promisc(struct qlcnic_adapter *adapter,
1568 					u32 *int_id)
1569 {
1570 	u16 vpid;
1571 
1572 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1573 						adapter->ahw->pci_func);
1574 	*int_id |= (vpid << 16) | BIT_31;
1575 }
1576 
1577 void qlcnic_pf_set_interface_id_ipaddr(struct qlcnic_adapter *adapter,
1578 				       u32 *int_id)
1579 {
1580 	u16 vpid;
1581 
1582 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1583 						adapter->ahw->pci_func);
1584 	*int_id |= (vpid << 16) | BIT_31;
1585 }
1586 
1587 void qlcnic_pf_set_interface_id_macaddr(struct qlcnic_adapter *adapter,
1588 					u32 *int_id)
1589 {
1590 	u16 vpid;
1591 
1592 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1593 						adapter->ahw->pci_func);
1594 	*int_id |= (vpid << 16) | BIT_31;
1595 }
1596 
1597 static void qlcnic_sriov_del_rx_ctx(struct qlcnic_adapter *adapter,
1598 				    struct qlcnic_vf_info *vf)
1599 {
1600 	struct qlcnic_cmd_args cmd;
1601 	int vpid;
1602 
1603 	if (!vf->rx_ctx_id)
1604 		return;
1605 
1606 	if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_DESTROY_RX_CTX))
1607 		return;
1608 
1609 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func);
1610 	if (vpid >= 0) {
1611 		cmd.req.arg[1] = vf->rx_ctx_id | (vpid & 0xffff) << 16;
1612 		if (qlcnic_issue_cmd(adapter, &cmd))
1613 			dev_err(&adapter->pdev->dev,
1614 				"Failed to delete Tx ctx in firmware for func 0x%x\n",
1615 				vf->pci_func);
1616 		else
1617 			vf->rx_ctx_id = 0;
1618 	}
1619 
1620 	qlcnic_free_mbx_args(&cmd);
1621 }
1622 
1623 static void qlcnic_sriov_del_tx_ctx(struct qlcnic_adapter *adapter,
1624 				    struct qlcnic_vf_info *vf)
1625 {
1626 	struct qlcnic_cmd_args cmd;
1627 	int vpid;
1628 
1629 	if (!vf->tx_ctx_id)
1630 		return;
1631 
1632 	if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_DESTROY_TX_CTX))
1633 		return;
1634 
1635 	vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func);
1636 	if (vpid >= 0) {
1637 		cmd.req.arg[1] |= vf->tx_ctx_id | (vpid & 0xffff) << 16;
1638 		if (qlcnic_issue_cmd(adapter, &cmd))
1639 			dev_err(&adapter->pdev->dev,
1640 				"Failed to delete Tx ctx in firmware for func 0x%x\n",
1641 				vf->pci_func);
1642 		else
1643 			vf->tx_ctx_id = 0;
1644 	}
1645 
1646 	qlcnic_free_mbx_args(&cmd);
1647 }
1648 
1649 static int qlcnic_sriov_add_act_list_irqsave(struct qlcnic_sriov *sriov,
1650 					     struct qlcnic_vf_info *vf,
1651 					     struct qlcnic_bc_trans *trans)
1652 {
1653 	struct qlcnic_trans_list *t_list = &vf->rcv_act;
1654 	unsigned long flag;
1655 
1656 	spin_lock_irqsave(&t_list->lock, flag);
1657 
1658 	__qlcnic_sriov_add_act_list(sriov, vf, trans);
1659 
1660 	spin_unlock_irqrestore(&t_list->lock, flag);
1661 	return 0;
1662 }
1663 
1664 static void __qlcnic_sriov_process_flr(struct qlcnic_vf_info *vf)
1665 {
1666 	struct qlcnic_adapter *adapter = vf->adapter;
1667 
1668 	qlcnic_sriov_cleanup_list(&vf->rcv_pend);
1669 	cancel_work_sync(&vf->trans_work);
1670 	qlcnic_sriov_cleanup_list(&vf->rcv_act);
1671 
1672 	if (test_bit(QLC_BC_VF_SOFT_FLR, &vf->state)) {
1673 		qlcnic_sriov_del_tx_ctx(adapter, vf);
1674 		qlcnic_sriov_del_rx_ctx(adapter, vf);
1675 	}
1676 
1677 	qlcnic_sriov_pf_config_vport(adapter, 0, vf->pci_func);
1678 
1679 	clear_bit(QLC_BC_VF_FLR, &vf->state);
1680 	if (test_bit(QLC_BC_VF_SOFT_FLR, &vf->state)) {
1681 		qlcnic_sriov_add_act_list_irqsave(adapter->ahw->sriov, vf,
1682 						  vf->flr_trans);
1683 		clear_bit(QLC_BC_VF_SOFT_FLR, &vf->state);
1684 		vf->flr_trans = NULL;
1685 	}
1686 }
1687 
1688 static void qlcnic_sriov_pf_process_flr(struct work_struct *work)
1689 {
1690 	struct qlcnic_vf_info *vf;
1691 
1692 	vf = container_of(work, struct qlcnic_vf_info, flr_work);
1693 	__qlcnic_sriov_process_flr(vf);
1694 	return;
1695 }
1696 
1697 static void qlcnic_sriov_schedule_flr(struct qlcnic_sriov *sriov,
1698 				      struct qlcnic_vf_info *vf,
1699 				      work_func_t func)
1700 {
1701 	if (test_bit(__QLCNIC_RESETTING, &vf->adapter->state))
1702 		return;
1703 
1704 	INIT_WORK(&vf->flr_work, func);
1705 	queue_work(sriov->bc.bc_flr_wq, &vf->flr_work);
1706 }
1707 
1708 static void qlcnic_sriov_handle_soft_flr(struct qlcnic_adapter *adapter,
1709 					 struct qlcnic_bc_trans *trans,
1710 					 struct qlcnic_vf_info *vf)
1711 {
1712 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1713 
1714 	set_bit(QLC_BC_VF_FLR, &vf->state);
1715 	clear_bit(QLC_BC_VF_STATE, &vf->state);
1716 	set_bit(QLC_BC_VF_SOFT_FLR, &vf->state);
1717 	vf->flr_trans = trans;
1718 	qlcnic_sriov_schedule_flr(sriov, vf, qlcnic_sriov_pf_process_flr);
1719 	netdev_info(adapter->netdev, "Software FLR for PCI func %d\n",
1720 		    vf->pci_func);
1721 }
1722 
1723 bool qlcnic_sriov_soft_flr_check(struct qlcnic_adapter *adapter,
1724 				 struct qlcnic_bc_trans *trans,
1725 				 struct qlcnic_vf_info *vf)
1726 {
1727 	struct qlcnic_bc_hdr *hdr = trans->req_hdr;
1728 
1729 	if ((hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) &&
1730 	    (hdr->op_type == QLC_BC_CMD) &&
1731 	     test_bit(QLC_BC_VF_STATE, &vf->state)) {
1732 		qlcnic_sriov_handle_soft_flr(adapter, trans, vf);
1733 		return true;
1734 	}
1735 
1736 	return false;
1737 }
1738 
1739 void qlcnic_sriov_pf_handle_flr(struct qlcnic_sriov *sriov,
1740 				struct qlcnic_vf_info *vf)
1741 {
1742 	struct net_device *dev = vf->adapter->netdev;
1743 	struct qlcnic_vport *vp = vf->vp;
1744 
1745 	if (!test_and_clear_bit(QLC_BC_VF_STATE, &vf->state)) {
1746 		clear_bit(QLC_BC_VF_FLR, &vf->state);
1747 		return;
1748 	}
1749 
1750 	if (test_and_set_bit(QLC_BC_VF_FLR, &vf->state)) {
1751 		netdev_info(dev, "FLR for PCI func %d in progress\n",
1752 			    vf->pci_func);
1753 		return;
1754 	}
1755 
1756 	if (vp->vlan_mode == QLC_GUEST_VLAN_MODE)
1757 		memset(vf->sriov_vlans, 0,
1758 		       sizeof(*vf->sriov_vlans) * sriov->num_allowed_vlans);
1759 
1760 	qlcnic_sriov_schedule_flr(sriov, vf, qlcnic_sriov_pf_process_flr);
1761 	netdev_info(dev, "FLR received for PCI func %d\n", vf->pci_func);
1762 }
1763 
1764 void qlcnic_sriov_pf_reset(struct qlcnic_adapter *adapter)
1765 {
1766 	struct qlcnic_hardware_context *ahw = adapter->ahw;
1767 	struct qlcnic_sriov *sriov = ahw->sriov;
1768 	struct qlcnic_vf_info *vf;
1769 	u16 num_vfs = sriov->num_vfs;
1770 	int i;
1771 
1772 	for (i = 0; i < num_vfs; i++) {
1773 		vf = &sriov->vf_info[i];
1774 		vf->rx_ctx_id = 0;
1775 		vf->tx_ctx_id = 0;
1776 		cancel_work_sync(&vf->flr_work);
1777 		__qlcnic_sriov_process_flr(vf);
1778 		clear_bit(QLC_BC_VF_STATE, &vf->state);
1779 	}
1780 
1781 	qlcnic_sriov_pf_reset_vport_handle(adapter, ahw->pci_func);
1782 	QLCWRX(ahw, QLCNIC_MBX_INTR_ENBL, (ahw->num_msix - 1) << 8);
1783 }
1784 
1785 int qlcnic_sriov_pf_reinit(struct qlcnic_adapter *adapter)
1786 {
1787 	struct qlcnic_hardware_context *ahw = adapter->ahw;
1788 	int err;
1789 
1790 	if (!qlcnic_sriov_enable_check(adapter))
1791 		return 0;
1792 
1793 	ahw->op_mode = QLCNIC_SRIOV_PF_FUNC;
1794 
1795 	err = qlcnic_sriov_pf_init(adapter);
1796 	if (err)
1797 		return err;
1798 
1799 	dev_info(&adapter->pdev->dev, "%s: op_mode %d\n",
1800 		 __func__, ahw->op_mode);
1801 	return err;
1802 }
1803 
1804 int qlcnic_sriov_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1805 {
1806 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1807 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1808 	int i, num_vfs;
1809 	struct qlcnic_vf_info *vf_info;
1810 	u8 *curr_mac;
1811 
1812 	if (!qlcnic_sriov_pf_check(adapter))
1813 		return -EOPNOTSUPP;
1814 
1815 	num_vfs = sriov->num_vfs;
1816 
1817 	if (!is_valid_ether_addr(mac) || vf >= num_vfs)
1818 		return -EINVAL;
1819 
1820 	if (ether_addr_equal(adapter->mac_addr, mac)) {
1821 		netdev_err(netdev, "MAC address is already in use by the PF\n");
1822 		return -EINVAL;
1823 	}
1824 
1825 	for (i = 0; i < num_vfs; i++) {
1826 		vf_info = &sriov->vf_info[i];
1827 		if (ether_addr_equal(vf_info->vp->mac, mac)) {
1828 			netdev_err(netdev,
1829 				   "MAC address is already in use by VF %d\n",
1830 				   i);
1831 			return -EINVAL;
1832 		}
1833 	}
1834 
1835 	vf_info = &sriov->vf_info[vf];
1836 	curr_mac = vf_info->vp->mac;
1837 
1838 	if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
1839 		netdev_err(netdev,
1840 			   "MAC address change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n",
1841 			   vf);
1842 		return -EOPNOTSUPP;
1843 	}
1844 
1845 	memcpy(curr_mac, mac, netdev->addr_len);
1846 	netdev_info(netdev, "MAC Address %pM  is configured for VF %d\n",
1847 		    mac, vf);
1848 	return 0;
1849 }
1850 
1851 int qlcnic_sriov_set_vf_tx_rate(struct net_device *netdev, int vf,
1852 				int min_tx_rate, int max_tx_rate)
1853 {
1854 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1855 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1856 	struct qlcnic_vf_info *vf_info;
1857 	struct qlcnic_info nic_info;
1858 	struct qlcnic_vport *vp;
1859 	u16 vpid;
1860 
1861 	if (!qlcnic_sriov_pf_check(adapter))
1862 		return -EOPNOTSUPP;
1863 
1864 	if (vf >= sriov->num_vfs)
1865 		return -EINVAL;
1866 
1867 	vf_info = &sriov->vf_info[vf];
1868 	vp = vf_info->vp;
1869 	vpid = vp->handle;
1870 
1871 	if (!min_tx_rate)
1872 		min_tx_rate = QLC_VF_MIN_TX_RATE;
1873 
1874 	if (max_tx_rate &&
1875 	    (max_tx_rate >= 10000 || max_tx_rate < min_tx_rate)) {
1876 		netdev_err(netdev,
1877 			   "Invalid max Tx rate, allowed range is [%d - %d]",
1878 			   min_tx_rate, QLC_VF_MAX_TX_RATE);
1879 		return -EINVAL;
1880 	}
1881 
1882 	if (!max_tx_rate)
1883 		max_tx_rate = 10000;
1884 
1885 	if (min_tx_rate &&
1886 	    (min_tx_rate > max_tx_rate || min_tx_rate < QLC_VF_MIN_TX_RATE)) {
1887 		netdev_err(netdev,
1888 			   "Invalid min Tx rate, allowed range is [%d - %d]",
1889 			   QLC_VF_MIN_TX_RATE, max_tx_rate);
1890 		return -EINVAL;
1891 	}
1892 
1893 	if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
1894 		if (qlcnic_sriov_get_vf_vport_info(adapter, &nic_info, vpid))
1895 			return -EIO;
1896 
1897 		nic_info.max_tx_bw = max_tx_rate / 100;
1898 		nic_info.min_tx_bw = min_tx_rate / 100;
1899 		nic_info.bit_offsets = BIT_0;
1900 
1901 		if (qlcnic_sriov_pf_set_vport_info(adapter, &nic_info, vpid))
1902 			return -EIO;
1903 	}
1904 
1905 	vp->max_tx_bw = max_tx_rate / 100;
1906 	netdev_info(netdev,
1907 		    "Setting Max Tx rate %d (Mbps), %d %% of PF bandwidth, for VF %d\n",
1908 		    max_tx_rate, vp->max_tx_bw, vf);
1909 	vp->min_tx_bw = min_tx_rate / 100;
1910 	netdev_info(netdev,
1911 		    "Setting Min Tx rate %d (Mbps), %d %% of PF bandwidth, for VF %d\n",
1912 		    min_tx_rate, vp->min_tx_bw, vf);
1913 	return 0;
1914 }
1915 
1916 int qlcnic_sriov_set_vf_vlan(struct net_device *netdev, int vf,
1917 			     u16 vlan, u8 qos)
1918 {
1919 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1920 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1921 	struct qlcnic_vf_info *vf_info;
1922 	struct qlcnic_vport *vp;
1923 
1924 	if (!qlcnic_sriov_pf_check(adapter))
1925 		return -EOPNOTSUPP;
1926 
1927 	if (vf >= sriov->num_vfs || qos > 7)
1928 		return -EINVAL;
1929 
1930 	if (vlan > MAX_VLAN_ID) {
1931 		netdev_err(netdev,
1932 			   "Invalid VLAN ID, allowed range is [0 - %d]\n",
1933 			   MAX_VLAN_ID);
1934 		return -EINVAL;
1935 	}
1936 
1937 	vf_info = &sriov->vf_info[vf];
1938 	vp = vf_info->vp;
1939 	if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
1940 		netdev_err(netdev,
1941 			   "VLAN change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n",
1942 			   vf);
1943 		return -EOPNOTSUPP;
1944 	}
1945 
1946 	memset(vf_info->sriov_vlans, 0,
1947 	       sizeof(*vf_info->sriov_vlans) * sriov->num_allowed_vlans);
1948 
1949 	switch (vlan) {
1950 	case 4095:
1951 		vp->vlan_mode = QLC_GUEST_VLAN_MODE;
1952 		break;
1953 	case 0:
1954 		vp->vlan_mode = QLC_NO_VLAN_MODE;
1955 		vp->qos = 0;
1956 		break;
1957 	default:
1958 		vp->vlan_mode = QLC_PVID_MODE;
1959 		qlcnic_sriov_add_vlan_id(sriov, vf_info, vlan);
1960 		vp->qos = qos;
1961 		vp->pvid = vlan;
1962 	}
1963 
1964 	netdev_info(netdev, "Setting VLAN %d, QoS %d, for VF %d\n",
1965 		    vlan, qos, vf);
1966 	return 0;
1967 }
1968 
1969 static __u32 qlcnic_sriov_get_vf_vlan(struct qlcnic_adapter *adapter,
1970 				      struct qlcnic_vport *vp, int vf)
1971 {
1972 	__u32 vlan = 0;
1973 
1974 	switch (vp->vlan_mode) {
1975 	case QLC_PVID_MODE:
1976 		vlan = vp->pvid;
1977 		break;
1978 	case QLC_GUEST_VLAN_MODE:
1979 		vlan = MAX_VLAN_ID;
1980 		break;
1981 	case QLC_NO_VLAN_MODE:
1982 		vlan = 0;
1983 		break;
1984 	default:
1985 		netdev_info(adapter->netdev, "Invalid VLAN mode = %d for VF %d\n",
1986 			    vp->vlan_mode, vf);
1987 	}
1988 
1989 	return vlan;
1990 }
1991 
1992 int qlcnic_sriov_get_vf_config(struct net_device *netdev,
1993 			       int vf, struct ifla_vf_info *ivi)
1994 {
1995 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
1996 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1997 	struct qlcnic_vport *vp;
1998 
1999 	if (!qlcnic_sriov_pf_check(adapter))
2000 		return -EOPNOTSUPP;
2001 
2002 	if (vf >= sriov->num_vfs)
2003 		return -EINVAL;
2004 
2005 	vp = sriov->vf_info[vf].vp;
2006 	memcpy(&ivi->mac, vp->mac, ETH_ALEN);
2007 	ivi->vlan = qlcnic_sriov_get_vf_vlan(adapter, vp, vf);
2008 	ivi->qos = vp->qos;
2009 	ivi->spoofchk = vp->spoofchk;
2010 	if (vp->max_tx_bw == MAX_BW)
2011 		ivi->max_tx_rate = 0;
2012 	else
2013 		ivi->max_tx_rate = vp->max_tx_bw * 100;
2014 	if (vp->min_tx_bw == MIN_BW)
2015 		ivi->min_tx_rate = 0;
2016 	else
2017 		ivi->min_tx_rate = vp->min_tx_bw * 100;
2018 
2019 	ivi->vf = vf;
2020 	return 0;
2021 }
2022 
2023 int qlcnic_sriov_set_vf_spoofchk(struct net_device *netdev, int vf, bool chk)
2024 {
2025 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
2026 	struct qlcnic_sriov *sriov = adapter->ahw->sriov;
2027 	struct qlcnic_vf_info *vf_info;
2028 	struct qlcnic_vport *vp;
2029 
2030 	if (!qlcnic_sriov_pf_check(adapter))
2031 		return -EOPNOTSUPP;
2032 
2033 	if (vf >= sriov->num_vfs)
2034 		return -EINVAL;
2035 
2036 	vf_info = &sriov->vf_info[vf];
2037 	vp = vf_info->vp;
2038 	if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
2039 		netdev_err(netdev,
2040 			   "Spoof check change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n",
2041 			   vf);
2042 		return -EOPNOTSUPP;
2043 	}
2044 
2045 	vp->spoofchk = chk;
2046 	return 0;
2047 }
2048