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