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