xref: /linux/drivers/net/ethernet/intel/ice/virt/queues.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2022, Intel Corporation. */
3 
4 #include "virtchnl.h"
5 #include "queues.h"
6 #include "ice_vf_lib_private.h"
7 #include "ice.h"
8 #include "ice_base.h"
9 #include "ice_lib.h"
10 
11 /**
12  * ice_vc_get_max_frame_size - get max frame size allowed for VF
13  * @vf: VF used to determine max frame size
14  *
15  * Max frame size is determined based on the current port's max frame size and
16  * whether a port VLAN is configured on this VF. The VF is not aware whether
17  * it's in a port VLAN so the PF needs to account for this in max frame size
18  * checks and sending the max frame size to the VF.
19  */
ice_vc_get_max_frame_size(struct ice_vf * vf)20 u16 ice_vc_get_max_frame_size(struct ice_vf *vf)
21 {
22 	struct ice_port_info *pi = ice_vf_get_port_info(vf);
23 	u16 max_frame_size;
24 
25 	max_frame_size = pi->phy.link_info.max_frame_size;
26 
27 	if (ice_vf_is_port_vlan_ena(vf))
28 		max_frame_size -= VLAN_HLEN;
29 
30 	return max_frame_size;
31 }
32 
33 /**
34  * ice_vc_isvalid_q_id
35  * @vsi: VSI to check queue ID against
36  * @qid: VSI relative queue ID
37  *
38  * check for the valid queue ID
39  */
ice_vc_isvalid_q_id(struct ice_vsi * vsi,u16 qid)40 static bool ice_vc_isvalid_q_id(struct ice_vsi *vsi, u16 qid)
41 {
42 	/* allocated Tx and Rx queues should be always equal for VF VSI */
43 	return qid < vsi->alloc_txq;
44 }
45 
46 /**
47  * ice_vc_isvalid_ring_len
48  * @ring_len: length of ring
49  *
50  * check for the valid ring count, should be multiple of ICE_REQ_DESC_MULTIPLE
51  * or zero
52  */
ice_vc_isvalid_ring_len(u16 ring_len)53 static bool ice_vc_isvalid_ring_len(u16 ring_len)
54 {
55 	return ring_len == 0 ||
56 	       (ring_len >= ICE_MIN_NUM_DESC &&
57 		ring_len <= ICE_MAX_NUM_DESC_E810 &&
58 		!(ring_len % ICE_REQ_DESC_MULTIPLE));
59 }
60 
61 /**
62  * ice_vf_cfg_qs_bw - Configure per queue bandwidth
63  * @vf: pointer to the VF info
64  * @num_queues: number of queues to be configured
65  *
66  * Configure per queue bandwidth.
67  *
68  * Return: 0 on success or negative error value.
69  */
ice_vf_cfg_qs_bw(struct ice_vf * vf,u16 num_queues)70 static int ice_vf_cfg_qs_bw(struct ice_vf *vf, u16 num_queues)
71 {
72 	struct ice_hw *hw = &vf->pf->hw;
73 	struct ice_vsi *vsi;
74 	int ret;
75 	u16 i;
76 
77 	vsi = ice_get_vf_vsi(vf);
78 	if (!vsi)
79 		return -EINVAL;
80 
81 	for (i = 0; i < num_queues; i++) {
82 		u32 p_rate, min_rate;
83 		u8 tc;
84 
85 		p_rate = vf->qs_bw[i].peak;
86 		min_rate = vf->qs_bw[i].committed;
87 		tc = vf->qs_bw[i].tc;
88 		if (p_rate)
89 			ret = ice_cfg_q_bw_lmt(hw->port_info, vsi->idx, tc,
90 					       vf->qs_bw[i].queue_id,
91 					       ICE_MAX_BW, p_rate);
92 		else
93 			ret = ice_cfg_q_bw_dflt_lmt(hw->port_info, vsi->idx, tc,
94 						    vf->qs_bw[i].queue_id,
95 						    ICE_MAX_BW);
96 		if (ret)
97 			return ret;
98 
99 		if (min_rate)
100 			ret = ice_cfg_q_bw_lmt(hw->port_info, vsi->idx, tc,
101 					       vf->qs_bw[i].queue_id,
102 					       ICE_MIN_BW, min_rate);
103 		else
104 			ret = ice_cfg_q_bw_dflt_lmt(hw->port_info, vsi->idx, tc,
105 						    vf->qs_bw[i].queue_id,
106 						    ICE_MIN_BW);
107 
108 		if (ret)
109 			return ret;
110 	}
111 
112 	return 0;
113 }
114 
115 /**
116  * ice_vf_cfg_q_quanta_profile - Configure quanta profile
117  * @vf: pointer to the VF info
118  * @quanta_prof_idx: pointer to the quanta profile index
119  * @quanta_size: quanta size to be set
120  *
121  * This function chooses available quanta profile and configures the register.
122  * The quanta profile is evenly divided by the number of device ports, and then
123  * available to the specific PF and VFs. The first profile for each PF is a
124  * reserved default profile. Only quanta size of the rest unused profile can be
125  * modified.
126  *
127  * Return: 0 on success or negative error value.
128  */
ice_vf_cfg_q_quanta_profile(struct ice_vf * vf,u16 quanta_size,u16 * quanta_prof_idx)129 static int ice_vf_cfg_q_quanta_profile(struct ice_vf *vf, u16 quanta_size,
130 				       u16 *quanta_prof_idx)
131 {
132 	const u16 n_desc = calc_quanta_desc(quanta_size);
133 	struct ice_hw *hw = &vf->pf->hw;
134 	const u16 n_cmd = 2 * n_desc;
135 	struct ice_pf *pf = vf->pf;
136 	u16 per_pf, begin_id;
137 	u8 n_used;
138 	u32 reg;
139 
140 	begin_id = (GLCOMM_QUANTA_PROF_MAX_INDEX + 1) / hw->dev_caps.num_funcs *
141 		   hw->logical_pf_id;
142 
143 	if (quanta_size == ICE_DFLT_QUANTA) {
144 		*quanta_prof_idx = begin_id;
145 	} else {
146 		per_pf = (GLCOMM_QUANTA_PROF_MAX_INDEX + 1) /
147 			 hw->dev_caps.num_funcs;
148 		n_used = pf->num_quanta_prof_used;
149 		if (n_used < per_pf) {
150 			*quanta_prof_idx = begin_id + 1 + n_used;
151 			pf->num_quanta_prof_used++;
152 		} else {
153 			return -EINVAL;
154 		}
155 	}
156 
157 	reg = FIELD_PREP(GLCOMM_QUANTA_PROF_QUANTA_SIZE_M, quanta_size) |
158 	      FIELD_PREP(GLCOMM_QUANTA_PROF_MAX_CMD_M, n_cmd) |
159 	      FIELD_PREP(GLCOMM_QUANTA_PROF_MAX_DESC_M, n_desc);
160 	wr32(hw, GLCOMM_QUANTA_PROF(*quanta_prof_idx), reg);
161 
162 	return 0;
163 }
164 
165 /**
166  * ice_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTCHNL
167  * @vqs: virtchnl_queue_select structure containing bitmaps to validate
168  *
169  * Return true on successful validation, else false
170  */
ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select * vqs)171 static bool ice_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
172 {
173 	if ((!vqs->rx_queues && !vqs->tx_queues) ||
174 	    vqs->rx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF) ||
175 	    vqs->tx_queues >= BIT(ICE_MAX_RSS_QS_PER_VF))
176 		return false;
177 
178 	return true;
179 }
180 
181 /**
182  * ice_vf_ena_txq_interrupt - enable Tx queue interrupt via QINT_TQCTL
183  * @vsi: VSI of the VF to configure
184  * @q_idx: VF queue index used to determine the queue in the PF's space
185  */
ice_vf_ena_txq_interrupt(struct ice_vsi * vsi,u32 q_idx)186 void ice_vf_ena_txq_interrupt(struct ice_vsi *vsi, u32 q_idx)
187 {
188 	struct ice_hw *hw = &vsi->back->hw;
189 	u32 pfq = vsi->txq_map[q_idx];
190 	u32 reg;
191 
192 	reg = rd32(hw, QINT_TQCTL(pfq));
193 
194 	/* MSI-X index 0 in the VF's space is always for the OICR, which means
195 	 * this is most likely a poll mode VF driver, so don't enable an
196 	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
197 	 */
198 	if (!(reg & QINT_TQCTL_MSIX_INDX_M))
199 		return;
200 
201 	wr32(hw, QINT_TQCTL(pfq), reg | QINT_TQCTL_CAUSE_ENA_M);
202 }
203 
204 /**
205  * ice_vf_ena_rxq_interrupt - enable Tx queue interrupt via QINT_RQCTL
206  * @vsi: VSI of the VF to configure
207  * @q_idx: VF queue index used to determine the queue in the PF's space
208  */
ice_vf_ena_rxq_interrupt(struct ice_vsi * vsi,u32 q_idx)209 void ice_vf_ena_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
210 {
211 	struct ice_hw *hw = &vsi->back->hw;
212 	u32 pfq = vsi->rxq_map[q_idx];
213 	u32 reg;
214 
215 	reg = rd32(hw, QINT_RQCTL(pfq));
216 
217 	/* MSI-X index 0 in the VF's space is always for the OICR, which means
218 	 * this is most likely a poll mode VF driver, so don't enable an
219 	 * interrupt that was never configured via VIRTCHNL_OP_CONFIG_IRQ_MAP
220 	 */
221 	if (!(reg & QINT_RQCTL_MSIX_INDX_M))
222 		return;
223 
224 	wr32(hw, QINT_RQCTL(pfq), reg | QINT_RQCTL_CAUSE_ENA_M);
225 }
226 
227 /**
228  * ice_vf_dis_rxq_interrupt - disable Rx queue interrupt via QINT_RQCTL
229  * @vsi: VSI of the VF to configure
230  * @q_idx: VF queue index used to determine the queue in the PF's space
231  */
ice_vf_dis_rxq_interrupt(struct ice_vsi * vsi,u32 q_idx)232 static void ice_vf_dis_rxq_interrupt(struct ice_vsi *vsi, u32 q_idx)
233 {
234 	struct ice_hw *hw = &vsi->back->hw;
235 	u32 pfq = vsi->rxq_map[q_idx];
236 	u32 reg;
237 
238 	reg = rd32(hw, QINT_RQCTL(pfq));
239 	reg &= ~QINT_RQCTL_CAUSE_ENA_M;
240 	wr32(hw, QINT_RQCTL(pfq), reg);
241 
242 	ice_flush(hw);
243 }
244 
245 /**
246  * ice_vc_ena_qs_msg
247  * @vf: pointer to the VF info
248  * @msg: pointer to the msg buffer
249  *
250  * called from the VF to enable all or specific queue(s)
251  */
ice_vc_ena_qs_msg(struct ice_vf * vf,u8 * msg)252 int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
253 {
254 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
255 	struct virtchnl_queue_select *vqs =
256 	    (struct virtchnl_queue_select *)msg;
257 	struct ice_vsi *vsi;
258 	unsigned long q_map;
259 	u16 vf_q_id;
260 
261 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
262 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
263 		goto error_param;
264 	}
265 
266 	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
267 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
268 		goto error_param;
269 	}
270 
271 	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
272 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
273 		goto error_param;
274 	}
275 
276 	vsi = ice_get_vf_vsi(vf);
277 	if (!vsi) {
278 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
279 		goto error_param;
280 	}
281 
282 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
283 	 * Tx queue group list was configured and the context bits were
284 	 * programmed using ice_vsi_cfg_txqs
285 	 */
286 	q_map = vqs->rx_queues;
287 	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
288 		if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
289 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
290 			goto error_param;
291 		}
292 
293 		/* Skip queue if enabled */
294 		if (test_bit(vf_q_id, vf->rxq_ena))
295 			continue;
296 
297 		if (ice_vsi_ctrl_one_rx_ring(vsi, true, vf_q_id, true)) {
298 			dev_err(ice_pf_to_dev(vsi->back), "Failed to enable Rx ring %d on VSI %d\n",
299 				vf_q_id, vsi->vsi_num);
300 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
301 			goto error_param;
302 		}
303 
304 		ice_vf_ena_rxq_interrupt(vsi, vf_q_id);
305 		set_bit(vf_q_id, vf->rxq_ena);
306 	}
307 
308 	q_map = vqs->tx_queues;
309 	for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
310 		if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
311 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
312 			goto error_param;
313 		}
314 
315 		/* Skip queue if enabled */
316 		if (test_bit(vf_q_id, vf->txq_ena))
317 			continue;
318 
319 		ice_vf_ena_txq_interrupt(vsi, vf_q_id);
320 		set_bit(vf_q_id, vf->txq_ena);
321 	}
322 
323 	/* Set flag to indicate that queues are enabled */
324 	if (v_ret == VIRTCHNL_STATUS_SUCCESS)
325 		set_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
326 
327 error_param:
328 	/* send the response to the VF */
329 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, v_ret,
330 				     NULL, 0);
331 }
332 
333 /**
334  * ice_vf_vsi_dis_single_txq - disable a single Tx queue
335  * @vf: VF to disable queue for
336  * @vsi: VSI for the VF
337  * @q_id: VF relative (0-based) queue ID
338  *
339  * Attempt to disable the Tx queue passed in. If the Tx queue was successfully
340  * disabled then clear q_id bit in the enabled queues bitmap and return
341  * success. Otherwise return error.
342  */
ice_vf_vsi_dis_single_txq(struct ice_vf * vf,struct ice_vsi * vsi,u16 q_id)343 int ice_vf_vsi_dis_single_txq(struct ice_vf *vf, struct ice_vsi *vsi, u16 q_id)
344 {
345 	struct ice_txq_meta txq_meta = { 0 };
346 	struct ice_tx_ring *ring;
347 	int err;
348 
349 	if (!test_bit(q_id, vf->txq_ena))
350 		dev_dbg(ice_pf_to_dev(vsi->back), "Queue %u on VSI %u is not enabled, but stopping it anyway\n",
351 			q_id, vsi->vsi_num);
352 
353 	ring = vsi->tx_rings[q_id];
354 	if (!ring)
355 		return -EINVAL;
356 
357 	ice_fill_txq_meta(vsi, ring, &txq_meta);
358 
359 	err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, vf->vf_id, ring, &txq_meta);
360 	if (err) {
361 		dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Tx ring %d on VSI %d\n",
362 			q_id, vsi->vsi_num);
363 		return err;
364 	}
365 
366 	/* Clear enabled queues flag */
367 	clear_bit(q_id, vf->txq_ena);
368 
369 	return 0;
370 }
371 
372 /**
373  * ice_vc_dis_qs_msg
374  * @vf: pointer to the VF info
375  * @msg: pointer to the msg buffer
376  *
377  * called from the VF to disable all or specific queue(s)
378  */
ice_vc_dis_qs_msg(struct ice_vf * vf,u8 * msg)379 int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
380 {
381 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
382 	struct virtchnl_queue_select *vqs =
383 	    (struct virtchnl_queue_select *)msg;
384 	struct ice_vsi *vsi;
385 	unsigned long q_map;
386 	u16 vf_q_id;
387 
388 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
389 	    !test_bit(ICE_VF_STATE_QS_ENA, vf->vf_states)) {
390 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
391 		goto error_param;
392 	}
393 
394 	if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
395 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
396 		goto error_param;
397 	}
398 
399 	if (!ice_vc_validate_vqs_bitmaps(vqs)) {
400 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
401 		goto error_param;
402 	}
403 
404 	vsi = ice_get_vf_vsi(vf);
405 	if (!vsi) {
406 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
407 		goto error_param;
408 	}
409 
410 	if (vqs->tx_queues) {
411 		q_map = vqs->tx_queues;
412 
413 		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
414 			if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
415 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
416 				goto error_param;
417 			}
418 
419 			if (ice_vf_vsi_dis_single_txq(vf, vsi, vf_q_id)) {
420 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
421 				goto error_param;
422 			}
423 		}
424 	}
425 
426 	q_map = vqs->rx_queues;
427 	/* speed up Rx queue disable by batching them if possible */
428 	if (q_map &&
429 	    bitmap_equal(&q_map, vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF)) {
430 		if (ice_vsi_stop_all_rx_rings(vsi)) {
431 			dev_err(ice_pf_to_dev(vsi->back), "Failed to stop all Rx rings on VSI %d\n",
432 				vsi->vsi_num);
433 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
434 			goto error_param;
435 		}
436 
437 		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF)
438 			ice_vf_dis_rxq_interrupt(vsi, vf_q_id);
439 		bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
440 	} else if (q_map) {
441 		for_each_set_bit(vf_q_id, &q_map, ICE_MAX_RSS_QS_PER_VF) {
442 			if (!ice_vc_isvalid_q_id(vsi, vf_q_id)) {
443 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
444 				goto error_param;
445 			}
446 
447 			/* Skip queue if not enabled */
448 			if (!test_bit(vf_q_id, vf->rxq_ena))
449 				continue;
450 
451 			if (ice_vsi_ctrl_one_rx_ring(vsi, false, vf_q_id,
452 						     true)) {
453 				dev_err(ice_pf_to_dev(vsi->back), "Failed to stop Rx ring %d on VSI %d\n",
454 					vf_q_id, vsi->vsi_num);
455 				v_ret = VIRTCHNL_STATUS_ERR_PARAM;
456 				goto error_param;
457 			}
458 
459 			ice_vf_dis_rxq_interrupt(vsi, vf_q_id);
460 			/* Clear enabled queues flag */
461 			clear_bit(vf_q_id, vf->rxq_ena);
462 		}
463 	}
464 
465 	/* Clear enabled queues flag */
466 	if (v_ret == VIRTCHNL_STATUS_SUCCESS && ice_vf_has_no_qs_ena(vf))
467 		clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
468 
469 error_param:
470 	/* send the response to the VF */
471 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, v_ret,
472 				     NULL, 0);
473 }
474 
475 /**
476  * ice_cfg_interrupt
477  * @vf: pointer to the VF info
478  * @vsi: the VSI being configured
479  * @map: vector map for mapping vectors to queues
480  * @q_vector: structure for interrupt vector
481  * configure the IRQ to queue map
482  */
483 static enum virtchnl_status_code
ice_cfg_interrupt(struct ice_vf * vf,struct ice_vsi * vsi,struct virtchnl_vector_map * map,struct ice_q_vector * q_vector)484 ice_cfg_interrupt(struct ice_vf *vf, struct ice_vsi *vsi,
485 		  struct virtchnl_vector_map *map,
486 		  struct ice_q_vector *q_vector)
487 {
488 	u16 vsi_q_id, vsi_q_id_idx;
489 	unsigned long qmap;
490 
491 	q_vector->num_ring_rx = 0;
492 	q_vector->num_ring_tx = 0;
493 
494 	qmap = map->rxq_map;
495 	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
496 		vsi_q_id = vsi_q_id_idx;
497 
498 		if (!ice_vc_isvalid_q_id(vsi, vsi_q_id))
499 			return VIRTCHNL_STATUS_ERR_PARAM;
500 
501 		q_vector->num_ring_rx++;
502 		q_vector->rx.itr_idx = map->rxitr_idx;
503 		vsi->rx_rings[vsi_q_id]->q_vector = q_vector;
504 		ice_cfg_rxq_interrupt(vsi, vsi_q_id,
505 				      q_vector->vf_reg_idx,
506 				      q_vector->rx.itr_idx);
507 	}
508 
509 	qmap = map->txq_map;
510 	for_each_set_bit(vsi_q_id_idx, &qmap, ICE_MAX_RSS_QS_PER_VF) {
511 		vsi_q_id = vsi_q_id_idx;
512 
513 		if (!ice_vc_isvalid_q_id(vsi, vsi_q_id))
514 			return VIRTCHNL_STATUS_ERR_PARAM;
515 
516 		q_vector->num_ring_tx++;
517 		q_vector->tx.itr_idx = map->txitr_idx;
518 		vsi->tx_rings[vsi_q_id]->q_vector = q_vector;
519 		ice_cfg_txq_interrupt(vsi, vsi_q_id,
520 				      q_vector->vf_reg_idx,
521 				      q_vector->tx.itr_idx);
522 	}
523 
524 	return VIRTCHNL_STATUS_SUCCESS;
525 }
526 
527 /**
528  * ice_vc_cfg_irq_map_msg
529  * @vf: pointer to the VF info
530  * @msg: pointer to the msg buffer
531  *
532  * called from the VF to configure the IRQ to queue map
533  */
ice_vc_cfg_irq_map_msg(struct ice_vf * vf,u8 * msg)534 int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
535 {
536 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
537 	u16 num_q_vectors_mapped, vsi_id, vector_id;
538 	struct virtchnl_irq_map_info *irqmap_info;
539 	struct virtchnl_vector_map *map;
540 	struct ice_vsi *vsi;
541 	int i;
542 
543 	irqmap_info = (struct virtchnl_irq_map_info *)msg;
544 	num_q_vectors_mapped = irqmap_info->num_vectors;
545 
546 	/* Check to make sure number of VF vectors mapped is not greater than
547 	 * number of VF vectors originally allocated, and check that
548 	 * there is actually at least a single VF queue vector mapped
549 	 */
550 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
551 	    vf->num_msix < num_q_vectors_mapped ||
552 	    !num_q_vectors_mapped) {
553 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
554 		goto error_param;
555 	}
556 
557 	vsi = ice_get_vf_vsi(vf);
558 	if (!vsi) {
559 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
560 		goto error_param;
561 	}
562 
563 	for (i = 0; i < num_q_vectors_mapped; i++) {
564 		struct ice_q_vector *q_vector;
565 
566 		map = &irqmap_info->vecmap[i];
567 
568 		vector_id = map->vector_id;
569 		vsi_id = map->vsi_id;
570 		/* vector_id is always 0-based for each VF, and can never be
571 		 * larger than or equal to the max allowed interrupts per VF
572 		 */
573 		if (!(vector_id < vf->num_msix) ||
574 		    !ice_vc_isvalid_vsi_id(vf, vsi_id) ||
575 		    (!vector_id && (map->rxq_map || map->txq_map))) {
576 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
577 			goto error_param;
578 		}
579 
580 		/* No need to map VF miscellaneous or rogue vector */
581 		if (!vector_id)
582 			continue;
583 
584 		/* Subtract non queue vector from vector_id passed by VF
585 		 * to get actual number of VSI queue vector array index
586 		 */
587 		q_vector = vsi->q_vectors[vector_id - ICE_NONQ_VECS_VF];
588 		if (!q_vector) {
589 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
590 			goto error_param;
591 		}
592 
593 		/* lookout for the invalid queue index */
594 		v_ret = ice_cfg_interrupt(vf, vsi, map, q_vector);
595 		if (v_ret)
596 			goto error_param;
597 	}
598 
599 error_param:
600 	/* send the response to the VF */
601 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, v_ret,
602 				     NULL, 0);
603 }
604 
605 /**
606  * ice_vc_cfg_q_bw - Configure per queue bandwidth
607  * @vf: pointer to the VF info
608  * @msg: pointer to the msg buffer which holds the command descriptor
609  *
610  * Configure VF queues bandwidth.
611  *
612  * Return: 0 on success or negative error value.
613  */
ice_vc_cfg_q_bw(struct ice_vf * vf,u8 * msg)614 int ice_vc_cfg_q_bw(struct ice_vf *vf, u8 *msg)
615 {
616 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
617 	struct virtchnl_queues_bw_cfg *qbw =
618 		(struct virtchnl_queues_bw_cfg *)msg;
619 	struct ice_vsi *vsi;
620 	u16 i;
621 
622 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
623 	    !ice_vc_isvalid_vsi_id(vf, qbw->vsi_id)) {
624 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
625 		goto err;
626 	}
627 
628 	vsi = ice_get_vf_vsi(vf);
629 	if (!vsi) {
630 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
631 		goto err;
632 	}
633 
634 	if (qbw->num_queues > ICE_MAX_RSS_QS_PER_VF ||
635 	    qbw->num_queues > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
636 		dev_err(ice_pf_to_dev(vf->pf), "VF-%d trying to configure more than allocated number of queues: %d\n",
637 			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
638 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
639 		goto err;
640 	}
641 
642 	for (i = 0; i < qbw->num_queues; i++) {
643 		if (qbw->cfg[i].shaper.peak != 0 && vf->max_tx_rate != 0 &&
644 		    qbw->cfg[i].shaper.peak > vf->max_tx_rate) {
645 			dev_warn(ice_pf_to_dev(vf->pf), "The maximum queue %d rate limit configuration may not take effect because the maximum TX rate for VF-%d is %d\n",
646 				 qbw->cfg[i].queue_id, vf->vf_id,
647 				 vf->max_tx_rate);
648 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
649 			goto err;
650 		}
651 		if (qbw->cfg[i].shaper.committed != 0 && vf->min_tx_rate != 0 &&
652 		    qbw->cfg[i].shaper.committed < vf->min_tx_rate) {
653 			dev_warn(ice_pf_to_dev(vf->pf), "The minimum queue %d rate limit configuration may not take effect because the minimum TX rate for VF-%d is %d\n",
654 				 qbw->cfg[i].queue_id, vf->vf_id,
655 				 vf->min_tx_rate);
656 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
657 			goto err;
658 		}
659 		if (qbw->cfg[i].queue_id > vf->num_vf_qs) {
660 			dev_warn(ice_pf_to_dev(vf->pf), "VF-%d trying to configure invalid queue_id\n",
661 				 vf->vf_id);
662 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
663 			goto err;
664 		}
665 		if (qbw->cfg[i].tc >= ICE_MAX_TRAFFIC_CLASS) {
666 			dev_warn(ice_pf_to_dev(vf->pf), "VF-%d trying to configure a traffic class higher than allowed\n",
667 				 vf->vf_id);
668 			v_ret = VIRTCHNL_STATUS_ERR_PARAM;
669 			goto err;
670 		}
671 	}
672 
673 	for (i = 0; i < qbw->num_queues; i++) {
674 		vf->qs_bw[i].queue_id = qbw->cfg[i].queue_id;
675 		vf->qs_bw[i].peak = qbw->cfg[i].shaper.peak;
676 		vf->qs_bw[i].committed = qbw->cfg[i].shaper.committed;
677 		vf->qs_bw[i].tc = qbw->cfg[i].tc;
678 	}
679 
680 	if (ice_vf_cfg_qs_bw(vf, qbw->num_queues))
681 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
682 
683 err:
684 	/* send the response to the VF */
685 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_QUEUE_BW,
686 				    v_ret, NULL, 0);
687 }
688 
689 /**
690  * ice_vc_cfg_q_quanta - Configure per queue quanta
691  * @vf: pointer to the VF info
692  * @msg: pointer to the msg buffer which holds the command descriptor
693  *
694  * Configure VF queues quanta.
695  *
696  * Return: 0 on success or negative error value.
697  */
ice_vc_cfg_q_quanta(struct ice_vf * vf,u8 * msg)698 int ice_vc_cfg_q_quanta(struct ice_vf *vf, u8 *msg)
699 {
700 	u16 quanta_prof_id, quanta_size, start_qid, num_queues, end_qid, i;
701 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
702 	struct virtchnl_quanta_cfg *qquanta =
703 		(struct virtchnl_quanta_cfg *)msg;
704 	struct ice_vsi *vsi;
705 	int ret;
706 
707 	start_qid = qquanta->queue_select.start_queue_id;
708 	num_queues = qquanta->queue_select.num_queues;
709 
710 	if (check_add_overflow(start_qid, num_queues, &end_qid)) {
711 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
712 		goto err;
713 	}
714 
715 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
716 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
717 		goto err;
718 	}
719 
720 	vsi = ice_get_vf_vsi(vf);
721 	if (!vsi) {
722 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
723 		goto err;
724 	}
725 
726 	if (end_qid > ICE_MAX_RSS_QS_PER_VF ||
727 	    end_qid > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
728 		dev_err(ice_pf_to_dev(vf->pf), "VF-%d trying to configure more than allocated number of queues: %d\n",
729 			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
730 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
731 		goto err;
732 	}
733 
734 	quanta_size = qquanta->quanta_size;
735 	if (quanta_size > ICE_MAX_QUANTA_SIZE ||
736 	    quanta_size < ICE_MIN_QUANTA_SIZE) {
737 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
738 		goto err;
739 	}
740 
741 	if (quanta_size % 64) {
742 		dev_err(ice_pf_to_dev(vf->pf), "quanta size should be the product of 64\n");
743 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
744 		goto err;
745 	}
746 
747 	ret = ice_vf_cfg_q_quanta_profile(vf, quanta_size,
748 					  &quanta_prof_id);
749 	if (ret) {
750 		v_ret = VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
751 		goto err;
752 	}
753 
754 	for (i = start_qid; i < end_qid; i++)
755 		vsi->tx_rings[i]->quanta_prof_id = quanta_prof_id;
756 
757 err:
758 	/* send the response to the VF */
759 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_QUANTA,
760 				     v_ret, NULL, 0);
761 }
762 
763 /**
764  * ice_vc_cfg_qs_msg
765  * @vf: pointer to the VF info
766  * @msg: pointer to the msg buffer
767  *
768  * called from the VF to configure the Rx/Tx queues
769  */
ice_vc_cfg_qs_msg(struct ice_vf * vf,u8 * msg)770 int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
771 {
772 	struct virtchnl_vsi_queue_config_info *qci =
773 	    (struct virtchnl_vsi_queue_config_info *)msg;
774 	struct virtchnl_queue_pair_info *qpi;
775 	struct ice_pf *pf = vf->pf;
776 	struct ice_vsi *vsi;
777 	int i = -1, q_idx;
778 	bool ena_ts;
779 	u8 act_prt;
780 
781 	mutex_lock(&pf->lag_mutex);
782 	act_prt = ice_lag_prepare_vf_reset(pf->lag);
783 
784 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
785 		goto error_param;
786 
787 	if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id))
788 		goto error_param;
789 
790 	vsi = ice_get_vf_vsi(vf);
791 	if (!vsi)
792 		goto error_param;
793 
794 	if (qci->num_queue_pairs > ICE_MAX_RSS_QS_PER_VF ||
795 	    qci->num_queue_pairs > min_t(u16, vsi->alloc_txq, vsi->alloc_rxq)) {
796 		dev_err(ice_pf_to_dev(pf), "VF-%d requesting more than supported number of queues: %d\n",
797 			vf->vf_id, min_t(u16, vsi->alloc_txq, vsi->alloc_rxq));
798 		goto error_param;
799 	}
800 
801 	for (i = 0; i < qci->num_queue_pairs; i++) {
802 		if (!qci->qpair[i].rxq.crc_disable)
803 			continue;
804 
805 		if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_CRC) ||
806 		    vf->vlan_strip_ena)
807 			goto error_param;
808 	}
809 
810 	for (i = 0; i < qci->num_queue_pairs; i++) {
811 		qpi = &qci->qpair[i];
812 		if (qpi->txq.vsi_id != qci->vsi_id ||
813 		    qpi->rxq.vsi_id != qci->vsi_id ||
814 		    qpi->rxq.queue_id != qpi->txq.queue_id ||
815 		    qpi->txq.headwb_enabled ||
816 		    !ice_vc_isvalid_ring_len(qpi->txq.ring_len) ||
817 		    !ice_vc_isvalid_ring_len(qpi->rxq.ring_len) ||
818 		    !ice_vc_isvalid_q_id(vsi, qpi->txq.queue_id)) {
819 			goto error_param;
820 		}
821 
822 		q_idx = qpi->rxq.queue_id;
823 
824 		/* make sure selected "q_idx" is in valid range of queues
825 		 * for selected "vsi"
826 		 */
827 		if (q_idx >= vsi->alloc_txq || q_idx >= vsi->alloc_rxq) {
828 			goto error_param;
829 		}
830 
831 		/* copy Tx queue info from VF into VSI */
832 		if (qpi->txq.ring_len > 0) {
833 			vsi->tx_rings[q_idx]->dma = qpi->txq.dma_ring_addr;
834 			vsi->tx_rings[q_idx]->count = qpi->txq.ring_len;
835 
836 			/* Disable any existing queue first */
837 			if (ice_vf_vsi_dis_single_txq(vf, vsi, q_idx))
838 				goto error_param;
839 
840 			/* Configure a queue with the requested settings */
841 			if (ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx)) {
842 				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure TX queue %d\n",
843 					 vf->vf_id, q_idx);
844 				goto error_param;
845 			}
846 		}
847 
848 		/* copy Rx queue info from VF into VSI */
849 		if (qpi->rxq.ring_len > 0) {
850 			u16 max_frame_size = ice_vc_get_max_frame_size(vf);
851 			struct ice_rx_ring *ring = vsi->rx_rings[q_idx];
852 			u32 rxdid;
853 
854 			ring->dma = qpi->rxq.dma_ring_addr;
855 			ring->count = qpi->rxq.ring_len;
856 
857 			if (qpi->rxq.crc_disable)
858 				ring->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS;
859 			else
860 				ring->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS;
861 
862 			if (qpi->rxq.databuffer_size != 0 &&
863 			    (qpi->rxq.databuffer_size > ((16 * 1024) - 128) ||
864 			     qpi->rxq.databuffer_size < 128))
865 				goto error_param;
866 
867 			ring->rx_buf_len = qpi->rxq.databuffer_size;
868 
869 			if (qpi->rxq.max_pkt_size > max_frame_size ||
870 			    qpi->rxq.max_pkt_size < 64)
871 				goto error_param;
872 
873 			vsi->max_frame = qpi->rxq.max_pkt_size;
874 			/* add space for the port VLAN since the VF driver is
875 			 * not expected to account for it in the MTU
876 			 * calculation
877 			 */
878 			if (ice_vf_is_port_vlan_ena(vf))
879 				vsi->max_frame += VLAN_HLEN;
880 
881 			if (ice_vsi_cfg_single_rxq(vsi, q_idx)) {
882 				dev_warn(ice_pf_to_dev(pf), "VF-%d failed to configure RX queue %d\n",
883 					 vf->vf_id, q_idx);
884 				goto error_param;
885 			}
886 
887 			/* If Rx flex desc is supported, select RXDID for Rx
888 			 * queues. Otherwise, use legacy 32byte descriptor
889 			 * format. Legacy 16byte descriptor is not supported.
890 			 * If this RXDID is selected, return error.
891 			 */
892 			if (vf->driver_caps &
893 			    VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
894 				rxdid = qpi->rxq.rxdid;
895 				if (!(BIT(rxdid) & pf->supported_rxdids))
896 					goto error_param;
897 			} else {
898 				rxdid = ICE_RXDID_LEGACY_1;
899 			}
900 
901 			ena_ts = ((vf->driver_caps &
902 				  VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) &&
903 				  (vf->driver_caps & VIRTCHNL_VF_CAP_PTP) &&
904 				  (qpi->rxq.flags & VIRTCHNL_PTP_RX_TSTAMP));
905 
906 			ice_write_qrxflxp_cntxt(&vsi->back->hw,
907 						vsi->rxq_map[q_idx], rxdid,
908 						ICE_RXDID_PRIO, ena_ts);
909 		}
910 	}
911 
912 	ice_lag_complete_vf_reset(pf->lag, act_prt);
913 	mutex_unlock(&pf->lag_mutex);
914 
915 	/* send the response to the VF */
916 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
917 				     VIRTCHNL_STATUS_SUCCESS, NULL, 0);
918 error_param:
919 	/* disable whatever we can */
920 	for (; i >= 0; i--) {
921 		if (ice_vsi_ctrl_one_rx_ring(vsi, false, i, true))
922 			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable RX queue %d\n",
923 				vf->vf_id, i);
924 		if (ice_vf_vsi_dis_single_txq(vf, vsi, i))
925 			dev_err(ice_pf_to_dev(pf), "VF-%d could not disable TX queue %d\n",
926 				vf->vf_id, i);
927 	}
928 
929 	ice_lag_complete_vf_reset(pf->lag, act_prt);
930 	mutex_unlock(&pf->lag_mutex);
931 
932 	/* send the response to the VF */
933 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
934 				     VIRTCHNL_STATUS_ERR_PARAM, NULL, 0);
935 }
936 
937 /**
938  * ice_vc_request_qs_msg
939  * @vf: pointer to the VF info
940  * @msg: pointer to the msg buffer
941  *
942  * VFs get a default number of queues but can use this message to request a
943  * different number. If the request is successful, PF will reset the VF and
944  * return 0. If unsuccessful, PF will send message informing VF of number of
945  * available queue pairs via virtchnl message response to VF.
946  */
ice_vc_request_qs_msg(struct ice_vf * vf,u8 * msg)947 int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
948 {
949 	enum virtchnl_status_code v_ret = VIRTCHNL_STATUS_SUCCESS;
950 	struct virtchnl_vf_res_request *vfres =
951 		(struct virtchnl_vf_res_request *)msg;
952 	u16 req_queues = vfres->num_queue_pairs;
953 	struct ice_pf *pf = vf->pf;
954 	u16 max_allowed_vf_queues;
955 	u16 tx_rx_queue_left;
956 	struct device *dev;
957 	u16 cur_queues;
958 
959 	dev = ice_pf_to_dev(pf);
960 	if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
961 		v_ret = VIRTCHNL_STATUS_ERR_PARAM;
962 		goto error_param;
963 	}
964 
965 	cur_queues = vf->num_vf_qs;
966 	tx_rx_queue_left = min_t(u16, ice_get_avail_txq_count(pf),
967 				 ice_get_avail_rxq_count(pf));
968 	max_allowed_vf_queues = tx_rx_queue_left + cur_queues;
969 	if (!req_queues) {
970 		dev_err(dev, "VF %d tried to request 0 queues. Ignoring.\n",
971 			vf->vf_id);
972 	} else if (req_queues > ICE_MAX_RSS_QS_PER_VF) {
973 		dev_err(dev, "VF %d tried to request more than %d queues.\n",
974 			vf->vf_id, ICE_MAX_RSS_QS_PER_VF);
975 		vfres->num_queue_pairs = ICE_MAX_RSS_QS_PER_VF;
976 	} else if (req_queues > cur_queues &&
977 		   req_queues - cur_queues > tx_rx_queue_left) {
978 		dev_warn(dev, "VF %d requested %u more queues, but only %u left.\n",
979 			 vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
980 		vfres->num_queue_pairs = min_t(u16, max_allowed_vf_queues,
981 					       ICE_MAX_RSS_QS_PER_VF);
982 	} else {
983 		/* request is successful, then reset VF */
984 		vf->num_req_qs = req_queues;
985 		ice_reset_vf(vf, ICE_VF_RESET_NOTIFY);
986 		dev_info(dev, "VF %d granted request of %u queues.\n",
987 			 vf->vf_id, req_queues);
988 		return 0;
989 	}
990 
991 error_param:
992 	/* send the response to the VF */
993 	return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
994 				     v_ret, (u8 *)vfres, sizeof(*vfres));
995 }
996 
997