New upstream version 17.11.4
[deb_dpdk.git] / drivers / net / qede / base / ecore_sriov.c
1 /*
2  * Copyright (c) 2016 QLogic Corporation.
3  * All rights reserved.
4  * www.qlogic.com
5  *
6  * See LICENSE.qede_pmd for copyright and licensing details.
7  */
8
9 #include "bcm_osal.h"
10 #include "ecore.h"
11 #include "reg_addr.h"
12 #include "ecore_sriov.h"
13 #include "ecore_status.h"
14 #include "ecore_hw.h"
15 #include "ecore_hw_defs.h"
16 #include "ecore_int.h"
17 #include "ecore_hsi_eth.h"
18 #include "ecore_l2.h"
19 #include "ecore_vfpf_if.h"
20 #include "ecore_rt_defs.h"
21 #include "ecore_init_ops.h"
22 #include "ecore_gtt_reg_addr.h"
23 #include "ecore_iro.h"
24 #include "ecore_mcp.h"
25 #include "ecore_cxt.h"
26 #include "ecore_vf.h"
27 #include "ecore_init_fw_funcs.h"
28 #include "ecore_sp_commands.h"
29
30 static enum _ecore_status_t ecore_sriov_eqe_event(struct ecore_hwfn *p_hwfn,
31                                                   u8 opcode,
32                                                   __le16 echo,
33                                                   union event_ring_data *data,
34                                                   u8 fw_return_code);
35
36 const char *ecore_channel_tlvs_string[] = {
37         "CHANNEL_TLV_NONE",     /* ends tlv sequence */
38         "CHANNEL_TLV_ACQUIRE",
39         "CHANNEL_TLV_VPORT_START",
40         "CHANNEL_TLV_VPORT_UPDATE",
41         "CHANNEL_TLV_VPORT_TEARDOWN",
42         "CHANNEL_TLV_START_RXQ",
43         "CHANNEL_TLV_START_TXQ",
44         "CHANNEL_TLV_STOP_RXQ",
45         "CHANNEL_TLV_STOP_TXQ",
46         "CHANNEL_TLV_UPDATE_RXQ",
47         "CHANNEL_TLV_INT_CLEANUP",
48         "CHANNEL_TLV_CLOSE",
49         "CHANNEL_TLV_RELEASE",
50         "CHANNEL_TLV_LIST_END",
51         "CHANNEL_TLV_UCAST_FILTER",
52         "CHANNEL_TLV_VPORT_UPDATE_ACTIVATE",
53         "CHANNEL_TLV_VPORT_UPDATE_TX_SWITCH",
54         "CHANNEL_TLV_VPORT_UPDATE_VLAN_STRIP",
55         "CHANNEL_TLV_VPORT_UPDATE_MCAST",
56         "CHANNEL_TLV_VPORT_UPDATE_ACCEPT_PARAM",
57         "CHANNEL_TLV_VPORT_UPDATE_RSS",
58         "CHANNEL_TLV_VPORT_UPDATE_ACCEPT_ANY_VLAN",
59         "CHANNEL_TLV_VPORT_UPDATE_SGE_TPA",
60         "CHANNEL_TLV_UPDATE_TUNN_PARAM",
61         "CHANNEL_TLV_COALESCE_UPDATE",
62         "CHANNEL_TLV_QID",
63         "CHANNEL_TLV_COALESCE_READ",
64         "CHANNEL_TLV_BULLETIN_UPDATE_MAC",
65         "CHANNEL_TLV_UPDATE_MTU",
66         "CHANNEL_TLV_MAX"
67 };
68
69 static u8 ecore_vf_calculate_legacy(struct ecore_vf_info *p_vf)
70 {
71         u8 legacy = 0;
72
73         if (p_vf->acquire.vfdev_info.eth_fp_hsi_minor ==
74             ETH_HSI_VER_NO_PKT_LEN_TUNN)
75                 legacy |= ECORE_QCID_LEGACY_VF_RX_PROD;
76
77         if (!(p_vf->acquire.vfdev_info.capabilities &
78              VFPF_ACQUIRE_CAP_QUEUE_QIDS))
79                 legacy |= ECORE_QCID_LEGACY_VF_CID;
80
81         return legacy;
82 }
83
84 /* IOV ramrods */
85 static enum _ecore_status_t ecore_sp_vf_start(struct ecore_hwfn *p_hwfn,
86                                               struct ecore_vf_info *p_vf)
87 {
88         struct vf_start_ramrod_data *p_ramrod = OSAL_NULL;
89         struct ecore_spq_entry *p_ent = OSAL_NULL;
90         struct ecore_sp_init_data init_data;
91         enum _ecore_status_t rc = ECORE_NOTIMPL;
92         u8 fp_minor;
93
94         /* Get SPQ entry */
95         OSAL_MEMSET(&init_data, 0, sizeof(init_data));
96         init_data.cid = ecore_spq_get_cid(p_hwfn);
97         init_data.opaque_fid = p_vf->opaque_fid;
98         init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
99
100         rc = ecore_sp_init_request(p_hwfn, &p_ent,
101                                    COMMON_RAMROD_VF_START,
102                                    PROTOCOLID_COMMON, &init_data);
103         if (rc != ECORE_SUCCESS)
104                 return rc;
105
106         p_ramrod = &p_ent->ramrod.vf_start;
107
108         p_ramrod->vf_id = GET_FIELD(p_vf->concrete_fid, PXP_CONCRETE_FID_VFID);
109         p_ramrod->opaque_fid = OSAL_CPU_TO_LE16(p_vf->opaque_fid);
110
111         switch (p_hwfn->hw_info.personality) {
112         case ECORE_PCI_ETH:
113                 p_ramrod->personality = PERSONALITY_ETH;
114                 break;
115         case ECORE_PCI_ETH_ROCE:
116         case ECORE_PCI_ETH_IWARP:
117                 p_ramrod->personality = PERSONALITY_RDMA_AND_ETH;
118                 break;
119         default:
120                 DP_NOTICE(p_hwfn, true, "Unknown VF personality %d\n",
121                           p_hwfn->hw_info.personality);
122                 return ECORE_INVAL;
123         }
124
125         fp_minor = p_vf->acquire.vfdev_info.eth_fp_hsi_minor;
126         if (fp_minor > ETH_HSI_VER_MINOR &&
127             fp_minor != ETH_HSI_VER_NO_PKT_LEN_TUNN) {
128                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
129                            "VF [%d] - Requested fp hsi %02x.%02x which is"
130                            " slightly newer than PF's %02x.%02x; Configuring"
131                            " PFs version\n",
132                            p_vf->abs_vf_id,
133                            ETH_HSI_VER_MAJOR, fp_minor,
134                            ETH_HSI_VER_MAJOR, ETH_HSI_VER_MINOR);
135                 fp_minor = ETH_HSI_VER_MINOR;
136         }
137
138         p_ramrod->hsi_fp_ver.major_ver_arr[ETH_VER_KEY] = ETH_HSI_VER_MAJOR;
139         p_ramrod->hsi_fp_ver.minor_ver_arr[ETH_VER_KEY] = fp_minor;
140
141         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
142                    "VF[%d] - Starting using HSI %02x.%02x\n",
143                    p_vf->abs_vf_id, ETH_HSI_VER_MAJOR, fp_minor);
144
145         return ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
146 }
147
148 static enum _ecore_status_t ecore_sp_vf_stop(struct ecore_hwfn *p_hwfn,
149                                              u32 concrete_vfid,
150                                              u16 opaque_vfid)
151 {
152         struct vf_stop_ramrod_data *p_ramrod = OSAL_NULL;
153         struct ecore_spq_entry *p_ent = OSAL_NULL;
154         struct ecore_sp_init_data init_data;
155         enum _ecore_status_t rc = ECORE_NOTIMPL;
156
157         /* Get SPQ entry */
158         OSAL_MEMSET(&init_data, 0, sizeof(init_data));
159         init_data.cid = ecore_spq_get_cid(p_hwfn);
160         init_data.opaque_fid = opaque_vfid;
161         init_data.comp_mode = ECORE_SPQ_MODE_EBLOCK;
162
163         rc = ecore_sp_init_request(p_hwfn, &p_ent,
164                                    COMMON_RAMROD_VF_STOP,
165                                    PROTOCOLID_COMMON, &init_data);
166         if (rc != ECORE_SUCCESS)
167                 return rc;
168
169         p_ramrod = &p_ent->ramrod.vf_stop;
170
171         p_ramrod->vf_id = GET_FIELD(concrete_vfid, PXP_CONCRETE_FID_VFID);
172
173         return ecore_spq_post(p_hwfn, p_ent, OSAL_NULL);
174 }
175
176 bool ecore_iov_is_valid_vfid(struct ecore_hwfn *p_hwfn, int rel_vf_id,
177                              bool b_enabled_only, bool b_non_malicious)
178 {
179         if (!p_hwfn->pf_iov_info) {
180                 DP_NOTICE(p_hwfn->p_dev, true, "No iov info\n");
181                 return false;
182         }
183
184         if ((rel_vf_id >= p_hwfn->p_dev->p_iov_info->total_vfs) ||
185             (rel_vf_id < 0))
186                 return false;
187
188         if ((!p_hwfn->pf_iov_info->vfs_array[rel_vf_id].b_init) &&
189             b_enabled_only)
190                 return false;
191
192         if ((p_hwfn->pf_iov_info->vfs_array[rel_vf_id].b_malicious) &&
193             b_non_malicious)
194                 return false;
195
196         return true;
197 }
198
199 struct ecore_vf_info *ecore_iov_get_vf_info(struct ecore_hwfn *p_hwfn,
200                                             u16 relative_vf_id,
201                                             bool b_enabled_only)
202 {
203         struct ecore_vf_info *vf = OSAL_NULL;
204
205         if (!p_hwfn->pf_iov_info) {
206                 DP_NOTICE(p_hwfn->p_dev, true, "No iov info\n");
207                 return OSAL_NULL;
208         }
209
210         if (ecore_iov_is_valid_vfid(p_hwfn, relative_vf_id,
211                                     b_enabled_only, false))
212                 vf = &p_hwfn->pf_iov_info->vfs_array[relative_vf_id];
213         else
214                 DP_ERR(p_hwfn, "ecore_iov_get_vf_info: VF[%d] is not enabled\n",
215                        relative_vf_id);
216
217         return vf;
218 }
219
220 static struct ecore_queue_cid *
221 ecore_iov_get_vf_rx_queue_cid(struct ecore_vf_queue *p_queue)
222 {
223         int i;
224
225         for (i = 0; i < MAX_QUEUES_PER_QZONE; i++) {
226                 if (p_queue->cids[i].p_cid &&
227                     !p_queue->cids[i].b_is_tx)
228                         return p_queue->cids[i].p_cid;
229         }
230
231         return OSAL_NULL;
232 }
233
234 enum ecore_iov_validate_q_mode {
235         ECORE_IOV_VALIDATE_Q_NA,
236         ECORE_IOV_VALIDATE_Q_ENABLE,
237         ECORE_IOV_VALIDATE_Q_DISABLE,
238 };
239
240 static bool ecore_iov_validate_queue_mode(struct ecore_vf_info *p_vf,
241                                           u16 qid,
242                                           enum ecore_iov_validate_q_mode mode,
243                                           bool b_is_tx)
244 {
245         int i;
246
247         if (mode == ECORE_IOV_VALIDATE_Q_NA)
248                 return true;
249
250         for (i = 0; i < MAX_QUEUES_PER_QZONE; i++) {
251                 struct ecore_vf_queue_cid *p_qcid;
252
253                 p_qcid = &p_vf->vf_queues[qid].cids[i];
254
255                 if (p_qcid->p_cid == OSAL_NULL)
256                         continue;
257
258                 if (p_qcid->b_is_tx != b_is_tx)
259                         continue;
260
261                 /* Found. It's enabled. */
262                 return (mode == ECORE_IOV_VALIDATE_Q_ENABLE);
263         }
264
265         /* In case we haven't found any valid cid, then its disabled */
266         return (mode == ECORE_IOV_VALIDATE_Q_DISABLE);
267 }
268
269 static bool ecore_iov_validate_rxq(struct ecore_hwfn *p_hwfn,
270                                    struct ecore_vf_info *p_vf,
271                                    u16 rx_qid,
272                                    enum ecore_iov_validate_q_mode mode)
273 {
274         if (rx_qid >= p_vf->num_rxqs) {
275                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
276                            "VF[0x%02x] - can't touch Rx queue[%04x];"
277                            " Only 0x%04x are allocated\n",
278                            p_vf->abs_vf_id, rx_qid, p_vf->num_rxqs);
279                 return false;
280         }
281
282         return ecore_iov_validate_queue_mode(p_vf, rx_qid, mode, false);
283 }
284
285 static bool ecore_iov_validate_txq(struct ecore_hwfn *p_hwfn,
286                                    struct ecore_vf_info *p_vf,
287                                    u16 tx_qid,
288                                    enum ecore_iov_validate_q_mode mode)
289 {
290         if (tx_qid >= p_vf->num_txqs) {
291                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
292                            "VF[0x%02x] - can't touch Tx queue[%04x];"
293                            " Only 0x%04x are allocated\n",
294                            p_vf->abs_vf_id, tx_qid, p_vf->num_txqs);
295                 return false;
296         }
297
298         return ecore_iov_validate_queue_mode(p_vf, tx_qid, mode, true);
299 }
300
301 static bool ecore_iov_validate_sb(struct ecore_hwfn *p_hwfn,
302                                   struct ecore_vf_info *p_vf,
303                                   u16 sb_idx)
304 {
305         int i;
306
307         for (i = 0; i < p_vf->num_sbs; i++)
308                 if (p_vf->igu_sbs[i] == sb_idx)
309                         return true;
310
311         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
312                    "VF[0%02x] - tried using sb_idx %04x which doesn't exist as"
313                    " one of its 0x%02x SBs\n",
314                    p_vf->abs_vf_id, sb_idx, p_vf->num_sbs);
315
316         return false;
317 }
318
319 /* Is there at least 1 queue open? */
320 static bool ecore_iov_validate_active_rxq(struct ecore_vf_info *p_vf)
321 {
322         u8 i;
323
324         for (i = 0; i < p_vf->num_rxqs; i++)
325                 if (ecore_iov_validate_queue_mode(p_vf, i,
326                                                   ECORE_IOV_VALIDATE_Q_ENABLE,
327                                                   false))
328                         return true;
329
330         return false;
331 }
332
333 static bool ecore_iov_validate_active_txq(struct ecore_vf_info *p_vf)
334 {
335         u8 i;
336
337         for (i = 0; i < p_vf->num_txqs; i++)
338                 if (ecore_iov_validate_queue_mode(p_vf, i,
339                                                   ECORE_IOV_VALIDATE_Q_ENABLE,
340                                                   true))
341                         return true;
342
343         return false;
344 }
345
346 enum _ecore_status_t ecore_iov_post_vf_bulletin(struct ecore_hwfn *p_hwfn,
347                                                 int vfid,
348                                                 struct ecore_ptt *p_ptt)
349 {
350         struct ecore_bulletin_content *p_bulletin;
351         int crc_size = sizeof(p_bulletin->crc);
352         struct ecore_dmae_params params;
353         struct ecore_vf_info *p_vf;
354
355         p_vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
356         if (!p_vf)
357                 return ECORE_INVAL;
358
359         /* TODO - check VF is in a state where it can accept message */
360         if (!p_vf->vf_bulletin)
361                 return ECORE_INVAL;
362
363         p_bulletin = p_vf->bulletin.p_virt;
364
365         /* Increment bulletin board version and compute crc */
366         p_bulletin->version++;
367         p_bulletin->crc = OSAL_CRC32(0, (u8 *)p_bulletin + crc_size,
368                                      p_vf->bulletin.size - crc_size);
369
370         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
371                    "Posting Bulletin 0x%08x to VF[%d] (CRC 0x%08x)\n",
372                    p_bulletin->version, p_vf->relative_vf_id, p_bulletin->crc);
373
374         /* propagate bulletin board via dmae to vm memory */
375         OSAL_MEMSET(&params, 0, sizeof(params));
376         params.flags = ECORE_DMAE_FLAG_VF_DST;
377         params.dst_vfid = p_vf->abs_vf_id;
378         return ecore_dmae_host2host(p_hwfn, p_ptt, p_vf->bulletin.phys,
379                                     p_vf->vf_bulletin, p_vf->bulletin.size / 4,
380                                     &params);
381 }
382
383 static enum _ecore_status_t ecore_iov_pci_cfg_info(struct ecore_dev *p_dev)
384 {
385         struct ecore_hw_sriov_info *iov = p_dev->p_iov_info;
386         int pos = iov->pos;
387
388         DP_VERBOSE(p_dev, ECORE_MSG_IOV, "sriov ext pos %d\n", pos);
389         OSAL_PCI_READ_CONFIG_WORD(p_dev, pos + PCI_SRIOV_CTRL, &iov->ctrl);
390
391         OSAL_PCI_READ_CONFIG_WORD(p_dev,
392                                   pos + PCI_SRIOV_TOTAL_VF, &iov->total_vfs);
393         OSAL_PCI_READ_CONFIG_WORD(p_dev,
394                                   pos + PCI_SRIOV_INITIAL_VF,
395                                   &iov->initial_vfs);
396
397         OSAL_PCI_READ_CONFIG_WORD(p_dev, pos + PCI_SRIOV_NUM_VF, &iov->num_vfs);
398         if (iov->num_vfs) {
399                 /* @@@TODO - in future we might want to add an OSAL here to
400                  * allow each OS to decide on its own how to act.
401                  */
402                 DP_VERBOSE(p_dev, ECORE_MSG_IOV,
403                            "Number of VFs are already set to non-zero value."
404                            " Ignoring PCI configuration value\n");
405                 iov->num_vfs = 0;
406         }
407
408         OSAL_PCI_READ_CONFIG_WORD(p_dev,
409                                   pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
410
411         OSAL_PCI_READ_CONFIG_WORD(p_dev,
412                                   pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
413
414         OSAL_PCI_READ_CONFIG_WORD(p_dev,
415                                   pos + PCI_SRIOV_VF_DID, &iov->vf_device_id);
416
417         OSAL_PCI_READ_CONFIG_DWORD(p_dev,
418                                    pos + PCI_SRIOV_SUP_PGSIZE, &iov->pgsz);
419
420         OSAL_PCI_READ_CONFIG_DWORD(p_dev, pos + PCI_SRIOV_CAP, &iov->cap);
421
422         OSAL_PCI_READ_CONFIG_BYTE(p_dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
423
424         DP_VERBOSE(p_dev, ECORE_MSG_IOV, "IOV info: nres %d, cap 0x%x,"
425                    "ctrl 0x%x, total %d, initial %d, num vfs %d, offset %d,"
426                    " stride %d, page size 0x%x\n",
427                    iov->nres, iov->cap, iov->ctrl,
428                    iov->total_vfs, iov->initial_vfs, iov->nr_virtfn,
429                    iov->offset, iov->stride, iov->pgsz);
430
431         /* Some sanity checks */
432         if (iov->num_vfs > NUM_OF_VFS(p_dev) ||
433             iov->total_vfs > NUM_OF_VFS(p_dev)) {
434                 /* This can happen only due to a bug. In this case we set
435                  * num_vfs to zero to avoid memory corruption in the code that
436                  * assumes max number of vfs
437                  */
438                 DP_NOTICE(p_dev, false,
439                           "IOV: Unexpected number of vfs set: %d"
440                           " setting num_vf to zero\n",
441                           iov->num_vfs);
442
443                 iov->num_vfs = 0;
444                 iov->total_vfs = 0;
445         }
446
447         return ECORE_SUCCESS;
448 }
449
450 static void ecore_iov_setup_vfdb(struct ecore_hwfn *p_hwfn)
451 {
452         struct ecore_hw_sriov_info *p_iov = p_hwfn->p_dev->p_iov_info;
453         struct ecore_pf_iov *p_iov_info = p_hwfn->pf_iov_info;
454         struct ecore_bulletin_content *p_bulletin_virt;
455         dma_addr_t req_p, rply_p, bulletin_p;
456         union pfvf_tlvs *p_reply_virt_addr;
457         union vfpf_tlvs *p_req_virt_addr;
458         u8 idx = 0;
459
460         OSAL_MEMSET(p_iov_info->vfs_array, 0, sizeof(p_iov_info->vfs_array));
461
462         p_req_virt_addr = p_iov_info->mbx_msg_virt_addr;
463         req_p = p_iov_info->mbx_msg_phys_addr;
464         p_reply_virt_addr = p_iov_info->mbx_reply_virt_addr;
465         rply_p = p_iov_info->mbx_reply_phys_addr;
466         p_bulletin_virt = p_iov_info->p_bulletins;
467         bulletin_p = p_iov_info->bulletins_phys;
468         if (!p_req_virt_addr || !p_reply_virt_addr || !p_bulletin_virt) {
469                 DP_ERR(p_hwfn,
470                        "ecore_iov_setup_vfdb called without alloc mem first\n");
471                 return;
472         }
473
474         for (idx = 0; idx < p_iov->total_vfs; idx++) {
475                 struct ecore_vf_info *vf = &p_iov_info->vfs_array[idx];
476                 u32 concrete;
477
478                 vf->vf_mbx.req_virt = p_req_virt_addr + idx;
479                 vf->vf_mbx.req_phys = req_p + idx * sizeof(union vfpf_tlvs);
480                 vf->vf_mbx.reply_virt = p_reply_virt_addr + idx;
481                 vf->vf_mbx.reply_phys = rply_p + idx * sizeof(union pfvf_tlvs);
482
483 #ifdef CONFIG_ECORE_SW_CHANNEL
484                 vf->vf_mbx.sw_mbx.request_size = sizeof(union vfpf_tlvs);
485                 vf->vf_mbx.sw_mbx.mbx_state = VF_PF_WAIT_FOR_START_REQUEST;
486 #endif
487                 vf->state = VF_STOPPED;
488                 vf->b_init = false;
489
490                 vf->bulletin.phys = idx *
491                     sizeof(struct ecore_bulletin_content) + bulletin_p;
492                 vf->bulletin.p_virt = p_bulletin_virt + idx;
493                 vf->bulletin.size = sizeof(struct ecore_bulletin_content);
494
495                 vf->relative_vf_id = idx;
496                 vf->abs_vf_id = idx + p_iov->first_vf_in_pf;
497                 concrete = ecore_vfid_to_concrete(p_hwfn, vf->abs_vf_id);
498                 vf->concrete_fid = concrete;
499                 /* TODO - need to devise a better way of getting opaque */
500                 vf->opaque_fid = (p_hwfn->hw_info.opaque_fid & 0xff) |
501                     (vf->abs_vf_id << 8);
502
503                 vf->num_mac_filters = ECORE_ETH_VF_NUM_MAC_FILTERS;
504                 vf->num_vlan_filters = ECORE_ETH_VF_NUM_VLAN_FILTERS;
505         }
506 }
507
508 static enum _ecore_status_t ecore_iov_allocate_vfdb(struct ecore_hwfn *p_hwfn)
509 {
510         struct ecore_pf_iov *p_iov_info = p_hwfn->pf_iov_info;
511         void **p_v_addr;
512         u16 num_vfs = 0;
513
514         num_vfs = p_hwfn->p_dev->p_iov_info->total_vfs;
515
516         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
517                    "ecore_iov_allocate_vfdb for %d VFs\n", num_vfs);
518
519         /* Allocate PF Mailbox buffer (per-VF) */
520         p_iov_info->mbx_msg_size = sizeof(union vfpf_tlvs) * num_vfs;
521         p_v_addr = &p_iov_info->mbx_msg_virt_addr;
522         *p_v_addr = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev,
523                                             &p_iov_info->mbx_msg_phys_addr,
524                                             p_iov_info->mbx_msg_size);
525         if (!*p_v_addr)
526                 return ECORE_NOMEM;
527
528         /* Allocate PF Mailbox Reply buffer (per-VF) */
529         p_iov_info->mbx_reply_size = sizeof(union pfvf_tlvs) * num_vfs;
530         p_v_addr = &p_iov_info->mbx_reply_virt_addr;
531         *p_v_addr = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev,
532                                             &p_iov_info->mbx_reply_phys_addr,
533                                             p_iov_info->mbx_reply_size);
534         if (!*p_v_addr)
535                 return ECORE_NOMEM;
536
537         p_iov_info->bulletins_size = sizeof(struct ecore_bulletin_content) *
538             num_vfs;
539         p_v_addr = &p_iov_info->p_bulletins;
540         *p_v_addr = OSAL_DMA_ALLOC_COHERENT(p_hwfn->p_dev,
541                                             &p_iov_info->bulletins_phys,
542                                             p_iov_info->bulletins_size);
543         if (!*p_v_addr)
544                 return ECORE_NOMEM;
545
546         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
547                    "PF's Requests mailbox [%p virt 0x%lx phys],  "
548                    "Response mailbox [%p virt 0x%lx phys] Bulletinsi"
549                    " [%p virt 0x%lx phys]\n",
550                    p_iov_info->mbx_msg_virt_addr,
551                    (unsigned long)p_iov_info->mbx_msg_phys_addr,
552                    p_iov_info->mbx_reply_virt_addr,
553                    (unsigned long)p_iov_info->mbx_reply_phys_addr,
554                    p_iov_info->p_bulletins,
555                    (unsigned long)p_iov_info->bulletins_phys);
556
557         return ECORE_SUCCESS;
558 }
559
560 static void ecore_iov_free_vfdb(struct ecore_hwfn *p_hwfn)
561 {
562         struct ecore_pf_iov *p_iov_info = p_hwfn->pf_iov_info;
563
564         if (p_hwfn->pf_iov_info->mbx_msg_virt_addr)
565                 OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
566                                        p_iov_info->mbx_msg_virt_addr,
567                                        p_iov_info->mbx_msg_phys_addr,
568                                        p_iov_info->mbx_msg_size);
569
570         if (p_hwfn->pf_iov_info->mbx_reply_virt_addr)
571                 OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
572                                        p_iov_info->mbx_reply_virt_addr,
573                                        p_iov_info->mbx_reply_phys_addr,
574                                        p_iov_info->mbx_reply_size);
575
576         if (p_iov_info->p_bulletins)
577                 OSAL_DMA_FREE_COHERENT(p_hwfn->p_dev,
578                                        p_iov_info->p_bulletins,
579                                        p_iov_info->bulletins_phys,
580                                        p_iov_info->bulletins_size);
581 }
582
583 enum _ecore_status_t ecore_iov_alloc(struct ecore_hwfn *p_hwfn)
584 {
585         struct ecore_pf_iov *p_sriov;
586
587         if (!IS_PF_SRIOV(p_hwfn)) {
588                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
589                            "No SR-IOV - no need for IOV db\n");
590                 return ECORE_SUCCESS;
591         }
592
593         p_sriov = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL, sizeof(*p_sriov));
594         if (!p_sriov) {
595                 DP_NOTICE(p_hwfn, true,
596                           "Failed to allocate `struct ecore_sriov'\n");
597                 return ECORE_NOMEM;
598         }
599
600         p_hwfn->pf_iov_info = p_sriov;
601
602         ecore_spq_register_async_cb(p_hwfn, PROTOCOLID_COMMON,
603                                     ecore_sriov_eqe_event);
604
605         return ecore_iov_allocate_vfdb(p_hwfn);
606 }
607
608 void ecore_iov_setup(struct ecore_hwfn *p_hwfn)
609 {
610         if (!IS_PF_SRIOV(p_hwfn) || !IS_PF_SRIOV_ALLOC(p_hwfn))
611                 return;
612
613         ecore_iov_setup_vfdb(p_hwfn);
614 }
615
616 void ecore_iov_free(struct ecore_hwfn *p_hwfn)
617 {
618         ecore_spq_unregister_async_cb(p_hwfn, PROTOCOLID_COMMON);
619
620         if (IS_PF_SRIOV_ALLOC(p_hwfn)) {
621                 ecore_iov_free_vfdb(p_hwfn);
622                 OSAL_FREE(p_hwfn->p_dev, p_hwfn->pf_iov_info);
623         }
624 }
625
626 void ecore_iov_free_hw_info(struct ecore_dev *p_dev)
627 {
628         OSAL_FREE(p_dev, p_dev->p_iov_info);
629 }
630
631 enum _ecore_status_t ecore_iov_hw_info(struct ecore_hwfn *p_hwfn)
632 {
633         struct ecore_dev *p_dev = p_hwfn->p_dev;
634         int pos;
635         enum _ecore_status_t rc;
636
637         if (IS_VF(p_hwfn->p_dev))
638                 return ECORE_SUCCESS;
639
640         /* Learn the PCI configuration */
641         pos = OSAL_PCI_FIND_EXT_CAPABILITY(p_hwfn->p_dev,
642                                            PCI_EXT_CAP_ID_SRIOV);
643         if (!pos) {
644                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV, "No PCIe IOV support\n");
645                 return ECORE_SUCCESS;
646         }
647
648         /* Allocate a new struct for IOV information */
649         /* TODO - can change to VALLOC when its available */
650         p_dev->p_iov_info = OSAL_ZALLOC(p_dev, GFP_KERNEL,
651                                         sizeof(*p_dev->p_iov_info));
652         if (!p_dev->p_iov_info) {
653                 DP_NOTICE(p_hwfn, true,
654                           "Can't support IOV due to lack of memory\n");
655                 return ECORE_NOMEM;
656         }
657         p_dev->p_iov_info->pos = pos;
658
659         rc = ecore_iov_pci_cfg_info(p_dev);
660         if (rc)
661                 return rc;
662
663         /* We want PF IOV to be synonemous with the existence of p_iov_info;
664          * In case the capability is published but there are no VFs, simply
665          * de-allocate the struct.
666          */
667         if (!p_dev->p_iov_info->total_vfs) {
668                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
669                            "IOV capabilities, but no VFs are published\n");
670                 OSAL_FREE(p_dev, p_dev->p_iov_info);
671                 return ECORE_SUCCESS;
672         }
673
674         /* First VF index based on offset is tricky:
675          *  - If ARI is supported [likely], offset - (16 - pf_id) would
676          *    provide the number for eng0. 2nd engine Vfs would begin
677          *    after the first engine's VFs.
678          *  - If !ARI, VFs would start on next device.
679          *    so offset - (256 - pf_id) would provide the number.
680          * Utilize the fact that (256 - pf_id) is achieved only be later
681          * to diffrentiate between the two.
682          */
683
684         if (p_hwfn->p_dev->p_iov_info->offset < (256 - p_hwfn->abs_pf_id)) {
685                 u32 first = p_hwfn->p_dev->p_iov_info->offset +
686                             p_hwfn->abs_pf_id - 16;
687
688                 p_dev->p_iov_info->first_vf_in_pf = first;
689
690                 if (ECORE_PATH_ID(p_hwfn))
691                         p_dev->p_iov_info->first_vf_in_pf -= MAX_NUM_VFS_BB;
692         } else {
693                 u32 first = p_hwfn->p_dev->p_iov_info->offset +
694                             p_hwfn->abs_pf_id - 256;
695
696                 p_dev->p_iov_info->first_vf_in_pf = first;
697         }
698
699         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
700                    "First VF in hwfn 0x%08x\n",
701                    p_dev->p_iov_info->first_vf_in_pf);
702
703         return ECORE_SUCCESS;
704 }
705
706 static bool _ecore_iov_pf_sanity_check(struct ecore_hwfn *p_hwfn, int vfid,
707                                        bool b_fail_malicious)
708 {
709         /* Check PF supports sriov */
710         if (IS_VF(p_hwfn->p_dev) || !IS_ECORE_SRIOV(p_hwfn->p_dev) ||
711             !IS_PF_SRIOV_ALLOC(p_hwfn))
712                 return false;
713
714         /* Check VF validity */
715         if (!ecore_iov_is_valid_vfid(p_hwfn, vfid, true, b_fail_malicious))
716                 return false;
717
718         return true;
719 }
720
721 bool ecore_iov_pf_sanity_check(struct ecore_hwfn *p_hwfn, int vfid)
722 {
723         return _ecore_iov_pf_sanity_check(p_hwfn, vfid, true);
724 }
725
726 void ecore_iov_set_vf_to_disable(struct ecore_dev *p_dev,
727                                  u16 rel_vf_id, u8 to_disable)
728 {
729         struct ecore_vf_info *vf;
730         int i;
731
732         for_each_hwfn(p_dev, i) {
733                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
734
735                 vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, false);
736                 if (!vf)
737                         continue;
738
739                 vf->to_disable = to_disable;
740         }
741 }
742
743 void ecore_iov_set_vfs_to_disable(struct ecore_dev *p_dev,
744                                   u8 to_disable)
745 {
746         u16 i;
747
748         if (!IS_ECORE_SRIOV(p_dev))
749                 return;
750
751         for (i = 0; i < p_dev->p_iov_info->total_vfs; i++)
752                 ecore_iov_set_vf_to_disable(p_dev, i, to_disable);
753 }
754
755 #ifndef LINUX_REMOVE
756 /* @@@TBD Consider taking outside of ecore... */
757 enum _ecore_status_t ecore_iov_set_vf_ctx(struct ecore_hwfn *p_hwfn,
758                                           u16               vf_id,
759                                           void              *ctx)
760 {
761         enum _ecore_status_t rc = ECORE_SUCCESS;
762         struct ecore_vf_info *vf = ecore_iov_get_vf_info(p_hwfn, vf_id, true);
763
764         if (vf != OSAL_NULL) {
765                 vf->ctx = ctx;
766 #ifdef CONFIG_ECORE_SW_CHANNEL
767                 vf->vf_mbx.sw_mbx.mbx_state = VF_PF_WAIT_FOR_START_REQUEST;
768 #endif
769         } else {
770                 rc = ECORE_UNKNOWN_ERROR;
771         }
772         return rc;
773 }
774 #endif
775
776 static void ecore_iov_vf_pglue_clear_err(struct ecore_hwfn      *p_hwfn,
777                                          struct ecore_ptt       *p_ptt,
778                                          u8                     abs_vfid)
779 {
780         ecore_wr(p_hwfn, p_ptt,
781                  PGLUE_B_REG_WAS_ERROR_VF_31_0_CLR + (abs_vfid >> 5) * 4,
782                  1 << (abs_vfid & 0x1f));
783 }
784
785 static void ecore_iov_vf_igu_reset(struct ecore_hwfn *p_hwfn,
786                                    struct ecore_ptt *p_ptt,
787                                    struct ecore_vf_info *vf)
788 {
789         int i;
790
791         /* Set VF masks and configuration - pretend */
792         ecore_fid_pretend(p_hwfn, p_ptt, (u16)vf->concrete_fid);
793
794         ecore_wr(p_hwfn, p_ptt, IGU_REG_STATISTIC_NUM_VF_MSG_SENT, 0);
795
796         /* unpretend */
797         ecore_fid_pretend(p_hwfn, p_ptt, (u16)p_hwfn->hw_info.concrete_fid);
798
799         /* iterate over all queues, clear sb consumer */
800         for (i = 0; i < vf->num_sbs; i++)
801                 ecore_int_igu_init_pure_rt_single(p_hwfn, p_ptt,
802                                                   vf->igu_sbs[i],
803                                                   vf->opaque_fid, true);
804 }
805
806 static void ecore_iov_vf_igu_set_int(struct ecore_hwfn *p_hwfn,
807                                      struct ecore_ptt *p_ptt,
808                                      struct ecore_vf_info *vf, bool enable)
809 {
810         u32 igu_vf_conf;
811
812         ecore_fid_pretend(p_hwfn, p_ptt, (u16)vf->concrete_fid);
813
814         igu_vf_conf = ecore_rd(p_hwfn, p_ptt, IGU_REG_VF_CONFIGURATION);
815
816         if (enable)
817                 igu_vf_conf |= IGU_VF_CONF_MSI_MSIX_EN;
818         else
819                 igu_vf_conf &= ~IGU_VF_CONF_MSI_MSIX_EN;
820
821         ecore_wr(p_hwfn, p_ptt, IGU_REG_VF_CONFIGURATION, igu_vf_conf);
822
823         /* unpretend */
824         ecore_fid_pretend(p_hwfn, p_ptt, (u16)p_hwfn->hw_info.concrete_fid);
825 }
826
827 static enum _ecore_status_t
828 ecore_iov_enable_vf_access_msix(struct ecore_hwfn *p_hwfn,
829                                 struct ecore_ptt *p_ptt,
830                                 u8 abs_vf_id,
831                                 u8 num_sbs)
832 {
833         u8 current_max = 0;
834         int i;
835
836         /* If client overrides this, don't do anything */
837         if (p_hwfn->p_dev->b_dont_override_vf_msix)
838                 return ECORE_SUCCESS;
839
840         /* For AH onward, configuration is per-PF. Find maximum of all
841          * the currently enabled child VFs, and set the number to be that.
842          */
843         if (!ECORE_IS_BB(p_hwfn->p_dev)) {
844                 ecore_for_each_vf(p_hwfn, i) {
845                         struct ecore_vf_info *p_vf;
846
847                         p_vf  = ecore_iov_get_vf_info(p_hwfn, (u16)i, true);
848                         if (!p_vf)
849                                 continue;
850
851                         current_max = OSAL_MAX_T(u8, current_max,
852                                                  p_vf->num_sbs);
853                 }
854         }
855
856         if (num_sbs > current_max)
857                 return ecore_mcp_config_vf_msix(p_hwfn, p_ptt,
858                                                 abs_vf_id, num_sbs);
859
860         return ECORE_SUCCESS;
861 }
862
863 static enum _ecore_status_t
864 ecore_iov_enable_vf_access(struct ecore_hwfn *p_hwfn,
865                            struct ecore_ptt *p_ptt, struct ecore_vf_info *vf)
866 {
867         u32 igu_vf_conf = IGU_VF_CONF_FUNC_EN;
868         enum _ecore_status_t rc = ECORE_SUCCESS;
869
870         /* It's possible VF was previously considered malicious -
871          * clear the indication even if we're only going to disable VF.
872          */
873         vf->b_malicious = false;
874
875         if (vf->to_disable)
876                 return ECORE_SUCCESS;
877
878         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
879                    "Enable internal access for vf %x [abs %x]\n", vf->abs_vf_id,
880                    ECORE_VF_ABS_ID(p_hwfn, vf));
881
882         ecore_iov_vf_pglue_clear_err(p_hwfn, p_ptt,
883                                      ECORE_VF_ABS_ID(p_hwfn, vf));
884
885         ecore_iov_vf_igu_reset(p_hwfn, p_ptt, vf);
886
887         rc = ecore_iov_enable_vf_access_msix(p_hwfn, p_ptt,
888                                              vf->abs_vf_id, vf->num_sbs);
889         if (rc != ECORE_SUCCESS)
890                 return rc;
891
892         ecore_fid_pretend(p_hwfn, p_ptt, (u16)vf->concrete_fid);
893
894         SET_FIELD(igu_vf_conf, IGU_VF_CONF_PARENT, p_hwfn->rel_pf_id);
895         STORE_RT_REG(p_hwfn, IGU_REG_VF_CONFIGURATION_RT_OFFSET, igu_vf_conf);
896
897         ecore_init_run(p_hwfn, p_ptt, PHASE_VF, vf->abs_vf_id,
898                        p_hwfn->hw_info.hw_mode);
899
900         /* unpretend */
901         ecore_fid_pretend(p_hwfn, p_ptt, (u16)p_hwfn->hw_info.concrete_fid);
902
903         vf->state = VF_FREE;
904
905         return rc;
906 }
907
908 /**
909  *
910  * @brief ecore_iov_config_perm_table - configure the permission
911  *      zone table.
912  *      In E4, queue zone permission table size is 320x9. There
913  *      are 320 VF queues for single engine device (256 for dual
914  *      engine device), and each entry has the following format:
915  *      {Valid, VF[7:0]}
916  * @param p_hwfn
917  * @param p_ptt
918  * @param vf
919  * @param enable
920  */
921 static void ecore_iov_config_perm_table(struct ecore_hwfn *p_hwfn,
922                                         struct ecore_ptt *p_ptt,
923                                         struct ecore_vf_info *vf, u8 enable)
924 {
925         u32 reg_addr, val;
926         u16 qzone_id = 0;
927         int qid;
928
929         for (qid = 0; qid < vf->num_rxqs; qid++) {
930                 ecore_fw_l2_queue(p_hwfn, vf->vf_queues[qid].fw_rx_qid,
931                                   &qzone_id);
932
933                 reg_addr = PSWHST_REG_ZONE_PERMISSION_TABLE + qzone_id * 4;
934                 val = enable ? (vf->abs_vf_id | (1 << 8)) : 0;
935                 ecore_wr(p_hwfn, p_ptt, reg_addr, val);
936         }
937 }
938
939 static void ecore_iov_enable_vf_traffic(struct ecore_hwfn *p_hwfn,
940                                         struct ecore_ptt *p_ptt,
941                                         struct ecore_vf_info *vf)
942 {
943         /* Reset vf in IGU - interrupts are still disabled */
944         ecore_iov_vf_igu_reset(p_hwfn, p_ptt, vf);
945
946         ecore_iov_vf_igu_set_int(p_hwfn, p_ptt, vf, 1);
947
948         /* Permission Table */
949         ecore_iov_config_perm_table(p_hwfn, p_ptt, vf, true);
950 }
951
952 static u8 ecore_iov_alloc_vf_igu_sbs(struct ecore_hwfn *p_hwfn,
953                                      struct ecore_ptt *p_ptt,
954                                      struct ecore_vf_info *vf,
955                                      u16 num_rx_queues)
956 {
957         struct ecore_igu_block *p_block;
958         struct cau_sb_entry sb_entry;
959         int qid = 0;
960         u32 val = 0;
961
962         if (num_rx_queues > p_hwfn->hw_info.p_igu_info->usage.free_cnt_iov)
963                 num_rx_queues =
964                 (u16)p_hwfn->hw_info.p_igu_info->usage.free_cnt_iov;
965         p_hwfn->hw_info.p_igu_info->usage.free_cnt_iov -= num_rx_queues;
966
967         SET_FIELD(val, IGU_MAPPING_LINE_FUNCTION_NUMBER, vf->abs_vf_id);
968         SET_FIELD(val, IGU_MAPPING_LINE_VALID, 1);
969         SET_FIELD(val, IGU_MAPPING_LINE_PF_VALID, 0);
970
971         for (qid = 0; qid < num_rx_queues; qid++) {
972                 p_block = ecore_get_igu_free_sb(p_hwfn, false);
973                 vf->igu_sbs[qid] = p_block->igu_sb_id;
974                 p_block->status &= ~ECORE_IGU_STATUS_FREE;
975                 SET_FIELD(val, IGU_MAPPING_LINE_VECTOR_NUMBER, qid);
976
977                 ecore_wr(p_hwfn, p_ptt,
978                          IGU_REG_MAPPING_MEMORY +
979                          sizeof(u32) * p_block->igu_sb_id, val);
980
981                 /* Configure igu sb in CAU which were marked valid */
982                 ecore_init_cau_sb_entry(p_hwfn, &sb_entry,
983                                         p_hwfn->rel_pf_id,
984                                         vf->abs_vf_id, 1);
985                 ecore_dmae_host2grc(p_hwfn, p_ptt,
986                                     (u64)(osal_uintptr_t)&sb_entry,
987                                     CAU_REG_SB_VAR_MEMORY +
988                                     p_block->igu_sb_id * sizeof(u64), 2, 0);
989         }
990
991         vf->num_sbs = (u8)num_rx_queues;
992
993         return vf->num_sbs;
994 }
995
996 /**
997  *
998  * @brief The function invalidates all the VF entries,
999  *        technically this isn't required, but added for
1000  *        cleaness and ease of debugging incase a VF attempts to
1001  *        produce an interrupt after it has been taken down.
1002  *
1003  * @param p_hwfn
1004  * @param p_ptt
1005  * @param vf
1006  */
1007 static void ecore_iov_free_vf_igu_sbs(struct ecore_hwfn *p_hwfn,
1008                                       struct ecore_ptt *p_ptt,
1009                                       struct ecore_vf_info *vf)
1010 {
1011         struct ecore_igu_info *p_info = p_hwfn->hw_info.p_igu_info;
1012         int idx, igu_id;
1013         u32 addr, val;
1014
1015         /* Invalidate igu CAM lines and mark them as free */
1016         for (idx = 0; idx < vf->num_sbs; idx++) {
1017                 igu_id = vf->igu_sbs[idx];
1018                 addr = IGU_REG_MAPPING_MEMORY + sizeof(u32) * igu_id;
1019
1020                 val = ecore_rd(p_hwfn, p_ptt, addr);
1021                 SET_FIELD(val, IGU_MAPPING_LINE_VALID, 0);
1022                 ecore_wr(p_hwfn, p_ptt, addr, val);
1023
1024                 p_info->entry[igu_id].status |= ECORE_IGU_STATUS_FREE;
1025                 p_hwfn->hw_info.p_igu_info->usage.free_cnt_iov++;
1026         }
1027
1028         vf->num_sbs = 0;
1029 }
1030
1031 void ecore_iov_set_link(struct ecore_hwfn *p_hwfn,
1032                         u16 vfid,
1033                         struct ecore_mcp_link_params *params,
1034                         struct ecore_mcp_link_state *link,
1035                         struct ecore_mcp_link_capabilities *p_caps)
1036 {
1037         struct ecore_vf_info *p_vf = ecore_iov_get_vf_info(p_hwfn, vfid, false);
1038         struct ecore_bulletin_content *p_bulletin;
1039
1040         if (!p_vf)
1041                 return;
1042
1043         p_bulletin = p_vf->bulletin.p_virt;
1044         p_bulletin->req_autoneg = params->speed.autoneg;
1045         p_bulletin->req_adv_speed = params->speed.advertised_speeds;
1046         p_bulletin->req_forced_speed = params->speed.forced_speed;
1047         p_bulletin->req_autoneg_pause = params->pause.autoneg;
1048         p_bulletin->req_forced_rx = params->pause.forced_rx;
1049         p_bulletin->req_forced_tx = params->pause.forced_tx;
1050         p_bulletin->req_loopback = params->loopback_mode;
1051
1052         p_bulletin->link_up = link->link_up;
1053         p_bulletin->speed = link->speed;
1054         p_bulletin->full_duplex = link->full_duplex;
1055         p_bulletin->autoneg = link->an;
1056         p_bulletin->autoneg_complete = link->an_complete;
1057         p_bulletin->parallel_detection = link->parallel_detection;
1058         p_bulletin->pfc_enabled = link->pfc_enabled;
1059         p_bulletin->partner_adv_speed = link->partner_adv_speed;
1060         p_bulletin->partner_tx_flow_ctrl_en = link->partner_tx_flow_ctrl_en;
1061         p_bulletin->partner_rx_flow_ctrl_en = link->partner_rx_flow_ctrl_en;
1062         p_bulletin->partner_adv_pause = link->partner_adv_pause;
1063         p_bulletin->sfp_tx_fault = link->sfp_tx_fault;
1064
1065         p_bulletin->capability_speed = p_caps->speed_capabilities;
1066 }
1067
1068 enum _ecore_status_t
1069 ecore_iov_init_hw_for_vf(struct ecore_hwfn *p_hwfn,
1070                          struct ecore_ptt *p_ptt,
1071                          struct ecore_iov_vf_init_params *p_params)
1072 {
1073         struct ecore_mcp_link_capabilities link_caps;
1074         struct ecore_mcp_link_params link_params;
1075         struct ecore_mcp_link_state link_state;
1076         u8 num_of_vf_available_chains  = 0;
1077         struct ecore_vf_info *vf = OSAL_NULL;
1078         u16 qid, num_irqs;
1079         enum _ecore_status_t rc = ECORE_SUCCESS;
1080         u32 cids;
1081         u8 i;
1082
1083         vf = ecore_iov_get_vf_info(p_hwfn, p_params->rel_vf_id, false);
1084         if (!vf) {
1085                 DP_ERR(p_hwfn, "ecore_iov_init_hw_for_vf : vf is OSAL_NULL\n");
1086                 return ECORE_UNKNOWN_ERROR;
1087         }
1088
1089         if (vf->b_init) {
1090                 DP_NOTICE(p_hwfn, true, "VF[%d] is already active.\n",
1091                           p_params->rel_vf_id);
1092                 return ECORE_INVAL;
1093         }
1094
1095         /* Perform sanity checking on the requested vport/rss */
1096         if (p_params->vport_id >= RESC_NUM(p_hwfn, ECORE_VPORT)) {
1097                 DP_NOTICE(p_hwfn, true, "VF[%d] - can't use VPORT %02x\n",
1098                           p_params->rel_vf_id, p_params->vport_id);
1099                 return ECORE_INVAL;
1100         }
1101
1102         if ((p_params->num_queues > 1) &&
1103             (p_params->rss_eng_id >= RESC_NUM(p_hwfn, ECORE_RSS_ENG))) {
1104                 DP_NOTICE(p_hwfn, true, "VF[%d] - can't use RSS_ENG %02x\n",
1105                           p_params->rel_vf_id, p_params->rss_eng_id);
1106                 return ECORE_INVAL;
1107         }
1108
1109         /* TODO - remove this once we get confidence of change */
1110         if (!p_params->vport_id) {
1111                 DP_NOTICE(p_hwfn, false,
1112                           "VF[%d] - Unlikely that VF uses vport0. Forgotten?\n",
1113                           p_params->rel_vf_id);
1114         }
1115         if ((!p_params->rss_eng_id) && (p_params->num_queues > 1)) {
1116                 DP_NOTICE(p_hwfn, false,
1117                           "VF[%d] - Unlikely that VF uses RSS_eng0. Forgotten?\n",
1118                           p_params->rel_vf_id);
1119         }
1120         vf->vport_id = p_params->vport_id;
1121         vf->rss_eng_id = p_params->rss_eng_id;
1122
1123         /* Since it's possible to relocate SBs, it's a bit difficult to check
1124          * things here. Simply check whether the index falls in the range
1125          * belonging to the PF.
1126          */
1127         for (i = 0; i < p_params->num_queues; i++) {
1128                 qid = p_params->req_rx_queue[i];
1129                 if (qid > (u16)RESC_NUM(p_hwfn, ECORE_L2_QUEUE)) {
1130                         DP_NOTICE(p_hwfn, true,
1131                                   "Can't enable Rx qid [%04x] for VF[%d]: qids [0,,...,0x%04x] available\n",
1132                                   qid, p_params->rel_vf_id,
1133                                   (u16)RESC_NUM(p_hwfn, ECORE_L2_QUEUE));
1134                         return ECORE_INVAL;
1135                 }
1136
1137                 qid = p_params->req_tx_queue[i];
1138                 if (qid > (u16)RESC_NUM(p_hwfn, ECORE_L2_QUEUE)) {
1139                         DP_NOTICE(p_hwfn, true,
1140                                   "Can't enable Tx qid [%04x] for VF[%d]: qids [0,,...,0x%04x] available\n",
1141                                   qid, p_params->rel_vf_id,
1142                                   (u16)RESC_NUM(p_hwfn, ECORE_L2_QUEUE));
1143                         return ECORE_INVAL;
1144                 }
1145         }
1146
1147         /* Limit number of queues according to number of CIDs */
1148         ecore_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_ETH, &cids);
1149         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1150                    "VF[%d] - requesting to initialize for 0x%04x queues"
1151                    " [0x%04x CIDs available]\n",
1152                    vf->relative_vf_id, p_params->num_queues, (u16)cids);
1153         num_irqs = OSAL_MIN_T(u16, p_params->num_queues, ((u16)cids));
1154
1155         num_of_vf_available_chains = ecore_iov_alloc_vf_igu_sbs(p_hwfn,
1156                                                                p_ptt,
1157                                                                vf,
1158                                                                num_irqs);
1159         if (num_of_vf_available_chains == 0) {
1160                 DP_ERR(p_hwfn, "no available igu sbs\n");
1161                 return ECORE_NOMEM;
1162         }
1163
1164         /* Choose queue number and index ranges */
1165         vf->num_rxqs = num_of_vf_available_chains;
1166         vf->num_txqs = num_of_vf_available_chains;
1167
1168         for (i = 0; i < vf->num_rxqs; i++) {
1169                 struct ecore_vf_queue *p_queue = &vf->vf_queues[i];
1170
1171                 p_queue->fw_rx_qid = p_params->req_rx_queue[i];
1172                 p_queue->fw_tx_qid = p_params->req_tx_queue[i];
1173
1174                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1175                            "VF[%d] - Q[%d] SB %04x, qid [Rx %04x Tx %04x]\n",
1176                            vf->relative_vf_id, i, vf->igu_sbs[i],
1177                            p_queue->fw_rx_qid, p_queue->fw_tx_qid);
1178         }
1179
1180         /* Update the link configuration in bulletin.
1181          */
1182         OSAL_MEMCPY(&link_params, ecore_mcp_get_link_params(p_hwfn),
1183                     sizeof(link_params));
1184         OSAL_MEMCPY(&link_state, ecore_mcp_get_link_state(p_hwfn),
1185                     sizeof(link_state));
1186         OSAL_MEMCPY(&link_caps, ecore_mcp_get_link_capabilities(p_hwfn),
1187                     sizeof(link_caps));
1188         ecore_iov_set_link(p_hwfn, p_params->rel_vf_id,
1189                            &link_params, &link_state, &link_caps);
1190
1191         rc = ecore_iov_enable_vf_access(p_hwfn, p_ptt, vf);
1192
1193         if (rc == ECORE_SUCCESS) {
1194                 vf->b_init = true;
1195                 p_hwfn->pf_iov_info->active_vfs[vf->relative_vf_id / 64] |=
1196                         (1ULL << (vf->relative_vf_id % 64));
1197
1198                 if (IS_LEAD_HWFN(p_hwfn))
1199                         p_hwfn->p_dev->p_iov_info->num_vfs++;
1200         }
1201
1202         return rc;
1203 }
1204
1205 enum _ecore_status_t ecore_iov_release_hw_for_vf(struct ecore_hwfn *p_hwfn,
1206                                                  struct ecore_ptt *p_ptt,
1207                                                  u16 rel_vf_id)
1208 {
1209         struct ecore_mcp_link_capabilities caps;
1210         struct ecore_mcp_link_params params;
1211         struct ecore_mcp_link_state link;
1212         struct ecore_vf_info *vf = OSAL_NULL;
1213
1214         vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
1215         if (!vf) {
1216                 DP_ERR(p_hwfn, "ecore_iov_release_hw_for_vf : vf is NULL\n");
1217                 return ECORE_UNKNOWN_ERROR;
1218         }
1219
1220         if (vf->bulletin.p_virt)
1221                 OSAL_MEMSET(vf->bulletin.p_virt, 0,
1222                             sizeof(*vf->bulletin.p_virt));
1223
1224         OSAL_MEMSET(&vf->p_vf_info, 0, sizeof(vf->p_vf_info));
1225
1226         /* Get the link configuration back in bulletin so
1227          * that when VFs are re-enabled they get the actual
1228          * link configuration.
1229          */
1230         OSAL_MEMCPY(&params, ecore_mcp_get_link_params(p_hwfn), sizeof(params));
1231         OSAL_MEMCPY(&link, ecore_mcp_get_link_state(p_hwfn), sizeof(link));
1232         OSAL_MEMCPY(&caps, ecore_mcp_get_link_capabilities(p_hwfn),
1233                     sizeof(caps));
1234         ecore_iov_set_link(p_hwfn, rel_vf_id, &params, &link, &caps);
1235
1236         /* Forget the VF's acquisition message */
1237         OSAL_MEMSET(&vf->acquire, 0, sizeof(vf->acquire));
1238
1239         /* disablng interrupts and resetting permission table was done during
1240          * vf-close, however, we could get here without going through vf_close
1241          */
1242         /* Disable Interrupts for VF */
1243         ecore_iov_vf_igu_set_int(p_hwfn, p_ptt, vf, 0);
1244
1245         /* Reset Permission table */
1246         ecore_iov_config_perm_table(p_hwfn, p_ptt, vf, 0);
1247
1248         vf->num_rxqs = 0;
1249         vf->num_txqs = 0;
1250         ecore_iov_free_vf_igu_sbs(p_hwfn, p_ptt, vf);
1251
1252         if (vf->b_init) {
1253                 vf->b_init = false;
1254                 p_hwfn->pf_iov_info->active_vfs[vf->relative_vf_id / 64] &=
1255                                         ~(1ULL << (vf->relative_vf_id / 64));
1256
1257                 if (IS_LEAD_HWFN(p_hwfn))
1258                         p_hwfn->p_dev->p_iov_info->num_vfs--;
1259         }
1260
1261         return ECORE_SUCCESS;
1262 }
1263
1264 static bool ecore_iov_tlv_supported(u16 tlvtype)
1265 {
1266         return tlvtype > CHANNEL_TLV_NONE && tlvtype < CHANNEL_TLV_MAX;
1267 }
1268
1269 static void ecore_iov_lock_vf_pf_channel(struct ecore_hwfn *p_hwfn,
1270                                          struct ecore_vf_info *vf, u16 tlv)
1271 {
1272         /* lock the channel */
1273         /* mutex_lock(&vf->op_mutex); @@@TBD MichalK - add lock... */
1274
1275         /* record the locking op */
1276         /* vf->op_current = tlv; @@@TBD MichalK */
1277
1278         /* log the lock */
1279         if (ecore_iov_tlv_supported(tlv))
1280                 DP_VERBOSE(p_hwfn,
1281                            ECORE_MSG_IOV,
1282                            "VF[%d]: vf pf channel locked by %s\n",
1283                            vf->abs_vf_id,
1284                            ecore_channel_tlvs_string[tlv]);
1285         else
1286                 DP_VERBOSE(p_hwfn,
1287                            ECORE_MSG_IOV,
1288                            "VF[%d]: vf pf channel locked by %04x\n",
1289                            vf->abs_vf_id, tlv);
1290 }
1291
1292 static void ecore_iov_unlock_vf_pf_channel(struct ecore_hwfn *p_hwfn,
1293                                            struct ecore_vf_info *vf,
1294                                            u16 expected_tlv)
1295 {
1296         /* log the unlock */
1297         if (ecore_iov_tlv_supported(expected_tlv))
1298                 DP_VERBOSE(p_hwfn,
1299                            ECORE_MSG_IOV,
1300                            "VF[%d]: vf pf channel unlocked by %s\n",
1301                            vf->abs_vf_id,
1302                            ecore_channel_tlvs_string[expected_tlv]);
1303         else
1304                 DP_VERBOSE(p_hwfn,
1305                            ECORE_MSG_IOV,
1306                            "VF[%d]: vf pf channel unlocked by %04x\n",
1307                            vf->abs_vf_id, expected_tlv);
1308
1309         /* record the locking op */
1310         /* vf->op_current = CHANNEL_TLV_NONE; */
1311 }
1312
1313 /* place a given tlv on the tlv buffer, continuing current tlv list */
1314 void *ecore_add_tlv(u8 **offset, u16 type, u16 length)
1315 {
1316         struct channel_tlv *tl = (struct channel_tlv *)*offset;
1317
1318         tl->type = type;
1319         tl->length = length;
1320
1321         /* Offset should keep pointing to next TLV (the end of the last) */
1322         *offset += length;
1323
1324         /* Return a pointer to the start of the added tlv */
1325         return *offset - length;
1326 }
1327
1328 /* list the types and lengths of the tlvs on the buffer */
1329 void ecore_dp_tlv_list(struct ecore_hwfn *p_hwfn, void *tlvs_list)
1330 {
1331         u16 i = 1, total_length = 0;
1332         struct channel_tlv *tlv;
1333
1334         do {
1335                 /* cast current tlv list entry to channel tlv header */
1336                 tlv = (struct channel_tlv *)((u8 *)tlvs_list + total_length);
1337
1338                 /* output tlv */
1339                 if (ecore_iov_tlv_supported(tlv->type))
1340                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1341                                    "TLV number %d: type %s, length %d\n",
1342                                    i, ecore_channel_tlvs_string[tlv->type],
1343                                    tlv->length);
1344                 else
1345                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1346                                    "TLV number %d: type %d, length %d\n",
1347                                    i, tlv->type, tlv->length);
1348
1349                 if (tlv->type == CHANNEL_TLV_LIST_END)
1350                         return;
1351
1352                 /* Validate entry - protect against malicious VFs */
1353                 if (!tlv->length) {
1354                         DP_NOTICE(p_hwfn, false, "TLV of length 0 found\n");
1355                         return;
1356                 }
1357                 total_length += tlv->length;
1358                 if (total_length >= sizeof(struct tlv_buffer_size)) {
1359                         DP_NOTICE(p_hwfn, false, "TLV ==> Buffer overflow\n");
1360                         return;
1361                 }
1362
1363                 i++;
1364         } while (1);
1365 }
1366
1367 static void ecore_iov_send_response(struct ecore_hwfn *p_hwfn,
1368                                     struct ecore_ptt *p_ptt,
1369                                     struct ecore_vf_info *p_vf,
1370 #ifdef CONFIG_ECORE_SW_CHANNEL
1371                                     u16 length,
1372 #else
1373                                     u16 OSAL_UNUSED length,
1374 #endif
1375                                     u8 status)
1376 {
1377         struct ecore_iov_vf_mbx *mbx = &p_vf->vf_mbx;
1378         struct ecore_dmae_params params;
1379         u8 eng_vf_id;
1380
1381         mbx->reply_virt->default_resp.hdr.status = status;
1382
1383         ecore_dp_tlv_list(p_hwfn, mbx->reply_virt);
1384
1385 #ifdef CONFIG_ECORE_SW_CHANNEL
1386         mbx->sw_mbx.response_size =
1387             length + sizeof(struct channel_list_end_tlv);
1388
1389         if (!p_vf->b_hw_channel)
1390                 return;
1391 #endif
1392
1393         eng_vf_id = p_vf->abs_vf_id;
1394
1395         OSAL_MEMSET(&params, 0, sizeof(struct ecore_dmae_params));
1396         params.flags = ECORE_DMAE_FLAG_VF_DST;
1397         params.dst_vfid = eng_vf_id;
1398
1399         ecore_dmae_host2host(p_hwfn, p_ptt, mbx->reply_phys + sizeof(u64),
1400                              mbx->req_virt->first_tlv.reply_address +
1401                              sizeof(u64),
1402                              (sizeof(union pfvf_tlvs) - sizeof(u64)) / 4,
1403                              &params);
1404
1405         /* Once PF copies the rc to the VF, the latter can continue and
1406          * and send an additional message. So we have to make sure the
1407          * channel would be re-set to ready prior to that.
1408          */
1409         REG_WR(p_hwfn,
1410                GTT_BAR0_MAP_REG_USDM_RAM +
1411                USTORM_VF_PF_CHANNEL_READY_OFFSET(eng_vf_id), 1);
1412
1413         ecore_dmae_host2host(p_hwfn, p_ptt, mbx->reply_phys,
1414                              mbx->req_virt->first_tlv.reply_address,
1415                              sizeof(u64) / 4, &params);
1416
1417         OSAL_IOV_PF_RESP_TYPE(p_hwfn, p_vf->relative_vf_id, status);
1418 }
1419
1420 static u16 ecore_iov_vport_to_tlv(enum ecore_iov_vport_update_flag flag)
1421 {
1422         switch (flag) {
1423         case ECORE_IOV_VP_UPDATE_ACTIVATE:
1424                 return CHANNEL_TLV_VPORT_UPDATE_ACTIVATE;
1425         case ECORE_IOV_VP_UPDATE_VLAN_STRIP:
1426                 return CHANNEL_TLV_VPORT_UPDATE_VLAN_STRIP;
1427         case ECORE_IOV_VP_UPDATE_TX_SWITCH:
1428                 return CHANNEL_TLV_VPORT_UPDATE_TX_SWITCH;
1429         case ECORE_IOV_VP_UPDATE_MCAST:
1430                 return CHANNEL_TLV_VPORT_UPDATE_MCAST;
1431         case ECORE_IOV_VP_UPDATE_ACCEPT_PARAM:
1432                 return CHANNEL_TLV_VPORT_UPDATE_ACCEPT_PARAM;
1433         case ECORE_IOV_VP_UPDATE_RSS:
1434                 return CHANNEL_TLV_VPORT_UPDATE_RSS;
1435         case ECORE_IOV_VP_UPDATE_ACCEPT_ANY_VLAN:
1436                 return CHANNEL_TLV_VPORT_UPDATE_ACCEPT_ANY_VLAN;
1437         case ECORE_IOV_VP_UPDATE_SGE_TPA:
1438                 return CHANNEL_TLV_VPORT_UPDATE_SGE_TPA;
1439         default:
1440                 return 0;
1441         }
1442 }
1443
1444 static u16 ecore_iov_prep_vp_update_resp_tlvs(struct ecore_hwfn *p_hwfn,
1445                                               struct ecore_vf_info *p_vf,
1446                                               struct ecore_iov_vf_mbx *p_mbx,
1447                                               u8 status, u16 tlvs_mask,
1448                                               u16 tlvs_accepted)
1449 {
1450         struct pfvf_def_resp_tlv *resp;
1451         u16 size, total_len, i;
1452
1453         OSAL_MEMSET(p_mbx->reply_virt, 0, sizeof(union pfvf_tlvs));
1454         p_mbx->offset = (u8 *)p_mbx->reply_virt;
1455         size = sizeof(struct pfvf_def_resp_tlv);
1456         total_len = size;
1457
1458         ecore_add_tlv(&p_mbx->offset, CHANNEL_TLV_VPORT_UPDATE, size);
1459
1460         /* Prepare response for all extended tlvs if they are found by PF */
1461         for (i = 0; i < ECORE_IOV_VP_UPDATE_MAX; i++) {
1462                 if (!(tlvs_mask & (1 << i)))
1463                         continue;
1464
1465                 resp = ecore_add_tlv(&p_mbx->offset, ecore_iov_vport_to_tlv(i),
1466                                      size);
1467
1468                 if (tlvs_accepted & (1 << i))
1469                         resp->hdr.status = status;
1470                 else
1471                         resp->hdr.status = PFVF_STATUS_NOT_SUPPORTED;
1472
1473                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1474                            "VF[%d] - vport_update resp: TLV %d, status %02x\n",
1475                            p_vf->relative_vf_id,
1476                            ecore_iov_vport_to_tlv(i),
1477                            resp->hdr.status);
1478
1479                 total_len += size;
1480         }
1481
1482         ecore_add_tlv(&p_mbx->offset, CHANNEL_TLV_LIST_END,
1483                       sizeof(struct channel_list_end_tlv));
1484
1485         return total_len;
1486 }
1487
1488 static void ecore_iov_prepare_resp(struct ecore_hwfn *p_hwfn,
1489                                    struct ecore_ptt *p_ptt,
1490                                    struct ecore_vf_info *vf_info,
1491                                    u16 type, u16 length, u8 status)
1492 {
1493         struct ecore_iov_vf_mbx *mbx = &vf_info->vf_mbx;
1494
1495         mbx->offset = (u8 *)mbx->reply_virt;
1496
1497         ecore_add_tlv(&mbx->offset, type, length);
1498         ecore_add_tlv(&mbx->offset, CHANNEL_TLV_LIST_END,
1499                       sizeof(struct channel_list_end_tlv));
1500
1501         ecore_iov_send_response(p_hwfn, p_ptt, vf_info, length, status);
1502 }
1503
1504 struct ecore_public_vf_info
1505 *ecore_iov_get_public_vf_info(struct ecore_hwfn *p_hwfn,
1506                               u16 relative_vf_id,
1507                               bool b_enabled_only)
1508 {
1509         struct ecore_vf_info *vf = OSAL_NULL;
1510
1511         vf = ecore_iov_get_vf_info(p_hwfn, relative_vf_id, b_enabled_only);
1512         if (!vf)
1513                 return OSAL_NULL;
1514
1515         return &vf->p_vf_info;
1516 }
1517
1518 static void ecore_iov_vf_cleanup(struct ecore_hwfn *p_hwfn,
1519                                  struct ecore_vf_info *p_vf)
1520 {
1521         u32 i, j;
1522         p_vf->vf_bulletin = 0;
1523         p_vf->vport_instance = 0;
1524         p_vf->configured_features = 0;
1525
1526         /* If VF previously requested less resources, go back to default */
1527         p_vf->num_rxqs = p_vf->num_sbs;
1528         p_vf->num_txqs = p_vf->num_sbs;
1529
1530         p_vf->num_active_rxqs = 0;
1531
1532         for (i = 0; i < ECORE_MAX_VF_CHAINS_PER_PF; i++) {
1533                 struct ecore_vf_queue *p_queue = &p_vf->vf_queues[i];
1534
1535                 for (j = 0; j < MAX_QUEUES_PER_QZONE; j++) {
1536                         if (!p_queue->cids[j].p_cid)
1537                                 continue;
1538
1539                         ecore_eth_queue_cid_release(p_hwfn,
1540                                                     p_queue->cids[j].p_cid);
1541                         p_queue->cids[j].p_cid = OSAL_NULL;
1542                 }
1543         }
1544
1545         OSAL_MEMSET(&p_vf->shadow_config, 0, sizeof(p_vf->shadow_config));
1546         OSAL_MEMSET(&p_vf->acquire, 0, sizeof(p_vf->acquire));
1547         OSAL_IOV_VF_CLEANUP(p_hwfn, p_vf->relative_vf_id);
1548 }
1549
1550 /* Returns either 0, or log(size) */
1551 static u32 ecore_iov_vf_db_bar_size(struct ecore_hwfn *p_hwfn,
1552                                     struct ecore_ptt *p_ptt)
1553 {
1554         u32 val = ecore_rd(p_hwfn, p_ptt, PGLUE_B_REG_VF_BAR1_SIZE);
1555
1556         if (val)
1557                 return val + 11;
1558         return 0;
1559 }
1560
1561 static void
1562 ecore_iov_vf_mbx_acquire_resc_cids(struct ecore_hwfn *p_hwfn,
1563                                    struct ecore_ptt *p_ptt,
1564                                    struct ecore_vf_info *p_vf,
1565                                    struct vf_pf_resc_request *p_req,
1566                                    struct pf_vf_resc *p_resp)
1567 {
1568         u8 num_vf_cons = p_hwfn->pf_params.eth_pf_params.num_vf_cons;
1569         u8 db_size = DB_ADDR_VF(1, DQ_DEMS_LEGACY) -
1570                      DB_ADDR_VF(0, DQ_DEMS_LEGACY);
1571         u32 bar_size;
1572
1573         p_resp->num_cids = OSAL_MIN_T(u8, p_req->num_cids, num_vf_cons);
1574
1575         /* If VF didn't bother asking for QIDs than don't bother limiting
1576          * number of CIDs. The VF doesn't care about the number, and this
1577          * has the likely result of causing an additional acquisition.
1578          */
1579         if (!(p_vf->acquire.vfdev_info.capabilities &
1580               VFPF_ACQUIRE_CAP_QUEUE_QIDS))
1581                 return;
1582
1583         /* If doorbell bar was mapped by VF, limit the VF CIDs to an amount
1584          * that would make sure doorbells for all CIDs fall within the bar.
1585          * If it doesn't, make sure regview window is sufficient.
1586          */
1587         if (p_vf->acquire.vfdev_info.capabilities &
1588             VFPF_ACQUIRE_CAP_PHYSICAL_BAR) {
1589                 bar_size = ecore_iov_vf_db_bar_size(p_hwfn, p_ptt);
1590                 if (bar_size)
1591                         bar_size = 1 << bar_size;
1592
1593                 if (ECORE_IS_CMT(p_hwfn->p_dev))
1594                         bar_size /= 2;
1595         } else {
1596                 bar_size = PXP_VF_BAR0_DQ_LENGTH;
1597         }
1598
1599         if (bar_size / db_size < 256)
1600                 p_resp->num_cids = OSAL_MIN_T(u8, p_resp->num_cids,
1601                                               (u8)(bar_size / db_size));
1602 }
1603
1604 static u8 ecore_iov_vf_mbx_acquire_resc(struct ecore_hwfn *p_hwfn,
1605                                         struct ecore_ptt *p_ptt,
1606                                         struct ecore_vf_info *p_vf,
1607                                         struct vf_pf_resc_request *p_req,
1608                                         struct pf_vf_resc *p_resp)
1609 {
1610         u8 i;
1611
1612         /* Queue related information */
1613         p_resp->num_rxqs = p_vf->num_rxqs;
1614         p_resp->num_txqs = p_vf->num_txqs;
1615         p_resp->num_sbs = p_vf->num_sbs;
1616
1617         for (i = 0; i < p_resp->num_sbs; i++) {
1618                 p_resp->hw_sbs[i].hw_sb_id = p_vf->igu_sbs[i];
1619                 /* TODO - what's this sb_qid field? Is it deprecated?
1620                  * or is there an ecore_client that looks at this?
1621                  */
1622                 p_resp->hw_sbs[i].sb_qid = 0;
1623         }
1624
1625         /* These fields are filled for backward compatibility.
1626          * Unused by modern vfs.
1627          */
1628         for (i = 0; i < p_resp->num_rxqs; i++) {
1629                 ecore_fw_l2_queue(p_hwfn, p_vf->vf_queues[i].fw_rx_qid,
1630                                   (u16 *)&p_resp->hw_qid[i]);
1631                 p_resp->cid[i] = i;
1632         }
1633
1634         /* Filter related information */
1635         p_resp->num_mac_filters = OSAL_MIN_T(u8, p_vf->num_mac_filters,
1636                                              p_req->num_mac_filters);
1637         p_resp->num_vlan_filters = OSAL_MIN_T(u8, p_vf->num_vlan_filters,
1638                                               p_req->num_vlan_filters);
1639
1640         ecore_iov_vf_mbx_acquire_resc_cids(p_hwfn, p_ptt, p_vf, p_req, p_resp);
1641
1642         /* This isn't really needed/enforced, but some legacy VFs might depend
1643          * on the correct filling of this field.
1644          */
1645         p_resp->num_mc_filters = ECORE_MAX_MC_ADDRS;
1646
1647         /* Validate sufficient resources for VF */
1648         if (p_resp->num_rxqs < p_req->num_rxqs ||
1649             p_resp->num_txqs < p_req->num_txqs ||
1650             p_resp->num_sbs < p_req->num_sbs ||
1651             p_resp->num_mac_filters < p_req->num_mac_filters ||
1652             p_resp->num_vlan_filters < p_req->num_vlan_filters ||
1653             p_resp->num_mc_filters < p_req->num_mc_filters ||
1654             p_resp->num_cids < p_req->num_cids) {
1655                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1656                            "VF[%d] - Insufficient resources: rxq [%02x/%02x] txq [%02x/%02x] sbs [%02x/%02x] mac [%02x/%02x] vlan [%02x/%02x] mc [%02x/%02x] cids [%02x/%02x]\n",
1657                            p_vf->abs_vf_id,
1658                            p_req->num_rxqs, p_resp->num_rxqs,
1659                            p_req->num_rxqs, p_resp->num_txqs,
1660                            p_req->num_sbs, p_resp->num_sbs,
1661                            p_req->num_mac_filters, p_resp->num_mac_filters,
1662                            p_req->num_vlan_filters, p_resp->num_vlan_filters,
1663                            p_req->num_mc_filters, p_resp->num_mc_filters,
1664                            p_req->num_cids, p_resp->num_cids);
1665
1666                 /* Some legacy OSes are incapable of correctly handling this
1667                  * failure.
1668                  */
1669                 if ((p_vf->acquire.vfdev_info.eth_fp_hsi_minor ==
1670                      ETH_HSI_VER_NO_PKT_LEN_TUNN) &&
1671                     (p_vf->acquire.vfdev_info.os_type ==
1672                      VFPF_ACQUIRE_OS_WINDOWS))
1673                         return PFVF_STATUS_SUCCESS;
1674
1675                 return PFVF_STATUS_NO_RESOURCE;
1676         }
1677
1678         return PFVF_STATUS_SUCCESS;
1679 }
1680
1681 static void ecore_iov_vf_mbx_acquire_stats(struct pfvf_stats_info *p_stats)
1682 {
1683         p_stats->mstats.address = PXP_VF_BAR0_START_MSDM_ZONE_B +
1684                                   OFFSETOF(struct mstorm_vf_zone,
1685                                            non_trigger.eth_queue_stat);
1686         p_stats->mstats.len = sizeof(struct eth_mstorm_per_queue_stat);
1687         p_stats->ustats.address = PXP_VF_BAR0_START_USDM_ZONE_B +
1688                                   OFFSETOF(struct ustorm_vf_zone,
1689                                            non_trigger.eth_queue_stat);
1690         p_stats->ustats.len = sizeof(struct eth_ustorm_per_queue_stat);
1691         p_stats->pstats.address = PXP_VF_BAR0_START_PSDM_ZONE_B +
1692                                   OFFSETOF(struct pstorm_vf_zone,
1693                                            non_trigger.eth_queue_stat);
1694         p_stats->pstats.len = sizeof(struct eth_pstorm_per_queue_stat);
1695         p_stats->tstats.address = 0;
1696         p_stats->tstats.len = 0;
1697 }
1698
1699 static void ecore_iov_vf_mbx_acquire(struct ecore_hwfn       *p_hwfn,
1700                                      struct ecore_ptt        *p_ptt,
1701                                      struct ecore_vf_info    *vf)
1702 {
1703         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
1704         struct pfvf_acquire_resp_tlv *resp = &mbx->reply_virt->acquire_resp;
1705         struct pf_vf_pfdev_info *pfdev_info = &resp->pfdev_info;
1706         struct vfpf_acquire_tlv *req = &mbx->req_virt->acquire;
1707         u8 vfpf_status = PFVF_STATUS_NOT_SUPPORTED;
1708         struct pf_vf_resc *resc = &resp->resc;
1709         enum _ecore_status_t rc;
1710
1711         OSAL_MEMSET(resp, 0, sizeof(*resp));
1712
1713         /* Write the PF version so that VF would know which version
1714          * is supported - might be later overridden. This guarantees that
1715          * VF could recognize legacy PF based on lack of versions in reply.
1716          */
1717         pfdev_info->major_fp_hsi = ETH_HSI_VER_MAJOR;
1718         pfdev_info->minor_fp_hsi = ETH_HSI_VER_MINOR;
1719
1720         /* TODO - not doing anything is bad since we'll assert, but this isn't
1721          * necessarily the right behavior - perhaps we should have allowed some
1722          * versatility here.
1723          */
1724         if (vf->state != VF_FREE &&
1725             vf->state != VF_STOPPED) {
1726                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1727                            "VF[%d] sent ACQUIRE but is already in state %d - fail request\n",
1728                            vf->abs_vf_id, vf->state);
1729                 goto out;
1730         }
1731
1732         /* Validate FW compatibility */
1733         if (req->vfdev_info.eth_fp_hsi_major != ETH_HSI_VER_MAJOR) {
1734                 if (req->vfdev_info.capabilities &
1735                     VFPF_ACQUIRE_CAP_PRE_FP_HSI) {
1736                         struct vf_pf_vfdev_info *p_vfdev = &req->vfdev_info;
1737
1738                         /* This legacy support would need to be removed once
1739                          * the major has changed.
1740                          */
1741                         OSAL_BUILD_BUG_ON(ETH_HSI_VER_MAJOR != 3);
1742
1743                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1744                                    "VF[%d] is pre-fastpath HSI\n",
1745                                    vf->abs_vf_id);
1746                         p_vfdev->eth_fp_hsi_major = ETH_HSI_VER_MAJOR;
1747                         p_vfdev->eth_fp_hsi_minor = ETH_HSI_VER_NO_PKT_LEN_TUNN;
1748                 } else {
1749                         DP_INFO(p_hwfn,
1750                                 "VF[%d] needs fastpath HSI %02x.%02x, which is"
1751                                 " incompatible with loaded FW's faspath"
1752                                 " HSI %02x.%02x\n",
1753                                 vf->abs_vf_id,
1754                                 req->vfdev_info.eth_fp_hsi_major,
1755                                 req->vfdev_info.eth_fp_hsi_minor,
1756                                 ETH_HSI_VER_MAJOR, ETH_HSI_VER_MINOR);
1757
1758                         goto out;
1759                 }
1760         }
1761
1762         /* On 100g PFs, prevent old VFs from loading */
1763         if (ECORE_IS_CMT(p_hwfn->p_dev) &&
1764             !(req->vfdev_info.capabilities & VFPF_ACQUIRE_CAP_100G)) {
1765                 DP_INFO(p_hwfn,
1766                         "VF[%d] is running an old driver that doesn't support"
1767                         " 100g\n",
1768                         vf->abs_vf_id);
1769                 goto out;
1770         }
1771
1772 #ifndef __EXTRACT__LINUX__
1773         if (OSAL_IOV_VF_ACQUIRE(p_hwfn, vf->relative_vf_id) != ECORE_SUCCESS) {
1774                 vfpf_status = PFVF_STATUS_NOT_SUPPORTED;
1775                 goto out;
1776         }
1777 #endif
1778
1779         /* Store the acquire message */
1780         OSAL_MEMCPY(&vf->acquire, req, sizeof(vf->acquire));
1781
1782         vf->opaque_fid = req->vfdev_info.opaque_fid;
1783
1784         vf->vf_bulletin = req->bulletin_addr;
1785         vf->bulletin.size = (vf->bulletin.size < req->bulletin_size) ?
1786             vf->bulletin.size : req->bulletin_size;
1787
1788         /* fill in pfdev info */
1789         pfdev_info->chip_num = p_hwfn->p_dev->chip_num;
1790         pfdev_info->db_size = 0;        /* @@@ TBD MichalK Vf Doorbells */
1791         pfdev_info->indices_per_sb = PIS_PER_SB_E4;
1792
1793         pfdev_info->capabilities = PFVF_ACQUIRE_CAP_DEFAULT_UNTAGGED |
1794                                    PFVF_ACQUIRE_CAP_POST_FW_OVERRIDE;
1795         if (ECORE_IS_CMT(p_hwfn->p_dev))
1796                 pfdev_info->capabilities |= PFVF_ACQUIRE_CAP_100G;
1797
1798         /* Share our ability to use multiple queue-ids only with VFs
1799          * that request it.
1800          */
1801         if (req->vfdev_info.capabilities & VFPF_ACQUIRE_CAP_QUEUE_QIDS)
1802                 pfdev_info->capabilities |= PFVF_ACQUIRE_CAP_QUEUE_QIDS;
1803
1804         /* Share the sizes of the bars with VF */
1805         resp->pfdev_info.bar_size = (u8)ecore_iov_vf_db_bar_size(p_hwfn,
1806                                                              p_ptt);
1807
1808         ecore_iov_vf_mbx_acquire_stats(&pfdev_info->stats_info);
1809
1810         OSAL_MEMCPY(pfdev_info->port_mac, p_hwfn->hw_info.hw_mac_addr,
1811                     ETH_ALEN);
1812
1813         pfdev_info->fw_major = FW_MAJOR_VERSION;
1814         pfdev_info->fw_minor = FW_MINOR_VERSION;
1815         pfdev_info->fw_rev = FW_REVISION_VERSION;
1816         pfdev_info->fw_eng = FW_ENGINEERING_VERSION;
1817
1818         /* Incorrect when legacy, but doesn't matter as legacy isn't reading
1819          * this field.
1820          */
1821         pfdev_info->minor_fp_hsi = OSAL_MIN_T(u8, ETH_HSI_VER_MINOR,
1822                                               req->vfdev_info.eth_fp_hsi_minor);
1823         pfdev_info->os_type = OSAL_IOV_GET_OS_TYPE();
1824         ecore_mcp_get_mfw_ver(p_hwfn, p_ptt, &pfdev_info->mfw_ver,
1825                               OSAL_NULL);
1826
1827         pfdev_info->dev_type = p_hwfn->p_dev->type;
1828         pfdev_info->chip_rev = p_hwfn->p_dev->chip_rev;
1829
1830         /* Fill resources available to VF; Make sure there are enough to
1831          * satisfy the VF's request.
1832          */
1833         vfpf_status = ecore_iov_vf_mbx_acquire_resc(p_hwfn, p_ptt, vf,
1834                                                     &req->resc_request, resc);
1835         if (vfpf_status != PFVF_STATUS_SUCCESS)
1836                 goto out;
1837
1838         /* Start the VF in FW */
1839         rc = ecore_sp_vf_start(p_hwfn, vf);
1840         if (rc != ECORE_SUCCESS) {
1841                 DP_NOTICE(p_hwfn, true, "Failed to start VF[%02x]\n",
1842                           vf->abs_vf_id);
1843                 vfpf_status = PFVF_STATUS_FAILURE;
1844                 goto out;
1845         }
1846
1847         /* Fill agreed size of bulletin board in response, and post
1848          * an initial image to the bulletin board.
1849          */
1850         resp->bulletin_size = vf->bulletin.size;
1851         ecore_iov_post_vf_bulletin(p_hwfn, vf->relative_vf_id, p_ptt);
1852
1853         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1854                    "VF[%d] ACQUIRE_RESPONSE: pfdev_info- chip_num=0x%x,"
1855                    " db_size=%d, idx_per_sb=%d, pf_cap=0x%lx\n"
1856                    "resources- n_rxq-%d, n_txq-%d, n_sbs-%d, n_macs-%d,"
1857                    " n_vlans-%d\n",
1858                    vf->abs_vf_id, resp->pfdev_info.chip_num,
1859                    resp->pfdev_info.db_size, resp->pfdev_info.indices_per_sb,
1860                    (unsigned long)resp->pfdev_info.capabilities, resc->num_rxqs,
1861                    resc->num_txqs, resc->num_sbs, resc->num_mac_filters,
1862                    resc->num_vlan_filters);
1863
1864         vf->state = VF_ACQUIRED;
1865
1866 out:
1867         /* Prepare Response */
1868         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_ACQUIRE,
1869                                sizeof(struct pfvf_acquire_resp_tlv),
1870                                vfpf_status);
1871 }
1872
1873 static enum _ecore_status_t
1874 __ecore_iov_spoofchk_set(struct ecore_hwfn *p_hwfn,
1875                          struct ecore_vf_info *p_vf, bool val)
1876 {
1877         struct ecore_sp_vport_update_params params;
1878         enum _ecore_status_t rc;
1879
1880         if (val == p_vf->spoof_chk) {
1881                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1882                            "Spoofchk value[%d] is already configured\n", val);
1883                 return ECORE_SUCCESS;
1884         }
1885
1886         OSAL_MEMSET(&params, 0, sizeof(struct ecore_sp_vport_update_params));
1887         params.opaque_fid = p_vf->opaque_fid;
1888         params.vport_id = p_vf->vport_id;
1889         params.update_anti_spoofing_en_flg = 1;
1890         params.anti_spoofing_en = val;
1891
1892         rc = ecore_sp_vport_update(p_hwfn, &params, ECORE_SPQ_MODE_EBLOCK,
1893                                    OSAL_NULL);
1894         if (rc == ECORE_SUCCESS) {
1895                 p_vf->spoof_chk = val;
1896                 p_vf->req_spoofchk_val = p_vf->spoof_chk;
1897                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1898                            "Spoofchk val[%d] configured\n", val);
1899         } else {
1900                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1901                            "Spoofchk configuration[val:%d] failed for VF[%d]\n",
1902                            val, p_vf->relative_vf_id);
1903         }
1904
1905         return rc;
1906 }
1907
1908 static enum _ecore_status_t
1909 ecore_iov_reconfigure_unicast_vlan(struct ecore_hwfn *p_hwfn,
1910                                    struct ecore_vf_info *p_vf)
1911 {
1912         struct ecore_filter_ucast filter;
1913         enum _ecore_status_t rc = ECORE_SUCCESS;
1914         int i;
1915
1916         OSAL_MEMSET(&filter, 0, sizeof(filter));
1917         filter.is_rx_filter = 1;
1918         filter.is_tx_filter = 1;
1919         filter.vport_to_add_to = p_vf->vport_id;
1920         filter.opcode = ECORE_FILTER_ADD;
1921
1922         /* Reconfigure vlans */
1923         for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++) {
1924                 if (!p_vf->shadow_config.vlans[i].used)
1925                         continue;
1926
1927                 filter.type = ECORE_FILTER_VLAN;
1928                 filter.vlan = p_vf->shadow_config.vlans[i].vid;
1929                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1930                            "Reconfiguring VLAN [0x%04x] for VF [%04x]\n",
1931                            filter.vlan, p_vf->relative_vf_id);
1932                 rc = ecore_sp_eth_filter_ucast(p_hwfn, p_vf->opaque_fid,
1933                                                &filter, ECORE_SPQ_MODE_CB,
1934                                                OSAL_NULL);
1935                 if (rc) {
1936                         DP_NOTICE(p_hwfn, true,
1937                                   "Failed to configure VLAN [%04x]"
1938                                   " to VF [%04x]\n",
1939                                   filter.vlan, p_vf->relative_vf_id);
1940                         break;
1941                 }
1942         }
1943
1944         return rc;
1945 }
1946
1947 static enum _ecore_status_t
1948 ecore_iov_reconfigure_unicast_shadow(struct ecore_hwfn *p_hwfn,
1949                                      struct ecore_vf_info *p_vf, u64 events)
1950 {
1951         enum _ecore_status_t rc = ECORE_SUCCESS;
1952
1953         /*TODO - what about MACs? */
1954
1955         if ((events & (1 << VLAN_ADDR_FORCED)) &&
1956             !(p_vf->configured_features & (1 << VLAN_ADDR_FORCED)))
1957                 rc = ecore_iov_reconfigure_unicast_vlan(p_hwfn, p_vf);
1958
1959         return rc;
1960 }
1961
1962 static  enum _ecore_status_t
1963 ecore_iov_configure_vport_forced(struct ecore_hwfn *p_hwfn,
1964                                  struct ecore_vf_info *p_vf,
1965                                  u64 events)
1966 {
1967         enum _ecore_status_t rc = ECORE_SUCCESS;
1968         struct ecore_filter_ucast filter;
1969
1970         if (!p_vf->vport_instance)
1971                 return ECORE_INVAL;
1972
1973         if (events & (1 << MAC_ADDR_FORCED)) {
1974                 /* Since there's no way [currently] of removing the MAC,
1975                  * we can always assume this means we need to force it.
1976                  */
1977                 OSAL_MEMSET(&filter, 0, sizeof(filter));
1978                 filter.type = ECORE_FILTER_MAC;
1979                 filter.opcode = ECORE_FILTER_REPLACE;
1980                 filter.is_rx_filter = 1;
1981                 filter.is_tx_filter = 1;
1982                 filter.vport_to_add_to = p_vf->vport_id;
1983                 OSAL_MEMCPY(filter.mac, p_vf->bulletin.p_virt->mac, ETH_ALEN);
1984
1985                 rc = ecore_sp_eth_filter_ucast(p_hwfn, p_vf->opaque_fid,
1986                                                &filter,
1987                                                ECORE_SPQ_MODE_CB, OSAL_NULL);
1988                 if (rc) {
1989                         DP_NOTICE(p_hwfn, true,
1990                                   "PF failed to configure MAC for VF\n");
1991                         return rc;
1992                 }
1993
1994                 p_vf->configured_features |= 1 << MAC_ADDR_FORCED;
1995         }
1996
1997         if (events & (1 << VLAN_ADDR_FORCED)) {
1998                 struct ecore_sp_vport_update_params vport_update;
1999                 u8 removal;
2000                 int i;
2001
2002                 OSAL_MEMSET(&filter, 0, sizeof(filter));
2003                 filter.type = ECORE_FILTER_VLAN;
2004                 filter.is_rx_filter = 1;
2005                 filter.is_tx_filter = 1;
2006                 filter.vport_to_add_to = p_vf->vport_id;
2007                 filter.vlan = p_vf->bulletin.p_virt->pvid;
2008                 filter.opcode = filter.vlan ? ECORE_FILTER_REPLACE :
2009                     ECORE_FILTER_FLUSH;
2010
2011                 /* Send the ramrod */
2012                 rc = ecore_sp_eth_filter_ucast(p_hwfn, p_vf->opaque_fid,
2013                                                &filter,
2014                                                ECORE_SPQ_MODE_CB, OSAL_NULL);
2015                 if (rc) {
2016                         DP_NOTICE(p_hwfn, true,
2017                                   "PF failed to configure VLAN for VF\n");
2018                         return rc;
2019                 }
2020
2021                 /* Update the default-vlan & silent vlan stripping */
2022                 OSAL_MEMSET(&vport_update, 0, sizeof(vport_update));
2023                 vport_update.opaque_fid = p_vf->opaque_fid;
2024                 vport_update.vport_id = p_vf->vport_id;
2025                 vport_update.update_default_vlan_enable_flg = 1;
2026                 vport_update.default_vlan_enable_flg = filter.vlan ? 1 : 0;
2027                 vport_update.update_default_vlan_flg = 1;
2028                 vport_update.default_vlan = filter.vlan;
2029
2030                 vport_update.update_inner_vlan_removal_flg = 1;
2031                 removal = filter.vlan ?
2032                     1 : p_vf->shadow_config.inner_vlan_removal;
2033                 vport_update.inner_vlan_removal_flg = removal;
2034                 vport_update.silent_vlan_removal_flg = filter.vlan ? 1 : 0;
2035                 rc = ecore_sp_vport_update(p_hwfn, &vport_update,
2036                                            ECORE_SPQ_MODE_EBLOCK, OSAL_NULL);
2037                 if (rc) {
2038                         DP_NOTICE(p_hwfn, true,
2039                                   "PF failed to configure VF vport for vlan\n");
2040                         return rc;
2041                 }
2042
2043                 /* Update all the Rx queues */
2044                 for (i = 0; i < ECORE_MAX_VF_CHAINS_PER_PF; i++) {
2045                         struct ecore_vf_queue *p_queue = &p_vf->vf_queues[i];
2046                         struct ecore_queue_cid *p_cid = OSAL_NULL;
2047
2048                         /* There can be at most 1 Rx queue on qzone. Find it */
2049                         p_cid = ecore_iov_get_vf_rx_queue_cid(p_queue);
2050                         if (p_cid == OSAL_NULL)
2051                                 continue;
2052
2053                         rc = ecore_sp_eth_rx_queues_update(p_hwfn,
2054                                                            (void **)&p_cid,
2055                                                    1, 0, 1,
2056                                                    ECORE_SPQ_MODE_EBLOCK,
2057                                                    OSAL_NULL);
2058                         if (rc) {
2059                                 DP_NOTICE(p_hwfn, true,
2060                                           "Failed to send Rx update"
2061                                           " fo queue[0x%04x]\n",
2062                                           p_cid->rel.queue_id);
2063                                 return rc;
2064                         }
2065                 }
2066
2067                 if (filter.vlan)
2068                         p_vf->configured_features |= 1 << VLAN_ADDR_FORCED;
2069                 else
2070                         p_vf->configured_features &= ~(1 << VLAN_ADDR_FORCED);
2071         }
2072
2073         /* If forced features are terminated, we need to configure the shadow
2074          * configuration back again.
2075          */
2076         if (events)
2077                 ecore_iov_reconfigure_unicast_shadow(p_hwfn, p_vf, events);
2078
2079         return rc;
2080 }
2081
2082 static void ecore_iov_vf_mbx_start_vport(struct ecore_hwfn *p_hwfn,
2083                                          struct ecore_ptt *p_ptt,
2084                                          struct ecore_vf_info *vf)
2085 {
2086         struct ecore_sp_vport_start_params params = { 0 };
2087         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2088         struct vfpf_vport_start_tlv *start;
2089         u8 status = PFVF_STATUS_SUCCESS;
2090         struct ecore_vf_info *vf_info;
2091         u64 *p_bitmap;
2092         int sb_id;
2093         enum _ecore_status_t rc;
2094
2095         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vf->relative_vf_id, true);
2096         if (!vf_info) {
2097                 DP_NOTICE(p_hwfn->p_dev, true,
2098                           "Failed to get VF info, invalid vfid [%d]\n",
2099                           vf->relative_vf_id);
2100                 return;
2101         }
2102
2103         vf->state = VF_ENABLED;
2104         start = &mbx->req_virt->start_vport;
2105
2106         ecore_iov_enable_vf_traffic(p_hwfn, p_ptt, vf);
2107
2108         /* Initialize Status block in CAU */
2109         for (sb_id = 0; sb_id < vf->num_sbs; sb_id++) {
2110                 if (!start->sb_addr[sb_id]) {
2111                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2112                                    "VF[%d] did not fill the address of SB %d\n",
2113                                    vf->relative_vf_id, sb_id);
2114                         break;
2115                 }
2116
2117                 ecore_int_cau_conf_sb(p_hwfn, p_ptt,
2118                                       start->sb_addr[sb_id],
2119                                       vf->igu_sbs[sb_id],
2120                                       vf->abs_vf_id, 1);
2121         }
2122
2123         vf->mtu = start->mtu;
2124         vf->shadow_config.inner_vlan_removal = start->inner_vlan_removal;
2125
2126         /* Take into consideration configuration forced by hypervisor;
2127          * If none is configured, use the supplied VF values [for old
2128          * vfs that would still be fine, since they passed '0' as padding].
2129          */
2130         p_bitmap = &vf_info->bulletin.p_virt->valid_bitmap;
2131         if (!(*p_bitmap & (1 << VFPF_BULLETIN_UNTAGGED_DEFAULT_FORCED))) {
2132                 u8 vf_req = start->only_untagged;
2133
2134                 vf_info->bulletin.p_virt->default_only_untagged = vf_req;
2135                 *p_bitmap |= 1 << VFPF_BULLETIN_UNTAGGED_DEFAULT;
2136         }
2137
2138         params.tpa_mode = start->tpa_mode;
2139         params.remove_inner_vlan = start->inner_vlan_removal;
2140         params.tx_switching = true;
2141
2142 #ifndef ASIC_ONLY
2143         if (CHIP_REV_IS_FPGA(p_hwfn->p_dev)) {
2144                 DP_NOTICE(p_hwfn, false,
2145                           "FPGA: Don't config VF for Tx-switching [no pVFC]\n");
2146                 params.tx_switching = false;
2147         }
2148 #endif
2149
2150         params.only_untagged = vf_info->bulletin.p_virt->default_only_untagged;
2151         params.drop_ttl0 = false;
2152         params.concrete_fid = vf->concrete_fid;
2153         params.opaque_fid = vf->opaque_fid;
2154         params.vport_id = vf->vport_id;
2155         params.max_buffers_per_cqe = start->max_buffers_per_cqe;
2156         params.mtu = vf->mtu;
2157         params.check_mac = true;
2158
2159         rc = ecore_sp_eth_vport_start(p_hwfn, &params);
2160         if (rc != ECORE_SUCCESS) {
2161                 DP_ERR(p_hwfn,
2162                        "ecore_iov_vf_mbx_start_vport returned error %d\n", rc);
2163                 status = PFVF_STATUS_FAILURE;
2164         } else {
2165                 vf->vport_instance++;
2166
2167                 /* Force configuration if needed on the newly opened vport */
2168                 ecore_iov_configure_vport_forced(p_hwfn, vf, *p_bitmap);
2169                 OSAL_IOV_POST_START_VPORT(p_hwfn, vf->relative_vf_id,
2170                                           vf->vport_id, vf->opaque_fid);
2171                 __ecore_iov_spoofchk_set(p_hwfn, vf, vf->req_spoofchk_val);
2172         }
2173
2174         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_VPORT_START,
2175                                sizeof(struct pfvf_def_resp_tlv), status);
2176 }
2177
2178 static void ecore_iov_vf_mbx_stop_vport(struct ecore_hwfn *p_hwfn,
2179                                         struct ecore_ptt *p_ptt,
2180                                         struct ecore_vf_info *vf)
2181 {
2182         u8 status = PFVF_STATUS_SUCCESS;
2183         enum _ecore_status_t rc;
2184
2185         OSAL_IOV_VF_VPORT_STOP(p_hwfn, vf);
2186         vf->vport_instance--;
2187         vf->spoof_chk = false;
2188
2189         if ((ecore_iov_validate_active_rxq(vf)) ||
2190             (ecore_iov_validate_active_txq(vf))) {
2191                 vf->b_malicious = true;
2192                 DP_NOTICE(p_hwfn, false,
2193                           "VF [%02x] - considered malicious;"
2194                           " Unable to stop RX/TX queuess\n",
2195                           vf->abs_vf_id);
2196                 status = PFVF_STATUS_MALICIOUS;
2197                 goto out;
2198         }
2199
2200         rc = ecore_sp_vport_stop(p_hwfn, vf->opaque_fid, vf->vport_id);
2201         if (rc != ECORE_SUCCESS) {
2202                 DP_ERR(p_hwfn,
2203                        "ecore_iov_vf_mbx_stop_vport returned error %d\n", rc);
2204                 status = PFVF_STATUS_FAILURE;
2205         }
2206
2207         /* Forget the configuration on the vport */
2208         vf->configured_features = 0;
2209         OSAL_MEMSET(&vf->shadow_config, 0, sizeof(vf->shadow_config));
2210
2211 out:
2212         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_VPORT_TEARDOWN,
2213                                sizeof(struct pfvf_def_resp_tlv), status);
2214 }
2215
2216 static void ecore_iov_vf_mbx_start_rxq_resp(struct ecore_hwfn *p_hwfn,
2217                                             struct ecore_ptt *p_ptt,
2218                                             struct ecore_vf_info *vf,
2219                                             u8 status, bool b_legacy)
2220 {
2221         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2222         struct pfvf_start_queue_resp_tlv *p_tlv;
2223         struct vfpf_start_rxq_tlv *req;
2224         u16 length;
2225
2226         mbx->offset = (u8 *)mbx->reply_virt;
2227
2228         /* Taking a bigger struct instead of adding a TLV to list was a
2229          * mistake, but one which we're now stuck with, as some older
2230          * clients assume the size of the previous response.
2231          */
2232         if (!b_legacy)
2233                 length = sizeof(*p_tlv);
2234         else
2235                 length = sizeof(struct pfvf_def_resp_tlv);
2236
2237         p_tlv = ecore_add_tlv(&mbx->offset, CHANNEL_TLV_START_RXQ, length);
2238         ecore_add_tlv(&mbx->offset, CHANNEL_TLV_LIST_END,
2239                       sizeof(struct channel_list_end_tlv));
2240
2241         /* Update the TLV with the response */
2242         if ((status == PFVF_STATUS_SUCCESS) && !b_legacy) {
2243                 req = &mbx->req_virt->start_rxq;
2244                 p_tlv->offset = PXP_VF_BAR0_START_MSDM_ZONE_B +
2245                                 OFFSETOF(struct mstorm_vf_zone,
2246                                          non_trigger.eth_rx_queue_producers) +
2247                                 sizeof(struct eth_rx_prod_data) * req->rx_qid;
2248         }
2249
2250         ecore_iov_send_response(p_hwfn, p_ptt, vf, length, status);
2251 }
2252
2253 static u8 ecore_iov_vf_mbx_qid(struct ecore_hwfn *p_hwfn,
2254                                struct ecore_vf_info *p_vf, bool b_is_tx)
2255 {
2256         struct ecore_iov_vf_mbx *p_mbx = &p_vf->vf_mbx;
2257         struct vfpf_qid_tlv *p_qid_tlv;
2258
2259         /* Search for the qid if the VF published if its going to provide it */
2260         if (!(p_vf->acquire.vfdev_info.capabilities &
2261               VFPF_ACQUIRE_CAP_QUEUE_QIDS)) {
2262                 if (b_is_tx)
2263                         return ECORE_IOV_LEGACY_QID_TX;
2264                 else
2265                         return ECORE_IOV_LEGACY_QID_RX;
2266         }
2267
2268         p_qid_tlv = (struct vfpf_qid_tlv *)
2269                     ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt,
2270                                                CHANNEL_TLV_QID);
2271         if (p_qid_tlv == OSAL_NULL) {
2272                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2273                            "VF[%2x]: Failed to provide qid\n",
2274                            p_vf->relative_vf_id);
2275
2276                 return ECORE_IOV_QID_INVALID;
2277         }
2278
2279         if (p_qid_tlv->qid >= MAX_QUEUES_PER_QZONE) {
2280                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2281                            "VF[%02x]: Provided qid out-of-bounds %02x\n",
2282                            p_vf->relative_vf_id, p_qid_tlv->qid);
2283                 return ECORE_IOV_QID_INVALID;
2284         }
2285
2286         return p_qid_tlv->qid;
2287 }
2288
2289 static void ecore_iov_vf_mbx_start_rxq(struct ecore_hwfn *p_hwfn,
2290                                        struct ecore_ptt *p_ptt,
2291                                        struct ecore_vf_info *vf)
2292 {
2293         struct ecore_queue_start_common_params params;
2294         struct ecore_queue_cid_vf_params vf_params;
2295         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2296         u8 status = PFVF_STATUS_NO_RESOURCE;
2297         u8 qid_usage_idx, vf_legacy = 0;
2298         struct ecore_vf_queue *p_queue;
2299         struct vfpf_start_rxq_tlv *req;
2300         struct ecore_queue_cid *p_cid;
2301         struct ecore_sb_info sb_dummy;
2302         enum _ecore_status_t rc;
2303
2304         req = &mbx->req_virt->start_rxq;
2305
2306         if (!ecore_iov_validate_rxq(p_hwfn, vf, req->rx_qid,
2307                                     ECORE_IOV_VALIDATE_Q_DISABLE) ||
2308             !ecore_iov_validate_sb(p_hwfn, vf, req->hw_sb))
2309                 goto out;
2310
2311         qid_usage_idx = ecore_iov_vf_mbx_qid(p_hwfn, vf, false);
2312         if (qid_usage_idx == ECORE_IOV_QID_INVALID)
2313                 goto out;
2314
2315         p_queue = &vf->vf_queues[req->rx_qid];
2316         if (p_queue->cids[qid_usage_idx].p_cid)
2317                 goto out;
2318
2319         vf_legacy = ecore_vf_calculate_legacy(vf);
2320
2321         /* Acquire a new queue-cid */
2322         OSAL_MEMSET(&params, 0, sizeof(params));
2323         params.queue_id = (u8)p_queue->fw_rx_qid;
2324         params.vport_id = vf->vport_id;
2325         params.stats_id = vf->abs_vf_id + 0x10;
2326
2327         /* Since IGU index is passed via sb_info, construct a dummy one */
2328         OSAL_MEM_ZERO(&sb_dummy, sizeof(sb_dummy));
2329         sb_dummy.igu_sb_id = req->hw_sb;
2330         params.p_sb = &sb_dummy;
2331         params.sb_idx = req->sb_index;
2332
2333         OSAL_MEM_ZERO(&vf_params, sizeof(vf_params));
2334         vf_params.vfid = vf->relative_vf_id;
2335         vf_params.vf_qid = (u8)req->rx_qid;
2336         vf_params.vf_legacy = vf_legacy;
2337         vf_params.qid_usage_idx = qid_usage_idx;
2338
2339         p_cid = ecore_eth_queue_to_cid(p_hwfn, vf->opaque_fid,
2340                                        &params, true, &vf_params);
2341         if (p_cid == OSAL_NULL)
2342                 goto out;
2343
2344         /* Legacy VFs have their Producers in a different location, which they
2345          * calculate on their own and clean the producer prior to this.
2346          */
2347         if (!(vf_legacy & ECORE_QCID_LEGACY_VF_RX_PROD))
2348                 REG_WR(p_hwfn,
2349                        GTT_BAR0_MAP_REG_MSDM_RAM +
2350                        MSTORM_ETH_VF_PRODS_OFFSET(vf->abs_vf_id, req->rx_qid),
2351                        0);
2352
2353         rc = ecore_eth_rxq_start_ramrod(p_hwfn, p_cid,
2354                                         req->bd_max_bytes,
2355                                         req->rxq_addr,
2356                                         req->cqe_pbl_addr,
2357                                         req->cqe_pbl_size);
2358         if (rc != ECORE_SUCCESS) {
2359                 status = PFVF_STATUS_FAILURE;
2360                 ecore_eth_queue_cid_release(p_hwfn, p_cid);
2361         } else {
2362                 p_queue->cids[qid_usage_idx].p_cid = p_cid;
2363                 p_queue->cids[qid_usage_idx].b_is_tx = false;
2364                 status = PFVF_STATUS_SUCCESS;
2365                 vf->num_active_rxqs++;
2366         }
2367
2368 out:
2369         ecore_iov_vf_mbx_start_rxq_resp(p_hwfn, p_ptt, vf, status,
2370                                         !!(vf_legacy &
2371                                            ECORE_QCID_LEGACY_VF_RX_PROD));
2372 }
2373
2374 static void
2375 ecore_iov_pf_update_tun_response(struct pfvf_update_tunn_param_tlv *p_resp,
2376                                  struct ecore_tunnel_info *p_tun,
2377                                  u16 tunn_feature_mask)
2378 {
2379         p_resp->tunn_feature_mask = tunn_feature_mask;
2380         p_resp->vxlan_mode = p_tun->vxlan.b_mode_enabled;
2381         p_resp->l2geneve_mode = p_tun->l2_geneve.b_mode_enabled;
2382         p_resp->ipgeneve_mode = p_tun->ip_geneve.b_mode_enabled;
2383         p_resp->l2gre_mode = p_tun->l2_gre.b_mode_enabled;
2384         p_resp->ipgre_mode = p_tun->l2_gre.b_mode_enabled;
2385         p_resp->vxlan_clss = p_tun->vxlan.tun_cls;
2386         p_resp->l2gre_clss = p_tun->l2_gre.tun_cls;
2387         p_resp->ipgre_clss = p_tun->ip_gre.tun_cls;
2388         p_resp->l2geneve_clss = p_tun->l2_geneve.tun_cls;
2389         p_resp->ipgeneve_clss = p_tun->ip_geneve.tun_cls;
2390         p_resp->geneve_udp_port = p_tun->geneve_port.port;
2391         p_resp->vxlan_udp_port = p_tun->vxlan_port.port;
2392 }
2393
2394 static void
2395 __ecore_iov_pf_update_tun_param(struct vfpf_update_tunn_param_tlv *p_req,
2396                                 struct ecore_tunn_update_type *p_tun,
2397                                 enum ecore_tunn_mode mask, u8 tun_cls)
2398 {
2399         if (p_req->tun_mode_update_mask & (1 << mask)) {
2400                 p_tun->b_update_mode = true;
2401
2402                 if (p_req->tunn_mode & (1 << mask))
2403                         p_tun->b_mode_enabled = true;
2404         }
2405
2406         p_tun->tun_cls = tun_cls;
2407 }
2408
2409 static void
2410 ecore_iov_pf_update_tun_param(struct vfpf_update_tunn_param_tlv *p_req,
2411                               struct ecore_tunn_update_type *p_tun,
2412                               struct ecore_tunn_update_udp_port *p_port,
2413                               enum ecore_tunn_mode mask,
2414                               u8 tun_cls, u8 update_port, u16 port)
2415 {
2416         if (update_port) {
2417                 p_port->b_update_port = true;
2418                 p_port->port = port;
2419         }
2420
2421         __ecore_iov_pf_update_tun_param(p_req, p_tun, mask, tun_cls);
2422 }
2423
2424 static bool
2425 ecore_iov_pf_validate_tunn_param(struct vfpf_update_tunn_param_tlv *p_req)
2426 {
2427         bool b_update_requested = false;
2428
2429         if (p_req->tun_mode_update_mask || p_req->update_tun_cls ||
2430             p_req->update_geneve_port || p_req->update_vxlan_port)
2431                 b_update_requested = true;
2432
2433         return b_update_requested;
2434 }
2435
2436 static void ecore_iov_vf_mbx_update_tunn_param(struct ecore_hwfn *p_hwfn,
2437                                                struct ecore_ptt *p_ptt,
2438                                                struct ecore_vf_info *p_vf)
2439 {
2440         struct ecore_tunnel_info *p_tun = &p_hwfn->p_dev->tunnel;
2441         struct ecore_iov_vf_mbx *mbx = &p_vf->vf_mbx;
2442         struct pfvf_update_tunn_param_tlv *p_resp;
2443         struct vfpf_update_tunn_param_tlv *p_req;
2444         enum _ecore_status_t rc = ECORE_SUCCESS;
2445         u8 status = PFVF_STATUS_SUCCESS;
2446         bool b_update_required = false;
2447         struct ecore_tunnel_info tunn;
2448         u16 tunn_feature_mask = 0;
2449         int i;
2450
2451         mbx->offset = (u8 *)mbx->reply_virt;
2452
2453         OSAL_MEM_ZERO(&tunn, sizeof(tunn));
2454         p_req = &mbx->req_virt->tunn_param_update;
2455
2456         if (!ecore_iov_pf_validate_tunn_param(p_req)) {
2457                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2458                            "No tunnel update requested by VF\n");
2459                 status = PFVF_STATUS_FAILURE;
2460                 goto send_resp;
2461         }
2462
2463         tunn.b_update_rx_cls = p_req->update_tun_cls;
2464         tunn.b_update_tx_cls = p_req->update_tun_cls;
2465
2466         ecore_iov_pf_update_tun_param(p_req, &tunn.vxlan, &tunn.vxlan_port,
2467                                       ECORE_MODE_VXLAN_TUNN, p_req->vxlan_clss,
2468                                       p_req->update_vxlan_port,
2469                                       p_req->vxlan_port);
2470         ecore_iov_pf_update_tun_param(p_req, &tunn.l2_geneve, &tunn.geneve_port,
2471                                       ECORE_MODE_L2GENEVE_TUNN,
2472                                       p_req->l2geneve_clss,
2473                                       p_req->update_geneve_port,
2474                                       p_req->geneve_port);
2475         __ecore_iov_pf_update_tun_param(p_req, &tunn.ip_geneve,
2476                                         ECORE_MODE_IPGENEVE_TUNN,
2477                                         p_req->ipgeneve_clss);
2478         __ecore_iov_pf_update_tun_param(p_req, &tunn.l2_gre,
2479                                         ECORE_MODE_L2GRE_TUNN,
2480                                         p_req->l2gre_clss);
2481         __ecore_iov_pf_update_tun_param(p_req, &tunn.ip_gre,
2482                                         ECORE_MODE_IPGRE_TUNN,
2483                                         p_req->ipgre_clss);
2484
2485         /* If PF modifies VF's req then it should
2486          * still return an error in case of partial configuration
2487          * or modified configuration as opposed to requested one.
2488          */
2489         rc = OSAL_PF_VALIDATE_MODIFY_TUNN_CONFIG(p_hwfn, &tunn_feature_mask,
2490                                                  &b_update_required, &tunn);
2491
2492         if (rc != ECORE_SUCCESS)
2493                 status = PFVF_STATUS_FAILURE;
2494
2495         /* If ECORE client is willing to update anything ? */
2496         if (b_update_required) {
2497                 u16 geneve_port;
2498
2499                 rc = ecore_sp_pf_update_tunn_cfg(p_hwfn, p_ptt, &tunn,
2500                                                  ECORE_SPQ_MODE_EBLOCK,
2501                                                  OSAL_NULL);
2502                 if (rc != ECORE_SUCCESS)
2503                         status = PFVF_STATUS_FAILURE;
2504
2505                 geneve_port = p_tun->geneve_port.port;
2506                 ecore_for_each_vf(p_hwfn, i) {
2507                         ecore_iov_bulletin_set_udp_ports(p_hwfn, i,
2508                                                          p_tun->vxlan_port.port,
2509                                                          geneve_port);
2510                 }
2511         }
2512
2513 send_resp:
2514         p_resp = ecore_add_tlv(&mbx->offset,
2515                                CHANNEL_TLV_UPDATE_TUNN_PARAM, sizeof(*p_resp));
2516
2517         ecore_iov_pf_update_tun_response(p_resp, p_tun, tunn_feature_mask);
2518         ecore_add_tlv(&mbx->offset, CHANNEL_TLV_LIST_END,
2519                       sizeof(struct channel_list_end_tlv));
2520
2521         ecore_iov_send_response(p_hwfn, p_ptt, p_vf, sizeof(*p_resp), status);
2522 }
2523
2524 static void ecore_iov_vf_mbx_start_txq_resp(struct ecore_hwfn *p_hwfn,
2525                                             struct ecore_ptt *p_ptt,
2526                                             struct ecore_vf_info *p_vf,
2527                                             u32 cid,
2528                                             u8 status)
2529 {
2530         struct ecore_iov_vf_mbx *mbx = &p_vf->vf_mbx;
2531         struct pfvf_start_queue_resp_tlv *p_tlv;
2532         bool b_legacy = false;
2533         u16 length;
2534
2535         mbx->offset = (u8 *)mbx->reply_virt;
2536
2537         /* Taking a bigger struct instead of adding a TLV to list was a
2538          * mistake, but one which we're now stuck with, as some older
2539          * clients assume the size of the previous response.
2540          */
2541         if (p_vf->acquire.vfdev_info.eth_fp_hsi_minor ==
2542             ETH_HSI_VER_NO_PKT_LEN_TUNN)
2543                 b_legacy = true;
2544
2545         if (!b_legacy)
2546                 length = sizeof(*p_tlv);
2547         else
2548                 length = sizeof(struct pfvf_def_resp_tlv);
2549
2550         p_tlv = ecore_add_tlv(&mbx->offset, CHANNEL_TLV_START_TXQ, length);
2551         ecore_add_tlv(&mbx->offset, CHANNEL_TLV_LIST_END,
2552                       sizeof(struct channel_list_end_tlv));
2553
2554         /* Update the TLV with the response */
2555         if ((status == PFVF_STATUS_SUCCESS) && !b_legacy)
2556                 p_tlv->offset = DB_ADDR_VF(cid, DQ_DEMS_LEGACY);
2557
2558         ecore_iov_send_response(p_hwfn, p_ptt, p_vf, length, status);
2559 }
2560
2561 static void ecore_iov_vf_mbx_start_txq(struct ecore_hwfn *p_hwfn,
2562                                        struct ecore_ptt *p_ptt,
2563                                        struct ecore_vf_info *vf)
2564 {
2565         struct ecore_queue_start_common_params params;
2566         struct ecore_queue_cid_vf_params vf_params;
2567         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2568         u8 status = PFVF_STATUS_NO_RESOURCE;
2569         struct ecore_vf_queue *p_queue;
2570         struct vfpf_start_txq_tlv *req;
2571         struct ecore_queue_cid *p_cid;
2572         struct ecore_sb_info sb_dummy;
2573         u8 qid_usage_idx, vf_legacy;
2574         u32 cid = 0;
2575         enum _ecore_status_t rc;
2576         u16 pq;
2577
2578         OSAL_MEMSET(&params, 0, sizeof(params));
2579         req = &mbx->req_virt->start_txq;
2580
2581         if (!ecore_iov_validate_txq(p_hwfn, vf, req->tx_qid,
2582                                     ECORE_IOV_VALIDATE_Q_NA) ||
2583             !ecore_iov_validate_sb(p_hwfn, vf, req->hw_sb))
2584                 goto out;
2585
2586         qid_usage_idx = ecore_iov_vf_mbx_qid(p_hwfn, vf, true);
2587         if (qid_usage_idx == ECORE_IOV_QID_INVALID)
2588                 goto out;
2589
2590         p_queue = &vf->vf_queues[req->tx_qid];
2591         if (p_queue->cids[qid_usage_idx].p_cid)
2592                 goto out;
2593
2594         vf_legacy = ecore_vf_calculate_legacy(vf);
2595
2596         /* Acquire a new queue-cid */
2597         params.queue_id = p_queue->fw_tx_qid;
2598         params.vport_id = vf->vport_id;
2599         params.stats_id = vf->abs_vf_id + 0x10;
2600
2601         /* Since IGU index is passed via sb_info, construct a dummy one */
2602         OSAL_MEM_ZERO(&sb_dummy, sizeof(sb_dummy));
2603         sb_dummy.igu_sb_id = req->hw_sb;
2604         params.p_sb = &sb_dummy;
2605         params.sb_idx = req->sb_index;
2606
2607         OSAL_MEM_ZERO(&vf_params, sizeof(vf_params));
2608         vf_params.vfid = vf->relative_vf_id;
2609         vf_params.vf_qid = (u8)req->tx_qid;
2610         vf_params.vf_legacy = vf_legacy;
2611         vf_params.qid_usage_idx = qid_usage_idx;
2612
2613         p_cid = ecore_eth_queue_to_cid(p_hwfn, vf->opaque_fid,
2614                                        &params, false, &vf_params);
2615         if (p_cid == OSAL_NULL)
2616                 goto out;
2617
2618         pq = ecore_get_cm_pq_idx_vf(p_hwfn,
2619                                     vf->relative_vf_id);
2620         rc = ecore_eth_txq_start_ramrod(p_hwfn, p_cid,
2621                                         req->pbl_addr, req->pbl_size, pq);
2622         if (rc != ECORE_SUCCESS) {
2623                 status = PFVF_STATUS_FAILURE;
2624                 ecore_eth_queue_cid_release(p_hwfn, p_cid);
2625         } else {
2626                 status = PFVF_STATUS_SUCCESS;
2627                 p_queue->cids[qid_usage_idx].p_cid = p_cid;
2628                 p_queue->cids[qid_usage_idx].b_is_tx = true;
2629                 cid = p_cid->cid;
2630         }
2631
2632 out:
2633         ecore_iov_vf_mbx_start_txq_resp(p_hwfn, p_ptt, vf,
2634                                         cid, status);
2635 }
2636
2637 static enum _ecore_status_t ecore_iov_vf_stop_rxqs(struct ecore_hwfn *p_hwfn,
2638                                                    struct ecore_vf_info *vf,
2639                                                    u16 rxq_id,
2640                                                    u8 qid_usage_idx,
2641                                                    bool cqe_completion)
2642 {
2643         struct ecore_vf_queue *p_queue;
2644         enum _ecore_status_t rc = ECORE_SUCCESS;
2645
2646         if (!ecore_iov_validate_rxq(p_hwfn, vf, rxq_id,
2647                                     ECORE_IOV_VALIDATE_Q_NA)) {
2648                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2649                            "VF[%d] Tried Closing Rx 0x%04x.%02x which is inactive\n",
2650                            vf->relative_vf_id, rxq_id, qid_usage_idx);
2651                 return ECORE_INVAL;
2652         }
2653
2654         p_queue = &vf->vf_queues[rxq_id];
2655
2656         /* We've validated the index and the existence of the active RXQ -
2657          * now we need to make sure that it's using the correct qid.
2658          */
2659         if (!p_queue->cids[qid_usage_idx].p_cid ||
2660             p_queue->cids[qid_usage_idx].b_is_tx) {
2661                 struct ecore_queue_cid *p_cid;
2662
2663                 p_cid = ecore_iov_get_vf_rx_queue_cid(p_queue);
2664                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2665                            "VF[%d] - Tried Closing Rx 0x%04x.%02x, but Rx is at %04x.%02x\n",
2666                             vf->relative_vf_id, rxq_id, qid_usage_idx,
2667                             rxq_id, p_cid->qid_usage_idx);
2668                 return ECORE_INVAL;
2669         }
2670
2671         /* Now that we know we have a valid Rx-queue - close it */
2672         rc = ecore_eth_rx_queue_stop(p_hwfn,
2673                                      p_queue->cids[qid_usage_idx].p_cid,
2674                                      false, cqe_completion);
2675         if (rc != ECORE_SUCCESS)
2676                 return rc;
2677
2678         p_queue->cids[qid_usage_idx].p_cid = OSAL_NULL;
2679         vf->num_active_rxqs--;
2680
2681         return ECORE_SUCCESS;
2682 }
2683
2684 static enum _ecore_status_t ecore_iov_vf_stop_txqs(struct ecore_hwfn *p_hwfn,
2685                                                    struct ecore_vf_info *vf,
2686                                                    u16 txq_id,
2687                                                    u8 qid_usage_idx)
2688 {
2689         struct ecore_vf_queue *p_queue;
2690         enum _ecore_status_t rc = ECORE_SUCCESS;
2691
2692         if (!ecore_iov_validate_txq(p_hwfn, vf, txq_id,
2693                                     ECORE_IOV_VALIDATE_Q_NA))
2694                 return ECORE_INVAL;
2695
2696         p_queue = &vf->vf_queues[txq_id];
2697         if (!p_queue->cids[qid_usage_idx].p_cid ||
2698             !p_queue->cids[qid_usage_idx].b_is_tx)
2699                 return ECORE_INVAL;
2700
2701         rc = ecore_eth_tx_queue_stop(p_hwfn,
2702                                      p_queue->cids[qid_usage_idx].p_cid);
2703         if (rc != ECORE_SUCCESS)
2704                 return rc;
2705
2706         p_queue->cids[qid_usage_idx].p_cid = OSAL_NULL;
2707         return ECORE_SUCCESS;
2708 }
2709
2710 static void ecore_iov_vf_mbx_stop_rxqs(struct ecore_hwfn *p_hwfn,
2711                                        struct ecore_ptt *p_ptt,
2712                                        struct ecore_vf_info *vf)
2713 {
2714         u16 length = sizeof(struct pfvf_def_resp_tlv);
2715         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2716         u8 status = PFVF_STATUS_FAILURE;
2717         struct vfpf_stop_rxqs_tlv *req;
2718         u8 qid_usage_idx;
2719         enum _ecore_status_t rc;
2720
2721         /* Starting with CHANNEL_TLV_QID, it's assumed the 'num_rxqs'
2722          * would be one. Since no older ecore passed multiple queues
2723          * using this API, sanitize on the value.
2724          */
2725         req = &mbx->req_virt->stop_rxqs;
2726         if (req->num_rxqs != 1) {
2727                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2728                            "Odd; VF[%d] tried stopping multiple Rx queues\n",
2729                            vf->relative_vf_id);
2730                 status = PFVF_STATUS_NOT_SUPPORTED;
2731                 goto out;
2732         }
2733
2734         /* Find which qid-index is associated with the queue */
2735         qid_usage_idx = ecore_iov_vf_mbx_qid(p_hwfn, vf, false);
2736         if (qid_usage_idx == ECORE_IOV_QID_INVALID)
2737                 goto out;
2738
2739         rc = ecore_iov_vf_stop_rxqs(p_hwfn, vf, req->rx_qid,
2740                                     qid_usage_idx, req->cqe_completion);
2741         if (rc == ECORE_SUCCESS)
2742                 status = PFVF_STATUS_SUCCESS;
2743 out:
2744         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_STOP_RXQS,
2745                                length, status);
2746 }
2747
2748 static void ecore_iov_vf_mbx_stop_txqs(struct ecore_hwfn *p_hwfn,
2749                                        struct ecore_ptt *p_ptt,
2750                                        struct ecore_vf_info *vf)
2751 {
2752         u16 length = sizeof(struct pfvf_def_resp_tlv);
2753         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2754         u8 status = PFVF_STATUS_FAILURE;
2755         struct vfpf_stop_txqs_tlv *req;
2756         u8 qid_usage_idx;
2757         enum _ecore_status_t rc;
2758
2759         /* Starting with CHANNEL_TLV_QID, it's assumed the 'num_txqs'
2760          * would be one. Since no older ecore passed multiple queues
2761          * using this API, sanitize on the value.
2762          */
2763         req = &mbx->req_virt->stop_txqs;
2764         if (req->num_txqs != 1) {
2765                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2766                            "Odd; VF[%d] tried stopping multiple Tx queues\n",
2767                            vf->relative_vf_id);
2768                 status = PFVF_STATUS_NOT_SUPPORTED;
2769                 goto out;
2770         }
2771
2772         /* Find which qid-index is associated with the queue */
2773         qid_usage_idx = ecore_iov_vf_mbx_qid(p_hwfn, vf, true);
2774         if (qid_usage_idx == ECORE_IOV_QID_INVALID)
2775                 goto out;
2776
2777         rc = ecore_iov_vf_stop_txqs(p_hwfn, vf, req->tx_qid,
2778                                     qid_usage_idx);
2779         if (rc == ECORE_SUCCESS)
2780                 status = PFVF_STATUS_SUCCESS;
2781
2782 out:
2783         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_STOP_TXQS,
2784                                length, status);
2785 }
2786
2787 static void ecore_iov_vf_mbx_update_rxqs(struct ecore_hwfn *p_hwfn,
2788                                          struct ecore_ptt *p_ptt,
2789                                          struct ecore_vf_info *vf)
2790 {
2791         struct ecore_queue_cid *handlers[ECORE_MAX_VF_CHAINS_PER_PF];
2792         u16 length = sizeof(struct pfvf_def_resp_tlv);
2793         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2794         struct vfpf_update_rxq_tlv *req;
2795         u8 status = PFVF_STATUS_FAILURE;
2796         u8 complete_event_flg;
2797         u8 complete_cqe_flg;
2798         u8 qid_usage_idx;
2799         enum _ecore_status_t rc;
2800         u16 i;
2801
2802         req = &mbx->req_virt->update_rxq;
2803         complete_cqe_flg = !!(req->flags & VFPF_RXQ_UPD_COMPLETE_CQE_FLAG);
2804         complete_event_flg = !!(req->flags & VFPF_RXQ_UPD_COMPLETE_EVENT_FLAG);
2805
2806         qid_usage_idx = ecore_iov_vf_mbx_qid(p_hwfn, vf, false);
2807         if (qid_usage_idx == ECORE_IOV_QID_INVALID)
2808                 goto out;
2809
2810         /* Starting with the addition of CHANNEL_TLV_QID, this API started
2811          * expecting a single queue at a time. Validate this.
2812          */
2813         if ((vf->acquire.vfdev_info.capabilities &
2814              VFPF_ACQUIRE_CAP_QUEUE_QIDS) &&
2815              req->num_rxqs != 1) {
2816                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2817                            "VF[%d] supports QIDs but sends multiple queues\n",
2818                            vf->relative_vf_id);
2819                 goto out;
2820         }
2821
2822         /* Validate inputs - for the legacy case this is still true since
2823          * qid_usage_idx for each Rx queue would be LEGACY_QID_RX.
2824          */
2825         for (i = req->rx_qid; i < req->rx_qid + req->num_rxqs; i++) {
2826                 if (!ecore_iov_validate_rxq(p_hwfn, vf, i,
2827                                             ECORE_IOV_VALIDATE_Q_NA) ||
2828                     !vf->vf_queues[i].cids[qid_usage_idx].p_cid ||
2829                     vf->vf_queues[i].cids[qid_usage_idx].b_is_tx) {
2830                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2831                                    "VF[%d]: Incorrect Rxqs [%04x, %02x]\n",
2832                                    vf->relative_vf_id, req->rx_qid,
2833                                    req->num_rxqs);
2834                         goto out;
2835                 }
2836         }
2837
2838         for (i = 0; i < req->num_rxqs; i++) {
2839                 u16 qid = req->rx_qid + i;
2840
2841                 handlers[i] = vf->vf_queues[qid].cids[qid_usage_idx].p_cid;
2842         }
2843
2844         rc = ecore_sp_eth_rx_queues_update(p_hwfn, (void **)&handlers,
2845                                            req->num_rxqs,
2846                                            complete_cqe_flg,
2847                                            complete_event_flg,
2848                                            ECORE_SPQ_MODE_EBLOCK,
2849                                            OSAL_NULL);
2850         if (rc != ECORE_SUCCESS)
2851                 goto out;
2852
2853         status = PFVF_STATUS_SUCCESS;
2854 out:
2855         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_UPDATE_RXQ,
2856                                length, status);
2857 }
2858
2859 static enum _ecore_status_t
2860 ecore_iov_vf_pf_update_mtu(struct ecore_hwfn *p_hwfn,
2861                                     struct ecore_ptt *p_ptt,
2862                                     struct ecore_vf_info *p_vf)
2863 {
2864         struct ecore_iov_vf_mbx *mbx = &p_vf->vf_mbx;
2865         struct ecore_sp_vport_update_params params;
2866         enum _ecore_status_t rc = ECORE_SUCCESS;
2867         struct vfpf_update_mtu_tlv *p_req;
2868         u8 status = PFVF_STATUS_SUCCESS;
2869
2870         /* Valiate PF can send such a request */
2871         if (!p_vf->vport_instance) {
2872                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2873                            "No VPORT instance available for VF[%d], failing MTU update\n",
2874                            p_vf->abs_vf_id);
2875                 status = PFVF_STATUS_FAILURE;
2876                 goto send_status;
2877         }
2878
2879         p_req = &mbx->req_virt->update_mtu;
2880
2881         OSAL_MEMSET(&params, 0, sizeof(params));
2882         params.opaque_fid =  p_vf->opaque_fid;
2883         params.vport_id = p_vf->vport_id;
2884         params.mtu = p_req->mtu;
2885         rc = ecore_sp_vport_update(p_hwfn, &params, ECORE_SPQ_MODE_EBLOCK,
2886                                    OSAL_NULL);
2887
2888         if (rc)
2889                 status = PFVF_STATUS_FAILURE;
2890 send_status:
2891         ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf,
2892                                CHANNEL_TLV_UPDATE_MTU,
2893                                sizeof(struct pfvf_def_resp_tlv),
2894                                status);
2895         return rc;
2896 }
2897
2898 void *ecore_iov_search_list_tlvs(struct ecore_hwfn *p_hwfn,
2899                                  void *p_tlvs_list, u16 req_type)
2900 {
2901         struct channel_tlv *p_tlv = (struct channel_tlv *)p_tlvs_list;
2902         int len = 0;
2903
2904         do {
2905                 if (!p_tlv->length) {
2906                         DP_NOTICE(p_hwfn, true, "Zero length TLV found\n");
2907                         return OSAL_NULL;
2908                 }
2909
2910                 if (p_tlv->type == req_type) {
2911                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2912                                    "Extended tlv type %s, length %d found\n",
2913                                    ecore_channel_tlvs_string[p_tlv->type],
2914                                    p_tlv->length);
2915                         return p_tlv;
2916                 }
2917
2918                 len += p_tlv->length;
2919                 p_tlv = (struct channel_tlv *)((u8 *)p_tlv + p_tlv->length);
2920
2921                 if ((len + p_tlv->length) > TLV_BUFFER_SIZE) {
2922                         DP_NOTICE(p_hwfn, true,
2923                                   "TLVs has overrun the buffer size\n");
2924                         return OSAL_NULL;
2925                 }
2926         } while (p_tlv->type != CHANNEL_TLV_LIST_END);
2927
2928         return OSAL_NULL;
2929 }
2930
2931 static void
2932 ecore_iov_vp_update_act_param(struct ecore_hwfn *p_hwfn,
2933                               struct ecore_sp_vport_update_params *p_data,
2934                               struct ecore_iov_vf_mbx *p_mbx, u16 *tlvs_mask)
2935 {
2936         struct vfpf_vport_update_activate_tlv *p_act_tlv;
2937         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_ACTIVATE;
2938
2939         p_act_tlv = (struct vfpf_vport_update_activate_tlv *)
2940             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2941         if (!p_act_tlv)
2942                 return;
2943
2944         p_data->update_vport_active_rx_flg = p_act_tlv->update_rx;
2945         p_data->vport_active_rx_flg = p_act_tlv->active_rx;
2946         p_data->update_vport_active_tx_flg = p_act_tlv->update_tx;
2947         p_data->vport_active_tx_flg = p_act_tlv->active_tx;
2948         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_ACTIVATE;
2949 }
2950
2951 static void
2952 ecore_iov_vp_update_vlan_param(struct ecore_hwfn *p_hwfn,
2953                                struct ecore_sp_vport_update_params *p_data,
2954                                struct ecore_vf_info *p_vf,
2955                                struct ecore_iov_vf_mbx *p_mbx, u16 *tlvs_mask)
2956 {
2957         struct vfpf_vport_update_vlan_strip_tlv *p_vlan_tlv;
2958         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_VLAN_STRIP;
2959
2960         p_vlan_tlv = (struct vfpf_vport_update_vlan_strip_tlv *)
2961             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2962         if (!p_vlan_tlv)
2963                 return;
2964
2965         p_vf->shadow_config.inner_vlan_removal = p_vlan_tlv->remove_vlan;
2966
2967         /* Ignore the VF request if we're forcing a vlan */
2968         if (!(p_vf->configured_features & (1 << VLAN_ADDR_FORCED))) {
2969                 p_data->update_inner_vlan_removal_flg = 1;
2970                 p_data->inner_vlan_removal_flg = p_vlan_tlv->remove_vlan;
2971         }
2972
2973         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_VLAN_STRIP;
2974 }
2975
2976 static void
2977 ecore_iov_vp_update_tx_switch(struct ecore_hwfn *p_hwfn,
2978                               struct ecore_sp_vport_update_params *p_data,
2979                               struct ecore_iov_vf_mbx *p_mbx, u16 *tlvs_mask)
2980 {
2981         struct vfpf_vport_update_tx_switch_tlv *p_tx_switch_tlv;
2982         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_TX_SWITCH;
2983
2984         p_tx_switch_tlv = (struct vfpf_vport_update_tx_switch_tlv *)
2985             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2986         if (!p_tx_switch_tlv)
2987                 return;
2988
2989 #ifndef ASIC_ONLY
2990         if (CHIP_REV_IS_FPGA(p_hwfn->p_dev)) {
2991                 DP_NOTICE(p_hwfn, false,
2992                           "FPGA: Ignore tx-switching configuration originating"
2993                           " from VFs\n");
2994                 return;
2995         }
2996 #endif
2997
2998         p_data->update_tx_switching_flg = 1;
2999         p_data->tx_switching_flg = p_tx_switch_tlv->tx_switching;
3000         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_TX_SWITCH;
3001 }
3002
3003 static void
3004 ecore_iov_vp_update_mcast_bin_param(struct ecore_hwfn *p_hwfn,
3005                                     struct ecore_sp_vport_update_params *p_data,
3006                                     struct ecore_iov_vf_mbx *p_mbx,
3007                                     u16 *tlvs_mask)
3008 {
3009         struct vfpf_vport_update_mcast_bin_tlv *p_mcast_tlv;
3010         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_MCAST;
3011
3012         p_mcast_tlv = (struct vfpf_vport_update_mcast_bin_tlv *)
3013             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
3014         if (!p_mcast_tlv)
3015                 return;
3016
3017         p_data->update_approx_mcast_flg = 1;
3018         OSAL_MEMCPY(p_data->bins, p_mcast_tlv->bins,
3019                     sizeof(u32) * ETH_MULTICAST_MAC_BINS_IN_REGS);
3020         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_MCAST;
3021 }
3022
3023 static void
3024 ecore_iov_vp_update_accept_flag(struct ecore_hwfn *p_hwfn,
3025                                 struct ecore_sp_vport_update_params *p_data,
3026                                 struct ecore_iov_vf_mbx *p_mbx, u16 *tlvs_mask)
3027 {
3028         struct ecore_filter_accept_flags *p_flags = &p_data->accept_flags;
3029         struct vfpf_vport_update_accept_param_tlv *p_accept_tlv;
3030         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_ACCEPT_PARAM;
3031
3032         p_accept_tlv = (struct vfpf_vport_update_accept_param_tlv *)
3033             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
3034         if (!p_accept_tlv)
3035                 return;
3036
3037         p_flags->update_rx_mode_config = p_accept_tlv->update_rx_mode;
3038         p_flags->rx_accept_filter = p_accept_tlv->rx_accept_filter;
3039         p_flags->update_tx_mode_config = p_accept_tlv->update_tx_mode;
3040         p_flags->tx_accept_filter = p_accept_tlv->tx_accept_filter;
3041         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_ACCEPT_PARAM;
3042 }
3043
3044 static void
3045 ecore_iov_vp_update_accept_any_vlan(struct ecore_hwfn *p_hwfn,
3046                                     struct ecore_sp_vport_update_params *p_data,
3047                                     struct ecore_iov_vf_mbx *p_mbx,
3048                                     u16 *tlvs_mask)
3049 {
3050         struct vfpf_vport_update_accept_any_vlan_tlv *p_accept_any_vlan;
3051         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_ACCEPT_ANY_VLAN;
3052
3053         p_accept_any_vlan = (struct vfpf_vport_update_accept_any_vlan_tlv *)
3054             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
3055         if (!p_accept_any_vlan)
3056                 return;
3057
3058         p_data->accept_any_vlan = p_accept_any_vlan->accept_any_vlan;
3059         p_data->update_accept_any_vlan_flg =
3060                         p_accept_any_vlan->update_accept_any_vlan_flg;
3061         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_ACCEPT_ANY_VLAN;
3062 }
3063
3064 static void
3065 ecore_iov_vp_update_rss_param(struct ecore_hwfn *p_hwfn,
3066                               struct ecore_vf_info *vf,
3067                               struct ecore_sp_vport_update_params *p_data,
3068                               struct ecore_rss_params *p_rss,
3069                               struct ecore_iov_vf_mbx *p_mbx,
3070                               u16 *tlvs_mask, u16 *tlvs_accepted)
3071 {
3072         struct vfpf_vport_update_rss_tlv *p_rss_tlv;
3073         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_RSS;
3074         bool b_reject = false;
3075         u16 table_size;
3076         u16 i, q_idx;
3077
3078         p_rss_tlv = (struct vfpf_vport_update_rss_tlv *)
3079             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
3080         if (!p_rss_tlv) {
3081                 p_data->rss_params = OSAL_NULL;
3082                 return;
3083         }
3084
3085         OSAL_MEMSET(p_rss, 0, sizeof(struct ecore_rss_params));
3086
3087         p_rss->update_rss_config =
3088             !!(p_rss_tlv->update_rss_flags &
3089                 VFPF_UPDATE_RSS_CONFIG_FLAG);
3090         p_rss->update_rss_capabilities =
3091             !!(p_rss_tlv->update_rss_flags &
3092                 VFPF_UPDATE_RSS_CAPS_FLAG);
3093         p_rss->update_rss_ind_table =
3094             !!(p_rss_tlv->update_rss_flags &
3095                 VFPF_UPDATE_RSS_IND_TABLE_FLAG);
3096         p_rss->update_rss_key =
3097             !!(p_rss_tlv->update_rss_flags &
3098                 VFPF_UPDATE_RSS_KEY_FLAG);
3099
3100         p_rss->rss_enable = p_rss_tlv->rss_enable;
3101         p_rss->rss_eng_id = vf->rss_eng_id;
3102         p_rss->rss_caps = p_rss_tlv->rss_caps;
3103         p_rss->rss_table_size_log = p_rss_tlv->rss_table_size_log;
3104         OSAL_MEMCPY(p_rss->rss_key, p_rss_tlv->rss_key,
3105                     sizeof(p_rss->rss_key));
3106
3107         table_size = OSAL_MIN_T(u16, OSAL_ARRAY_SIZE(p_rss->rss_ind_table),
3108                                 (1 << p_rss_tlv->rss_table_size_log));
3109
3110         for (i = 0; i < table_size; i++) {
3111                 struct ecore_queue_cid *p_cid;
3112
3113                 q_idx = p_rss_tlv->rss_ind_table[i];
3114                 if (!ecore_iov_validate_rxq(p_hwfn, vf, q_idx,
3115                                             ECORE_IOV_VALIDATE_Q_ENABLE)) {
3116                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3117                                    "VF[%d]: Omitting RSS due to wrong queue %04x\n",
3118                                    vf->relative_vf_id, q_idx);
3119                         b_reject = true;
3120                         goto out;
3121                 }
3122
3123                 p_cid = ecore_iov_get_vf_rx_queue_cid(&vf->vf_queues[q_idx]);
3124                 p_rss->rss_ind_table[i] = p_cid;
3125         }
3126
3127         p_data->rss_params = p_rss;
3128 out:
3129         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_RSS;
3130         if (!b_reject)
3131                 *tlvs_accepted |= 1 << ECORE_IOV_VP_UPDATE_RSS;
3132 }
3133
3134 static void
3135 ecore_iov_vp_update_sge_tpa_param(struct ecore_hwfn *p_hwfn,
3136                                   struct ecore_sp_vport_update_params *p_data,
3137                                   struct ecore_sge_tpa_params *p_sge_tpa,
3138                                   struct ecore_iov_vf_mbx *p_mbx,
3139                                   u16 *tlvs_mask)
3140 {
3141         struct vfpf_vport_update_sge_tpa_tlv *p_sge_tpa_tlv;
3142         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_SGE_TPA;
3143
3144         p_sge_tpa_tlv = (struct vfpf_vport_update_sge_tpa_tlv *)
3145             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
3146
3147         if (!p_sge_tpa_tlv) {
3148                 p_data->sge_tpa_params = OSAL_NULL;
3149                 return;
3150         }
3151
3152         OSAL_MEMSET(p_sge_tpa, 0, sizeof(struct ecore_sge_tpa_params));
3153
3154         p_sge_tpa->update_tpa_en_flg =
3155             !!(p_sge_tpa_tlv->update_sge_tpa_flags & VFPF_UPDATE_TPA_EN_FLAG);
3156         p_sge_tpa->update_tpa_param_flg =
3157             !!(p_sge_tpa_tlv->update_sge_tpa_flags &
3158                 VFPF_UPDATE_TPA_PARAM_FLAG);
3159
3160         p_sge_tpa->tpa_ipv4_en_flg =
3161             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_IPV4_EN_FLAG);
3162         p_sge_tpa->tpa_ipv6_en_flg =
3163             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_IPV6_EN_FLAG);
3164         p_sge_tpa->tpa_pkt_split_flg =
3165             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_PKT_SPLIT_FLAG);
3166         p_sge_tpa->tpa_hdr_data_split_flg =
3167             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_HDR_DATA_SPLIT_FLAG);
3168         p_sge_tpa->tpa_gro_consistent_flg =
3169             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_GRO_CONSIST_FLAG);
3170
3171         p_sge_tpa->tpa_max_aggs_num = p_sge_tpa_tlv->tpa_max_aggs_num;
3172         p_sge_tpa->tpa_max_size = p_sge_tpa_tlv->tpa_max_size;
3173         p_sge_tpa->tpa_min_size_to_start = p_sge_tpa_tlv->tpa_min_size_to_start;
3174         p_sge_tpa->tpa_min_size_to_cont = p_sge_tpa_tlv->tpa_min_size_to_cont;
3175         p_sge_tpa->max_buffers_per_cqe = p_sge_tpa_tlv->max_buffers_per_cqe;
3176
3177         p_data->sge_tpa_params = p_sge_tpa;
3178
3179         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_SGE_TPA;
3180 }
3181
3182 static void ecore_iov_vf_mbx_vport_update(struct ecore_hwfn *p_hwfn,
3183                                           struct ecore_ptt *p_ptt,
3184                                           struct ecore_vf_info *vf)
3185 {
3186         struct ecore_rss_params *p_rss_params = OSAL_NULL;
3187         struct ecore_sp_vport_update_params params;
3188         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
3189         struct ecore_sge_tpa_params sge_tpa_params;
3190         u16 tlvs_mask = 0, tlvs_accepted = 0;
3191         u8 status = PFVF_STATUS_SUCCESS;
3192         u16 length;
3193         enum _ecore_status_t rc;
3194
3195         /* Valiate PF can send such a request */
3196         if (!vf->vport_instance) {
3197                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3198                            "No VPORT instance available for VF[%d],"
3199                            " failing vport update\n",
3200                            vf->abs_vf_id);
3201                 status = PFVF_STATUS_FAILURE;
3202                 goto out;
3203         }
3204
3205         p_rss_params = OSAL_VZALLOC(p_hwfn->p_dev, sizeof(*p_rss_params));
3206         if (p_rss_params == OSAL_NULL) {
3207                 status = PFVF_STATUS_FAILURE;
3208                 goto out;
3209         }
3210
3211         OSAL_MEMSET(&params, 0, sizeof(params));
3212         params.opaque_fid = vf->opaque_fid;
3213         params.vport_id = vf->vport_id;
3214         params.rss_params = OSAL_NULL;
3215
3216         /* Search for extended tlvs list and update values
3217          * from VF in struct ecore_sp_vport_update_params.
3218          */
3219         ecore_iov_vp_update_act_param(p_hwfn, &params, mbx, &tlvs_mask);
3220         ecore_iov_vp_update_vlan_param(p_hwfn, &params, vf, mbx, &tlvs_mask);
3221         ecore_iov_vp_update_tx_switch(p_hwfn, &params, mbx, &tlvs_mask);
3222         ecore_iov_vp_update_mcast_bin_param(p_hwfn, &params, mbx, &tlvs_mask);
3223         ecore_iov_vp_update_accept_flag(p_hwfn, &params, mbx, &tlvs_mask);
3224         ecore_iov_vp_update_accept_any_vlan(p_hwfn, &params, mbx, &tlvs_mask);
3225         ecore_iov_vp_update_sge_tpa_param(p_hwfn, &params,
3226                                           &sge_tpa_params, mbx, &tlvs_mask);
3227
3228         tlvs_accepted = tlvs_mask;
3229
3230         /* Some of the extended TLVs need to be validated first; In that case,
3231          * they can update the mask without updating the accepted [so that
3232          * PF could communicate to VF it has rejected request].
3233          */
3234         ecore_iov_vp_update_rss_param(p_hwfn, vf, &params, p_rss_params,
3235                                       mbx, &tlvs_mask, &tlvs_accepted);
3236
3237         /* Just log a message if there is no single extended tlv in buffer.
3238          * When all features of vport update ramrod would be requested by VF
3239          * as extended TLVs in buffer then an error can be returned in response
3240          * if there is no extended TLV present in buffer.
3241          */
3242         if (OSAL_IOV_VF_VPORT_UPDATE(p_hwfn, vf->relative_vf_id,
3243                                      &params, &tlvs_accepted) !=
3244             ECORE_SUCCESS) {
3245                 tlvs_accepted = 0;
3246                 status = PFVF_STATUS_NOT_SUPPORTED;
3247                 goto out;
3248         }
3249
3250         if (!tlvs_accepted) {
3251                 if (tlvs_mask)
3252                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3253                                    "Upper-layer prevents said VF"
3254                                    " configuration\n");
3255                 else
3256                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3257                                    "No feature tlvs found for vport update\n");
3258                 status = PFVF_STATUS_NOT_SUPPORTED;
3259                 goto out;
3260         }
3261
3262         rc = ecore_sp_vport_update(p_hwfn, &params, ECORE_SPQ_MODE_EBLOCK,
3263                                    OSAL_NULL);
3264
3265         if (rc)
3266                 status = PFVF_STATUS_FAILURE;
3267
3268 out:
3269         OSAL_VFREE(p_hwfn->p_dev, p_rss_params);
3270         length = ecore_iov_prep_vp_update_resp_tlvs(p_hwfn, vf, mbx, status,
3271                                                     tlvs_mask, tlvs_accepted);
3272         ecore_iov_send_response(p_hwfn, p_ptt, vf, length, status);
3273 }
3274
3275 static enum _ecore_status_t
3276 ecore_iov_vf_update_vlan_shadow(struct ecore_hwfn *p_hwfn,
3277                                 struct ecore_vf_info *p_vf,
3278                                 struct ecore_filter_ucast *p_params)
3279 {
3280         int i;
3281
3282         /* First remove entries and then add new ones */
3283         if (p_params->opcode == ECORE_FILTER_REMOVE) {
3284                 for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++)
3285                         if (p_vf->shadow_config.vlans[i].used &&
3286                             p_vf->shadow_config.vlans[i].vid ==
3287                             p_params->vlan) {
3288                                 p_vf->shadow_config.vlans[i].used = false;
3289                                 break;
3290                         }
3291                 if (i == ECORE_ETH_VF_NUM_VLAN_FILTERS + 1) {
3292                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3293                                    "VF [%d] - Tries to remove a non-existing"
3294                                    " vlan\n",
3295                                    p_vf->relative_vf_id);
3296                         return ECORE_INVAL;
3297                 }
3298         } else if (p_params->opcode == ECORE_FILTER_REPLACE ||
3299                    p_params->opcode == ECORE_FILTER_FLUSH) {
3300                 for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++)
3301                         p_vf->shadow_config.vlans[i].used = false;
3302         }
3303
3304         /* In forced mode, we're willing to remove entries - but we don't add
3305          * new ones.
3306          */
3307         if (p_vf->bulletin.p_virt->valid_bitmap & (1 << VLAN_ADDR_FORCED))
3308                 return ECORE_SUCCESS;
3309
3310         if (p_params->opcode == ECORE_FILTER_ADD ||
3311             p_params->opcode == ECORE_FILTER_REPLACE) {
3312                 for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++) {
3313                         if (p_vf->shadow_config.vlans[i].used)
3314                                 continue;
3315
3316                         p_vf->shadow_config.vlans[i].used = true;
3317                         p_vf->shadow_config.vlans[i].vid = p_params->vlan;
3318                         break;
3319                 }
3320
3321                 if (i == ECORE_ETH_VF_NUM_VLAN_FILTERS + 1) {
3322                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3323                                    "VF [%d] - Tries to configure more than %d"
3324                                    " vlan filters\n",
3325                                    p_vf->relative_vf_id,
3326                                    ECORE_ETH_VF_NUM_VLAN_FILTERS + 1);
3327                         return ECORE_INVAL;
3328                 }
3329         }
3330
3331         return ECORE_SUCCESS;
3332 }
3333
3334 static enum _ecore_status_t
3335 ecore_iov_vf_update_mac_shadow(struct ecore_hwfn *p_hwfn,
3336                                struct ecore_vf_info *p_vf,
3337                                struct ecore_filter_ucast *p_params)
3338 {
3339         char empty_mac[ETH_ALEN];
3340         int i;
3341
3342         OSAL_MEM_ZERO(empty_mac, ETH_ALEN);
3343
3344         /* If we're in forced-mode, we don't allow any change */
3345         /* TODO - this would change if we were ever to implement logic for
3346          * removing a forced MAC altogether [in which case, like for vlans,
3347          * we should be able to re-trace previous configuration.
3348          */
3349         if (p_vf->bulletin.p_virt->valid_bitmap & (1 << MAC_ADDR_FORCED))
3350                 return ECORE_SUCCESS;
3351
3352         /* First remove entries and then add new ones */
3353         if (p_params->opcode == ECORE_FILTER_REMOVE) {
3354                 for (i = 0; i < ECORE_ETH_VF_NUM_MAC_FILTERS; i++) {
3355                         if (!OSAL_MEMCMP(p_vf->shadow_config.macs[i],
3356                                          p_params->mac, ETH_ALEN)) {
3357                                 OSAL_MEM_ZERO(p_vf->shadow_config.macs[i],
3358                                               ETH_ALEN);
3359                                 break;
3360                         }
3361                 }
3362
3363                 if (i == ECORE_ETH_VF_NUM_MAC_FILTERS) {
3364                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3365                                    "MAC isn't configured\n");
3366                         return ECORE_INVAL;
3367                 }
3368         } else if (p_params->opcode == ECORE_FILTER_REPLACE ||
3369                    p_params->opcode == ECORE_FILTER_FLUSH) {
3370                 for (i = 0; i < ECORE_ETH_VF_NUM_MAC_FILTERS; i++)
3371                         OSAL_MEM_ZERO(p_vf->shadow_config.macs[i], ETH_ALEN);
3372         }
3373
3374         /* List the new MAC address */
3375         if (p_params->opcode != ECORE_FILTER_ADD &&
3376             p_params->opcode != ECORE_FILTER_REPLACE)
3377                 return ECORE_SUCCESS;
3378
3379         for (i = 0; i < ECORE_ETH_VF_NUM_MAC_FILTERS; i++) {
3380                 if (!OSAL_MEMCMP(p_vf->shadow_config.macs[i],
3381                                  empty_mac, ETH_ALEN)) {
3382                         OSAL_MEMCPY(p_vf->shadow_config.macs[i],
3383                                     p_params->mac, ETH_ALEN);
3384                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3385                                    "Added MAC at %d entry in shadow\n", i);
3386                         break;
3387                 }
3388         }
3389
3390         if (i == ECORE_ETH_VF_NUM_MAC_FILTERS) {
3391                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3392                            "No available place for MAC\n");
3393                 return ECORE_INVAL;
3394         }
3395
3396         return ECORE_SUCCESS;
3397 }
3398
3399 static enum _ecore_status_t
3400 ecore_iov_vf_update_unicast_shadow(struct ecore_hwfn *p_hwfn,
3401                                    struct ecore_vf_info *p_vf,
3402                                    struct ecore_filter_ucast *p_params)
3403 {
3404         enum _ecore_status_t rc = ECORE_SUCCESS;
3405
3406         if (p_params->type == ECORE_FILTER_MAC) {
3407                 rc = ecore_iov_vf_update_mac_shadow(p_hwfn, p_vf, p_params);
3408                 if (rc != ECORE_SUCCESS)
3409                         return rc;
3410         }
3411
3412         if (p_params->type == ECORE_FILTER_VLAN)
3413                 rc = ecore_iov_vf_update_vlan_shadow(p_hwfn, p_vf, p_params);
3414
3415         return rc;
3416 }
3417
3418 static void ecore_iov_vf_mbx_ucast_filter(struct ecore_hwfn *p_hwfn,
3419                                           struct ecore_ptt *p_ptt,
3420                                           struct ecore_vf_info *vf)
3421 {
3422         struct ecore_bulletin_content *p_bulletin = vf->bulletin.p_virt;
3423         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
3424         struct vfpf_ucast_filter_tlv *req;
3425         u8 status = PFVF_STATUS_SUCCESS;
3426         struct ecore_filter_ucast params;
3427         enum _ecore_status_t rc;
3428
3429         /* Prepare the unicast filter params */
3430         OSAL_MEMSET(&params, 0, sizeof(struct ecore_filter_ucast));
3431         req = &mbx->req_virt->ucast_filter;
3432         params.opcode = (enum ecore_filter_opcode)req->opcode;
3433         params.type = (enum ecore_filter_ucast_type)req->type;
3434
3435         /* @@@TBD - We might need logic on HV side in determining this */
3436         params.is_rx_filter = 1;
3437         params.is_tx_filter = 1;
3438         params.vport_to_remove_from = vf->vport_id;
3439         params.vport_to_add_to = vf->vport_id;
3440         OSAL_MEMCPY(params.mac, req->mac, ETH_ALEN);
3441         params.vlan = req->vlan;
3442
3443         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3444                    "VF[%d]: opcode 0x%02x type 0x%02x [%s %s] [vport 0x%02x]"
3445                    " MAC %02x:%02x:%02x:%02x:%02x:%02x, vlan 0x%04x\n",
3446                    vf->abs_vf_id, params.opcode, params.type,
3447                    params.is_rx_filter ? "RX" : "",
3448                    params.is_tx_filter ? "TX" : "",
3449                    params.vport_to_add_to,
3450                    params.mac[0], params.mac[1], params.mac[2],
3451                    params.mac[3], params.mac[4], params.mac[5], params.vlan);
3452
3453         if (!vf->vport_instance) {
3454                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3455                            "No VPORT instance available for VF[%d],"
3456                            " failing ucast MAC configuration\n",
3457                            vf->abs_vf_id);
3458                 status = PFVF_STATUS_FAILURE;
3459                 goto out;
3460         }
3461
3462         /* Update shadow copy of the VF configuration. In case shadow indicates
3463          * the action should be blocked return success to VF to imitate the
3464          * firmware behaviour in such case.
3465          */
3466         if (ecore_iov_vf_update_unicast_shadow(p_hwfn, vf, &params) !=
3467             ECORE_SUCCESS)
3468                 goto out;
3469
3470         /* Determine if the unicast filtering is acceptible by PF */
3471         if ((p_bulletin->valid_bitmap & (1 << VLAN_ADDR_FORCED)) &&
3472             (params.type == ECORE_FILTER_VLAN ||
3473              params.type == ECORE_FILTER_MAC_VLAN)) {
3474                 /* Once VLAN is forced or PVID is set, do not allow
3475                  * to add/replace any further VLANs.
3476                  */
3477                 if (params.opcode == ECORE_FILTER_ADD ||
3478                     params.opcode == ECORE_FILTER_REPLACE)
3479                         status = PFVF_STATUS_FORCED;
3480                 goto out;
3481         }
3482
3483         if ((p_bulletin->valid_bitmap & (1 << MAC_ADDR_FORCED)) &&
3484             (params.type == ECORE_FILTER_MAC ||
3485              params.type == ECORE_FILTER_MAC_VLAN)) {
3486                 if (OSAL_MEMCMP(p_bulletin->mac, params.mac, ETH_ALEN) ||
3487                     (params.opcode != ECORE_FILTER_ADD &&
3488                      params.opcode != ECORE_FILTER_REPLACE))
3489                         status = PFVF_STATUS_FORCED;
3490                 goto out;
3491         }
3492
3493         rc = OSAL_IOV_CHK_UCAST(p_hwfn, vf->relative_vf_id, &params);
3494         if (rc == ECORE_EXISTS) {
3495                 goto out;
3496         } else if (rc == ECORE_INVAL) {
3497                 status = PFVF_STATUS_FAILURE;
3498                 goto out;
3499         }
3500
3501         rc = ecore_sp_eth_filter_ucast(p_hwfn, vf->opaque_fid, &params,
3502                                        ECORE_SPQ_MODE_CB, OSAL_NULL);
3503         if (rc)
3504                 status = PFVF_STATUS_FAILURE;
3505
3506 out:
3507         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_UCAST_FILTER,
3508                                sizeof(struct pfvf_def_resp_tlv), status);
3509 }
3510
3511 static void ecore_iov_vf_mbx_int_cleanup(struct ecore_hwfn *p_hwfn,
3512                                          struct ecore_ptt *p_ptt,
3513                                          struct ecore_vf_info *vf)
3514 {
3515         int i;
3516
3517         /* Reset the SBs */
3518         for (i = 0; i < vf->num_sbs; i++)
3519                 ecore_int_igu_init_pure_rt_single(p_hwfn, p_ptt,
3520                                                   vf->igu_sbs[i],
3521                                                   vf->opaque_fid, false);
3522
3523         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_INT_CLEANUP,
3524                                sizeof(struct pfvf_def_resp_tlv),
3525                                PFVF_STATUS_SUCCESS);
3526 }
3527
3528 static void ecore_iov_vf_mbx_close(struct ecore_hwfn *p_hwfn,
3529                                    struct ecore_ptt *p_ptt,
3530                                    struct ecore_vf_info *vf)
3531 {
3532         u16 length = sizeof(struct pfvf_def_resp_tlv);
3533         u8 status = PFVF_STATUS_SUCCESS;
3534
3535         /* Disable Interrupts for VF */
3536         ecore_iov_vf_igu_set_int(p_hwfn, p_ptt, vf, 0);
3537
3538         /* Reset Permission table */
3539         ecore_iov_config_perm_table(p_hwfn, p_ptt, vf, 0);
3540
3541         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_CLOSE,
3542                                length, status);
3543 }
3544
3545 static void ecore_iov_vf_mbx_release(struct ecore_hwfn *p_hwfn,
3546                                      struct ecore_ptt *p_ptt,
3547                                      struct ecore_vf_info *p_vf)
3548 {
3549         u16 length = sizeof(struct pfvf_def_resp_tlv);
3550         u8 status = PFVF_STATUS_SUCCESS;
3551         enum _ecore_status_t rc = ECORE_SUCCESS;
3552
3553         ecore_iov_vf_cleanup(p_hwfn, p_vf);
3554
3555         if (p_vf->state != VF_STOPPED && p_vf->state != VF_FREE) {
3556                 /* Stopping the VF */
3557                 rc = ecore_sp_vf_stop(p_hwfn, p_vf->concrete_fid,
3558                                       p_vf->opaque_fid);
3559
3560                 if (rc != ECORE_SUCCESS) {
3561                         DP_ERR(p_hwfn, "ecore_sp_vf_stop returned error %d\n",
3562                                rc);
3563                         status = PFVF_STATUS_FAILURE;
3564                 }
3565
3566                 p_vf->state = VF_STOPPED;
3567         }
3568
3569         ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf, CHANNEL_TLV_RELEASE,
3570                                length, status);
3571 }
3572
3573 static void ecore_iov_vf_pf_get_coalesce(struct ecore_hwfn *p_hwfn,
3574                                          struct ecore_ptt *p_ptt,
3575                                          struct ecore_vf_info *p_vf)
3576 {
3577         struct ecore_iov_vf_mbx *mbx = &p_vf->vf_mbx;
3578         struct pfvf_read_coal_resp_tlv *p_resp;
3579         struct vfpf_read_coal_req_tlv *req;
3580         u8 status = PFVF_STATUS_FAILURE;
3581         struct ecore_vf_queue *p_queue;
3582         struct ecore_queue_cid *p_cid;
3583         enum _ecore_status_t rc = ECORE_SUCCESS;
3584         u16 coal = 0, qid, i;
3585         bool b_is_rx;
3586
3587         mbx->offset = (u8 *)mbx->reply_virt;
3588         req = &mbx->req_virt->read_coal_req;
3589
3590         qid = req->qid;
3591         b_is_rx = req->is_rx ? true : false;
3592
3593         if (b_is_rx) {
3594                 if (!ecore_iov_validate_rxq(p_hwfn, p_vf, qid,
3595                                             ECORE_IOV_VALIDATE_Q_ENABLE)) {
3596                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3597                                    "VF[%d]: Invalid Rx queue_id = %d\n",
3598                                    p_vf->abs_vf_id, qid);
3599                         goto send_resp;
3600                 }
3601
3602                 p_cid = ecore_iov_get_vf_rx_queue_cid(&p_vf->vf_queues[qid]);
3603                 rc = ecore_get_rxq_coalesce(p_hwfn, p_ptt, p_cid, &coal);
3604                 if (rc != ECORE_SUCCESS)
3605                         goto send_resp;
3606         } else {
3607                 if (!ecore_iov_validate_txq(p_hwfn, p_vf, qid,
3608                                             ECORE_IOV_VALIDATE_Q_ENABLE)) {
3609                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3610                                    "VF[%d]: Invalid Tx queue_id = %d\n",
3611                                    p_vf->abs_vf_id, qid);
3612                         goto send_resp;
3613                 }
3614                 for (i = 0; i < MAX_QUEUES_PER_QZONE; i++) {
3615                         p_queue = &p_vf->vf_queues[qid];
3616                         if ((p_queue->cids[i].p_cid == OSAL_NULL) ||
3617                             (!p_queue->cids[i].b_is_tx))
3618                                 continue;
3619
3620                         p_cid = p_queue->cids[i].p_cid;
3621
3622                         rc = ecore_get_txq_coalesce(p_hwfn, p_ptt,
3623                                                     p_cid, &coal);
3624                         if (rc != ECORE_SUCCESS)
3625                                 goto send_resp;
3626                         break;
3627                 }
3628         }
3629
3630         status = PFVF_STATUS_SUCCESS;
3631
3632 send_resp:
3633         p_resp = ecore_add_tlv(&mbx->offset, CHANNEL_TLV_COALESCE_READ,
3634                                sizeof(*p_resp));
3635         p_resp->coal = coal;
3636
3637         ecore_add_tlv(&mbx->offset, CHANNEL_TLV_LIST_END,
3638                       sizeof(struct channel_list_end_tlv));
3639
3640         ecore_iov_send_response(p_hwfn, p_ptt, p_vf, sizeof(*p_resp), status);
3641 }
3642
3643 static void ecore_iov_vf_pf_set_coalesce(struct ecore_hwfn *p_hwfn,
3644                                          struct ecore_ptt *p_ptt,
3645                                          struct ecore_vf_info *vf)
3646 {
3647         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
3648         enum _ecore_status_t rc = ECORE_SUCCESS;
3649         struct vfpf_update_coalesce *req;
3650         u8 status = PFVF_STATUS_FAILURE;
3651         struct ecore_queue_cid *p_cid;
3652         u16 rx_coal, tx_coal;
3653         u16 qid;
3654         int i;
3655
3656         req = &mbx->req_virt->update_coalesce;
3657
3658         rx_coal = req->rx_coal;
3659         tx_coal = req->tx_coal;
3660         qid = req->qid;
3661
3662         if (!ecore_iov_validate_rxq(p_hwfn, vf, qid,
3663                                     ECORE_IOV_VALIDATE_Q_ENABLE) &&
3664             rx_coal) {
3665                 DP_ERR(p_hwfn, "VF[%d]: Invalid Rx queue_id = %d\n",
3666                        vf->abs_vf_id, qid);
3667                 goto out;
3668         }
3669
3670         if (!ecore_iov_validate_txq(p_hwfn, vf, qid,
3671                                     ECORE_IOV_VALIDATE_Q_ENABLE) &&
3672             tx_coal) {
3673                 DP_ERR(p_hwfn, "VF[%d]: Invalid Tx queue_id = %d\n",
3674                        vf->abs_vf_id, qid);
3675                 goto out;
3676         }
3677
3678         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3679                    "VF[%d]: Setting coalesce for VF rx_coal = %d, tx_coal = %d at queue = %d\n",
3680                    vf->abs_vf_id, rx_coal, tx_coal, qid);
3681
3682         if (rx_coal) {
3683                 p_cid = ecore_iov_get_vf_rx_queue_cid(&vf->vf_queues[qid]);
3684
3685                 rc = ecore_set_rxq_coalesce(p_hwfn, p_ptt, rx_coal, p_cid);
3686                 if (rc != ECORE_SUCCESS) {
3687                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3688                                    "VF[%d]: Unable to set rx queue = %d coalesce\n",
3689                                    vf->abs_vf_id, vf->vf_queues[qid].fw_rx_qid);
3690                         goto out;
3691                 }
3692                 vf->rx_coal = rx_coal;
3693         }
3694
3695         /* TODO - in future, it might be possible to pass this in a per-cid
3696          * granularity. For now, do this for all Tx queues.
3697          */
3698         if (tx_coal) {
3699                 struct ecore_vf_queue *p_queue = &vf->vf_queues[qid];
3700
3701                 for (i = 0; i < MAX_QUEUES_PER_QZONE; i++) {
3702                         if (p_queue->cids[i].p_cid == OSAL_NULL)
3703                                 continue;
3704
3705                         if (!p_queue->cids[i].b_is_tx)
3706                                 continue;
3707
3708                         rc = ecore_set_txq_coalesce(p_hwfn, p_ptt, tx_coal,
3709                                                     p_queue->cids[i].p_cid);
3710                         if (rc != ECORE_SUCCESS) {
3711                                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3712                                            "VF[%d]: Unable to set tx queue coalesce\n",
3713                                            vf->abs_vf_id);
3714                                 goto out;
3715                         }
3716                 }
3717                 vf->tx_coal = tx_coal;
3718         }
3719
3720         status = PFVF_STATUS_SUCCESS;
3721 out:
3722         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_COALESCE_UPDATE,
3723                                sizeof(struct pfvf_def_resp_tlv), status);
3724 }
3725
3726 enum _ecore_status_t
3727 ecore_iov_pf_configure_vf_queue_coalesce(struct ecore_hwfn *p_hwfn,
3728                                          u16 rx_coal, u16 tx_coal,
3729                                          u16 vf_id, u16 qid)
3730 {
3731         struct ecore_queue_cid *p_cid;
3732         struct ecore_vf_info *vf;
3733         struct ecore_ptt *p_ptt;
3734         int i, rc = 0;
3735
3736         if (!ecore_iov_is_valid_vfid(p_hwfn, vf_id, true, true)) {
3737                 DP_NOTICE(p_hwfn, true,
3738                           "VF[%d] - Can not set coalescing: VF is not active\n",
3739                           vf_id);
3740                 return ECORE_INVAL;
3741         }
3742
3743         vf = &p_hwfn->pf_iov_info->vfs_array[vf_id];
3744         p_ptt = ecore_ptt_acquire(p_hwfn);
3745         if (!p_ptt)
3746                 return ECORE_AGAIN;
3747
3748         if (!ecore_iov_validate_rxq(p_hwfn, vf, qid,
3749                                     ECORE_IOV_VALIDATE_Q_ENABLE) &&
3750             rx_coal) {
3751                 DP_ERR(p_hwfn, "VF[%d]: Invalid Rx queue_id = %d\n",
3752                        vf->abs_vf_id, qid);
3753                 goto out;
3754         }
3755
3756         if (!ecore_iov_validate_txq(p_hwfn, vf, qid,
3757                                     ECORE_IOV_VALIDATE_Q_ENABLE) &&
3758             tx_coal) {
3759                 DP_ERR(p_hwfn, "VF[%d]: Invalid Tx queue_id = %d\n",
3760                        vf->abs_vf_id, qid);
3761                 goto out;
3762         }
3763
3764         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3765                    "VF[%d]: Setting coalesce for VF rx_coal = %d, tx_coal = %d at queue = %d\n",
3766                    vf->abs_vf_id, rx_coal, tx_coal, qid);
3767
3768         if (rx_coal) {
3769                 p_cid = ecore_iov_get_vf_rx_queue_cid(&vf->vf_queues[qid]);
3770
3771                 rc = ecore_set_rxq_coalesce(p_hwfn, p_ptt, rx_coal, p_cid);
3772                 if (rc != ECORE_SUCCESS) {
3773                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3774                                    "VF[%d]: Unable to set rx queue = %d coalesce\n",
3775                                    vf->abs_vf_id, vf->vf_queues[qid].fw_rx_qid);
3776                         goto out;
3777                 }
3778                 vf->rx_coal = rx_coal;
3779         }
3780
3781         /* TODO - in future, it might be possible to pass this in a per-cid
3782          * granularity. For now, do this for all Tx queues.
3783          */
3784         if (tx_coal) {
3785                 struct ecore_vf_queue *p_queue = &vf->vf_queues[qid];
3786
3787                 for (i = 0; i < MAX_QUEUES_PER_QZONE; i++) {
3788                         if (p_queue->cids[i].p_cid == OSAL_NULL)
3789                                 continue;
3790
3791                         if (!p_queue->cids[i].b_is_tx)
3792                                 continue;
3793
3794                         rc = ecore_set_txq_coalesce(p_hwfn, p_ptt, tx_coal,
3795                                                     p_queue->cids[i].p_cid);
3796                         if (rc != ECORE_SUCCESS) {
3797                                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3798                                            "VF[%d]: Unable to set tx queue coalesce\n",
3799                                            vf->abs_vf_id);
3800                                 goto out;
3801                         }
3802                 }
3803                 vf->tx_coal = tx_coal;
3804         }
3805
3806 out:
3807         ecore_ptt_release(p_hwfn, p_ptt);
3808
3809         return rc;
3810 }
3811
3812 static enum _ecore_status_t
3813 ecore_iov_vf_flr_poll_dorq(struct ecore_hwfn *p_hwfn,
3814                            struct ecore_vf_info *p_vf, struct ecore_ptt *p_ptt)
3815 {
3816         int cnt;
3817         u32 val;
3818
3819         ecore_fid_pretend(p_hwfn, p_ptt, (u16)p_vf->concrete_fid);
3820
3821         for (cnt = 0; cnt < 50; cnt++) {
3822                 val = ecore_rd(p_hwfn, p_ptt, DORQ_REG_VF_USAGE_CNT);
3823                 if (!val)
3824                         break;
3825                 OSAL_MSLEEP(20);
3826         }
3827         ecore_fid_pretend(p_hwfn, p_ptt, (u16)p_hwfn->hw_info.concrete_fid);
3828
3829         if (cnt == 50) {
3830                 DP_ERR(p_hwfn,
3831                        "VF[%d] - dorq failed to cleanup [usage 0x%08x]\n",
3832                        p_vf->abs_vf_id, val);
3833                 return ECORE_TIMEOUT;
3834         }
3835
3836         return ECORE_SUCCESS;
3837 }
3838
3839 static enum _ecore_status_t
3840 ecore_iov_vf_flr_poll_pbf(struct ecore_hwfn *p_hwfn,
3841                           struct ecore_vf_info *p_vf, struct ecore_ptt *p_ptt)
3842 {
3843         u32 cons[MAX_NUM_VOQS_E4], distance[MAX_NUM_VOQS_E4];
3844         int i, cnt;
3845
3846         /* Read initial consumers & producers */
3847         for (i = 0; i < MAX_NUM_VOQS_E4; i++) {
3848                 u32 prod;
3849
3850                 cons[i] = ecore_rd(p_hwfn, p_ptt,
3851                                    PBF_REG_NUM_BLOCKS_ALLOCATED_CONS_VOQ0 +
3852                                    i * 0x40);
3853                 prod = ecore_rd(p_hwfn, p_ptt,
3854                                 PBF_REG_NUM_BLOCKS_ALLOCATED_PROD_VOQ0 +
3855                                 i * 0x40);
3856                 distance[i] = prod - cons[i];
3857         }
3858
3859         /* Wait for consumers to pass the producers */
3860         i = 0;
3861         for (cnt = 0; cnt < 50; cnt++) {
3862                 for (; i < MAX_NUM_VOQS_E4; i++) {
3863                         u32 tmp;
3864
3865                         tmp = ecore_rd(p_hwfn, p_ptt,
3866                                        PBF_REG_NUM_BLOCKS_ALLOCATED_CONS_VOQ0 +
3867                                        i * 0x40);
3868                         if (distance[i] > tmp - cons[i])
3869                                 break;
3870                 }
3871
3872                 if (i == MAX_NUM_VOQS_E4)
3873                         break;
3874
3875                 OSAL_MSLEEP(20);
3876         }
3877
3878         if (cnt == 50) {
3879                 DP_ERR(p_hwfn, "VF[%d] - pbf polling failed on VOQ %d\n",
3880                        p_vf->abs_vf_id, i);
3881                 return ECORE_TIMEOUT;
3882         }
3883
3884         return ECORE_SUCCESS;
3885 }
3886
3887 static enum _ecore_status_t ecore_iov_vf_flr_poll(struct ecore_hwfn *p_hwfn,
3888                                                   struct ecore_vf_info *p_vf,
3889                                                   struct ecore_ptt *p_ptt)
3890 {
3891         enum _ecore_status_t rc;
3892
3893         /* TODO - add SRC and TM polling once we add storage IOV */
3894
3895         rc = ecore_iov_vf_flr_poll_dorq(p_hwfn, p_vf, p_ptt);
3896         if (rc)
3897                 return rc;
3898
3899         rc = ecore_iov_vf_flr_poll_pbf(p_hwfn, p_vf, p_ptt);
3900         if (rc)
3901                 return rc;
3902
3903         return ECORE_SUCCESS;
3904 }
3905
3906 static enum _ecore_status_t
3907 ecore_iov_execute_vf_flr_cleanup(struct ecore_hwfn *p_hwfn,
3908                                  struct ecore_ptt *p_ptt,
3909                                  u16 rel_vf_id, u32 *ack_vfs)
3910 {
3911         struct ecore_vf_info *p_vf;
3912         enum _ecore_status_t rc = ECORE_SUCCESS;
3913
3914         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, false);
3915         if (!p_vf)
3916                 return ECORE_SUCCESS;
3917
3918         if (p_hwfn->pf_iov_info->pending_flr[rel_vf_id / 64] &
3919             (1ULL << (rel_vf_id % 64))) {
3920                 u16 vfid = p_vf->abs_vf_id;
3921
3922                 /* TODO - should we lock channel? */
3923
3924                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3925                            "VF[%d] - Handling FLR\n", vfid);
3926
3927                 ecore_iov_vf_cleanup(p_hwfn, p_vf);
3928
3929                 /* If VF isn't active, no need for anything but SW */
3930                 if (!p_vf->b_init)
3931                         goto cleanup;
3932
3933                 /* TODO - what to do in case of failure? */
3934                 rc = ecore_iov_vf_flr_poll(p_hwfn, p_vf, p_ptt);
3935                 if (rc != ECORE_SUCCESS)
3936                         goto cleanup;
3937
3938                 rc = ecore_final_cleanup(p_hwfn, p_ptt, vfid, true);
3939                 if (rc) {
3940                         /* TODO - what's now? What a mess.... */
3941                         DP_ERR(p_hwfn, "Failed handle FLR of VF[%d]\n", vfid);
3942                         return rc;
3943                 }
3944
3945                 /* Workaround to make VF-PF channel ready, as FW
3946                  * doesn't do that as a part of FLR.
3947                  */
3948                 REG_WR(p_hwfn,
3949                        GTT_BAR0_MAP_REG_USDM_RAM +
3950                        USTORM_VF_PF_CHANNEL_READY_OFFSET(vfid), 1);
3951
3952                 /* VF_STOPPED has to be set only after final cleanup
3953                  * but prior to re-enabling the VF.
3954                  */
3955                 p_vf->state = VF_STOPPED;
3956
3957                 rc = ecore_iov_enable_vf_access(p_hwfn, p_ptt, p_vf);
3958                 if (rc) {
3959                         /* TODO - again, a mess... */
3960                         DP_ERR(p_hwfn, "Failed to re-enable VF[%d] acces\n",
3961                                vfid);
3962                         return rc;
3963                 }
3964 cleanup:
3965                 /* Mark VF for ack and clean pending state */
3966                 if (p_vf->state == VF_RESET)
3967                         p_vf->state = VF_STOPPED;
3968                 ack_vfs[vfid / 32] |= (1 << (vfid % 32));
3969                 p_hwfn->pf_iov_info->pending_flr[rel_vf_id / 64] &=
3970                     ~(1ULL << (rel_vf_id % 64));
3971                 p_vf->vf_mbx.b_pending_msg = false;
3972         }
3973
3974         return rc;
3975 }
3976
3977 enum _ecore_status_t ecore_iov_vf_flr_cleanup(struct ecore_hwfn *p_hwfn,
3978                                               struct ecore_ptt *p_ptt)
3979 {
3980         u32 ack_vfs[VF_MAX_STATIC / 32];
3981         enum _ecore_status_t rc = ECORE_SUCCESS;
3982         u16 i;
3983
3984         OSAL_MEMSET(ack_vfs, 0, sizeof(u32) * (VF_MAX_STATIC / 32));
3985
3986         /* Since BRB <-> PRS interface can't be tested as part of the flr
3987          * polling due to HW limitations, simply sleep a bit. And since
3988          * there's no need to wait per-vf, do it before looping.
3989          */
3990         OSAL_MSLEEP(100);
3991
3992         for (i = 0; i < p_hwfn->p_dev->p_iov_info->total_vfs; i++)
3993                 ecore_iov_execute_vf_flr_cleanup(p_hwfn, p_ptt, i, ack_vfs);
3994
3995         rc = ecore_mcp_ack_vf_flr(p_hwfn, p_ptt, ack_vfs);
3996         return rc;
3997 }
3998
3999 enum _ecore_status_t
4000 ecore_iov_single_vf_flr_cleanup(struct ecore_hwfn *p_hwfn,
4001                                 struct ecore_ptt *p_ptt, u16 rel_vf_id)
4002 {
4003         u32 ack_vfs[VF_MAX_STATIC / 32];
4004         enum _ecore_status_t rc = ECORE_SUCCESS;
4005
4006         OSAL_MEMSET(ack_vfs, 0, sizeof(u32) * (VF_MAX_STATIC / 32));
4007
4008         /* Wait instead of polling the BRB <-> PRS interface */
4009         OSAL_MSLEEP(100);
4010
4011         ecore_iov_execute_vf_flr_cleanup(p_hwfn, p_ptt, rel_vf_id, ack_vfs);
4012
4013         rc = ecore_mcp_ack_vf_flr(p_hwfn, p_ptt, ack_vfs);
4014         return rc;
4015 }
4016
4017 bool ecore_iov_mark_vf_flr(struct ecore_hwfn *p_hwfn, u32 *p_disabled_vfs)
4018 {
4019         bool found = false;
4020         u16 i;
4021
4022         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV, "Marking FLR-ed VFs\n");
4023         for (i = 0; i < (VF_MAX_STATIC / 32); i++)
4024                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4025                            "[%08x,...,%08x]: %08x\n",
4026                            i * 32, (i + 1) * 32 - 1, p_disabled_vfs[i]);
4027
4028         if (!p_hwfn->p_dev->p_iov_info) {
4029                 DP_NOTICE(p_hwfn, true, "VF flr but no IOV\n");
4030                 return false;
4031         }
4032
4033         /* Mark VFs */
4034         for (i = 0; i < p_hwfn->p_dev->p_iov_info->total_vfs; i++) {
4035                 struct ecore_vf_info *p_vf;
4036                 u8 vfid;
4037
4038                 p_vf = ecore_iov_get_vf_info(p_hwfn, i, false);
4039                 if (!p_vf)
4040                         continue;
4041
4042                 vfid = p_vf->abs_vf_id;
4043                 if ((1 << (vfid % 32)) & p_disabled_vfs[vfid / 32]) {
4044                         u64 *p_flr = p_hwfn->pf_iov_info->pending_flr;
4045                         u16 rel_vf_id = p_vf->relative_vf_id;
4046
4047                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4048                                    "VF[%d] [rel %d] got FLR-ed\n",
4049                                    vfid, rel_vf_id);
4050
4051                         p_vf->state = VF_RESET;
4052
4053                         /* No need to lock here, since pending_flr should
4054                          * only change here and before ACKing MFw. Since
4055                          * MFW will not trigger an additional attention for
4056                          * VF flr until ACKs, we're safe.
4057                          */
4058                         p_flr[rel_vf_id / 64] |= 1ULL << (rel_vf_id % 64);
4059                         found = true;
4060                 }
4061         }
4062
4063         return found;
4064 }
4065
4066 void ecore_iov_get_link(struct ecore_hwfn *p_hwfn,
4067                         u16 vfid,
4068                         struct ecore_mcp_link_params *p_params,
4069                         struct ecore_mcp_link_state *p_link,
4070                         struct ecore_mcp_link_capabilities *p_caps)
4071 {
4072         struct ecore_vf_info *p_vf = ecore_iov_get_vf_info(p_hwfn, vfid, false);
4073         struct ecore_bulletin_content *p_bulletin;
4074
4075         if (!p_vf)
4076                 return;
4077
4078         p_bulletin = p_vf->bulletin.p_virt;
4079
4080         if (p_params)
4081                 __ecore_vf_get_link_params(p_params, p_bulletin);
4082         if (p_link)
4083                 __ecore_vf_get_link_state(p_link, p_bulletin);
4084         if (p_caps)
4085                 __ecore_vf_get_link_caps(p_caps, p_bulletin);
4086 }
4087
4088 void ecore_iov_process_mbx_req(struct ecore_hwfn *p_hwfn,
4089                                struct ecore_ptt *p_ptt, int vfid)
4090 {
4091         struct ecore_iov_vf_mbx *mbx;
4092         struct ecore_vf_info *p_vf;
4093
4094         p_vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4095         if (!p_vf)
4096                 return;
4097
4098         mbx = &p_vf->vf_mbx;
4099
4100         /* ecore_iov_process_mbx_request */
4101 #ifndef CONFIG_ECORE_SW_CHANNEL
4102         if (!mbx->b_pending_msg) {
4103                 DP_NOTICE(p_hwfn, true,
4104                           "VF[%02x]: Trying to process mailbox message when none is pending\n",
4105                           p_vf->abs_vf_id);
4106                 return;
4107         }
4108         mbx->b_pending_msg = false;
4109 #endif
4110
4111         mbx->first_tlv = mbx->req_virt->first_tlv;
4112
4113         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4114                    "VF[%02x]: Processing mailbox message [type %04x]\n",
4115                    p_vf->abs_vf_id, mbx->first_tlv.tl.type);
4116
4117         OSAL_IOV_VF_MSG_TYPE(p_hwfn,
4118                              p_vf->relative_vf_id,
4119                              mbx->first_tlv.tl.type);
4120
4121         /* Lock the per vf op mutex and note the locker's identity.
4122          * The unlock will take place in mbx response.
4123          */
4124         ecore_iov_lock_vf_pf_channel(p_hwfn,
4125                                      p_vf, mbx->first_tlv.tl.type);
4126
4127         /* check if tlv type is known */
4128         if (ecore_iov_tlv_supported(mbx->first_tlv.tl.type) &&
4129             !p_vf->b_malicious) {
4130                 /* switch on the opcode */
4131                 switch (mbx->first_tlv.tl.type) {
4132                 case CHANNEL_TLV_ACQUIRE:
4133                         ecore_iov_vf_mbx_acquire(p_hwfn, p_ptt, p_vf);
4134                         break;
4135                 case CHANNEL_TLV_VPORT_START:
4136                         ecore_iov_vf_mbx_start_vport(p_hwfn, p_ptt, p_vf);
4137                         break;
4138                 case CHANNEL_TLV_VPORT_TEARDOWN:
4139                         ecore_iov_vf_mbx_stop_vport(p_hwfn, p_ptt, p_vf);
4140                         break;
4141                 case CHANNEL_TLV_START_RXQ:
4142                         ecore_iov_vf_mbx_start_rxq(p_hwfn, p_ptt, p_vf);
4143                         break;
4144                 case CHANNEL_TLV_START_TXQ:
4145                         ecore_iov_vf_mbx_start_txq(p_hwfn, p_ptt, p_vf);
4146                         break;
4147                 case CHANNEL_TLV_STOP_RXQS:
4148                         ecore_iov_vf_mbx_stop_rxqs(p_hwfn, p_ptt, p_vf);
4149                         break;
4150                 case CHANNEL_TLV_STOP_TXQS:
4151                         ecore_iov_vf_mbx_stop_txqs(p_hwfn, p_ptt, p_vf);
4152                         break;
4153                 case CHANNEL_TLV_UPDATE_RXQ:
4154                         ecore_iov_vf_mbx_update_rxqs(p_hwfn, p_ptt, p_vf);
4155                         break;
4156                 case CHANNEL_TLV_VPORT_UPDATE:
4157                         ecore_iov_vf_mbx_vport_update(p_hwfn, p_ptt, p_vf);
4158                         break;
4159                 case CHANNEL_TLV_UCAST_FILTER:
4160                         ecore_iov_vf_mbx_ucast_filter(p_hwfn, p_ptt, p_vf);
4161                         break;
4162                 case CHANNEL_TLV_CLOSE:
4163                         ecore_iov_vf_mbx_close(p_hwfn, p_ptt, p_vf);
4164                         break;
4165                 case CHANNEL_TLV_INT_CLEANUP:
4166                         ecore_iov_vf_mbx_int_cleanup(p_hwfn, p_ptt, p_vf);
4167                         break;
4168                 case CHANNEL_TLV_RELEASE:
4169                         ecore_iov_vf_mbx_release(p_hwfn, p_ptt, p_vf);
4170                         break;
4171                 case CHANNEL_TLV_UPDATE_TUNN_PARAM:
4172                         ecore_iov_vf_mbx_update_tunn_param(p_hwfn, p_ptt, p_vf);
4173                         break;
4174                 case CHANNEL_TLV_COALESCE_UPDATE:
4175                         ecore_iov_vf_pf_set_coalesce(p_hwfn, p_ptt, p_vf);
4176                         break;
4177                 case CHANNEL_TLV_COALESCE_READ:
4178                         ecore_iov_vf_pf_get_coalesce(p_hwfn, p_ptt, p_vf);
4179                         break;
4180                 case CHANNEL_TLV_UPDATE_MTU:
4181                         ecore_iov_vf_pf_update_mtu(p_hwfn, p_ptt, p_vf);
4182                         break;
4183                 }
4184         } else if (ecore_iov_tlv_supported(mbx->first_tlv.tl.type)) {
4185                 /* If we've received a message from a VF we consider malicious
4186                  * we ignore the messasge unless it's one for RELEASE, in which
4187                  * case we'll let it have the benefit of doubt, allowing the
4188                  * next loaded driver to start again.
4189                  */
4190                 if (mbx->first_tlv.tl.type == CHANNEL_TLV_RELEASE) {
4191                         /* TODO - initiate FLR, remove malicious indication */
4192                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4193                                    "VF [%02x] - considered malicious, but wanted to RELEASE. TODO\n",
4194                                    p_vf->abs_vf_id);
4195                 } else {
4196                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4197                                    "VF [%02x] - considered malicious; Ignoring TLV [%04x]\n",
4198                                    p_vf->abs_vf_id, mbx->first_tlv.tl.type);
4199                 }
4200
4201                 ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf,
4202                                        mbx->first_tlv.tl.type,
4203                                        sizeof(struct pfvf_def_resp_tlv),
4204                                        PFVF_STATUS_MALICIOUS);
4205         } else {
4206                 /* unknown TLV - this may belong to a VF driver from the future
4207                  * - a version written after this PF driver was written, which
4208                  * supports features unknown as of yet. Too bad since we don't
4209                  * support them. Or this may be because someone wrote a crappy
4210                  * VF driver and is sending garbage over the channel.
4211                  */
4212                 DP_NOTICE(p_hwfn, false,
4213                           "VF[%02x]: unknown TLV. type %04x length %04x"
4214                           " padding %08x reply address %lu\n",
4215                           p_vf->abs_vf_id,
4216                           mbx->first_tlv.tl.type,
4217                           mbx->first_tlv.tl.length,
4218                           mbx->first_tlv.padding,
4219                           (unsigned long)mbx->first_tlv.reply_address);
4220
4221                 /* Try replying in case reply address matches the acquisition's
4222                  * posted address.
4223                  */
4224                 if (p_vf->acquire.first_tlv.reply_address &&
4225                     (mbx->first_tlv.reply_address ==
4226                      p_vf->acquire.first_tlv.reply_address))
4227                         ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf,
4228                                                mbx->first_tlv.tl.type,
4229                                                sizeof(struct pfvf_def_resp_tlv),
4230                                                PFVF_STATUS_NOT_SUPPORTED);
4231                 else
4232                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4233                                    "VF[%02x]: Can't respond to TLV -"
4234                                    " no valid reply address\n",
4235                                    p_vf->abs_vf_id);
4236         }
4237
4238         ecore_iov_unlock_vf_pf_channel(p_hwfn, p_vf,
4239                                        mbx->first_tlv.tl.type);
4240
4241 #ifdef CONFIG_ECORE_SW_CHANNEL
4242         mbx->sw_mbx.mbx_state = VF_PF_RESPONSE_READY;
4243         mbx->sw_mbx.response_offset = 0;
4244 #endif
4245 }
4246
4247 void ecore_iov_pf_get_pending_events(struct ecore_hwfn *p_hwfn,
4248                                      u64 *events)
4249 {
4250         int i;
4251
4252         OSAL_MEM_ZERO(events, sizeof(u64) * ECORE_VF_ARRAY_LENGTH);
4253
4254         ecore_for_each_vf(p_hwfn, i) {
4255                 struct ecore_vf_info *p_vf;
4256
4257                 p_vf = &p_hwfn->pf_iov_info->vfs_array[i];
4258                 if (p_vf->vf_mbx.b_pending_msg)
4259                         events[i / 64] |= 1ULL << (i % 64);
4260         }
4261 }
4262
4263 static struct ecore_vf_info *
4264 ecore_sriov_get_vf_from_absid(struct ecore_hwfn *p_hwfn, u16 abs_vfid)
4265 {
4266         u8 min = (u8)p_hwfn->p_dev->p_iov_info->first_vf_in_pf;
4267
4268         if (!_ecore_iov_pf_sanity_check(p_hwfn, (int)abs_vfid - min, false)) {
4269                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4270                            "Got indication for VF [abs 0x%08x] that cannot be"
4271                            " handled by PF\n",
4272                            abs_vfid);
4273                 return OSAL_NULL;
4274         }
4275
4276         return &p_hwfn->pf_iov_info->vfs_array[(u8)abs_vfid - min];
4277 }
4278
4279 static enum _ecore_status_t ecore_sriov_vfpf_msg(struct ecore_hwfn *p_hwfn,
4280                                                  u16 abs_vfid,
4281                                                  struct regpair *vf_msg)
4282 {
4283         struct ecore_vf_info *p_vf = ecore_sriov_get_vf_from_absid(p_hwfn,
4284                                                                    abs_vfid);
4285
4286         if (!p_vf)
4287                 return ECORE_SUCCESS;
4288
4289         /* List the physical address of the request so that handler
4290          * could later on copy the message from it.
4291          */
4292         p_vf->vf_mbx.pending_req = (((u64)vf_msg->hi) << 32) | vf_msg->lo;
4293
4294         p_vf->vf_mbx.b_pending_msg = true;
4295
4296         return OSAL_PF_VF_MSG(p_hwfn, p_vf->relative_vf_id);
4297 }
4298
4299 static void ecore_sriov_vfpf_malicious(struct ecore_hwfn *p_hwfn,
4300                                        struct malicious_vf_eqe_data *p_data)
4301 {
4302         struct ecore_vf_info *p_vf;
4303
4304         p_vf = ecore_sriov_get_vf_from_absid(p_hwfn, p_data->vf_id);
4305
4306         if (!p_vf)
4307                 return;
4308
4309         if (!p_vf->b_malicious) {
4310                 DP_NOTICE(p_hwfn, false,
4311                           "VF [%d] - Malicious behavior [%02x]\n",
4312                           p_vf->abs_vf_id, p_data->err_id);
4313
4314                 p_vf->b_malicious = true;
4315         } else {
4316                 DP_INFO(p_hwfn,
4317                         "VF [%d] - Malicious behavior [%02x]\n",
4318                         p_vf->abs_vf_id, p_data->err_id);
4319         }
4320
4321         OSAL_PF_VF_MALICIOUS(p_hwfn, p_vf->relative_vf_id);
4322 }
4323
4324 static enum _ecore_status_t ecore_sriov_eqe_event(struct ecore_hwfn *p_hwfn,
4325                                                   u8 opcode,
4326                                                   __le16 echo,
4327                                                   union event_ring_data *data,
4328                                                   u8 OSAL_UNUSED fw_return_code)
4329 {
4330         switch (opcode) {
4331         case COMMON_EVENT_VF_PF_CHANNEL:
4332                 return ecore_sriov_vfpf_msg(p_hwfn, OSAL_LE16_TO_CPU(echo),
4333                                             &data->vf_pf_channel.msg_addr);
4334         case COMMON_EVENT_VF_FLR:
4335                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4336                            "VF-FLR is still not supported\n");
4337                 return ECORE_SUCCESS;
4338         case COMMON_EVENT_MALICIOUS_VF:
4339                 ecore_sriov_vfpf_malicious(p_hwfn, &data->malicious_vf);
4340                 return ECORE_SUCCESS;
4341         default:
4342                 DP_INFO(p_hwfn->p_dev, "Unknown sriov eqe event 0x%02x\n",
4343                         opcode);
4344                 return ECORE_INVAL;
4345         }
4346 }
4347
4348 bool ecore_iov_is_vf_pending_flr(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4349 {
4350         return !!(p_hwfn->pf_iov_info->pending_flr[rel_vf_id / 64] &
4351                    (1ULL << (rel_vf_id % 64)));
4352 }
4353
4354 u16 ecore_iov_get_next_active_vf(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4355 {
4356         struct ecore_hw_sriov_info *p_iov = p_hwfn->p_dev->p_iov_info;
4357         u16 i;
4358
4359         if (!p_iov)
4360                 goto out;
4361
4362         for (i = rel_vf_id; i < p_iov->total_vfs; i++)
4363                 if (ecore_iov_is_valid_vfid(p_hwfn, rel_vf_id, true, false))
4364                         return i;
4365
4366 out:
4367         return MAX_NUM_VFS_E4;
4368 }
4369
4370 enum _ecore_status_t ecore_iov_copy_vf_msg(struct ecore_hwfn *p_hwfn,
4371                                            struct ecore_ptt *ptt, int vfid)
4372 {
4373         struct ecore_dmae_params params;
4374         struct ecore_vf_info *vf_info;
4375
4376         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4377         if (!vf_info)
4378                 return ECORE_INVAL;
4379
4380         OSAL_MEMSET(&params, 0, sizeof(struct ecore_dmae_params));
4381         params.flags = ECORE_DMAE_FLAG_VF_SRC | ECORE_DMAE_FLAG_COMPLETION_DST;
4382         params.src_vfid = vf_info->abs_vf_id;
4383
4384         if (ecore_dmae_host2host(p_hwfn, ptt,
4385                                  vf_info->vf_mbx.pending_req,
4386                                  vf_info->vf_mbx.req_phys,
4387                                  sizeof(union vfpf_tlvs) / 4, &params)) {
4388                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4389                            "Failed to copy message from VF 0x%02x\n", vfid);
4390
4391                 return ECORE_IO;
4392         }
4393
4394         return ECORE_SUCCESS;
4395 }
4396
4397 void ecore_iov_bulletin_set_forced_mac(struct ecore_hwfn *p_hwfn,
4398                                        u8 *mac, int vfid)
4399 {
4400         struct ecore_vf_info *vf_info;
4401         u64 feature;
4402
4403         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4404         if (!vf_info) {
4405                 DP_NOTICE(p_hwfn->p_dev, true,
4406                           "Can not set forced MAC, invalid vfid [%d]\n", vfid);
4407                 return;
4408         }
4409         if (vf_info->b_malicious) {
4410                 DP_NOTICE(p_hwfn->p_dev, false,
4411                           "Can't set forced MAC to malicious VF [%d]\n",
4412                           vfid);
4413                 return;
4414         }
4415
4416         feature = 1 << MAC_ADDR_FORCED;
4417         OSAL_MEMCPY(vf_info->bulletin.p_virt->mac, mac, ETH_ALEN);
4418
4419         vf_info->bulletin.p_virt->valid_bitmap |= feature;
4420         /* Forced MAC will disable MAC_ADDR */
4421         vf_info->bulletin.p_virt->valid_bitmap &=
4422             ~(1 << VFPF_BULLETIN_MAC_ADDR);
4423
4424         ecore_iov_configure_vport_forced(p_hwfn, vf_info, feature);
4425 }
4426
4427 enum _ecore_status_t ecore_iov_bulletin_set_mac(struct ecore_hwfn *p_hwfn,
4428                                                 u8 *mac, int vfid)
4429 {
4430         struct ecore_vf_info *vf_info;
4431         u64 feature;
4432
4433         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4434         if (!vf_info) {
4435                 DP_NOTICE(p_hwfn->p_dev, true,
4436                           "Can not set MAC, invalid vfid [%d]\n", vfid);
4437                 return ECORE_INVAL;
4438         }
4439         if (vf_info->b_malicious) {
4440                 DP_NOTICE(p_hwfn->p_dev, false,
4441                           "Can't set MAC to malicious VF [%d]\n",
4442                           vfid);
4443                 return ECORE_INVAL;
4444         }
4445
4446         if (vf_info->bulletin.p_virt->valid_bitmap & (1 << MAC_ADDR_FORCED)) {
4447                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4448                            "Can not set MAC, Forced MAC is configured\n");
4449                 return ECORE_INVAL;
4450         }
4451
4452         feature = 1 << VFPF_BULLETIN_MAC_ADDR;
4453         OSAL_MEMCPY(vf_info->bulletin.p_virt->mac, mac, ETH_ALEN);
4454
4455         vf_info->bulletin.p_virt->valid_bitmap |= feature;
4456
4457         return ECORE_SUCCESS;
4458 }
4459
4460 enum _ecore_status_t
4461 ecore_iov_bulletin_set_forced_untagged_default(struct ecore_hwfn *p_hwfn,
4462                                                bool b_untagged_only, int vfid)
4463 {
4464         struct ecore_vf_info *vf_info;
4465         u64 feature;
4466
4467         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4468         if (!vf_info) {
4469                 DP_NOTICE(p_hwfn->p_dev, true,
4470                           "Can not set untagged default, invalid vfid [%d]\n",
4471                           vfid);
4472                 return ECORE_INVAL;
4473         }
4474         if (vf_info->b_malicious) {
4475                 DP_NOTICE(p_hwfn->p_dev, false,
4476                           "Can't set untagged default to malicious VF [%d]\n",
4477                           vfid);
4478                 return ECORE_INVAL;
4479         }
4480
4481         /* Since this is configurable only during vport-start, don't take it
4482          * if we're past that point.
4483          */
4484         if (vf_info->state == VF_ENABLED) {
4485                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4486                            "Can't support untagged change for vfid[%d] -"
4487                            " VF is already active\n",
4488                            vfid);
4489                 return ECORE_INVAL;
4490         }
4491
4492         /* Set configuration; This will later be taken into account during the
4493          * VF initialization.
4494          */
4495         feature = (1 << VFPF_BULLETIN_UNTAGGED_DEFAULT) |
4496             (1 << VFPF_BULLETIN_UNTAGGED_DEFAULT_FORCED);
4497         vf_info->bulletin.p_virt->valid_bitmap |= feature;
4498
4499         vf_info->bulletin.p_virt->default_only_untagged = b_untagged_only ? 1
4500             : 0;
4501
4502         return ECORE_SUCCESS;
4503 }
4504
4505 void ecore_iov_get_vfs_opaque_fid(struct ecore_hwfn *p_hwfn, int vfid,
4506                                   u16 *opaque_fid)
4507 {
4508         struct ecore_vf_info *vf_info;
4509
4510         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4511         if (!vf_info)
4512                 return;
4513
4514         *opaque_fid = vf_info->opaque_fid;
4515 }
4516
4517 void ecore_iov_bulletin_set_forced_vlan(struct ecore_hwfn *p_hwfn,
4518                                         u16 pvid, int vfid)
4519 {
4520         struct ecore_vf_info *vf_info;
4521         u64 feature;
4522
4523         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4524         if (!vf_info) {
4525                 DP_NOTICE(p_hwfn->p_dev, true,
4526                           "Can not set forced MAC, invalid vfid [%d]\n",
4527                           vfid);
4528                 return;
4529         }
4530         if (vf_info->b_malicious) {
4531                 DP_NOTICE(p_hwfn->p_dev, false,
4532                           "Can't set forced vlan to malicious VF [%d]\n",
4533                           vfid);
4534                 return;
4535         }
4536
4537         feature = 1 << VLAN_ADDR_FORCED;
4538         vf_info->bulletin.p_virt->pvid = pvid;
4539         if (pvid)
4540                 vf_info->bulletin.p_virt->valid_bitmap |= feature;
4541         else
4542                 vf_info->bulletin.p_virt->valid_bitmap &= ~feature;
4543
4544         ecore_iov_configure_vport_forced(p_hwfn, vf_info, feature);
4545 }
4546
4547 void ecore_iov_bulletin_set_udp_ports(struct ecore_hwfn *p_hwfn,
4548                                       int vfid, u16 vxlan_port, u16 geneve_port)
4549 {
4550         struct ecore_vf_info *vf_info;
4551
4552         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4553         if (!vf_info) {
4554                 DP_NOTICE(p_hwfn->p_dev, true,
4555                           "Can not set udp ports, invalid vfid [%d]\n", vfid);
4556                 return;
4557         }
4558
4559         if (vf_info->b_malicious) {
4560                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
4561                            "Can not set udp ports to malicious VF [%d]\n",
4562                            vfid);
4563                 return;
4564         }
4565
4566         vf_info->bulletin.p_virt->vxlan_udp_port = vxlan_port;
4567         vf_info->bulletin.p_virt->geneve_udp_port = geneve_port;
4568 }
4569
4570 bool ecore_iov_vf_has_vport_instance(struct ecore_hwfn *p_hwfn, int vfid)
4571 {
4572         struct ecore_vf_info *p_vf_info;
4573
4574         p_vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4575         if (!p_vf_info)
4576                 return false;
4577
4578         return !!p_vf_info->vport_instance;
4579 }
4580
4581 bool ecore_iov_is_vf_stopped(struct ecore_hwfn *p_hwfn, int vfid)
4582 {
4583         struct ecore_vf_info *p_vf_info;
4584
4585         p_vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4586         if (!p_vf_info)
4587                 return true;
4588
4589         return p_vf_info->state == VF_STOPPED;
4590 }
4591
4592 bool ecore_iov_spoofchk_get(struct ecore_hwfn *p_hwfn, int vfid)
4593 {
4594         struct ecore_vf_info *vf_info;
4595
4596         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4597         if (!vf_info)
4598                 return false;
4599
4600         return vf_info->spoof_chk;
4601 }
4602
4603 enum _ecore_status_t ecore_iov_spoofchk_set(struct ecore_hwfn *p_hwfn,
4604                                             int vfid, bool val)
4605 {
4606         struct ecore_vf_info *vf;
4607         enum _ecore_status_t rc = ECORE_INVAL;
4608
4609         if (!ecore_iov_pf_sanity_check(p_hwfn, vfid)) {
4610                 DP_NOTICE(p_hwfn, true,
4611                           "SR-IOV sanity check failed, can't set spoofchk\n");
4612                 goto out;
4613         }
4614
4615         vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4616         if (!vf)
4617                 goto out;
4618
4619         if (!ecore_iov_vf_has_vport_instance(p_hwfn, vfid)) {
4620                 /* After VF VPORT start PF will configure spoof check */
4621                 vf->req_spoofchk_val = val;
4622                 rc = ECORE_SUCCESS;
4623                 goto out;
4624         }
4625
4626         rc = __ecore_iov_spoofchk_set(p_hwfn, vf, val);
4627
4628 out:
4629         return rc;
4630 }
4631
4632 u8 ecore_iov_vf_chains_per_pf(struct ecore_hwfn *p_hwfn)
4633 {
4634         u8 max_chains_per_vf = p_hwfn->hw_info.max_chains_per_vf;
4635
4636         max_chains_per_vf = (max_chains_per_vf) ? max_chains_per_vf
4637             : ECORE_MAX_VF_CHAINS_PER_PF;
4638
4639         return max_chains_per_vf;
4640 }
4641
4642 void ecore_iov_get_vf_req_virt_mbx_params(struct ecore_hwfn *p_hwfn,
4643                                           u16 rel_vf_id,
4644                                           void **pp_req_virt_addr,
4645                                           u16 *p_req_virt_size)
4646 {
4647         struct ecore_vf_info *vf_info =
4648             ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4649
4650         if (!vf_info)
4651                 return;
4652
4653         if (pp_req_virt_addr)
4654                 *pp_req_virt_addr = vf_info->vf_mbx.req_virt;
4655
4656         if (p_req_virt_size)
4657                 *p_req_virt_size = sizeof(*vf_info->vf_mbx.req_virt);
4658 }
4659
4660 void ecore_iov_get_vf_reply_virt_mbx_params(struct ecore_hwfn *p_hwfn,
4661                                             u16 rel_vf_id,
4662                                             void **pp_reply_virt_addr,
4663                                             u16 *p_reply_virt_size)
4664 {
4665         struct ecore_vf_info *vf_info =
4666             ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4667
4668         if (!vf_info)
4669                 return;
4670
4671         if (pp_reply_virt_addr)
4672                 *pp_reply_virt_addr = vf_info->vf_mbx.reply_virt;
4673
4674         if (p_reply_virt_size)
4675                 *p_reply_virt_size = sizeof(*vf_info->vf_mbx.reply_virt);
4676 }
4677
4678 #ifdef CONFIG_ECORE_SW_CHANNEL
4679 struct ecore_iov_sw_mbx *ecore_iov_get_vf_sw_mbx(struct ecore_hwfn *p_hwfn,
4680                                                  u16 rel_vf_id)
4681 {
4682         struct ecore_vf_info *vf_info =
4683             ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4684
4685         if (!vf_info)
4686                 return OSAL_NULL;
4687
4688         return &vf_info->vf_mbx.sw_mbx;
4689 }
4690 #endif
4691
4692 bool ecore_iov_is_valid_vfpf_msg_length(u32 length)
4693 {
4694         return (length >= sizeof(struct vfpf_first_tlv) &&
4695                 (length <= sizeof(union vfpf_tlvs)));
4696 }
4697
4698 u32 ecore_iov_pfvf_msg_length(void)
4699 {
4700         return sizeof(union pfvf_tlvs);
4701 }
4702
4703 u8 *ecore_iov_bulletin_get_forced_mac(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4704 {
4705         struct ecore_vf_info *p_vf;
4706
4707         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4708         if (!p_vf || !p_vf->bulletin.p_virt)
4709                 return OSAL_NULL;
4710
4711         if (!(p_vf->bulletin.p_virt->valid_bitmap & (1 << MAC_ADDR_FORCED)))
4712                 return OSAL_NULL;
4713
4714         return p_vf->bulletin.p_virt->mac;
4715 }
4716
4717 u16 ecore_iov_bulletin_get_forced_vlan(struct ecore_hwfn *p_hwfn,
4718                                        u16 rel_vf_id)
4719 {
4720         struct ecore_vf_info *p_vf;
4721
4722         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4723         if (!p_vf || !p_vf->bulletin.p_virt)
4724                 return 0;
4725
4726         if (!(p_vf->bulletin.p_virt->valid_bitmap & (1 << VLAN_ADDR_FORCED)))
4727                 return 0;
4728
4729         return p_vf->bulletin.p_virt->pvid;
4730 }
4731
4732 enum _ecore_status_t ecore_iov_configure_tx_rate(struct ecore_hwfn *p_hwfn,
4733                                                  struct ecore_ptt *p_ptt,
4734                                                  int vfid, int val)
4735 {
4736         struct ecore_mcp_link_state *p_link;
4737         struct ecore_vf_info *vf;
4738         u8 abs_vp_id = 0;
4739         enum _ecore_status_t rc;
4740
4741         vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4742
4743         if (!vf)
4744                 return ECORE_INVAL;
4745
4746         rc = ecore_fw_vport(p_hwfn, vf->vport_id, &abs_vp_id);
4747         if (rc != ECORE_SUCCESS)
4748                 return rc;
4749
4750         p_link = &ECORE_LEADING_HWFN(p_hwfn->p_dev)->mcp_info->link_output;
4751
4752         return ecore_init_vport_rl(p_hwfn, p_ptt, abs_vp_id, (u32)val,
4753                                    p_link->speed);
4754 }
4755
4756 enum _ecore_status_t ecore_iov_get_vf_stats(struct ecore_hwfn *p_hwfn,
4757                                             struct ecore_ptt *p_ptt,
4758                                             int vfid,
4759                                             struct ecore_eth_stats *p_stats)
4760 {
4761         struct ecore_vf_info *vf;
4762
4763         vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4764         if (!vf)
4765                 return ECORE_INVAL;
4766
4767         if (vf->state != VF_ENABLED)
4768                 return ECORE_INVAL;
4769
4770         __ecore_get_vport_stats(p_hwfn, p_ptt, p_stats,
4771                                 vf->abs_vf_id + 0x10, false);
4772
4773         return ECORE_SUCCESS;
4774 }
4775
4776 u8 ecore_iov_get_vf_num_rxqs(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4777 {
4778         struct ecore_vf_info *p_vf;
4779
4780         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4781         if (!p_vf)
4782                 return 0;
4783
4784         return p_vf->num_rxqs;
4785 }
4786
4787 u8 ecore_iov_get_vf_num_active_rxqs(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4788 {
4789         struct ecore_vf_info *p_vf;
4790
4791         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4792         if (!p_vf)
4793                 return 0;
4794
4795         return p_vf->num_active_rxqs;
4796 }
4797
4798 void *ecore_iov_get_vf_ctx(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4799 {
4800         struct ecore_vf_info *p_vf;
4801
4802         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4803         if (!p_vf)
4804                 return OSAL_NULL;
4805
4806         return p_vf->ctx;
4807 }
4808
4809 u8 ecore_iov_get_vf_num_sbs(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4810 {
4811         struct ecore_vf_info *p_vf;
4812
4813         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4814         if (!p_vf)
4815                 return 0;
4816
4817         return p_vf->num_sbs;
4818 }
4819
4820 bool ecore_iov_is_vf_wait_for_acquire(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4821 {
4822         struct ecore_vf_info *p_vf;
4823
4824         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4825         if (!p_vf)
4826                 return false;
4827
4828         return (p_vf->state == VF_FREE);
4829 }
4830
4831 bool ecore_iov_is_vf_acquired_not_initialized(struct ecore_hwfn *p_hwfn,
4832                                               u16 rel_vf_id)
4833 {
4834         struct ecore_vf_info *p_vf;
4835
4836         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4837         if (!p_vf)
4838                 return false;
4839
4840         return (p_vf->state == VF_ACQUIRED);
4841 }
4842
4843 bool ecore_iov_is_vf_initialized(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4844 {
4845         struct ecore_vf_info *p_vf;
4846
4847         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4848         if (!p_vf)
4849                 return false;
4850
4851         return (p_vf->state == VF_ENABLED);
4852 }
4853
4854 bool ecore_iov_is_vf_started(struct ecore_hwfn *p_hwfn,
4855                              u16 rel_vf_id)
4856 {
4857         struct ecore_vf_info *p_vf;
4858
4859         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4860         if (!p_vf)
4861                 return false;
4862
4863         return (p_vf->state != VF_FREE && p_vf->state != VF_STOPPED);
4864 }
4865
4866 enum _ecore_status_t
4867 ecore_iov_get_vf_min_rate(struct ecore_hwfn *p_hwfn, int vfid)
4868 {
4869         struct ecore_wfq_data *vf_vp_wfq;
4870         struct ecore_vf_info *vf_info;
4871
4872         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4873         if (!vf_info)
4874                 return 0;
4875
4876         vf_vp_wfq = &p_hwfn->qm_info.wfq_data[vf_info->vport_id];
4877
4878         if (vf_vp_wfq->configured)
4879                 return vf_vp_wfq->min_speed;
4880         else
4881                 return 0;
4882 }
4883
4884 #ifdef CONFIG_ECORE_SW_CHANNEL
4885 void ecore_iov_set_vf_hw_channel(struct ecore_hwfn *p_hwfn, int vfid,
4886                                  bool b_is_hw)
4887 {
4888         struct ecore_vf_info *vf_info;
4889
4890         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4891         if (!vf_info)
4892                 return;
4893
4894         vf_info->b_hw_channel = b_is_hw;
4895 }
4896 #endif