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