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