File Index
Files and directories
Sign in
FILE

ice_txrx_lib.c

File preview with syntax highlighting.

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