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