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