1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
3
4 #include <linux/bpf_trace.h>
5 #include <net/xdp_sock_drv.h>
6 #include <net/xdp.h>
7 #include "ice.h"
8 #include "ice_base.h"
9 #include "ice_type.h"
10 #include "ice_xsk.h"
11 #include "ice_txrx.h"
12 #include "ice_txrx_lib.h"
13 #include "ice_lib.h"
14
15 /**
16 * ice_qp_reset_stats - Resets all stats for rings of given index
17 * @vsi: VSI that contains rings of interest
18 * @q_idx: ring index in array
19 */
ice_qp_reset_stats(struct ice_vsi * vsi,u16 q_idx)20 static void ice_qp_reset_stats(struct ice_vsi *vsi, u16 q_idx)
21 {
22 memset(&vsi->rx_rings[q_idx]->rx_stats, 0,
23 sizeof(vsi->rx_rings[q_idx]->rx_stats));
24 memset(&vsi->tx_rings[q_idx]->stats, 0,
25 sizeof(vsi->tx_rings[q_idx]->stats));
26 if (ice_is_xdp_ena_vsi(vsi))
27 memset(&vsi->xdp_rings[q_idx]->stats, 0,
28 sizeof(vsi->xdp_rings[q_idx]->stats));
29 }
30
31 /**
32 * ice_qp_clean_rings - Cleans all the rings of a given index
33 * @vsi: VSI that contains rings of interest
34 * @q_idx: ring index in array
35 */
ice_qp_clean_rings(struct ice_vsi * vsi,u16 q_idx)36 static void ice_qp_clean_rings(struct ice_vsi *vsi, u16 q_idx)
37 {
38 ice_clean_tx_ring(vsi->tx_rings[q_idx]);
39 if (ice_is_xdp_ena_vsi(vsi)) {
40 synchronize_rcu();
41 ice_clean_tx_ring(vsi->xdp_rings[q_idx]);
42 }
43 ice_clean_rx_ring(vsi->rx_rings[q_idx]);
44 }
45
46 /**
47 * ice_qvec_toggle_napi - Enables/disables NAPI for a given q_vector
48 * @vsi: VSI that has netdev
49 * @q_vector: q_vector that has NAPI context
50 * @enable: true for enable, false for disable
51 */
52 static void
ice_qvec_toggle_napi(struct ice_vsi * vsi,struct ice_q_vector * q_vector,bool enable)53 ice_qvec_toggle_napi(struct ice_vsi *vsi, struct ice_q_vector *q_vector,
54 bool enable)
55 {
56 if (!vsi->netdev || !q_vector)
57 return;
58
59 if (enable)
60 napi_enable(&q_vector->napi);
61 else
62 napi_disable(&q_vector->napi);
63 }
64
65 /**
66 * ice_qvec_dis_irq - Mask off queue interrupt generation on given ring
67 * @vsi: the VSI that contains queue vector being un-configured
68 * @rx_ring: Rx ring that will have its IRQ disabled
69 * @q_vector: queue vector
70 */
71 static void
ice_qvec_dis_irq(struct ice_vsi * vsi,struct ice_ring * rx_ring,struct ice_q_vector * q_vector)72 ice_qvec_dis_irq(struct ice_vsi *vsi, struct ice_ring *rx_ring,
73 struct ice_q_vector *q_vector)
74 {
75 struct ice_pf *pf = vsi->back;
76 struct ice_hw *hw = &pf->hw;
77 int base = vsi->base_vector;
78 u16 reg;
79 u32 val;
80
81 /* QINT_TQCTL is being cleared in ice_vsi_stop_tx_ring, so handle
82 * here only QINT_RQCTL
83 */
84 reg = rx_ring->reg_idx;
85 val = rd32(hw, QINT_RQCTL(reg));
86 val &= ~QINT_RQCTL_CAUSE_ENA_M;
87 wr32(hw, QINT_RQCTL(reg), val);
88
89 if (q_vector) {
90 u16 v_idx = q_vector->v_idx;
91
92 wr32(hw, GLINT_DYN_CTL(q_vector->reg_idx), 0);
93 ice_flush(hw);
94 synchronize_irq(pf->msix_entries[v_idx + base].vector);
95 }
96 }
97
98 /**
99 * ice_qvec_cfg_msix - Enable IRQ for given queue vector
100 * @vsi: the VSI that contains queue vector
101 * @q_vector: queue vector
102 */
103 static void
ice_qvec_cfg_msix(struct ice_vsi * vsi,struct ice_q_vector * q_vector)104 ice_qvec_cfg_msix(struct ice_vsi *vsi, struct ice_q_vector *q_vector)
105 {
106 u16 reg_idx = q_vector->reg_idx;
107 struct ice_pf *pf = vsi->back;
108 struct ice_hw *hw = &pf->hw;
109 struct ice_ring *ring;
110
111 ice_cfg_itr(hw, q_vector);
112
113 wr32(hw, GLINT_RATE(reg_idx),
114 ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
115
116 ice_for_each_ring(ring, q_vector->tx)
117 ice_cfg_txq_interrupt(vsi, ring->reg_idx, reg_idx,
118 q_vector->tx.itr_idx);
119
120 ice_for_each_ring(ring, q_vector->rx)
121 ice_cfg_rxq_interrupt(vsi, ring->reg_idx, reg_idx,
122 q_vector->rx.itr_idx);
123
124 ice_flush(hw);
125 }
126
127 /**
128 * ice_qvec_ena_irq - Enable IRQ for given queue vector
129 * @vsi: the VSI that contains queue vector
130 * @q_vector: queue vector
131 */
ice_qvec_ena_irq(struct ice_vsi * vsi,struct ice_q_vector * q_vector)132 static void ice_qvec_ena_irq(struct ice_vsi *vsi, struct ice_q_vector *q_vector)
133 {
134 struct ice_pf *pf = vsi->back;
135 struct ice_hw *hw = &pf->hw;
136
137 ice_irq_dynamic_ena(hw, vsi, q_vector);
138
139 ice_flush(hw);
140 }
141
142 /**
143 * ice_qp_dis - Disables a queue pair
144 * @vsi: VSI of interest
145 * @q_idx: ring index in array
146 *
147 * Returns 0 on success, negative on failure.
148 */
ice_qp_dis(struct ice_vsi * vsi,u16 q_idx)149 static int ice_qp_dis(struct ice_vsi *vsi, u16 q_idx)
150 {
151 struct ice_txq_meta txq_meta = { };
152 struct ice_ring *tx_ring, *rx_ring;
153 struct ice_q_vector *q_vector;
154 int timeout = 50;
155 int err;
156
157 if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq)
158 return -EINVAL;
159
160 tx_ring = vsi->tx_rings[q_idx];
161 rx_ring = vsi->rx_rings[q_idx];
162 q_vector = rx_ring->q_vector;
163
164 while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state)) {
165 timeout--;
166 if (!timeout)
167 return -EBUSY;
168 usleep_range(1000, 2000);
169 }
170 netif_tx_stop_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
171
172 ice_fill_txq_meta(vsi, tx_ring, &txq_meta);
173 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, tx_ring, &txq_meta);
174 if (err)
175 return err;
176 if (ice_is_xdp_ena_vsi(vsi)) {
177 struct ice_ring *xdp_ring = vsi->xdp_rings[q_idx];
178
179 memset(&txq_meta, 0, sizeof(txq_meta));
180 ice_fill_txq_meta(vsi, xdp_ring, &txq_meta);
181 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, xdp_ring,
182 &txq_meta);
183 if (err)
184 return err;
185 }
186 ice_qvec_dis_irq(vsi, rx_ring, q_vector);
187
188 err = ice_vsi_ctrl_one_rx_ring(vsi, false, q_idx, true);
189 if (err)
190 return err;
191
192 ice_qvec_toggle_napi(vsi, q_vector, false);
193 ice_qp_clean_rings(vsi, q_idx);
194 ice_qp_reset_stats(vsi, q_idx);
195
196 return 0;
197 }
198
199 /**
200 * ice_qp_ena - Enables a queue pair
201 * @vsi: VSI of interest
202 * @q_idx: ring index in array
203 *
204 * Returns 0 on success, negative on failure.
205 */
ice_qp_ena(struct ice_vsi * vsi,u16 q_idx)206 static int ice_qp_ena(struct ice_vsi *vsi, u16 q_idx)
207 {
208 struct ice_aqc_add_tx_qgrp *qg_buf;
209 struct ice_ring *tx_ring, *rx_ring;
210 struct ice_q_vector *q_vector;
211 u16 size;
212 int err;
213
214 if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq)
215 return -EINVAL;
216
217 size = struct_size(qg_buf, txqs, 1);
218 qg_buf = kzalloc(size, GFP_KERNEL);
219 if (!qg_buf)
220 return -ENOMEM;
221
222 qg_buf->num_txqs = 1;
223
224 tx_ring = vsi->tx_rings[q_idx];
225 rx_ring = vsi->rx_rings[q_idx];
226 q_vector = rx_ring->q_vector;
227
228 err = ice_vsi_cfg_txq(vsi, tx_ring, qg_buf);
229 if (err)
230 goto free_buf;
231
232 if (ice_is_xdp_ena_vsi(vsi)) {
233 struct ice_ring *xdp_ring = vsi->xdp_rings[q_idx];
234
235 memset(qg_buf, 0, size);
236 qg_buf->num_txqs = 1;
237 err = ice_vsi_cfg_txq(vsi, xdp_ring, qg_buf);
238 if (err)
239 goto free_buf;
240 ice_set_ring_xdp(xdp_ring);
241 xdp_ring->xsk_pool = ice_xsk_pool(xdp_ring);
242 }
243
244 err = ice_setup_rx_ctx(rx_ring);
245 if (err)
246 goto free_buf;
247
248 ice_qvec_cfg_msix(vsi, q_vector);
249
250 err = ice_vsi_ctrl_one_rx_ring(vsi, true, q_idx, true);
251 if (err)
252 goto free_buf;
253
254 clear_bit(__ICE_CFG_BUSY, vsi->state);
255 ice_qvec_toggle_napi(vsi, q_vector, true);
256 ice_qvec_ena_irq(vsi, q_vector);
257
258 netif_tx_start_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
259 free_buf:
260 kfree(qg_buf);
261 return err;
262 }
263
264 /**
265 * ice_xsk_alloc_pools - allocate a buffer pool for an XDP socket
266 * @vsi: VSI to allocate the buffer pool on
267 *
268 * Returns 0 on success, negative on error
269 */
ice_xsk_alloc_pools(struct ice_vsi * vsi)270 static int ice_xsk_alloc_pools(struct ice_vsi *vsi)
271 {
272 if (vsi->xsk_pools)
273 return 0;
274
275 vsi->xsk_pools = kcalloc(vsi->num_xsk_pools, sizeof(*vsi->xsk_pools),
276 GFP_KERNEL);
277
278 if (!vsi->xsk_pools) {
279 vsi->num_xsk_pools = 0;
280 return -ENOMEM;
281 }
282
283 return 0;
284 }
285
286 /**
287 * ice_xsk_remove_pool - Remove an buffer pool for a certain ring/qid
288 * @vsi: VSI from which the VSI will be removed
289 * @qid: Ring/qid associated with the buffer pool
290 */
ice_xsk_remove_pool(struct ice_vsi * vsi,u16 qid)291 static void ice_xsk_remove_pool(struct ice_vsi *vsi, u16 qid)
292 {
293 vsi->xsk_pools[qid] = NULL;
294 vsi->num_xsk_pools_used--;
295
296 if (vsi->num_xsk_pools_used == 0) {
297 kfree(vsi->xsk_pools);
298 vsi->xsk_pools = NULL;
299 vsi->num_xsk_pools = 0;
300 }
301 }
302
303 /**
304 * ice_xsk_pool_disable - disable a buffer pool region
305 * @vsi: Current VSI
306 * @qid: queue ID
307 *
308 * Returns 0 on success, negative on failure
309 */
ice_xsk_pool_disable(struct ice_vsi * vsi,u16 qid)310 static int ice_xsk_pool_disable(struct ice_vsi *vsi, u16 qid)
311 {
312 if (!vsi->xsk_pools || qid >= vsi->num_xsk_pools ||
313 !vsi->xsk_pools[qid])
314 return -EINVAL;
315
316 xsk_pool_dma_unmap(vsi->xsk_pools[qid], ICE_RX_DMA_ATTR);
317 ice_xsk_remove_pool(vsi, qid);
318
319 return 0;
320 }
321
322 /**
323 * ice_xsk_pool_enable - enable a buffer pool region
324 * @vsi: Current VSI
325 * @pool: pointer to a requested buffer pool region
326 * @qid: queue ID
327 *
328 * Returns 0 on success, negative on failure
329 */
330 static int
ice_xsk_pool_enable(struct ice_vsi * vsi,struct xsk_buff_pool * pool,u16 qid)331 ice_xsk_pool_enable(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid)
332 {
333 int err;
334
335 if (vsi->type != ICE_VSI_PF)
336 return -EINVAL;
337
338 if (!vsi->num_xsk_pools)
339 vsi->num_xsk_pools = min_t(u16, vsi->num_rxq, vsi->num_txq);
340 if (qid >= vsi->num_xsk_pools)
341 return -EINVAL;
342
343 err = ice_xsk_alloc_pools(vsi);
344 if (err)
345 return err;
346
347 if (vsi->xsk_pools && vsi->xsk_pools[qid])
348 return -EBUSY;
349
350 vsi->xsk_pools[qid] = pool;
351 vsi->num_xsk_pools_used++;
352
353 err = xsk_pool_dma_map(vsi->xsk_pools[qid], ice_pf_to_dev(vsi->back),
354 ICE_RX_DMA_ATTR);
355 if (err)
356 return err;
357
358 return 0;
359 }
360
361 /**
362 * ice_xsk_pool_setup - enable/disable a buffer pool region depending on its state
363 * @vsi: Current VSI
364 * @pool: buffer pool to enable/associate to a ring, NULL to disable
365 * @qid: queue ID
366 *
367 * Returns 0 on success, negative on failure
368 */
ice_xsk_pool_setup(struct ice_vsi * vsi,struct xsk_buff_pool * pool,u16 qid)369 int ice_xsk_pool_setup(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid)
370 {
371 bool if_running, pool_present = !!pool;
372 int ret = 0, pool_failure = 0;
373
374 if (qid >= vsi->num_rxq || qid >= vsi->num_txq) {
375 netdev_err(vsi->netdev, "Please use queue id in scope of combined queues count\n");
376 pool_failure = -EINVAL;
377 goto failure;
378 }
379
380 if (!is_power_of_2(vsi->rx_rings[qid]->count) ||
381 !is_power_of_2(vsi->tx_rings[qid]->count)) {
382 netdev_err(vsi->netdev, "Please align ring sizes to power of 2\n");
383 pool_failure = -EINVAL;
384 goto failure;
385 }
386
387 if_running = netif_running(vsi->netdev) && ice_is_xdp_ena_vsi(vsi);
388
389 if (if_running) {
390 ret = ice_qp_dis(vsi, qid);
391 if (ret) {
392 netdev_err(vsi->netdev, "ice_qp_dis error = %d\n", ret);
393 goto xsk_pool_if_up;
394 }
395 }
396
397 pool_failure = pool_present ? ice_xsk_pool_enable(vsi, pool, qid) :
398 ice_xsk_pool_disable(vsi, qid);
399
400 xsk_pool_if_up:
401 if (if_running) {
402 ret = ice_qp_ena(vsi, qid);
403 if (!ret && pool_present)
404 napi_schedule(&vsi->xdp_rings[qid]->q_vector->napi);
405 else if (ret)
406 netdev_err(vsi->netdev, "ice_qp_ena error = %d\n", ret);
407 }
408
409 failure:
410 if (pool_failure) {
411 netdev_err(vsi->netdev, "Could not %sable buffer pool, error = %d\n",
412 pool_present ? "en" : "dis", pool_failure);
413 return pool_failure;
414 }
415
416 return ret;
417 }
418
419 /**
420 * ice_alloc_rx_bufs_zc - allocate a number of Rx buffers
421 * @rx_ring: Rx ring
422 * @count: The number of buffers to allocate
423 *
424 * This function allocates a number of Rx buffers from the fill ring
425 * or the internal recycle mechanism and places them on the Rx ring.
426 *
427 * Returns false if all allocations were successful, true if any fail.
428 */
ice_alloc_rx_bufs_zc(struct ice_ring * rx_ring,u16 count)429 bool ice_alloc_rx_bufs_zc(struct ice_ring *rx_ring, u16 count)
430 {
431 union ice_32b_rx_flex_desc *rx_desc;
432 u16 ntu = rx_ring->next_to_use;
433 struct ice_rx_buf *rx_buf;
434 bool ret = false;
435 dma_addr_t dma;
436
437 if (!count)
438 return false;
439
440 rx_desc = ICE_RX_DESC(rx_ring, ntu);
441 rx_buf = &rx_ring->rx_buf[ntu];
442
443 do {
444 rx_buf->xdp = xsk_buff_alloc(rx_ring->xsk_pool);
445 if (!rx_buf->xdp) {
446 ret = true;
447 break;
448 }
449
450 dma = xsk_buff_xdp_get_dma(rx_buf->xdp);
451 rx_desc->read.pkt_addr = cpu_to_le64(dma);
452 rx_desc->wb.status_error0 = 0;
453
454 rx_desc++;
455 rx_buf++;
456 ntu++;
457
458 if (unlikely(ntu == rx_ring->count)) {
459 rx_desc = ICE_RX_DESC(rx_ring, 0);
460 rx_buf = rx_ring->rx_buf;
461 ntu = 0;
462 }
463 } while (--count);
464
465 if (rx_ring->next_to_use != ntu) {
466 /* clear the status bits for the next_to_use descriptor */
467 rx_desc->wb.status_error0 = 0;
468 ice_release_rx_desc(rx_ring, ntu);
469 }
470
471 return ret;
472 }
473
474 /**
475 * ice_bump_ntc - Bump the next_to_clean counter of an Rx ring
476 * @rx_ring: Rx ring
477 */
ice_bump_ntc(struct ice_ring * rx_ring)478 static void ice_bump_ntc(struct ice_ring *rx_ring)
479 {
480 int ntc = rx_ring->next_to_clean + 1;
481
482 ntc = (ntc < rx_ring->count) ? ntc : 0;
483 rx_ring->next_to_clean = ntc;
484 prefetch(ICE_RX_DESC(rx_ring, ntc));
485 }
486
487 /**
488 * ice_construct_skb_zc - Create an sk_buff from zero-copy buffer
489 * @rx_ring: Rx ring
490 * @rx_buf: zero-copy Rx buffer
491 *
492 * This function allocates a new skb from a zero-copy Rx buffer.
493 *
494 * Returns the skb on success, NULL on failure.
495 */
496 static struct sk_buff *
ice_construct_skb_zc(struct ice_ring * rx_ring,struct ice_rx_buf * rx_buf)497 ice_construct_skb_zc(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf)
498 {
499 unsigned int metasize = rx_buf->xdp->data - rx_buf->xdp->data_meta;
500 unsigned int datasize = rx_buf->xdp->data_end - rx_buf->xdp->data;
501 unsigned int datasize_hard = rx_buf->xdp->data_end -
502 rx_buf->xdp->data_hard_start;
503 struct sk_buff *skb;
504
505 skb = __napi_alloc_skb(&rx_ring->q_vector->napi, datasize_hard,
506 GFP_ATOMIC | __GFP_NOWARN);
507 if (unlikely(!skb))
508 return NULL;
509
510 skb_reserve(skb, rx_buf->xdp->data - rx_buf->xdp->data_hard_start);
511 memcpy(__skb_put(skb, datasize), rx_buf->xdp->data, datasize);
512 if (metasize)
513 skb_metadata_set(skb, metasize);
514
515 xsk_buff_free(rx_buf->xdp);
516 rx_buf->xdp = NULL;
517 return skb;
518 }
519
520 /**
521 * ice_run_xdp_zc - Executes an XDP program in zero-copy path
522 * @rx_ring: Rx ring
523 * @xdp: xdp_buff used as input to the XDP program
524 *
525 * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR}
526 */
527 static int
ice_run_xdp_zc(struct ice_ring * rx_ring,struct xdp_buff * xdp)528 ice_run_xdp_zc(struct ice_ring *rx_ring, struct xdp_buff *xdp)
529 {
530 int err, result = ICE_XDP_PASS;
531 struct bpf_prog *xdp_prog;
532 struct ice_ring *xdp_ring;
533 u32 act;
534
535 rcu_read_lock();
536 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
537 if (!xdp_prog) {
538 rcu_read_unlock();
539 return ICE_XDP_PASS;
540 }
541
542 act = bpf_prog_run_xdp(xdp_prog, xdp);
543
544 if (likely(act == XDP_REDIRECT)) {
545 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
546 if (err)
547 goto out_failure;
548 rcu_read_unlock();
549 return ICE_XDP_REDIR;
550 }
551
552 switch (act) {
553 case XDP_PASS:
554 break;
555 case XDP_TX:
556 xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->q_index];
557 result = ice_xmit_xdp_buff(xdp, xdp_ring);
558 if (result == ICE_XDP_CONSUMED)
559 goto out_failure;
560 break;
561 default:
562 bpf_warn_invalid_xdp_action(act);
563 fallthrough;
564 case XDP_ABORTED:
565 out_failure:
566 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
567 fallthrough;
568 case XDP_DROP:
569 result = ICE_XDP_CONSUMED;
570 break;
571 }
572
573 rcu_read_unlock();
574 return result;
575 }
576
577 /**
578 * ice_clean_rx_irq_zc - consumes packets from the hardware ring
579 * @rx_ring: AF_XDP Rx ring
580 * @budget: NAPI budget
581 *
582 * Returns number of processed packets on success, remaining budget on failure.
583 */
ice_clean_rx_irq_zc(struct ice_ring * rx_ring,int budget)584 int ice_clean_rx_irq_zc(struct ice_ring *rx_ring, int budget)
585 {
586 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
587 u16 cleaned_count = ICE_DESC_UNUSED(rx_ring);
588 unsigned int xdp_xmit = 0;
589 bool failure = false;
590
591 while (likely(total_rx_packets < (unsigned int)budget)) {
592 union ice_32b_rx_flex_desc *rx_desc;
593 unsigned int size, xdp_res = 0;
594 struct ice_rx_buf *rx_buf;
595 struct sk_buff *skb;
596 u16 stat_err_bits;
597 u16 vlan_tag = 0;
598 u8 rx_ptype;
599
600 if (cleaned_count >= ICE_RX_BUF_WRITE) {
601 failure |= ice_alloc_rx_bufs_zc(rx_ring,
602 cleaned_count);
603 cleaned_count = 0;
604 }
605
606 rx_desc = ICE_RX_DESC(rx_ring, rx_ring->next_to_clean);
607
608 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
609 if (!ice_test_staterr(rx_desc, stat_err_bits))
610 break;
611
612 /* This memory barrier is needed to keep us from reading
613 * any other fields out of the rx_desc until we have
614 * verified the descriptor has been written back.
615 */
616 dma_rmb();
617
618 size = le16_to_cpu(rx_desc->wb.pkt_len) &
619 ICE_RX_FLX_DESC_PKT_LEN_M;
620 if (!size)
621 break;
622
623 rx_buf = &rx_ring->rx_buf[rx_ring->next_to_clean];
624 rx_buf->xdp->data_end = rx_buf->xdp->data + size;
625 xsk_buff_dma_sync_for_cpu(rx_buf->xdp, rx_ring->xsk_pool);
626
627 xdp_res = ice_run_xdp_zc(rx_ring, rx_buf->xdp);
628 if (xdp_res) {
629 if (xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR))
630 xdp_xmit |= xdp_res;
631 else
632 xsk_buff_free(rx_buf->xdp);
633
634 rx_buf->xdp = NULL;
635 total_rx_bytes += size;
636 total_rx_packets++;
637 cleaned_count++;
638
639 ice_bump_ntc(rx_ring);
640 continue;
641 }
642
643 /* XDP_PASS path */
644 skb = ice_construct_skb_zc(rx_ring, rx_buf);
645 if (!skb) {
646 rx_ring->rx_stats.alloc_buf_failed++;
647 break;
648 }
649
650 cleaned_count++;
651 ice_bump_ntc(rx_ring);
652
653 if (eth_skb_pad(skb)) {
654 skb = NULL;
655 continue;
656 }
657
658 total_rx_bytes += skb->len;
659 total_rx_packets++;
660
661 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_L2TAG1P_S);
662 if (ice_test_staterr(rx_desc, stat_err_bits))
663 vlan_tag = le16_to_cpu(rx_desc->wb.l2tag1);
664
665 rx_ptype = le16_to_cpu(rx_desc->wb.ptype_flex_flags0) &
666 ICE_RX_FLEX_DESC_PTYPE_M;
667
668 ice_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype);
669 ice_receive_skb(rx_ring, skb, vlan_tag);
670 }
671
672 ice_finalize_xdp_rx(rx_ring, xdp_xmit);
673 ice_update_rx_ring_stats(rx_ring, total_rx_packets, total_rx_bytes);
674
675 if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) {
676 if (failure || rx_ring->next_to_clean == rx_ring->next_to_use)
677 xsk_set_rx_need_wakeup(rx_ring->xsk_pool);
678 else
679 xsk_clear_rx_need_wakeup(rx_ring->xsk_pool);
680
681 return (int)total_rx_packets;
682 }
683
684 return failure ? budget : (int)total_rx_packets;
685 }
686
687 /**
688 * ice_xmit_zc - Completes AF_XDP entries, and cleans XDP entries
689 * @xdp_ring: XDP Tx ring
690 * @budget: max number of frames to xmit
691 *
692 * Returns true if cleanup/transmission is done.
693 */
ice_xmit_zc(struct ice_ring * xdp_ring,int budget)694 static bool ice_xmit_zc(struct ice_ring *xdp_ring, int budget)
695 {
696 struct ice_tx_desc *tx_desc = NULL;
697 bool work_done = true;
698 struct xdp_desc desc;
699 dma_addr_t dma;
700
701 while (likely(budget-- > 0)) {
702 struct ice_tx_buf *tx_buf;
703
704 if (unlikely(!ICE_DESC_UNUSED(xdp_ring))) {
705 xdp_ring->tx_stats.tx_busy++;
706 work_done = false;
707 break;
708 }
709
710 tx_buf = &xdp_ring->tx_buf[xdp_ring->next_to_use];
711
712 if (!xsk_tx_peek_desc(xdp_ring->xsk_pool, &desc))
713 break;
714
715 dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc.addr);
716 xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma,
717 desc.len);
718
719 tx_buf->bytecount = desc.len;
720
721 tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_to_use);
722 tx_desc->buf_addr = cpu_to_le64(dma);
723 tx_desc->cmd_type_offset_bsz =
724 ice_build_ctob(ICE_TXD_LAST_DESC_CMD, 0, desc.len, 0);
725
726 xdp_ring->next_to_use++;
727 if (xdp_ring->next_to_use == xdp_ring->count)
728 xdp_ring->next_to_use = 0;
729 }
730
731 if (tx_desc) {
732 ice_xdp_ring_update_tail(xdp_ring);
733 xsk_tx_release(xdp_ring->xsk_pool);
734 }
735
736 return budget > 0 && work_done;
737 }
738
739 /**
740 * ice_clean_xdp_tx_buf - Free and unmap XDP Tx buffer
741 * @xdp_ring: XDP Tx ring
742 * @tx_buf: Tx buffer to clean
743 */
744 static void
ice_clean_xdp_tx_buf(struct ice_ring * xdp_ring,struct ice_tx_buf * tx_buf)745 ice_clean_xdp_tx_buf(struct ice_ring *xdp_ring, struct ice_tx_buf *tx_buf)
746 {
747 xdp_return_frame((struct xdp_frame *)tx_buf->raw_buf);
748 dma_unmap_single(xdp_ring->dev, dma_unmap_addr(tx_buf, dma),
749 dma_unmap_len(tx_buf, len), DMA_TO_DEVICE);
750 dma_unmap_len_set(tx_buf, len, 0);
751 }
752
753 /**
754 * ice_clean_tx_irq_zc - Completes AF_XDP entries, and cleans XDP entries
755 * @xdp_ring: XDP Tx ring
756 * @budget: NAPI budget
757 *
758 * Returns true if cleanup/tranmission is done.
759 */
ice_clean_tx_irq_zc(struct ice_ring * xdp_ring,int budget)760 bool ice_clean_tx_irq_zc(struct ice_ring *xdp_ring, int budget)
761 {
762 int total_packets = 0, total_bytes = 0;
763 s16 ntc = xdp_ring->next_to_clean;
764 struct ice_tx_desc *tx_desc;
765 struct ice_tx_buf *tx_buf;
766 u32 xsk_frames = 0;
767 bool xmit_done;
768
769 tx_desc = ICE_TX_DESC(xdp_ring, ntc);
770 tx_buf = &xdp_ring->tx_buf[ntc];
771 ntc -= xdp_ring->count;
772
773 do {
774 if (!(tx_desc->cmd_type_offset_bsz &
775 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
776 break;
777
778 total_bytes += tx_buf->bytecount;
779 total_packets++;
780
781 if (tx_buf->raw_buf) {
782 ice_clean_xdp_tx_buf(xdp_ring, tx_buf);
783 tx_buf->raw_buf = NULL;
784 } else {
785 xsk_frames++;
786 }
787
788 tx_desc->cmd_type_offset_bsz = 0;
789 tx_buf++;
790 tx_desc++;
791 ntc++;
792
793 if (unlikely(!ntc)) {
794 ntc -= xdp_ring->count;
795 tx_buf = xdp_ring->tx_buf;
796 tx_desc = ICE_TX_DESC(xdp_ring, 0);
797 }
798
799 prefetch(tx_desc);
800
801 } while (likely(--budget));
802
803 ntc += xdp_ring->count;
804 xdp_ring->next_to_clean = ntc;
805
806 if (xsk_frames)
807 xsk_tx_completed(xdp_ring->xsk_pool, xsk_frames);
808
809 if (xsk_uses_need_wakeup(xdp_ring->xsk_pool))
810 xsk_set_tx_need_wakeup(xdp_ring->xsk_pool);
811
812 ice_update_tx_ring_stats(xdp_ring, total_packets, total_bytes);
813 xmit_done = ice_xmit_zc(xdp_ring, ICE_DFLT_IRQ_WORK);
814
815 return budget > 0 && xmit_done;
816 }
817
818 /**
819 * ice_xsk_wakeup - Implements ndo_xsk_wakeup
820 * @netdev: net_device
821 * @queue_id: queue to wake up
822 * @flags: ignored in our case, since we have Rx and Tx in the same NAPI
823 *
824 * Returns negative on error, zero otherwise.
825 */
826 int
ice_xsk_wakeup(struct net_device * netdev,u32 queue_id,u32 __always_unused flags)827 ice_xsk_wakeup(struct net_device *netdev, u32 queue_id,
828 u32 __always_unused flags)
829 {
830 struct ice_netdev_priv *np = netdev_priv(netdev);
831 struct ice_q_vector *q_vector;
832 struct ice_vsi *vsi = np->vsi;
833 struct ice_ring *ring;
834
835 if (test_bit(__ICE_DOWN, vsi->state))
836 return -ENETDOWN;
837
838 if (!ice_is_xdp_ena_vsi(vsi))
839 return -ENXIO;
840
841 if (queue_id >= vsi->num_txq)
842 return -ENXIO;
843
844 if (!vsi->xdp_rings[queue_id]->xsk_pool)
845 return -ENXIO;
846
847 ring = vsi->xdp_rings[queue_id];
848
849 /* The idea here is that if NAPI is running, mark a miss, so
850 * it will run again. If not, trigger an interrupt and
851 * schedule the NAPI from interrupt context. If NAPI would be
852 * scheduled here, the interrupt affinity would not be
853 * honored.
854 */
855 q_vector = ring->q_vector;
856 if (!napi_if_scheduled_mark_missed(&q_vector->napi))
857 ice_trigger_sw_intr(&vsi->back->hw, q_vector);
858
859 return 0;
860 }
861
862 /**
863 * ice_xsk_any_rx_ring_ena - Checks if Rx rings have AF_XDP buff pool attached
864 * @vsi: VSI to be checked
865 *
866 * Returns true if any of the Rx rings has an AF_XDP buff pool attached
867 */
ice_xsk_any_rx_ring_ena(struct ice_vsi * vsi)868 bool ice_xsk_any_rx_ring_ena(struct ice_vsi *vsi)
869 {
870 int i;
871
872 if (!vsi->xsk_pools)
873 return false;
874
875 for (i = 0; i < vsi->num_xsk_pools; i++) {
876 if (vsi->xsk_pools[i])
877 return true;
878 }
879
880 return false;
881 }
882
883 /**
884 * ice_xsk_clean_rx_ring - clean buffer pool queues connected to a given Rx ring
885 * @rx_ring: ring to be cleaned
886 */
ice_xsk_clean_rx_ring(struct ice_ring * rx_ring)887 void ice_xsk_clean_rx_ring(struct ice_ring *rx_ring)
888 {
889 u16 i;
890
891 for (i = 0; i < rx_ring->count; i++) {
892 struct ice_rx_buf *rx_buf = &rx_ring->rx_buf[i];
893
894 if (!rx_buf->xdp)
895 continue;
896
897 rx_buf->xdp = NULL;
898 }
899 }
900
901 /**
902 * ice_xsk_clean_xdp_ring - Clean the XDP Tx ring and its buffer pool queues
903 * @xdp_ring: XDP_Tx ring
904 */
ice_xsk_clean_xdp_ring(struct ice_ring * xdp_ring)905 void ice_xsk_clean_xdp_ring(struct ice_ring *xdp_ring)
906 {
907 u16 ntc = xdp_ring->next_to_clean, ntu = xdp_ring->next_to_use;
908 u32 xsk_frames = 0;
909
910 while (ntc != ntu) {
911 struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[ntc];
912
913 if (tx_buf->raw_buf)
914 ice_clean_xdp_tx_buf(xdp_ring, tx_buf);
915 else
916 xsk_frames++;
917
918 tx_buf->raw_buf = NULL;
919
920 ntc++;
921 if (ntc >= xdp_ring->count)
922 ntc = 0;
923 }
924
925 if (xsk_frames)
926 xsk_tx_completed(xdp_ring->xsk_pool, xsk_frames);
927 }
928