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