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