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