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