2 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2013 QLogic Corporation
5 * See LICENSE.qlcnic for copyright and licensing details.
8 #include "qlcnic_sriov.h"
10 #include <linux/types.h>
12 #define QLCNIC_SRIOV_VF_MAX_MAC 1
14 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *, u8);
16 struct qlcnic_sriov_cmd_handler {
17 int (*fn) (struct qlcnic_bc_trans *, struct qlcnic_cmd_args *);
20 static int qlcnic_sriov_pf_set_vport_info(struct qlcnic_adapter *adapter,
21 struct qlcnic_info *npar_info,
24 struct qlcnic_cmd_args cmd;
27 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_SET_NIC_INFO))
30 cmd.req.arg[1] = (vport_id << 16) | 0x1;
31 cmd.req.arg[2] = npar_info->bit_offsets;
32 cmd.req.arg[2] |= npar_info->min_tx_bw << 16;
33 cmd.req.arg[3] = npar_info->max_tx_bw | (npar_info->max_tx_ques << 16);
34 cmd.req.arg[4] = npar_info->max_tx_mac_filters;
35 cmd.req.arg[4] |= npar_info->max_rx_mcast_mac_filters << 16;
36 cmd.req.arg[5] = npar_info->max_rx_ucast_mac_filters |
37 (npar_info->max_rx_ip_addr << 16);
38 cmd.req.arg[6] = npar_info->max_rx_lro_flow |
39 (npar_info->max_rx_status_rings << 16);
40 cmd.req.arg[7] = npar_info->max_rx_buf_rings |
41 (npar_info->max_rx_ques << 16);
42 cmd.req.arg[8] = npar_info->max_tx_vlan_keys;
43 cmd.req.arg[8] |= npar_info->max_local_ipv6_addrs << 16;
44 cmd.req.arg[9] = npar_info->max_remote_ipv6_addrs;
46 err = qlcnic_issue_cmd(adapter, &cmd);
48 dev_err(&adapter->pdev->dev,
49 "Failed to set vport info, err=%d\n", err);
51 qlcnic_free_mbx_args(&cmd);
55 static int qlcnic_sriov_pf_cal_res_limit(struct qlcnic_adapter *adapter,
56 struct qlcnic_info *info, u16 func)
58 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
59 struct qlcnic_resources *res = &sriov->ff_max;
61 u32 temp, num_vf_macs, num_vfs, max;
63 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func);
67 num_vfs = sriov->num_vfs;
69 info->bit_offsets = 0xffff;
71 info->max_tx_bw = MAX_BW;
72 info->max_tx_ques = res->num_tx_queues / max;
73 info->max_rx_mcast_mac_filters = res->num_rx_mcast_mac_filters;
74 num_vf_macs = QLCNIC_SRIOV_VF_MAX_MAC;
76 if (adapter->ahw->pci_func == func) {
77 temp = res->num_rx_mcast_mac_filters - (num_vfs * num_vf_macs);
78 info->max_rx_ucast_mac_filters = temp;
79 temp = res->num_tx_mac_filters - (num_vfs * num_vf_macs);
80 info->max_tx_mac_filters = temp;
82 info->max_rx_ucast_mac_filters = num_vf_macs;
83 info->max_tx_mac_filters = num_vf_macs;
86 info->max_rx_ip_addr = res->num_destip / max;
87 info->max_rx_status_rings = res->num_rx_status_rings / max;
88 info->max_rx_buf_rings = res->num_rx_buf_rings / max;
89 info->max_rx_ques = res->num_rx_queues / max;
90 info->max_rx_lro_flow = res->num_lro_flows_supported / max;
91 info->max_tx_vlan_keys = res->num_txvlan_keys;
92 info->max_local_ipv6_addrs = res->max_local_ipv6_addrs;
93 info->max_remote_ipv6_addrs = res->max_remote_ipv6_addrs;
95 ret = qlcnic_sriov_pf_set_vport_info(adapter, info, vpid);
102 static void qlcnic_sriov_pf_set_ff_max_res(struct qlcnic_adapter *adapter,
103 struct qlcnic_info *info)
105 struct qlcnic_resources *ff_max = &adapter->ahw->sriov->ff_max;
107 ff_max->num_tx_mac_filters = info->max_tx_mac_filters;
108 ff_max->num_rx_ucast_mac_filters = info->max_rx_ucast_mac_filters;
109 ff_max->num_rx_mcast_mac_filters = info->max_rx_mcast_mac_filters;
110 ff_max->num_txvlan_keys = info->max_tx_vlan_keys;
111 ff_max->num_rx_queues = info->max_rx_ques;
112 ff_max->num_tx_queues = info->max_tx_ques;
113 ff_max->num_lro_flows_supported = info->max_rx_lro_flow;
114 ff_max->num_destip = info->max_rx_ip_addr;
115 ff_max->num_rx_buf_rings = info->max_rx_buf_rings;
116 ff_max->num_rx_status_rings = info->max_rx_status_rings;
117 ff_max->max_remote_ipv6_addrs = info->max_remote_ipv6_addrs;
118 ff_max->max_local_ipv6_addrs = info->max_local_ipv6_addrs;
121 static int qlcnic_sriov_get_pf_info(struct qlcnic_adapter *adapter,
122 struct qlcnic_info *npar_info)
125 struct qlcnic_cmd_args cmd;
127 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_GET_NIC_INFO))
130 cmd.req.arg[1] = 0x2;
131 err = qlcnic_issue_cmd(adapter, &cmd);
133 dev_err(&adapter->pdev->dev,
134 "Failed to get PF info, err=%d\n", err);
138 npar_info->total_pf = cmd.rsp.arg[2] & 0xff;
139 npar_info->total_rss_engines = (cmd.rsp.arg[2] >> 8) & 0xff;
140 npar_info->max_vports = MSW(cmd.rsp.arg[2]);
141 npar_info->max_tx_ques = LSW(cmd.rsp.arg[3]);
142 npar_info->max_tx_mac_filters = MSW(cmd.rsp.arg[3]);
143 npar_info->max_rx_mcast_mac_filters = LSW(cmd.rsp.arg[4]);
144 npar_info->max_rx_ucast_mac_filters = MSW(cmd.rsp.arg[4]);
145 npar_info->max_rx_ip_addr = LSW(cmd.rsp.arg[5]);
146 npar_info->max_rx_lro_flow = MSW(cmd.rsp.arg[5]);
147 npar_info->max_rx_status_rings = LSW(cmd.rsp.arg[6]);
148 npar_info->max_rx_buf_rings = MSW(cmd.rsp.arg[6]);
149 npar_info->max_rx_ques = LSW(cmd.rsp.arg[7]);
150 npar_info->max_tx_vlan_keys = MSW(cmd.rsp.arg[7]);
151 npar_info->max_local_ipv6_addrs = LSW(cmd.rsp.arg[8]);
152 npar_info->max_remote_ipv6_addrs = MSW(cmd.rsp.arg[8]);
154 dev_info(&adapter->pdev->dev,
155 "\n\ttotal_pf: %d,\n"
156 "\n\ttotal_rss_engines: %d max_vports: %d max_tx_ques %d,\n"
157 "\tmax_tx_mac_filters: %d max_rx_mcast_mac_filters: %d,\n"
158 "\tmax_rx_ucast_mac_filters: 0x%x, max_rx_ip_addr: %d,\n"
159 "\tmax_rx_lro_flow: %d max_rx_status_rings: %d,\n"
160 "\tmax_rx_buf_rings: %d, max_rx_ques: %d, max_tx_vlan_keys %d\n"
161 "\tmax_local_ipv6_addrs: %d, max_remote_ipv6_addrs: %d\n",
162 npar_info->total_pf, npar_info->total_rss_engines,
163 npar_info->max_vports, npar_info->max_tx_ques,
164 npar_info->max_tx_mac_filters,
165 npar_info->max_rx_mcast_mac_filters,
166 npar_info->max_rx_ucast_mac_filters, npar_info->max_rx_ip_addr,
167 npar_info->max_rx_lro_flow, npar_info->max_rx_status_rings,
168 npar_info->max_rx_buf_rings, npar_info->max_rx_ques,
169 npar_info->max_tx_vlan_keys, npar_info->max_local_ipv6_addrs,
170 npar_info->max_remote_ipv6_addrs);
173 qlcnic_free_mbx_args(&cmd);
177 static void qlcnic_sriov_pf_reset_vport_handle(struct qlcnic_adapter *adapter,
180 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
181 struct qlcnic_vport *vp;
184 if (adapter->ahw->pci_func == func) {
185 sriov->vp_handle = 0;
187 index = qlcnic_sriov_func_to_index(adapter, func);
190 vp = sriov->vf_info[index].vp;
195 static void qlcnic_sriov_pf_set_vport_handle(struct qlcnic_adapter *adapter,
196 u16 vport_handle, u8 func)
198 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
199 struct qlcnic_vport *vp;
202 if (adapter->ahw->pci_func == func) {
203 sriov->vp_handle = vport_handle;
205 index = qlcnic_sriov_func_to_index(adapter, func);
208 vp = sriov->vf_info[index].vp;
209 vp->handle = vport_handle;
213 static int qlcnic_sriov_pf_get_vport_handle(struct qlcnic_adapter *adapter,
216 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
217 struct qlcnic_vf_info *vf_info;
220 if (adapter->ahw->pci_func == func) {
221 return sriov->vp_handle;
223 index = qlcnic_sriov_func_to_index(adapter, func);
225 vf_info = &sriov->vf_info[index];
226 return vf_info->vp->handle;
233 static int qlcnic_sriov_pf_config_vport(struct qlcnic_adapter *adapter,
236 struct qlcnic_cmd_args cmd;
240 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_CONFIG_VPORT))
244 cmd.req.arg[3] = func << 8;
246 vpid = qlcnic_sriov_pf_get_vport_handle(adapter, func);
251 cmd.req.arg[3] = ((vpid & 0xffff) << 8) | 1;
254 ret = qlcnic_issue_cmd(adapter, &cmd);
256 dev_err(&adapter->pdev->dev,
257 "Failed %s vport, err %d for func 0x%x\n",
258 (flag ? "enable" : "disable"), ret, func);
263 vpid = cmd.rsp.arg[2] & 0xffff;
264 qlcnic_sriov_pf_set_vport_handle(adapter, vpid, func);
266 qlcnic_sriov_pf_reset_vport_handle(adapter, func);
270 qlcnic_free_mbx_args(&cmd);
274 static int qlcnic_sriov_pf_cfg_eswitch(struct qlcnic_adapter *adapter,
277 struct qlcnic_cmd_args cmd;
280 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_TOGGLE_ESWITCH))
283 cmd.req.arg[0] |= (3 << 29);
284 cmd.req.arg[1] = ((func & 0xf) << 2) | BIT_6 | BIT_1;
286 cmd.req.arg[1] |= BIT_0;
288 err = qlcnic_issue_cmd(adapter, &cmd);
290 if (err != QLCNIC_RCODE_SUCCESS) {
291 dev_err(&adapter->pdev->dev,
292 "Failed to enable sriov eswitch%d\n", err);
296 qlcnic_free_mbx_args(&cmd);
300 void qlcnic_sriov_pf_cleanup(struct qlcnic_adapter *adapter)
302 u8 func = adapter->ahw->pci_func;
304 if (!qlcnic_sriov_enable_check(adapter))
307 qlcnic_sriov_cfg_bc_intr(adapter, 0);
308 qlcnic_sriov_pf_config_vport(adapter, 0, func);
309 qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0);
310 __qlcnic_sriov_cleanup(adapter);
311 adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
312 clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
315 void qlcnic_sriov_pf_disable(struct qlcnic_adapter *adapter)
317 if (!qlcnic_sriov_pf_check(adapter))
320 if (!qlcnic_sriov_enable_check(adapter))
323 pci_disable_sriov(adapter->pdev);
324 netdev_info(adapter->netdev,
325 "SR-IOV is disabled successfully on port %d\n",
329 static int qlcnic_pci_sriov_disable(struct qlcnic_adapter *adapter)
331 struct net_device *netdev = adapter->netdev;
333 if (netif_running(netdev))
334 __qlcnic_down(adapter, netdev);
336 qlcnic_sriov_pf_disable(adapter);
338 qlcnic_sriov_pf_cleanup(adapter);
340 /* After disabling SRIOV re-init the driver in default mode
341 configure opmode based on op_mode of function
343 if (qlcnic_83xx_configure_opmode(adapter))
346 if (netif_running(netdev))
347 __qlcnic_up(adapter, netdev);
352 static int qlcnic_sriov_pf_init(struct qlcnic_adapter *adapter)
354 struct qlcnic_hardware_context *ahw = adapter->ahw;
355 struct qlcnic_info nic_info, pf_info, vp_info;
357 u8 func = ahw->pci_func;
359 if (!qlcnic_sriov_enable_check(adapter))
362 err = qlcnic_sriov_pf_cfg_eswitch(adapter, func, 1);
364 goto clear_sriov_enable;
366 err = qlcnic_sriov_pf_config_vport(adapter, 1, func);
368 goto disable_eswitch;
370 err = qlcnic_sriov_get_pf_info(adapter, &pf_info);
374 qlcnic_sriov_pf_set_ff_max_res(adapter, &pf_info);
376 err = qlcnic_get_nic_info(adapter, &nic_info, func);
380 err = qlcnic_sriov_pf_cal_res_limit(adapter, &vp_info, func);
384 err = qlcnic_sriov_cfg_bc_intr(adapter, 1);
388 ahw->physical_port = (u8) nic_info.phys_port;
389 ahw->switch_mode = nic_info.switch_mode;
390 ahw->max_mtu = nic_info.max_mtu;
391 ahw->capabilities = nic_info.capabilities;
392 ahw->nic_mode = QLC_83XX_SRIOV_MODE;
396 qlcnic_sriov_pf_config_vport(adapter, 0, func);
399 qlcnic_sriov_pf_cfg_eswitch(adapter, func, 0);
402 __qlcnic_sriov_cleanup(adapter);
403 adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
404 clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
408 static int qlcnic_sriov_pf_enable(struct qlcnic_adapter *adapter, int num_vfs)
412 if (!qlcnic_sriov_enable_check(adapter))
415 err = pci_enable_sriov(adapter->pdev, num_vfs);
417 qlcnic_sriov_pf_cleanup(adapter);
422 static int __qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter,
427 set_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
428 adapter->ahw->op_mode = QLCNIC_SRIOV_PF_FUNC;
430 if (qlcnic_sriov_init(adapter, num_vfs)) {
431 clear_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
432 adapter->ahw->op_mode = QLCNIC_MGMT_FUNC;
436 if (qlcnic_sriov_pf_init(adapter))
439 err = qlcnic_sriov_pf_enable(adapter, num_vfs);
443 static int qlcnic_pci_sriov_enable(struct qlcnic_adapter *adapter, int num_vfs)
445 struct net_device *netdev = adapter->netdev;
448 if (!(adapter->flags & QLCNIC_MSIX_ENABLED)) {
450 "SR-IOV cannot be enabled, when legacy interrupts are enabled\n");
454 if (netif_running(netdev))
455 __qlcnic_down(adapter, netdev);
457 err = __qlcnic_pci_sriov_enable(adapter, num_vfs);
459 netdev_info(netdev, "Failed to enable SR-IOV on port %d\n",
462 if (qlcnic_83xx_configure_opmode(adapter))
465 netdev_info(adapter->netdev,
466 "SR-IOV is enabled successfully on port %d\n",
469 if (netif_running(netdev))
470 __qlcnic_up(adapter, netdev);
476 int qlcnic_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
478 struct qlcnic_adapter *adapter = pci_get_drvdata(dev);
481 if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
485 err = qlcnic_pci_sriov_disable(adapter);
487 err = qlcnic_pci_sriov_enable(adapter, num_vfs);
489 clear_bit(__QLCNIC_RESETTING, &adapter->state);
493 static int qlcnic_sriov_set_vf_vport_info(struct qlcnic_adapter *adapter,
496 struct qlcnic_info defvp_info;
499 err = qlcnic_sriov_pf_cal_res_limit(adapter, &defvp_info, func);
506 static int qlcnic_sriov_pf_channel_cfg_cmd(struct qlcnic_bc_trans *trans,
507 struct qlcnic_cmd_args *cmd)
509 struct qlcnic_vf_info *vf = trans->vf;
510 struct qlcnic_adapter *adapter = vf->adapter;
512 u16 func = vf->pci_func;
514 cmd->rsp.arg[0] = trans->req_hdr->cmd_op;
515 cmd->rsp.arg[0] |= (1 << 16);
517 if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) {
518 err = qlcnic_sriov_pf_config_vport(adapter, 1, func);
520 err = qlcnic_sriov_set_vf_vport_info(adapter, func);
522 qlcnic_sriov_pf_config_vport(adapter, 0, func);
525 err = qlcnic_sriov_pf_config_vport(adapter, 0, func);
531 cmd->rsp.arg[0] |= (1 << 25);
533 if (trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT)
534 set_bit(QLC_BC_VF_STATE, &vf->state);
536 clear_bit(QLC_BC_VF_STATE, &vf->state);
541 cmd->rsp.arg[0] |= (2 << 25);
545 static const struct qlcnic_sriov_cmd_handler qlcnic_pf_bc_cmd_hdlr[] = {
546 [QLCNIC_BC_CMD_CHANNEL_INIT] = {&qlcnic_sriov_pf_channel_cfg_cmd},
547 [QLCNIC_BC_CMD_CHANNEL_TERM] = {&qlcnic_sriov_pf_channel_cfg_cmd},
550 void qlcnic_sriov_pf_process_bc_cmd(struct qlcnic_adapter *adapter,
551 struct qlcnic_bc_trans *trans,
552 struct qlcnic_cmd_args *cmd)
556 cmd_op = trans->req_hdr->cmd_op;
558 if (trans->req_hdr->op_type == QLC_BC_CMD) {
559 size = ARRAY_SIZE(qlcnic_pf_bc_cmd_hdlr);
561 qlcnic_pf_bc_cmd_hdlr[cmd_op].fn(trans, cmd);
566 cmd->rsp.arg[0] |= (0x9 << 25);