1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2018 Intel Corporation. */
4 #include <linux/bpf_trace.h>
5 #include <net/xdp_sock_drv.h>
9 #include "ixgbe_txrx_common.h"
11 struct xsk_buff_pool
*ixgbe_xsk_pool(struct ixgbe_adapter
*adapter
,
12 struct ixgbe_ring
*ring
)
14 bool xdp_on
= READ_ONCE(adapter
->xdp_prog
);
15 int qid
= ring
->ring_idx
;
17 if (!xdp_on
|| !test_bit(qid
, adapter
->af_xdp_zc_qps
))
20 return xsk_get_pool_from_qid(adapter
->netdev
, qid
);
23 static int ixgbe_xsk_pool_enable(struct ixgbe_adapter
*adapter
,
24 struct xsk_buff_pool
*pool
,
27 struct net_device
*netdev
= adapter
->netdev
;
31 if (qid
>= adapter
->num_rx_queues
)
34 if (qid
>= netdev
->real_num_rx_queues
||
35 qid
>= netdev
->real_num_tx_queues
)
38 err
= xsk_pool_dma_map(pool
, &adapter
->pdev
->dev
, IXGBE_RX_DMA_ATTR
);
42 if_running
= netif_running(adapter
->netdev
) &&
43 ixgbe_enabled_xdp_adapter(adapter
);
46 ixgbe_txrx_ring_disable(adapter
, qid
);
48 set_bit(qid
, adapter
->af_xdp_zc_qps
);
51 ixgbe_txrx_ring_enable(adapter
, qid
);
53 /* Kick start the NAPI context so that receiving will start */
54 err
= ixgbe_xsk_wakeup(adapter
->netdev
, qid
, XDP_WAKEUP_RX
);
56 clear_bit(qid
, adapter
->af_xdp_zc_qps
);
57 xsk_pool_dma_unmap(pool
, IXGBE_RX_DMA_ATTR
);
65 static int ixgbe_xsk_pool_disable(struct ixgbe_adapter
*adapter
, u16 qid
)
67 struct xsk_buff_pool
*pool
;
70 pool
= xsk_get_pool_from_qid(adapter
->netdev
, qid
);
74 if_running
= netif_running(adapter
->netdev
) &&
75 ixgbe_enabled_xdp_adapter(adapter
);
78 ixgbe_txrx_ring_disable(adapter
, qid
);
80 clear_bit(qid
, adapter
->af_xdp_zc_qps
);
81 xsk_pool_dma_unmap(pool
, IXGBE_RX_DMA_ATTR
);
84 ixgbe_txrx_ring_enable(adapter
, qid
);
89 int ixgbe_xsk_pool_setup(struct ixgbe_adapter
*adapter
,
90 struct xsk_buff_pool
*pool
,
93 return pool
? ixgbe_xsk_pool_enable(adapter
, pool
, qid
) :
94 ixgbe_xsk_pool_disable(adapter
, qid
);
97 static int ixgbe_run_xdp_zc(struct ixgbe_adapter
*adapter
,
98 struct ixgbe_ring
*rx_ring
,
101 int err
, result
= IXGBE_XDP_PASS
;
102 struct bpf_prog
*xdp_prog
;
103 struct ixgbe_ring
*ring
;
104 struct xdp_frame
*xdpf
;
107 xdp_prog
= READ_ONCE(rx_ring
->xdp_prog
);
108 act
= bpf_prog_run_xdp(xdp_prog
, xdp
);
110 if (likely(act
== XDP_REDIRECT
)) {
111 err
= xdp_do_redirect(rx_ring
->netdev
, xdp
, xdp_prog
);
113 return IXGBE_XDP_REDIR
;
114 if (xsk_uses_need_wakeup(rx_ring
->xsk_pool
) && err
== -ENOBUFS
)
115 result
= IXGBE_XDP_EXIT
;
117 result
= IXGBE_XDP_CONSUMED
;
125 xdpf
= xdp_convert_buff_to_frame(xdp
);
128 ring
= ixgbe_determine_xdp_ring(adapter
);
129 if (static_branch_unlikely(&ixgbe_xdp_locking_key
))
130 spin_lock(&ring
->tx_lock
);
131 result
= ixgbe_xmit_xdp_ring(ring
, xdpf
);
132 if (static_branch_unlikely(&ixgbe_xdp_locking_key
))
133 spin_unlock(&ring
->tx_lock
);
134 if (result
== IXGBE_XDP_CONSUMED
)
138 result
= IXGBE_XDP_CONSUMED
;
141 bpf_warn_invalid_xdp_action(rx_ring
->netdev
, xdp_prog
, act
);
144 result
= IXGBE_XDP_CONSUMED
;
146 trace_xdp_exception(rx_ring
->netdev
, xdp_prog
, act
);
151 bool ixgbe_alloc_rx_buffers_zc(struct ixgbe_ring
*rx_ring
, u16 count
)
153 union ixgbe_adv_rx_desc
*rx_desc
;
154 struct ixgbe_rx_buffer
*bi
;
155 u16 i
= rx_ring
->next_to_use
;
163 rx_desc
= IXGBE_RX_DESC(rx_ring
, i
);
164 bi
= &rx_ring
->rx_buffer_info
[i
];
168 bi
->xdp
= xsk_buff_alloc(rx_ring
->xsk_pool
);
174 dma
= xsk_buff_xdp_get_dma(bi
->xdp
);
176 /* Refresh the desc even if buffer_addrs didn't change
177 * because each write-back erases this info.
179 rx_desc
->read
.pkt_addr
= cpu_to_le64(dma
);
185 rx_desc
= IXGBE_RX_DESC(rx_ring
, 0);
186 bi
= rx_ring
->rx_buffer_info
;
190 /* clear the length for the next_to_use descriptor */
191 rx_desc
->wb
.upper
.length
= 0;
198 if (rx_ring
->next_to_use
!= i
) {
199 rx_ring
->next_to_use
= i
;
201 /* Force memory writes to complete before letting h/w
202 * know there are new descriptors to fetch. (Only
203 * applicable for weak-ordered memory model archs,
207 writel(i
, rx_ring
->tail
);
213 static struct sk_buff
*ixgbe_construct_skb_zc(struct ixgbe_ring
*rx_ring
,
214 const struct xdp_buff
*xdp
)
216 unsigned int totalsize
= xdp
->data_end
- xdp
->data_meta
;
217 unsigned int metasize
= xdp
->data
- xdp
->data_meta
;
220 net_prefetch(xdp
->data_meta
);
222 /* allocate a skb to store the frags */
223 skb
= napi_alloc_skb(&rx_ring
->q_vector
->napi
, totalsize
);
227 memcpy(__skb_put(skb
, totalsize
), xdp
->data_meta
,
228 ALIGN(totalsize
, sizeof(long)));
231 skb_metadata_set(skb
, metasize
);
232 __skb_pull(skb
, metasize
);
238 static void ixgbe_inc_ntc(struct ixgbe_ring
*rx_ring
)
240 u32 ntc
= rx_ring
->next_to_clean
+ 1;
242 ntc
= (ntc
< rx_ring
->count
) ? ntc
: 0;
243 rx_ring
->next_to_clean
= ntc
;
244 prefetch(IXGBE_RX_DESC(rx_ring
, ntc
));
247 int ixgbe_clean_rx_irq_zc(struct ixgbe_q_vector
*q_vector
,
248 struct ixgbe_ring
*rx_ring
,
251 unsigned int total_rx_bytes
= 0, total_rx_packets
= 0;
252 struct ixgbe_adapter
*adapter
= q_vector
->adapter
;
253 u16 cleaned_count
= ixgbe_desc_unused(rx_ring
);
254 unsigned int xdp_res
, xdp_xmit
= 0;
255 bool failure
= false;
258 while (likely(total_rx_packets
< budget
)) {
259 union ixgbe_adv_rx_desc
*rx_desc
;
260 struct ixgbe_rx_buffer
*bi
;
263 /* return some buffers to hardware, one at a time is too slow */
264 if (cleaned_count
>= IXGBE_RX_BUFFER_WRITE
) {
266 !ixgbe_alloc_rx_buffers_zc(rx_ring
,
271 rx_desc
= IXGBE_RX_DESC(rx_ring
, rx_ring
->next_to_clean
);
272 size
= le16_to_cpu(rx_desc
->wb
.upper
.length
);
276 /* This memory barrier is needed to keep us from reading
277 * any other fields out of the rx_desc until we know the
278 * descriptor has been written back
282 bi
= &rx_ring
->rx_buffer_info
[rx_ring
->next_to_clean
];
284 if (unlikely(!ixgbe_test_staterr(rx_desc
,
285 IXGBE_RXD_STAT_EOP
))) {
286 struct ixgbe_rx_buffer
*next_bi
;
288 xsk_buff_free(bi
->xdp
);
290 ixgbe_inc_ntc(rx_ring
);
292 &rx_ring
->rx_buffer_info
[rx_ring
->next_to_clean
];
293 next_bi
->discard
= true;
297 if (unlikely(bi
->discard
)) {
298 xsk_buff_free(bi
->xdp
);
301 ixgbe_inc_ntc(rx_ring
);
305 bi
->xdp
->data_end
= bi
->xdp
->data
+ size
;
306 xsk_buff_dma_sync_for_cpu(bi
->xdp
);
307 xdp_res
= ixgbe_run_xdp_zc(adapter
, rx_ring
, bi
->xdp
);
309 if (likely(xdp_res
& (IXGBE_XDP_TX
| IXGBE_XDP_REDIR
))) {
311 } else if (xdp_res
== IXGBE_XDP_EXIT
) {
314 } else if (xdp_res
== IXGBE_XDP_CONSUMED
) {
315 xsk_buff_free(bi
->xdp
);
316 } else if (xdp_res
== IXGBE_XDP_PASS
) {
322 total_rx_bytes
+= size
;
325 ixgbe_inc_ntc(rx_ring
);
330 skb
= ixgbe_construct_skb_zc(rx_ring
, bi
->xdp
);
332 rx_ring
->rx_stats
.alloc_rx_buff_failed
++;
336 xsk_buff_free(bi
->xdp
);
340 ixgbe_inc_ntc(rx_ring
);
342 if (eth_skb_pad(skb
))
345 total_rx_bytes
+= skb
->len
;
348 ixgbe_process_skb_fields(rx_ring
, rx_desc
, skb
);
349 ixgbe_rx_skb(q_vector
, skb
);
352 if (xdp_xmit
& IXGBE_XDP_REDIR
)
355 if (xdp_xmit
& IXGBE_XDP_TX
) {
356 struct ixgbe_ring
*ring
= ixgbe_determine_xdp_ring(adapter
);
358 ixgbe_xdp_ring_update_tail_locked(ring
);
361 ixgbe_update_rx_ring_stats(rx_ring
, q_vector
, total_rx_packets
,
364 if (xsk_uses_need_wakeup(rx_ring
->xsk_pool
)) {
365 if (failure
|| rx_ring
->next_to_clean
== rx_ring
->next_to_use
)
366 xsk_set_rx_need_wakeup(rx_ring
->xsk_pool
);
368 xsk_clear_rx_need_wakeup(rx_ring
->xsk_pool
);
370 return (int)total_rx_packets
;
372 return failure
? budget
: (int)total_rx_packets
;
375 void ixgbe_xsk_clean_rx_ring(struct ixgbe_ring
*rx_ring
)
377 struct ixgbe_rx_buffer
*bi
;
380 for (i
= 0; i
< rx_ring
->count
; i
++) {
381 bi
= &rx_ring
->rx_buffer_info
[i
];
386 xsk_buff_free(bi
->xdp
);
391 static bool ixgbe_xmit_zc(struct ixgbe_ring
*xdp_ring
, unsigned int budget
)
393 struct xsk_buff_pool
*pool
= xdp_ring
->xsk_pool
;
394 union ixgbe_adv_tx_desc
*tx_desc
= NULL
;
395 struct ixgbe_tx_buffer
*tx_bi
;
396 bool work_done
= true;
397 struct xdp_desc desc
;
401 while (budget
-- > 0) {
402 if (unlikely(!ixgbe_desc_unused(xdp_ring
))) {
407 if (!netif_carrier_ok(xdp_ring
->netdev
))
410 if (!xsk_tx_peek_desc(pool
, &desc
))
413 dma
= xsk_buff_raw_get_dma(pool
, desc
.addr
);
414 xsk_buff_raw_dma_sync_for_device(pool
, dma
, desc
.len
);
416 tx_bi
= &xdp_ring
->tx_buffer_info
[xdp_ring
->next_to_use
];
417 tx_bi
->bytecount
= desc
.len
;
421 tx_desc
= IXGBE_TX_DESC(xdp_ring
, xdp_ring
->next_to_use
);
422 tx_desc
->read
.buffer_addr
= cpu_to_le64(dma
);
424 /* put descriptor type bits */
425 cmd_type
= IXGBE_ADVTXD_DTYP_DATA
|
426 IXGBE_ADVTXD_DCMD_DEXT
|
427 IXGBE_ADVTXD_DCMD_IFCS
;
428 cmd_type
|= desc
.len
| IXGBE_TXD_CMD
;
429 tx_desc
->read
.cmd_type_len
= cpu_to_le32(cmd_type
);
430 tx_desc
->read
.olinfo_status
=
431 cpu_to_le32(desc
.len
<< IXGBE_ADVTXD_PAYLEN_SHIFT
);
433 xdp_ring
->next_to_use
++;
434 if (xdp_ring
->next_to_use
== xdp_ring
->count
)
435 xdp_ring
->next_to_use
= 0;
439 ixgbe_xdp_ring_update_tail(xdp_ring
);
440 xsk_tx_release(pool
);
443 return !!budget
&& work_done
;
446 static void ixgbe_clean_xdp_tx_buffer(struct ixgbe_ring
*tx_ring
,
447 struct ixgbe_tx_buffer
*tx_bi
)
449 xdp_return_frame(tx_bi
->xdpf
);
450 dma_unmap_single(tx_ring
->dev
,
451 dma_unmap_addr(tx_bi
, dma
),
452 dma_unmap_len(tx_bi
, len
), DMA_TO_DEVICE
);
453 dma_unmap_len_set(tx_bi
, len
, 0);
456 bool ixgbe_clean_xdp_tx_irq(struct ixgbe_q_vector
*q_vector
,
457 struct ixgbe_ring
*tx_ring
, int napi_budget
)
459 u16 ntc
= tx_ring
->next_to_clean
, ntu
= tx_ring
->next_to_use
;
460 unsigned int total_packets
= 0, total_bytes
= 0;
461 struct xsk_buff_pool
*pool
= tx_ring
->xsk_pool
;
462 union ixgbe_adv_tx_desc
*tx_desc
;
463 struct ixgbe_tx_buffer
*tx_bi
;
466 tx_bi
= &tx_ring
->tx_buffer_info
[ntc
];
467 tx_desc
= IXGBE_TX_DESC(tx_ring
, ntc
);
470 if (!(tx_desc
->wb
.status
& cpu_to_le32(IXGBE_TXD_STAT_DD
)))
473 total_bytes
+= tx_bi
->bytecount
;
474 total_packets
+= tx_bi
->gso_segs
;
477 ixgbe_clean_xdp_tx_buffer(tx_ring
, tx_bi
);
486 if (unlikely(ntc
== tx_ring
->count
)) {
488 tx_bi
= tx_ring
->tx_buffer_info
;
489 tx_desc
= IXGBE_TX_DESC(tx_ring
, 0);
492 /* issue prefetch for next Tx descriptor */
496 tx_ring
->next_to_clean
= ntc
;
497 ixgbe_update_tx_ring_stats(tx_ring
, q_vector
, total_packets
,
501 xsk_tx_completed(pool
, xsk_frames
);
503 if (xsk_uses_need_wakeup(pool
))
504 xsk_set_tx_need_wakeup(pool
);
506 return ixgbe_xmit_zc(tx_ring
, q_vector
->tx
.work_limit
);
509 int ixgbe_xsk_wakeup(struct net_device
*dev
, u32 qid
, u32 flags
)
511 struct ixgbe_adapter
*adapter
= netdev_priv(dev
);
512 struct ixgbe_ring
*ring
;
514 if (test_bit(__IXGBE_DOWN
, &adapter
->state
))
517 if (!READ_ONCE(adapter
->xdp_prog
))
520 if (qid
>= adapter
->num_xdp_queues
)
523 ring
= adapter
->xdp_ring
[qid
];
525 if (test_bit(__IXGBE_TX_DISABLED
, &ring
->state
))
531 if (!napi_if_scheduled_mark_missed(&ring
->q_vector
->napi
)) {
532 u64 eics
= BIT_ULL(ring
->q_vector
->v_idx
);
534 ixgbe_irq_rearm_queues(adapter
, eics
);
540 void ixgbe_xsk_clean_tx_ring(struct ixgbe_ring
*tx_ring
)
542 u16 ntc
= tx_ring
->next_to_clean
, ntu
= tx_ring
->next_to_use
;
543 struct xsk_buff_pool
*pool
= tx_ring
->xsk_pool
;
544 struct ixgbe_tx_buffer
*tx_bi
;
548 tx_bi
= &tx_ring
->tx_buffer_info
[ntc
];
551 ixgbe_clean_xdp_tx_buffer(tx_ring
, tx_bi
);
558 if (ntc
== tx_ring
->count
)
563 xsk_tx_completed(pool
, xsk_frames
);