Debian Codegraph @

drivers/net/ethernet/intel/ice/ice_txrx_lib.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
3
4 #include <linux/filter.h>
5 #include <linux/net/intel/libie/rx.h>
6 #include <net/libeth/xdp.h>
7
8 #include "ice_txrx_lib.h"
9 #include "ice_eswitch.h"
10 #include "ice_lib.h"
11
12 /**
13 * ice_release_rx_desc - Store the new tail and head values
14 * @rx_ring: ring to bump
15 * @val: new head index
16 */
17 void ice_release_rx_desc(struct ice_rx_ring *rx_ring, u16 val)
18 {
19 u16 prev_ntu = rx_ring->next_to_use & ~0x7;
20
21 rx_ring->next_to_use = val;
22
23 /* QRX_TAIL will be updated with any tail value, but hardware ignores
24 * the lower 3 bits. This makes it so we only bump tail on meaningful
25 * boundaries. Also, this allows us to bump tail on intervals of 8 up to
26 * the budget depending on the current traffic load.
27 */
28 val &= ~0x7;
29 if (prev_ntu != val) {
30 /* Force memory writes to complete before letting h/w
31 * know there are new descriptors to fetch. (Only
32 * applicable for weak-ordered memory model archs,
33 * such as IA-64).
34 */
35 wmb();
36 writel(val, rx_ring->tail);
37 }
38 }
39
40 /**
41 * ice_get_rx_hash - get RX hash value from descriptor
42 * @rx_desc: specific descriptor
43 *
44 * Returns hash, if present, 0 otherwise.
45 */
46 static u32 ice_get_rx_hash(const union ice_32b_rx_flex_desc *rx_desc)
47 {
48 const struct ice_32b_rx_flex_desc_nic *nic_mdid;
49
50 if (unlikely(rx_desc->wb.rxdid != ICE_RXDID_FLEX_NIC))
51 return 0;
52
53 nic_mdid = (struct ice_32b_rx_flex_desc_nic *)rx_desc;
54 return le32_to_cpu(nic_mdid->rss_hash);
55 }
56
57 /**
58 * ice_rx_hash_to_skb - set the hash value in the skb
59 * @rx_ring: descriptor ring
60 * @rx_desc: specific descriptor
61 * @skb: pointer to current skb
62 * @rx_ptype: the ptype value from the descriptor
63 */
64 static void
65 ice_rx_hash_to_skb(const struct ice_rx_ring *rx_ring,
66 const union ice_32b_rx_flex_desc *rx_desc,
67 struct sk_buff *skb, u16 rx_ptype)
68 {
69 struct libeth_rx_pt decoded;
70 u32 hash;
71
72 decoded = libie_rx_pt_parse(rx_ptype);
73 if (!libeth_rx_pt_has_hash(rx_ring->netdev, decoded))
74 return;
75
76 hash = ice_get_rx_hash(rx_desc);
77 if (likely(hash))
78 libeth_rx_pt_set_hash(skb, hash, decoded);
79 }
80
81 /**
82 * ice_rx_gcs - Set generic checksum in skb
83 * @skb: skb currently being received and modified
84 * @rx_desc: receive descriptor
85 */
86 static void ice_rx_gcs(struct sk_buff *skb,
87 const union ice_32b_rx_flex_desc *rx_desc)
88 {
89 const struct ice_32b_rx_flex_desc_nic *desc;
90 u16 csum;
91
92 desc = (struct ice_32b_rx_flex_desc_nic *)rx_desc;
93 skb->ip_summed = CHECKSUM_COMPLETE;
94 csum = (__force u16)desc->raw_csum;
95 skb->csum = csum_unfold((__force __sum16)swab16(csum));
96 }
97
98 /**
99 * ice_rx_csum - Indicate in skb if checksum is good
100 * @ring: the ring we care about
101 * @skb: skb currently being received and modified
102 * @rx_desc: the receive descriptor
103 * @ptype: the packet type decoded by hardware
104 *
105 * skb->protocol must be set before this function is called
106 */
107 static void
108 ice_rx_csum(struct ice_rx_ring *ring, struct sk_buff *skb,
109 union ice_32b_rx_flex_desc *rx_desc, u16 ptype)
110 {
111 struct libeth_rx_pt decoded;
112 u16 rx_status0, rx_status1;
113 bool ipv4, ipv6;
114
115 /* Start with CHECKSUM_NONE and by default csum_level = 0 */
116 skb->ip_summed = CHECKSUM_NONE;
117
118 decoded = libie_rx_pt_parse(ptype);
119 if (!libeth_rx_pt_has_checksum(ring->netdev, decoded))
120 return;
121
122 rx_status0 = le16_to_cpu(rx_desc->wb.status_error0);
123 rx_status1 = le16_to_cpu(rx_desc->wb.status_error1);
124
125 if ((ring->flags & ICE_RX_FLAGS_RING_GCS) &&
126 rx_desc->wb.rxdid == ICE_RXDID_FLEX_NIC &&
127 (decoded.inner_prot == LIBETH_RX_PT_INNER_TCP ||
128 decoded.inner_prot == LIBETH_RX_PT_INNER_UDP ||
129 decoded.inner_prot == LIBETH_RX_PT_INNER_ICMP)) {
130 ice_rx_gcs(skb, rx_desc);
131 return;
132 }
133
134 /* check if HW has decoded the packet and checksum */
135 if (!(rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_L3L4P_S)))
136 return;
137
138 ipv4 = libeth_rx_pt_get_ip_ver(decoded) == LIBETH_RX_PT_OUTER_IPV4;
139 ipv6 = libeth_rx_pt_get_ip_ver(decoded) == LIBETH_RX_PT_OUTER_IPV6;
140
141 if (ipv4 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_EIPE_S)))) {
142 ring->vsi->back->hw_rx_eipe_error++;
143 return;
144 }
145
146 if (ipv4 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_IPE_S))))
147 goto checksum_fail;
148
149 if (ipv6 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_IPV6EXADD_S))))
150 goto checksum_fail;
151
152 /* check for L4 errors and handle packets that were not able to be
153 * checksummed due to arrival speed
154 */
155 if (rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_L4E_S))
156 goto checksum_fail;
157
158 /* check for outer UDP checksum error in tunneled packets */
159 if ((rx_status1 & BIT(ICE_RX_FLEX_DESC_STATUS1_NAT_S)) &&
160 (rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_EUDPE_S)))
161 goto checksum_fail;
162
163 /* If there is an outer header present that might contain a checksum
164 * we need to bump the checksum level by 1 to reflect the fact that
165 * we are indicating we validated the inner checksum.
166 */
167 if (decoded.tunnel_type >= LIBETH_RX_PT_TUNNEL_IP_GRENAT)
168 skb->csum_level = 1;
169
170 skb->ip_summed = CHECKSUM_UNNECESSARY;
171 return;
172
173 checksum_fail:
174 ring->vsi->back->hw_csum_rx_error++;
175 }
176
177 /**
178 * ice_ptp_rx_hwts_to_skb - Put RX timestamp into skb
179 * @rx_ring: Ring to get the VSI info
180 * @rx_desc: Receive descriptor
181 * @skb: Particular skb to send timestamp with
182 *
183 * The timestamp is in ns, so we must convert the result first.
184 */
185 static void
186 ice_ptp_rx_hwts_to_skb(struct ice_rx_ring *rx_ring,
187 const union ice_32b_rx_flex_desc *rx_desc,
188 struct sk_buff *skb)
189 {
190 u64 ts_ns = ice_ptp_get_rx_hwts(rx_desc, &rx_ring->pkt_ctx);
191
192 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ts_ns);
193 }
194
195 /**
196 * ice_get_ptype - Read HW packet type from the descriptor
197 * @rx_desc: RX descriptor
198 */
199 static u16 ice_get_ptype(const union ice_32b_rx_flex_desc *rx_desc)
200 {
201 return le16_to_cpu(rx_desc->wb.ptype_flex_flags0) &
202 ICE_RX_FLEX_DESC_PTYPE_M;
203 }
204
205 /**
206 * ice_process_skb_fields - Populate skb header fields from Rx descriptor
207 * @rx_ring: Rx descriptor ring packet is being transacted on
208 * @rx_desc: pointer to the EOP Rx descriptor
209 * @skb: pointer to current skb being populated
210 *
211 * This function checks the ring, descriptor, and packet information in
212 * order to populate the hash, checksum, VLAN, protocol, and
213 * other fields within the skb.
214 */
215 void
216 ice_process_skb_fields(struct ice_rx_ring *rx_ring,
217 union ice_32b_rx_flex_desc *rx_desc,
218 struct sk_buff *skb)
219 {
220 u16 ptype = ice_get_ptype(rx_desc);
221
222 ice_rx_hash_to_skb(rx_ring, rx_desc, skb, ptype);
223
224 /* modifies the skb - consumes the enet header */
225 if (unlikely(rx_ring->flags & ICE_RX_FLAGS_MULTIDEV)) {
226 struct net_device *netdev = ice_eswitch_get_target(rx_ring,
227 rx_desc);
228
229 if (ice_is_port_repr_netdev(netdev))
230 ice_repr_inc_rx_stats(netdev, skb->len);
231
232 /* __skb_push() is needed because xdp_build_skb_from_buff()
233 * calls eth_type_trans()
234 */
235 __skb_push(skb, ETH_HLEN);
236 skb->protocol = eth_type_trans(skb, netdev);
237 }
238
239 ice_rx_csum(rx_ring, skb, rx_desc, ptype);
240
241 if (rx_ring->ptp_rx)
242 ice_ptp_rx_hwts_to_skb(rx_ring, rx_desc, skb);
243 }
244
245 /**
246 * ice_receive_skb - Send a completed packet up the stack
247 * @rx_ring: Rx ring in play
248 * @skb: packet to send up
249 * @vlan_tci: VLAN TCI for packet
250 *
251 * This function sends the completed packet (via. skb) up the stack using
252 * gro receive functions (with/without VLAN tag)
253 */
254 void
255 ice_receive_skb(struct ice_rx_ring *rx_ring, struct sk_buff *skb, u16 vlan_tci)
256 {
257 if ((vlan_tci & VLAN_VID_MASK) && rx_ring->vlan_proto)
258 __vlan_hwaccel_put_tag(skb, rx_ring->vlan_proto,
259 vlan_tci);
260
261 napi_gro_receive(&rx_ring->q_vector->napi, skb);
262 }
263
264 /**
265 * ice_clean_xdp_tx_buf - Free and unmap XDP Tx buffer
266 * @dev: device for DMA mapping
267 * @tx_buf: Tx buffer to clean
268 * @bq: XDP bulk flush struct
269 */
270 static void
271 ice_clean_xdp_tx_buf(struct device *dev, struct ice_tx_buf *tx_buf,
272 struct xdp_frame_bulk *bq)
273 {
274 switch (tx_buf->type) {
275 case ICE_TX_BUF_XDP_TX:
276 libeth_xdp_return_va(tx_buf->raw_buf, true);
277 break;
278 case ICE_TX_BUF_XDP_XMIT:
279 dma_unmap_single(dev, dma_unmap_addr(tx_buf, dma),
280 dma_unmap_len(tx_buf, len), DMA_TO_DEVICE);
281 xdp_return_frame_bulk(tx_buf->xdpf, bq);
282 break;
283 }
284
285 dma_unmap_len_set(tx_buf, len, 0);
286 tx_buf->type = ICE_TX_BUF_EMPTY;
287 }
288
289 /**
290 * ice_clean_xdp_irq - Reclaim resources after transmit completes on XDP ring
291 * @xdp_ring: XDP ring to clean
292 */
293 static u32 ice_clean_xdp_irq(struct ice_tx_ring *xdp_ring)
294 {
295 int total_bytes = 0, total_pkts = 0;
296 struct device *dev = xdp_ring->dev;
297 u32 ntc = xdp_ring->next_to_clean;
298 struct ice_tx_desc *tx_desc;
299 u32 cnt = xdp_ring->count;
300 struct xdp_frame_bulk bq;
301 u32 frags, xdp_tx = 0;
302 u32 ready_frames = 0;
303 u32 idx;
304 u32 ret;
305
306 idx = xdp_ring->tx_buf[ntc].rs_idx;
307 tx_desc = ICE_TX_DESC(xdp_ring, idx);
308 if (tx_desc->cmd_type_offset_bsz &
309 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)) {
310 if (idx >= ntc)
311 ready_frames = idx - ntc + 1;
312 else
313 ready_frames = idx + cnt - ntc + 1;
314 }
315
316 if (unlikely(!ready_frames))
317 return 0;
318 ret = ready_frames;
319
320 xdp_frame_bulk_init(&bq);
321 rcu_read_lock(); /* xdp_return_frame_bulk() */
322
323 while (ready_frames) {
324 struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[ntc];
325 struct ice_tx_buf *head = tx_buf;
326
327 /* bytecount holds size of head + frags */
328 total_bytes += tx_buf->bytecount;
329 frags = tx_buf->nr_frags;
330 total_pkts++;
331 /* count head + frags */
332 ready_frames -= frags + 1;
333 xdp_tx++;
334
335 ntc++;
336 if (ntc == cnt)
337 ntc = 0;
338
339 for (int i = 0; i < frags; i++) {
340 tx_buf = &xdp_ring->tx_buf[ntc];
341
342 ice_clean_xdp_tx_buf(dev, tx_buf, &bq);
343 ntc++;
344 if (ntc == cnt)
345 ntc = 0;
346 }
347
348 ice_clean_xdp_tx_buf(dev, head, &bq);
349 }
350
351 xdp_flush_frame_bulk(&bq);
352 rcu_read_unlock();
353
354 tx_desc->cmd_type_offset_bsz = 0;
355 xdp_ring->next_to_clean = ntc;
356 xdp_ring->xdp_tx_active -= xdp_tx;
357 ice_update_tx_ring_stats(xdp_ring, total_pkts, total_bytes);
358
359 return ret;
360 }
361
362 /**
363 * __ice_xmit_xdp_ring - submit frame to XDP ring for transmission
364 * @xdp: XDP buffer to be placed onto Tx descriptors
365 * @xdp_ring: XDP ring for transmission
366 * @frame: whether this comes from .ndo_xdp_xmit()
367 */
368 int __ice_xmit_xdp_ring(struct xdp_buff *xdp, struct ice_tx_ring *xdp_ring,
369 bool frame)
370 {
371 struct skb_shared_info *sinfo = NULL;
372 u32 size = xdp->data_end - xdp->data;
373 struct device *dev = xdp_ring->dev;
374 u32 ntu = xdp_ring->next_to_use;
375 struct ice_tx_desc *tx_desc;
376 struct ice_tx_buf *tx_head;
377 struct ice_tx_buf *tx_buf;
378 u32 cnt = xdp_ring->count;
379 void *data = xdp->data;
380 struct page *page;
381 u32 nr_frags = 0;
382 u32 free_space;
383 u32 frag = 0;
384 u32 offset;
385
386 free_space = ICE_DESC_UNUSED(xdp_ring);
387 if (free_space < ICE_RING_QUARTER(xdp_ring))
388 free_space += ice_clean_xdp_irq(xdp_ring);
389
390 if (unlikely(!free_space))
391 goto busy;
392
393 if (unlikely(xdp_buff_has_frags(xdp))) {
394 sinfo = xdp_get_shared_info_from_buff(xdp);
395 nr_frags = sinfo->nr_frags;
396 if (free_space < nr_frags + 1)
397 goto busy;
398 }
399
400 tx_desc = ICE_TX_DESC(xdp_ring, ntu);
401 tx_head = &xdp_ring->tx_buf[ntu];
402 tx_buf = tx_head;
403
404 page = virt_to_page(data);
405 offset = offset_in_page(xdp->data);
406
407 for (;;) {
408 dma_addr_t dma;
409
410 if (frame) {
411 dma = dma_map_single(dev, data, size, DMA_TO_DEVICE);
412 if (dma_mapping_error(dev, dma))
413 goto dma_unmap;
414 tx_buf->type = ICE_TX_BUF_FRAG;
415 } else {
416 dma = page_pool_get_dma_addr(page) + offset;
417 dma_sync_single_for_device(dev, dma, size, DMA_BIDIRECTIONAL);
418 tx_buf->type = ICE_TX_BUF_XDP_TX;
419 tx_buf->raw_buf = data;
420 }
421
422 /* record length, and DMA address */
423 dma_unmap_len_set(tx_buf, len, size);
424 dma_unmap_addr_set(tx_buf, dma, dma);
425
426 tx_desc->buf_addr = cpu_to_le64(dma);
427 tx_desc->cmd_type_offset_bsz = ice_build_ctob(0, 0, size, 0);
428
429 ntu++;
430 if (ntu == cnt)
431 ntu = 0;
432
433 if (frag == nr_frags)
434 break;
435
436 tx_desc = ICE_TX_DESC(xdp_ring, ntu);
437 tx_buf = &xdp_ring->tx_buf[ntu];
438
439 page = skb_frag_page(&sinfo->frags[frag]);
440 offset = skb_frag_off(&sinfo->frags[frag]);
441 data = skb_frag_address(&sinfo->frags[frag]);
442 size = skb_frag_size(&sinfo->frags[frag]);
443 frag++;
444 }
445
446 /* store info about bytecount and frag count in first desc */
447 tx_head->bytecount = xdp_get_buff_len(xdp);
448 tx_head->nr_frags = nr_frags;
449
450 if (frame) {
451 tx_head->type = ICE_TX_BUF_XDP_XMIT;
452 tx_head->xdpf = xdp->data_hard_start;
453 }
454
455 /* update last descriptor from a frame with EOP */
456 tx_desc->cmd_type_offset_bsz |=
457 cpu_to_le64(ICE_TX_DESC_CMD_EOP << ICE_TXD_QW1_CMD_S);
458
459 xdp_ring->xdp_tx_active++;
460 xdp_ring->next_to_use = ntu;
461
462 return ICE_XDP_TX;
463
464 dma_unmap:
465 for (;;) {
466 tx_buf = &xdp_ring->tx_buf[ntu];
467 dma_unmap_page(dev, dma_unmap_addr(tx_buf, dma),
468 dma_unmap_len(tx_buf, len), DMA_TO_DEVICE);
469 dma_unmap_len_set(tx_buf, len, 0);
470 if (tx_buf == tx_head)
471 break;
472
473 if (!ntu)
474 ntu += cnt;
475 ntu--;
476 }
477 return ICE_XDP_CONSUMED;
478
479 busy:
480 ice_stats_inc(xdp_ring->ring_stats, tx_busy);
481
482 return ICE_XDP_CONSUMED;
483 }
484
485 /**
486 * ice_finalize_xdp_rx - Bump XDP Tx tail and/or flush redirect map
487 * @xdp_ring: XDP ring
488 * @xdp_res: Result of the receive batch
489 * @first_idx: index to write from caller
490 *
491 * This function bumps XDP Tx tail and/or flush redirect map, and
492 * should be called when a batch of packets has been processed in the
493 * napi loop.
494 */
495 void ice_finalize_xdp_rx(struct ice_tx_ring *xdp_ring, unsigned int xdp_res,
496 u32 first_idx)
497 {
498 struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[first_idx];
499
500 if (xdp_res & ICE_XDP_REDIR)
501 xdp_do_flush();
502
503 if (xdp_res & ICE_XDP_TX) {
504 if (static_branch_unlikely(&ice_xdp_locking_key))
505 spin_lock(&xdp_ring->tx_lock);
506 /* store index of descriptor with RS bit set in the first
507 * ice_tx_buf of given NAPI batch
508 */
509 tx_buf->rs_idx = ice_set_rs_bit(xdp_ring);
510 ice_xdp_ring_update_tail(xdp_ring);
511 if (static_branch_unlikely(&ice_xdp_locking_key))
512 spin_unlock(&xdp_ring->tx_lock);
513 }
514 }
515
516 /**
517 * ice_xdp_rx_hw_ts - HW timestamp XDP hint handler
518 * @ctx: XDP buff pointer
519 * @ts_ns: destination address
520 *
521 * Copy HW timestamp (if available) to the destination address.
522 */
523 static int ice_xdp_rx_hw_ts(const struct xdp_md *ctx, u64 *ts_ns)
524 {
525 const struct libeth_xdp_buff *xdp_ext = (void *)ctx;
526 struct ice_rx_ring *rx_ring;
527
528 rx_ring = libeth_xdp_buff_to_rq(xdp_ext, typeof(*rx_ring), xdp_rxq);
529
530 *ts_ns = ice_ptp_get_rx_hwts(xdp_ext->desc,
531 &rx_ring->pkt_ctx);
532 if (!*ts_ns)
533 return -ENODATA;
534
535 return 0;
536 }
537
538 /**
539 * ice_xdp_rx_hash_type - Get XDP-specific hash type from the RX descriptor
540 * @eop_desc: End of Packet descriptor
541 */
542 static enum xdp_rss_hash_type
543 ice_xdp_rx_hash_type(const union ice_32b_rx_flex_desc *eop_desc)
544 {
545 return libie_rx_pt_parse(ice_get_ptype(eop_desc)).hash_type;
546 }
547
548 /**
549 * ice_xdp_rx_hash - RX hash XDP hint handler
550 * @ctx: XDP buff pointer
551 * @hash: hash destination address
552 * @rss_type: XDP hash type destination address
553 *
554 * Copy RX hash (if available) and its type to the destination address.
555 */
556 static int ice_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash,
557 enum xdp_rss_hash_type *rss_type)
558 {
559 const struct libeth_xdp_buff *xdp_ext = (void *)ctx;
560
561 *hash = ice_get_rx_hash(xdp_ext->desc);
562 *rss_type = ice_xdp_rx_hash_type(xdp_ext->desc);
563 if (!likely(*hash))
564 return -ENODATA;
565
566 return 0;
567 }
568
569 /**
570 * ice_xdp_rx_vlan_tag - VLAN tag XDP hint handler
571 * @ctx: XDP buff pointer
572 * @vlan_proto: destination address for VLAN protocol
573 * @vlan_tci: destination address for VLAN TCI
574 *
575 * Copy VLAN tag (if was stripped) and corresponding protocol
576 * to the destination address.
577 */
578 static int ice_xdp_rx_vlan_tag(const struct xdp_md *ctx, __be16 *vlan_proto,
579 u16 *vlan_tci)
580 {
581 const struct libeth_xdp_buff *xdp_ext = (void *)ctx;
582 struct ice_rx_ring *rx_ring;
583
584 rx_ring = libeth_xdp_buff_to_rq(xdp_ext, typeof(*rx_ring), xdp_rxq);
585
586 *vlan_proto = rx_ring->pkt_ctx.vlan_proto;
587 if (!*vlan_proto)
588 return -ENODATA;
589
590 *vlan_tci = ice_get_vlan_tci(xdp_ext->desc);
591 if (!*vlan_tci)
592 return -ENODATA;
593
594 return 0;
595 }
596
597 const struct xdp_metadata_ops ice_xdp_md_ops = {
598 .xmo_rx_timestamp = ice_xdp_rx_hw_ts,
599 .xmo_rx_hash = ice_xdp_rx_hash,
600 .xmo_rx_vlan_tag = ice_xdp_rx_vlan_tag,
601 };