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