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