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