1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
3 
4 #include <net/xdp_sock_drv.h>
5 #include "ice_base.h"
6 #include "ice_lib.h"
7 #include "ice_dcb_lib.h"
8 #include "ice_sriov.h"
9 
10 static bool ice_alloc_rx_buf_zc(struct ice_rx_ring *rx_ring)
11 {
12 	rx_ring->xdp_buf = kcalloc(rx_ring->count, sizeof(*rx_ring->xdp_buf), GFP_KERNEL);
13 	return !!rx_ring->xdp_buf;
14 }
15 
16 static bool ice_alloc_rx_buf(struct ice_rx_ring *rx_ring)
17 {
18 	rx_ring->rx_buf = kcalloc(rx_ring->count, sizeof(*rx_ring->rx_buf), GFP_KERNEL);
19 	return !!rx_ring->rx_buf;
20 }
21 
22 /**
23  * __ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
24  * @qs_cfg: gathered variables needed for PF->VSI queues assignment
25  *
26  * Return 0 on success and -ENOMEM in case of no left space in PF queue bitmap
27  */
28 static int __ice_vsi_get_qs_contig(struct ice_qs_cfg *qs_cfg)
29 {
30 	unsigned int offset, i;
31 
32 	mutex_lock(qs_cfg->qs_mutex);
33 	offset = bitmap_find_next_zero_area(qs_cfg->pf_map, qs_cfg->pf_map_size,
34 					    0, qs_cfg->q_count, 0);
35 	if (offset >= qs_cfg->pf_map_size) {
36 		mutex_unlock(qs_cfg->qs_mutex);
37 		return -ENOMEM;
38 	}
39 
40 	bitmap_set(qs_cfg->pf_map, offset, qs_cfg->q_count);
41 	for (i = 0; i < qs_cfg->q_count; i++)
42 		qs_cfg->vsi_map[i + qs_cfg->vsi_map_offset] = (u16)(i + offset);
43 	mutex_unlock(qs_cfg->qs_mutex);
44 
45 	return 0;
46 }
47 
48 /**
49  * __ice_vsi_get_qs_sc - Assign a scattered queues from PF to VSI
50  * @qs_cfg: gathered variables needed for pf->vsi queues assignment
51  *
52  * Return 0 on success and -ENOMEM in case of no left space in PF queue bitmap
53  */
54 static int __ice_vsi_get_qs_sc(struct ice_qs_cfg *qs_cfg)
55 {
56 	unsigned int i, index = 0;
57 
58 	mutex_lock(qs_cfg->qs_mutex);
59 	for (i = 0; i < qs_cfg->q_count; i++) {
60 		index = find_next_zero_bit(qs_cfg->pf_map,
61 					   qs_cfg->pf_map_size, index);
62 		if (index >= qs_cfg->pf_map_size)
63 			goto err_scatter;
64 		set_bit(index, qs_cfg->pf_map);
65 		qs_cfg->vsi_map[i + qs_cfg->vsi_map_offset] = (u16)index;
66 	}
67 	mutex_unlock(qs_cfg->qs_mutex);
68 
69 	return 0;
70 err_scatter:
71 	for (index = 0; index < i; index++) {
72 		clear_bit(qs_cfg->vsi_map[index], qs_cfg->pf_map);
73 		qs_cfg->vsi_map[index + qs_cfg->vsi_map_offset] = 0;
74 	}
75 	mutex_unlock(qs_cfg->qs_mutex);
76 
77 	return -ENOMEM;
78 }
79 
80 /**
81  * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
82  * @pf: the PF being configured
83  * @pf_q: the PF queue
84  * @ena: enable or disable state of the queue
85  *
86  * This routine will wait for the given Rx queue of the PF to reach the
87  * enabled or disabled state.
88  * Returns -ETIMEDOUT in case of failing to reach the requested state after
89  * multiple retries; else will return 0 in case of success.
90  */
91 static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
92 {
93 	int i;
94 
95 	for (i = 0; i < ICE_Q_WAIT_MAX_RETRY; i++) {
96 		if (ena == !!(rd32(&pf->hw, QRX_CTRL(pf_q)) &
97 			      QRX_CTRL_QENA_STAT_M))
98 			return 0;
99 
100 		usleep_range(20, 40);
101 	}
102 
103 	return -ETIMEDOUT;
104 }
105 
106 /**
107  * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
108  * @vsi: the VSI being configured
109  * @v_idx: index of the vector in the VSI struct
110  *
111  * We allocate one q_vector and set default value for ITR setting associated
112  * with this q_vector. If allocation fails we return -ENOMEM.
113  */
114 static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, u16 v_idx)
115 {
116 	struct ice_pf *pf = vsi->back;
117 	struct ice_q_vector *q_vector;
118 
119 	/* allocate q_vector */
120 	q_vector = devm_kzalloc(ice_pf_to_dev(pf), sizeof(*q_vector),
121 				GFP_KERNEL);
122 	if (!q_vector)
123 		return -ENOMEM;
124 
125 	q_vector->vsi = vsi;
126 	q_vector->v_idx = v_idx;
127 	q_vector->tx.itr_setting = ICE_DFLT_TX_ITR;
128 	q_vector->rx.itr_setting = ICE_DFLT_RX_ITR;
129 	q_vector->tx.itr_mode = ITR_DYNAMIC;
130 	q_vector->rx.itr_mode = ITR_DYNAMIC;
131 	q_vector->tx.type = ICE_TX_CONTAINER;
132 	q_vector->rx.type = ICE_RX_CONTAINER;
133 
134 	if (vsi->type == ICE_VSI_VF)
135 		goto out;
136 	/* only set affinity_mask if the CPU is online */
137 	if (cpu_online(v_idx))
138 		cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
139 
140 	/* This will not be called in the driver load path because the netdev
141 	 * will not be created yet. All other cases with register the NAPI
142 	 * handler here (i.e. resume, reset/rebuild, etc.)
143 	 */
144 	if (vsi->netdev)
145 		netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
146 			       NAPI_POLL_WEIGHT);
147 
148 out:
149 	/* tie q_vector and VSI together */
150 	vsi->q_vectors[v_idx] = q_vector;
151 
152 	return 0;
153 }
154 
155 /**
156  * ice_free_q_vector - Free memory allocated for a specific interrupt vector
157  * @vsi: VSI having the memory freed
158  * @v_idx: index of the vector to be freed
159  */
160 static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
161 {
162 	struct ice_q_vector *q_vector;
163 	struct ice_pf *pf = vsi->back;
164 	struct ice_tx_ring *tx_ring;
165 	struct ice_rx_ring *rx_ring;
166 	struct device *dev;
167 
168 	dev = ice_pf_to_dev(pf);
169 	if (!vsi->q_vectors[v_idx]) {
170 		dev_dbg(dev, "Queue vector at index %d not found\n", v_idx);
171 		return;
172 	}
173 	q_vector = vsi->q_vectors[v_idx];
174 
175 	ice_for_each_tx_ring(tx_ring, q_vector->tx)
176 		tx_ring->q_vector = NULL;
177 	ice_for_each_rx_ring(rx_ring, q_vector->rx)
178 		rx_ring->q_vector = NULL;
179 
180 	/* only VSI with an associated netdev is set up with NAPI */
181 	if (vsi->netdev)
182 		netif_napi_del(&q_vector->napi);
183 
184 	devm_kfree(dev, q_vector);
185 	vsi->q_vectors[v_idx] = NULL;
186 }
187 
188 /**
189  * ice_cfg_itr_gran - set the ITR granularity to 2 usecs if not already set
190  * @hw: board specific structure
191  */
192 static void ice_cfg_itr_gran(struct ice_hw *hw)
193 {
194 	u32 regval = rd32(hw, GLINT_CTL);
195 
196 	/* no need to update global register if ITR gran is already set */
197 	if (!(regval & GLINT_CTL_DIS_AUTOMASK_M) &&
198 	    (((regval & GLINT_CTL_ITR_GRAN_200_M) >>
199 	     GLINT_CTL_ITR_GRAN_200_S) == ICE_ITR_GRAN_US) &&
200 	    (((regval & GLINT_CTL_ITR_GRAN_100_M) >>
201 	     GLINT_CTL_ITR_GRAN_100_S) == ICE_ITR_GRAN_US) &&
202 	    (((regval & GLINT_CTL_ITR_GRAN_50_M) >>
203 	     GLINT_CTL_ITR_GRAN_50_S) == ICE_ITR_GRAN_US) &&
204 	    (((regval & GLINT_CTL_ITR_GRAN_25_M) >>
205 	      GLINT_CTL_ITR_GRAN_25_S) == ICE_ITR_GRAN_US))
206 		return;
207 
208 	regval = ((ICE_ITR_GRAN_US << GLINT_CTL_ITR_GRAN_200_S) &
209 		  GLINT_CTL_ITR_GRAN_200_M) |
210 		 ((ICE_ITR_GRAN_US << GLINT_CTL_ITR_GRAN_100_S) &
211 		  GLINT_CTL_ITR_GRAN_100_M) |
212 		 ((ICE_ITR_GRAN_US << GLINT_CTL_ITR_GRAN_50_S) &
213 		  GLINT_CTL_ITR_GRAN_50_M) |
214 		 ((ICE_ITR_GRAN_US << GLINT_CTL_ITR_GRAN_25_S) &
215 		  GLINT_CTL_ITR_GRAN_25_M);
216 	wr32(hw, GLINT_CTL, regval);
217 }
218 
219 /**
220  * ice_calc_txq_handle - calculate the queue handle
221  * @vsi: VSI that ring belongs to
222  * @ring: ring to get the absolute queue index
223  * @tc: traffic class number
224  */
225 static u16 ice_calc_txq_handle(struct ice_vsi *vsi, struct ice_tx_ring *ring, u8 tc)
226 {
227 	WARN_ONCE(ice_ring_is_xdp(ring) && tc, "XDP ring can't belong to TC other than 0\n");
228 
229 	if (ring->ch)
230 		return ring->q_index - ring->ch->base_q;
231 
232 	/* Idea here for calculation is that we subtract the number of queue
233 	 * count from TC that ring belongs to from it's absolute queue index
234 	 * and as a result we get the queue's index within TC.
235 	 */
236 	return ring->q_index - vsi->tc_cfg.tc_info[tc].qoffset;
237 }
238 
239 /**
240  * ice_eswitch_calc_txq_handle
241  * @ring: pointer to ring which unique index is needed
242  *
243  * To correctly work with many netdevs ring->q_index of Tx rings on switchdev
244  * VSI can repeat. Hardware ring setup requires unique q_index. Calculate it
245  * here by finding index in vsi->tx_rings of this ring.
246  *
247  * Return ICE_INVAL_Q_INDEX when index wasn't found. Should never happen,
248  * because VSI is get from ring->vsi, so it has to be present in this VSI.
249  */
250 static u16 ice_eswitch_calc_txq_handle(struct ice_tx_ring *ring)
251 {
252 	struct ice_vsi *vsi = ring->vsi;
253 	int i;
254 
255 	ice_for_each_txq(vsi, i) {
256 		if (vsi->tx_rings[i] == ring)
257 			return i;
258 	}
259 
260 	return ICE_INVAL_Q_INDEX;
261 }
262 
263 /**
264  * ice_cfg_xps_tx_ring - Configure XPS for a Tx ring
265  * @ring: The Tx ring to configure
266  *
267  * This enables/disables XPS for a given Tx descriptor ring
268  * based on the TCs enabled for the VSI that ring belongs to.
269  */
270 static void ice_cfg_xps_tx_ring(struct ice_tx_ring *ring)
271 {
272 	if (!ring->q_vector || !ring->netdev)
273 		return;
274 
275 	/* We only initialize XPS once, so as not to overwrite user settings */
276 	if (test_and_set_bit(ICE_TX_XPS_INIT_DONE, ring->xps_state))
277 		return;
278 
279 	netif_set_xps_queue(ring->netdev, &ring->q_vector->affinity_mask,
280 			    ring->q_index);
281 }
282 
283 /**
284  * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
285  * @ring: The Tx ring to configure
286  * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
287  * @pf_q: queue index in the PF space
288  *
289  * Configure the Tx descriptor ring in TLAN context.
290  */
291 static void
292 ice_setup_tx_ctx(struct ice_tx_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
293 {
294 	struct ice_vsi *vsi = ring->vsi;
295 	struct ice_hw *hw = &vsi->back->hw;
296 
297 	tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
298 
299 	tlan_ctx->port_num = vsi->port_info->lport;
300 
301 	/* Transmit Queue Length */
302 	tlan_ctx->qlen = ring->count;
303 
304 	ice_set_cgd_num(tlan_ctx, ring->dcb_tc);
305 
306 	/* PF number */
307 	tlan_ctx->pf_num = hw->pf_id;
308 
309 	/* queue belongs to a specific VSI type
310 	 * VF / VM index should be programmed per vmvf_type setting:
311 	 * for vmvf_type = VF, it is VF number between 0-256
312 	 * for vmvf_type = VM, it is VM number between 0-767
313 	 * for PF or EMP this field should be set to zero
314 	 */
315 	switch (vsi->type) {
316 	case ICE_VSI_LB:
317 	case ICE_VSI_CTRL:
318 	case ICE_VSI_PF:
319 		if (ring->ch)
320 			tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_VMQ;
321 		else
322 			tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
323 		break;
324 	case ICE_VSI_VF:
325 		/* Firmware expects vmvf_num to be absolute VF ID */
326 		tlan_ctx->vmvf_num = hw->func_caps.vf_base_id + vsi->vf->vf_id;
327 		tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_VF;
328 		break;
329 	case ICE_VSI_SWITCHDEV_CTRL:
330 		tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_VMQ;
331 		break;
332 	default:
333 		return;
334 	}
335 
336 	/* make sure the context is associated with the right VSI */
337 	if (ring->ch)
338 		tlan_ctx->src_vsi = ring->ch->vsi_num;
339 	else
340 		tlan_ctx->src_vsi = ice_get_hw_vsi_num(hw, vsi->idx);
341 
342 	/* Restrict Tx timestamps to the PF VSI */
343 	switch (vsi->type) {
344 	case ICE_VSI_PF:
345 		tlan_ctx->tsyn_ena = 1;
346 		break;
347 	default:
348 		break;
349 	}
350 
351 	tlan_ctx->tso_ena = ICE_TX_LEGACY;
352 	tlan_ctx->tso_qnum = pf_q;
353 
354 	/* Legacy or Advanced Host Interface:
355 	 * 0: Advanced Host Interface
356 	 * 1: Legacy Host Interface
357 	 */
358 	tlan_ctx->legacy_int = ICE_TX_LEGACY;
359 }
360 
361 /**
362  * ice_rx_offset - Return expected offset into page to access data
363  * @rx_ring: Ring we are requesting offset of
364  *
365  * Returns the offset value for ring into the data buffer.
366  */
367 static unsigned int ice_rx_offset(struct ice_rx_ring *rx_ring)
368 {
369 	if (ice_ring_uses_build_skb(rx_ring))
370 		return ICE_SKB_PAD;
371 	else if (ice_is_xdp_ena_vsi(rx_ring->vsi))
372 		return XDP_PACKET_HEADROOM;
373 
374 	return 0;
375 }
376 
377 /**
378  * ice_setup_rx_ctx - Configure a receive ring context
379  * @ring: The Rx ring to configure
380  *
381  * Configure the Rx descriptor ring in RLAN context.
382  */
383 static int ice_setup_rx_ctx(struct ice_rx_ring *ring)
384 {
385 	int chain_len = ICE_MAX_CHAINED_RX_BUFS;
386 	struct ice_vsi *vsi = ring->vsi;
387 	u32 rxdid = ICE_RXDID_FLEX_NIC;
388 	struct ice_rlan_ctx rlan_ctx;
389 	struct ice_hw *hw;
390 	u16 pf_q;
391 	int err;
392 
393 	hw = &vsi->back->hw;
394 
395 	/* what is Rx queue number in global space of 2K Rx queues */
396 	pf_q = vsi->rxq_map[ring->q_index];
397 
398 	/* clear the context structure first */
399 	memset(&rlan_ctx, 0, sizeof(rlan_ctx));
400 
401 	/* Receive Queue Base Address.
402 	 * Indicates the starting address of the descriptor queue defined in
403 	 * 128 Byte units.
404 	 */
405 	rlan_ctx.base = ring->dma >> 7;
406 
407 	rlan_ctx.qlen = ring->count;
408 
409 	/* Receive Packet Data Buffer Size.
410 	 * The Packet Data Buffer Size is defined in 128 byte units.
411 	 */
412 	rlan_ctx.dbuf = ring->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
413 
414 	/* use 32 byte descriptors */
415 	rlan_ctx.dsize = 1;
416 
417 	/* Strip the Ethernet CRC bytes before the packet is posted to host
418 	 * memory.
419 	 */
420 	rlan_ctx.crcstrip = 1;
421 
422 	/* L2TSEL flag defines the reported L2 Tags in the receive descriptor
423 	 * and it needs to remain 1 for non-DVM capable configurations to not
424 	 * break backward compatibility for VF drivers. Setting this field to 0
425 	 * will cause the single/outer VLAN tag to be stripped to the L2TAG2_2ND
426 	 * field in the Rx descriptor. Setting it to 1 allows the VLAN tag to
427 	 * be stripped in L2TAG1 of the Rx descriptor, which is where VFs will
428 	 * check for the tag
429 	 */
430 	if (ice_is_dvm_ena(hw))
431 		if (vsi->type == ICE_VSI_VF &&
432 		    ice_vf_is_port_vlan_ena(vsi->vf))
433 			rlan_ctx.l2tsel = 1;
434 		else
435 			rlan_ctx.l2tsel = 0;
436 	else
437 		rlan_ctx.l2tsel = 1;
438 
439 	rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
440 	rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
441 	rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
442 
443 	/* This controls whether VLAN is stripped from inner headers
444 	 * The VLAN in the inner L2 header is stripped to the receive
445 	 * descriptor if enabled by this flag.
446 	 */
447 	rlan_ctx.showiv = 0;
448 
449 	/* For AF_XDP ZC, we disallow packets to span on
450 	 * multiple buffers, thus letting us skip that
451 	 * handling in the fast-path.
452 	 */
453 	if (ring->xsk_pool)
454 		chain_len = 1;
455 	/* Max packet size for this queue - must not be set to a larger value
456 	 * than 5 x DBUF
457 	 */
458 	rlan_ctx.rxmax = min_t(u32, vsi->max_frame,
459 			       chain_len * ring->rx_buf_len);
460 
461 	/* Rx queue threshold in units of 64 */
462 	rlan_ctx.lrxqthresh = 1;
463 
464 	/* Enable Flexible Descriptors in the queue context which
465 	 * allows this driver to select a specific receive descriptor format
466 	 * increasing context priority to pick up profile ID; default is 0x01;
467 	 * setting to 0x03 to ensure profile is programming if prev context is
468 	 * of same priority
469 	 */
470 	if (vsi->type != ICE_VSI_VF)
471 		ice_write_qrxflxp_cntxt(hw, pf_q, rxdid, 0x3, true);
472 	else
473 		ice_write_qrxflxp_cntxt(hw, pf_q, ICE_RXDID_LEGACY_1, 0x3,
474 					false);
475 
476 	/* Absolute queue number out of 2K needs to be passed */
477 	err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
478 	if (err) {
479 		dev_err(ice_pf_to_dev(vsi->back), "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
480 			pf_q, err);
481 		return -EIO;
482 	}
483 
484 	if (vsi->type == ICE_VSI_VF)
485 		return 0;
486 
487 	/* configure Rx buffer alignment */
488 	if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
489 		ice_clear_ring_build_skb_ena(ring);
490 	else
491 		ice_set_ring_build_skb_ena(ring);
492 
493 	ring->rx_offset = ice_rx_offset(ring);
494 
495 	/* init queue specific tail register */
496 	ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
497 	writel(0, ring->tail);
498 
499 	return 0;
500 }
501 
502 /**
503  * ice_vsi_cfg_rxq - Configure an Rx queue
504  * @ring: the ring being configured
505  *
506  * Return 0 on success and a negative value on error.
507  */
508 int ice_vsi_cfg_rxq(struct ice_rx_ring *ring)
509 {
510 	struct device *dev = ice_pf_to_dev(ring->vsi->back);
511 	u16 num_bufs = ICE_DESC_UNUSED(ring);
512 	int err;
513 
514 	ring->rx_buf_len = ring->vsi->rx_buf_len;
515 
516 	if (ring->vsi->type == ICE_VSI_PF) {
517 		if (!xdp_rxq_info_is_reg(&ring->xdp_rxq))
518 			/* coverity[check_return] */
519 			xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
520 					 ring->q_index, ring->q_vector->napi.napi_id);
521 
522 		kfree(ring->rx_buf);
523 		ring->xsk_pool = ice_xsk_pool(ring);
524 		if (ring->xsk_pool) {
525 			if (!ice_alloc_rx_buf_zc(ring))
526 				return -ENOMEM;
527 			xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
528 
529 			ring->rx_buf_len =
530 				xsk_pool_get_rx_frame_size(ring->xsk_pool);
531 			err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
532 							 MEM_TYPE_XSK_BUFF_POOL,
533 							 NULL);
534 			if (err)
535 				return err;
536 			xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
537 
538 			dev_info(dev, "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
539 				 ring->q_index);
540 		} else {
541 			if (!ice_alloc_rx_buf(ring))
542 				return -ENOMEM;
543 			if (!xdp_rxq_info_is_reg(&ring->xdp_rxq))
544 				/* coverity[check_return] */
545 				xdp_rxq_info_reg(&ring->xdp_rxq,
546 						 ring->netdev,
547 						 ring->q_index, ring->q_vector->napi.napi_id);
548 
549 			err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
550 							 MEM_TYPE_PAGE_SHARED,
551 							 NULL);
552 			if (err)
553 				return err;
554 		}
555 	}
556 
557 	err = ice_setup_rx_ctx(ring);
558 	if (err) {
559 		dev_err(dev, "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
560 			ring->q_index, err);
561 		return err;
562 	}
563 
564 	if (ring->xsk_pool) {
565 		bool ok;
566 
567 		if (!xsk_buff_can_alloc(ring->xsk_pool, num_bufs)) {
568 			dev_warn(dev, "XSK buffer pool does not provide enough addresses to fill %d buffers on Rx ring %d\n",
569 				 num_bufs, ring->q_index);
570 			dev_warn(dev, "Change Rx ring/fill queue size to avoid performance issues\n");
571 
572 			return 0;
573 		}
574 
575 		ok = ice_alloc_rx_bufs_zc(ring, num_bufs);
576 		if (!ok) {
577 			u16 pf_q = ring->vsi->rxq_map[ring->q_index];
578 
579 			dev_info(dev, "Failed to allocate some buffers on XSK buffer pool enabled Rx ring %d (pf_q %d)\n",
580 				 ring->q_index, pf_q);
581 		}
582 
583 		return 0;
584 	}
585 
586 	ice_alloc_rx_bufs(ring, num_bufs);
587 
588 	return 0;
589 }
590 
591 /**
592  * __ice_vsi_get_qs - helper function for assigning queues from PF to VSI
593  * @qs_cfg: gathered variables needed for pf->vsi queues assignment
594  *
595  * This function first tries to find contiguous space. If it is not successful,
596  * it tries with the scatter approach.
597  *
598  * Return 0 on success and -ENOMEM in case of no left space in PF queue bitmap
599  */
600 int __ice_vsi_get_qs(struct ice_qs_cfg *qs_cfg)
601 {
602 	int ret = 0;
603 
604 	ret = __ice_vsi_get_qs_contig(qs_cfg);
605 	if (ret) {
606 		/* contig failed, so try with scatter approach */
607 		qs_cfg->mapping_mode = ICE_VSI_MAP_SCATTER;
608 		qs_cfg->q_count = min_t(unsigned int, qs_cfg->q_count,
609 					qs_cfg->scatter_count);
610 		ret = __ice_vsi_get_qs_sc(qs_cfg);
611 	}
612 	return ret;
613 }
614 
615 /**
616  * ice_vsi_ctrl_one_rx_ring - start/stop VSI's Rx ring with no busy wait
617  * @vsi: the VSI being configured
618  * @ena: start or stop the Rx ring
619  * @rxq_idx: 0-based Rx queue index for the VSI passed in
620  * @wait: wait or don't wait for configuration to finish in hardware
621  *
622  * Return 0 on success and negative on error.
623  */
624 int
625 ice_vsi_ctrl_one_rx_ring(struct ice_vsi *vsi, bool ena, u16 rxq_idx, bool wait)
626 {
627 	int pf_q = vsi->rxq_map[rxq_idx];
628 	struct ice_pf *pf = vsi->back;
629 	struct ice_hw *hw = &pf->hw;
630 	u32 rx_reg;
631 
632 	rx_reg = rd32(hw, QRX_CTRL(pf_q));
633 
634 	/* Skip if the queue is already in the requested state */
635 	if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
636 		return 0;
637 
638 	/* turn on/off the queue */
639 	if (ena)
640 		rx_reg |= QRX_CTRL_QENA_REQ_M;
641 	else
642 		rx_reg &= ~QRX_CTRL_QENA_REQ_M;
643 	wr32(hw, QRX_CTRL(pf_q), rx_reg);
644 
645 	if (!wait)
646 		return 0;
647 
648 	ice_flush(hw);
649 	return ice_pf_rxq_wait(pf, pf_q, ena);
650 }
651 
652 /**
653  * ice_vsi_wait_one_rx_ring - wait for a VSI's Rx ring to be stopped/started
654  * @vsi: the VSI being configured
655  * @ena: true/false to verify Rx ring has been enabled/disabled respectively
656  * @rxq_idx: 0-based Rx queue index for the VSI passed in
657  *
658  * This routine will wait for the given Rx queue of the VSI to reach the
659  * enabled or disabled state. Returns -ETIMEDOUT in case of failing to reach
660  * the requested state after multiple retries; else will return 0 in case of
661  * success.
662  */
663 int ice_vsi_wait_one_rx_ring(struct ice_vsi *vsi, bool ena, u16 rxq_idx)
664 {
665 	int pf_q = vsi->rxq_map[rxq_idx];
666 	struct ice_pf *pf = vsi->back;
667 
668 	return ice_pf_rxq_wait(pf, pf_q, ena);
669 }
670 
671 /**
672  * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
673  * @vsi: the VSI being configured
674  *
675  * We allocate one q_vector per queue interrupt. If allocation fails we
676  * return -ENOMEM.
677  */
678 int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
679 {
680 	struct device *dev = ice_pf_to_dev(vsi->back);
681 	u16 v_idx;
682 	int err;
683 
684 	if (vsi->q_vectors[0]) {
685 		dev_dbg(dev, "VSI %d has existing q_vectors\n", vsi->vsi_num);
686 		return -EEXIST;
687 	}
688 
689 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++) {
690 		err = ice_vsi_alloc_q_vector(vsi, v_idx);
691 		if (err)
692 			goto err_out;
693 	}
694 
695 	return 0;
696 
697 err_out:
698 	while (v_idx--)
699 		ice_free_q_vector(vsi, v_idx);
700 
701 	dev_err(dev, "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
702 		vsi->num_q_vectors, vsi->vsi_num, err);
703 	vsi->num_q_vectors = 0;
704 	return err;
705 }
706 
707 /**
708  * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
709  * @vsi: the VSI being configured
710  *
711  * This function maps descriptor rings to the queue-specific vectors allotted
712  * through the MSI-X enabling code. On a constrained vector budget, we map Tx
713  * and Rx rings to the vector as "efficiently" as possible.
714  */
715 void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
716 {
717 	int q_vectors = vsi->num_q_vectors;
718 	u16 tx_rings_rem, rx_rings_rem;
719 	int v_id;
720 
721 	/* initially assigning remaining rings count to VSIs num queue value */
722 	tx_rings_rem = vsi->num_txq;
723 	rx_rings_rem = vsi->num_rxq;
724 
725 	for (v_id = 0; v_id < q_vectors; v_id++) {
726 		struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
727 		u8 tx_rings_per_v, rx_rings_per_v;
728 		u16 q_id, q_base;
729 
730 		/* Tx rings mapping to vector */
731 		tx_rings_per_v = (u8)DIV_ROUND_UP(tx_rings_rem,
732 						  q_vectors - v_id);
733 		q_vector->num_ring_tx = tx_rings_per_v;
734 		q_vector->tx.tx_ring = NULL;
735 		q_vector->tx.itr_idx = ICE_TX_ITR;
736 		q_base = vsi->num_txq - tx_rings_rem;
737 
738 		for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
739 			struct ice_tx_ring *tx_ring = vsi->tx_rings[q_id];
740 
741 			tx_ring->q_vector = q_vector;
742 			tx_ring->next = q_vector->tx.tx_ring;
743 			q_vector->tx.tx_ring = tx_ring;
744 		}
745 		tx_rings_rem -= tx_rings_per_v;
746 
747 		/* Rx rings mapping to vector */
748 		rx_rings_per_v = (u8)DIV_ROUND_UP(rx_rings_rem,
749 						  q_vectors - v_id);
750 		q_vector->num_ring_rx = rx_rings_per_v;
751 		q_vector->rx.rx_ring = NULL;
752 		q_vector->rx.itr_idx = ICE_RX_ITR;
753 		q_base = vsi->num_rxq - rx_rings_rem;
754 
755 		for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
756 			struct ice_rx_ring *rx_ring = vsi->rx_rings[q_id];
757 
758 			rx_ring->q_vector = q_vector;
759 			rx_ring->next = q_vector->rx.rx_ring;
760 			q_vector->rx.rx_ring = rx_ring;
761 		}
762 		rx_rings_rem -= rx_rings_per_v;
763 	}
764 }
765 
766 /**
767  * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
768  * @vsi: the VSI having memory freed
769  */
770 void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
771 {
772 	int v_idx;
773 
774 	ice_for_each_q_vector(vsi, v_idx)
775 		ice_free_q_vector(vsi, v_idx);
776 }
777 
778 /**
779  * ice_vsi_cfg_txq - Configure single Tx queue
780  * @vsi: the VSI that queue belongs to
781  * @ring: Tx ring to be configured
782  * @qg_buf: queue group buffer
783  */
784 int
785 ice_vsi_cfg_txq(struct ice_vsi *vsi, struct ice_tx_ring *ring,
786 		struct ice_aqc_add_tx_qgrp *qg_buf)
787 {
788 	u8 buf_len = struct_size(qg_buf, txqs, 1);
789 	struct ice_tlan_ctx tlan_ctx = { 0 };
790 	struct ice_aqc_add_txqs_perq *txq;
791 	struct ice_channel *ch = ring->ch;
792 	struct ice_pf *pf = vsi->back;
793 	struct ice_hw *hw = &pf->hw;
794 	int status;
795 	u16 pf_q;
796 	u8 tc;
797 
798 	/* Configure XPS */
799 	ice_cfg_xps_tx_ring(ring);
800 
801 	pf_q = ring->reg_idx;
802 	ice_setup_tx_ctx(ring, &tlan_ctx, pf_q);
803 	/* copy context contents into the qg_buf */
804 	qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
805 	ice_set_ctx(hw, (u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
806 		    ice_tlan_ctx_info);
807 
808 	/* init queue specific tail reg. It is referred as
809 	 * transmit comm scheduler queue doorbell.
810 	 */
811 	ring->tail = hw->hw_addr + QTX_COMM_DBELL(pf_q);
812 
813 	if (IS_ENABLED(CONFIG_DCB))
814 		tc = ring->dcb_tc;
815 	else
816 		tc = 0;
817 
818 	/* Add unique software queue handle of the Tx queue per
819 	 * TC into the VSI Tx ring
820 	 */
821 	if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
822 		ring->q_handle = ice_eswitch_calc_txq_handle(ring);
823 
824 		if (ring->q_handle == ICE_INVAL_Q_INDEX)
825 			return -ENODEV;
826 	} else {
827 		ring->q_handle = ice_calc_txq_handle(vsi, ring, tc);
828 	}
829 
830 	if (ch)
831 		status = ice_ena_vsi_txq(vsi->port_info, ch->ch_vsi->idx, 0,
832 					 ring->q_handle, 1, qg_buf, buf_len,
833 					 NULL);
834 	else
835 		status = ice_ena_vsi_txq(vsi->port_info, vsi->idx, tc,
836 					 ring->q_handle, 1, qg_buf, buf_len,
837 					 NULL);
838 	if (status) {
839 		dev_err(ice_pf_to_dev(pf), "Failed to set LAN Tx queue context, error: %d\n",
840 			status);
841 		return status;
842 	}
843 
844 	/* Add Tx Queue TEID into the VSI Tx ring from the
845 	 * response. This will complete configuring and
846 	 * enabling the queue.
847 	 */
848 	txq = &qg_buf->txqs[0];
849 	if (pf_q == le16_to_cpu(txq->txq_id))
850 		ring->txq_teid = le32_to_cpu(txq->q_teid);
851 
852 	return 0;
853 }
854 
855 /**
856  * ice_cfg_itr - configure the initial interrupt throttle values
857  * @hw: pointer to the HW structure
858  * @q_vector: interrupt vector that's being configured
859  *
860  * Configure interrupt throttling values for the ring containers that are
861  * associated with the interrupt vector passed in.
862  */
863 void ice_cfg_itr(struct ice_hw *hw, struct ice_q_vector *q_vector)
864 {
865 	ice_cfg_itr_gran(hw);
866 
867 	if (q_vector->num_ring_rx)
868 		ice_write_itr(&q_vector->rx, q_vector->rx.itr_setting);
869 
870 	if (q_vector->num_ring_tx)
871 		ice_write_itr(&q_vector->tx, q_vector->tx.itr_setting);
872 
873 	ice_write_intrl(q_vector, q_vector->intrl);
874 }
875 
876 /**
877  * ice_cfg_txq_interrupt - configure interrupt on Tx queue
878  * @vsi: the VSI being configured
879  * @txq: Tx queue being mapped to MSI-X vector
880  * @msix_idx: MSI-X vector index within the function
881  * @itr_idx: ITR index of the interrupt cause
882  *
883  * Configure interrupt on Tx queue by associating Tx queue to MSI-X vector
884  * within the function space.
885  */
886 void
887 ice_cfg_txq_interrupt(struct ice_vsi *vsi, u16 txq, u16 msix_idx, u16 itr_idx)
888 {
889 	struct ice_pf *pf = vsi->back;
890 	struct ice_hw *hw = &pf->hw;
891 	u32 val;
892 
893 	itr_idx = (itr_idx << QINT_TQCTL_ITR_INDX_S) & QINT_TQCTL_ITR_INDX_M;
894 
895 	val = QINT_TQCTL_CAUSE_ENA_M | itr_idx |
896 	      ((msix_idx << QINT_TQCTL_MSIX_INDX_S) & QINT_TQCTL_MSIX_INDX_M);
897 
898 	wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
899 	if (ice_is_xdp_ena_vsi(vsi)) {
900 		u32 xdp_txq = txq + vsi->num_xdp_txq;
901 
902 		wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]),
903 		     val);
904 	}
905 	ice_flush(hw);
906 }
907 
908 /**
909  * ice_cfg_rxq_interrupt - configure interrupt on Rx queue
910  * @vsi: the VSI being configured
911  * @rxq: Rx queue being mapped to MSI-X vector
912  * @msix_idx: MSI-X vector index within the function
913  * @itr_idx: ITR index of the interrupt cause
914  *
915  * Configure interrupt on Rx queue by associating Rx queue to MSI-X vector
916  * within the function space.
917  */
918 void
919 ice_cfg_rxq_interrupt(struct ice_vsi *vsi, u16 rxq, u16 msix_idx, u16 itr_idx)
920 {
921 	struct ice_pf *pf = vsi->back;
922 	struct ice_hw *hw = &pf->hw;
923 	u32 val;
924 
925 	itr_idx = (itr_idx << QINT_RQCTL_ITR_INDX_S) & QINT_RQCTL_ITR_INDX_M;
926 
927 	val = QINT_RQCTL_CAUSE_ENA_M | itr_idx |
928 	      ((msix_idx << QINT_RQCTL_MSIX_INDX_S) & QINT_RQCTL_MSIX_INDX_M);
929 
930 	wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
931 
932 	ice_flush(hw);
933 }
934 
935 /**
936  * ice_trigger_sw_intr - trigger a software interrupt
937  * @hw: pointer to the HW structure
938  * @q_vector: interrupt vector to trigger the software interrupt for
939  */
940 void ice_trigger_sw_intr(struct ice_hw *hw, struct ice_q_vector *q_vector)
941 {
942 	wr32(hw, GLINT_DYN_CTL(q_vector->reg_idx),
943 	     (ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S) |
944 	     GLINT_DYN_CTL_SWINT_TRIG_M |
945 	     GLINT_DYN_CTL_INTENA_M);
946 }
947 
948 /**
949  * ice_vsi_stop_tx_ring - Disable single Tx ring
950  * @vsi: the VSI being configured
951  * @rst_src: reset source
952  * @rel_vmvf_num: Relative ID of VF/VM
953  * @ring: Tx ring to be stopped
954  * @txq_meta: Meta data of Tx ring to be stopped
955  */
956 int
957 ice_vsi_stop_tx_ring(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
958 		     u16 rel_vmvf_num, struct ice_tx_ring *ring,
959 		     struct ice_txq_meta *txq_meta)
960 {
961 	struct ice_pf *pf = vsi->back;
962 	struct ice_q_vector *q_vector;
963 	struct ice_hw *hw = &pf->hw;
964 	int status;
965 	u32 val;
966 
967 	/* clear cause_ena bit for disabled queues */
968 	val = rd32(hw, QINT_TQCTL(ring->reg_idx));
969 	val &= ~QINT_TQCTL_CAUSE_ENA_M;
970 	wr32(hw, QINT_TQCTL(ring->reg_idx), val);
971 
972 	/* software is expected to wait for 100 ns */
973 	ndelay(100);
974 
975 	/* trigger a software interrupt for the vector
976 	 * associated to the queue to schedule NAPI handler
977 	 */
978 	q_vector = ring->q_vector;
979 	if (q_vector)
980 		ice_trigger_sw_intr(hw, q_vector);
981 
982 	status = ice_dis_vsi_txq(vsi->port_info, txq_meta->vsi_idx,
983 				 txq_meta->tc, 1, &txq_meta->q_handle,
984 				 &txq_meta->q_id, &txq_meta->q_teid, rst_src,
985 				 rel_vmvf_num, NULL);
986 
987 	/* if the disable queue command was exercised during an
988 	 * active reset flow, -EBUSY is returned.
989 	 * This is not an error as the reset operation disables
990 	 * queues at the hardware level anyway.
991 	 */
992 	if (status == -EBUSY) {
993 		dev_dbg(ice_pf_to_dev(vsi->back), "Reset in progress. LAN Tx queues already disabled\n");
994 	} else if (status == -ENOENT) {
995 		dev_dbg(ice_pf_to_dev(vsi->back), "LAN Tx queues do not exist, nothing to disable\n");
996 	} else if (status) {
997 		dev_dbg(ice_pf_to_dev(vsi->back), "Failed to disable LAN Tx queues, error: %d\n",
998 			status);
999 		return status;
1000 	}
1001 
1002 	return 0;
1003 }
1004 
1005 /**
1006  * ice_fill_txq_meta - Prepare the Tx queue's meta data
1007  * @vsi: VSI that ring belongs to
1008  * @ring: ring that txq_meta will be based on
1009  * @txq_meta: a helper struct that wraps Tx queue's information
1010  *
1011  * Set up a helper struct that will contain all the necessary fields that
1012  * are needed for stopping Tx queue
1013  */
1014 void
1015 ice_fill_txq_meta(struct ice_vsi *vsi, struct ice_tx_ring *ring,
1016 		  struct ice_txq_meta *txq_meta)
1017 {
1018 	struct ice_channel *ch = ring->ch;
1019 	u8 tc;
1020 
1021 	if (IS_ENABLED(CONFIG_DCB))
1022 		tc = ring->dcb_tc;
1023 	else
1024 		tc = 0;
1025 
1026 	txq_meta->q_id = ring->reg_idx;
1027 	txq_meta->q_teid = ring->txq_teid;
1028 	txq_meta->q_handle = ring->q_handle;
1029 	if (ch) {
1030 		txq_meta->vsi_idx = ch->ch_vsi->idx;
1031 		txq_meta->tc = 0;
1032 	} else {
1033 		txq_meta->vsi_idx = vsi->idx;
1034 		txq_meta->tc = tc;
1035 	}
1036 }
1037