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