73c4015a98e778ffc41616a0880e97e4b8cec51f
[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].rxq_active)
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].txq_active)
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 ecore_iov_init_hw_for_vf(struct ecore_hwfn *p_hwfn,
957                                               struct ecore_ptt *p_ptt,
958                                               u16 rel_vf_id, u16 num_rx_queues)
959 {
960         u8 num_of_vf_available_chains  = 0;
961         struct ecore_vf_info *vf = OSAL_NULL;
962         enum _ecore_status_t rc = ECORE_SUCCESS;
963         u32 cids;
964         u8 i;
965
966         vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, false);
967         if (!vf) {
968                 DP_ERR(p_hwfn, "ecore_iov_init_hw_for_vf : vf is OSAL_NULL\n");
969                 return ECORE_UNKNOWN_ERROR;
970         }
971
972         if (vf->b_init) {
973                 DP_NOTICE(p_hwfn, true, "VF[%d] is already active.\n",
974                           rel_vf_id);
975                 return ECORE_INVAL;
976         }
977
978         /* Limit number of queues according to number of CIDs */
979         ecore_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_ETH, &cids);
980         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
981                    "VF[%d] - requesting to initialize for 0x%04x queues"
982                    " [0x%04x CIDs available]\n",
983                    vf->relative_vf_id, num_rx_queues, (u16)cids);
984         num_rx_queues = OSAL_MIN_T(u16, num_rx_queues, ((u16)cids));
985
986         num_of_vf_available_chains = ecore_iov_alloc_vf_igu_sbs(p_hwfn,
987                                                                p_ptt,
988                                                                vf,
989                                                                num_rx_queues);
990         if (num_of_vf_available_chains == 0) {
991                 DP_ERR(p_hwfn, "no available igu sbs\n");
992                 return ECORE_NOMEM;
993         }
994
995         /* Choose queue number and index ranges */
996         vf->num_rxqs = num_of_vf_available_chains;
997         vf->num_txqs = num_of_vf_available_chains;
998
999         for (i = 0; i < vf->num_rxqs; i++) {
1000                 u16 queue_id = ecore_int_queue_id_from_sb_id(p_hwfn,
1001                                                              vf->igu_sbs[i]);
1002
1003                 if (queue_id > RESC_NUM(p_hwfn, ECORE_L2_QUEUE)) {
1004                         DP_NOTICE(p_hwfn, true,
1005                                   "VF[%d] will require utilizing of"
1006                                   " out-of-bounds queues - %04x\n",
1007                                   vf->relative_vf_id, queue_id);
1008                         /* TODO - cleanup the already allocate SBs */
1009                         return ECORE_INVAL;
1010                 }
1011
1012                 /* CIDs are per-VF, so no problem having them 0-based. */
1013                 vf->vf_queues[i].fw_rx_qid = queue_id;
1014                 vf->vf_queues[i].fw_tx_qid = queue_id;
1015                 vf->vf_queues[i].fw_cid = i;
1016
1017                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1018                            "VF[%d] - [%d] SB %04x, Tx/Rx queue %04x CID %04x\n",
1019                            vf->relative_vf_id, i, vf->igu_sbs[i], queue_id, i);
1020         }
1021
1022         rc = ecore_iov_enable_vf_access(p_hwfn, p_ptt, vf);
1023
1024         if (rc == ECORE_SUCCESS) {
1025                 vf->b_init = true;
1026                 p_hwfn->pf_iov_info->active_vfs[vf->relative_vf_id / 64] |=
1027                         (1ULL << (vf->relative_vf_id % 64));
1028
1029                 if (IS_LEAD_HWFN(p_hwfn))
1030                         p_hwfn->p_dev->p_iov_info->num_vfs++;
1031         }
1032
1033         return rc;
1034 }
1035
1036 void ecore_iov_set_link(struct ecore_hwfn *p_hwfn,
1037                         u16 vfid,
1038                         struct ecore_mcp_link_params *params,
1039                         struct ecore_mcp_link_state *link,
1040                         struct ecore_mcp_link_capabilities *p_caps)
1041 {
1042         struct ecore_vf_info *p_vf = ecore_iov_get_vf_info(p_hwfn, vfid, false);
1043         struct ecore_bulletin_content *p_bulletin;
1044
1045         if (!p_vf)
1046                 return;
1047
1048         p_bulletin = p_vf->bulletin.p_virt;
1049         p_bulletin->req_autoneg = params->speed.autoneg;
1050         p_bulletin->req_adv_speed = params->speed.advertised_speeds;
1051         p_bulletin->req_forced_speed = params->speed.forced_speed;
1052         p_bulletin->req_autoneg_pause = params->pause.autoneg;
1053         p_bulletin->req_forced_rx = params->pause.forced_rx;
1054         p_bulletin->req_forced_tx = params->pause.forced_tx;
1055         p_bulletin->req_loopback = params->loopback_mode;
1056
1057         p_bulletin->link_up = link->link_up;
1058         p_bulletin->speed = link->speed;
1059         p_bulletin->full_duplex = link->full_duplex;
1060         p_bulletin->autoneg = link->an;
1061         p_bulletin->autoneg_complete = link->an_complete;
1062         p_bulletin->parallel_detection = link->parallel_detection;
1063         p_bulletin->pfc_enabled = link->pfc_enabled;
1064         p_bulletin->partner_adv_speed = link->partner_adv_speed;
1065         p_bulletin->partner_tx_flow_ctrl_en = link->partner_tx_flow_ctrl_en;
1066         p_bulletin->partner_rx_flow_ctrl_en = link->partner_rx_flow_ctrl_en;
1067         p_bulletin->partner_adv_pause = link->partner_adv_pause;
1068         p_bulletin->sfp_tx_fault = link->sfp_tx_fault;
1069
1070         p_bulletin->capability_speed = p_caps->speed_capabilities;
1071 }
1072
1073 enum _ecore_status_t ecore_iov_release_hw_for_vf(struct ecore_hwfn *p_hwfn,
1074                                                  struct ecore_ptt *p_ptt,
1075                                                  u16 rel_vf_id)
1076 {
1077         struct ecore_mcp_link_capabilities caps;
1078         struct ecore_mcp_link_params params;
1079         struct ecore_mcp_link_state link;
1080         struct ecore_vf_info *vf = OSAL_NULL;
1081
1082         vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
1083         if (!vf) {
1084                 DP_ERR(p_hwfn, "ecore_iov_release_hw_for_vf : vf is NULL\n");
1085                 return ECORE_UNKNOWN_ERROR;
1086         }
1087
1088         if (vf->bulletin.p_virt)
1089                 OSAL_MEMSET(vf->bulletin.p_virt, 0,
1090                             sizeof(*vf->bulletin.p_virt));
1091
1092         OSAL_MEMSET(&vf->p_vf_info, 0, sizeof(vf->p_vf_info));
1093
1094         /* Get the link configuration back in bulletin so
1095          * that when VFs are re-enabled they get the actual
1096          * link configuration.
1097          */
1098         OSAL_MEMCPY(&params, ecore_mcp_get_link_params(p_hwfn), sizeof(params));
1099         OSAL_MEMCPY(&link, ecore_mcp_get_link_state(p_hwfn), sizeof(link));
1100         OSAL_MEMCPY(&caps, ecore_mcp_get_link_capabilities(p_hwfn),
1101                     sizeof(caps));
1102         ecore_iov_set_link(p_hwfn, rel_vf_id, &params, &link, &caps);
1103
1104         /* Forget the VF's acquisition message */
1105         OSAL_MEMSET(&vf->acquire, 0, sizeof(vf->acquire));
1106
1107         /* disablng interrupts and resetting permission table was done during
1108          * vf-close, however, we could get here without going through vf_close
1109          */
1110         /* Disable Interrupts for VF */
1111         ecore_iov_vf_igu_set_int(p_hwfn, p_ptt, vf, 0);
1112
1113         /* Reset Permission table */
1114         ecore_iov_config_perm_table(p_hwfn, p_ptt, vf, 0);
1115
1116         vf->num_rxqs = 0;
1117         vf->num_txqs = 0;
1118         ecore_iov_free_vf_igu_sbs(p_hwfn, p_ptt, vf);
1119
1120         if (vf->b_init) {
1121                 vf->b_init = false;
1122                 p_hwfn->pf_iov_info->active_vfs[vf->relative_vf_id / 64] &=
1123                                         ~(1ULL << (vf->relative_vf_id / 64));
1124
1125                 if (IS_LEAD_HWFN(p_hwfn))
1126                         p_hwfn->p_dev->p_iov_info->num_vfs--;
1127         }
1128
1129         return ECORE_SUCCESS;
1130 }
1131
1132 static bool ecore_iov_tlv_supported(u16 tlvtype)
1133 {
1134         return tlvtype > CHANNEL_TLV_NONE && tlvtype < CHANNEL_TLV_MAX;
1135 }
1136
1137 static void ecore_iov_lock_vf_pf_channel(struct ecore_hwfn *p_hwfn,
1138                                          struct ecore_vf_info *vf, u16 tlv)
1139 {
1140         /* lock the channel */
1141         /* mutex_lock(&vf->op_mutex); @@@TBD MichalK - add lock... */
1142
1143         /* record the locking op */
1144         /* vf->op_current = tlv; @@@TBD MichalK */
1145
1146         /* log the lock */
1147         if (ecore_iov_tlv_supported(tlv))
1148                 DP_VERBOSE(p_hwfn,
1149                            ECORE_MSG_IOV,
1150                            "VF[%d]: vf pf channel locked by %s\n",
1151                            vf->abs_vf_id,
1152                            ecore_channel_tlvs_string[tlv]);
1153         else
1154                 DP_VERBOSE(p_hwfn,
1155                            ECORE_MSG_IOV,
1156                            "VF[%d]: vf pf channel locked by %04x\n",
1157                            vf->abs_vf_id, tlv);
1158 }
1159
1160 static void ecore_iov_unlock_vf_pf_channel(struct ecore_hwfn *p_hwfn,
1161                                            struct ecore_vf_info *vf,
1162                                            u16 expected_tlv)
1163 {
1164         /* log the unlock */
1165         if (ecore_iov_tlv_supported(expected_tlv))
1166                 DP_VERBOSE(p_hwfn,
1167                            ECORE_MSG_IOV,
1168                            "VF[%d]: vf pf channel unlocked by %s\n",
1169                            vf->abs_vf_id,
1170                            ecore_channel_tlvs_string[expected_tlv]);
1171         else
1172                 DP_VERBOSE(p_hwfn,
1173                            ECORE_MSG_IOV,
1174                            "VF[%d]: vf pf channel unlocked by %04x\n",
1175                            vf->abs_vf_id, expected_tlv);
1176
1177         /* record the locking op */
1178         /* vf->op_current = CHANNEL_TLV_NONE; */
1179 }
1180
1181 /* place a given tlv on the tlv buffer, continuing current tlv list */
1182 void *ecore_add_tlv(struct ecore_hwfn *p_hwfn,
1183                     u8 **offset, u16 type, u16 length)
1184 {
1185         struct channel_tlv *tl = (struct channel_tlv *)*offset;
1186
1187         tl->type = type;
1188         tl->length = length;
1189
1190         /* Offset should keep pointing to next TLV (the end of the last) */
1191         *offset += length;
1192
1193         /* Return a pointer to the start of the added tlv */
1194         return *offset - length;
1195 }
1196
1197 /* list the types and lengths of the tlvs on the buffer */
1198 void ecore_dp_tlv_list(struct ecore_hwfn *p_hwfn, void *tlvs_list)
1199 {
1200         u16 i = 1, total_length = 0;
1201         struct channel_tlv *tlv;
1202
1203         do {
1204                 /* cast current tlv list entry to channel tlv header */
1205                 tlv = (struct channel_tlv *)((u8 *)tlvs_list + total_length);
1206
1207                 /* output tlv */
1208                 if (ecore_iov_tlv_supported(tlv->type))
1209                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1210                                    "TLV number %d: type %s, length %d\n",
1211                                    i, ecore_channel_tlvs_string[tlv->type],
1212                                    tlv->length);
1213                 else
1214                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1215                                    "TLV number %d: type %d, length %d\n",
1216                                    i, tlv->type, tlv->length);
1217
1218                 if (tlv->type == CHANNEL_TLV_LIST_END)
1219                         return;
1220
1221                 /* Validate entry - protect against malicious VFs */
1222                 if (!tlv->length) {
1223                         DP_NOTICE(p_hwfn, false, "TLV of length 0 found\n");
1224                         return;
1225                 }
1226                 total_length += tlv->length;
1227                 if (total_length >= sizeof(struct tlv_buffer_size)) {
1228                         DP_NOTICE(p_hwfn, false, "TLV ==> Buffer overflow\n");
1229                         return;
1230                 }
1231
1232                 i++;
1233         } while (1);
1234 }
1235
1236 static void ecore_iov_send_response(struct ecore_hwfn *p_hwfn,
1237                                     struct ecore_ptt *p_ptt,
1238                                     struct ecore_vf_info *p_vf,
1239                                     u16 length, u8 status)
1240 {
1241         struct ecore_iov_vf_mbx *mbx = &p_vf->vf_mbx;
1242         struct ecore_dmae_params params;
1243         u8 eng_vf_id;
1244
1245         mbx->reply_virt->default_resp.hdr.status = status;
1246
1247         ecore_dp_tlv_list(p_hwfn, mbx->reply_virt);
1248
1249 #ifdef CONFIG_ECORE_SW_CHANNEL
1250         mbx->sw_mbx.response_size =
1251             length + sizeof(struct channel_list_end_tlv);
1252
1253         if (!p_hwfn->p_dev->b_hw_channel)
1254                 return;
1255 #endif
1256
1257         eng_vf_id = p_vf->abs_vf_id;
1258
1259         OSAL_MEMSET(&params, 0, sizeof(struct ecore_dmae_params));
1260         params.flags = ECORE_DMAE_FLAG_VF_DST;
1261         params.dst_vfid = eng_vf_id;
1262
1263         ecore_dmae_host2host(p_hwfn, p_ptt, mbx->reply_phys + sizeof(u64),
1264                              mbx->req_virt->first_tlv.reply_address +
1265                              sizeof(u64),
1266                              (sizeof(union pfvf_tlvs) - sizeof(u64)) / 4,
1267                              &params);
1268
1269         ecore_dmae_host2host(p_hwfn, p_ptt, mbx->reply_phys,
1270                              mbx->req_virt->first_tlv.reply_address,
1271                              sizeof(u64) / 4, &params);
1272
1273         REG_WR(p_hwfn,
1274                GTT_BAR0_MAP_REG_USDM_RAM +
1275                USTORM_VF_PF_CHANNEL_READY_OFFSET(eng_vf_id), 1);
1276 }
1277
1278 static u16 ecore_iov_vport_to_tlv(struct ecore_hwfn *p_hwfn,
1279                                   enum ecore_iov_vport_update_flag flag)
1280 {
1281         switch (flag) {
1282         case ECORE_IOV_VP_UPDATE_ACTIVATE:
1283                 return CHANNEL_TLV_VPORT_UPDATE_ACTIVATE;
1284         case ECORE_IOV_VP_UPDATE_VLAN_STRIP:
1285                 return CHANNEL_TLV_VPORT_UPDATE_VLAN_STRIP;
1286         case ECORE_IOV_VP_UPDATE_TX_SWITCH:
1287                 return CHANNEL_TLV_VPORT_UPDATE_TX_SWITCH;
1288         case ECORE_IOV_VP_UPDATE_MCAST:
1289                 return CHANNEL_TLV_VPORT_UPDATE_MCAST;
1290         case ECORE_IOV_VP_UPDATE_ACCEPT_PARAM:
1291                 return CHANNEL_TLV_VPORT_UPDATE_ACCEPT_PARAM;
1292         case ECORE_IOV_VP_UPDATE_RSS:
1293                 return CHANNEL_TLV_VPORT_UPDATE_RSS;
1294         case ECORE_IOV_VP_UPDATE_ACCEPT_ANY_VLAN:
1295                 return CHANNEL_TLV_VPORT_UPDATE_ACCEPT_ANY_VLAN;
1296         case ECORE_IOV_VP_UPDATE_SGE_TPA:
1297                 return CHANNEL_TLV_VPORT_UPDATE_SGE_TPA;
1298         default:
1299                 return 0;
1300         }
1301 }
1302
1303 static u16 ecore_iov_prep_vp_update_resp_tlvs(struct ecore_hwfn *p_hwfn,
1304                                               struct ecore_vf_info *p_vf,
1305                                               struct ecore_iov_vf_mbx *p_mbx,
1306                                               u8 status, u16 tlvs_mask,
1307                                               u16 tlvs_accepted)
1308 {
1309         struct pfvf_def_resp_tlv *resp;
1310         u16 size, total_len, i;
1311
1312         OSAL_MEMSET(p_mbx->reply_virt, 0, sizeof(union pfvf_tlvs));
1313         p_mbx->offset = (u8 *)p_mbx->reply_virt;
1314         size = sizeof(struct pfvf_def_resp_tlv);
1315         total_len = size;
1316
1317         ecore_add_tlv(p_hwfn, &p_mbx->offset, CHANNEL_TLV_VPORT_UPDATE, size);
1318
1319         /* Prepare response for all extended tlvs if they are found by PF */
1320         for (i = 0; i < ECORE_IOV_VP_UPDATE_MAX; i++) {
1321                 if (!(tlvs_mask & (1 << i)))
1322                         continue;
1323
1324                 resp = ecore_add_tlv(p_hwfn, &p_mbx->offset,
1325                                      ecore_iov_vport_to_tlv(p_hwfn, i), size);
1326
1327                 if (tlvs_accepted & (1 << i))
1328                         resp->hdr.status = status;
1329                 else
1330                         resp->hdr.status = PFVF_STATUS_NOT_SUPPORTED;
1331
1332                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1333                            "VF[%d] - vport_update resp: TLV %d, status %02x\n",
1334                            p_vf->relative_vf_id,
1335                            ecore_iov_vport_to_tlv(p_hwfn, i), resp->hdr.status);
1336
1337                 total_len += size;
1338         }
1339
1340         ecore_add_tlv(p_hwfn, &p_mbx->offset, CHANNEL_TLV_LIST_END,
1341                       sizeof(struct channel_list_end_tlv));
1342
1343         return total_len;
1344 }
1345
1346 static void ecore_iov_prepare_resp(struct ecore_hwfn *p_hwfn,
1347                                    struct ecore_ptt *p_ptt,
1348                                    struct ecore_vf_info *vf_info,
1349                                    u16 type, u16 length, u8 status)
1350 {
1351         struct ecore_iov_vf_mbx *mbx = &vf_info->vf_mbx;
1352
1353         mbx->offset = (u8 *)mbx->reply_virt;
1354
1355         ecore_add_tlv(p_hwfn, &mbx->offset, type, length);
1356         ecore_add_tlv(p_hwfn, &mbx->offset, CHANNEL_TLV_LIST_END,
1357                       sizeof(struct channel_list_end_tlv));
1358
1359         ecore_iov_send_response(p_hwfn, p_ptt, vf_info, length, status);
1360
1361         OSAL_IOV_PF_RESP_TYPE(p_hwfn, vf_info->relative_vf_id, status);
1362 }
1363
1364 struct ecore_public_vf_info
1365 *ecore_iov_get_public_vf_info(struct ecore_hwfn *p_hwfn,
1366                               u16 relative_vf_id,
1367                               bool b_enabled_only)
1368 {
1369         struct ecore_vf_info *vf = OSAL_NULL;
1370
1371         vf = ecore_iov_get_vf_info(p_hwfn, relative_vf_id, b_enabled_only);
1372         if (!vf)
1373                 return OSAL_NULL;
1374
1375         return &vf->p_vf_info;
1376 }
1377
1378 static void ecore_iov_vf_cleanup(struct ecore_hwfn *p_hwfn,
1379                                  struct ecore_vf_info *p_vf)
1380 {
1381         u32 i;
1382         p_vf->vf_bulletin = 0;
1383         p_vf->vport_instance = 0;
1384         p_vf->configured_features = 0;
1385
1386         /* If VF previously requested less resources, go back to default */
1387         p_vf->num_rxqs = p_vf->num_sbs;
1388         p_vf->num_txqs = p_vf->num_sbs;
1389
1390         p_vf->num_active_rxqs = 0;
1391
1392         for (i = 0; i < ECORE_MAX_VF_CHAINS_PER_PF; i++) {
1393                 p_vf->vf_queues[i].rxq_active = 0;
1394                 p_vf->vf_queues[i].txq_active = 0;
1395         }
1396
1397         OSAL_MEMSET(&p_vf->shadow_config, 0, sizeof(p_vf->shadow_config));
1398         OSAL_MEMSET(&p_vf->acquire, 0, sizeof(p_vf->acquire));
1399         OSAL_IOV_VF_CLEANUP(p_hwfn, p_vf->relative_vf_id);
1400 }
1401
1402 static u8 ecore_iov_vf_mbx_acquire_resc(struct ecore_hwfn *p_hwfn,
1403                                         struct ecore_ptt *p_ptt,
1404                                         struct ecore_vf_info *p_vf,
1405                                         struct vf_pf_resc_request *p_req,
1406                                         struct pf_vf_resc *p_resp)
1407 {
1408         int i;
1409
1410         /* Queue related information */
1411         p_resp->num_rxqs = p_vf->num_rxqs;
1412         p_resp->num_txqs = p_vf->num_txqs;
1413         p_resp->num_sbs = p_vf->num_sbs;
1414
1415         for (i = 0; i < p_resp->num_sbs; i++) {
1416                 p_resp->hw_sbs[i].hw_sb_id = p_vf->igu_sbs[i];
1417                 /* TODO - what's this sb_qid field? Is it deprecated?
1418                  * or is there an ecore_client that looks at this?
1419                  */
1420                 p_resp->hw_sbs[i].sb_qid = 0;
1421         }
1422
1423         /* These fields are filled for backward compatibility.
1424          * Unused by modern vfs.
1425          */
1426         for (i = 0; i < p_resp->num_rxqs; i++) {
1427                 ecore_fw_l2_queue(p_hwfn, p_vf->vf_queues[i].fw_rx_qid,
1428                                   (u16 *)&p_resp->hw_qid[i]);
1429                 p_resp->cid[i] = p_vf->vf_queues[i].fw_cid;
1430         }
1431
1432         /* Filter related information */
1433         p_resp->num_mac_filters = OSAL_MIN_T(u8, p_vf->num_mac_filters,
1434                                              p_req->num_mac_filters);
1435         p_resp->num_vlan_filters = OSAL_MIN_T(u8, p_vf->num_vlan_filters,
1436                                               p_req->num_vlan_filters);
1437
1438         /* This isn't really needed/enforced, but some legacy VFs might depend
1439          * on the correct filling of this field.
1440          */
1441         p_resp->num_mc_filters = ECORE_MAX_MC_ADDRS;
1442
1443         /* Validate sufficient resources for VF */
1444         if (p_resp->num_rxqs < p_req->num_rxqs ||
1445             p_resp->num_txqs < p_req->num_txqs ||
1446             p_resp->num_sbs < p_req->num_sbs ||
1447             p_resp->num_mac_filters < p_req->num_mac_filters ||
1448             p_resp->num_vlan_filters < p_req->num_vlan_filters ||
1449             p_resp->num_mc_filters < p_req->num_mc_filters) {
1450                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1451                            "VF[%d] - Insufficient resources: rxq [%02x/%02x]"
1452                            " txq [%02x/%02x] sbs [%02x/%02x] mac [%02x/%02x]"
1453                            " vlan [%02x/%02x] mc [%02x/%02x]\n",
1454                            p_vf->abs_vf_id,
1455                            p_req->num_rxqs, p_resp->num_rxqs,
1456                            p_req->num_rxqs, p_resp->num_txqs,
1457                            p_req->num_sbs, p_resp->num_sbs,
1458                            p_req->num_mac_filters, p_resp->num_mac_filters,
1459                            p_req->num_vlan_filters, p_resp->num_vlan_filters,
1460                            p_req->num_mc_filters, p_resp->num_mc_filters);
1461
1462                 /* Some legacy OSes are incapable of correctly handling this
1463                  * failure.
1464                  */
1465                 if ((p_vf->acquire.vfdev_info.eth_fp_hsi_minor ==
1466                      ETH_HSI_VER_NO_PKT_LEN_TUNN) &&
1467                     (p_vf->acquire.vfdev_info.os_type ==
1468                      VFPF_ACQUIRE_OS_WINDOWS))
1469                         return PFVF_STATUS_SUCCESS;
1470
1471                 return PFVF_STATUS_NO_RESOURCE;
1472         }
1473
1474         return PFVF_STATUS_SUCCESS;
1475 }
1476
1477 static void ecore_iov_vf_mbx_acquire_stats(struct ecore_hwfn *p_hwfn,
1478                                            struct pfvf_stats_info *p_stats)
1479 {
1480         p_stats->mstats.address = PXP_VF_BAR0_START_MSDM_ZONE_B +
1481                                   OFFSETOF(struct mstorm_vf_zone,
1482                                            non_trigger.eth_queue_stat);
1483         p_stats->mstats.len = sizeof(struct eth_mstorm_per_queue_stat);
1484         p_stats->ustats.address = PXP_VF_BAR0_START_USDM_ZONE_B +
1485                                   OFFSETOF(struct ustorm_vf_zone,
1486                                            non_trigger.eth_queue_stat);
1487         p_stats->ustats.len = sizeof(struct eth_ustorm_per_queue_stat);
1488         p_stats->pstats.address = PXP_VF_BAR0_START_PSDM_ZONE_B +
1489                                   OFFSETOF(struct pstorm_vf_zone,
1490                                            non_trigger.eth_queue_stat);
1491         p_stats->pstats.len = sizeof(struct eth_pstorm_per_queue_stat);
1492         p_stats->tstats.address = 0;
1493         p_stats->tstats.len = 0;
1494 }
1495
1496 static void ecore_iov_vf_mbx_acquire(struct ecore_hwfn       *p_hwfn,
1497                                      struct ecore_ptt        *p_ptt,
1498                                      struct ecore_vf_info    *vf)
1499 {
1500         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
1501         struct pfvf_acquire_resp_tlv *resp = &mbx->reply_virt->acquire_resp;
1502         struct pf_vf_pfdev_info *pfdev_info = &resp->pfdev_info;
1503         struct vfpf_acquire_tlv *req = &mbx->req_virt->acquire;
1504         u8 vfpf_status = PFVF_STATUS_NOT_SUPPORTED;
1505         struct pf_vf_resc *resc = &resp->resc;
1506         enum _ecore_status_t rc;
1507
1508         OSAL_MEMSET(resp, 0, sizeof(*resp));
1509
1510         /* Write the PF version so that VF would know which version
1511          * is supported - might be later overridden. This guarantees that
1512          * VF could recognize legacy PF based on lack of versions in reply.
1513          */
1514         pfdev_info->major_fp_hsi = ETH_HSI_VER_MAJOR;
1515         pfdev_info->minor_fp_hsi = ETH_HSI_VER_MINOR;
1516
1517         /* TODO - not doing anything is bad since we'll assert, but this isn't
1518          * necessarily the right behavior - perhaps we should have allowed some
1519          * versatility here.
1520          */
1521         if (vf->state != VF_FREE &&
1522             vf->state != VF_STOPPED) {
1523                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1524                            "VF[%d] sent ACQUIRE but is already in state %d - fail request\n",
1525                            vf->abs_vf_id, vf->state);
1526                 goto out;
1527         }
1528
1529         /* Validate FW compatibility */
1530         if (req->vfdev_info.eth_fp_hsi_major != ETH_HSI_VER_MAJOR) {
1531                 if (req->vfdev_info.capabilities &
1532                     VFPF_ACQUIRE_CAP_PRE_FP_HSI) {
1533                         struct vf_pf_vfdev_info *p_vfdev = &req->vfdev_info;
1534
1535                         /* This legacy support would need to be removed once
1536                          * the major has changed.
1537                          */
1538                         OSAL_BUILD_BUG_ON(ETH_HSI_VER_MAJOR != 3);
1539
1540                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1541                                    "VF[%d] is pre-fastpath HSI\n",
1542                                    vf->abs_vf_id);
1543                         p_vfdev->eth_fp_hsi_major = ETH_HSI_VER_MAJOR;
1544                         p_vfdev->eth_fp_hsi_minor = ETH_HSI_VER_NO_PKT_LEN_TUNN;
1545                 } else {
1546                         DP_INFO(p_hwfn,
1547                                 "VF[%d] needs fastpath HSI %02x.%02x, which is"
1548                                 " incompatible with loaded FW's faspath"
1549                                 " HSI %02x.%02x\n",
1550                                 vf->abs_vf_id,
1551                                 req->vfdev_info.eth_fp_hsi_major,
1552                                 req->vfdev_info.eth_fp_hsi_minor,
1553                                 ETH_HSI_VER_MAJOR, ETH_HSI_VER_MINOR);
1554
1555                         goto out;
1556                 }
1557         }
1558
1559         /* On 100g PFs, prevent old VFs from loading */
1560         if ((p_hwfn->p_dev->num_hwfns > 1) &&
1561             !(req->vfdev_info.capabilities & VFPF_ACQUIRE_CAP_100G)) {
1562                 DP_INFO(p_hwfn,
1563                         "VF[%d] is running an old driver that doesn't support"
1564                         " 100g\n",
1565                         vf->abs_vf_id);
1566                 goto out;
1567         }
1568
1569 #ifndef __EXTRACT__LINUX__
1570         if (OSAL_IOV_VF_ACQUIRE(p_hwfn, vf->relative_vf_id) != ECORE_SUCCESS) {
1571                 vfpf_status = PFVF_STATUS_NOT_SUPPORTED;
1572                 goto out;
1573         }
1574 #endif
1575
1576         /* Store the acquire message */
1577         OSAL_MEMCPY(&vf->acquire, req, sizeof(vf->acquire));
1578
1579         vf->opaque_fid = req->vfdev_info.opaque_fid;
1580
1581         vf->vf_bulletin = req->bulletin_addr;
1582         vf->bulletin.size = (vf->bulletin.size < req->bulletin_size) ?
1583             vf->bulletin.size : req->bulletin_size;
1584
1585         /* fill in pfdev info */
1586         pfdev_info->chip_num = p_hwfn->p_dev->chip_num;
1587         pfdev_info->db_size = 0;        /* @@@ TBD MichalK Vf Doorbells */
1588         pfdev_info->indices_per_sb = PIS_PER_SB;
1589
1590         pfdev_info->capabilities = PFVF_ACQUIRE_CAP_DEFAULT_UNTAGGED |
1591                                    PFVF_ACQUIRE_CAP_POST_FW_OVERRIDE;
1592         if (p_hwfn->p_dev->num_hwfns > 1)
1593                 pfdev_info->capabilities |= PFVF_ACQUIRE_CAP_100G;
1594
1595         ecore_iov_vf_mbx_acquire_stats(p_hwfn, &pfdev_info->stats_info);
1596
1597         OSAL_MEMCPY(pfdev_info->port_mac, p_hwfn->hw_info.hw_mac_addr,
1598                     ETH_ALEN);
1599
1600         pfdev_info->fw_major = FW_MAJOR_VERSION;
1601         pfdev_info->fw_minor = FW_MINOR_VERSION;
1602         pfdev_info->fw_rev = FW_REVISION_VERSION;
1603         pfdev_info->fw_eng = FW_ENGINEERING_VERSION;
1604
1605         /* Incorrect when legacy, but doesn't matter as legacy isn't reading
1606          * this field.
1607          */
1608         pfdev_info->minor_fp_hsi = OSAL_MIN_T(u8, ETH_HSI_VER_MINOR,
1609                                               req->vfdev_info.eth_fp_hsi_minor);
1610         pfdev_info->os_type = OSAL_IOV_GET_OS_TYPE();
1611         ecore_mcp_get_mfw_ver(p_hwfn, p_ptt, &pfdev_info->mfw_ver,
1612                               OSAL_NULL);
1613
1614         pfdev_info->dev_type = p_hwfn->p_dev->type;
1615         pfdev_info->chip_rev = p_hwfn->p_dev->chip_rev;
1616
1617         /* Fill resources available to VF; Make sure there are enough to
1618          * satisfy the VF's request.
1619          */
1620         vfpf_status = ecore_iov_vf_mbx_acquire_resc(p_hwfn, p_ptt, vf,
1621                                                     &req->resc_request, resc);
1622         if (vfpf_status != PFVF_STATUS_SUCCESS)
1623                 goto out;
1624
1625         /* Start the VF in FW */
1626         rc = ecore_sp_vf_start(p_hwfn, vf);
1627         if (rc != ECORE_SUCCESS) {
1628                 DP_NOTICE(p_hwfn, true, "Failed to start VF[%02x]\n",
1629                           vf->abs_vf_id);
1630                 vfpf_status = PFVF_STATUS_FAILURE;
1631                 goto out;
1632         }
1633
1634         /* Fill agreed size of bulletin board in response, and post
1635          * an initial image to the bulletin board.
1636          */
1637         resp->bulletin_size = vf->bulletin.size;
1638         ecore_iov_post_vf_bulletin(p_hwfn, vf->relative_vf_id, p_ptt);
1639
1640         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1641                    "VF[%d] ACQUIRE_RESPONSE: pfdev_info- chip_num=0x%x,"
1642                    " db_size=%d, idx_per_sb=%d, pf_cap=0x%lx\n"
1643                    "resources- n_rxq-%d, n_txq-%d, n_sbs-%d, n_macs-%d,"
1644                    " n_vlans-%d\n",
1645                    vf->abs_vf_id, resp->pfdev_info.chip_num,
1646                    resp->pfdev_info.db_size, resp->pfdev_info.indices_per_sb,
1647                    (unsigned long)resp->pfdev_info.capabilities, resc->num_rxqs,
1648                    resc->num_txqs, resc->num_sbs, resc->num_mac_filters,
1649                    resc->num_vlan_filters);
1650
1651         vf->state = VF_ACQUIRED;
1652
1653 out:
1654         /* Prepare Response */
1655         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_ACQUIRE,
1656                                sizeof(struct pfvf_acquire_resp_tlv),
1657                                vfpf_status);
1658 }
1659
1660 static enum _ecore_status_t
1661 __ecore_iov_spoofchk_set(struct ecore_hwfn *p_hwfn,
1662                          struct ecore_vf_info *p_vf, bool val)
1663 {
1664         struct ecore_sp_vport_update_params params;
1665         enum _ecore_status_t rc;
1666
1667         if (val == p_vf->spoof_chk) {
1668                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1669                            "Spoofchk value[%d] is already configured\n", val);
1670                 return ECORE_SUCCESS;
1671         }
1672
1673         OSAL_MEMSET(&params, 0, sizeof(struct ecore_sp_vport_update_params));
1674         params.opaque_fid = p_vf->opaque_fid;
1675         params.vport_id = p_vf->vport_id;
1676         params.update_anti_spoofing_en_flg = 1;
1677         params.anti_spoofing_en = val;
1678
1679         rc = ecore_sp_vport_update(p_hwfn, &params, ECORE_SPQ_MODE_EBLOCK,
1680                                    OSAL_NULL);
1681         if (rc == ECORE_SUCCESS) {
1682                 p_vf->spoof_chk = val;
1683                 p_vf->req_spoofchk_val = p_vf->spoof_chk;
1684                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1685                            "Spoofchk val[%d] configured\n", val);
1686         } else {
1687                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1688                            "Spoofchk configuration[val:%d] failed for VF[%d]\n",
1689                            val, p_vf->relative_vf_id);
1690         }
1691
1692         return rc;
1693 }
1694
1695 static enum _ecore_status_t
1696 ecore_iov_reconfigure_unicast_vlan(struct ecore_hwfn *p_hwfn,
1697                                    struct ecore_vf_info *p_vf)
1698 {
1699         struct ecore_filter_ucast filter;
1700         enum _ecore_status_t rc = ECORE_SUCCESS;
1701         int i;
1702
1703         OSAL_MEMSET(&filter, 0, sizeof(filter));
1704         filter.is_rx_filter = 1;
1705         filter.is_tx_filter = 1;
1706         filter.vport_to_add_to = p_vf->vport_id;
1707         filter.opcode = ECORE_FILTER_ADD;
1708
1709         /* Reconfigure vlans */
1710         for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++) {
1711                 if (!p_vf->shadow_config.vlans[i].used)
1712                         continue;
1713
1714                 filter.type = ECORE_FILTER_VLAN;
1715                 filter.vlan = p_vf->shadow_config.vlans[i].vid;
1716                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1717                            "Reconfiguring VLAN [0x%04x] for VF [%04x]\n",
1718                            filter.vlan, p_vf->relative_vf_id);
1719                 rc = ecore_sp_eth_filter_ucast(p_hwfn, p_vf->opaque_fid,
1720                                                &filter, ECORE_SPQ_MODE_CB,
1721                                                OSAL_NULL);
1722                 if (rc) {
1723                         DP_NOTICE(p_hwfn, true,
1724                                   "Failed to configure VLAN [%04x]"
1725                                   " to VF [%04x]\n",
1726                                   filter.vlan, p_vf->relative_vf_id);
1727                         break;
1728                 }
1729         }
1730
1731         return rc;
1732 }
1733
1734 static enum _ecore_status_t
1735 ecore_iov_reconfigure_unicast_shadow(struct ecore_hwfn *p_hwfn,
1736                                      struct ecore_vf_info *p_vf, u64 events)
1737 {
1738         enum _ecore_status_t rc = ECORE_SUCCESS;
1739
1740         /*TODO - what about MACs? */
1741
1742         if ((events & (1 << VLAN_ADDR_FORCED)) &&
1743             !(p_vf->configured_features & (1 << VLAN_ADDR_FORCED)))
1744                 rc = ecore_iov_reconfigure_unicast_vlan(p_hwfn, p_vf);
1745
1746         return rc;
1747 }
1748
1749 static  enum _ecore_status_t
1750 ecore_iov_configure_vport_forced(struct ecore_hwfn *p_hwfn,
1751                                  struct ecore_vf_info *p_vf,
1752                                  u64 events)
1753 {
1754         enum _ecore_status_t rc = ECORE_SUCCESS;
1755         struct ecore_filter_ucast filter;
1756
1757         if (!p_vf->vport_instance)
1758                 return ECORE_INVAL;
1759
1760         if (events & (1 << MAC_ADDR_FORCED)) {
1761                 /* Since there's no way [currently] of removing the MAC,
1762                  * we can always assume this means we need to force it.
1763                  */
1764                 OSAL_MEMSET(&filter, 0, sizeof(filter));
1765                 filter.type = ECORE_FILTER_MAC;
1766                 filter.opcode = ECORE_FILTER_REPLACE;
1767                 filter.is_rx_filter = 1;
1768                 filter.is_tx_filter = 1;
1769                 filter.vport_to_add_to = p_vf->vport_id;
1770                 OSAL_MEMCPY(filter.mac, p_vf->bulletin.p_virt->mac, ETH_ALEN);
1771
1772                 rc = ecore_sp_eth_filter_ucast(p_hwfn, p_vf->opaque_fid,
1773                                                &filter,
1774                                                ECORE_SPQ_MODE_CB, OSAL_NULL);
1775                 if (rc) {
1776                         DP_NOTICE(p_hwfn, true,
1777                                   "PF failed to configure MAC for VF\n");
1778                         return rc;
1779                 }
1780
1781                 p_vf->configured_features |= 1 << MAC_ADDR_FORCED;
1782         }
1783
1784         if (events & (1 << VLAN_ADDR_FORCED)) {
1785                 struct ecore_sp_vport_update_params vport_update;
1786                 u8 removal;
1787                 int i;
1788
1789                 OSAL_MEMSET(&filter, 0, sizeof(filter));
1790                 filter.type = ECORE_FILTER_VLAN;
1791                 filter.is_rx_filter = 1;
1792                 filter.is_tx_filter = 1;
1793                 filter.vport_to_add_to = p_vf->vport_id;
1794                 filter.vlan = p_vf->bulletin.p_virt->pvid;
1795                 filter.opcode = filter.vlan ? ECORE_FILTER_REPLACE :
1796                     ECORE_FILTER_FLUSH;
1797
1798                 /* Send the ramrod */
1799                 rc = ecore_sp_eth_filter_ucast(p_hwfn, p_vf->opaque_fid,
1800                                                &filter,
1801                                                ECORE_SPQ_MODE_CB, OSAL_NULL);
1802                 if (rc) {
1803                         DP_NOTICE(p_hwfn, true,
1804                                   "PF failed to configure VLAN for VF\n");
1805                         return rc;
1806                 }
1807
1808                 /* Update the default-vlan & silent vlan stripping */
1809                 OSAL_MEMSET(&vport_update, 0, sizeof(vport_update));
1810                 vport_update.opaque_fid = p_vf->opaque_fid;
1811                 vport_update.vport_id = p_vf->vport_id;
1812                 vport_update.update_default_vlan_enable_flg = 1;
1813                 vport_update.default_vlan_enable_flg = filter.vlan ? 1 : 0;
1814                 vport_update.update_default_vlan_flg = 1;
1815                 vport_update.default_vlan = filter.vlan;
1816
1817                 vport_update.update_inner_vlan_removal_flg = 1;
1818                 removal = filter.vlan ?
1819                     1 : p_vf->shadow_config.inner_vlan_removal;
1820                 vport_update.inner_vlan_removal_flg = removal;
1821                 vport_update.silent_vlan_removal_flg = filter.vlan ? 1 : 0;
1822                 rc = ecore_sp_vport_update(p_hwfn, &vport_update,
1823                                            ECORE_SPQ_MODE_EBLOCK, OSAL_NULL);
1824                 if (rc) {
1825                         DP_NOTICE(p_hwfn, true,
1826                                   "PF failed to configure VF vport for vlan\n");
1827                         return rc;
1828                 }
1829
1830                 /* Update all the Rx queues */
1831                 for (i = 0; i < ECORE_MAX_VF_CHAINS_PER_PF; i++) {
1832                         u16 qid;
1833
1834                         if (!p_vf->vf_queues[i].rxq_active)
1835                                 continue;
1836
1837                         qid = p_vf->vf_queues[i].fw_rx_qid;
1838
1839                         rc = ecore_sp_eth_rx_queues_update(p_hwfn, qid,
1840                                                    1, 0, 1,
1841                                                    ECORE_SPQ_MODE_EBLOCK,
1842                                                    OSAL_NULL);
1843                         if (rc) {
1844                                 DP_NOTICE(p_hwfn, true,
1845                                           "Failed to send Rx update"
1846                                           " fo queue[0x%04x]\n",
1847                                           qid);
1848                                 return rc;
1849                         }
1850                 }
1851
1852                 if (filter.vlan)
1853                         p_vf->configured_features |= 1 << VLAN_ADDR_FORCED;
1854                 else
1855                         p_vf->configured_features &= ~(1 << VLAN_ADDR_FORCED);
1856         }
1857
1858         /* If forced features are terminated, we need to configure the shadow
1859          * configuration back again.
1860          */
1861         if (events)
1862                 ecore_iov_reconfigure_unicast_shadow(p_hwfn, p_vf, events);
1863
1864         return rc;
1865 }
1866
1867 static void ecore_iov_vf_mbx_start_vport(struct ecore_hwfn *p_hwfn,
1868                                          struct ecore_ptt *p_ptt,
1869                                          struct ecore_vf_info *vf)
1870 {
1871         struct ecore_sp_vport_start_params params = { 0 };
1872         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
1873         struct vfpf_vport_start_tlv *start;
1874         u8 status = PFVF_STATUS_SUCCESS;
1875         struct ecore_vf_info *vf_info;
1876         u64 *p_bitmap;
1877         int sb_id;
1878         enum _ecore_status_t rc;
1879
1880         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vf->relative_vf_id, true);
1881         if (!vf_info) {
1882                 DP_NOTICE(p_hwfn->p_dev, true,
1883                           "Failed to get VF info, invalid vfid [%d]\n",
1884                           vf->relative_vf_id);
1885                 return;
1886         }
1887
1888         vf->state = VF_ENABLED;
1889         start = &mbx->req_virt->start_vport;
1890
1891         /* Initialize Status block in CAU */
1892         for (sb_id = 0; sb_id < vf->num_sbs; sb_id++) {
1893                 if (!start->sb_addr[sb_id]) {
1894                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
1895                                    "VF[%d] did not fill the address of SB %d\n",
1896                                    vf->relative_vf_id, sb_id);
1897                         break;
1898                 }
1899
1900                 ecore_int_cau_conf_sb(p_hwfn, p_ptt,
1901                                       start->sb_addr[sb_id],
1902                                       vf->igu_sbs[sb_id],
1903                                       vf->abs_vf_id, 1);
1904         }
1905         ecore_iov_enable_vf_traffic(p_hwfn, p_ptt, vf);
1906
1907         vf->mtu = start->mtu;
1908         vf->shadow_config.inner_vlan_removal = start->inner_vlan_removal;
1909
1910         /* Take into consideration configuration forced by hypervisor;
1911          * If none is configured, use the supplied VF values [for old
1912          * vfs that would still be fine, since they passed '0' as padding].
1913          */
1914         p_bitmap = &vf_info->bulletin.p_virt->valid_bitmap;
1915         if (!(*p_bitmap & (1 << VFPF_BULLETIN_UNTAGGED_DEFAULT_FORCED))) {
1916                 u8 vf_req = start->only_untagged;
1917
1918                 vf_info->bulletin.p_virt->default_only_untagged = vf_req;
1919                 *p_bitmap |= 1 << VFPF_BULLETIN_UNTAGGED_DEFAULT;
1920         }
1921
1922         params.tpa_mode = start->tpa_mode;
1923         params.remove_inner_vlan = start->inner_vlan_removal;
1924         params.tx_switching = true;
1925
1926 #ifndef ASIC_ONLY
1927         if (CHIP_REV_IS_FPGA(p_hwfn->p_dev)) {
1928                 DP_NOTICE(p_hwfn, false,
1929                           "FPGA: Don't config VF for Tx-switching [no pVFC]\n");
1930                 params.tx_switching = false;
1931         }
1932 #endif
1933
1934         params.only_untagged = vf_info->bulletin.p_virt->default_only_untagged;
1935         params.drop_ttl0 = false;
1936         params.concrete_fid = vf->concrete_fid;
1937         params.opaque_fid = vf->opaque_fid;
1938         params.vport_id = vf->vport_id;
1939         params.max_buffers_per_cqe = start->max_buffers_per_cqe;
1940         params.mtu = vf->mtu;
1941         params.check_mac = true;
1942
1943         rc = ecore_sp_eth_vport_start(p_hwfn, &params);
1944         if (rc != ECORE_SUCCESS) {
1945                 DP_ERR(p_hwfn,
1946                        "ecore_iov_vf_mbx_start_vport returned error %d\n", rc);
1947                 status = PFVF_STATUS_FAILURE;
1948         } else {
1949                 vf->vport_instance++;
1950
1951                 /* Force configuration if needed on the newly opened vport */
1952                 ecore_iov_configure_vport_forced(p_hwfn, vf, *p_bitmap);
1953                 OSAL_IOV_POST_START_VPORT(p_hwfn, vf->relative_vf_id,
1954                                           vf->vport_id, vf->opaque_fid);
1955                 __ecore_iov_spoofchk_set(p_hwfn, vf, vf->req_spoofchk_val);
1956         }
1957
1958         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_VPORT_START,
1959                                sizeof(struct pfvf_def_resp_tlv), status);
1960 }
1961
1962 static void ecore_iov_vf_mbx_stop_vport(struct ecore_hwfn *p_hwfn,
1963                                         struct ecore_ptt *p_ptt,
1964                                         struct ecore_vf_info *vf)
1965 {
1966         u8 status = PFVF_STATUS_SUCCESS;
1967         enum _ecore_status_t rc;
1968
1969         vf->vport_instance--;
1970         vf->spoof_chk = false;
1971
1972         if ((ecore_iov_validate_active_rxq(p_hwfn, vf)) ||
1973             (ecore_iov_validate_active_txq(p_hwfn, vf))) {
1974                 vf->b_malicious = true;
1975                 DP_NOTICE(p_hwfn, false,
1976                           "VF [%02x] - considered malicious;"
1977                           " Unable to stop RX/TX queuess\n",
1978                           vf->abs_vf_id);
1979         }
1980
1981         rc = ecore_sp_vport_stop(p_hwfn, vf->opaque_fid, vf->vport_id);
1982         if (rc != ECORE_SUCCESS) {
1983                 DP_ERR(p_hwfn,
1984                        "ecore_iov_vf_mbx_stop_vport returned error %d\n", rc);
1985                 status = PFVF_STATUS_FAILURE;
1986         }
1987
1988         /* Forget the configuration on the vport */
1989         vf->configured_features = 0;
1990         OSAL_MEMSET(&vf->shadow_config, 0, sizeof(vf->shadow_config));
1991
1992         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_VPORT_TEARDOWN,
1993                                sizeof(struct pfvf_def_resp_tlv), status);
1994 }
1995
1996 static void ecore_iov_vf_mbx_start_rxq_resp(struct ecore_hwfn *p_hwfn,
1997                                             struct ecore_ptt *p_ptt,
1998                                             struct ecore_vf_info *vf,
1999                                             u8 status, bool b_legacy)
2000 {
2001         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2002         struct pfvf_start_queue_resp_tlv *p_tlv;
2003         struct vfpf_start_rxq_tlv *req;
2004         u16 length;
2005
2006         mbx->offset = (u8 *)mbx->reply_virt;
2007
2008         /* Taking a bigger struct instead of adding a TLV to list was a
2009          * mistake, but one which we're now stuck with, as some older
2010          * clients assume the size of the previous response.
2011          */
2012         if (!b_legacy)
2013                 length = sizeof(*p_tlv);
2014         else
2015                 length = sizeof(struct pfvf_def_resp_tlv);
2016
2017         p_tlv = ecore_add_tlv(p_hwfn, &mbx->offset, CHANNEL_TLV_START_RXQ,
2018                               length);
2019         ecore_add_tlv(p_hwfn, &mbx->offset, CHANNEL_TLV_LIST_END,
2020                       sizeof(struct channel_list_end_tlv));
2021
2022         /* Update the TLV with the response */
2023         if ((status == PFVF_STATUS_SUCCESS) && !b_legacy) {
2024                 req = &mbx->req_virt->start_rxq;
2025                 p_tlv->offset = PXP_VF_BAR0_START_MSDM_ZONE_B +
2026                                 OFFSETOF(struct mstorm_vf_zone,
2027                                          non_trigger.eth_rx_queue_producers) +
2028                                 sizeof(struct eth_rx_prod_data) * req->rx_qid;
2029         }
2030
2031         ecore_iov_send_response(p_hwfn, p_ptt, vf, length, status);
2032 }
2033
2034 static void ecore_iov_vf_mbx_start_rxq(struct ecore_hwfn *p_hwfn,
2035                                        struct ecore_ptt *p_ptt,
2036                                        struct ecore_vf_info *vf)
2037 {
2038         struct ecore_queue_start_common_params params;
2039         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2040         u8 status = PFVF_STATUS_NO_RESOURCE;
2041         struct vfpf_start_rxq_tlv *req;
2042         bool b_legacy_vf = false;
2043         enum _ecore_status_t rc;
2044
2045         req = &mbx->req_virt->start_rxq;
2046
2047         if (!ecore_iov_validate_rxq(p_hwfn, vf, req->rx_qid) ||
2048             !ecore_iov_validate_sb(p_hwfn, vf, req->hw_sb))
2049                 goto out;
2050
2051         OSAL_MEMSET(&params, 0, sizeof(params));
2052         params.queue_id = (u8)vf->vf_queues[req->rx_qid].fw_rx_qid;
2053         params.vf_qid = req->rx_qid;
2054         params.vport_id = vf->vport_id;
2055         params.stats_id = vf->abs_vf_id + 0x10;
2056         params.sb = req->hw_sb;
2057         params.sb_idx = req->sb_index;
2058
2059         /* Legacy VFs have their Producers in a different location, which they
2060          * calculate on their own and clean the producer prior to this.
2061          */
2062         if (vf->acquire.vfdev_info.eth_fp_hsi_minor ==
2063             ETH_HSI_VER_NO_PKT_LEN_TUNN)
2064                 b_legacy_vf = true;
2065         else
2066                 REG_WR(p_hwfn,
2067                        GTT_BAR0_MAP_REG_MSDM_RAM +
2068                        MSTORM_ETH_VF_PRODS_OFFSET(vf->abs_vf_id, req->rx_qid),
2069                        0);
2070
2071         rc = ecore_sp_eth_rxq_start_ramrod(p_hwfn, vf->opaque_fid,
2072                                            vf->vf_queues[req->rx_qid].fw_cid,
2073                                            &params,
2074                                            req->bd_max_bytes,
2075                                            req->rxq_addr,
2076                                            req->cqe_pbl_addr,
2077                                            req->cqe_pbl_size,
2078                                            b_legacy_vf);
2079
2080         if (rc) {
2081                 status = PFVF_STATUS_FAILURE;
2082         } else {
2083                 status = PFVF_STATUS_SUCCESS;
2084                 vf->vf_queues[req->rx_qid].rxq_active = true;
2085                 vf->num_active_rxqs++;
2086         }
2087
2088 out:
2089         ecore_iov_vf_mbx_start_rxq_resp(p_hwfn, p_ptt, vf,
2090                                         status, b_legacy_vf);
2091 }
2092
2093 static void ecore_iov_vf_mbx_start_txq_resp(struct ecore_hwfn *p_hwfn,
2094                                             struct ecore_ptt *p_ptt,
2095                                             struct ecore_vf_info *p_vf,
2096                                             u8 status)
2097 {
2098         struct ecore_iov_vf_mbx *mbx = &p_vf->vf_mbx;
2099         struct pfvf_start_queue_resp_tlv *p_tlv;
2100         bool b_legacy = false;
2101         u16 length;
2102
2103         mbx->offset = (u8 *)mbx->reply_virt;
2104
2105         /* Taking a bigger struct instead of adding a TLV to list was a
2106          * mistake, but one which we're now stuck with, as some older
2107          * clients assume the size of the previous response.
2108          */
2109         if (p_vf->acquire.vfdev_info.eth_fp_hsi_minor ==
2110             ETH_HSI_VER_NO_PKT_LEN_TUNN)
2111                 b_legacy = true;
2112
2113         if (!b_legacy)
2114                 length = sizeof(*p_tlv);
2115         else
2116                 length = sizeof(struct pfvf_def_resp_tlv);
2117
2118         p_tlv = ecore_add_tlv(p_hwfn, &mbx->offset, CHANNEL_TLV_START_TXQ,
2119                               length);
2120         ecore_add_tlv(p_hwfn, &mbx->offset, CHANNEL_TLV_LIST_END,
2121                       sizeof(struct channel_list_end_tlv));
2122
2123         /* Update the TLV with the response */
2124         if ((status == PFVF_STATUS_SUCCESS) && !b_legacy) {
2125                 u16 qid = mbx->req_virt->start_txq.tx_qid;
2126
2127                 p_tlv->offset = DB_ADDR_VF(p_vf->vf_queues[qid].fw_cid,
2128                                            DQ_DEMS_LEGACY);
2129         }
2130
2131         ecore_iov_send_response(p_hwfn, p_ptt, p_vf, length, status);
2132 }
2133
2134 static void ecore_iov_vf_mbx_start_txq(struct ecore_hwfn *p_hwfn,
2135                                        struct ecore_ptt *p_ptt,
2136                                        struct ecore_vf_info *vf)
2137 {
2138         struct ecore_queue_start_common_params params;
2139         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2140         u8 status = PFVF_STATUS_NO_RESOURCE;
2141         struct vfpf_start_txq_tlv *req;
2142         enum _ecore_status_t rc;
2143
2144         OSAL_MEMSET(&params, 0, sizeof(params));
2145         req = &mbx->req_virt->start_txq;
2146
2147         if (!ecore_iov_validate_txq(p_hwfn, vf, req->tx_qid) ||
2148             !ecore_iov_validate_sb(p_hwfn, vf, req->hw_sb))
2149                 goto out;
2150
2151         params.queue_id = vf->vf_queues[req->tx_qid].fw_tx_qid;
2152         params.qzone_id = vf->vf_queues[req->tx_qid].fw_tx_qid;
2153         params.vport_id = vf->vport_id;
2154         params.stats_id = vf->abs_vf_id + 0x10;
2155         params.sb = req->hw_sb;
2156         params.sb_idx = req->sb_index;
2157
2158         rc = ecore_sp_eth_txq_start_ramrod(p_hwfn,
2159                                            vf->opaque_fid,
2160                                            vf->vf_queues[req->tx_qid].fw_cid,
2161                                            &params,
2162                                            req->pbl_addr,
2163                                            req->pbl_size,
2164                                            ecore_get_cm_pq_idx_vf(p_hwfn,
2165                                                         vf->relative_vf_id));
2166
2167         if (rc)
2168                 status = PFVF_STATUS_FAILURE;
2169         else {
2170                 status = PFVF_STATUS_SUCCESS;
2171                 vf->vf_queues[req->tx_qid].txq_active = true;
2172         }
2173
2174 out:
2175         ecore_iov_vf_mbx_start_txq_resp(p_hwfn, p_ptt, vf, status);
2176 }
2177
2178 static enum _ecore_status_t ecore_iov_vf_stop_rxqs(struct ecore_hwfn *p_hwfn,
2179                                                    struct ecore_vf_info *vf,
2180                                                    u16 rxq_id,
2181                                                    u8 num_rxqs,
2182                                                    bool cqe_completion)
2183 {
2184         enum _ecore_status_t rc = ECORE_SUCCESS;
2185         int qid;
2186
2187         if (rxq_id + num_rxqs > OSAL_ARRAY_SIZE(vf->vf_queues))
2188                 return ECORE_INVAL;
2189
2190         for (qid = rxq_id; qid < rxq_id + num_rxqs; qid++) {
2191                 if (vf->vf_queues[qid].rxq_active) {
2192                         rc = ecore_sp_eth_rx_queue_stop(p_hwfn,
2193                                                         vf->vf_queues[qid].
2194                                                         fw_rx_qid, false,
2195                                                         cqe_completion);
2196
2197                         if (rc)
2198                                 return rc;
2199                 }
2200                 vf->vf_queues[qid].rxq_active = false;
2201                 vf->num_active_rxqs--;
2202         }
2203
2204         return rc;
2205 }
2206
2207 static enum _ecore_status_t ecore_iov_vf_stop_txqs(struct ecore_hwfn *p_hwfn,
2208                                                    struct ecore_vf_info *vf,
2209                                                    u16 txq_id, u8 num_txqs)
2210 {
2211         enum _ecore_status_t rc = ECORE_SUCCESS;
2212         int qid;
2213
2214         if (txq_id + num_txqs > OSAL_ARRAY_SIZE(vf->vf_queues))
2215                 return ECORE_INVAL;
2216
2217         for (qid = txq_id; qid < txq_id + num_txqs; qid++) {
2218                 if (vf->vf_queues[qid].txq_active) {
2219                         rc = ecore_sp_eth_tx_queue_stop(p_hwfn,
2220                                                         vf->vf_queues[qid].
2221                                                         fw_tx_qid);
2222
2223                         if (rc)
2224                                 return rc;
2225                 }
2226                 vf->vf_queues[qid].txq_active = false;
2227         }
2228         return rc;
2229 }
2230
2231 static void ecore_iov_vf_mbx_stop_rxqs(struct ecore_hwfn *p_hwfn,
2232                                        struct ecore_ptt *p_ptt,
2233                                        struct ecore_vf_info *vf)
2234 {
2235         u16 length = sizeof(struct pfvf_def_resp_tlv);
2236         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2237         u8 status = PFVF_STATUS_SUCCESS;
2238         struct vfpf_stop_rxqs_tlv *req;
2239         enum _ecore_status_t rc;
2240
2241         /* We give the option of starting from qid != 0, in this case we
2242          * need to make sure that qid + num_qs doesn't exceed the actual
2243          * amount of queues that exist.
2244          */
2245         req = &mbx->req_virt->stop_rxqs;
2246         rc = ecore_iov_vf_stop_rxqs(p_hwfn, vf, req->rx_qid,
2247                                     req->num_rxqs, req->cqe_completion);
2248         if (rc)
2249                 status = PFVF_STATUS_FAILURE;
2250
2251         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_STOP_RXQS,
2252                                length, status);
2253 }
2254
2255 static void ecore_iov_vf_mbx_stop_txqs(struct ecore_hwfn *p_hwfn,
2256                                        struct ecore_ptt *p_ptt,
2257                                        struct ecore_vf_info *vf)
2258 {
2259         u16 length = sizeof(struct pfvf_def_resp_tlv);
2260         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2261         u8 status = PFVF_STATUS_SUCCESS;
2262         struct vfpf_stop_txqs_tlv *req;
2263         enum _ecore_status_t rc;
2264
2265         /* We give the option of starting from qid != 0, in this case we
2266          * need to make sure that qid + num_qs doesn't exceed the actual
2267          * amount of queues that exist.
2268          */
2269         req = &mbx->req_virt->stop_txqs;
2270         rc = ecore_iov_vf_stop_txqs(p_hwfn, vf, req->tx_qid, req->num_txqs);
2271         if (rc)
2272                 status = PFVF_STATUS_FAILURE;
2273
2274         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_STOP_TXQS,
2275                                length, status);
2276 }
2277
2278 static void ecore_iov_vf_mbx_update_rxqs(struct ecore_hwfn *p_hwfn,
2279                                          struct ecore_ptt *p_ptt,
2280                                          struct ecore_vf_info *vf)
2281 {
2282         u16 length = sizeof(struct pfvf_def_resp_tlv);
2283         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2284         struct vfpf_update_rxq_tlv *req;
2285         u8 status = PFVF_STATUS_SUCCESS;
2286         u8 complete_event_flg;
2287         u8 complete_cqe_flg;
2288         u16 qid;
2289         enum _ecore_status_t rc;
2290         u8 i;
2291
2292         req = &mbx->req_virt->update_rxq;
2293         complete_cqe_flg = !!(req->flags & VFPF_RXQ_UPD_COMPLETE_CQE_FLAG);
2294         complete_event_flg = !!(req->flags & VFPF_RXQ_UPD_COMPLETE_EVENT_FLAG);
2295
2296         for (i = 0; i < req->num_rxqs; i++) {
2297                 qid = req->rx_qid + i;
2298
2299                 if (!vf->vf_queues[qid].rxq_active) {
2300                         DP_NOTICE(p_hwfn, true,
2301                                   "VF rx_qid = %d isn`t active!\n", qid);
2302                         status = PFVF_STATUS_FAILURE;
2303                         break;
2304                 }
2305
2306                 rc = ecore_sp_eth_rx_queues_update(p_hwfn,
2307                                                    vf->vf_queues[qid].fw_rx_qid,
2308                                                    1,
2309                                                    complete_cqe_flg,
2310                                                    complete_event_flg,
2311                                                    ECORE_SPQ_MODE_EBLOCK,
2312                                                    OSAL_NULL);
2313
2314                 if (rc) {
2315                         status = PFVF_STATUS_FAILURE;
2316                         break;
2317                 }
2318         }
2319
2320         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_UPDATE_RXQ,
2321                                length, status);
2322 }
2323
2324 void *ecore_iov_search_list_tlvs(struct ecore_hwfn *p_hwfn,
2325                                  void *p_tlvs_list, u16 req_type)
2326 {
2327         struct channel_tlv *p_tlv = (struct channel_tlv *)p_tlvs_list;
2328         int len = 0;
2329
2330         do {
2331                 if (!p_tlv->length) {
2332                         DP_NOTICE(p_hwfn, true, "Zero length TLV found\n");
2333                         return OSAL_NULL;
2334                 }
2335
2336                 if (p_tlv->type == req_type) {
2337                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2338                                    "Extended tlv type %s, length %d found\n",
2339                                    ecore_channel_tlvs_string[p_tlv->type],
2340                                    p_tlv->length);
2341                         return p_tlv;
2342                 }
2343
2344                 len += p_tlv->length;
2345                 p_tlv = (struct channel_tlv *)((u8 *)p_tlv + p_tlv->length);
2346
2347                 if ((len + p_tlv->length) > TLV_BUFFER_SIZE) {
2348                         DP_NOTICE(p_hwfn, true,
2349                                   "TLVs has overrun the buffer size\n");
2350                         return OSAL_NULL;
2351                 }
2352         } while (p_tlv->type != CHANNEL_TLV_LIST_END);
2353
2354         return OSAL_NULL;
2355 }
2356
2357 static void
2358 ecore_iov_vp_update_act_param(struct ecore_hwfn *p_hwfn,
2359                               struct ecore_sp_vport_update_params *p_data,
2360                               struct ecore_iov_vf_mbx *p_mbx, u16 *tlvs_mask)
2361 {
2362         struct vfpf_vport_update_activate_tlv *p_act_tlv;
2363         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_ACTIVATE;
2364
2365         p_act_tlv = (struct vfpf_vport_update_activate_tlv *)
2366             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2367         if (!p_act_tlv)
2368                 return;
2369
2370         p_data->update_vport_active_rx_flg = p_act_tlv->update_rx;
2371         p_data->vport_active_rx_flg = p_act_tlv->active_rx;
2372         p_data->update_vport_active_tx_flg = p_act_tlv->update_tx;
2373         p_data->vport_active_tx_flg = p_act_tlv->active_tx;
2374         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_ACTIVATE;
2375 }
2376
2377 static void
2378 ecore_iov_vp_update_vlan_param(struct ecore_hwfn *p_hwfn,
2379                                struct ecore_sp_vport_update_params *p_data,
2380                                struct ecore_vf_info *p_vf,
2381                                struct ecore_iov_vf_mbx *p_mbx, u16 *tlvs_mask)
2382 {
2383         struct vfpf_vport_update_vlan_strip_tlv *p_vlan_tlv;
2384         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_VLAN_STRIP;
2385
2386         p_vlan_tlv = (struct vfpf_vport_update_vlan_strip_tlv *)
2387             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2388         if (!p_vlan_tlv)
2389                 return;
2390
2391         p_vf->shadow_config.inner_vlan_removal = p_vlan_tlv->remove_vlan;
2392
2393         /* Ignore the VF request if we're forcing a vlan */
2394         if (!(p_vf->configured_features & (1 << VLAN_ADDR_FORCED))) {
2395                 p_data->update_inner_vlan_removal_flg = 1;
2396                 p_data->inner_vlan_removal_flg = p_vlan_tlv->remove_vlan;
2397         }
2398
2399         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_VLAN_STRIP;
2400 }
2401
2402 static void
2403 ecore_iov_vp_update_tx_switch(struct ecore_hwfn *p_hwfn,
2404                               struct ecore_sp_vport_update_params *p_data,
2405                               struct ecore_iov_vf_mbx *p_mbx, u16 *tlvs_mask)
2406 {
2407         struct vfpf_vport_update_tx_switch_tlv *p_tx_switch_tlv;
2408         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_TX_SWITCH;
2409
2410         p_tx_switch_tlv = (struct vfpf_vport_update_tx_switch_tlv *)
2411             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2412         if (!p_tx_switch_tlv)
2413                 return;
2414
2415 #ifndef ASIC_ONLY
2416         if (CHIP_REV_IS_FPGA(p_hwfn->p_dev)) {
2417                 DP_NOTICE(p_hwfn, false,
2418                           "FPGA: Ignore tx-switching configuration originating"
2419                           " from VFs\n");
2420                 return;
2421         }
2422 #endif
2423
2424         p_data->update_tx_switching_flg = 1;
2425         p_data->tx_switching_flg = p_tx_switch_tlv->tx_switching;
2426         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_TX_SWITCH;
2427 }
2428
2429 static void
2430 ecore_iov_vp_update_mcast_bin_param(struct ecore_hwfn *p_hwfn,
2431                                     struct ecore_sp_vport_update_params *p_data,
2432                                     struct ecore_iov_vf_mbx *p_mbx,
2433                                     u16 *tlvs_mask)
2434 {
2435         struct vfpf_vport_update_mcast_bin_tlv *p_mcast_tlv;
2436         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_MCAST;
2437
2438         p_mcast_tlv = (struct vfpf_vport_update_mcast_bin_tlv *)
2439             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2440         if (!p_mcast_tlv)
2441                 return;
2442
2443         p_data->update_approx_mcast_flg = 1;
2444         OSAL_MEMCPY(p_data->bins, p_mcast_tlv->bins,
2445                     sizeof(unsigned long) *
2446                     ETH_MULTICAST_MAC_BINS_IN_REGS);
2447         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_MCAST;
2448 }
2449
2450 static void
2451 ecore_iov_vp_update_accept_flag(struct ecore_hwfn *p_hwfn,
2452                                 struct ecore_sp_vport_update_params *p_data,
2453                                 struct ecore_iov_vf_mbx *p_mbx, u16 *tlvs_mask)
2454 {
2455         struct ecore_filter_accept_flags *p_flags = &p_data->accept_flags;
2456         struct vfpf_vport_update_accept_param_tlv *p_accept_tlv;
2457         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_ACCEPT_PARAM;
2458
2459         p_accept_tlv = (struct vfpf_vport_update_accept_param_tlv *)
2460             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2461         if (!p_accept_tlv)
2462                 return;
2463
2464         p_flags->update_rx_mode_config = p_accept_tlv->update_rx_mode;
2465         p_flags->rx_accept_filter = p_accept_tlv->rx_accept_filter;
2466         p_flags->update_tx_mode_config = p_accept_tlv->update_tx_mode;
2467         p_flags->tx_accept_filter = p_accept_tlv->tx_accept_filter;
2468         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_ACCEPT_PARAM;
2469 }
2470
2471 static void
2472 ecore_iov_vp_update_accept_any_vlan(struct ecore_hwfn *p_hwfn,
2473                                     struct ecore_sp_vport_update_params *p_data,
2474                                     struct ecore_iov_vf_mbx *p_mbx,
2475                                     u16 *tlvs_mask)
2476 {
2477         struct vfpf_vport_update_accept_any_vlan_tlv *p_accept_any_vlan;
2478         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_ACCEPT_ANY_VLAN;
2479
2480         p_accept_any_vlan = (struct vfpf_vport_update_accept_any_vlan_tlv *)
2481             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2482         if (!p_accept_any_vlan)
2483                 return;
2484
2485         p_data->accept_any_vlan = p_accept_any_vlan->accept_any_vlan;
2486         p_data->update_accept_any_vlan_flg =
2487                         p_accept_any_vlan->update_accept_any_vlan_flg;
2488         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_ACCEPT_ANY_VLAN;
2489 }
2490
2491 static void
2492 ecore_iov_vp_update_rss_param(struct ecore_hwfn *p_hwfn,
2493                               struct ecore_vf_info *vf,
2494                               struct ecore_sp_vport_update_params *p_data,
2495                               struct ecore_rss_params *p_rss,
2496                               struct ecore_iov_vf_mbx *p_mbx, u16 *tlvs_mask)
2497 {
2498         struct vfpf_vport_update_rss_tlv *p_rss_tlv;
2499         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_RSS;
2500         u16 i, q_idx, max_q_idx;
2501         u16 table_size;
2502
2503         p_rss_tlv = (struct vfpf_vport_update_rss_tlv *)
2504             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2505         if (!p_rss_tlv) {
2506                 p_data->rss_params = OSAL_NULL;
2507                 return;
2508         }
2509
2510         OSAL_MEMSET(p_rss, 0, sizeof(struct ecore_rss_params));
2511
2512         p_rss->update_rss_config =
2513             !!(p_rss_tlv->update_rss_flags &
2514                 VFPF_UPDATE_RSS_CONFIG_FLAG);
2515         p_rss->update_rss_capabilities =
2516             !!(p_rss_tlv->update_rss_flags &
2517                 VFPF_UPDATE_RSS_CAPS_FLAG);
2518         p_rss->update_rss_ind_table =
2519             !!(p_rss_tlv->update_rss_flags &
2520                 VFPF_UPDATE_RSS_IND_TABLE_FLAG);
2521         p_rss->update_rss_key =
2522             !!(p_rss_tlv->update_rss_flags &
2523                 VFPF_UPDATE_RSS_KEY_FLAG);
2524
2525         p_rss->rss_enable = p_rss_tlv->rss_enable;
2526         p_rss->rss_eng_id = vf->relative_vf_id + 1;
2527         p_rss->rss_caps = p_rss_tlv->rss_caps;
2528         p_rss->rss_table_size_log = p_rss_tlv->rss_table_size_log;
2529         OSAL_MEMCPY(p_rss->rss_ind_table, p_rss_tlv->rss_ind_table,
2530                     sizeof(p_rss->rss_ind_table));
2531         OSAL_MEMCPY(p_rss->rss_key, p_rss_tlv->rss_key,
2532                     sizeof(p_rss->rss_key));
2533
2534         table_size = OSAL_MIN_T(u16, OSAL_ARRAY_SIZE(p_rss->rss_ind_table),
2535                                 (1 << p_rss_tlv->rss_table_size_log));
2536
2537         max_q_idx = OSAL_ARRAY_SIZE(vf->vf_queues);
2538
2539         for (i = 0; i < table_size; i++) {
2540                 u16 index = vf->vf_queues[0].fw_rx_qid;
2541
2542                 q_idx = p_rss->rss_ind_table[i];
2543                 if (q_idx >= max_q_idx)
2544                         DP_NOTICE(p_hwfn, true,
2545                                   "rss_ind_table[%d] = %d,"
2546                                   " rxq is out of range\n",
2547                                   i, q_idx);
2548                 else if (!vf->vf_queues[q_idx].rxq_active)
2549                         DP_NOTICE(p_hwfn, true,
2550                                   "rss_ind_table[%d] = %d, rxq is not active\n",
2551                                   i, q_idx);
2552                 else
2553                         index = vf->vf_queues[q_idx].fw_rx_qid;
2554                 p_rss->rss_ind_table[i] = index;
2555         }
2556
2557         p_data->rss_params = p_rss;
2558         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_RSS;
2559 }
2560
2561 static void
2562 ecore_iov_vp_update_sge_tpa_param(struct ecore_hwfn *p_hwfn,
2563                                   struct ecore_vf_info *vf,
2564                                   struct ecore_sp_vport_update_params *p_data,
2565                                   struct ecore_sge_tpa_params *p_sge_tpa,
2566                                   struct ecore_iov_vf_mbx *p_mbx,
2567                                   u16 *tlvs_mask)
2568 {
2569         struct vfpf_vport_update_sge_tpa_tlv *p_sge_tpa_tlv;
2570         u16 tlv = CHANNEL_TLV_VPORT_UPDATE_SGE_TPA;
2571
2572         p_sge_tpa_tlv = (struct vfpf_vport_update_sge_tpa_tlv *)
2573             ecore_iov_search_list_tlvs(p_hwfn, p_mbx->req_virt, tlv);
2574
2575         if (!p_sge_tpa_tlv) {
2576                 p_data->sge_tpa_params = OSAL_NULL;
2577                 return;
2578         }
2579
2580         OSAL_MEMSET(p_sge_tpa, 0, sizeof(struct ecore_sge_tpa_params));
2581
2582         p_sge_tpa->update_tpa_en_flg =
2583             !!(p_sge_tpa_tlv->update_sge_tpa_flags & VFPF_UPDATE_TPA_EN_FLAG);
2584         p_sge_tpa->update_tpa_param_flg =
2585             !!(p_sge_tpa_tlv->update_sge_tpa_flags &
2586                 VFPF_UPDATE_TPA_PARAM_FLAG);
2587
2588         p_sge_tpa->tpa_ipv4_en_flg =
2589             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_IPV4_EN_FLAG);
2590         p_sge_tpa->tpa_ipv6_en_flg =
2591             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_IPV6_EN_FLAG);
2592         p_sge_tpa->tpa_pkt_split_flg =
2593             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_PKT_SPLIT_FLAG);
2594         p_sge_tpa->tpa_hdr_data_split_flg =
2595             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_HDR_DATA_SPLIT_FLAG);
2596         p_sge_tpa->tpa_gro_consistent_flg =
2597             !!(p_sge_tpa_tlv->sge_tpa_flags & VFPF_TPA_GRO_CONSIST_FLAG);
2598
2599         p_sge_tpa->tpa_max_aggs_num = p_sge_tpa_tlv->tpa_max_aggs_num;
2600         p_sge_tpa->tpa_max_size = p_sge_tpa_tlv->tpa_max_size;
2601         p_sge_tpa->tpa_min_size_to_start = p_sge_tpa_tlv->tpa_min_size_to_start;
2602         p_sge_tpa->tpa_min_size_to_cont = p_sge_tpa_tlv->tpa_min_size_to_cont;
2603         p_sge_tpa->max_buffers_per_cqe = p_sge_tpa_tlv->max_buffers_per_cqe;
2604
2605         p_data->sge_tpa_params = p_sge_tpa;
2606
2607         *tlvs_mask |= 1 << ECORE_IOV_VP_UPDATE_SGE_TPA;
2608 }
2609
2610 static void ecore_iov_vf_mbx_vport_update(struct ecore_hwfn *p_hwfn,
2611                                           struct ecore_ptt *p_ptt,
2612                                           struct ecore_vf_info *vf)
2613 {
2614         struct ecore_sp_vport_update_params params;
2615         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2616         struct ecore_sge_tpa_params sge_tpa_params;
2617         u16 tlvs_mask = 0, tlvs_accepted = 0;
2618         struct ecore_rss_params rss_params;
2619         u8 status = PFVF_STATUS_SUCCESS;
2620         u16 length;
2621         enum _ecore_status_t rc;
2622
2623         /* Valiate PF can send such a request */
2624         if (!vf->vport_instance) {
2625                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2626                            "No VPORT instance available for VF[%d],"
2627                            " failing vport update\n",
2628                            vf->abs_vf_id);
2629                 status = PFVF_STATUS_FAILURE;
2630                 goto out;
2631         }
2632
2633         OSAL_MEMSET(&params, 0, sizeof(params));
2634         params.opaque_fid = vf->opaque_fid;
2635         params.vport_id = vf->vport_id;
2636         params.rss_params = OSAL_NULL;
2637
2638         /* Search for extended tlvs list and update values
2639          * from VF in struct ecore_sp_vport_update_params.
2640          */
2641         ecore_iov_vp_update_act_param(p_hwfn, &params, mbx, &tlvs_mask);
2642         ecore_iov_vp_update_vlan_param(p_hwfn, &params, vf, mbx, &tlvs_mask);
2643         ecore_iov_vp_update_tx_switch(p_hwfn, &params, mbx, &tlvs_mask);
2644         ecore_iov_vp_update_mcast_bin_param(p_hwfn, &params, mbx, &tlvs_mask);
2645         ecore_iov_vp_update_accept_flag(p_hwfn, &params, mbx, &tlvs_mask);
2646         ecore_iov_vp_update_rss_param(p_hwfn, vf, &params, &rss_params,
2647                                       mbx, &tlvs_mask);
2648         ecore_iov_vp_update_accept_any_vlan(p_hwfn, &params, mbx, &tlvs_mask);
2649         ecore_iov_vp_update_sge_tpa_param(p_hwfn, vf, &params,
2650                                           &sge_tpa_params, mbx, &tlvs_mask);
2651
2652         /* Just log a message if there is no single extended tlv in buffer.
2653          * When all features of vport update ramrod would be requested by VF
2654          * as extended TLVs in buffer then an error can be returned in response
2655          * if there is no extended TLV present in buffer.
2656          */
2657         tlvs_accepted = tlvs_mask;
2658
2659         if (OSAL_IOV_VF_VPORT_UPDATE(p_hwfn, vf->relative_vf_id,
2660                                      &params, &tlvs_accepted) !=
2661             ECORE_SUCCESS) {
2662                 tlvs_accepted = 0;
2663                 status = PFVF_STATUS_NOT_SUPPORTED;
2664                 goto out;
2665         }
2666
2667         if (!tlvs_accepted) {
2668                 if (tlvs_mask)
2669                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2670                                    "Upper-layer prevents said VF"
2671                                    " configuration\n");
2672                 else
2673                         DP_NOTICE(p_hwfn, true,
2674                                   "No feature tlvs found for vport update\n");
2675                 status = PFVF_STATUS_NOT_SUPPORTED;
2676                 goto out;
2677         }
2678
2679         rc = ecore_sp_vport_update(p_hwfn, &params, ECORE_SPQ_MODE_EBLOCK,
2680                                    OSAL_NULL);
2681
2682         if (rc)
2683                 status = PFVF_STATUS_FAILURE;
2684
2685 out:
2686         length = ecore_iov_prep_vp_update_resp_tlvs(p_hwfn, vf, mbx, status,
2687                                                     tlvs_mask, tlvs_accepted);
2688         ecore_iov_send_response(p_hwfn, p_ptt, vf, length, status);
2689 }
2690
2691 static enum _ecore_status_t
2692 ecore_iov_vf_update_vlan_shadow(struct ecore_hwfn *p_hwfn,
2693                                 struct ecore_vf_info *p_vf,
2694                                 struct ecore_filter_ucast *p_params)
2695 {
2696         int i;
2697
2698         /* First remove entries and then add new ones */
2699         if (p_params->opcode == ECORE_FILTER_REMOVE) {
2700                 for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++)
2701                         if (p_vf->shadow_config.vlans[i].used &&
2702                             p_vf->shadow_config.vlans[i].vid ==
2703                             p_params->vlan) {
2704                                 p_vf->shadow_config.vlans[i].used = false;
2705                                 break;
2706                         }
2707                 if (i == ECORE_ETH_VF_NUM_VLAN_FILTERS + 1) {
2708                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2709                                    "VF [%d] - Tries to remove a non-existing"
2710                                    " vlan\n",
2711                                    p_vf->relative_vf_id);
2712                         return ECORE_INVAL;
2713                 }
2714         } else if (p_params->opcode == ECORE_FILTER_REPLACE ||
2715                    p_params->opcode == ECORE_FILTER_FLUSH) {
2716                 for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++)
2717                         p_vf->shadow_config.vlans[i].used = false;
2718         }
2719
2720         /* In forced mode, we're willing to remove entries - but we don't add
2721          * new ones.
2722          */
2723         if (p_vf->bulletin.p_virt->valid_bitmap & (1 << VLAN_ADDR_FORCED))
2724                 return ECORE_SUCCESS;
2725
2726         if (p_params->opcode == ECORE_FILTER_ADD ||
2727             p_params->opcode == ECORE_FILTER_REPLACE) {
2728                 for (i = 0; i < ECORE_ETH_VF_NUM_VLAN_FILTERS + 1; i++) {
2729                         if (p_vf->shadow_config.vlans[i].used)
2730                                 continue;
2731
2732                         p_vf->shadow_config.vlans[i].used = true;
2733                         p_vf->shadow_config.vlans[i].vid = p_params->vlan;
2734                         break;
2735                 }
2736
2737                 if (i == ECORE_ETH_VF_NUM_VLAN_FILTERS + 1) {
2738                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2739                                    "VF [%d] - Tries to configure more than %d"
2740                                    " vlan filters\n",
2741                                    p_vf->relative_vf_id,
2742                                    ECORE_ETH_VF_NUM_VLAN_FILTERS + 1);
2743                         return ECORE_INVAL;
2744                 }
2745         }
2746
2747         return ECORE_SUCCESS;
2748 }
2749
2750 static enum _ecore_status_t
2751 ecore_iov_vf_update_mac_shadow(struct ecore_hwfn *p_hwfn,
2752                                struct ecore_vf_info *p_vf,
2753                                struct ecore_filter_ucast *p_params)
2754 {
2755         char empty_mac[ETH_ALEN];
2756         int i;
2757
2758         OSAL_MEM_ZERO(empty_mac, ETH_ALEN);
2759
2760         /* If we're in forced-mode, we don't allow any change */
2761         /* TODO - this would change if we were ever to implement logic for
2762          * removing a forced MAC altogether [in which case, like for vlans,
2763          * we should be able to re-trace previous configuration.
2764          */
2765         if (p_vf->bulletin.p_virt->valid_bitmap & (1 << MAC_ADDR_FORCED))
2766                 return ECORE_SUCCESS;
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_MAC_FILTERS; i++) {
2771                         if (!OSAL_MEMCMP(p_vf->shadow_config.macs[i],
2772                                          p_params->mac, ETH_ALEN)) {
2773                                 OSAL_MEM_ZERO(p_vf->shadow_config.macs[i],
2774                                               ETH_ALEN);
2775                                 break;
2776                         }
2777                 }
2778
2779                 if (i == ECORE_ETH_VF_NUM_MAC_FILTERS) {
2780                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2781                                    "MAC isn't configured\n");
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_MAC_FILTERS; i++)
2787                         OSAL_MEM_ZERO(p_vf->shadow_config.macs[i], ETH_ALEN);
2788         }
2789
2790         /* List the new MAC address */
2791         if (p_params->opcode != ECORE_FILTER_ADD &&
2792             p_params->opcode != ECORE_FILTER_REPLACE)
2793                 return ECORE_SUCCESS;
2794
2795         for (i = 0; i < ECORE_ETH_VF_NUM_MAC_FILTERS; i++) {
2796                 if (!OSAL_MEMCMP(p_vf->shadow_config.macs[i],
2797                                  empty_mac, ETH_ALEN)) {
2798                         OSAL_MEMCPY(p_vf->shadow_config.macs[i],
2799                                     p_params->mac, ETH_ALEN);
2800                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2801                                    "Added MAC at %d entry in shadow\n", i);
2802                         break;
2803                 }
2804         }
2805
2806         if (i == ECORE_ETH_VF_NUM_MAC_FILTERS) {
2807                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2808                            "No available place for MAC\n");
2809                 return ECORE_INVAL;
2810         }
2811
2812         return ECORE_SUCCESS;
2813 }
2814
2815 static enum _ecore_status_t
2816 ecore_iov_vf_update_unicast_shadow(struct ecore_hwfn *p_hwfn,
2817                                    struct ecore_vf_info *p_vf,
2818                                    struct ecore_filter_ucast *p_params)
2819 {
2820         enum _ecore_status_t rc = ECORE_SUCCESS;
2821
2822         if (p_params->type == ECORE_FILTER_MAC) {
2823                 rc = ecore_iov_vf_update_mac_shadow(p_hwfn, p_vf, p_params);
2824                 if (rc != ECORE_SUCCESS)
2825                         return rc;
2826         }
2827
2828         if (p_params->type == ECORE_FILTER_VLAN)
2829                 rc = ecore_iov_vf_update_vlan_shadow(p_hwfn, p_vf, p_params);
2830
2831         return rc;
2832 }
2833
2834 static void ecore_iov_vf_mbx_ucast_filter(struct ecore_hwfn *p_hwfn,
2835                                           struct ecore_ptt *p_ptt,
2836                                           struct ecore_vf_info *vf)
2837 {
2838         struct ecore_bulletin_content *p_bulletin = vf->bulletin.p_virt;
2839         struct ecore_iov_vf_mbx *mbx = &vf->vf_mbx;
2840         struct vfpf_ucast_filter_tlv *req;
2841         u8 status = PFVF_STATUS_SUCCESS;
2842         struct ecore_filter_ucast params;
2843         enum _ecore_status_t rc;
2844
2845         /* Prepare the unicast filter params */
2846         OSAL_MEMSET(&params, 0, sizeof(struct ecore_filter_ucast));
2847         req = &mbx->req_virt->ucast_filter;
2848         params.opcode = (enum ecore_filter_opcode)req->opcode;
2849         params.type = (enum ecore_filter_ucast_type)req->type;
2850
2851         /* @@@TBD - We might need logic on HV side in determining this */
2852         params.is_rx_filter = 1;
2853         params.is_tx_filter = 1;
2854         params.vport_to_remove_from = vf->vport_id;
2855         params.vport_to_add_to = vf->vport_id;
2856         OSAL_MEMCPY(params.mac, req->mac, ETH_ALEN);
2857         params.vlan = req->vlan;
2858
2859         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2860                    "VF[%d]: opcode 0x%02x type 0x%02x [%s %s] [vport 0x%02x]"
2861                    " MAC %02x:%02x:%02x:%02x:%02x:%02x, vlan 0x%04x\n",
2862                    vf->abs_vf_id, params.opcode, params.type,
2863                    params.is_rx_filter ? "RX" : "",
2864                    params.is_tx_filter ? "TX" : "",
2865                    params.vport_to_add_to,
2866                    params.mac[0], params.mac[1], params.mac[2],
2867                    params.mac[3], params.mac[4], params.mac[5], params.vlan);
2868
2869         if (!vf->vport_instance) {
2870                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
2871                            "No VPORT instance available for VF[%d],"
2872                            " failing ucast MAC configuration\n",
2873                            vf->abs_vf_id);
2874                 status = PFVF_STATUS_FAILURE;
2875                 goto out;
2876         }
2877
2878         /* Update shadow copy of the VF configuration */
2879         if (ecore_iov_vf_update_unicast_shadow(p_hwfn, vf, &params) !=
2880             ECORE_SUCCESS) {
2881                 status = PFVF_STATUS_FAILURE;
2882                 goto out;
2883         }
2884
2885         /* Determine if the unicast filtering is acceptible by PF */
2886         if ((p_bulletin->valid_bitmap & (1 << VLAN_ADDR_FORCED)) &&
2887             (params.type == ECORE_FILTER_VLAN ||
2888              params.type == ECORE_FILTER_MAC_VLAN)) {
2889                 /* Once VLAN is forced or PVID is set, do not allow
2890                  * to add/replace any further VLANs.
2891                  */
2892                 if (params.opcode == ECORE_FILTER_ADD ||
2893                     params.opcode == ECORE_FILTER_REPLACE)
2894                         status = PFVF_STATUS_FORCED;
2895                 goto out;
2896         }
2897
2898         if ((p_bulletin->valid_bitmap & (1 << MAC_ADDR_FORCED)) &&
2899             (params.type == ECORE_FILTER_MAC ||
2900              params.type == ECORE_FILTER_MAC_VLAN)) {
2901                 if (OSAL_MEMCMP(p_bulletin->mac, params.mac, ETH_ALEN) ||
2902                     (params.opcode != ECORE_FILTER_ADD &&
2903                      params.opcode != ECORE_FILTER_REPLACE))
2904                         status = PFVF_STATUS_FORCED;
2905                 goto out;
2906         }
2907
2908         rc = OSAL_IOV_CHK_UCAST(p_hwfn, vf->relative_vf_id, &params);
2909         if (rc == ECORE_EXISTS) {
2910                 goto out;
2911         } else if (rc == ECORE_INVAL) {
2912                 status = PFVF_STATUS_FAILURE;
2913                 goto out;
2914         }
2915
2916         rc = ecore_sp_eth_filter_ucast(p_hwfn, vf->opaque_fid, &params,
2917                                        ECORE_SPQ_MODE_CB, OSAL_NULL);
2918         if (rc)
2919                 status = PFVF_STATUS_FAILURE;
2920
2921 out:
2922         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_UCAST_FILTER,
2923                                sizeof(struct pfvf_def_resp_tlv), status);
2924 }
2925
2926 static void ecore_iov_vf_mbx_int_cleanup(struct ecore_hwfn *p_hwfn,
2927                                          struct ecore_ptt *p_ptt,
2928                                          struct ecore_vf_info *vf)
2929 {
2930         int i;
2931
2932         /* Reset the SBs */
2933         for (i = 0; i < vf->num_sbs; i++)
2934                 ecore_int_igu_init_pure_rt_single(p_hwfn, p_ptt,
2935                                                   vf->igu_sbs[i],
2936                                                   vf->opaque_fid, false);
2937
2938         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_INT_CLEANUP,
2939                                sizeof(struct pfvf_def_resp_tlv),
2940                                PFVF_STATUS_SUCCESS);
2941 }
2942
2943 static void ecore_iov_vf_mbx_close(struct ecore_hwfn *p_hwfn,
2944                                    struct ecore_ptt *p_ptt,
2945                                    struct ecore_vf_info *vf)
2946 {
2947         u16 length = sizeof(struct pfvf_def_resp_tlv);
2948         u8 status = PFVF_STATUS_SUCCESS;
2949
2950         /* Disable Interrupts for VF */
2951         ecore_iov_vf_igu_set_int(p_hwfn, p_ptt, vf, 0);
2952
2953         /* Reset Permission table */
2954         ecore_iov_config_perm_table(p_hwfn, p_ptt, vf, 0);
2955
2956         ecore_iov_prepare_resp(p_hwfn, p_ptt, vf, CHANNEL_TLV_CLOSE,
2957                                length, status);
2958 }
2959
2960 static void ecore_iov_vf_mbx_release(struct ecore_hwfn *p_hwfn,
2961                                      struct ecore_ptt *p_ptt,
2962                                      struct ecore_vf_info *p_vf)
2963 {
2964         u16 length = sizeof(struct pfvf_def_resp_tlv);
2965         u8 status = PFVF_STATUS_SUCCESS;
2966         enum _ecore_status_t rc = ECORE_SUCCESS;
2967
2968         ecore_iov_vf_cleanup(p_hwfn, p_vf);
2969
2970         if (p_vf->state != VF_STOPPED && p_vf->state != VF_FREE) {
2971                 /* Stopping the VF */
2972                 rc = ecore_sp_vf_stop(p_hwfn, p_vf->concrete_fid,
2973                                       p_vf->opaque_fid);
2974
2975                 if (rc != ECORE_SUCCESS) {
2976                         DP_ERR(p_hwfn, "ecore_sp_vf_stop returned error %d\n",
2977                                rc);
2978                         status = PFVF_STATUS_FAILURE;
2979                 }
2980
2981                 p_vf->state = VF_STOPPED;
2982         }
2983
2984         ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf, CHANNEL_TLV_RELEASE,
2985                                length, status);
2986 }
2987
2988 static enum _ecore_status_t
2989 ecore_iov_vf_flr_poll_dorq(struct ecore_hwfn *p_hwfn,
2990                            struct ecore_vf_info *p_vf, struct ecore_ptt *p_ptt)
2991 {
2992         int cnt;
2993         u32 val;
2994
2995         ecore_fid_pretend(p_hwfn, p_ptt, (u16)p_vf->concrete_fid);
2996
2997         for (cnt = 0; cnt < 50; cnt++) {
2998                 val = ecore_rd(p_hwfn, p_ptt, DORQ_REG_VF_USAGE_CNT);
2999                 if (!val)
3000                         break;
3001                 OSAL_MSLEEP(20);
3002         }
3003         ecore_fid_pretend(p_hwfn, p_ptt, (u16)p_hwfn->hw_info.concrete_fid);
3004
3005         if (cnt == 50) {
3006                 DP_ERR(p_hwfn,
3007                        "VF[%d] - dorq failed to cleanup [usage 0x%08x]\n",
3008                        p_vf->abs_vf_id, val);
3009                 return ECORE_TIMEOUT;
3010         }
3011
3012         return ECORE_SUCCESS;
3013 }
3014
3015 static enum _ecore_status_t
3016 ecore_iov_vf_flr_poll_pbf(struct ecore_hwfn *p_hwfn,
3017                           struct ecore_vf_info *p_vf, struct ecore_ptt *p_ptt)
3018 {
3019         u32 cons[MAX_NUM_VOQS], distance[MAX_NUM_VOQS];
3020         int i, cnt;
3021
3022         /* Read initial consumers & producers */
3023         for (i = 0; i < MAX_NUM_VOQS; i++) {
3024                 u32 prod;
3025
3026                 cons[i] = ecore_rd(p_hwfn, p_ptt,
3027                                    PBF_REG_NUM_BLOCKS_ALLOCATED_CONS_VOQ0 +
3028                                    i * 0x40);
3029                 prod = ecore_rd(p_hwfn, p_ptt,
3030                                 PBF_REG_NUM_BLOCKS_ALLOCATED_PROD_VOQ0 +
3031                                 i * 0x40);
3032                 distance[i] = prod - cons[i];
3033         }
3034
3035         /* Wait for consumers to pass the producers */
3036         i = 0;
3037         for (cnt = 0; cnt < 50; cnt++) {
3038                 for (; i < MAX_NUM_VOQS; i++) {
3039                         u32 tmp;
3040
3041                         tmp = ecore_rd(p_hwfn, p_ptt,
3042                                        PBF_REG_NUM_BLOCKS_ALLOCATED_CONS_VOQ0 +
3043                                        i * 0x40);
3044                         if (distance[i] > tmp - cons[i])
3045                                 break;
3046                 }
3047
3048                 if (i == MAX_NUM_VOQS)
3049                         break;
3050
3051                 OSAL_MSLEEP(20);
3052         }
3053
3054         if (cnt == 50) {
3055                 DP_ERR(p_hwfn, "VF[%d] - pbf polling failed on VOQ %d\n",
3056                        p_vf->abs_vf_id, i);
3057                 return ECORE_TIMEOUT;
3058         }
3059
3060         return ECORE_SUCCESS;
3061 }
3062
3063 static enum _ecore_status_t ecore_iov_vf_flr_poll(struct ecore_hwfn *p_hwfn,
3064                                                   struct ecore_vf_info *p_vf,
3065                                                   struct ecore_ptt *p_ptt)
3066 {
3067         enum _ecore_status_t rc;
3068
3069         /* TODO - add SRC and TM polling once we add storage IOV */
3070
3071         rc = ecore_iov_vf_flr_poll_dorq(p_hwfn, p_vf, p_ptt);
3072         if (rc)
3073                 return rc;
3074
3075         rc = ecore_iov_vf_flr_poll_pbf(p_hwfn, p_vf, p_ptt);
3076         if (rc)
3077                 return rc;
3078
3079         return ECORE_SUCCESS;
3080 }
3081
3082 static enum _ecore_status_t
3083 ecore_iov_execute_vf_flr_cleanup(struct ecore_hwfn *p_hwfn,
3084                                  struct ecore_ptt *p_ptt,
3085                                  u16 rel_vf_id, u32 *ack_vfs)
3086 {
3087         struct ecore_vf_info *p_vf;
3088         enum _ecore_status_t rc = ECORE_SUCCESS;
3089
3090         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, false);
3091         if (!p_vf)
3092                 return ECORE_SUCCESS;
3093
3094         if (p_hwfn->pf_iov_info->pending_flr[rel_vf_id / 64] &
3095             (1ULL << (rel_vf_id % 64))) {
3096                 u16 vfid = p_vf->abs_vf_id;
3097
3098                 /* TODO - should we lock channel? */
3099
3100                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3101                            "VF[%d] - Handling FLR\n", vfid);
3102
3103                 ecore_iov_vf_cleanup(p_hwfn, p_vf);
3104
3105                 /* If VF isn't active, no need for anything but SW */
3106                 if (!p_vf->b_init)
3107                         goto cleanup;
3108
3109                 /* TODO - what to do in case of failure? */
3110                 rc = ecore_iov_vf_flr_poll(p_hwfn, p_vf, p_ptt);
3111                 if (rc != ECORE_SUCCESS)
3112                         goto cleanup;
3113
3114                 rc = ecore_final_cleanup(p_hwfn, p_ptt, vfid, true);
3115                 if (rc) {
3116                         /* TODO - what's now? What a mess.... */
3117                         DP_ERR(p_hwfn, "Failed handle FLR of VF[%d]\n", vfid);
3118                         return rc;
3119                 }
3120
3121                 /* Workaround to make VF-PF channel ready, as FW
3122                  * doesn't do that as a part of FLR.
3123                  */
3124                 REG_WR(p_hwfn,
3125                        GTT_BAR0_MAP_REG_USDM_RAM +
3126                        USTORM_VF_PF_CHANNEL_READY_OFFSET(vfid), 1);
3127
3128                 /* VF_STOPPED has to be set only after final cleanup
3129                  * but prior to re-enabling the VF.
3130                  */
3131                 p_vf->state = VF_STOPPED;
3132
3133                 rc = ecore_iov_enable_vf_access(p_hwfn, p_ptt, p_vf);
3134                 if (rc) {
3135                         /* TODO - again, a mess... */
3136                         DP_ERR(p_hwfn, "Failed to re-enable VF[%d] acces\n",
3137                                vfid);
3138                         return rc;
3139                 }
3140 cleanup:
3141                 /* Mark VF for ack and clean pending state */
3142                 if (p_vf->state == VF_RESET)
3143                         p_vf->state = VF_STOPPED;
3144                 ack_vfs[vfid / 32] |= (1 << (vfid % 32));
3145                 p_hwfn->pf_iov_info->pending_flr[rel_vf_id / 64] &=
3146                     ~(1ULL << (rel_vf_id % 64));
3147                 p_hwfn->pf_iov_info->pending_events[rel_vf_id / 64] &=
3148                     ~(1ULL << (rel_vf_id % 64));
3149         }
3150
3151         return rc;
3152 }
3153
3154 enum _ecore_status_t ecore_iov_vf_flr_cleanup(struct ecore_hwfn *p_hwfn,
3155                                               struct ecore_ptt *p_ptt)
3156 {
3157         u32 ack_vfs[VF_MAX_STATIC / 32];
3158         enum _ecore_status_t rc = ECORE_SUCCESS;
3159         u16 i;
3160
3161         OSAL_MEMSET(ack_vfs, 0, sizeof(u32) * (VF_MAX_STATIC / 32));
3162
3163         /* Since BRB <-> PRS interface can't be tested as part of the flr
3164          * polling due to HW limitations, simply sleep a bit. And since
3165          * there's no need to wait per-vf, do it before looping.
3166          */
3167         OSAL_MSLEEP(100);
3168
3169         for (i = 0; i < p_hwfn->p_dev->p_iov_info->total_vfs; i++)
3170                 ecore_iov_execute_vf_flr_cleanup(p_hwfn, p_ptt, i, ack_vfs);
3171
3172         rc = ecore_mcp_ack_vf_flr(p_hwfn, p_ptt, ack_vfs);
3173         return rc;
3174 }
3175
3176 enum _ecore_status_t
3177 ecore_iov_single_vf_flr_cleanup(struct ecore_hwfn *p_hwfn,
3178                                 struct ecore_ptt *p_ptt, u16 rel_vf_id)
3179 {
3180         u32 ack_vfs[VF_MAX_STATIC / 32];
3181         enum _ecore_status_t rc = ECORE_SUCCESS;
3182
3183         OSAL_MEMSET(ack_vfs, 0, sizeof(u32) * (VF_MAX_STATIC / 32));
3184
3185         /* Wait instead of polling the BRB <-> PRS interface */
3186         OSAL_MSLEEP(100);
3187
3188         ecore_iov_execute_vf_flr_cleanup(p_hwfn, p_ptt, rel_vf_id, ack_vfs);
3189
3190         rc = ecore_mcp_ack_vf_flr(p_hwfn, p_ptt, ack_vfs);
3191         return rc;
3192 }
3193
3194 bool ecore_iov_mark_vf_flr(struct ecore_hwfn *p_hwfn, u32 *p_disabled_vfs)
3195 {
3196         bool found = false;
3197         u16 i;
3198
3199         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV, "Marking FLR-ed VFs\n");
3200         for (i = 0; i < (VF_MAX_STATIC / 32); i++)
3201                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3202                            "[%08x,...,%08x]: %08x\n",
3203                            i * 32, (i + 1) * 32 - 1, p_disabled_vfs[i]);
3204
3205         if (!p_hwfn->p_dev->p_iov_info) {
3206                 DP_NOTICE(p_hwfn, true, "VF flr but no IOV\n");
3207                 return false;
3208         }
3209
3210         /* Mark VFs */
3211         for (i = 0; i < p_hwfn->p_dev->p_iov_info->total_vfs; i++) {
3212                 struct ecore_vf_info *p_vf;
3213                 u8 vfid;
3214
3215                 p_vf = ecore_iov_get_vf_info(p_hwfn, i, false);
3216                 if (!p_vf)
3217                         continue;
3218
3219                 vfid = p_vf->abs_vf_id;
3220                 if ((1 << (vfid % 32)) & p_disabled_vfs[vfid / 32]) {
3221                         u64 *p_flr = p_hwfn->pf_iov_info->pending_flr;
3222                         u16 rel_vf_id = p_vf->relative_vf_id;
3223
3224                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3225                                    "VF[%d] [rel %d] got FLR-ed\n",
3226                                    vfid, rel_vf_id);
3227
3228                         p_vf->state = VF_RESET;
3229
3230                         /* No need to lock here, since pending_flr should
3231                          * only change here and before ACKing MFw. Since
3232                          * MFW will not trigger an additional attention for
3233                          * VF flr until ACKs, we're safe.
3234                          */
3235                         p_flr[rel_vf_id / 64] |= 1ULL << (rel_vf_id % 64);
3236                         found = true;
3237                 }
3238         }
3239
3240         return found;
3241 }
3242
3243 void ecore_iov_get_link(struct ecore_hwfn *p_hwfn,
3244                         u16 vfid,
3245                         struct ecore_mcp_link_params *p_params,
3246                         struct ecore_mcp_link_state *p_link,
3247                         struct ecore_mcp_link_capabilities *p_caps)
3248 {
3249         struct ecore_vf_info *p_vf = ecore_iov_get_vf_info(p_hwfn, vfid, false);
3250         struct ecore_bulletin_content *p_bulletin;
3251
3252         if (!p_vf)
3253                 return;
3254
3255         p_bulletin = p_vf->bulletin.p_virt;
3256
3257         if (p_params)
3258                 __ecore_vf_get_link_params(p_hwfn, p_params, p_bulletin);
3259         if (p_link)
3260                 __ecore_vf_get_link_state(p_hwfn, p_link, p_bulletin);
3261         if (p_caps)
3262                 __ecore_vf_get_link_caps(p_hwfn, p_caps, p_bulletin);
3263 }
3264
3265 void ecore_iov_process_mbx_req(struct ecore_hwfn *p_hwfn,
3266                                struct ecore_ptt *p_ptt, int vfid)
3267 {
3268         struct ecore_iov_vf_mbx *mbx;
3269         struct ecore_vf_info *p_vf;
3270
3271         p_vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3272         if (!p_vf)
3273                 return;
3274
3275         mbx = &p_vf->vf_mbx;
3276
3277         /* ecore_iov_process_mbx_request */
3278         DP_VERBOSE(p_hwfn,
3279                    ECORE_MSG_IOV,
3280                    "VF[%02x]: Processing mailbox message\n", p_vf->abs_vf_id);
3281
3282         mbx->first_tlv = mbx->req_virt->first_tlv;
3283
3284         OSAL_IOV_VF_MSG_TYPE(p_hwfn,
3285                              p_vf->relative_vf_id,
3286                              mbx->first_tlv.tl.type);
3287
3288         /* Lock the per vf op mutex and note the locker's identity.
3289          * The unlock will take place in mbx response.
3290          */
3291         ecore_iov_lock_vf_pf_channel(p_hwfn,
3292                                      p_vf, mbx->first_tlv.tl.type);
3293
3294         /* check if tlv type is known */
3295         if (ecore_iov_tlv_supported(mbx->first_tlv.tl.type) &&
3296             !p_vf->b_malicious) {
3297                 /* switch on the opcode */
3298                 switch (mbx->first_tlv.tl.type) {
3299                 case CHANNEL_TLV_ACQUIRE:
3300                         ecore_iov_vf_mbx_acquire(p_hwfn, p_ptt, p_vf);
3301                         break;
3302                 case CHANNEL_TLV_VPORT_START:
3303                         ecore_iov_vf_mbx_start_vport(p_hwfn, p_ptt, p_vf);
3304                         break;
3305                 case CHANNEL_TLV_VPORT_TEARDOWN:
3306                         ecore_iov_vf_mbx_stop_vport(p_hwfn, p_ptt, p_vf);
3307                         break;
3308                 case CHANNEL_TLV_START_RXQ:
3309                         ecore_iov_vf_mbx_start_rxq(p_hwfn, p_ptt, p_vf);
3310                         break;
3311                 case CHANNEL_TLV_START_TXQ:
3312                         ecore_iov_vf_mbx_start_txq(p_hwfn, p_ptt, p_vf);
3313                         break;
3314                 case CHANNEL_TLV_STOP_RXQS:
3315                         ecore_iov_vf_mbx_stop_rxqs(p_hwfn, p_ptt, p_vf);
3316                         break;
3317                 case CHANNEL_TLV_STOP_TXQS:
3318                         ecore_iov_vf_mbx_stop_txqs(p_hwfn, p_ptt, p_vf);
3319                         break;
3320                 case CHANNEL_TLV_UPDATE_RXQ:
3321                         ecore_iov_vf_mbx_update_rxqs(p_hwfn, p_ptt, p_vf);
3322                         break;
3323                 case CHANNEL_TLV_VPORT_UPDATE:
3324                         ecore_iov_vf_mbx_vport_update(p_hwfn, p_ptt, p_vf);
3325                         break;
3326                 case CHANNEL_TLV_UCAST_FILTER:
3327                         ecore_iov_vf_mbx_ucast_filter(p_hwfn, p_ptt, p_vf);
3328                         break;
3329                 case CHANNEL_TLV_CLOSE:
3330                         ecore_iov_vf_mbx_close(p_hwfn, p_ptt, p_vf);
3331                         break;
3332                 case CHANNEL_TLV_INT_CLEANUP:
3333                         ecore_iov_vf_mbx_int_cleanup(p_hwfn, p_ptt, p_vf);
3334                         break;
3335                 case CHANNEL_TLV_RELEASE:
3336                         ecore_iov_vf_mbx_release(p_hwfn, p_ptt, p_vf);
3337                         break;
3338                 }
3339         } else if (ecore_iov_tlv_supported(mbx->first_tlv.tl.type)) {
3340                 /* If we've received a message from a VF we consider malicious
3341                  * we ignore the messasge unless it's one for RELEASE, in which
3342                  * case we'll let it have the benefit of doubt, allowing the
3343                  * next loaded driver to start again.
3344                  */
3345                 if (mbx->first_tlv.tl.type == CHANNEL_TLV_RELEASE) {
3346                         /* TODO - initiate FLR, remove malicious indication */
3347                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3348                                    "VF [%02x] - considered malicious, but wanted to RELEASE. TODO\n",
3349                                    p_vf->abs_vf_id);
3350                 } else {
3351                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3352                                    "VF [%02x] - considered malicious; Ignoring TLV [%04x]\n",
3353                                    p_vf->abs_vf_id, mbx->first_tlv.tl.type);
3354                 }
3355
3356                 ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf,
3357                                        mbx->first_tlv.tl.type,
3358                                        sizeof(struct pfvf_def_resp_tlv),
3359                                        PFVF_STATUS_MALICIOUS);
3360         } else {
3361                 /* unknown TLV - this may belong to a VF driver from the future
3362                  * - a version written after this PF driver was written, which
3363                  * supports features unknown as of yet. Too bad since we don't
3364                  * support them. Or this may be because someone wrote a crappy
3365                  * VF driver and is sending garbage over the channel.
3366                  */
3367                 DP_NOTICE(p_hwfn, false,
3368                           "VF[%02x]: unknown TLV. type %04x length %04x"
3369                           " padding %08x reply address %lu\n",
3370                           p_vf->abs_vf_id,
3371                           mbx->first_tlv.tl.type,
3372                           mbx->first_tlv.tl.length,
3373                           mbx->first_tlv.padding,
3374                           (unsigned long)mbx->first_tlv.reply_address);
3375
3376                 /* Try replying in case reply address matches the acquisition's
3377                  * posted address.
3378                  */
3379                 if (p_vf->acquire.first_tlv.reply_address &&
3380                     (mbx->first_tlv.reply_address ==
3381                      p_vf->acquire.first_tlv.reply_address))
3382                         ecore_iov_prepare_resp(p_hwfn, p_ptt, p_vf,
3383                                                mbx->first_tlv.tl.type,
3384                                                sizeof(struct pfvf_def_resp_tlv),
3385                                                PFVF_STATUS_NOT_SUPPORTED);
3386                 else
3387                         DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3388                                    "VF[%02x]: Can't respond to TLV -"
3389                                    " no valid reply address\n",
3390                                    p_vf->abs_vf_id);
3391         }
3392
3393         ecore_iov_unlock_vf_pf_channel(p_hwfn, p_vf,
3394                                        mbx->first_tlv.tl.type);
3395
3396 #ifdef CONFIG_ECORE_SW_CHANNEL
3397         mbx->sw_mbx.mbx_state = VF_PF_RESPONSE_READY;
3398         mbx->sw_mbx.response_offset = 0;
3399 #endif
3400 }
3401
3402 void ecore_iov_pf_add_pending_events(struct ecore_hwfn *p_hwfn, u8 vfid)
3403 {
3404         u64 add_bit = 1ULL << (vfid % 64);
3405
3406         /* TODO - add locking mechanisms [no atomics in ecore, so we can't
3407         * add the lock inside the ecore_pf_iov struct].
3408         */
3409         p_hwfn->pf_iov_info->pending_events[vfid / 64] |= add_bit;
3410 }
3411
3412 void ecore_iov_pf_get_and_clear_pending_events(struct ecore_hwfn *p_hwfn,
3413                                                u64 *events)
3414 {
3415         u64 *p_pending_events = p_hwfn->pf_iov_info->pending_events;
3416
3417         /* TODO - Take a lock */
3418         OSAL_MEMCPY(events, p_pending_events,
3419                     sizeof(u64) * ECORE_VF_ARRAY_LENGTH);
3420         OSAL_MEMSET(p_pending_events, 0,
3421                     sizeof(u64) * ECORE_VF_ARRAY_LENGTH);
3422 }
3423
3424 static struct ecore_vf_info *
3425 ecore_sriov_get_vf_from_absid(struct ecore_hwfn *p_hwfn, u16 abs_vfid)
3426 {
3427         u8 min = (u8)p_hwfn->p_dev->p_iov_info->first_vf_in_pf;
3428
3429         if (!_ecore_iov_pf_sanity_check(p_hwfn, (int)abs_vfid - min, false)) {
3430                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3431                            "Got indication for VF [abs 0x%08x] that cannot be"
3432                            " handled by PF\n",
3433                            abs_vfid);
3434                 return OSAL_NULL;
3435         }
3436
3437         return &p_hwfn->pf_iov_info->vfs_array[(u8)abs_vfid - min];
3438 }
3439
3440 static enum _ecore_status_t ecore_sriov_vfpf_msg(struct ecore_hwfn *p_hwfn,
3441                                                  u16 abs_vfid,
3442                                                  struct regpair *vf_msg)
3443 {
3444         struct ecore_vf_info *p_vf = ecore_sriov_get_vf_from_absid(p_hwfn,
3445                                                                    abs_vfid);
3446
3447         if (!p_vf)
3448                 return ECORE_SUCCESS;
3449
3450         /* List the physical address of the request so that handler
3451          * could later on copy the message from it.
3452          */
3453         p_vf->vf_mbx.pending_req = (((u64)vf_msg->hi) << 32) | vf_msg->lo;
3454
3455         return OSAL_PF_VF_MSG(p_hwfn, p_vf->relative_vf_id);
3456 }
3457
3458 static void ecore_sriov_vfpf_malicious(struct ecore_hwfn *p_hwfn,
3459                                        struct malicious_vf_eqe_data *p_data)
3460 {
3461         struct ecore_vf_info *p_vf;
3462
3463         p_vf = ecore_sriov_get_vf_from_absid(p_hwfn, p_data->vfId);
3464
3465         if (!p_vf)
3466                 return;
3467
3468         DP_INFO(p_hwfn,
3469                 "VF [%d] - Malicious behavior [%02x]\n",
3470                 p_vf->abs_vf_id, p_data->errId);
3471
3472         p_vf->b_malicious = true;
3473
3474         OSAL_PF_VF_MALICIOUS(p_hwfn, p_vf->relative_vf_id);
3475 }
3476
3477 enum _ecore_status_t ecore_sriov_eqe_event(struct ecore_hwfn *p_hwfn,
3478                                            u8 opcode,
3479                                            __le16 echo,
3480                                            union event_ring_data *data)
3481 {
3482         switch (opcode) {
3483         case COMMON_EVENT_VF_PF_CHANNEL:
3484                 return ecore_sriov_vfpf_msg(p_hwfn, OSAL_LE16_TO_CPU(echo),
3485                                             &data->vf_pf_channel.msg_addr);
3486         case COMMON_EVENT_VF_FLR:
3487                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3488                            "VF-FLR is still not supported\n");
3489                 return ECORE_SUCCESS;
3490         case COMMON_EVENT_MALICIOUS_VF:
3491                 ecore_sriov_vfpf_malicious(p_hwfn, &data->malicious_vf);
3492                 return ECORE_SUCCESS;
3493         default:
3494                 DP_INFO(p_hwfn->p_dev, "Unknown sriov eqe event 0x%02x\n",
3495                         opcode);
3496                 return ECORE_INVAL;
3497         }
3498 }
3499
3500 bool ecore_iov_is_vf_pending_flr(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3501 {
3502         return !!(p_hwfn->pf_iov_info->pending_flr[rel_vf_id / 64] &
3503                    (1ULL << (rel_vf_id % 64)));
3504 }
3505
3506 u16 ecore_iov_get_next_active_vf(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3507 {
3508         struct ecore_hw_sriov_info *p_iov = p_hwfn->p_dev->p_iov_info;
3509         u16 i;
3510
3511         if (!p_iov)
3512                 goto out;
3513
3514         for (i = rel_vf_id; i < p_iov->total_vfs; i++)
3515                 if (ecore_iov_is_valid_vfid(p_hwfn, rel_vf_id, true, false))
3516                         return i;
3517
3518 out:
3519         return E4_MAX_NUM_VFS;
3520 }
3521
3522 enum _ecore_status_t ecore_iov_copy_vf_msg(struct ecore_hwfn *p_hwfn,
3523                                            struct ecore_ptt *ptt, int vfid)
3524 {
3525         struct ecore_dmae_params params;
3526         struct ecore_vf_info *vf_info;
3527
3528         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3529         if (!vf_info)
3530                 return ECORE_INVAL;
3531
3532         OSAL_MEMSET(&params, 0, sizeof(struct ecore_dmae_params));
3533         params.flags = ECORE_DMAE_FLAG_VF_SRC | ECORE_DMAE_FLAG_COMPLETION_DST;
3534         params.src_vfid = vf_info->abs_vf_id;
3535
3536         if (ecore_dmae_host2host(p_hwfn, ptt,
3537                                  vf_info->vf_mbx.pending_req,
3538                                  vf_info->vf_mbx.req_phys,
3539                                  sizeof(union vfpf_tlvs) / 4, &params)) {
3540                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3541                            "Failed to copy message from VF 0x%02x\n", vfid);
3542
3543                 return ECORE_IO;
3544         }
3545
3546         return ECORE_SUCCESS;
3547 }
3548
3549 void ecore_iov_bulletin_set_forced_mac(struct ecore_hwfn *p_hwfn,
3550                                        u8 *mac, int vfid)
3551 {
3552         struct ecore_vf_info *vf_info;
3553         u64 feature;
3554
3555         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3556         if (!vf_info) {
3557                 DP_NOTICE(p_hwfn->p_dev, true,
3558                           "Can not set forced MAC, invalid vfid [%d]\n", vfid);
3559                 return;
3560         }
3561         if (vf_info->b_malicious) {
3562                 DP_NOTICE(p_hwfn->p_dev, false,
3563                           "Can't set forced MAC to malicious VF [%d]\n",
3564                           vfid);
3565                 return;
3566         }
3567
3568         feature = 1 << MAC_ADDR_FORCED;
3569         OSAL_MEMCPY(vf_info->bulletin.p_virt->mac, mac, ETH_ALEN);
3570
3571         vf_info->bulletin.p_virt->valid_bitmap |= feature;
3572         /* Forced MAC will disable MAC_ADDR */
3573         vf_info->bulletin.p_virt->valid_bitmap &=
3574             ~(1 << VFPF_BULLETIN_MAC_ADDR);
3575
3576         ecore_iov_configure_vport_forced(p_hwfn, vf_info, feature);
3577 }
3578
3579 enum _ecore_status_t ecore_iov_bulletin_set_mac(struct ecore_hwfn *p_hwfn,
3580                                                 u8 *mac, int vfid)
3581 {
3582         struct ecore_vf_info *vf_info;
3583         u64 feature;
3584
3585         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3586         if (!vf_info) {
3587                 DP_NOTICE(p_hwfn->p_dev, true,
3588                           "Can not set MAC, invalid vfid [%d]\n", vfid);
3589                 return ECORE_INVAL;
3590         }
3591         if (vf_info->b_malicious) {
3592                 DP_NOTICE(p_hwfn->p_dev, false,
3593                           "Can't set MAC to malicious VF [%d]\n",
3594                           vfid);
3595                 return ECORE_INVAL;
3596         }
3597
3598         if (vf_info->bulletin.p_virt->valid_bitmap & (1 << MAC_ADDR_FORCED)) {
3599                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3600                            "Can not set MAC, Forced MAC is configured\n");
3601                 return ECORE_INVAL;
3602         }
3603
3604         feature = 1 << VFPF_BULLETIN_MAC_ADDR;
3605         OSAL_MEMCPY(vf_info->bulletin.p_virt->mac, mac, ETH_ALEN);
3606
3607         vf_info->bulletin.p_virt->valid_bitmap |= feature;
3608
3609         return ECORE_SUCCESS;
3610 }
3611
3612 enum _ecore_status_t
3613 ecore_iov_bulletin_set_forced_untagged_default(struct ecore_hwfn *p_hwfn,
3614                                                bool b_untagged_only, int vfid)
3615 {
3616         struct ecore_vf_info *vf_info;
3617         u64 feature;
3618
3619         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3620         if (!vf_info) {
3621                 DP_NOTICE(p_hwfn->p_dev, true,
3622                           "Can not set untagged default, invalid vfid [%d]\n",
3623                           vfid);
3624                 return ECORE_INVAL;
3625         }
3626         if (vf_info->b_malicious) {
3627                 DP_NOTICE(p_hwfn->p_dev, false,
3628                           "Can't set untagged default to malicious VF [%d]\n",
3629                           vfid);
3630                 return ECORE_INVAL;
3631         }
3632
3633         /* Since this is configurable only during vport-start, don't take it
3634          * if we're past that point.
3635          */
3636         if (vf_info->state == VF_ENABLED) {
3637                 DP_VERBOSE(p_hwfn, ECORE_MSG_IOV,
3638                            "Can't support untagged change for vfid[%d] -"
3639                            " VF is already active\n",
3640                            vfid);
3641                 return ECORE_INVAL;
3642         }
3643
3644         /* Set configuration; This will later be taken into account during the
3645          * VF initialization.
3646          */
3647         feature = (1 << VFPF_BULLETIN_UNTAGGED_DEFAULT) |
3648             (1 << VFPF_BULLETIN_UNTAGGED_DEFAULT_FORCED);
3649         vf_info->bulletin.p_virt->valid_bitmap |= feature;
3650
3651         vf_info->bulletin.p_virt->default_only_untagged = b_untagged_only ? 1
3652             : 0;
3653
3654         return ECORE_SUCCESS;
3655 }
3656
3657 void ecore_iov_get_vfs_opaque_fid(struct ecore_hwfn *p_hwfn, int vfid,
3658                                   u16 *opaque_fid)
3659 {
3660         struct ecore_vf_info *vf_info;
3661
3662         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3663         if (!vf_info)
3664                 return;
3665
3666         *opaque_fid = vf_info->opaque_fid;
3667 }
3668
3669 void ecore_iov_get_vfs_vport_id(struct ecore_hwfn *p_hwfn, int vfid,
3670                                 u8 *p_vort_id)
3671 {
3672         struct ecore_vf_info *vf_info;
3673
3674         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3675         if (!vf_info)
3676                 return;
3677
3678         *p_vort_id = vf_info->vport_id;
3679 }
3680
3681 void ecore_iov_bulletin_set_forced_vlan(struct ecore_hwfn *p_hwfn,
3682                                         u16 pvid, int vfid)
3683 {
3684         struct ecore_vf_info *vf_info;
3685         u64 feature;
3686
3687         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3688         if (!vf_info) {
3689                 DP_NOTICE(p_hwfn->p_dev, true,
3690                           "Can not set forced MAC, invalid vfid [%d]\n",
3691                           vfid);
3692                 return;
3693         }
3694         if (vf_info->b_malicious) {
3695                 DP_NOTICE(p_hwfn->p_dev, false,
3696                           "Can't set forced vlan to malicious VF [%d]\n",
3697                           vfid);
3698                 return;
3699         }
3700
3701         feature = 1 << VLAN_ADDR_FORCED;
3702         vf_info->bulletin.p_virt->pvid = pvid;
3703         if (pvid)
3704                 vf_info->bulletin.p_virt->valid_bitmap |= feature;
3705         else
3706                 vf_info->bulletin.p_virt->valid_bitmap &= ~feature;
3707
3708         ecore_iov_configure_vport_forced(p_hwfn, vf_info, feature);
3709 }
3710
3711 bool ecore_iov_vf_has_vport_instance(struct ecore_hwfn *p_hwfn, int vfid)
3712 {
3713         struct ecore_vf_info *p_vf_info;
3714
3715         p_vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3716         if (!p_vf_info)
3717                 return false;
3718
3719         return !!p_vf_info->vport_instance;
3720 }
3721
3722 bool ecore_iov_is_vf_stopped(struct ecore_hwfn *p_hwfn, int vfid)
3723 {
3724         struct ecore_vf_info *p_vf_info;
3725
3726         p_vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3727         if (!p_vf_info)
3728                 return true;
3729
3730         return p_vf_info->state == VF_STOPPED;
3731 }
3732
3733 bool ecore_iov_spoofchk_get(struct ecore_hwfn *p_hwfn, int vfid)
3734 {
3735         struct ecore_vf_info *vf_info;
3736
3737         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3738         if (!vf_info)
3739                 return false;
3740
3741         return vf_info->spoof_chk;
3742 }
3743
3744 enum _ecore_status_t ecore_iov_spoofchk_set(struct ecore_hwfn *p_hwfn,
3745                                             int vfid, bool val)
3746 {
3747         struct ecore_vf_info *vf;
3748         enum _ecore_status_t rc = ECORE_INVAL;
3749
3750         if (!ecore_iov_pf_sanity_check(p_hwfn, vfid)) {
3751                 DP_NOTICE(p_hwfn, true,
3752                           "SR-IOV sanity check failed, can't set spoofchk\n");
3753                 goto out;
3754         }
3755
3756         vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3757         if (!vf)
3758                 goto out;
3759
3760         if (!ecore_iov_vf_has_vport_instance(p_hwfn, vfid)) {
3761                 /* After VF VPORT start PF will configure spoof check */
3762                 vf->req_spoofchk_val = val;
3763                 rc = ECORE_SUCCESS;
3764                 goto out;
3765         }
3766
3767         rc = __ecore_iov_spoofchk_set(p_hwfn, vf, val);
3768
3769 out:
3770         return rc;
3771 }
3772
3773 u8 ecore_iov_vf_chains_per_pf(struct ecore_hwfn *p_hwfn)
3774 {
3775         u8 max_chains_per_vf = p_hwfn->hw_info.max_chains_per_vf;
3776
3777         max_chains_per_vf = (max_chains_per_vf) ? max_chains_per_vf
3778             : ECORE_MAX_VF_CHAINS_PER_PF;
3779
3780         return max_chains_per_vf;
3781 }
3782
3783 void ecore_iov_get_vf_req_virt_mbx_params(struct ecore_hwfn *p_hwfn,
3784                                           u16 rel_vf_id,
3785                                           void **pp_req_virt_addr,
3786                                           u16 *p_req_virt_size)
3787 {
3788         struct ecore_vf_info *vf_info =
3789             ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3790
3791         if (!vf_info)
3792                 return;
3793
3794         if (pp_req_virt_addr)
3795                 *pp_req_virt_addr = vf_info->vf_mbx.req_virt;
3796
3797         if (p_req_virt_size)
3798                 *p_req_virt_size = sizeof(*vf_info->vf_mbx.req_virt);
3799 }
3800
3801 void ecore_iov_get_vf_reply_virt_mbx_params(struct ecore_hwfn *p_hwfn,
3802                                             u16 rel_vf_id,
3803                                             void **pp_reply_virt_addr,
3804                                             u16 *p_reply_virt_size)
3805 {
3806         struct ecore_vf_info *vf_info =
3807             ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3808
3809         if (!vf_info)
3810                 return;
3811
3812         if (pp_reply_virt_addr)
3813                 *pp_reply_virt_addr = vf_info->vf_mbx.reply_virt;
3814
3815         if (p_reply_virt_size)
3816                 *p_reply_virt_size = sizeof(*vf_info->vf_mbx.reply_virt);
3817 }
3818
3819 #ifdef CONFIG_ECORE_SW_CHANNEL
3820 struct ecore_iov_sw_mbx *ecore_iov_get_vf_sw_mbx(struct ecore_hwfn *p_hwfn,
3821                                                  u16 rel_vf_id)
3822 {
3823         struct ecore_vf_info *vf_info =
3824             ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3825
3826         if (!vf_info)
3827                 return OSAL_NULL;
3828
3829         return &vf_info->vf_mbx.sw_mbx;
3830 }
3831 #endif
3832
3833 bool ecore_iov_is_valid_vfpf_msg_length(u32 length)
3834 {
3835         return (length >= sizeof(struct vfpf_first_tlv) &&
3836                 (length <= sizeof(union vfpf_tlvs)));
3837 }
3838
3839 u32 ecore_iov_pfvf_msg_length(void)
3840 {
3841         return sizeof(union pfvf_tlvs);
3842 }
3843
3844 u8 *ecore_iov_bulletin_get_forced_mac(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3845 {
3846         struct ecore_vf_info *p_vf;
3847
3848         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3849         if (!p_vf || !p_vf->bulletin.p_virt)
3850                 return OSAL_NULL;
3851
3852         if (!(p_vf->bulletin.p_virt->valid_bitmap & (1 << MAC_ADDR_FORCED)))
3853                 return OSAL_NULL;
3854
3855         return p_vf->bulletin.p_virt->mac;
3856 }
3857
3858 u16 ecore_iov_bulletin_get_forced_vlan(struct ecore_hwfn *p_hwfn,
3859                                        u16 rel_vf_id)
3860 {
3861         struct ecore_vf_info *p_vf;
3862
3863         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3864         if (!p_vf || !p_vf->bulletin.p_virt)
3865                 return 0;
3866
3867         if (!(p_vf->bulletin.p_virt->valid_bitmap & (1 << VLAN_ADDR_FORCED)))
3868                 return 0;
3869
3870         return p_vf->bulletin.p_virt->pvid;
3871 }
3872
3873 enum _ecore_status_t ecore_iov_configure_tx_rate(struct ecore_hwfn *p_hwfn,
3874                                                  struct ecore_ptt *p_ptt,
3875                                                  int vfid, int val)
3876 {
3877         struct ecore_vf_info *vf;
3878         u8 abs_vp_id = 0;
3879         enum _ecore_status_t rc;
3880
3881         vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3882
3883         if (!vf)
3884                 return ECORE_INVAL;
3885
3886         rc = ecore_fw_vport(p_hwfn, vf->vport_id, &abs_vp_id);
3887         if (rc != ECORE_SUCCESS)
3888                 return rc;
3889
3890         return ecore_init_vport_rl(p_hwfn, p_ptt, abs_vp_id, (u32)val);
3891 }
3892
3893 enum _ecore_status_t ecore_iov_configure_min_tx_rate(struct ecore_dev *p_dev,
3894                                                      int vfid, u32 rate)
3895 {
3896         struct ecore_vf_info *vf;
3897         u8 vport_id;
3898         int i;
3899
3900         for_each_hwfn(p_dev, i) {
3901                 struct ecore_hwfn *p_hwfn = &p_dev->hwfns[i];
3902
3903                 if (!ecore_iov_pf_sanity_check(p_hwfn, vfid)) {
3904                         DP_NOTICE(p_hwfn, true,
3905                                   "SR-IOV sanity check failed,"
3906                                   " can't set min rate\n");
3907                         return ECORE_INVAL;
3908                 }
3909         }
3910
3911         vf = ecore_iov_get_vf_info(ECORE_LEADING_HWFN(p_dev), (u16)vfid, true);
3912         vport_id = vf->vport_id;
3913
3914         return ecore_configure_vport_wfq(p_dev, vport_id, rate);
3915 }
3916
3917 enum _ecore_status_t ecore_iov_get_vf_stats(struct ecore_hwfn *p_hwfn,
3918                                             struct ecore_ptt *p_ptt,
3919                                             int vfid,
3920                                             struct ecore_eth_stats *p_stats)
3921 {
3922         struct ecore_vf_info *vf;
3923
3924         vf = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
3925         if (!vf)
3926                 return ECORE_INVAL;
3927
3928         if (vf->state != VF_ENABLED)
3929                 return ECORE_INVAL;
3930
3931         __ecore_get_vport_stats(p_hwfn, p_ptt, p_stats,
3932                                 vf->abs_vf_id + 0x10, false);
3933
3934         return ECORE_SUCCESS;
3935 }
3936
3937 u8 ecore_iov_get_vf_num_rxqs(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3938 {
3939         struct ecore_vf_info *p_vf;
3940
3941         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3942         if (!p_vf)
3943                 return 0;
3944
3945         return p_vf->num_rxqs;
3946 }
3947
3948 u8 ecore_iov_get_vf_num_active_rxqs(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3949 {
3950         struct ecore_vf_info *p_vf;
3951
3952         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3953         if (!p_vf)
3954                 return 0;
3955
3956         return p_vf->num_active_rxqs;
3957 }
3958
3959 void *ecore_iov_get_vf_ctx(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3960 {
3961         struct ecore_vf_info *p_vf;
3962
3963         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3964         if (!p_vf)
3965                 return OSAL_NULL;
3966
3967         return p_vf->ctx;
3968 }
3969
3970 u8 ecore_iov_get_vf_num_sbs(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3971 {
3972         struct ecore_vf_info *p_vf;
3973
3974         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3975         if (!p_vf)
3976                 return 0;
3977
3978         return p_vf->num_sbs;
3979 }
3980
3981 bool ecore_iov_is_vf_wait_for_acquire(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
3982 {
3983         struct ecore_vf_info *p_vf;
3984
3985         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3986         if (!p_vf)
3987                 return false;
3988
3989         return (p_vf->state == VF_FREE);
3990 }
3991
3992 bool ecore_iov_is_vf_acquired_not_initialized(struct ecore_hwfn *p_hwfn,
3993                                               u16 rel_vf_id)
3994 {
3995         struct ecore_vf_info *p_vf;
3996
3997         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
3998         if (!p_vf)
3999                 return false;
4000
4001         return (p_vf->state == VF_ACQUIRED);
4002 }
4003
4004 bool ecore_iov_is_vf_initialized(struct ecore_hwfn *p_hwfn, u16 rel_vf_id)
4005 {
4006         struct ecore_vf_info *p_vf;
4007
4008         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4009         if (!p_vf)
4010                 return false;
4011
4012         return (p_vf->state == VF_ENABLED);
4013 }
4014
4015 bool ecore_iov_is_vf_started(struct ecore_hwfn *p_hwfn,
4016                              u16 rel_vf_id)
4017 {
4018         struct ecore_vf_info *p_vf;
4019
4020         p_vf = ecore_iov_get_vf_info(p_hwfn, rel_vf_id, true);
4021         if (!p_vf)
4022                 return false;
4023
4024         return (p_vf->state != VF_FREE && p_vf->state != VF_STOPPED);
4025 }
4026
4027 enum _ecore_status_t
4028 ecore_iov_get_vf_min_rate(struct ecore_hwfn *p_hwfn, int vfid)
4029 {
4030         struct ecore_wfq_data *vf_vp_wfq;
4031         struct ecore_vf_info *vf_info;
4032
4033         vf_info = ecore_iov_get_vf_info(p_hwfn, (u16)vfid, true);
4034         if (!vf_info)
4035                 return 0;
4036
4037         vf_vp_wfq = &p_hwfn->qm_info.wfq_data[vf_info->vport_id];
4038
4039         if (vf_vp_wfq->configured)
4040                 return vf_vp_wfq->min_speed;
4041         else
4042                 return 0;
4043 }