• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 #include "i40e.h"
5 
6 /*********************notification routines***********************/
7 
8 /**
9  * i40e_vc_vf_broadcast
10  * @pf: pointer to the PF structure
11  * @v_opcode: operation code
12  * @v_retval: return value
13  * @msg: pointer to the msg buffer
14  * @msglen: msg length
15  *
16  * send a message to all VFs on a given PF
17  **/
i40e_vc_vf_broadcast(struct i40e_pf * pf,enum virtchnl_ops v_opcode,i40e_status v_retval,u8 * msg,u16 msglen)18 static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
19 				 enum virtchnl_ops v_opcode,
20 				 i40e_status v_retval, u8 *msg,
21 				 u16 msglen)
22 {
23 	struct i40e_hw *hw = &pf->hw;
24 	struct i40e_vf *vf = pf->vf;
25 	int i;
26 
27 	for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
28 		int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
29 		/* Not all vfs are enabled so skip the ones that are not */
30 		if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
31 		    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
32 			continue;
33 
34 		/* Ignore return value on purpose - a given VF may fail, but
35 		 * we need to keep going and send to all of them
36 		 */
37 		i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
38 				       msg, msglen, NULL);
39 	}
40 }
41 
42 /**
43  * i40e_vc_notify_vf_link_state
44  * @vf: pointer to the VF structure
45  *
46  * send a link status message to a single VF
47  **/
i40e_vc_notify_vf_link_state(struct i40e_vf * vf)48 static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
49 {
50 	struct virtchnl_pf_event pfe;
51 	struct i40e_pf *pf = vf->pf;
52 	struct i40e_hw *hw = &pf->hw;
53 	struct i40e_link_status *ls = &pf->hw.phy.link_info;
54 	int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
55 
56 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
57 	pfe.severity = PF_EVENT_SEVERITY_INFO;
58 	if (vf->link_forced) {
59 		pfe.event_data.link_event.link_status = vf->link_up;
60 		pfe.event_data.link_event.link_speed =
61 			(vf->link_up ? VIRTCHNL_LINK_SPEED_40GB : 0);
62 	} else {
63 		pfe.event_data.link_event.link_status =
64 			ls->link_info & I40E_AQ_LINK_UP;
65 		pfe.event_data.link_event.link_speed =
66 			i40e_virtchnl_link_speed(ls->link_speed);
67 	}
68 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
69 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
70 }
71 
72 /**
73  * i40e_vc_notify_link_state
74  * @pf: pointer to the PF structure
75  *
76  * send a link status message to all VFs on a given PF
77  **/
i40e_vc_notify_link_state(struct i40e_pf * pf)78 void i40e_vc_notify_link_state(struct i40e_pf *pf)
79 {
80 	int i;
81 
82 	for (i = 0; i < pf->num_alloc_vfs; i++)
83 		i40e_vc_notify_vf_link_state(&pf->vf[i]);
84 }
85 
86 /**
87  * i40e_vc_notify_reset
88  * @pf: pointer to the PF structure
89  *
90  * indicate a pending reset to all VFs on a given PF
91  **/
i40e_vc_notify_reset(struct i40e_pf * pf)92 void i40e_vc_notify_reset(struct i40e_pf *pf)
93 {
94 	struct virtchnl_pf_event pfe;
95 
96 	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
97 	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
98 	i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
99 			     (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
100 }
101 
102 #ifdef CONFIG_PCI_IOV
i40e_restore_all_vfs_msi_state(struct pci_dev * pdev)103 void i40e_restore_all_vfs_msi_state(struct pci_dev *pdev)
104 {
105 	u16 vf_id;
106 	u16 pos;
107 
108 	/* Continue only if this is a PF */
109 	if (!pdev->is_physfn)
110 		return;
111 
112 	if (!pci_num_vf(pdev))
113 		return;
114 
115 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
116 	if (pos) {
117 		struct pci_dev *vf_dev = NULL;
118 
119 		pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID, &vf_id);
120 		while ((vf_dev = pci_get_device(pdev->vendor, vf_id, vf_dev))) {
121 			if (vf_dev->is_virtfn && vf_dev->physfn == pdev)
122 				pci_restore_msi_state(vf_dev);
123 		}
124 	}
125 }
126 #endif /* CONFIG_PCI_IOV */
127 
128 /**
129  * i40e_vc_notify_vf_reset
130  * @vf: pointer to the VF structure
131  *
132  * indicate a pending reset to the given VF
133  **/
i40e_vc_notify_vf_reset(struct i40e_vf * vf)134 void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
135 {
136 	struct virtchnl_pf_event pfe;
137 	int abs_vf_id;
138 
139 	/* validate the request */
140 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
141 		return;
142 
143 	/* verify if the VF is in either init or active before proceeding */
144 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
145 	    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
146 		return;
147 
148 	abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
149 
150 	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
151 	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
152 	i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, VIRTCHNL_OP_EVENT,
153 			       0, (u8 *)&pfe,
154 			       sizeof(struct virtchnl_pf_event), NULL);
155 }
156 /***********************misc routines*****************************/
157 
158 /**
159  * i40e_vc_reset_vf
160  * @vf: pointer to the VF info
161  * @notify_vf: notify vf about reset or not
162  * Reset VF handler.
163  **/
i40e_vc_reset_vf(struct i40e_vf * vf,bool notify_vf)164 static void i40e_vc_reset_vf(struct i40e_vf *vf, bool notify_vf)
165 {
166 	struct i40e_pf *pf = vf->pf;
167 	int i;
168 
169 	if (notify_vf)
170 		i40e_vc_notify_vf_reset(vf);
171 
172 	/* We want to ensure that an actual reset occurs initiated after this
173 	 * function was called. However, we do not want to wait forever, so
174 	 * we'll give a reasonable time and print a message if we failed to
175 	 * ensure a reset.
176 	 */
177 	for (i = 0; i < 20; i++) {
178 		/* If PF is in VFs releasing state reset VF is impossible,
179 		 * so leave it.
180 		 */
181 		if (test_bit(__I40E_VFS_RELEASING, pf->state))
182 			return;
183 		if (i40e_reset_vf(vf, false))
184 			return;
185 		usleep_range(10000, 20000);
186 	}
187 
188 	if (notify_vf)
189 		dev_warn(&vf->pf->pdev->dev,
190 			 "Failed to initiate reset for VF %d after 200 milliseconds\n",
191 			 vf->vf_id);
192 	else
193 		dev_dbg(&vf->pf->pdev->dev,
194 			"Failed to initiate reset for VF %d after 200 milliseconds\n",
195 			vf->vf_id);
196 }
197 
198 /**
199  * i40e_vc_isvalid_vsi_id
200  * @vf: pointer to the VF info
201  * @vsi_id: VF relative VSI id
202  *
203  * check for the valid VSI id
204  **/
i40e_vc_isvalid_vsi_id(struct i40e_vf * vf,u16 vsi_id)205 static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
206 {
207 	struct i40e_pf *pf = vf->pf;
208 	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
209 
210 	return (vsi && (vsi->vf_id == vf->vf_id));
211 }
212 
213 /**
214  * i40e_vc_isvalid_queue_id
215  * @vf: pointer to the VF info
216  * @vsi_id: vsi id
217  * @qid: vsi relative queue id
218  *
219  * check for the valid queue id
220  **/
i40e_vc_isvalid_queue_id(struct i40e_vf * vf,u16 vsi_id,u16 qid)221 static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
222 					    u16 qid)
223 {
224 	struct i40e_pf *pf = vf->pf;
225 	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
226 
227 	return (vsi && (qid < vsi->alloc_queue_pairs));
228 }
229 
230 /**
231  * i40e_vc_isvalid_vector_id
232  * @vf: pointer to the VF info
233  * @vector_id: VF relative vector id
234  *
235  * check for the valid vector id
236  **/
i40e_vc_isvalid_vector_id(struct i40e_vf * vf,u32 vector_id)237 static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u32 vector_id)
238 {
239 	struct i40e_pf *pf = vf->pf;
240 
241 	return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
242 }
243 
244 /***********************vf resource mgmt routines*****************/
245 
246 /**
247  * i40e_vc_get_pf_queue_id
248  * @vf: pointer to the VF info
249  * @vsi_id: id of VSI as provided by the FW
250  * @vsi_queue_id: vsi relative queue id
251  *
252  * return PF relative queue id
253  **/
i40e_vc_get_pf_queue_id(struct i40e_vf * vf,u16 vsi_id,u8 vsi_queue_id)254 static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
255 				   u8 vsi_queue_id)
256 {
257 	struct i40e_pf *pf = vf->pf;
258 	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
259 	u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
260 
261 	if (!vsi)
262 		return pf_queue_id;
263 
264 	if (le16_to_cpu(vsi->info.mapping_flags) &
265 	    I40E_AQ_VSI_QUE_MAP_NONCONTIG)
266 		pf_queue_id =
267 			le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
268 	else
269 		pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
270 			      vsi_queue_id;
271 
272 	return pf_queue_id;
273 }
274 
275 /**
276  * i40e_get_real_pf_qid
277  * @vf: pointer to the VF info
278  * @vsi_id: vsi id
279  * @queue_id: queue number
280  *
281  * wrapper function to get pf_queue_id handling ADq code as well
282  **/
i40e_get_real_pf_qid(struct i40e_vf * vf,u16 vsi_id,u16 queue_id)283 static u16 i40e_get_real_pf_qid(struct i40e_vf *vf, u16 vsi_id, u16 queue_id)
284 {
285 	int i;
286 
287 	if (vf->adq_enabled) {
288 		/* Although VF considers all the queues(can be 1 to 16) as its
289 		 * own but they may actually belong to different VSIs(up to 4).
290 		 * We need to find which queues belongs to which VSI.
291 		 */
292 		for (i = 0; i < vf->num_tc; i++) {
293 			if (queue_id < vf->ch[i].num_qps) {
294 				vsi_id = vf->ch[i].vsi_id;
295 				break;
296 			}
297 			/* find right queue id which is relative to a
298 			 * given VSI.
299 			 */
300 			queue_id -= vf->ch[i].num_qps;
301 			}
302 		}
303 
304 	return i40e_vc_get_pf_queue_id(vf, vsi_id, queue_id);
305 }
306 
307 /**
308  * i40e_config_irq_link_list
309  * @vf: pointer to the VF info
310  * @vsi_id: id of VSI as given by the FW
311  * @vecmap: irq map info
312  *
313  * configure irq link list from the map
314  **/
i40e_config_irq_link_list(struct i40e_vf * vf,u16 vsi_id,struct virtchnl_vector_map * vecmap)315 static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
316 				      struct virtchnl_vector_map *vecmap)
317 {
318 	unsigned long linklistmap = 0, tempmap;
319 	struct i40e_pf *pf = vf->pf;
320 	struct i40e_hw *hw = &pf->hw;
321 	u16 vsi_queue_id, pf_queue_id;
322 	enum i40e_queue_type qtype;
323 	u16 next_q, vector_id, size;
324 	u32 reg, reg_idx;
325 	u16 itr_idx = 0;
326 
327 	vector_id = vecmap->vector_id;
328 	/* setup the head */
329 	if (0 == vector_id)
330 		reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
331 	else
332 		reg_idx = I40E_VPINT_LNKLSTN(
333 		     ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
334 		     (vector_id - 1));
335 
336 	if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
337 		/* Special case - No queues mapped on this vector */
338 		wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
339 		goto irq_list_done;
340 	}
341 	tempmap = vecmap->rxq_map;
342 	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
343 		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
344 				    vsi_queue_id));
345 	}
346 
347 	tempmap = vecmap->txq_map;
348 	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
349 		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
350 				     vsi_queue_id + 1));
351 	}
352 
353 	size = I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES;
354 	next_q = find_first_bit(&linklistmap, size);
355 	if (unlikely(next_q == size))
356 		goto irq_list_done;
357 
358 	vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
359 	qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
360 	pf_queue_id = i40e_get_real_pf_qid(vf, vsi_id, vsi_queue_id);
361 	reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
362 
363 	wr32(hw, reg_idx, reg);
364 
365 	while (next_q < size) {
366 		switch (qtype) {
367 		case I40E_QUEUE_TYPE_RX:
368 			reg_idx = I40E_QINT_RQCTL(pf_queue_id);
369 			itr_idx = vecmap->rxitr_idx;
370 			break;
371 		case I40E_QUEUE_TYPE_TX:
372 			reg_idx = I40E_QINT_TQCTL(pf_queue_id);
373 			itr_idx = vecmap->txitr_idx;
374 			break;
375 		default:
376 			break;
377 		}
378 
379 		next_q = find_next_bit(&linklistmap, size, next_q + 1);
380 		if (next_q < size) {
381 			vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
382 			qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
383 			pf_queue_id = i40e_get_real_pf_qid(vf,
384 							   vsi_id,
385 							   vsi_queue_id);
386 		} else {
387 			pf_queue_id = I40E_QUEUE_END_OF_LIST;
388 			qtype = 0;
389 		}
390 
391 		/* format for the RQCTL & TQCTL regs is same */
392 		reg = (vector_id) |
393 		    (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
394 		    (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
395 		    BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
396 		    (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
397 		wr32(hw, reg_idx, reg);
398 	}
399 
400 	/* if the vf is running in polling mode and using interrupt zero,
401 	 * need to disable auto-mask on enabling zero interrupt for VFs.
402 	 */
403 	if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
404 	    (vector_id == 0)) {
405 		reg = rd32(hw, I40E_GLINT_CTL);
406 		if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
407 			reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
408 			wr32(hw, I40E_GLINT_CTL, reg);
409 		}
410 	}
411 
412 irq_list_done:
413 	i40e_flush(hw);
414 }
415 
416 /**
417  * i40e_release_iwarp_qvlist
418  * @vf: pointer to the VF.
419  *
420  **/
i40e_release_iwarp_qvlist(struct i40e_vf * vf)421 static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
422 {
423 	struct i40e_pf *pf = vf->pf;
424 	struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
425 	u32 msix_vf;
426 	u32 i;
427 
428 	if (!vf->qvlist_info)
429 		return;
430 
431 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
432 	for (i = 0; i < qvlist_info->num_vectors; i++) {
433 		struct virtchnl_iwarp_qv_info *qv_info;
434 		u32 next_q_index, next_q_type;
435 		struct i40e_hw *hw = &pf->hw;
436 		u32 v_idx, reg_idx, reg;
437 
438 		qv_info = &qvlist_info->qv_info[i];
439 		if (!qv_info)
440 			continue;
441 		v_idx = qv_info->v_idx;
442 		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
443 			/* Figure out the queue after CEQ and make that the
444 			 * first queue.
445 			 */
446 			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
447 			reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
448 			next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
449 					>> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
450 			next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
451 					>> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
452 
453 			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
454 			reg = (next_q_index &
455 			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
456 			       (next_q_type <<
457 			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
458 
459 			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
460 		}
461 	}
462 	kfree(vf->qvlist_info);
463 	vf->qvlist_info = NULL;
464 }
465 
466 /**
467  * i40e_config_iwarp_qvlist
468  * @vf: pointer to the VF info
469  * @qvlist_info: queue and vector list
470  *
471  * Return 0 on success or < 0 on error
472  **/
i40e_config_iwarp_qvlist(struct i40e_vf * vf,struct virtchnl_iwarp_qvlist_info * qvlist_info)473 static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
474 				    struct virtchnl_iwarp_qvlist_info *qvlist_info)
475 {
476 	struct i40e_pf *pf = vf->pf;
477 	struct i40e_hw *hw = &pf->hw;
478 	struct virtchnl_iwarp_qv_info *qv_info;
479 	u32 v_idx, i, reg_idx, reg;
480 	u32 next_q_idx, next_q_type;
481 	u32 msix_vf;
482 	int ret = 0;
483 
484 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
485 
486 	if (qvlist_info->num_vectors > msix_vf) {
487 		dev_warn(&pf->pdev->dev,
488 			 "Incorrect number of iwarp vectors %u. Maximum %u allowed.\n",
489 			 qvlist_info->num_vectors,
490 			 msix_vf);
491 		ret = -EINVAL;
492 		goto err_out;
493 	}
494 
495 	kfree(vf->qvlist_info);
496 	vf->qvlist_info = kzalloc(struct_size(vf->qvlist_info, qv_info,
497 					      qvlist_info->num_vectors - 1),
498 				  GFP_KERNEL);
499 	if (!vf->qvlist_info) {
500 		ret = -ENOMEM;
501 		goto err_out;
502 	}
503 	vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
504 
505 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
506 	for (i = 0; i < qvlist_info->num_vectors; i++) {
507 		qv_info = &qvlist_info->qv_info[i];
508 		if (!qv_info)
509 			continue;
510 
511 		/* Validate vector id belongs to this vf */
512 		if (!i40e_vc_isvalid_vector_id(vf, qv_info->v_idx)) {
513 			ret = -EINVAL;
514 			goto err_free;
515 		}
516 
517 		v_idx = qv_info->v_idx;
518 
519 		vf->qvlist_info->qv_info[i] = *qv_info;
520 
521 		reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
522 		/* We might be sharing the interrupt, so get the first queue
523 		 * index and type, push it down the list by adding the new
524 		 * queue on top. Also link it with the new queue in CEQCTL.
525 		 */
526 		reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
527 		next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
528 				I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
529 		next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
530 				I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
531 
532 		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
533 			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
534 			reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
535 			(v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
536 			(qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
537 			(next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
538 			(next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
539 			wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
540 
541 			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
542 			reg = (qv_info->ceq_idx &
543 			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
544 			       (I40E_QUEUE_TYPE_PE_CEQ <<
545 			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
546 			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
547 		}
548 
549 		if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
550 			reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
551 			(v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
552 			(qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
553 
554 			wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
555 		}
556 	}
557 
558 	return 0;
559 err_free:
560 	kfree(vf->qvlist_info);
561 	vf->qvlist_info = NULL;
562 err_out:
563 	return ret;
564 }
565 
566 /**
567  * i40e_config_vsi_tx_queue
568  * @vf: pointer to the VF info
569  * @vsi_id: id of VSI as provided by the FW
570  * @vsi_queue_id: vsi relative queue index
571  * @info: config. info
572  *
573  * configure tx queue
574  **/
i40e_config_vsi_tx_queue(struct i40e_vf * vf,u16 vsi_id,u16 vsi_queue_id,struct virtchnl_txq_info * info)575 static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
576 				    u16 vsi_queue_id,
577 				    struct virtchnl_txq_info *info)
578 {
579 	struct i40e_pf *pf = vf->pf;
580 	struct i40e_hw *hw = &pf->hw;
581 	struct i40e_hmc_obj_txq tx_ctx;
582 	struct i40e_vsi *vsi;
583 	u16 pf_queue_id;
584 	u32 qtx_ctl;
585 	int ret = 0;
586 
587 	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
588 		ret = -ENOENT;
589 		goto error_context;
590 	}
591 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
592 	vsi = i40e_find_vsi_from_id(pf, vsi_id);
593 	if (!vsi) {
594 		ret = -ENOENT;
595 		goto error_context;
596 	}
597 
598 	/* clear the context structure first */
599 	memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
600 
601 	/* only set the required fields */
602 	tx_ctx.base = info->dma_ring_addr / 128;
603 	tx_ctx.qlen = info->ring_len;
604 	tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
605 	tx_ctx.rdylist_act = 0;
606 	tx_ctx.head_wb_ena = info->headwb_enabled;
607 	tx_ctx.head_wb_addr = info->dma_headwb_addr;
608 
609 	/* clear the context in the HMC */
610 	ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
611 	if (ret) {
612 		dev_err(&pf->pdev->dev,
613 			"Failed to clear VF LAN Tx queue context %d, error: %d\n",
614 			pf_queue_id, ret);
615 		ret = -ENOENT;
616 		goto error_context;
617 	}
618 
619 	/* set the context in the HMC */
620 	ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
621 	if (ret) {
622 		dev_err(&pf->pdev->dev,
623 			"Failed to set VF LAN Tx queue context %d error: %d\n",
624 			pf_queue_id, ret);
625 		ret = -ENOENT;
626 		goto error_context;
627 	}
628 
629 	/* associate this queue with the PCI VF function */
630 	qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
631 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
632 		    & I40E_QTX_CTL_PF_INDX_MASK);
633 	qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
634 		     << I40E_QTX_CTL_VFVM_INDX_SHIFT)
635 		    & I40E_QTX_CTL_VFVM_INDX_MASK);
636 	wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
637 	i40e_flush(hw);
638 
639 error_context:
640 	return ret;
641 }
642 
643 /**
644  * i40e_config_vsi_rx_queue
645  * @vf: pointer to the VF info
646  * @vsi_id: id of VSI  as provided by the FW
647  * @vsi_queue_id: vsi relative queue index
648  * @info: config. info
649  *
650  * configure rx queue
651  **/
i40e_config_vsi_rx_queue(struct i40e_vf * vf,u16 vsi_id,u16 vsi_queue_id,struct virtchnl_rxq_info * info)652 static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
653 				    u16 vsi_queue_id,
654 				    struct virtchnl_rxq_info *info)
655 {
656 	u16 pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
657 	struct i40e_pf *pf = vf->pf;
658 	struct i40e_vsi *vsi = pf->vsi[vf->lan_vsi_idx];
659 	struct i40e_hw *hw = &pf->hw;
660 	struct i40e_hmc_obj_rxq rx_ctx;
661 	int ret = 0;
662 
663 	/* clear the context structure first */
664 	memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
665 
666 	/* only set the required fields */
667 	rx_ctx.base = info->dma_ring_addr / 128;
668 	rx_ctx.qlen = info->ring_len;
669 
670 	if (info->splithdr_enabled) {
671 		rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2      |
672 				  I40E_RX_SPLIT_IP      |
673 				  I40E_RX_SPLIT_TCP_UDP |
674 				  I40E_RX_SPLIT_SCTP;
675 		/* header length validation */
676 		if (info->hdr_size > ((2 * 1024) - 64)) {
677 			ret = -EINVAL;
678 			goto error_param;
679 		}
680 		rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
681 
682 		/* set split mode 10b */
683 		rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
684 	}
685 
686 	/* databuffer length validation */
687 	if (info->databuffer_size > ((16 * 1024) - 128)) {
688 		ret = -EINVAL;
689 		goto error_param;
690 	}
691 	rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
692 
693 	/* max pkt. length validation */
694 	if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
695 		ret = -EINVAL;
696 		goto error_param;
697 	}
698 	rx_ctx.rxmax = info->max_pkt_size;
699 
700 	/* if port VLAN is configured increase the max packet size */
701 	if (vsi->info.pvid)
702 		rx_ctx.rxmax += VLAN_HLEN;
703 
704 	/* enable 32bytes desc always */
705 	rx_ctx.dsize = 1;
706 
707 	/* default values */
708 	rx_ctx.lrxqthresh = 1;
709 	rx_ctx.crcstrip = 1;
710 	rx_ctx.prefena = 1;
711 	rx_ctx.l2tsel = 1;
712 
713 	/* clear the context in the HMC */
714 	ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
715 	if (ret) {
716 		dev_err(&pf->pdev->dev,
717 			"Failed to clear VF LAN Rx queue context %d, error: %d\n",
718 			pf_queue_id, ret);
719 		ret = -ENOENT;
720 		goto error_param;
721 	}
722 
723 	/* set the context in the HMC */
724 	ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
725 	if (ret) {
726 		dev_err(&pf->pdev->dev,
727 			"Failed to set VF LAN Rx queue context %d error: %d\n",
728 			pf_queue_id, ret);
729 		ret = -ENOENT;
730 		goto error_param;
731 	}
732 
733 error_param:
734 	return ret;
735 }
736 
737 /**
738  * i40e_alloc_vsi_res
739  * @vf: pointer to the VF info
740  * @idx: VSI index, applies only for ADq mode, zero otherwise
741  *
742  * alloc VF vsi context & resources
743  **/
i40e_alloc_vsi_res(struct i40e_vf * vf,u8 idx)744 static int i40e_alloc_vsi_res(struct i40e_vf *vf, u8 idx)
745 {
746 	struct i40e_mac_filter *f = NULL;
747 	struct i40e_pf *pf = vf->pf;
748 	struct i40e_vsi *vsi;
749 	u64 max_tx_rate = 0;
750 	int ret = 0;
751 
752 	vsi = i40e_vsi_setup(pf, I40E_VSI_SRIOV, pf->vsi[pf->lan_vsi]->seid,
753 			     vf->vf_id);
754 
755 	if (!vsi) {
756 		dev_err(&pf->pdev->dev,
757 			"add vsi failed for VF %d, aq_err %d\n",
758 			vf->vf_id, pf->hw.aq.asq_last_status);
759 		ret = -ENOENT;
760 		goto error_alloc_vsi_res;
761 	}
762 
763 	if (!idx) {
764 		u64 hena = i40e_pf_get_default_rss_hena(pf);
765 		u8 broadcast[ETH_ALEN];
766 
767 		vf->lan_vsi_idx = vsi->idx;
768 		vf->lan_vsi_id = vsi->id;
769 		/* If the port VLAN has been configured and then the
770 		 * VF driver was removed then the VSI port VLAN
771 		 * configuration was destroyed.  Check if there is
772 		 * a port VLAN and restore the VSI configuration if
773 		 * needed.
774 		 */
775 		if (vf->port_vlan_id)
776 			i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
777 
778 		spin_lock_bh(&vsi->mac_filter_hash_lock);
779 		if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
780 			f = i40e_add_mac_filter(vsi,
781 						vf->default_lan_addr.addr);
782 			if (!f)
783 				dev_info(&pf->pdev->dev,
784 					 "Could not add MAC filter %pM for VF %d\n",
785 					vf->default_lan_addr.addr, vf->vf_id);
786 		}
787 		eth_broadcast_addr(broadcast);
788 		f = i40e_add_mac_filter(vsi, broadcast);
789 		if (!f)
790 			dev_info(&pf->pdev->dev,
791 				 "Could not allocate VF broadcast filter\n");
792 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
793 		wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
794 		wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
795 		/* program mac filter only for VF VSI */
796 		ret = i40e_sync_vsi_filters(vsi);
797 		if (ret)
798 			dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
799 	}
800 
801 	/* storing VSI index and id for ADq and don't apply the mac filter */
802 	if (vf->adq_enabled) {
803 		vf->ch[idx].vsi_idx = vsi->idx;
804 		vf->ch[idx].vsi_id = vsi->id;
805 	}
806 
807 	/* Set VF bandwidth if specified */
808 	if (vf->tx_rate) {
809 		max_tx_rate = vf->tx_rate;
810 	} else if (vf->ch[idx].max_tx_rate) {
811 		max_tx_rate = vf->ch[idx].max_tx_rate;
812 	}
813 
814 	if (max_tx_rate) {
815 		max_tx_rate = div_u64(max_tx_rate, I40E_BW_CREDIT_DIVISOR);
816 		ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
817 						  max_tx_rate, 0, NULL);
818 		if (ret)
819 			dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
820 				vf->vf_id, ret);
821 	}
822 
823 error_alloc_vsi_res:
824 	return ret;
825 }
826 
827 /**
828  * i40e_map_pf_queues_to_vsi
829  * @vf: pointer to the VF info
830  *
831  * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
832  * function takes care of first part VSILAN_QTABLE, mapping pf queues to VSI.
833  **/
i40e_map_pf_queues_to_vsi(struct i40e_vf * vf)834 static void i40e_map_pf_queues_to_vsi(struct i40e_vf *vf)
835 {
836 	struct i40e_pf *pf = vf->pf;
837 	struct i40e_hw *hw = &pf->hw;
838 	u32 reg, num_tc = 1; /* VF has at least one traffic class */
839 	u16 vsi_id, qps;
840 	int i, j;
841 
842 	if (vf->adq_enabled)
843 		num_tc = vf->num_tc;
844 
845 	for (i = 0; i < num_tc; i++) {
846 		if (vf->adq_enabled) {
847 			qps = vf->ch[i].num_qps;
848 			vsi_id =  vf->ch[i].vsi_id;
849 		} else {
850 			qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
851 			vsi_id = vf->lan_vsi_id;
852 		}
853 
854 		for (j = 0; j < 7; j++) {
855 			if (j * 2 >= qps) {
856 				/* end of list */
857 				reg = 0x07FF07FF;
858 			} else {
859 				u16 qid = i40e_vc_get_pf_queue_id(vf,
860 								  vsi_id,
861 								  j * 2);
862 				reg = qid;
863 				qid = i40e_vc_get_pf_queue_id(vf, vsi_id,
864 							      (j * 2) + 1);
865 				reg |= qid << 16;
866 			}
867 			i40e_write_rx_ctl(hw,
868 					  I40E_VSILAN_QTABLE(j, vsi_id),
869 					  reg);
870 		}
871 	}
872 }
873 
874 /**
875  * i40e_map_pf_to_vf_queues
876  * @vf: pointer to the VF info
877  *
878  * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
879  * function takes care of the second part VPLAN_QTABLE & completes VF mappings.
880  **/
i40e_map_pf_to_vf_queues(struct i40e_vf * vf)881 static void i40e_map_pf_to_vf_queues(struct i40e_vf *vf)
882 {
883 	struct i40e_pf *pf = vf->pf;
884 	struct i40e_hw *hw = &pf->hw;
885 	u32 reg, total_qps = 0;
886 	u32 qps, num_tc = 1; /* VF has at least one traffic class */
887 	u16 vsi_id, qid;
888 	int i, j;
889 
890 	if (vf->adq_enabled)
891 		num_tc = vf->num_tc;
892 
893 	for (i = 0; i < num_tc; i++) {
894 		if (vf->adq_enabled) {
895 			qps = vf->ch[i].num_qps;
896 			vsi_id =  vf->ch[i].vsi_id;
897 		} else {
898 			qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
899 			vsi_id = vf->lan_vsi_id;
900 		}
901 
902 		for (j = 0; j < qps; j++) {
903 			qid = i40e_vc_get_pf_queue_id(vf, vsi_id, j);
904 
905 			reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
906 			wr32(hw, I40E_VPLAN_QTABLE(total_qps, vf->vf_id),
907 			     reg);
908 			total_qps++;
909 		}
910 	}
911 }
912 
913 /**
914  * i40e_enable_vf_mappings
915  * @vf: pointer to the VF info
916  *
917  * enable VF mappings
918  **/
i40e_enable_vf_mappings(struct i40e_vf * vf)919 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
920 {
921 	struct i40e_pf *pf = vf->pf;
922 	struct i40e_hw *hw = &pf->hw;
923 	u32 reg;
924 
925 	/* Tell the hardware we're using noncontiguous mapping. HW requires
926 	 * that VF queues be mapped using this method, even when they are
927 	 * contiguous in real life
928 	 */
929 	i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
930 			  I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
931 
932 	/* enable VF vplan_qtable mappings */
933 	reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
934 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
935 
936 	i40e_map_pf_to_vf_queues(vf);
937 	i40e_map_pf_queues_to_vsi(vf);
938 
939 	i40e_flush(hw);
940 }
941 
942 /**
943  * i40e_disable_vf_mappings
944  * @vf: pointer to the VF info
945  *
946  * disable VF mappings
947  **/
i40e_disable_vf_mappings(struct i40e_vf * vf)948 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
949 {
950 	struct i40e_pf *pf = vf->pf;
951 	struct i40e_hw *hw = &pf->hw;
952 	int i;
953 
954 	/* disable qp mappings */
955 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
956 	for (i = 0; i < I40E_MAX_VSI_QP; i++)
957 		wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
958 		     I40E_QUEUE_END_OF_LIST);
959 	i40e_flush(hw);
960 }
961 
962 /**
963  * i40e_free_vf_res
964  * @vf: pointer to the VF info
965  *
966  * free VF resources
967  **/
i40e_free_vf_res(struct i40e_vf * vf)968 static void i40e_free_vf_res(struct i40e_vf *vf)
969 {
970 	struct i40e_pf *pf = vf->pf;
971 	struct i40e_hw *hw = &pf->hw;
972 	u32 reg_idx, reg;
973 	int i, j, msix_vf;
974 
975 	/* Start by disabling VF's configuration API to prevent the OS from
976 	 * accessing the VF's VSI after it's freed / invalidated.
977 	 */
978 	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
979 
980 	/* It's possible the VF had requeuested more queues than the default so
981 	 * do the accounting here when we're about to free them.
982 	 */
983 	if (vf->num_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF) {
984 		pf->queues_left += vf->num_queue_pairs -
985 				   I40E_DEFAULT_QUEUES_PER_VF;
986 	}
987 
988 	/* free vsi & disconnect it from the parent uplink */
989 	if (vf->lan_vsi_idx) {
990 		i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
991 		vf->lan_vsi_idx = 0;
992 		vf->lan_vsi_id = 0;
993 	}
994 
995 	/* do the accounting and remove additional ADq VSI's */
996 	if (vf->adq_enabled && vf->ch[0].vsi_idx) {
997 		for (j = 0; j < vf->num_tc; j++) {
998 			/* At this point VSI0 is already released so don't
999 			 * release it again and only clear their values in
1000 			 * structure variables
1001 			 */
1002 			if (j)
1003 				i40e_vsi_release(pf->vsi[vf->ch[j].vsi_idx]);
1004 			vf->ch[j].vsi_idx = 0;
1005 			vf->ch[j].vsi_id = 0;
1006 		}
1007 	}
1008 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
1009 
1010 	/* disable interrupts so the VF starts in a known state */
1011 	for (i = 0; i < msix_vf; i++) {
1012 		/* format is same for both registers */
1013 		if (0 == i)
1014 			reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
1015 		else
1016 			reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
1017 						      (vf->vf_id))
1018 						     + (i - 1));
1019 		wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
1020 		i40e_flush(hw);
1021 	}
1022 
1023 	/* clear the irq settings */
1024 	for (i = 0; i < msix_vf; i++) {
1025 		/* format is same for both registers */
1026 		if (0 == i)
1027 			reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
1028 		else
1029 			reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
1030 						      (vf->vf_id))
1031 						     + (i - 1));
1032 		reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
1033 		       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
1034 		wr32(hw, reg_idx, reg);
1035 		i40e_flush(hw);
1036 	}
1037 	/* reset some of the state variables keeping track of the resources */
1038 	vf->num_queue_pairs = 0;
1039 	clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
1040 	clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
1041 }
1042 
1043 /**
1044  * i40e_alloc_vf_res
1045  * @vf: pointer to the VF info
1046  *
1047  * allocate VF resources
1048  **/
i40e_alloc_vf_res(struct i40e_vf * vf)1049 static int i40e_alloc_vf_res(struct i40e_vf *vf)
1050 {
1051 	struct i40e_pf *pf = vf->pf;
1052 	int total_queue_pairs = 0;
1053 	int ret, idx;
1054 
1055 	if (vf->num_req_queues &&
1056 	    vf->num_req_queues <= pf->queues_left + I40E_DEFAULT_QUEUES_PER_VF)
1057 		pf->num_vf_qps = vf->num_req_queues;
1058 	else
1059 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
1060 
1061 	/* allocate hw vsi context & associated resources */
1062 	ret = i40e_alloc_vsi_res(vf, 0);
1063 	if (ret)
1064 		goto error_alloc;
1065 	total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
1066 
1067 	/* allocate additional VSIs based on tc information for ADq */
1068 	if (vf->adq_enabled) {
1069 		if (pf->queues_left >=
1070 		    (I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF)) {
1071 			/* TC 0 always belongs to VF VSI */
1072 			for (idx = 1; idx < vf->num_tc; idx++) {
1073 				ret = i40e_alloc_vsi_res(vf, idx);
1074 				if (ret)
1075 					goto error_alloc;
1076 			}
1077 			/* send correct number of queues */
1078 			total_queue_pairs = I40E_MAX_VF_QUEUES;
1079 		} else {
1080 			dev_info(&pf->pdev->dev, "VF %d: Not enough queues to allocate, disabling ADq\n",
1081 				 vf->vf_id);
1082 			vf->adq_enabled = false;
1083 		}
1084 	}
1085 
1086 	/* We account for each VF to get a default number of queue pairs.  If
1087 	 * the VF has now requested more, we need to account for that to make
1088 	 * certain we never request more queues than we actually have left in
1089 	 * HW.
1090 	 */
1091 	if (total_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF)
1092 		pf->queues_left -=
1093 			total_queue_pairs - I40E_DEFAULT_QUEUES_PER_VF;
1094 
1095 	if (vf->trusted)
1096 		set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1097 	else
1098 		clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1099 
1100 	/* store the total qps number for the runtime
1101 	 * VF req validation
1102 	 */
1103 	vf->num_queue_pairs = total_queue_pairs;
1104 
1105 	/* VF is now completely initialized */
1106 	set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
1107 
1108 error_alloc:
1109 	if (ret)
1110 		i40e_free_vf_res(vf);
1111 
1112 	return ret;
1113 }
1114 
1115 #define VF_DEVICE_STATUS 0xAA
1116 #define VF_TRANS_PENDING_MASK 0x20
1117 /**
1118  * i40e_quiesce_vf_pci
1119  * @vf: pointer to the VF structure
1120  *
1121  * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
1122  * if the transactions never clear.
1123  **/
i40e_quiesce_vf_pci(struct i40e_vf * vf)1124 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
1125 {
1126 	struct i40e_pf *pf = vf->pf;
1127 	struct i40e_hw *hw = &pf->hw;
1128 	int vf_abs_id, i;
1129 	u32 reg;
1130 
1131 	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
1132 
1133 	wr32(hw, I40E_PF_PCI_CIAA,
1134 	     VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
1135 	for (i = 0; i < 100; i++) {
1136 		reg = rd32(hw, I40E_PF_PCI_CIAD);
1137 		if ((reg & VF_TRANS_PENDING_MASK) == 0)
1138 			return 0;
1139 		udelay(1);
1140 	}
1141 	return -EIO;
1142 }
1143 
1144 /**
1145  * __i40e_getnum_vf_vsi_vlan_filters
1146  * @vsi: pointer to the vsi
1147  *
1148  * called to get the number of VLANs offloaded on this VF
1149  **/
__i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi * vsi)1150 static int __i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
1151 {
1152 	struct i40e_mac_filter *f;
1153 	u16 num_vlans = 0, bkt;
1154 
1155 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1156 		if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
1157 			num_vlans++;
1158 	}
1159 
1160 	return num_vlans;
1161 }
1162 
1163 /**
1164  * i40e_getnum_vf_vsi_vlan_filters
1165  * @vsi: pointer to the vsi
1166  *
1167  * wrapper for __i40e_getnum_vf_vsi_vlan_filters() with spinlock held
1168  **/
i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi * vsi)1169 static int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
1170 {
1171 	int num_vlans;
1172 
1173 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1174 	num_vlans = __i40e_getnum_vf_vsi_vlan_filters(vsi);
1175 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1176 
1177 	return num_vlans;
1178 }
1179 
1180 /**
1181  * i40e_get_vlan_list_sync
1182  * @vsi: pointer to the VSI
1183  * @num_vlans: number of VLANs in mac_filter_hash, returned to caller
1184  * @vlan_list: list of VLANs present in mac_filter_hash, returned to caller.
1185  *             This array is allocated here, but has to be freed in caller.
1186  *
1187  * Called to get number of VLANs and VLAN list present in mac_filter_hash.
1188  **/
i40e_get_vlan_list_sync(struct i40e_vsi * vsi,u16 * num_vlans,s16 ** vlan_list)1189 static void i40e_get_vlan_list_sync(struct i40e_vsi *vsi, u16 *num_vlans,
1190 				    s16 **vlan_list)
1191 {
1192 	struct i40e_mac_filter *f;
1193 	int i = 0;
1194 	int bkt;
1195 
1196 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1197 	*num_vlans = __i40e_getnum_vf_vsi_vlan_filters(vsi);
1198 	*vlan_list = kcalloc(*num_vlans, sizeof(**vlan_list), GFP_ATOMIC);
1199 	if (!(*vlan_list))
1200 		goto err;
1201 
1202 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1203 		if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1204 			continue;
1205 		(*vlan_list)[i++] = f->vlan;
1206 	}
1207 err:
1208 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1209 }
1210 
1211 /**
1212  * i40e_set_vsi_promisc
1213  * @vf: pointer to the VF struct
1214  * @seid: VSI number
1215  * @multi_enable: set MAC L2 layer multicast promiscuous enable/disable
1216  *                for a given VLAN
1217  * @unicast_enable: set MAC L2 layer unicast promiscuous enable/disable
1218  *                  for a given VLAN
1219  * @vl: List of VLANs - apply filter for given VLANs
1220  * @num_vlans: Number of elements in @vl
1221  **/
1222 static i40e_status
i40e_set_vsi_promisc(struct i40e_vf * vf,u16 seid,bool multi_enable,bool unicast_enable,s16 * vl,u16 num_vlans)1223 i40e_set_vsi_promisc(struct i40e_vf *vf, u16 seid, bool multi_enable,
1224 		     bool unicast_enable, s16 *vl, u16 num_vlans)
1225 {
1226 	i40e_status aq_ret, aq_tmp = 0;
1227 	struct i40e_pf *pf = vf->pf;
1228 	struct i40e_hw *hw = &pf->hw;
1229 	int i;
1230 
1231 	/* No VLAN to set promisc on, set on VSI */
1232 	if (!num_vlans || !vl) {
1233 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, seid,
1234 							       multi_enable,
1235 							       NULL);
1236 		if (aq_ret) {
1237 			int aq_err = pf->hw.aq.asq_last_status;
1238 
1239 			dev_err(&pf->pdev->dev,
1240 				"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1241 				vf->vf_id,
1242 				i40e_stat_str(&pf->hw, aq_ret),
1243 				i40e_aq_str(&pf->hw, aq_err));
1244 
1245 			return aq_ret;
1246 		}
1247 
1248 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, seid,
1249 							     unicast_enable,
1250 							     NULL, true);
1251 
1252 		if (aq_ret) {
1253 			int aq_err = pf->hw.aq.asq_last_status;
1254 
1255 			dev_err(&pf->pdev->dev,
1256 				"VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
1257 				vf->vf_id,
1258 				i40e_stat_str(&pf->hw, aq_ret),
1259 				i40e_aq_str(&pf->hw, aq_err));
1260 		}
1261 
1262 		return aq_ret;
1263 	}
1264 
1265 	for (i = 0; i < num_vlans; i++) {
1266 		aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, seid,
1267 							    multi_enable,
1268 							    vl[i], NULL);
1269 		if (aq_ret) {
1270 			int aq_err = pf->hw.aq.asq_last_status;
1271 
1272 			dev_err(&pf->pdev->dev,
1273 				"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1274 				vf->vf_id,
1275 				i40e_stat_str(&pf->hw, aq_ret),
1276 				i40e_aq_str(&pf->hw, aq_err));
1277 
1278 			if (!aq_tmp)
1279 				aq_tmp = aq_ret;
1280 		}
1281 
1282 		aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, seid,
1283 							    unicast_enable,
1284 							    vl[i], NULL);
1285 		if (aq_ret) {
1286 			int aq_err = pf->hw.aq.asq_last_status;
1287 
1288 			dev_err(&pf->pdev->dev,
1289 				"VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
1290 				vf->vf_id,
1291 				i40e_stat_str(&pf->hw, aq_ret),
1292 				i40e_aq_str(&pf->hw, aq_err));
1293 
1294 			if (!aq_tmp)
1295 				aq_tmp = aq_ret;
1296 		}
1297 	}
1298 
1299 	if (aq_tmp)
1300 		aq_ret = aq_tmp;
1301 
1302 	return aq_ret;
1303 }
1304 
1305 /**
1306  * i40e_config_vf_promiscuous_mode
1307  * @vf: pointer to the VF info
1308  * @vsi_id: VSI id
1309  * @allmulti: set MAC L2 layer multicast promiscuous enable/disable
1310  * @alluni: set MAC L2 layer unicast promiscuous enable/disable
1311  *
1312  * Called from the VF to configure the promiscuous mode of
1313  * VF vsis and from the VF reset path to reset promiscuous mode.
1314  **/
i40e_config_vf_promiscuous_mode(struct i40e_vf * vf,u16 vsi_id,bool allmulti,bool alluni)1315 static i40e_status i40e_config_vf_promiscuous_mode(struct i40e_vf *vf,
1316 						   u16 vsi_id,
1317 						   bool allmulti,
1318 						   bool alluni)
1319 {
1320 	i40e_status aq_ret = I40E_SUCCESS;
1321 	struct i40e_pf *pf = vf->pf;
1322 	struct i40e_vsi *vsi;
1323 	u16 num_vlans;
1324 	s16 *vl;
1325 
1326 	vsi = i40e_find_vsi_from_id(pf, vsi_id);
1327 	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id) || !vsi)
1328 		return I40E_ERR_PARAM;
1329 
1330 	if (vf->port_vlan_id) {
1331 		aq_ret = i40e_set_vsi_promisc(vf, vsi->seid, allmulti,
1332 					      alluni, &vf->port_vlan_id, 1);
1333 		return aq_ret;
1334 	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1335 		i40e_get_vlan_list_sync(vsi, &num_vlans, &vl);
1336 
1337 		if (!vl)
1338 			return I40E_ERR_NO_MEMORY;
1339 
1340 		aq_ret = i40e_set_vsi_promisc(vf, vsi->seid, allmulti, alluni,
1341 					      vl, num_vlans);
1342 		kfree(vl);
1343 		return aq_ret;
1344 	}
1345 
1346 	/* no VLANs to set on, set on VSI */
1347 	aq_ret = i40e_set_vsi_promisc(vf, vsi->seid, allmulti, alluni,
1348 				      NULL, 0);
1349 	return aq_ret;
1350 }
1351 
1352 /**
1353  * i40e_sync_vfr_reset
1354  * @hw: pointer to hw struct
1355  * @vf_id: VF identifier
1356  *
1357  * Before trigger hardware reset, we need to know if no other process has
1358  * reserved the hardware for any reset operations. This check is done by
1359  * examining the status of the RSTAT1 register used to signal the reset.
1360  **/
i40e_sync_vfr_reset(struct i40e_hw * hw,int vf_id)1361 static int i40e_sync_vfr_reset(struct i40e_hw *hw, int vf_id)
1362 {
1363 	u32 reg;
1364 	int i;
1365 
1366 	for (i = 0; i < I40E_VFR_WAIT_COUNT; i++) {
1367 		reg = rd32(hw, I40E_VFINT_ICR0_ENA(vf_id)) &
1368 			   I40E_VFINT_ICR0_ADMINQ_MASK;
1369 		if (reg)
1370 			return 0;
1371 
1372 		usleep_range(100, 200);
1373 	}
1374 
1375 	return -EAGAIN;
1376 }
1377 
1378 /**
1379  * i40e_trigger_vf_reset
1380  * @vf: pointer to the VF structure
1381  * @flr: VFLR was issued or not
1382  *
1383  * Trigger hardware to start a reset for a particular VF. Expects the caller
1384  * to wait the proper amount of time to allow hardware to reset the VF before
1385  * it cleans up and restores VF functionality.
1386  **/
i40e_trigger_vf_reset(struct i40e_vf * vf,bool flr)1387 static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
1388 {
1389 	struct i40e_pf *pf = vf->pf;
1390 	struct i40e_hw *hw = &pf->hw;
1391 	u32 reg, reg_idx, bit_idx;
1392 	bool vf_active;
1393 	u32 radq;
1394 
1395 	/* warn the VF */
1396 	vf_active = test_and_clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1397 
1398 	/* Disable VF's configuration API during reset. The flag is re-enabled
1399 	 * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
1400 	 * It's normally disabled in i40e_free_vf_res(), but it's safer
1401 	 * to do it earlier to give some time to finish to any VF config
1402 	 * functions that may still be running at this point.
1403 	 */
1404 	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
1405 
1406 	/* In the case of a VFLR, the HW has already reset the VF and we
1407 	 * just need to clean up, so don't hit the VFRTRIG register.
1408 	 */
1409 	if (!flr) {
1410 		/* Sync VFR reset before trigger next one */
1411 		radq = rd32(hw, I40E_VFINT_ICR0_ENA(vf->vf_id)) &
1412 			    I40E_VFINT_ICR0_ADMINQ_MASK;
1413 		if (vf_active && !radq)
1414 			/* waiting for finish reset by virtual driver */
1415 			if (i40e_sync_vfr_reset(hw, vf->vf_id))
1416 				dev_info(&pf->pdev->dev,
1417 					 "Reset VF %d never finished\n",
1418 				vf->vf_id);
1419 
1420 		/* Reset VF using VPGEN_VFRTRIG reg. It is also setting
1421 		 * in progress state in rstat1 register.
1422 		 */
1423 		reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1424 		reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1425 		wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1426 		i40e_flush(hw);
1427 	}
1428 	/* clear the VFLR bit in GLGEN_VFLRSTAT */
1429 	reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
1430 	bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
1431 	wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1432 	i40e_flush(hw);
1433 
1434 	if (i40e_quiesce_vf_pci(vf))
1435 		dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
1436 			vf->vf_id);
1437 }
1438 
1439 /**
1440  * i40e_cleanup_reset_vf
1441  * @vf: pointer to the VF structure
1442  *
1443  * Cleanup a VF after the hardware reset is finished. Expects the caller to
1444  * have verified whether the reset is finished properly, and ensure the
1445  * minimum amount of wait time has passed.
1446  **/
i40e_cleanup_reset_vf(struct i40e_vf * vf)1447 static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
1448 {
1449 	struct i40e_pf *pf = vf->pf;
1450 	struct i40e_hw *hw = &pf->hw;
1451 	u32 reg;
1452 
1453 	/* disable promisc modes in case they were enabled */
1454 	i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id, false, false);
1455 
1456 	/* free VF resources to begin resetting the VSI state */
1457 	i40e_free_vf_res(vf);
1458 
1459 	/* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
1460 	 * By doing this we allow HW to access VF memory at any point. If we
1461 	 * did it any sooner, HW could access memory while it was being freed
1462 	 * in i40e_free_vf_res(), causing an IOMMU fault.
1463 	 *
1464 	 * On the other hand, this needs to be done ASAP, because the VF driver
1465 	 * is waiting for this to happen and may report a timeout. It's
1466 	 * harmless, but it gets logged into Guest OS kernel log, so best avoid
1467 	 * it.
1468 	 */
1469 	reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1470 	reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1471 	wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1472 
1473 	/* reallocate VF resources to finish resetting the VSI state */
1474 	if (!i40e_alloc_vf_res(vf)) {
1475 		int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1476 		i40e_enable_vf_mappings(vf);
1477 		set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1478 		clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1479 		/* Do not notify the client during VF init */
1480 		if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
1481 					&vf->vf_states))
1482 			i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1483 		vf->num_vlan = 0;
1484 	}
1485 
1486 	/* Tell the VF driver the reset is done. This needs to be done only
1487 	 * after VF has been fully initialized, because the VF driver may
1488 	 * request resources immediately after setting this flag.
1489 	 */
1490 	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1491 }
1492 
1493 /**
1494  * i40e_reset_vf
1495  * @vf: pointer to the VF structure
1496  * @flr: VFLR was issued or not
1497  *
1498  * Returns true if the VF is in reset, resets successfully, or resets
1499  * are disabled and false otherwise.
1500  **/
i40e_reset_vf(struct i40e_vf * vf,bool flr)1501 bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
1502 {
1503 	struct i40e_pf *pf = vf->pf;
1504 	struct i40e_hw *hw = &pf->hw;
1505 	bool rsd = false;
1506 	u32 reg;
1507 	int i;
1508 
1509 	if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state))
1510 		return true;
1511 
1512 	/* Bail out if VFs are disabled. */
1513 	if (test_bit(__I40E_VF_DISABLE, pf->state))
1514 		return true;
1515 
1516 	/* If VF is being reset already we don't need to continue. */
1517 	if (test_and_set_bit(I40E_VF_STATE_RESETTING, &vf->vf_states))
1518 		return true;
1519 
1520 	i40e_trigger_vf_reset(vf, flr);
1521 
1522 	/* poll VPGEN_VFRSTAT reg to make sure
1523 	 * that reset is complete
1524 	 */
1525 	for (i = 0; i < 10; i++) {
1526 		/* VF reset requires driver to first reset the VF and then
1527 		 * poll the status register to make sure that the reset
1528 		 * completed successfully. Due to internal HW FIFO flushes,
1529 		 * we must wait 10ms before the register will be valid.
1530 		 */
1531 		usleep_range(10000, 20000);
1532 		reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1533 		if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
1534 			rsd = true;
1535 			break;
1536 		}
1537 	}
1538 
1539 	if (flr)
1540 		usleep_range(10000, 20000);
1541 
1542 	if (!rsd)
1543 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1544 			vf->vf_id);
1545 	usleep_range(10000, 20000);
1546 
1547 	/* On initial reset, we don't have any queues to disable */
1548 	if (vf->lan_vsi_idx != 0)
1549 		i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1550 
1551 	i40e_cleanup_reset_vf(vf);
1552 
1553 	i40e_flush(hw);
1554 	usleep_range(20000, 40000);
1555 	clear_bit(I40E_VF_STATE_RESETTING, &vf->vf_states);
1556 
1557 	return true;
1558 }
1559 
1560 /**
1561  * i40e_reset_all_vfs
1562  * @pf: pointer to the PF structure
1563  * @flr: VFLR was issued or not
1564  *
1565  * Reset all allocated VFs in one go. First, tell the hardware to reset each
1566  * VF, then do all the waiting in one chunk, and finally finish restoring each
1567  * VF after the wait. This is useful during PF routines which need to reset
1568  * all VFs, as otherwise it must perform these resets in a serialized fashion.
1569  *
1570  * Returns true if any VFs were reset, and false otherwise.
1571  **/
i40e_reset_all_vfs(struct i40e_pf * pf,bool flr)1572 bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
1573 {
1574 	struct i40e_hw *hw = &pf->hw;
1575 	struct i40e_vf *vf;
1576 	int i, v;
1577 	u32 reg;
1578 
1579 	/* If we don't have any VFs, then there is nothing to reset */
1580 	if (!pf->num_alloc_vfs)
1581 		return false;
1582 
1583 	/* If VFs have been disabled, there is no need to reset */
1584 	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1585 		return false;
1586 
1587 	/* Begin reset on all VFs at once */
1588 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1589 		vf = &pf->vf[v];
1590 		/* If VF is being reset no need to trigger reset again */
1591 		if (!test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states))
1592 			i40e_trigger_vf_reset(&pf->vf[v], flr);
1593 	}
1594 
1595 	/* HW requires some time to make sure it can flush the FIFO for a VF
1596 	 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1597 	 * sequence to make sure that it has completed. We'll keep track of
1598 	 * the VFs using a simple iterator that increments once that VF has
1599 	 * finished resetting.
1600 	 */
1601 	for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1602 		usleep_range(10000, 20000);
1603 
1604 		/* Check each VF in sequence, beginning with the VF to fail
1605 		 * the previous check.
1606 		 */
1607 		while (v < pf->num_alloc_vfs) {
1608 			vf = &pf->vf[v];
1609 			if (!test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states)) {
1610 				reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1611 				if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
1612 					break;
1613 			}
1614 
1615 			/* If the current VF has finished resetting, move on
1616 			 * to the next VF in sequence.
1617 			 */
1618 			v++;
1619 		}
1620 	}
1621 
1622 	if (flr)
1623 		usleep_range(10000, 20000);
1624 
1625 	/* Display a warning if at least one VF didn't manage to reset in
1626 	 * time, but continue on with the operation.
1627 	 */
1628 	if (v < pf->num_alloc_vfs)
1629 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1630 			pf->vf[v].vf_id);
1631 	usleep_range(10000, 20000);
1632 
1633 	/* Begin disabling all the rings associated with VFs, but do not wait
1634 	 * between each VF.
1635 	 */
1636 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1637 		/* On initial reset, we don't have any queues to disable */
1638 		if (pf->vf[v].lan_vsi_idx == 0)
1639 			continue;
1640 
1641 		/* If VF is reset in another thread just continue */
1642 		if (test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states))
1643 			continue;
1644 
1645 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
1646 	}
1647 
1648 	/* Now that we've notified HW to disable all of the VF rings, wait
1649 	 * until they finish.
1650 	 */
1651 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1652 		/* On initial reset, we don't have any queues to disable */
1653 		if (pf->vf[v].lan_vsi_idx == 0)
1654 			continue;
1655 
1656 		/* If VF is reset in another thread just continue */
1657 		if (test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states))
1658 			continue;
1659 
1660 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
1661 	}
1662 
1663 	/* Hw may need up to 50ms to finish disabling the RX queues. We
1664 	 * minimize the wait by delaying only once for all VFs.
1665 	 */
1666 	mdelay(50);
1667 
1668 	/* Finish the reset on each VF */
1669 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1670 		/* If VF is reset in another thread just continue */
1671 		if (test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states))
1672 			continue;
1673 
1674 		i40e_cleanup_reset_vf(&pf->vf[v]);
1675 	}
1676 
1677 	i40e_flush(hw);
1678 	usleep_range(20000, 40000);
1679 	clear_bit(__I40E_VF_DISABLE, pf->state);
1680 
1681 	return true;
1682 }
1683 
1684 /**
1685  * i40e_free_vfs
1686  * @pf: pointer to the PF structure
1687  *
1688  * free VF resources
1689  **/
i40e_free_vfs(struct i40e_pf * pf)1690 void i40e_free_vfs(struct i40e_pf *pf)
1691 {
1692 	struct i40e_hw *hw = &pf->hw;
1693 	u32 reg_idx, bit_idx;
1694 	int i, tmp, vf_id;
1695 
1696 	if (!pf->vf)
1697 		return;
1698 
1699 	set_bit(__I40E_VFS_RELEASING, pf->state);
1700 	while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1701 		usleep_range(1000, 2000);
1702 
1703 	i40e_notify_client_of_vf_enable(pf, 0);
1704 
1705 	/* Disable IOV before freeing resources. This lets any VF drivers
1706 	 * running in the host get themselves cleaned up before we yank
1707 	 * the carpet out from underneath their feet.
1708 	 */
1709 	if (!pci_vfs_assigned(pf->pdev))
1710 		pci_disable_sriov(pf->pdev);
1711 	else
1712 		dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1713 
1714 	/* Amortize wait time by stopping all VFs at the same time */
1715 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1716 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1717 			continue;
1718 
1719 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
1720 	}
1721 
1722 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1723 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1724 			continue;
1725 
1726 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
1727 	}
1728 
1729 	/* free up VF resources */
1730 	tmp = pf->num_alloc_vfs;
1731 	pf->num_alloc_vfs = 0;
1732 	for (i = 0; i < tmp; i++) {
1733 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1734 			i40e_free_vf_res(&pf->vf[i]);
1735 		/* disable qp mappings */
1736 		i40e_disable_vf_mappings(&pf->vf[i]);
1737 	}
1738 
1739 	kfree(pf->vf);
1740 	pf->vf = NULL;
1741 
1742 	/* This check is for when the driver is unloaded while VFs are
1743 	 * assigned. Setting the number of VFs to 0 through sysfs is caught
1744 	 * before this function ever gets called.
1745 	 */
1746 	if (!pci_vfs_assigned(pf->pdev)) {
1747 		/* Acknowledge VFLR for all VFS. Without this, VFs will fail to
1748 		 * work correctly when SR-IOV gets re-enabled.
1749 		 */
1750 		for (vf_id = 0; vf_id < tmp; vf_id++) {
1751 			reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1752 			bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1753 			wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1754 		}
1755 	}
1756 	clear_bit(__I40E_VF_DISABLE, pf->state);
1757 	clear_bit(__I40E_VFS_RELEASING, pf->state);
1758 }
1759 
1760 #ifdef CONFIG_PCI_IOV
1761 /**
1762  * i40e_alloc_vfs
1763  * @pf: pointer to the PF structure
1764  * @num_alloc_vfs: number of VFs to allocate
1765  *
1766  * allocate VF resources
1767  **/
i40e_alloc_vfs(struct i40e_pf * pf,u16 num_alloc_vfs)1768 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1769 {
1770 	struct i40e_vf *vfs;
1771 	int i, ret = 0;
1772 
1773 	/* Disable interrupt 0 so we don't try to handle the VFLR. */
1774 	i40e_irq_dynamic_disable_icr0(pf);
1775 
1776 	/* Check to see if we're just allocating resources for extant VFs */
1777 	if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
1778 		ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1779 		if (ret) {
1780 			pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1781 			pf->num_alloc_vfs = 0;
1782 			goto err_iov;
1783 		}
1784 	}
1785 	/* allocate memory */
1786 	vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1787 	if (!vfs) {
1788 		ret = -ENOMEM;
1789 		goto err_alloc;
1790 	}
1791 	pf->vf = vfs;
1792 
1793 	/* apply default profile */
1794 	for (i = 0; i < num_alloc_vfs; i++) {
1795 		vfs[i].pf = pf;
1796 		vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
1797 		vfs[i].vf_id = i;
1798 
1799 		/* assign default capabilities */
1800 		set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1801 		vfs[i].spoofchk = true;
1802 
1803 		set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
1804 
1805 	}
1806 	pf->num_alloc_vfs = num_alloc_vfs;
1807 
1808 	/* VF resources get allocated during reset */
1809 	i40e_reset_all_vfs(pf, false);
1810 
1811 	i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
1812 
1813 err_alloc:
1814 	if (ret)
1815 		i40e_free_vfs(pf);
1816 err_iov:
1817 	/* Re-enable interrupt 0. */
1818 	i40e_irq_dynamic_enable_icr0(pf);
1819 	return ret;
1820 }
1821 
1822 #endif
1823 /**
1824  * i40e_pci_sriov_enable
1825  * @pdev: pointer to a pci_dev structure
1826  * @num_vfs: number of VFs to allocate
1827  *
1828  * Enable or change the number of VFs
1829  **/
i40e_pci_sriov_enable(struct pci_dev * pdev,int num_vfs)1830 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1831 {
1832 #ifdef CONFIG_PCI_IOV
1833 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1834 	int pre_existing_vfs = pci_num_vf(pdev);
1835 	int err = 0;
1836 
1837 	if (test_bit(__I40E_TESTING, pf->state)) {
1838 		dev_warn(&pdev->dev,
1839 			 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1840 		err = -EPERM;
1841 		goto err_out;
1842 	}
1843 
1844 	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1845 		i40e_free_vfs(pf);
1846 	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1847 		goto out;
1848 
1849 	if (num_vfs > pf->num_req_vfs) {
1850 		dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1851 			 num_vfs, pf->num_req_vfs);
1852 		err = -EPERM;
1853 		goto err_out;
1854 	}
1855 
1856 	dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1857 	err = i40e_alloc_vfs(pf, num_vfs);
1858 	if (err) {
1859 		dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1860 		goto err_out;
1861 	}
1862 
1863 out:
1864 	return num_vfs;
1865 
1866 err_out:
1867 	return err;
1868 #endif
1869 	return 0;
1870 }
1871 
1872 /**
1873  * i40e_pci_sriov_configure
1874  * @pdev: pointer to a pci_dev structure
1875  * @num_vfs: number of VFs to allocate
1876  *
1877  * Enable or change the number of VFs. Called when the user updates the number
1878  * of VFs in sysfs.
1879  **/
i40e_pci_sriov_configure(struct pci_dev * pdev,int num_vfs)1880 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1881 {
1882 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1883 	int ret = 0;
1884 
1885 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
1886 		dev_warn(&pdev->dev, "Unable to configure VFs, other operation is pending.\n");
1887 		return -EAGAIN;
1888 	}
1889 
1890 	if (num_vfs) {
1891 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1892 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1893 			i40e_do_reset_safe(pf, I40E_PF_RESET_AND_REBUILD_FLAG);
1894 		}
1895 		ret = i40e_pci_sriov_enable(pdev, num_vfs);
1896 		goto sriov_configure_out;
1897 	}
1898 
1899 	if (!pci_vfs_assigned(pf->pdev)) {
1900 		i40e_free_vfs(pf);
1901 		pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1902 		i40e_do_reset_safe(pf, I40E_PF_RESET_AND_REBUILD_FLAG);
1903 	} else {
1904 		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1905 		ret = -EINVAL;
1906 		goto sriov_configure_out;
1907 	}
1908 sriov_configure_out:
1909 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
1910 	return ret;
1911 }
1912 
1913 /***********************virtual channel routines******************/
1914 
1915 /**
1916  * i40e_vc_send_msg_to_vf
1917  * @vf: pointer to the VF info
1918  * @v_opcode: virtual channel opcode
1919  * @v_retval: virtual channel return value
1920  * @msg: pointer to the msg buffer
1921  * @msglen: msg length
1922  *
1923  * send msg to VF
1924  **/
i40e_vc_send_msg_to_vf(struct i40e_vf * vf,u32 v_opcode,u32 v_retval,u8 * msg,u16 msglen)1925 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1926 				  u32 v_retval, u8 *msg, u16 msglen)
1927 {
1928 	struct i40e_pf *pf;
1929 	struct i40e_hw *hw;
1930 	int abs_vf_id;
1931 	i40e_status aq_ret;
1932 
1933 	/* validate the request */
1934 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1935 		return -EINVAL;
1936 
1937 	pf = vf->pf;
1938 	hw = &pf->hw;
1939 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1940 
1941 	aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,	v_opcode, v_retval,
1942 					msg, msglen, NULL);
1943 	if (aq_ret) {
1944 		dev_info(&pf->pdev->dev,
1945 			 "Unable to send the message to VF %d aq_err %d\n",
1946 			 vf->vf_id, pf->hw.aq.asq_last_status);
1947 		return -EIO;
1948 	}
1949 
1950 	return 0;
1951 }
1952 
1953 /**
1954  * i40e_vc_send_resp_to_vf
1955  * @vf: pointer to the VF info
1956  * @opcode: operation code
1957  * @retval: return value
1958  *
1959  * send resp msg to VF
1960  **/
i40e_vc_send_resp_to_vf(struct i40e_vf * vf,enum virtchnl_ops opcode,i40e_status retval)1961 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1962 				   enum virtchnl_ops opcode,
1963 				   i40e_status retval)
1964 {
1965 	return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1966 }
1967 
1968 /**
1969  * i40e_sync_vf_state
1970  * @vf: pointer to the VF info
1971  * @state: VF state
1972  *
1973  * Called from a VF message to synchronize the service with a potential
1974  * VF reset state
1975  **/
i40e_sync_vf_state(struct i40e_vf * vf,enum i40e_vf_states state)1976 static bool i40e_sync_vf_state(struct i40e_vf *vf, enum i40e_vf_states state)
1977 {
1978 	int i;
1979 
1980 	/* When handling some messages, it needs VF state to be set.
1981 	 * It is possible that this flag is cleared during VF reset,
1982 	 * so there is a need to wait until the end of the reset to
1983 	 * handle the request message correctly.
1984 	 */
1985 	for (i = 0; i < I40E_VF_STATE_WAIT_COUNT; i++) {
1986 		if (test_bit(state, &vf->vf_states))
1987 			return true;
1988 		usleep_range(10000, 20000);
1989 	}
1990 
1991 	return test_bit(state, &vf->vf_states);
1992 }
1993 
1994 /**
1995  * i40e_vc_get_version_msg
1996  * @vf: pointer to the VF info
1997  * @msg: pointer to the msg buffer
1998  *
1999  * called from the VF to request the API version used by the PF
2000  **/
i40e_vc_get_version_msg(struct i40e_vf * vf,u8 * msg)2001 static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
2002 {
2003 	struct virtchnl_version_info info = {
2004 		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
2005 	};
2006 
2007 	vf->vf_ver = *(struct virtchnl_version_info *)msg;
2008 	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
2009 	if (VF_IS_V10(&vf->vf_ver))
2010 		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
2011 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
2012 				      I40E_SUCCESS, (u8 *)&info,
2013 				      sizeof(struct virtchnl_version_info));
2014 }
2015 
2016 /**
2017  * i40e_del_qch - delete all the additional VSIs created as a part of ADq
2018  * @vf: pointer to VF structure
2019  **/
i40e_del_qch(struct i40e_vf * vf)2020 static void i40e_del_qch(struct i40e_vf *vf)
2021 {
2022 	struct i40e_pf *pf = vf->pf;
2023 	int i;
2024 
2025 	/* first element in the array belongs to primary VF VSI and we shouldn't
2026 	 * delete it. We should however delete the rest of the VSIs created
2027 	 */
2028 	for (i = 1; i < vf->num_tc; i++) {
2029 		if (vf->ch[i].vsi_idx) {
2030 			i40e_vsi_release(pf->vsi[vf->ch[i].vsi_idx]);
2031 			vf->ch[i].vsi_idx = 0;
2032 			vf->ch[i].vsi_id = 0;
2033 		}
2034 	}
2035 }
2036 
2037 /**
2038  * i40e_vc_get_max_frame_size
2039  * @vf: pointer to the VF
2040  *
2041  * Max frame size is determined based on the current port's max frame size and
2042  * whether a port VLAN is configured on this VF. The VF is not aware whether
2043  * it's in a port VLAN so the PF needs to account for this in max frame size
2044  * checks and sending the max frame size to the VF.
2045  **/
i40e_vc_get_max_frame_size(struct i40e_vf * vf)2046 static u16 i40e_vc_get_max_frame_size(struct i40e_vf *vf)
2047 {
2048 	u16 max_frame_size = vf->pf->hw.phy.link_info.max_frame_size;
2049 
2050 	if (vf->port_vlan_id)
2051 		max_frame_size -= VLAN_HLEN;
2052 
2053 	return max_frame_size;
2054 }
2055 
2056 /**
2057  * i40e_vc_get_vf_resources_msg
2058  * @vf: pointer to the VF info
2059  * @msg: pointer to the msg buffer
2060  *
2061  * called from the VF to request its resources
2062  **/
i40e_vc_get_vf_resources_msg(struct i40e_vf * vf,u8 * msg)2063 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
2064 {
2065 	struct virtchnl_vf_resource *vfres = NULL;
2066 	struct i40e_pf *pf = vf->pf;
2067 	i40e_status aq_ret = 0;
2068 	struct i40e_vsi *vsi;
2069 	int num_vsis = 1;
2070 	size_t len = 0;
2071 	int ret;
2072 
2073 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_INIT)) {
2074 		aq_ret = I40E_ERR_PARAM;
2075 		goto err;
2076 	}
2077 
2078 	len = struct_size(vfres, vsi_res, num_vsis);
2079 	vfres = kzalloc(len, GFP_KERNEL);
2080 	if (!vfres) {
2081 		aq_ret = I40E_ERR_NO_MEMORY;
2082 		len = 0;
2083 		goto err;
2084 	}
2085 	if (VF_IS_V11(&vf->vf_ver))
2086 		vf->driver_caps = *(u32 *)msg;
2087 	else
2088 		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
2089 				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
2090 				  VIRTCHNL_VF_OFFLOAD_VLAN;
2091 
2092 	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
2093 	vsi = pf->vsi[vf->lan_vsi_idx];
2094 	if (!vsi->info.pvid)
2095 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
2096 
2097 	if (i40e_vf_client_capable(pf, vf->vf_id) &&
2098 	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
2099 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
2100 		set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
2101 	} else {
2102 		clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
2103 	}
2104 
2105 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2106 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
2107 	} else {
2108 		if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
2109 		    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
2110 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
2111 		else
2112 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
2113 	}
2114 
2115 	if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
2116 		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
2117 			vfres->vf_cap_flags |=
2118 				VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
2119 	}
2120 
2121 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
2122 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
2123 
2124 	if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
2125 	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
2126 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
2127 
2128 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
2129 		if (pf->flags & I40E_FLAG_MFP_ENABLED) {
2130 			dev_err(&pf->pdev->dev,
2131 				"VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
2132 				 vf->vf_id);
2133 			aq_ret = I40E_ERR_PARAM;
2134 			goto err;
2135 		}
2136 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
2137 	}
2138 
2139 	if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
2140 		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2141 			vfres->vf_cap_flags |=
2142 					VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
2143 	}
2144 
2145 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
2146 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
2147 
2148 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)
2149 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADQ;
2150 
2151 	vfres->num_vsis = num_vsis;
2152 	vfres->num_queue_pairs = vf->num_queue_pairs;
2153 	vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
2154 	vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
2155 	vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
2156 	vfres->max_mtu = i40e_vc_get_max_frame_size(vf);
2157 
2158 	if (vf->lan_vsi_idx) {
2159 		vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
2160 		vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
2161 		vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
2162 		/* VFs only use TC 0 */
2163 		vfres->vsi_res[0].qset_handle
2164 					  = le16_to_cpu(vsi->info.qs_handle[0]);
2165 		ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2166 				vf->default_lan_addr.addr);
2167 	}
2168 	set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
2169 
2170 err:
2171 	/* send the response back to the VF */
2172 	ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
2173 				     aq_ret, (u8 *)vfres, len);
2174 
2175 	kfree(vfres);
2176 	return ret;
2177 }
2178 
2179 /**
2180  * i40e_vc_config_promiscuous_mode_msg
2181  * @vf: pointer to the VF info
2182  * @msg: pointer to the msg buffer
2183  *
2184  * called from the VF to configure the promiscuous mode of
2185  * VF vsis
2186  **/
i40e_vc_config_promiscuous_mode_msg(struct i40e_vf * vf,u8 * msg)2187 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf, u8 *msg)
2188 {
2189 	struct virtchnl_promisc_info *info =
2190 	    (struct virtchnl_promisc_info *)msg;
2191 	struct i40e_pf *pf = vf->pf;
2192 	i40e_status aq_ret = 0;
2193 	bool allmulti = false;
2194 	bool alluni = false;
2195 
2196 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2197 		aq_ret = I40E_ERR_PARAM;
2198 		goto err_out;
2199 	}
2200 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2201 		dev_err(&pf->pdev->dev,
2202 			"Unprivileged VF %d is attempting to configure promiscuous mode\n",
2203 			vf->vf_id);
2204 
2205 		/* Lie to the VF on purpose, because this is an error we can
2206 		 * ignore. Unprivileged VF is not a virtual channel error.
2207 		 */
2208 		aq_ret = 0;
2209 		goto err_out;
2210 	}
2211 
2212 	if (info->flags > I40E_MAX_VF_PROMISC_FLAGS) {
2213 		aq_ret = I40E_ERR_PARAM;
2214 		goto err_out;
2215 	}
2216 
2217 	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
2218 		aq_ret = I40E_ERR_PARAM;
2219 		goto err_out;
2220 	}
2221 
2222 	/* Multicast promiscuous handling*/
2223 	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
2224 		allmulti = true;
2225 
2226 	if (info->flags & FLAG_VF_UNICAST_PROMISC)
2227 		alluni = true;
2228 	aq_ret = i40e_config_vf_promiscuous_mode(vf, info->vsi_id, allmulti,
2229 						 alluni);
2230 	if (aq_ret)
2231 		goto err_out;
2232 
2233 	if (allmulti) {
2234 		if (!test_and_set_bit(I40E_VF_STATE_MC_PROMISC,
2235 				      &vf->vf_states))
2236 			dev_info(&pf->pdev->dev,
2237 				 "VF %d successfully set multicast promiscuous mode\n",
2238 				 vf->vf_id);
2239 	} else if (test_and_clear_bit(I40E_VF_STATE_MC_PROMISC,
2240 				      &vf->vf_states))
2241 		dev_info(&pf->pdev->dev,
2242 			 "VF %d successfully unset multicast promiscuous mode\n",
2243 			 vf->vf_id);
2244 
2245 	if (alluni) {
2246 		if (!test_and_set_bit(I40E_VF_STATE_UC_PROMISC,
2247 				      &vf->vf_states))
2248 			dev_info(&pf->pdev->dev,
2249 				 "VF %d successfully set unicast promiscuous mode\n",
2250 				 vf->vf_id);
2251 	} else if (test_and_clear_bit(I40E_VF_STATE_UC_PROMISC,
2252 				      &vf->vf_states))
2253 		dev_info(&pf->pdev->dev,
2254 			 "VF %d successfully unset unicast promiscuous mode\n",
2255 			 vf->vf_id);
2256 
2257 err_out:
2258 	/* send the response to the VF */
2259 	return i40e_vc_send_resp_to_vf(vf,
2260 				       VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
2261 				       aq_ret);
2262 }
2263 
2264 /**
2265  * i40e_vc_config_queues_msg
2266  * @vf: pointer to the VF info
2267  * @msg: pointer to the msg buffer
2268  *
2269  * called from the VF to configure the rx/tx
2270  * queues
2271  **/
i40e_vc_config_queues_msg(struct i40e_vf * vf,u8 * msg)2272 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg)
2273 {
2274 	struct virtchnl_vsi_queue_config_info *qci =
2275 	    (struct virtchnl_vsi_queue_config_info *)msg;
2276 	struct virtchnl_queue_pair_info *qpi;
2277 	u16 vsi_id, vsi_queue_id = 0;
2278 	struct i40e_pf *pf = vf->pf;
2279 	i40e_status aq_ret = 0;
2280 	int i, j = 0, idx = 0;
2281 	struct i40e_vsi *vsi;
2282 	u16 num_qps_all = 0;
2283 
2284 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2285 		aq_ret = I40E_ERR_PARAM;
2286 		goto error_param;
2287 	}
2288 
2289 	if (!i40e_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
2290 		aq_ret = I40E_ERR_PARAM;
2291 		goto error_param;
2292 	}
2293 
2294 	if (qci->num_queue_pairs > I40E_MAX_VF_QUEUES) {
2295 		aq_ret = I40E_ERR_PARAM;
2296 		goto error_param;
2297 	}
2298 
2299 	if (vf->adq_enabled) {
2300 		for (i = 0; i < vf->num_tc; i++)
2301 			num_qps_all += vf->ch[i].num_qps;
2302 		if (num_qps_all != qci->num_queue_pairs) {
2303 			aq_ret = I40E_ERR_PARAM;
2304 			goto error_param;
2305 		}
2306 	}
2307 
2308 	vsi_id = qci->vsi_id;
2309 
2310 	for (i = 0; i < qci->num_queue_pairs; i++) {
2311 		qpi = &qci->qpair[i];
2312 
2313 		if (!vf->adq_enabled) {
2314 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
2315 						      qpi->txq.queue_id)) {
2316 				aq_ret = I40E_ERR_PARAM;
2317 				goto error_param;
2318 			}
2319 
2320 			vsi_queue_id = qpi->txq.queue_id;
2321 
2322 			if (qpi->txq.vsi_id != qci->vsi_id ||
2323 			    qpi->rxq.vsi_id != qci->vsi_id ||
2324 			    qpi->rxq.queue_id != vsi_queue_id) {
2325 				aq_ret = I40E_ERR_PARAM;
2326 				goto error_param;
2327 			}
2328 		}
2329 
2330 		if (vf->adq_enabled) {
2331 			if (idx >= ARRAY_SIZE(vf->ch)) {
2332 				aq_ret = I40E_ERR_NO_AVAILABLE_VSI;
2333 				goto error_param;
2334 			}
2335 			vsi_id = vf->ch[idx].vsi_id;
2336 		}
2337 
2338 		if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
2339 					     &qpi->rxq) ||
2340 		    i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
2341 					     &qpi->txq)) {
2342 			aq_ret = I40E_ERR_PARAM;
2343 			goto error_param;
2344 		}
2345 
2346 		/* For ADq there can be up to 4 VSIs with max 4 queues each.
2347 		 * VF does not know about these additional VSIs and all
2348 		 * it cares is about its own queues. PF configures these queues
2349 		 * to its appropriate VSIs based on TC mapping
2350 		 */
2351 		if (vf->adq_enabled) {
2352 			if (idx >= ARRAY_SIZE(vf->ch)) {
2353 				aq_ret = I40E_ERR_NO_AVAILABLE_VSI;
2354 				goto error_param;
2355 			}
2356 			if (j == (vf->ch[idx].num_qps - 1)) {
2357 				idx++;
2358 				j = 0; /* resetting the queue count */
2359 				vsi_queue_id = 0;
2360 			} else {
2361 				j++;
2362 				vsi_queue_id++;
2363 			}
2364 		}
2365 	}
2366 	/* set vsi num_queue_pairs in use to num configured by VF */
2367 	if (!vf->adq_enabled) {
2368 		pf->vsi[vf->lan_vsi_idx]->num_queue_pairs =
2369 			qci->num_queue_pairs;
2370 	} else {
2371 		for (i = 0; i < vf->num_tc; i++) {
2372 			vsi = pf->vsi[vf->ch[i].vsi_idx];
2373 			vsi->num_queue_pairs = vf->ch[i].num_qps;
2374 
2375 			if (i40e_update_adq_vsi_queues(vsi, i)) {
2376 				aq_ret = I40E_ERR_CONFIG;
2377 				goto error_param;
2378 			}
2379 		}
2380 	}
2381 
2382 error_param:
2383 	/* send the response to the VF */
2384 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2385 				       aq_ret);
2386 }
2387 
2388 /**
2389  * i40e_validate_queue_map - check queue map is valid
2390  * @vf: the VF structure pointer
2391  * @vsi_id: vsi id
2392  * @queuemap: Tx or Rx queue map
2393  *
2394  * check if Tx or Rx queue map is valid
2395  **/
i40e_validate_queue_map(struct i40e_vf * vf,u16 vsi_id,unsigned long queuemap)2396 static int i40e_validate_queue_map(struct i40e_vf *vf, u16 vsi_id,
2397 				   unsigned long queuemap)
2398 {
2399 	u16 vsi_queue_id, queue_id;
2400 
2401 	for_each_set_bit(vsi_queue_id, &queuemap, I40E_MAX_VSI_QP) {
2402 		if (vf->adq_enabled) {
2403 			vsi_id = vf->ch[vsi_queue_id / I40E_MAX_VF_VSI].vsi_id;
2404 			queue_id = (vsi_queue_id % I40E_DEFAULT_QUEUES_PER_VF);
2405 		} else {
2406 			queue_id = vsi_queue_id;
2407 		}
2408 
2409 		if (!i40e_vc_isvalid_queue_id(vf, vsi_id, queue_id))
2410 			return -EINVAL;
2411 	}
2412 
2413 	return 0;
2414 }
2415 
2416 /**
2417  * i40e_vc_config_irq_map_msg
2418  * @vf: pointer to the VF info
2419  * @msg: pointer to the msg buffer
2420  *
2421  * called from the VF to configure the irq to
2422  * queue map
2423  **/
i40e_vc_config_irq_map_msg(struct i40e_vf * vf,u8 * msg)2424 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg)
2425 {
2426 	struct virtchnl_irq_map_info *irqmap_info =
2427 	    (struct virtchnl_irq_map_info *)msg;
2428 	struct virtchnl_vector_map *map;
2429 	u16 vsi_id;
2430 	i40e_status aq_ret = 0;
2431 	int i;
2432 
2433 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2434 		aq_ret = I40E_ERR_PARAM;
2435 		goto error_param;
2436 	}
2437 
2438 	if (irqmap_info->num_vectors >
2439 	    vf->pf->hw.func_caps.num_msix_vectors_vf) {
2440 		aq_ret = I40E_ERR_PARAM;
2441 		goto error_param;
2442 	}
2443 
2444 	for (i = 0; i < irqmap_info->num_vectors; i++) {
2445 		map = &irqmap_info->vecmap[i];
2446 		/* validate msg params */
2447 		if (!i40e_vc_isvalid_vector_id(vf, map->vector_id) ||
2448 		    !i40e_vc_isvalid_vsi_id(vf, map->vsi_id)) {
2449 			aq_ret = I40E_ERR_PARAM;
2450 			goto error_param;
2451 		}
2452 		vsi_id = map->vsi_id;
2453 
2454 		if (i40e_validate_queue_map(vf, vsi_id, map->rxq_map)) {
2455 			aq_ret = I40E_ERR_PARAM;
2456 			goto error_param;
2457 		}
2458 
2459 		if (i40e_validate_queue_map(vf, vsi_id, map->txq_map)) {
2460 			aq_ret = I40E_ERR_PARAM;
2461 			goto error_param;
2462 		}
2463 
2464 		i40e_config_irq_link_list(vf, vsi_id, map);
2465 	}
2466 error_param:
2467 	/* send the response to the VF */
2468 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
2469 				       aq_ret);
2470 }
2471 
2472 /**
2473  * i40e_ctrl_vf_tx_rings
2474  * @vsi: the SRIOV VSI being configured
2475  * @q_map: bit map of the queues to be enabled
2476  * @enable: start or stop the queue
2477  **/
i40e_ctrl_vf_tx_rings(struct i40e_vsi * vsi,unsigned long q_map,bool enable)2478 static int i40e_ctrl_vf_tx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2479 				 bool enable)
2480 {
2481 	struct i40e_pf *pf = vsi->back;
2482 	int ret = 0;
2483 	u16 q_id;
2484 
2485 	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2486 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
2487 					     vsi->base_queue + q_id,
2488 					     false /*is xdp*/, enable);
2489 		if (ret)
2490 			break;
2491 	}
2492 	return ret;
2493 }
2494 
2495 /**
2496  * i40e_ctrl_vf_rx_rings
2497  * @vsi: the SRIOV VSI being configured
2498  * @q_map: bit map of the queues to be enabled
2499  * @enable: start or stop the queue
2500  **/
i40e_ctrl_vf_rx_rings(struct i40e_vsi * vsi,unsigned long q_map,bool enable)2501 static int i40e_ctrl_vf_rx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2502 				 bool enable)
2503 {
2504 	struct i40e_pf *pf = vsi->back;
2505 	int ret = 0;
2506 	u16 q_id;
2507 
2508 	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2509 		ret = i40e_control_wait_rx_q(pf, vsi->base_queue + q_id,
2510 					     enable);
2511 		if (ret)
2512 			break;
2513 	}
2514 	return ret;
2515 }
2516 
2517 /**
2518  * i40e_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTHCHNL
2519  * @vqs: virtchnl_queue_select structure containing bitmaps to validate
2520  *
2521  * Returns true if validation was successful, else false.
2522  */
i40e_vc_validate_vqs_bitmaps(struct virtchnl_queue_select * vqs)2523 static bool i40e_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
2524 {
2525 	if ((!vqs->rx_queues && !vqs->tx_queues) ||
2526 	    vqs->rx_queues >= BIT(I40E_MAX_VF_QUEUES) ||
2527 	    vqs->tx_queues >= BIT(I40E_MAX_VF_QUEUES))
2528 		return false;
2529 
2530 	return true;
2531 }
2532 
2533 /**
2534  * i40e_vc_enable_queues_msg
2535  * @vf: pointer to the VF info
2536  * @msg: pointer to the msg buffer
2537  *
2538  * called from the VF to enable all or specific queue(s)
2539  **/
i40e_vc_enable_queues_msg(struct i40e_vf * vf,u8 * msg)2540 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg)
2541 {
2542 	struct virtchnl_queue_select *vqs =
2543 	    (struct virtchnl_queue_select *)msg;
2544 	struct i40e_pf *pf = vf->pf;
2545 	i40e_status aq_ret = 0;
2546 	int i;
2547 
2548 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2549 		aq_ret = I40E_ERR_PARAM;
2550 		goto error_param;
2551 	}
2552 
2553 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2554 		aq_ret = I40E_ERR_PARAM;
2555 		goto error_param;
2556 	}
2557 
2558 	if (!i40e_vc_validate_vqs_bitmaps(vqs)) {
2559 		aq_ret = I40E_ERR_PARAM;
2560 		goto error_param;
2561 	}
2562 
2563 	/* Use the queue bit map sent by the VF */
2564 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2565 				  true)) {
2566 		aq_ret = I40E_ERR_TIMEOUT;
2567 		goto error_param;
2568 	}
2569 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2570 				  true)) {
2571 		aq_ret = I40E_ERR_TIMEOUT;
2572 		goto error_param;
2573 	}
2574 
2575 	/* need to start the rings for additional ADq VSI's as well */
2576 	if (vf->adq_enabled) {
2577 		/* zero belongs to LAN VSI */
2578 		for (i = 1; i < vf->num_tc; i++) {
2579 			if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
2580 				aq_ret = I40E_ERR_TIMEOUT;
2581 		}
2582 	}
2583 
2584 error_param:
2585 	/* send the response to the VF */
2586 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
2587 				       aq_ret);
2588 }
2589 
2590 /**
2591  * i40e_vc_disable_queues_msg
2592  * @vf: pointer to the VF info
2593  * @msg: pointer to the msg buffer
2594  *
2595  * called from the VF to disable all or specific
2596  * queue(s)
2597  **/
i40e_vc_disable_queues_msg(struct i40e_vf * vf,u8 * msg)2598 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg)
2599 {
2600 	struct virtchnl_queue_select *vqs =
2601 	    (struct virtchnl_queue_select *)msg;
2602 	struct i40e_pf *pf = vf->pf;
2603 	i40e_status aq_ret = 0;
2604 
2605 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2606 		aq_ret = I40E_ERR_PARAM;
2607 		goto error_param;
2608 	}
2609 
2610 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2611 		aq_ret = I40E_ERR_PARAM;
2612 		goto error_param;
2613 	}
2614 
2615 	if (!i40e_vc_validate_vqs_bitmaps(vqs)) {
2616 		aq_ret = I40E_ERR_PARAM;
2617 		goto error_param;
2618 	}
2619 
2620 	/* Use the queue bit map sent by the VF */
2621 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2622 				  false)) {
2623 		aq_ret = I40E_ERR_TIMEOUT;
2624 		goto error_param;
2625 	}
2626 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2627 				  false)) {
2628 		aq_ret = I40E_ERR_TIMEOUT;
2629 		goto error_param;
2630 	}
2631 error_param:
2632 	/* send the response to the VF */
2633 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
2634 				       aq_ret);
2635 }
2636 
2637 /**
2638  * i40e_check_enough_queue - find big enough queue number
2639  * @vf: pointer to the VF info
2640  * @needed: the number of items needed
2641  *
2642  * Returns the base item index of the queue, or negative for error
2643  **/
i40e_check_enough_queue(struct i40e_vf * vf,u16 needed)2644 static int i40e_check_enough_queue(struct i40e_vf *vf, u16 needed)
2645 {
2646 	unsigned int  i, cur_queues, more, pool_size;
2647 	struct i40e_lump_tracking *pile;
2648 	struct i40e_pf *pf = vf->pf;
2649 	struct i40e_vsi *vsi;
2650 
2651 	vsi = pf->vsi[vf->lan_vsi_idx];
2652 	cur_queues = vsi->alloc_queue_pairs;
2653 
2654 	/* if current allocated queues are enough for need */
2655 	if (cur_queues >= needed)
2656 		return vsi->base_queue;
2657 
2658 	pile = pf->qp_pile;
2659 	if (cur_queues > 0) {
2660 		/* if the allocated queues are not zero
2661 		 * just check if there are enough queues for more
2662 		 * behind the allocated queues.
2663 		 */
2664 		more = needed - cur_queues;
2665 		for (i = vsi->base_queue + cur_queues;
2666 			i < pile->num_entries; i++) {
2667 			if (pile->list[i] & I40E_PILE_VALID_BIT)
2668 				break;
2669 
2670 			if (more-- == 1)
2671 				/* there is enough */
2672 				return vsi->base_queue;
2673 		}
2674 	}
2675 
2676 	pool_size = 0;
2677 	for (i = 0; i < pile->num_entries; i++) {
2678 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
2679 			pool_size = 0;
2680 			continue;
2681 		}
2682 		if (needed <= ++pool_size)
2683 			/* there is enough */
2684 			return i;
2685 	}
2686 
2687 	return -ENOMEM;
2688 }
2689 
2690 /**
2691  * i40e_vc_request_queues_msg
2692  * @vf: pointer to the VF info
2693  * @msg: pointer to the msg buffer
2694  *
2695  * VFs get a default number of queues but can use this message to request a
2696  * different number.  If the request is successful, PF will reset the VF and
2697  * return 0.  If unsuccessful, PF will send message informing VF of number of
2698  * available queues and return result of sending VF a message.
2699  **/
i40e_vc_request_queues_msg(struct i40e_vf * vf,u8 * msg)2700 static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg)
2701 {
2702 	struct virtchnl_vf_res_request *vfres =
2703 		(struct virtchnl_vf_res_request *)msg;
2704 	u16 req_pairs = vfres->num_queue_pairs;
2705 	u8 cur_pairs = vf->num_queue_pairs;
2706 	struct i40e_pf *pf = vf->pf;
2707 
2708 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE))
2709 		return -EINVAL;
2710 
2711 	if (req_pairs > I40E_MAX_VF_QUEUES) {
2712 		dev_err(&pf->pdev->dev,
2713 			"VF %d tried to request more than %d queues.\n",
2714 			vf->vf_id,
2715 			I40E_MAX_VF_QUEUES);
2716 		vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
2717 	} else if (req_pairs - cur_pairs > pf->queues_left) {
2718 		dev_warn(&pf->pdev->dev,
2719 			 "VF %d requested %d more queues, but only %d left.\n",
2720 			 vf->vf_id,
2721 			 req_pairs - cur_pairs,
2722 			 pf->queues_left);
2723 		vfres->num_queue_pairs = pf->queues_left + cur_pairs;
2724 	} else if (i40e_check_enough_queue(vf, req_pairs) < 0) {
2725 		dev_warn(&pf->pdev->dev,
2726 			 "VF %d requested %d more queues, but there is not enough for it.\n",
2727 			 vf->vf_id,
2728 			 req_pairs - cur_pairs);
2729 		vfres->num_queue_pairs = cur_pairs;
2730 	} else {
2731 		/* successful request */
2732 		vf->num_req_queues = req_pairs;
2733 		i40e_vc_reset_vf(vf, true);
2734 		return 0;
2735 	}
2736 
2737 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
2738 				      (u8 *)vfres, sizeof(*vfres));
2739 }
2740 
2741 /**
2742  * i40e_vc_get_stats_msg
2743  * @vf: pointer to the VF info
2744  * @msg: pointer to the msg buffer
2745  *
2746  * called from the VF to get vsi stats
2747  **/
i40e_vc_get_stats_msg(struct i40e_vf * vf,u8 * msg)2748 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg)
2749 {
2750 	struct virtchnl_queue_select *vqs =
2751 	    (struct virtchnl_queue_select *)msg;
2752 	struct i40e_pf *pf = vf->pf;
2753 	struct i40e_eth_stats stats;
2754 	i40e_status aq_ret = 0;
2755 	struct i40e_vsi *vsi;
2756 
2757 	memset(&stats, 0, sizeof(struct i40e_eth_stats));
2758 
2759 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2760 		aq_ret = I40E_ERR_PARAM;
2761 		goto error_param;
2762 	}
2763 
2764 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2765 		aq_ret = I40E_ERR_PARAM;
2766 		goto error_param;
2767 	}
2768 
2769 	vsi = pf->vsi[vf->lan_vsi_idx];
2770 	if (!vsi) {
2771 		aq_ret = I40E_ERR_PARAM;
2772 		goto error_param;
2773 	}
2774 	i40e_update_eth_stats(vsi);
2775 	stats = vsi->eth_stats;
2776 
2777 error_param:
2778 	/* send the response back to the VF */
2779 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
2780 				      (u8 *)&stats, sizeof(stats));
2781 }
2782 
2783 /* If the VF is not trusted restrict the number of MAC/VLAN it can program
2784  * MAC filters: 16 for multicast, 1 for MAC, 1 for broadcast
2785  */
2786 #define I40E_VC_MAX_MAC_ADDR_PER_VF (16 + 1 + 1)
2787 #define I40E_VC_MAX_VLAN_PER_VF 16
2788 
2789 /**
2790  * i40e_check_vf_permission
2791  * @vf: pointer to the VF info
2792  * @al: MAC address list from virtchnl
2793  *
2794  * Check that the given list of MAC addresses is allowed. Will return -EPERM
2795  * if any address in the list is not valid. Checks the following conditions:
2796  *
2797  * 1) broadcast and zero addresses are never valid
2798  * 2) unicast addresses are not allowed if the VMM has administratively set
2799  *    the VF MAC address, unless the VF is marked as privileged.
2800  * 3) There is enough space to add all the addresses.
2801  *
2802  * Note that to guarantee consistency, it is expected this function be called
2803  * while holding the mac_filter_hash_lock, as otherwise the current number of
2804  * addresses might not be accurate.
2805  **/
i40e_check_vf_permission(struct i40e_vf * vf,struct virtchnl_ether_addr_list * al)2806 static inline int i40e_check_vf_permission(struct i40e_vf *vf,
2807 					   struct virtchnl_ether_addr_list *al)
2808 {
2809 	struct i40e_pf *pf = vf->pf;
2810 	struct i40e_vsi *vsi = pf->vsi[vf->lan_vsi_idx];
2811 	int mac2add_cnt = 0;
2812 	int i;
2813 
2814 	for (i = 0; i < al->num_elements; i++) {
2815 		struct i40e_mac_filter *f;
2816 		u8 *addr = al->list[i].addr;
2817 
2818 		if (is_broadcast_ether_addr(addr) ||
2819 		    is_zero_ether_addr(addr)) {
2820 			dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
2821 				addr);
2822 			return I40E_ERR_INVALID_MAC_ADDR;
2823 		}
2824 
2825 		/* If the host VMM administrator has set the VF MAC address
2826 		 * administratively via the ndo_set_vf_mac command then deny
2827 		 * permission to the VF to add or delete unicast MAC addresses.
2828 		 * Unless the VF is privileged and then it can do whatever.
2829 		 * The VF may request to set the MAC address filter already
2830 		 * assigned to it so do not return an error in that case.
2831 		 */
2832 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2833 		    !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
2834 		    !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
2835 			dev_err(&pf->pdev->dev,
2836 				"VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
2837 			return -EPERM;
2838 		}
2839 
2840 		/*count filters that really will be added*/
2841 		f = i40e_find_mac(vsi, addr);
2842 		if (!f)
2843 			++mac2add_cnt;
2844 	}
2845 
2846 	/* If this VF is not privileged, then we can't add more than a limited
2847 	 * number of addresses. Check to make sure that the additions do not
2848 	 * push us over the limit.
2849 	 */
2850 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2851 	    (i40e_count_filters(vsi) + mac2add_cnt) >
2852 		    I40E_VC_MAX_MAC_ADDR_PER_VF) {
2853 		dev_err(&pf->pdev->dev,
2854 			"Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
2855 		return -EPERM;
2856 	}
2857 	return 0;
2858 }
2859 
2860 /**
2861  * i40e_vc_add_mac_addr_msg
2862  * @vf: pointer to the VF info
2863  * @msg: pointer to the msg buffer
2864  *
2865  * add guest mac address filter
2866  **/
i40e_vc_add_mac_addr_msg(struct i40e_vf * vf,u8 * msg)2867 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2868 {
2869 	struct virtchnl_ether_addr_list *al =
2870 	    (struct virtchnl_ether_addr_list *)msg;
2871 	struct i40e_pf *pf = vf->pf;
2872 	struct i40e_vsi *vsi = NULL;
2873 	i40e_status ret = 0;
2874 	int i;
2875 
2876 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
2877 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2878 		ret = I40E_ERR_PARAM;
2879 		goto error_param;
2880 	}
2881 
2882 	vsi = pf->vsi[vf->lan_vsi_idx];
2883 
2884 	/* Lock once, because all function inside for loop accesses VSI's
2885 	 * MAC filter list which needs to be protected using same lock.
2886 	 */
2887 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2888 
2889 	ret = i40e_check_vf_permission(vf, al);
2890 	if (ret) {
2891 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2892 		goto error_param;
2893 	}
2894 
2895 	/* add new addresses to the list */
2896 	for (i = 0; i < al->num_elements; i++) {
2897 		struct i40e_mac_filter *f;
2898 
2899 		f = i40e_find_mac(vsi, al->list[i].addr);
2900 		if (!f) {
2901 			f = i40e_add_mac_filter(vsi, al->list[i].addr);
2902 
2903 			if (!f) {
2904 				dev_err(&pf->pdev->dev,
2905 					"Unable to add MAC filter %pM for VF %d\n",
2906 					al->list[i].addr, vf->vf_id);
2907 				ret = I40E_ERR_PARAM;
2908 				spin_unlock_bh(&vsi->mac_filter_hash_lock);
2909 				goto error_param;
2910 			}
2911 			if (is_valid_ether_addr(al->list[i].addr) &&
2912 			    is_zero_ether_addr(vf->default_lan_addr.addr))
2913 				ether_addr_copy(vf->default_lan_addr.addr,
2914 						al->list[i].addr);
2915 		}
2916 	}
2917 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2918 
2919 	/* program the updated filter list */
2920 	ret = i40e_sync_vsi_filters(vsi);
2921 	if (ret)
2922 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2923 			vf->vf_id, ret);
2924 
2925 error_param:
2926 	/* send the response to the VF */
2927 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
2928 				      ret, NULL, 0);
2929 }
2930 
2931 /**
2932  * i40e_vc_del_mac_addr_msg
2933  * @vf: pointer to the VF info
2934  * @msg: pointer to the msg buffer
2935  *
2936  * remove guest mac address filter
2937  **/
i40e_vc_del_mac_addr_msg(struct i40e_vf * vf,u8 * msg)2938 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2939 {
2940 	struct virtchnl_ether_addr_list *al =
2941 	    (struct virtchnl_ether_addr_list *)msg;
2942 	bool was_unimac_deleted = false;
2943 	struct i40e_pf *pf = vf->pf;
2944 	struct i40e_vsi *vsi = NULL;
2945 	i40e_status ret = 0;
2946 	int i;
2947 
2948 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
2949 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2950 		ret = I40E_ERR_PARAM;
2951 		goto error_param;
2952 	}
2953 
2954 	for (i = 0; i < al->num_elements; i++) {
2955 		if (is_broadcast_ether_addr(al->list[i].addr) ||
2956 		    is_zero_ether_addr(al->list[i].addr)) {
2957 			dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
2958 				al->list[i].addr, vf->vf_id);
2959 			ret = I40E_ERR_INVALID_MAC_ADDR;
2960 			goto error_param;
2961 		}
2962 		if (ether_addr_equal(al->list[i].addr, vf->default_lan_addr.addr))
2963 			was_unimac_deleted = true;
2964 	}
2965 	vsi = pf->vsi[vf->lan_vsi_idx];
2966 
2967 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2968 	/* delete addresses from the list */
2969 	for (i = 0; i < al->num_elements; i++)
2970 		if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2971 			ret = I40E_ERR_INVALID_MAC_ADDR;
2972 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2973 			goto error_param;
2974 		}
2975 
2976 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2977 
2978 	/* program the updated filter list */
2979 	ret = i40e_sync_vsi_filters(vsi);
2980 	if (ret)
2981 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2982 			vf->vf_id, ret);
2983 
2984 	if (vf->trusted && was_unimac_deleted) {
2985 		struct i40e_mac_filter *f;
2986 		struct hlist_node *h;
2987 		u8 *macaddr = NULL;
2988 		int bkt;
2989 
2990 		/* set last unicast mac address as default */
2991 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2992 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2993 			if (is_valid_ether_addr(f->macaddr))
2994 				macaddr = f->macaddr;
2995 		}
2996 		if (macaddr)
2997 			ether_addr_copy(vf->default_lan_addr.addr, macaddr);
2998 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2999 	}
3000 error_param:
3001 	/* send the response to the VF */
3002 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR, ret);
3003 }
3004 
3005 /**
3006  * i40e_vc_add_vlan_msg
3007  * @vf: pointer to the VF info
3008  * @msg: pointer to the msg buffer
3009  *
3010  * program guest vlan id
3011  **/
i40e_vc_add_vlan_msg(struct i40e_vf * vf,u8 * msg)3012 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg)
3013 {
3014 	struct virtchnl_vlan_filter_list *vfl =
3015 	    (struct virtchnl_vlan_filter_list *)msg;
3016 	struct i40e_pf *pf = vf->pf;
3017 	struct i40e_vsi *vsi = NULL;
3018 	i40e_status aq_ret = 0;
3019 	int i;
3020 
3021 	if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
3022 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3023 		dev_err(&pf->pdev->dev,
3024 			"VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
3025 		goto error_param;
3026 	}
3027 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3028 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
3029 		aq_ret = I40E_ERR_PARAM;
3030 		goto error_param;
3031 	}
3032 
3033 	for (i = 0; i < vfl->num_elements; i++) {
3034 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
3035 			aq_ret = I40E_ERR_PARAM;
3036 			dev_err(&pf->pdev->dev,
3037 				"invalid VF VLAN id %d\n", vfl->vlan_id[i]);
3038 			goto error_param;
3039 		}
3040 	}
3041 	vsi = pf->vsi[vf->lan_vsi_idx];
3042 	if (vsi->info.pvid) {
3043 		aq_ret = I40E_ERR_PARAM;
3044 		goto error_param;
3045 	}
3046 
3047 	i40e_vlan_stripping_enable(vsi);
3048 	for (i = 0; i < vfl->num_elements; i++) {
3049 		/* add new VLAN filter */
3050 		int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
3051 		if (!ret)
3052 			vf->num_vlan++;
3053 
3054 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
3055 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
3056 							   true,
3057 							   vfl->vlan_id[i],
3058 							   NULL);
3059 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
3060 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
3061 							   true,
3062 							   vfl->vlan_id[i],
3063 							   NULL);
3064 
3065 		if (ret)
3066 			dev_err(&pf->pdev->dev,
3067 				"Unable to add VLAN filter %d for VF %d, error %d\n",
3068 				vfl->vlan_id[i], vf->vf_id, ret);
3069 	}
3070 
3071 error_param:
3072 	/* send the response to the VF */
3073 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
3074 }
3075 
3076 /**
3077  * i40e_vc_remove_vlan_msg
3078  * @vf: pointer to the VF info
3079  * @msg: pointer to the msg buffer
3080  *
3081  * remove programmed guest vlan id
3082  **/
i40e_vc_remove_vlan_msg(struct i40e_vf * vf,u8 * msg)3083 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg)
3084 {
3085 	struct virtchnl_vlan_filter_list *vfl =
3086 	    (struct virtchnl_vlan_filter_list *)msg;
3087 	struct i40e_pf *pf = vf->pf;
3088 	struct i40e_vsi *vsi = NULL;
3089 	i40e_status aq_ret = 0;
3090 	int i;
3091 
3092 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3093 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
3094 		aq_ret = I40E_ERR_PARAM;
3095 		goto error_param;
3096 	}
3097 
3098 	for (i = 0; i < vfl->num_elements; i++) {
3099 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
3100 			aq_ret = I40E_ERR_PARAM;
3101 			goto error_param;
3102 		}
3103 	}
3104 
3105 	vsi = pf->vsi[vf->lan_vsi_idx];
3106 	if (vsi->info.pvid) {
3107 		if (vfl->num_elements > 1 || vfl->vlan_id[0])
3108 			aq_ret = I40E_ERR_PARAM;
3109 		goto error_param;
3110 	}
3111 
3112 	for (i = 0; i < vfl->num_elements; i++) {
3113 		i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
3114 		vf->num_vlan--;
3115 
3116 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
3117 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
3118 							   false,
3119 							   vfl->vlan_id[i],
3120 							   NULL);
3121 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
3122 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
3123 							   false,
3124 							   vfl->vlan_id[i],
3125 							   NULL);
3126 	}
3127 
3128 error_param:
3129 	/* send the response to the VF */
3130 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
3131 }
3132 
3133 /**
3134  * i40e_vc_iwarp_msg
3135  * @vf: pointer to the VF info
3136  * @msg: pointer to the msg buffer
3137  * @msglen: msg length
3138  *
3139  * called from the VF for the iwarp msgs
3140  **/
i40e_vc_iwarp_msg(struct i40e_vf * vf,u8 * msg,u16 msglen)3141 static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
3142 {
3143 	struct i40e_pf *pf = vf->pf;
3144 	int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
3145 	i40e_status aq_ret = 0;
3146 
3147 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3148 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
3149 		aq_ret = I40E_ERR_PARAM;
3150 		goto error_param;
3151 	}
3152 
3153 	i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
3154 				     msg, msglen);
3155 
3156 error_param:
3157 	/* send the response to the VF */
3158 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
3159 				       aq_ret);
3160 }
3161 
3162 /**
3163  * i40e_vc_iwarp_qvmap_msg
3164  * @vf: pointer to the VF info
3165  * @msg: pointer to the msg buffer
3166  * @config: config qvmap or release it
3167  *
3168  * called from the VF for the iwarp msgs
3169  **/
i40e_vc_iwarp_qvmap_msg(struct i40e_vf * vf,u8 * msg,bool config)3170 static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, bool config)
3171 {
3172 	struct virtchnl_iwarp_qvlist_info *qvlist_info =
3173 				(struct virtchnl_iwarp_qvlist_info *)msg;
3174 	i40e_status aq_ret = 0;
3175 
3176 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3177 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
3178 		aq_ret = I40E_ERR_PARAM;
3179 		goto error_param;
3180 	}
3181 
3182 	if (config) {
3183 		if (i40e_config_iwarp_qvlist(vf, qvlist_info))
3184 			aq_ret = I40E_ERR_PARAM;
3185 	} else {
3186 		i40e_release_iwarp_qvlist(vf);
3187 	}
3188 
3189 error_param:
3190 	/* send the response to the VF */
3191 	return i40e_vc_send_resp_to_vf(vf,
3192 			       config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
3193 			       VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
3194 			       aq_ret);
3195 }
3196 
3197 /**
3198  * i40e_vc_config_rss_key
3199  * @vf: pointer to the VF info
3200  * @msg: pointer to the msg buffer
3201  *
3202  * Configure the VF's RSS key
3203  **/
i40e_vc_config_rss_key(struct i40e_vf * vf,u8 * msg)3204 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg)
3205 {
3206 	struct virtchnl_rss_key *vrk =
3207 		(struct virtchnl_rss_key *)msg;
3208 	struct i40e_pf *pf = vf->pf;
3209 	struct i40e_vsi *vsi = NULL;
3210 	i40e_status aq_ret = 0;
3211 
3212 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3213 	    !i40e_vc_isvalid_vsi_id(vf, vrk->vsi_id) ||
3214 	    vrk->key_len != I40E_HKEY_ARRAY_SIZE) {
3215 		aq_ret = I40E_ERR_PARAM;
3216 		goto err;
3217 	}
3218 
3219 	vsi = pf->vsi[vf->lan_vsi_idx];
3220 	aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
3221 err:
3222 	/* send the response to the VF */
3223 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
3224 				       aq_ret);
3225 }
3226 
3227 /**
3228  * i40e_vc_config_rss_lut
3229  * @vf: pointer to the VF info
3230  * @msg: pointer to the msg buffer
3231  *
3232  * Configure the VF's RSS LUT
3233  **/
i40e_vc_config_rss_lut(struct i40e_vf * vf,u8 * msg)3234 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg)
3235 {
3236 	struct virtchnl_rss_lut *vrl =
3237 		(struct virtchnl_rss_lut *)msg;
3238 	struct i40e_pf *pf = vf->pf;
3239 	struct i40e_vsi *vsi = NULL;
3240 	i40e_status aq_ret = 0;
3241 	u16 i;
3242 
3243 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3244 	    !i40e_vc_isvalid_vsi_id(vf, vrl->vsi_id) ||
3245 	    vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE) {
3246 		aq_ret = I40E_ERR_PARAM;
3247 		goto err;
3248 	}
3249 
3250 	for (i = 0; i < vrl->lut_entries; i++)
3251 		if (vrl->lut[i] >= vf->num_queue_pairs) {
3252 			aq_ret = I40E_ERR_PARAM;
3253 			goto err;
3254 		}
3255 
3256 	vsi = pf->vsi[vf->lan_vsi_idx];
3257 	aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
3258 	/* send the response to the VF */
3259 err:
3260 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
3261 				       aq_ret);
3262 }
3263 
3264 /**
3265  * i40e_vc_get_rss_hena
3266  * @vf: pointer to the VF info
3267  * @msg: pointer to the msg buffer
3268  *
3269  * Return the RSS HENA bits allowed by the hardware
3270  **/
i40e_vc_get_rss_hena(struct i40e_vf * vf,u8 * msg)3271 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg)
3272 {
3273 	struct virtchnl_rss_hena *vrh = NULL;
3274 	struct i40e_pf *pf = vf->pf;
3275 	i40e_status aq_ret = 0;
3276 	int len = 0;
3277 
3278 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3279 		aq_ret = I40E_ERR_PARAM;
3280 		goto err;
3281 	}
3282 	len = sizeof(struct virtchnl_rss_hena);
3283 
3284 	vrh = kzalloc(len, GFP_KERNEL);
3285 	if (!vrh) {
3286 		aq_ret = I40E_ERR_NO_MEMORY;
3287 		len = 0;
3288 		goto err;
3289 	}
3290 	vrh->hena = i40e_pf_get_default_rss_hena(pf);
3291 err:
3292 	/* send the response back to the VF */
3293 	aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
3294 					aq_ret, (u8 *)vrh, len);
3295 	kfree(vrh);
3296 	return aq_ret;
3297 }
3298 
3299 /**
3300  * i40e_vc_set_rss_hena
3301  * @vf: pointer to the VF info
3302  * @msg: pointer to the msg buffer
3303  *
3304  * Set the RSS HENA bits for the VF
3305  **/
i40e_vc_set_rss_hena(struct i40e_vf * vf,u8 * msg)3306 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg)
3307 {
3308 	struct virtchnl_rss_hena *vrh =
3309 		(struct virtchnl_rss_hena *)msg;
3310 	struct i40e_pf *pf = vf->pf;
3311 	struct i40e_hw *hw = &pf->hw;
3312 	i40e_status aq_ret = 0;
3313 
3314 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3315 		aq_ret = I40E_ERR_PARAM;
3316 		goto err;
3317 	}
3318 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
3319 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
3320 			  (u32)(vrh->hena >> 32));
3321 
3322 	/* send the response to the VF */
3323 err:
3324 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
3325 }
3326 
3327 /**
3328  * i40e_vc_enable_vlan_stripping
3329  * @vf: pointer to the VF info
3330  * @msg: pointer to the msg buffer
3331  *
3332  * Enable vlan header stripping for the VF
3333  **/
i40e_vc_enable_vlan_stripping(struct i40e_vf * vf,u8 * msg)3334 static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3335 {
3336 	i40e_status aq_ret = 0;
3337 	struct i40e_vsi *vsi;
3338 
3339 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3340 		aq_ret = I40E_ERR_PARAM;
3341 		goto err;
3342 	}
3343 
3344 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3345 	i40e_vlan_stripping_enable(vsi);
3346 
3347 	/* send the response to the VF */
3348 err:
3349 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
3350 				       aq_ret);
3351 }
3352 
3353 /**
3354  * i40e_vc_disable_vlan_stripping
3355  * @vf: pointer to the VF info
3356  * @msg: pointer to the msg buffer
3357  *
3358  * Disable vlan header stripping for the VF
3359  **/
i40e_vc_disable_vlan_stripping(struct i40e_vf * vf,u8 * msg)3360 static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3361 {
3362 	i40e_status aq_ret = 0;
3363 	struct i40e_vsi *vsi;
3364 
3365 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3366 		aq_ret = I40E_ERR_PARAM;
3367 		goto err;
3368 	}
3369 
3370 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3371 	i40e_vlan_stripping_disable(vsi);
3372 
3373 	/* send the response to the VF */
3374 err:
3375 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
3376 				       aq_ret);
3377 }
3378 
3379 /**
3380  * i40e_validate_cloud_filter
3381  * @vf: pointer to VF structure
3382  * @tc_filter: pointer to filter requested
3383  *
3384  * This function validates cloud filter programmed as TC filter for ADq
3385  **/
i40e_validate_cloud_filter(struct i40e_vf * vf,struct virtchnl_filter * tc_filter)3386 static int i40e_validate_cloud_filter(struct i40e_vf *vf,
3387 				      struct virtchnl_filter *tc_filter)
3388 {
3389 	struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
3390 	struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
3391 	struct i40e_pf *pf = vf->pf;
3392 	struct i40e_vsi *vsi = NULL;
3393 	struct i40e_mac_filter *f;
3394 	struct hlist_node *h;
3395 	bool found = false;
3396 	int bkt;
3397 
3398 	if (tc_filter->action != VIRTCHNL_ACTION_TC_REDIRECT) {
3399 		dev_info(&pf->pdev->dev,
3400 			 "VF %d: ADQ doesn't support this action (%d)\n",
3401 			 vf->vf_id, tc_filter->action);
3402 		goto err;
3403 	}
3404 
3405 	/* action_meta is TC number here to which the filter is applied */
3406 	if (!tc_filter->action_meta ||
3407 	    tc_filter->action_meta > vf->num_tc) {
3408 		dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
3409 			 vf->vf_id, tc_filter->action_meta);
3410 		goto err;
3411 	}
3412 
3413 	/* Check filter if it's programmed for advanced mode or basic mode.
3414 	 * There are two ADq modes (for VF only),
3415 	 * 1. Basic mode: intended to allow as many filter options as possible
3416 	 *		  to be added to a VF in Non-trusted mode. Main goal is
3417 	 *		  to add filters to its own MAC and VLAN id.
3418 	 * 2. Advanced mode: is for allowing filters to be applied other than
3419 	 *		  its own MAC or VLAN. This mode requires the VF to be
3420 	 *		  Trusted.
3421 	 */
3422 	if (mask.dst_mac[0] && !mask.dst_ip[0]) {
3423 		vsi = pf->vsi[vf->lan_vsi_idx];
3424 		f = i40e_find_mac(vsi, data.dst_mac);
3425 
3426 		if (!f) {
3427 			dev_info(&pf->pdev->dev,
3428 				 "Destination MAC %pM doesn't belong to VF %d\n",
3429 				 data.dst_mac, vf->vf_id);
3430 			goto err;
3431 		}
3432 
3433 		if (mask.vlan_id) {
3434 			hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
3435 					   hlist) {
3436 				if (f->vlan == ntohs(data.vlan_id)) {
3437 					found = true;
3438 					break;
3439 				}
3440 			}
3441 			if (!found) {
3442 				dev_info(&pf->pdev->dev,
3443 					 "VF %d doesn't have any VLAN id %u\n",
3444 					 vf->vf_id, ntohs(data.vlan_id));
3445 				goto err;
3446 			}
3447 		}
3448 	} else {
3449 		/* Check if VF is trusted */
3450 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3451 			dev_err(&pf->pdev->dev,
3452 				"VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
3453 				vf->vf_id);
3454 			return I40E_ERR_CONFIG;
3455 		}
3456 	}
3457 
3458 	if (mask.dst_mac[0] & data.dst_mac[0]) {
3459 		if (is_broadcast_ether_addr(data.dst_mac) ||
3460 		    is_zero_ether_addr(data.dst_mac)) {
3461 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
3462 				 vf->vf_id, data.dst_mac);
3463 			goto err;
3464 		}
3465 	}
3466 
3467 	if (mask.src_mac[0] & data.src_mac[0]) {
3468 		if (is_broadcast_ether_addr(data.src_mac) ||
3469 		    is_zero_ether_addr(data.src_mac)) {
3470 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
3471 				 vf->vf_id, data.src_mac);
3472 			goto err;
3473 		}
3474 	}
3475 
3476 	if (mask.dst_port & data.dst_port) {
3477 		if (!data.dst_port) {
3478 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
3479 				 vf->vf_id);
3480 			goto err;
3481 		}
3482 	}
3483 
3484 	if (mask.src_port & data.src_port) {
3485 		if (!data.src_port) {
3486 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
3487 				 vf->vf_id);
3488 			goto err;
3489 		}
3490 	}
3491 
3492 	if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
3493 	    tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
3494 		dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
3495 			 vf->vf_id);
3496 		goto err;
3497 	}
3498 
3499 	if (mask.vlan_id & data.vlan_id) {
3500 		if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
3501 			dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
3502 				 vf->vf_id);
3503 			goto err;
3504 		}
3505 	}
3506 
3507 	return I40E_SUCCESS;
3508 err:
3509 	return I40E_ERR_CONFIG;
3510 }
3511 
3512 /**
3513  * i40e_find_vsi_from_seid - searches for the vsi with the given seid
3514  * @vf: pointer to the VF info
3515  * @seid: seid of the vsi it is searching for
3516  **/
i40e_find_vsi_from_seid(struct i40e_vf * vf,u16 seid)3517 static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
3518 {
3519 	struct i40e_pf *pf = vf->pf;
3520 	struct i40e_vsi *vsi = NULL;
3521 	int i;
3522 
3523 	for (i = 0; i < vf->num_tc ; i++) {
3524 		vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
3525 		if (vsi && vsi->seid == seid)
3526 			return vsi;
3527 	}
3528 	return NULL;
3529 }
3530 
3531 /**
3532  * i40e_del_all_cloud_filters
3533  * @vf: pointer to the VF info
3534  *
3535  * This function deletes all cloud filters
3536  **/
i40e_del_all_cloud_filters(struct i40e_vf * vf)3537 static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
3538 {
3539 	struct i40e_cloud_filter *cfilter = NULL;
3540 	struct i40e_pf *pf = vf->pf;
3541 	struct i40e_vsi *vsi = NULL;
3542 	struct hlist_node *node;
3543 	int ret;
3544 
3545 	hlist_for_each_entry_safe(cfilter, node,
3546 				  &vf->cloud_filter_list, cloud_node) {
3547 		vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);
3548 
3549 		if (!vsi) {
3550 			dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
3551 				vf->vf_id, cfilter->seid);
3552 			continue;
3553 		}
3554 
3555 		if (cfilter->dst_port)
3556 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
3557 								false);
3558 		else
3559 			ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
3560 		if (ret)
3561 			dev_err(&pf->pdev->dev,
3562 				"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3563 				vf->vf_id, i40e_stat_str(&pf->hw, ret),
3564 				i40e_aq_str(&pf->hw,
3565 					    pf->hw.aq.asq_last_status));
3566 
3567 		hlist_del(&cfilter->cloud_node);
3568 		kfree(cfilter);
3569 		vf->num_cloud_filters--;
3570 	}
3571 }
3572 
3573 /**
3574  * i40e_vc_del_cloud_filter
3575  * @vf: pointer to the VF info
3576  * @msg: pointer to the msg buffer
3577  *
3578  * This function deletes a cloud filter programmed as TC filter for ADq
3579  **/
i40e_vc_del_cloud_filter(struct i40e_vf * vf,u8 * msg)3580 static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
3581 {
3582 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3583 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3584 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3585 	struct i40e_cloud_filter cfilter, *cf = NULL;
3586 	struct i40e_pf *pf = vf->pf;
3587 	struct i40e_vsi *vsi = NULL;
3588 	struct hlist_node *node;
3589 	i40e_status aq_ret = 0;
3590 	int i, ret;
3591 
3592 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3593 		aq_ret = I40E_ERR_PARAM;
3594 		goto err;
3595 	}
3596 
3597 	if (!vf->adq_enabled) {
3598 		dev_info(&pf->pdev->dev,
3599 			 "VF %d: ADq not enabled, can't apply cloud filter\n",
3600 			 vf->vf_id);
3601 		aq_ret = I40E_ERR_PARAM;
3602 		goto err;
3603 	}
3604 
3605 	if (i40e_validate_cloud_filter(vf, vcf)) {
3606 		dev_info(&pf->pdev->dev,
3607 			 "VF %d: Invalid input, can't apply cloud filter\n",
3608 			 vf->vf_id);
3609 		aq_ret = I40E_ERR_PARAM;
3610 		goto err;
3611 	}
3612 
3613 	memset(&cfilter, 0, sizeof(cfilter));
3614 	/* parse destination mac address */
3615 	for (i = 0; i < ETH_ALEN; i++)
3616 		cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3617 
3618 	/* parse source mac address */
3619 	for (i = 0; i < ETH_ALEN; i++)
3620 		cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3621 
3622 	cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
3623 	cfilter.dst_port = mask.dst_port & tcf.dst_port;
3624 	cfilter.src_port = mask.src_port & tcf.src_port;
3625 
3626 	switch (vcf->flow_type) {
3627 	case VIRTCHNL_TCP_V4_FLOW:
3628 		cfilter.n_proto = ETH_P_IP;
3629 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3630 			memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
3631 			       ARRAY_SIZE(tcf.dst_ip));
3632 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3633 			memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
3634 			       ARRAY_SIZE(tcf.dst_ip));
3635 		break;
3636 	case VIRTCHNL_TCP_V6_FLOW:
3637 		cfilter.n_proto = ETH_P_IPV6;
3638 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3639 			memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
3640 			       sizeof(cfilter.ip.v6.dst_ip6));
3641 		if (mask.src_ip[3] & tcf.src_ip[3])
3642 			memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
3643 			       sizeof(cfilter.ip.v6.src_ip6));
3644 		break;
3645 	default:
3646 		/* TC filter can be configured based on different combinations
3647 		 * and in this case IP is not a part of filter config
3648 		 */
3649 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3650 			 vf->vf_id);
3651 	}
3652 
3653 	/* get the vsi to which the tc belongs to */
3654 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3655 	cfilter.seid = vsi->seid;
3656 	cfilter.flags = vcf->field_flags;
3657 
3658 	/* Deleting TC filter */
3659 	if (tcf.dst_port)
3660 		ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
3661 	else
3662 		ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
3663 	if (ret) {
3664 		dev_err(&pf->pdev->dev,
3665 			"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3666 			vf->vf_id, i40e_stat_str(&pf->hw, ret),
3667 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3668 		goto err;
3669 	}
3670 
3671 	hlist_for_each_entry_safe(cf, node,
3672 				  &vf->cloud_filter_list, cloud_node) {
3673 		if (cf->seid != cfilter.seid)
3674 			continue;
3675 		if (mask.dst_port)
3676 			if (cfilter.dst_port != cf->dst_port)
3677 				continue;
3678 		if (mask.dst_mac[0])
3679 			if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
3680 				continue;
3681 		/* for ipv4 data to be valid, only first byte of mask is set */
3682 		if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
3683 			if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
3684 				   ARRAY_SIZE(tcf.dst_ip)))
3685 				continue;
3686 		/* for ipv6, mask is set for all sixteen bytes (4 words) */
3687 		if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
3688 			if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
3689 				   sizeof(cfilter.ip.v6.src_ip6)))
3690 				continue;
3691 		if (mask.vlan_id)
3692 			if (cfilter.vlan_id != cf->vlan_id)
3693 				continue;
3694 
3695 		hlist_del(&cf->cloud_node);
3696 		kfree(cf);
3697 		vf->num_cloud_filters--;
3698 	}
3699 
3700 err:
3701 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
3702 				       aq_ret);
3703 }
3704 
3705 /**
3706  * i40e_vc_add_cloud_filter
3707  * @vf: pointer to the VF info
3708  * @msg: pointer to the msg buffer
3709  *
3710  * This function adds a cloud filter programmed as TC filter for ADq
3711  **/
i40e_vc_add_cloud_filter(struct i40e_vf * vf,u8 * msg)3712 static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
3713 {
3714 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3715 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3716 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3717 	struct i40e_cloud_filter *cfilter = NULL;
3718 	struct i40e_pf *pf = vf->pf;
3719 	struct i40e_vsi *vsi = NULL;
3720 	i40e_status aq_ret = 0;
3721 	int i, ret;
3722 
3723 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3724 		aq_ret = I40E_ERR_PARAM;
3725 		goto err_out;
3726 	}
3727 
3728 	if (!vf->adq_enabled) {
3729 		dev_info(&pf->pdev->dev,
3730 			 "VF %d: ADq is not enabled, can't apply cloud filter\n",
3731 			 vf->vf_id);
3732 		aq_ret = I40E_ERR_PARAM;
3733 		goto err_out;
3734 	}
3735 
3736 	if (i40e_validate_cloud_filter(vf, vcf)) {
3737 		dev_info(&pf->pdev->dev,
3738 			 "VF %d: Invalid input/s, can't apply cloud filter\n",
3739 			 vf->vf_id);
3740 		aq_ret = I40E_ERR_PARAM;
3741 		goto err_out;
3742 	}
3743 
3744 	cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
3745 	if (!cfilter)
3746 		return -ENOMEM;
3747 
3748 	/* parse destination mac address */
3749 	for (i = 0; i < ETH_ALEN; i++)
3750 		cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3751 
3752 	/* parse source mac address */
3753 	for (i = 0; i < ETH_ALEN; i++)
3754 		cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3755 
3756 	cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
3757 	cfilter->dst_port = mask.dst_port & tcf.dst_port;
3758 	cfilter->src_port = mask.src_port & tcf.src_port;
3759 
3760 	switch (vcf->flow_type) {
3761 	case VIRTCHNL_TCP_V4_FLOW:
3762 		cfilter->n_proto = ETH_P_IP;
3763 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3764 			memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
3765 			       ARRAY_SIZE(tcf.dst_ip));
3766 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3767 			memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
3768 			       ARRAY_SIZE(tcf.dst_ip));
3769 		break;
3770 	case VIRTCHNL_TCP_V6_FLOW:
3771 		cfilter->n_proto = ETH_P_IPV6;
3772 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3773 			memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
3774 			       sizeof(cfilter->ip.v6.dst_ip6));
3775 		if (mask.src_ip[3] & tcf.src_ip[3])
3776 			memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
3777 			       sizeof(cfilter->ip.v6.src_ip6));
3778 		break;
3779 	default:
3780 		/* TC filter can be configured based on different combinations
3781 		 * and in this case IP is not a part of filter config
3782 		 */
3783 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3784 			 vf->vf_id);
3785 	}
3786 
3787 	/* get the VSI to which the TC belongs to */
3788 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3789 	cfilter->seid = vsi->seid;
3790 	cfilter->flags = vcf->field_flags;
3791 
3792 	/* Adding cloud filter programmed as TC filter */
3793 	if (tcf.dst_port)
3794 		ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
3795 	else
3796 		ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
3797 	if (ret) {
3798 		dev_err(&pf->pdev->dev,
3799 			"VF %d: Failed to add cloud filter, err %s aq_err %s\n",
3800 			vf->vf_id, i40e_stat_str(&pf->hw, ret),
3801 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3802 		goto err_free;
3803 	}
3804 
3805 	INIT_HLIST_NODE(&cfilter->cloud_node);
3806 	hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
3807 	/* release the pointer passing it to the collection */
3808 	cfilter = NULL;
3809 	vf->num_cloud_filters++;
3810 err_free:
3811 	kfree(cfilter);
3812 err_out:
3813 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
3814 				       aq_ret);
3815 }
3816 
3817 /**
3818  * i40e_vc_add_qch_msg: Add queue channel and enable ADq
3819  * @vf: pointer to the VF info
3820  * @msg: pointer to the msg buffer
3821  **/
i40e_vc_add_qch_msg(struct i40e_vf * vf,u8 * msg)3822 static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
3823 {
3824 	struct virtchnl_tc_info *tci =
3825 		(struct virtchnl_tc_info *)msg;
3826 	struct i40e_pf *pf = vf->pf;
3827 	struct i40e_link_status *ls = &pf->hw.phy.link_info;
3828 	int i, adq_request_qps = 0;
3829 	i40e_status aq_ret = 0;
3830 	u64 speed = 0;
3831 
3832 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3833 		aq_ret = I40E_ERR_PARAM;
3834 		goto err;
3835 	}
3836 
3837 	/* ADq cannot be applied if spoof check is ON */
3838 	if (vf->spoofchk) {
3839 		dev_err(&pf->pdev->dev,
3840 			"Spoof check is ON, turn it OFF to enable ADq\n");
3841 		aq_ret = I40E_ERR_PARAM;
3842 		goto err;
3843 	}
3844 
3845 	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3846 		dev_err(&pf->pdev->dev,
3847 			"VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
3848 			vf->vf_id);
3849 		aq_ret = I40E_ERR_PARAM;
3850 		goto err;
3851 	}
3852 
3853 	/* max number of traffic classes for VF currently capped at 4 */
3854 	if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
3855 		dev_err(&pf->pdev->dev,
3856 			"VF %d trying to set %u TCs, valid range 1-%u TCs per VF\n",
3857 			vf->vf_id, tci->num_tc, I40E_MAX_VF_VSI);
3858 		aq_ret = I40E_ERR_PARAM;
3859 		goto err;
3860 	}
3861 
3862 	/* validate queues for each TC */
3863 	for (i = 0; i < tci->num_tc; i++)
3864 		if (!tci->list[i].count ||
3865 		    tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
3866 			dev_err(&pf->pdev->dev,
3867 				"VF %d: TC %d trying to set %u queues, valid range 1-%u queues per TC\n",
3868 				vf->vf_id, i, tci->list[i].count,
3869 				I40E_DEFAULT_QUEUES_PER_VF);
3870 			aq_ret = I40E_ERR_PARAM;
3871 			goto err;
3872 		}
3873 
3874 	/* need Max VF queues but already have default number of queues */
3875 	adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;
3876 
3877 	if (pf->queues_left < adq_request_qps) {
3878 		dev_err(&pf->pdev->dev,
3879 			"No queues left to allocate to VF %d\n",
3880 			vf->vf_id);
3881 		aq_ret = I40E_ERR_PARAM;
3882 		goto err;
3883 	} else {
3884 		/* we need to allocate max VF queues to enable ADq so as to
3885 		 * make sure ADq enabled VF always gets back queues when it
3886 		 * goes through a reset.
3887 		 */
3888 		vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
3889 	}
3890 
3891 	/* get link speed in MB to validate rate limit */
3892 	switch (ls->link_speed) {
3893 	case VIRTCHNL_LINK_SPEED_100MB:
3894 		speed = SPEED_100;
3895 		break;
3896 	case VIRTCHNL_LINK_SPEED_1GB:
3897 		speed = SPEED_1000;
3898 		break;
3899 	case VIRTCHNL_LINK_SPEED_10GB:
3900 		speed = SPEED_10000;
3901 		break;
3902 	case VIRTCHNL_LINK_SPEED_20GB:
3903 		speed = SPEED_20000;
3904 		break;
3905 	case VIRTCHNL_LINK_SPEED_25GB:
3906 		speed = SPEED_25000;
3907 		break;
3908 	case VIRTCHNL_LINK_SPEED_40GB:
3909 		speed = SPEED_40000;
3910 		break;
3911 	default:
3912 		dev_err(&pf->pdev->dev,
3913 			"Cannot detect link speed\n");
3914 		aq_ret = I40E_ERR_PARAM;
3915 		goto err;
3916 	}
3917 
3918 	/* parse data from the queue channel info */
3919 	vf->num_tc = tci->num_tc;
3920 	for (i = 0; i < vf->num_tc; i++) {
3921 		if (tci->list[i].max_tx_rate) {
3922 			if (tci->list[i].max_tx_rate > speed) {
3923 				dev_err(&pf->pdev->dev,
3924 					"Invalid max tx rate %llu specified for VF %d.",
3925 					tci->list[i].max_tx_rate,
3926 					vf->vf_id);
3927 				aq_ret = I40E_ERR_PARAM;
3928 				goto err;
3929 			} else {
3930 				vf->ch[i].max_tx_rate =
3931 					tci->list[i].max_tx_rate;
3932 			}
3933 		}
3934 		vf->ch[i].num_qps = tci->list[i].count;
3935 	}
3936 
3937 	/* set this flag only after making sure all inputs are sane */
3938 	vf->adq_enabled = true;
3939 
3940 	/* reset the VF in order to allocate resources */
3941 	i40e_vc_reset_vf(vf, true);
3942 
3943 	return I40E_SUCCESS;
3944 
3945 	/* send the response to the VF */
3946 err:
3947 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
3948 				       aq_ret);
3949 }
3950 
3951 /**
3952  * i40e_vc_del_qch_msg
3953  * @vf: pointer to the VF info
3954  * @msg: pointer to the msg buffer
3955  **/
i40e_vc_del_qch_msg(struct i40e_vf * vf,u8 * msg)3956 static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
3957 {
3958 	struct i40e_pf *pf = vf->pf;
3959 	i40e_status aq_ret = 0;
3960 
3961 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3962 		aq_ret = I40E_ERR_PARAM;
3963 		goto err;
3964 	}
3965 
3966 	if (vf->adq_enabled) {
3967 		i40e_del_all_cloud_filters(vf);
3968 		i40e_del_qch(vf);
3969 		vf->adq_enabled = false;
3970 		vf->num_tc = 0;
3971 		dev_info(&pf->pdev->dev,
3972 			 "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
3973 			 vf->vf_id);
3974 	} else {
3975 		dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
3976 			 vf->vf_id);
3977 		aq_ret = I40E_ERR_PARAM;
3978 	}
3979 
3980 	/* reset the VF in order to allocate resources */
3981 	i40e_vc_reset_vf(vf, true);
3982 
3983 	return I40E_SUCCESS;
3984 
3985 err:
3986 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
3987 				       aq_ret);
3988 }
3989 
3990 /**
3991  * i40e_vc_process_vf_msg
3992  * @pf: pointer to the PF structure
3993  * @vf_id: source VF id
3994  * @v_opcode: operation code
3995  * @v_retval: unused return value code
3996  * @msg: pointer to the msg buffer
3997  * @msglen: msg length
3998  *
3999  * called from the common aeq/arq handler to
4000  * process request from VF
4001  **/
i40e_vc_process_vf_msg(struct i40e_pf * pf,s16 vf_id,u32 v_opcode,u32 __always_unused v_retval,u8 * msg,u16 msglen)4002 int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
4003 			   u32 __always_unused v_retval, u8 *msg, u16 msglen)
4004 {
4005 	struct i40e_hw *hw = &pf->hw;
4006 	int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
4007 	struct i40e_vf *vf;
4008 	int ret;
4009 
4010 	pf->vf_aq_requests++;
4011 	if (local_vf_id < 0 || local_vf_id >= pf->num_alloc_vfs)
4012 		return -EINVAL;
4013 	vf = &(pf->vf[local_vf_id]);
4014 
4015 	/* Check if VF is disabled. */
4016 	if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
4017 		return I40E_ERR_PARAM;
4018 
4019 	/* perform basic checks on the msg */
4020 	ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4021 
4022 	if (ret) {
4023 		i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
4024 		dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
4025 			local_vf_id, v_opcode, msglen);
4026 		switch (ret) {
4027 		case VIRTCHNL_STATUS_ERR_PARAM:
4028 			return -EPERM;
4029 		default:
4030 			return -EINVAL;
4031 		}
4032 	}
4033 
4034 	switch (v_opcode) {
4035 	case VIRTCHNL_OP_VERSION:
4036 		ret = i40e_vc_get_version_msg(vf, msg);
4037 		break;
4038 	case VIRTCHNL_OP_GET_VF_RESOURCES:
4039 		ret = i40e_vc_get_vf_resources_msg(vf, msg);
4040 		i40e_vc_notify_vf_link_state(vf);
4041 		break;
4042 	case VIRTCHNL_OP_RESET_VF:
4043 		i40e_vc_reset_vf(vf, false);
4044 		ret = 0;
4045 		break;
4046 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4047 		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg);
4048 		break;
4049 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4050 		ret = i40e_vc_config_queues_msg(vf, msg);
4051 		break;
4052 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4053 		ret = i40e_vc_config_irq_map_msg(vf, msg);
4054 		break;
4055 	case VIRTCHNL_OP_ENABLE_QUEUES:
4056 		ret = i40e_vc_enable_queues_msg(vf, msg);
4057 		i40e_vc_notify_vf_link_state(vf);
4058 		break;
4059 	case VIRTCHNL_OP_DISABLE_QUEUES:
4060 		ret = i40e_vc_disable_queues_msg(vf, msg);
4061 		break;
4062 	case VIRTCHNL_OP_ADD_ETH_ADDR:
4063 		ret = i40e_vc_add_mac_addr_msg(vf, msg);
4064 		break;
4065 	case VIRTCHNL_OP_DEL_ETH_ADDR:
4066 		ret = i40e_vc_del_mac_addr_msg(vf, msg);
4067 		break;
4068 	case VIRTCHNL_OP_ADD_VLAN:
4069 		ret = i40e_vc_add_vlan_msg(vf, msg);
4070 		break;
4071 	case VIRTCHNL_OP_DEL_VLAN:
4072 		ret = i40e_vc_remove_vlan_msg(vf, msg);
4073 		break;
4074 	case VIRTCHNL_OP_GET_STATS:
4075 		ret = i40e_vc_get_stats_msg(vf, msg);
4076 		break;
4077 	case VIRTCHNL_OP_IWARP:
4078 		ret = i40e_vc_iwarp_msg(vf, msg, msglen);
4079 		break;
4080 	case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
4081 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, true);
4082 		break;
4083 	case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
4084 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, false);
4085 		break;
4086 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
4087 		ret = i40e_vc_config_rss_key(vf, msg);
4088 		break;
4089 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
4090 		ret = i40e_vc_config_rss_lut(vf, msg);
4091 		break;
4092 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
4093 		ret = i40e_vc_get_rss_hena(vf, msg);
4094 		break;
4095 	case VIRTCHNL_OP_SET_RSS_HENA:
4096 		ret = i40e_vc_set_rss_hena(vf, msg);
4097 		break;
4098 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4099 		ret = i40e_vc_enable_vlan_stripping(vf, msg);
4100 		break;
4101 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4102 		ret = i40e_vc_disable_vlan_stripping(vf, msg);
4103 		break;
4104 	case VIRTCHNL_OP_REQUEST_QUEUES:
4105 		ret = i40e_vc_request_queues_msg(vf, msg);
4106 		break;
4107 	case VIRTCHNL_OP_ENABLE_CHANNELS:
4108 		ret = i40e_vc_add_qch_msg(vf, msg);
4109 		break;
4110 	case VIRTCHNL_OP_DISABLE_CHANNELS:
4111 		ret = i40e_vc_del_qch_msg(vf, msg);
4112 		break;
4113 	case VIRTCHNL_OP_ADD_CLOUD_FILTER:
4114 		ret = i40e_vc_add_cloud_filter(vf, msg);
4115 		break;
4116 	case VIRTCHNL_OP_DEL_CLOUD_FILTER:
4117 		ret = i40e_vc_del_cloud_filter(vf, msg);
4118 		break;
4119 	case VIRTCHNL_OP_UNKNOWN:
4120 	default:
4121 		dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
4122 			v_opcode, local_vf_id);
4123 		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
4124 					      I40E_ERR_NOT_IMPLEMENTED);
4125 		break;
4126 	}
4127 
4128 	return ret;
4129 }
4130 
4131 /**
4132  * i40e_vc_process_vflr_event
4133  * @pf: pointer to the PF structure
4134  *
4135  * called from the vlfr irq handler to
4136  * free up VF resources and state variables
4137  **/
i40e_vc_process_vflr_event(struct i40e_pf * pf)4138 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
4139 {
4140 	struct i40e_hw *hw = &pf->hw;
4141 	u32 reg, reg_idx, bit_idx;
4142 	struct i40e_vf *vf;
4143 	int vf_id;
4144 
4145 	if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
4146 		return 0;
4147 
4148 	/* Re-enable the VFLR interrupt cause here, before looking for which
4149 	 * VF got reset. Otherwise, if another VF gets a reset while the
4150 	 * first one is being processed, that interrupt will be lost, and
4151 	 * that VF will be stuck in reset forever.
4152 	 */
4153 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4154 	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
4155 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4156 	i40e_flush(hw);
4157 
4158 	clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4159 	for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
4160 		reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
4161 		bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
4162 		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
4163 		vf = &pf->vf[vf_id];
4164 		reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
4165 		if (reg & BIT(bit_idx))
4166 			/* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
4167 			i40e_reset_vf(vf, true);
4168 	}
4169 
4170 	return 0;
4171 }
4172 
4173 /**
4174  * i40e_validate_vf
4175  * @pf: the physical function
4176  * @vf_id: VF identifier
4177  *
4178  * Check that the VF is enabled and the VSI exists.
4179  *
4180  * Returns 0 on success, negative on failure
4181  **/
i40e_validate_vf(struct i40e_pf * pf,int vf_id)4182 static int i40e_validate_vf(struct i40e_pf *pf, int vf_id)
4183 {
4184 	struct i40e_vsi *vsi;
4185 	struct i40e_vf *vf;
4186 	int ret = 0;
4187 
4188 	if (vf_id >= pf->num_alloc_vfs) {
4189 		dev_err(&pf->pdev->dev,
4190 			"Invalid VF Identifier %d\n", vf_id);
4191 		ret = -EINVAL;
4192 		goto err_out;
4193 	}
4194 	vf = &pf->vf[vf_id];
4195 	vsi = i40e_find_vsi_from_id(pf, vf->lan_vsi_id);
4196 	if (!vsi)
4197 		ret = -EINVAL;
4198 err_out:
4199 	return ret;
4200 }
4201 
4202 /**
4203  * i40e_ndo_set_vf_mac
4204  * @netdev: network interface device structure
4205  * @vf_id: VF identifier
4206  * @mac: mac address
4207  *
4208  * program VF mac address
4209  **/
i40e_ndo_set_vf_mac(struct net_device * netdev,int vf_id,u8 * mac)4210 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
4211 {
4212 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4213 	struct i40e_vsi *vsi = np->vsi;
4214 	struct i40e_pf *pf = vsi->back;
4215 	struct i40e_mac_filter *f;
4216 	struct i40e_vf *vf;
4217 	int ret = 0;
4218 	struct hlist_node *h;
4219 	int bkt;
4220 	u8 i;
4221 
4222 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4223 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4224 		return -EAGAIN;
4225 	}
4226 
4227 	/* validate the request */
4228 	ret = i40e_validate_vf(pf, vf_id);
4229 	if (ret)
4230 		goto error_param;
4231 
4232 	vf = &pf->vf[vf_id];
4233 
4234 	/* When the VF is resetting wait until it is done.
4235 	 * It can take up to 200 milliseconds,
4236 	 * but wait for up to 300 milliseconds to be safe.
4237 	 * Acquire the VSI pointer only after the VF has been
4238 	 * properly initialized.
4239 	 */
4240 	for (i = 0; i < 15; i++) {
4241 		if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states))
4242 			break;
4243 		msleep(20);
4244 	}
4245 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4246 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4247 			vf_id);
4248 		ret = -EAGAIN;
4249 		goto error_param;
4250 	}
4251 	vsi = pf->vsi[vf->lan_vsi_idx];
4252 
4253 	if (is_multicast_ether_addr(mac)) {
4254 		dev_err(&pf->pdev->dev,
4255 			"Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
4256 		ret = -EINVAL;
4257 		goto error_param;
4258 	}
4259 
4260 	/* Lock once because below invoked function add/del_filter requires
4261 	 * mac_filter_hash_lock to be held
4262 	 */
4263 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4264 
4265 	/* delete the temporary mac address */
4266 	if (!is_zero_ether_addr(vf->default_lan_addr.addr))
4267 		i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
4268 
4269 	/* Delete all the filters for this VSI - we're going to kill it
4270 	 * anyway.
4271 	 */
4272 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
4273 		__i40e_del_filter(vsi, f);
4274 
4275 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4276 
4277 	/* program mac filter */
4278 	if (i40e_sync_vsi_filters(vsi)) {
4279 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
4280 		ret = -EIO;
4281 		goto error_param;
4282 	}
4283 	ether_addr_copy(vf->default_lan_addr.addr, mac);
4284 
4285 	if (is_zero_ether_addr(mac)) {
4286 		vf->pf_set_mac = false;
4287 		dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
4288 	} else {
4289 		vf->pf_set_mac = true;
4290 		dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
4291 			 mac, vf_id);
4292 	}
4293 
4294 	/* Force the VF interface down so it has to bring up with new MAC
4295 	 * address
4296 	 */
4297 	i40e_vc_reset_vf(vf, true);
4298 	dev_info(&pf->pdev->dev, "Bring down and up the VF interface to make this change effective.\n");
4299 
4300 error_param:
4301 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4302 	return ret;
4303 }
4304 
4305 /**
4306  * i40e_ndo_set_vf_port_vlan
4307  * @netdev: network interface device structure
4308  * @vf_id: VF identifier
4309  * @vlan_id: mac address
4310  * @qos: priority setting
4311  * @vlan_proto: vlan protocol
4312  *
4313  * program VF vlan id and/or qos
4314  **/
i40e_ndo_set_vf_port_vlan(struct net_device * netdev,int vf_id,u16 vlan_id,u8 qos,__be16 vlan_proto)4315 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
4316 			      u16 vlan_id, u8 qos, __be16 vlan_proto)
4317 {
4318 	u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
4319 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4320 	bool allmulti = false, alluni = false;
4321 	struct i40e_pf *pf = np->vsi->back;
4322 	struct i40e_vsi *vsi;
4323 	struct i40e_vf *vf;
4324 	int ret = 0;
4325 
4326 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4327 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4328 		return -EAGAIN;
4329 	}
4330 
4331 	/* validate the request */
4332 	ret = i40e_validate_vf(pf, vf_id);
4333 	if (ret)
4334 		goto error_pvid;
4335 
4336 	if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
4337 		dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
4338 		ret = -EINVAL;
4339 		goto error_pvid;
4340 	}
4341 
4342 	if (vlan_proto != htons(ETH_P_8021Q)) {
4343 		dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
4344 		ret = -EPROTONOSUPPORT;
4345 		goto error_pvid;
4346 	}
4347 
4348 	vf = &pf->vf[vf_id];
4349 	vsi = pf->vsi[vf->lan_vsi_idx];
4350 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4351 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4352 			vf_id);
4353 		ret = -EAGAIN;
4354 		goto error_pvid;
4355 	}
4356 
4357 	if (le16_to_cpu(vsi->info.pvid) == vlanprio)
4358 		/* duplicate request, so just return success */
4359 		goto error_pvid;
4360 
4361 	/* Locked once because multiple functions below iterate list */
4362 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4363 
4364 	/* Check for condition where there was already a port VLAN ID
4365 	 * filter set and now it is being deleted by setting it to zero.
4366 	 * Additionally check for the condition where there was a port
4367 	 * VLAN but now there is a new and different port VLAN being set.
4368 	 * Before deleting all the old VLAN filters we must add new ones
4369 	 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
4370 	 * MAC addresses deleted.
4371 	 */
4372 	if ((!(vlan_id || qos) ||
4373 	    vlanprio != le16_to_cpu(vsi->info.pvid)) &&
4374 	    vsi->info.pvid) {
4375 		ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
4376 		if (ret) {
4377 			dev_info(&vsi->back->pdev->dev,
4378 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4379 				 vsi->back->hw.aq.asq_last_status);
4380 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4381 			goto error_pvid;
4382 		}
4383 	}
4384 
4385 	if (vsi->info.pvid) {
4386 		/* remove all filters on the old VLAN */
4387 		i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
4388 					   VLAN_VID_MASK));
4389 	}
4390 
4391 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4392 
4393 	/* disable promisc modes in case they were enabled */
4394 	ret = i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id,
4395 					      allmulti, alluni);
4396 	if (ret) {
4397 		dev_err(&pf->pdev->dev, "Unable to config VF promiscuous mode\n");
4398 		goto error_pvid;
4399 	}
4400 
4401 	if (vlan_id || qos)
4402 		ret = i40e_vsi_add_pvid(vsi, vlanprio);
4403 	else
4404 		i40e_vsi_remove_pvid(vsi);
4405 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4406 
4407 	if (vlan_id) {
4408 		dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
4409 			 vlan_id, qos, vf_id);
4410 
4411 		/* add new VLAN filter for each MAC */
4412 		ret = i40e_add_vlan_all_mac(vsi, vlan_id);
4413 		if (ret) {
4414 			dev_info(&vsi->back->pdev->dev,
4415 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4416 				 vsi->back->hw.aq.asq_last_status);
4417 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4418 			goto error_pvid;
4419 		}
4420 
4421 		/* remove the previously added non-VLAN MAC filters */
4422 		i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
4423 	}
4424 
4425 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4426 
4427 	if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
4428 		alluni = true;
4429 
4430 	if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
4431 		allmulti = true;
4432 
4433 	/* Schedule the worker thread to take care of applying changes */
4434 	i40e_service_event_schedule(vsi->back);
4435 
4436 	if (ret) {
4437 		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
4438 		goto error_pvid;
4439 	}
4440 
4441 	/* The Port VLAN needs to be saved across resets the same as the
4442 	 * default LAN MAC address.
4443 	 */
4444 	vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
4445 
4446 	i40e_vc_reset_vf(vf, true);
4447 	/* During reset the VF got a new VSI, so refresh a pointer. */
4448 	vsi = pf->vsi[vf->lan_vsi_idx];
4449 
4450 	ret = i40e_config_vf_promiscuous_mode(vf, vsi->id, allmulti, alluni);
4451 	if (ret) {
4452 		dev_err(&pf->pdev->dev, "Unable to config vf promiscuous mode\n");
4453 		goto error_pvid;
4454 	}
4455 
4456 	ret = 0;
4457 
4458 error_pvid:
4459 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4460 	return ret;
4461 }
4462 
4463 /**
4464  * i40e_ndo_set_vf_bw
4465  * @netdev: network interface device structure
4466  * @vf_id: VF identifier
4467  * @min_tx_rate: Minimum Tx rate
4468  * @max_tx_rate: Maximum Tx rate
4469  *
4470  * configure VF Tx rate
4471  **/
i40e_ndo_set_vf_bw(struct net_device * netdev,int vf_id,int min_tx_rate,int max_tx_rate)4472 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
4473 		       int max_tx_rate)
4474 {
4475 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4476 	struct i40e_pf *pf = np->vsi->back;
4477 	struct i40e_vsi *vsi;
4478 	struct i40e_vf *vf;
4479 	int ret = 0;
4480 
4481 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4482 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4483 		return -EAGAIN;
4484 	}
4485 
4486 	/* validate the request */
4487 	ret = i40e_validate_vf(pf, vf_id);
4488 	if (ret)
4489 		goto error;
4490 
4491 	if (min_tx_rate) {
4492 		dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
4493 			min_tx_rate, vf_id);
4494 		ret = -EINVAL;
4495 		goto error;
4496 	}
4497 
4498 	vf = &pf->vf[vf_id];
4499 	vsi = pf->vsi[vf->lan_vsi_idx];
4500 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4501 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4502 			vf_id);
4503 		ret = -EAGAIN;
4504 		goto error;
4505 	}
4506 
4507 	ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
4508 	if (ret)
4509 		goto error;
4510 
4511 	vf->tx_rate = max_tx_rate;
4512 error:
4513 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4514 	return ret;
4515 }
4516 
4517 /**
4518  * i40e_ndo_get_vf_config
4519  * @netdev: network interface device structure
4520  * @vf_id: VF identifier
4521  * @ivi: VF configuration structure
4522  *
4523  * return VF configuration
4524  **/
i40e_ndo_get_vf_config(struct net_device * netdev,int vf_id,struct ifla_vf_info * ivi)4525 int i40e_ndo_get_vf_config(struct net_device *netdev,
4526 			   int vf_id, struct ifla_vf_info *ivi)
4527 {
4528 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4529 	struct i40e_vsi *vsi = np->vsi;
4530 	struct i40e_pf *pf = vsi->back;
4531 	struct i40e_vf *vf;
4532 	int ret = 0;
4533 
4534 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4535 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4536 		return -EAGAIN;
4537 	}
4538 
4539 	/* validate the request */
4540 	ret = i40e_validate_vf(pf, vf_id);
4541 	if (ret)
4542 		goto error_param;
4543 
4544 	vf = &pf->vf[vf_id];
4545 	/* first vsi is always the LAN vsi */
4546 	vsi = pf->vsi[vf->lan_vsi_idx];
4547 	if (!vsi) {
4548 		ret = -ENOENT;
4549 		goto error_param;
4550 	}
4551 
4552 	ivi->vf = vf_id;
4553 
4554 	ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
4555 
4556 	ivi->max_tx_rate = vf->tx_rate;
4557 	ivi->min_tx_rate = 0;
4558 	ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
4559 	ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
4560 		   I40E_VLAN_PRIORITY_SHIFT;
4561 	if (vf->link_forced == false)
4562 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4563 	else if (vf->link_up == true)
4564 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4565 	else
4566 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4567 	ivi->spoofchk = vf->spoofchk;
4568 	ivi->trusted = vf->trusted;
4569 	ret = 0;
4570 
4571 error_param:
4572 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4573 	return ret;
4574 }
4575 
4576 /**
4577  * i40e_ndo_set_vf_link_state
4578  * @netdev: network interface device structure
4579  * @vf_id: VF identifier
4580  * @link: required link state
4581  *
4582  * Set the link state of a specified VF, regardless of physical link state
4583  **/
i40e_ndo_set_vf_link_state(struct net_device * netdev,int vf_id,int link)4584 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
4585 {
4586 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4587 	struct i40e_pf *pf = np->vsi->back;
4588 	struct virtchnl_pf_event pfe;
4589 	struct i40e_hw *hw = &pf->hw;
4590 	struct i40e_vf *vf;
4591 	int abs_vf_id;
4592 	int ret = 0;
4593 
4594 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4595 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4596 		return -EAGAIN;
4597 	}
4598 
4599 	/* validate the request */
4600 	if (vf_id >= pf->num_alloc_vfs) {
4601 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4602 		ret = -EINVAL;
4603 		goto error_out;
4604 	}
4605 
4606 	vf = &pf->vf[vf_id];
4607 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
4608 
4609 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
4610 	pfe.severity = PF_EVENT_SEVERITY_INFO;
4611 
4612 	switch (link) {
4613 	case IFLA_VF_LINK_STATE_AUTO:
4614 		vf->link_forced = false;
4615 		pfe.event_data.link_event.link_status =
4616 			pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
4617 		pfe.event_data.link_event.link_speed =
4618 			(enum virtchnl_link_speed)
4619 			pf->hw.phy.link_info.link_speed;
4620 		break;
4621 	case IFLA_VF_LINK_STATE_ENABLE:
4622 		vf->link_forced = true;
4623 		vf->link_up = true;
4624 		pfe.event_data.link_event.link_status = true;
4625 		pfe.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
4626 		break;
4627 	case IFLA_VF_LINK_STATE_DISABLE:
4628 		vf->link_forced = true;
4629 		vf->link_up = false;
4630 		pfe.event_data.link_event.link_status = false;
4631 		pfe.event_data.link_event.link_speed = 0;
4632 		break;
4633 	default:
4634 		ret = -EINVAL;
4635 		goto error_out;
4636 	}
4637 	/* Notify the VF of its new link state */
4638 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
4639 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
4640 
4641 error_out:
4642 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4643 	return ret;
4644 }
4645 
4646 /**
4647  * i40e_ndo_set_vf_spoofchk
4648  * @netdev: network interface device structure
4649  * @vf_id: VF identifier
4650  * @enable: flag to enable or disable feature
4651  *
4652  * Enable or disable VF spoof checking
4653  **/
i40e_ndo_set_vf_spoofchk(struct net_device * netdev,int vf_id,bool enable)4654 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
4655 {
4656 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4657 	struct i40e_vsi *vsi = np->vsi;
4658 	struct i40e_pf *pf = vsi->back;
4659 	struct i40e_vsi_context ctxt;
4660 	struct i40e_hw *hw = &pf->hw;
4661 	struct i40e_vf *vf;
4662 	int ret = 0;
4663 
4664 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4665 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4666 		return -EAGAIN;
4667 	}
4668 
4669 	/* validate the request */
4670 	if (vf_id >= pf->num_alloc_vfs) {
4671 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4672 		ret = -EINVAL;
4673 		goto out;
4674 	}
4675 
4676 	vf = &(pf->vf[vf_id]);
4677 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4678 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4679 			vf_id);
4680 		ret = -EAGAIN;
4681 		goto out;
4682 	}
4683 
4684 	if (enable == vf->spoofchk)
4685 		goto out;
4686 
4687 	vf->spoofchk = enable;
4688 	memset(&ctxt, 0, sizeof(ctxt));
4689 	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
4690 	ctxt.pf_num = pf->hw.pf_id;
4691 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
4692 	if (enable)
4693 		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
4694 					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
4695 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
4696 	if (ret) {
4697 		dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
4698 			ret);
4699 		ret = -EIO;
4700 	}
4701 out:
4702 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4703 	return ret;
4704 }
4705 
4706 /**
4707  * i40e_ndo_set_vf_trust
4708  * @netdev: network interface device structure of the pf
4709  * @vf_id: VF identifier
4710  * @setting: trust setting
4711  *
4712  * Enable or disable VF trust setting
4713  **/
i40e_ndo_set_vf_trust(struct net_device * netdev,int vf_id,bool setting)4714 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
4715 {
4716 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4717 	struct i40e_pf *pf = np->vsi->back;
4718 	struct i40e_vf *vf;
4719 	int ret = 0;
4720 
4721 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4722 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4723 		return -EAGAIN;
4724 	}
4725 
4726 	/* validate the request */
4727 	if (vf_id >= pf->num_alloc_vfs) {
4728 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4729 		ret = -EINVAL;
4730 		goto out;
4731 	}
4732 
4733 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4734 		dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
4735 		ret = -EINVAL;
4736 		goto out;
4737 	}
4738 
4739 	vf = &pf->vf[vf_id];
4740 
4741 	if (setting == vf->trusted)
4742 		goto out;
4743 
4744 	vf->trusted = setting;
4745 	i40e_vc_reset_vf(vf, true);
4746 	dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
4747 		 vf_id, setting ? "" : "un");
4748 
4749 	if (vf->adq_enabled) {
4750 		if (!vf->trusted) {
4751 			dev_info(&pf->pdev->dev,
4752 				 "VF %u no longer Trusted, deleting all cloud filters\n",
4753 				 vf_id);
4754 			i40e_del_all_cloud_filters(vf);
4755 		}
4756 	}
4757 
4758 out:
4759 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4760 	return ret;
4761 }
4762 
4763 /**
4764  * i40e_get_vf_stats - populate some stats for the VF
4765  * @netdev: the netdev of the PF
4766  * @vf_id: the host OS identifier (0-127)
4767  * @vf_stats: pointer to the OS memory to be initialized
4768  */
i40e_get_vf_stats(struct net_device * netdev,int vf_id,struct ifla_vf_stats * vf_stats)4769 int i40e_get_vf_stats(struct net_device *netdev, int vf_id,
4770 		      struct ifla_vf_stats *vf_stats)
4771 {
4772 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4773 	struct i40e_pf *pf = np->vsi->back;
4774 	struct i40e_eth_stats *stats;
4775 	struct i40e_vsi *vsi;
4776 	struct i40e_vf *vf;
4777 
4778 	/* validate the request */
4779 	if (i40e_validate_vf(pf, vf_id))
4780 		return -EINVAL;
4781 
4782 	vf = &pf->vf[vf_id];
4783 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4784 		dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
4785 		return -EBUSY;
4786 	}
4787 
4788 	vsi = pf->vsi[vf->lan_vsi_idx];
4789 	if (!vsi)
4790 		return -EINVAL;
4791 
4792 	i40e_update_eth_stats(vsi);
4793 	stats = &vsi->eth_stats;
4794 
4795 	memset(vf_stats, 0, sizeof(*vf_stats));
4796 
4797 	vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
4798 		stats->rx_multicast;
4799 	vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
4800 		stats->tx_multicast;
4801 	vf_stats->rx_bytes   = stats->rx_bytes;
4802 	vf_stats->tx_bytes   = stats->tx_bytes;
4803 	vf_stats->broadcast  = stats->rx_broadcast;
4804 	vf_stats->multicast  = stats->rx_multicast;
4805 	vf_stats->rx_dropped = stats->rx_discards;
4806 	vf_stats->tx_dropped = stats->tx_discards;
4807 
4808 	return 0;
4809 }
4810