net/qede/base: use L2-handles for RSS configuration
[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,
2708                               u16 *tlvs_mask, u16 *tlvs_accepted)
2709 {
2710         struct vfpf_vport_update_rss_tlv *p_rss_tlv;
2711         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_RSS;
2712         bool b_reject = false;
2713         u16 table_size;
2714         u16 i, q_idx;
2715
2716         p_rss_tlv = (struct vfpf_vport_update_rss_tlv *)
2717             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2718         if (!p_rss_tlv) {
2719                 p_data->rss_params = OSAL_NULL;
2720                 return;
2721         }
2722
2723         OSAL_MEMSET(p_rss, 0, sizeof(struct ecore_rss_params));
2724
2725         p_rss->update_rss_config =
2726             !!(p_rss_tlv->update_rss_flags &
2727                 VFPF_UPDATE_RSS_CONFIG_FLAG);
2728         p_rss->update_rss_capabilities =
2729             !!(p_rss_tlv->update_rss_flags &
2730                 VFPF_UPDATE_RSS_CAPS_FLAG);
2731         p_rss->update_rss_ind_table =
2732             !!(p_rss_tlv->update_rss_flags &
2733                 VFPF_UPDATE_RSS_IND_TABLE_FLAG);
2734         p_rss->update_rss_key =
2735             !!(p_rss_tlv->update_rss_flags &
2736                 VFPF_UPDATE_RSS_KEY_FLAG);
2737
2738         p_rss->rss_enable = p_rss_tlv->rss_enable;
2739         p_rss->rss_eng_id = vf->relative_vf_id + 1;
2740         p_rss->rss_caps = p_rss_tlv->rss_caps;
2741         p_rss->rss_table_size_log = p_rss_tlv->rss_table_size_log;
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         for (i = 0; i < table_size; i++) {
2749                 q_idx = p_rss_tlv->rss_ind_table[i];
2750                 if (!ecore_iov_validate_rxq(p_hwfn, vf, q_idx)) {
2751                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2752                                    "VF[%d]: Omitting RSS due to wrong queue %04x\n",
2753                                    vf->relative_vf_id, q_idx);
2754                         b_reject = true;
2755                         goto out;
2756                 }
2757
2758                 if (!vf->vf_queues[q_idx].p_rx_cid) {
2759                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2760                                    "VF[%d]: Omitting RSS due to inactive queue %08x\n",
2761                                    vf->relative_vf_id, q_idx);
2762                         b_reject = true;
2763                         goto out;
2764                 }
2765
2766                 p_rss->rss_ind_table[i] = vf->vf_queues[q_idx].p_rx_cid;
2767         }
2768
2769         p_data->rss_params = p_rss;
2770 out:
2771         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_RSS;
2772         if (!b_reject)
2773                 *tlvs_accepted |= 1 << ECORE_IOV_VP_UPDATE_RSS;
2774 }
2775
2776 static void
2777 ecore_iov_vp_update_sge_tpa_param(struct ecore_hwfn *p_hwfn,
2778                                   struct ecore_vf_info *vf,
2779                                   struct ecore_sp_vport_update_params *p_data,
2780                                   struct ecore_sge_tpa_params *p_sge_tpa,
2781                                   struct ecore_iov_vf_mbx *p_mbx,
2782                                   u16 *tlvs_mask)
2783 {
2784         struct vfpf_vport_update_sge_tpa_tlv *p_sge_tpa_tlv;
2785         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_SGE_TPA;
2786
2787         p_sge_tpa_tlv = (struct vfpf_vport_update_sge_tpa_tlv *)
2788             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2789
2790         if (!p_sge_tpa_tlv) {
2791                 p_data->sge_tpa_params = OSAL_NULL;
2792                 return;
2793         }
2794
2795         OSAL_MEMSET(p_sge_tpa, 0, sizeof(struct ecore_sge_tpa_params));
2796
2797         p_sge_tpa->update_tpa_en_flg =
2798             !!(p_sge_tpa_tlv->update_sge_tpa_flags & VFPF_UPDATE_TPA_EN_FLAG);
2799         p_sge_tpa->update_tpa_param_flg =
2800             !!(p_sge_tpa_tlv->update_sge_tpa_flags &
2801                 VFPF_UPDATE_TPA_PARAM_FLAG);
2802
2803         p_sge_tpa->tpa_ipv4_en_flg =
2804             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_IPV4_EN_FLAG);
2805         p_sge_tpa->tpa_ipv6_en_flg =
2806             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_IPV6_EN_FLAG);
2807         p_sge_tpa->tpa_pkt_split_flg =
2808             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_PKT_SPLIT_FLAG);
2809         p_sge_tpa->tpa_hdr_data_split_flg =
2810             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_HDR_DATA_SPLIT_FLAG);
2811         p_sge_tpa->tpa_gro_consistent_flg =
2812             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_GRO_CONSIST_FLAG);
2813
2814         p_sge_tpa->tpa_max_aggs_num = p_sge_tpa_tlv->tpa_max_aggs_num;
2815         p_sge_tpa->tpa_max_size = p_sge_tpa_tlv->tpa_max_size;
2816         p_sge_tpa->tpa_min_size_to_start = p_sge_tpa_tlv->tpa_min_size_to_start;
2817         p_sge_tpa->tpa_min_size_to_cont = p_sge_tpa_tlv->tpa_min_size_to_cont;
2818         p_sge_tpa->max_buffers_per_cqe = p_sge_tpa_tlv->max_buffers_per_cqe;
2819
2820         p_data->sge_tpa_params = p_sge_tpa;
2821
2822         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_SGE_TPA;
2823 }
2824
2825 static void ecore_iov_vf_mbx_vport_update(struct ecore_hwfn *p_hwfn,
2826                                           struct ecore_ptt *p_ptt,
2827                                           struct ecore_vf_info *vf)
2828 {
2829         struct ecore_rss_params *p_rss_params = OSAL_NULL;
2830         struct ecore_sp_vport_update_params params;
2831         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2832         struct ecore_sge_tpa_params sge_tpa_params;
2833         u16 tlvs_mask = 0, tlvs_accepted = 0;
2834         u8 status = PFVF_STATUS_SUCCESS;
2835         u16 length;
2836         enum _ecore_status_t rc;
2837
2838         /* Valiate PF can send such a request */
2839         if (!vf->vport_instance) {
2840                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2841                            "No VPORT instance available for VF[%d],"
2842                            " failing vport update\n",
2843                            vf->abs_vf_id);
2844                 status = PFVF_STATUS_FAILURE;
2845                 goto out;
2846         }
2847
2848         p_rss_params = OSAL_VALLOC(p_hwfn->p_dev, sizeof(*p_rss_params));
2849         if (p_rss_params == OSAL_NULL) {
2850                 status = PFVF_STATUS_FAILURE;
2851                 goto out;
2852         }
2853
2854         OSAL_MEMSET(&params, 0, sizeof(params));
2855         params.opaque_fid = vf->opaque_fid;
2856         params.vport_id = vf->vport_id;
2857         params.rss_params = OSAL_NULL;
2858
2859         /* Search for extended tlvs list and update values
2860          * from VF in struct ecore_sp_vport_update_params.
2861          */
2862         ecore_iov_vp_update_act_param(p_hwfn, &params, mbx, &tlvs_mask);
2863         ecore_iov_vp_update_vlan_param(p_hwfn, &params, vf, mbx, &tlvs_mask);
2864         ecore_iov_vp_update_tx_switch(p_hwfn, &params, mbx, &tlvs_mask);
2865         ecore_iov_vp_update_mcast_bin_param(p_hwfn, &params, mbx, &tlvs_mask);
2866         ecore_iov_vp_update_accept_flag(p_hwfn, &params, mbx, &tlvs_mask);
2867         ecore_iov_vp_update_accept_any_vlan(p_hwfn, &params, mbx, &tlvs_mask);
2868         ecore_iov_vp_update_sge_tpa_param(p_hwfn, vf, &params,
2869                                           &sge_tpa_params, mbx, &tlvs_mask);
2870
2871         tlvs_accepted = tlvs_mask;
2872
2873         /* Some of the extended TLVs need to be validated first; In that case,
2874          * they can update the mask without updating the accepted [so that
2875          * PF could communicate to VF it has rejected request].
2876          */
2877         ecore_iov_vp_update_rss_param(p_hwfn, vf, &params, p_rss_params,
2878                                       mbx, &tlvs_mask, &tlvs_accepted);
2879
2880         /* Just log a message if there is no single extended tlv in buffer.
2881          * When all features of vport update ramrod would be requested by VF
2882          * as extended TLVs in buffer then an error can be returned in response
2883          * if there is no extended TLV present in buffer.
2884          */
2885         if (OSAL_IOV_VF_VPORT_UPDATE(p_hwfn, vf->relative_vf_id,
2886                                      &params, &tlvs_accepted) !=
2887             ECORE_SUCCESS) {
2888                 tlvs_accepted = 0;
2889                 status = PFVF_STATUS_NOT_SUPPORTED;
2890                 goto out;
2891         }
2892
2893         if (!tlvs_accepted) {
2894                 if (tlvs_mask)
2895                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2896                                    "Upper-layer prevents said VF"
2897                                    " configuration\n");
2898                 else
2899                         DP_NOTICE(p_hwfn, true,
2900                                   "No feature tlvs found for vport update\n");
2901                 status = PFVF_STATUS_NOT_SUPPORTED;
2902                 goto out;
2903         }
2904
2905         rc = ecore_sp_vport_update(p_hwfn, &params, ECORE_SPQ_MODE_EBLOCK,
2906                                    OSAL_NULL);
2907
2908         if (rc)
2909                 status = PFVF_STATUS_FAILURE;
2910
2911 out:
2912         OSAL_VFREE(p_hwfn->p_dev, p_rss_params);
2913         length = ecore_iov_prep_vp_update_resp_tlvs(p_hwfn, vf, mbx, status,
2914                                                     tlvs_mask, tlvs_accepted);
2915         ecore_iov_send_response(p_hwfn, p_ptt, vf, length, status);
2916 }
2917
2918 static enum _ecore_status_t
2919 ecore_iov_vf_update_vlan_shadow(struct ecore_hwfn *p_hwfn,
2920                                 struct ecore_vf_info *p_vf,
2921                                 struct ecore_filter_ucast *p_params)
2922 {
2923         int i;
2924
2925         /* First remove entries and then add new ones */
2926         if (p_params->opcode == ECORE_FILTER_REMOVE) {
2927                 for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++)
2928                         if (p_vf->shadow_config.vlans[i].used &&
2929                             p_vf->shadow_config.vlans[i].vid ==
2930                             p_params->vlan) {
2931                                 p_vf->shadow_config.vlans[i].used = false;
2932                                 break;
2933                         }
2934                 if (i == ECORE_ETH_VF_NUM_VLAN_FILTERS + 1) {
2935                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2936                                    "VF [%d] - Tries to remove a non-existing"
2937                                    " vlan\n",
2938                                    p_vf->relative_vf_id);
2939                         return ECORE_INVAL;
2940                 }
2941         } else if (p_params->opcode == ECORE_FILTER_REPLACE ||
2942                    p_params->opcode == ECORE_FILTER_FLUSH) {
2943                 for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++)
2944                         p_vf->shadow_config.vlans[i].used = false;
2945         }
2946
2947         /* In forced mode, we're willing to remove entries - but we don't add
2948          * new ones.
2949          */
2950         if (p_vf->bulletin.p_virt->valid_bitmap & (1 << VLAN_ADDR_FORCED))
2951                 return ECORE_SUCCESS;
2952
2953         if (p_params->opcode == ECORE_FILTER_ADD ||
2954             p_params->opcode == ECORE_FILTER_REPLACE) {
2955                 for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++) {
2956                         if (p_vf->shadow_config.vlans[i].used)
2957                                 continue;
2958
2959                         p_vf->shadow_config.vlans[i].used = true;
2960                         p_vf->shadow_config.vlans[i].vid = p_params->vlan;
2961                         break;
2962                 }
2963
2964                 if (i == ECORE_ETH_VF_NUM_VLAN_FILTERS + 1) {
2965                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2966                                    "VF [%d] - Tries to configure more than %d"
2967                                    " vlan filters\n",
2968                                    p_vf->relative_vf_id,
2969                                    ECORE_ETH_VF_NUM_VLAN_FILTERS + 1);
2970                         return ECORE_INVAL;
2971                 }
2972         }
2973
2974         return ECORE_SUCCESS;
2975 }
2976
2977 static enum _ecore_status_t
2978 ecore_iov_vf_update_mac_shadow(struct ecore_hwfn *p_hwfn,
2979                                struct ecore_vf_info *p_vf,
2980                                struct ecore_filter_ucast *p_params)
2981 {
2982         char empty_mac[ETH_ALEN];
2983         int i;
2984
2985         OSAL_MEM_ZERO(empty_mac, ETH_ALEN);
2986
2987         /* If we're in forced-mode, we don't allow any change */
2988         /* TODO - this would change if we were ever to implement logic for
2989          * removing a forced MAC altogether [in which case, like for vlans,
2990          * we should be able to re-trace previous configuration.
2991          */
2992         if (p_vf->bulletin.p_virt->valid_bitmap & (1 << MAC_ADDR_FORCED))
2993                 return ECORE_SUCCESS;
2994
2995         /* First remove entries and then add new ones */
2996         if (p_params->opcode == ECORE_FILTER_REMOVE) {
2997                 for (i = 0; i < ECORE_ETH_VF_NUM_MAC_FILTERS; i++) {
2998                         if (!OSAL_MEMCMP(p_vf->shadow_config.macs[i],
2999                                          p_params->mac, ETH_ALEN)) {
3000                                 OSAL_MEM_ZERO(p_vf->shadow_config.macs[i],
3001                                               ETH_ALEN);
3002                                 break;
3003                         }
3004                 }
3005
3006                 if (i == ECORE_ETH_VF_NUM_MAC_FILTERS) {
3007                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3008                                    "MAC isn't configured\n");
3009                         return ECORE_INVAL;
3010                 }
3011         } else if (p_params->opcode == ECORE_FILTER_REPLACE ||
3012                    p_params->opcode == ECORE_FILTER_FLUSH) {
3013                 for (i = 0; i < ECORE_ETH_VF_NUM_MAC_FILTERS; i++)
3014                         OSAL_MEM_ZERO(p_vf->shadow_config.macs[i], ETH_ALEN);
3015         }
3016
3017         /* List the new MAC address */
3018         if (p_params->opcode != ECORE_FILTER_ADD &&
3019             p_params->opcode != ECORE_FILTER_REPLACE)
3020                 return ECORE_SUCCESS;
3021
3022         for (i = 0; i < ECORE_ETH_VF_NUM_MAC_FILTERS; i++) {
3023                 if (!OSAL_MEMCMP(p_vf->shadow_config.macs[i],
3024                                  empty_mac, ETH_ALEN)) {
3025                         OSAL_MEMCPY(p_vf->shadow_config.macs[i],
3026                                     p_params->mac, ETH_ALEN);
3027                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3028                                    "Added MAC at %d entry in shadow\n", i);
3029                         break;
3030                 }
3031         }
3032
3033         if (i == ECORE_ETH_VF_NUM_MAC_FILTERS) {
3034                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3035                            "No available place for MAC\n");
3036                 return ECORE_INVAL;
3037         }
3038
3039         return ECORE_SUCCESS;
3040 }
3041
3042 static enum _ecore_status_t
3043 ecore_iov_vf_update_unicast_shadow(struct ecore_hwfn *p_hwfn,
3044                                    struct ecore_vf_info *p_vf,
3045                                    struct ecore_filter_ucast *p_params)
3046 {
3047         enum _ecore_status_t rc = ECORE_SUCCESS;
3048
3049         if (p_params->type == ECORE_FILTER_MAC) {
3050                 rc = ecore_iov_vf_update_mac_shadow(p_hwfn, p_vf, p_params);
3051                 if (rc != ECORE_SUCCESS)
3052                         return rc;
3053         }
3054
3055         if (p_params->type == ECORE_FILTER_VLAN)
3056                 rc = ecore_iov_vf_update_vlan_shadow(p_hwfn, p_vf, p_params);
3057
3058         return rc;
3059 }
3060
3061 static void ecore_iov_vf_mbx_ucast_filter(struct ecore_hwfn *p_hwfn,
3062                                           struct ecore_ptt *p_ptt,
3063                                           struct ecore_vf_info *vf)
3064 {
3065         struct ecore_bulletin_content *p_bulletin = vf->bulletin.p_virt;
3066         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
3067         struct vfpf_ucast_filter_tlv *req;
3068         u8 status = PFVF_STATUS_SUCCESS;
3069         struct ecore_filter_ucast params;
3070         enum _ecore_status_t rc;
3071
3072         /* Prepare the unicast filter params */
3073         OSAL_MEMSET(&params, 0, sizeof(struct ecore_filter_ucast));
3074         req = &mbx->req_virt->ucast_filter;
3075         params.opcode = (enum ecore_filter_opcode)req->opcode;
3076         params.type = (enum ecore_filter_ucast_type)req->type;
3077
3078         /* @@@TBD - We might need logic on HV side in determining this */
3079         params.is_rx_filter = 1;
3080         params.is_tx_filter = 1;
3081         params.vport_to_remove_from = vf->vport_id;
3082         params.vport_to_add_to = vf->vport_id;
3083         OSAL_MEMCPY(params.mac, req->mac, ETH_ALEN);
3084         params.vlan = req->vlan;
3085
3086         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3087                    "VF[%d]: opcode 0x%02x type 0x%02x [%s %s] [vport 0x%02x]"
3088                    " MAC %02x:%02x:%02x:%02x:%02x:%02x, vlan 0x%04x\n",
3089                    vf->abs_vf_id, params.opcode, params.type,
3090                    params.is_rx_filter ? "RX" : "",
3091                    params.is_tx_filter ? "TX" : "",
3092                    params.vport_to_add_to,
3093                    params.mac[0], params.mac[1], params.mac[2],
3094                    params.mac[3], params.mac[4], params.mac[5], params.vlan);
3095
3096         if (!vf->vport_instance) {
3097                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3098                            "No VPORT instance available for VF[%d],"
3099                            " failing ucast MAC configuration\n",
3100                            vf->abs_vf_id);
3101                 status = PFVF_STATUS_FAILURE;
3102                 goto out;
3103         }
3104
3105         /* Update shadow copy of the VF configuration */
3106         if (ecore_iov_vf_update_unicast_shadow(p_hwfn, vf, &params) !=
3107             ECORE_SUCCESS) {
3108                 status = PFVF_STATUS_FAILURE;
3109                 goto out;
3110         }
3111
3112         /* Determine if the unicast filtering is acceptible by PF */
3113         if ((p_bulletin->valid_bitmap & (1 << VLAN_ADDR_FORCED)) &&
3114             (params.type == ECORE_FILTER_VLAN ||
3115              params.type == ECORE_FILTER_MAC_VLAN)) {
3116                 /* Once VLAN is forced or PVID is set, do not allow
3117                  * to add/replace any further VLANs.
3118                  */
3119                 if (params.opcode == ECORE_FILTER_ADD ||
3120                     params.opcode == ECORE_FILTER_REPLACE)
3121                         status = PFVF_STATUS_FORCED;
3122                 goto out;
3123         }
3124
3125         if ((p_bulletin->valid_bitmap & (1 << MAC_ADDR_FORCED)) &&
3126             (params.type == ECORE_FILTER_MAC ||
3127              params.type == ECORE_FILTER_MAC_VLAN)) {
3128                 if (OSAL_MEMCMP(p_bulletin->mac, params.mac, ETH_ALEN) ||
3129                     (params.opcode != ECORE_FILTER_ADD &&
3130                      params.opcode != ECORE_FILTER_REPLACE))
3131                         status = PFVF_STATUS_FORCED;
3132                 goto out;
3133         }
3134
3135         rc = OSAL_IOV_CHK_UCAST(p_hwfn, vf->relative_vf_id, &params);
3136         if (rc == ECORE_EXISTS) {
3137                 goto out;
3138         } else if (rc == ECORE_INVAL) {
3139                 status = PFVF_STATUS_FAILURE;
3140                 goto out;
3141         }
3142
3143         rc = ecore_sp_eth_filter_ucast(p_hwfn, vf->opaque_fid, &params,
3144                                        ECORE_SPQ_MODE_CB, OSAL_NULL);
3145         if (rc)
3146                 status = PFVF_STATUS_FAILURE;
3147
3148 out:
3149         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_UCAST_FILTER,
3150                                sizeof(struct pfvf_def_resp_tlv), status);
3151 }
3152
3153 static void ecore_iov_vf_mbx_int_cleanup(struct ecore_hwfn *p_hwfn,
3154                                          struct ecore_ptt *p_ptt,
3155                                          struct ecore_vf_info *vf)
3156 {
3157         int i;
3158
3159         /* Reset the SBs */
3160         for (i = 0; i < vf->num_sbs; i++)
3161                 ecore_int_igu_init_pure_rt_single(p_hwfn, p_ptt,
3162                                                   vf->igu_sbs[i],
3163                                                   vf->opaque_fid, false);
3164
3165         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_INT_CLEANUP,
3166                                sizeof(struct pfvf_def_resp_tlv),
3167                                PFVF_STATUS_SUCCESS);
3168 }
3169
3170 static void ecore_iov_vf_mbx_close(struct ecore_hwfn *p_hwfn,
3171                                    struct ecore_ptt *p_ptt,
3172                                    struct ecore_vf_info *vf)
3173 {
3174         u16 length = sizeof(struct pfvf_def_resp_tlv);
3175         u8 status = PFVF_STATUS_SUCCESS;
3176
3177         /* Disable Interrupts for VF */
3178         ecore_iov_vf_igu_set_int(p_hwfn, p_ptt, vf, 0);
3179
3180         /* Reset Permission table */
3181         ecore_iov_config_perm_table(p_hwfn, p_ptt, vf, 0);
3182
3183         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_CLOSE,
3184                                length, status);
3185 }
3186
3187 static void ecore_iov_vf_mbx_release(struct ecore_hwfn *p_hwfn,
3188                                      struct ecore_ptt *p_ptt,
3189                                      struct ecore_vf_info *p_vf)
3190 {
3191         u16 length = sizeof(struct pfvf_def_resp_tlv);
3192         u8 status = PFVF_STATUS_SUCCESS;
3193         enum _ecore_status_t rc = ECORE_SUCCESS;
3194
3195         ecore_iov_vf_cleanup(p_hwfn, p_vf);
3196
3197         if (p_vf->state != VF_STOPPED && p_vf->state != VF_FREE) {
3198                 /* Stopping the VF */
3199                 rc = ecore_sp_vf_stop(p_hwfn, p_vf->concrete_fid,
3200                                       p_vf->opaque_fid);
3201
3202                 if (rc != ECORE_SUCCESS) {
3203                         DP_ERR(p_hwfn, "ecore_sp_vf_stop returned error %d\n",
3204                                rc);
3205                         status = PFVF_STATUS_FAILURE;
3206                 }
3207
3208                 p_vf->state = VF_STOPPED;
3209         }
3210
3211         ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf, CHANNEL_TLV_RELEASE,
3212                                length, status);
3213 }
3214
3215 static enum _ecore_status_t
3216 ecore_iov_vf_flr_poll_dorq(struct ecore_hwfn *p_hwfn,
3217                            struct ecore_vf_info *p_vf, struct ecore_ptt *p_ptt)
3218 {
3219         int cnt;
3220         u32 val;
3221
3222         ecore_fid_pretend(p_hwfn, p_ptt, (u16)p_vf->concrete_fid);
3223
3224         for (cnt = 0; cnt < 50; cnt++) {
3225                 val = ecore_rd(p_hwfn, p_ptt, DORQ_REG_VF_USAGE_CNT);
3226                 if (!val)
3227                         break;
3228                 OSAL_MSLEEP(20);
3229         }
3230         ecore_fid_pretend(p_hwfn, p_ptt, (u16)p_hwfn->hw_info.concrete_fid);
3231
3232         if (cnt == 50) {
3233                 DP_ERR(p_hwfn,
3234                        "VF[%d] - dorq failed to cleanup [usage 0x%08x]\n",
3235                        p_vf->abs_vf_id, val);
3236                 return ECORE_TIMEOUT;
3237         }
3238
3239         return ECORE_SUCCESS;
3240 }
3241
3242 static enum _ecore_status_t
3243 ecore_iov_vf_flr_poll_pbf(struct ecore_hwfn *p_hwfn,
3244                           struct ecore_vf_info *p_vf, struct ecore_ptt *p_ptt)
3245 {
3246         u32 cons[MAX_NUM_VOQS], distance[MAX_NUM_VOQS];
3247         int i, cnt;
3248
3249         /* Read initial consumers & producers */
3250         for (i = 0; i < MAX_NUM_VOQS; i++) {
3251                 u32 prod;
3252
3253                 cons[i] = ecore_rd(p_hwfn, p_ptt,
3254                                    PBF_REG_NUM_BLOCKS_ALLOCATED_CONS_VOQ0 +
3255                                    i * 0x40);
3256                 prod = ecore_rd(p_hwfn, p_ptt,
3257                                 PBF_REG_NUM_BLOCKS_ALLOCATED_PROD_VOQ0 +
3258                                 i * 0x40);
3259                 distance[i] = prod - cons[i];
3260         }
3261
3262         /* Wait for consumers to pass the producers */
3263         i = 0;
3264         for (cnt = 0; cnt < 50; cnt++) {
3265                 for (; i < MAX_NUM_VOQS; i++) {
3266                         u32 tmp;
3267
3268                         tmp = ecore_rd(p_hwfn, p_ptt,
3269                                        PBF_REG_NUM_BLOCKS_ALLOCATED_CONS_VOQ0 +
3270                                        i * 0x40);
3271                         if (distance[i] > tmp - cons[i])
3272                                 break;
3273                 }
3274
3275                 if (i == MAX_NUM_VOQS)
3276                         break;
3277
3278                 OSAL_MSLEEP(20);
3279         }
3280
3281         if (cnt == 50) {
3282                 DP_ERR(p_hwfn, "VF[%d] - pbf polling failed on VOQ %d\n",
3283                        p_vf->abs_vf_id, i);
3284                 return ECORE_TIMEOUT;
3285         }
3286
3287         return ECORE_SUCCESS;
3288 }
3289
3290 static enum _ecore_status_t ecore_iov_vf_flr_poll(struct ecore_hwfn *p_hwfn,
3291                                                   struct ecore_vf_info *p_vf,
3292                                                   struct ecore_ptt *p_ptt)
3293 {
3294         enum _ecore_status_t rc;
3295
3296         /* TODO - add SRC and TM polling once we add storage IOV */
3297
3298         rc = ecore_iov_vf_flr_poll_dorq(p_hwfn, p_vf, p_ptt);
3299         if (rc)
3300                 return rc;
3301
3302         rc = ecore_iov_vf_flr_poll_pbf(p_hwfn, p_vf, p_ptt);
3303         if (rc)
3304                 return rc;
3305
3306         return ECORE_SUCCESS;
3307 }
3308
3309 static enum _ecore_status_t
3310 ecore_iov_execute_vf_flr_cleanup(struct ecore_hwfn *p_hwfn,
3311                                  struct ecore_ptt *p_ptt,
3312                                  u16 rel_vf_id, u32 *ack_vfs)
3313 {
3314         struct ecore_vf_info *p_vf;
3315         enum _ecore_status_t rc = ECORE_SUCCESS;
3316
3317         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, false);
3318         if (!p_vf)
3319                 return ECORE_SUCCESS;
3320
3321         if (p_hwfn->pf_iov_info->pending_flr[rel_vf_id / 64] &
3322             (1ULL << (rel_vf_id % 64))) {
3323                 u16 vfid = p_vf->abs_vf_id;
3324
3325                 /* TODO - should we lock channel? */
3326
3327                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3328                            "VF[%d] - Handling FLR\n", vfid);
3329
3330                 ecore_iov_vf_cleanup(p_hwfn, p_vf);
3331
3332                 /* If VF isn't active, no need for anything but SW */
3333                 if (!p_vf->b_init)
3334                         goto cleanup;
3335
3336                 /* TODO - what to do in case of failure? */
3337                 rc = ecore_iov_vf_flr_poll(p_hwfn, p_vf, p_ptt);
3338                 if (rc != ECORE_SUCCESS)
3339                         goto cleanup;
3340
3341                 rc = ecore_final_cleanup(p_hwfn, p_ptt, vfid, true);
3342                 if (rc) {
3343                         /* TODO - what's now? What a mess.... */
3344                         DP_ERR(p_hwfn, "Failed handle FLR of VF[%d]\n", vfid);
3345                         return rc;
3346                 }
3347
3348                 /* Workaround to make VF-PF channel ready, as FW
3349                  * doesn't do that as a part of FLR.
3350                  */
3351                 REG_WR(p_hwfn,
3352                        GTT_BAR0_MAP_REG_USDM_RAM +
3353                        USTORM_VF_PF_CHANNEL_READY_OFFSET(vfid), 1);
3354
3355                 /* VF_STOPPED has to be set only after final cleanup
3356                  * but prior to re-enabling the VF.
3357                  */
3358                 p_vf->state = VF_STOPPED;
3359
3360                 rc = ecore_iov_enable_vf_access(p_hwfn, p_ptt, p_vf);
3361                 if (rc) {
3362                         /* TODO - again, a mess... */
3363                         DP_ERR(p_hwfn, "Failed to re-enable VF[%d] acces\n",
3364                                vfid);
3365                         return rc;
3366                 }
3367 cleanup:
3368                 /* Mark VF for ack and clean pending state */
3369                 if (p_vf->state == VF_RESET)
3370                         p_vf->state = VF_STOPPED;
3371                 ack_vfs[vfid / 32] |= (1 << (vfid % 32));
3372                 p_hwfn->pf_iov_info->pending_flr[rel_vf_id / 64] &=
3373                     ~(1ULL << (rel_vf_id % 64));
3374                 p_hwfn->pf_iov_info->pending_events[rel_vf_id / 64] &=
3375                     ~(1ULL << (rel_vf_id % 64));
3376         }
3377
3378         return rc;
3379 }
3380
3381 enum _ecore_status_t ecore_iov_vf_flr_cleanup(struct ecore_hwfn *p_hwfn,
3382                                               struct ecore_ptt *p_ptt)
3383 {
3384         u32 ack_vfs[VF_MAX_STATIC / 32];
3385         enum _ecore_status_t rc = ECORE_SUCCESS;
3386         u16 i;
3387
3388         OSAL_MEMSET(ack_vfs, 0, sizeof(u32) * (VF_MAX_STATIC / 32));
3389
3390         /* Since BRB <-> PRS interface can't be tested as part of the flr
3391          * polling due to HW limitations, simply sleep a bit. And since
3392          * there's no need to wait per-vf, do it before looping.
3393          */
3394         OSAL_MSLEEP(100);
3395
3396         for (i = 0; i < p_hwfn->p_dev->p_iov_info->total_vfs; i++)
3397                 ecore_iov_execute_vf_flr_cleanup(p_hwfn, p_ptt, i, ack_vfs);
3398
3399         rc = ecore_mcp_ack_vf_flr(p_hwfn, p_ptt, ack_vfs);
3400         return rc;
3401 }
3402
3403 enum _ecore_status_t
3404 ecore_iov_single_vf_flr_cleanup(struct ecore_hwfn *p_hwfn,
3405                                 struct ecore_ptt *p_ptt, u16 rel_vf_id)
3406 {
3407         u32 ack_vfs[VF_MAX_STATIC / 32];
3408         enum _ecore_status_t rc = ECORE_SUCCESS;
3409
3410         OSAL_MEMSET(ack_vfs, 0, sizeof(u32) * (VF_MAX_STATIC / 32));
3411
3412         /* Wait instead of polling the BRB <-> PRS interface */
3413         OSAL_MSLEEP(100);
3414
3415         ecore_iov_execute_vf_flr_cleanup(p_hwfn, p_ptt, rel_vf_id, ack_vfs);
3416
3417         rc = ecore_mcp_ack_vf_flr(p_hwfn, p_ptt, ack_vfs);
3418         return rc;
3419 }
3420
3421 bool ecore_iov_mark_vf_flr(struct ecore_hwfn *p_hwfn, u32 *p_disabled_vfs)
3422 {
3423         bool found = false;
3424         u16 i;
3425
3426         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV, "Marking FLR-ed VFs\n");
3427         for (i = 0; i < (VF_MAX_STATIC / 32); i++)
3428                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3429                            "[%08x,...,%08x]: %08x\n",
3430                            i * 32, (i + 1) * 32 - 1, p_disabled_vfs[i]);
3431
3432         if (!p_hwfn->p_dev->p_iov_info) {
3433                 DP_NOTICE(p_hwfn, true, "VF flr but no IOV\n");
3434                 return false;
3435         }
3436
3437         /* Mark VFs */
3438         for (i = 0; i < p_hwfn->p_dev->p_iov_info->total_vfs; i++) {
3439                 struct ecore_vf_info *p_vf;
3440                 u8 vfid;
3441
3442                 p_vf = ecore_iov_get_vf_info(p_hwfn, i, false);
3443                 if (!p_vf)
3444                         continue;
3445
3446                 vfid = p_vf->abs_vf_id;
3447                 if ((1 << (vfid % 32)) & p_disabled_vfs[vfid / 32]) {
3448                         u64 *p_flr = p_hwfn->pf_iov_info->pending_flr;
3449                         u16 rel_vf_id = p_vf->relative_vf_id;
3450
3451                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3452                                    "VF[%d] [rel %d] got FLR-ed\n",
3453                                    vfid, rel_vf_id);
3454
3455                         p_vf->state = VF_RESET;
3456
3457                         /* No need to lock here, since pending_flr should
3458                          * only change here and before ACKing MFw. Since
3459                          * MFW will not trigger an additional attention for
3460                          * VF flr until ACKs, we're safe.
3461                          */
3462                         p_flr[rel_vf_id / 64] |= 1ULL << (rel_vf_id % 64);
3463                         found = true;
3464                 }
3465         }
3466
3467         return found;
3468 }
3469
3470 void ecore_iov_get_link(struct ecore_hwfn *p_hwfn,
3471                         u16 vfid,
3472                         struct ecore_mcp_link_params *p_params,
3473                         struct ecore_mcp_link_state *p_link,
3474                         struct ecore_mcp_link_capabilities *p_caps)
3475 {
3476         struct ecore_vf_info *p_vf = ecore_iov_get_vf_info(p_hwfn, vfid, false);
3477         struct ecore_bulletin_content *p_bulletin;
3478
3479         if (!p_vf)
3480                 return;
3481
3482         p_bulletin = p_vf->bulletin.p_virt;
3483
3484         if (p_params)
3485                 __ecore_vf_get_link_params(p_hwfn, p_params, p_bulletin);
3486         if (p_link)
3487                 __ecore_vf_get_link_state(p_hwfn, p_link, p_bulletin);
3488         if (p_caps)
3489                 __ecore_vf_get_link_caps(p_hwfn, p_caps, p_bulletin);
3490 }
3491
3492 void ecore_iov_process_mbx_req(struct ecore_hwfn *p_hwfn,
3493                                struct ecore_ptt *p_ptt, int vfid)
3494 {
3495         struct ecore_iov_vf_mbx *mbx;
3496         struct ecore_vf_info *p_vf;
3497
3498         p_vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3499         if (!p_vf)
3500                 return;
3501
3502         mbx = &p_vf->vf_mbx;
3503
3504         /* ecore_iov_process_mbx_request */
3505         DP_VERBOSE(p_hwfn,
3506                    ECORE_MSG_IOV,
3507                    "VF[%02x]: Processing mailbox message\n", p_vf->abs_vf_id);
3508
3509         mbx->first_tlv = mbx->req_virt->first_tlv;
3510
3511         OSAL_IOV_VF_MSG_TYPE(p_hwfn,
3512                              p_vf->relative_vf_id,
3513                              mbx->first_tlv.tl.type);
3514
3515         /* Lock the per vf op mutex and note the locker's identity.
3516          * The unlock will take place in mbx response.
3517          */
3518         ecore_iov_lock_vf_pf_channel(p_hwfn,
3519                                      p_vf, mbx->first_tlv.tl.type);
3520
3521         /* check if tlv type is known */
3522         if (ecore_iov_tlv_supported(mbx->first_tlv.tl.type) &&
3523             !p_vf->b_malicious) {
3524                 /* switch on the opcode */
3525                 switch (mbx->first_tlv.tl.type) {
3526                 case CHANNEL_TLV_ACQUIRE:
3527                         ecore_iov_vf_mbx_acquire(p_hwfn, p_ptt, p_vf);
3528                         break;
3529                 case CHANNEL_TLV_VPORT_START:
3530                         ecore_iov_vf_mbx_start_vport(p_hwfn, p_ptt, p_vf);
3531                         break;
3532                 case CHANNEL_TLV_VPORT_TEARDOWN:
3533                         ecore_iov_vf_mbx_stop_vport(p_hwfn, p_ptt, p_vf);
3534                         break;
3535                 case CHANNEL_TLV_START_RXQ:
3536                         ecore_iov_vf_mbx_start_rxq(p_hwfn, p_ptt, p_vf);
3537                         break;
3538                 case CHANNEL_TLV_START_TXQ:
3539                         ecore_iov_vf_mbx_start_txq(p_hwfn, p_ptt, p_vf);
3540                         break;
3541                 case CHANNEL_TLV_STOP_RXQS:
3542                         ecore_iov_vf_mbx_stop_rxqs(p_hwfn, p_ptt, p_vf);
3543                         break;
3544                 case CHANNEL_TLV_STOP_TXQS:
3545                         ecore_iov_vf_mbx_stop_txqs(p_hwfn, p_ptt, p_vf);
3546                         break;
3547                 case CHANNEL_TLV_UPDATE_RXQ:
3548                         ecore_iov_vf_mbx_update_rxqs(p_hwfn, p_ptt, p_vf);
3549                         break;
3550                 case CHANNEL_TLV_VPORT_UPDATE:
3551                         ecore_iov_vf_mbx_vport_update(p_hwfn, p_ptt, p_vf);
3552                         break;
3553                 case CHANNEL_TLV_UCAST_FILTER:
3554                         ecore_iov_vf_mbx_ucast_filter(p_hwfn, p_ptt, p_vf);
3555                         break;
3556                 case CHANNEL_TLV_CLOSE:
3557                         ecore_iov_vf_mbx_close(p_hwfn, p_ptt, p_vf);
3558                         break;
3559                 case CHANNEL_TLV_INT_CLEANUP:
3560                         ecore_iov_vf_mbx_int_cleanup(p_hwfn, p_ptt, p_vf);
3561                         break;
3562                 case CHANNEL_TLV_RELEASE:
3563                         ecore_iov_vf_mbx_release(p_hwfn, p_ptt, p_vf);
3564                         break;
3565                 case CHANNEL_TLV_UPDATE_TUNN_PARAM:
3566                         ecore_iov_vf_mbx_update_tunn_param(p_hwfn, p_ptt, p_vf);
3567                         break;
3568                 }
3569         } else if (ecore_iov_tlv_supported(mbx->first_tlv.tl.type)) {
3570                 /* If we've received a message from a VF we consider malicious
3571                  * we ignore the messasge unless it's one for RELEASE, in which
3572                  * case we'll let it have the benefit of doubt, allowing the
3573                  * next loaded driver to start again.
3574                  */
3575                 if (mbx->first_tlv.tl.type == CHANNEL_TLV_RELEASE) {
3576                         /* TODO - initiate FLR, remove malicious indication */
3577                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3578                                    "VF [%02x] - considered malicious, but wanted to RELEASE. TODO\n",
3579                                    p_vf->abs_vf_id);
3580                 } else {
3581                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3582                                    "VF [%02x] - considered malicious; Ignoring TLV [%04x]\n",
3583                                    p_vf->abs_vf_id, mbx->first_tlv.tl.type);
3584                 }
3585
3586                 ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf,
3587                                        mbx->first_tlv.tl.type,
3588                                        sizeof(struct pfvf_def_resp_tlv),
3589                                        PFVF_STATUS_MALICIOUS);
3590         } else {
3591                 /* unknown TLV - this may belong to a VF driver from the future
3592                  * - a version written after this PF driver was written, which
3593                  * supports features unknown as of yet. Too bad since we don't
3594                  * support them. Or this may be because someone wrote a crappy
3595                  * VF driver and is sending garbage over the channel.
3596                  */
3597                 DP_NOTICE(p_hwfn, false,
3598                           "VF[%02x]: unknown TLV. type %04x length %04x"
3599                           " padding %08x reply address %lu\n",
3600                           p_vf->abs_vf_id,
3601                           mbx->first_tlv.tl.type,
3602                           mbx->first_tlv.tl.length,
3603                           mbx->first_tlv.padding,
3604                           (unsigned long)mbx->first_tlv.reply_address);
3605
3606                 /* Try replying in case reply address matches the acquisition's
3607                  * posted address.
3608                  */
3609                 if (p_vf->acquire.first_tlv.reply_address &&
3610                     (mbx->first_tlv.reply_address ==
3611                      p_vf->acquire.first_tlv.reply_address))
3612                         ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf,
3613                                                mbx->first_tlv.tl.type,
3614                                                sizeof(struct pfvf_def_resp_tlv),
3615                                                PFVF_STATUS_NOT_SUPPORTED);
3616                 else
3617                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3618                                    "VF[%02x]: Can't respond to TLV -"
3619                                    " no valid reply address\n",
3620                                    p_vf->abs_vf_id);
3621         }
3622
3623         ecore_iov_unlock_vf_pf_channel(p_hwfn, p_vf,
3624                                        mbx->first_tlv.tl.type);
3625
3626 #ifdef CONFIG_ECORE_SW_CHANNEL
3627         mbx->sw_mbx.mbx_state = VF_PF_RESPONSE_READY;
3628         mbx->sw_mbx.response_offset = 0;
3629 #endif
3630 }
3631
3632 void ecore_iov_pf_add_pending_events(struct ecore_hwfn *p_hwfn, u8 vfid)
3633 {
3634         u64 add_bit = 1ULL << (vfid % 64);
3635
3636         /* TODO - add locking mechanisms [no atomics in ecore, so we can't
3637         * add the lock inside the ecore_pf_iov struct].
3638         */
3639         p_hwfn->pf_iov_info->pending_events[vfid / 64] |= add_bit;
3640 }
3641
3642 void ecore_iov_pf_get_and_clear_pending_events(struct ecore_hwfn *p_hwfn,
3643                                                u64 *events)
3644 {
3645         u64 *p_pending_events = p_hwfn->pf_iov_info->pending_events;
3646
3647         /* TODO - Take a lock */
3648         OSAL_MEMCPY(events, p_pending_events,
3649                     sizeof(u64) * ECORE_VF_ARRAY_LENGTH);
3650         OSAL_MEMSET(p_pending_events, 0,
3651                     sizeof(u64) * ECORE_VF_ARRAY_LENGTH);
3652 }
3653
3654 static struct ecore_vf_info *
3655 ecore_sriov_get_vf_from_absid(struct ecore_hwfn *p_hwfn, u16 abs_vfid)
3656 {
3657         u8 min = (u8)p_hwfn->p_dev->p_iov_info->first_vf_in_pf;
3658
3659         if (!_ecore_iov_pf_sanity_check(p_hwfn, (int)abs_vfid - min, false)) {
3660                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3661                            "Got indication for VF [abs 0x%08x] that cannot be"
3662                            " handled by PF\n",
3663                            abs_vfid);
3664                 return OSAL_NULL;
3665         }
3666
3667         return &p_hwfn->pf_iov_info->vfs_array[(u8)abs_vfid - min];
3668 }
3669
3670 static enum _ecore_status_t ecore_sriov_vfpf_msg(struct ecore_hwfn *p_hwfn,
3671                                                  u16 abs_vfid,
3672                                                  struct regpair *vf_msg)
3673 {
3674         struct ecore_vf_info *p_vf = ecore_sriov_get_vf_from_absid(p_hwfn,
3675                                                                    abs_vfid);
3676
3677         if (!p_vf)
3678                 return ECORE_SUCCESS;
3679
3680         /* List the physical address of the request so that handler
3681          * could later on copy the message from it.
3682          */
3683         p_vf->vf_mbx.pending_req = (((u64)vf_msg->hi) << 32) | vf_msg->lo;
3684
3685         return OSAL_PF_VF_MSG(p_hwfn, p_vf->relative_vf_id);
3686 }
3687
3688 static void ecore_sriov_vfpf_malicious(struct ecore_hwfn *p_hwfn,
3689                                        struct malicious_vf_eqe_data *p_data)
3690 {
3691         struct ecore_vf_info *p_vf;
3692
3693         p_vf = ecore_sriov_get_vf_from_absid(p_hwfn, p_data->vfId);
3694
3695         if (!p_vf)
3696                 return;
3697
3698         DP_INFO(p_hwfn,
3699                 "VF [%d] - Malicious behavior [%02x]\n",
3700                 p_vf->abs_vf_id, p_data->errId);
3701
3702         p_vf->b_malicious = true;
3703
3704         OSAL_PF_VF_MALICIOUS(p_hwfn, p_vf->relative_vf_id);
3705 }
3706
3707 enum _ecore_status_t ecore_sriov_eqe_event(struct ecore_hwfn *p_hwfn,
3708                                            u8 opcode,
3709                                            __le16 echo,
3710                                            union event_ring_data *data)
3711 {
3712         switch (opcode) {
3713         case COMMON_EVENT_VF_PF_CHANNEL:
3714                 return ecore_sriov_vfpf_msg(p_hwfn, OSAL_LE16_TO_CPU(echo),
3715                                             &data->vf_pf_channel.msg_addr);
3716         case COMMON_EVENT_VF_FLR:
3717                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3718                            "VF-FLR is still not supported\n");
3719                 return ECORE_SUCCESS;
3720         case COMMON_EVENT_MALICIOUS_VF:
3721                 ecore_sriov_vfpf_malicious(p_hwfn, &data->malicious_vf);
3722                 return ECORE_SUCCESS;
3723         default:
3724                 DP_INFO(p_hwfn->p_dev, "Unknown sriov eqe event 0x%02x\n",
3725                         opcode);
3726                 return ECORE_INVAL;
3727         }
3728 }
3729
3730 bool ecore_iov_is_vf_pending_flr(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3731 {
3732         return !!(p_hwfn->pf_iov_info->pending_flr[rel_vf_id / 64] &
3733                    (1ULL << (rel_vf_id % 64)));
3734 }
3735
3736 u16 ecore_iov_get_next_active_vf(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3737 {
3738         struct ecore_hw_sriov_info *p_iov = p_hwfn->p_dev->p_iov_info;
3739         u16 i;
3740
3741         if (!p_iov)
3742                 goto out;
3743
3744         for (i = rel_vf_id; i < p_iov->total_vfs; i++)
3745                 if (ecore_iov_is_valid_vfid(p_hwfn, rel_vf_id, true, false))
3746                         return i;
3747
3748 out:
3749         return E4_MAX_NUM_VFS;
3750 }
3751
3752 enum _ecore_status_t ecore_iov_copy_vf_msg(struct ecore_hwfn *p_hwfn,
3753                                            struct ecore_ptt *ptt, int vfid)
3754 {
3755         struct ecore_dmae_params params;
3756         struct ecore_vf_info *vf_info;
3757
3758         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3759         if (!vf_info)
3760                 return ECORE_INVAL;
3761
3762         OSAL_MEMSET(&params, 0, sizeof(struct ecore_dmae_params));
3763         params.flags = ECORE_DMAE_FLAG_VF_SRC | ECORE_DMAE_FLAG_COMPLETION_DST;
3764         params.src_vfid = vf_info->abs_vf_id;
3765
3766         if (ecore_dmae_host2host(p_hwfn, ptt,
3767                                  vf_info->vf_mbx.pending_req,
3768                                  vf_info->vf_mbx.req_phys,
3769                                  sizeof(union vfpf_tlvs) / 4, &params)) {
3770                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3771                            "Failed to copy message from VF 0x%02x\n", vfid);
3772
3773                 return ECORE_IO;
3774         }
3775
3776         return ECORE_SUCCESS;
3777 }
3778
3779 void ecore_iov_bulletin_set_forced_mac(struct ecore_hwfn *p_hwfn,
3780                                        u8 *mac, int vfid)
3781 {
3782         struct ecore_vf_info *vf_info;
3783         u64 feature;
3784
3785         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3786         if (!vf_info) {
3787                 DP_NOTICE(p_hwfn->p_dev, true,
3788                           "Can not set forced MAC, invalid vfid [%d]\n", vfid);
3789                 return;
3790         }
3791         if (vf_info->b_malicious) {
3792                 DP_NOTICE(p_hwfn->p_dev, false,
3793                           "Can't set forced MAC to malicious VF [%d]\n",
3794                           vfid);
3795                 return;
3796         }
3797
3798         feature = 1 << MAC_ADDR_FORCED;
3799         OSAL_MEMCPY(vf_info->bulletin.p_virt->mac, mac, ETH_ALEN);
3800
3801         vf_info->bulletin.p_virt->valid_bitmap |= feature;
3802         /* Forced MAC will disable MAC_ADDR */
3803         vf_info->bulletin.p_virt->valid_bitmap &=
3804             ~(1 << VFPF_BULLETIN_MAC_ADDR);
3805
3806         ecore_iov_configure_vport_forced(p_hwfn, vf_info, feature);
3807 }
3808
3809 enum _ecore_status_t ecore_iov_bulletin_set_mac(struct ecore_hwfn *p_hwfn,
3810                                                 u8 *mac, int vfid)
3811 {
3812         struct ecore_vf_info *vf_info;
3813         u64 feature;
3814
3815         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3816         if (!vf_info) {
3817                 DP_NOTICE(p_hwfn->p_dev, true,
3818                           "Can not set MAC, invalid vfid [%d]\n", vfid);
3819                 return ECORE_INVAL;
3820         }
3821         if (vf_info->b_malicious) {
3822                 DP_NOTICE(p_hwfn->p_dev, false,
3823                           "Can't set MAC to malicious VF [%d]\n",
3824                           vfid);
3825                 return ECORE_INVAL;
3826         }
3827
3828         if (vf_info->bulletin.p_virt->valid_bitmap & (1 << MAC_ADDR_FORCED)) {
3829                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3830                            "Can not set MAC, Forced MAC is configured\n");
3831                 return ECORE_INVAL;
3832         }
3833
3834         feature = 1 << VFPF_BULLETIN_MAC_ADDR;
3835         OSAL_MEMCPY(vf_info->bulletin.p_virt->mac, mac, ETH_ALEN);
3836
3837         vf_info->bulletin.p_virt->valid_bitmap |= feature;
3838
3839         return ECORE_SUCCESS;
3840 }
3841
3842 enum _ecore_status_t
3843 ecore_iov_bulletin_set_forced_untagged_default(struct ecore_hwfn *p_hwfn,
3844                                                bool b_untagged_only, int vfid)
3845 {
3846         struct ecore_vf_info *vf_info;
3847         u64 feature;
3848
3849         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3850         if (!vf_info) {
3851                 DP_NOTICE(p_hwfn->p_dev, true,
3852                           "Can not set untagged default, invalid vfid [%d]\n",
3853                           vfid);
3854                 return ECORE_INVAL;
3855         }
3856         if (vf_info->b_malicious) {
3857                 DP_NOTICE(p_hwfn->p_dev, false,
3858                           "Can't set untagged default to malicious VF [%d]\n",
3859                           vfid);
3860                 return ECORE_INVAL;
3861         }
3862
3863         /* Since this is configurable only during vport-start, don't take it
3864          * if we're past that point.
3865          */
3866         if (vf_info->state == VF_ENABLED) {
3867                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3868                            "Can't support untagged change for vfid[%d] -"
3869                            " VF is already active\n",
3870                            vfid);
3871                 return ECORE_INVAL;
3872         }
3873
3874         /* Set configuration; This will later be taken into account during the
3875          * VF initialization.
3876          */
3877         feature = (1 << VFPF_BULLETIN_UNTAGGED_DEFAULT) |
3878             (1 << VFPF_BULLETIN_UNTAGGED_DEFAULT_FORCED);
3879         vf_info->bulletin.p_virt->valid_bitmap |= feature;
3880
3881         vf_info->bulletin.p_virt->default_only_untagged = b_untagged_only ? 1
3882             : 0;
3883
3884         return ECORE_SUCCESS;
3885 }
3886
3887 void ecore_iov_get_vfs_opaque_fid(struct ecore_hwfn *p_hwfn, int vfid,
3888                                   u16 *opaque_fid)
3889 {
3890         struct ecore_vf_info *vf_info;
3891
3892         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3893         if (!vf_info)
3894                 return;
3895
3896         *opaque_fid = vf_info->opaque_fid;
3897 }
3898
3899 void ecore_iov_get_vfs_vport_id(struct ecore_hwfn *p_hwfn, int vfid,
3900                                 u8 *p_vort_id)
3901 {
3902         struct ecore_vf_info *vf_info;
3903
3904         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3905         if (!vf_info)
3906                 return;
3907
3908         *p_vort_id = vf_info->vport_id;
3909 }
3910
3911 void ecore_iov_bulletin_set_forced_vlan(struct ecore_hwfn *p_hwfn,
3912                                         u16 pvid, int vfid)
3913 {
3914         struct ecore_vf_info *vf_info;
3915         u64 feature;
3916
3917         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3918         if (!vf_info) {
3919                 DP_NOTICE(p_hwfn->p_dev, true,
3920                           "Can not set forced MAC, invalid vfid [%d]\n",
3921                           vfid);
3922                 return;
3923         }
3924         if (vf_info->b_malicious) {
3925                 DP_NOTICE(p_hwfn->p_dev, false,
3926                           "Can't set forced vlan to malicious VF [%d]\n",
3927                           vfid);
3928                 return;
3929         }
3930
3931         feature = 1 << VLAN_ADDR_FORCED;
3932         vf_info->bulletin.p_virt->pvid = pvid;
3933         if (pvid)
3934                 vf_info->bulletin.p_virt->valid_bitmap |= feature;
3935         else
3936                 vf_info->bulletin.p_virt->valid_bitmap &= ~feature;
3937
3938         ecore_iov_configure_vport_forced(p_hwfn, vf_info, feature);
3939 }
3940
3941 bool ecore_iov_vf_has_vport_instance(struct ecore_hwfn *p_hwfn, int vfid)
3942 {
3943         struct ecore_vf_info *p_vf_info;
3944
3945         p_vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3946         if (!p_vf_info)
3947                 return false;
3948
3949         return !!p_vf_info->vport_instance;
3950 }
3951
3952 bool ecore_iov_is_vf_stopped(struct ecore_hwfn *p_hwfn, int vfid)
3953 {
3954         struct ecore_vf_info *p_vf_info;
3955
3956         p_vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3957         if (!p_vf_info)
3958                 return true;
3959
3960         return p_vf_info->state == VF_STOPPED;
3961 }
3962
3963 bool ecore_iov_spoofchk_get(struct ecore_hwfn *p_hwfn, int vfid)
3964 {
3965         struct ecore_vf_info *vf_info;
3966
3967         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3968         if (!vf_info)
3969                 return false;
3970
3971         return vf_info->spoof_chk;
3972 }
3973
3974 enum _ecore_status_t ecore_iov_spoofchk_set(struct ecore_hwfn *p_hwfn,
3975                                             int vfid, bool val)
3976 {
3977         struct ecore_vf_info *vf;
3978         enum _ecore_status_t rc = ECORE_INVAL;
3979
3980         if (!ecore_iov_pf_sanity_check(p_hwfn, vfid)) {
3981                 DP_NOTICE(p_hwfn, true,
3982                           "SR-IOV sanity check failed, can't set spoofchk\n");
3983                 goto out;
3984         }
3985
3986         vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3987         if (!vf)
3988                 goto out;
3989
3990         if (!ecore_iov_vf_has_vport_instance(p_hwfn, vfid)) {
3991                 /* After VF VPORT start PF will configure spoof check */
3992                 vf->req_spoofchk_val = val;
3993                 rc = ECORE_SUCCESS;
3994                 goto out;
3995         }
3996
3997         rc = __ecore_iov_spoofchk_set(p_hwfn, vf, val);
3998
3999 out:
4000         return rc;
4001 }
4002
4003 u8 ecore_iov_vf_chains_per_pf(struct ecore_hwfn *p_hwfn)
4004 {
4005         u8 max_chains_per_vf = p_hwfn->hw_info.max_chains_per_vf;
4006
4007         max_chains_per_vf = (max_chains_per_vf) ? max_chains_per_vf
4008             : ECORE_MAX_VF_CHAINS_PER_PF;
4009
4010         return max_chains_per_vf;
4011 }
4012
4013 void ecore_iov_get_vf_req_virt_mbx_params(struct ecore_hwfn *p_hwfn,
4014                                           u16 rel_vf_id,
4015                                           void **pp_req_virt_addr,
4016                                           u16 *p_req_virt_size)
4017 {
4018         struct ecore_vf_info *vf_info =
4019             ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4020
4021         if (!vf_info)
4022                 return;
4023
4024         if (pp_req_virt_addr)
4025                 *pp_req_virt_addr = vf_info->vf_mbx.req_virt;
4026
4027         if (p_req_virt_size)
4028                 *p_req_virt_size = sizeof(*vf_info->vf_mbx.req_virt);
4029 }
4030
4031 void ecore_iov_get_vf_reply_virt_mbx_params(struct ecore_hwfn *p_hwfn,
4032                                             u16 rel_vf_id,
4033                                             void **pp_reply_virt_addr,
4034                                             u16 *p_reply_virt_size)
4035 {
4036         struct ecore_vf_info *vf_info =
4037             ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4038
4039         if (!vf_info)
4040                 return;
4041
4042         if (pp_reply_virt_addr)
4043                 *pp_reply_virt_addr = vf_info->vf_mbx.reply_virt;
4044
4045         if (p_reply_virt_size)
4046                 *p_reply_virt_size = sizeof(*vf_info->vf_mbx.reply_virt);
4047 }
4048
4049 #ifdef CONFIG_ECORE_SW_CHANNEL
4050 struct ecore_iov_sw_mbx *ecore_iov_get_vf_sw_mbx(struct ecore_hwfn *p_hwfn,
4051                                                  u16 rel_vf_id)
4052 {
4053         struct ecore_vf_info *vf_info =
4054             ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4055
4056         if (!vf_info)
4057                 return OSAL_NULL;
4058
4059         return &vf_info->vf_mbx.sw_mbx;
4060 }
4061 #endif
4062
4063 bool ecore_iov_is_valid_vfpf_msg_length(u32 length)
4064 {
4065         return (length >= sizeof(struct vfpf_first_tlv) &&
4066                 (length <= sizeof(union vfpf_tlvs)));
4067 }
4068
4069 u32 ecore_iov_pfvf_msg_length(void)
4070 {
4071         return sizeof(union pfvf_tlvs);
4072 }
4073
4074 u8 *ecore_iov_bulletin_get_forced_mac(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4075 {
4076         struct ecore_vf_info *p_vf;
4077
4078         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4079         if (!p_vf || !p_vf->bulletin.p_virt)
4080                 return OSAL_NULL;
4081
4082         if (!(p_vf->bulletin.p_virt->valid_bitmap & (1 << MAC_ADDR_FORCED)))
4083                 return OSAL_NULL;
4084
4085         return p_vf->bulletin.p_virt->mac;
4086 }
4087
4088 u16 ecore_iov_bulletin_get_forced_vlan(struct ecore_hwfn *p_hwfn,
4089                                        u16 rel_vf_id)
4090 {
4091         struct ecore_vf_info *p_vf;
4092
4093         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4094         if (!p_vf || !p_vf->bulletin.p_virt)
4095                 return 0;
4096
4097         if (!(p_vf->bulletin.p_virt->valid_bitmap & (1 << VLAN_ADDR_FORCED)))
4098                 return 0;
4099
4100         return p_vf->bulletin.p_virt->pvid;
4101 }
4102
4103 enum _ecore_status_t ecore_iov_configure_tx_rate(struct ecore_hwfn *p_hwfn,
4104                                                  struct ecore_ptt *p_ptt,
4105                                                  int vfid, int val)
4106 {
4107         struct ecore_vf_info *vf;
4108         u8 abs_vp_id = 0;
4109         enum _ecore_status_t rc;
4110
4111         vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4112
4113         if (!vf)
4114                 return ECORE_INVAL;
4115
4116         rc = ecore_fw_vport(p_hwfn, vf->vport_id, &abs_vp_id);
4117         if (rc != ECORE_SUCCESS)
4118                 return rc;
4119
4120         return ecore_init_vport_rl(p_hwfn, p_ptt, abs_vp_id, (u32)val);
4121 }
4122
4123 enum _ecore_status_t ecore_iov_configure_min_tx_rate(struct ecore_dev *p_dev,
4124                                                      int vfid, u32 rate)
4125 {
4126         struct ecore_vf_info *vf;
4127         u8 vport_id;
4128         int i;
4129
4130         for_each_hwfn(p_dev, i) {
4131                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
4132
4133                 if (!ecore_iov_pf_sanity_check(p_hwfn, vfid)) {
4134                         DP_NOTICE(p_hwfn, true,
4135                                   "SR-IOV sanity check failed,"
4136                                   " can't set min rate\n");
4137                         return ECORE_INVAL;
4138                 }
4139         }
4140
4141         vf = ecore_iov_get_vf_info(ECORE_LEADING_HWFN(p_dev), (u16)vfid, true);
4142         vport_id = vf->vport_id;
4143
4144         return ecore_configure_vport_wfq(p_dev, vport_id, rate);
4145 }
4146
4147 enum _ecore_status_t ecore_iov_get_vf_stats(struct ecore_hwfn *p_hwfn,
4148                                             struct ecore_ptt *p_ptt,
4149                                             int vfid,
4150                                             struct ecore_eth_stats *p_stats)
4151 {
4152         struct ecore_vf_info *vf;
4153
4154         vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4155         if (!vf)
4156                 return ECORE_INVAL;
4157
4158         if (vf->state != VF_ENABLED)
4159                 return ECORE_INVAL;
4160
4161         __ecore_get_vport_stats(p_hwfn, p_ptt, p_stats,
4162                                 vf->abs_vf_id + 0x10, false);
4163
4164         return ECORE_SUCCESS;
4165 }
4166
4167 u8 ecore_iov_get_vf_num_rxqs(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4168 {
4169         struct ecore_vf_info *p_vf;
4170
4171         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4172         if (!p_vf)
4173                 return 0;
4174
4175         return p_vf->num_rxqs;
4176 }
4177
4178 u8 ecore_iov_get_vf_num_active_rxqs(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4179 {
4180         struct ecore_vf_info *p_vf;
4181
4182         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4183         if (!p_vf)
4184                 return 0;
4185
4186         return p_vf->num_active_rxqs;
4187 }
4188
4189 void *ecore_iov_get_vf_ctx(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4190 {
4191         struct ecore_vf_info *p_vf;
4192
4193         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4194         if (!p_vf)
4195                 return OSAL_NULL;
4196
4197         return p_vf->ctx;
4198 }
4199
4200 u8 ecore_iov_get_vf_num_sbs(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4201 {
4202         struct ecore_vf_info *p_vf;
4203
4204         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4205         if (!p_vf)
4206                 return 0;
4207
4208         return p_vf->num_sbs;
4209 }
4210
4211 bool ecore_iov_is_vf_wait_for_acquire(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4212 {
4213         struct ecore_vf_info *p_vf;
4214
4215         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4216         if (!p_vf)
4217                 return false;
4218
4219         return (p_vf->state == VF_FREE);
4220 }
4221
4222 bool ecore_iov_is_vf_acquired_not_initialized(struct ecore_hwfn *p_hwfn,
4223                                               u16 rel_vf_id)
4224 {
4225         struct ecore_vf_info *p_vf;
4226
4227         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4228         if (!p_vf)
4229                 return false;
4230
4231         return (p_vf->state == VF_ACQUIRED);
4232 }
4233
4234 bool ecore_iov_is_vf_initialized(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4235 {
4236         struct ecore_vf_info *p_vf;
4237
4238         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4239         if (!p_vf)
4240                 return false;
4241
4242         return (p_vf->state == VF_ENABLED);
4243 }
4244
4245 bool ecore_iov_is_vf_started(struct ecore_hwfn *p_hwfn,
4246                              u16 rel_vf_id)
4247 {
4248         struct ecore_vf_info *p_vf;
4249
4250         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4251         if (!p_vf)
4252                 return false;
4253
4254         return (p_vf->state != VF_FREE && p_vf->state != VF_STOPPED);
4255 }
4256
4257 enum _ecore_status_t
4258 ecore_iov_get_vf_min_rate(struct ecore_hwfn *p_hwfn, int vfid)
4259 {
4260         struct ecore_wfq_data *vf_vp_wfq;
4261         struct ecore_vf_info *vf_info;
4262
4263         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4264         if (!vf_info)
4265                 return 0;
4266
4267         vf_vp_wfq = &p_hwfn->qm_info.wfq_data[vf_info->vport_id];
4268
4269         if (vf_vp_wfq->configured)
4270                 return vf_vp_wfq->min_speed;
4271         else
4272                 return 0;
4273 }