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