Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / ethernet / qlogic / qlcnic / qlcnic_sriov_pf.c
blob50eaafa3eaba362d0ed9b35821234dcf2551e03f
1 /*
2 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2013 QLogic Corporation
5 * See LICENSE.qlcnic for copyright and licensing details.
6 */
8 #include <linux/types.h>
10 #include "qlcnic_sriov.h"
11 #include "qlcnic.h"
13 #define QLCNIC_SRIOV_VF_MAX_MAC 7
14 #define QLC_VF_MIN_TX_RATE 100
15 #define QLC_VF_MAX_TX_RATE 9999
16 #define QLC_MAC_OPCODE_MASK 0x7
17 #define QLC_VF_FLOOD_BIT BIT_16
18 #define QLC_FLOOD_MODE 0x5
19 #define QLC_SRIOV_ALLOW_VLAN0 BIT_19
20 #define QLC_INTR_COAL_TYPE_MASK 0x7
22 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *, u8);
24 struct qlcnic_sriov_cmd_handler {
25 int (*fn) (struct qlcnic_bc_trans *, struct qlcnic_cmd_args *);
28 struct qlcnic_sriov_fw_cmd_handler {
29 u32 cmd;
30 int (*fn) (struct qlcnic_bc_trans *, struct qlcnic_cmd_args *);
33 static int qlcnic_sriov_pf_set_vport_info(struct qlcnic_adapter *adapter,
34 struct qlcnic_info *npar_info,
35 u16 vport_id)
37 struct qlcnic_cmd_args cmd;
38 int err;
40 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO))
41 return -ENOMEM;
43 cmd.req.arg[1] = (vport_id << 16) | 0x1;
44 cmd.req.arg[2] = npar_info->bit_offsets;
45 cmd.req.arg[2] |= npar_info->min_tx_bw << 16;
46 cmd.req.arg[3] = npar_info->max_tx_bw | (npar_info->max_tx_ques << 16);
47 cmd.req.arg[4] = npar_info->max_tx_mac_filters;
48 cmd.req.arg[4] |= npar_info->max_rx_mcast_mac_filters << 16;
49 cmd.req.arg[5] = npar_info->max_rx_ucast_mac_filters |
50 (npar_info->max_rx_ip_addr << 16);
51 cmd.req.arg[6] = npar_info->max_rx_lro_flow |
52 (npar_info->max_rx_status_rings << 16);
53 cmd.req.arg[7] = npar_info->max_rx_buf_rings |
54 (npar_info->max_rx_ques << 16);
55 cmd.req.arg[8] = npar_info->max_tx_vlan_keys;
56 cmd.req.arg[8] |= npar_info->max_local_ipv6_addrs << 16;
57 cmd.req.arg[9] = npar_info->max_remote_ipv6_addrs;
59 err = qlcnic_issue_cmd(adapter, &cmd);
60 if (err)
61 dev_err(&adapter->pdev->dev,
62 "Failed to set vport info, err=%d\n", err);
64 qlcnic_free_mbx_args(&cmd);
65 return err;
68 static int qlcnic_sriov_pf_cal_res_limit(struct qlcnic_adapter *adapter,
69 struct qlcnic_info *info, u16 func)
71 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
72 struct qlcnic_resources *res = &sriov->ff_max;
73 u16 num_macs = sriov->num_allowed_vlans + 1;
74 int ret = -EIO, vpid, id;
75 struct qlcnic_vport *vp;
76 u32 num_vfs, max, temp;
78 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func);
79 if (vpid < 0)
80 return -EINVAL;
82 num_vfs = sriov->num_vfs;
83 max = num_vfs + 1;
84 info->bit_offsets = 0xffff;
85 info->max_tx_ques = res->num_tx_queues / max;
87 if (qlcnic_83xx_pf_check(adapter))
88 num_macs = QLCNIC_83XX_SRIOV_VF_MAX_MAC;
90 info->max_rx_mcast_mac_filters = res->num_rx_mcast_mac_filters;
92 if (adapter->ahw->pci_func == func) {
93 info->min_tx_bw = 0;
94 info->max_tx_bw = MAX_BW;
96 temp = res->num_rx_ucast_mac_filters - num_macs * num_vfs;
97 info->max_rx_ucast_mac_filters = temp;
98 temp = res->num_tx_mac_filters - num_macs * num_vfs;
99 info->max_tx_mac_filters = temp;
100 temp = num_macs * num_vfs * QLCNIC_SRIOV_VF_MAX_MAC;
101 temp = res->num_rx_mcast_mac_filters - temp;
102 info->max_rx_mcast_mac_filters = temp;
104 info->max_tx_ques = res->num_tx_queues - sriov->num_vfs;
105 } else {
106 id = qlcnic_sriov_func_to_index(adapter, func);
107 if (id < 0)
108 return id;
109 vp = sriov->vf_info[id].vp;
110 info->min_tx_bw = vp->min_tx_bw;
111 info->max_tx_bw = vp->max_tx_bw;
113 info->max_rx_ucast_mac_filters = num_macs;
114 info->max_tx_mac_filters = num_macs;
115 temp = num_macs * QLCNIC_SRIOV_VF_MAX_MAC;
116 info->max_rx_mcast_mac_filters = temp;
118 info->max_tx_ques = QLCNIC_SINGLE_RING;
121 info->max_rx_ip_addr = res->num_destip / max;
122 info->max_rx_status_rings = res->num_rx_status_rings / max;
123 info->max_rx_buf_rings = res->num_rx_buf_rings / max;
124 info->max_rx_ques = res->num_rx_queues / max;
125 info->max_rx_lro_flow = res->num_lro_flows_supported / max;
126 info->max_tx_vlan_keys = res->num_txvlan_keys;
127 info->max_local_ipv6_addrs = res->max_local_ipv6_addrs;
128 info->max_remote_ipv6_addrs = res->max_remote_ipv6_addrs;
130 ret = qlcnic_sriov_pf_set_vport_info(adapter, info, vpid);
131 if (ret)
132 return ret;
134 return 0;
137 static void qlcnic_sriov_pf_set_ff_max_res(struct qlcnic_adapter *adapter,
138 struct qlcnic_info *info)
140 struct qlcnic_resources *ff_max = &adapter->ahw->sriov->ff_max;
142 ff_max->num_tx_mac_filters = info->max_tx_mac_filters;
143 ff_max->num_rx_ucast_mac_filters = info->max_rx_ucast_mac_filters;
144 ff_max->num_rx_mcast_mac_filters = info->max_rx_mcast_mac_filters;
145 ff_max->num_txvlan_keys = info->max_tx_vlan_keys;
146 ff_max->num_rx_queues = info->max_rx_ques;
147 ff_max->num_tx_queues = info->max_tx_ques;
148 ff_max->num_lro_flows_supported = info->max_rx_lro_flow;
149 ff_max->num_destip = info->max_rx_ip_addr;
150 ff_max->num_rx_buf_rings = info->max_rx_buf_rings;
151 ff_max->num_rx_status_rings = info->max_rx_status_rings;
152 ff_max->max_remote_ipv6_addrs = info->max_remote_ipv6_addrs;
153 ff_max->max_local_ipv6_addrs = info->max_local_ipv6_addrs;
156 static void qlcnic_sriov_set_vf_max_vlan(struct qlcnic_adapter *adapter,
157 struct qlcnic_info *npar_info)
159 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
160 int temp, total_fn;
162 temp = npar_info->max_rx_mcast_mac_filters;
163 total_fn = sriov->num_vfs + 1;
165 temp = temp / (QLCNIC_SRIOV_VF_MAX_MAC * total_fn);
166 sriov->num_allowed_vlans = temp - 1;
168 if (qlcnic_83xx_pf_check(adapter))
169 sriov->num_allowed_vlans = 1;
171 netdev_info(adapter->netdev, "Max Guest VLANs supported per VF = %d\n",
172 sriov->num_allowed_vlans);
175 static int qlcnic_sriov_get_pf_info(struct qlcnic_adapter *adapter,
176 struct qlcnic_info *npar_info)
178 int err;
179 struct qlcnic_cmd_args cmd;
181 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_GET_NIC_INFO))
182 return -ENOMEM;
184 cmd.req.arg[1] = 0x2;
185 err = qlcnic_issue_cmd(adapter, &cmd);
186 if (err) {
187 dev_err(&adapter->pdev->dev,
188 "Failed to get PF info, err=%d\n", err);
189 goto out;
192 npar_info->total_pf = cmd.rsp.arg[2] & 0xff;
193 npar_info->total_rss_engines = (cmd.rsp.arg[2] >> 8) & 0xff;
194 npar_info->max_vports = MSW(cmd.rsp.arg[2]);
195 npar_info->max_tx_ques = LSW(cmd.rsp.arg[3]);
196 npar_info->max_tx_mac_filters = MSW(cmd.rsp.arg[3]);
197 npar_info->max_rx_mcast_mac_filters = LSW(cmd.rsp.arg[4]);
198 npar_info->max_rx_ucast_mac_filters = MSW(cmd.rsp.arg[4]);
199 npar_info->max_rx_ip_addr = LSW(cmd.rsp.arg[5]);
200 npar_info->max_rx_lro_flow = MSW(cmd.rsp.arg[5]);
201 npar_info->max_rx_status_rings = LSW(cmd.rsp.arg[6]);
202 npar_info->max_rx_buf_rings = MSW(cmd.rsp.arg[6]);
203 npar_info->max_rx_ques = LSW(cmd.rsp.arg[7]);
204 npar_info->max_tx_vlan_keys = MSW(cmd.rsp.arg[7]);
205 npar_info->max_local_ipv6_addrs = LSW(cmd.rsp.arg[8]);
206 npar_info->max_remote_ipv6_addrs = MSW(cmd.rsp.arg[8]);
208 qlcnic_sriov_set_vf_max_vlan(adapter, npar_info);
209 qlcnic_sriov_pf_set_ff_max_res(adapter, npar_info);
210 dev_info(&adapter->pdev->dev,
211 "\n\ttotal_pf: %d,\n"
212 "\n\ttotal_rss_engines: %d max_vports: %d max_tx_ques %d,\n"
213 "\tmax_tx_mac_filters: %d max_rx_mcast_mac_filters: %d,\n"
214 "\tmax_rx_ucast_mac_filters: 0x%x, max_rx_ip_addr: %d,\n"
215 "\tmax_rx_lro_flow: %d max_rx_status_rings: %d,\n"
216 "\tmax_rx_buf_rings: %d, max_rx_ques: %d, max_tx_vlan_keys %d\n"
217 "\tmax_local_ipv6_addrs: %d, max_remote_ipv6_addrs: %d\n",
218 npar_info->total_pf, npar_info->total_rss_engines,
219 npar_info->max_vports, npar_info->max_tx_ques,
220 npar_info->max_tx_mac_filters,
221 npar_info->max_rx_mcast_mac_filters,
222 npar_info->max_rx_ucast_mac_filters, npar_info->max_rx_ip_addr,
223 npar_info->max_rx_lro_flow, npar_info->max_rx_status_rings,
224 npar_info->max_rx_buf_rings, npar_info->max_rx_ques,
225 npar_info->max_tx_vlan_keys, npar_info->max_local_ipv6_addrs,
226 npar_info->max_remote_ipv6_addrs);
228 out:
229 qlcnic_free_mbx_args(&cmd);
230 return err;
233 static void qlcnic_sriov_pf_reset_vport_handle(struct qlcnic_adapter *adapter,
234 u8 func)
236 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
237 struct qlcnic_vport *vp;
238 int index;
240 if (adapter->ahw->pci_func == func) {
241 sriov->vp_handle = 0;
242 } else {
243 index = qlcnic_sriov_func_to_index(adapter, func);
244 if (index < 0)
245 return;
246 vp = sriov->vf_info[index].vp;
247 vp->handle = 0;
251 static void qlcnic_sriov_pf_set_vport_handle(struct qlcnic_adapter *adapter,
252 u16 vport_handle, u8 func)
254 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
255 struct qlcnic_vport *vp;
256 int index;
258 if (adapter->ahw->pci_func == func) {
259 sriov->vp_handle = vport_handle;
260 } else {
261 index = qlcnic_sriov_func_to_index(adapter, func);
262 if (index < 0)
263 return;
264 vp = sriov->vf_info[index].vp;
265 vp->handle = vport_handle;
269 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *adapter,
270 u8 func)
272 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
273 struct qlcnic_vf_info *vf_info;
274 int index;
276 if (adapter->ahw->pci_func == func) {
277 return sriov->vp_handle;
278 } else {
279 index = qlcnic_sriov_func_to_index(adapter, func);
280 if (index >= 0) {
281 vf_info = &sriov->vf_info[index];
282 return vf_info->vp->handle;
286 return -EINVAL;
289 static int qlcnic_sriov_pf_config_vport(struct qlcnic_adapter *adapter,
290 u8 flag, u16 func)
292 struct qlcnic_cmd_args cmd;
293 int ret;
294 int vpid;
296 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_CONFIG_VPORT))
297 return -ENOMEM;
299 if (flag) {
300 cmd.req.arg[3] = func << 8;
301 } else {
302 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func);
303 if (vpid < 0) {
304 ret = -EINVAL;
305 goto out;
307 cmd.req.arg[3] = ((vpid & 0xffff) << 8) | 1;
310 ret = qlcnic_issue_cmd(adapter, &cmd);
311 if (ret) {
312 dev_err(&adapter->pdev->dev,
313 "Failed %s vport, err %d for func 0x%x\n",
314 (flag ? "enable" : "disable"), ret, func);
315 goto out;
318 if (flag) {
319 vpid = cmd.rsp.arg[2] & 0xffff;
320 qlcnic_sriov_pf_set_vport_handle(adapter, vpid, func);
321 } else {
322 qlcnic_sriov_pf_reset_vport_handle(adapter, func);
325 out:
326 qlcnic_free_mbx_args(&cmd);
327 return ret;
330 static int qlcnic_sriov_pf_cfg_vlan_filtering(struct qlcnic_adapter *adapter,
331 u8 enable)
333 struct qlcnic_cmd_args cmd;
334 int err;
336 err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO);
337 if (err)
338 return err;
340 cmd.req.arg[1] = 0x4;
341 if (enable) {
342 adapter->flags |= QLCNIC_VLAN_FILTERING;
343 cmd.req.arg[1] |= BIT_16;
344 if (qlcnic_84xx_check(adapter))
345 cmd.req.arg[1] |= QLC_SRIOV_ALLOW_VLAN0;
346 } else {
347 adapter->flags &= ~QLCNIC_VLAN_FILTERING;
350 err = qlcnic_issue_cmd(adapter, &cmd);
351 if (err)
352 dev_err(&adapter->pdev->dev,
353 "Failed to configure VLAN filtering, err=%d\n", err);
355 qlcnic_free_mbx_args(&cmd);
356 return err;
359 /* On configuring VF flood bit, PFD will receive traffic from all VFs */
360 static int qlcnic_sriov_pf_cfg_flood(struct qlcnic_adapter *adapter)
362 struct qlcnic_cmd_args cmd;
363 int err;
365 err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO);
366 if (err)
367 return err;
369 cmd.req.arg[1] = QLC_FLOOD_MODE | QLC_VF_FLOOD_BIT;
371 err = qlcnic_issue_cmd(adapter, &cmd);
372 if (err)
373 dev_err(&adapter->pdev->dev,
374 "Failed to configure VF Flood bit on PF, err=%d\n",
375 err);
377 qlcnic_free_mbx_args(&cmd);
378 return err;
381 static int qlcnic_sriov_pf_cfg_eswitch(struct qlcnic_adapter *adapter,
382 u8 func, u8 enable)
384 struct qlcnic_cmd_args cmd;
385 int err = -EIO;
387 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_TOGGLE_ESWITCH))
388 return -ENOMEM;
390 cmd.req.arg[0] |= (3 << 29);
391 cmd.req.arg[1] = ((func & 0xf) << 2) | BIT_6 | BIT_1;
392 if (enable)
393 cmd.req.arg[1] |= BIT_0;
395 err = qlcnic_issue_cmd(adapter, &cmd);
397 if (err != QLCNIC_RCODE_SUCCESS) {
398 dev_err(&adapter->pdev->dev,
399 "Failed to enable sriov eswitch%d\n", err);
400 err = -EIO;
403 qlcnic_free_mbx_args(&cmd);
404 return err;
407 static void qlcnic_sriov_pf_del_flr_queue(struct qlcnic_adapter *adapter)
409 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
410 struct qlcnic_back_channel *bc = &sriov->bc;
411 int i;
413 for (i = 0; i < sriov->num_vfs; i++)
414 cancel_work_sync(&sriov->vf_info[i].flr_work);
416 destroy_workqueue(bc->bc_flr_wq);
419 static int qlcnic_sriov_pf_create_flr_queue(struct qlcnic_adapter *adapter)
421 struct qlcnic_back_channel *bc = &adapter->ahw->sriov->bc;
422 struct workqueue_struct *wq;
424 wq = create_singlethread_workqueue("qlcnic-flr");
425 if (wq == NULL) {
426 dev_err(&adapter->pdev->dev, "Cannot create FLR workqueue\n");
427 return -ENOMEM;
430 bc->bc_flr_wq = wq;
431 return 0;
434 void qlcnic_sriov_pf_cleanup(struct qlcnic_adapter *adapter)
436 u8 func = adapter->ahw->pci_func;
438 if (!qlcnic_sriov_enable_check(adapter))
439 return;
441 qlcnic_sriov_pf_del_flr_queue(adapter);
442 qlcnic_sriov_cfg_bc_intr(adapter, 0);
443 qlcnic_sriov_pf_config_vport(adapter, 0, func);
444 qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0);
445 qlcnic_sriov_pf_cfg_vlan_filtering(adapter, 0);
446 __qlcnic_sriov_cleanup(adapter);
447 adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
448 clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
451 void qlcnic_sriov_pf_disable(struct qlcnic_adapter *adapter)
453 if (!qlcnic_sriov_pf_check(adapter))
454 return;
456 if (!qlcnic_sriov_enable_check(adapter))
457 return;
459 pci_disable_sriov(adapter->pdev);
460 netdev_info(adapter->netdev,
461 "SR-IOV is disabled successfully on port %d\n",
462 adapter->portnum);
465 static int qlcnic_pci_sriov_disable(struct qlcnic_adapter *adapter)
467 struct net_device *netdev = adapter->netdev;
469 if (pci_vfs_assigned(adapter->pdev)) {
470 netdev_err(adapter->netdev,
471 "SR-IOV VFs belonging to port %d are assigned to VMs. SR-IOV can not be disabled on this port\n",
472 adapter->portnum);
473 netdev_info(adapter->netdev,
474 "Please detach SR-IOV VFs belonging to port %d from VMs, and then try to disable SR-IOV on this port\n",
475 adapter->portnum);
476 return -EPERM;
479 qlcnic_sriov_pf_disable(adapter);
481 rtnl_lock();
482 if (netif_running(netdev))
483 __qlcnic_down(adapter, netdev);
485 qlcnic_sriov_free_vlans(adapter);
487 qlcnic_sriov_pf_cleanup(adapter);
489 /* After disabling SRIOV re-init the driver in default mode
490 configure opmode based on op_mode of function
492 if (qlcnic_83xx_configure_opmode(adapter)) {
493 rtnl_unlock();
494 return -EIO;
497 if (netif_running(netdev))
498 __qlcnic_up(adapter, netdev);
500 rtnl_unlock();
501 return 0;
504 static int qlcnic_sriov_pf_init(struct qlcnic_adapter *adapter)
506 struct qlcnic_hardware_context *ahw = adapter->ahw;
507 struct qlcnic_info nic_info, pf_info, vp_info;
508 int err;
509 u8 func = ahw->pci_func;
511 if (!qlcnic_sriov_enable_check(adapter))
512 return 0;
514 err = qlcnic_sriov_pf_cfg_vlan_filtering(adapter, 1);
515 if (err)
516 return err;
518 if (qlcnic_84xx_check(adapter)) {
519 err = qlcnic_sriov_pf_cfg_flood(adapter);
520 if (err)
521 goto disable_vlan_filtering;
524 err = qlcnic_sriov_pf_cfg_eswitch(adapter, func, 1);
525 if (err)
526 goto disable_vlan_filtering;
528 err = qlcnic_sriov_pf_config_vport(adapter, 1, func);
529 if (err)
530 goto disable_eswitch;
532 err = qlcnic_sriov_get_pf_info(adapter, &pf_info);
533 if (err)
534 goto delete_vport;
536 err = qlcnic_get_nic_info(adapter, &nic_info, func);
537 if (err)
538 goto delete_vport;
540 err = qlcnic_sriov_pf_cal_res_limit(adapter, &vp_info, func);
541 if (err)
542 goto delete_vport;
544 err = qlcnic_sriov_cfg_bc_intr(adapter, 1);
545 if (err)
546 goto delete_vport;
548 ahw->physical_port = (u8) nic_info.phys_port;
549 ahw->switch_mode = nic_info.switch_mode;
550 ahw->max_mtu = nic_info.max_mtu;
551 ahw->capabilities = nic_info.capabilities;
552 ahw->nic_mode = QLC_83XX_SRIOV_MODE;
553 return err;
555 delete_vport:
556 qlcnic_sriov_pf_config_vport(adapter, 0, func);
558 disable_eswitch:
559 qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0);
561 disable_vlan_filtering:
562 qlcnic_sriov_pf_cfg_vlan_filtering(adapter, 0);
564 return err;
567 static int qlcnic_sriov_pf_enable(struct qlcnic_adapter *adapter, int num_vfs)
569 int err;
571 if (!qlcnic_sriov_enable_check(adapter))
572 return 0;
574 err = pci_enable_sriov(adapter->pdev, num_vfs);
575 if (err)
576 qlcnic_sriov_pf_cleanup(adapter);
578 return err;
581 static int __qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter,
582 int num_vfs)
584 int err = 0;
586 set_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
587 adapter->ahw->op_mode = QLCNIC_SRIOV_PF_FUNC;
589 err = qlcnic_sriov_init(adapter, num_vfs);
590 if (err)
591 goto clear_op_mode;
593 err = qlcnic_sriov_pf_create_flr_queue(adapter);
594 if (err)
595 goto sriov_cleanup;
597 err = qlcnic_sriov_pf_init(adapter);
598 if (err)
599 goto del_flr_queue;
601 qlcnic_sriov_alloc_vlans(adapter);
603 return err;
605 del_flr_queue:
606 qlcnic_sriov_pf_del_flr_queue(adapter);
608 sriov_cleanup:
609 __qlcnic_sriov_cleanup(adapter);
611 clear_op_mode:
612 clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
613 adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
614 return err;
617 static int qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter, int num_vfs)
619 struct net_device *netdev = adapter->netdev;
620 int err;
622 if (!(adapter->flags & QLCNIC_MSIX_ENABLED)) {
623 netdev_err(netdev,
624 "SR-IOV cannot be enabled, when legacy interrupts are enabled\n");
625 return -EIO;
628 rtnl_lock();
629 if (netif_running(netdev))
630 __qlcnic_down(adapter, netdev);
632 err = __qlcnic_pci_sriov_enable(adapter, num_vfs);
633 if (err)
634 goto error;
636 if (netif_running(netdev))
637 __qlcnic_up(adapter, netdev);
639 rtnl_unlock();
640 err = qlcnic_sriov_pf_enable(adapter, num_vfs);
641 if (!err) {
642 netdev_info(netdev,
643 "SR-IOV is enabled successfully on port %d\n",
644 adapter->portnum);
645 /* Return number of vfs enabled */
646 return num_vfs;
649 rtnl_lock();
650 if (netif_running(netdev))
651 __qlcnic_down(adapter, netdev);
653 error:
654 if (!qlcnic_83xx_configure_opmode(adapter)) {
655 if (netif_running(netdev))
656 __qlcnic_up(adapter, netdev);
659 rtnl_unlock();
660 netdev_info(netdev, "Failed to enable SR-IOV on port %d\n",
661 adapter->portnum);
663 return err;
666 int qlcnic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
668 struct qlcnic_adapter *adapter = pci_get_drvdata(dev);
669 int err;
671 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
672 return -EBUSY;
674 if (num_vfs == 0)
675 err = qlcnic_pci_sriov_disable(adapter);
676 else
677 err = qlcnic_pci_sriov_enable(adapter, num_vfs);
679 clear_bit(__QLCNIC_RESETTING, &adapter->state);
680 return err;
683 static int qlcnic_sriov_set_vf_acl(struct qlcnic_adapter *adapter, u8 func)
685 struct qlcnic_cmd_args cmd;
686 struct qlcnic_vport *vp;
687 int err, id;
688 u8 *mac;
690 id = qlcnic_sriov_func_to_index(adapter, func);
691 if (id < 0)
692 return id;
694 vp = adapter->ahw->sriov->vf_info[id].vp;
695 err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO);
696 if (err)
697 return err;
699 cmd.req.arg[1] = 0x3 | func << 16;
700 if (vp->spoofchk == true) {
701 mac = vp->mac;
702 cmd.req.arg[2] |= BIT_1 | BIT_3 | BIT_8;
703 cmd.req.arg[4] = mac[5] | mac[4] << 8 | mac[3] << 16 |
704 mac[2] << 24;
705 cmd.req.arg[5] = mac[1] | mac[0] << 8;
708 if (vp->vlan_mode == QLC_PVID_MODE) {
709 cmd.req.arg[2] |= BIT_6;
710 cmd.req.arg[3] |= vp->pvid << 8;
713 err = qlcnic_issue_cmd(adapter, &cmd);
714 if (err)
715 dev_err(&adapter->pdev->dev, "Failed to set ACL, err=%d\n",
716 err);
718 qlcnic_free_mbx_args(&cmd);
719 return err;
722 static int qlcnic_sriov_set_vf_vport_info(struct qlcnic_adapter *adapter,
723 u16 func)
725 struct qlcnic_info defvp_info;
726 int err;
728 err = qlcnic_sriov_pf_cal_res_limit(adapter, &defvp_info, func);
729 if (err)
730 return -EIO;
732 err = qlcnic_sriov_set_vf_acl(adapter, func);
733 if (err)
734 return err;
736 return 0;
739 static int qlcnic_sriov_pf_channel_cfg_cmd(struct qlcnic_bc_trans *trans,
740 struct qlcnic_cmd_args *cmd)
742 struct qlcnic_vf_info *vf = trans->vf;
743 struct qlcnic_vport *vp = vf->vp;
744 struct qlcnic_adapter *adapter;
745 struct qlcnic_sriov *sriov;
746 u16 func = vf->pci_func;
747 size_t size;
748 int err;
750 adapter = vf->adapter;
751 sriov = adapter->ahw->sriov;
753 if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) {
754 err = qlcnic_sriov_pf_config_vport(adapter, 1, func);
755 if (!err) {
756 err = qlcnic_sriov_set_vf_vport_info(adapter, func);
757 if (err)
758 qlcnic_sriov_pf_config_vport(adapter, 0, func);
760 } else {
761 if (vp->vlan_mode == QLC_GUEST_VLAN_MODE) {
762 size = sizeof(*vf->sriov_vlans);
763 size = size * sriov->num_allowed_vlans;
764 memset(vf->sriov_vlans, 0, size);
767 err = qlcnic_sriov_pf_config_vport(adapter, 0, func);
770 if (err)
771 goto err_out;
773 cmd->rsp.arg[0] |= (1 << 25);
775 if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT)
776 set_bit(QLC_BC_VF_STATE, &vf->state);
777 else
778 clear_bit(QLC_BC_VF_STATE, &vf->state);
780 return err;
782 err_out:
783 cmd->rsp.arg[0] |= (2 << 25);
784 return err;
787 static int qlcnic_sriov_cfg_vf_def_mac(struct qlcnic_adapter *adapter,
788 struct qlcnic_vf_info *vf,
789 u16 vlan, u8 op)
791 struct qlcnic_cmd_args *cmd;
792 struct qlcnic_macvlan_mbx mv;
793 struct qlcnic_vport *vp;
794 u8 *addr;
795 int err;
796 u32 *buf;
797 int vpid;
799 vp = vf->vp;
801 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
802 if (!cmd)
803 return -ENOMEM;
805 err = qlcnic_alloc_mbx_args(cmd, adapter, QLCNIC_CMD_CONFIG_MAC_VLAN);
806 if (err)
807 goto free_cmd;
809 cmd->type = QLC_83XX_MBX_CMD_NO_WAIT;
810 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func);
811 if (vpid < 0) {
812 err = -EINVAL;
813 goto free_args;
816 if (vlan)
817 op = ((op == QLCNIC_MAC_ADD || op == QLCNIC_MAC_VLAN_ADD) ?
818 QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_VLAN_DEL);
820 cmd->req.arg[1] = op | (1 << 8) | (3 << 6);
821 cmd->req.arg[1] |= ((vpid & 0xffff) << 16) | BIT_31;
823 addr = vp->mac;
824 mv.vlan = vlan;
825 mv.mac_addr0 = addr[0];
826 mv.mac_addr1 = addr[1];
827 mv.mac_addr2 = addr[2];
828 mv.mac_addr3 = addr[3];
829 mv.mac_addr4 = addr[4];
830 mv.mac_addr5 = addr[5];
831 buf = &cmd->req.arg[2];
832 memcpy(buf, &mv, sizeof(struct qlcnic_macvlan_mbx));
834 err = qlcnic_issue_cmd(adapter, cmd);
836 if (!err)
837 return err;
839 free_args:
840 qlcnic_free_mbx_args(cmd);
841 free_cmd:
842 kfree(cmd);
843 return err;
846 static int qlcnic_sriov_validate_create_rx_ctx(struct qlcnic_cmd_args *cmd)
848 if ((cmd->req.arg[0] >> 29) != 0x3)
849 return -EINVAL;
851 return 0;
854 static void qlcnic_83xx_cfg_default_mac_vlan(struct qlcnic_adapter *adapter,
855 struct qlcnic_vf_info *vf,
856 int opcode)
858 struct qlcnic_sriov *sriov;
859 u16 vlan;
860 int i;
862 sriov = adapter->ahw->sriov;
864 spin_lock_bh(&vf->vlan_list_lock);
865 if (vf->num_vlan) {
866 for (i = 0; i < sriov->num_allowed_vlans; i++) {
867 vlan = vf->sriov_vlans[i];
868 if (vlan)
869 qlcnic_sriov_cfg_vf_def_mac(adapter, vf, vlan,
870 opcode);
873 spin_unlock_bh(&vf->vlan_list_lock);
875 if (vf->vp->vlan_mode != QLC_PVID_MODE) {
876 if (qlcnic_83xx_pf_check(adapter) &&
877 qlcnic_sriov_check_any_vlan(vf))
878 return;
879 qlcnic_sriov_cfg_vf_def_mac(adapter, vf, 0, opcode);
883 static int qlcnic_sriov_pf_create_rx_ctx_cmd(struct qlcnic_bc_trans *tran,
884 struct qlcnic_cmd_args *cmd)
886 struct qlcnic_vf_info *vf = tran->vf;
887 struct qlcnic_adapter *adapter = vf->adapter;
888 struct qlcnic_rcv_mbx_out *mbx_out;
889 int err;
891 err = qlcnic_sriov_validate_create_rx_ctx(cmd);
892 if (err) {
893 cmd->rsp.arg[0] |= (0x6 << 25);
894 return err;
897 cmd->req.arg[6] = vf->vp->handle;
898 err = qlcnic_issue_cmd(adapter, cmd);
900 if (!err) {
901 mbx_out = (struct qlcnic_rcv_mbx_out *)&cmd->rsp.arg[1];
902 vf->rx_ctx_id = mbx_out->ctx_id;
903 qlcnic_83xx_cfg_default_mac_vlan(adapter, vf, QLCNIC_MAC_ADD);
904 } else {
905 vf->rx_ctx_id = 0;
908 return err;
911 static int qlcnic_sriov_pf_mac_address_cmd(struct qlcnic_bc_trans *trans,
912 struct qlcnic_cmd_args *cmd)
914 struct qlcnic_vf_info *vf = trans->vf;
915 u8 type, *mac;
917 type = cmd->req.arg[1];
918 switch (type) {
919 case QLCNIC_SET_STATION_MAC:
920 case QLCNIC_SET_FAC_DEF_MAC:
921 cmd->rsp.arg[0] = (2 << 25);
922 break;
923 case QLCNIC_GET_CURRENT_MAC:
924 cmd->rsp.arg[0] = (1 << 25);
925 mac = vf->vp->mac;
926 cmd->rsp.arg[2] = mac[1] | ((mac[0] << 8) & 0xff00);
927 cmd->rsp.arg[1] = mac[5] | ((mac[4] << 8) & 0xff00) |
928 ((mac[3]) << 16 & 0xff0000) |
929 ((mac[2]) << 24 & 0xff000000);
932 return 0;
935 static int qlcnic_sriov_validate_create_tx_ctx(struct qlcnic_cmd_args *cmd)
937 if ((cmd->req.arg[0] >> 29) != 0x3)
938 return -EINVAL;
940 return 0;
943 static int qlcnic_sriov_pf_create_tx_ctx_cmd(struct qlcnic_bc_trans *trans,
944 struct qlcnic_cmd_args *cmd)
946 struct qlcnic_vf_info *vf = trans->vf;
947 struct qlcnic_adapter *adapter = vf->adapter;
948 struct qlcnic_tx_mbx_out *mbx_out;
949 int err;
951 err = qlcnic_sriov_validate_create_tx_ctx(cmd);
952 if (err) {
953 cmd->rsp.arg[0] |= (0x6 << 25);
954 return err;
957 cmd->req.arg[5] |= vf->vp->handle << 16;
958 err = qlcnic_issue_cmd(adapter, cmd);
959 if (!err) {
960 mbx_out = (struct qlcnic_tx_mbx_out *)&cmd->rsp.arg[2];
961 vf->tx_ctx_id = mbx_out->ctx_id;
962 } else {
963 vf->tx_ctx_id = 0;
966 return err;
969 static int qlcnic_sriov_validate_del_rx_ctx(struct qlcnic_vf_info *vf,
970 struct qlcnic_cmd_args *cmd)
972 if ((cmd->req.arg[0] >> 29) != 0x3)
973 return -EINVAL;
975 if ((cmd->req.arg[1] & 0xffff) != vf->rx_ctx_id)
976 return -EINVAL;
978 return 0;
981 static int qlcnic_sriov_pf_del_rx_ctx_cmd(struct qlcnic_bc_trans *trans,
982 struct qlcnic_cmd_args *cmd)
984 struct qlcnic_vf_info *vf = trans->vf;
985 struct qlcnic_adapter *adapter = vf->adapter;
986 int err;
988 err = qlcnic_sriov_validate_del_rx_ctx(vf, cmd);
989 if (err) {
990 cmd->rsp.arg[0] |= (0x6 << 25);
991 return err;
994 qlcnic_83xx_cfg_default_mac_vlan(adapter, vf, QLCNIC_MAC_DEL);
995 cmd->req.arg[1] |= vf->vp->handle << 16;
996 err = qlcnic_issue_cmd(adapter, cmd);
998 if (!err)
999 vf->rx_ctx_id = 0;
1001 return err;
1004 static int qlcnic_sriov_validate_del_tx_ctx(struct qlcnic_vf_info *vf,
1005 struct qlcnic_cmd_args *cmd)
1007 if ((cmd->req.arg[0] >> 29) != 0x3)
1008 return -EINVAL;
1010 if ((cmd->req.arg[1] & 0xffff) != vf->tx_ctx_id)
1011 return -EINVAL;
1013 return 0;
1016 static int qlcnic_sriov_pf_del_tx_ctx_cmd(struct qlcnic_bc_trans *trans,
1017 struct qlcnic_cmd_args *cmd)
1019 struct qlcnic_vf_info *vf = trans->vf;
1020 struct qlcnic_adapter *adapter = vf->adapter;
1021 int err;
1023 err = qlcnic_sriov_validate_del_tx_ctx(vf, cmd);
1024 if (err) {
1025 cmd->rsp.arg[0] |= (0x6 << 25);
1026 return err;
1029 cmd->req.arg[1] |= vf->vp->handle << 16;
1030 err = qlcnic_issue_cmd(adapter, cmd);
1032 if (!err)
1033 vf->tx_ctx_id = 0;
1035 return err;
1038 static int qlcnic_sriov_validate_cfg_lro(struct qlcnic_vf_info *vf,
1039 struct qlcnic_cmd_args *cmd)
1041 if ((cmd->req.arg[1] >> 16) != vf->rx_ctx_id)
1042 return -EINVAL;
1044 return 0;
1047 static int qlcnic_sriov_pf_cfg_lro_cmd(struct qlcnic_bc_trans *trans,
1048 struct qlcnic_cmd_args *cmd)
1050 struct qlcnic_vf_info *vf = trans->vf;
1051 struct qlcnic_adapter *adapter = vf->adapter;
1052 int err;
1054 err = qlcnic_sriov_validate_cfg_lro(vf, cmd);
1055 if (err) {
1056 cmd->rsp.arg[0] |= (0x6 << 25);
1057 return err;
1060 err = qlcnic_issue_cmd(adapter, cmd);
1061 return err;
1064 static int qlcnic_sriov_pf_cfg_ip_cmd(struct qlcnic_bc_trans *trans,
1065 struct qlcnic_cmd_args *cmd)
1067 struct qlcnic_vf_info *vf = trans->vf;
1068 struct qlcnic_adapter *adapter = vf->adapter;
1069 int err = -EIO;
1070 u8 op;
1072 op = cmd->req.arg[1] & 0xff;
1074 cmd->req.arg[1] |= vf->vp->handle << 16;
1075 cmd->req.arg[1] |= BIT_31;
1077 err = qlcnic_issue_cmd(adapter, cmd);
1078 return err;
1081 static int qlcnic_sriov_validate_cfg_intrpt(struct qlcnic_vf_info *vf,
1082 struct qlcnic_cmd_args *cmd)
1084 if (((cmd->req.arg[1] >> 8) & 0xff) != vf->pci_func)
1085 return -EINVAL;
1087 if (!(cmd->req.arg[1] & BIT_16))
1088 return -EINVAL;
1090 if ((cmd->req.arg[1] & 0xff) != 0x1)
1091 return -EINVAL;
1093 return 0;
1096 static int qlcnic_sriov_pf_cfg_intrpt_cmd(struct qlcnic_bc_trans *trans,
1097 struct qlcnic_cmd_args *cmd)
1099 struct qlcnic_vf_info *vf = trans->vf;
1100 struct qlcnic_adapter *adapter = vf->adapter;
1101 int err;
1103 err = qlcnic_sriov_validate_cfg_intrpt(vf, cmd);
1104 if (err)
1105 cmd->rsp.arg[0] |= (0x6 << 25);
1106 else
1107 err = qlcnic_issue_cmd(adapter, cmd);
1109 return err;
1112 static int qlcnic_sriov_validate_mtu(struct qlcnic_adapter *adapter,
1113 struct qlcnic_vf_info *vf,
1114 struct qlcnic_cmd_args *cmd)
1116 if (cmd->req.arg[1] != vf->rx_ctx_id)
1117 return -EINVAL;
1119 if (cmd->req.arg[2] > adapter->ahw->max_mtu)
1120 return -EINVAL;
1122 return 0;
1125 static int qlcnic_sriov_pf_set_mtu_cmd(struct qlcnic_bc_trans *trans,
1126 struct qlcnic_cmd_args *cmd)
1128 struct qlcnic_vf_info *vf = trans->vf;
1129 struct qlcnic_adapter *adapter = vf->adapter;
1130 int err;
1132 err = qlcnic_sriov_validate_mtu(adapter, vf, cmd);
1133 if (err)
1134 cmd->rsp.arg[0] |= (0x6 << 25);
1135 else
1136 err = qlcnic_issue_cmd(adapter, cmd);
1138 return err;
1141 static int qlcnic_sriov_validate_get_nic_info(struct qlcnic_vf_info *vf,
1142 struct qlcnic_cmd_args *cmd)
1144 if (cmd->req.arg[1] & BIT_31) {
1145 if (((cmd->req.arg[1] >> 16) & 0x7fff) != vf->pci_func)
1146 return -EINVAL;
1147 } else {
1148 cmd->req.arg[1] |= vf->vp->handle << 16;
1151 return 0;
1154 static int qlcnic_sriov_pf_get_nic_info_cmd(struct qlcnic_bc_trans *trans,
1155 struct qlcnic_cmd_args *cmd)
1157 struct qlcnic_vf_info *vf = trans->vf;
1158 struct qlcnic_adapter *adapter = vf->adapter;
1159 int err;
1161 err = qlcnic_sriov_validate_get_nic_info(vf, cmd);
1162 if (err) {
1163 cmd->rsp.arg[0] |= (0x6 << 25);
1164 return err;
1167 err = qlcnic_issue_cmd(adapter, cmd);
1168 return err;
1171 static int qlcnic_sriov_validate_cfg_rss(struct qlcnic_vf_info *vf,
1172 struct qlcnic_cmd_args *cmd)
1174 if (cmd->req.arg[1] != vf->rx_ctx_id)
1175 return -EINVAL;
1177 return 0;
1180 static int qlcnic_sriov_pf_cfg_rss_cmd(struct qlcnic_bc_trans *trans,
1181 struct qlcnic_cmd_args *cmd)
1183 struct qlcnic_vf_info *vf = trans->vf;
1184 struct qlcnic_adapter *adapter = vf->adapter;
1185 int err;
1187 err = qlcnic_sriov_validate_cfg_rss(vf, cmd);
1188 if (err)
1189 cmd->rsp.arg[0] |= (0x6 << 25);
1190 else
1191 err = qlcnic_issue_cmd(adapter, cmd);
1193 return err;
1196 static int qlcnic_sriov_validate_cfg_intrcoal(struct qlcnic_adapter *adapter,
1197 struct qlcnic_vf_info *vf,
1198 struct qlcnic_cmd_args *cmd)
1200 struct qlcnic_nic_intr_coalesce *coal = &adapter->ahw->coal;
1201 u16 ctx_id, pkts, time;
1202 int err = -EINVAL;
1203 u8 type;
1205 type = cmd->req.arg[1] & QLC_INTR_COAL_TYPE_MASK;
1206 ctx_id = cmd->req.arg[1] >> 16;
1207 pkts = cmd->req.arg[2] & 0xffff;
1208 time = cmd->req.arg[2] >> 16;
1210 switch (type) {
1211 case QLCNIC_INTR_COAL_TYPE_RX:
1212 if (ctx_id != vf->rx_ctx_id || pkts > coal->rx_packets ||
1213 time < coal->rx_time_us)
1214 goto err_label;
1215 break;
1216 case QLCNIC_INTR_COAL_TYPE_TX:
1217 if (ctx_id != vf->tx_ctx_id || pkts > coal->tx_packets ||
1218 time < coal->tx_time_us)
1219 goto err_label;
1220 break;
1221 default:
1222 netdev_err(adapter->netdev, "Invalid coalescing type 0x%x received\n",
1223 type);
1224 return err;
1227 return 0;
1229 err_label:
1230 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",
1231 vf->rx_ctx_id, coal->rx_packets, coal->rx_time_us,
1232 vf->tx_ctx_id, coal->tx_packets, coal->tx_time_us);
1233 netdev_err(adapter->netdev, "Received: ctx_id 0x%x packets 0x%x time_us 0x%x type 0x%x\n",
1234 ctx_id, pkts, time, type);
1236 return err;
1239 static int qlcnic_sriov_pf_cfg_intrcoal_cmd(struct qlcnic_bc_trans *tran,
1240 struct qlcnic_cmd_args *cmd)
1242 struct qlcnic_vf_info *vf = tran->vf;
1243 struct qlcnic_adapter *adapter = vf->adapter;
1244 int err;
1246 err = qlcnic_sriov_validate_cfg_intrcoal(adapter, vf, cmd);
1247 if (err) {
1248 cmd->rsp.arg[0] |= (0x6 << 25);
1249 return err;
1252 err = qlcnic_issue_cmd(adapter, cmd);
1253 return err;
1256 static int qlcnic_sriov_validate_cfg_macvlan(struct qlcnic_adapter *adapter,
1257 struct qlcnic_vf_info *vf,
1258 struct qlcnic_cmd_args *cmd)
1260 struct qlcnic_vport *vp = vf->vp;
1261 u8 op, new_op;
1263 if (!(cmd->req.arg[1] & BIT_8))
1264 return -EINVAL;
1266 cmd->req.arg[1] |= (vf->vp->handle << 16);
1267 cmd->req.arg[1] |= BIT_31;
1269 if (vp->vlan_mode == QLC_PVID_MODE) {
1270 op = cmd->req.arg[1] & 0x7;
1271 cmd->req.arg[1] &= ~0x7;
1272 new_op = (op == QLCNIC_MAC_ADD || op == QLCNIC_MAC_VLAN_ADD) ?
1273 QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_VLAN_DEL;
1274 cmd->req.arg[3] |= vp->pvid << 16;
1275 cmd->req.arg[1] |= new_op;
1278 return 0;
1281 static int qlcnic_sriov_pf_cfg_macvlan_cmd(struct qlcnic_bc_trans *trans,
1282 struct qlcnic_cmd_args *cmd)
1284 struct qlcnic_vf_info *vf = trans->vf;
1285 struct qlcnic_adapter *adapter = vf->adapter;
1286 int err;
1288 err = qlcnic_sriov_validate_cfg_macvlan(adapter, vf, cmd);
1289 if (err) {
1290 cmd->rsp.arg[0] |= (0x6 << 25);
1291 return err;
1294 err = qlcnic_issue_cmd(adapter, cmd);
1295 return err;
1298 static int qlcnic_sriov_validate_linkevent(struct qlcnic_vf_info *vf,
1299 struct qlcnic_cmd_args *cmd)
1301 if ((cmd->req.arg[1] >> 16) != vf->rx_ctx_id)
1302 return -EINVAL;
1304 return 0;
1307 static int qlcnic_sriov_pf_linkevent_cmd(struct qlcnic_bc_trans *trans,
1308 struct qlcnic_cmd_args *cmd)
1310 struct qlcnic_vf_info *vf = trans->vf;
1311 struct qlcnic_adapter *adapter = vf->adapter;
1312 int err;
1314 err = qlcnic_sriov_validate_linkevent(vf, cmd);
1315 if (err) {
1316 cmd->rsp.arg[0] |= (0x6 << 25);
1317 return err;
1320 err = qlcnic_issue_cmd(adapter, cmd);
1321 return err;
1324 static int qlcnic_sriov_pf_cfg_promisc_cmd(struct qlcnic_bc_trans *trans,
1325 struct qlcnic_cmd_args *cmd)
1327 struct qlcnic_vf_info *vf = trans->vf;
1328 struct qlcnic_adapter *adapter = vf->adapter;
1329 int err;
1331 cmd->req.arg[1] |= vf->vp->handle << 16;
1332 cmd->req.arg[1] |= BIT_31;
1333 err = qlcnic_issue_cmd(adapter, cmd);
1334 return err;
1337 static int qlcnic_sriov_pf_get_acl_cmd(struct qlcnic_bc_trans *trans,
1338 struct qlcnic_cmd_args *cmd)
1340 struct qlcnic_vf_info *vf = trans->vf;
1341 struct qlcnic_vport *vp = vf->vp;
1342 u8 cmd_op, mode = vp->vlan_mode;
1343 struct qlcnic_adapter *adapter;
1344 struct qlcnic_sriov *sriov;
1346 adapter = vf->adapter;
1347 sriov = adapter->ahw->sriov;
1349 cmd_op = trans->req_hdr->cmd_op;
1350 cmd->rsp.arg[0] |= 1 << 25;
1352 /* For 84xx adapter in case of PVID , PFD should send vlan mode as
1353 * QLC_NO_VLAN_MODE to VFD which is zero in mailbox response
1355 if (qlcnic_84xx_check(adapter) && mode == QLC_PVID_MODE)
1356 return 0;
1358 switch (mode) {
1359 case QLC_GUEST_VLAN_MODE:
1360 cmd->rsp.arg[1] = mode | 1 << 8;
1361 cmd->rsp.arg[2] = sriov->num_allowed_vlans << 16;
1362 break;
1363 case QLC_PVID_MODE:
1364 cmd->rsp.arg[1] = mode | 1 << 8 | vp->pvid << 16;
1365 break;
1368 return 0;
1371 static int qlcnic_sriov_pf_del_guest_vlan(struct qlcnic_adapter *adapter,
1372 struct qlcnic_vf_info *vf,
1373 struct qlcnic_cmd_args *cmd)
1375 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1376 u16 vlan;
1378 if (!qlcnic_sriov_check_any_vlan(vf))
1379 return -EINVAL;
1381 vlan = cmd->req.arg[1] >> 16;
1382 if (!vf->rx_ctx_id) {
1383 qlcnic_sriov_del_vlan_id(sriov, vf, vlan);
1384 return 0;
1387 qlcnic_sriov_cfg_vf_def_mac(adapter, vf, vlan, QLCNIC_MAC_DEL);
1388 qlcnic_sriov_del_vlan_id(sriov, vf, vlan);
1390 if (qlcnic_83xx_pf_check(adapter))
1391 qlcnic_sriov_cfg_vf_def_mac(adapter, vf,
1392 0, QLCNIC_MAC_ADD);
1393 return 0;
1396 static int qlcnic_sriov_pf_add_guest_vlan(struct qlcnic_adapter *adapter,
1397 struct qlcnic_vf_info *vf,
1398 struct qlcnic_cmd_args *cmd)
1400 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1401 int err = -EIO;
1402 u16 vlan;
1404 if (qlcnic_83xx_pf_check(adapter) && qlcnic_sriov_check_any_vlan(vf))
1405 return err;
1407 vlan = cmd->req.arg[1] >> 16;
1409 if (!vf->rx_ctx_id) {
1410 qlcnic_sriov_add_vlan_id(sriov, vf, vlan);
1411 return 0;
1414 if (qlcnic_83xx_pf_check(adapter)) {
1415 err = qlcnic_sriov_cfg_vf_def_mac(adapter, vf, 0,
1416 QLCNIC_MAC_DEL);
1417 if (err)
1418 return err;
1421 err = qlcnic_sriov_cfg_vf_def_mac(adapter, vf, vlan, QLCNIC_MAC_ADD);
1423 if (err) {
1424 if (qlcnic_83xx_pf_check(adapter))
1425 qlcnic_sriov_cfg_vf_def_mac(adapter, vf, 0,
1426 QLCNIC_MAC_ADD);
1427 return err;
1430 qlcnic_sriov_add_vlan_id(sriov, vf, vlan);
1431 return err;
1434 static int qlcnic_sriov_pf_cfg_guest_vlan_cmd(struct qlcnic_bc_trans *tran,
1435 struct qlcnic_cmd_args *cmd)
1437 struct qlcnic_vf_info *vf = tran->vf;
1438 struct qlcnic_adapter *adapter = vf->adapter;
1439 struct qlcnic_vport *vp = vf->vp;
1440 int err = -EIO;
1441 u8 op;
1443 if (vp->vlan_mode != QLC_GUEST_VLAN_MODE) {
1444 cmd->rsp.arg[0] |= 2 << 25;
1445 return err;
1448 op = cmd->req.arg[1] & 0xf;
1450 if (op)
1451 err = qlcnic_sriov_pf_add_guest_vlan(adapter, vf, cmd);
1452 else
1453 err = qlcnic_sriov_pf_del_guest_vlan(adapter, vf, cmd);
1455 cmd->rsp.arg[0] |= err ? 2 << 25 : 1 << 25;
1456 return err;
1459 static const int qlcnic_pf_passthru_supp_cmds[] = {
1460 QLCNIC_CMD_GET_STATISTICS,
1461 QLCNIC_CMD_GET_PORT_CONFIG,
1462 QLCNIC_CMD_GET_LINK_STATUS,
1463 QLCNIC_CMD_INIT_NIC_FUNC,
1464 QLCNIC_CMD_STOP_NIC_FUNC,
1467 static const struct qlcnic_sriov_cmd_handler qlcnic_pf_bc_cmd_hdlr[] = {
1468 [QLCNIC_BC_CMD_CHANNEL_INIT] = {&qlcnic_sriov_pf_channel_cfg_cmd},
1469 [QLCNIC_BC_CMD_CHANNEL_TERM] = {&qlcnic_sriov_pf_channel_cfg_cmd},
1470 [QLCNIC_BC_CMD_GET_ACL] = {&qlcnic_sriov_pf_get_acl_cmd},
1471 [QLCNIC_BC_CMD_CFG_GUEST_VLAN] = {&qlcnic_sriov_pf_cfg_guest_vlan_cmd},
1474 static const struct qlcnic_sriov_fw_cmd_handler qlcnic_pf_fw_cmd_hdlr[] = {
1475 {QLCNIC_CMD_CREATE_RX_CTX, qlcnic_sriov_pf_create_rx_ctx_cmd},
1476 {QLCNIC_CMD_CREATE_TX_CTX, qlcnic_sriov_pf_create_tx_ctx_cmd},
1477 {QLCNIC_CMD_MAC_ADDRESS, qlcnic_sriov_pf_mac_address_cmd},
1478 {QLCNIC_CMD_DESTROY_RX_CTX, qlcnic_sriov_pf_del_rx_ctx_cmd},
1479 {QLCNIC_CMD_DESTROY_TX_CTX, qlcnic_sriov_pf_del_tx_ctx_cmd},
1480 {QLCNIC_CMD_CONFIGURE_HW_LRO, qlcnic_sriov_pf_cfg_lro_cmd},
1481 {QLCNIC_CMD_CONFIGURE_IP_ADDR, qlcnic_sriov_pf_cfg_ip_cmd},
1482 {QLCNIC_CMD_CONFIG_INTRPT, qlcnic_sriov_pf_cfg_intrpt_cmd},
1483 {QLCNIC_CMD_SET_MTU, qlcnic_sriov_pf_set_mtu_cmd},
1484 {QLCNIC_CMD_GET_NIC_INFO, qlcnic_sriov_pf_get_nic_info_cmd},
1485 {QLCNIC_CMD_CONFIGURE_RSS, qlcnic_sriov_pf_cfg_rss_cmd},
1486 {QLCNIC_CMD_CONFIG_INTR_COAL, qlcnic_sriov_pf_cfg_intrcoal_cmd},
1487 {QLCNIC_CMD_CONFIG_MAC_VLAN, qlcnic_sriov_pf_cfg_macvlan_cmd},
1488 {QLCNIC_CMD_GET_LINK_EVENT, qlcnic_sriov_pf_linkevent_cmd},
1489 {QLCNIC_CMD_CONFIGURE_MAC_RX_MODE, qlcnic_sriov_pf_cfg_promisc_cmd},
1492 void qlcnic_sriov_pf_process_bc_cmd(struct qlcnic_adapter *adapter,
1493 struct qlcnic_bc_trans *trans,
1494 struct qlcnic_cmd_args *cmd)
1496 u8 size, cmd_op;
1498 cmd_op = trans->req_hdr->cmd_op;
1500 if (trans->req_hdr->op_type == QLC_BC_CMD) {
1501 size = ARRAY_SIZE(qlcnic_pf_bc_cmd_hdlr);
1502 if (cmd_op < size) {
1503 qlcnic_pf_bc_cmd_hdlr[cmd_op].fn(trans, cmd);
1504 return;
1506 } else {
1507 int i;
1508 size = ARRAY_SIZE(qlcnic_pf_fw_cmd_hdlr);
1509 for (i = 0; i < size; i++) {
1510 if (cmd_op == qlcnic_pf_fw_cmd_hdlr[i].cmd) {
1511 qlcnic_pf_fw_cmd_hdlr[i].fn(trans, cmd);
1512 return;
1516 size = ARRAY_SIZE(qlcnic_pf_passthru_supp_cmds);
1517 for (i = 0; i < size; i++) {
1518 if (cmd_op == qlcnic_pf_passthru_supp_cmds[i]) {
1519 qlcnic_issue_cmd(adapter, cmd);
1520 return;
1525 cmd->rsp.arg[0] |= (0x9 << 25);
1528 void qlcnic_pf_set_interface_id_create_rx_ctx(struct qlcnic_adapter *adapter,
1529 u32 *int_id)
1531 u16 vpid;
1533 vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1534 adapter->ahw->pci_func);
1535 *int_id |= vpid;
1538 void qlcnic_pf_set_interface_id_del_rx_ctx(struct qlcnic_adapter *adapter,
1539 u32 *int_id)
1541 u16 vpid;
1543 vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1544 adapter->ahw->pci_func);
1545 *int_id |= vpid << 16;
1548 void qlcnic_pf_set_interface_id_create_tx_ctx(struct qlcnic_adapter *adapter,
1549 u32 *int_id)
1551 int vpid;
1553 vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1554 adapter->ahw->pci_func);
1555 *int_id |= vpid << 16;
1558 void qlcnic_pf_set_interface_id_del_tx_ctx(struct qlcnic_adapter *adapter,
1559 u32 *int_id)
1561 u16 vpid;
1563 vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1564 adapter->ahw->pci_func);
1565 *int_id |= vpid << 16;
1568 void qlcnic_pf_set_interface_id_promisc(struct qlcnic_adapter *adapter,
1569 u32 *int_id)
1571 u16 vpid;
1573 vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1574 adapter->ahw->pci_func);
1575 *int_id |= (vpid << 16) | BIT_31;
1578 void qlcnic_pf_set_interface_id_ipaddr(struct qlcnic_adapter *adapter,
1579 u32 *int_id)
1581 u16 vpid;
1583 vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1584 adapter->ahw->pci_func);
1585 *int_id |= (vpid << 16) | BIT_31;
1588 void qlcnic_pf_set_interface_id_macaddr(struct qlcnic_adapter *adapter,
1589 u32 *int_id)
1591 u16 vpid;
1593 vpid = qlcnic_sriov_pf_get_vport_handle(adapter,
1594 adapter->ahw->pci_func);
1595 *int_id |= (vpid << 16) | BIT_31;
1598 static void qlcnic_sriov_del_rx_ctx(struct qlcnic_adapter *adapter,
1599 struct qlcnic_vf_info *vf)
1601 struct qlcnic_cmd_args cmd;
1602 int vpid;
1604 if (!vf->rx_ctx_id)
1605 return;
1607 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_DESTROY_RX_CTX))
1608 return;
1610 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func);
1611 if (vpid >= 0) {
1612 cmd.req.arg[1] = vf->rx_ctx_id | (vpid & 0xffff) << 16;
1613 if (qlcnic_issue_cmd(adapter, &cmd))
1614 dev_err(&adapter->pdev->dev,
1615 "Failed to delete Tx ctx in firmware for func 0x%x\n",
1616 vf->pci_func);
1617 else
1618 vf->rx_ctx_id = 0;
1621 qlcnic_free_mbx_args(&cmd);
1624 static void qlcnic_sriov_del_tx_ctx(struct qlcnic_adapter *adapter,
1625 struct qlcnic_vf_info *vf)
1627 struct qlcnic_cmd_args cmd;
1628 int vpid;
1630 if (!vf->tx_ctx_id)
1631 return;
1633 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_DESTROY_TX_CTX))
1634 return;
1636 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, vf->pci_func);
1637 if (vpid >= 0) {
1638 cmd.req.arg[1] |= vf->tx_ctx_id | (vpid & 0xffff) << 16;
1639 if (qlcnic_issue_cmd(adapter, &cmd))
1640 dev_err(&adapter->pdev->dev,
1641 "Failed to delete Tx ctx in firmware for func 0x%x\n",
1642 vf->pci_func);
1643 else
1644 vf->tx_ctx_id = 0;
1647 qlcnic_free_mbx_args(&cmd);
1650 static int qlcnic_sriov_add_act_list_irqsave(struct qlcnic_sriov *sriov,
1651 struct qlcnic_vf_info *vf,
1652 struct qlcnic_bc_trans *trans)
1654 struct qlcnic_trans_list *t_list = &vf->rcv_act;
1655 unsigned long flag;
1657 spin_lock_irqsave(&t_list->lock, flag);
1659 __qlcnic_sriov_add_act_list(sriov, vf, trans);
1661 spin_unlock_irqrestore(&t_list->lock, flag);
1662 return 0;
1665 static void __qlcnic_sriov_process_flr(struct qlcnic_vf_info *vf)
1667 struct qlcnic_adapter *adapter = vf->adapter;
1669 qlcnic_sriov_cleanup_list(&vf->rcv_pend);
1670 cancel_work_sync(&vf->trans_work);
1671 qlcnic_sriov_cleanup_list(&vf->rcv_act);
1673 if (test_bit(QLC_BC_VF_SOFT_FLR, &vf->state)) {
1674 qlcnic_sriov_del_tx_ctx(adapter, vf);
1675 qlcnic_sriov_del_rx_ctx(adapter, vf);
1678 qlcnic_sriov_pf_config_vport(adapter, 0, vf->pci_func);
1680 clear_bit(QLC_BC_VF_FLR, &vf->state);
1681 if (test_bit(QLC_BC_VF_SOFT_FLR, &vf->state)) {
1682 qlcnic_sriov_add_act_list_irqsave(adapter->ahw->sriov, vf,
1683 vf->flr_trans);
1684 clear_bit(QLC_BC_VF_SOFT_FLR, &vf->state);
1685 vf->flr_trans = NULL;
1689 static void qlcnic_sriov_pf_process_flr(struct work_struct *work)
1691 struct qlcnic_vf_info *vf;
1693 vf = container_of(work, struct qlcnic_vf_info, flr_work);
1694 __qlcnic_sriov_process_flr(vf);
1695 return;
1698 static void qlcnic_sriov_schedule_flr(struct qlcnic_sriov *sriov,
1699 struct qlcnic_vf_info *vf,
1700 work_func_t func)
1702 if (test_bit(__QLCNIC_RESETTING, &vf->adapter->state))
1703 return;
1705 INIT_WORK(&vf->flr_work, func);
1706 queue_work(sriov->bc.bc_flr_wq, &vf->flr_work);
1709 static void qlcnic_sriov_handle_soft_flr(struct qlcnic_adapter *adapter,
1710 struct qlcnic_bc_trans *trans,
1711 struct qlcnic_vf_info *vf)
1713 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1715 set_bit(QLC_BC_VF_FLR, &vf->state);
1716 clear_bit(QLC_BC_VF_STATE, &vf->state);
1717 set_bit(QLC_BC_VF_SOFT_FLR, &vf->state);
1718 vf->flr_trans = trans;
1719 qlcnic_sriov_schedule_flr(sriov, vf, qlcnic_sriov_pf_process_flr);
1720 netdev_info(adapter->netdev, "Software FLR for PCI func %d\n",
1721 vf->pci_func);
1724 bool qlcnic_sriov_soft_flr_check(struct qlcnic_adapter *adapter,
1725 struct qlcnic_bc_trans *trans,
1726 struct qlcnic_vf_info *vf)
1728 struct qlcnic_bc_hdr *hdr = trans->req_hdr;
1730 if ((hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) &&
1731 (hdr->op_type == QLC_BC_CMD) &&
1732 test_bit(QLC_BC_VF_STATE, &vf->state)) {
1733 qlcnic_sriov_handle_soft_flr(adapter, trans, vf);
1734 return true;
1737 return false;
1740 void qlcnic_sriov_pf_handle_flr(struct qlcnic_sriov *sriov,
1741 struct qlcnic_vf_info *vf)
1743 struct net_device *dev = vf->adapter->netdev;
1744 struct qlcnic_vport *vp = vf->vp;
1746 if (!test_and_clear_bit(QLC_BC_VF_STATE, &vf->state)) {
1747 clear_bit(QLC_BC_VF_FLR, &vf->state);
1748 return;
1751 if (test_and_set_bit(QLC_BC_VF_FLR, &vf->state)) {
1752 netdev_info(dev, "FLR for PCI func %d in progress\n",
1753 vf->pci_func);
1754 return;
1757 if (vp->vlan_mode == QLC_GUEST_VLAN_MODE)
1758 memset(vf->sriov_vlans, 0,
1759 sizeof(*vf->sriov_vlans) * sriov->num_allowed_vlans);
1761 qlcnic_sriov_schedule_flr(sriov, vf, qlcnic_sriov_pf_process_flr);
1762 netdev_info(dev, "FLR received for PCI func %d\n", vf->pci_func);
1765 void qlcnic_sriov_pf_reset(struct qlcnic_adapter *adapter)
1767 struct qlcnic_hardware_context *ahw = adapter->ahw;
1768 struct qlcnic_sriov *sriov = ahw->sriov;
1769 struct qlcnic_vf_info *vf;
1770 u16 num_vfs = sriov->num_vfs;
1771 int i;
1773 for (i = 0; i < num_vfs; i++) {
1774 vf = &sriov->vf_info[i];
1775 vf->rx_ctx_id = 0;
1776 vf->tx_ctx_id = 0;
1777 cancel_work_sync(&vf->flr_work);
1778 __qlcnic_sriov_process_flr(vf);
1779 clear_bit(QLC_BC_VF_STATE, &vf->state);
1782 qlcnic_sriov_pf_reset_vport_handle(adapter, ahw->pci_func);
1783 QLCWRX(ahw, QLCNIC_MBX_INTR_ENBL, (ahw->num_msix - 1) << 8);
1786 int qlcnic_sriov_pf_reinit(struct qlcnic_adapter *adapter)
1788 struct qlcnic_hardware_context *ahw = adapter->ahw;
1789 int err;
1791 if (!qlcnic_sriov_enable_check(adapter))
1792 return 0;
1794 ahw->op_mode = QLCNIC_SRIOV_PF_FUNC;
1796 err = qlcnic_sriov_pf_init(adapter);
1797 if (err)
1798 return err;
1800 dev_info(&adapter->pdev->dev, "%s: op_mode %d\n",
1801 __func__, ahw->op_mode);
1802 return err;
1805 int qlcnic_sriov_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1807 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1808 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1809 int i, num_vfs;
1810 struct qlcnic_vf_info *vf_info;
1811 u8 *curr_mac;
1813 if (!qlcnic_sriov_pf_check(adapter))
1814 return -EOPNOTSUPP;
1816 num_vfs = sriov->num_vfs;
1818 if (!is_valid_ether_addr(mac) || vf >= num_vfs)
1819 return -EINVAL;
1821 if (ether_addr_equal(adapter->mac_addr, mac)) {
1822 netdev_err(netdev, "MAC address is already in use by the PF\n");
1823 return -EINVAL;
1826 for (i = 0; i < num_vfs; i++) {
1827 vf_info = &sriov->vf_info[i];
1828 if (ether_addr_equal(vf_info->vp->mac, mac)) {
1829 netdev_err(netdev,
1830 "MAC address is already in use by VF %d\n",
1832 return -EINVAL;
1836 vf_info = &sriov->vf_info[vf];
1837 curr_mac = vf_info->vp->mac;
1839 if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
1840 netdev_err(netdev,
1841 "MAC address change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n",
1842 vf);
1843 return -EOPNOTSUPP;
1846 memcpy(curr_mac, mac, netdev->addr_len);
1847 netdev_info(netdev, "MAC Address %pM is configured for VF %d\n",
1848 mac, vf);
1849 return 0;
1852 int qlcnic_sriov_set_vf_tx_rate(struct net_device *netdev, int vf,
1853 int min_tx_rate, int max_tx_rate)
1855 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1856 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1857 struct qlcnic_vf_info *vf_info;
1858 struct qlcnic_info nic_info;
1859 struct qlcnic_vport *vp;
1860 u16 vpid;
1862 if (!qlcnic_sriov_pf_check(adapter))
1863 return -EOPNOTSUPP;
1865 if (vf >= sriov->num_vfs)
1866 return -EINVAL;
1868 vf_info = &sriov->vf_info[vf];
1869 vp = vf_info->vp;
1870 vpid = vp->handle;
1872 if (!min_tx_rate)
1873 min_tx_rate = QLC_VF_MIN_TX_RATE;
1875 if (max_tx_rate &&
1876 (max_tx_rate >= 10000 || max_tx_rate < min_tx_rate)) {
1877 netdev_err(netdev,
1878 "Invalid max Tx rate, allowed range is [%d - %d]",
1879 min_tx_rate, QLC_VF_MAX_TX_RATE);
1880 return -EINVAL;
1883 if (!max_tx_rate)
1884 max_tx_rate = 10000;
1886 if (min_tx_rate &&
1887 (min_tx_rate > max_tx_rate || min_tx_rate < QLC_VF_MIN_TX_RATE)) {
1888 netdev_err(netdev,
1889 "Invalid min Tx rate, allowed range is [%d - %d]",
1890 QLC_VF_MIN_TX_RATE, max_tx_rate);
1891 return -EINVAL;
1894 if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
1895 if (qlcnic_sriov_get_vf_vport_info(adapter, &nic_info, vpid))
1896 return -EIO;
1898 nic_info.max_tx_bw = max_tx_rate / 100;
1899 nic_info.min_tx_bw = min_tx_rate / 100;
1900 nic_info.bit_offsets = BIT_0;
1902 if (qlcnic_sriov_pf_set_vport_info(adapter, &nic_info, vpid))
1903 return -EIO;
1906 vp->max_tx_bw = max_tx_rate / 100;
1907 netdev_info(netdev,
1908 "Setting Max Tx rate %d (Mbps), %d %% of PF bandwidth, for VF %d\n",
1909 max_tx_rate, vp->max_tx_bw, vf);
1910 vp->min_tx_bw = min_tx_rate / 100;
1911 netdev_info(netdev,
1912 "Setting Min Tx rate %d (Mbps), %d %% of PF bandwidth, for VF %d\n",
1913 min_tx_rate, vp->min_tx_bw, vf);
1914 return 0;
1917 int qlcnic_sriov_set_vf_vlan(struct net_device *netdev, int vf,
1918 u16 vlan, u8 qos, __be16 vlan_proto)
1920 struct qlcnic_adapter *adapter = netdev_priv(netdev);
1921 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1922 struct qlcnic_vf_info *vf_info;
1923 struct qlcnic_vport *vp;
1925 if (!qlcnic_sriov_pf_check(adapter))
1926 return -EOPNOTSUPP;
1928 if (vf >= sriov->num_vfs || qos > 7)
1929 return -EINVAL;
1931 if (vlan_proto != htons(ETH_P_8021Q))
1932 return -EPROTONOSUPPORT;
1934 if (vlan > MAX_VLAN_ID) {
1935 netdev_err(netdev,
1936 "Invalid VLAN ID, allowed range is [0 - %d]\n",
1937 MAX_VLAN_ID);
1938 return -EINVAL;
1941 vf_info = &sriov->vf_info[vf];
1942 vp = vf_info->vp;
1943 if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
1944 netdev_err(netdev,
1945 "VLAN change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n",
1946 vf);
1947 return -EOPNOTSUPP;
1950 memset(vf_info->sriov_vlans, 0,
1951 sizeof(*vf_info->sriov_vlans) * sriov->num_allowed_vlans);
1953 switch (vlan) {
1954 case 4095:
1955 vp->vlan_mode = QLC_GUEST_VLAN_MODE;
1956 break;
1957 case 0:
1958 vp->vlan_mode = QLC_NO_VLAN_MODE;
1959 vp->qos = 0;
1960 break;
1961 default:
1962 vp->vlan_mode = QLC_PVID_MODE;
1963 qlcnic_sriov_add_vlan_id(sriov, vf_info, vlan);
1964 vp->qos = qos;
1965 vp->pvid = vlan;
1968 netdev_info(netdev, "Setting VLAN %d, QoS %d, for VF %d\n",
1969 vlan, qos, vf);
1970 return 0;
1973 static __u32 qlcnic_sriov_get_vf_vlan(struct qlcnic_adapter *adapter,
1974 struct qlcnic_vport *vp, int vf)
1976 __u32 vlan = 0;
1978 switch (vp->vlan_mode) {
1979 case QLC_PVID_MODE:
1980 vlan = vp->pvid;
1981 break;
1982 case QLC_GUEST_VLAN_MODE:
1983 vlan = MAX_VLAN_ID;
1984 break;
1985 case QLC_NO_VLAN_MODE:
1986 vlan = 0;
1987 break;
1988 default:
1989 netdev_info(adapter->netdev, "Invalid VLAN mode = %d for VF %d\n",
1990 vp->vlan_mode, vf);
1993 return vlan;
1996 int qlcnic_sriov_get_vf_config(struct net_device *netdev,
1997 int vf, struct ifla_vf_info *ivi)
1999 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2000 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
2001 struct qlcnic_vport *vp;
2003 if (!qlcnic_sriov_pf_check(adapter))
2004 return -EOPNOTSUPP;
2006 if (vf >= sriov->num_vfs)
2007 return -EINVAL;
2009 vp = sriov->vf_info[vf].vp;
2010 memcpy(&ivi->mac, vp->mac, ETH_ALEN);
2011 ivi->vlan = qlcnic_sriov_get_vf_vlan(adapter, vp, vf);
2012 ivi->qos = vp->qos;
2013 ivi->spoofchk = vp->spoofchk;
2014 if (vp->max_tx_bw == MAX_BW)
2015 ivi->max_tx_rate = 0;
2016 else
2017 ivi->max_tx_rate = vp->max_tx_bw * 100;
2018 if (vp->min_tx_bw == MIN_BW)
2019 ivi->min_tx_rate = 0;
2020 else
2021 ivi->min_tx_rate = vp->min_tx_bw * 100;
2023 ivi->vf = vf;
2024 return 0;
2027 int qlcnic_sriov_set_vf_spoofchk(struct net_device *netdev, int vf, bool chk)
2029 struct qlcnic_adapter *adapter = netdev_priv(netdev);
2030 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
2031 struct qlcnic_vf_info *vf_info;
2032 struct qlcnic_vport *vp;
2034 if (!qlcnic_sriov_pf_check(adapter))
2035 return -EOPNOTSUPP;
2037 if (vf >= sriov->num_vfs)
2038 return -EINVAL;
2040 vf_info = &sriov->vf_info[vf];
2041 vp = vf_info->vp;
2042 if (test_bit(QLC_BC_VF_STATE, &vf_info->state)) {
2043 netdev_err(netdev,
2044 "Spoof check change failed for VF %d, as VF driver is loaded. Please unload VF driver and retry the operation\n",
2045 vf);
2046 return -EOPNOTSUPP;
2049 vp->spoofchk = chk;
2050 return 0;