bnx2x: replace mechanism to check for next available packet
[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 = 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
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-channel) */
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
848         /* issue the command and wait for completion */
849         return bnx2x_issue_dmae_with_comp(bp, &dmae);
850 }
851
852 static void bnx2x_vf_mbx_resp(struct bnx2x *bp, struct bnx2x_virtf *vf)
853 {
854         struct bnx2x_vf_mbx *mbx = BP_VF_MBX(bp, vf->index);
855         u64 vf_addr;
856         dma_addr_t pf_addr;
857         u16 length, type;
858         int rc;
859         struct pfvf_general_resp_tlv *resp = &mbx->msg->resp.general_resp;
860
861         /* prepare response */
862         type = mbx->first_tlv.tl.type;
863         length = type == CHANNEL_TLV_ACQUIRE ?
864                 sizeof(struct pfvf_acquire_resp_tlv) :
865                 sizeof(struct pfvf_general_resp_tlv);
866         bnx2x_add_tlv(bp, resp, 0, type, length);
867         resp->hdr.status = bnx2x_pfvf_status_codes(vf->op_rc);
868         bnx2x_add_tlv(bp, resp, length, CHANNEL_TLV_LIST_END,
869                       sizeof(struct channel_list_end_tlv));
870         bnx2x_dp_tlv_list(bp, resp);
871         DP(BNX2X_MSG_IOV, "mailbox vf address hi 0x%x, lo 0x%x, offset 0x%x\n",
872            mbx->vf_addr_hi, mbx->vf_addr_lo, mbx->first_tlv.resp_msg_offset);
873
874         /* send response */
875         vf_addr = HILO_U64(mbx->vf_addr_hi, mbx->vf_addr_lo) +
876                   mbx->first_tlv.resp_msg_offset;
877         pf_addr = mbx->msg_mapping +
878                   offsetof(struct bnx2x_vf_mbx_msg, resp);
879
880         /* copy the response body, if there is one, before the header, as the vf
881          * is sensitive to the header being written
882          */
883         if (resp->hdr.tl.length > sizeof(u64)) {
884                 length = resp->hdr.tl.length - sizeof(u64);
885                 vf_addr += sizeof(u64);
886                 pf_addr += sizeof(u64);
887                 rc = bnx2x_copy32_vf_dmae(bp, false, pf_addr, vf->abs_vfid,
888                                           U64_HI(vf_addr),
889                                           U64_LO(vf_addr),
890                                           length/4);
891                 if (rc) {
892                         BNX2X_ERR("Failed to copy response body to VF %d\n",
893                                   vf->abs_vfid);
894                         goto mbx_error;
895                 }
896                 vf_addr -= sizeof(u64);
897                 pf_addr -= sizeof(u64);
898         }
899
900         /* ack the FW */
901         storm_memset_vf_mbx_ack(bp, vf->abs_vfid);
902         mmiowb();
903
904         /* initiate dmae to send the response */
905         mbx->flags &= ~VF_MSG_INPROCESS;
906
907         /* copy the response header including status-done field,
908          * must be last dmae, must be after FW is acked
909          */
910         rc = bnx2x_copy32_vf_dmae(bp, false, pf_addr, vf->abs_vfid,
911                                   U64_HI(vf_addr),
912                                   U64_LO(vf_addr),
913                                   sizeof(u64)/4);
914
915         /* unlock channel mutex */
916         bnx2x_unlock_vf_pf_channel(bp, vf, mbx->first_tlv.tl.type);
917
918         if (rc) {
919                 BNX2X_ERR("Failed to copy response status to VF %d\n",
920                           vf->abs_vfid);
921                 goto mbx_error;
922         }
923         return;
924
925 mbx_error:
926         bnx2x_vf_release(bp, vf, false); /* non blocking */
927 }
928
929 static void bnx2x_vf_mbx_acquire_resp(struct bnx2x *bp, struct bnx2x_virtf *vf,
930                                       struct bnx2x_vf_mbx *mbx, int vfop_status)
931 {
932         int i;
933         struct pfvf_acquire_resp_tlv *resp = &mbx->msg->resp.acquire_resp;
934         struct pf_vf_resc *resc = &resp->resc;
935         u8 status = bnx2x_pfvf_status_codes(vfop_status);
936
937         memset(resp, 0, sizeof(*resp));
938
939         /* fill in pfdev info */
940         resp->pfdev_info.chip_num = bp->common.chip_id;
941         resp->pfdev_info.db_size = (1 << BNX2X_DB_SHIFT);
942         resp->pfdev_info.indices_per_sb = HC_SB_MAX_INDICES_E2;
943         resp->pfdev_info.pf_cap = (PFVF_CAP_RSS |
944                                    /* PFVF_CAP_DHC |*/ PFVF_CAP_TPA);
945         bnx2x_fill_fw_str(bp, resp->pfdev_info.fw_ver,
946                           sizeof(resp->pfdev_info.fw_ver));
947
948         if (status == PFVF_STATUS_NO_RESOURCE ||
949             status == PFVF_STATUS_SUCCESS) {
950                 /* set resources numbers, if status equals NO_RESOURCE these
951                  * are max possible numbers
952                  */
953                 resc->num_rxqs = vf_rxq_count(vf) ? :
954                         bnx2x_vf_max_queue_cnt(bp, vf);
955                 resc->num_txqs = vf_txq_count(vf) ? :
956                         bnx2x_vf_max_queue_cnt(bp, vf);
957                 resc->num_sbs = vf_sb_count(vf);
958                 resc->num_mac_filters = vf_mac_rules_cnt(vf);
959                 resc->num_vlan_filters = vf_vlan_rules_cnt(vf);
960                 resc->num_mc_filters = 0;
961
962                 if (status == PFVF_STATUS_SUCCESS) {
963                         /* fill in the allocated resources */
964                         struct pf_vf_bulletin_content *bulletin =
965                                 BP_VF_BULLETIN(bp, vf->index);
966
967                         for_each_vfq(vf, i)
968                                 resc->hw_qid[i] =
969                                         vfq_qzone_id(vf, vfq_get(vf, i));
970
971                         for_each_vf_sb(vf, i) {
972                                 resc->hw_sbs[i].hw_sb_id = vf_igu_sb(vf, i);
973                                 resc->hw_sbs[i].sb_qid = vf_hc_qzone(vf, i);
974                         }
975
976                         /* if a mac has been set for this vf, supply it */
977                         if (bulletin->valid_bitmap & 1 << MAC_ADDR_VALID) {
978                                 memcpy(resc->current_mac_addr, bulletin->mac,
979                                        ETH_ALEN);
980                         }
981                 }
982         }
983
984         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"
985            "resources- n_rxq-%d, n_txq-%d, n_sbs-%d, n_macs-%d, n_vlans-%d, n_mcs-%d, fw_ver: '%s'\n",
986            vf->abs_vfid,
987            resp->pfdev_info.chip_num,
988            resp->pfdev_info.db_size,
989            resp->pfdev_info.indices_per_sb,
990            resp->pfdev_info.pf_cap,
991            resc->num_rxqs,
992            resc->num_txqs,
993            resc->num_sbs,
994            resc->num_mac_filters,
995            resc->num_vlan_filters,
996            resc->num_mc_filters,
997            resp->pfdev_info.fw_ver);
998
999         DP_CONT(BNX2X_MSG_IOV, "hw_qids- [ ");
1000         for (i = 0; i < vf_rxq_count(vf); i++)
1001                 DP_CONT(BNX2X_MSG_IOV, "%d ", resc->hw_qid[i]);
1002         DP_CONT(BNX2X_MSG_IOV, "], sb_info- [ ");
1003         for (i = 0; i < vf_sb_count(vf); i++)
1004                 DP_CONT(BNX2X_MSG_IOV, "%d:%d ",
1005                         resc->hw_sbs[i].hw_sb_id,
1006                         resc->hw_sbs[i].sb_qid);
1007         DP_CONT(BNX2X_MSG_IOV, "]\n");
1008
1009         /* send the response */
1010         vf->op_rc = vfop_status;
1011         bnx2x_vf_mbx_resp(bp, vf);
1012 }
1013
1014 static void bnx2x_vf_mbx_acquire(struct bnx2x *bp, struct bnx2x_virtf *vf,
1015                                  struct bnx2x_vf_mbx *mbx)
1016 {
1017         int rc;
1018         struct vfpf_acquire_tlv *acquire = &mbx->msg->req.acquire;
1019
1020         /* log vfdef info */
1021         DP(BNX2X_MSG_IOV,
1022            "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",
1023            vf->abs_vfid, acquire->vfdev_info.vf_id, acquire->vfdev_info.vf_os,
1024            acquire->resc_request.num_rxqs, acquire->resc_request.num_txqs,
1025            acquire->resc_request.num_sbs, acquire->resc_request.num_mac_filters,
1026            acquire->resc_request.num_vlan_filters,
1027            acquire->resc_request.num_mc_filters);
1028
1029         /* acquire the resources */
1030         rc = bnx2x_vf_acquire(bp, vf, &acquire->resc_request);
1031
1032         /* store address of vf's bulletin board */
1033         vf->bulletin_map = acquire->bulletin_addr;
1034
1035         /* response */
1036         bnx2x_vf_mbx_acquire_resp(bp, vf, mbx, rc);
1037 }
1038
1039 static void bnx2x_vf_mbx_init_vf(struct bnx2x *bp, struct bnx2x_virtf *vf,
1040                               struct bnx2x_vf_mbx *mbx)
1041 {
1042         struct vfpf_init_tlv *init = &mbx->msg->req.init;
1043
1044         /* record ghost addresses from vf message */
1045         vf->spq_map = init->spq_addr;
1046         vf->fw_stat_map = init->stats_addr;
1047         vf->op_rc = bnx2x_vf_init(bp, vf, (dma_addr_t *)init->sb_addr);
1048
1049         /* response */
1050         bnx2x_vf_mbx_resp(bp, vf);
1051 }
1052
1053 /* convert MBX queue-flags to standard SP queue-flags */
1054 static void bnx2x_vf_mbx_set_q_flags(struct bnx2x *bp, u32 mbx_q_flags,
1055                                      unsigned long *sp_q_flags)
1056 {
1057         if (mbx_q_flags & VFPF_QUEUE_FLG_TPA)
1058                 __set_bit(BNX2X_Q_FLG_TPA, sp_q_flags);
1059         if (mbx_q_flags & VFPF_QUEUE_FLG_TPA_IPV6)
1060                 __set_bit(BNX2X_Q_FLG_TPA_IPV6, sp_q_flags);
1061         if (mbx_q_flags & VFPF_QUEUE_FLG_TPA_GRO)
1062                 __set_bit(BNX2X_Q_FLG_TPA_GRO, sp_q_flags);
1063         if (mbx_q_flags & VFPF_QUEUE_FLG_STATS)
1064                 __set_bit(BNX2X_Q_FLG_STATS, sp_q_flags);
1065         if (mbx_q_flags & VFPF_QUEUE_FLG_VLAN)
1066                 __set_bit(BNX2X_Q_FLG_VLAN, sp_q_flags);
1067         if (mbx_q_flags & VFPF_QUEUE_FLG_COS)
1068                 __set_bit(BNX2X_Q_FLG_COS, sp_q_flags);
1069         if (mbx_q_flags & VFPF_QUEUE_FLG_HC)
1070                 __set_bit(BNX2X_Q_FLG_HC, sp_q_flags);
1071         if (mbx_q_flags & VFPF_QUEUE_FLG_DHC)
1072                 __set_bit(BNX2X_Q_FLG_DHC, sp_q_flags);
1073
1074         /* outer vlan removal is set according to PF's multi function mode */
1075         if (IS_MF_SD(bp))
1076                 __set_bit(BNX2X_Q_FLG_OV, sp_q_flags);
1077 }
1078
1079 static void bnx2x_vf_mbx_setup_q(struct bnx2x *bp, struct bnx2x_virtf *vf,
1080                                  struct bnx2x_vf_mbx *mbx)
1081 {
1082         struct vfpf_setup_q_tlv *setup_q = &mbx->msg->req.setup_q;
1083         struct bnx2x_vfop_cmd cmd = {
1084                 .done = bnx2x_vf_mbx_resp,
1085                 .block = false,
1086         };
1087
1088         /* verify vf_qid */
1089         if (setup_q->vf_qid >= vf_rxq_count(vf)) {
1090                 BNX2X_ERR("vf_qid %d invalid, max queue count is %d\n",
1091                           setup_q->vf_qid, vf_rxq_count(vf));
1092                 vf->op_rc = -EINVAL;
1093                 goto response;
1094         }
1095
1096         /* tx queues must be setup alongside rx queues thus if the rx queue
1097          * is not marked as valid there's nothing to do.
1098          */
1099         if (setup_q->param_valid & (VFPF_RXQ_VALID|VFPF_TXQ_VALID)) {
1100                 struct bnx2x_vf_queue *q = vfq_get(vf, setup_q->vf_qid);
1101                 unsigned long q_type = 0;
1102
1103                 struct bnx2x_queue_init_params *init_p;
1104                 struct bnx2x_queue_setup_params *setup_p;
1105
1106                 /* re-init the VF operation context */
1107                 memset(&vf->op_params.qctor, 0 , sizeof(vf->op_params.qctor));
1108                 setup_p = &vf->op_params.qctor.prep_qsetup;
1109                 init_p =  &vf->op_params.qctor.qstate.params.init;
1110
1111                 /* activate immediately */
1112                 __set_bit(BNX2X_Q_FLG_ACTIVE, &setup_p->flags);
1113
1114                 if (setup_q->param_valid & VFPF_TXQ_VALID) {
1115                         struct bnx2x_txq_setup_params *txq_params =
1116                                 &setup_p->txq_params;
1117
1118                         __set_bit(BNX2X_Q_TYPE_HAS_TX, &q_type);
1119
1120                         /* save sb resource index */
1121                         q->sb_idx = setup_q->txq.vf_sb;
1122
1123                         /* tx init */
1124                         init_p->tx.hc_rate = setup_q->txq.hc_rate;
1125                         init_p->tx.sb_cq_index = setup_q->txq.sb_index;
1126
1127                         bnx2x_vf_mbx_set_q_flags(bp, setup_q->txq.flags,
1128                                                  &init_p->tx.flags);
1129
1130                         /* tx setup - flags */
1131                         bnx2x_vf_mbx_set_q_flags(bp, setup_q->txq.flags,
1132                                                  &setup_p->flags);
1133
1134                         /* tx setup - general, nothing */
1135
1136                         /* tx setup - tx */
1137                         txq_params->dscr_map = setup_q->txq.txq_addr;
1138                         txq_params->sb_cq_index = setup_q->txq.sb_index;
1139                         txq_params->traffic_type = setup_q->txq.traffic_type;
1140
1141                         bnx2x_vfop_qctor_dump_tx(bp, vf, init_p, setup_p,
1142                                                  q->index, q->sb_idx);
1143                 }
1144
1145                 if (setup_q->param_valid & VFPF_RXQ_VALID) {
1146                         struct bnx2x_rxq_setup_params *rxq_params =
1147                                                         &setup_p->rxq_params;
1148
1149                         __set_bit(BNX2X_Q_TYPE_HAS_RX, &q_type);
1150
1151                         /* Note: there is no support for different SBs
1152                          * for TX and RX
1153                          */
1154                         q->sb_idx = setup_q->rxq.vf_sb;
1155
1156                         /* rx init */
1157                         init_p->rx.hc_rate = setup_q->rxq.hc_rate;
1158                         init_p->rx.sb_cq_index = setup_q->rxq.sb_index;
1159                         bnx2x_vf_mbx_set_q_flags(bp, setup_q->rxq.flags,
1160                                                  &init_p->rx.flags);
1161
1162                         /* rx setup - flags */
1163                         bnx2x_vf_mbx_set_q_flags(bp, setup_q->rxq.flags,
1164                                                  &setup_p->flags);
1165
1166                         /* rx setup - general */
1167                         setup_p->gen_params.mtu = setup_q->rxq.mtu;
1168
1169                         /* rx setup - rx */
1170                         rxq_params->drop_flags = setup_q->rxq.drop_flags;
1171                         rxq_params->dscr_map = setup_q->rxq.rxq_addr;
1172                         rxq_params->sge_map = setup_q->rxq.sge_addr;
1173                         rxq_params->rcq_map = setup_q->rxq.rcq_addr;
1174                         rxq_params->rcq_np_map = setup_q->rxq.rcq_np_addr;
1175                         rxq_params->buf_sz = setup_q->rxq.buf_sz;
1176                         rxq_params->tpa_agg_sz = setup_q->rxq.tpa_agg_sz;
1177                         rxq_params->max_sges_pkt = setup_q->rxq.max_sge_pkt;
1178                         rxq_params->sge_buf_sz = setup_q->rxq.sge_buf_sz;
1179                         rxq_params->cache_line_log =
1180                                 setup_q->rxq.cache_line_log;
1181                         rxq_params->sb_cq_index = setup_q->rxq.sb_index;
1182
1183                         bnx2x_vfop_qctor_dump_rx(bp, vf, init_p, setup_p,
1184                                                  q->index, q->sb_idx);
1185                 }
1186                 /* complete the preparations */
1187                 bnx2x_vfop_qctor_prep(bp, vf, q, &vf->op_params.qctor, q_type);
1188
1189                 vf->op_rc = bnx2x_vfop_qsetup_cmd(bp, vf, &cmd, q->index);
1190                 if (vf->op_rc)
1191                         goto response;
1192                 return;
1193         }
1194 response:
1195         bnx2x_vf_mbx_resp(bp, vf);
1196 }
1197
1198 enum bnx2x_vfop_filters_state {
1199            BNX2X_VFOP_MBX_Q_FILTERS_MACS,
1200            BNX2X_VFOP_MBX_Q_FILTERS_VLANS,
1201            BNX2X_VFOP_MBX_Q_FILTERS_RXMODE,
1202            BNX2X_VFOP_MBX_Q_FILTERS_MCAST,
1203            BNX2X_VFOP_MBX_Q_FILTERS_DONE
1204 };
1205
1206 static int bnx2x_vf_mbx_macvlan_list(struct bnx2x *bp,
1207                                      struct bnx2x_virtf *vf,
1208                                      struct vfpf_set_q_filters_tlv *tlv,
1209                                      struct bnx2x_vfop_filters **pfl,
1210                                      u32 type_flag)
1211 {
1212         int i, j;
1213         struct bnx2x_vfop_filters *fl = NULL;
1214         size_t fsz;
1215
1216         fsz = tlv->n_mac_vlan_filters * sizeof(struct bnx2x_vfop_filter) +
1217                 sizeof(struct bnx2x_vfop_filters);
1218
1219         fl = kzalloc(fsz, GFP_KERNEL);
1220         if (!fl)
1221                 return -ENOMEM;
1222
1223         INIT_LIST_HEAD(&fl->head);
1224
1225         for (i = 0, j = 0; i < tlv->n_mac_vlan_filters; i++) {
1226                 struct vfpf_q_mac_vlan_filter *msg_filter = &tlv->filters[i];
1227
1228                 if ((msg_filter->flags & type_flag) != type_flag)
1229                         continue;
1230                 if (type_flag == VFPF_Q_FILTER_DEST_MAC_VALID) {
1231                         fl->filters[j].mac = msg_filter->mac;
1232                         fl->filters[j].type = BNX2X_VFOP_FILTER_MAC;
1233                 } else {
1234                         fl->filters[j].vid = msg_filter->vlan_tag;
1235                         fl->filters[j].type = BNX2X_VFOP_FILTER_VLAN;
1236                 }
1237                 fl->filters[j].add =
1238                         (msg_filter->flags & VFPF_Q_FILTER_SET_MAC) ?
1239                         true : false;
1240                 list_add_tail(&fl->filters[j++].link, &fl->head);
1241         }
1242         if (list_empty(&fl->head))
1243                 kfree(fl);
1244         else
1245                 *pfl = fl;
1246
1247         return 0;
1248 }
1249
1250 static void bnx2x_vf_mbx_dp_q_filter(struct bnx2x *bp, int msglvl, int idx,
1251                                        struct vfpf_q_mac_vlan_filter *filter)
1252 {
1253         DP(msglvl, "MAC-VLAN[%d] -- flags=0x%x\n", idx, filter->flags);
1254         if (filter->flags & VFPF_Q_FILTER_VLAN_TAG_VALID)
1255                 DP_CONT(msglvl, ", vlan=%d", filter->vlan_tag);
1256         if (filter->flags & VFPF_Q_FILTER_DEST_MAC_VALID)
1257                 DP_CONT(msglvl, ", MAC=%pM", filter->mac);
1258         DP_CONT(msglvl, "\n");
1259 }
1260
1261 static void bnx2x_vf_mbx_dp_q_filters(struct bnx2x *bp, int msglvl,
1262                                        struct vfpf_set_q_filters_tlv *filters)
1263 {
1264         int i;
1265
1266         if (filters->flags & VFPF_SET_Q_FILTERS_MAC_VLAN_CHANGED)
1267                 for (i = 0; i < filters->n_mac_vlan_filters; i++)
1268                         bnx2x_vf_mbx_dp_q_filter(bp, msglvl, i,
1269                                                  &filters->filters[i]);
1270
1271         if (filters->flags & VFPF_SET_Q_FILTERS_RX_MASK_CHANGED)
1272                 DP(msglvl, "RX-MASK=0x%x\n", filters->rx_mask);
1273
1274         if (filters->flags & VFPF_SET_Q_FILTERS_MULTICAST_CHANGED)
1275                 for (i = 0; i < filters->n_multicast; i++)
1276                         DP(msglvl, "MULTICAST=%pM\n", filters->multicast[i]);
1277 }
1278
1279 #define VFPF_MAC_FILTER         VFPF_Q_FILTER_DEST_MAC_VALID
1280 #define VFPF_VLAN_FILTER        VFPF_Q_FILTER_VLAN_TAG_VALID
1281
1282 static void bnx2x_vfop_mbx_qfilters(struct bnx2x *bp, struct bnx2x_virtf *vf)
1283 {
1284         int rc;
1285
1286         struct vfpf_set_q_filters_tlv *msg =
1287                 &BP_VF_MBX(bp, vf->index)->msg->req.set_q_filters;
1288
1289         struct bnx2x_vfop *vfop = bnx2x_vfop_cur(bp, vf);
1290         enum bnx2x_vfop_filters_state state = vfop->state;
1291
1292         struct bnx2x_vfop_cmd cmd = {
1293                 .done = bnx2x_vfop_mbx_qfilters,
1294                 .block = false,
1295         };
1296
1297         DP(BNX2X_MSG_IOV, "STATE: %d\n", state);
1298
1299         if (vfop->rc < 0)
1300                 goto op_err;
1301
1302         switch (state) {
1303         case BNX2X_VFOP_MBX_Q_FILTERS_MACS:
1304                 /* next state */
1305                 vfop->state = BNX2X_VFOP_MBX_Q_FILTERS_VLANS;
1306
1307                 /* check for any vlan/mac changes */
1308                 if (msg->flags & VFPF_SET_Q_FILTERS_MAC_VLAN_CHANGED) {
1309                         /* build mac list */
1310                         struct bnx2x_vfop_filters *fl = NULL;
1311
1312                         vfop->rc = bnx2x_vf_mbx_macvlan_list(bp, vf, msg, &fl,
1313                                                              VFPF_MAC_FILTER);
1314                         if (vfop->rc)
1315                                 goto op_err;
1316
1317                         if (fl) {
1318                                 /* set mac list */
1319                                 rc = bnx2x_vfop_mac_list_cmd(bp, vf, &cmd, fl,
1320                                                              msg->vf_qid,
1321                                                              false);
1322                                 if (rc) {
1323                                         vfop->rc = rc;
1324                                         goto op_err;
1325                                 }
1326                                 return;
1327                         }
1328                 }
1329                 /* fall through */
1330
1331         case BNX2X_VFOP_MBX_Q_FILTERS_VLANS:
1332                 /* next state */
1333                 vfop->state = BNX2X_VFOP_MBX_Q_FILTERS_RXMODE;
1334
1335                 /* check for any vlan/mac changes */
1336                 if (msg->flags & VFPF_SET_Q_FILTERS_MAC_VLAN_CHANGED) {
1337                         /* build vlan list */
1338                         struct bnx2x_vfop_filters *fl = NULL;
1339
1340                         vfop->rc = bnx2x_vf_mbx_macvlan_list(bp, vf, msg, &fl,
1341                                                              VFPF_VLAN_FILTER);
1342                         if (vfop->rc)
1343                                 goto op_err;
1344
1345                         if (fl) {
1346                                 /* set vlan list */
1347                                 rc = bnx2x_vfop_vlan_list_cmd(bp, vf, &cmd, fl,
1348                                                               msg->vf_qid,
1349                                                               false);
1350                                 if (rc) {
1351                                         vfop->rc = rc;
1352                                         goto op_err;
1353                                 }
1354                                 return;
1355                         }
1356                 }
1357                 /* fall through */
1358
1359         case BNX2X_VFOP_MBX_Q_FILTERS_RXMODE:
1360                 /* next state */
1361                 vfop->state = BNX2X_VFOP_MBX_Q_FILTERS_MCAST;
1362
1363                 if (msg->flags & VFPF_SET_Q_FILTERS_RX_MASK_CHANGED) {
1364                         unsigned long accept = 0;
1365
1366                         /* covert VF-PF if mask to bnx2x accept flags */
1367                         if (msg->rx_mask & VFPF_RX_MASK_ACCEPT_MATCHED_UNICAST)
1368                                 __set_bit(BNX2X_ACCEPT_UNICAST, &accept);
1369
1370                         if (msg->rx_mask &
1371                                         VFPF_RX_MASK_ACCEPT_MATCHED_MULTICAST)
1372                                 __set_bit(BNX2X_ACCEPT_MULTICAST, &accept);
1373
1374                         if (msg->rx_mask & VFPF_RX_MASK_ACCEPT_ALL_UNICAST)
1375                                 __set_bit(BNX2X_ACCEPT_ALL_UNICAST, &accept);
1376
1377                         if (msg->rx_mask & VFPF_RX_MASK_ACCEPT_ALL_MULTICAST)
1378                                 __set_bit(BNX2X_ACCEPT_ALL_MULTICAST, &accept);
1379
1380                         if (msg->rx_mask & VFPF_RX_MASK_ACCEPT_BROADCAST)
1381                                 __set_bit(BNX2X_ACCEPT_BROADCAST, &accept);
1382
1383                         /* A packet arriving the vf's mac should be accepted
1384                          * with any vlan
1385                          */
1386                         __set_bit(BNX2X_ACCEPT_ANY_VLAN, &accept);
1387
1388                         /* set rx-mode */
1389                         rc = bnx2x_vfop_rxmode_cmd(bp, vf, &cmd,
1390                                                    msg->vf_qid, accept);
1391                         if (rc) {
1392                                 vfop->rc = rc;
1393                                 goto op_err;
1394                         }
1395                         return;
1396                 }
1397                 /* fall through */
1398
1399         case BNX2X_VFOP_MBX_Q_FILTERS_MCAST:
1400                 /* next state */
1401                 vfop->state = BNX2X_VFOP_MBX_Q_FILTERS_DONE;
1402
1403                 if (msg->flags & VFPF_SET_Q_FILTERS_MULTICAST_CHANGED) {
1404                         /* set mcasts */
1405                         rc = bnx2x_vfop_mcast_cmd(bp, vf, &cmd, msg->multicast,
1406                                                   msg->n_multicast, false);
1407                         if (rc) {
1408                                 vfop->rc = rc;
1409                                 goto op_err;
1410                         }
1411                         return;
1412                 }
1413                 /* fall through */
1414 op_done:
1415         case BNX2X_VFOP_MBX_Q_FILTERS_DONE:
1416                 bnx2x_vfop_end(bp, vf, vfop);
1417                 return;
1418 op_err:
1419         BNX2X_ERR("QFILTERS[%d:%d] error: rc %d\n",
1420                   vf->abs_vfid, msg->vf_qid, vfop->rc);
1421         goto op_done;
1422
1423         default:
1424                 bnx2x_vfop_default(state);
1425         }
1426 }
1427
1428 static int bnx2x_vfop_mbx_qfilters_cmd(struct bnx2x *bp,
1429                                         struct bnx2x_virtf *vf,
1430                                         struct bnx2x_vfop_cmd *cmd)
1431 {
1432         struct bnx2x_vfop *vfop = bnx2x_vfop_add(bp, vf);
1433         if (vfop) {
1434                 bnx2x_vfop_opset(BNX2X_VFOP_MBX_Q_FILTERS_MACS,
1435                                  bnx2x_vfop_mbx_qfilters, cmd->done);
1436                 return bnx2x_vfop_transition(bp, vf, bnx2x_vfop_mbx_qfilters,
1437                                              cmd->block);
1438         }
1439         return -ENOMEM;
1440 }
1441
1442 static void bnx2x_vf_mbx_set_q_filters(struct bnx2x *bp,
1443                                        struct bnx2x_virtf *vf,
1444                                        struct bnx2x_vf_mbx *mbx)
1445 {
1446         struct vfpf_set_q_filters_tlv *filters = &mbx->msg->req.set_q_filters;
1447         struct pf_vf_bulletin_content *bulletin = BP_VF_BULLETIN(bp, vf->index);
1448         struct bnx2x_vfop_cmd cmd = {
1449                 .done = bnx2x_vf_mbx_resp,
1450                 .block = false,
1451         };
1452
1453         /* if a mac was already set for this VF via the set vf mac ndo, we only
1454          * accept mac configurations of that mac. Why accept them at all?
1455          * because PF may have been unable to configure the mac at the time
1456          * since queue was not set up.
1457          */
1458         if (bulletin->valid_bitmap & 1 << MAC_ADDR_VALID) {
1459                 /* once a mac was set by ndo can only accept a single mac... */
1460                 if (filters->n_mac_vlan_filters > 1) {
1461                         BNX2X_ERR("VF[%d] requested the addition of multiple macs after set_vf_mac ndo was called\n",
1462                                   vf->abs_vfid);
1463                         vf->op_rc = -EPERM;
1464                         goto response;
1465                 }
1466
1467                 /* ...and only the mac set by the ndo */
1468                 if (filters->n_mac_vlan_filters == 1 &&
1469                     memcmp(filters->filters->mac, bulletin->mac, ETH_ALEN)) {
1470                         BNX2X_ERR("VF[%d] requested the addition of a mac address not matching the one configured by set_vf_mac ndo\n",
1471                                   vf->abs_vfid);
1472
1473                         vf->op_rc = -EPERM;
1474                         goto response;
1475                 }
1476         }
1477
1478         /* verify vf_qid */
1479         if (filters->vf_qid > vf_rxq_count(vf))
1480                 goto response;
1481
1482         DP(BNX2X_MSG_IOV, "VF[%d] Q_FILTERS: queue[%d]\n",
1483            vf->abs_vfid,
1484            filters->vf_qid);
1485
1486         /* print q_filter message */
1487         bnx2x_vf_mbx_dp_q_filters(bp, BNX2X_MSG_IOV, filters);
1488
1489         vf->op_rc = bnx2x_vfop_mbx_qfilters_cmd(bp, vf, &cmd);
1490         if (vf->op_rc)
1491                 goto response;
1492         return;
1493
1494 response:
1495         bnx2x_vf_mbx_resp(bp, vf);
1496 }
1497
1498 static void bnx2x_vf_mbx_teardown_q(struct bnx2x *bp, struct bnx2x_virtf *vf,
1499                                     struct bnx2x_vf_mbx *mbx)
1500 {
1501         int qid = mbx->msg->req.q_op.vf_qid;
1502         struct bnx2x_vfop_cmd cmd = {
1503                 .done = bnx2x_vf_mbx_resp,
1504                 .block = false,
1505         };
1506
1507         DP(BNX2X_MSG_IOV, "VF[%d] Q_TEARDOWN: vf_qid=%d\n",
1508            vf->abs_vfid, qid);
1509
1510         vf->op_rc = bnx2x_vfop_qdown_cmd(bp, vf, &cmd, qid);
1511         if (vf->op_rc)
1512                 bnx2x_vf_mbx_resp(bp, vf);
1513 }
1514
1515 static void bnx2x_vf_mbx_close_vf(struct bnx2x *bp, struct bnx2x_virtf *vf,
1516                                   struct bnx2x_vf_mbx *mbx)
1517 {
1518         struct bnx2x_vfop_cmd cmd = {
1519                 .done = bnx2x_vf_mbx_resp,
1520                 .block = false,
1521         };
1522
1523         DP(BNX2X_MSG_IOV, "VF[%d] VF_CLOSE\n", vf->abs_vfid);
1524
1525         vf->op_rc = bnx2x_vfop_close_cmd(bp, vf, &cmd);
1526         if (vf->op_rc)
1527                 bnx2x_vf_mbx_resp(bp, vf);
1528 }
1529
1530 static void bnx2x_vf_mbx_release_vf(struct bnx2x *bp, struct bnx2x_virtf *vf,
1531                                     struct bnx2x_vf_mbx *mbx)
1532 {
1533         struct bnx2x_vfop_cmd cmd = {
1534                 .done = bnx2x_vf_mbx_resp,
1535                 .block = false,
1536         };
1537
1538         DP(BNX2X_MSG_IOV, "VF[%d] VF_RELEASE\n", vf->abs_vfid);
1539
1540         vf->op_rc = bnx2x_vfop_release_cmd(bp, vf, &cmd);
1541         if (vf->op_rc)
1542                 bnx2x_vf_mbx_resp(bp, vf);
1543 }
1544
1545 /* dispatch request */
1546 static void bnx2x_vf_mbx_request(struct bnx2x *bp, struct bnx2x_virtf *vf,
1547                                   struct bnx2x_vf_mbx *mbx)
1548 {
1549         int i;
1550
1551         /* check if tlv type is known */
1552         if (bnx2x_tlv_supported(mbx->first_tlv.tl.type)) {
1553                 /* Lock the per vf op mutex and note the locker's identity.
1554                  * The unlock will take place in mbx response.
1555                  */
1556                 bnx2x_lock_vf_pf_channel(bp, vf, mbx->first_tlv.tl.type);
1557
1558                 /* switch on the opcode */
1559                 switch (mbx->first_tlv.tl.type) {
1560                 case CHANNEL_TLV_ACQUIRE:
1561                         bnx2x_vf_mbx_acquire(bp, vf, mbx);
1562                         break;
1563                 case CHANNEL_TLV_INIT:
1564                         bnx2x_vf_mbx_init_vf(bp, vf, mbx);
1565                         break;
1566                 case CHANNEL_TLV_SETUP_Q:
1567                         bnx2x_vf_mbx_setup_q(bp, vf, mbx);
1568                         break;
1569                 case CHANNEL_TLV_SET_Q_FILTERS:
1570                         bnx2x_vf_mbx_set_q_filters(bp, vf, mbx);
1571                         break;
1572                 case CHANNEL_TLV_TEARDOWN_Q:
1573                         bnx2x_vf_mbx_teardown_q(bp, vf, mbx);
1574                         break;
1575                 case CHANNEL_TLV_CLOSE:
1576                         bnx2x_vf_mbx_close_vf(bp, vf, mbx);
1577                         break;
1578                 case CHANNEL_TLV_RELEASE:
1579                         bnx2x_vf_mbx_release_vf(bp, vf, mbx);
1580                         break;
1581                 }
1582
1583         } else {
1584                 /* unknown TLV - this may belong to a VF driver from the future
1585                  * - a version written after this PF driver was written, which
1586                  * supports features unknown as of yet. Too bad since we don't
1587                  * support them. Or this may be because someone wrote a crappy
1588                  * VF driver and is sending garbage over the channel.
1589                  */
1590                 BNX2X_ERR("unknown TLV. type %d length %d vf->state was %d. first 20 bytes of mailbox buffer:\n",
1591                           mbx->first_tlv.tl.type, mbx->first_tlv.tl.length,
1592                           vf->state);
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 ack the FW to release the mailbox and unlock
1609                          * the channel.
1610                          */
1611                         storm_memset_vf_mbx_ack(bp, vf->abs_vfid);
1612                         mmiowb();
1613                         bnx2x_unlock_vf_pf_channel(bp, vf,
1614                                                    mbx->first_tlv.tl.type);
1615                 }
1616         }
1617 }
1618
1619 /* handle new vf-pf message */
1620 void bnx2x_vf_mbx(struct bnx2x *bp, struct vf_pf_event_data *vfpf_event)
1621 {
1622         struct bnx2x_virtf *vf;
1623         struct bnx2x_vf_mbx *mbx;
1624         u8 vf_idx;
1625         int rc;
1626
1627         DP(BNX2X_MSG_IOV,
1628            "vf pf event received: vfid %d, address_hi %x, address lo %x",
1629            vfpf_event->vf_id, vfpf_event->msg_addr_hi, vfpf_event->msg_addr_lo);
1630         /* Sanity checks consider removing later */
1631
1632         /* check if the vf_id is valid */
1633         if (vfpf_event->vf_id - BP_VFDB(bp)->sriov.first_vf_in_pf >
1634             BNX2X_NR_VIRTFN(bp)) {
1635                 BNX2X_ERR("Illegal vf_id %d max allowed: %d\n",
1636                           vfpf_event->vf_id, BNX2X_NR_VIRTFN(bp));
1637                 goto mbx_done;
1638         }
1639         vf_idx = bnx2x_vf_idx_by_abs_fid(bp, vfpf_event->vf_id);
1640         mbx = BP_VF_MBX(bp, vf_idx);
1641
1642         /* verify an event is not currently being processed -
1643          * debug failsafe only
1644          */
1645         if (mbx->flags & VF_MSG_INPROCESS) {
1646                 BNX2X_ERR("Previous message is still being processed, vf_id %d\n",
1647                           vfpf_event->vf_id);
1648                 goto mbx_done;
1649         }
1650         vf = BP_VF(bp, vf_idx);
1651
1652         /* save the VF message address */
1653         mbx->vf_addr_hi = vfpf_event->msg_addr_hi;
1654         mbx->vf_addr_lo = vfpf_event->msg_addr_lo;
1655         DP(BNX2X_MSG_IOV, "mailbox vf address hi 0x%x, lo 0x%x, offset 0x%x\n",
1656            mbx->vf_addr_hi, mbx->vf_addr_lo, mbx->first_tlv.resp_msg_offset);
1657
1658         /* dmae to get the VF request */
1659         rc = bnx2x_copy32_vf_dmae(bp, true, mbx->msg_mapping, vf->abs_vfid,
1660                                   mbx->vf_addr_hi, mbx->vf_addr_lo,
1661                                   sizeof(union vfpf_tlvs)/4);
1662         if (rc) {
1663                 BNX2X_ERR("Failed to copy request VF %d\n", vf->abs_vfid);
1664                 goto mbx_error;
1665         }
1666
1667         /* process the VF message header */
1668         mbx->first_tlv = mbx->msg->req.first_tlv;
1669
1670         /* dispatch the request (will prepare the response) */
1671         bnx2x_vf_mbx_request(bp, vf, mbx);
1672         goto mbx_done;
1673
1674 mbx_error:
1675         bnx2x_vf_release(bp, vf, false); /* non blocking */
1676 mbx_done:
1677         return;
1678 }
1679
1680 /* propagate local bulletin board to vf */
1681 int bnx2x_post_vf_bulletin(struct bnx2x *bp, int vf)
1682 {
1683         struct pf_vf_bulletin_content *bulletin = BP_VF_BULLETIN(bp, vf);
1684         dma_addr_t pf_addr = BP_VF_BULLETIN_DMA(bp)->mapping +
1685                 vf * BULLETIN_CONTENT_SIZE;
1686         dma_addr_t vf_addr = bnx2x_vf(bp, vf, bulletin_map);
1687         int rc;
1688
1689         /* can only update vf after init took place */
1690         if (bnx2x_vf(bp, vf, state) != VF_ENABLED &&
1691             bnx2x_vf(bp, vf, state) != VF_ACQUIRED)
1692                 return 0;
1693
1694         /* increment bulletin board version and compute crc */
1695         bulletin->version++;
1696         bulletin->length = BULLETIN_CONTENT_SIZE;
1697         bulletin->crc = bnx2x_crc_vf_bulletin(bp, bulletin);
1698
1699         /* propagate bulletin board via dmae to vm memory */
1700         rc = bnx2x_copy32_vf_dmae(bp, false, pf_addr,
1701                                   bnx2x_vf(bp, vf, abs_vfid), U64_HI(vf_addr),
1702                                   U64_LO(vf_addr), bulletin->length / 4);
1703         return rc;
1704 }