1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
3
4 #include <linux/bpf_trace.h>
5 #include <linux/unroll.h>
6 #include <net/xdp_sock_drv.h>
7 #include <net/xdp.h>
8 #include "ice.h"
9 #include "ice_base.h"
10 #include "ice_type.h"
11 #include "ice_xsk.h"
12 #include "ice_txrx.h"
13 #include "ice_txrx_lib.h"
14 #include "ice_lib.h"
15
ice_xdp_buf(struct ice_rx_ring * rx_ring,u32 idx)16 static struct xdp_buff **ice_xdp_buf(struct ice_rx_ring *rx_ring, u32 idx)
17 {
18 return &rx_ring->xdp_buf[idx];
19 }
20
21 /**
22 * ice_qp_reset_stats - Resets all stats for rings of given index
23 * @vsi: VSI that contains rings of interest
24 * @q_idx: ring index in array
25 */
ice_qp_reset_stats(struct ice_vsi * vsi,u16 q_idx)26 static void ice_qp_reset_stats(struct ice_vsi *vsi, u16 q_idx)
27 {
28 struct ice_vsi_stats *vsi_stat;
29 struct ice_pf *pf;
30
31 pf = vsi->back;
32 if (!pf->vsi_stats)
33 return;
34
35 vsi_stat = pf->vsi_stats[vsi->idx];
36 if (!vsi_stat)
37 return;
38
39 memset(&vsi_stat->rx_ring_stats[q_idx]->rx_stats, 0,
40 sizeof(vsi_stat->rx_ring_stats[q_idx]->rx_stats));
41 memset(&vsi_stat->tx_ring_stats[q_idx]->stats, 0,
42 sizeof(vsi_stat->tx_ring_stats[q_idx]->stats));
43 if (vsi->xdp_rings)
44 memset(&vsi->xdp_rings[q_idx]->ring_stats->stats, 0,
45 sizeof(vsi->xdp_rings[q_idx]->ring_stats->stats));
46 }
47
48 /**
49 * ice_qp_clean_rings - Cleans all the rings of a given index
50 * @vsi: VSI that contains rings of interest
51 * @q_idx: ring index in array
52 */
ice_qp_clean_rings(struct ice_vsi * vsi,u16 q_idx)53 static void ice_qp_clean_rings(struct ice_vsi *vsi, u16 q_idx)
54 {
55 ice_clean_tx_ring(vsi->tx_rings[q_idx]);
56 if (vsi->xdp_rings)
57 ice_clean_tx_ring(vsi->xdp_rings[q_idx]);
58 ice_clean_rx_ring(vsi->rx_rings[q_idx]);
59 }
60
61 /**
62 * ice_qvec_toggle_napi - Enables/disables NAPI for a given q_vector
63 * @vsi: VSI that has netdev
64 * @q_vector: q_vector that has NAPI context
65 * @enable: true for enable, false for disable
66 */
67 static void
ice_qvec_toggle_napi(struct ice_vsi * vsi,struct ice_q_vector * q_vector,bool enable)68 ice_qvec_toggle_napi(struct ice_vsi *vsi, struct ice_q_vector *q_vector,
69 bool enable)
70 {
71 if (!vsi->netdev || !q_vector)
72 return;
73
74 if (enable)
75 napi_enable(&q_vector->napi);
76 else
77 napi_disable(&q_vector->napi);
78 }
79
80 /**
81 * ice_qvec_dis_irq - Mask off queue interrupt generation on given ring
82 * @vsi: the VSI that contains queue vector being un-configured
83 * @rx_ring: Rx ring that will have its IRQ disabled
84 * @q_vector: queue vector
85 */
86 static void
ice_qvec_dis_irq(struct ice_vsi * vsi,struct ice_rx_ring * rx_ring,struct ice_q_vector * q_vector)87 ice_qvec_dis_irq(struct ice_vsi *vsi, struct ice_rx_ring *rx_ring,
88 struct ice_q_vector *q_vector)
89 {
90 struct ice_pf *pf = vsi->back;
91 struct ice_hw *hw = &pf->hw;
92 u16 reg;
93 u32 val;
94
95 /* QINT_TQCTL is being cleared in ice_vsi_stop_tx_ring, so handle
96 * here only QINT_RQCTL
97 */
98 reg = rx_ring->reg_idx;
99 val = rd32(hw, QINT_RQCTL(reg));
100 val &= ~QINT_RQCTL_CAUSE_ENA_M;
101 wr32(hw, QINT_RQCTL(reg), val);
102
103 if (q_vector) {
104 wr32(hw, GLINT_DYN_CTL(q_vector->reg_idx), 0);
105 ice_flush(hw);
106 synchronize_irq(q_vector->irq.virq);
107 }
108 }
109
110 /**
111 * ice_qvec_cfg_msix - Enable IRQ for given queue vector
112 * @vsi: the VSI that contains queue vector
113 * @q_vector: queue vector
114 * @qid: queue index
115 */
116 static void
ice_qvec_cfg_msix(struct ice_vsi * vsi,struct ice_q_vector * q_vector,u16 qid)117 ice_qvec_cfg_msix(struct ice_vsi *vsi, struct ice_q_vector *q_vector, u16 qid)
118 {
119 u16 reg_idx = q_vector->reg_idx;
120 struct ice_pf *pf = vsi->back;
121 struct ice_hw *hw = &pf->hw;
122 int q, _qid = qid;
123
124 ice_cfg_itr(hw, q_vector);
125
126 for (q = 0; q < q_vector->num_ring_tx; q++) {
127 ice_cfg_txq_interrupt(vsi, _qid, reg_idx, q_vector->tx.itr_idx);
128 _qid++;
129 }
130
131 _qid = qid;
132
133 for (q = 0; q < q_vector->num_ring_rx; q++) {
134 ice_cfg_rxq_interrupt(vsi, _qid, reg_idx, q_vector->rx.itr_idx);
135 _qid++;
136 }
137
138 ice_flush(hw);
139 }
140
141 /**
142 * ice_qvec_ena_irq - Enable IRQ for given queue vector
143 * @vsi: the VSI that contains queue vector
144 * @q_vector: queue vector
145 */
ice_qvec_ena_irq(struct ice_vsi * vsi,struct ice_q_vector * q_vector)146 static void ice_qvec_ena_irq(struct ice_vsi *vsi, struct ice_q_vector *q_vector)
147 {
148 struct ice_pf *pf = vsi->back;
149 struct ice_hw *hw = &pf->hw;
150
151 ice_irq_dynamic_ena(hw, vsi, q_vector);
152
153 ice_flush(hw);
154 }
155
156 /**
157 * ice_qp_dis - Disables a queue pair
158 * @vsi: VSI of interest
159 * @q_idx: ring index in array
160 *
161 * Returns 0 on success, negative on failure.
162 */
ice_qp_dis(struct ice_vsi * vsi,u16 q_idx)163 static int ice_qp_dis(struct ice_vsi *vsi, u16 q_idx)
164 {
165 struct ice_txq_meta txq_meta = { };
166 struct ice_q_vector *q_vector;
167 struct ice_tx_ring *tx_ring;
168 struct ice_rx_ring *rx_ring;
169 int fail = 0;
170 int err;
171
172 if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq)
173 return -EINVAL;
174
175 tx_ring = vsi->tx_rings[q_idx];
176 rx_ring = vsi->rx_rings[q_idx];
177 q_vector = rx_ring->q_vector;
178
179 synchronize_net();
180 netif_carrier_off(vsi->netdev);
181 netif_tx_stop_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
182
183 ice_qvec_dis_irq(vsi, rx_ring, q_vector);
184 ice_qvec_toggle_napi(vsi, q_vector, false);
185
186 ice_fill_txq_meta(vsi, tx_ring, &txq_meta);
187 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, tx_ring, &txq_meta);
188 if (!fail)
189 fail = err;
190 if (vsi->xdp_rings) {
191 struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_idx];
192
193 memset(&txq_meta, 0, sizeof(txq_meta));
194 ice_fill_txq_meta(vsi, xdp_ring, &txq_meta);
195 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, xdp_ring,
196 &txq_meta);
197 if (!fail)
198 fail = err;
199 }
200
201 ice_vsi_ctrl_one_rx_ring(vsi, false, q_idx, false);
202 ice_qp_clean_rings(vsi, q_idx);
203 ice_qp_reset_stats(vsi, q_idx);
204
205 return fail;
206 }
207
208 /**
209 * ice_qp_ena - Enables a queue pair
210 * @vsi: VSI of interest
211 * @q_idx: ring index in array
212 *
213 * Returns 0 on success, negative on failure.
214 */
ice_qp_ena(struct ice_vsi * vsi,u16 q_idx)215 static int ice_qp_ena(struct ice_vsi *vsi, u16 q_idx)
216 {
217 struct ice_q_vector *q_vector;
218 int fail = 0;
219 bool link_up;
220 int err;
221
222 err = ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx);
223 if (!fail)
224 fail = err;
225
226 if (ice_is_xdp_ena_vsi(vsi)) {
227 struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_idx];
228
229 err = ice_vsi_cfg_single_txq(vsi, vsi->xdp_rings, q_idx);
230 if (!fail)
231 fail = err;
232 ice_set_ring_xdp(xdp_ring);
233 ice_tx_xsk_pool(vsi, q_idx);
234 }
235
236 err = ice_vsi_cfg_single_rxq(vsi, q_idx);
237 if (!fail)
238 fail = err;
239
240 q_vector = vsi->rx_rings[q_idx]->q_vector;
241 ice_qvec_cfg_msix(vsi, q_vector, q_idx);
242
243 err = ice_vsi_ctrl_one_rx_ring(vsi, true, q_idx, true);
244 if (!fail)
245 fail = err;
246
247 ice_qvec_toggle_napi(vsi, q_vector, true);
248 ice_qvec_ena_irq(vsi, q_vector);
249
250 /* make sure NAPI sees updated ice_{t,x}_ring::xsk_pool */
251 synchronize_net();
252 ice_get_link_status(vsi->port_info, &link_up);
253 if (link_up) {
254 netif_tx_start_queue(netdev_get_tx_queue(vsi->netdev, q_idx));
255 netif_carrier_on(vsi->netdev);
256 }
257
258 return fail;
259 }
260
261 /**
262 * ice_xsk_pool_disable - disable a buffer pool region
263 * @vsi: Current VSI
264 * @qid: queue ID
265 *
266 * Returns 0 on success, negative on failure
267 */
ice_xsk_pool_disable(struct ice_vsi * vsi,u16 qid)268 static int ice_xsk_pool_disable(struct ice_vsi *vsi, u16 qid)
269 {
270 struct xsk_buff_pool *pool = xsk_get_pool_from_qid(vsi->netdev, qid);
271
272 if (!pool)
273 return -EINVAL;
274
275 xsk_pool_dma_unmap(pool, ICE_RX_DMA_ATTR);
276
277 return 0;
278 }
279
280 /**
281 * ice_xsk_pool_enable - enable a buffer pool region
282 * @vsi: Current VSI
283 * @pool: pointer to a requested buffer pool region
284 * @qid: queue ID
285 *
286 * Returns 0 on success, negative on failure
287 */
288 static int
ice_xsk_pool_enable(struct ice_vsi * vsi,struct xsk_buff_pool * pool,u16 qid)289 ice_xsk_pool_enable(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid)
290 {
291 int err;
292
293 if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_SF)
294 return -EINVAL;
295
296 if (qid >= vsi->netdev->real_num_rx_queues ||
297 qid >= vsi->netdev->real_num_tx_queues)
298 return -EINVAL;
299
300 err = xsk_pool_dma_map(pool, ice_pf_to_dev(vsi->back),
301 ICE_RX_DMA_ATTR);
302 if (err)
303 return err;
304
305 return 0;
306 }
307
308 /**
309 * ice_realloc_rx_xdp_bufs - reallocate for either XSK or normal buffer
310 * @rx_ring: Rx ring
311 * @pool_present: is pool for XSK present
312 *
313 * Try allocating memory and return ENOMEM, if failed to allocate.
314 * If allocation was successful, substitute buffer with allocated one.
315 * Returns 0 on success, negative on failure
316 */
317 static int
ice_realloc_rx_xdp_bufs(struct ice_rx_ring * rx_ring,bool pool_present)318 ice_realloc_rx_xdp_bufs(struct ice_rx_ring *rx_ring, bool pool_present)
319 {
320 size_t elem_size = pool_present ? sizeof(*rx_ring->xdp_buf) :
321 sizeof(*rx_ring->rx_buf);
322 void *sw_ring = kcalloc(rx_ring->count, elem_size, GFP_KERNEL);
323
324 if (!sw_ring)
325 return -ENOMEM;
326
327 if (pool_present) {
328 kfree(rx_ring->rx_buf);
329 rx_ring->rx_buf = NULL;
330 rx_ring->xdp_buf = sw_ring;
331 } else {
332 kfree(rx_ring->xdp_buf);
333 rx_ring->xdp_buf = NULL;
334 rx_ring->rx_buf = sw_ring;
335 }
336
337 return 0;
338 }
339
340 /**
341 * ice_realloc_zc_buf - reallocate XDP ZC queue pairs
342 * @vsi: Current VSI
343 * @zc: is zero copy set
344 *
345 * Reallocate buffer for rx_rings that might be used by XSK.
346 * XDP requires more memory, than rx_buf provides.
347 * Returns 0 on success, negative on failure
348 */
ice_realloc_zc_buf(struct ice_vsi * vsi,bool zc)349 int ice_realloc_zc_buf(struct ice_vsi *vsi, bool zc)
350 {
351 struct ice_rx_ring *rx_ring;
352 uint i;
353
354 ice_for_each_rxq(vsi, i) {
355 rx_ring = vsi->rx_rings[i];
356 if (!rx_ring->xsk_pool)
357 continue;
358
359 if (ice_realloc_rx_xdp_bufs(rx_ring, zc))
360 return -ENOMEM;
361 }
362
363 return 0;
364 }
365
366 /**
367 * ice_xsk_pool_setup - enable/disable a buffer pool region depending on its state
368 * @vsi: Current VSI
369 * @pool: buffer pool to enable/associate to a ring, NULL to disable
370 * @qid: queue ID
371 *
372 * Returns 0 on success, negative on failure
373 */
ice_xsk_pool_setup(struct ice_vsi * vsi,struct xsk_buff_pool * pool,u16 qid)374 int ice_xsk_pool_setup(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid)
375 {
376 bool if_running, pool_present = !!pool;
377 int ret = 0, pool_failure = 0;
378
379 if (qid >= vsi->num_rxq || qid >= vsi->num_txq) {
380 netdev_err(vsi->netdev, "Please use queue id in scope of combined queues count\n");
381 pool_failure = -EINVAL;
382 goto failure;
383 }
384
385 if_running = !test_bit(ICE_VSI_DOWN, vsi->state) &&
386 ice_is_xdp_ena_vsi(vsi);
387
388 if (if_running) {
389 struct ice_rx_ring *rx_ring = vsi->rx_rings[qid];
390
391 ret = ice_qp_dis(vsi, qid);
392 if (ret) {
393 netdev_err(vsi->netdev, "ice_qp_dis error = %d\n", ret);
394 goto xsk_pool_if_up;
395 }
396
397 ret = ice_realloc_rx_xdp_bufs(rx_ring, pool_present);
398 if (ret)
399 goto xsk_pool_if_up;
400 }
401
402 pool_failure = pool_present ? ice_xsk_pool_enable(vsi, pool, qid) :
403 ice_xsk_pool_disable(vsi, qid);
404
405 xsk_pool_if_up:
406 if (if_running) {
407 ret = ice_qp_ena(vsi, qid);
408 if (!ret && pool_present)
409 napi_schedule(&vsi->rx_rings[qid]->xdp_ring->q_vector->napi);
410 else if (ret)
411 netdev_err(vsi->netdev, "ice_qp_ena error = %d\n", ret);
412 }
413
414 failure:
415 if (pool_failure) {
416 netdev_err(vsi->netdev, "Could not %sable buffer pool, error = %d\n",
417 pool_present ? "en" : "dis", pool_failure);
418 return pool_failure;
419 }
420
421 return ret;
422 }
423
424 /**
425 * ice_fill_rx_descs - pick buffers from XSK buffer pool and use it
426 * @pool: XSK Buffer pool to pull the buffers from
427 * @xdp: SW ring of xdp_buff that will hold the buffers
428 * @rx_desc: Pointer to Rx descriptors that will be filled
429 * @count: The number of buffers to allocate
430 *
431 * This function allocates a number of Rx buffers from the fill ring
432 * or the internal recycle mechanism and places them on the Rx ring.
433 *
434 * Note that ring wrap should be handled by caller of this function.
435 *
436 * Returns the amount of allocated Rx descriptors
437 */
ice_fill_rx_descs(struct xsk_buff_pool * pool,struct xdp_buff ** xdp,union ice_32b_rx_flex_desc * rx_desc,u16 count)438 static u16 ice_fill_rx_descs(struct xsk_buff_pool *pool, struct xdp_buff **xdp,
439 union ice_32b_rx_flex_desc *rx_desc, u16 count)
440 {
441 dma_addr_t dma;
442 u16 buffs;
443 int i;
444
445 buffs = xsk_buff_alloc_batch(pool, xdp, count);
446 for (i = 0; i < buffs; i++) {
447 dma = xsk_buff_xdp_get_dma(*xdp);
448 rx_desc->read.pkt_addr = cpu_to_le64(dma);
449 rx_desc->wb.status_error0 = 0;
450
451 /* Put private info that changes on a per-packet basis
452 * into xdp_buff_xsk->cb.
453 */
454 ice_xdp_meta_set_desc(*xdp, rx_desc);
455
456 rx_desc++;
457 xdp++;
458 }
459
460 return buffs;
461 }
462
463 /**
464 * __ice_alloc_rx_bufs_zc - allocate a number of Rx buffers
465 * @rx_ring: Rx ring
466 * @xsk_pool: XSK buffer pool to pick buffers to be filled by HW
467 * @count: The number of buffers to allocate
468 *
469 * Place the @count of descriptors onto Rx ring. Handle the ring wrap
470 * for case where space from next_to_use up to the end of ring is less
471 * than @count. Finally do a tail bump.
472 *
473 * Returns true if all allocations were successful, false if any fail.
474 */
__ice_alloc_rx_bufs_zc(struct ice_rx_ring * rx_ring,struct xsk_buff_pool * xsk_pool,u16 count)475 static bool __ice_alloc_rx_bufs_zc(struct ice_rx_ring *rx_ring,
476 struct xsk_buff_pool *xsk_pool, u16 count)
477 {
478 u32 nb_buffs_extra = 0, nb_buffs = 0;
479 union ice_32b_rx_flex_desc *rx_desc;
480 u16 ntu = rx_ring->next_to_use;
481 u16 total_count = count;
482 struct xdp_buff **xdp;
483
484 rx_desc = ICE_RX_DESC(rx_ring, ntu);
485 xdp = ice_xdp_buf(rx_ring, ntu);
486
487 if (ntu + count >= rx_ring->count) {
488 nb_buffs_extra = ice_fill_rx_descs(xsk_pool, xdp, rx_desc,
489 rx_ring->count - ntu);
490 if (nb_buffs_extra != rx_ring->count - ntu) {
491 ntu += nb_buffs_extra;
492 goto exit;
493 }
494 rx_desc = ICE_RX_DESC(rx_ring, 0);
495 xdp = ice_xdp_buf(rx_ring, 0);
496 ntu = 0;
497 count -= nb_buffs_extra;
498 ice_release_rx_desc(rx_ring, 0);
499 }
500
501 nb_buffs = ice_fill_rx_descs(xsk_pool, xdp, rx_desc, count);
502
503 ntu += nb_buffs;
504 if (ntu == rx_ring->count)
505 ntu = 0;
506
507 exit:
508 if (rx_ring->next_to_use != ntu)
509 ice_release_rx_desc(rx_ring, ntu);
510
511 return total_count == (nb_buffs_extra + nb_buffs);
512 }
513
514 /**
515 * ice_alloc_rx_bufs_zc - allocate a number of Rx buffers
516 * @rx_ring: Rx ring
517 * @xsk_pool: XSK buffer pool to pick buffers to be filled by HW
518 * @count: The number of buffers to allocate
519 *
520 * Wrapper for internal allocation routine; figure out how many tail
521 * bumps should take place based on the given threshold
522 *
523 * Returns true if all calls to internal alloc routine succeeded
524 */
ice_alloc_rx_bufs_zc(struct ice_rx_ring * rx_ring,struct xsk_buff_pool * xsk_pool,u16 count)525 bool ice_alloc_rx_bufs_zc(struct ice_rx_ring *rx_ring,
526 struct xsk_buff_pool *xsk_pool, u16 count)
527 {
528 u16 rx_thresh = ICE_RING_QUARTER(rx_ring);
529 u16 leftover, i, tail_bumps;
530
531 tail_bumps = count / rx_thresh;
532 leftover = count - (tail_bumps * rx_thresh);
533
534 for (i = 0; i < tail_bumps; i++)
535 if (!__ice_alloc_rx_bufs_zc(rx_ring, xsk_pool, rx_thresh))
536 return false;
537 return __ice_alloc_rx_bufs_zc(rx_ring, xsk_pool, leftover);
538 }
539
540 /**
541 * ice_construct_skb_zc - Create an sk_buff from zero-copy buffer
542 * @rx_ring: Rx ring
543 * @xdp: Pointer to XDP buffer
544 *
545 * This function allocates a new skb from a zero-copy Rx buffer.
546 *
547 * Returns the skb on success, NULL on failure.
548 */
549 static struct sk_buff *
ice_construct_skb_zc(struct ice_rx_ring * rx_ring,struct xdp_buff * xdp)550 ice_construct_skb_zc(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp)
551 {
552 unsigned int totalsize = xdp->data_end - xdp->data_meta;
553 unsigned int metasize = xdp->data - xdp->data_meta;
554 struct skb_shared_info *sinfo = NULL;
555 struct sk_buff *skb;
556 u32 nr_frags = 0;
557
558 if (unlikely(xdp_buff_has_frags(xdp))) {
559 sinfo = xdp_get_shared_info_from_buff(xdp);
560 nr_frags = sinfo->nr_frags;
561 }
562 net_prefetch(xdp->data_meta);
563
564 skb = napi_alloc_skb(&rx_ring->q_vector->napi, totalsize);
565 if (unlikely(!skb))
566 return NULL;
567
568 memcpy(__skb_put(skb, totalsize), xdp->data_meta,
569 ALIGN(totalsize, sizeof(long)));
570
571 if (metasize) {
572 skb_metadata_set(skb, metasize);
573 __skb_pull(skb, metasize);
574 }
575
576 if (likely(!xdp_buff_has_frags(xdp)))
577 goto out;
578
579 for (int i = 0; i < nr_frags; i++) {
580 struct skb_shared_info *skinfo = skb_shinfo(skb);
581 skb_frag_t *frag = &sinfo->frags[i];
582 struct page *page;
583 void *addr;
584
585 page = dev_alloc_page();
586 if (!page) {
587 dev_kfree_skb(skb);
588 return NULL;
589 }
590 addr = page_to_virt(page);
591
592 memcpy(addr, skb_frag_page(frag), skb_frag_size(frag));
593
594 __skb_fill_page_desc_noacc(skinfo, skinfo->nr_frags++,
595 addr, 0, skb_frag_size(frag));
596 }
597
598 out:
599 xsk_buff_free(xdp);
600 return skb;
601 }
602
603 /**
604 * ice_clean_xdp_irq_zc - produce AF_XDP descriptors to CQ
605 * @xdp_ring: XDP Tx ring
606 * @xsk_pool: AF_XDP buffer pool pointer
607 */
ice_clean_xdp_irq_zc(struct ice_tx_ring * xdp_ring,struct xsk_buff_pool * xsk_pool)608 static u32 ice_clean_xdp_irq_zc(struct ice_tx_ring *xdp_ring,
609 struct xsk_buff_pool *xsk_pool)
610 {
611 u16 ntc = xdp_ring->next_to_clean;
612 struct ice_tx_desc *tx_desc;
613 u16 cnt = xdp_ring->count;
614 struct ice_tx_buf *tx_buf;
615 u16 completed_frames = 0;
616 u16 xsk_frames = 0;
617 u16 last_rs;
618 int i;
619
620 last_rs = xdp_ring->next_to_use ? xdp_ring->next_to_use - 1 : cnt - 1;
621 tx_desc = ICE_TX_DESC(xdp_ring, last_rs);
622 if (tx_desc->cmd_type_offset_bsz &
623 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)) {
624 if (last_rs >= ntc)
625 completed_frames = last_rs - ntc + 1;
626 else
627 completed_frames = last_rs + cnt - ntc + 1;
628 }
629
630 if (!completed_frames)
631 return 0;
632
633 if (likely(!xdp_ring->xdp_tx_active)) {
634 xsk_frames = completed_frames;
635 goto skip;
636 }
637
638 ntc = xdp_ring->next_to_clean;
639 for (i = 0; i < completed_frames; i++) {
640 tx_buf = &xdp_ring->tx_buf[ntc];
641
642 if (tx_buf->type == ICE_TX_BUF_XSK_TX) {
643 tx_buf->type = ICE_TX_BUF_EMPTY;
644 xsk_buff_free(tx_buf->xdp);
645 xdp_ring->xdp_tx_active--;
646 } else {
647 xsk_frames++;
648 }
649
650 ntc++;
651 if (ntc >= xdp_ring->count)
652 ntc = 0;
653 }
654 skip:
655 tx_desc->cmd_type_offset_bsz = 0;
656 xdp_ring->next_to_clean += completed_frames;
657 if (xdp_ring->next_to_clean >= cnt)
658 xdp_ring->next_to_clean -= cnt;
659 if (xsk_frames)
660 xsk_tx_completed(xsk_pool, xsk_frames);
661
662 return completed_frames;
663 }
664
665 /**
666 * ice_xmit_xdp_tx_zc - AF_XDP ZC handler for XDP_TX
667 * @xdp: XDP buffer to xmit
668 * @xdp_ring: XDP ring to produce descriptor onto
669 * @xsk_pool: AF_XDP buffer pool pointer
670 *
671 * note that this function works directly on xdp_buff, no need to convert
672 * it to xdp_frame. xdp_buff pointer is stored to ice_tx_buf so that cleaning
673 * side will be able to xsk_buff_free() it.
674 *
675 * Returns ICE_XDP_TX for successfully produced desc, ICE_XDP_CONSUMED if there
676 * was not enough space on XDP ring
677 */
ice_xmit_xdp_tx_zc(struct xdp_buff * xdp,struct ice_tx_ring * xdp_ring,struct xsk_buff_pool * xsk_pool)678 static int ice_xmit_xdp_tx_zc(struct xdp_buff *xdp,
679 struct ice_tx_ring *xdp_ring,
680 struct xsk_buff_pool *xsk_pool)
681 {
682 struct skb_shared_info *sinfo = NULL;
683 u32 size = xdp->data_end - xdp->data;
684 u32 ntu = xdp_ring->next_to_use;
685 struct ice_tx_desc *tx_desc;
686 struct ice_tx_buf *tx_buf;
687 struct xdp_buff *head;
688 u32 nr_frags = 0;
689 u32 free_space;
690 u32 frag = 0;
691
692 free_space = ICE_DESC_UNUSED(xdp_ring);
693 if (free_space < ICE_RING_QUARTER(xdp_ring))
694 free_space += ice_clean_xdp_irq_zc(xdp_ring, xsk_pool);
695
696 if (unlikely(!free_space))
697 goto busy;
698
699 if (unlikely(xdp_buff_has_frags(xdp))) {
700 sinfo = xdp_get_shared_info_from_buff(xdp);
701 nr_frags = sinfo->nr_frags;
702 if (free_space < nr_frags + 1)
703 goto busy;
704 }
705
706 tx_desc = ICE_TX_DESC(xdp_ring, ntu);
707 tx_buf = &xdp_ring->tx_buf[ntu];
708 head = xdp;
709
710 for (;;) {
711 dma_addr_t dma;
712
713 dma = xsk_buff_xdp_get_dma(xdp);
714 xsk_buff_raw_dma_sync_for_device(xsk_pool, dma, size);
715
716 tx_buf->xdp = xdp;
717 tx_buf->type = ICE_TX_BUF_XSK_TX;
718 tx_desc->buf_addr = cpu_to_le64(dma);
719 tx_desc->cmd_type_offset_bsz = ice_build_ctob(0, 0, size, 0);
720 /* account for each xdp_buff from xsk_buff_pool */
721 xdp_ring->xdp_tx_active++;
722
723 if (++ntu == xdp_ring->count)
724 ntu = 0;
725
726 if (frag == nr_frags)
727 break;
728
729 tx_desc = ICE_TX_DESC(xdp_ring, ntu);
730 tx_buf = &xdp_ring->tx_buf[ntu];
731
732 xdp = xsk_buff_get_frag(head);
733 size = skb_frag_size(&sinfo->frags[frag]);
734 frag++;
735 }
736
737 xdp_ring->next_to_use = ntu;
738 /* update last descriptor from a frame with EOP */
739 tx_desc->cmd_type_offset_bsz |=
740 cpu_to_le64(ICE_TX_DESC_CMD_EOP << ICE_TXD_QW1_CMD_S);
741
742 return ICE_XDP_TX;
743
744 busy:
745 xdp_ring->ring_stats->tx_stats.tx_busy++;
746
747 return ICE_XDP_CONSUMED;
748 }
749
750 /**
751 * ice_run_xdp_zc - Executes an XDP program in zero-copy path
752 * @rx_ring: Rx ring
753 * @xdp: xdp_buff used as input to the XDP program
754 * @xdp_prog: XDP program to run
755 * @xdp_ring: ring to be used for XDP_TX action
756 * @xsk_pool: AF_XDP buffer pool pointer
757 *
758 * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR}
759 */
760 static int
ice_run_xdp_zc(struct ice_rx_ring * rx_ring,struct xdp_buff * xdp,struct bpf_prog * xdp_prog,struct ice_tx_ring * xdp_ring,struct xsk_buff_pool * xsk_pool)761 ice_run_xdp_zc(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,
762 struct bpf_prog *xdp_prog, struct ice_tx_ring *xdp_ring,
763 struct xsk_buff_pool *xsk_pool)
764 {
765 int err, result = ICE_XDP_PASS;
766 u32 act;
767
768 act = bpf_prog_run_xdp(xdp_prog, xdp);
769
770 if (likely(act == XDP_REDIRECT)) {
771 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
772 if (!err)
773 return ICE_XDP_REDIR;
774 if (xsk_uses_need_wakeup(xsk_pool) && err == -ENOBUFS)
775 result = ICE_XDP_EXIT;
776 else
777 result = ICE_XDP_CONSUMED;
778 goto out_failure;
779 }
780
781 switch (act) {
782 case XDP_PASS:
783 break;
784 case XDP_TX:
785 result = ice_xmit_xdp_tx_zc(xdp, xdp_ring, xsk_pool);
786 if (result == ICE_XDP_CONSUMED)
787 goto out_failure;
788 break;
789 case XDP_DROP:
790 result = ICE_XDP_CONSUMED;
791 break;
792 default:
793 bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
794 fallthrough;
795 case XDP_ABORTED:
796 result = ICE_XDP_CONSUMED;
797 out_failure:
798 trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
799 break;
800 }
801
802 return result;
803 }
804
805 /**
806 * ice_clean_rx_irq_zc - consumes packets from the hardware ring
807 * @rx_ring: AF_XDP Rx ring
808 * @xsk_pool: AF_XDP buffer pool pointer
809 * @budget: NAPI budget
810 *
811 * Returns number of processed packets on success, remaining budget on failure.
812 */
ice_clean_rx_irq_zc(struct ice_rx_ring * rx_ring,struct xsk_buff_pool * xsk_pool,int budget)813 int ice_clean_rx_irq_zc(struct ice_rx_ring *rx_ring,
814 struct xsk_buff_pool *xsk_pool,
815 int budget)
816 {
817 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
818 u32 ntc = rx_ring->next_to_clean;
819 u32 ntu = rx_ring->next_to_use;
820 struct xdp_buff *first = NULL;
821 struct ice_tx_ring *xdp_ring;
822 unsigned int xdp_xmit = 0;
823 struct bpf_prog *xdp_prog;
824 u32 cnt = rx_ring->count;
825 bool failure = false;
826 int entries_to_alloc;
827
828 /* ZC patch is enabled only when XDP program is set,
829 * so here it can not be NULL
830 */
831 xdp_prog = READ_ONCE(rx_ring->xdp_prog);
832 xdp_ring = rx_ring->xdp_ring;
833
834 if (ntc != rx_ring->first_desc)
835 first = *ice_xdp_buf(rx_ring, rx_ring->first_desc);
836
837 while (likely(total_rx_packets < (unsigned int)budget)) {
838 union ice_32b_rx_flex_desc *rx_desc;
839 unsigned int size, xdp_res = 0;
840 struct xdp_buff *xdp;
841 struct sk_buff *skb;
842 u16 stat_err_bits;
843 u16 vlan_tci;
844
845 rx_desc = ICE_RX_DESC(rx_ring, ntc);
846
847 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S);
848 if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits))
849 break;
850
851 /* This memory barrier is needed to keep us from reading
852 * any other fields out of the rx_desc until we have
853 * verified the descriptor has been written back.
854 */
855 dma_rmb();
856
857 if (unlikely(ntc == ntu))
858 break;
859
860 xdp = *ice_xdp_buf(rx_ring, ntc);
861
862 size = le16_to_cpu(rx_desc->wb.pkt_len) &
863 ICE_RX_FLX_DESC_PKT_LEN_M;
864
865 xsk_buff_set_size(xdp, size);
866 xsk_buff_dma_sync_for_cpu(xdp);
867
868 if (!first) {
869 first = xdp;
870 } else if (likely(size) && !xsk_buff_add_frag(first, xdp)) {
871 xsk_buff_free(first);
872 break;
873 }
874
875 if (++ntc == cnt)
876 ntc = 0;
877
878 if (ice_is_non_eop(rx_ring, rx_desc))
879 continue;
880
881 xdp_res = ice_run_xdp_zc(rx_ring, first, xdp_prog, xdp_ring,
882 xsk_pool);
883 if (likely(xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR))) {
884 xdp_xmit |= xdp_res;
885 } else if (xdp_res == ICE_XDP_EXIT) {
886 failure = true;
887 first = NULL;
888 rx_ring->first_desc = ntc;
889 break;
890 } else if (xdp_res == ICE_XDP_CONSUMED) {
891 xsk_buff_free(first);
892 } else if (xdp_res == ICE_XDP_PASS) {
893 goto construct_skb;
894 }
895
896 total_rx_bytes += xdp_get_buff_len(first);
897 total_rx_packets++;
898
899 first = NULL;
900 rx_ring->first_desc = ntc;
901 continue;
902
903 construct_skb:
904 /* XDP_PASS path */
905 skb = ice_construct_skb_zc(rx_ring, first);
906 if (!skb) {
907 rx_ring->ring_stats->rx_stats.alloc_buf_failed++;
908 break;
909 }
910
911 first = NULL;
912 rx_ring->first_desc = ntc;
913
914 if (eth_skb_pad(skb)) {
915 skb = NULL;
916 continue;
917 }
918
919 total_rx_bytes += skb->len;
920 total_rx_packets++;
921
922 vlan_tci = ice_get_vlan_tci(rx_desc);
923
924 ice_process_skb_fields(rx_ring, rx_desc, skb);
925 ice_receive_skb(rx_ring, skb, vlan_tci);
926 }
927
928 rx_ring->next_to_clean = ntc;
929 entries_to_alloc = ICE_RX_DESC_UNUSED(rx_ring);
930 if (entries_to_alloc > ICE_RING_QUARTER(rx_ring))
931 failure |= !ice_alloc_rx_bufs_zc(rx_ring, xsk_pool,
932 entries_to_alloc);
933
934 ice_finalize_xdp_rx(xdp_ring, xdp_xmit, 0);
935 ice_update_rx_ring_stats(rx_ring, total_rx_packets, total_rx_bytes);
936
937 if (xsk_uses_need_wakeup(xsk_pool)) {
938 /* ntu could have changed when allocating entries above, so
939 * use rx_ring value instead of stack based one
940 */
941 if (failure || ntc == rx_ring->next_to_use)
942 xsk_set_rx_need_wakeup(xsk_pool);
943 else
944 xsk_clear_rx_need_wakeup(xsk_pool);
945
946 return (int)total_rx_packets;
947 }
948
949 return failure ? budget : (int)total_rx_packets;
950 }
951
952 /**
953 * ice_xmit_pkt - produce a single HW Tx descriptor out of AF_XDP descriptor
954 * @xdp_ring: XDP ring to produce the HW Tx descriptor on
955 * @xsk_pool: XSK buffer pool to pick buffers to be consumed by HW
956 * @desc: AF_XDP descriptor to pull the DMA address and length from
957 * @total_bytes: bytes accumulator that will be used for stats update
958 */
ice_xmit_pkt(struct ice_tx_ring * xdp_ring,struct xsk_buff_pool * xsk_pool,struct xdp_desc * desc,unsigned int * total_bytes)959 static void ice_xmit_pkt(struct ice_tx_ring *xdp_ring,
960 struct xsk_buff_pool *xsk_pool, struct xdp_desc *desc,
961 unsigned int *total_bytes)
962 {
963 struct ice_tx_desc *tx_desc;
964 dma_addr_t dma;
965
966 dma = xsk_buff_raw_get_dma(xsk_pool, desc->addr);
967 xsk_buff_raw_dma_sync_for_device(xsk_pool, dma, desc->len);
968
969 tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_to_use++);
970 tx_desc->buf_addr = cpu_to_le64(dma);
971 tx_desc->cmd_type_offset_bsz = ice_build_ctob(xsk_is_eop_desc(desc),
972 0, desc->len, 0);
973
974 *total_bytes += desc->len;
975 }
976
977 /**
978 * ice_xmit_pkt_batch - produce a batch of HW Tx descriptors out of AF_XDP descriptors
979 * @xdp_ring: XDP ring to produce the HW Tx descriptors on
980 * @xsk_pool: XSK buffer pool to pick buffers to be consumed by HW
981 * @descs: AF_XDP descriptors to pull the DMA addresses and lengths from
982 * @total_bytes: bytes accumulator that will be used for stats update
983 */
ice_xmit_pkt_batch(struct ice_tx_ring * xdp_ring,struct xsk_buff_pool * xsk_pool,struct xdp_desc * descs,unsigned int * total_bytes)984 static void ice_xmit_pkt_batch(struct ice_tx_ring *xdp_ring,
985 struct xsk_buff_pool *xsk_pool,
986 struct xdp_desc *descs,
987 unsigned int *total_bytes)
988 {
989 u16 ntu = xdp_ring->next_to_use;
990 struct ice_tx_desc *tx_desc;
991 u32 i;
992
993 unrolled_count(PKTS_PER_BATCH)
994 for (i = 0; i < PKTS_PER_BATCH; i++) {
995 dma_addr_t dma;
996
997 dma = xsk_buff_raw_get_dma(xsk_pool, descs[i].addr);
998 xsk_buff_raw_dma_sync_for_device(xsk_pool, dma, descs[i].len);
999
1000 tx_desc = ICE_TX_DESC(xdp_ring, ntu++);
1001 tx_desc->buf_addr = cpu_to_le64(dma);
1002 tx_desc->cmd_type_offset_bsz = ice_build_ctob(xsk_is_eop_desc(&descs[i]),
1003 0, descs[i].len, 0);
1004
1005 *total_bytes += descs[i].len;
1006 }
1007
1008 xdp_ring->next_to_use = ntu;
1009 }
1010
1011 /**
1012 * ice_fill_tx_hw_ring - produce the number of Tx descriptors onto ring
1013 * @xdp_ring: XDP ring to produce the HW Tx descriptors on
1014 * @xsk_pool: XSK buffer pool to pick buffers to be consumed by HW
1015 * @descs: AF_XDP descriptors to pull the DMA addresses and lengths from
1016 * @nb_pkts: count of packets to be send
1017 * @total_bytes: bytes accumulator that will be used for stats update
1018 */
ice_fill_tx_hw_ring(struct ice_tx_ring * xdp_ring,struct xsk_buff_pool * xsk_pool,struct xdp_desc * descs,u32 nb_pkts,unsigned int * total_bytes)1019 static void ice_fill_tx_hw_ring(struct ice_tx_ring *xdp_ring,
1020 struct xsk_buff_pool *xsk_pool,
1021 struct xdp_desc *descs, u32 nb_pkts,
1022 unsigned int *total_bytes)
1023 {
1024 u32 batched, leftover, i;
1025
1026 batched = ALIGN_DOWN(nb_pkts, PKTS_PER_BATCH);
1027 leftover = nb_pkts & (PKTS_PER_BATCH - 1);
1028 for (i = 0; i < batched; i += PKTS_PER_BATCH)
1029 ice_xmit_pkt_batch(xdp_ring, xsk_pool, &descs[i], total_bytes);
1030 for (; i < batched + leftover; i++)
1031 ice_xmit_pkt(xdp_ring, xsk_pool, &descs[i], total_bytes);
1032 }
1033
1034 /**
1035 * ice_xmit_zc - take entries from XSK Tx ring and place them onto HW Tx ring
1036 * @xdp_ring: XDP ring to produce the HW Tx descriptors on
1037 * @xsk_pool: AF_XDP buffer pool pointer
1038 *
1039 * Returns true if there is no more work that needs to be done, false otherwise
1040 */
ice_xmit_zc(struct ice_tx_ring * xdp_ring,struct xsk_buff_pool * xsk_pool)1041 bool ice_xmit_zc(struct ice_tx_ring *xdp_ring, struct xsk_buff_pool *xsk_pool)
1042 {
1043 struct xdp_desc *descs = xsk_pool->tx_descs;
1044 u32 nb_pkts, nb_processed = 0;
1045 unsigned int total_bytes = 0;
1046 int budget;
1047
1048 ice_clean_xdp_irq_zc(xdp_ring, xsk_pool);
1049
1050 if (!netif_carrier_ok(xdp_ring->vsi->netdev) ||
1051 !netif_running(xdp_ring->vsi->netdev))
1052 return true;
1053
1054 budget = ICE_DESC_UNUSED(xdp_ring);
1055 budget = min_t(u16, budget, ICE_RING_QUARTER(xdp_ring));
1056
1057 nb_pkts = xsk_tx_peek_release_desc_batch(xsk_pool, budget);
1058 if (!nb_pkts)
1059 return true;
1060
1061 if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) {
1062 nb_processed = xdp_ring->count - xdp_ring->next_to_use;
1063 ice_fill_tx_hw_ring(xdp_ring, xsk_pool, descs, nb_processed,
1064 &total_bytes);
1065 xdp_ring->next_to_use = 0;
1066 }
1067
1068 ice_fill_tx_hw_ring(xdp_ring, xsk_pool, &descs[nb_processed],
1069 nb_pkts - nb_processed, &total_bytes);
1070
1071 ice_set_rs_bit(xdp_ring);
1072 ice_xdp_ring_update_tail(xdp_ring);
1073 ice_update_tx_ring_stats(xdp_ring, nb_pkts, total_bytes);
1074
1075 if (xsk_uses_need_wakeup(xsk_pool))
1076 xsk_set_tx_need_wakeup(xsk_pool);
1077
1078 return nb_pkts < budget;
1079 }
1080
1081 /**
1082 * ice_xsk_wakeup - Implements ndo_xsk_wakeup
1083 * @netdev: net_device
1084 * @queue_id: queue to wake up
1085 * @flags: ignored in our case, since we have Rx and Tx in the same NAPI
1086 *
1087 * Returns negative on error, zero otherwise.
1088 */
1089 int
ice_xsk_wakeup(struct net_device * netdev,u32 queue_id,u32 __always_unused flags)1090 ice_xsk_wakeup(struct net_device *netdev, u32 queue_id,
1091 u32 __always_unused flags)
1092 {
1093 struct ice_netdev_priv *np = netdev_priv(netdev);
1094 struct ice_q_vector *q_vector;
1095 struct ice_vsi *vsi = np->vsi;
1096 struct ice_tx_ring *ring;
1097
1098 if (test_bit(ICE_VSI_DOWN, vsi->state) || !netif_carrier_ok(netdev))
1099 return -ENETDOWN;
1100
1101 if (!ice_is_xdp_ena_vsi(vsi))
1102 return -EINVAL;
1103
1104 if (queue_id >= vsi->num_txq || queue_id >= vsi->num_rxq)
1105 return -EINVAL;
1106
1107 ring = vsi->rx_rings[queue_id]->xdp_ring;
1108
1109 if (!READ_ONCE(ring->xsk_pool))
1110 return -EINVAL;
1111
1112 /* The idea here is that if NAPI is running, mark a miss, so
1113 * it will run again. If not, trigger an interrupt and
1114 * schedule the NAPI from interrupt context. If NAPI would be
1115 * scheduled here, the interrupt affinity would not be
1116 * honored.
1117 */
1118 q_vector = ring->q_vector;
1119 if (!napi_if_scheduled_mark_missed(&q_vector->napi))
1120 ice_trigger_sw_intr(&vsi->back->hw, q_vector);
1121
1122 return 0;
1123 }
1124
1125 /**
1126 * ice_xsk_any_rx_ring_ena - Checks if Rx rings have AF_XDP buff pool attached
1127 * @vsi: VSI to be checked
1128 *
1129 * Returns true if any of the Rx rings has an AF_XDP buff pool attached
1130 */
ice_xsk_any_rx_ring_ena(struct ice_vsi * vsi)1131 bool ice_xsk_any_rx_ring_ena(struct ice_vsi *vsi)
1132 {
1133 int i;
1134
1135 ice_for_each_rxq(vsi, i) {
1136 if (xsk_get_pool_from_qid(vsi->netdev, i))
1137 return true;
1138 }
1139
1140 return false;
1141 }
1142
1143 /**
1144 * ice_xsk_clean_rx_ring - clean buffer pool queues connected to a given Rx ring
1145 * @rx_ring: ring to be cleaned
1146 */
ice_xsk_clean_rx_ring(struct ice_rx_ring * rx_ring)1147 void ice_xsk_clean_rx_ring(struct ice_rx_ring *rx_ring)
1148 {
1149 u16 ntc = rx_ring->next_to_clean;
1150 u16 ntu = rx_ring->next_to_use;
1151
1152 while (ntc != ntu) {
1153 struct xdp_buff *xdp = *ice_xdp_buf(rx_ring, ntc);
1154
1155 xsk_buff_free(xdp);
1156 ntc++;
1157 if (ntc >= rx_ring->count)
1158 ntc = 0;
1159 }
1160 }
1161
1162 /**
1163 * ice_xsk_clean_xdp_ring - Clean the XDP Tx ring and its buffer pool queues
1164 * @xdp_ring: XDP_Tx ring
1165 */
ice_xsk_clean_xdp_ring(struct ice_tx_ring * xdp_ring)1166 void ice_xsk_clean_xdp_ring(struct ice_tx_ring *xdp_ring)
1167 {
1168 u16 ntc = xdp_ring->next_to_clean, ntu = xdp_ring->next_to_use;
1169 u32 xsk_frames = 0;
1170
1171 while (ntc != ntu) {
1172 struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[ntc];
1173
1174 if (tx_buf->type == ICE_TX_BUF_XSK_TX) {
1175 tx_buf->type = ICE_TX_BUF_EMPTY;
1176 xsk_buff_free(tx_buf->xdp);
1177 } else {
1178 xsk_frames++;
1179 }
1180
1181 ntc++;
1182 if (ntc >= xdp_ring->count)
1183 ntc = 0;
1184 }
1185
1186 if (xsk_frames)
1187 xsk_tx_completed(xdp_ring->xsk_pool, xsk_frames);
1188 }
1189