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