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