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