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