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