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