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