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