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