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