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