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