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