• 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 (vf->is_disabled_from_host) {
2549 		aq_ret = -EPERM;
2550 		dev_info(&pf->pdev->dev,
2551 			 "Admin has disabled VF %d, will not enable queues\n",
2552 			 vf->vf_id);
2553 		goto error_param;
2554 	}
2555 
2556 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2557 		aq_ret = I40E_ERR_PARAM;
2558 		goto error_param;
2559 	}
2560 
2561 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2562 		aq_ret = I40E_ERR_PARAM;
2563 		goto error_param;
2564 	}
2565 
2566 	if (!i40e_vc_validate_vqs_bitmaps(vqs)) {
2567 		aq_ret = I40E_ERR_PARAM;
2568 		goto error_param;
2569 	}
2570 
2571 	/* Use the queue bit map sent by the VF */
2572 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2573 				  true)) {
2574 		aq_ret = I40E_ERR_TIMEOUT;
2575 		goto error_param;
2576 	}
2577 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2578 				  true)) {
2579 		aq_ret = I40E_ERR_TIMEOUT;
2580 		goto error_param;
2581 	}
2582 
2583 	/* need to start the rings for additional ADq VSI's as well */
2584 	if (vf->adq_enabled) {
2585 		/* zero belongs to LAN VSI */
2586 		for (i = 1; i < vf->num_tc; i++) {
2587 			if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
2588 				aq_ret = I40E_ERR_TIMEOUT;
2589 		}
2590 	}
2591 
2592 error_param:
2593 	/* send the response to the VF */
2594 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
2595 				       aq_ret);
2596 }
2597 
2598 /**
2599  * i40e_vc_disable_queues_msg
2600  * @vf: pointer to the VF info
2601  * @msg: pointer to the msg buffer
2602  *
2603  * called from the VF to disable all or specific
2604  * queue(s)
2605  **/
i40e_vc_disable_queues_msg(struct i40e_vf * vf,u8 * msg)2606 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg)
2607 {
2608 	struct virtchnl_queue_select *vqs =
2609 	    (struct virtchnl_queue_select *)msg;
2610 	struct i40e_pf *pf = vf->pf;
2611 	i40e_status aq_ret = 0;
2612 
2613 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2614 		aq_ret = I40E_ERR_PARAM;
2615 		goto error_param;
2616 	}
2617 
2618 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2619 		aq_ret = I40E_ERR_PARAM;
2620 		goto error_param;
2621 	}
2622 
2623 	if (!i40e_vc_validate_vqs_bitmaps(vqs)) {
2624 		aq_ret = I40E_ERR_PARAM;
2625 		goto error_param;
2626 	}
2627 
2628 	/* Use the queue bit map sent by the VF */
2629 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2630 				  false)) {
2631 		aq_ret = I40E_ERR_TIMEOUT;
2632 		goto error_param;
2633 	}
2634 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2635 				  false)) {
2636 		aq_ret = I40E_ERR_TIMEOUT;
2637 		goto error_param;
2638 	}
2639 error_param:
2640 	/* send the response to the VF */
2641 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
2642 				       aq_ret);
2643 }
2644 
2645 /**
2646  * i40e_check_enough_queue - find big enough queue number
2647  * @vf: pointer to the VF info
2648  * @needed: the number of items needed
2649  *
2650  * Returns the base item index of the queue, or negative for error
2651  **/
i40e_check_enough_queue(struct i40e_vf * vf,u16 needed)2652 static int i40e_check_enough_queue(struct i40e_vf *vf, u16 needed)
2653 {
2654 	unsigned int  i, cur_queues, more, pool_size;
2655 	struct i40e_lump_tracking *pile;
2656 	struct i40e_pf *pf = vf->pf;
2657 	struct i40e_vsi *vsi;
2658 
2659 	vsi = pf->vsi[vf->lan_vsi_idx];
2660 	cur_queues = vsi->alloc_queue_pairs;
2661 
2662 	/* if current allocated queues are enough for need */
2663 	if (cur_queues >= needed)
2664 		return vsi->base_queue;
2665 
2666 	pile = pf->qp_pile;
2667 	if (cur_queues > 0) {
2668 		/* if the allocated queues are not zero
2669 		 * just check if there are enough queues for more
2670 		 * behind the allocated queues.
2671 		 */
2672 		more = needed - cur_queues;
2673 		for (i = vsi->base_queue + cur_queues;
2674 			i < pile->num_entries; i++) {
2675 			if (pile->list[i] & I40E_PILE_VALID_BIT)
2676 				break;
2677 
2678 			if (more-- == 1)
2679 				/* there is enough */
2680 				return vsi->base_queue;
2681 		}
2682 	}
2683 
2684 	pool_size = 0;
2685 	for (i = 0; i < pile->num_entries; i++) {
2686 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
2687 			pool_size = 0;
2688 			continue;
2689 		}
2690 		if (needed <= ++pool_size)
2691 			/* there is enough */
2692 			return i;
2693 	}
2694 
2695 	return -ENOMEM;
2696 }
2697 
2698 /**
2699  * i40e_vc_request_queues_msg
2700  * @vf: pointer to the VF info
2701  * @msg: pointer to the msg buffer
2702  *
2703  * VFs get a default number of queues but can use this message to request a
2704  * different number.  If the request is successful, PF will reset the VF and
2705  * return 0.  If unsuccessful, PF will send message informing VF of number of
2706  * available queues and return result of sending VF a message.
2707  **/
i40e_vc_request_queues_msg(struct i40e_vf * vf,u8 * msg)2708 static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg)
2709 {
2710 	struct virtchnl_vf_res_request *vfres =
2711 		(struct virtchnl_vf_res_request *)msg;
2712 	u16 req_pairs = vfres->num_queue_pairs;
2713 	u8 cur_pairs = vf->num_queue_pairs;
2714 	struct i40e_pf *pf = vf->pf;
2715 
2716 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE))
2717 		return -EINVAL;
2718 
2719 	if (req_pairs > I40E_MAX_VF_QUEUES) {
2720 		dev_err(&pf->pdev->dev,
2721 			"VF %d tried to request more than %d queues.\n",
2722 			vf->vf_id,
2723 			I40E_MAX_VF_QUEUES);
2724 		vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
2725 	} else if (req_pairs - cur_pairs > pf->queues_left) {
2726 		dev_warn(&pf->pdev->dev,
2727 			 "VF %d requested %d more queues, but only %d left.\n",
2728 			 vf->vf_id,
2729 			 req_pairs - cur_pairs,
2730 			 pf->queues_left);
2731 		vfres->num_queue_pairs = pf->queues_left + cur_pairs;
2732 	} else if (i40e_check_enough_queue(vf, req_pairs) < 0) {
2733 		dev_warn(&pf->pdev->dev,
2734 			 "VF %d requested %d more queues, but there is not enough for it.\n",
2735 			 vf->vf_id,
2736 			 req_pairs - cur_pairs);
2737 		vfres->num_queue_pairs = cur_pairs;
2738 	} else {
2739 		/* successful request */
2740 		vf->num_req_queues = req_pairs;
2741 		i40e_vc_reset_vf(vf, true);
2742 		return 0;
2743 	}
2744 
2745 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
2746 				      (u8 *)vfres, sizeof(*vfres));
2747 }
2748 
2749 /**
2750  * i40e_vc_get_stats_msg
2751  * @vf: pointer to the VF info
2752  * @msg: pointer to the msg buffer
2753  *
2754  * called from the VF to get vsi stats
2755  **/
i40e_vc_get_stats_msg(struct i40e_vf * vf,u8 * msg)2756 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg)
2757 {
2758 	struct virtchnl_queue_select *vqs =
2759 	    (struct virtchnl_queue_select *)msg;
2760 	struct i40e_pf *pf = vf->pf;
2761 	struct i40e_eth_stats stats;
2762 	i40e_status aq_ret = 0;
2763 	struct i40e_vsi *vsi;
2764 
2765 	memset(&stats, 0, sizeof(struct i40e_eth_stats));
2766 
2767 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2768 		aq_ret = I40E_ERR_PARAM;
2769 		goto error_param;
2770 	}
2771 
2772 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2773 		aq_ret = I40E_ERR_PARAM;
2774 		goto error_param;
2775 	}
2776 
2777 	vsi = pf->vsi[vf->lan_vsi_idx];
2778 	if (!vsi) {
2779 		aq_ret = I40E_ERR_PARAM;
2780 		goto error_param;
2781 	}
2782 	i40e_update_eth_stats(vsi);
2783 	stats = vsi->eth_stats;
2784 
2785 error_param:
2786 	/* send the response back to the VF */
2787 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
2788 				      (u8 *)&stats, sizeof(stats));
2789 }
2790 
2791 /* If the VF is not trusted restrict the number of MAC/VLAN it can program
2792  * MAC filters: 16 for multicast, 1 for MAC, 1 for broadcast
2793  */
2794 #define I40E_VC_MAX_MAC_ADDR_PER_VF (16 + 1 + 1)
2795 #define I40E_VC_MAX_VLAN_PER_VF 16
2796 
2797 /**
2798  * i40e_check_vf_permission
2799  * @vf: pointer to the VF info
2800  * @al: MAC address list from virtchnl
2801  *
2802  * Check that the given list of MAC addresses is allowed. Will return -EPERM
2803  * if any address in the list is not valid. Checks the following conditions:
2804  *
2805  * 1) broadcast and zero addresses are never valid
2806  * 2) unicast addresses are not allowed if the VMM has administratively set
2807  *    the VF MAC address, unless the VF is marked as privileged.
2808  * 3) There is enough space to add all the addresses.
2809  *
2810  * Note that to guarantee consistency, it is expected this function be called
2811  * while holding the mac_filter_hash_lock, as otherwise the current number of
2812  * addresses might not be accurate.
2813  **/
i40e_check_vf_permission(struct i40e_vf * vf,struct virtchnl_ether_addr_list * al)2814 static inline int i40e_check_vf_permission(struct i40e_vf *vf,
2815 					   struct virtchnl_ether_addr_list *al)
2816 {
2817 	struct i40e_pf *pf = vf->pf;
2818 	struct i40e_vsi *vsi = pf->vsi[vf->lan_vsi_idx];
2819 	int mac2add_cnt = 0;
2820 	int i;
2821 
2822 	for (i = 0; i < al->num_elements; i++) {
2823 		struct i40e_mac_filter *f;
2824 		u8 *addr = al->list[i].addr;
2825 
2826 		if (is_broadcast_ether_addr(addr) ||
2827 		    is_zero_ether_addr(addr)) {
2828 			dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
2829 				addr);
2830 			return I40E_ERR_INVALID_MAC_ADDR;
2831 		}
2832 
2833 		/* If the host VMM administrator has set the VF MAC address
2834 		 * administratively via the ndo_set_vf_mac command then deny
2835 		 * permission to the VF to add or delete unicast MAC addresses.
2836 		 * Unless the VF is privileged and then it can do whatever.
2837 		 * The VF may request to set the MAC address filter already
2838 		 * assigned to it so do not return an error in that case.
2839 		 */
2840 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2841 		    !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
2842 		    !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
2843 			dev_err(&pf->pdev->dev,
2844 				"VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
2845 			return -EPERM;
2846 		}
2847 
2848 		/*count filters that really will be added*/
2849 		f = i40e_find_mac(vsi, addr);
2850 		if (!f)
2851 			++mac2add_cnt;
2852 	}
2853 
2854 	/* If this VF is not privileged, then we can't add more than a limited
2855 	 * number of addresses. Check to make sure that the additions do not
2856 	 * push us over the limit.
2857 	 */
2858 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2859 	    (i40e_count_filters(vsi) + mac2add_cnt) >
2860 		    I40E_VC_MAX_MAC_ADDR_PER_VF) {
2861 		dev_err(&pf->pdev->dev,
2862 			"Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
2863 		return -EPERM;
2864 	}
2865 	return 0;
2866 }
2867 
2868 /**
2869  * i40e_vc_add_mac_addr_msg
2870  * @vf: pointer to the VF info
2871  * @msg: pointer to the msg buffer
2872  *
2873  * add guest mac address filter
2874  **/
i40e_vc_add_mac_addr_msg(struct i40e_vf * vf,u8 * msg)2875 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2876 {
2877 	struct virtchnl_ether_addr_list *al =
2878 	    (struct virtchnl_ether_addr_list *)msg;
2879 	struct i40e_pf *pf = vf->pf;
2880 	struct i40e_vsi *vsi = NULL;
2881 	i40e_status ret = 0;
2882 	int i;
2883 
2884 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
2885 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2886 		ret = I40E_ERR_PARAM;
2887 		goto error_param;
2888 	}
2889 
2890 	vsi = pf->vsi[vf->lan_vsi_idx];
2891 
2892 	/* Lock once, because all function inside for loop accesses VSI's
2893 	 * MAC filter list which needs to be protected using same lock.
2894 	 */
2895 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2896 
2897 	ret = i40e_check_vf_permission(vf, al);
2898 	if (ret) {
2899 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2900 		goto error_param;
2901 	}
2902 
2903 	/* add new addresses to the list */
2904 	for (i = 0; i < al->num_elements; i++) {
2905 		struct i40e_mac_filter *f;
2906 
2907 		f = i40e_find_mac(vsi, al->list[i].addr);
2908 		if (!f) {
2909 			f = i40e_add_mac_filter(vsi, al->list[i].addr);
2910 
2911 			if (!f) {
2912 				dev_err(&pf->pdev->dev,
2913 					"Unable to add MAC filter %pM for VF %d\n",
2914 					al->list[i].addr, vf->vf_id);
2915 				ret = I40E_ERR_PARAM;
2916 				spin_unlock_bh(&vsi->mac_filter_hash_lock);
2917 				goto error_param;
2918 			}
2919 			if (is_valid_ether_addr(al->list[i].addr) &&
2920 			    is_zero_ether_addr(vf->default_lan_addr.addr))
2921 				ether_addr_copy(vf->default_lan_addr.addr,
2922 						al->list[i].addr);
2923 		}
2924 	}
2925 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2926 
2927 	/* program the updated filter list */
2928 	ret = i40e_sync_vsi_filters(vsi);
2929 	if (ret)
2930 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2931 			vf->vf_id, ret);
2932 
2933 error_param:
2934 	/* send the response to the VF */
2935 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
2936 				      ret, NULL, 0);
2937 }
2938 
2939 /**
2940  * i40e_vc_del_mac_addr_msg
2941  * @vf: pointer to the VF info
2942  * @msg: pointer to the msg buffer
2943  *
2944  * remove guest mac address filter
2945  **/
i40e_vc_del_mac_addr_msg(struct i40e_vf * vf,u8 * msg)2946 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2947 {
2948 	struct virtchnl_ether_addr_list *al =
2949 	    (struct virtchnl_ether_addr_list *)msg;
2950 	bool was_unimac_deleted = false;
2951 	struct i40e_pf *pf = vf->pf;
2952 	struct i40e_vsi *vsi = NULL;
2953 	i40e_status ret = 0;
2954 	int i;
2955 
2956 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
2957 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2958 		ret = I40E_ERR_PARAM;
2959 		goto error_param;
2960 	}
2961 
2962 	for (i = 0; i < al->num_elements; i++) {
2963 		if (is_broadcast_ether_addr(al->list[i].addr) ||
2964 		    is_zero_ether_addr(al->list[i].addr)) {
2965 			dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
2966 				al->list[i].addr, vf->vf_id);
2967 			ret = I40E_ERR_INVALID_MAC_ADDR;
2968 			goto error_param;
2969 		}
2970 		if (ether_addr_equal(al->list[i].addr, vf->default_lan_addr.addr))
2971 			was_unimac_deleted = true;
2972 	}
2973 	vsi = pf->vsi[vf->lan_vsi_idx];
2974 
2975 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2976 	/* delete addresses from the list */
2977 	for (i = 0; i < al->num_elements; i++)
2978 		if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2979 			ret = I40E_ERR_INVALID_MAC_ADDR;
2980 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2981 			goto error_param;
2982 		}
2983 
2984 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2985 
2986 	/* program the updated filter list */
2987 	ret = i40e_sync_vsi_filters(vsi);
2988 	if (ret)
2989 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2990 			vf->vf_id, ret);
2991 
2992 	if (vf->trusted && was_unimac_deleted) {
2993 		struct i40e_mac_filter *f;
2994 		struct hlist_node *h;
2995 		u8 *macaddr = NULL;
2996 		int bkt;
2997 
2998 		/* set last unicast mac address as default */
2999 		spin_lock_bh(&vsi->mac_filter_hash_lock);
3000 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3001 			if (is_valid_ether_addr(f->macaddr))
3002 				macaddr = f->macaddr;
3003 		}
3004 		if (macaddr)
3005 			ether_addr_copy(vf->default_lan_addr.addr, macaddr);
3006 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
3007 	}
3008 error_param:
3009 	/* send the response to the VF */
3010 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR, ret);
3011 }
3012 
3013 /**
3014  * i40e_vc_add_vlan_msg
3015  * @vf: pointer to the VF info
3016  * @msg: pointer to the msg buffer
3017  *
3018  * program guest vlan id
3019  **/
i40e_vc_add_vlan_msg(struct i40e_vf * vf,u8 * msg)3020 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg)
3021 {
3022 	struct virtchnl_vlan_filter_list *vfl =
3023 	    (struct virtchnl_vlan_filter_list *)msg;
3024 	struct i40e_pf *pf = vf->pf;
3025 	struct i40e_vsi *vsi = NULL;
3026 	i40e_status aq_ret = 0;
3027 	int i;
3028 
3029 	if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
3030 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3031 		dev_err(&pf->pdev->dev,
3032 			"VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
3033 		goto error_param;
3034 	}
3035 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3036 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
3037 		aq_ret = I40E_ERR_PARAM;
3038 		goto error_param;
3039 	}
3040 
3041 	for (i = 0; i < vfl->num_elements; i++) {
3042 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
3043 			aq_ret = I40E_ERR_PARAM;
3044 			dev_err(&pf->pdev->dev,
3045 				"invalid VF VLAN id %d\n", vfl->vlan_id[i]);
3046 			goto error_param;
3047 		}
3048 	}
3049 	vsi = pf->vsi[vf->lan_vsi_idx];
3050 	if (vsi->info.pvid) {
3051 		aq_ret = I40E_ERR_PARAM;
3052 		goto error_param;
3053 	}
3054 
3055 	i40e_vlan_stripping_enable(vsi);
3056 	for (i = 0; i < vfl->num_elements; i++) {
3057 		/* add new VLAN filter */
3058 		int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
3059 		if (!ret)
3060 			vf->num_vlan++;
3061 
3062 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
3063 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
3064 							   true,
3065 							   vfl->vlan_id[i],
3066 							   NULL);
3067 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
3068 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
3069 							   true,
3070 							   vfl->vlan_id[i],
3071 							   NULL);
3072 
3073 		if (ret)
3074 			dev_err(&pf->pdev->dev,
3075 				"Unable to add VLAN filter %d for VF %d, error %d\n",
3076 				vfl->vlan_id[i], vf->vf_id, ret);
3077 	}
3078 
3079 error_param:
3080 	/* send the response to the VF */
3081 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
3082 }
3083 
3084 /**
3085  * i40e_vc_remove_vlan_msg
3086  * @vf: pointer to the VF info
3087  * @msg: pointer to the msg buffer
3088  *
3089  * remove programmed guest vlan id
3090  **/
i40e_vc_remove_vlan_msg(struct i40e_vf * vf,u8 * msg)3091 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg)
3092 {
3093 	struct virtchnl_vlan_filter_list *vfl =
3094 	    (struct virtchnl_vlan_filter_list *)msg;
3095 	struct i40e_pf *pf = vf->pf;
3096 	struct i40e_vsi *vsi = NULL;
3097 	i40e_status aq_ret = 0;
3098 	int i;
3099 
3100 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3101 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
3102 		aq_ret = I40E_ERR_PARAM;
3103 		goto error_param;
3104 	}
3105 
3106 	for (i = 0; i < vfl->num_elements; i++) {
3107 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
3108 			aq_ret = I40E_ERR_PARAM;
3109 			goto error_param;
3110 		}
3111 	}
3112 
3113 	vsi = pf->vsi[vf->lan_vsi_idx];
3114 	if (vsi->info.pvid) {
3115 		if (vfl->num_elements > 1 || vfl->vlan_id[0])
3116 			aq_ret = I40E_ERR_PARAM;
3117 		goto error_param;
3118 	}
3119 
3120 	for (i = 0; i < vfl->num_elements; i++) {
3121 		i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
3122 		vf->num_vlan--;
3123 
3124 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
3125 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
3126 							   false,
3127 							   vfl->vlan_id[i],
3128 							   NULL);
3129 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
3130 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
3131 							   false,
3132 							   vfl->vlan_id[i],
3133 							   NULL);
3134 	}
3135 
3136 error_param:
3137 	/* send the response to the VF */
3138 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
3139 }
3140 
3141 /**
3142  * i40e_vc_iwarp_msg
3143  * @vf: pointer to the VF info
3144  * @msg: pointer to the msg buffer
3145  * @msglen: msg length
3146  *
3147  * called from the VF for the iwarp msgs
3148  **/
i40e_vc_iwarp_msg(struct i40e_vf * vf,u8 * msg,u16 msglen)3149 static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
3150 {
3151 	struct i40e_pf *pf = vf->pf;
3152 	int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
3153 	i40e_status aq_ret = 0;
3154 
3155 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3156 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
3157 		aq_ret = I40E_ERR_PARAM;
3158 		goto error_param;
3159 	}
3160 
3161 	i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
3162 				     msg, msglen);
3163 
3164 error_param:
3165 	/* send the response to the VF */
3166 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
3167 				       aq_ret);
3168 }
3169 
3170 /**
3171  * i40e_vc_iwarp_qvmap_msg
3172  * @vf: pointer to the VF info
3173  * @msg: pointer to the msg buffer
3174  * @config: config qvmap or release it
3175  *
3176  * called from the VF for the iwarp msgs
3177  **/
i40e_vc_iwarp_qvmap_msg(struct i40e_vf * vf,u8 * msg,bool config)3178 static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, bool config)
3179 {
3180 	struct virtchnl_iwarp_qvlist_info *qvlist_info =
3181 				(struct virtchnl_iwarp_qvlist_info *)msg;
3182 	i40e_status aq_ret = 0;
3183 
3184 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3185 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
3186 		aq_ret = I40E_ERR_PARAM;
3187 		goto error_param;
3188 	}
3189 
3190 	if (config) {
3191 		if (i40e_config_iwarp_qvlist(vf, qvlist_info))
3192 			aq_ret = I40E_ERR_PARAM;
3193 	} else {
3194 		i40e_release_iwarp_qvlist(vf);
3195 	}
3196 
3197 error_param:
3198 	/* send the response to the VF */
3199 	return i40e_vc_send_resp_to_vf(vf,
3200 			       config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
3201 			       VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
3202 			       aq_ret);
3203 }
3204 
3205 /**
3206  * i40e_vc_config_rss_key
3207  * @vf: pointer to the VF info
3208  * @msg: pointer to the msg buffer
3209  *
3210  * Configure the VF's RSS key
3211  **/
i40e_vc_config_rss_key(struct i40e_vf * vf,u8 * msg)3212 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg)
3213 {
3214 	struct virtchnl_rss_key *vrk =
3215 		(struct virtchnl_rss_key *)msg;
3216 	struct i40e_pf *pf = vf->pf;
3217 	struct i40e_vsi *vsi = NULL;
3218 	i40e_status aq_ret = 0;
3219 
3220 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3221 	    !i40e_vc_isvalid_vsi_id(vf, vrk->vsi_id) ||
3222 	    vrk->key_len != I40E_HKEY_ARRAY_SIZE) {
3223 		aq_ret = I40E_ERR_PARAM;
3224 		goto err;
3225 	}
3226 
3227 	vsi = pf->vsi[vf->lan_vsi_idx];
3228 	aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
3229 err:
3230 	/* send the response to the VF */
3231 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
3232 				       aq_ret);
3233 }
3234 
3235 /**
3236  * i40e_vc_config_rss_lut
3237  * @vf: pointer to the VF info
3238  * @msg: pointer to the msg buffer
3239  *
3240  * Configure the VF's RSS LUT
3241  **/
i40e_vc_config_rss_lut(struct i40e_vf * vf,u8 * msg)3242 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg)
3243 {
3244 	struct virtchnl_rss_lut *vrl =
3245 		(struct virtchnl_rss_lut *)msg;
3246 	struct i40e_pf *pf = vf->pf;
3247 	struct i40e_vsi *vsi = NULL;
3248 	i40e_status aq_ret = 0;
3249 	u16 i;
3250 
3251 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3252 	    !i40e_vc_isvalid_vsi_id(vf, vrl->vsi_id) ||
3253 	    vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE) {
3254 		aq_ret = I40E_ERR_PARAM;
3255 		goto err;
3256 	}
3257 
3258 	for (i = 0; i < vrl->lut_entries; i++)
3259 		if (vrl->lut[i] >= vf->num_queue_pairs) {
3260 			aq_ret = I40E_ERR_PARAM;
3261 			goto err;
3262 		}
3263 
3264 	vsi = pf->vsi[vf->lan_vsi_idx];
3265 	aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
3266 	/* send the response to the VF */
3267 err:
3268 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
3269 				       aq_ret);
3270 }
3271 
3272 /**
3273  * i40e_vc_get_rss_hena
3274  * @vf: pointer to the VF info
3275  * @msg: pointer to the msg buffer
3276  *
3277  * Return the RSS HENA bits allowed by the hardware
3278  **/
i40e_vc_get_rss_hena(struct i40e_vf * vf,u8 * msg)3279 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg)
3280 {
3281 	struct virtchnl_rss_hena *vrh = NULL;
3282 	struct i40e_pf *pf = vf->pf;
3283 	i40e_status aq_ret = 0;
3284 	int len = 0;
3285 
3286 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3287 		aq_ret = I40E_ERR_PARAM;
3288 		goto err;
3289 	}
3290 	len = sizeof(struct virtchnl_rss_hena);
3291 
3292 	vrh = kzalloc(len, GFP_KERNEL);
3293 	if (!vrh) {
3294 		aq_ret = I40E_ERR_NO_MEMORY;
3295 		len = 0;
3296 		goto err;
3297 	}
3298 	vrh->hena = i40e_pf_get_default_rss_hena(pf);
3299 err:
3300 	/* send the response back to the VF */
3301 	aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
3302 					aq_ret, (u8 *)vrh, len);
3303 	kfree(vrh);
3304 	return aq_ret;
3305 }
3306 
3307 /**
3308  * i40e_vc_set_rss_hena
3309  * @vf: pointer to the VF info
3310  * @msg: pointer to the msg buffer
3311  *
3312  * Set the RSS HENA bits for the VF
3313  **/
i40e_vc_set_rss_hena(struct i40e_vf * vf,u8 * msg)3314 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg)
3315 {
3316 	struct virtchnl_rss_hena *vrh =
3317 		(struct virtchnl_rss_hena *)msg;
3318 	struct i40e_pf *pf = vf->pf;
3319 	struct i40e_hw *hw = &pf->hw;
3320 	i40e_status aq_ret = 0;
3321 
3322 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3323 		aq_ret = I40E_ERR_PARAM;
3324 		goto err;
3325 	}
3326 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
3327 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
3328 			  (u32)(vrh->hena >> 32));
3329 
3330 	/* send the response to the VF */
3331 err:
3332 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
3333 }
3334 
3335 /**
3336  * i40e_vc_enable_vlan_stripping
3337  * @vf: pointer to the VF info
3338  * @msg: pointer to the msg buffer
3339  *
3340  * Enable vlan header stripping for the VF
3341  **/
i40e_vc_enable_vlan_stripping(struct i40e_vf * vf,u8 * msg)3342 static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3343 {
3344 	i40e_status aq_ret = 0;
3345 	struct i40e_vsi *vsi;
3346 
3347 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3348 		aq_ret = I40E_ERR_PARAM;
3349 		goto err;
3350 	}
3351 
3352 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3353 	i40e_vlan_stripping_enable(vsi);
3354 
3355 	/* send the response to the VF */
3356 err:
3357 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
3358 				       aq_ret);
3359 }
3360 
3361 /**
3362  * i40e_vc_disable_vlan_stripping
3363  * @vf: pointer to the VF info
3364  * @msg: pointer to the msg buffer
3365  *
3366  * Disable vlan header stripping for the VF
3367  **/
i40e_vc_disable_vlan_stripping(struct i40e_vf * vf,u8 * msg)3368 static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3369 {
3370 	i40e_status aq_ret = 0;
3371 	struct i40e_vsi *vsi;
3372 
3373 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3374 		aq_ret = I40E_ERR_PARAM;
3375 		goto err;
3376 	}
3377 
3378 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3379 	i40e_vlan_stripping_disable(vsi);
3380 
3381 	/* send the response to the VF */
3382 err:
3383 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
3384 				       aq_ret);
3385 }
3386 
3387 /**
3388  * i40e_validate_cloud_filter
3389  * @vf: pointer to VF structure
3390  * @tc_filter: pointer to filter requested
3391  *
3392  * This function validates cloud filter programmed as TC filter for ADq
3393  **/
i40e_validate_cloud_filter(struct i40e_vf * vf,struct virtchnl_filter * tc_filter)3394 static int i40e_validate_cloud_filter(struct i40e_vf *vf,
3395 				      struct virtchnl_filter *tc_filter)
3396 {
3397 	struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
3398 	struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
3399 	struct i40e_pf *pf = vf->pf;
3400 	struct i40e_vsi *vsi = NULL;
3401 	struct i40e_mac_filter *f;
3402 	struct hlist_node *h;
3403 	bool found = false;
3404 	int bkt;
3405 
3406 	if (tc_filter->action != VIRTCHNL_ACTION_TC_REDIRECT) {
3407 		dev_info(&pf->pdev->dev,
3408 			 "VF %d: ADQ doesn't support this action (%d)\n",
3409 			 vf->vf_id, tc_filter->action);
3410 		goto err;
3411 	}
3412 
3413 	/* action_meta is TC number here to which the filter is applied */
3414 	if (!tc_filter->action_meta ||
3415 	    tc_filter->action_meta > vf->num_tc) {
3416 		dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
3417 			 vf->vf_id, tc_filter->action_meta);
3418 		goto err;
3419 	}
3420 
3421 	/* Check filter if it's programmed for advanced mode or basic mode.
3422 	 * There are two ADq modes (for VF only),
3423 	 * 1. Basic mode: intended to allow as many filter options as possible
3424 	 *		  to be added to a VF in Non-trusted mode. Main goal is
3425 	 *		  to add filters to its own MAC and VLAN id.
3426 	 * 2. Advanced mode: is for allowing filters to be applied other than
3427 	 *		  its own MAC or VLAN. This mode requires the VF to be
3428 	 *		  Trusted.
3429 	 */
3430 	if (mask.dst_mac[0] && !mask.dst_ip[0]) {
3431 		vsi = pf->vsi[vf->lan_vsi_idx];
3432 		f = i40e_find_mac(vsi, data.dst_mac);
3433 
3434 		if (!f) {
3435 			dev_info(&pf->pdev->dev,
3436 				 "Destination MAC %pM doesn't belong to VF %d\n",
3437 				 data.dst_mac, vf->vf_id);
3438 			goto err;
3439 		}
3440 
3441 		if (mask.vlan_id) {
3442 			hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
3443 					   hlist) {
3444 				if (f->vlan == ntohs(data.vlan_id)) {
3445 					found = true;
3446 					break;
3447 				}
3448 			}
3449 			if (!found) {
3450 				dev_info(&pf->pdev->dev,
3451 					 "VF %d doesn't have any VLAN id %u\n",
3452 					 vf->vf_id, ntohs(data.vlan_id));
3453 				goto err;
3454 			}
3455 		}
3456 	} else {
3457 		/* Check if VF is trusted */
3458 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3459 			dev_err(&pf->pdev->dev,
3460 				"VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
3461 				vf->vf_id);
3462 			return I40E_ERR_CONFIG;
3463 		}
3464 	}
3465 
3466 	if (mask.dst_mac[0] & data.dst_mac[0]) {
3467 		if (is_broadcast_ether_addr(data.dst_mac) ||
3468 		    is_zero_ether_addr(data.dst_mac)) {
3469 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
3470 				 vf->vf_id, data.dst_mac);
3471 			goto err;
3472 		}
3473 	}
3474 
3475 	if (mask.src_mac[0] & data.src_mac[0]) {
3476 		if (is_broadcast_ether_addr(data.src_mac) ||
3477 		    is_zero_ether_addr(data.src_mac)) {
3478 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
3479 				 vf->vf_id, data.src_mac);
3480 			goto err;
3481 		}
3482 	}
3483 
3484 	if (mask.dst_port & data.dst_port) {
3485 		if (!data.dst_port) {
3486 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
3487 				 vf->vf_id);
3488 			goto err;
3489 		}
3490 	}
3491 
3492 	if (mask.src_port & data.src_port) {
3493 		if (!data.src_port) {
3494 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
3495 				 vf->vf_id);
3496 			goto err;
3497 		}
3498 	}
3499 
3500 	if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
3501 	    tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
3502 		dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
3503 			 vf->vf_id);
3504 		goto err;
3505 	}
3506 
3507 	if (mask.vlan_id & data.vlan_id) {
3508 		if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
3509 			dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
3510 				 vf->vf_id);
3511 			goto err;
3512 		}
3513 	}
3514 
3515 	return I40E_SUCCESS;
3516 err:
3517 	return I40E_ERR_CONFIG;
3518 }
3519 
3520 /**
3521  * i40e_find_vsi_from_seid - searches for the vsi with the given seid
3522  * @vf: pointer to the VF info
3523  * @seid: seid of the vsi it is searching for
3524  **/
i40e_find_vsi_from_seid(struct i40e_vf * vf,u16 seid)3525 static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
3526 {
3527 	struct i40e_pf *pf = vf->pf;
3528 	struct i40e_vsi *vsi = NULL;
3529 	int i;
3530 
3531 	for (i = 0; i < vf->num_tc ; i++) {
3532 		vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
3533 		if (vsi && vsi->seid == seid)
3534 			return vsi;
3535 	}
3536 	return NULL;
3537 }
3538 
3539 /**
3540  * i40e_del_all_cloud_filters
3541  * @vf: pointer to the VF info
3542  *
3543  * This function deletes all cloud filters
3544  **/
i40e_del_all_cloud_filters(struct i40e_vf * vf)3545 static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
3546 {
3547 	struct i40e_cloud_filter *cfilter = NULL;
3548 	struct i40e_pf *pf = vf->pf;
3549 	struct i40e_vsi *vsi = NULL;
3550 	struct hlist_node *node;
3551 	int ret;
3552 
3553 	hlist_for_each_entry_safe(cfilter, node,
3554 				  &vf->cloud_filter_list, cloud_node) {
3555 		vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);
3556 
3557 		if (!vsi) {
3558 			dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
3559 				vf->vf_id, cfilter->seid);
3560 			continue;
3561 		}
3562 
3563 		if (cfilter->dst_port)
3564 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
3565 								false);
3566 		else
3567 			ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
3568 		if (ret)
3569 			dev_err(&pf->pdev->dev,
3570 				"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3571 				vf->vf_id, i40e_stat_str(&pf->hw, ret),
3572 				i40e_aq_str(&pf->hw,
3573 					    pf->hw.aq.asq_last_status));
3574 
3575 		hlist_del(&cfilter->cloud_node);
3576 		kfree(cfilter);
3577 		vf->num_cloud_filters--;
3578 	}
3579 }
3580 
3581 /**
3582  * i40e_vc_del_cloud_filter
3583  * @vf: pointer to the VF info
3584  * @msg: pointer to the msg buffer
3585  *
3586  * This function deletes a cloud filter programmed as TC filter for ADq
3587  **/
i40e_vc_del_cloud_filter(struct i40e_vf * vf,u8 * msg)3588 static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
3589 {
3590 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3591 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3592 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3593 	struct i40e_cloud_filter cfilter, *cf = NULL;
3594 	struct i40e_pf *pf = vf->pf;
3595 	struct i40e_vsi *vsi = NULL;
3596 	struct hlist_node *node;
3597 	i40e_status aq_ret = 0;
3598 	int i, ret;
3599 
3600 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3601 		aq_ret = I40E_ERR_PARAM;
3602 		goto err;
3603 	}
3604 
3605 	if (!vf->adq_enabled) {
3606 		dev_info(&pf->pdev->dev,
3607 			 "VF %d: ADq not enabled, can't apply cloud filter\n",
3608 			 vf->vf_id);
3609 		aq_ret = I40E_ERR_PARAM;
3610 		goto err;
3611 	}
3612 
3613 	if (i40e_validate_cloud_filter(vf, vcf)) {
3614 		dev_info(&pf->pdev->dev,
3615 			 "VF %d: Invalid input, can't apply cloud filter\n",
3616 			 vf->vf_id);
3617 		aq_ret = I40E_ERR_PARAM;
3618 		goto err;
3619 	}
3620 
3621 	memset(&cfilter, 0, sizeof(cfilter));
3622 	/* parse destination mac address */
3623 	for (i = 0; i < ETH_ALEN; i++)
3624 		cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3625 
3626 	/* parse source mac address */
3627 	for (i = 0; i < ETH_ALEN; i++)
3628 		cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3629 
3630 	cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
3631 	cfilter.dst_port = mask.dst_port & tcf.dst_port;
3632 	cfilter.src_port = mask.src_port & tcf.src_port;
3633 
3634 	switch (vcf->flow_type) {
3635 	case VIRTCHNL_TCP_V4_FLOW:
3636 		cfilter.n_proto = ETH_P_IP;
3637 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3638 			memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
3639 			       ARRAY_SIZE(tcf.dst_ip));
3640 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3641 			memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
3642 			       ARRAY_SIZE(tcf.dst_ip));
3643 		break;
3644 	case VIRTCHNL_TCP_V6_FLOW:
3645 		cfilter.n_proto = ETH_P_IPV6;
3646 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3647 			memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
3648 			       sizeof(cfilter.ip.v6.dst_ip6));
3649 		if (mask.src_ip[3] & tcf.src_ip[3])
3650 			memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
3651 			       sizeof(cfilter.ip.v6.src_ip6));
3652 		break;
3653 	default:
3654 		/* TC filter can be configured based on different combinations
3655 		 * and in this case IP is not a part of filter config
3656 		 */
3657 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3658 			 vf->vf_id);
3659 	}
3660 
3661 	/* get the vsi to which the tc belongs to */
3662 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3663 	cfilter.seid = vsi->seid;
3664 	cfilter.flags = vcf->field_flags;
3665 
3666 	/* Deleting TC filter */
3667 	if (tcf.dst_port)
3668 		ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
3669 	else
3670 		ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
3671 	if (ret) {
3672 		dev_err(&pf->pdev->dev,
3673 			"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3674 			vf->vf_id, i40e_stat_str(&pf->hw, ret),
3675 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3676 		goto err;
3677 	}
3678 
3679 	hlist_for_each_entry_safe(cf, node,
3680 				  &vf->cloud_filter_list, cloud_node) {
3681 		if (cf->seid != cfilter.seid)
3682 			continue;
3683 		if (mask.dst_port)
3684 			if (cfilter.dst_port != cf->dst_port)
3685 				continue;
3686 		if (mask.dst_mac[0])
3687 			if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
3688 				continue;
3689 		/* for ipv4 data to be valid, only first byte of mask is set */
3690 		if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
3691 			if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
3692 				   ARRAY_SIZE(tcf.dst_ip)))
3693 				continue;
3694 		/* for ipv6, mask is set for all sixteen bytes (4 words) */
3695 		if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
3696 			if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
3697 				   sizeof(cfilter.ip.v6.src_ip6)))
3698 				continue;
3699 		if (mask.vlan_id)
3700 			if (cfilter.vlan_id != cf->vlan_id)
3701 				continue;
3702 
3703 		hlist_del(&cf->cloud_node);
3704 		kfree(cf);
3705 		vf->num_cloud_filters--;
3706 	}
3707 
3708 err:
3709 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
3710 				       aq_ret);
3711 }
3712 
3713 /**
3714  * i40e_vc_add_cloud_filter
3715  * @vf: pointer to the VF info
3716  * @msg: pointer to the msg buffer
3717  *
3718  * This function adds a cloud filter programmed as TC filter for ADq
3719  **/
i40e_vc_add_cloud_filter(struct i40e_vf * vf,u8 * msg)3720 static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
3721 {
3722 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3723 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3724 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3725 	struct i40e_cloud_filter *cfilter = NULL;
3726 	struct i40e_pf *pf = vf->pf;
3727 	struct i40e_vsi *vsi = NULL;
3728 	i40e_status aq_ret = 0;
3729 	int i, ret;
3730 
3731 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3732 		aq_ret = I40E_ERR_PARAM;
3733 		goto err_out;
3734 	}
3735 
3736 	if (!vf->adq_enabled) {
3737 		dev_info(&pf->pdev->dev,
3738 			 "VF %d: ADq is not enabled, can't apply cloud filter\n",
3739 			 vf->vf_id);
3740 		aq_ret = I40E_ERR_PARAM;
3741 		goto err_out;
3742 	}
3743 
3744 	if (i40e_validate_cloud_filter(vf, vcf)) {
3745 		dev_info(&pf->pdev->dev,
3746 			 "VF %d: Invalid input/s, can't apply cloud filter\n",
3747 			 vf->vf_id);
3748 		aq_ret = I40E_ERR_PARAM;
3749 		goto err_out;
3750 	}
3751 
3752 	cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
3753 	if (!cfilter)
3754 		return -ENOMEM;
3755 
3756 	/* parse destination mac address */
3757 	for (i = 0; i < ETH_ALEN; i++)
3758 		cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3759 
3760 	/* parse source mac address */
3761 	for (i = 0; i < ETH_ALEN; i++)
3762 		cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3763 
3764 	cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
3765 	cfilter->dst_port = mask.dst_port & tcf.dst_port;
3766 	cfilter->src_port = mask.src_port & tcf.src_port;
3767 
3768 	switch (vcf->flow_type) {
3769 	case VIRTCHNL_TCP_V4_FLOW:
3770 		cfilter->n_proto = ETH_P_IP;
3771 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3772 			memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
3773 			       ARRAY_SIZE(tcf.dst_ip));
3774 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3775 			memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
3776 			       ARRAY_SIZE(tcf.dst_ip));
3777 		break;
3778 	case VIRTCHNL_TCP_V6_FLOW:
3779 		cfilter->n_proto = ETH_P_IPV6;
3780 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3781 			memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
3782 			       sizeof(cfilter->ip.v6.dst_ip6));
3783 		if (mask.src_ip[3] & tcf.src_ip[3])
3784 			memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
3785 			       sizeof(cfilter->ip.v6.src_ip6));
3786 		break;
3787 	default:
3788 		/* TC filter can be configured based on different combinations
3789 		 * and in this case IP is not a part of filter config
3790 		 */
3791 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3792 			 vf->vf_id);
3793 	}
3794 
3795 	/* get the VSI to which the TC belongs to */
3796 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3797 	cfilter->seid = vsi->seid;
3798 	cfilter->flags = vcf->field_flags;
3799 
3800 	/* Adding cloud filter programmed as TC filter */
3801 	if (tcf.dst_port)
3802 		ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
3803 	else
3804 		ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
3805 	if (ret) {
3806 		dev_err(&pf->pdev->dev,
3807 			"VF %d: Failed to add cloud filter, err %s aq_err %s\n",
3808 			vf->vf_id, i40e_stat_str(&pf->hw, ret),
3809 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3810 		goto err_free;
3811 	}
3812 
3813 	INIT_HLIST_NODE(&cfilter->cloud_node);
3814 	hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
3815 	/* release the pointer passing it to the collection */
3816 	cfilter = NULL;
3817 	vf->num_cloud_filters++;
3818 err_free:
3819 	kfree(cfilter);
3820 err_out:
3821 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
3822 				       aq_ret);
3823 }
3824 
3825 /**
3826  * i40e_vc_add_qch_msg: Add queue channel and enable ADq
3827  * @vf: pointer to the VF info
3828  * @msg: pointer to the msg buffer
3829  **/
i40e_vc_add_qch_msg(struct i40e_vf * vf,u8 * msg)3830 static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
3831 {
3832 	struct virtchnl_tc_info *tci =
3833 		(struct virtchnl_tc_info *)msg;
3834 	struct i40e_pf *pf = vf->pf;
3835 	struct i40e_link_status *ls = &pf->hw.phy.link_info;
3836 	int i, adq_request_qps = 0;
3837 	i40e_status aq_ret = 0;
3838 	u64 speed = 0;
3839 
3840 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3841 		aq_ret = I40E_ERR_PARAM;
3842 		goto err;
3843 	}
3844 
3845 	/* ADq cannot be applied if spoof check is ON */
3846 	if (vf->spoofchk) {
3847 		dev_err(&pf->pdev->dev,
3848 			"Spoof check is ON, turn it OFF to enable ADq\n");
3849 		aq_ret = I40E_ERR_PARAM;
3850 		goto err;
3851 	}
3852 
3853 	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3854 		dev_err(&pf->pdev->dev,
3855 			"VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
3856 			vf->vf_id);
3857 		aq_ret = I40E_ERR_PARAM;
3858 		goto err;
3859 	}
3860 
3861 	/* max number of traffic classes for VF currently capped at 4 */
3862 	if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
3863 		dev_err(&pf->pdev->dev,
3864 			"VF %d trying to set %u TCs, valid range 1-%u TCs per VF\n",
3865 			vf->vf_id, tci->num_tc, I40E_MAX_VF_VSI);
3866 		aq_ret = I40E_ERR_PARAM;
3867 		goto err;
3868 	}
3869 
3870 	/* validate queues for each TC */
3871 	for (i = 0; i < tci->num_tc; i++)
3872 		if (!tci->list[i].count ||
3873 		    tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
3874 			dev_err(&pf->pdev->dev,
3875 				"VF %d: TC %d trying to set %u queues, valid range 1-%u queues per TC\n",
3876 				vf->vf_id, i, tci->list[i].count,
3877 				I40E_DEFAULT_QUEUES_PER_VF);
3878 			aq_ret = I40E_ERR_PARAM;
3879 			goto err;
3880 		}
3881 
3882 	/* need Max VF queues but already have default number of queues */
3883 	adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;
3884 
3885 	if (pf->queues_left < adq_request_qps) {
3886 		dev_err(&pf->pdev->dev,
3887 			"No queues left to allocate to VF %d\n",
3888 			vf->vf_id);
3889 		aq_ret = I40E_ERR_PARAM;
3890 		goto err;
3891 	} else {
3892 		/* we need to allocate max VF queues to enable ADq so as to
3893 		 * make sure ADq enabled VF always gets back queues when it
3894 		 * goes through a reset.
3895 		 */
3896 		vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
3897 	}
3898 
3899 	/* get link speed in MB to validate rate limit */
3900 	switch (ls->link_speed) {
3901 	case VIRTCHNL_LINK_SPEED_100MB:
3902 		speed = SPEED_100;
3903 		break;
3904 	case VIRTCHNL_LINK_SPEED_1GB:
3905 		speed = SPEED_1000;
3906 		break;
3907 	case VIRTCHNL_LINK_SPEED_10GB:
3908 		speed = SPEED_10000;
3909 		break;
3910 	case VIRTCHNL_LINK_SPEED_20GB:
3911 		speed = SPEED_20000;
3912 		break;
3913 	case VIRTCHNL_LINK_SPEED_25GB:
3914 		speed = SPEED_25000;
3915 		break;
3916 	case VIRTCHNL_LINK_SPEED_40GB:
3917 		speed = SPEED_40000;
3918 		break;
3919 	default:
3920 		dev_err(&pf->pdev->dev,
3921 			"Cannot detect link speed\n");
3922 		aq_ret = I40E_ERR_PARAM;
3923 		goto err;
3924 	}
3925 
3926 	/* parse data from the queue channel info */
3927 	vf->num_tc = tci->num_tc;
3928 	for (i = 0; i < vf->num_tc; i++) {
3929 		if (tci->list[i].max_tx_rate) {
3930 			if (tci->list[i].max_tx_rate > speed) {
3931 				dev_err(&pf->pdev->dev,
3932 					"Invalid max tx rate %llu specified for VF %d.",
3933 					tci->list[i].max_tx_rate,
3934 					vf->vf_id);
3935 				aq_ret = I40E_ERR_PARAM;
3936 				goto err;
3937 			} else {
3938 				vf->ch[i].max_tx_rate =
3939 					tci->list[i].max_tx_rate;
3940 			}
3941 		}
3942 		vf->ch[i].num_qps = tci->list[i].count;
3943 	}
3944 
3945 	/* set this flag only after making sure all inputs are sane */
3946 	vf->adq_enabled = true;
3947 
3948 	/* reset the VF in order to allocate resources */
3949 	i40e_vc_reset_vf(vf, true);
3950 
3951 	return I40E_SUCCESS;
3952 
3953 	/* send the response to the VF */
3954 err:
3955 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
3956 				       aq_ret);
3957 }
3958 
3959 /**
3960  * i40e_vc_del_qch_msg
3961  * @vf: pointer to the VF info
3962  * @msg: pointer to the msg buffer
3963  **/
i40e_vc_del_qch_msg(struct i40e_vf * vf,u8 * msg)3964 static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
3965 {
3966 	struct i40e_pf *pf = vf->pf;
3967 	i40e_status aq_ret = 0;
3968 
3969 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3970 		aq_ret = I40E_ERR_PARAM;
3971 		goto err;
3972 	}
3973 
3974 	if (vf->adq_enabled) {
3975 		i40e_del_all_cloud_filters(vf);
3976 		i40e_del_qch(vf);
3977 		vf->adq_enabled = false;
3978 		vf->num_tc = 0;
3979 		dev_info(&pf->pdev->dev,
3980 			 "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
3981 			 vf->vf_id);
3982 	} else {
3983 		dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
3984 			 vf->vf_id);
3985 		aq_ret = I40E_ERR_PARAM;
3986 	}
3987 
3988 	/* reset the VF in order to allocate resources */
3989 	i40e_vc_reset_vf(vf, true);
3990 
3991 	return I40E_SUCCESS;
3992 
3993 err:
3994 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
3995 				       aq_ret);
3996 }
3997 
3998 /**
3999  * i40e_vc_process_vf_msg
4000  * @pf: pointer to the PF structure
4001  * @vf_id: source VF id
4002  * @v_opcode: operation code
4003  * @v_retval: unused return value code
4004  * @msg: pointer to the msg buffer
4005  * @msglen: msg length
4006  *
4007  * called from the common aeq/arq handler to
4008  * process request from VF
4009  **/
i40e_vc_process_vf_msg(struct i40e_pf * pf,s16 vf_id,u32 v_opcode,u32 __always_unused v_retval,u8 * msg,u16 msglen)4010 int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
4011 			   u32 __always_unused v_retval, u8 *msg, u16 msglen)
4012 {
4013 	struct i40e_hw *hw = &pf->hw;
4014 	int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
4015 	struct i40e_vf *vf;
4016 	int ret;
4017 
4018 	pf->vf_aq_requests++;
4019 	if (local_vf_id < 0 || local_vf_id >= pf->num_alloc_vfs)
4020 		return -EINVAL;
4021 	vf = &(pf->vf[local_vf_id]);
4022 
4023 	/* Check if VF is disabled. */
4024 	if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
4025 		return I40E_ERR_PARAM;
4026 
4027 	/* perform basic checks on the msg */
4028 	ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4029 
4030 	if (ret) {
4031 		i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
4032 		dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
4033 			local_vf_id, v_opcode, msglen);
4034 		switch (ret) {
4035 		case VIRTCHNL_STATUS_ERR_PARAM:
4036 			return -EPERM;
4037 		default:
4038 			return -EINVAL;
4039 		}
4040 	}
4041 
4042 	switch (v_opcode) {
4043 	case VIRTCHNL_OP_VERSION:
4044 		ret = i40e_vc_get_version_msg(vf, msg);
4045 		break;
4046 	case VIRTCHNL_OP_GET_VF_RESOURCES:
4047 		ret = i40e_vc_get_vf_resources_msg(vf, msg);
4048 		i40e_vc_notify_vf_link_state(vf);
4049 		break;
4050 	case VIRTCHNL_OP_RESET_VF:
4051 		i40e_vc_reset_vf(vf, false);
4052 		ret = 0;
4053 		break;
4054 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4055 		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg);
4056 		break;
4057 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4058 		ret = i40e_vc_config_queues_msg(vf, msg);
4059 		break;
4060 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4061 		ret = i40e_vc_config_irq_map_msg(vf, msg);
4062 		break;
4063 	case VIRTCHNL_OP_ENABLE_QUEUES:
4064 		ret = i40e_vc_enable_queues_msg(vf, msg);
4065 		i40e_vc_notify_vf_link_state(vf);
4066 		break;
4067 	case VIRTCHNL_OP_DISABLE_QUEUES:
4068 		ret = i40e_vc_disable_queues_msg(vf, msg);
4069 		break;
4070 	case VIRTCHNL_OP_ADD_ETH_ADDR:
4071 		ret = i40e_vc_add_mac_addr_msg(vf, msg);
4072 		break;
4073 	case VIRTCHNL_OP_DEL_ETH_ADDR:
4074 		ret = i40e_vc_del_mac_addr_msg(vf, msg);
4075 		break;
4076 	case VIRTCHNL_OP_ADD_VLAN:
4077 		ret = i40e_vc_add_vlan_msg(vf, msg);
4078 		break;
4079 	case VIRTCHNL_OP_DEL_VLAN:
4080 		ret = i40e_vc_remove_vlan_msg(vf, msg);
4081 		break;
4082 	case VIRTCHNL_OP_GET_STATS:
4083 		ret = i40e_vc_get_stats_msg(vf, msg);
4084 		break;
4085 	case VIRTCHNL_OP_IWARP:
4086 		ret = i40e_vc_iwarp_msg(vf, msg, msglen);
4087 		break;
4088 	case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
4089 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, true);
4090 		break;
4091 	case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
4092 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, false);
4093 		break;
4094 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
4095 		ret = i40e_vc_config_rss_key(vf, msg);
4096 		break;
4097 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
4098 		ret = i40e_vc_config_rss_lut(vf, msg);
4099 		break;
4100 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
4101 		ret = i40e_vc_get_rss_hena(vf, msg);
4102 		break;
4103 	case VIRTCHNL_OP_SET_RSS_HENA:
4104 		ret = i40e_vc_set_rss_hena(vf, msg);
4105 		break;
4106 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4107 		ret = i40e_vc_enable_vlan_stripping(vf, msg);
4108 		break;
4109 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4110 		ret = i40e_vc_disable_vlan_stripping(vf, msg);
4111 		break;
4112 	case VIRTCHNL_OP_REQUEST_QUEUES:
4113 		ret = i40e_vc_request_queues_msg(vf, msg);
4114 		break;
4115 	case VIRTCHNL_OP_ENABLE_CHANNELS:
4116 		ret = i40e_vc_add_qch_msg(vf, msg);
4117 		break;
4118 	case VIRTCHNL_OP_DISABLE_CHANNELS:
4119 		ret = i40e_vc_del_qch_msg(vf, msg);
4120 		break;
4121 	case VIRTCHNL_OP_ADD_CLOUD_FILTER:
4122 		ret = i40e_vc_add_cloud_filter(vf, msg);
4123 		break;
4124 	case VIRTCHNL_OP_DEL_CLOUD_FILTER:
4125 		ret = i40e_vc_del_cloud_filter(vf, msg);
4126 		break;
4127 	case VIRTCHNL_OP_UNKNOWN:
4128 	default:
4129 		dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
4130 			v_opcode, local_vf_id);
4131 		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
4132 					      I40E_ERR_NOT_IMPLEMENTED);
4133 		break;
4134 	}
4135 
4136 	return ret;
4137 }
4138 
4139 /**
4140  * i40e_vc_process_vflr_event
4141  * @pf: pointer to the PF structure
4142  *
4143  * called from the vlfr irq handler to
4144  * free up VF resources and state variables
4145  **/
i40e_vc_process_vflr_event(struct i40e_pf * pf)4146 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
4147 {
4148 	struct i40e_hw *hw = &pf->hw;
4149 	u32 reg, reg_idx, bit_idx;
4150 	struct i40e_vf *vf;
4151 	int vf_id;
4152 
4153 	if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
4154 		return 0;
4155 
4156 	/* Re-enable the VFLR interrupt cause here, before looking for which
4157 	 * VF got reset. Otherwise, if another VF gets a reset while the
4158 	 * first one is being processed, that interrupt will be lost, and
4159 	 * that VF will be stuck in reset forever.
4160 	 */
4161 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4162 	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
4163 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4164 	i40e_flush(hw);
4165 
4166 	clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4167 	for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
4168 		reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
4169 		bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
4170 		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
4171 		vf = &pf->vf[vf_id];
4172 		reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
4173 		if (reg & BIT(bit_idx))
4174 			/* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
4175 			i40e_reset_vf(vf, true);
4176 	}
4177 
4178 	return 0;
4179 }
4180 
4181 /**
4182  * i40e_validate_vf
4183  * @pf: the physical function
4184  * @vf_id: VF identifier
4185  *
4186  * Check that the VF is enabled and the VSI exists.
4187  *
4188  * Returns 0 on success, negative on failure
4189  **/
i40e_validate_vf(struct i40e_pf * pf,int vf_id)4190 static int i40e_validate_vf(struct i40e_pf *pf, int vf_id)
4191 {
4192 	struct i40e_vsi *vsi;
4193 	struct i40e_vf *vf;
4194 	int ret = 0;
4195 
4196 	if (vf_id >= pf->num_alloc_vfs) {
4197 		dev_err(&pf->pdev->dev,
4198 			"Invalid VF Identifier %d\n", vf_id);
4199 		ret = -EINVAL;
4200 		goto err_out;
4201 	}
4202 	vf = &pf->vf[vf_id];
4203 	vsi = i40e_find_vsi_from_id(pf, vf->lan_vsi_id);
4204 	if (!vsi)
4205 		ret = -EINVAL;
4206 err_out:
4207 	return ret;
4208 }
4209 
4210 /**
4211  * i40e_ndo_set_vf_mac
4212  * @netdev: network interface device structure
4213  * @vf_id: VF identifier
4214  * @mac: mac address
4215  *
4216  * program VF mac address
4217  **/
i40e_ndo_set_vf_mac(struct net_device * netdev,int vf_id,u8 * mac)4218 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
4219 {
4220 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4221 	struct i40e_vsi *vsi = np->vsi;
4222 	struct i40e_pf *pf = vsi->back;
4223 	struct i40e_mac_filter *f;
4224 	struct i40e_vf *vf;
4225 	int ret = 0;
4226 	struct hlist_node *h;
4227 	int bkt;
4228 	u8 i;
4229 
4230 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4231 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4232 		return -EAGAIN;
4233 	}
4234 
4235 	/* validate the request */
4236 	ret = i40e_validate_vf(pf, vf_id);
4237 	if (ret)
4238 		goto error_param;
4239 
4240 	vf = &pf->vf[vf_id];
4241 
4242 	/* When the VF is resetting wait until it is done.
4243 	 * It can take up to 200 milliseconds,
4244 	 * but wait for up to 300 milliseconds to be safe.
4245 	 * Acquire the VSI pointer only after the VF has been
4246 	 * properly initialized.
4247 	 */
4248 	for (i = 0; i < 15; i++) {
4249 		if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states))
4250 			break;
4251 		msleep(20);
4252 	}
4253 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4254 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4255 			vf_id);
4256 		ret = -EAGAIN;
4257 		goto error_param;
4258 	}
4259 	vsi = pf->vsi[vf->lan_vsi_idx];
4260 
4261 	if (is_multicast_ether_addr(mac)) {
4262 		dev_err(&pf->pdev->dev,
4263 			"Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
4264 		ret = -EINVAL;
4265 		goto error_param;
4266 	}
4267 
4268 	/* Lock once because below invoked function add/del_filter requires
4269 	 * mac_filter_hash_lock to be held
4270 	 */
4271 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4272 
4273 	/* delete the temporary mac address */
4274 	if (!is_zero_ether_addr(vf->default_lan_addr.addr))
4275 		i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
4276 
4277 	/* Delete all the filters for this VSI - we're going to kill it
4278 	 * anyway.
4279 	 */
4280 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
4281 		__i40e_del_filter(vsi, f);
4282 
4283 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4284 
4285 	/* program mac filter */
4286 	if (i40e_sync_vsi_filters(vsi)) {
4287 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
4288 		ret = -EIO;
4289 		goto error_param;
4290 	}
4291 	ether_addr_copy(vf->default_lan_addr.addr, mac);
4292 
4293 	if (is_zero_ether_addr(mac)) {
4294 		vf->pf_set_mac = false;
4295 		dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
4296 	} else {
4297 		vf->pf_set_mac = true;
4298 		dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
4299 			 mac, vf_id);
4300 	}
4301 
4302 	/* Force the VF interface down so it has to bring up with new MAC
4303 	 * address
4304 	 */
4305 	i40e_vc_reset_vf(vf, true);
4306 	dev_info(&pf->pdev->dev, "Bring down and up the VF interface to make this change effective.\n");
4307 
4308 error_param:
4309 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4310 	return ret;
4311 }
4312 
4313 /**
4314  * i40e_ndo_set_vf_port_vlan
4315  * @netdev: network interface device structure
4316  * @vf_id: VF identifier
4317  * @vlan_id: mac address
4318  * @qos: priority setting
4319  * @vlan_proto: vlan protocol
4320  *
4321  * program VF vlan id and/or qos
4322  **/
i40e_ndo_set_vf_port_vlan(struct net_device * netdev,int vf_id,u16 vlan_id,u8 qos,__be16 vlan_proto)4323 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
4324 			      u16 vlan_id, u8 qos, __be16 vlan_proto)
4325 {
4326 	u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
4327 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4328 	bool allmulti = false, alluni = false;
4329 	struct i40e_pf *pf = np->vsi->back;
4330 	struct i40e_vsi *vsi;
4331 	struct i40e_vf *vf;
4332 	int ret = 0;
4333 
4334 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4335 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4336 		return -EAGAIN;
4337 	}
4338 
4339 	/* validate the request */
4340 	ret = i40e_validate_vf(pf, vf_id);
4341 	if (ret)
4342 		goto error_pvid;
4343 
4344 	if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
4345 		dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
4346 		ret = -EINVAL;
4347 		goto error_pvid;
4348 	}
4349 
4350 	if (vlan_proto != htons(ETH_P_8021Q)) {
4351 		dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
4352 		ret = -EPROTONOSUPPORT;
4353 		goto error_pvid;
4354 	}
4355 
4356 	vf = &pf->vf[vf_id];
4357 	vsi = pf->vsi[vf->lan_vsi_idx];
4358 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4359 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4360 			vf_id);
4361 		ret = -EAGAIN;
4362 		goto error_pvid;
4363 	}
4364 
4365 	if (le16_to_cpu(vsi->info.pvid) == vlanprio)
4366 		/* duplicate request, so just return success */
4367 		goto error_pvid;
4368 
4369 	/* Locked once because multiple functions below iterate list */
4370 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4371 
4372 	/* Check for condition where there was already a port VLAN ID
4373 	 * filter set and now it is being deleted by setting it to zero.
4374 	 * Additionally check for the condition where there was a port
4375 	 * VLAN but now there is a new and different port VLAN being set.
4376 	 * Before deleting all the old VLAN filters we must add new ones
4377 	 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
4378 	 * MAC addresses deleted.
4379 	 */
4380 	if ((!(vlan_id || qos) ||
4381 	    vlanprio != le16_to_cpu(vsi->info.pvid)) &&
4382 	    vsi->info.pvid) {
4383 		ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
4384 		if (ret) {
4385 			dev_info(&vsi->back->pdev->dev,
4386 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4387 				 vsi->back->hw.aq.asq_last_status);
4388 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4389 			goto error_pvid;
4390 		}
4391 	}
4392 
4393 	if (vsi->info.pvid) {
4394 		/* remove all filters on the old VLAN */
4395 		i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
4396 					   VLAN_VID_MASK));
4397 	}
4398 
4399 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4400 
4401 	/* disable promisc modes in case they were enabled */
4402 	ret = i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id,
4403 					      allmulti, alluni);
4404 	if (ret) {
4405 		dev_err(&pf->pdev->dev, "Unable to config VF promiscuous mode\n");
4406 		goto error_pvid;
4407 	}
4408 
4409 	if (vlan_id || qos)
4410 		ret = i40e_vsi_add_pvid(vsi, vlanprio);
4411 	else
4412 		i40e_vsi_remove_pvid(vsi);
4413 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4414 
4415 	if (vlan_id) {
4416 		dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
4417 			 vlan_id, qos, vf_id);
4418 
4419 		/* add new VLAN filter for each MAC */
4420 		ret = i40e_add_vlan_all_mac(vsi, vlan_id);
4421 		if (ret) {
4422 			dev_info(&vsi->back->pdev->dev,
4423 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4424 				 vsi->back->hw.aq.asq_last_status);
4425 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4426 			goto error_pvid;
4427 		}
4428 
4429 		/* remove the previously added non-VLAN MAC filters */
4430 		i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
4431 	}
4432 
4433 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4434 
4435 	if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
4436 		alluni = true;
4437 
4438 	if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
4439 		allmulti = true;
4440 
4441 	/* Schedule the worker thread to take care of applying changes */
4442 	i40e_service_event_schedule(vsi->back);
4443 
4444 	if (ret) {
4445 		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
4446 		goto error_pvid;
4447 	}
4448 
4449 	/* The Port VLAN needs to be saved across resets the same as the
4450 	 * default LAN MAC address.
4451 	 */
4452 	vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
4453 
4454 	i40e_vc_reset_vf(vf, true);
4455 	/* During reset the VF got a new VSI, so refresh a pointer. */
4456 	vsi = pf->vsi[vf->lan_vsi_idx];
4457 
4458 	ret = i40e_config_vf_promiscuous_mode(vf, vsi->id, allmulti, alluni);
4459 	if (ret) {
4460 		dev_err(&pf->pdev->dev, "Unable to config vf promiscuous mode\n");
4461 		goto error_pvid;
4462 	}
4463 
4464 	ret = 0;
4465 
4466 error_pvid:
4467 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4468 	return ret;
4469 }
4470 
4471 /**
4472  * i40e_ndo_set_vf_bw
4473  * @netdev: network interface device structure
4474  * @vf_id: VF identifier
4475  * @min_tx_rate: Minimum Tx rate
4476  * @max_tx_rate: Maximum Tx rate
4477  *
4478  * configure VF Tx rate
4479  **/
i40e_ndo_set_vf_bw(struct net_device * netdev,int vf_id,int min_tx_rate,int max_tx_rate)4480 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
4481 		       int max_tx_rate)
4482 {
4483 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4484 	struct i40e_pf *pf = np->vsi->back;
4485 	struct i40e_vsi *vsi;
4486 	struct i40e_vf *vf;
4487 	int ret = 0;
4488 
4489 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4490 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4491 		return -EAGAIN;
4492 	}
4493 
4494 	/* validate the request */
4495 	ret = i40e_validate_vf(pf, vf_id);
4496 	if (ret)
4497 		goto error;
4498 
4499 	if (min_tx_rate) {
4500 		dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
4501 			min_tx_rate, vf_id);
4502 		ret = -EINVAL;
4503 		goto error;
4504 	}
4505 
4506 	vf = &pf->vf[vf_id];
4507 	vsi = pf->vsi[vf->lan_vsi_idx];
4508 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4509 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4510 			vf_id);
4511 		ret = -EAGAIN;
4512 		goto error;
4513 	}
4514 
4515 	ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
4516 	if (ret)
4517 		goto error;
4518 
4519 	vf->tx_rate = max_tx_rate;
4520 error:
4521 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4522 	return ret;
4523 }
4524 
4525 /**
4526  * i40e_ndo_get_vf_config
4527  * @netdev: network interface device structure
4528  * @vf_id: VF identifier
4529  * @ivi: VF configuration structure
4530  *
4531  * return VF configuration
4532  **/
i40e_ndo_get_vf_config(struct net_device * netdev,int vf_id,struct ifla_vf_info * ivi)4533 int i40e_ndo_get_vf_config(struct net_device *netdev,
4534 			   int vf_id, struct ifla_vf_info *ivi)
4535 {
4536 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4537 	struct i40e_vsi *vsi = np->vsi;
4538 	struct i40e_pf *pf = vsi->back;
4539 	struct i40e_vf *vf;
4540 	int ret = 0;
4541 
4542 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4543 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4544 		return -EAGAIN;
4545 	}
4546 
4547 	/* validate the request */
4548 	ret = i40e_validate_vf(pf, vf_id);
4549 	if (ret)
4550 		goto error_param;
4551 
4552 	vf = &pf->vf[vf_id];
4553 	/* first vsi is always the LAN vsi */
4554 	vsi = pf->vsi[vf->lan_vsi_idx];
4555 	if (!vsi) {
4556 		ret = -ENOENT;
4557 		goto error_param;
4558 	}
4559 
4560 	ivi->vf = vf_id;
4561 
4562 	ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
4563 
4564 	ivi->max_tx_rate = vf->tx_rate;
4565 	ivi->min_tx_rate = 0;
4566 	ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
4567 	ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
4568 		   I40E_VLAN_PRIORITY_SHIFT;
4569 	if (vf->link_forced == false)
4570 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4571 	else if (vf->link_up == true)
4572 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4573 	else
4574 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4575 	ivi->spoofchk = vf->spoofchk;
4576 	ivi->trusted = vf->trusted;
4577 	ret = 0;
4578 
4579 error_param:
4580 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4581 	return ret;
4582 }
4583 
4584 /**
4585  * i40e_ndo_set_vf_link_state
4586  * @netdev: network interface device structure
4587  * @vf_id: VF identifier
4588  * @link: required link state
4589  *
4590  * Set the link state of a specified VF, regardless of physical link state
4591  **/
i40e_ndo_set_vf_link_state(struct net_device * netdev,int vf_id,int link)4592 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
4593 {
4594 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4595 	struct i40e_pf *pf = np->vsi->back;
4596 	struct virtchnl_pf_event pfe;
4597 	struct i40e_hw *hw = &pf->hw;
4598 	struct i40e_vsi *vsi;
4599 	unsigned long q_map;
4600 	struct i40e_vf *vf;
4601 	int abs_vf_id;
4602 	int ret = 0;
4603 	int tmp;
4604 
4605 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4606 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4607 		return -EAGAIN;
4608 	}
4609 
4610 	/* validate the request */
4611 	if (vf_id >= pf->num_alloc_vfs) {
4612 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4613 		ret = -EINVAL;
4614 		goto error_out;
4615 	}
4616 
4617 	vf = &pf->vf[vf_id];
4618 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
4619 
4620 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
4621 	pfe.severity = PF_EVENT_SEVERITY_INFO;
4622 
4623 	switch (link) {
4624 	case IFLA_VF_LINK_STATE_AUTO:
4625 		vf->link_forced = false;
4626 		vf->is_disabled_from_host = false;
4627 		/* reset needed to reinit VF resources */
4628 		i40e_vc_reset_vf(vf, true);
4629 		pfe.event_data.link_event.link_status =
4630 			pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
4631 		pfe.event_data.link_event.link_speed =
4632 			(enum virtchnl_link_speed)
4633 			pf->hw.phy.link_info.link_speed;
4634 		break;
4635 	case IFLA_VF_LINK_STATE_ENABLE:
4636 		vf->link_forced = true;
4637 		vf->link_up = true;
4638 		vf->is_disabled_from_host = false;
4639 		/* reset needed to reinit VF resources */
4640 		i40e_vc_reset_vf(vf, true);
4641 		pfe.event_data.link_event.link_status = true;
4642 		pfe.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
4643 		break;
4644 	case IFLA_VF_LINK_STATE_DISABLE:
4645 		vf->link_forced = true;
4646 		vf->link_up = false;
4647 		pfe.event_data.link_event.link_status = false;
4648 		pfe.event_data.link_event.link_speed = 0;
4649 
4650 		vsi = pf->vsi[vf->lan_vsi_idx];
4651 		q_map = BIT(vsi->num_queue_pairs) - 1;
4652 
4653 		vf->is_disabled_from_host = true;
4654 
4655 		/* Try to stop both Tx&Rx rings even if one of the calls fails
4656 		 * to ensure we stop the rings even in case of errors.
4657 		 * If any of them returns with an error then the first
4658 		 * error that occurred will be returned.
4659 		 */
4660 		tmp = i40e_ctrl_vf_tx_rings(vsi, q_map, false);
4661 		ret = i40e_ctrl_vf_rx_rings(vsi, q_map, false);
4662 
4663 		ret = tmp ? tmp : ret;
4664 		break;
4665 	default:
4666 		ret = -EINVAL;
4667 		goto error_out;
4668 	}
4669 	/* Notify the VF of its new link state */
4670 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
4671 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
4672 
4673 error_out:
4674 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4675 	return ret;
4676 }
4677 
4678 /**
4679  * i40e_ndo_set_vf_spoofchk
4680  * @netdev: network interface device structure
4681  * @vf_id: VF identifier
4682  * @enable: flag to enable or disable feature
4683  *
4684  * Enable or disable VF spoof checking
4685  **/
i40e_ndo_set_vf_spoofchk(struct net_device * netdev,int vf_id,bool enable)4686 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
4687 {
4688 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4689 	struct i40e_vsi *vsi = np->vsi;
4690 	struct i40e_pf *pf = vsi->back;
4691 	struct i40e_vsi_context ctxt;
4692 	struct i40e_hw *hw = &pf->hw;
4693 	struct i40e_vf *vf;
4694 	int ret = 0;
4695 
4696 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4697 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4698 		return -EAGAIN;
4699 	}
4700 
4701 	/* validate the request */
4702 	if (vf_id >= pf->num_alloc_vfs) {
4703 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4704 		ret = -EINVAL;
4705 		goto out;
4706 	}
4707 
4708 	vf = &(pf->vf[vf_id]);
4709 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4710 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4711 			vf_id);
4712 		ret = -EAGAIN;
4713 		goto out;
4714 	}
4715 
4716 	if (enable == vf->spoofchk)
4717 		goto out;
4718 
4719 	vf->spoofchk = enable;
4720 	memset(&ctxt, 0, sizeof(ctxt));
4721 	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
4722 	ctxt.pf_num = pf->hw.pf_id;
4723 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
4724 	if (enable)
4725 		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
4726 					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
4727 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
4728 	if (ret) {
4729 		dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
4730 			ret);
4731 		ret = -EIO;
4732 	}
4733 out:
4734 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4735 	return ret;
4736 }
4737 
4738 /**
4739  * i40e_ndo_set_vf_trust
4740  * @netdev: network interface device structure of the pf
4741  * @vf_id: VF identifier
4742  * @setting: trust setting
4743  *
4744  * Enable or disable VF trust setting
4745  **/
i40e_ndo_set_vf_trust(struct net_device * netdev,int vf_id,bool setting)4746 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
4747 {
4748 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4749 	struct i40e_pf *pf = np->vsi->back;
4750 	struct i40e_vf *vf;
4751 	int ret = 0;
4752 
4753 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4754 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4755 		return -EAGAIN;
4756 	}
4757 
4758 	/* validate the request */
4759 	if (vf_id >= pf->num_alloc_vfs) {
4760 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4761 		ret = -EINVAL;
4762 		goto out;
4763 	}
4764 
4765 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4766 		dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
4767 		ret = -EINVAL;
4768 		goto out;
4769 	}
4770 
4771 	vf = &pf->vf[vf_id];
4772 
4773 	if (setting == vf->trusted)
4774 		goto out;
4775 
4776 	vf->trusted = setting;
4777 	i40e_vc_reset_vf(vf, true);
4778 	dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
4779 		 vf_id, setting ? "" : "un");
4780 
4781 	if (vf->adq_enabled) {
4782 		if (!vf->trusted) {
4783 			dev_info(&pf->pdev->dev,
4784 				 "VF %u no longer Trusted, deleting all cloud filters\n",
4785 				 vf_id);
4786 			i40e_del_all_cloud_filters(vf);
4787 		}
4788 	}
4789 
4790 out:
4791 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4792 	return ret;
4793 }
4794 
4795 /**
4796  * i40e_get_vf_stats - populate some stats for the VF
4797  * @netdev: the netdev of the PF
4798  * @vf_id: the host OS identifier (0-127)
4799  * @vf_stats: pointer to the OS memory to be initialized
4800  */
i40e_get_vf_stats(struct net_device * netdev,int vf_id,struct ifla_vf_stats * vf_stats)4801 int i40e_get_vf_stats(struct net_device *netdev, int vf_id,
4802 		      struct ifla_vf_stats *vf_stats)
4803 {
4804 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4805 	struct i40e_pf *pf = np->vsi->back;
4806 	struct i40e_eth_stats *stats;
4807 	struct i40e_vsi *vsi;
4808 	struct i40e_vf *vf;
4809 
4810 	/* validate the request */
4811 	if (i40e_validate_vf(pf, vf_id))
4812 		return -EINVAL;
4813 
4814 	vf = &pf->vf[vf_id];
4815 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4816 		dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
4817 		return -EBUSY;
4818 	}
4819 
4820 	vsi = pf->vsi[vf->lan_vsi_idx];
4821 	if (!vsi)
4822 		return -EINVAL;
4823 
4824 	i40e_update_eth_stats(vsi);
4825 	stats = &vsi->eth_stats;
4826 
4827 	memset(vf_stats, 0, sizeof(*vf_stats));
4828 
4829 	vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
4830 		stats->rx_multicast;
4831 	vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
4832 		stats->tx_multicast;
4833 	vf_stats->rx_bytes   = stats->rx_bytes;
4834 	vf_stats->tx_bytes   = stats->tx_bytes;
4835 	vf_stats->broadcast  = stats->rx_broadcast;
4836 	vf_stats->multicast  = stats->rx_multicast;
4837 	vf_stats->rx_dropped = stats->rx_discards;
4838 	vf_stats->tx_dropped = stats->tx_discards;
4839 
4840 	return 0;
4841 }
4842