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