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