1 /* bnx2x_sriov.c: Broadcom Everest network driver.
2  *
3  * Copyright 2009-2012 Broadcom Corporation
4  *
5  * Unless you and Broadcom execute a separate written software license
6  * agreement governing use of this software, this software is licensed to you
7  * under the terms of the GNU General Public License version 2, available
8  * at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html (the "GPL").
9  *
10  * Notwithstanding the above, under no circumstances may you combine this
11  * software in any way with any other Broadcom software provided under a
12  * license other than the GPL, without Broadcom's express prior written
13  * consent.
14  *
15  * Maintained by: Eilon Greenstein <eilong@broadcom.com>
16  * Written by: Shmulik Ravid <shmulikr@broadcom.com>
17  *	       Ariel Elior <ariele@broadcom.com>
18  *
19  */
20 #include "bnx2x.h"
21 #include "bnx2x_init.h"
22 #include "bnx2x_cmn.h"
23 #include <linux/crc32.h>
24 
25 /* General service functions */
26 static void storm_memset_vf_to_pf(struct bnx2x *bp, u16 abs_fid,
27 					 u16 pf_id)
28 {
29 	REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid),
30 		pf_id);
31 	REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid),
32 		pf_id);
33 	REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid),
34 		pf_id);
35 	REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid),
36 		pf_id);
37 }
38 
39 static void storm_memset_func_en(struct bnx2x *bp, u16 abs_fid,
40 					u8 enable)
41 {
42 	REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid),
43 		enable);
44 	REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid),
45 		enable);
46 	REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid),
47 		enable);
48 	REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid),
49 		enable);
50 }
51 
52 int bnx2x_vf_idx_by_abs_fid(struct bnx2x *bp, u16 abs_vfid)
53 {
54 	int idx;
55 
56 	for_each_vf(bp, idx)
57 		if (bnx2x_vf(bp, idx, abs_vfid) == abs_vfid)
58 			break;
59 	return idx;
60 }
61 
62 static
63 struct bnx2x_virtf *bnx2x_vf_by_abs_fid(struct bnx2x *bp, u16 abs_vfid)
64 {
65 	u16 idx =  (u16)bnx2x_vf_idx_by_abs_fid(bp, abs_vfid);
66 	return (idx < BNX2X_NR_VIRTFN(bp)) ? BP_VF(bp, idx) : NULL;
67 }
68 
69 static void bnx2x_vf_igu_ack_sb(struct bnx2x *bp, struct bnx2x_virtf *vf,
70 				u8 igu_sb_id, u8 segment, u16 index, u8 op,
71 				u8 update)
72 {
73 	/* acking a VF sb through the PF - use the GRC */
74 	u32 ctl;
75 	u32 igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
76 	u32 igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
77 	u32 func_encode = vf->abs_vfid;
78 	u32 addr_encode = IGU_CMD_E2_PROD_UPD_BASE + igu_sb_id;
79 	struct igu_regular cmd_data = {0};
80 
81 	cmd_data.sb_id_and_flags =
82 			((index << IGU_REGULAR_SB_INDEX_SHIFT) |
83 			 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
84 			 (update << IGU_REGULAR_BUPDATE_SHIFT) |
85 			 (op << IGU_REGULAR_ENABLE_INT_SHIFT));
86 
87 	ctl = addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT		|
88 	      func_encode << IGU_CTRL_REG_FID_SHIFT		|
89 	      IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT;
90 
91 	DP(NETIF_MSG_HW, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
92 	   cmd_data.sb_id_and_flags, igu_addr_data);
93 	REG_WR(bp, igu_addr_data, cmd_data.sb_id_and_flags);
94 	mmiowb();
95 	barrier();
96 
97 	DP(NETIF_MSG_HW, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
98 	   ctl, igu_addr_ctl);
99 	REG_WR(bp, igu_addr_ctl, ctl);
100 	mmiowb();
101 	barrier();
102 }
103 /* VFOP - VF slow-path operation support */
104 
105 #define BNX2X_VFOP_FILTER_ADD_CNT_MAX		0x10000
106 
107 /* VFOP operations states */
108 enum bnx2x_vfop_qctor_state {
109 	   BNX2X_VFOP_QCTOR_INIT,
110 	   BNX2X_VFOP_QCTOR_SETUP,
111 	   BNX2X_VFOP_QCTOR_INT_EN
112 };
113 
114 enum bnx2x_vfop_qdtor_state {
115 	   BNX2X_VFOP_QDTOR_HALT,
116 	   BNX2X_VFOP_QDTOR_TERMINATE,
117 	   BNX2X_VFOP_QDTOR_CFCDEL,
118 	   BNX2X_VFOP_QDTOR_DONE
119 };
120 
121 enum bnx2x_vfop_vlan_mac_state {
122 	   BNX2X_VFOP_VLAN_MAC_CONFIG_SINGLE,
123 	   BNX2X_VFOP_VLAN_MAC_CLEAR,
124 	   BNX2X_VFOP_VLAN_MAC_CHK_DONE,
125 	   BNX2X_VFOP_MAC_CONFIG_LIST,
126 	   BNX2X_VFOP_VLAN_CONFIG_LIST,
127 	   BNX2X_VFOP_VLAN_CONFIG_LIST_0
128 };
129 
130 enum bnx2x_vfop_qsetup_state {
131 	   BNX2X_VFOP_QSETUP_CTOR,
132 	   BNX2X_VFOP_QSETUP_VLAN0,
133 	   BNX2X_VFOP_QSETUP_DONE
134 };
135 
136 enum bnx2x_vfop_mcast_state {
137 	   BNX2X_VFOP_MCAST_DEL,
138 	   BNX2X_VFOP_MCAST_ADD,
139 	   BNX2X_VFOP_MCAST_CHK_DONE
140 };
141 enum bnx2x_vfop_qflr_state {
142 	   BNX2X_VFOP_QFLR_CLR_VLAN,
143 	   BNX2X_VFOP_QFLR_CLR_MAC,
144 	   BNX2X_VFOP_QFLR_TERMINATE,
145 	   BNX2X_VFOP_QFLR_DONE
146 };
147 
148 enum bnx2x_vfop_flr_state {
149 	   BNX2X_VFOP_FLR_QUEUES,
150 	   BNX2X_VFOP_FLR_HW
151 };
152 
153 enum bnx2x_vfop_close_state {
154 	   BNX2X_VFOP_CLOSE_QUEUES,
155 	   BNX2X_VFOP_CLOSE_HW
156 };
157 
158 enum bnx2x_vfop_rxmode_state {
159 	   BNX2X_VFOP_RXMODE_CONFIG,
160 	   BNX2X_VFOP_RXMODE_DONE
161 };
162 
163 enum bnx2x_vfop_qteardown_state {
164 	   BNX2X_VFOP_QTEARDOWN_RXMODE,
165 	   BNX2X_VFOP_QTEARDOWN_CLR_VLAN,
166 	   BNX2X_VFOP_QTEARDOWN_CLR_MAC,
167 	   BNX2X_VFOP_QTEARDOWN_QDTOR,
168 	   BNX2X_VFOP_QTEARDOWN_DONE
169 };
170 
171 #define bnx2x_vfop_reset_wq(vf)	atomic_set(&vf->op_in_progress, 0)
172 
173 void bnx2x_vfop_qctor_dump_tx(struct bnx2x *bp, struct bnx2x_virtf *vf,
174 			      struct bnx2x_queue_init_params *init_params,
175 			      struct bnx2x_queue_setup_params *setup_params,
176 			      u16 q_idx, u16 sb_idx)
177 {
178 	DP(BNX2X_MSG_IOV,
179 	   "VF[%d] Q_SETUP: txq[%d]-- vfsb=%d, sb-index=%d, hc-rate=%d, flags=0x%lx, traffic-type=%d",
180 	   vf->abs_vfid,
181 	   q_idx,
182 	   sb_idx,
183 	   init_params->tx.sb_cq_index,
184 	   init_params->tx.hc_rate,
185 	   setup_params->flags,
186 	   setup_params->txq_params.traffic_type);
187 }
188 
189 void bnx2x_vfop_qctor_dump_rx(struct bnx2x *bp, struct bnx2x_virtf *vf,
190 			    struct bnx2x_queue_init_params *init_params,
191 			    struct bnx2x_queue_setup_params *setup_params,
192 			    u16 q_idx, u16 sb_idx)
193 {
194 	struct bnx2x_rxq_setup_params *rxq_params = &setup_params->rxq_params;
195 
196 	DP(BNX2X_MSG_IOV, "VF[%d] Q_SETUP: rxq[%d]-- vfsb=%d, sb-index=%d, hc-rate=%d, mtu=%d, buf-size=%d\n"
197 	   "sge-size=%d, max_sge_pkt=%d, tpa-agg-size=%d, flags=0x%lx, drop-flags=0x%x, cache-log=%d\n",
198 	   vf->abs_vfid,
199 	   q_idx,
200 	   sb_idx,
201 	   init_params->rx.sb_cq_index,
202 	   init_params->rx.hc_rate,
203 	   setup_params->gen_params.mtu,
204 	   rxq_params->buf_sz,
205 	   rxq_params->sge_buf_sz,
206 	   rxq_params->max_sges_pkt,
207 	   rxq_params->tpa_agg_sz,
208 	   setup_params->flags,
209 	   rxq_params->drop_flags,
210 	   rxq_params->cache_line_log);
211 }
212 
213 void bnx2x_vfop_qctor_prep(struct bnx2x *bp,
214 			   struct bnx2x_virtf *vf,
215 			   struct bnx2x_vf_queue *q,
216 			   struct bnx2x_vfop_qctor_params *p,
217 			   unsigned long q_type)
218 {
219 	struct bnx2x_queue_init_params *init_p = &p->qstate.params.init;
220 	struct bnx2x_queue_setup_params *setup_p = &p->prep_qsetup;
221 
222 	/* INIT */
223 
224 	/* Enable host coalescing in the transition to INIT state */
225 	if (test_bit(BNX2X_Q_FLG_HC, &init_p->rx.flags))
226 		__set_bit(BNX2X_Q_FLG_HC_EN, &init_p->rx.flags);
227 
228 	if (test_bit(BNX2X_Q_FLG_HC, &init_p->tx.flags))
229 		__set_bit(BNX2X_Q_FLG_HC_EN, &init_p->tx.flags);
230 
231 	/* FW SB ID */
232 	init_p->rx.fw_sb_id = vf_igu_sb(vf, q->sb_idx);
233 	init_p->tx.fw_sb_id = vf_igu_sb(vf, q->sb_idx);
234 
235 	/* context */
236 	init_p->cxts[0] = q->cxt;
237 
238 	/* SETUP */
239 
240 	/* Setup-op general parameters */
241 	setup_p->gen_params.spcl_id = vf->sp_cl_id;
242 	setup_p->gen_params.stat_id = vfq_stat_id(vf, q);
243 
244 	/* Setup-op pause params:
245 	 * Nothing to do, the pause thresholds are set by default to 0 which
246 	 * effectively turns off the feature for this queue. We don't want
247 	 * one queue (VF) to interfering with another queue (another VF)
248 	 */
249 	if (vf->cfg_flags & VF_CFG_FW_FC)
250 		BNX2X_ERR("No support for pause to VFs (abs_vfid: %d)\n",
251 			  vf->abs_vfid);
252 	/* Setup-op flags:
253 	 * collect statistics, zero statistics, local-switching, security,
254 	 * OV for Flex10, RSS and MCAST for leading
255 	 */
256 	if (test_bit(BNX2X_Q_FLG_STATS, &setup_p->flags))
257 		__set_bit(BNX2X_Q_FLG_ZERO_STATS, &setup_p->flags);
258 
259 	/* for VFs, enable tx switching, bd coherency, and mac address
260 	 * anti-spoofing
261 	 */
262 	__set_bit(BNX2X_Q_FLG_TX_SWITCH, &setup_p->flags);
263 	__set_bit(BNX2X_Q_FLG_TX_SEC, &setup_p->flags);
264 	__set_bit(BNX2X_Q_FLG_ANTI_SPOOF, &setup_p->flags);
265 
266 	if (vfq_is_leading(q)) {
267 		__set_bit(BNX2X_Q_FLG_LEADING_RSS, &setup_p->flags);
268 		__set_bit(BNX2X_Q_FLG_MCAST, &setup_p->flags);
269 	}
270 
271 	/* Setup-op rx parameters */
272 	if (test_bit(BNX2X_Q_TYPE_HAS_RX, &q_type)) {
273 		struct bnx2x_rxq_setup_params *rxq_p = &setup_p->rxq_params;
274 
275 		rxq_p->cl_qzone_id = vfq_qzone_id(vf, q);
276 		rxq_p->fw_sb_id = vf_igu_sb(vf, q->sb_idx);
277 		rxq_p->rss_engine_id = FW_VF_HANDLE(vf->abs_vfid);
278 
279 		if (test_bit(BNX2X_Q_FLG_TPA, &setup_p->flags))
280 			rxq_p->max_tpa_queues = BNX2X_VF_MAX_TPA_AGG_QUEUES;
281 	}
282 
283 	/* Setup-op tx parameters */
284 	if (test_bit(BNX2X_Q_TYPE_HAS_TX, &q_type)) {
285 		setup_p->txq_params.tss_leading_cl_id = vf->leading_rss;
286 		setup_p->txq_params.fw_sb_id = vf_igu_sb(vf, q->sb_idx);
287 	}
288 }
289 
290 /* VFOP queue construction */
291 static void bnx2x_vfop_qctor(struct bnx2x *bp, struct bnx2x_virtf *vf)
292 {
293 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
294 	struct bnx2x_vfop_args_qctor *args = &vfop->args.qctor;
295 	struct bnx2x_queue_state_params *q_params = &vfop->op_p->qctor.qstate;
296 	enum bnx2x_vfop_qctor_state state = vfop->state;
297 
298 	bnx2x_vfop_reset_wq(vf);
299 
300 	if (vfop->rc < 0)
301 		goto op_err;
302 
303 	DP(BNX2X_MSG_IOV, "vf[%d] STATE: %d\n", vf->abs_vfid, state);
304 
305 	switch (state) {
306 	case BNX2X_VFOP_QCTOR_INIT:
307 
308 		/* has this queue already been opened? */
309 		if (bnx2x_get_q_logical_state(bp, q_params->q_obj) ==
310 		    BNX2X_Q_LOGICAL_STATE_ACTIVE) {
311 			DP(BNX2X_MSG_IOV,
312 			   "Entered qctor but queue was already up. Aborting gracefully\n");
313 			goto op_done;
314 		}
315 
316 		/* next state */
317 		vfop->state = BNX2X_VFOP_QCTOR_SETUP;
318 
319 		q_params->cmd = BNX2X_Q_CMD_INIT;
320 		vfop->rc = bnx2x_queue_state_change(bp, q_params);
321 
322 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_CONT);
323 
324 	case BNX2X_VFOP_QCTOR_SETUP:
325 		/* next state */
326 		vfop->state = BNX2X_VFOP_QCTOR_INT_EN;
327 
328 		/* copy pre-prepared setup params to the queue-state params */
329 		vfop->op_p->qctor.qstate.params.setup =
330 			vfop->op_p->qctor.prep_qsetup;
331 
332 		q_params->cmd = BNX2X_Q_CMD_SETUP;
333 		vfop->rc = bnx2x_queue_state_change(bp, q_params);
334 
335 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_CONT);
336 
337 	case BNX2X_VFOP_QCTOR_INT_EN:
338 
339 		/* enable interrupts */
340 		bnx2x_vf_igu_ack_sb(bp, vf, vf_igu_sb(vf, args->sb_idx),
341 				    USTORM_ID, 0, IGU_INT_ENABLE, 0);
342 		goto op_done;
343 	default:
344 		bnx2x_vfop_default(state);
345 	}
346 op_err:
347 	BNX2X_ERR("QCTOR[%d:%d] error: cmd %d, rc %d\n",
348 		  vf->abs_vfid, args->qid, q_params->cmd, vfop->rc);
349 op_done:
350 	bnx2x_vfop_end(bp, vf, vfop);
351 op_pending:
352 	return;
353 }
354 
355 static int bnx2x_vfop_qctor_cmd(struct bnx2x *bp,
356 				struct bnx2x_virtf *vf,
357 				struct bnx2x_vfop_cmd *cmd,
358 				int qid)
359 {
360 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
361 
362 	if (vfop) {
363 		vf->op_params.qctor.qstate.q_obj = &bnx2x_vfq(vf, qid, sp_obj);
364 
365 		vfop->args.qctor.qid = qid;
366 		vfop->args.qctor.sb_idx = bnx2x_vfq(vf, qid, sb_idx);
367 
368 		bnx2x_vfop_opset(BNX2X_VFOP_QCTOR_INIT,
369 				 bnx2x_vfop_qctor, cmd->done);
370 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_qctor,
371 					     cmd->block);
372 	}
373 	return -ENOMEM;
374 }
375 
376 /* VFOP queue destruction */
377 static void bnx2x_vfop_qdtor(struct bnx2x *bp, struct bnx2x_virtf *vf)
378 {
379 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
380 	struct bnx2x_vfop_args_qdtor *qdtor = &vfop->args.qdtor;
381 	struct bnx2x_queue_state_params *q_params = &vfop->op_p->qctor.qstate;
382 	enum bnx2x_vfop_qdtor_state state = vfop->state;
383 
384 	bnx2x_vfop_reset_wq(vf);
385 
386 	if (vfop->rc < 0)
387 		goto op_err;
388 
389 	DP(BNX2X_MSG_IOV, "vf[%d] STATE: %d\n", vf->abs_vfid, state);
390 
391 	switch (state) {
392 	case BNX2X_VFOP_QDTOR_HALT:
393 
394 		/* has this queue already been stopped? */
395 		if (bnx2x_get_q_logical_state(bp, q_params->q_obj) ==
396 		    BNX2X_Q_LOGICAL_STATE_STOPPED) {
397 			DP(BNX2X_MSG_IOV,
398 			   "Entered qdtor but queue was already stopped. Aborting gracefully\n");
399 			goto op_done;
400 		}
401 
402 		/* next state */
403 		vfop->state = BNX2X_VFOP_QDTOR_TERMINATE;
404 
405 		q_params->cmd = BNX2X_Q_CMD_HALT;
406 		vfop->rc = bnx2x_queue_state_change(bp, q_params);
407 
408 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_CONT);
409 
410 	case BNX2X_VFOP_QDTOR_TERMINATE:
411 		/* next state */
412 		vfop->state = BNX2X_VFOP_QDTOR_CFCDEL;
413 
414 		q_params->cmd = BNX2X_Q_CMD_TERMINATE;
415 		vfop->rc = bnx2x_queue_state_change(bp, q_params);
416 
417 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_CONT);
418 
419 	case BNX2X_VFOP_QDTOR_CFCDEL:
420 		/* next state */
421 		vfop->state = BNX2X_VFOP_QDTOR_DONE;
422 
423 		q_params->cmd = BNX2X_Q_CMD_CFC_DEL;
424 		vfop->rc = bnx2x_queue_state_change(bp, q_params);
425 
426 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_DONE);
427 op_err:
428 	BNX2X_ERR("QDTOR[%d:%d] error: cmd %d, rc %d\n",
429 		  vf->abs_vfid, qdtor->qid, q_params->cmd, vfop->rc);
430 op_done:
431 	case BNX2X_VFOP_QDTOR_DONE:
432 		/* invalidate the context */
433 		qdtor->cxt->ustorm_ag_context.cdu_usage = 0;
434 		qdtor->cxt->xstorm_ag_context.cdu_reserved = 0;
435 		bnx2x_vfop_end(bp, vf, vfop);
436 		return;
437 	default:
438 		bnx2x_vfop_default(state);
439 	}
440 op_pending:
441 	return;
442 }
443 
444 static int bnx2x_vfop_qdtor_cmd(struct bnx2x *bp,
445 				struct bnx2x_virtf *vf,
446 				struct bnx2x_vfop_cmd *cmd,
447 				int qid)
448 {
449 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
450 
451 	if (vfop) {
452 		struct bnx2x_queue_state_params *qstate =
453 			&vf->op_params.qctor.qstate;
454 
455 		memset(qstate, 0, sizeof(*qstate));
456 		qstate->q_obj = &bnx2x_vfq(vf, qid, sp_obj);
457 
458 		vfop->args.qdtor.qid = qid;
459 		vfop->args.qdtor.cxt = bnx2x_vfq(vf, qid, cxt);
460 
461 		bnx2x_vfop_opset(BNX2X_VFOP_QDTOR_HALT,
462 				 bnx2x_vfop_qdtor, cmd->done);
463 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_qdtor,
464 					     cmd->block);
465 	}
466 	DP(BNX2X_MSG_IOV, "VF[%d] failed to add a vfop. rc %d\n",
467 	   vf->abs_vfid, vfop->rc);
468 	return -ENOMEM;
469 }
470 
471 static void
472 bnx2x_vf_set_igu_info(struct bnx2x *bp, u8 igu_sb_id, u8 abs_vfid)
473 {
474 	struct bnx2x_virtf *vf = bnx2x_vf_by_abs_fid(bp, abs_vfid);
475 	if (vf) {
476 		if (!vf_sb_count(vf))
477 			vf->igu_base_id = igu_sb_id;
478 		++vf_sb_count(vf);
479 	}
480 }
481 
482 /* VFOP MAC/VLAN helpers */
483 static inline void bnx2x_vfop_credit(struct bnx2x *bp,
484 				     struct bnx2x_vfop *vfop,
485 				     struct bnx2x_vlan_mac_obj *obj)
486 {
487 	struct bnx2x_vfop_args_filters *args = &vfop->args.filters;
488 
489 	/* update credit only if there is no error
490 	 * and a valid credit counter
491 	 */
492 	if (!vfop->rc && args->credit) {
493 		int cnt = 0;
494 		struct list_head *pos;
495 
496 		list_for_each(pos, &obj->head)
497 			cnt++;
498 
499 		atomic_set(args->credit, cnt);
500 	}
501 }
502 
503 static int bnx2x_vfop_set_user_req(struct bnx2x *bp,
504 				    struct bnx2x_vfop_filter *pos,
505 				    struct bnx2x_vlan_mac_data *user_req)
506 {
507 	user_req->cmd = pos->add ? BNX2X_VLAN_MAC_ADD :
508 		BNX2X_VLAN_MAC_DEL;
509 
510 	switch (pos->type) {
511 	case BNX2X_VFOP_FILTER_MAC:
512 		memcpy(user_req->u.mac.mac, pos->mac, ETH_ALEN);
513 		break;
514 	case BNX2X_VFOP_FILTER_VLAN:
515 		user_req->u.vlan.vlan = pos->vid;
516 		break;
517 	default:
518 		BNX2X_ERR("Invalid filter type, skipping\n");
519 		return 1;
520 	}
521 	return 0;
522 }
523 
524 static int
525 bnx2x_vfop_config_vlan0(struct bnx2x *bp,
526 			struct bnx2x_vlan_mac_ramrod_params *vlan_mac,
527 			bool add)
528 {
529 	int rc;
530 
531 	vlan_mac->user_req.cmd = add ? BNX2X_VLAN_MAC_ADD :
532 		BNX2X_VLAN_MAC_DEL;
533 	vlan_mac->user_req.u.vlan.vlan = 0;
534 
535 	rc = bnx2x_config_vlan_mac(bp, vlan_mac);
536 	if (rc == -EEXIST)
537 		rc = 0;
538 	return rc;
539 }
540 
541 static int bnx2x_vfop_config_list(struct bnx2x *bp,
542 				  struct bnx2x_vfop_filters *filters,
543 				  struct bnx2x_vlan_mac_ramrod_params *vlan_mac)
544 {
545 	struct bnx2x_vfop_filter *pos, *tmp;
546 	struct list_head rollback_list, *filters_list = &filters->head;
547 	struct bnx2x_vlan_mac_data *user_req = &vlan_mac->user_req;
548 	int rc = 0, cnt = 0;
549 
550 	INIT_LIST_HEAD(&rollback_list);
551 
552 	list_for_each_entry_safe(pos, tmp, filters_list, link) {
553 		if (bnx2x_vfop_set_user_req(bp, pos, user_req))
554 			continue;
555 
556 		rc = bnx2x_config_vlan_mac(bp, vlan_mac);
557 		if (rc >= 0) {
558 			cnt += pos->add ? 1 : -1;
559 			list_del(&pos->link);
560 			list_add(&pos->link, &rollback_list);
561 			rc = 0;
562 		} else if (rc == -EEXIST) {
563 			rc = 0;
564 		} else {
565 			BNX2X_ERR("Failed to add a new vlan_mac command\n");
566 			break;
567 		}
568 	}
569 
570 	/* rollback if error or too many rules added */
571 	if (rc || cnt > filters->add_cnt) {
572 		BNX2X_ERR("error or too many rules added. Performing rollback\n");
573 		list_for_each_entry_safe(pos, tmp, &rollback_list, link) {
574 			pos->add = !pos->add;	/* reverse op */
575 			bnx2x_vfop_set_user_req(bp, pos, user_req);
576 			bnx2x_config_vlan_mac(bp, vlan_mac);
577 			list_del(&pos->link);
578 		}
579 		cnt = 0;
580 		if (!rc)
581 			rc = -EINVAL;
582 	}
583 	filters->add_cnt = cnt;
584 	return rc;
585 }
586 
587 /* VFOP set VLAN/MAC */
588 static void bnx2x_vfop_vlan_mac(struct bnx2x *bp, struct bnx2x_virtf *vf)
589 {
590 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
591 	struct bnx2x_vlan_mac_ramrod_params *vlan_mac = &vfop->op_p->vlan_mac;
592 	struct bnx2x_vlan_mac_obj *obj = vlan_mac->vlan_mac_obj;
593 	struct bnx2x_vfop_filters *filters = vfop->args.filters.multi_filter;
594 
595 	enum bnx2x_vfop_vlan_mac_state state = vfop->state;
596 
597 	if (vfop->rc < 0)
598 		goto op_err;
599 
600 	DP(BNX2X_MSG_IOV, "vf[%d] STATE: %d\n", vf->abs_vfid, state);
601 
602 	bnx2x_vfop_reset_wq(vf);
603 
604 	switch (state) {
605 	case BNX2X_VFOP_VLAN_MAC_CLEAR:
606 		/* next state */
607 		vfop->state = BNX2X_VFOP_VLAN_MAC_CHK_DONE;
608 
609 		/* do delete */
610 		vfop->rc = obj->delete_all(bp, obj,
611 					   &vlan_mac->user_req.vlan_mac_flags,
612 					   &vlan_mac->ramrod_flags);
613 
614 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_DONE);
615 
616 	case BNX2X_VFOP_VLAN_MAC_CONFIG_SINGLE:
617 		/* next state */
618 		vfop->state = BNX2X_VFOP_VLAN_MAC_CHK_DONE;
619 
620 		/* do config */
621 		vfop->rc = bnx2x_config_vlan_mac(bp, vlan_mac);
622 		if (vfop->rc == -EEXIST)
623 			vfop->rc = 0;
624 
625 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_DONE);
626 
627 	case BNX2X_VFOP_VLAN_MAC_CHK_DONE:
628 		vfop->rc = !!obj->raw.check_pending(&obj->raw);
629 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_DONE);
630 
631 	case BNX2X_VFOP_MAC_CONFIG_LIST:
632 		/* next state */
633 		vfop->state = BNX2X_VFOP_VLAN_MAC_CHK_DONE;
634 
635 		/* do list config */
636 		vfop->rc = bnx2x_vfop_config_list(bp, filters, vlan_mac);
637 		if (vfop->rc)
638 			goto op_err;
639 
640 		set_bit(RAMROD_CONT, &vlan_mac->ramrod_flags);
641 		vfop->rc = bnx2x_config_vlan_mac(bp, vlan_mac);
642 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_DONE);
643 
644 	case BNX2X_VFOP_VLAN_CONFIG_LIST:
645 		/* next state */
646 		vfop->state = BNX2X_VFOP_VLAN_CONFIG_LIST_0;
647 
648 		/* remove vlan0 - could be no-op */
649 		vfop->rc = bnx2x_vfop_config_vlan0(bp, vlan_mac, false);
650 		if (vfop->rc)
651 			goto op_err;
652 
653 		/* Do vlan list config. if this operation fails we try to
654 		 * restore vlan0 to keep the queue is working order
655 		 */
656 		vfop->rc = bnx2x_vfop_config_list(bp, filters, vlan_mac);
657 		if (!vfop->rc) {
658 			set_bit(RAMROD_CONT, &vlan_mac->ramrod_flags);
659 			vfop->rc = bnx2x_config_vlan_mac(bp, vlan_mac);
660 		}
661 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_CONT); /* fall-through */
662 
663 	case BNX2X_VFOP_VLAN_CONFIG_LIST_0:
664 		/* next state */
665 		vfop->state = BNX2X_VFOP_VLAN_MAC_CHK_DONE;
666 
667 		if (list_empty(&obj->head))
668 			/* add vlan0 */
669 			vfop->rc = bnx2x_vfop_config_vlan0(bp, vlan_mac, true);
670 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_DONE);
671 
672 	default:
673 		bnx2x_vfop_default(state);
674 	}
675 op_err:
676 	BNX2X_ERR("VLAN-MAC error: rc %d\n", vfop->rc);
677 op_done:
678 	kfree(filters);
679 	bnx2x_vfop_credit(bp, vfop, obj);
680 	bnx2x_vfop_end(bp, vf, vfop);
681 op_pending:
682 	return;
683 }
684 
685 struct bnx2x_vfop_vlan_mac_flags {
686 	bool drv_only;
687 	bool dont_consume;
688 	bool single_cmd;
689 	bool add;
690 };
691 
692 static void
693 bnx2x_vfop_vlan_mac_prep_ramrod(struct bnx2x_vlan_mac_ramrod_params *ramrod,
694 				struct bnx2x_vfop_vlan_mac_flags *flags)
695 {
696 	struct bnx2x_vlan_mac_data *ureq = &ramrod->user_req;
697 
698 	memset(ramrod, 0, sizeof(*ramrod));
699 
700 	/* ramrod flags */
701 	if (flags->drv_only)
702 		set_bit(RAMROD_DRV_CLR_ONLY, &ramrod->ramrod_flags);
703 	if (flags->single_cmd)
704 		set_bit(RAMROD_EXEC, &ramrod->ramrod_flags);
705 
706 	/* mac_vlan flags */
707 	if (flags->dont_consume)
708 		set_bit(BNX2X_DONT_CONSUME_CAM_CREDIT, &ureq->vlan_mac_flags);
709 
710 	/* cmd */
711 	ureq->cmd = flags->add ? BNX2X_VLAN_MAC_ADD : BNX2X_VLAN_MAC_DEL;
712 }
713 
714 static inline void
715 bnx2x_vfop_mac_prep_ramrod(struct bnx2x_vlan_mac_ramrod_params *ramrod,
716 			   struct bnx2x_vfop_vlan_mac_flags *flags)
717 {
718 	bnx2x_vfop_vlan_mac_prep_ramrod(ramrod, flags);
719 	set_bit(BNX2X_ETH_MAC, &ramrod->user_req.vlan_mac_flags);
720 }
721 
722 static int bnx2x_vfop_mac_delall_cmd(struct bnx2x *bp,
723 				     struct bnx2x_virtf *vf,
724 				     struct bnx2x_vfop_cmd *cmd,
725 				     int qid, bool drv_only)
726 {
727 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
728 
729 	if (vfop) {
730 		struct bnx2x_vfop_args_filters filters = {
731 			.multi_filter = NULL,	/* single */
732 			.credit = NULL,		/* consume credit */
733 		};
734 		struct bnx2x_vfop_vlan_mac_flags flags = {
735 			.drv_only = drv_only,
736 			.dont_consume = (filters.credit != NULL),
737 			.single_cmd = true,
738 			.add = false /* don't care */,
739 		};
740 		struct bnx2x_vlan_mac_ramrod_params *ramrod =
741 			&vf->op_params.vlan_mac;
742 
743 		/* set ramrod params */
744 		bnx2x_vfop_mac_prep_ramrod(ramrod, &flags);
745 
746 		/* set object */
747 		ramrod->vlan_mac_obj = &bnx2x_vfq(vf, qid, mac_obj);
748 
749 		/* set extra args */
750 		vfop->args.filters = filters;
751 
752 		bnx2x_vfop_opset(BNX2X_VFOP_VLAN_MAC_CLEAR,
753 				 bnx2x_vfop_vlan_mac, cmd->done);
754 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_vlan_mac,
755 					     cmd->block);
756 	}
757 	return -ENOMEM;
758 }
759 
760 int bnx2x_vfop_mac_list_cmd(struct bnx2x *bp,
761 			    struct bnx2x_virtf *vf,
762 			    struct bnx2x_vfop_cmd *cmd,
763 			    struct bnx2x_vfop_filters *macs,
764 			    int qid, bool drv_only)
765 {
766 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
767 
768 	if (vfop) {
769 		struct bnx2x_vfop_args_filters filters = {
770 			.multi_filter = macs,
771 			.credit = NULL,		/* consume credit */
772 		};
773 		struct bnx2x_vfop_vlan_mac_flags flags = {
774 			.drv_only = drv_only,
775 			.dont_consume = (filters.credit != NULL),
776 			.single_cmd = false,
777 			.add = false, /* don't care since only the items in the
778 				       * filters list affect the sp operation,
779 				       * not the list itself
780 				       */
781 		};
782 		struct bnx2x_vlan_mac_ramrod_params *ramrod =
783 			&vf->op_params.vlan_mac;
784 
785 		/* set ramrod params */
786 		bnx2x_vfop_mac_prep_ramrod(ramrod, &flags);
787 
788 		/* set object */
789 		ramrod->vlan_mac_obj = &bnx2x_vfq(vf, qid, mac_obj);
790 
791 		/* set extra args */
792 		filters.multi_filter->add_cnt = BNX2X_VFOP_FILTER_ADD_CNT_MAX;
793 		vfop->args.filters = filters;
794 
795 		bnx2x_vfop_opset(BNX2X_VFOP_MAC_CONFIG_LIST,
796 				 bnx2x_vfop_vlan_mac, cmd->done);
797 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_vlan_mac,
798 					     cmd->block);
799 	}
800 	return -ENOMEM;
801 }
802 
803 int bnx2x_vfop_vlan_set_cmd(struct bnx2x *bp,
804 			    struct bnx2x_virtf *vf,
805 			    struct bnx2x_vfop_cmd *cmd,
806 			    int qid, u16 vid, bool add)
807 {
808 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
809 
810 	if (vfop) {
811 		struct bnx2x_vfop_args_filters filters = {
812 			.multi_filter = NULL, /* single command */
813 			.credit = &bnx2x_vfq(vf, qid, vlan_count),
814 		};
815 		struct bnx2x_vfop_vlan_mac_flags flags = {
816 			.drv_only = false,
817 			.dont_consume = (filters.credit != NULL),
818 			.single_cmd = true,
819 			.add = add,
820 		};
821 		struct bnx2x_vlan_mac_ramrod_params *ramrod =
822 			&vf->op_params.vlan_mac;
823 
824 		/* set ramrod params */
825 		bnx2x_vfop_vlan_mac_prep_ramrod(ramrod, &flags);
826 		ramrod->user_req.u.vlan.vlan = vid;
827 
828 		/* set object */
829 		ramrod->vlan_mac_obj = &bnx2x_vfq(vf, qid, vlan_obj);
830 
831 		/* set extra args */
832 		vfop->args.filters = filters;
833 
834 		bnx2x_vfop_opset(BNX2X_VFOP_VLAN_MAC_CONFIG_SINGLE,
835 				 bnx2x_vfop_vlan_mac, cmd->done);
836 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_vlan_mac,
837 					     cmd->block);
838 	}
839 	return -ENOMEM;
840 }
841 
842 static int bnx2x_vfop_vlan_delall_cmd(struct bnx2x *bp,
843 			       struct bnx2x_virtf *vf,
844 			       struct bnx2x_vfop_cmd *cmd,
845 			       int qid, bool drv_only)
846 {
847 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
848 
849 	if (vfop) {
850 		struct bnx2x_vfop_args_filters filters = {
851 			.multi_filter = NULL, /* single command */
852 			.credit = &bnx2x_vfq(vf, qid, vlan_count),
853 		};
854 		struct bnx2x_vfop_vlan_mac_flags flags = {
855 			.drv_only = drv_only,
856 			.dont_consume = (filters.credit != NULL),
857 			.single_cmd = true,
858 			.add = false, /* don't care */
859 		};
860 		struct bnx2x_vlan_mac_ramrod_params *ramrod =
861 			&vf->op_params.vlan_mac;
862 
863 		/* set ramrod params */
864 		bnx2x_vfop_vlan_mac_prep_ramrod(ramrod, &flags);
865 
866 		/* set object */
867 		ramrod->vlan_mac_obj = &bnx2x_vfq(vf, qid, vlan_obj);
868 
869 		/* set extra args */
870 		vfop->args.filters = filters;
871 
872 		bnx2x_vfop_opset(BNX2X_VFOP_VLAN_MAC_CLEAR,
873 				 bnx2x_vfop_vlan_mac, cmd->done);
874 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_vlan_mac,
875 					     cmd->block);
876 	}
877 	return -ENOMEM;
878 }
879 
880 int bnx2x_vfop_vlan_list_cmd(struct bnx2x *bp,
881 			     struct bnx2x_virtf *vf,
882 			     struct bnx2x_vfop_cmd *cmd,
883 			     struct bnx2x_vfop_filters *vlans,
884 			     int qid, bool drv_only)
885 {
886 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
887 
888 	if (vfop) {
889 		struct bnx2x_vfop_args_filters filters = {
890 			.multi_filter = vlans,
891 			.credit = &bnx2x_vfq(vf, qid, vlan_count),
892 		};
893 		struct bnx2x_vfop_vlan_mac_flags flags = {
894 			.drv_only = drv_only,
895 			.dont_consume = (filters.credit != NULL),
896 			.single_cmd = false,
897 			.add = false, /* don't care */
898 		};
899 		struct bnx2x_vlan_mac_ramrod_params *ramrod =
900 			&vf->op_params.vlan_mac;
901 
902 		/* set ramrod params */
903 		bnx2x_vfop_vlan_mac_prep_ramrod(ramrod, &flags);
904 
905 		/* set object */
906 		ramrod->vlan_mac_obj = &bnx2x_vfq(vf, qid, vlan_obj);
907 
908 		/* set extra args */
909 		filters.multi_filter->add_cnt = vf_vlan_rules_cnt(vf) -
910 			atomic_read(filters.credit);
911 
912 		vfop->args.filters = filters;
913 
914 		bnx2x_vfop_opset(BNX2X_VFOP_VLAN_CONFIG_LIST,
915 				 bnx2x_vfop_vlan_mac, cmd->done);
916 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_vlan_mac,
917 					     cmd->block);
918 	}
919 	return -ENOMEM;
920 }
921 
922 /* VFOP queue setup (queue constructor + set vlan 0) */
923 static void bnx2x_vfop_qsetup(struct bnx2x *bp, struct bnx2x_virtf *vf)
924 {
925 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
926 	int qid = vfop->args.qctor.qid;
927 	enum bnx2x_vfop_qsetup_state state = vfop->state;
928 	struct bnx2x_vfop_cmd cmd = {
929 		.done = bnx2x_vfop_qsetup,
930 		.block = false,
931 	};
932 
933 	if (vfop->rc < 0)
934 		goto op_err;
935 
936 	DP(BNX2X_MSG_IOV, "vf[%d] STATE: %d\n", vf->abs_vfid, state);
937 
938 	switch (state) {
939 	case BNX2X_VFOP_QSETUP_CTOR:
940 		/* init the queue ctor command */
941 		vfop->state = BNX2X_VFOP_QSETUP_VLAN0;
942 		vfop->rc = bnx2x_vfop_qctor_cmd(bp, vf, &cmd, qid);
943 		if (vfop->rc)
944 			goto op_err;
945 		return;
946 
947 	case BNX2X_VFOP_QSETUP_VLAN0:
948 		/* skip if non-leading or FPGA/EMU*/
949 		if (qid)
950 			goto op_done;
951 
952 		/* init the queue set-vlan command (for vlan 0) */
953 		vfop->state = BNX2X_VFOP_QSETUP_DONE;
954 		vfop->rc = bnx2x_vfop_vlan_set_cmd(bp, vf, &cmd, qid, 0, true);
955 		if (vfop->rc)
956 			goto op_err;
957 		return;
958 op_err:
959 	BNX2X_ERR("QSETUP[%d:%d] error: rc %d\n", vf->abs_vfid, qid, vfop->rc);
960 op_done:
961 	case BNX2X_VFOP_QSETUP_DONE:
962 		bnx2x_vfop_end(bp, vf, vfop);
963 		return;
964 	default:
965 		bnx2x_vfop_default(state);
966 	}
967 }
968 
969 int bnx2x_vfop_qsetup_cmd(struct bnx2x *bp,
970 			  struct bnx2x_virtf *vf,
971 			  struct bnx2x_vfop_cmd *cmd,
972 			  int qid)
973 {
974 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
975 
976 	if (vfop) {
977 		vfop->args.qctor.qid = qid;
978 
979 		bnx2x_vfop_opset(BNX2X_VFOP_QSETUP_CTOR,
980 				 bnx2x_vfop_qsetup, cmd->done);
981 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_qsetup,
982 					     cmd->block);
983 	}
984 	return -ENOMEM;
985 }
986 
987 /* VFOP queue FLR handling (clear vlans, clear macs, queue destructor) */
988 static void bnx2x_vfop_qflr(struct bnx2x *bp, struct bnx2x_virtf *vf)
989 {
990 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
991 	int qid = vfop->args.qx.qid;
992 	enum bnx2x_vfop_qflr_state state = vfop->state;
993 	struct bnx2x_queue_state_params *qstate;
994 	struct bnx2x_vfop_cmd cmd;
995 
996 	bnx2x_vfop_reset_wq(vf);
997 
998 	if (vfop->rc < 0)
999 		goto op_err;
1000 
1001 	DP(BNX2X_MSG_IOV, "VF[%d] STATE: %d\n", vf->abs_vfid, state);
1002 
1003 	cmd.done = bnx2x_vfop_qflr;
1004 	cmd.block = false;
1005 
1006 	switch (state) {
1007 	case BNX2X_VFOP_QFLR_CLR_VLAN:
1008 		/* vlan-clear-all: driver-only, don't consume credit */
1009 		vfop->state = BNX2X_VFOP_QFLR_CLR_MAC;
1010 		vfop->rc = bnx2x_vfop_vlan_delall_cmd(bp, vf, &cmd, qid, true);
1011 		if (vfop->rc)
1012 			goto op_err;
1013 		return;
1014 
1015 	case BNX2X_VFOP_QFLR_CLR_MAC:
1016 		/* mac-clear-all: driver only consume credit */
1017 		vfop->state = BNX2X_VFOP_QFLR_TERMINATE;
1018 		vfop->rc = bnx2x_vfop_mac_delall_cmd(bp, vf, &cmd, qid, true);
1019 		DP(BNX2X_MSG_IOV,
1020 		   "VF[%d] vfop->rc after bnx2x_vfop_mac_delall_cmd was %d",
1021 		   vf->abs_vfid, vfop->rc);
1022 		if (vfop->rc)
1023 			goto op_err;
1024 		return;
1025 
1026 	case BNX2X_VFOP_QFLR_TERMINATE:
1027 		qstate = &vfop->op_p->qctor.qstate;
1028 		memset(qstate , 0, sizeof(*qstate));
1029 		qstate->q_obj = &bnx2x_vfq(vf, qid, sp_obj);
1030 		vfop->state = BNX2X_VFOP_QFLR_DONE;
1031 
1032 		DP(BNX2X_MSG_IOV, "VF[%d] qstate during flr was %d\n",
1033 		   vf->abs_vfid, qstate->q_obj->state);
1034 
1035 		if (qstate->q_obj->state != BNX2X_Q_STATE_RESET) {
1036 			qstate->q_obj->state = BNX2X_Q_STATE_STOPPED;
1037 			qstate->cmd = BNX2X_Q_CMD_TERMINATE;
1038 			vfop->rc = bnx2x_queue_state_change(bp, qstate);
1039 			bnx2x_vfop_finalize(vf, vfop->rc, VFOP_VERIFY_PEND);
1040 		} else {
1041 			goto op_done;
1042 		}
1043 
1044 op_err:
1045 	BNX2X_ERR("QFLR[%d:%d] error: rc %d\n",
1046 		  vf->abs_vfid, qid, vfop->rc);
1047 op_done:
1048 	case BNX2X_VFOP_QFLR_DONE:
1049 		bnx2x_vfop_end(bp, vf, vfop);
1050 		return;
1051 	default:
1052 		bnx2x_vfop_default(state);
1053 	}
1054 op_pending:
1055 	return;
1056 }
1057 
1058 static int bnx2x_vfop_qflr_cmd(struct bnx2x *bp,
1059 			       struct bnx2x_virtf *vf,
1060 			       struct bnx2x_vfop_cmd *cmd,
1061 			       int qid)
1062 {
1063 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
1064 
1065 	if (vfop) {
1066 		vfop->args.qx.qid = qid;
1067 		bnx2x_vfop_opset(BNX2X_VFOP_QFLR_CLR_VLAN,
1068 				 bnx2x_vfop_qflr, cmd->done);
1069 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_qflr,
1070 					     cmd->block);
1071 	}
1072 	return -ENOMEM;
1073 }
1074 
1075 /* VFOP multi-casts */
1076 static void bnx2x_vfop_mcast(struct bnx2x *bp, struct bnx2x_virtf *vf)
1077 {
1078 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
1079 	struct bnx2x_mcast_ramrod_params *mcast = &vfop->op_p->mcast;
1080 	struct bnx2x_raw_obj *raw = &mcast->mcast_obj->raw;
1081 	struct bnx2x_vfop_args_mcast *args = &vfop->args.mc_list;
1082 	enum bnx2x_vfop_mcast_state state = vfop->state;
1083 	int i;
1084 
1085 	bnx2x_vfop_reset_wq(vf);
1086 
1087 	if (vfop->rc < 0)
1088 		goto op_err;
1089 
1090 	DP(BNX2X_MSG_IOV, "vf[%d] STATE: %d\n", vf->abs_vfid, state);
1091 
1092 	switch (state) {
1093 	case BNX2X_VFOP_MCAST_DEL:
1094 		/* clear existing mcasts */
1095 		vfop->state = BNX2X_VFOP_MCAST_ADD;
1096 		vfop->rc = bnx2x_config_mcast(bp, mcast, BNX2X_MCAST_CMD_DEL);
1097 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_CONT);
1098 
1099 	case BNX2X_VFOP_MCAST_ADD:
1100 		if (raw->check_pending(raw))
1101 			goto op_pending;
1102 
1103 		if (args->mc_num) {
1104 			/* update mcast list on the ramrod params */
1105 			INIT_LIST_HEAD(&mcast->mcast_list);
1106 			for (i = 0; i < args->mc_num; i++)
1107 				list_add_tail(&(args->mc[i].link),
1108 					      &mcast->mcast_list);
1109 			/* add new mcasts */
1110 			vfop->state = BNX2X_VFOP_MCAST_CHK_DONE;
1111 			vfop->rc = bnx2x_config_mcast(bp, mcast,
1112 						      BNX2X_MCAST_CMD_ADD);
1113 		}
1114 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_DONE);
1115 
1116 	case BNX2X_VFOP_MCAST_CHK_DONE:
1117 		vfop->rc = raw->check_pending(raw) ? 1 : 0;
1118 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_DONE);
1119 	default:
1120 		bnx2x_vfop_default(state);
1121 	}
1122 op_err:
1123 	BNX2X_ERR("MCAST CONFIG error: rc %d\n", vfop->rc);
1124 op_done:
1125 	kfree(args->mc);
1126 	bnx2x_vfop_end(bp, vf, vfop);
1127 op_pending:
1128 	return;
1129 }
1130 
1131 int bnx2x_vfop_mcast_cmd(struct bnx2x *bp,
1132 			 struct bnx2x_virtf *vf,
1133 			 struct bnx2x_vfop_cmd *cmd,
1134 			 bnx2x_mac_addr_t *mcasts,
1135 			 int mcast_num, bool drv_only)
1136 {
1137 	struct bnx2x_vfop *vfop = NULL;
1138 	size_t mc_sz = mcast_num * sizeof(struct bnx2x_mcast_list_elem);
1139 	struct bnx2x_mcast_list_elem *mc = mc_sz ? kzalloc(mc_sz, GFP_KERNEL) :
1140 					   NULL;
1141 
1142 	if (!mc_sz || mc) {
1143 		vfop = bnx2x_vfop_add(bp, vf);
1144 		if (vfop) {
1145 			int i;
1146 			struct bnx2x_mcast_ramrod_params *ramrod =
1147 				&vf->op_params.mcast;
1148 
1149 			/* set ramrod params */
1150 			memset(ramrod, 0, sizeof(*ramrod));
1151 			ramrod->mcast_obj = &vf->mcast_obj;
1152 			if (drv_only)
1153 				set_bit(RAMROD_DRV_CLR_ONLY,
1154 					&ramrod->ramrod_flags);
1155 
1156 			/* copy mcasts pointers */
1157 			vfop->args.mc_list.mc_num = mcast_num;
1158 			vfop->args.mc_list.mc = mc;
1159 			for (i = 0; i < mcast_num; i++)
1160 				mc[i].mac = mcasts[i];
1161 
1162 			bnx2x_vfop_opset(BNX2X_VFOP_MCAST_DEL,
1163 					 bnx2x_vfop_mcast, cmd->done);
1164 			return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_mcast,
1165 						     cmd->block);
1166 		} else {
1167 			kfree(mc);
1168 		}
1169 	}
1170 	return -ENOMEM;
1171 }
1172 
1173 /* VFOP rx-mode */
1174 static void bnx2x_vfop_rxmode(struct bnx2x *bp, struct bnx2x_virtf *vf)
1175 {
1176 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
1177 	struct bnx2x_rx_mode_ramrod_params *ramrod = &vfop->op_p->rx_mode;
1178 	enum bnx2x_vfop_rxmode_state state = vfop->state;
1179 
1180 	bnx2x_vfop_reset_wq(vf);
1181 
1182 	if (vfop->rc < 0)
1183 		goto op_err;
1184 
1185 	DP(BNX2X_MSG_IOV, "vf[%d] STATE: %d\n", vf->abs_vfid, state);
1186 
1187 	switch (state) {
1188 	case BNX2X_VFOP_RXMODE_CONFIG:
1189 		/* next state */
1190 		vfop->state = BNX2X_VFOP_RXMODE_DONE;
1191 
1192 		vfop->rc = bnx2x_config_rx_mode(bp, ramrod);
1193 		bnx2x_vfop_finalize(vf, vfop->rc, VFOP_DONE);
1194 op_err:
1195 		BNX2X_ERR("RXMODE error: rc %d\n", vfop->rc);
1196 op_done:
1197 	case BNX2X_VFOP_RXMODE_DONE:
1198 		bnx2x_vfop_end(bp, vf, vfop);
1199 		return;
1200 	default:
1201 		bnx2x_vfop_default(state);
1202 	}
1203 op_pending:
1204 	return;
1205 }
1206 
1207 int bnx2x_vfop_rxmode_cmd(struct bnx2x *bp,
1208 			  struct bnx2x_virtf *vf,
1209 			  struct bnx2x_vfop_cmd *cmd,
1210 			  int qid, unsigned long accept_flags)
1211 {
1212 	struct bnx2x_vf_queue *vfq = vfq_get(vf, qid);
1213 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
1214 
1215 	if (vfop) {
1216 		struct bnx2x_rx_mode_ramrod_params *ramrod =
1217 			&vf->op_params.rx_mode;
1218 
1219 		memset(ramrod, 0, sizeof(*ramrod));
1220 
1221 		/* Prepare ramrod parameters */
1222 		ramrod->cid = vfq->cid;
1223 		ramrod->cl_id = vfq_cl_id(vf, vfq);
1224 		ramrod->rx_mode_obj = &bp->rx_mode_obj;
1225 		ramrod->func_id = FW_VF_HANDLE(vf->abs_vfid);
1226 
1227 		ramrod->rx_accept_flags = accept_flags;
1228 		ramrod->tx_accept_flags = accept_flags;
1229 		ramrod->pstate = &vf->filter_state;
1230 		ramrod->state = BNX2X_FILTER_RX_MODE_PENDING;
1231 
1232 		set_bit(BNX2X_FILTER_RX_MODE_PENDING, &vf->filter_state);
1233 		set_bit(RAMROD_RX, &ramrod->ramrod_flags);
1234 		set_bit(RAMROD_TX, &ramrod->ramrod_flags);
1235 
1236 		ramrod->rdata =
1237 			bnx2x_vf_sp(bp, vf, rx_mode_rdata.e2);
1238 		ramrod->rdata_mapping =
1239 			bnx2x_vf_sp_map(bp, vf, rx_mode_rdata.e2);
1240 
1241 		bnx2x_vfop_opset(BNX2X_VFOP_RXMODE_CONFIG,
1242 				 bnx2x_vfop_rxmode, cmd->done);
1243 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_rxmode,
1244 					     cmd->block);
1245 	}
1246 	return -ENOMEM;
1247 }
1248 
1249 /* VFOP queue tear-down ('drop all' rx-mode, clear vlans, clear macs,
1250  * queue destructor)
1251  */
1252 static void bnx2x_vfop_qdown(struct bnx2x *bp, struct bnx2x_virtf *vf)
1253 {
1254 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
1255 	int qid = vfop->args.qx.qid;
1256 	enum bnx2x_vfop_qteardown_state state = vfop->state;
1257 	struct bnx2x_vfop_cmd cmd;
1258 
1259 	if (vfop->rc < 0)
1260 		goto op_err;
1261 
1262 	DP(BNX2X_MSG_IOV, "vf[%d] STATE: %d\n", vf->abs_vfid, state);
1263 
1264 	cmd.done = bnx2x_vfop_qdown;
1265 	cmd.block = false;
1266 
1267 	switch (state) {
1268 	case BNX2X_VFOP_QTEARDOWN_RXMODE:
1269 		/* Drop all */
1270 		vfop->state = BNX2X_VFOP_QTEARDOWN_CLR_VLAN;
1271 		vfop->rc = bnx2x_vfop_rxmode_cmd(bp, vf, &cmd, qid, 0);
1272 		if (vfop->rc)
1273 			goto op_err;
1274 		return;
1275 
1276 	case BNX2X_VFOP_QTEARDOWN_CLR_VLAN:
1277 		/* vlan-clear-all: don't consume credit */
1278 		vfop->state = BNX2X_VFOP_QTEARDOWN_CLR_MAC;
1279 		vfop->rc = bnx2x_vfop_vlan_delall_cmd(bp, vf, &cmd, qid, false);
1280 		if (vfop->rc)
1281 			goto op_err;
1282 		return;
1283 
1284 	case BNX2X_VFOP_QTEARDOWN_CLR_MAC:
1285 		/* mac-clear-all: consume credit */
1286 		vfop->state = BNX2X_VFOP_QTEARDOWN_QDTOR;
1287 		vfop->rc = bnx2x_vfop_mac_delall_cmd(bp, vf, &cmd, qid, false);
1288 		if (vfop->rc)
1289 			goto op_err;
1290 		return;
1291 
1292 	case BNX2X_VFOP_QTEARDOWN_QDTOR:
1293 		/* run the queue destruction flow */
1294 		DP(BNX2X_MSG_IOV, "case: BNX2X_VFOP_QTEARDOWN_QDTOR\n");
1295 		vfop->state = BNX2X_VFOP_QTEARDOWN_DONE;
1296 		DP(BNX2X_MSG_IOV, "new state: BNX2X_VFOP_QTEARDOWN_DONE\n");
1297 		vfop->rc = bnx2x_vfop_qdtor_cmd(bp, vf, &cmd, qid);
1298 		DP(BNX2X_MSG_IOV, "returned from cmd\n");
1299 		if (vfop->rc)
1300 			goto op_err;
1301 		return;
1302 op_err:
1303 	BNX2X_ERR("QTEARDOWN[%d:%d] error: rc %d\n",
1304 		  vf->abs_vfid, qid, vfop->rc);
1305 
1306 	case BNX2X_VFOP_QTEARDOWN_DONE:
1307 		bnx2x_vfop_end(bp, vf, vfop);
1308 		return;
1309 	default:
1310 		bnx2x_vfop_default(state);
1311 	}
1312 }
1313 
1314 int bnx2x_vfop_qdown_cmd(struct bnx2x *bp,
1315 			 struct bnx2x_virtf *vf,
1316 			 struct bnx2x_vfop_cmd *cmd,
1317 			 int qid)
1318 {
1319 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
1320 
1321 	if (vfop) {
1322 		vfop->args.qx.qid = qid;
1323 		bnx2x_vfop_opset(BNX2X_VFOP_QTEARDOWN_RXMODE,
1324 				 bnx2x_vfop_qdown, cmd->done);
1325 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_qdown,
1326 					     cmd->block);
1327 	}
1328 
1329 	return -ENOMEM;
1330 }
1331 
1332 /* VF enable primitives
1333  * when pretend is required the caller is responsible
1334  * for calling pretend prior to calling these routines
1335  */
1336 
1337 /* internal vf enable - until vf is enabled internally all transactions
1338  * are blocked. this routine should always be called last with pretend.
1339  */
1340 static void bnx2x_vf_enable_internal(struct bnx2x *bp, u8 enable)
1341 {
1342 	REG_WR(bp, PGLUE_B_REG_INTERNAL_VFID_ENABLE, enable ? 1 : 0);
1343 }
1344 
1345 /* clears vf error in all semi blocks */
1346 static void bnx2x_vf_semi_clear_err(struct bnx2x *bp, u8 abs_vfid)
1347 {
1348 	REG_WR(bp, TSEM_REG_VFPF_ERR_NUM, abs_vfid);
1349 	REG_WR(bp, USEM_REG_VFPF_ERR_NUM, abs_vfid);
1350 	REG_WR(bp, CSEM_REG_VFPF_ERR_NUM, abs_vfid);
1351 	REG_WR(bp, XSEM_REG_VFPF_ERR_NUM, abs_vfid);
1352 }
1353 
1354 static void bnx2x_vf_pglue_clear_err(struct bnx2x *bp, u8 abs_vfid)
1355 {
1356 	u32 was_err_group = (2 * BP_PATH(bp) + abs_vfid) >> 5;
1357 	u32 was_err_reg = 0;
1358 
1359 	switch (was_err_group) {
1360 	case 0:
1361 	    was_err_reg = PGLUE_B_REG_WAS_ERROR_VF_31_0_CLR;
1362 	    break;
1363 	case 1:
1364 	    was_err_reg = PGLUE_B_REG_WAS_ERROR_VF_63_32_CLR;
1365 	    break;
1366 	case 2:
1367 	    was_err_reg = PGLUE_B_REG_WAS_ERROR_VF_95_64_CLR;
1368 	    break;
1369 	case 3:
1370 	    was_err_reg = PGLUE_B_REG_WAS_ERROR_VF_127_96_CLR;
1371 	    break;
1372 	}
1373 	REG_WR(bp, was_err_reg, 1 << (abs_vfid & 0x1f));
1374 }
1375 
1376 static void bnx2x_vf_igu_reset(struct bnx2x *bp, struct bnx2x_virtf *vf)
1377 {
1378 	int i;
1379 	u32 val;
1380 
1381 	/* Set VF masks and configuration - pretend */
1382 	bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, vf->abs_vfid));
1383 
1384 	REG_WR(bp, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
1385 	REG_WR(bp, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
1386 	REG_WR(bp, IGU_REG_SB_MASK_LSB, 0);
1387 	REG_WR(bp, IGU_REG_SB_MASK_MSB, 0);
1388 	REG_WR(bp, IGU_REG_PBA_STATUS_LSB, 0);
1389 	REG_WR(bp, IGU_REG_PBA_STATUS_MSB, 0);
1390 
1391 	val = REG_RD(bp, IGU_REG_VF_CONFIGURATION);
1392 	val |= (IGU_VF_CONF_FUNC_EN | IGU_VF_CONF_MSI_MSIX_EN);
1393 	if (vf->cfg_flags & VF_CFG_INT_SIMD)
1394 		val |= IGU_VF_CONF_SINGLE_ISR_EN;
1395 	val &= ~IGU_VF_CONF_PARENT_MASK;
1396 	val |= BP_FUNC(bp) << IGU_VF_CONF_PARENT_SHIFT;	/* parent PF */
1397 	REG_WR(bp, IGU_REG_VF_CONFIGURATION, val);
1398 
1399 	DP(BNX2X_MSG_IOV,
1400 	   "value in IGU_REG_VF_CONFIGURATION of vf %d after write %x\n",
1401 	   vf->abs_vfid, REG_RD(bp, IGU_REG_VF_CONFIGURATION));
1402 
1403 	bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
1404 
1405 	/* iterate over all queues, clear sb consumer */
1406 	for (i = 0; i < vf_sb_count(vf); i++) {
1407 		u8 igu_sb_id = vf_igu_sb(vf, i);
1408 
1409 		/* zero prod memory */
1410 		REG_WR(bp, IGU_REG_PROD_CONS_MEMORY + igu_sb_id * 4, 0);
1411 
1412 		/* clear sb state machine */
1413 		bnx2x_igu_clear_sb_gen(bp, vf->abs_vfid, igu_sb_id,
1414 				       false /* VF */);
1415 
1416 		/* disable + update */
1417 		bnx2x_vf_igu_ack_sb(bp, vf, igu_sb_id, USTORM_ID, 0,
1418 				    IGU_INT_DISABLE, 1);
1419 	}
1420 }
1421 
1422 void bnx2x_vf_enable_access(struct bnx2x *bp, u8 abs_vfid)
1423 {
1424 	/* set the VF-PF association in the FW */
1425 	storm_memset_vf_to_pf(bp, FW_VF_HANDLE(abs_vfid), BP_FUNC(bp));
1426 	storm_memset_func_en(bp, FW_VF_HANDLE(abs_vfid), 1);
1427 
1428 	/* clear vf errors*/
1429 	bnx2x_vf_semi_clear_err(bp, abs_vfid);
1430 	bnx2x_vf_pglue_clear_err(bp, abs_vfid);
1431 
1432 	/* internal vf-enable - pretend */
1433 	bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, abs_vfid));
1434 	DP(BNX2X_MSG_IOV, "enabling internal access for vf %x\n", abs_vfid);
1435 	bnx2x_vf_enable_internal(bp, true);
1436 	bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
1437 }
1438 
1439 static void bnx2x_vf_enable_traffic(struct bnx2x *bp, struct bnx2x_virtf *vf)
1440 {
1441 	/* Reset vf in IGU  interrupts are still disabled */
1442 	bnx2x_vf_igu_reset(bp, vf);
1443 
1444 	/* pretend to enable the vf with the PBF */
1445 	bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, vf->abs_vfid));
1446 	REG_WR(bp, PBF_REG_DISABLE_VF, 0);
1447 	bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
1448 }
1449 
1450 static u8 bnx2x_vf_is_pcie_pending(struct bnx2x *bp, u8 abs_vfid)
1451 {
1452 	struct pci_dev *dev;
1453 	struct bnx2x_virtf *vf = bnx2x_vf_by_abs_fid(bp, abs_vfid);
1454 
1455 	if (!vf)
1456 		goto unknown_dev;
1457 
1458 	dev = pci_get_bus_and_slot(vf->bus, vf->devfn);
1459 	if (dev)
1460 		return bnx2x_is_pcie_pending(dev);
1461 
1462 unknown_dev:
1463 	BNX2X_ERR("Unknown device\n");
1464 	return false;
1465 }
1466 
1467 int bnx2x_vf_flr_clnup_epilog(struct bnx2x *bp, u8 abs_vfid)
1468 {
1469 	/* Wait 100ms */
1470 	msleep(100);
1471 
1472 	/* Verify no pending pci transactions */
1473 	if (bnx2x_vf_is_pcie_pending(bp, abs_vfid))
1474 		BNX2X_ERR("PCIE Transactions still pending\n");
1475 
1476 	return 0;
1477 }
1478 
1479 /* must be called after the number of PF queues and the number of VFs are
1480  * both known
1481  */
1482 static void
1483 bnx2x_iov_static_resc(struct bnx2x *bp, struct vf_pf_resc_request *resc)
1484 {
1485 	u16 vlan_count = 0;
1486 
1487 	/* will be set only during VF-ACQUIRE */
1488 	resc->num_rxqs = 0;
1489 	resc->num_txqs = 0;
1490 
1491 	/* no credit calculcis for macs (just yet) */
1492 	resc->num_mac_filters = 1;
1493 
1494 	/* divvy up vlan rules */
1495 	vlan_count = bp->vlans_pool.check(&bp->vlans_pool);
1496 	vlan_count = 1 << ilog2(vlan_count);
1497 	resc->num_vlan_filters = vlan_count / BNX2X_NR_VIRTFN(bp);
1498 
1499 	/* no real limitation */
1500 	resc->num_mc_filters = 0;
1501 
1502 	/* num_sbs already set */
1503 }
1504 
1505 /* FLR routines: */
1506 static void bnx2x_vf_free_resc(struct bnx2x *bp, struct bnx2x_virtf *vf)
1507 {
1508 	/* reset the state variables */
1509 	bnx2x_iov_static_resc(bp, &vf->alloc_resc);
1510 	vf->state = VF_FREE;
1511 }
1512 
1513 static void bnx2x_vf_flr_clnup_hw(struct bnx2x *bp, struct bnx2x_virtf *vf)
1514 {
1515 	u32 poll_cnt = bnx2x_flr_clnup_poll_count(bp);
1516 
1517 	/* DQ usage counter */
1518 	bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, vf->abs_vfid));
1519 	bnx2x_flr_clnup_poll_hw_counter(bp, DORQ_REG_VF_USAGE_CNT,
1520 					"DQ VF usage counter timed out",
1521 					poll_cnt);
1522 	bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
1523 
1524 	/* FW cleanup command - poll for the results */
1525 	if (bnx2x_send_final_clnup(bp, (u8)FW_VF_HANDLE(vf->abs_vfid),
1526 				   poll_cnt))
1527 		BNX2X_ERR("VF[%d] Final cleanup timed-out\n", vf->abs_vfid);
1528 
1529 	/* verify TX hw is flushed */
1530 	bnx2x_tx_hw_flushed(bp, poll_cnt);
1531 }
1532 
1533 static void bnx2x_vfop_flr(struct bnx2x *bp, struct bnx2x_virtf *vf)
1534 {
1535 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
1536 	struct bnx2x_vfop_args_qx *qx = &vfop->args.qx;
1537 	enum bnx2x_vfop_flr_state state = vfop->state;
1538 	struct bnx2x_vfop_cmd cmd = {
1539 		.done = bnx2x_vfop_flr,
1540 		.block = false,
1541 	};
1542 
1543 	if (vfop->rc < 0)
1544 		goto op_err;
1545 
1546 	DP(BNX2X_MSG_IOV, "vf[%d] STATE: %d\n", vf->abs_vfid, state);
1547 
1548 	switch (state) {
1549 	case BNX2X_VFOP_FLR_QUEUES:
1550 		/* the cleanup operations are valid if and only if the VF
1551 		 * was first acquired.
1552 		 */
1553 		if (++(qx->qid) < vf_rxq_count(vf)) {
1554 			vfop->rc = bnx2x_vfop_qflr_cmd(bp, vf, &cmd,
1555 						       qx->qid);
1556 			if (vfop->rc)
1557 				goto op_err;
1558 			return;
1559 		}
1560 		/* remove multicasts */
1561 		vfop->state = BNX2X_VFOP_FLR_HW;
1562 		vfop->rc = bnx2x_vfop_mcast_cmd(bp, vf, &cmd, NULL,
1563 						0, true);
1564 		if (vfop->rc)
1565 			goto op_err;
1566 		return;
1567 	case BNX2X_VFOP_FLR_HW:
1568 
1569 		/* dispatch final cleanup and wait for HW queues to flush */
1570 		bnx2x_vf_flr_clnup_hw(bp, vf);
1571 
1572 		/* release VF resources */
1573 		bnx2x_vf_free_resc(bp, vf);
1574 
1575 		/* re-open the mailbox */
1576 		bnx2x_vf_enable_mbx(bp, vf->abs_vfid);
1577 
1578 		goto op_done;
1579 	default:
1580 		bnx2x_vfop_default(state);
1581 	}
1582 op_err:
1583 	BNX2X_ERR("VF[%d] FLR error: rc %d\n", vf->abs_vfid, vfop->rc);
1584 op_done:
1585 	vf->flr_clnup_stage = VF_FLR_ACK;
1586 	bnx2x_vfop_end(bp, vf, vfop);
1587 	bnx2x_unlock_vf_pf_channel(bp, vf, CHANNEL_TLV_FLR);
1588 }
1589 
1590 static int bnx2x_vfop_flr_cmd(struct bnx2x *bp,
1591 			      struct bnx2x_virtf *vf,
1592 			      vfop_handler_t done)
1593 {
1594 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
1595 	if (vfop) {
1596 		vfop->args.qx.qid = -1; /* loop */
1597 		bnx2x_vfop_opset(BNX2X_VFOP_FLR_QUEUES,
1598 				 bnx2x_vfop_flr, done);
1599 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_flr, false);
1600 	}
1601 	return -ENOMEM;
1602 }
1603 
1604 static void bnx2x_vf_flr_clnup(struct bnx2x *bp, struct bnx2x_virtf *prev_vf)
1605 {
1606 	int i = prev_vf ? prev_vf->index + 1 : 0;
1607 	struct bnx2x_virtf *vf;
1608 
1609 	/* find next VF to cleanup */
1610 next_vf_to_clean:
1611 	for (;
1612 	     i < BNX2X_NR_VIRTFN(bp) &&
1613 	     (bnx2x_vf(bp, i, state) != VF_RESET ||
1614 	      bnx2x_vf(bp, i, flr_clnup_stage) != VF_FLR_CLN);
1615 	     i++)
1616 		;
1617 
1618 	DP(BNX2X_MSG_IOV, "next vf to cleanup: %d. num of vfs: %d\n", i,
1619 	   BNX2X_NR_VIRTFN(bp));
1620 
1621 	if (i < BNX2X_NR_VIRTFN(bp)) {
1622 		vf = BP_VF(bp, i);
1623 
1624 		/* lock the vf pf channel */
1625 		bnx2x_lock_vf_pf_channel(bp, vf, CHANNEL_TLV_FLR);
1626 
1627 		/* invoke the VF FLR SM */
1628 		if (bnx2x_vfop_flr_cmd(bp, vf, bnx2x_vf_flr_clnup)) {
1629 			BNX2X_ERR("VF[%d]: FLR cleanup failed -ENOMEM\n",
1630 				  vf->abs_vfid);
1631 
1632 			/* mark the VF to be ACKED and continue */
1633 			vf->flr_clnup_stage = VF_FLR_ACK;
1634 			goto next_vf_to_clean;
1635 		}
1636 		return;
1637 	}
1638 
1639 	/* we are done, update vf records */
1640 	for_each_vf(bp, i) {
1641 		vf = BP_VF(bp, i);
1642 
1643 		if (vf->flr_clnup_stage != VF_FLR_ACK)
1644 			continue;
1645 
1646 		vf->flr_clnup_stage = VF_FLR_EPILOG;
1647 	}
1648 
1649 	/* Acknowledge the handled VFs.
1650 	 * we are acknowledge all the vfs which an flr was requested for, even
1651 	 * if amongst them there are such that we never opened, since the mcp
1652 	 * will interrupt us immediately again if we only ack some of the bits,
1653 	 * resulting in an endless loop. This can happen for example in KVM
1654 	 * where an 'all ones' flr request is sometimes given by hyper visor
1655 	 */
1656 	DP(BNX2X_MSG_MCP, "DRV_STATUS_VF_DISABLED ACK for vfs 0x%x 0x%x\n",
1657 	   bp->vfdb->flrd_vfs[0], bp->vfdb->flrd_vfs[1]);
1658 	for (i = 0; i < FLRD_VFS_DWORDS; i++)
1659 		SHMEM2_WR(bp, drv_ack_vf_disabled[BP_FW_MB_IDX(bp)][i],
1660 			  bp->vfdb->flrd_vfs[i]);
1661 
1662 	bnx2x_fw_command(bp, DRV_MSG_CODE_VF_DISABLED_DONE, 0);
1663 
1664 	/* clear the acked bits - better yet if the MCP implemented
1665 	 * write to clear semantics
1666 	 */
1667 	for (i = 0; i < FLRD_VFS_DWORDS; i++)
1668 		SHMEM2_WR(bp, drv_ack_vf_disabled[BP_FW_MB_IDX(bp)][i], 0);
1669 }
1670 
1671 void bnx2x_vf_handle_flr_event(struct bnx2x *bp)
1672 {
1673 	int i;
1674 
1675 	/* Read FLR'd VFs */
1676 	for (i = 0; i < FLRD_VFS_DWORDS; i++)
1677 		bp->vfdb->flrd_vfs[i] = SHMEM2_RD(bp, mcp_vf_disabled[i]);
1678 
1679 	DP(BNX2X_MSG_MCP,
1680 	   "DRV_STATUS_VF_DISABLED received for vfs 0x%x 0x%x\n",
1681 	   bp->vfdb->flrd_vfs[0], bp->vfdb->flrd_vfs[1]);
1682 
1683 	for_each_vf(bp, i) {
1684 		struct bnx2x_virtf *vf = BP_VF(bp, i);
1685 		u32 reset = 0;
1686 
1687 		if (vf->abs_vfid < 32)
1688 			reset = bp->vfdb->flrd_vfs[0] & (1 << vf->abs_vfid);
1689 		else
1690 			reset = bp->vfdb->flrd_vfs[1] &
1691 				(1 << (vf->abs_vfid - 32));
1692 
1693 		if (reset) {
1694 			/* set as reset and ready for cleanup */
1695 			vf->state = VF_RESET;
1696 			vf->flr_clnup_stage = VF_FLR_CLN;
1697 
1698 			DP(BNX2X_MSG_IOV,
1699 			   "Initiating Final cleanup for VF %d\n",
1700 			   vf->abs_vfid);
1701 		}
1702 	}
1703 
1704 	/* do the FLR cleanup for all marked VFs*/
1705 	bnx2x_vf_flr_clnup(bp, NULL);
1706 }
1707 
1708 /* IOV global initialization routines  */
1709 void bnx2x_iov_init_dq(struct bnx2x *bp)
1710 {
1711 	if (!IS_SRIOV(bp))
1712 		return;
1713 
1714 	/* Set the DQ such that the CID reflect the abs_vfid */
1715 	REG_WR(bp, DORQ_REG_VF_NORM_VF_BASE, 0);
1716 	REG_WR(bp, DORQ_REG_MAX_RVFID_SIZE, ilog2(BNX2X_MAX_NUM_OF_VFS));
1717 
1718 	/* Set VFs starting CID. If its > 0 the preceding CIDs are belong to
1719 	 * the PF L2 queues
1720 	 */
1721 	REG_WR(bp, DORQ_REG_VF_NORM_CID_BASE, BNX2X_FIRST_VF_CID);
1722 
1723 	/* The VF window size is the log2 of the max number of CIDs per VF */
1724 	REG_WR(bp, DORQ_REG_VF_NORM_CID_WND_SIZE, BNX2X_VF_CID_WND);
1725 
1726 	/* The VF doorbell size  0 - *B, 4 - 128B. We set it here to match
1727 	 * the Pf doorbell size although the 2 are independent.
1728 	 */
1729 	REG_WR(bp, DORQ_REG_VF_NORM_CID_OFST,
1730 	       BNX2X_DB_SHIFT - BNX2X_DB_MIN_SHIFT);
1731 
1732 	/* No security checks for now -
1733 	 * configure single rule (out of 16) mask = 0x1, value = 0x0,
1734 	 * CID range 0 - 0x1ffff
1735 	 */
1736 	REG_WR(bp, DORQ_REG_VF_TYPE_MASK_0, 1);
1737 	REG_WR(bp, DORQ_REG_VF_TYPE_VALUE_0, 0);
1738 	REG_WR(bp, DORQ_REG_VF_TYPE_MIN_MCID_0, 0);
1739 	REG_WR(bp, DORQ_REG_VF_TYPE_MAX_MCID_0, 0x1ffff);
1740 
1741 	/* set the number of VF alllowed doorbells to the full DQ range */
1742 	REG_WR(bp, DORQ_REG_VF_NORM_MAX_CID_COUNT, 0x20000);
1743 
1744 	/* set the VF doorbell threshold */
1745 	REG_WR(bp, DORQ_REG_VF_USAGE_CT_LIMIT, 4);
1746 }
1747 
1748 void bnx2x_iov_init_dmae(struct bnx2x *bp)
1749 {
1750 	DP(BNX2X_MSG_IOV, "SRIOV is %s\n", IS_SRIOV(bp) ? "ON" : "OFF");
1751 	if (!IS_SRIOV(bp))
1752 		return;
1753 
1754 	REG_WR(bp, DMAE_REG_BACKWARD_COMP_EN, 0);
1755 }
1756 
1757 static int bnx2x_vf_bus(struct bnx2x *bp, int vfid)
1758 {
1759 	struct pci_dev *dev = bp->pdev;
1760 	struct bnx2x_sriov *iov = &bp->vfdb->sriov;
1761 
1762 	return dev->bus->number + ((dev->devfn + iov->offset +
1763 				    iov->stride * vfid) >> 8);
1764 }
1765 
1766 static int bnx2x_vf_devfn(struct bnx2x *bp, int vfid)
1767 {
1768 	struct pci_dev *dev = bp->pdev;
1769 	struct bnx2x_sriov *iov = &bp->vfdb->sriov;
1770 
1771 	return (dev->devfn + iov->offset + iov->stride * vfid) & 0xff;
1772 }
1773 
1774 static void bnx2x_vf_set_bars(struct bnx2x *bp, struct bnx2x_virtf *vf)
1775 {
1776 	int i, n;
1777 	struct pci_dev *dev = bp->pdev;
1778 	struct bnx2x_sriov *iov = &bp->vfdb->sriov;
1779 
1780 	for (i = 0, n = 0; i < PCI_SRIOV_NUM_BARS; i += 2, n++) {
1781 		u64 start = pci_resource_start(dev, PCI_IOV_RESOURCES + i);
1782 		u32 size = pci_resource_len(dev, PCI_IOV_RESOURCES + i);
1783 
1784 		size /= iov->total;
1785 		vf->bars[n].bar = start + size * vf->abs_vfid;
1786 		vf->bars[n].size = size;
1787 	}
1788 }
1789 
1790 static int bnx2x_ari_enabled(struct pci_dev *dev)
1791 {
1792 	return dev->bus->self && dev->bus->self->ari_enabled;
1793 }
1794 
1795 static void
1796 bnx2x_get_vf_igu_cam_info(struct bnx2x *bp)
1797 {
1798 	int sb_id;
1799 	u32 val;
1800 	u8 fid;
1801 
1802 	/* IGU in normal mode - read CAM */
1803 	for (sb_id = 0; sb_id < IGU_REG_MAPPING_MEMORY_SIZE; sb_id++) {
1804 		val = REG_RD(bp, IGU_REG_MAPPING_MEMORY + sb_id * 4);
1805 		if (!(val & IGU_REG_MAPPING_MEMORY_VALID))
1806 			continue;
1807 		fid = GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID);
1808 		if (!(fid & IGU_FID_ENCODE_IS_PF))
1809 			bnx2x_vf_set_igu_info(bp, sb_id,
1810 					      (fid & IGU_FID_VF_NUM_MASK));
1811 
1812 		DP(BNX2X_MSG_IOV, "%s[%d], igu_sb_id=%d, msix=%d\n",
1813 		   ((fid & IGU_FID_ENCODE_IS_PF) ? "PF" : "VF"),
1814 		   ((fid & IGU_FID_ENCODE_IS_PF) ? (fid & IGU_FID_PF_NUM_MASK) :
1815 		   (fid & IGU_FID_VF_NUM_MASK)), sb_id,
1816 		   GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR));
1817 	}
1818 }
1819 
1820 static void __bnx2x_iov_free_vfdb(struct bnx2x *bp)
1821 {
1822 	if (bp->vfdb) {
1823 		kfree(bp->vfdb->vfqs);
1824 		kfree(bp->vfdb->vfs);
1825 		kfree(bp->vfdb);
1826 	}
1827 	bp->vfdb = NULL;
1828 }
1829 
1830 static int bnx2x_sriov_pci_cfg_info(struct bnx2x *bp, struct bnx2x_sriov *iov)
1831 {
1832 	int pos;
1833 	struct pci_dev *dev = bp->pdev;
1834 
1835 	pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
1836 	if (!pos) {
1837 		BNX2X_ERR("failed to find SRIOV capability in device\n");
1838 		return -ENODEV;
1839 	}
1840 
1841 	iov->pos = pos;
1842 	DP(BNX2X_MSG_IOV, "sriov ext pos %d\n", pos);
1843 	pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &iov->ctrl);
1844 	pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &iov->total);
1845 	pci_read_config_word(dev, pos + PCI_SRIOV_INITIAL_VF, &iov->initial);
1846 	pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
1847 	pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
1848 	pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &iov->pgsz);
1849 	pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
1850 	pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
1851 
1852 	return 0;
1853 }
1854 
1855 static int bnx2x_sriov_info(struct bnx2x *bp, struct bnx2x_sriov *iov)
1856 {
1857 	u32 val;
1858 
1859 	/* read the SRIOV capability structure
1860 	 * The fields can be read via configuration read or
1861 	 * directly from the device (starting at offset PCICFG_OFFSET)
1862 	 */
1863 	if (bnx2x_sriov_pci_cfg_info(bp, iov))
1864 		return -ENODEV;
1865 
1866 	/* get the number of SRIOV bars */
1867 	iov->nres = 0;
1868 
1869 	/* read the first_vfid */
1870 	val = REG_RD(bp, PCICFG_OFFSET + GRC_CONFIG_REG_PF_INIT_VF);
1871 	iov->first_vf_in_pf = ((val & GRC_CR_PF_INIT_VF_PF_FIRST_VF_NUM_MASK)
1872 			       * 8) - (BNX2X_MAX_NUM_OF_VFS * BP_PATH(bp));
1873 
1874 	DP(BNX2X_MSG_IOV,
1875 	   "IOV info[%d]: first vf %d, nres %d, cap 0x%x, ctrl 0x%x, total %d, initial %d, num vfs %d, offset %d, stride %d, page size 0x%x\n",
1876 	   BP_FUNC(bp),
1877 	   iov->first_vf_in_pf, iov->nres, iov->cap, iov->ctrl, iov->total,
1878 	   iov->initial, iov->nr_virtfn, iov->offset, iov->stride, iov->pgsz);
1879 
1880 	return 0;
1881 }
1882 
1883 static u8 bnx2x_iov_get_max_queue_count(struct bnx2x *bp)
1884 {
1885 	int i;
1886 	u8 queue_count = 0;
1887 
1888 	if (IS_SRIOV(bp))
1889 		for_each_vf(bp, i)
1890 			queue_count += bnx2x_vf(bp, i, alloc_resc.num_sbs);
1891 
1892 	return queue_count;
1893 }
1894 
1895 /* must be called after PF bars are mapped */
1896 int bnx2x_iov_init_one(struct bnx2x *bp, int int_mode_param,
1897 			int num_vfs_param)
1898 {
1899 	int err, i, qcount;
1900 	struct bnx2x_sriov *iov;
1901 	struct pci_dev *dev = bp->pdev;
1902 
1903 	bp->vfdb = NULL;
1904 
1905 	/* verify is pf */
1906 	if (IS_VF(bp))
1907 		return 0;
1908 
1909 	/* verify sriov capability is present in configuration space */
1910 	if (!pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV))
1911 		return 0;
1912 
1913 	/* verify chip revision */
1914 	if (CHIP_IS_E1x(bp))
1915 		return 0;
1916 
1917 	/* check if SRIOV support is turned off */
1918 	if (!num_vfs_param)
1919 		return 0;
1920 
1921 	/* SRIOV assumes that num of PF CIDs < BNX2X_FIRST_VF_CID */
1922 	if (BNX2X_L2_MAX_CID(bp) >= BNX2X_FIRST_VF_CID) {
1923 		BNX2X_ERR("PF cids %d are overspilling into vf space (starts at %d). Abort SRIOV\n",
1924 			  BNX2X_L2_MAX_CID(bp), BNX2X_FIRST_VF_CID);
1925 		return 0;
1926 	}
1927 
1928 	/* SRIOV can be enabled only with MSIX */
1929 	if (int_mode_param == BNX2X_INT_MODE_MSI ||
1930 	    int_mode_param == BNX2X_INT_MODE_INTX)
1931 		BNX2X_ERR("Forced MSI/INTx mode is incompatible with SRIOV\n");
1932 
1933 	err = -EIO;
1934 	/* verify ari is enabled */
1935 	if (!bnx2x_ari_enabled(bp->pdev)) {
1936 		BNX2X_ERR("ARI not supported, SRIOV can not be enabled\n");
1937 		return err;
1938 	}
1939 
1940 	/* verify igu is in normal mode */
1941 	if (CHIP_INT_MODE_IS_BC(bp)) {
1942 		BNX2X_ERR("IGU not normal mode,  SRIOV can not be enabled\n");
1943 		return err;
1944 	}
1945 
1946 	/* allocate the vfs database */
1947 	bp->vfdb = kzalloc(sizeof(*(bp->vfdb)), GFP_KERNEL);
1948 	if (!bp->vfdb) {
1949 		BNX2X_ERR("failed to allocate vf database\n");
1950 		err = -ENOMEM;
1951 		goto failed;
1952 	}
1953 
1954 	/* get the sriov info - Linux already collected all the pertinent
1955 	 * information, however the sriov structure is for the private use
1956 	 * of the pci module. Also we want this information regardless
1957 	 * of the hyper-visor.
1958 	 */
1959 	iov = &(bp->vfdb->sriov);
1960 	err = bnx2x_sriov_info(bp, iov);
1961 	if (err)
1962 		goto failed;
1963 
1964 	/* SR-IOV capability was enabled but there are no VFs*/
1965 	if (iov->total == 0)
1966 		goto failed;
1967 
1968 	/* calculate the actual number of VFs */
1969 	iov->nr_virtfn = min_t(u16, iov->total, (u16)num_vfs_param);
1970 
1971 	/* allocate the vf array */
1972 	bp->vfdb->vfs = kzalloc(sizeof(struct bnx2x_virtf) *
1973 				BNX2X_NR_VIRTFN(bp), GFP_KERNEL);
1974 	if (!bp->vfdb->vfs) {
1975 		BNX2X_ERR("failed to allocate vf array\n");
1976 		err = -ENOMEM;
1977 		goto failed;
1978 	}
1979 
1980 	/* Initial VF init - index and abs_vfid - nr_virtfn must be set */
1981 	for_each_vf(bp, i) {
1982 		bnx2x_vf(bp, i, index) = i;
1983 		bnx2x_vf(bp, i, abs_vfid) = iov->first_vf_in_pf + i;
1984 		bnx2x_vf(bp, i, state) = VF_FREE;
1985 		INIT_LIST_HEAD(&bnx2x_vf(bp, i, op_list_head));
1986 		mutex_init(&bnx2x_vf(bp, i, op_mutex));
1987 		bnx2x_vf(bp, i, op_current) = CHANNEL_TLV_NONE;
1988 	}
1989 
1990 	/* re-read the IGU CAM for VFs - index and abs_vfid must be set */
1991 	bnx2x_get_vf_igu_cam_info(bp);
1992 
1993 	/* get the total queue count and allocate the global queue arrays */
1994 	qcount = bnx2x_iov_get_max_queue_count(bp);
1995 
1996 	/* allocate the queue arrays for all VFs */
1997 	bp->vfdb->vfqs = kzalloc(qcount * sizeof(struct bnx2x_vf_queue),
1998 				 GFP_KERNEL);
1999 	if (!bp->vfdb->vfqs) {
2000 		BNX2X_ERR("failed to allocate vf queue array\n");
2001 		err = -ENOMEM;
2002 		goto failed;
2003 	}
2004 
2005 	return 0;
2006 failed:
2007 	DP(BNX2X_MSG_IOV, "Failed err=%d\n", err);
2008 	__bnx2x_iov_free_vfdb(bp);
2009 	return err;
2010 }
2011 
2012 void bnx2x_iov_remove_one(struct bnx2x *bp)
2013 {
2014 	/* if SRIOV is not enabled there's nothing to do */
2015 	if (!IS_SRIOV(bp))
2016 		return;
2017 
2018 	DP(BNX2X_MSG_IOV, "about to call disable sriov\n");
2019 	pci_disable_sriov(bp->pdev);
2020 	DP(BNX2X_MSG_IOV, "sriov disabled\n");
2021 
2022 	/* free vf database */
2023 	__bnx2x_iov_free_vfdb(bp);
2024 }
2025 
2026 void bnx2x_iov_free_mem(struct bnx2x *bp)
2027 {
2028 	int i;
2029 
2030 	if (!IS_SRIOV(bp))
2031 		return;
2032 
2033 	/* free vfs hw contexts */
2034 	for (i = 0; i < BNX2X_VF_CIDS/ILT_PAGE_CIDS; i++) {
2035 		struct hw_dma *cxt = &bp->vfdb->context[i];
2036 		BNX2X_PCI_FREE(cxt->addr, cxt->mapping, cxt->size);
2037 	}
2038 
2039 	BNX2X_PCI_FREE(BP_VFDB(bp)->sp_dma.addr,
2040 		       BP_VFDB(bp)->sp_dma.mapping,
2041 		       BP_VFDB(bp)->sp_dma.size);
2042 
2043 	BNX2X_PCI_FREE(BP_VF_MBX_DMA(bp)->addr,
2044 		       BP_VF_MBX_DMA(bp)->mapping,
2045 		       BP_VF_MBX_DMA(bp)->size);
2046 
2047 	BNX2X_PCI_FREE(BP_VF_BULLETIN_DMA(bp)->addr,
2048 		       BP_VF_BULLETIN_DMA(bp)->mapping,
2049 		       BP_VF_BULLETIN_DMA(bp)->size);
2050 }
2051 
2052 int bnx2x_iov_alloc_mem(struct bnx2x *bp)
2053 {
2054 	size_t tot_size;
2055 	int i, rc = 0;
2056 
2057 	if (!IS_SRIOV(bp))
2058 		return rc;
2059 
2060 	/* allocate vfs hw contexts */
2061 	tot_size = (BP_VFDB(bp)->sriov.first_vf_in_pf + BNX2X_NR_VIRTFN(bp)) *
2062 		BNX2X_CIDS_PER_VF * sizeof(union cdu_context);
2063 
2064 	for (i = 0; i < BNX2X_VF_CIDS/ILT_PAGE_CIDS; i++) {
2065 		struct hw_dma *cxt = BP_VF_CXT_PAGE(bp, i);
2066 		cxt->size = min_t(size_t, tot_size, CDU_ILT_PAGE_SZ);
2067 
2068 		if (cxt->size) {
2069 			BNX2X_PCI_ALLOC(cxt->addr, &cxt->mapping, cxt->size);
2070 		} else {
2071 			cxt->addr = NULL;
2072 			cxt->mapping = 0;
2073 		}
2074 		tot_size -= cxt->size;
2075 	}
2076 
2077 	/* allocate vfs ramrods dma memory - client_init and set_mac */
2078 	tot_size = BNX2X_NR_VIRTFN(bp) * sizeof(struct bnx2x_vf_sp);
2079 	BNX2X_PCI_ALLOC(BP_VFDB(bp)->sp_dma.addr, &BP_VFDB(bp)->sp_dma.mapping,
2080 			tot_size);
2081 	BP_VFDB(bp)->sp_dma.size = tot_size;
2082 
2083 	/* allocate mailboxes */
2084 	tot_size = BNX2X_NR_VIRTFN(bp) * MBX_MSG_ALIGNED_SIZE;
2085 	BNX2X_PCI_ALLOC(BP_VF_MBX_DMA(bp)->addr, &BP_VF_MBX_DMA(bp)->mapping,
2086 			tot_size);
2087 	BP_VF_MBX_DMA(bp)->size = tot_size;
2088 
2089 	/* allocate local bulletin boards */
2090 	tot_size = BNX2X_NR_VIRTFN(bp) * BULLETIN_CONTENT_SIZE;
2091 	BNX2X_PCI_ALLOC(BP_VF_BULLETIN_DMA(bp)->addr,
2092 			&BP_VF_BULLETIN_DMA(bp)->mapping, tot_size);
2093 	BP_VF_BULLETIN_DMA(bp)->size = tot_size;
2094 
2095 	return 0;
2096 
2097 alloc_mem_err:
2098 	return -ENOMEM;
2099 }
2100 
2101 static void bnx2x_vfq_init(struct bnx2x *bp, struct bnx2x_virtf *vf,
2102 			   struct bnx2x_vf_queue *q)
2103 {
2104 	u8 cl_id = vfq_cl_id(vf, q);
2105 	u8 func_id = FW_VF_HANDLE(vf->abs_vfid);
2106 	unsigned long q_type = 0;
2107 
2108 	set_bit(BNX2X_Q_TYPE_HAS_TX, &q_type);
2109 	set_bit(BNX2X_Q_TYPE_HAS_RX, &q_type);
2110 
2111 	/* Queue State object */
2112 	bnx2x_init_queue_obj(bp, &q->sp_obj,
2113 			     cl_id, &q->cid, 1, func_id,
2114 			     bnx2x_vf_sp(bp, vf, q_data),
2115 			     bnx2x_vf_sp_map(bp, vf, q_data),
2116 			     q_type);
2117 
2118 	DP(BNX2X_MSG_IOV,
2119 	   "initialized vf %d's queue object. func id set to %d\n",
2120 	   vf->abs_vfid, q->sp_obj.func_id);
2121 
2122 	/* mac/vlan objects are per queue, but only those
2123 	 * that belong to the leading queue are initialized
2124 	 */
2125 	if (vfq_is_leading(q)) {
2126 		/* mac */
2127 		bnx2x_init_mac_obj(bp, &q->mac_obj,
2128 				   cl_id, q->cid, func_id,
2129 				   bnx2x_vf_sp(bp, vf, mac_rdata),
2130 				   bnx2x_vf_sp_map(bp, vf, mac_rdata),
2131 				   BNX2X_FILTER_MAC_PENDING,
2132 				   &vf->filter_state,
2133 				   BNX2X_OBJ_TYPE_RX_TX,
2134 				   &bp->macs_pool);
2135 		/* vlan */
2136 		bnx2x_init_vlan_obj(bp, &q->vlan_obj,
2137 				    cl_id, q->cid, func_id,
2138 				    bnx2x_vf_sp(bp, vf, vlan_rdata),
2139 				    bnx2x_vf_sp_map(bp, vf, vlan_rdata),
2140 				    BNX2X_FILTER_VLAN_PENDING,
2141 				    &vf->filter_state,
2142 				    BNX2X_OBJ_TYPE_RX_TX,
2143 				    &bp->vlans_pool);
2144 
2145 		/* mcast */
2146 		bnx2x_init_mcast_obj(bp, &vf->mcast_obj, cl_id,
2147 				     q->cid, func_id, func_id,
2148 				     bnx2x_vf_sp(bp, vf, mcast_rdata),
2149 				     bnx2x_vf_sp_map(bp, vf, mcast_rdata),
2150 				     BNX2X_FILTER_MCAST_PENDING,
2151 				     &vf->filter_state,
2152 				     BNX2X_OBJ_TYPE_RX_TX);
2153 
2154 		vf->leading_rss = cl_id;
2155 	}
2156 }
2157 
2158 /* called by bnx2x_nic_load */
2159 int bnx2x_iov_nic_init(struct bnx2x *bp)
2160 {
2161 	int vfid, qcount, i;
2162 
2163 	if (!IS_SRIOV(bp)) {
2164 		DP(BNX2X_MSG_IOV, "vfdb was not allocated\n");
2165 		return 0;
2166 	}
2167 
2168 	DP(BNX2X_MSG_IOV, "num of vfs: %d\n", (bp)->vfdb->sriov.nr_virtfn);
2169 
2170 	/* initialize vf database */
2171 	for_each_vf(bp, vfid) {
2172 		struct bnx2x_virtf *vf = BP_VF(bp, vfid);
2173 
2174 		int base_vf_cid = (BP_VFDB(bp)->sriov.first_vf_in_pf + vfid) *
2175 			BNX2X_CIDS_PER_VF;
2176 
2177 		union cdu_context *base_cxt = (union cdu_context *)
2178 			BP_VF_CXT_PAGE(bp, base_vf_cid/ILT_PAGE_CIDS)->addr +
2179 			(base_vf_cid & (ILT_PAGE_CIDS-1));
2180 
2181 		DP(BNX2X_MSG_IOV,
2182 		   "VF[%d] Max IGU SBs: %d, base vf cid 0x%x, base cid 0x%x, base cxt %p\n",
2183 		   vf->abs_vfid, vf_sb_count(vf), base_vf_cid,
2184 		   BNX2X_FIRST_VF_CID + base_vf_cid, base_cxt);
2185 
2186 		/* init statically provisioned resources */
2187 		bnx2x_iov_static_resc(bp, &vf->alloc_resc);
2188 
2189 		/* queues are initialized during VF-ACQUIRE */
2190 
2191 		/* reserve the vf vlan credit */
2192 		bp->vlans_pool.get(&bp->vlans_pool, vf_vlan_rules_cnt(vf));
2193 
2194 		vf->filter_state = 0;
2195 		vf->sp_cl_id = bnx2x_fp(bp, 0, cl_id);
2196 
2197 		/*  init mcast object - This object will be re-initialized
2198 		 *  during VF-ACQUIRE with the proper cl_id and cid.
2199 		 *  It needs to be initialized here so that it can be safely
2200 		 *  handled by a subsequent FLR flow.
2201 		 */
2202 		bnx2x_init_mcast_obj(bp, &vf->mcast_obj, 0xFF,
2203 				     0xFF, 0xFF, 0xFF,
2204 				     bnx2x_vf_sp(bp, vf, mcast_rdata),
2205 				     bnx2x_vf_sp_map(bp, vf, mcast_rdata),
2206 				     BNX2X_FILTER_MCAST_PENDING,
2207 				     &vf->filter_state,
2208 				     BNX2X_OBJ_TYPE_RX_TX);
2209 
2210 		/* set the mailbox message addresses */
2211 		BP_VF_MBX(bp, vfid)->msg = (struct bnx2x_vf_mbx_msg *)
2212 			(((u8 *)BP_VF_MBX_DMA(bp)->addr) + vfid *
2213 			MBX_MSG_ALIGNED_SIZE);
2214 
2215 		BP_VF_MBX(bp, vfid)->msg_mapping = BP_VF_MBX_DMA(bp)->mapping +
2216 			vfid * MBX_MSG_ALIGNED_SIZE;
2217 
2218 		/* Enable vf mailbox */
2219 		bnx2x_vf_enable_mbx(bp, vf->abs_vfid);
2220 	}
2221 
2222 	/* Final VF init */
2223 	qcount = 0;
2224 	for_each_vf(bp, i) {
2225 		struct bnx2x_virtf *vf = BP_VF(bp, i);
2226 
2227 		/* fill in the BDF and bars */
2228 		vf->bus = bnx2x_vf_bus(bp, i);
2229 		vf->devfn = bnx2x_vf_devfn(bp, i);
2230 		bnx2x_vf_set_bars(bp, vf);
2231 
2232 		DP(BNX2X_MSG_IOV,
2233 		   "VF info[%d]: bus 0x%x, devfn 0x%x, bar0 [0x%x, %d], bar1 [0x%x, %d], bar2 [0x%x, %d]\n",
2234 		   vf->abs_vfid, vf->bus, vf->devfn,
2235 		   (unsigned)vf->bars[0].bar, vf->bars[0].size,
2236 		   (unsigned)vf->bars[1].bar, vf->bars[1].size,
2237 		   (unsigned)vf->bars[2].bar, vf->bars[2].size);
2238 
2239 		/* set local queue arrays */
2240 		vf->vfqs = &bp->vfdb->vfqs[qcount];
2241 		qcount += bnx2x_vf(bp, i, alloc_resc.num_sbs);
2242 	}
2243 
2244 	return 0;
2245 }
2246 
2247 /* called by bnx2x_chip_cleanup */
2248 int bnx2x_iov_chip_cleanup(struct bnx2x *bp)
2249 {
2250 	int i;
2251 
2252 	if (!IS_SRIOV(bp))
2253 		return 0;
2254 
2255 	/* release all the VFs */
2256 	for_each_vf(bp, i)
2257 		bnx2x_vf_release(bp, BP_VF(bp, i), true); /* blocking */
2258 
2259 	return 0;
2260 }
2261 
2262 /* called by bnx2x_init_hw_func, returns the next ilt line */
2263 int bnx2x_iov_init_ilt(struct bnx2x *bp, u16 line)
2264 {
2265 	int i;
2266 	struct bnx2x_ilt *ilt = BP_ILT(bp);
2267 
2268 	if (!IS_SRIOV(bp))
2269 		return line;
2270 
2271 	/* set vfs ilt lines */
2272 	for (i = 0; i < BNX2X_VF_CIDS/ILT_PAGE_CIDS; i++) {
2273 		struct hw_dma *hw_cxt = BP_VF_CXT_PAGE(bp, i);
2274 
2275 		ilt->lines[line+i].page = hw_cxt->addr;
2276 		ilt->lines[line+i].page_mapping = hw_cxt->mapping;
2277 		ilt->lines[line+i].size = hw_cxt->size; /* doesn't matter */
2278 	}
2279 	return line + i;
2280 }
2281 
2282 static u8 bnx2x_iov_is_vf_cid(struct bnx2x *bp, u16 cid)
2283 {
2284 	return ((cid >= BNX2X_FIRST_VF_CID) &&
2285 		((cid - BNX2X_FIRST_VF_CID) < BNX2X_VF_CIDS));
2286 }
2287 
2288 static
2289 void bnx2x_vf_handle_classification_eqe(struct bnx2x *bp,
2290 					struct bnx2x_vf_queue *vfq,
2291 					union event_ring_elem *elem)
2292 {
2293 	unsigned long ramrod_flags = 0;
2294 	int rc = 0;
2295 
2296 	/* Always push next commands out, don't wait here */
2297 	set_bit(RAMROD_CONT, &ramrod_flags);
2298 
2299 	switch (elem->message.data.eth_event.echo >> BNX2X_SWCID_SHIFT) {
2300 	case BNX2X_FILTER_MAC_PENDING:
2301 		rc = vfq->mac_obj.complete(bp, &vfq->mac_obj, elem,
2302 					   &ramrod_flags);
2303 		break;
2304 	case BNX2X_FILTER_VLAN_PENDING:
2305 		rc = vfq->vlan_obj.complete(bp, &vfq->vlan_obj, elem,
2306 					    &ramrod_flags);
2307 		break;
2308 	default:
2309 		BNX2X_ERR("Unsupported classification command: %d\n",
2310 			  elem->message.data.eth_event.echo);
2311 		return;
2312 	}
2313 	if (rc < 0)
2314 		BNX2X_ERR("Failed to schedule new commands: %d\n", rc);
2315 	else if (rc > 0)
2316 		DP(BNX2X_MSG_IOV, "Scheduled next pending commands...\n");
2317 }
2318 
2319 static
2320 void bnx2x_vf_handle_mcast_eqe(struct bnx2x *bp,
2321 			       struct bnx2x_virtf *vf)
2322 {
2323 	struct bnx2x_mcast_ramrod_params rparam = {NULL};
2324 	int rc;
2325 
2326 	rparam.mcast_obj = &vf->mcast_obj;
2327 	vf->mcast_obj.raw.clear_pending(&vf->mcast_obj.raw);
2328 
2329 	/* If there are pending mcast commands - send them */
2330 	if (vf->mcast_obj.check_pending(&vf->mcast_obj)) {
2331 		rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
2332 		if (rc < 0)
2333 			BNX2X_ERR("Failed to send pending mcast commands: %d\n",
2334 				  rc);
2335 	}
2336 }
2337 
2338 static
2339 void bnx2x_vf_handle_filters_eqe(struct bnx2x *bp,
2340 				 struct bnx2x_virtf *vf)
2341 {
2342 	smp_mb__before_clear_bit();
2343 	clear_bit(BNX2X_FILTER_RX_MODE_PENDING, &vf->filter_state);
2344 	smp_mb__after_clear_bit();
2345 }
2346 
2347 int bnx2x_iov_eq_sp_event(struct bnx2x *bp, union event_ring_elem *elem)
2348 {
2349 	struct bnx2x_virtf *vf;
2350 	int qidx = 0, abs_vfid;
2351 	u8 opcode;
2352 	u16 cid = 0xffff;
2353 
2354 	if (!IS_SRIOV(bp))
2355 		return 1;
2356 
2357 	/* first get the cid - the only events we handle here are cfc-delete
2358 	 * and set-mac completion
2359 	 */
2360 	opcode = elem->message.opcode;
2361 
2362 	switch (opcode) {
2363 	case EVENT_RING_OPCODE_CFC_DEL:
2364 		cid = SW_CID((__force __le32)
2365 			     elem->message.data.cfc_del_event.cid);
2366 		DP(BNX2X_MSG_IOV, "checking cfc-del comp cid=%d\n", cid);
2367 		break;
2368 	case EVENT_RING_OPCODE_CLASSIFICATION_RULES:
2369 	case EVENT_RING_OPCODE_MULTICAST_RULES:
2370 	case EVENT_RING_OPCODE_FILTERS_RULES:
2371 		cid = (elem->message.data.eth_event.echo &
2372 		       BNX2X_SWCID_MASK);
2373 		DP(BNX2X_MSG_IOV, "checking filtering comp cid=%d\n", cid);
2374 		break;
2375 	case EVENT_RING_OPCODE_VF_FLR:
2376 		abs_vfid = elem->message.data.vf_flr_event.vf_id;
2377 		DP(BNX2X_MSG_IOV, "Got VF FLR notification abs_vfid=%d\n",
2378 		   abs_vfid);
2379 		goto get_vf;
2380 	case EVENT_RING_OPCODE_MALICIOUS_VF:
2381 		abs_vfid = elem->message.data.malicious_vf_event.vf_id;
2382 		DP(BNX2X_MSG_IOV, "Got VF MALICIOUS notification abs_vfid=%d\n",
2383 		   abs_vfid);
2384 		goto get_vf;
2385 	default:
2386 		return 1;
2387 	}
2388 
2389 	/* check if the cid is the VF range */
2390 	if (!bnx2x_iov_is_vf_cid(bp, cid)) {
2391 		DP(BNX2X_MSG_IOV, "cid is outside vf range: %d\n", cid);
2392 		return 1;
2393 	}
2394 
2395 	/* extract vf and rxq index from vf_cid - relies on the following:
2396 	 * 1. vfid on cid reflects the true abs_vfid
2397 	 * 2. the max number of VFs (per path) is 64
2398 	 */
2399 	qidx = cid & ((1 << BNX2X_VF_CID_WND)-1);
2400 	abs_vfid = (cid >> BNX2X_VF_CID_WND) & (BNX2X_MAX_NUM_OF_VFS-1);
2401 get_vf:
2402 	vf = bnx2x_vf_by_abs_fid(bp, abs_vfid);
2403 
2404 	if (!vf) {
2405 		BNX2X_ERR("EQ completion for unknown VF, cid %d, abs_vfid %d\n",
2406 			  cid, abs_vfid);
2407 		return 0;
2408 	}
2409 
2410 	switch (opcode) {
2411 	case EVENT_RING_OPCODE_CFC_DEL:
2412 		DP(BNX2X_MSG_IOV, "got VF [%d:%d] cfc delete ramrod\n",
2413 		   vf->abs_vfid, qidx);
2414 		vfq_get(vf, qidx)->sp_obj.complete_cmd(bp,
2415 						       &vfq_get(vf,
2416 								qidx)->sp_obj,
2417 						       BNX2X_Q_CMD_CFC_DEL);
2418 		break;
2419 	case EVENT_RING_OPCODE_CLASSIFICATION_RULES:
2420 		DP(BNX2X_MSG_IOV, "got VF [%d:%d] set mac/vlan ramrod\n",
2421 		   vf->abs_vfid, qidx);
2422 		bnx2x_vf_handle_classification_eqe(bp, vfq_get(vf, qidx), elem);
2423 		break;
2424 	case EVENT_RING_OPCODE_MULTICAST_RULES:
2425 		DP(BNX2X_MSG_IOV, "got VF [%d:%d] set mcast ramrod\n",
2426 		   vf->abs_vfid, qidx);
2427 		bnx2x_vf_handle_mcast_eqe(bp, vf);
2428 		break;
2429 	case EVENT_RING_OPCODE_FILTERS_RULES:
2430 		DP(BNX2X_MSG_IOV, "got VF [%d:%d] set rx-mode ramrod\n",
2431 		   vf->abs_vfid, qidx);
2432 		bnx2x_vf_handle_filters_eqe(bp, vf);
2433 		break;
2434 	case EVENT_RING_OPCODE_VF_FLR:
2435 		DP(BNX2X_MSG_IOV, "got VF [%d] FLR notification\n",
2436 		   vf->abs_vfid);
2437 		/* Do nothing for now */
2438 		break;
2439 	case EVENT_RING_OPCODE_MALICIOUS_VF:
2440 		DP(BNX2X_MSG_IOV, "got VF [%d] MALICIOUS notification\n",
2441 		   vf->abs_vfid);
2442 		/* Do nothing for now */
2443 		break;
2444 	}
2445 	/* SRIOV: reschedule any 'in_progress' operations */
2446 	bnx2x_iov_sp_event(bp, cid, false);
2447 
2448 	return 0;
2449 }
2450 
2451 static struct bnx2x_virtf *bnx2x_vf_by_cid(struct bnx2x *bp, int vf_cid)
2452 {
2453 	/* extract the vf from vf_cid - relies on the following:
2454 	 * 1. vfid on cid reflects the true abs_vfid
2455 	 * 2. the max number of VFs (per path) is 64
2456 	 */
2457 	int abs_vfid = (vf_cid >> BNX2X_VF_CID_WND) & (BNX2X_MAX_NUM_OF_VFS-1);
2458 	return bnx2x_vf_by_abs_fid(bp, abs_vfid);
2459 }
2460 
2461 void bnx2x_iov_set_queue_sp_obj(struct bnx2x *bp, int vf_cid,
2462 				struct bnx2x_queue_sp_obj **q_obj)
2463 {
2464 	struct bnx2x_virtf *vf;
2465 
2466 	if (!IS_SRIOV(bp))
2467 		return;
2468 
2469 	vf = bnx2x_vf_by_cid(bp, vf_cid);
2470 
2471 	if (vf) {
2472 		/* extract queue index from vf_cid - relies on the following:
2473 		 * 1. vfid on cid reflects the true abs_vfid
2474 		 * 2. the max number of VFs (per path) is 64
2475 		 */
2476 		int q_index = vf_cid & ((1 << BNX2X_VF_CID_WND)-1);
2477 		*q_obj = &bnx2x_vfq(vf, q_index, sp_obj);
2478 	} else {
2479 		BNX2X_ERR("No vf matching cid %d\n", vf_cid);
2480 	}
2481 }
2482 
2483 void bnx2x_iov_sp_event(struct bnx2x *bp, int vf_cid, bool queue_work)
2484 {
2485 	struct bnx2x_virtf *vf;
2486 
2487 	/* check if the cid is the VF range */
2488 	if (!IS_SRIOV(bp) || !bnx2x_iov_is_vf_cid(bp, vf_cid))
2489 		return;
2490 
2491 	vf = bnx2x_vf_by_cid(bp, vf_cid);
2492 	if (vf) {
2493 		/* set in_progress flag */
2494 		atomic_set(&vf->op_in_progress, 1);
2495 		if (queue_work)
2496 			queue_delayed_work(bnx2x_wq, &bp->sp_task, 0);
2497 	}
2498 }
2499 
2500 void bnx2x_iov_adjust_stats_req(struct bnx2x *bp)
2501 {
2502 	int i;
2503 	int first_queue_query_index, num_queues_req;
2504 	dma_addr_t cur_data_offset;
2505 	struct stats_query_entry *cur_query_entry;
2506 	u8 stats_count = 0;
2507 	bool is_fcoe = false;
2508 
2509 	if (!IS_SRIOV(bp))
2510 		return;
2511 
2512 	if (!NO_FCOE(bp))
2513 		is_fcoe = true;
2514 
2515 	/* fcoe adds one global request and one queue request */
2516 	num_queues_req = BNX2X_NUM_ETH_QUEUES(bp) + is_fcoe;
2517 	first_queue_query_index = BNX2X_FIRST_QUEUE_QUERY_IDX -
2518 		(is_fcoe ? 0 : 1);
2519 
2520 	DP(BNX2X_MSG_IOV,
2521 	   "BNX2X_NUM_ETH_QUEUES %d, is_fcoe %d, first_queue_query_index %d => determined the last non virtual statistics query index is %d. Will add queries on top of that\n",
2522 	   BNX2X_NUM_ETH_QUEUES(bp), is_fcoe, first_queue_query_index,
2523 	   first_queue_query_index + num_queues_req);
2524 
2525 	cur_data_offset = bp->fw_stats_data_mapping +
2526 		offsetof(struct bnx2x_fw_stats_data, queue_stats) +
2527 		num_queues_req * sizeof(struct per_queue_stats);
2528 
2529 	cur_query_entry = &bp->fw_stats_req->
2530 		query[first_queue_query_index + num_queues_req];
2531 
2532 	for_each_vf(bp, i) {
2533 		int j;
2534 		struct bnx2x_virtf *vf = BP_VF(bp, i);
2535 
2536 		if (vf->state != VF_ENABLED) {
2537 			DP(BNX2X_MSG_IOV,
2538 			   "vf %d not enabled so no stats for it\n",
2539 			   vf->abs_vfid);
2540 			continue;
2541 		}
2542 
2543 		DP(BNX2X_MSG_IOV, "add addresses for vf %d\n", vf->abs_vfid);
2544 		for_each_vfq(vf, j) {
2545 			struct bnx2x_vf_queue *rxq = vfq_get(vf, j);
2546 
2547 			/* collect stats fro active queues only */
2548 			if (bnx2x_get_q_logical_state(bp, &rxq->sp_obj) ==
2549 			    BNX2X_Q_LOGICAL_STATE_STOPPED)
2550 				continue;
2551 
2552 			/* create stats query entry for this queue */
2553 			cur_query_entry->kind = STATS_TYPE_QUEUE;
2554 			cur_query_entry->index = vfq_cl_id(vf, rxq);
2555 			cur_query_entry->funcID =
2556 				cpu_to_le16(FW_VF_HANDLE(vf->abs_vfid));
2557 			cur_query_entry->address.hi =
2558 				cpu_to_le32(U64_HI(vf->fw_stat_map));
2559 			cur_query_entry->address.lo =
2560 				cpu_to_le32(U64_LO(vf->fw_stat_map));
2561 			DP(BNX2X_MSG_IOV,
2562 			   "added address %x %x for vf %d queue %d client %d\n",
2563 			   cur_query_entry->address.hi,
2564 			   cur_query_entry->address.lo, cur_query_entry->funcID,
2565 			   j, cur_query_entry->index);
2566 			cur_query_entry++;
2567 			cur_data_offset += sizeof(struct per_queue_stats);
2568 			stats_count++;
2569 		}
2570 	}
2571 	bp->fw_stats_req->hdr.cmd_num = bp->fw_stats_num + stats_count;
2572 }
2573 
2574 void bnx2x_iov_sp_task(struct bnx2x *bp)
2575 {
2576 	int i;
2577 
2578 	if (!IS_SRIOV(bp))
2579 		return;
2580 	/* Iterate over all VFs and invoke state transition for VFs with
2581 	 * 'in-progress' slow-path operations
2582 	 */
2583 	DP(BNX2X_MSG_IOV, "searching for pending vf operations\n");
2584 	for_each_vf(bp, i) {
2585 		struct bnx2x_virtf *vf = BP_VF(bp, i);
2586 
2587 		if (!list_empty(&vf->op_list_head) &&
2588 		    atomic_read(&vf->op_in_progress)) {
2589 			DP(BNX2X_MSG_IOV, "running pending op for vf %d\n", i);
2590 			bnx2x_vfop_cur(bp, vf)->transition(bp, vf);
2591 		}
2592 	}
2593 }
2594 
2595 static inline
2596 struct bnx2x_virtf *__vf_from_stat_id(struct bnx2x *bp, u8 stat_id)
2597 {
2598 	int i;
2599 	struct bnx2x_virtf *vf = NULL;
2600 
2601 	for_each_vf(bp, i) {
2602 		vf = BP_VF(bp, i);
2603 		if (stat_id >= vf->igu_base_id &&
2604 		    stat_id < vf->igu_base_id + vf_sb_count(vf))
2605 			break;
2606 	}
2607 	return vf;
2608 }
2609 
2610 /* VF API helpers */
2611 static void bnx2x_vf_qtbl_set_q(struct bnx2x *bp, u8 abs_vfid, u8 qid,
2612 				u8 enable)
2613 {
2614 	u32 reg = PXP_REG_HST_ZONE_PERMISSION_TABLE + qid * 4;
2615 	u32 val = enable ? (abs_vfid | (1 << 6)) : 0;
2616 
2617 	REG_WR(bp, reg, val);
2618 }
2619 
2620 static void bnx2x_vf_clr_qtbl(struct bnx2x *bp, struct bnx2x_virtf *vf)
2621 {
2622 	int i;
2623 
2624 	for_each_vfq(vf, i)
2625 		bnx2x_vf_qtbl_set_q(bp, vf->abs_vfid,
2626 				    vfq_qzone_id(vf, vfq_get(vf, i)), false);
2627 }
2628 
2629 static void bnx2x_vf_igu_disable(struct bnx2x *bp, struct bnx2x_virtf *vf)
2630 {
2631 	u32 val;
2632 
2633 	/* clear the VF configuration - pretend */
2634 	bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, vf->abs_vfid));
2635 	val = REG_RD(bp, IGU_REG_VF_CONFIGURATION);
2636 	val &= ~(IGU_VF_CONF_MSI_MSIX_EN | IGU_VF_CONF_SINGLE_ISR_EN |
2637 		 IGU_VF_CONF_FUNC_EN | IGU_VF_CONF_PARENT_MASK);
2638 	REG_WR(bp, IGU_REG_VF_CONFIGURATION, val);
2639 	bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
2640 }
2641 
2642 u8 bnx2x_vf_max_queue_cnt(struct bnx2x *bp, struct bnx2x_virtf *vf)
2643 {
2644 	return min_t(u8, min_t(u8, vf_sb_count(vf), BNX2X_CIDS_PER_VF),
2645 		     BNX2X_VF_MAX_QUEUES);
2646 }
2647 
2648 static
2649 int bnx2x_vf_chk_avail_resc(struct bnx2x *bp, struct bnx2x_virtf *vf,
2650 			    struct vf_pf_resc_request *req_resc)
2651 {
2652 	u8 rxq_cnt = vf_rxq_count(vf) ? : bnx2x_vf_max_queue_cnt(bp, vf);
2653 	u8 txq_cnt = vf_txq_count(vf) ? : bnx2x_vf_max_queue_cnt(bp, vf);
2654 
2655 	return ((req_resc->num_rxqs <= rxq_cnt) &&
2656 		(req_resc->num_txqs <= txq_cnt) &&
2657 		(req_resc->num_sbs <= vf_sb_count(vf))   &&
2658 		(req_resc->num_mac_filters <= vf_mac_rules_cnt(vf)) &&
2659 		(req_resc->num_vlan_filters <= vf_vlan_rules_cnt(vf)));
2660 }
2661 
2662 /* CORE VF API */
2663 int bnx2x_vf_acquire(struct bnx2x *bp, struct bnx2x_virtf *vf,
2664 		     struct vf_pf_resc_request *resc)
2665 {
2666 	int base_vf_cid = (BP_VFDB(bp)->sriov.first_vf_in_pf + vf->index) *
2667 		BNX2X_CIDS_PER_VF;
2668 
2669 	union cdu_context *base_cxt = (union cdu_context *)
2670 		BP_VF_CXT_PAGE(bp, base_vf_cid/ILT_PAGE_CIDS)->addr +
2671 		(base_vf_cid & (ILT_PAGE_CIDS-1));
2672 	int i;
2673 
2674 	/* if state is 'acquired' the VF was not released or FLR'd, in
2675 	 * this case the returned resources match the acquired already
2676 	 * acquired resources. Verify that the requested numbers do
2677 	 * not exceed the already acquired numbers.
2678 	 */
2679 	if (vf->state == VF_ACQUIRED) {
2680 		DP(BNX2X_MSG_IOV, "VF[%d] Trying to re-acquire resources (VF was not released or FLR'd)\n",
2681 		   vf->abs_vfid);
2682 
2683 		if (!bnx2x_vf_chk_avail_resc(bp, vf, resc)) {
2684 			BNX2X_ERR("VF[%d] When re-acquiring resources, requested numbers must be <= then previously acquired numbers\n",
2685 				  vf->abs_vfid);
2686 			return -EINVAL;
2687 		}
2688 		return 0;
2689 	}
2690 
2691 	/* Otherwise vf state must be 'free' or 'reset' */
2692 	if (vf->state != VF_FREE && vf->state != VF_RESET) {
2693 		BNX2X_ERR("VF[%d] Can not acquire a VF with state %d\n",
2694 			  vf->abs_vfid, vf->state);
2695 		return -EINVAL;
2696 	}
2697 
2698 	/* static allocation:
2699 	 * the global maximum number are fixed per VF. fail the request if
2700 	 * requested number exceed these globals
2701 	 */
2702 	if (!bnx2x_vf_chk_avail_resc(bp, vf, resc)) {
2703 		DP(BNX2X_MSG_IOV,
2704 		   "cannot fulfill vf resource request. Placing maximal available values in response\n");
2705 		/* set the max resource in the vf */
2706 		return -ENOMEM;
2707 	}
2708 
2709 	/* Set resources counters - 0 request means max available */
2710 	vf_sb_count(vf) = resc->num_sbs;
2711 	vf_rxq_count(vf) = resc->num_rxqs ? : bnx2x_vf_max_queue_cnt(bp, vf);
2712 	vf_txq_count(vf) = resc->num_txqs ? : bnx2x_vf_max_queue_cnt(bp, vf);
2713 	if (resc->num_mac_filters)
2714 		vf_mac_rules_cnt(vf) = resc->num_mac_filters;
2715 	if (resc->num_vlan_filters)
2716 		vf_vlan_rules_cnt(vf) = resc->num_vlan_filters;
2717 
2718 	DP(BNX2X_MSG_IOV,
2719 	   "Fulfilling vf request: sb count %d, tx_count %d, rx_count %d, mac_rules_count %d, vlan_rules_count %d\n",
2720 	   vf_sb_count(vf), vf_rxq_count(vf),
2721 	   vf_txq_count(vf), vf_mac_rules_cnt(vf),
2722 	   vf_vlan_rules_cnt(vf));
2723 
2724 	/* Initialize the queues */
2725 	if (!vf->vfqs) {
2726 		DP(BNX2X_MSG_IOV, "vf->vfqs was not allocated\n");
2727 		return -EINVAL;
2728 	}
2729 
2730 	for_each_vfq(vf, i) {
2731 		struct bnx2x_vf_queue *q = vfq_get(vf, i);
2732 
2733 		if (!q) {
2734 			DP(BNX2X_MSG_IOV, "q number %d was not allocated\n", i);
2735 			return -EINVAL;
2736 		}
2737 
2738 		q->index = i;
2739 		q->cxt = &((base_cxt + i)->eth);
2740 		q->cid = BNX2X_FIRST_VF_CID + base_vf_cid + i;
2741 
2742 		DP(BNX2X_MSG_IOV, "VFQ[%d:%d]: index %d, cid 0x%x, cxt %p\n",
2743 		   vf->abs_vfid, i, q->index, q->cid, q->cxt);
2744 
2745 		/* init SP objects */
2746 		bnx2x_vfq_init(bp, vf, q);
2747 	}
2748 	vf->state = VF_ACQUIRED;
2749 	return 0;
2750 }
2751 
2752 int bnx2x_vf_init(struct bnx2x *bp, struct bnx2x_virtf *vf, dma_addr_t *sb_map)
2753 {
2754 	struct bnx2x_func_init_params func_init = {0};
2755 	u16 flags = 0;
2756 	int i;
2757 
2758 	/* the sb resources are initialized at this point, do the
2759 	 * FW/HW initializations
2760 	 */
2761 	for_each_vf_sb(vf, i)
2762 		bnx2x_init_sb(bp, (dma_addr_t)sb_map[i], vf->abs_vfid, true,
2763 			      vf_igu_sb(vf, i), vf_igu_sb(vf, i));
2764 
2765 	/* Sanity checks */
2766 	if (vf->state != VF_ACQUIRED) {
2767 		DP(BNX2X_MSG_IOV, "VF[%d] is not in VF_ACQUIRED, but %d\n",
2768 		   vf->abs_vfid, vf->state);
2769 		return -EINVAL;
2770 	}
2771 	/* FLR cleanup epilogue */
2772 	if (bnx2x_vf_flr_clnup_epilog(bp, vf->abs_vfid))
2773 		return -EBUSY;
2774 
2775 	/* reset IGU VF statistics: MSIX */
2776 	REG_WR(bp, IGU_REG_STATISTIC_NUM_MESSAGE_SENT + vf->abs_vfid * 4 , 0);
2777 
2778 	/* vf init */
2779 	if (vf->cfg_flags & VF_CFG_STATS)
2780 		flags |= (FUNC_FLG_STATS | FUNC_FLG_SPQ);
2781 
2782 	if (vf->cfg_flags & VF_CFG_TPA)
2783 		flags |= FUNC_FLG_TPA;
2784 
2785 	if (is_vf_multi(vf))
2786 		flags |= FUNC_FLG_RSS;
2787 
2788 	/* function setup */
2789 	func_init.func_flgs = flags;
2790 	func_init.pf_id = BP_FUNC(bp);
2791 	func_init.func_id = FW_VF_HANDLE(vf->abs_vfid);
2792 	func_init.fw_stat_map = vf->fw_stat_map;
2793 	func_init.spq_map = vf->spq_map;
2794 	func_init.spq_prod = 0;
2795 	bnx2x_func_init(bp, &func_init);
2796 
2797 	/* Enable the vf */
2798 	bnx2x_vf_enable_access(bp, vf->abs_vfid);
2799 	bnx2x_vf_enable_traffic(bp, vf);
2800 
2801 	/* queue protection table */
2802 	for_each_vfq(vf, i)
2803 		bnx2x_vf_qtbl_set_q(bp, vf->abs_vfid,
2804 				    vfq_qzone_id(vf, vfq_get(vf, i)), true);
2805 
2806 	vf->state = VF_ENABLED;
2807 
2808 	/* update vf bulletin board */
2809 	bnx2x_post_vf_bulletin(bp, vf->index);
2810 
2811 	return 0;
2812 }
2813 
2814 /* VFOP close (teardown the queues, delete mcasts and close HW) */
2815 static void bnx2x_vfop_close(struct bnx2x *bp, struct bnx2x_virtf *vf)
2816 {
2817 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
2818 	struct bnx2x_vfop_args_qx *qx = &vfop->args.qx;
2819 	enum bnx2x_vfop_close_state state = vfop->state;
2820 	struct bnx2x_vfop_cmd cmd = {
2821 		.done = bnx2x_vfop_close,
2822 		.block = false,
2823 	};
2824 
2825 	if (vfop->rc < 0)
2826 		goto op_err;
2827 
2828 	DP(BNX2X_MSG_IOV, "vf[%d] STATE: %d\n", vf->abs_vfid, state);
2829 
2830 	switch (state) {
2831 	case BNX2X_VFOP_CLOSE_QUEUES:
2832 
2833 		if (++(qx->qid) < vf_rxq_count(vf)) {
2834 			vfop->rc = bnx2x_vfop_qdown_cmd(bp, vf, &cmd, qx->qid);
2835 			if (vfop->rc)
2836 				goto op_err;
2837 			return;
2838 		}
2839 
2840 		/* remove multicasts */
2841 		vfop->state = BNX2X_VFOP_CLOSE_HW;
2842 		vfop->rc = bnx2x_vfop_mcast_cmd(bp, vf, &cmd, NULL, 0, false);
2843 		if (vfop->rc)
2844 			goto op_err;
2845 		return;
2846 
2847 	case BNX2X_VFOP_CLOSE_HW:
2848 
2849 		/* disable the interrupts */
2850 		DP(BNX2X_MSG_IOV, "disabling igu\n");
2851 		bnx2x_vf_igu_disable(bp, vf);
2852 
2853 		/* disable the VF */
2854 		DP(BNX2X_MSG_IOV, "clearing qtbl\n");
2855 		bnx2x_vf_clr_qtbl(bp, vf);
2856 
2857 		goto op_done;
2858 	default:
2859 		bnx2x_vfop_default(state);
2860 	}
2861 op_err:
2862 	BNX2X_ERR("VF[%d] CLOSE error: rc %d\n", vf->abs_vfid, vfop->rc);
2863 op_done:
2864 	vf->state = VF_ACQUIRED;
2865 	DP(BNX2X_MSG_IOV, "set state to acquired\n");
2866 	bnx2x_vfop_end(bp, vf, vfop);
2867 }
2868 
2869 int bnx2x_vfop_close_cmd(struct bnx2x *bp,
2870 			 struct bnx2x_virtf *vf,
2871 			 struct bnx2x_vfop_cmd *cmd)
2872 {
2873 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
2874 	if (vfop) {
2875 		vfop->args.qx.qid = -1; /* loop */
2876 		bnx2x_vfop_opset(BNX2X_VFOP_CLOSE_QUEUES,
2877 				 bnx2x_vfop_close, cmd->done);
2878 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_close,
2879 					     cmd->block);
2880 	}
2881 	return -ENOMEM;
2882 }
2883 
2884 /* VF release can be called either: 1. the VF was acquired but
2885  * not enabled 2. the vf was enabled or in the process of being
2886  * enabled
2887  */
2888 static void bnx2x_vfop_release(struct bnx2x *bp, struct bnx2x_virtf *vf)
2889 {
2890 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
2891 	struct bnx2x_vfop_cmd cmd = {
2892 		.done = bnx2x_vfop_release,
2893 		.block = false,
2894 	};
2895 
2896 	DP(BNX2X_MSG_IOV, "vfop->rc %d\n", vfop->rc);
2897 
2898 	if (vfop->rc < 0)
2899 		goto op_err;
2900 
2901 	DP(BNX2X_MSG_IOV, "VF[%d] STATE: %s\n", vf->abs_vfid,
2902 	   vf->state == VF_FREE ? "Free" :
2903 	   vf->state == VF_ACQUIRED ? "Acquired" :
2904 	   vf->state == VF_ENABLED ? "Enabled" :
2905 	   vf->state == VF_RESET ? "Reset" :
2906 	   "Unknown");
2907 
2908 	switch (vf->state) {
2909 	case VF_ENABLED:
2910 		vfop->rc = bnx2x_vfop_close_cmd(bp, vf, &cmd);
2911 		if (vfop->rc)
2912 			goto op_err;
2913 		return;
2914 
2915 	case VF_ACQUIRED:
2916 		DP(BNX2X_MSG_IOV, "about to free resources\n");
2917 		bnx2x_vf_free_resc(bp, vf);
2918 		DP(BNX2X_MSG_IOV, "vfop->rc %d\n", vfop->rc);
2919 		goto op_done;
2920 
2921 	case VF_FREE:
2922 	case VF_RESET:
2923 		/* do nothing */
2924 		goto op_done;
2925 	default:
2926 		bnx2x_vfop_default(vf->state);
2927 	}
2928 op_err:
2929 	BNX2X_ERR("VF[%d] RELEASE error: rc %d\n", vf->abs_vfid, vfop->rc);
2930 op_done:
2931 	bnx2x_vfop_end(bp, vf, vfop);
2932 }
2933 
2934 int bnx2x_vfop_release_cmd(struct bnx2x *bp,
2935 			   struct bnx2x_virtf *vf,
2936 			   struct bnx2x_vfop_cmd *cmd)
2937 {
2938 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
2939 	if (vfop) {
2940 		bnx2x_vfop_opset(-1, /* use vf->state */
2941 				 bnx2x_vfop_release, cmd->done);
2942 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_release,
2943 					     cmd->block);
2944 	}
2945 	return -ENOMEM;
2946 }
2947 
2948 /* VF release ~ VF close + VF release-resources
2949  * Release is the ultimate SW shutdown and is called whenever an
2950  * irrecoverable error is encountered.
2951  */
2952 void bnx2x_vf_release(struct bnx2x *bp, struct bnx2x_virtf *vf, bool block)
2953 {
2954 	struct bnx2x_vfop_cmd cmd = {
2955 		.done = NULL,
2956 		.block = block,
2957 	};
2958 	int rc;
2959 	bnx2x_lock_vf_pf_channel(bp, vf, CHANNEL_TLV_PF_RELEASE_VF);
2960 
2961 	rc = bnx2x_vfop_release_cmd(bp, vf, &cmd);
2962 	if (rc)
2963 		WARN(rc,
2964 		     "VF[%d] Failed to allocate resources for release op- rc=%d\n",
2965 		     vf->abs_vfid, rc);
2966 }
2967 
2968 static inline void bnx2x_vf_get_sbdf(struct bnx2x *bp,
2969 			      struct bnx2x_virtf *vf, u32 *sbdf)
2970 {
2971 	*sbdf = vf->devfn | (vf->bus << 8);
2972 }
2973 
2974 static inline void bnx2x_vf_get_bars(struct bnx2x *bp, struct bnx2x_virtf *vf,
2975 		       struct bnx2x_vf_bar_info *bar_info)
2976 {
2977 	int n;
2978 
2979 	bar_info->nr_bars = bp->vfdb->sriov.nres;
2980 	for (n = 0; n < bar_info->nr_bars; n++)
2981 		bar_info->bars[n] = vf->bars[n];
2982 }
2983 
2984 void bnx2x_lock_vf_pf_channel(struct bnx2x *bp, struct bnx2x_virtf *vf,
2985 			      enum channel_tlvs tlv)
2986 {
2987 	/* lock the channel */
2988 	mutex_lock(&vf->op_mutex);
2989 
2990 	/* record the locking op */
2991 	vf->op_current = tlv;
2992 
2993 	/* log the lock */
2994 	DP(BNX2X_MSG_IOV, "VF[%d]: vf pf channel locked by %d\n",
2995 	   vf->abs_vfid, tlv);
2996 }
2997 
2998 void bnx2x_unlock_vf_pf_channel(struct bnx2x *bp, struct bnx2x_virtf *vf,
2999 				enum channel_tlvs expected_tlv)
3000 {
3001 	WARN(expected_tlv != vf->op_current,
3002 	     "lock mismatch: expected %d found %d", expected_tlv,
3003 	     vf->op_current);
3004 
3005 	/* lock the channel */
3006 	mutex_unlock(&vf->op_mutex);
3007 
3008 	/* log the unlock */
3009 	DP(BNX2X_MSG_IOV, "VF[%d]: vf pf channel unlocked by %d\n",
3010 	   vf->abs_vfid, vf->op_current);
3011 
3012 	/* record the locking op */
3013 	vf->op_current = CHANNEL_TLV_NONE;
3014 }
3015 
3016 void bnx2x_enable_sriov(struct bnx2x *bp)
3017 {
3018 	int rc = 0;
3019 
3020 	/* disbale sriov in case it is still enabled */
3021 	pci_disable_sriov(bp->pdev);
3022 	DP(BNX2X_MSG_IOV, "sriov disabled\n");
3023 
3024 	/* enable sriov */
3025 	DP(BNX2X_MSG_IOV, "vf num (%d)\n", (bp->vfdb->sriov.nr_virtfn));
3026 	rc = pci_enable_sriov(bp->pdev, (bp->vfdb->sriov.nr_virtfn));
3027 	if (rc)
3028 		BNX2X_ERR("pci_enable_sriov failed with %d\n", rc);
3029 	else
3030 		DP(BNX2X_MSG_IOV, "sriov enabled\n");
3031 }
3032 
3033 /* New mac for VF. Consider these cases:
3034  * 1. VF hasn't been acquired yet - save the mac in local bulletin board and
3035  *    supply at acquire.
3036  * 2. VF has already been acquired but has not yet initialized - store in local
3037  *    bulletin board. mac will be posted on VF bulletin board after VF init. VF
3038  *    will configure this mac when it is ready.
3039  * 3. VF has already initialized but has not yet setup a queue - post the new
3040  *    mac on VF's bulletin board right now. VF will configure this mac when it
3041  *    is ready.
3042  * 4. VF has already set a queue - delete any macs already configured for this
3043  *    queue and manually config the new mac.
3044  * In any event, once this function has been called refuse any attempts by the
3045  * VF to configure any mac for itself except for this mac. In case of a race
3046  * where the VF fails to see the new post on its bulletin board before sending a
3047  * mac configuration request, the PF will simply fail the request and VF can try
3048  * again after consulting its bulletin board
3049  */
3050 int bnx2x_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
3051 {
3052 	struct bnx2x *bp = netdev_priv(dev);
3053 	int rc, q_logical_state, vfidx = queue;
3054 	struct bnx2x_virtf *vf = BP_VF(bp, vfidx);
3055 	struct pf_vf_bulletin_content *bulletin = BP_VF_BULLETIN(bp, vfidx);
3056 
3057 	/* if SRIOV is disabled there is nothing to do (and somewhere, someone
3058 	 * has erred).
3059 	 */
3060 	if (!IS_SRIOV(bp)) {
3061 		BNX2X_ERR("bnx2x_set_vf_mac called though sriov is disabled\n");
3062 		return -EINVAL;
3063 	}
3064 
3065 	if (!is_valid_ether_addr(mac)) {
3066 		BNX2X_ERR("mac address invalid\n");
3067 		return -EINVAL;
3068 	}
3069 
3070 	/* update PF's copy of the VF's bulletin. will no longer accept mac
3071 	 * configuration requests from vf unless match this mac
3072 	 */
3073 	bulletin->valid_bitmap |= 1 << MAC_ADDR_VALID;
3074 	memcpy(bulletin->mac, mac, ETH_ALEN);
3075 
3076 	/* Post update on VF's bulletin board */
3077 	rc = bnx2x_post_vf_bulletin(bp, vfidx);
3078 	if (rc) {
3079 		BNX2X_ERR("failed to update VF[%d] bulletin\n", vfidx);
3080 		return rc;
3081 	}
3082 
3083 	/* is vf initialized and queue set up? */
3084 	q_logical_state =
3085 		bnx2x_get_q_logical_state(bp, &bnx2x_vfq(vf, 0, sp_obj));
3086 	if (vf->state == VF_ENABLED &&
3087 	    q_logical_state == BNX2X_Q_LOGICAL_STATE_ACTIVE) {
3088 		/* configure the mac in device on this vf's queue */
3089 		unsigned long flags = 0;
3090 		struct bnx2x_vlan_mac_obj *mac_obj = &bnx2x_vfq(vf, 0, mac_obj);
3091 
3092 		/* must lock vfpf channel to protect against vf flows */
3093 		bnx2x_lock_vf_pf_channel(bp, vf, CHANNEL_TLV_PF_SET_MAC);
3094 
3095 		/* remove existing eth macs */
3096 		rc = bnx2x_del_all_macs(bp, mac_obj, BNX2X_ETH_MAC, true);
3097 		if (rc) {
3098 			BNX2X_ERR("failed to delete eth macs\n");
3099 			return -EINVAL;
3100 		}
3101 
3102 		/* remove existing uc list macs */
3103 		rc = bnx2x_del_all_macs(bp, mac_obj, BNX2X_UC_LIST_MAC, true);
3104 		if (rc) {
3105 			BNX2X_ERR("failed to delete uc_list macs\n");
3106 			return -EINVAL;
3107 		}
3108 
3109 		/* configure the new mac to device */
3110 		__set_bit(RAMROD_COMP_WAIT, &flags);
3111 		bnx2x_set_mac_one(bp, (u8 *)&bulletin->mac, mac_obj, true,
3112 				  BNX2X_ETH_MAC, &flags);
3113 
3114 		bnx2x_unlock_vf_pf_channel(bp, vf, CHANNEL_TLV_PF_SET_MAC);
3115 	}
3116 
3117 	return rc;
3118 }
3119 
3120 /* crc is the first field in the bulletin board. compute the crc over the
3121  * entire bulletin board excluding the crc field itself
3122  */
3123 u32 bnx2x_crc_vf_bulletin(struct bnx2x *bp,
3124 			  struct pf_vf_bulletin_content *bulletin)
3125 {
3126 	return crc32(BULLETIN_CRC_SEED,
3127 		 ((u8 *)bulletin) + sizeof(bulletin->crc),
3128 		 BULLETIN_CONTENT_SIZE - sizeof(bulletin->crc));
3129 }
3130 
3131 /* Check for new posts on the bulletin board */
3132 enum sample_bulletin_result bnx2x_sample_bulletin(struct bnx2x *bp)
3133 {
3134 	struct pf_vf_bulletin_content bulletin = bp->pf2vf_bulletin->content;
3135 	int attempts;
3136 
3137 	/* bulletin board hasn't changed since last sample */
3138 	if (bp->old_bulletin.version == bulletin.version)
3139 		return PFVF_BULLETIN_UNCHANGED;
3140 
3141 	/* validate crc of new bulletin board */
3142 	if (bp->old_bulletin.version != bp->pf2vf_bulletin->content.version) {
3143 		/* sampling structure in mid post may result with corrupted data
3144 		 * validate crc to ensure coherency.
3145 		 */
3146 		for (attempts = 0; attempts < BULLETIN_ATTEMPTS; attempts++) {
3147 			bulletin = bp->pf2vf_bulletin->content;
3148 			if (bulletin.crc == bnx2x_crc_vf_bulletin(bp,
3149 								  &bulletin))
3150 				break;
3151 			BNX2X_ERR("bad crc on bulletin board. contained %x computed %x\n",
3152 				  bulletin.crc,
3153 				  bnx2x_crc_vf_bulletin(bp, &bulletin));
3154 		}
3155 		if (attempts >= BULLETIN_ATTEMPTS) {
3156 			BNX2X_ERR("pf to vf bulletin board crc was wrong %d consecutive times. Aborting\n",
3157 				  attempts);
3158 			return PFVF_BULLETIN_CRC_ERR;
3159 		}
3160 	}
3161 
3162 	/* the mac address in bulletin board is valid and is new */
3163 	if (bulletin.valid_bitmap & 1 << MAC_ADDR_VALID &&
3164 	    memcmp(bulletin.mac, bp->old_bulletin.mac, ETH_ALEN)) {
3165 		/* update new mac to net device */
3166 		memcpy(bp->dev->dev_addr, bulletin.mac, ETH_ALEN);
3167 	}
3168 
3169 	/* copy new bulletin board to bp */
3170 	bp->old_bulletin = bulletin;
3171 
3172 	return PFVF_BULLETIN_UPDATED;
3173 }
3174 
3175 void bnx2x_vf_map_doorbells(struct bnx2x *bp)
3176 {
3177 	/* vf doorbells are embedded within the regview */
3178 	bp->doorbells = bp->regview + PXP_VF_ADDR_DB_START;
3179 }
3180 
3181 int bnx2x_vf_pci_alloc(struct bnx2x *bp)
3182 {
3183 	/* allocate vf2pf mailbox for vf to pf channel */
3184 	BNX2X_PCI_ALLOC(bp->vf2pf_mbox, &bp->vf2pf_mbox_mapping,
3185 			sizeof(struct bnx2x_vf_mbx_msg));
3186 
3187 	/* allocate pf 2 vf bulletin board */
3188 	BNX2X_PCI_ALLOC(bp->pf2vf_bulletin, &bp->pf2vf_bulletin_mapping,
3189 			sizeof(union pf_vf_bulletin));
3190 
3191 	return 0;
3192 
3193 alloc_mem_err:
3194 	BNX2X_PCI_FREE(bp->vf2pf_mbox, bp->vf2pf_mbox_mapping,
3195 		       sizeof(struct bnx2x_vf_mbx_msg));
3196 	BNX2X_PCI_FREE(bp->vf2pf_mbox, bp->vf2pf_mbox_mapping,
3197 		       sizeof(union pf_vf_bulletin));
3198 	return -ENOMEM;
3199 }
3200