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