blob: d7107055ec6035bf206380d98bbd4670d06660cd [file] [log] [blame]
Rajesh Borundia02feda12013-03-29 05:46:33 +00001/*
2 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2013 QLogic Corporation
4 *
5 * See LICENSE.qlcnic for copyright and licensing details.
6 */
7
Harish Patila930a462015-08-07 07:17:02 -04008#include <linux/types.h>
9
Rajesh Borundia02feda12013-03-29 05:46:33 +000010#include "qlcnic_sriov.h"
11#include "qlcnic.h"
Rajesh Borundiaf8468332013-03-29 05:46:34 +000012#include "qlcnic_83xx_hw.h"
Rajesh Borundia02feda12013-03-29 05:46:33 +000013
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +000014#define QLC_BC_COMMAND 0
15#define QLC_BC_RESPONSE 1
16
17#define QLC_MBOX_RESP_TIMEOUT (10 * HZ)
18#define QLC_MBOX_CH_FREE_TIMEOUT (10 * HZ)
19
20#define QLC_BC_MSG 0
21#define QLC_BC_CFREE 1
Rajesh Borundia97d81052013-04-19 07:01:09 +000022#define QLC_BC_FLR 2
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +000023#define QLC_BC_HDR_SZ 16
24#define QLC_BC_PAYLOAD_SZ (1024 - QLC_BC_HDR_SZ)
25
Rajesh Borundia7cb03b22013-03-29 05:46:37 +000026#define QLC_DEFAULT_RCV_DESCRIPTORS_SRIOV_VF 2048
27#define QLC_DEFAULT_JUMBO_RCV_DESCRIPTORS_SRIOV_VF 512
28
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +000029#define QLC_83XX_VF_RESET_FAIL_THRESH 8
30#define QLC_BC_CMD_MAX_RETRY_CNT 5
31
Manish Chopra2b10d3e2016-08-03 04:02:02 -040032static void qlcnic_sriov_handle_async_issue_cmd(struct work_struct *work);
Rajesh Borundia91b72822013-04-19 07:01:12 +000033static void qlcnic_sriov_vf_free_mac_list(struct qlcnic_adapter *);
34static int qlcnic_sriov_alloc_bc_mbx_args(struct qlcnic_cmd_args *, u32);
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +000035static void qlcnic_sriov_vf_poll_dev_state(struct work_struct *);
36static void qlcnic_sriov_vf_cancel_fw_work(struct qlcnic_adapter *);
Rajesh Borundia97d81052013-04-19 07:01:09 +000037static void qlcnic_sriov_cleanup_transaction(struct qlcnic_bc_trans *);
Manish Choprae5c4e6c2013-08-02 00:57:40 -040038static int qlcnic_sriov_issue_cmd(struct qlcnic_adapter *,
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +000039 struct qlcnic_cmd_args *);
stephen hemminger21041402014-01-16 10:31:27 -080040static int qlcnic_sriov_channel_cfg_cmd(struct qlcnic_adapter *, u8);
Sucheta Chakraborty1267ff92013-05-23 21:04:29 +000041static void qlcnic_sriov_process_bc_cmd(struct work_struct *);
stephen hemminger21041402014-01-16 10:31:27 -080042static int qlcnic_sriov_vf_shutdown(struct pci_dev *);
43static int qlcnic_sriov_vf_resume(struct qlcnic_adapter *);
Rajesh Borundia74b7ba12014-05-09 02:51:30 -040044static int qlcnic_sriov_async_issue_cmd(struct qlcnic_adapter *,
45 struct qlcnic_cmd_args *);
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +000046
Rajesh Borundiaf8468332013-03-29 05:46:34 +000047static struct qlcnic_hardware_ops qlcnic_sriov_vf_hw_ops = {
48 .read_crb = qlcnic_83xx_read_crb,
49 .write_crb = qlcnic_83xx_write_crb,
50 .read_reg = qlcnic_83xx_rd_reg_indirect,
51 .write_reg = qlcnic_83xx_wrt_reg_indirect,
52 .get_mac_address = qlcnic_83xx_get_mac_address,
53 .setup_intr = qlcnic_83xx_setup_intr,
54 .alloc_mbx_args = qlcnic_83xx_alloc_mbx_args,
Manish Choprae5c4e6c2013-08-02 00:57:40 -040055 .mbx_cmd = qlcnic_sriov_issue_cmd,
Rajesh Borundiaf8468332013-03-29 05:46:34 +000056 .get_func_no = qlcnic_83xx_get_func_no,
57 .api_lock = qlcnic_83xx_cam_lock,
58 .api_unlock = qlcnic_83xx_cam_unlock,
59 .process_lb_rcv_ring_diag = qlcnic_83xx_process_rcv_ring_diag,
60 .create_rx_ctx = qlcnic_83xx_create_rx_ctx,
61 .create_tx_ctx = qlcnic_83xx_create_tx_ctx,
Rajesh Borundia7cb03b22013-03-29 05:46:37 +000062 .del_rx_ctx = qlcnic_83xx_del_rx_ctx,
63 .del_tx_ctx = qlcnic_83xx_del_tx_ctx,
Rajesh Borundiaf8468332013-03-29 05:46:34 +000064 .setup_link_event = qlcnic_83xx_setup_link_event,
65 .get_nic_info = qlcnic_83xx_get_nic_info,
66 .get_pci_info = qlcnic_83xx_get_pci_info,
67 .set_nic_info = qlcnic_83xx_set_nic_info,
68 .change_macvlan = qlcnic_83xx_sre_macaddr_change,
69 .napi_enable = qlcnic_83xx_napi_enable,
70 .napi_disable = qlcnic_83xx_napi_disable,
71 .config_intr_coal = qlcnic_83xx_config_intr_coal,
72 .config_rss = qlcnic_83xx_config_rss,
73 .config_hw_lro = qlcnic_83xx_config_hw_lro,
74 .config_promisc_mode = qlcnic_83xx_nic_set_promisc,
75 .change_l2_filter = qlcnic_83xx_change_l2_filter,
76 .get_board_info = qlcnic_83xx_get_port_info,
Rajesh Borundia91b72822013-04-19 07:01:12 +000077 .free_mac_list = qlcnic_sriov_vf_free_mac_list,
Manish chopra2cc57522014-01-23 17:18:30 -050078 .enable_sds_intr = qlcnic_83xx_enable_sds_intr,
79 .disable_sds_intr = qlcnic_83xx_disable_sds_intr,
Rajesh Borundiaf8468332013-03-29 05:46:34 +000080};
81
82static struct qlcnic_nic_template qlcnic_sriov_vf_ops = {
83 .config_bridged_mode = qlcnic_config_bridged_mode,
84 .config_led = qlcnic_config_led,
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +000085 .cancel_idc_work = qlcnic_sriov_vf_cancel_fw_work,
Rajesh Borundiaf8468332013-03-29 05:46:34 +000086 .napi_add = qlcnic_83xx_napi_add,
87 .napi_del = qlcnic_83xx_napi_del,
Rajesh Borundia486a5bc2013-06-22 04:12:06 -040088 .shutdown = qlcnic_sriov_vf_shutdown,
89 .resume = qlcnic_sriov_vf_resume,
Rajesh Borundiaf8468332013-03-29 05:46:34 +000090 .config_ipaddr = qlcnic_83xx_config_ipaddr,
91 .clear_legacy_intr = qlcnic_83xx_clear_legacy_intr,
92};
93
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +000094static const struct qlcnic_mailbox_metadata qlcnic_sriov_bc_mbx_tbl[] = {
95 {QLCNIC_BC_CMD_CHANNEL_INIT, 2, 2},
96 {QLCNIC_BC_CMD_CHANNEL_TERM, 2, 2},
Rajesh Borundia91b72822013-04-19 07:01:12 +000097 {QLCNIC_BC_CMD_GET_ACL, 3, 14},
98 {QLCNIC_BC_CMD_CFG_GUEST_VLAN, 2, 2},
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +000099};
100
101static inline bool qlcnic_sriov_bc_msg_check(u32 val)
102{
103 return (val & (1 << QLC_BC_MSG)) ? true : false;
104}
105
106static inline bool qlcnic_sriov_channel_free_check(u32 val)
107{
108 return (val & (1 << QLC_BC_CFREE)) ? true : false;
109}
110
Rajesh Borundia97d81052013-04-19 07:01:09 +0000111static inline bool qlcnic_sriov_flr_check(u32 val)
112{
113 return (val & (1 << QLC_BC_FLR)) ? true : false;
114}
115
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000116static inline u8 qlcnic_sriov_target_func_id(u32 val)
117{
118 return (val >> 4) & 0xff;
119}
120
121static int qlcnic_sriov_virtid_fn(struct qlcnic_adapter *adapter, int vf_id)
122{
123 struct pci_dev *dev = adapter->pdev;
124 int pos;
125 u16 stride, offset;
126
127 if (qlcnic_sriov_vf_check(adapter))
128 return 0;
129
130 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
131 pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &offset);
132 pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &stride);
133
134 return (dev->devfn + offset + stride * vf_id) & 0xff;
135}
136
Rajesh Borundia02feda12013-03-29 05:46:33 +0000137int qlcnic_sriov_init(struct qlcnic_adapter *adapter, int num_vfs)
138{
139 struct qlcnic_sriov *sriov;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000140 struct qlcnic_back_channel *bc;
141 struct workqueue_struct *wq;
142 struct qlcnic_vport *vp;
143 struct qlcnic_vf_info *vf;
144 int err, i;
Rajesh Borundia02feda12013-03-29 05:46:33 +0000145
146 if (!qlcnic_sriov_enable_check(adapter))
147 return -EIO;
148
149 sriov = kzalloc(sizeof(struct qlcnic_sriov), GFP_KERNEL);
150 if (!sriov)
151 return -ENOMEM;
152
153 adapter->ahw->sriov = sriov;
154 sriov->num_vfs = num_vfs;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000155 bc = &sriov->bc;
156 sriov->vf_info = kzalloc(sizeof(struct qlcnic_vf_info) *
157 num_vfs, GFP_KERNEL);
158 if (!sriov->vf_info) {
159 err = -ENOMEM;
160 goto qlcnic_free_sriov;
161 }
162
163 wq = create_singlethread_workqueue("bc-trans");
164 if (wq == NULL) {
165 err = -ENOMEM;
166 dev_err(&adapter->pdev->dev,
167 "Cannot create bc-trans workqueue\n");
168 goto qlcnic_free_vf_info;
169 }
170
171 bc->bc_trans_wq = wq;
172
Rajesh Borundiae8b508e2013-03-29 05:46:38 +0000173 wq = create_singlethread_workqueue("async");
174 if (wq == NULL) {
175 err = -ENOMEM;
176 dev_err(&adapter->pdev->dev, "Cannot create async workqueue\n");
177 goto qlcnic_destroy_trans_wq;
178 }
179
180 bc->bc_async_wq = wq;
Manish Chopra2b10d3e2016-08-03 04:02:02 -0400181 INIT_LIST_HEAD(&bc->async_cmd_list);
182 INIT_WORK(&bc->vf_async_work, qlcnic_sriov_handle_async_issue_cmd);
183 spin_lock_init(&bc->queue_lock);
184 bc->adapter = adapter;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +0000185
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000186 for (i = 0; i < num_vfs; i++) {
187 vf = &sriov->vf_info[i];
188 vf->adapter = adapter;
189 vf->pci_func = qlcnic_sriov_virtid_fn(adapter, i);
190 mutex_init(&vf->send_cmd_lock);
Rajesh Borundia74b7ba12014-05-09 02:51:30 -0400191 spin_lock_init(&vf->vlan_list_lock);
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000192 INIT_LIST_HEAD(&vf->rcv_act.wait_list);
193 INIT_LIST_HEAD(&vf->rcv_pend.wait_list);
194 spin_lock_init(&vf->rcv_act.lock);
195 spin_lock_init(&vf->rcv_pend.lock);
196 init_completion(&vf->ch_free_cmpl);
197
Sucheta Chakraborty1267ff92013-05-23 21:04:29 +0000198 INIT_WORK(&vf->trans_work, qlcnic_sriov_process_bc_cmd);
199
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000200 if (qlcnic_sriov_pf_check(adapter)) {
201 vp = kzalloc(sizeof(struct qlcnic_vport), GFP_KERNEL);
202 if (!vp) {
203 err = -ENOMEM;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +0000204 goto qlcnic_destroy_async_wq;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000205 }
206 sriov->vf_info[i].vp = vp;
Rajesh Borundiad747c332014-05-09 02:51:31 -0400207 vp->vlan_mode = QLC_GUEST_VLAN_MODE;
Rajesh Borundia4000e7a2013-04-19 07:01:11 +0000208 vp->max_tx_bw = MAX_BW;
Sucheta Chakrabortyed616682014-05-22 09:59:05 -0400209 vp->min_tx_bw = MIN_BW;
Rajesh Borundia132a3f22014-05-09 02:51:29 -0400210 vp->spoofchk = false;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000211 random_ether_addr(vp->mac);
212 dev_info(&adapter->pdev->dev,
213 "MAC Address %pM is configured for VF %d\n",
214 vp->mac, i);
215 }
216 }
217
Rajesh Borundia02feda12013-03-29 05:46:33 +0000218 return 0;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000219
Rajesh Borundiae8b508e2013-03-29 05:46:38 +0000220qlcnic_destroy_async_wq:
221 destroy_workqueue(bc->bc_async_wq);
222
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000223qlcnic_destroy_trans_wq:
224 destroy_workqueue(bc->bc_trans_wq);
225
226qlcnic_free_vf_info:
227 kfree(sriov->vf_info);
228
229qlcnic_free_sriov:
230 kfree(adapter->ahw->sriov);
231 return err;
Rajesh Borundia02feda12013-03-29 05:46:33 +0000232}
233
Rajesh Borundia97d81052013-04-19 07:01:09 +0000234void qlcnic_sriov_cleanup_list(struct qlcnic_trans_list *t_list)
235{
236 struct qlcnic_bc_trans *trans;
237 struct qlcnic_cmd_args cmd;
238 unsigned long flags;
239
240 spin_lock_irqsave(&t_list->lock, flags);
241
242 while (!list_empty(&t_list->wait_list)) {
243 trans = list_first_entry(&t_list->wait_list,
244 struct qlcnic_bc_trans, list);
245 list_del(&trans->list);
246 t_list->count--;
247 cmd.req.arg = (u32 *)trans->req_pay;
248 cmd.rsp.arg = (u32 *)trans->rsp_pay;
249 qlcnic_free_mbx_args(&cmd);
250 qlcnic_sriov_cleanup_transaction(trans);
251 }
252
253 spin_unlock_irqrestore(&t_list->lock, flags);
254}
255
Rajesh Borundia02feda12013-03-29 05:46:33 +0000256void __qlcnic_sriov_cleanup(struct qlcnic_adapter *adapter)
257{
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000258 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
259 struct qlcnic_back_channel *bc = &sriov->bc;
Rajesh Borundia97d81052013-04-19 07:01:09 +0000260 struct qlcnic_vf_info *vf;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000261 int i;
262
Rajesh Borundia02feda12013-03-29 05:46:33 +0000263 if (!qlcnic_sriov_enable_check(adapter))
264 return;
265
Rajesh Borundiae8b508e2013-03-29 05:46:38 +0000266 qlcnic_sriov_cleanup_async_list(bc);
267 destroy_workqueue(bc->bc_async_wq);
Rajesh Borundia97d81052013-04-19 07:01:09 +0000268
269 for (i = 0; i < sriov->num_vfs; i++) {
270 vf = &sriov->vf_info[i];
271 qlcnic_sriov_cleanup_list(&vf->rcv_pend);
272 cancel_work_sync(&vf->trans_work);
273 qlcnic_sriov_cleanup_list(&vf->rcv_act);
274 }
275
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000276 destroy_workqueue(bc->bc_trans_wq);
277
278 for (i = 0; i < sriov->num_vfs; i++)
279 kfree(sriov->vf_info[i].vp);
280
281 kfree(sriov->vf_info);
Rajesh Borundia02feda12013-03-29 05:46:33 +0000282 kfree(adapter->ahw->sriov);
283}
284
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000285static void qlcnic_sriov_vf_cleanup(struct qlcnic_adapter *adapter)
286{
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000287 qlcnic_sriov_channel_cfg_cmd(adapter, QLCNIC_BC_CMD_CHANNEL_TERM);
288 qlcnic_sriov_cfg_bc_intr(adapter, 0);
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000289 __qlcnic_sriov_cleanup(adapter);
290}
291
Rajesh Borundia02feda12013-03-29 05:46:33 +0000292void qlcnic_sriov_cleanup(struct qlcnic_adapter *adapter)
293{
Manish Chopra6e1f5862014-01-10 11:48:57 -0500294 if (!test_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state))
Manish Chopra154d0c82013-12-17 09:01:53 -0500295 return;
296
297 qlcnic_sriov_free_vlans(adapter);
298
Rajesh Borundia02feda12013-03-29 05:46:33 +0000299 if (qlcnic_sriov_pf_check(adapter))
300 qlcnic_sriov_pf_cleanup(adapter);
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000301
302 if (qlcnic_sriov_vf_check(adapter))
303 qlcnic_sriov_vf_cleanup(adapter);
304}
305
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000306static int qlcnic_sriov_post_bc_msg(struct qlcnic_adapter *adapter, u32 *hdr,
307 u32 *pay, u8 pci_func, u8 size)
308{
309 struct qlcnic_hardware_context *ahw = adapter->ahw;
Manish Chopra068a8d12013-08-02 00:57:41 -0400310 struct qlcnic_mailbox *mbx = ahw->mailbox;
311 struct qlcnic_cmd_args cmd;
312 unsigned long timeout;
313 int err;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000314
Manish Chopra068a8d12013-08-02 00:57:41 -0400315 memset(&cmd, 0, sizeof(struct qlcnic_cmd_args));
316 cmd.hdr = hdr;
317 cmd.pay = pay;
318 cmd.pay_size = size;
319 cmd.func_num = pci_func;
320 cmd.op_type = QLC_83XX_MBX_POST_BC_OP;
321 cmd.cmd_op = ((struct qlcnic_bc_hdr *)hdr)->cmd_op;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000322
Manish Chopra068a8d12013-08-02 00:57:41 -0400323 err = mbx->ops->enqueue_cmd(adapter, &cmd, &timeout);
324 if (err) {
325 dev_err(&adapter->pdev->dev,
326 "%s: Mailbox not available, cmd_op=0x%x, cmd_type=0x%x, pci_func=0x%x, op_mode=0x%x\n",
327 __func__, cmd.cmd_op, cmd.type, ahw->pci_func,
328 ahw->op_mode);
329 return err;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000330 }
331
Manish Chopra068a8d12013-08-02 00:57:41 -0400332 if (!wait_for_completion_timeout(&cmd.completion, timeout)) {
333 dev_err(&adapter->pdev->dev,
334 "%s: Mailbox command timed out, cmd_op=0x%x, cmd_type=0x%x, pci_func=0x%x, op_mode=0x%x\n",
335 __func__, cmd.cmd_op, cmd.type, ahw->pci_func,
336 ahw->op_mode);
337 flush_workqueue(mbx->work_q);
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000338 }
339
Manish Chopra068a8d12013-08-02 00:57:41 -0400340 return cmd.rsp_opcode;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000341}
342
Rajesh Borundia7cb03b22013-03-29 05:46:37 +0000343static void qlcnic_sriov_vf_cfg_buff_desc(struct qlcnic_adapter *adapter)
344{
345 adapter->num_rxd = QLC_DEFAULT_RCV_DESCRIPTORS_SRIOV_VF;
346 adapter->max_rxd = MAX_RCV_DESCRIPTORS_10G;
347 adapter->num_jumbo_rxd = QLC_DEFAULT_JUMBO_RCV_DESCRIPTORS_SRIOV_VF;
348 adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
349 adapter->num_txd = MAX_CMD_DESCRIPTORS;
350 adapter->max_rds_rings = MAX_RDS_RINGS;
351}
352
Rajesh Borundia4000e7a2013-04-19 07:01:11 +0000353int qlcnic_sriov_get_vf_vport_info(struct qlcnic_adapter *adapter,
354 struct qlcnic_info *npar_info, u16 vport_id)
355{
356 struct device *dev = &adapter->pdev->dev;
357 struct qlcnic_cmd_args cmd;
358 int err;
359 u32 status;
360
361 err = qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_GET_NIC_INFO);
362 if (err)
363 return err;
364
365 cmd.req.arg[1] = vport_id << 16 | 0x1;
366 err = qlcnic_issue_cmd(adapter, &cmd);
367 if (err) {
368 dev_err(&adapter->pdev->dev,
369 "Failed to get vport info, err=%d\n", err);
370 qlcnic_free_mbx_args(&cmd);
371 return err;
372 }
373
374 status = cmd.rsp.arg[2] & 0xffff;
375 if (status & BIT_0)
376 npar_info->min_tx_bw = MSW(cmd.rsp.arg[2]);
377 if (status & BIT_1)
378 npar_info->max_tx_bw = LSW(cmd.rsp.arg[3]);
379 if (status & BIT_2)
380 npar_info->max_tx_ques = MSW(cmd.rsp.arg[3]);
381 if (status & BIT_3)
382 npar_info->max_tx_mac_filters = LSW(cmd.rsp.arg[4]);
383 if (status & BIT_4)
384 npar_info->max_rx_mcast_mac_filters = MSW(cmd.rsp.arg[4]);
385 if (status & BIT_5)
386 npar_info->max_rx_ucast_mac_filters = LSW(cmd.rsp.arg[5]);
387 if (status & BIT_6)
388 npar_info->max_rx_ip_addr = MSW(cmd.rsp.arg[5]);
389 if (status & BIT_7)
390 npar_info->max_rx_lro_flow = LSW(cmd.rsp.arg[6]);
391 if (status & BIT_8)
392 npar_info->max_rx_status_rings = MSW(cmd.rsp.arg[6]);
393 if (status & BIT_9)
394 npar_info->max_rx_buf_rings = LSW(cmd.rsp.arg[7]);
395
396 npar_info->max_rx_ques = MSW(cmd.rsp.arg[7]);
397 npar_info->max_tx_vlan_keys = LSW(cmd.rsp.arg[8]);
398 npar_info->max_local_ipv6_addrs = MSW(cmd.rsp.arg[8]);
399 npar_info->max_remote_ipv6_addrs = LSW(cmd.rsp.arg[9]);
400
401 dev_info(dev, "\n\tmin_tx_bw: %d, max_tx_bw: %d max_tx_ques: %d,\n"
402 "\tmax_tx_mac_filters: %d max_rx_mcast_mac_filters: %d,\n"
403 "\tmax_rx_ucast_mac_filters: 0x%x, max_rx_ip_addr: %d,\n"
404 "\tmax_rx_lro_flow: %d max_rx_status_rings: %d,\n"
405 "\tmax_rx_buf_rings: %d, max_rx_ques: %d, max_tx_vlan_keys %d\n"
406 "\tlocal_ipv6_addr: %d, remote_ipv6_addr: %d\n",
407 npar_info->min_tx_bw, npar_info->max_tx_bw,
408 npar_info->max_tx_ques, npar_info->max_tx_mac_filters,
409 npar_info->max_rx_mcast_mac_filters,
410 npar_info->max_rx_ucast_mac_filters, npar_info->max_rx_ip_addr,
411 npar_info->max_rx_lro_flow, npar_info->max_rx_status_rings,
412 npar_info->max_rx_buf_rings, npar_info->max_rx_ques,
413 npar_info->max_tx_vlan_keys, npar_info->max_local_ipv6_addrs,
414 npar_info->max_remote_ipv6_addrs);
415
416 qlcnic_free_mbx_args(&cmd);
417 return err;
418}
419
Rajesh Borundia91b72822013-04-19 07:01:12 +0000420static int qlcnic_sriov_set_pvid_mode(struct qlcnic_adapter *adapter,
Manish Chopra991ca262013-08-30 13:51:16 -0400421 struct qlcnic_cmd_args *cmd)
Rajesh Borundia91b72822013-04-19 07:01:12 +0000422{
Manish Chopra991ca262013-08-30 13:51:16 -0400423 adapter->rx_pvid = MSW(cmd->rsp.arg[1]) & 0xffff;
424 adapter->flags &= ~QLCNIC_TAGGING_ENABLED;
Rajesh Borundia91b72822013-04-19 07:01:12 +0000425 return 0;
426}
427
428static int qlcnic_sriov_set_guest_vlan_mode(struct qlcnic_adapter *adapter,
429 struct qlcnic_cmd_args *cmd)
430{
431 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
432 int i, num_vlans;
433 u16 *vlans;
434
435 if (sriov->allowed_vlans)
436 return 0;
437
438 sriov->any_vlan = cmd->rsp.arg[2] & 0xf;
Manish Chopra154d0c82013-12-17 09:01:53 -0500439 sriov->num_allowed_vlans = cmd->rsp.arg[2] >> 16;
440 dev_info(&adapter->pdev->dev, "Number of allowed Guest VLANs = %d\n",
441 sriov->num_allowed_vlans);
442
443 qlcnic_sriov_alloc_vlans(adapter);
444
Rajesh Borundia91b72822013-04-19 07:01:12 +0000445 if (!sriov->any_vlan)
446 return 0;
447
Rajesh Borundia91b72822013-04-19 07:01:12 +0000448 num_vlans = sriov->num_allowed_vlans;
449 sriov->allowed_vlans = kzalloc(sizeof(u16) * num_vlans, GFP_KERNEL);
450 if (!sriov->allowed_vlans)
451 return -ENOMEM;
452
453 vlans = (u16 *)&cmd->rsp.arg[3];
454 for (i = 0; i < num_vlans; i++)
455 sriov->allowed_vlans[i] = vlans[i];
456
457 return 0;
458}
459
Rajesh Borundiabcf6cb12014-01-28 11:55:28 -0500460static int qlcnic_sriov_get_vf_acl(struct qlcnic_adapter *adapter)
Rajesh Borundia91b72822013-04-19 07:01:12 +0000461{
462 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
463 struct qlcnic_cmd_args cmd;
Manish Chopra991ca262013-08-30 13:51:16 -0400464 int ret = 0;
Rajesh Borundia91b72822013-04-19 07:01:12 +0000465
Rajesh Borundiac5316922014-06-04 03:40:54 -0400466 memset(&cmd, 0, sizeof(cmd));
Rajesh Borundia91b72822013-04-19 07:01:12 +0000467 ret = qlcnic_sriov_alloc_bc_mbx_args(&cmd, QLCNIC_BC_CMD_GET_ACL);
468 if (ret)
469 return ret;
470
471 ret = qlcnic_issue_cmd(adapter, &cmd);
472 if (ret) {
473 dev_err(&adapter->pdev->dev, "Failed to get ACL, err=%d\n",
474 ret);
475 } else {
476 sriov->vlan_mode = cmd.rsp.arg[1] & 0x3;
477 switch (sriov->vlan_mode) {
478 case QLC_GUEST_VLAN_MODE:
479 ret = qlcnic_sriov_set_guest_vlan_mode(adapter, &cmd);
480 break;
481 case QLC_PVID_MODE:
Manish Chopra991ca262013-08-30 13:51:16 -0400482 ret = qlcnic_sriov_set_pvid_mode(adapter, &cmd);
Rajesh Borundia91b72822013-04-19 07:01:12 +0000483 break;
484 }
485 }
486
487 qlcnic_free_mbx_args(&cmd);
488 return ret;
489}
490
Rajesh Borundia7cb03b22013-03-29 05:46:37 +0000491static int qlcnic_sriov_vf_init_driver(struct qlcnic_adapter *adapter)
492{
Rajesh Borundia7cb03b22013-03-29 05:46:37 +0000493 struct qlcnic_hardware_context *ahw = adapter->ahw;
Manish Chopra068a8d12013-08-02 00:57:41 -0400494 struct qlcnic_info nic_info;
Rajesh Borundia7cb03b22013-03-29 05:46:37 +0000495 int err;
496
Rajesh Borundia4000e7a2013-04-19 07:01:11 +0000497 err = qlcnic_sriov_get_vf_vport_info(adapter, &nic_info, 0);
498 if (err)
499 return err;
500
Manish Chopra154d0c82013-12-17 09:01:53 -0500501 ahw->max_mc_count = nic_info.max_rx_mcast_mac_filters;
502
Rajesh Borundia7cb03b22013-03-29 05:46:37 +0000503 err = qlcnic_get_nic_info(adapter, &nic_info, ahw->pci_func);
504 if (err)
505 return -EIO;
506
507 if (qlcnic_83xx_get_port_info(adapter))
508 return -EIO;
509
510 qlcnic_sriov_vf_cfg_buff_desc(adapter);
511 adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
512 dev_info(&adapter->pdev->dev, "HAL Version: %d\n",
513 adapter->ahw->fw_hal_version);
514
515 ahw->physical_port = (u8) nic_info.phys_port;
516 ahw->switch_mode = nic_info.switch_mode;
517 ahw->max_mtu = nic_info.max_mtu;
518 ahw->op_mode = nic_info.op_mode;
519 ahw->capabilities = nic_info.capabilities;
520 return 0;
521}
522
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000523static int qlcnic_sriov_setup_vf(struct qlcnic_adapter *adapter,
524 int pci_using_dac)
525{
526 int err;
527
Rajesh Borundiad747c332014-05-09 02:51:31 -0400528 adapter->flags |= QLCNIC_VLAN_FILTERING;
529 adapter->ahw->total_nic_func = 1;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +0000530 INIT_LIST_HEAD(&adapter->vf_mc_list);
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000531 if (!qlcnic_use_msi_x && !!qlcnic_use_msi)
532 dev_warn(&adapter->pdev->dev,
Pratik Pujar01b91f42013-08-02 23:15:57 -0400533 "Device does not support MSI interrupts\n");
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000534
Himanshu Madhani34e8c402013-11-04 13:31:31 -0500535 /* compute and set default and max tx/sds rings */
536 qlcnic_set_tx_ring_count(adapter, QLCNIC_SINGLE_RING);
537 qlcnic_set_sds_ring_count(adapter, QLCNIC_SINGLE_RING);
538
539 err = qlcnic_setup_intr(adapter);
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000540 if (err) {
541 dev_err(&adapter->pdev->dev, "Failed to setup interrupt\n");
542 goto err_out_disable_msi;
543 }
544
545 err = qlcnic_83xx_setup_mbx_intr(adapter);
546 if (err)
547 goto err_out_disable_msi;
548
549 err = qlcnic_sriov_init(adapter, 1);
550 if (err)
551 goto err_out_disable_mbx_intr;
552
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000553 err = qlcnic_sriov_cfg_bc_intr(adapter, 1);
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000554 if (err)
555 goto err_out_cleanup_sriov;
556
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000557 err = qlcnic_sriov_channel_cfg_cmd(adapter, QLCNIC_BC_CMD_CHANNEL_INIT);
558 if (err)
559 goto err_out_disable_bc_intr;
560
Rajesh Borundia7cb03b22013-03-29 05:46:37 +0000561 err = qlcnic_sriov_vf_init_driver(adapter);
562 if (err)
563 goto err_out_send_channel_term;
564
Rajesh Borundiabcf6cb12014-01-28 11:55:28 -0500565 err = qlcnic_sriov_get_vf_acl(adapter);
566 if (err)
567 goto err_out_send_channel_term;
568
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000569 err = qlcnic_setup_netdev(adapter, adapter->netdev, pci_using_dac);
570 if (err)
571 goto err_out_send_channel_term;
572
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000573 pci_set_drvdata(adapter->pdev, adapter);
574 dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
575 adapter->netdev->name);
Sucheta Chakraborty14d385b2013-08-23 13:38:25 -0400576
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +0000577 qlcnic_schedule_work(adapter, qlcnic_sriov_vf_poll_dev_state,
578 adapter->ahw->idc.delay);
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000579 return 0;
580
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000581err_out_send_channel_term:
582 qlcnic_sriov_channel_cfg_cmd(adapter, QLCNIC_BC_CMD_CHANNEL_TERM);
583
584err_out_disable_bc_intr:
585 qlcnic_sriov_cfg_bc_intr(adapter, 0);
586
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000587err_out_cleanup_sriov:
588 __qlcnic_sriov_cleanup(adapter);
589
590err_out_disable_mbx_intr:
591 qlcnic_83xx_free_mbx_intr(adapter);
592
593err_out_disable_msi:
594 qlcnic_teardown_intr(adapter);
595 return err;
596}
597
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +0000598static int qlcnic_sriov_check_dev_ready(struct qlcnic_adapter *adapter)
599{
600 u32 state;
601
602 do {
603 msleep(20);
604 if (++adapter->fw_fail_cnt > QLC_BC_CMD_MAX_RETRY_CNT)
605 return -EIO;
606 state = QLCRDX(adapter->ahw, QLC_83XX_IDC_DEV_STATE);
607 } while (state != QLC_83XX_IDC_DEV_READY);
608
609 return 0;
610}
611
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000612int qlcnic_sriov_vf_init(struct qlcnic_adapter *adapter, int pci_using_dac)
613{
614 struct qlcnic_hardware_context *ahw = adapter->ahw;
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +0000615 int err;
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000616
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +0000617 set_bit(QLC_83XX_MODULE_LOADED, &ahw->idc.status);
618 ahw->idc.delay = QLC_83XX_IDC_FW_POLL_DELAY;
619 ahw->reset_context = 0;
620 adapter->fw_fail_cnt = 0;
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000621 ahw->msix_supported = 1;
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +0000622 adapter->need_fw_reset = 0;
Rajesh Borundiada6c8062013-03-29 05:46:35 +0000623 adapter->flags |= QLCNIC_TX_INTR_SHARED;
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000624
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +0000625 err = qlcnic_sriov_check_dev_ready(adapter);
626 if (err)
627 return err;
628
629 err = qlcnic_sriov_setup_vf(adapter, pci_using_dac);
630 if (err)
631 return err;
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000632
633 if (qlcnic_read_mac_addr(adapter))
634 dev_warn(&adapter->pdev->dev, "failed to read mac addr\n");
635
Sucheta Chakraborty1267ff92013-05-23 21:04:29 +0000636 INIT_DELAYED_WORK(&adapter->idc_aen_work, qlcnic_83xx_idc_aen_work);
637
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000638 clear_bit(__QLCNIC_RESETTING, &adapter->state);
Rajesh Borundiaf8468332013-03-29 05:46:34 +0000639 return 0;
640}
641
642void qlcnic_sriov_vf_set_ops(struct qlcnic_adapter *adapter)
643{
644 struct qlcnic_hardware_context *ahw = adapter->ahw;
645
646 ahw->op_mode = QLCNIC_SRIOV_VF_FUNC;
647 dev_info(&adapter->pdev->dev,
648 "HAL Version: %d Non Privileged SRIOV function\n",
649 ahw->fw_hal_version);
650 adapter->nic_ops = &qlcnic_sriov_vf_ops;
651 set_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state);
652 return;
653}
654
655void qlcnic_sriov_vf_register_map(struct qlcnic_hardware_context *ahw)
656{
657 ahw->hw_ops = &qlcnic_sriov_vf_hw_ops;
658 ahw->reg_tbl = (u32 *)qlcnic_83xx_reg_tbl;
659 ahw->ext_reg_tbl = (u32 *)qlcnic_83xx_ext_reg_tbl;
Rajesh Borundia02feda12013-03-29 05:46:33 +0000660}
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000661
662static u32 qlcnic_sriov_get_bc_paysize(u32 real_pay_size, u8 curr_frag)
663{
664 u32 pay_size;
665
666 pay_size = real_pay_size / ((curr_frag + 1) * QLC_BC_PAYLOAD_SZ);
667
668 if (pay_size)
669 pay_size = QLC_BC_PAYLOAD_SZ;
670 else
671 pay_size = real_pay_size % QLC_BC_PAYLOAD_SZ;
672
673 return pay_size;
674}
675
676int qlcnic_sriov_func_to_index(struct qlcnic_adapter *adapter, u8 pci_func)
677{
678 struct qlcnic_vf_info *vf_info = adapter->ahw->sriov->vf_info;
679 u8 i;
680
681 if (qlcnic_sriov_vf_check(adapter))
682 return 0;
683
684 for (i = 0; i < adapter->ahw->sriov->num_vfs; i++) {
685 if (vf_info[i].pci_func == pci_func)
686 return i;
687 }
688
689 return -EINVAL;
690}
691
692static inline int qlcnic_sriov_alloc_bc_trans(struct qlcnic_bc_trans **trans)
693{
694 *trans = kzalloc(sizeof(struct qlcnic_bc_trans), GFP_ATOMIC);
695 if (!*trans)
696 return -ENOMEM;
697
698 init_completion(&(*trans)->resp_cmpl);
699 return 0;
700}
701
702static inline int qlcnic_sriov_alloc_bc_msg(struct qlcnic_bc_hdr **hdr,
703 u32 size)
704{
705 *hdr = kzalloc(sizeof(struct qlcnic_bc_hdr) * size, GFP_ATOMIC);
706 if (!*hdr)
707 return -ENOMEM;
708
709 return 0;
710}
711
712static int qlcnic_sriov_alloc_bc_mbx_args(struct qlcnic_cmd_args *mbx, u32 type)
713{
714 const struct qlcnic_mailbox_metadata *mbx_tbl;
715 int i, size;
716
717 mbx_tbl = qlcnic_sriov_bc_mbx_tbl;
718 size = ARRAY_SIZE(qlcnic_sriov_bc_mbx_tbl);
719
720 for (i = 0; i < size; i++) {
721 if (type == mbx_tbl[i].cmd) {
722 mbx->op_type = QLC_BC_CMD;
723 mbx->req.num = mbx_tbl[i].in_args;
724 mbx->rsp.num = mbx_tbl[i].out_args;
725 mbx->req.arg = kcalloc(mbx->req.num, sizeof(u32),
726 GFP_ATOMIC);
727 if (!mbx->req.arg)
728 return -ENOMEM;
729 mbx->rsp.arg = kcalloc(mbx->rsp.num, sizeof(u32),
730 GFP_ATOMIC);
731 if (!mbx->rsp.arg) {
732 kfree(mbx->req.arg);
733 mbx->req.arg = NULL;
734 return -ENOMEM;
735 }
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000736 mbx->req.arg[0] = (type | (mbx->req.num << 16) |
737 (3 << 29));
Pratik Pujar62262042013-07-26 16:24:01 -0400738 mbx->rsp.arg[0] = (type & 0xffff) | mbx->rsp.num << 16;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000739 return 0;
740 }
741 }
742 return -EINVAL;
743}
744
745static int qlcnic_sriov_prepare_bc_hdr(struct qlcnic_bc_trans *trans,
746 struct qlcnic_cmd_args *cmd,
747 u16 seq, u8 msg_type)
748{
749 struct qlcnic_bc_hdr *hdr;
750 int i;
751 u32 num_regs, bc_pay_sz;
752 u16 remainder;
753 u8 cmd_op, num_frags, t_num_frags;
754
755 bc_pay_sz = QLC_BC_PAYLOAD_SZ;
756 if (msg_type == QLC_BC_COMMAND) {
757 trans->req_pay = (struct qlcnic_bc_payload *)cmd->req.arg;
758 trans->rsp_pay = (struct qlcnic_bc_payload *)cmd->rsp.arg;
759 num_regs = cmd->req.num;
760 trans->req_pay_size = (num_regs * 4);
761 num_regs = cmd->rsp.num;
762 trans->rsp_pay_size = (num_regs * 4);
763 cmd_op = cmd->req.arg[0] & 0xff;
764 remainder = (trans->req_pay_size) % (bc_pay_sz);
765 num_frags = (trans->req_pay_size) / (bc_pay_sz);
766 if (remainder)
767 num_frags++;
768 t_num_frags = num_frags;
769 if (qlcnic_sriov_alloc_bc_msg(&trans->req_hdr, num_frags))
770 return -ENOMEM;
771 remainder = (trans->rsp_pay_size) % (bc_pay_sz);
772 num_frags = (trans->rsp_pay_size) / (bc_pay_sz);
773 if (remainder)
774 num_frags++;
775 if (qlcnic_sriov_alloc_bc_msg(&trans->rsp_hdr, num_frags))
776 return -ENOMEM;
777 num_frags = t_num_frags;
778 hdr = trans->req_hdr;
779 } else {
780 cmd->req.arg = (u32 *)trans->req_pay;
781 cmd->rsp.arg = (u32 *)trans->rsp_pay;
782 cmd_op = cmd->req.arg[0] & 0xff;
Rajesh Borundiad747c332014-05-09 02:51:31 -0400783 cmd->cmd_op = cmd_op;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000784 remainder = (trans->rsp_pay_size) % (bc_pay_sz);
785 num_frags = (trans->rsp_pay_size) / (bc_pay_sz);
786 if (remainder)
787 num_frags++;
788 cmd->req.num = trans->req_pay_size / 4;
789 cmd->rsp.num = trans->rsp_pay_size / 4;
790 hdr = trans->rsp_hdr;
Pratik Pujar62262042013-07-26 16:24:01 -0400791 cmd->op_type = trans->req_hdr->op_type;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000792 }
793
794 trans->trans_id = seq;
795 trans->cmd_id = cmd_op;
796 for (i = 0; i < num_frags; i++) {
797 hdr[i].version = 2;
798 hdr[i].msg_type = msg_type;
799 hdr[i].op_type = cmd->op_type;
800 hdr[i].num_cmds = 1;
801 hdr[i].num_frags = num_frags;
802 hdr[i].frag_num = i + 1;
803 hdr[i].cmd_op = cmd_op;
804 hdr[i].seq_id = seq;
805 }
806 return 0;
807}
808
809static void qlcnic_sriov_cleanup_transaction(struct qlcnic_bc_trans *trans)
810{
811 if (!trans)
812 return;
813 kfree(trans->req_hdr);
814 kfree(trans->rsp_hdr);
815 kfree(trans);
816}
817
818static int qlcnic_sriov_clear_trans(struct qlcnic_vf_info *vf,
819 struct qlcnic_bc_trans *trans, u8 type)
820{
821 struct qlcnic_trans_list *t_list;
822 unsigned long flags;
823 int ret = 0;
824
825 if (type == QLC_BC_RESPONSE) {
826 t_list = &vf->rcv_act;
827 spin_lock_irqsave(&t_list->lock, flags);
828 t_list->count--;
829 list_del(&trans->list);
830 if (t_list->count > 0)
831 ret = 1;
832 spin_unlock_irqrestore(&t_list->lock, flags);
833 }
834 if (type == QLC_BC_COMMAND) {
835 while (test_and_set_bit(QLC_BC_VF_SEND, &vf->state))
836 msleep(100);
837 vf->send_cmd = NULL;
838 clear_bit(QLC_BC_VF_SEND, &vf->state);
839 }
840 return ret;
841}
842
843static void qlcnic_sriov_schedule_bc_cmd(struct qlcnic_sriov *sriov,
844 struct qlcnic_vf_info *vf,
845 work_func_t func)
846{
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +0000847 if (test_bit(QLC_BC_VF_FLR, &vf->state) ||
848 vf->adapter->need_fw_reset)
Rajesh Borundia97d81052013-04-19 07:01:09 +0000849 return;
850
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000851 queue_work(sriov->bc.bc_trans_wq, &vf->trans_work);
852}
853
854static inline void qlcnic_sriov_wait_for_resp(struct qlcnic_bc_trans *trans)
855{
856 struct completion *cmpl = &trans->resp_cmpl;
857
858 if (wait_for_completion_timeout(cmpl, QLC_MBOX_RESP_TIMEOUT))
859 trans->trans_state = QLC_END;
860 else
861 trans->trans_state = QLC_ABORT;
862
863 return;
864}
865
866static void qlcnic_sriov_handle_multi_frags(struct qlcnic_bc_trans *trans,
867 u8 type)
868{
869 if (type == QLC_BC_RESPONSE) {
870 trans->curr_rsp_frag++;
871 if (trans->curr_rsp_frag < trans->rsp_hdr->num_frags)
872 trans->trans_state = QLC_INIT;
873 else
874 trans->trans_state = QLC_END;
875 } else {
876 trans->curr_req_frag++;
877 if (trans->curr_req_frag < trans->req_hdr->num_frags)
878 trans->trans_state = QLC_INIT;
879 else
880 trans->trans_state = QLC_WAIT_FOR_RESP;
881 }
882}
883
884static void qlcnic_sriov_wait_for_channel_free(struct qlcnic_bc_trans *trans,
885 u8 type)
886{
887 struct qlcnic_vf_info *vf = trans->vf;
888 struct completion *cmpl = &vf->ch_free_cmpl;
889
890 if (!wait_for_completion_timeout(cmpl, QLC_MBOX_CH_FREE_TIMEOUT)) {
891 trans->trans_state = QLC_ABORT;
892 return;
893 }
894
895 clear_bit(QLC_BC_VF_CHANNEL, &vf->state);
896 qlcnic_sriov_handle_multi_frags(trans, type);
897}
898
899static void qlcnic_sriov_pull_bc_msg(struct qlcnic_adapter *adapter,
900 u32 *hdr, u32 *pay, u32 size)
901{
902 struct qlcnic_hardware_context *ahw = adapter->ahw;
903 u32 fw_mbx;
904 u8 i, max = 2, hdr_size, j;
905
906 hdr_size = (sizeof(struct qlcnic_bc_hdr) / sizeof(u32));
907 max = (size / sizeof(u32)) + hdr_size;
908
909 fw_mbx = readl(QLCNIC_MBX_FW(ahw, 0));
910 for (i = 2, j = 0; j < hdr_size; i++, j++)
911 *(hdr++) = readl(QLCNIC_MBX_FW(ahw, i));
912 for (; j < max; i++, j++)
913 *(pay++) = readl(QLCNIC_MBX_FW(ahw, i));
914}
915
916static int __qlcnic_sriov_issue_bc_post(struct qlcnic_vf_info *vf)
917{
918 int ret = -EBUSY;
919 u32 timeout = 10000;
920
921 do {
922 if (!test_and_set_bit(QLC_BC_VF_CHANNEL, &vf->state)) {
923 ret = 0;
924 break;
925 }
926 mdelay(1);
927 } while (--timeout);
928
929 return ret;
930}
931
932static int qlcnic_sriov_issue_bc_post(struct qlcnic_bc_trans *trans, u8 type)
933{
934 struct qlcnic_vf_info *vf = trans->vf;
935 u32 pay_size, hdr_size;
936 u32 *hdr, *pay;
937 int ret;
938 u8 pci_func = trans->func_id;
939
940 if (__qlcnic_sriov_issue_bc_post(vf))
941 return -EBUSY;
942
943 if (type == QLC_BC_COMMAND) {
944 hdr = (u32 *)(trans->req_hdr + trans->curr_req_frag);
945 pay = (u32 *)(trans->req_pay + trans->curr_req_frag);
946 hdr_size = (sizeof(struct qlcnic_bc_hdr) / sizeof(u32));
947 pay_size = qlcnic_sriov_get_bc_paysize(trans->req_pay_size,
948 trans->curr_req_frag);
949 pay_size = (pay_size / sizeof(u32));
950 } else {
951 hdr = (u32 *)(trans->rsp_hdr + trans->curr_rsp_frag);
952 pay = (u32 *)(trans->rsp_pay + trans->curr_rsp_frag);
953 hdr_size = (sizeof(struct qlcnic_bc_hdr) / sizeof(u32));
954 pay_size = qlcnic_sriov_get_bc_paysize(trans->rsp_pay_size,
955 trans->curr_rsp_frag);
956 pay_size = (pay_size / sizeof(u32));
957 }
958
959 ret = qlcnic_sriov_post_bc_msg(vf->adapter, hdr, pay,
960 pci_func, pay_size);
961 return ret;
962}
963
964static int __qlcnic_sriov_send_bc_msg(struct qlcnic_bc_trans *trans,
965 struct qlcnic_vf_info *vf, u8 type)
966{
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000967 bool flag = true;
Rajesh Borundia97d81052013-04-19 07:01:09 +0000968 int err = -EIO;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000969
970 while (flag) {
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +0000971 if (test_bit(QLC_BC_VF_FLR, &vf->state) ||
972 vf->adapter->need_fw_reset)
Rajesh Borundia97d81052013-04-19 07:01:09 +0000973 trans->trans_state = QLC_ABORT;
974
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +0000975 switch (trans->trans_state) {
976 case QLC_INIT:
977 trans->trans_state = QLC_WAIT_FOR_CHANNEL_FREE;
978 if (qlcnic_sriov_issue_bc_post(trans, type))
979 trans->trans_state = QLC_ABORT;
980 break;
981 case QLC_WAIT_FOR_CHANNEL_FREE:
982 qlcnic_sriov_wait_for_channel_free(trans, type);
983 break;
984 case QLC_WAIT_FOR_RESP:
985 qlcnic_sriov_wait_for_resp(trans);
986 break;
987 case QLC_END:
988 err = 0;
989 flag = false;
990 break;
991 case QLC_ABORT:
992 err = -EIO;
993 flag = false;
994 clear_bit(QLC_BC_VF_CHANNEL, &vf->state);
995 break;
996 default:
997 err = -EIO;
998 flag = false;
999 }
1000 }
1001 return err;
1002}
1003
1004static int qlcnic_sriov_send_bc_cmd(struct qlcnic_adapter *adapter,
1005 struct qlcnic_bc_trans *trans, int pci_func)
1006{
1007 struct qlcnic_vf_info *vf;
1008 int err, index = qlcnic_sriov_func_to_index(adapter, pci_func);
1009
1010 if (index < 0)
1011 return -EIO;
1012
1013 vf = &adapter->ahw->sriov->vf_info[index];
1014 trans->vf = vf;
1015 trans->func_id = pci_func;
1016
1017 if (!test_bit(QLC_BC_VF_STATE, &vf->state)) {
1018 if (qlcnic_sriov_pf_check(adapter))
1019 return -EIO;
1020 if (qlcnic_sriov_vf_check(adapter) &&
1021 trans->cmd_id != QLCNIC_BC_CMD_CHANNEL_INIT)
1022 return -EIO;
1023 }
1024
1025 mutex_lock(&vf->send_cmd_lock);
1026 vf->send_cmd = trans;
1027 err = __qlcnic_sriov_send_bc_msg(trans, vf, QLC_BC_COMMAND);
1028 qlcnic_sriov_clear_trans(vf, trans, QLC_BC_COMMAND);
1029 mutex_unlock(&vf->send_cmd_lock);
1030 return err;
1031}
1032
1033static void __qlcnic_sriov_process_bc_cmd(struct qlcnic_adapter *adapter,
1034 struct qlcnic_bc_trans *trans,
1035 struct qlcnic_cmd_args *cmd)
1036{
1037#ifdef CONFIG_QLCNIC_SRIOV
1038 if (qlcnic_sriov_pf_check(adapter)) {
1039 qlcnic_sriov_pf_process_bc_cmd(adapter, trans, cmd);
1040 return;
1041 }
1042#endif
1043 cmd->rsp.arg[0] |= (0x9 << 25);
1044 return;
1045}
1046
1047static void qlcnic_sriov_process_bc_cmd(struct work_struct *work)
1048{
1049 struct qlcnic_vf_info *vf = container_of(work, struct qlcnic_vf_info,
1050 trans_work);
1051 struct qlcnic_bc_trans *trans = NULL;
1052 struct qlcnic_adapter *adapter = vf->adapter;
1053 struct qlcnic_cmd_args cmd;
1054 u8 req;
1055
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001056 if (adapter->need_fw_reset)
1057 return;
1058
Rajesh Borundia97d81052013-04-19 07:01:09 +00001059 if (test_bit(QLC_BC_VF_FLR, &vf->state))
1060 return;
1061
Manish Choprae5c4e6c2013-08-02 00:57:40 -04001062 memset(&cmd, 0, sizeof(struct qlcnic_cmd_args));
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001063 trans = list_first_entry(&vf->rcv_act.wait_list,
1064 struct qlcnic_bc_trans, list);
1065 adapter = vf->adapter;
1066
1067 if (qlcnic_sriov_prepare_bc_hdr(trans, &cmd, trans->req_hdr->seq_id,
1068 QLC_BC_RESPONSE))
1069 goto cleanup_trans;
1070
1071 __qlcnic_sriov_process_bc_cmd(adapter, trans, &cmd);
1072 trans->trans_state = QLC_INIT;
1073 __qlcnic_sriov_send_bc_msg(trans, vf, QLC_BC_RESPONSE);
1074
1075cleanup_trans:
1076 qlcnic_free_mbx_args(&cmd);
1077 req = qlcnic_sriov_clear_trans(vf, trans, QLC_BC_RESPONSE);
1078 qlcnic_sriov_cleanup_transaction(trans);
1079 if (req)
1080 qlcnic_sriov_schedule_bc_cmd(adapter->ahw->sriov, vf,
1081 qlcnic_sriov_process_bc_cmd);
1082}
1083
1084static void qlcnic_sriov_handle_bc_resp(struct qlcnic_bc_hdr *hdr,
1085 struct qlcnic_vf_info *vf)
1086{
1087 struct qlcnic_bc_trans *trans;
1088 u32 pay_size;
1089
1090 if (test_and_set_bit(QLC_BC_VF_SEND, &vf->state))
1091 return;
1092
1093 trans = vf->send_cmd;
1094
1095 if (trans == NULL)
1096 goto clear_send;
1097
1098 if (trans->trans_id != hdr->seq_id)
1099 goto clear_send;
1100
1101 pay_size = qlcnic_sriov_get_bc_paysize(trans->rsp_pay_size,
1102 trans->curr_rsp_frag);
1103 qlcnic_sriov_pull_bc_msg(vf->adapter,
1104 (u32 *)(trans->rsp_hdr + trans->curr_rsp_frag),
1105 (u32 *)(trans->rsp_pay + trans->curr_rsp_frag),
1106 pay_size);
1107 if (++trans->curr_rsp_frag < trans->rsp_hdr->num_frags)
1108 goto clear_send;
1109
1110 complete(&trans->resp_cmpl);
1111
1112clear_send:
1113 clear_bit(QLC_BC_VF_SEND, &vf->state);
1114}
1115
Rajesh Borundia97d81052013-04-19 07:01:09 +00001116int __qlcnic_sriov_add_act_list(struct qlcnic_sriov *sriov,
1117 struct qlcnic_vf_info *vf,
1118 struct qlcnic_bc_trans *trans)
1119{
1120 struct qlcnic_trans_list *t_list = &vf->rcv_act;
1121
1122 t_list->count++;
1123 list_add_tail(&trans->list, &t_list->wait_list);
1124 if (t_list->count == 1)
1125 qlcnic_sriov_schedule_bc_cmd(sriov, vf,
1126 qlcnic_sriov_process_bc_cmd);
1127 return 0;
1128}
1129
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001130static int qlcnic_sriov_add_act_list(struct qlcnic_sriov *sriov,
1131 struct qlcnic_vf_info *vf,
1132 struct qlcnic_bc_trans *trans)
1133{
1134 struct qlcnic_trans_list *t_list = &vf->rcv_act;
1135
1136 spin_lock(&t_list->lock);
Rajesh Borundia97d81052013-04-19 07:01:09 +00001137
1138 __qlcnic_sriov_add_act_list(sriov, vf, trans);
1139
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001140 spin_unlock(&t_list->lock);
1141 return 0;
1142}
1143
1144static void qlcnic_sriov_handle_pending_trans(struct qlcnic_sriov *sriov,
1145 struct qlcnic_vf_info *vf,
1146 struct qlcnic_bc_hdr *hdr)
1147{
1148 struct qlcnic_bc_trans *trans = NULL;
1149 struct list_head *node;
1150 u32 pay_size, curr_frag;
1151 u8 found = 0, active = 0;
1152
1153 spin_lock(&vf->rcv_pend.lock);
1154 if (vf->rcv_pend.count > 0) {
1155 list_for_each(node, &vf->rcv_pend.wait_list) {
1156 trans = list_entry(node, struct qlcnic_bc_trans, list);
1157 if (trans->trans_id == hdr->seq_id) {
1158 found = 1;
1159 break;
1160 }
1161 }
1162 }
1163
1164 if (found) {
1165 curr_frag = trans->curr_req_frag;
1166 pay_size = qlcnic_sriov_get_bc_paysize(trans->req_pay_size,
1167 curr_frag);
1168 qlcnic_sriov_pull_bc_msg(vf->adapter,
1169 (u32 *)(trans->req_hdr + curr_frag),
1170 (u32 *)(trans->req_pay + curr_frag),
1171 pay_size);
1172 trans->curr_req_frag++;
1173 if (trans->curr_req_frag >= hdr->num_frags) {
1174 vf->rcv_pend.count--;
1175 list_del(&trans->list);
1176 active = 1;
1177 }
1178 }
1179 spin_unlock(&vf->rcv_pend.lock);
1180
1181 if (active)
1182 if (qlcnic_sriov_add_act_list(sriov, vf, trans))
1183 qlcnic_sriov_cleanup_transaction(trans);
1184
1185 return;
1186}
1187
1188static void qlcnic_sriov_handle_bc_cmd(struct qlcnic_sriov *sriov,
1189 struct qlcnic_bc_hdr *hdr,
1190 struct qlcnic_vf_info *vf)
1191{
1192 struct qlcnic_bc_trans *trans;
1193 struct qlcnic_adapter *adapter = vf->adapter;
1194 struct qlcnic_cmd_args cmd;
1195 u32 pay_size;
1196 int err;
1197 u8 cmd_op;
1198
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001199 if (adapter->need_fw_reset)
1200 return;
1201
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001202 if (!test_bit(QLC_BC_VF_STATE, &vf->state) &&
1203 hdr->op_type != QLC_BC_CMD &&
1204 hdr->cmd_op != QLCNIC_BC_CMD_CHANNEL_INIT)
1205 return;
1206
1207 if (hdr->frag_num > 1) {
1208 qlcnic_sriov_handle_pending_trans(sriov, vf, hdr);
1209 return;
1210 }
1211
Manish Choprae5c4e6c2013-08-02 00:57:40 -04001212 memset(&cmd, 0, sizeof(struct qlcnic_cmd_args));
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001213 cmd_op = hdr->cmd_op;
1214 if (qlcnic_sriov_alloc_bc_trans(&trans))
1215 return;
1216
1217 if (hdr->op_type == QLC_BC_CMD)
1218 err = qlcnic_sriov_alloc_bc_mbx_args(&cmd, cmd_op);
1219 else
1220 err = qlcnic_alloc_mbx_args(&cmd, adapter, cmd_op);
1221
1222 if (err) {
1223 qlcnic_sriov_cleanup_transaction(trans);
1224 return;
1225 }
1226
1227 cmd.op_type = hdr->op_type;
1228 if (qlcnic_sriov_prepare_bc_hdr(trans, &cmd, hdr->seq_id,
1229 QLC_BC_COMMAND)) {
1230 qlcnic_free_mbx_args(&cmd);
1231 qlcnic_sriov_cleanup_transaction(trans);
1232 return;
1233 }
1234
1235 pay_size = qlcnic_sriov_get_bc_paysize(trans->req_pay_size,
1236 trans->curr_req_frag);
1237 qlcnic_sriov_pull_bc_msg(vf->adapter,
1238 (u32 *)(trans->req_hdr + trans->curr_req_frag),
1239 (u32 *)(trans->req_pay + trans->curr_req_frag),
1240 pay_size);
1241 trans->func_id = vf->pci_func;
1242 trans->vf = vf;
1243 trans->trans_id = hdr->seq_id;
1244 trans->curr_req_frag++;
Rajesh Borundia97d81052013-04-19 07:01:09 +00001245
1246 if (qlcnic_sriov_soft_flr_check(adapter, trans, vf))
1247 return;
1248
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001249 if (trans->curr_req_frag == trans->req_hdr->num_frags) {
1250 if (qlcnic_sriov_add_act_list(sriov, vf, trans)) {
1251 qlcnic_free_mbx_args(&cmd);
1252 qlcnic_sriov_cleanup_transaction(trans);
1253 }
1254 } else {
1255 spin_lock(&vf->rcv_pend.lock);
1256 list_add_tail(&trans->list, &vf->rcv_pend.wait_list);
1257 vf->rcv_pend.count++;
1258 spin_unlock(&vf->rcv_pend.lock);
1259 }
1260}
1261
1262static void qlcnic_sriov_handle_msg_event(struct qlcnic_sriov *sriov,
1263 struct qlcnic_vf_info *vf)
1264{
1265 struct qlcnic_bc_hdr hdr;
1266 u32 *ptr = (u32 *)&hdr;
1267 u8 msg_type, i;
1268
1269 for (i = 2; i < 6; i++)
1270 ptr[i - 2] = readl(QLCNIC_MBX_FW(vf->adapter->ahw, i));
1271 msg_type = hdr.msg_type;
1272
1273 switch (msg_type) {
1274 case QLC_BC_COMMAND:
1275 qlcnic_sriov_handle_bc_cmd(sriov, &hdr, vf);
1276 break;
1277 case QLC_BC_RESPONSE:
1278 qlcnic_sriov_handle_bc_resp(&hdr, vf);
1279 break;
1280 }
1281}
1282
Rajesh Borundia97d81052013-04-19 07:01:09 +00001283static void qlcnic_sriov_handle_flr_event(struct qlcnic_sriov *sriov,
1284 struct qlcnic_vf_info *vf)
1285{
1286 struct qlcnic_adapter *adapter = vf->adapter;
1287
1288 if (qlcnic_sriov_pf_check(adapter))
1289 qlcnic_sriov_pf_handle_flr(sriov, vf);
1290 else
1291 dev_err(&adapter->pdev->dev,
1292 "Invalid event to VF. VF should not get FLR event\n");
1293}
1294
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001295void qlcnic_sriov_handle_bc_event(struct qlcnic_adapter *adapter, u32 event)
1296{
1297 struct qlcnic_vf_info *vf;
1298 struct qlcnic_sriov *sriov;
1299 int index;
1300 u8 pci_func;
1301
1302 sriov = adapter->ahw->sriov;
1303 pci_func = qlcnic_sriov_target_func_id(event);
1304 index = qlcnic_sriov_func_to_index(adapter, pci_func);
1305
1306 if (index < 0)
1307 return;
1308
1309 vf = &sriov->vf_info[index];
1310 vf->pci_func = pci_func;
1311
1312 if (qlcnic_sriov_channel_free_check(event))
1313 complete(&vf->ch_free_cmpl);
1314
Rajesh Borundia97d81052013-04-19 07:01:09 +00001315 if (qlcnic_sriov_flr_check(event)) {
1316 qlcnic_sriov_handle_flr_event(sriov, vf);
1317 return;
1318 }
1319
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001320 if (qlcnic_sriov_bc_msg_check(event))
1321 qlcnic_sriov_handle_msg_event(sriov, vf);
1322}
1323
1324int qlcnic_sriov_cfg_bc_intr(struct qlcnic_adapter *adapter, u8 enable)
1325{
1326 struct qlcnic_cmd_args cmd;
1327 int err;
1328
1329 if (!test_bit(__QLCNIC_SRIOV_ENABLE, &adapter->state))
1330 return 0;
1331
1332 if (qlcnic_alloc_mbx_args(&cmd, adapter, QLCNIC_CMD_BC_EVENT_SETUP))
1333 return -ENOMEM;
1334
1335 if (enable)
1336 cmd.req.arg[1] = (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7);
1337
Manish Choprae5c4e6c2013-08-02 00:57:40 -04001338 err = qlcnic_83xx_issue_cmd(adapter, &cmd);
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001339
1340 if (err != QLCNIC_RCODE_SUCCESS) {
1341 dev_err(&adapter->pdev->dev,
1342 "Failed to %s bc events, err=%d\n",
1343 (enable ? "enable" : "disable"), err);
1344 }
1345
1346 qlcnic_free_mbx_args(&cmd);
1347 return err;
1348}
1349
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001350static int qlcnic_sriov_retry_bc_cmd(struct qlcnic_adapter *adapter,
1351 struct qlcnic_bc_trans *trans)
1352{
1353 u8 max = QLC_BC_CMD_MAX_RETRY_CNT;
1354 u32 state;
1355
1356 state = QLCRDX(adapter->ahw, QLC_83XX_IDC_DEV_STATE);
1357 if (state == QLC_83XX_IDC_DEV_READY) {
1358 msleep(20);
1359 clear_bit(QLC_BC_VF_CHANNEL, &trans->vf->state);
1360 trans->trans_state = QLC_INIT;
1361 if (++adapter->fw_fail_cnt > max)
1362 return -EIO;
1363 else
1364 return 0;
1365 }
1366
1367 return -EIO;
1368}
1369
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001370static int __qlcnic_sriov_issue_cmd(struct qlcnic_adapter *adapter,
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001371 struct qlcnic_cmd_args *cmd)
1372{
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001373 struct qlcnic_hardware_context *ahw = adapter->ahw;
Manish Chopra068a8d12013-08-02 00:57:41 -04001374 struct qlcnic_mailbox *mbx = ahw->mailbox;
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001375 struct device *dev = &adapter->pdev->dev;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001376 struct qlcnic_bc_trans *trans;
1377 int err;
1378 u32 rsp_data, opcode, mbx_err_code, rsp;
1379 u16 seq = ++adapter->ahw->sriov->bc.trans_counter;
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001380 u8 func = ahw->pci_func;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001381
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001382 rsp = qlcnic_sriov_alloc_bc_trans(&trans);
1383 if (rsp)
Rajesh Borundiaab0648e2014-04-23 09:59:56 -04001384 goto free_cmd;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001385
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001386 rsp = qlcnic_sriov_prepare_bc_hdr(trans, cmd, seq, QLC_BC_COMMAND);
1387 if (rsp)
1388 goto cleanup_transaction;
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001389
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001390retry:
Manish Chopra068a8d12013-08-02 00:57:41 -04001391 if (!test_bit(QLC_83XX_MBX_READY, &mbx->status)) {
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001392 rsp = -EIO;
1393 QLCDB(adapter, DRV, "MBX not Ready!(cmd 0x%x) for VF 0x%x\n",
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001394 QLCNIC_MBX_RSP(cmd->req.arg[0]), func);
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001395 goto err_out;
1396 }
1397
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001398 err = qlcnic_sriov_send_bc_cmd(adapter, trans, func);
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001399 if (err) {
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001400 dev_err(dev, "MBX command 0x%x timed out for VF %d\n",
1401 (cmd->req.arg[0] & 0xffff), func);
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001402 rsp = QLCNIC_RCODE_TIMEOUT;
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001403
1404 /* After adapter reset PF driver may take some time to
1405 * respond to VF's request. Retry request till maximum retries.
1406 */
1407 if ((trans->req_hdr->cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT) &&
1408 !qlcnic_sriov_retry_bc_cmd(adapter, trans))
1409 goto retry;
1410
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001411 goto err_out;
1412 }
1413
1414 rsp_data = cmd->rsp.arg[0];
1415 mbx_err_code = QLCNIC_MBX_STATUS(rsp_data);
1416 opcode = QLCNIC_MBX_RSP(cmd->req.arg[0]);
1417
1418 if ((mbx_err_code == QLCNIC_MBX_RSP_OK) ||
1419 (mbx_err_code == QLCNIC_MBX_PORT_RSP_OK)) {
1420 rsp = QLCNIC_RCODE_SUCCESS;
1421 } else {
Rajesh Borundiad747c332014-05-09 02:51:31 -04001422 if (cmd->type == QLC_83XX_MBX_CMD_NO_WAIT) {
1423 rsp = QLCNIC_RCODE_SUCCESS;
1424 } else {
1425 rsp = mbx_err_code;
1426 if (!rsp)
1427 rsp = 1;
1428
1429 dev_err(dev,
1430 "MBX command 0x%x failed with err:0x%x for VF %d\n",
1431 opcode, mbx_err_code, func);
1432 }
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001433 }
1434
1435err_out:
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001436 if (rsp == QLCNIC_RCODE_TIMEOUT) {
1437 ahw->reset_context = 1;
1438 adapter->need_fw_reset = 1;
Manish Chopra068a8d12013-08-02 00:57:41 -04001439 clear_bit(QLC_83XX_MBX_READY, &mbx->status);
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001440 }
1441
1442cleanup_transaction:
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001443 qlcnic_sriov_cleanup_transaction(trans);
Rajesh Borundiaab0648e2014-04-23 09:59:56 -04001444
1445free_cmd:
1446 if (cmd->type == QLC_83XX_MBX_CMD_NO_WAIT) {
1447 qlcnic_free_mbx_args(cmd);
1448 kfree(cmd);
1449 }
1450
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001451 return rsp;
1452}
1453
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001454
1455static int qlcnic_sriov_issue_cmd(struct qlcnic_adapter *adapter,
1456 struct qlcnic_cmd_args *cmd)
1457{
1458 if (cmd->type == QLC_83XX_MBX_CMD_NO_WAIT)
1459 return qlcnic_sriov_async_issue_cmd(adapter, cmd);
1460 else
1461 return __qlcnic_sriov_issue_cmd(adapter, cmd);
1462}
1463
stephen hemminger21041402014-01-16 10:31:27 -08001464static int qlcnic_sriov_channel_cfg_cmd(struct qlcnic_adapter *adapter, u8 cmd_op)
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001465{
1466 struct qlcnic_cmd_args cmd;
1467 struct qlcnic_vf_info *vf = &adapter->ahw->sriov->vf_info[0];
1468 int ret;
1469
Rajesh Borundiac5316922014-06-04 03:40:54 -04001470 memset(&cmd, 0, sizeof(cmd));
Rajesh Borundiaf197a7a2013-03-29 05:46:36 +00001471 if (qlcnic_sriov_alloc_bc_mbx_args(&cmd, cmd_op))
1472 return -ENOMEM;
1473
1474 ret = qlcnic_issue_cmd(adapter, &cmd);
1475 if (ret) {
1476 dev_err(&adapter->pdev->dev,
1477 "Failed bc channel %s %d\n", cmd_op ? "term" : "init",
1478 ret);
1479 goto out;
1480 }
1481
1482 cmd_op = (cmd.rsp.arg[0] & 0xff);
1483 if (cmd.rsp.arg[0] >> 25 == 2)
1484 return 2;
1485 if (cmd_op == QLCNIC_BC_CMD_CHANNEL_INIT)
1486 set_bit(QLC_BC_VF_STATE, &vf->state);
1487 else
1488 clear_bit(QLC_BC_VF_STATE, &vf->state);
1489
1490out:
1491 qlcnic_free_mbx_args(&cmd);
1492 return ret;
1493}
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001494
Shahed Shaikhfe79fab2015-02-11 09:45:24 -05001495static void qlcnic_vf_add_mc_list(struct net_device *netdev, const u8 *mac,
1496 enum qlcnic_mac_type mac_type)
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001497{
1498 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Manish Chopra154d0c82013-12-17 09:01:53 -05001499 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
Manish Chopra154d0c82013-12-17 09:01:53 -05001500 struct qlcnic_vf_info *vf;
1501 u16 vlan_id;
1502 int i;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001503
Manish Chopra154d0c82013-12-17 09:01:53 -05001504 vf = &adapter->ahw->sriov->vf_info[0];
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001505
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001506 if (!qlcnic_sriov_check_any_vlan(vf)) {
Shahed Shaikhfe79fab2015-02-11 09:45:24 -05001507 qlcnic_nic_add_mac(adapter, mac, 0, mac_type);
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001508 } else {
1509 spin_lock(&vf->vlan_list_lock);
1510 for (i = 0; i < sriov->num_allowed_vlans; i++) {
1511 vlan_id = vf->sriov_vlans[i];
1512 if (vlan_id)
Shahed Shaikhfe79fab2015-02-11 09:45:24 -05001513 qlcnic_nic_add_mac(adapter, mac, vlan_id,
1514 mac_type);
Manish Chopra154d0c82013-12-17 09:01:53 -05001515 }
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001516 spin_unlock(&vf->vlan_list_lock);
1517 if (qlcnic_84xx_check(adapter))
Shahed Shaikhfe79fab2015-02-11 09:45:24 -05001518 qlcnic_nic_add_mac(adapter, mac, 0, mac_type);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001519 }
1520}
1521
1522void qlcnic_sriov_cleanup_async_list(struct qlcnic_back_channel *bc)
1523{
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001524 struct list_head *head = &bc->async_cmd_list;
1525 struct qlcnic_async_cmd *entry;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001526
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001527 flush_workqueue(bc->bc_async_wq);
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001528 cancel_work_sync(&bc->vf_async_work);
1529
1530 spin_lock(&bc->queue_lock);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001531 while (!list_empty(head)) {
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001532 entry = list_entry(head->next, struct qlcnic_async_cmd,
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001533 list);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001534 list_del(&entry->list);
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001535 kfree(entry->cmd);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001536 kfree(entry);
1537 }
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001538 spin_unlock(&bc->queue_lock);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001539}
1540
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001541void qlcnic_sriov_vf_set_multi(struct net_device *netdev)
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001542{
1543 struct qlcnic_adapter *adapter = netdev_priv(netdev);
Manish Chopra154d0c82013-12-17 09:01:53 -05001544 struct qlcnic_hardware_context *ahw = adapter->ahw;
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001545 static const u8 bcast_addr[ETH_ALEN] = {
1546 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
1547 };
1548 struct netdev_hw_addr *ha;
Manish Chopra154d0c82013-12-17 09:01:53 -05001549 u32 mode = VPORT_MISS_MODE_DROP;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001550
1551 if (!test_bit(__QLCNIC_FW_ATTACHED, &adapter->state))
1552 return;
1553
Manish Chopra154d0c82013-12-17 09:01:53 -05001554 if (netdev->flags & IFF_PROMISC) {
1555 if (!(adapter->flags & QLCNIC_PROMISC_DISABLED))
1556 mode = VPORT_MISS_MODE_ACCEPT_ALL;
1557 } else if ((netdev->flags & IFF_ALLMULTI) ||
1558 (netdev_mc_count(netdev) > ahw->max_mc_count)) {
1559 mode = VPORT_MISS_MODE_ACCEPT_MULTI;
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001560 } else {
Shahed Shaikhfe79fab2015-02-11 09:45:24 -05001561 qlcnic_vf_add_mc_list(netdev, bcast_addr, QLCNIC_BROADCAST_MAC);
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001562 if (!netdev_mc_empty(netdev)) {
Shahed Shaikhfe79fab2015-02-11 09:45:24 -05001563 qlcnic_flush_mcast_mac(adapter);
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001564 netdev_for_each_mc_addr(ha, netdev)
Shahed Shaikhfe79fab2015-02-11 09:45:24 -05001565 qlcnic_vf_add_mc_list(netdev, ha->addr,
1566 QLCNIC_MULTICAST_MAC);
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001567 }
Manish Chopra154d0c82013-12-17 09:01:53 -05001568 }
1569
Rajesh Borundiad747c332014-05-09 02:51:31 -04001570 /* configure unicast MAC address, if there is not sufficient space
1571 * to store all the unicast addresses then enable promiscuous mode
1572 */
1573 if (netdev_uc_count(netdev) > ahw->max_uc_count) {
1574 mode = VPORT_MISS_MODE_ACCEPT_ALL;
1575 } else if (!netdev_uc_empty(netdev)) {
1576 netdev_for_each_uc_addr(ha, netdev)
Shahed Shaikhfe79fab2015-02-11 09:45:24 -05001577 qlcnic_vf_add_mc_list(netdev, ha->addr,
1578 QLCNIC_UNICAST_MAC);
Rajesh Borundiad747c332014-05-09 02:51:31 -04001579 }
1580
1581 if (adapter->pdev->is_virtfn) {
1582 if (mode == VPORT_MISS_MODE_ACCEPT_ALL &&
1583 !adapter->fdb_mac_learn) {
1584 qlcnic_alloc_lb_filters_mem(adapter);
1585 adapter->drv_mac_learn = 1;
1586 adapter->rx_mac_learn = true;
1587 } else {
1588 adapter->drv_mac_learn = 0;
1589 adapter->rx_mac_learn = false;
1590 }
1591 }
1592
Manish Chopra154d0c82013-12-17 09:01:53 -05001593 qlcnic_nic_set_promisc(adapter, mode);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001594}
1595
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001596static void qlcnic_sriov_handle_async_issue_cmd(struct work_struct *work)
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001597{
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001598 struct qlcnic_async_cmd *entry, *tmp;
1599 struct qlcnic_back_channel *bc;
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001600 struct qlcnic_cmd_args *cmd;
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001601 struct list_head *head;
1602 LIST_HEAD(del_list);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001603
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001604 bc = container_of(work, struct qlcnic_back_channel, vf_async_work);
1605 head = &bc->async_cmd_list;
1606
1607 spin_lock(&bc->queue_lock);
1608 list_splice_init(head, &del_list);
1609 spin_unlock(&bc->queue_lock);
1610
1611 list_for_each_entry_safe(entry, tmp, &del_list, list) {
1612 list_del(&entry->list);
1613 cmd = entry->cmd;
1614 __qlcnic_sriov_issue_cmd(bc->adapter, cmd);
1615 kfree(entry);
1616 }
1617
1618 if (!list_empty(head))
1619 queue_work(bc->bc_async_wq, &bc->vf_async_work);
1620
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001621 return;
1622}
1623
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001624static struct qlcnic_async_cmd *
1625qlcnic_sriov_alloc_async_cmd(struct qlcnic_back_channel *bc,
1626 struct qlcnic_cmd_args *cmd)
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001627{
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001628 struct qlcnic_async_cmd *entry = NULL;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001629
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001630 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1631 if (!entry)
1632 return NULL;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001633
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001634 entry->cmd = cmd;
1635
1636 spin_lock(&bc->queue_lock);
1637 list_add_tail(&entry->list, &bc->async_cmd_list);
1638 spin_unlock(&bc->queue_lock);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001639
1640 return entry;
1641}
1642
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001643static void qlcnic_sriov_schedule_async_cmd(struct qlcnic_back_channel *bc,
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001644 struct qlcnic_cmd_args *cmd)
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001645{
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001646 struct qlcnic_async_cmd *entry = NULL;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001647
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001648 entry = qlcnic_sriov_alloc_async_cmd(bc, cmd);
1649 if (!entry) {
1650 qlcnic_free_mbx_args(cmd);
1651 kfree(cmd);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001652 return;
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001653 }
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001654
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001655 queue_work(bc->bc_async_wq, &bc->vf_async_work);
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001656}
1657
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001658static int qlcnic_sriov_async_issue_cmd(struct qlcnic_adapter *adapter,
1659 struct qlcnic_cmd_args *cmd)
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001660{
1661
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001662 struct qlcnic_back_channel *bc = &adapter->ahw->sriov->bc;
1663
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001664 if (adapter->need_fw_reset)
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001665 return -EIO;
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001666
Manish Chopra2b10d3e2016-08-03 04:02:02 -04001667 qlcnic_sriov_schedule_async_cmd(bc, cmd);
1668
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001669 return 0;
Rajesh Borundiae8b508e2013-03-29 05:46:38 +00001670}
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001671
1672static int qlcnic_sriov_vf_reinit_driver(struct qlcnic_adapter *adapter)
1673{
1674 int err;
1675
Manish Chopra5c44bbd2013-09-13 06:13:47 -04001676 adapter->need_fw_reset = 0;
Manish Chopra91b86e32013-08-15 08:27:25 -04001677 qlcnic_83xx_reinit_mbx_work(adapter->ahw->mailbox);
Manish Choprae5c4e6c2013-08-02 00:57:40 -04001678 qlcnic_83xx_enable_mbx_interrupt(adapter);
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001679
1680 err = qlcnic_sriov_cfg_bc_intr(adapter, 1);
1681 if (err)
1682 return err;
1683
1684 err = qlcnic_sriov_channel_cfg_cmd(adapter, QLCNIC_BC_CMD_CHANNEL_INIT);
1685 if (err)
1686 goto err_out_cleanup_bc_intr;
1687
1688 err = qlcnic_sriov_vf_init_driver(adapter);
1689 if (err)
1690 goto err_out_term_channel;
1691
1692 return 0;
1693
1694err_out_term_channel:
1695 qlcnic_sriov_channel_cfg_cmd(adapter, QLCNIC_BC_CMD_CHANNEL_TERM);
1696
1697err_out_cleanup_bc_intr:
1698 qlcnic_sriov_cfg_bc_intr(adapter, 0);
1699 return err;
1700}
1701
1702static void qlcnic_sriov_vf_attach(struct qlcnic_adapter *adapter)
1703{
1704 struct net_device *netdev = adapter->netdev;
1705
1706 if (netif_running(netdev)) {
1707 if (!qlcnic_up(adapter, netdev))
1708 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
1709 }
1710
1711 netif_device_attach(netdev);
1712}
1713
1714static void qlcnic_sriov_vf_detach(struct qlcnic_adapter *adapter)
1715{
1716 struct qlcnic_hardware_context *ahw = adapter->ahw;
1717 struct qlcnic_intrpt_config *intr_tbl = ahw->intr_tbl;
1718 struct net_device *netdev = adapter->netdev;
1719 u8 i, max_ints = ahw->num_msix - 1;
1720
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001721 netif_device_detach(netdev);
Manish Chopra068a8d12013-08-02 00:57:41 -04001722 qlcnic_83xx_detach_mailbox_work(adapter);
1723 qlcnic_83xx_disable_mbx_intr(adapter);
1724
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001725 if (netif_running(netdev))
1726 qlcnic_down(adapter, netdev);
1727
1728 for (i = 0; i < max_ints; i++) {
1729 intr_tbl[i].id = i;
1730 intr_tbl[i].enabled = 0;
1731 intr_tbl[i].src = 0;
1732 }
1733 ahw->reset_context = 0;
1734}
1735
1736static int qlcnic_sriov_vf_handle_dev_ready(struct qlcnic_adapter *adapter)
1737{
1738 struct qlcnic_hardware_context *ahw = adapter->ahw;
1739 struct device *dev = &adapter->pdev->dev;
1740 struct qlc_83xx_idc *idc = &ahw->idc;
1741 u8 func = ahw->pci_func;
1742 u32 state;
1743
1744 if ((idc->prev_state == QLC_83XX_IDC_DEV_NEED_RESET) ||
1745 (idc->prev_state == QLC_83XX_IDC_DEV_INIT)) {
1746 if (!qlcnic_sriov_vf_reinit_driver(adapter)) {
1747 qlcnic_sriov_vf_attach(adapter);
1748 adapter->fw_fail_cnt = 0;
1749 dev_info(dev,
Masanari Iida8b513d02013-05-21 23:13:12 +09001750 "%s: Reinitialization of VF 0x%x done after FW reset\n",
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001751 __func__, func);
1752 } else {
1753 dev_err(dev,
1754 "%s: Reinitialization of VF 0x%x failed after FW reset\n",
1755 __func__, func);
1756 state = QLCRDX(ahw, QLC_83XX_IDC_DEV_STATE);
1757 dev_info(dev, "Current state 0x%x after FW reset\n",
1758 state);
1759 }
1760 }
1761
1762 return 0;
1763}
1764
1765static int qlcnic_sriov_vf_handle_context_reset(struct qlcnic_adapter *adapter)
1766{
1767 struct qlcnic_hardware_context *ahw = adapter->ahw;
Manish Chopra068a8d12013-08-02 00:57:41 -04001768 struct qlcnic_mailbox *mbx = ahw->mailbox;
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001769 struct device *dev = &adapter->pdev->dev;
1770 struct qlc_83xx_idc *idc = &ahw->idc;
1771 u8 func = ahw->pci_func;
1772 u32 state;
1773
1774 adapter->reset_ctx_cnt++;
1775
1776 /* Skip the context reset and check if FW is hung */
1777 if (adapter->reset_ctx_cnt < 3) {
1778 adapter->need_fw_reset = 1;
Manish Chopra068a8d12013-08-02 00:57:41 -04001779 clear_bit(QLC_83XX_MBX_READY, &mbx->status);
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001780 dev_info(dev,
1781 "Resetting context, wait here to check if FW is in failed state\n");
1782 return 0;
1783 }
1784
1785 /* Check if number of resets exceed the threshold.
1786 * If it exceeds the threshold just fail the VF.
1787 */
1788 if (adapter->reset_ctx_cnt > QLC_83XX_VF_RESET_FAIL_THRESH) {
1789 clear_bit(QLC_83XX_MODULE_LOADED, &idc->status);
1790 adapter->tx_timeo_cnt = 0;
1791 adapter->fw_fail_cnt = 0;
1792 adapter->reset_ctx_cnt = 0;
1793 qlcnic_sriov_vf_detach(adapter);
1794 dev_err(dev,
1795 "Device context resets have exceeded the threshold, device interface will be shutdown\n");
1796 return -EIO;
1797 }
1798
1799 dev_info(dev, "Resetting context of VF 0x%x\n", func);
1800 dev_info(dev, "%s: Context reset count %d for VF 0x%x\n",
1801 __func__, adapter->reset_ctx_cnt, func);
1802 set_bit(__QLCNIC_RESETTING, &adapter->state);
1803 adapter->need_fw_reset = 1;
Manish Chopra068a8d12013-08-02 00:57:41 -04001804 clear_bit(QLC_83XX_MBX_READY, &mbx->status);
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001805 qlcnic_sriov_vf_detach(adapter);
1806 adapter->need_fw_reset = 0;
1807
1808 if (!qlcnic_sriov_vf_reinit_driver(adapter)) {
1809 qlcnic_sriov_vf_attach(adapter);
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001810 adapter->tx_timeo_cnt = 0;
1811 adapter->reset_ctx_cnt = 0;
1812 adapter->fw_fail_cnt = 0;
1813 dev_info(dev, "Done resetting context for VF 0x%x\n", func);
1814 } else {
1815 dev_err(dev, "%s: Reinitialization of VF 0x%x failed\n",
1816 __func__, func);
1817 state = QLCRDX(ahw, QLC_83XX_IDC_DEV_STATE);
1818 dev_info(dev, "%s: Current state 0x%x\n", __func__, state);
1819 }
1820
1821 return 0;
1822}
1823
1824static int qlcnic_sriov_vf_idc_ready_state(struct qlcnic_adapter *adapter)
1825{
1826 struct qlcnic_hardware_context *ahw = adapter->ahw;
1827 int ret = 0;
1828
1829 if (ahw->idc.prev_state != QLC_83XX_IDC_DEV_READY)
1830 ret = qlcnic_sriov_vf_handle_dev_ready(adapter);
1831 else if (ahw->reset_context)
1832 ret = qlcnic_sriov_vf_handle_context_reset(adapter);
1833
1834 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1835 return ret;
1836}
1837
1838static int qlcnic_sriov_vf_idc_failed_state(struct qlcnic_adapter *adapter)
1839{
1840 struct qlc_83xx_idc *idc = &adapter->ahw->idc;
1841
1842 dev_err(&adapter->pdev->dev, "Device is in failed state\n");
1843 if (idc->prev_state == QLC_83XX_IDC_DEV_READY)
1844 qlcnic_sriov_vf_detach(adapter);
1845
1846 clear_bit(QLC_83XX_MODULE_LOADED, &idc->status);
1847 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1848 return -EIO;
1849}
1850
1851static int
1852qlcnic_sriov_vf_idc_need_quiescent_state(struct qlcnic_adapter *adapter)
1853{
Manish Chopra068a8d12013-08-02 00:57:41 -04001854 struct qlcnic_mailbox *mbx = adapter->ahw->mailbox;
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001855 struct qlc_83xx_idc *idc = &adapter->ahw->idc;
1856
1857 dev_info(&adapter->pdev->dev, "Device is in quiescent state\n");
1858 if (idc->prev_state == QLC_83XX_IDC_DEV_READY) {
1859 set_bit(__QLCNIC_RESETTING, &adapter->state);
1860 adapter->tx_timeo_cnt = 0;
1861 adapter->reset_ctx_cnt = 0;
Manish Chopra068a8d12013-08-02 00:57:41 -04001862 clear_bit(QLC_83XX_MBX_READY, &mbx->status);
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001863 qlcnic_sriov_vf_detach(adapter);
1864 }
1865
1866 return 0;
1867}
1868
1869static int qlcnic_sriov_vf_idc_init_reset_state(struct qlcnic_adapter *adapter)
1870{
Manish Chopra068a8d12013-08-02 00:57:41 -04001871 struct qlcnic_mailbox *mbx = adapter->ahw->mailbox;
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001872 struct qlc_83xx_idc *idc = &adapter->ahw->idc;
1873 u8 func = adapter->ahw->pci_func;
1874
1875 if (idc->prev_state == QLC_83XX_IDC_DEV_READY) {
1876 dev_err(&adapter->pdev->dev,
1877 "Firmware hang detected by VF 0x%x\n", func);
1878 set_bit(__QLCNIC_RESETTING, &adapter->state);
1879 adapter->tx_timeo_cnt = 0;
1880 adapter->reset_ctx_cnt = 0;
Manish Chopra068a8d12013-08-02 00:57:41 -04001881 clear_bit(QLC_83XX_MBX_READY, &mbx->status);
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001882 qlcnic_sriov_vf_detach(adapter);
1883 }
1884 return 0;
1885}
1886
1887static int qlcnic_sriov_vf_idc_unknown_state(struct qlcnic_adapter *adapter)
1888{
1889 dev_err(&adapter->pdev->dev, "%s: Device in unknown state\n", __func__);
1890 return 0;
1891}
1892
Rajesh Borundiad747c332014-05-09 02:51:31 -04001893static void qlcnic_sriov_vf_periodic_tasks(struct qlcnic_adapter *adapter)
1894{
1895 if (adapter->fhash.fnum)
1896 qlcnic_prune_lb_filters(adapter);
1897}
1898
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001899static void qlcnic_sriov_vf_poll_dev_state(struct work_struct *work)
1900{
1901 struct qlcnic_adapter *adapter;
1902 struct qlc_83xx_idc *idc;
1903 int ret = 0;
1904
1905 adapter = container_of(work, struct qlcnic_adapter, fw_work.work);
1906 idc = &adapter->ahw->idc;
1907 idc->curr_state = QLCRDX(adapter->ahw, QLC_83XX_IDC_DEV_STATE);
1908
1909 switch (idc->curr_state) {
1910 case QLC_83XX_IDC_DEV_READY:
1911 ret = qlcnic_sriov_vf_idc_ready_state(adapter);
1912 break;
1913 case QLC_83XX_IDC_DEV_NEED_RESET:
1914 case QLC_83XX_IDC_DEV_INIT:
1915 ret = qlcnic_sriov_vf_idc_init_reset_state(adapter);
1916 break;
1917 case QLC_83XX_IDC_DEV_NEED_QUISCENT:
1918 ret = qlcnic_sriov_vf_idc_need_quiescent_state(adapter);
1919 break;
1920 case QLC_83XX_IDC_DEV_FAILED:
1921 ret = qlcnic_sriov_vf_idc_failed_state(adapter);
1922 break;
1923 case QLC_83XX_IDC_DEV_QUISCENT:
1924 break;
1925 default:
1926 ret = qlcnic_sriov_vf_idc_unknown_state(adapter);
1927 }
1928
1929 idc->prev_state = idc->curr_state;
Rajesh Borundiad747c332014-05-09 02:51:31 -04001930 qlcnic_sriov_vf_periodic_tasks(adapter);
1931
Rajesh Borundiaf036e4f2013-04-19 07:01:10 +00001932 if (!ret && test_bit(QLC_83XX_MODULE_LOADED, &idc->status))
1933 qlcnic_schedule_work(adapter, qlcnic_sriov_vf_poll_dev_state,
1934 idc->delay);
1935}
1936
1937static void qlcnic_sriov_vf_cancel_fw_work(struct qlcnic_adapter *adapter)
1938{
1939 while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
1940 msleep(20);
1941
1942 clear_bit(QLC_83XX_MODULE_LOADED, &adapter->ahw->idc.status);
1943 clear_bit(__QLCNIC_RESETTING, &adapter->state);
1944 cancel_delayed_work_sync(&adapter->fw_work);
1945}
Rajesh Borundia91b72822013-04-19 07:01:12 +00001946
Manish Chopra154d0c82013-12-17 09:01:53 -05001947static int qlcnic_sriov_check_vlan_id(struct qlcnic_sriov *sriov,
1948 struct qlcnic_vf_info *vf, u16 vlan_id)
1949{
1950 int i, err = -EINVAL;
1951
1952 if (!vf->sriov_vlans)
1953 return err;
1954
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001955 spin_lock_bh(&vf->vlan_list_lock);
Manish Chopra154d0c82013-12-17 09:01:53 -05001956
1957 for (i = 0; i < sriov->num_allowed_vlans; i++) {
1958 if (vf->sriov_vlans[i] == vlan_id) {
1959 err = 0;
1960 break;
1961 }
1962 }
1963
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001964 spin_unlock_bh(&vf->vlan_list_lock);
Manish Chopra154d0c82013-12-17 09:01:53 -05001965 return err;
1966}
1967
1968static int qlcnic_sriov_validate_num_vlans(struct qlcnic_sriov *sriov,
1969 struct qlcnic_vf_info *vf)
1970{
1971 int err = 0;
1972
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001973 spin_lock_bh(&vf->vlan_list_lock);
Manish Chopra154d0c82013-12-17 09:01:53 -05001974
1975 if (vf->num_vlan >= sriov->num_allowed_vlans)
1976 err = -EINVAL;
1977
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04001978 spin_unlock_bh(&vf->vlan_list_lock);
Manish Chopra154d0c82013-12-17 09:01:53 -05001979 return err;
1980}
1981
1982static int qlcnic_sriov_validate_vlan_cfg(struct qlcnic_adapter *adapter,
Rajesh Borundia91b72822013-04-19 07:01:12 +00001983 u16 vid, u8 enable)
1984{
Manish Chopra154d0c82013-12-17 09:01:53 -05001985 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
1986 struct qlcnic_vf_info *vf;
1987 bool vlan_exist;
Rajesh Borundia91b72822013-04-19 07:01:12 +00001988 u8 allowed = 0;
1989 int i;
1990
Manish Chopra154d0c82013-12-17 09:01:53 -05001991 vf = &adapter->ahw->sriov->vf_info[0];
1992 vlan_exist = qlcnic_sriov_check_any_vlan(vf);
Rajesh Borundia91b72822013-04-19 07:01:12 +00001993 if (sriov->vlan_mode != QLC_GUEST_VLAN_MODE)
1994 return -EINVAL;
1995
1996 if (enable) {
Manish Chopra154d0c82013-12-17 09:01:53 -05001997 if (qlcnic_83xx_vf_check(adapter) && vlan_exist)
1998 return -EINVAL;
1999
2000 if (qlcnic_sriov_validate_num_vlans(sriov, vf))
Rajesh Borundia91b72822013-04-19 07:01:12 +00002001 return -EINVAL;
2002
2003 if (sriov->any_vlan) {
2004 for (i = 0; i < sriov->num_allowed_vlans; i++) {
2005 if (sriov->allowed_vlans[i] == vid)
2006 allowed = 1;
2007 }
2008
2009 if (!allowed)
2010 return -EINVAL;
2011 }
2012 } else {
Manish Chopra154d0c82013-12-17 09:01:53 -05002013 if (!vlan_exist || qlcnic_sriov_check_vlan_id(sriov, vf, vid))
Rajesh Borundia91b72822013-04-19 07:01:12 +00002014 return -EINVAL;
2015 }
2016
2017 return 0;
2018}
2019
Manish Chopra154d0c82013-12-17 09:01:53 -05002020static void qlcnic_sriov_vlan_operation(struct qlcnic_vf_info *vf, u16 vlan_id,
2021 enum qlcnic_vlan_operations opcode)
2022{
2023 struct qlcnic_adapter *adapter = vf->adapter;
2024 struct qlcnic_sriov *sriov;
2025
2026 sriov = adapter->ahw->sriov;
2027
2028 if (!vf->sriov_vlans)
2029 return;
2030
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04002031 spin_lock_bh(&vf->vlan_list_lock);
Manish Chopra154d0c82013-12-17 09:01:53 -05002032
2033 switch (opcode) {
2034 case QLC_VLAN_ADD:
2035 qlcnic_sriov_add_vlan_id(sriov, vf, vlan_id);
2036 break;
2037 case QLC_VLAN_DELETE:
2038 qlcnic_sriov_del_vlan_id(sriov, vf, vlan_id);
2039 break;
2040 default:
2041 netdev_err(adapter->netdev, "Invalid VLAN operation\n");
2042 }
2043
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04002044 spin_unlock_bh(&vf->vlan_list_lock);
Manish Chopra154d0c82013-12-17 09:01:53 -05002045 return;
2046}
2047
Rajesh Borundia91b72822013-04-19 07:01:12 +00002048int qlcnic_sriov_cfg_vf_guest_vlan(struct qlcnic_adapter *adapter,
2049 u16 vid, u8 enable)
2050{
2051 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04002052 struct net_device *netdev = adapter->netdev;
Manish Chopra154d0c82013-12-17 09:01:53 -05002053 struct qlcnic_vf_info *vf;
Rajesh Borundia91b72822013-04-19 07:01:12 +00002054 struct qlcnic_cmd_args cmd;
2055 int ret;
2056
Rajesh Borundiac5316922014-06-04 03:40:54 -04002057 memset(&cmd, 0, sizeof(cmd));
Rajesh Borundia91b72822013-04-19 07:01:12 +00002058 if (vid == 0)
2059 return 0;
2060
Manish Chopra154d0c82013-12-17 09:01:53 -05002061 vf = &adapter->ahw->sriov->vf_info[0];
2062 ret = qlcnic_sriov_validate_vlan_cfg(adapter, vid, enable);
Rajesh Borundia91b72822013-04-19 07:01:12 +00002063 if (ret)
2064 return ret;
2065
2066 ret = qlcnic_sriov_alloc_bc_mbx_args(&cmd,
2067 QLCNIC_BC_CMD_CFG_GUEST_VLAN);
2068 if (ret)
2069 return ret;
2070
2071 cmd.req.arg[1] = (enable & 1) | vid << 16;
2072
2073 qlcnic_sriov_cleanup_async_list(&sriov->bc);
2074 ret = qlcnic_issue_cmd(adapter, &cmd);
2075 if (ret) {
2076 dev_err(&adapter->pdev->dev,
2077 "Failed to configure guest VLAN, err=%d\n", ret);
2078 } else {
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04002079 netif_addr_lock_bh(netdev);
Rajesh Borundia91b72822013-04-19 07:01:12 +00002080 qlcnic_free_mac_list(adapter);
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04002081 netif_addr_unlock_bh(netdev);
Rajesh Borundia91b72822013-04-19 07:01:12 +00002082
2083 if (enable)
Manish Chopra154d0c82013-12-17 09:01:53 -05002084 qlcnic_sriov_vlan_operation(vf, vid, QLC_VLAN_ADD);
Rajesh Borundia91b72822013-04-19 07:01:12 +00002085 else
Manish Chopra154d0c82013-12-17 09:01:53 -05002086 qlcnic_sriov_vlan_operation(vf, vid, QLC_VLAN_DELETE);
Rajesh Borundia91b72822013-04-19 07:01:12 +00002087
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04002088 netif_addr_lock_bh(netdev);
2089 qlcnic_set_multi(netdev);
2090 netif_addr_unlock_bh(netdev);
Rajesh Borundia91b72822013-04-19 07:01:12 +00002091 }
2092
2093 qlcnic_free_mbx_args(&cmd);
2094 return ret;
2095}
2096
2097static void qlcnic_sriov_vf_free_mac_list(struct qlcnic_adapter *adapter)
2098{
2099 struct list_head *head = &adapter->mac_list;
Manish Chopra154d0c82013-12-17 09:01:53 -05002100 struct qlcnic_mac_vlan_list *cur;
Rajesh Borundia91b72822013-04-19 07:01:12 +00002101
2102 while (!list_empty(head)) {
Manish Chopra154d0c82013-12-17 09:01:53 -05002103 cur = list_entry(head->next, struct qlcnic_mac_vlan_list, list);
2104 qlcnic_sre_macaddr_change(adapter, cur->mac_addr, cur->vlan_id,
2105 QLCNIC_MAC_DEL);
Rajesh Borundia91b72822013-04-19 07:01:12 +00002106 list_del(&cur->list);
2107 kfree(cur);
2108 }
2109}
Rajesh Borundia486a5bc2013-06-22 04:12:06 -04002110
Manish Chopra154d0c82013-12-17 09:01:53 -05002111
stephen hemminger21041402014-01-16 10:31:27 -08002112static int qlcnic_sriov_vf_shutdown(struct pci_dev *pdev)
Rajesh Borundia486a5bc2013-06-22 04:12:06 -04002113{
2114 struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
2115 struct net_device *netdev = adapter->netdev;
2116 int retval;
2117
2118 netif_device_detach(netdev);
2119 qlcnic_cancel_idc_work(adapter);
2120
2121 if (netif_running(netdev))
2122 qlcnic_down(adapter, netdev);
2123
2124 qlcnic_sriov_channel_cfg_cmd(adapter, QLCNIC_BC_CMD_CHANNEL_TERM);
2125 qlcnic_sriov_cfg_bc_intr(adapter, 0);
2126 qlcnic_83xx_disable_mbx_intr(adapter);
2127 cancel_delayed_work_sync(&adapter->idc_aen_work);
2128
2129 retval = pci_save_state(pdev);
2130 if (retval)
2131 return retval;
2132
2133 return 0;
2134}
2135
stephen hemminger21041402014-01-16 10:31:27 -08002136static int qlcnic_sriov_vf_resume(struct qlcnic_adapter *adapter)
Rajesh Borundia486a5bc2013-06-22 04:12:06 -04002137{
2138 struct qlc_83xx_idc *idc = &adapter->ahw->idc;
2139 struct net_device *netdev = adapter->netdev;
2140 int err;
2141
2142 set_bit(QLC_83XX_MODULE_LOADED, &idc->status);
Manish Choprae5c4e6c2013-08-02 00:57:40 -04002143 qlcnic_83xx_enable_mbx_interrupt(adapter);
Rajesh Borundia486a5bc2013-06-22 04:12:06 -04002144 err = qlcnic_sriov_cfg_bc_intr(adapter, 1);
2145 if (err)
2146 return err;
2147
2148 err = qlcnic_sriov_channel_cfg_cmd(adapter, QLCNIC_BC_CMD_CHANNEL_INIT);
2149 if (!err) {
2150 if (netif_running(netdev)) {
2151 err = qlcnic_up(adapter, netdev);
2152 if (!err)
2153 qlcnic_restore_indev_addr(netdev, NETDEV_UP);
2154 }
2155 }
2156
2157 netif_device_attach(netdev);
2158 qlcnic_schedule_work(adapter, qlcnic_sriov_vf_poll_dev_state,
2159 idc->delay);
2160 return err;
2161}
Manish Chopra154d0c82013-12-17 09:01:53 -05002162
2163void qlcnic_sriov_alloc_vlans(struct qlcnic_adapter *adapter)
2164{
2165 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
2166 struct qlcnic_vf_info *vf;
2167 int i;
2168
2169 for (i = 0; i < sriov->num_vfs; i++) {
2170 vf = &sriov->vf_info[i];
2171 vf->sriov_vlans = kcalloc(sriov->num_allowed_vlans,
2172 sizeof(*vf->sriov_vlans), GFP_KERNEL);
2173 }
2174}
2175
2176void qlcnic_sriov_free_vlans(struct qlcnic_adapter *adapter)
2177{
2178 struct qlcnic_sriov *sriov = adapter->ahw->sriov;
2179 struct qlcnic_vf_info *vf;
2180 int i;
2181
2182 for (i = 0; i < sriov->num_vfs; i++) {
2183 vf = &sriov->vf_info[i];
2184 kfree(vf->sriov_vlans);
2185 vf->sriov_vlans = NULL;
2186 }
2187}
2188
2189void qlcnic_sriov_add_vlan_id(struct qlcnic_sriov *sriov,
2190 struct qlcnic_vf_info *vf, u16 vlan_id)
2191{
2192 int i;
2193
2194 for (i = 0; i < sriov->num_allowed_vlans; i++) {
2195 if (!vf->sriov_vlans[i]) {
2196 vf->sriov_vlans[i] = vlan_id;
2197 vf->num_vlan++;
2198 return;
2199 }
2200 }
2201}
2202
2203void qlcnic_sriov_del_vlan_id(struct qlcnic_sriov *sriov,
2204 struct qlcnic_vf_info *vf, u16 vlan_id)
2205{
2206 int i;
2207
2208 for (i = 0; i < sriov->num_allowed_vlans; i++) {
2209 if (vf->sriov_vlans[i] == vlan_id) {
2210 vf->sriov_vlans[i] = 0;
2211 vf->num_vlan--;
2212 return;
2213 }
2214 }
2215}
2216
2217bool qlcnic_sriov_check_any_vlan(struct qlcnic_vf_info *vf)
2218{
2219 bool err = false;
2220
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04002221 spin_lock_bh(&vf->vlan_list_lock);
Manish Chopra154d0c82013-12-17 09:01:53 -05002222
2223 if (vf->num_vlan)
2224 err = true;
2225
Rajesh Borundia74b7ba12014-05-09 02:51:30 -04002226 spin_unlock_bh(&vf->vlan_list_lock);
Manish Chopra154d0c82013-12-17 09:01:53 -05002227 return err;
2228}