1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
4 #include "ice_txrx_lib.h"
7 * ice_release_rx_desc - Store the new tail and head values
8 * @rx_ring: ring to bump
11 void ice_release_rx_desc(struct ice_ring *rx_ring, u16 val)
13 u16 prev_ntu = rx_ring->next_to_use & ~0x7;
15 rx_ring->next_to_use = val;
17 /* update next to alloc since we have filled the ring */
18 rx_ring->next_to_alloc = val;
20 /* QRX_TAIL will be updated with any tail value, but hardware ignores
21 * the lower 3 bits. This makes it so we only bump tail on meaningful
22 * boundaries. Also, this allows us to bump tail on intervals of 8 up to
23 * the budget depending on the current traffic load.
26 if (prev_ntu != val) {
27 /* Force memory writes to complete before letting h/w
28 * know there are new descriptors to fetch. (Only
29 * applicable for weak-ordered memory model archs,
33 writel(val, rx_ring->tail);
38 * ice_ptype_to_htype - get a hash type
39 * @ptype: the ptype value from the descriptor
41 * Returns appropriate hash type (such as PKT_HASH_TYPE_L2/L3/L4) to be used by
42 * skb_set_hash based on PTYPE as parsed by HW Rx pipeline and is part of
45 static enum pkt_hash_types ice_ptype_to_htype(u16 ptype)
47 struct ice_rx_ptype_decoded decoded = ice_decode_rx_desc_ptype(ptype);
50 return PKT_HASH_TYPE_NONE;
51 if (decoded.payload_layer == ICE_RX_PTYPE_PAYLOAD_LAYER_PAY4)
52 return PKT_HASH_TYPE_L4;
53 if (decoded.payload_layer == ICE_RX_PTYPE_PAYLOAD_LAYER_PAY3)
54 return PKT_HASH_TYPE_L3;
55 if (decoded.outer_ip == ICE_RX_PTYPE_OUTER_L2)
56 return PKT_HASH_TYPE_L2;
58 return PKT_HASH_TYPE_NONE;
62 * ice_rx_hash - set the hash value in the skb
63 * @rx_ring: descriptor ring
64 * @rx_desc: specific descriptor
65 * @skb: pointer to current skb
66 * @rx_ptype: the ptype value from the descriptor
69 ice_rx_hash(struct ice_ring *rx_ring, union ice_32b_rx_flex_desc *rx_desc,
70 struct sk_buff *skb, u16 rx_ptype)
72 struct ice_32b_rx_flex_desc_nic *nic_mdid;
75 if (!(rx_ring->netdev->features & NETIF_F_RXHASH))
78 if (rx_desc->wb.rxdid != ICE_RXDID_FLEX_NIC)
81 nic_mdid = (struct ice_32b_rx_flex_desc_nic *)rx_desc;
82 hash = le32_to_cpu(nic_mdid->rss_hash);
83 skb_set_hash(skb, hash, ice_ptype_to_htype(rx_ptype));
87 * ice_rx_csum - Indicate in skb if checksum is good
88 * @ring: the ring we care about
89 * @skb: skb currently being received and modified
90 * @rx_desc: the receive descriptor
91 * @ptype: the packet type decoded by hardware
93 * skb->protocol must be set before this function is called
96 ice_rx_csum(struct ice_ring *ring, struct sk_buff *skb,
97 union ice_32b_rx_flex_desc *rx_desc, u16 ptype)
99 struct ice_rx_ptype_decoded decoded;
100 u16 rx_status0, rx_status1;
103 rx_status0 = le16_to_cpu(rx_desc->wb.status_error0);
104 rx_status1 = le16_to_cpu(rx_desc->wb.status_error1);
106 decoded = ice_decode_rx_desc_ptype(ptype);
108 /* Start with CHECKSUM_NONE and by default csum_level = 0 */
109 skb->ip_summed = CHECKSUM_NONE;
110 skb_checksum_none_assert(skb);
112 /* check if Rx checksum is enabled */
113 if (!(ring->netdev->features & NETIF_F_RXCSUM))
116 /* check if HW has decoded the packet and checksum */
117 if (!(rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_L3L4P_S)))
120 if (!(decoded.known && decoded.outer_ip))
123 ipv4 = (decoded.outer_ip == ICE_RX_PTYPE_OUTER_IP) &&
124 (decoded.outer_ip_ver == ICE_RX_PTYPE_OUTER_IPV4);
125 ipv6 = (decoded.outer_ip == ICE_RX_PTYPE_OUTER_IP) &&
126 (decoded.outer_ip_ver == ICE_RX_PTYPE_OUTER_IPV6);
128 if (ipv4 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_IPE_S) |
129 BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_EIPE_S))))
132 if (ipv6 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_IPV6EXADD_S))))
135 /* check for L4 errors and handle packets that were not able to be
136 * checksummed due to arrival speed
138 if (rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_L4E_S))
141 /* check for outer UDP checksum error in tunneled packets */
142 if ((rx_status1 & BIT(ICE_RX_FLEX_DESC_STATUS1_NAT_S)) &&
143 (rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_EUDPE_S)))
146 /* If there is an outer header present that might contain a checksum
147 * we need to bump the checksum level by 1 to reflect the fact that
148 * we are indicating we validated the inner checksum.
150 if (decoded.tunnel_type >= ICE_RX_PTYPE_TUNNEL_IP_GRENAT)
153 /* Only report checksum unnecessary for TCP, UDP, or SCTP */
154 switch (decoded.inner_prot) {
155 case ICE_RX_PTYPE_INNER_PROT_TCP:
156 case ICE_RX_PTYPE_INNER_PROT_UDP:
157 case ICE_RX_PTYPE_INNER_PROT_SCTP:
158 skb->ip_summed = CHECKSUM_UNNECESSARY;
166 ring->vsi->back->hw_csum_rx_error++;
170 * ice_process_skb_fields - Populate skb header fields from Rx descriptor
171 * @rx_ring: Rx descriptor ring packet is being transacted on
172 * @rx_desc: pointer to the EOP Rx descriptor
173 * @skb: pointer to current skb being populated
174 * @ptype: the packet type decoded by hardware
176 * This function checks the ring, descriptor, and packet information in
177 * order to populate the hash, checksum, VLAN, protocol, and
178 * other fields within the skb.
181 ice_process_skb_fields(struct ice_ring *rx_ring,
182 union ice_32b_rx_flex_desc *rx_desc,
183 struct sk_buff *skb, u16 ptype)
185 ice_rx_hash(rx_ring, rx_desc, skb, ptype);
187 /* modifies the skb - consumes the enet header */
188 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
190 ice_rx_csum(rx_ring, skb, rx_desc, ptype);
193 ice_ptp_rx_hwtstamp(rx_ring, rx_desc, skb);
197 * ice_receive_skb - Send a completed packet up the stack
198 * @rx_ring: Rx ring in play
199 * @skb: packet to send up
200 * @vlan_tag: VLAN tag for packet
202 * This function sends the completed packet (via. skb) up the stack using
203 * gro receive functions (with/without VLAN tag)
206 ice_receive_skb(struct ice_ring *rx_ring, struct sk_buff *skb, u16 vlan_tag)
208 if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
209 (vlan_tag & VLAN_VID_MASK))
210 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
211 napi_gro_receive(&rx_ring->q_vector->napi, skb);
215 * ice_xmit_xdp_ring - submit single packet to XDP ring for transmission
216 * @data: packet data pointer
217 * @size: packet data size
218 * @xdp_ring: XDP ring for transmission
220 int ice_xmit_xdp_ring(void *data, u16 size, struct ice_ring *xdp_ring)
222 u16 i = xdp_ring->next_to_use;
223 struct ice_tx_desc *tx_desc;
224 struct ice_tx_buf *tx_buf;
227 if (!unlikely(ICE_DESC_UNUSED(xdp_ring))) {
228 xdp_ring->tx_stats.tx_busy++;
229 return ICE_XDP_CONSUMED;
232 dma = dma_map_single(xdp_ring->dev, data, size, DMA_TO_DEVICE);
233 if (dma_mapping_error(xdp_ring->dev, dma))
234 return ICE_XDP_CONSUMED;
236 tx_buf = &xdp_ring->tx_buf[i];
237 tx_buf->bytecount = size;
238 tx_buf->gso_segs = 1;
239 tx_buf->raw_buf = data;
241 /* record length, and DMA address */
242 dma_unmap_len_set(tx_buf, len, size);
243 dma_unmap_addr_set(tx_buf, dma, dma);
245 tx_desc = ICE_TX_DESC(xdp_ring, i);
246 tx_desc->buf_addr = cpu_to_le64(dma);
247 tx_desc->cmd_type_offset_bsz = ice_build_ctob(ICE_TXD_LAST_DESC_CMD, 0,
250 /* Make certain all of the status bits have been updated
251 * before next_to_watch is written.
256 if (i == xdp_ring->count)
259 tx_buf->next_to_watch = tx_desc;
260 xdp_ring->next_to_use = i;
266 * ice_xmit_xdp_buff - convert an XDP buffer to an XDP frame and send it
268 * @xdp_ring: XDP Tx ring
270 * Returns negative on failure, 0 on success.
272 int ice_xmit_xdp_buff(struct xdp_buff *xdp, struct ice_ring *xdp_ring)
274 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
277 return ICE_XDP_CONSUMED;
279 return ice_xmit_xdp_ring(xdpf->data, xdpf->len, xdp_ring);
283 * ice_finalize_xdp_rx - Bump XDP Tx tail and/or flush redirect map
285 * @xdp_res: Result of the receive batch
287 * This function bumps XDP Tx tail and/or flush redirect map, and
288 * should be called when a batch of packets has been processed in the
291 void ice_finalize_xdp_rx(struct ice_ring *rx_ring, unsigned int xdp_res)
293 if (xdp_res & ICE_XDP_REDIR)
296 if (xdp_res & ICE_XDP_TX) {
297 struct ice_ring *xdp_ring =
298 rx_ring->vsi->xdp_rings[rx_ring->q_index];
300 ice_xdp_ring_update_tail(xdp_ring);