1 /* bnx2x_vfpf.c: Broadcom Everest network driver.
2  *
3  * Copyright 2009-2013 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_cmn.h"
22 #include <linux/crc32.h>
23 
24 /* place a given tlv on the tlv buffer at a given offset */
25 void bnx2x_add_tlv(struct bnx2x *bp, void *tlvs_list, u16 offset, u16 type,
26 		   u16 length)
27 {
28 	struct channel_tlv *tl =
29 		(struct channel_tlv *)(tlvs_list + offset);
30 
31 	tl->type = type;
32 	tl->length = length;
33 }
34 
35 /* Clear the mailbox and init the header of the first tlv */
36 void bnx2x_vfpf_prep(struct bnx2x *bp, struct vfpf_first_tlv *first_tlv,
37 		     u16 type, u16 length)
38 {
39 	mutex_lock(&bp->vf2pf_mutex);
40 
41 	DP(BNX2X_MSG_IOV, "preparing to send %d tlv over vf pf channel\n",
42 	   type);
43 
44 	/* Clear mailbox */
45 	memset(bp->vf2pf_mbox, 0, sizeof(struct bnx2x_vf_mbx_msg));
46 
47 	/* init type and length */
48 	bnx2x_add_tlv(bp, &first_tlv->tl, 0, type, length);
49 
50 	/* init first tlv header */
51 	first_tlv->resp_msg_offset = sizeof(bp->vf2pf_mbox->req);
52 }
53 
54 /* releases the mailbox */
55 void bnx2x_vfpf_finalize(struct bnx2x *bp, struct vfpf_first_tlv *first_tlv)
56 {
57 	DP(BNX2X_MSG_IOV, "done sending [%d] tlv over vf pf channel\n",
58 	   first_tlv->tl.type);
59 
60 	mutex_unlock(&bp->vf2pf_mutex);
61 }
62 
63 /* list the types and lengths of the tlvs on the buffer */
64 void bnx2x_dp_tlv_list(struct bnx2x *bp, void *tlvs_list)
65 {
66 	int i = 1;
67 	struct channel_tlv *tlv = (struct channel_tlv *)tlvs_list;
68 
69 	while (tlv->type != CHANNEL_TLV_LIST_END) {
70 		/* output tlv */
71 		DP(BNX2X_MSG_IOV, "TLV number %d: type %d, length %d\n", i,
72 		   tlv->type, tlv->length);
73 
74 		/* advance to next tlv */
75 		tlvs_list += tlv->length;
76 
77 		/* cast general tlv list pointer to channel tlv header*/
78 		tlv = (struct channel_tlv *)tlvs_list;
79 
80 		i++;
81 
82 		/* break condition for this loop */
83 		if (i > MAX_TLVS_IN_LIST) {
84 			WARN(true, "corrupt tlvs");
85 			return;
86 		}
87 	}
88 
89 	/* output last tlv */
90 	DP(BNX2X_MSG_IOV, "TLV number %d: type %d, length %d\n", i,
91 	   tlv->type, tlv->length);
92 }
93 
94 /* test whether we support a tlv type */
95 bool bnx2x_tlv_supported(u16 tlvtype)
96 {
97 	return CHANNEL_TLV_NONE < tlvtype && tlvtype < CHANNEL_TLV_MAX;
98 }
99 
100 static inline int bnx2x_pfvf_status_codes(int rc)
101 {
102 	switch (rc) {
103 	case 0:
104 		return PFVF_STATUS_SUCCESS;
105 	case -ENOMEM:
106 		return PFVF_STATUS_NO_RESOURCE;
107 	default:
108 		return PFVF_STATUS_FAILURE;
109 	}
110 }
111 
112 static int bnx2x_send_msg2pf(struct bnx2x *bp, u8 *done, dma_addr_t msg_mapping)
113 {
114 	struct cstorm_vf_zone_data __iomem *zone_data =
115 		REG_ADDR(bp, PXP_VF_ADDR_CSDM_GLOBAL_START);
116 	int tout = 600, interval = 100; /* wait for 60 seconds */
117 
118 	if (*done) {
119 		BNX2X_ERR("done was non zero before message to pf was sent\n");
120 		WARN_ON(true);
121 		return -EINVAL;
122 	}
123 
124 	/* Write message address */
125 	writel(U64_LO(msg_mapping),
126 	       &zone_data->non_trigger.vf_pf_channel.msg_addr_lo);
127 	writel(U64_HI(msg_mapping),
128 	       &zone_data->non_trigger.vf_pf_channel.msg_addr_hi);
129 
130 	/* make sure the address is written before FW accesses it */
131 	wmb();
132 
133 	/* Trigger the PF FW */
134 	writeb(1, &zone_data->trigger.vf_pf_channel.addr_valid);
135 
136 	/* Wait for PF to complete */
137 	while ((tout >= 0) && (!*done)) {
138 		msleep(interval);
139 		tout -= 1;
140 
141 		/* progress indicator - HV can take its own sweet time in
142 		 * answering VFs...
143 		 */
144 		DP_CONT(BNX2X_MSG_IOV, ".");
145 	}
146 
147 	if (!*done) {
148 		BNX2X_ERR("PF response has timed out\n");
149 		return -EAGAIN;
150 	}
151 	DP(BNX2X_MSG_SP, "Got a response from PF\n");
152 	return 0;
153 }
154 
155 static int bnx2x_get_vf_id(struct bnx2x *bp, u32 *vf_id)
156 {
157 	u32 me_reg;
158 	int tout = 10, interval = 100; /* Wait for 1 sec */
159 
160 	do {
161 		/* pxp traps vf read of doorbells and returns me reg value */
162 		me_reg = readl(bp->doorbells);
163 		if (GOOD_ME_REG(me_reg))
164 			break;
165 
166 		msleep(interval);
167 
168 		BNX2X_ERR("Invalid ME register value: 0x%08x\n. Is pf driver up?",
169 			  me_reg);
170 	} while (tout-- > 0);
171 
172 	if (!GOOD_ME_REG(me_reg)) {
173 		BNX2X_ERR("Invalid ME register value: 0x%08x\n", me_reg);
174 		return -EINVAL;
175 	}
176 
177 	BNX2X_ERR("valid ME register value: 0x%08x\n", me_reg);
178 
179 	*vf_id = (me_reg & ME_REG_VF_NUM_MASK) >> ME_REG_VF_NUM_SHIFT;
180 
181 	return 0;
182 }
183 
184 int bnx2x_vfpf_acquire(struct bnx2x *bp, u8 tx_count, u8 rx_count)
185 {
186 	int rc = 0, attempts = 0;
187 	struct vfpf_acquire_tlv *req = &bp->vf2pf_mbox->req.acquire;
188 	struct pfvf_acquire_resp_tlv *resp = &bp->vf2pf_mbox->resp.acquire_resp;
189 	u32 vf_id;
190 	bool resources_acquired = false;
191 
192 	/* clear mailbox and prep first tlv */
193 	bnx2x_vfpf_prep(bp, &req->first_tlv, CHANNEL_TLV_ACQUIRE, sizeof(*req));
194 
195 	if (bnx2x_get_vf_id(bp, &vf_id)) {
196 		rc = -EAGAIN;
197 		goto out;
198 	}
199 
200 	req->vfdev_info.vf_id = vf_id;
201 	req->vfdev_info.vf_os = 0;
202 
203 	req->resc_request.num_rxqs = rx_count;
204 	req->resc_request.num_txqs = tx_count;
205 	req->resc_request.num_sbs = bp->igu_sb_cnt;
206 	req->resc_request.num_mac_filters = VF_ACQUIRE_MAC_FILTERS;
207 	req->resc_request.num_mc_filters = VF_ACQUIRE_MC_FILTERS;
208 
209 	/* pf 2 vf bulletin board address */
210 	req->bulletin_addr = bp->pf2vf_bulletin_mapping;
211 
212 	/* add list termination tlv */
213 	bnx2x_add_tlv(bp, req, req->first_tlv.tl.length, CHANNEL_TLV_LIST_END,
214 		      sizeof(struct channel_list_end_tlv));
215 
216 	/* output tlvs list */
217 	bnx2x_dp_tlv_list(bp, req);
218 
219 	while (!resources_acquired) {
220 		DP(BNX2X_MSG_SP, "attempting to acquire resources\n");
221 
222 		/* send acquire request */
223 		rc = bnx2x_send_msg2pf(bp,
224 				       &resp->hdr.status,
225 				       bp->vf2pf_mbox_mapping);
226 
227 		/* PF timeout */
228 		if (rc)
229 			goto out;
230 
231 		/* copy acquire response from buffer to bp */
232 		memcpy(&bp->acquire_resp, resp, sizeof(bp->acquire_resp));
233 
234 		attempts++;
235 
236 		/* test whether the PF accepted our request. If not, humble the
237 		 * the request and try again.
238 		 */
239 		if (bp->acquire_resp.hdr.status == PFVF_STATUS_SUCCESS) {
240 			DP(BNX2X_MSG_SP, "resources acquired\n");
241 			resources_acquired = true;
242 		} else if (bp->acquire_resp.hdr.status ==
243 			   PFVF_STATUS_NO_RESOURCE &&
244 			   attempts < VF_ACQUIRE_THRESH) {
245 			DP(BNX2X_MSG_SP,
246 			   "PF unwilling to fulfill resource request. Try PF recommended amount\n");
247 
248 			/* humble our request */
249 			req->resc_request.num_txqs =
250 				bp->acquire_resp.resc.num_txqs;
251 			req->resc_request.num_rxqs =
252 				bp->acquire_resp.resc.num_rxqs;
253 			req->resc_request.num_sbs =
254 				bp->acquire_resp.resc.num_sbs;
255 			req->resc_request.num_mac_filters =
256 				bp->acquire_resp.resc.num_mac_filters;
257 			req->resc_request.num_vlan_filters =
258 				bp->acquire_resp.resc.num_vlan_filters;
259 			req->resc_request.num_mc_filters =
260 				bp->acquire_resp.resc.num_mc_filters;
261 
262 			/* Clear response buffer */
263 			memset(&bp->vf2pf_mbox->resp, 0,
264 			       sizeof(union pfvf_tlvs));
265 		} else {
266 			/* PF reports error */
267 			BNX2X_ERR("Failed to get the requested amount of resources: %d. Breaking...\n",
268 				  bp->acquire_resp.hdr.status);
269 			rc = -EAGAIN;
270 			goto out;
271 		}
272 	}
273 
274 	/* get HW info */
275 	bp->common.chip_id |= (bp->acquire_resp.pfdev_info.chip_num & 0xffff);
276 	bp->link_params.chip_id = bp->common.chip_id;
277 	bp->db_size = bp->acquire_resp.pfdev_info.db_size;
278 	bp->common.int_block = INT_BLOCK_IGU;
279 	bp->common.chip_port_mode = CHIP_2_PORT_MODE;
280 	bp->igu_dsb_id = -1;
281 	bp->mf_ov = 0;
282 	bp->mf_mode = 0;
283 	bp->common.flash_size = 0;
284 	bp->flags |=
285 		NO_WOL_FLAG | NO_ISCSI_OOO_FLAG | NO_ISCSI_FLAG | NO_FCOE_FLAG;
286 	bp->igu_sb_cnt = 1;
287 	bp->igu_base_sb = bp->acquire_resp.resc.hw_sbs[0].hw_sb_id;
288 	strlcpy(bp->fw_ver, bp->acquire_resp.pfdev_info.fw_ver,
289 		sizeof(bp->fw_ver));
290 
291 	if (is_valid_ether_addr(bp->acquire_resp.resc.current_mac_addr))
292 		memcpy(bp->dev->dev_addr,
293 		       bp->acquire_resp.resc.current_mac_addr,
294 		       ETH_ALEN);
295 
296 out:
297 	bnx2x_vfpf_finalize(bp, &req->first_tlv);
298 	return rc;
299 }
300 
301 int bnx2x_vfpf_release(struct bnx2x *bp)
302 {
303 	struct vfpf_release_tlv *req = &bp->vf2pf_mbox->req.release;
304 	struct pfvf_general_resp_tlv *resp = &bp->vf2pf_mbox->resp.general_resp;
305 	u32 rc, vf_id;
306 
307 	/* clear mailbox and prep first tlv */
308 	bnx2x_vfpf_prep(bp, &req->first_tlv, CHANNEL_TLV_RELEASE, sizeof(*req));
309 
310 	if (bnx2x_get_vf_id(bp, &vf_id)) {
311 		rc = -EAGAIN;
312 		goto out;
313 	}
314 
315 	req->vf_id = vf_id;
316 
317 	/* add list termination tlv */
318 	bnx2x_add_tlv(bp, req, req->first_tlv.tl.length, CHANNEL_TLV_LIST_END,
319 		      sizeof(struct channel_list_end_tlv));
320 
321 	/* output tlvs list */
322 	bnx2x_dp_tlv_list(bp, req);
323 
324 	/* send release request */
325 	rc = bnx2x_send_msg2pf(bp, &resp->hdr.status, bp->vf2pf_mbox_mapping);
326 
327 	if (rc)
328 		/* PF timeout */
329 		goto out;
330 
331 	if (resp->hdr.status == PFVF_STATUS_SUCCESS) {
332 		/* PF released us */
333 		DP(BNX2X_MSG_SP, "vf released\n");
334 	} else {
335 		/* PF reports error */
336 		BNX2X_ERR("PF failed our release request - are we out of sync? response status: %d\n",
337 			  resp->hdr.status);
338 		rc = -EAGAIN;
339 		goto out;
340 	}
341 out:
342 	bnx2x_vfpf_finalize(bp, &req->first_tlv);
343 
344 	return rc;
345 }
346 
347 /* Tell PF about SB addresses */
348 int bnx2x_vfpf_init(struct bnx2x *bp)
349 {
350 	struct vfpf_init_tlv *req = &bp->vf2pf_mbox->req.init;
351 	struct pfvf_general_resp_tlv *resp = &bp->vf2pf_mbox->resp.general_resp;
352 	int rc, i;
353 
354 	/* clear mailbox and prep first tlv */
355 	bnx2x_vfpf_prep(bp, &req->first_tlv, CHANNEL_TLV_INIT, sizeof(*req));
356 
357 	/* status blocks */
358 	for_each_eth_queue(bp, i)
359 		req->sb_addr[i] = (dma_addr_t)bnx2x_fp(bp, i,
360 						       status_blk_mapping);
361 
362 	/* statistics - requests only supports single queue for now */
363 	req->stats_addr = bp->fw_stats_data_mapping +
364 			  offsetof(struct bnx2x_fw_stats_data, queue_stats);
365 
366 	/* add list termination tlv */
367 	bnx2x_add_tlv(bp, req, req->first_tlv.tl.length, CHANNEL_TLV_LIST_END,
368 		      sizeof(struct channel_list_end_tlv));
369 
370 	/* output tlvs list */
371 	bnx2x_dp_tlv_list(bp, req);
372 
373 	rc = bnx2x_send_msg2pf(bp, &resp->hdr.status, bp->vf2pf_mbox_mapping);
374 	if (rc)
375 		goto out;
376 
377 	if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
378 		BNX2X_ERR("INIT VF failed: %d. Breaking...\n",
379 			  resp->hdr.status);
380 		rc = -EAGAIN;
381 		goto out;
382 	}
383 
384 	DP(BNX2X_MSG_SP, "INIT VF Succeeded\n");
385 out:
386 	bnx2x_vfpf_finalize(bp, &req->first_tlv);
387 
388 	return rc;
389 }
390 
391 /* CLOSE VF - opposite to INIT_VF */
392 void bnx2x_vfpf_close_vf(struct bnx2x *bp)
393 {
394 	struct vfpf_close_tlv *req = &bp->vf2pf_mbox->req.close;
395 	struct pfvf_general_resp_tlv *resp = &bp->vf2pf_mbox->resp.general_resp;
396 	int i, rc;
397 	u32 vf_id;
398 
399 	/* If we haven't got a valid VF id, there is no sense to
400 	 * continue with sending messages
401 	 */
402 	if (bnx2x_get_vf_id(bp, &vf_id))
403 		goto free_irq;
404 
405 	/* Close the queues */
406 	for_each_queue(bp, i)
407 		bnx2x_vfpf_teardown_queue(bp, i);
408 
409 	/* remove mac */
410 	bnx2x_vfpf_config_mac(bp, bp->dev->dev_addr, bp->fp->index, false);
411 
412 	/* clear mailbox and prep first tlv */
413 	bnx2x_vfpf_prep(bp, &req->first_tlv, CHANNEL_TLV_CLOSE, sizeof(*req));
414 
415 	req->vf_id = vf_id;
416 
417 	/* add list termination tlv */
418 	bnx2x_add_tlv(bp, req, req->first_tlv.tl.length, CHANNEL_TLV_LIST_END,
419 		      sizeof(struct channel_list_end_tlv));
420 
421 	/* output tlvs list */
422 	bnx2x_dp_tlv_list(bp, req);
423 
424 	rc = bnx2x_send_msg2pf(bp, &resp->hdr.status, bp->vf2pf_mbox_mapping);
425 
426 	if (rc)
427 		BNX2X_ERR("Sending CLOSE failed. rc was: %d\n", rc);
428 
429 	else if (resp->hdr.status != PFVF_STATUS_SUCCESS)
430 		BNX2X_ERR("Sending CLOSE failed: pf response was %d\n",
431 			  resp->hdr.status);
432 
433 	bnx2x_vfpf_finalize(bp, &req->first_tlv);
434 
435 free_irq:
436 	/* Disable HW interrupts, NAPI */
437 	bnx2x_netif_stop(bp, 0);
438 	/* Delete all NAPI objects */
439 	bnx2x_del_all_napi(bp);
440 
441 	/* Release IRQs */
442 	bnx2x_free_irq(bp);
443 }
444 
445 /* ask the pf to open a queue for the vf */
446 int bnx2x_vfpf_setup_q(struct bnx2x *bp, int fp_idx)
447 {
448 	struct vfpf_setup_q_tlv *req = &bp->vf2pf_mbox->req.setup_q;
449 	struct pfvf_general_resp_tlv *resp = &bp->vf2pf_mbox->resp.general_resp;
450 	struct bnx2x_fastpath *fp = &bp->fp[fp_idx];
451 	u16 tpa_agg_size = 0, flags = 0;
452 	int rc;
453 
454 	/* clear mailbox and prep first tlv */
455 	bnx2x_vfpf_prep(bp, &req->first_tlv, CHANNEL_TLV_SETUP_Q, sizeof(*req));
456 
457 	/* select tpa mode to request */
458 	if (!fp->disable_tpa) {
459 		flags |= VFPF_QUEUE_FLG_TPA;
460 		flags |= VFPF_QUEUE_FLG_TPA_IPV6;
461 		if (fp->mode == TPA_MODE_GRO)
462 			flags |= VFPF_QUEUE_FLG_TPA_GRO;
463 		tpa_agg_size = TPA_AGG_SIZE;
464 	}
465 
466 	/* calculate queue flags */
467 	flags |= VFPF_QUEUE_FLG_STATS;
468 	flags |= VFPF_QUEUE_FLG_CACHE_ALIGN;
469 	flags |= VFPF_QUEUE_FLG_VLAN;
470 	DP(NETIF_MSG_IFUP, "vlan removal enabled\n");
471 
472 	/* Common */
473 	req->vf_qid = fp_idx;
474 	req->param_valid = VFPF_RXQ_VALID | VFPF_TXQ_VALID;
475 
476 	/* Rx */
477 	req->rxq.rcq_addr = fp->rx_comp_mapping;
478 	req->rxq.rcq_np_addr = fp->rx_comp_mapping + BCM_PAGE_SIZE;
479 	req->rxq.rxq_addr = fp->rx_desc_mapping;
480 	req->rxq.sge_addr = fp->rx_sge_mapping;
481 	req->rxq.vf_sb = fp_idx;
482 	req->rxq.sb_index = HC_INDEX_ETH_RX_CQ_CONS;
483 	req->rxq.hc_rate = bp->rx_ticks ? 1000000/bp->rx_ticks : 0;
484 	req->rxq.mtu = bp->dev->mtu;
485 	req->rxq.buf_sz = fp->rx_buf_size;
486 	req->rxq.sge_buf_sz = BCM_PAGE_SIZE * PAGES_PER_SGE;
487 	req->rxq.tpa_agg_sz = tpa_agg_size;
488 	req->rxq.max_sge_pkt = SGE_PAGE_ALIGN(bp->dev->mtu) >> SGE_PAGE_SHIFT;
489 	req->rxq.max_sge_pkt = ((req->rxq.max_sge_pkt + PAGES_PER_SGE - 1) &
490 			  (~(PAGES_PER_SGE-1))) >> PAGES_PER_SGE_SHIFT;
491 	req->rxq.flags = flags;
492 	req->rxq.drop_flags = 0;
493 	req->rxq.cache_line_log = BNX2X_RX_ALIGN_SHIFT;
494 	req->rxq.stat_id = -1; /* No stats at the moment */
495 
496 	/* Tx */
497 	req->txq.txq_addr = fp->txdata_ptr[FIRST_TX_COS_INDEX]->tx_desc_mapping;
498 	req->txq.vf_sb = fp_idx;
499 	req->txq.sb_index = HC_INDEX_ETH_TX_CQ_CONS_COS0;
500 	req->txq.hc_rate = bp->tx_ticks ? 1000000/bp->tx_ticks : 0;
501 	req->txq.flags = flags;
502 	req->txq.traffic_type = LLFC_TRAFFIC_TYPE_NW;
503 
504 	/* add list termination tlv */
505 	bnx2x_add_tlv(bp, req, req->first_tlv.tl.length, CHANNEL_TLV_LIST_END,
506 		      sizeof(struct channel_list_end_tlv));
507 
508 	/* output tlvs list */
509 	bnx2x_dp_tlv_list(bp, req);
510 
511 	rc = bnx2x_send_msg2pf(bp, &resp->hdr.status, bp->vf2pf_mbox_mapping);
512 	if (rc)
513 		BNX2X_ERR("Sending SETUP_Q message for queue[%d] failed!\n",
514 			  fp_idx);
515 
516 	if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
517 		BNX2X_ERR("Status of SETUP_Q for queue[%d] is %d\n",
518 			  fp_idx, resp->hdr.status);
519 		rc = -EINVAL;
520 	}
521 
522 	bnx2x_vfpf_finalize(bp, &req->first_tlv);
523 
524 	return rc;
525 }
526 
527 int bnx2x_vfpf_teardown_queue(struct bnx2x *bp, int qidx)
528 {
529 	struct vfpf_q_op_tlv *req = &bp->vf2pf_mbox->req.q_op;
530 	struct pfvf_general_resp_tlv *resp = &bp->vf2pf_mbox->resp.general_resp;
531 	int rc;
532 
533 	/* clear mailbox and prep first tlv */
534 	bnx2x_vfpf_prep(bp, &req->first_tlv, CHANNEL_TLV_TEARDOWN_Q,
535 			sizeof(*req));
536 
537 	req->vf_qid = qidx;
538 
539 	/* add list termination tlv */
540 	bnx2x_add_tlv(bp, req, req->first_tlv.tl.length, CHANNEL_TLV_LIST_END,
541 		      sizeof(struct channel_list_end_tlv));
542 
543 	/* output tlvs list */
544 	bnx2x_dp_tlv_list(bp, req);
545 
546 	rc = bnx2x_send_msg2pf(bp, &resp->hdr.status, bp->vf2pf_mbox_mapping);
547 
548 	if (rc) {
549 		BNX2X_ERR("Sending TEARDOWN for queue %d failed: %d\n", qidx,
550 			  rc);
551 		goto out;
552 	}
553 
554 	/* PF failed the transaction */
555 	if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
556 		BNX2X_ERR("TEARDOWN for queue %d failed: %d\n", qidx,
557 			  resp->hdr.status);
558 		rc = -EINVAL;
559 	}
560 
561 out:
562 	bnx2x_vfpf_finalize(bp, &req->first_tlv);
563 	return rc;
564 }
565 
566 /* request pf to add a mac for the vf */
567 int bnx2x_vfpf_config_mac(struct bnx2x *bp, u8 *addr, u8 vf_qid, bool set)
568 {
569 	struct vfpf_set_q_filters_tlv *req = &bp->vf2pf_mbox->req.set_q_filters;
570 	struct pfvf_general_resp_tlv *resp = &bp->vf2pf_mbox->resp.general_resp;
571 	struct pf_vf_bulletin_content bulletin = bp->pf2vf_bulletin->content;
572 	int rc = 0;
573 
574 	/* clear mailbox and prep first tlv */
575 	bnx2x_vfpf_prep(bp, &req->first_tlv, CHANNEL_TLV_SET_Q_FILTERS,
576 			sizeof(*req));
577 
578 	req->flags = VFPF_SET_Q_FILTERS_MAC_VLAN_CHANGED;
579 	req->vf_qid = vf_qid;
580 	req->n_mac_vlan_filters = 1;
581 
582 	req->filters[0].flags = VFPF_Q_FILTER_DEST_MAC_VALID;
583 	if (set)
584 		req->filters[0].flags |= VFPF_Q_FILTER_SET_MAC;
585 
586 	/* sample bulletin board for new mac */
587 	bnx2x_sample_bulletin(bp);
588 
589 	/* copy mac from device to request */
590 	memcpy(req->filters[0].mac, addr, ETH_ALEN);
591 
592 	/* add list termination tlv */
593 	bnx2x_add_tlv(bp, req, req->first_tlv.tl.length, CHANNEL_TLV_LIST_END,
594 		      sizeof(struct channel_list_end_tlv));
595 
596 	/* output tlvs list */
597 	bnx2x_dp_tlv_list(bp, req);
598 
599 	/* send message to pf */
600 	rc = bnx2x_send_msg2pf(bp, &resp->hdr.status, bp->vf2pf_mbox_mapping);
601 	if (rc) {
602 		BNX2X_ERR("failed to send message to pf. rc was %d\n", rc);
603 		goto out;
604 	}
605 
606 	/* failure may mean PF was configured with a new mac for us */
607 	while (resp->hdr.status == PFVF_STATUS_FAILURE) {
608 		DP(BNX2X_MSG_IOV,
609 		   "vfpf SET MAC failed. Check bulletin board for new posts\n");
610 
611 		/* copy mac from bulletin to device */
612 		memcpy(bp->dev->dev_addr, bulletin.mac, ETH_ALEN);
613 
614 		/* check if bulletin board was updated */
615 		if (bnx2x_sample_bulletin(bp) == PFVF_BULLETIN_UPDATED) {
616 			/* copy mac from device to request */
617 			memcpy(req->filters[0].mac, bp->dev->dev_addr,
618 			       ETH_ALEN);
619 
620 			/* send message to pf */
621 			rc = bnx2x_send_msg2pf(bp, &resp->hdr.status,
622 					       bp->vf2pf_mbox_mapping);
623 		} else {
624 			/* no new info in bulletin */
625 			break;
626 		}
627 	}
628 
629 	if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
630 		BNX2X_ERR("vfpf SET MAC failed: %d\n", resp->hdr.status);
631 		rc = -EINVAL;
632 	}
633 out:
634 	bnx2x_vfpf_finalize(bp, &req->first_tlv);
635 
636 	return 0;
637 }
638 
639 int bnx2x_vfpf_set_mcast(struct net_device *dev)
640 {
641 	struct bnx2x *bp = netdev_priv(dev);
642 	struct vfpf_set_q_filters_tlv *req = &bp->vf2pf_mbox->req.set_q_filters;
643 	struct pfvf_general_resp_tlv *resp = &bp->vf2pf_mbox->resp.general_resp;
644 	int rc, i = 0;
645 	struct netdev_hw_addr *ha;
646 
647 	if (bp->state != BNX2X_STATE_OPEN) {
648 		DP(NETIF_MSG_IFUP, "state is %x, returning\n", bp->state);
649 		return -EINVAL;
650 	}
651 
652 	/* clear mailbox and prep first tlv */
653 	bnx2x_vfpf_prep(bp, &req->first_tlv, CHANNEL_TLV_SET_Q_FILTERS,
654 			sizeof(*req));
655 
656 	/* Get Rx mode requested */
657 	DP(NETIF_MSG_IFUP, "dev->flags = %x\n", dev->flags);
658 
659 	netdev_for_each_mc_addr(ha, dev) {
660 		DP(NETIF_MSG_IFUP, "Adding mcast MAC: %pM\n",
661 		   bnx2x_mc_addr(ha));
662 		memcpy(req->multicast[i], bnx2x_mc_addr(ha), ETH_ALEN);
663 		i++;
664 	}
665 
666 	/* We support four PFVF_MAX_MULTICAST_PER_VF mcast
667 	  * addresses tops
668 	  */
669 	if (i >= PFVF_MAX_MULTICAST_PER_VF) {
670 		DP(NETIF_MSG_IFUP,
671 		   "VF supports not more than %d multicast MAC addresses\n",
672 		   PFVF_MAX_MULTICAST_PER_VF);
673 		return -EINVAL;
674 	}
675 
676 	req->n_multicast = i;
677 	req->flags |= VFPF_SET_Q_FILTERS_MULTICAST_CHANGED;
678 	req->vf_qid = 0;
679 
680 	/* add list termination tlv */
681 	bnx2x_add_tlv(bp, req, req->first_tlv.tl.length, CHANNEL_TLV_LIST_END,
682 		      sizeof(struct channel_list_end_tlv));
683 
684 	/* output tlvs list */
685 	bnx2x_dp_tlv_list(bp, req);
686 	rc = bnx2x_send_msg2pf(bp, &resp->hdr.status, bp->vf2pf_mbox_mapping);
687 	if (rc) {
688 		BNX2X_ERR("Sending a message failed: %d\n", rc);
689 		goto out;
690 	}
691 
692 	if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
693 		BNX2X_ERR("Set Rx mode/multicast failed: %d\n",
694 			  resp->hdr.status);
695 		rc = -EINVAL;
696 	}
697 out:
698 	bnx2x_vfpf_finalize(bp, &req->first_tlv);
699 
700 	return 0;
701 }
702 
703 int bnx2x_vfpf_storm_rx_mode(struct bnx2x *bp)
704 {
705 	int mode = bp->rx_mode;
706 	struct vfpf_set_q_filters_tlv *req = &bp->vf2pf_mbox->req.set_q_filters;
707 	struct pfvf_general_resp_tlv *resp = &bp->vf2pf_mbox->resp.general_resp;
708 	int rc;
709 
710 	/* clear mailbox and prep first tlv */
711 	bnx2x_vfpf_prep(bp, &req->first_tlv, CHANNEL_TLV_SET_Q_FILTERS,
712 			sizeof(*req));
713 
714 	DP(NETIF_MSG_IFUP, "Rx mode is %d\n", mode);
715 
716 	switch (mode) {
717 	case BNX2X_RX_MODE_NONE: /* no Rx */
718 		req->rx_mask = VFPF_RX_MASK_ACCEPT_NONE;
719 		break;
720 	case BNX2X_RX_MODE_NORMAL:
721 		req->rx_mask = VFPF_RX_MASK_ACCEPT_MATCHED_MULTICAST;
722 		req->rx_mask |= VFPF_RX_MASK_ACCEPT_MATCHED_UNICAST;
723 		req->rx_mask |= VFPF_RX_MASK_ACCEPT_BROADCAST;
724 		break;
725 	case BNX2X_RX_MODE_ALLMULTI:
726 		req->rx_mask = VFPF_RX_MASK_ACCEPT_ALL_MULTICAST;
727 		req->rx_mask |= VFPF_RX_MASK_ACCEPT_MATCHED_UNICAST;
728 		req->rx_mask |= VFPF_RX_MASK_ACCEPT_BROADCAST;
729 		break;
730 	case BNX2X_RX_MODE_PROMISC:
731 		req->rx_mask = VFPF_RX_MASK_ACCEPT_ALL_UNICAST;
732 		req->rx_mask |= VFPF_RX_MASK_ACCEPT_ALL_MULTICAST;
733 		req->rx_mask |= VFPF_RX_MASK_ACCEPT_BROADCAST;
734 		break;
735 	default:
736 		BNX2X_ERR("BAD rx mode (%d)\n", mode);
737 		rc = -EINVAL;
738 		goto out;
739 	}
740 
741 	req->flags |= VFPF_SET_Q_FILTERS_RX_MASK_CHANGED;
742 	req->vf_qid = 0;
743 
744 	/* add list termination tlv */
745 	bnx2x_add_tlv(bp, req, req->first_tlv.tl.length, CHANNEL_TLV_LIST_END,
746 		      sizeof(struct channel_list_end_tlv));
747 
748 	/* output tlvs list */
749 	bnx2x_dp_tlv_list(bp, req);
750 
751 	rc = bnx2x_send_msg2pf(bp, &resp->hdr.status, bp->vf2pf_mbox_mapping);
752 	if (rc)
753 		BNX2X_ERR("Sending a message failed: %d\n", rc);
754 
755 	if (resp->hdr.status != PFVF_STATUS_SUCCESS) {
756 		BNX2X_ERR("Set Rx mode failed: %d\n", resp->hdr.status);
757 		rc = -EINVAL;
758 	}
759 out:
760 	bnx2x_vfpf_finalize(bp, &req->first_tlv);
761 
762 	return rc;
763 }
764 
765 /* General service functions */
766 static void storm_memset_vf_mbx_ack(struct bnx2x *bp, u16 abs_fid)
767 {
768 	u32 addr = BAR_CSTRORM_INTMEM +
769 		   CSTORM_VF_PF_CHANNEL_STATE_OFFSET(abs_fid);
770 
771 	REG_WR8(bp, addr, VF_PF_CHANNEL_STATE_READY);
772 }
773 
774 static void storm_memset_vf_mbx_valid(struct bnx2x *bp, u16 abs_fid)
775 {
776 	u32 addr = BAR_CSTRORM_INTMEM +
777 		   CSTORM_VF_PF_CHANNEL_VALID_OFFSET(abs_fid);
778 
779 	REG_WR8(bp, addr, 1);
780 }
781 
782 static inline void bnx2x_set_vf_mbxs_valid(struct bnx2x *bp)
783 {
784 	int i;
785 
786 	for_each_vf(bp, i)
787 		storm_memset_vf_mbx_valid(bp, bnx2x_vf(bp, i, abs_vfid));
788 }
789 
790 /* enable vf_pf mailbox (aka vf-pf-chanell) */
791 void bnx2x_vf_enable_mbx(struct bnx2x *bp, u8 abs_vfid)
792 {
793 	bnx2x_vf_flr_clnup_epilog(bp, abs_vfid);
794 
795 	/* enable the mailbox in the FW */
796 	storm_memset_vf_mbx_ack(bp, abs_vfid);
797 	storm_memset_vf_mbx_valid(bp, abs_vfid);
798 
799 	/* enable the VF access to the mailbox */
800 	bnx2x_vf_enable_access(bp, abs_vfid);
801 }
802 
803 /* this works only on !E1h */
804 static int bnx2x_copy32_vf_dmae(struct bnx2x *bp, u8 from_vf,
805 				dma_addr_t pf_addr, u8 vfid, u32 vf_addr_hi,
806 				u32 vf_addr_lo, u32 len32)
807 {
808 	struct dmae_command dmae;
809 
810 	if (CHIP_IS_E1x(bp)) {
811 		BNX2X_ERR("Chip revision does not support VFs\n");
812 		return DMAE_NOT_RDY;
813 	}
814 
815 	if (!bp->dmae_ready) {
816 		BNX2X_ERR("DMAE is not ready, can not copy\n");
817 		return DMAE_NOT_RDY;
818 	}
819 
820 	/* set opcode and fixed command fields */
821 	bnx2x_prep_dmae_with_comp(bp, &dmae, DMAE_SRC_PCI, DMAE_DST_PCI);
822 
823 	if (from_vf) {
824 		dmae.opcode_iov = (vfid << DMAE_COMMAND_SRC_VFID_SHIFT) |
825 			(DMAE_SRC_VF << DMAE_COMMAND_SRC_VFPF_SHIFT) |
826 			(DMAE_DST_PF << DMAE_COMMAND_DST_VFPF_SHIFT);
827 
828 		dmae.opcode |= (DMAE_C_DST << DMAE_COMMAND_C_FUNC_SHIFT);
829 
830 		dmae.src_addr_lo = vf_addr_lo;
831 		dmae.src_addr_hi = vf_addr_hi;
832 		dmae.dst_addr_lo = U64_LO(pf_addr);
833 		dmae.dst_addr_hi = U64_HI(pf_addr);
834 	} else {
835 		dmae.opcode_iov = (vfid << DMAE_COMMAND_DST_VFID_SHIFT) |
836 			(DMAE_DST_VF << DMAE_COMMAND_DST_VFPF_SHIFT) |
837 			(DMAE_SRC_PF << DMAE_COMMAND_SRC_VFPF_SHIFT);
838 
839 		dmae.opcode |= (DMAE_C_SRC << DMAE_COMMAND_C_FUNC_SHIFT);
840 
841 		dmae.src_addr_lo = U64_LO(pf_addr);
842 		dmae.src_addr_hi = U64_HI(pf_addr);
843 		dmae.dst_addr_lo = vf_addr_lo;
844 		dmae.dst_addr_hi = vf_addr_hi;
845 	}
846 	dmae.len = len32;
847 	bnx2x_dp_dmae(bp, &dmae, BNX2X_MSG_DMAE);
848 
849 	/* issue the command and wait for completion */
850 	return bnx2x_issue_dmae_with_comp(bp, &dmae);
851 }
852 
853 static void bnx2x_vf_mbx_resp(struct bnx2x *bp, struct bnx2x_virtf *vf)
854 {
855 	struct bnx2x_vf_mbx *mbx = BP_VF_MBX(bp, vf->index);
856 	u64 vf_addr;
857 	dma_addr_t pf_addr;
858 	u16 length, type;
859 	int rc;
860 	struct pfvf_general_resp_tlv *resp = &mbx->msg->resp.general_resp;
861 
862 	/* prepare response */
863 	type = mbx->first_tlv.tl.type;
864 	length = type == CHANNEL_TLV_ACQUIRE ?
865 		sizeof(struct pfvf_acquire_resp_tlv) :
866 		sizeof(struct pfvf_general_resp_tlv);
867 	bnx2x_add_tlv(bp, resp, 0, type, length);
868 	resp->hdr.status = bnx2x_pfvf_status_codes(vf->op_rc);
869 	bnx2x_add_tlv(bp, resp, length, CHANNEL_TLV_LIST_END,
870 		      sizeof(struct channel_list_end_tlv));
871 	bnx2x_dp_tlv_list(bp, resp);
872 	DP(BNX2X_MSG_IOV, "mailbox vf address hi 0x%x, lo 0x%x, offset 0x%x\n",
873 	   mbx->vf_addr_hi, mbx->vf_addr_lo, mbx->first_tlv.resp_msg_offset);
874 
875 	/* send response */
876 	vf_addr = HILO_U64(mbx->vf_addr_hi, mbx->vf_addr_lo) +
877 		  mbx->first_tlv.resp_msg_offset;
878 	pf_addr = mbx->msg_mapping +
879 		  offsetof(struct bnx2x_vf_mbx_msg, resp);
880 
881 	/* copy the response body, if there is one, before the header, as the vf
882 	 * is sensitive to the header being written
883 	 */
884 	if (resp->hdr.tl.length > sizeof(u64)) {
885 		length = resp->hdr.tl.length - sizeof(u64);
886 		vf_addr += sizeof(u64);
887 		pf_addr += sizeof(u64);
888 		rc = bnx2x_copy32_vf_dmae(bp, false, pf_addr, vf->abs_vfid,
889 					  U64_HI(vf_addr),
890 					  U64_LO(vf_addr),
891 					  length/4);
892 		if (rc) {
893 			BNX2X_ERR("Failed to copy response body to VF %d\n",
894 				  vf->abs_vfid);
895 			goto mbx_error;
896 		}
897 		vf_addr -= sizeof(u64);
898 		pf_addr -= sizeof(u64);
899 	}
900 
901 	/* ack the FW */
902 	storm_memset_vf_mbx_ack(bp, vf->abs_vfid);
903 	mmiowb();
904 
905 	/* initiate dmae to send the response */
906 	mbx->flags &= ~VF_MSG_INPROCESS;
907 
908 	/* copy the response header including status-done field,
909 	 * must be last dmae, must be after FW is acked
910 	 */
911 	rc = bnx2x_copy32_vf_dmae(bp, false, pf_addr, vf->abs_vfid,
912 				  U64_HI(vf_addr),
913 				  U64_LO(vf_addr),
914 				  sizeof(u64)/4);
915 
916 	/* unlock channel mutex */
917 	bnx2x_unlock_vf_pf_channel(bp, vf, mbx->first_tlv.tl.type);
918 
919 	if (rc) {
920 		BNX2X_ERR("Failed to copy response status to VF %d\n",
921 			  vf->abs_vfid);
922 		goto mbx_error;
923 	}
924 	return;
925 
926 mbx_error:
927 	bnx2x_vf_release(bp, vf, false); /* non blocking */
928 }
929 
930 static void bnx2x_vf_mbx_acquire_resp(struct bnx2x *bp, struct bnx2x_virtf *vf,
931 				      struct bnx2x_vf_mbx *mbx, int vfop_status)
932 {
933 	int i;
934 	struct pfvf_acquire_resp_tlv *resp = &mbx->msg->resp.acquire_resp;
935 	struct pf_vf_resc *resc = &resp->resc;
936 	u8 status = bnx2x_pfvf_status_codes(vfop_status);
937 
938 	memset(resp, 0, sizeof(*resp));
939 
940 	/* fill in pfdev info */
941 	resp->pfdev_info.chip_num = bp->common.chip_id;
942 	resp->pfdev_info.db_size = (1 << BNX2X_DB_SHIFT);
943 	resp->pfdev_info.indices_per_sb = HC_SB_MAX_INDICES_E2;
944 	resp->pfdev_info.pf_cap = (PFVF_CAP_RSS |
945 				   /* PFVF_CAP_DHC |*/ PFVF_CAP_TPA);
946 	bnx2x_fill_fw_str(bp, resp->pfdev_info.fw_ver,
947 			  sizeof(resp->pfdev_info.fw_ver));
948 
949 	if (status == PFVF_STATUS_NO_RESOURCE ||
950 	    status == PFVF_STATUS_SUCCESS) {
951 		/* set resources numbers, if status equals NO_RESOURCE these
952 		 * are max possible numbers
953 		 */
954 		resc->num_rxqs = vf_rxq_count(vf) ? :
955 			bnx2x_vf_max_queue_cnt(bp, vf);
956 		resc->num_txqs = vf_txq_count(vf) ? :
957 			bnx2x_vf_max_queue_cnt(bp, vf);
958 		resc->num_sbs = vf_sb_count(vf);
959 		resc->num_mac_filters = vf_mac_rules_cnt(vf);
960 		resc->num_vlan_filters = vf_vlan_rules_cnt(vf);
961 		resc->num_mc_filters = 0;
962 
963 		if (status == PFVF_STATUS_SUCCESS) {
964 			/* fill in the allocated resources */
965 			struct pf_vf_bulletin_content *bulletin =
966 				BP_VF_BULLETIN(bp, vf->index);
967 
968 			for_each_vfq(vf, i)
969 				resc->hw_qid[i] =
970 					vfq_qzone_id(vf, vfq_get(vf, i));
971 
972 			for_each_vf_sb(vf, i) {
973 				resc->hw_sbs[i].hw_sb_id = vf_igu_sb(vf, i);
974 				resc->hw_sbs[i].sb_qid = vf_hc_qzone(vf, i);
975 			}
976 
977 			/* if a mac has been set for this vf, supply it */
978 			if (bulletin->valid_bitmap & 1 << MAC_ADDR_VALID) {
979 				memcpy(resc->current_mac_addr, bulletin->mac,
980 				       ETH_ALEN);
981 			}
982 		}
983 	}
984 
985 	DP(BNX2X_MSG_IOV, "VF[%d] ACQUIRE_RESPONSE: pfdev_info- chip_num=0x%x, db_size=%d, idx_per_sb=%d, pf_cap=0x%x\n"
986 	   "resources- n_rxq-%d, n_txq-%d, n_sbs-%d, n_macs-%d, n_vlans-%d, n_mcs-%d, fw_ver: '%s'\n",
987 	   vf->abs_vfid,
988 	   resp->pfdev_info.chip_num,
989 	   resp->pfdev_info.db_size,
990 	   resp->pfdev_info.indices_per_sb,
991 	   resp->pfdev_info.pf_cap,
992 	   resc->num_rxqs,
993 	   resc->num_txqs,
994 	   resc->num_sbs,
995 	   resc->num_mac_filters,
996 	   resc->num_vlan_filters,
997 	   resc->num_mc_filters,
998 	   resp->pfdev_info.fw_ver);
999 
1000 	DP_CONT(BNX2X_MSG_IOV, "hw_qids- [ ");
1001 	for (i = 0; i < vf_rxq_count(vf); i++)
1002 		DP_CONT(BNX2X_MSG_IOV, "%d ", resc->hw_qid[i]);
1003 	DP_CONT(BNX2X_MSG_IOV, "], sb_info- [ ");
1004 	for (i = 0; i < vf_sb_count(vf); i++)
1005 		DP_CONT(BNX2X_MSG_IOV, "%d:%d ",
1006 			resc->hw_sbs[i].hw_sb_id,
1007 			resc->hw_sbs[i].sb_qid);
1008 	DP_CONT(BNX2X_MSG_IOV, "]\n");
1009 
1010 	/* send the response */
1011 	vf->op_rc = vfop_status;
1012 	bnx2x_vf_mbx_resp(bp, vf);
1013 }
1014 
1015 static void bnx2x_vf_mbx_acquire(struct bnx2x *bp, struct bnx2x_virtf *vf,
1016 				 struct bnx2x_vf_mbx *mbx)
1017 {
1018 	int rc;
1019 	struct vfpf_acquire_tlv *acquire = &mbx->msg->req.acquire;
1020 
1021 	/* log vfdef info */
1022 	DP(BNX2X_MSG_IOV,
1023 	   "VF[%d] ACQUIRE: vfdev_info- vf_id %d, vf_os %d resources- n_rxq-%d, n_txq-%d, n_sbs-%d, n_macs-%d, n_vlans-%d, n_mcs-%d\n",
1024 	   vf->abs_vfid, acquire->vfdev_info.vf_id, acquire->vfdev_info.vf_os,
1025 	   acquire->resc_request.num_rxqs, acquire->resc_request.num_txqs,
1026 	   acquire->resc_request.num_sbs, acquire->resc_request.num_mac_filters,
1027 	   acquire->resc_request.num_vlan_filters,
1028 	   acquire->resc_request.num_mc_filters);
1029 
1030 	/* acquire the resources */
1031 	rc = bnx2x_vf_acquire(bp, vf, &acquire->resc_request);
1032 
1033 	/* store address of vf's bulletin board */
1034 	vf->bulletin_map = acquire->bulletin_addr;
1035 
1036 	/* response */
1037 	bnx2x_vf_mbx_acquire_resp(bp, vf, mbx, rc);
1038 }
1039 
1040 static void bnx2x_vf_mbx_init_vf(struct bnx2x *bp, struct bnx2x_virtf *vf,
1041 			      struct bnx2x_vf_mbx *mbx)
1042 {
1043 	struct vfpf_init_tlv *init = &mbx->msg->req.init;
1044 
1045 	/* record ghost addresses from vf message */
1046 	vf->spq_map = init->spq_addr;
1047 	vf->fw_stat_map = init->stats_addr;
1048 	vf->op_rc = bnx2x_vf_init(bp, vf, (dma_addr_t *)init->sb_addr);
1049 
1050 	/* response */
1051 	bnx2x_vf_mbx_resp(bp, vf);
1052 }
1053 
1054 /* convert MBX queue-flags to standard SP queue-flags */
1055 static void bnx2x_vf_mbx_set_q_flags(struct bnx2x *bp, u32 mbx_q_flags,
1056 				     unsigned long *sp_q_flags)
1057 {
1058 	if (mbx_q_flags & VFPF_QUEUE_FLG_TPA)
1059 		__set_bit(BNX2X_Q_FLG_TPA, sp_q_flags);
1060 	if (mbx_q_flags & VFPF_QUEUE_FLG_TPA_IPV6)
1061 		__set_bit(BNX2X_Q_FLG_TPA_IPV6, sp_q_flags);
1062 	if (mbx_q_flags & VFPF_QUEUE_FLG_TPA_GRO)
1063 		__set_bit(BNX2X_Q_FLG_TPA_GRO, sp_q_flags);
1064 	if (mbx_q_flags & VFPF_QUEUE_FLG_STATS)
1065 		__set_bit(BNX2X_Q_FLG_STATS, sp_q_flags);
1066 	if (mbx_q_flags & VFPF_QUEUE_FLG_VLAN)
1067 		__set_bit(BNX2X_Q_FLG_VLAN, sp_q_flags);
1068 	if (mbx_q_flags & VFPF_QUEUE_FLG_COS)
1069 		__set_bit(BNX2X_Q_FLG_COS, sp_q_flags);
1070 	if (mbx_q_flags & VFPF_QUEUE_FLG_HC)
1071 		__set_bit(BNX2X_Q_FLG_HC, sp_q_flags);
1072 	if (mbx_q_flags & VFPF_QUEUE_FLG_DHC)
1073 		__set_bit(BNX2X_Q_FLG_DHC, sp_q_flags);
1074 
1075 	/* outer vlan removal is set according to the PF's multi fuction mode */
1076 	if (IS_MF_SD(bp))
1077 		__set_bit(BNX2X_Q_FLG_OV, sp_q_flags);
1078 }
1079 
1080 static void bnx2x_vf_mbx_setup_q(struct bnx2x *bp, struct bnx2x_virtf *vf,
1081 				 struct bnx2x_vf_mbx *mbx)
1082 {
1083 	struct vfpf_setup_q_tlv *setup_q = &mbx->msg->req.setup_q;
1084 	struct bnx2x_vfop_cmd cmd = {
1085 		.done = bnx2x_vf_mbx_resp,
1086 		.block = false,
1087 	};
1088 
1089 	/* verify vf_qid */
1090 	if (setup_q->vf_qid >= vf_rxq_count(vf)) {
1091 		BNX2X_ERR("vf_qid %d invalid, max queue count is %d\n",
1092 			  setup_q->vf_qid, vf_rxq_count(vf));
1093 		vf->op_rc = -EINVAL;
1094 		goto response;
1095 	}
1096 
1097 	/* tx queues must be setup alongside rx queues thus if the rx queue
1098 	 * is not marked as valid there's nothing to do.
1099 	 */
1100 	if (setup_q->param_valid & (VFPF_RXQ_VALID|VFPF_TXQ_VALID)) {
1101 		struct bnx2x_vf_queue *q = vfq_get(vf, setup_q->vf_qid);
1102 		unsigned long q_type = 0;
1103 
1104 		struct bnx2x_queue_init_params *init_p;
1105 		struct bnx2x_queue_setup_params *setup_p;
1106 
1107 		/* reinit the VF operation context */
1108 		memset(&vf->op_params.qctor, 0 , sizeof(vf->op_params.qctor));
1109 		setup_p = &vf->op_params.qctor.prep_qsetup;
1110 		init_p =  &vf->op_params.qctor.qstate.params.init;
1111 
1112 		/* activate immediately */
1113 		__set_bit(BNX2X_Q_FLG_ACTIVE, &setup_p->flags);
1114 
1115 		if (setup_q->param_valid & VFPF_TXQ_VALID) {
1116 			struct bnx2x_txq_setup_params *txq_params =
1117 				&setup_p->txq_params;
1118 
1119 			__set_bit(BNX2X_Q_TYPE_HAS_TX, &q_type);
1120 
1121 			/* save sb resource index */
1122 			q->sb_idx = setup_q->txq.vf_sb;
1123 
1124 			/* tx init */
1125 			init_p->tx.hc_rate = setup_q->txq.hc_rate;
1126 			init_p->tx.sb_cq_index = setup_q->txq.sb_index;
1127 
1128 			bnx2x_vf_mbx_set_q_flags(bp, setup_q->txq.flags,
1129 						 &init_p->tx.flags);
1130 
1131 			/* tx setup - flags */
1132 			bnx2x_vf_mbx_set_q_flags(bp, setup_q->txq.flags,
1133 						 &setup_p->flags);
1134 
1135 			/* tx setup - general, nothing */
1136 
1137 			/* tx setup - tx */
1138 			txq_params->dscr_map = setup_q->txq.txq_addr;
1139 			txq_params->sb_cq_index = setup_q->txq.sb_index;
1140 			txq_params->traffic_type = setup_q->txq.traffic_type;
1141 
1142 			bnx2x_vfop_qctor_dump_tx(bp, vf, init_p, setup_p,
1143 						 q->index, q->sb_idx);
1144 		}
1145 
1146 		if (setup_q->param_valid & VFPF_RXQ_VALID) {
1147 			struct bnx2x_rxq_setup_params *rxq_params =
1148 							&setup_p->rxq_params;
1149 
1150 			__set_bit(BNX2X_Q_TYPE_HAS_RX, &q_type);
1151 
1152 			/* Note: there is no support for different SBs
1153 			 * for TX and RX
1154 			 */
1155 			q->sb_idx = setup_q->rxq.vf_sb;
1156 
1157 			/* rx init */
1158 			init_p->rx.hc_rate = setup_q->rxq.hc_rate;
1159 			init_p->rx.sb_cq_index = setup_q->rxq.sb_index;
1160 			bnx2x_vf_mbx_set_q_flags(bp, setup_q->rxq.flags,
1161 						 &init_p->rx.flags);
1162 
1163 			/* rx setup - flags */
1164 			bnx2x_vf_mbx_set_q_flags(bp, setup_q->rxq.flags,
1165 						 &setup_p->flags);
1166 
1167 			/* rx setup - general */
1168 			setup_p->gen_params.mtu = setup_q->rxq.mtu;
1169 
1170 			/* rx setup - rx */
1171 			rxq_params->drop_flags = setup_q->rxq.drop_flags;
1172 			rxq_params->dscr_map = setup_q->rxq.rxq_addr;
1173 			rxq_params->sge_map = setup_q->rxq.sge_addr;
1174 			rxq_params->rcq_map = setup_q->rxq.rcq_addr;
1175 			rxq_params->rcq_np_map = setup_q->rxq.rcq_np_addr;
1176 			rxq_params->buf_sz = setup_q->rxq.buf_sz;
1177 			rxq_params->tpa_agg_sz = setup_q->rxq.tpa_agg_sz;
1178 			rxq_params->max_sges_pkt = setup_q->rxq.max_sge_pkt;
1179 			rxq_params->sge_buf_sz = setup_q->rxq.sge_buf_sz;
1180 			rxq_params->cache_line_log =
1181 				setup_q->rxq.cache_line_log;
1182 			rxq_params->sb_cq_index = setup_q->rxq.sb_index;
1183 
1184 			bnx2x_vfop_qctor_dump_rx(bp, vf, init_p, setup_p,
1185 						 q->index, q->sb_idx);
1186 		}
1187 		/* complete the preparations */
1188 		bnx2x_vfop_qctor_prep(bp, vf, q, &vf->op_params.qctor, q_type);
1189 
1190 		vf->op_rc = bnx2x_vfop_qsetup_cmd(bp, vf, &cmd, q->index);
1191 		if (vf->op_rc)
1192 			goto response;
1193 		return;
1194 	}
1195 response:
1196 	bnx2x_vf_mbx_resp(bp, vf);
1197 }
1198 
1199 enum bnx2x_vfop_filters_state {
1200 	   BNX2X_VFOP_MBX_Q_FILTERS_MACS,
1201 	   BNX2X_VFOP_MBX_Q_FILTERS_VLANS,
1202 	   BNX2X_VFOP_MBX_Q_FILTERS_RXMODE,
1203 	   BNX2X_VFOP_MBX_Q_FILTERS_MCAST,
1204 	   BNX2X_VFOP_MBX_Q_FILTERS_DONE
1205 };
1206 
1207 static int bnx2x_vf_mbx_macvlan_list(struct bnx2x *bp,
1208 				     struct bnx2x_virtf *vf,
1209 				     struct vfpf_set_q_filters_tlv *tlv,
1210 				     struct bnx2x_vfop_filters **pfl,
1211 				     u32 type_flag)
1212 {
1213 	int i, j;
1214 	struct bnx2x_vfop_filters *fl = NULL;
1215 	size_t fsz;
1216 
1217 	fsz = tlv->n_mac_vlan_filters * sizeof(struct bnx2x_vfop_filter) +
1218 		sizeof(struct bnx2x_vfop_filters);
1219 
1220 	fl = kzalloc(fsz, GFP_KERNEL);
1221 	if (!fl)
1222 		return -ENOMEM;
1223 
1224 	INIT_LIST_HEAD(&fl->head);
1225 
1226 	for (i = 0, j = 0; i < tlv->n_mac_vlan_filters; i++) {
1227 		struct vfpf_q_mac_vlan_filter *msg_filter = &tlv->filters[i];
1228 
1229 		if ((msg_filter->flags & type_flag) != type_flag)
1230 			continue;
1231 		if (type_flag == VFPF_Q_FILTER_DEST_MAC_VALID) {
1232 			fl->filters[j].mac = msg_filter->mac;
1233 			fl->filters[j].type = BNX2X_VFOP_FILTER_MAC;
1234 		} else {
1235 			fl->filters[j].vid = msg_filter->vlan_tag;
1236 			fl->filters[j].type = BNX2X_VFOP_FILTER_VLAN;
1237 		}
1238 		fl->filters[j].add =
1239 			(msg_filter->flags & VFPF_Q_FILTER_SET_MAC) ?
1240 			true : false;
1241 		list_add_tail(&fl->filters[j++].link, &fl->head);
1242 	}
1243 	if (list_empty(&fl->head))
1244 		kfree(fl);
1245 	else
1246 		*pfl = fl;
1247 
1248 	return 0;
1249 }
1250 
1251 static void bnx2x_vf_mbx_dp_q_filter(struct bnx2x *bp, int msglvl, int idx,
1252 				       struct vfpf_q_mac_vlan_filter *filter)
1253 {
1254 	DP(msglvl, "MAC-VLAN[%d] -- flags=0x%x\n", idx, filter->flags);
1255 	if (filter->flags & VFPF_Q_FILTER_VLAN_TAG_VALID)
1256 		DP_CONT(msglvl, ", vlan=%d", filter->vlan_tag);
1257 	if (filter->flags & VFPF_Q_FILTER_DEST_MAC_VALID)
1258 		DP_CONT(msglvl, ", MAC=%pM", filter->mac);
1259 	DP_CONT(msglvl, "\n");
1260 }
1261 
1262 static void bnx2x_vf_mbx_dp_q_filters(struct bnx2x *bp, int msglvl,
1263 				       struct vfpf_set_q_filters_tlv *filters)
1264 {
1265 	int i;
1266 
1267 	if (filters->flags & VFPF_SET_Q_FILTERS_MAC_VLAN_CHANGED)
1268 		for (i = 0; i < filters->n_mac_vlan_filters; i++)
1269 			bnx2x_vf_mbx_dp_q_filter(bp, msglvl, i,
1270 						 &filters->filters[i]);
1271 
1272 	if (filters->flags & VFPF_SET_Q_FILTERS_RX_MASK_CHANGED)
1273 		DP(msglvl, "RX-MASK=0x%x\n", filters->rx_mask);
1274 
1275 	if (filters->flags & VFPF_SET_Q_FILTERS_MULTICAST_CHANGED)
1276 		for (i = 0; i < filters->n_multicast; i++)
1277 			DP(msglvl, "MULTICAST=%pM\n", filters->multicast[i]);
1278 }
1279 
1280 #define VFPF_MAC_FILTER		VFPF_Q_FILTER_DEST_MAC_VALID
1281 #define VFPF_VLAN_FILTER	VFPF_Q_FILTER_VLAN_TAG_VALID
1282 
1283 static void bnx2x_vfop_mbx_qfilters(struct bnx2x *bp, struct bnx2x_virtf *vf)
1284 {
1285 	int rc;
1286 
1287 	struct vfpf_set_q_filters_tlv *msg =
1288 		&BP_VF_MBX(bp, vf->index)->msg->req.set_q_filters;
1289 
1290 	struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
1291 	enum bnx2x_vfop_filters_state state = vfop->state;
1292 
1293 	struct bnx2x_vfop_cmd cmd = {
1294 		.done = bnx2x_vfop_mbx_qfilters,
1295 		.block = false,
1296 	};
1297 
1298 	DP(BNX2X_MSG_IOV, "STATE: %d\n", state);
1299 
1300 	if (vfop->rc < 0)
1301 		goto op_err;
1302 
1303 	switch (state) {
1304 	case BNX2X_VFOP_MBX_Q_FILTERS_MACS:
1305 		/* next state */
1306 		vfop->state = BNX2X_VFOP_MBX_Q_FILTERS_VLANS;
1307 
1308 		/* check for any vlan/mac changes */
1309 		if (msg->flags & VFPF_SET_Q_FILTERS_MAC_VLAN_CHANGED) {
1310 			/* build mac list */
1311 			struct bnx2x_vfop_filters *fl = NULL;
1312 
1313 			vfop->rc = bnx2x_vf_mbx_macvlan_list(bp, vf, msg, &fl,
1314 							     VFPF_MAC_FILTER);
1315 			if (vfop->rc)
1316 				goto op_err;
1317 
1318 			if (fl) {
1319 				/* set mac list */
1320 				rc = bnx2x_vfop_mac_list_cmd(bp, vf, &cmd, fl,
1321 							     msg->vf_qid,
1322 							     false);
1323 				if (rc) {
1324 					vfop->rc = rc;
1325 					goto op_err;
1326 				}
1327 				return;
1328 			}
1329 		}
1330 		/* fall through */
1331 
1332 	case BNX2X_VFOP_MBX_Q_FILTERS_VLANS:
1333 		/* next state */
1334 		vfop->state = BNX2X_VFOP_MBX_Q_FILTERS_RXMODE;
1335 
1336 		/* check for any vlan/mac changes */
1337 		if (msg->flags & VFPF_SET_Q_FILTERS_MAC_VLAN_CHANGED) {
1338 			/* build vlan list */
1339 			struct bnx2x_vfop_filters *fl = NULL;
1340 
1341 			vfop->rc = bnx2x_vf_mbx_macvlan_list(bp, vf, msg, &fl,
1342 							     VFPF_VLAN_FILTER);
1343 			if (vfop->rc)
1344 				goto op_err;
1345 
1346 			if (fl) {
1347 				/* set vlan list */
1348 				rc = bnx2x_vfop_vlan_list_cmd(bp, vf, &cmd, fl,
1349 							      msg->vf_qid,
1350 							      false);
1351 				if (rc) {
1352 					vfop->rc = rc;
1353 					goto op_err;
1354 				}
1355 				return;
1356 			}
1357 		}
1358 		/* fall through */
1359 
1360 	case BNX2X_VFOP_MBX_Q_FILTERS_RXMODE:
1361 		/* next state */
1362 		vfop->state = BNX2X_VFOP_MBX_Q_FILTERS_MCAST;
1363 
1364 		if (msg->flags & VFPF_SET_Q_FILTERS_RX_MASK_CHANGED) {
1365 			unsigned long accept = 0;
1366 
1367 			/* covert VF-PF if mask to bnx2x accept flags */
1368 			if (msg->rx_mask & VFPF_RX_MASK_ACCEPT_MATCHED_UNICAST)
1369 				__set_bit(BNX2X_ACCEPT_UNICAST, &accept);
1370 
1371 			if (msg->rx_mask &
1372 					VFPF_RX_MASK_ACCEPT_MATCHED_MULTICAST)
1373 				__set_bit(BNX2X_ACCEPT_MULTICAST, &accept);
1374 
1375 			if (msg->rx_mask & VFPF_RX_MASK_ACCEPT_ALL_UNICAST)
1376 				__set_bit(BNX2X_ACCEPT_ALL_UNICAST, &accept);
1377 
1378 			if (msg->rx_mask & VFPF_RX_MASK_ACCEPT_ALL_MULTICAST)
1379 				__set_bit(BNX2X_ACCEPT_ALL_MULTICAST, &accept);
1380 
1381 			if (msg->rx_mask & VFPF_RX_MASK_ACCEPT_BROADCAST)
1382 				__set_bit(BNX2X_ACCEPT_BROADCAST, &accept);
1383 
1384 			/* A packet arriving the vf's mac should be accepted
1385 			 * with any vlan
1386 			 */
1387 			__set_bit(BNX2X_ACCEPT_ANY_VLAN, &accept);
1388 
1389 			/* set rx-mode */
1390 			rc = bnx2x_vfop_rxmode_cmd(bp, vf, &cmd,
1391 						   msg->vf_qid, accept);
1392 			if (rc) {
1393 				vfop->rc = rc;
1394 				goto op_err;
1395 			}
1396 			return;
1397 		}
1398 		/* fall through */
1399 
1400 	case BNX2X_VFOP_MBX_Q_FILTERS_MCAST:
1401 		/* next state */
1402 		vfop->state = BNX2X_VFOP_MBX_Q_FILTERS_DONE;
1403 
1404 		if (msg->flags & VFPF_SET_Q_FILTERS_MULTICAST_CHANGED) {
1405 			/* set mcasts */
1406 			rc = bnx2x_vfop_mcast_cmd(bp, vf, &cmd, msg->multicast,
1407 						  msg->n_multicast, false);
1408 			if (rc) {
1409 				vfop->rc = rc;
1410 				goto op_err;
1411 			}
1412 			return;
1413 		}
1414 		/* fall through */
1415 op_done:
1416 	case BNX2X_VFOP_MBX_Q_FILTERS_DONE:
1417 		bnx2x_vfop_end(bp, vf, vfop);
1418 		return;
1419 op_err:
1420 	BNX2X_ERR("QFILTERS[%d:%d] error: rc %d\n",
1421 		  vf->abs_vfid, msg->vf_qid, vfop->rc);
1422 	goto op_done;
1423 
1424 	default:
1425 		bnx2x_vfop_default(state);
1426 	}
1427 }
1428 
1429 static int bnx2x_vfop_mbx_qfilters_cmd(struct bnx2x *bp,
1430 					struct bnx2x_virtf *vf,
1431 					struct bnx2x_vfop_cmd *cmd)
1432 {
1433 	struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
1434 	if (vfop) {
1435 		bnx2x_vfop_opset(BNX2X_VFOP_MBX_Q_FILTERS_MACS,
1436 				 bnx2x_vfop_mbx_qfilters, cmd->done);
1437 		return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_mbx_qfilters,
1438 					     cmd->block);
1439 	}
1440 	return -ENOMEM;
1441 }
1442 
1443 static void bnx2x_vf_mbx_set_q_filters(struct bnx2x *bp,
1444 				       struct bnx2x_virtf *vf,
1445 				       struct bnx2x_vf_mbx *mbx)
1446 {
1447 	struct vfpf_set_q_filters_tlv *filters = &mbx->msg->req.set_q_filters;
1448 	struct pf_vf_bulletin_content *bulletin = BP_VF_BULLETIN(bp, vf->index);
1449 	struct bnx2x_vfop_cmd cmd = {
1450 		.done = bnx2x_vf_mbx_resp,
1451 		.block = false,
1452 	};
1453 
1454 	/* if a mac was already set for this VF via the set vf mac ndo, we only
1455 	 * accept mac configurations of that mac. Why accept them at all?
1456 	 * because PF may have been unable to configure the mac at the time
1457 	 * since queue was not set up.
1458 	 */
1459 	if (bulletin->valid_bitmap & 1 << MAC_ADDR_VALID) {
1460 		/* once a mac was set by ndo can only accept a single mac... */
1461 		if (filters->n_mac_vlan_filters > 1) {
1462 			BNX2X_ERR("VF[%d] requested the addition of multiple macs after set_vf_mac ndo was called\n",
1463 				  vf->abs_vfid);
1464 			vf->op_rc = -EPERM;
1465 			goto response;
1466 		}
1467 
1468 		/* ...and only the mac set by the ndo */
1469 		if (filters->n_mac_vlan_filters == 1 &&
1470 		    memcmp(filters->filters->mac, bulletin->mac, ETH_ALEN)) {
1471 			BNX2X_ERR("VF[%d] requested the addition of a mac address not matching the one configured by set_vf_mac ndo\n",
1472 				  vf->abs_vfid);
1473 
1474 			vf->op_rc = -EPERM;
1475 			goto response;
1476 		}
1477 	}
1478 
1479 	/* verify vf_qid */
1480 	if (filters->vf_qid > vf_rxq_count(vf))
1481 		goto response;
1482 
1483 	DP(BNX2X_MSG_IOV, "VF[%d] Q_FILTERS: queue[%d]\n",
1484 	   vf->abs_vfid,
1485 	   filters->vf_qid);
1486 
1487 	/* print q_filter message */
1488 	bnx2x_vf_mbx_dp_q_filters(bp, BNX2X_MSG_IOV, filters);
1489 
1490 	vf->op_rc = bnx2x_vfop_mbx_qfilters_cmd(bp, vf, &cmd);
1491 	if (vf->op_rc)
1492 		goto response;
1493 	return;
1494 
1495 response:
1496 	bnx2x_vf_mbx_resp(bp, vf);
1497 }
1498 
1499 static void bnx2x_vf_mbx_teardown_q(struct bnx2x *bp, struct bnx2x_virtf *vf,
1500 				    struct bnx2x_vf_mbx *mbx)
1501 {
1502 	int qid = mbx->msg->req.q_op.vf_qid;
1503 	struct bnx2x_vfop_cmd cmd = {
1504 		.done = bnx2x_vf_mbx_resp,
1505 		.block = false,
1506 	};
1507 
1508 	DP(BNX2X_MSG_IOV, "VF[%d] Q_TEARDOWN: vf_qid=%d\n",
1509 	   vf->abs_vfid, qid);
1510 
1511 	vf->op_rc = bnx2x_vfop_qdown_cmd(bp, vf, &cmd, qid);
1512 	if (vf->op_rc)
1513 		bnx2x_vf_mbx_resp(bp, vf);
1514 }
1515 
1516 static void bnx2x_vf_mbx_close_vf(struct bnx2x *bp, struct bnx2x_virtf *vf,
1517 				  struct bnx2x_vf_mbx *mbx)
1518 {
1519 	struct bnx2x_vfop_cmd cmd = {
1520 		.done = bnx2x_vf_mbx_resp,
1521 		.block = false,
1522 	};
1523 
1524 	DP(BNX2X_MSG_IOV, "VF[%d] VF_CLOSE\n", vf->abs_vfid);
1525 
1526 	vf->op_rc = bnx2x_vfop_close_cmd(bp, vf, &cmd);
1527 	if (vf->op_rc)
1528 		bnx2x_vf_mbx_resp(bp, vf);
1529 }
1530 
1531 static void bnx2x_vf_mbx_release_vf(struct bnx2x *bp, struct bnx2x_virtf *vf,
1532 				    struct bnx2x_vf_mbx *mbx)
1533 {
1534 	struct bnx2x_vfop_cmd cmd = {
1535 		.done = bnx2x_vf_mbx_resp,
1536 		.block = false,
1537 	};
1538 
1539 	DP(BNX2X_MSG_IOV, "VF[%d] VF_RELEASE\n", vf->abs_vfid);
1540 
1541 	vf->op_rc = bnx2x_vfop_release_cmd(bp, vf, &cmd);
1542 	if (vf->op_rc)
1543 		bnx2x_vf_mbx_resp(bp, vf);
1544 }
1545 
1546 /* dispatch request */
1547 static void bnx2x_vf_mbx_request(struct bnx2x *bp, struct bnx2x_virtf *vf,
1548 				  struct bnx2x_vf_mbx *mbx)
1549 {
1550 	int i;
1551 
1552 	/* check if tlv type is known */
1553 	if (bnx2x_tlv_supported(mbx->first_tlv.tl.type)) {
1554 		/* Lock the per vf op mutex and note the locker's identity.
1555 		 * The unlock will take place in mbx response.
1556 		 */
1557 		bnx2x_lock_vf_pf_channel(bp, vf, mbx->first_tlv.tl.type);
1558 
1559 		/* switch on the opcode */
1560 		switch (mbx->first_tlv.tl.type) {
1561 		case CHANNEL_TLV_ACQUIRE:
1562 			bnx2x_vf_mbx_acquire(bp, vf, mbx);
1563 			break;
1564 		case CHANNEL_TLV_INIT:
1565 			bnx2x_vf_mbx_init_vf(bp, vf, mbx);
1566 			break;
1567 		case CHANNEL_TLV_SETUP_Q:
1568 			bnx2x_vf_mbx_setup_q(bp, vf, mbx);
1569 			break;
1570 		case CHANNEL_TLV_SET_Q_FILTERS:
1571 			bnx2x_vf_mbx_set_q_filters(bp, vf, mbx);
1572 			break;
1573 		case CHANNEL_TLV_TEARDOWN_Q:
1574 			bnx2x_vf_mbx_teardown_q(bp, vf, mbx);
1575 			break;
1576 		case CHANNEL_TLV_CLOSE:
1577 			bnx2x_vf_mbx_close_vf(bp, vf, mbx);
1578 			break;
1579 		case CHANNEL_TLV_RELEASE:
1580 			bnx2x_vf_mbx_release_vf(bp, vf, mbx);
1581 			break;
1582 		}
1583 
1584 	} else {
1585 		/* unknown TLV - this may belong to a VF driver from the future
1586 		 * - a version written after this PF driver was written, which
1587 		 * supports features unknown as of yet. Too bad since we don't
1588 		 * support them. Or this may be because someone wrote a crappy
1589 		 * VF driver and is sending garbage over the channel.
1590 		 */
1591 		BNX2X_ERR("unknown TLV. type %d length %d. first 20 bytes of mailbox buffer:\n",
1592 			  mbx->first_tlv.tl.type, mbx->first_tlv.tl.length);
1593 		for (i = 0; i < 20; i++)
1594 			DP_CONT(BNX2X_MSG_IOV, "%x ",
1595 				mbx->msg->req.tlv_buf_size.tlv_buffer[i]);
1596 
1597 		/* test whether we can respond to the VF (do we have an address
1598 		 * for it?)
1599 		 */
1600 		if (vf->state == VF_ACQUIRED) {
1601 			/* mbx_resp uses the op_rc of the VF */
1602 			vf->op_rc = PFVF_STATUS_NOT_SUPPORTED;
1603 
1604 			/* notify the VF that we do not support this request */
1605 			bnx2x_vf_mbx_resp(bp, vf);
1606 		} else {
1607 			/* can't send a response since this VF is unknown to us
1608 			 * just unlock the channel and be done with.
1609 			 */
1610 			bnx2x_unlock_vf_pf_channel(bp, vf,
1611 						   mbx->first_tlv.tl.type);
1612 		}
1613 	}
1614 }
1615 
1616 /* handle new vf-pf message */
1617 void bnx2x_vf_mbx(struct bnx2x *bp, struct vf_pf_event_data *vfpf_event)
1618 {
1619 	struct bnx2x_virtf *vf;
1620 	struct bnx2x_vf_mbx *mbx;
1621 	u8 vf_idx;
1622 	int rc;
1623 
1624 	DP(BNX2X_MSG_IOV,
1625 	   "vf pf event received: vfid %d, address_hi %x, address lo %x",
1626 	   vfpf_event->vf_id, vfpf_event->msg_addr_hi, vfpf_event->msg_addr_lo);
1627 	/* Sanity checks consider removing later */
1628 
1629 	/* check if the vf_id is valid */
1630 	if (vfpf_event->vf_id - BP_VFDB(bp)->sriov.first_vf_in_pf >
1631 	    BNX2X_NR_VIRTFN(bp)) {
1632 		BNX2X_ERR("Illegal vf_id %d max allowed: %d\n",
1633 			  vfpf_event->vf_id, BNX2X_NR_VIRTFN(bp));
1634 		goto mbx_done;
1635 	}
1636 	vf_idx = bnx2x_vf_idx_by_abs_fid(bp, vfpf_event->vf_id);
1637 	mbx = BP_VF_MBX(bp, vf_idx);
1638 
1639 	/* verify an event is not currently being processed -
1640 	 * debug failsafe only
1641 	 */
1642 	if (mbx->flags & VF_MSG_INPROCESS) {
1643 		BNX2X_ERR("Previous message is still being processed, vf_id %d\n",
1644 			  vfpf_event->vf_id);
1645 		goto mbx_done;
1646 	}
1647 	vf = BP_VF(bp, vf_idx);
1648 
1649 	/* save the VF message address */
1650 	mbx->vf_addr_hi = vfpf_event->msg_addr_hi;
1651 	mbx->vf_addr_lo = vfpf_event->msg_addr_lo;
1652 	DP(BNX2X_MSG_IOV, "mailbox vf address hi 0x%x, lo 0x%x, offset 0x%x\n",
1653 	   mbx->vf_addr_hi, mbx->vf_addr_lo, mbx->first_tlv.resp_msg_offset);
1654 
1655 	/* dmae to get the VF request */
1656 	rc = bnx2x_copy32_vf_dmae(bp, true, mbx->msg_mapping, vf->abs_vfid,
1657 				  mbx->vf_addr_hi, mbx->vf_addr_lo,
1658 				  sizeof(union vfpf_tlvs)/4);
1659 	if (rc) {
1660 		BNX2X_ERR("Failed to copy request VF %d\n", vf->abs_vfid);
1661 		goto mbx_error;
1662 	}
1663 
1664 	/* process the VF message header */
1665 	mbx->first_tlv = mbx->msg->req.first_tlv;
1666 
1667 	/* dispatch the request (will prepare the response) */
1668 	bnx2x_vf_mbx_request(bp, vf, mbx);
1669 	goto mbx_done;
1670 
1671 mbx_error:
1672 	bnx2x_vf_release(bp, vf, false); /* non blocking */
1673 mbx_done:
1674 	return;
1675 }
1676 
1677 /* propagate local bulletin board to vf */
1678 int bnx2x_post_vf_bulletin(struct bnx2x *bp, int vf)
1679 {
1680 	struct pf_vf_bulletin_content *bulletin = BP_VF_BULLETIN(bp, vf);
1681 	dma_addr_t pf_addr = BP_VF_BULLETIN_DMA(bp)->mapping +
1682 		vf * BULLETIN_CONTENT_SIZE;
1683 	dma_addr_t vf_addr = bnx2x_vf(bp, vf, bulletin_map);
1684 	int rc;
1685 
1686 	/* can only update vf after init took place */
1687 	if (bnx2x_vf(bp, vf, state) != VF_ENABLED &&
1688 	    bnx2x_vf(bp, vf, state) != VF_ACQUIRED)
1689 		return 0;
1690 
1691 	/* increment bulletin board version and compute crc */
1692 	bulletin->version++;
1693 	bulletin->length = BULLETIN_CONTENT_SIZE;
1694 	bulletin->crc = bnx2x_crc_vf_bulletin(bp, bulletin);
1695 
1696 	/* propagate bulletin board via dmae to vm memory */
1697 	rc = bnx2x_copy32_vf_dmae(bp, false, pf_addr,
1698 				  bnx2x_vf(bp, vf, abs_vfid), U64_HI(vf_addr),
1699 				  U64_LO(vf_addr), bulletin->length / 4);
1700 	return rc;
1701 }
1702