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