1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019, Intel Corporation. */
3 
4 #include "ice_txrx_lib.h"
5 #include "ice_eswitch.h"
6 #include "ice_lib.h"
7 
8 /**
9  * ice_release_rx_desc - Store the new tail and head values
10  * @rx_ring: ring to bump
11  * @val: new head index
12  */
ice_release_rx_desc(struct ice_rx_ring * rx_ring,u16 val)13 void ice_release_rx_desc(struct ice_rx_ring *rx_ring, u16 val)
14 {
15 	u16 prev_ntu = rx_ring->next_to_use & ~0x7;
16 
17 	rx_ring->next_to_use = val;
18 
19 	/* update next to alloc since we have filled the ring */
20 	rx_ring->next_to_alloc = val;
21 
22 	/* QRX_TAIL will be updated with any tail value, but hardware ignores
23 	 * the lower 3 bits. This makes it so we only bump tail on meaningful
24 	 * boundaries. Also, this allows us to bump tail on intervals of 8 up to
25 	 * the budget depending on the current traffic load.
26 	 */
27 	val &= ~0x7;
28 	if (prev_ntu != val) {
29 		/* Force memory writes to complete before letting h/w
30 		 * know there are new descriptors to fetch. (Only
31 		 * applicable for weak-ordered memory model archs,
32 		 * such as IA-64).
33 		 */
34 		wmb();
35 		writel(val, rx_ring->tail);
36 	}
37 }
38 
39 /**
40  * ice_ptype_to_htype - get a hash type
41  * @ptype: the ptype value from the descriptor
42  *
43  * Returns appropriate hash type (such as PKT_HASH_TYPE_L2/L3/L4) to be used by
44  * skb_set_hash based on PTYPE as parsed by HW Rx pipeline and is part of
45  * Rx desc.
46  */
ice_ptype_to_htype(u16 ptype)47 static enum pkt_hash_types ice_ptype_to_htype(u16 ptype)
48 {
49 	struct ice_rx_ptype_decoded decoded = ice_decode_rx_desc_ptype(ptype);
50 
51 	if (!decoded.known)
52 		return PKT_HASH_TYPE_NONE;
53 	if (decoded.payload_layer == ICE_RX_PTYPE_PAYLOAD_LAYER_PAY4)
54 		return PKT_HASH_TYPE_L4;
55 	if (decoded.payload_layer == ICE_RX_PTYPE_PAYLOAD_LAYER_PAY3)
56 		return PKT_HASH_TYPE_L3;
57 	if (decoded.outer_ip == ICE_RX_PTYPE_OUTER_L2)
58 		return PKT_HASH_TYPE_L2;
59 
60 	return PKT_HASH_TYPE_NONE;
61 }
62 
63 /**
64  * ice_rx_hash - set the hash value in the skb
65  * @rx_ring: descriptor ring
66  * @rx_desc: specific descriptor
67  * @skb: pointer to current skb
68  * @rx_ptype: the ptype value from the descriptor
69  */
70 static void
ice_rx_hash(struct ice_rx_ring * rx_ring,union ice_32b_rx_flex_desc * rx_desc,struct sk_buff * skb,u16 rx_ptype)71 ice_rx_hash(struct ice_rx_ring *rx_ring, union ice_32b_rx_flex_desc *rx_desc,
72 	    struct sk_buff *skb, u16 rx_ptype)
73 {
74 	struct ice_32b_rx_flex_desc_nic *nic_mdid;
75 	u32 hash;
76 
77 	if (!(rx_ring->netdev->features & NETIF_F_RXHASH))
78 		return;
79 
80 	if (rx_desc->wb.rxdid != ICE_RXDID_FLEX_NIC)
81 		return;
82 
83 	nic_mdid = (struct ice_32b_rx_flex_desc_nic *)rx_desc;
84 	hash = le32_to_cpu(nic_mdid->rss_hash);
85 	skb_set_hash(skb, hash, ice_ptype_to_htype(rx_ptype));
86 }
87 
88 /**
89  * ice_rx_csum - Indicate in skb if checksum is good
90  * @ring: the ring we care about
91  * @skb: skb currently being received and modified
92  * @rx_desc: the receive descriptor
93  * @ptype: the packet type decoded by hardware
94  *
95  * skb->protocol must be set before this function is called
96  */
97 static void
ice_rx_csum(struct ice_rx_ring * ring,struct sk_buff * skb,union ice_32b_rx_flex_desc * rx_desc,u16 ptype)98 ice_rx_csum(struct ice_rx_ring *ring, struct sk_buff *skb,
99 	    union ice_32b_rx_flex_desc *rx_desc, u16 ptype)
100 {
101 	struct ice_rx_ptype_decoded decoded;
102 	u16 rx_status0, rx_status1;
103 	bool ipv4, ipv6;
104 
105 	rx_status0 = le16_to_cpu(rx_desc->wb.status_error0);
106 	rx_status1 = le16_to_cpu(rx_desc->wb.status_error1);
107 
108 	decoded = ice_decode_rx_desc_ptype(ptype);
109 
110 	/* Start with CHECKSUM_NONE and by default csum_level = 0 */
111 	skb->ip_summed = CHECKSUM_NONE;
112 	skb_checksum_none_assert(skb);
113 
114 	/* check if Rx checksum is enabled */
115 	if (!(ring->netdev->features & NETIF_F_RXCSUM))
116 		return;
117 
118 	/* check if HW has decoded the packet and checksum */
119 	if (!(rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_L3L4P_S)))
120 		return;
121 
122 	if (!(decoded.known && decoded.outer_ip))
123 		return;
124 
125 	ipv4 = (decoded.outer_ip == ICE_RX_PTYPE_OUTER_IP) &&
126 	       (decoded.outer_ip_ver == ICE_RX_PTYPE_OUTER_IPV4);
127 	ipv6 = (decoded.outer_ip == ICE_RX_PTYPE_OUTER_IP) &&
128 	       (decoded.outer_ip_ver == ICE_RX_PTYPE_OUTER_IPV6);
129 
130 	if (ipv4 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_IPE_S) |
131 				   BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_EIPE_S))))
132 		goto checksum_fail;
133 
134 	if (ipv6 && (rx_status0 & (BIT(ICE_RX_FLEX_DESC_STATUS0_IPV6EXADD_S))))
135 		goto checksum_fail;
136 
137 	/* check for L4 errors and handle packets that were not able to be
138 	 * checksummed due to arrival speed
139 	 */
140 	if (rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_L4E_S))
141 		goto checksum_fail;
142 
143 	/* check for outer UDP checksum error in tunneled packets */
144 	if ((rx_status1 & BIT(ICE_RX_FLEX_DESC_STATUS1_NAT_S)) &&
145 	    (rx_status0 & BIT(ICE_RX_FLEX_DESC_STATUS0_XSUM_EUDPE_S)))
146 		goto checksum_fail;
147 
148 	/* If there is an outer header present that might contain a checksum
149 	 * we need to bump the checksum level by 1 to reflect the fact that
150 	 * we are indicating we validated the inner checksum.
151 	 */
152 	if (decoded.tunnel_type >= ICE_RX_PTYPE_TUNNEL_IP_GRENAT)
153 		skb->csum_level = 1;
154 
155 	/* Only report checksum unnecessary for TCP, UDP, or SCTP */
156 	switch (decoded.inner_prot) {
157 	case ICE_RX_PTYPE_INNER_PROT_TCP:
158 	case ICE_RX_PTYPE_INNER_PROT_UDP:
159 	case ICE_RX_PTYPE_INNER_PROT_SCTP:
160 		skb->ip_summed = CHECKSUM_UNNECESSARY;
161 		break;
162 	default:
163 		break;
164 	}
165 	return;
166 
167 checksum_fail:
168 	ring->vsi->back->hw_csum_rx_error++;
169 }
170 
171 /**
172  * ice_process_skb_fields - Populate skb header fields from Rx descriptor
173  * @rx_ring: Rx descriptor ring packet is being transacted on
174  * @rx_desc: pointer to the EOP Rx descriptor
175  * @skb: pointer to current skb being populated
176  * @ptype: the packet type decoded by hardware
177  *
178  * This function checks the ring, descriptor, and packet information in
179  * order to populate the hash, checksum, VLAN, protocol, and
180  * other fields within the skb.
181  */
182 void
ice_process_skb_fields(struct ice_rx_ring * rx_ring,union ice_32b_rx_flex_desc * rx_desc,struct sk_buff * skb,u16 ptype)183 ice_process_skb_fields(struct ice_rx_ring *rx_ring,
184 		       union ice_32b_rx_flex_desc *rx_desc,
185 		       struct sk_buff *skb, u16 ptype)
186 {
187 	ice_rx_hash(rx_ring, rx_desc, skb, ptype);
188 
189 	/* modifies the skb - consumes the enet header */
190 	skb->protocol = eth_type_trans(skb, ice_eswitch_get_target_netdev
191 				       (rx_ring, rx_desc));
192 
193 	ice_rx_csum(rx_ring, skb, rx_desc, ptype);
194 
195 	if (rx_ring->ptp_rx)
196 		ice_ptp_rx_hwtstamp(rx_ring, rx_desc, skb);
197 }
198 
199 /**
200  * ice_receive_skb - Send a completed packet up the stack
201  * @rx_ring: Rx ring in play
202  * @skb: packet to send up
203  * @vlan_tag: VLAN tag for packet
204  *
205  * This function sends the completed packet (via. skb) up the stack using
206  * gro receive functions (with/without VLAN tag)
207  */
208 void
ice_receive_skb(struct ice_rx_ring * rx_ring,struct sk_buff * skb,u16 vlan_tag)209 ice_receive_skb(struct ice_rx_ring *rx_ring, struct sk_buff *skb, u16 vlan_tag)
210 {
211 	if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
212 	    (vlan_tag & VLAN_VID_MASK))
213 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
214 	napi_gro_receive(&rx_ring->q_vector->napi, skb);
215 }
216 
217 /**
218  * ice_clean_xdp_irq - Reclaim resources after transmit completes on XDP ring
219  * @xdp_ring: XDP ring to clean
220  */
ice_clean_xdp_irq(struct ice_tx_ring * xdp_ring)221 static void ice_clean_xdp_irq(struct ice_tx_ring *xdp_ring)
222 {
223 	unsigned int total_bytes = 0, total_pkts = 0;
224 	u16 ntc = xdp_ring->next_to_clean;
225 	struct ice_tx_desc *next_dd_desc;
226 	u16 next_dd = xdp_ring->next_dd;
227 	struct ice_tx_buf *tx_buf;
228 	int i;
229 
230 	next_dd_desc = ICE_TX_DESC(xdp_ring, next_dd);
231 	if (!(next_dd_desc->cmd_type_offset_bsz &
232 	    cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)))
233 		return;
234 
235 	for (i = 0; i < ICE_TX_THRESH; i++) {
236 		tx_buf = &xdp_ring->tx_buf[ntc];
237 
238 		total_bytes += tx_buf->bytecount;
239 		/* normally tx_buf->gso_segs was taken but at this point
240 		 * it's always 1 for us
241 		 */
242 		total_pkts++;
243 
244 		page_frag_free(tx_buf->raw_buf);
245 		dma_unmap_single(xdp_ring->dev, dma_unmap_addr(tx_buf, dma),
246 				 dma_unmap_len(tx_buf, len), DMA_TO_DEVICE);
247 		dma_unmap_len_set(tx_buf, len, 0);
248 		tx_buf->raw_buf = NULL;
249 
250 		ntc++;
251 		if (ntc >= xdp_ring->count)
252 			ntc = 0;
253 	}
254 
255 	next_dd_desc->cmd_type_offset_bsz = 0;
256 	xdp_ring->next_dd = xdp_ring->next_dd + ICE_TX_THRESH;
257 	if (xdp_ring->next_dd > xdp_ring->count)
258 		xdp_ring->next_dd = ICE_TX_THRESH - 1;
259 	xdp_ring->next_to_clean = ntc;
260 	ice_update_tx_ring_stats(xdp_ring, total_pkts, total_bytes);
261 }
262 
263 /**
264  * ice_xmit_xdp_ring - submit single packet to XDP ring for transmission
265  * @data: packet data pointer
266  * @size: packet data size
267  * @xdp_ring: XDP ring for transmission
268  */
ice_xmit_xdp_ring(void * data,u16 size,struct ice_tx_ring * xdp_ring)269 int ice_xmit_xdp_ring(void *data, u16 size, struct ice_tx_ring *xdp_ring)
270 {
271 	u16 i = xdp_ring->next_to_use;
272 	struct ice_tx_desc *tx_desc;
273 	struct ice_tx_buf *tx_buf;
274 	dma_addr_t dma;
275 
276 	if (ICE_DESC_UNUSED(xdp_ring) < ICE_TX_THRESH)
277 		ice_clean_xdp_irq(xdp_ring);
278 
279 	if (!unlikely(ICE_DESC_UNUSED(xdp_ring))) {
280 		xdp_ring->tx_stats.tx_busy++;
281 		return ICE_XDP_CONSUMED;
282 	}
283 
284 	dma = dma_map_single(xdp_ring->dev, data, size, DMA_TO_DEVICE);
285 	if (dma_mapping_error(xdp_ring->dev, dma))
286 		return ICE_XDP_CONSUMED;
287 
288 	tx_buf = &xdp_ring->tx_buf[i];
289 	tx_buf->bytecount = size;
290 	tx_buf->gso_segs = 1;
291 	tx_buf->raw_buf = data;
292 
293 	/* record length, and DMA address */
294 	dma_unmap_len_set(tx_buf, len, size);
295 	dma_unmap_addr_set(tx_buf, dma, dma);
296 
297 	tx_desc = ICE_TX_DESC(xdp_ring, i);
298 	tx_desc->buf_addr = cpu_to_le64(dma);
299 	tx_desc->cmd_type_offset_bsz = ice_build_ctob(ICE_TX_DESC_CMD_EOP, 0,
300 						      size, 0);
301 
302 	i++;
303 	if (i == xdp_ring->count) {
304 		i = 0;
305 		tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_rs);
306 		tx_desc->cmd_type_offset_bsz |=
307 			cpu_to_le64(ICE_TX_DESC_CMD_RS << ICE_TXD_QW1_CMD_S);
308 		xdp_ring->next_rs = ICE_TX_THRESH - 1;
309 	}
310 	xdp_ring->next_to_use = i;
311 
312 	if (i > xdp_ring->next_rs) {
313 		tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_rs);
314 		tx_desc->cmd_type_offset_bsz |=
315 			cpu_to_le64(ICE_TX_DESC_CMD_RS << ICE_TXD_QW1_CMD_S);
316 		xdp_ring->next_rs += ICE_TX_THRESH;
317 	}
318 
319 	return ICE_XDP_TX;
320 }
321 
322 /**
323  * ice_xmit_xdp_buff - convert an XDP buffer to an XDP frame and send it
324  * @xdp: XDP buffer
325  * @xdp_ring: XDP Tx ring
326  *
327  * Returns negative on failure, 0 on success.
328  */
ice_xmit_xdp_buff(struct xdp_buff * xdp,struct ice_tx_ring * xdp_ring)329 int ice_xmit_xdp_buff(struct xdp_buff *xdp, struct ice_tx_ring *xdp_ring)
330 {
331 	struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
332 
333 	if (unlikely(!xdpf))
334 		return ICE_XDP_CONSUMED;
335 
336 	return ice_xmit_xdp_ring(xdpf->data, xdpf->len, xdp_ring);
337 }
338 
339 /**
340  * ice_finalize_xdp_rx - Bump XDP Tx tail and/or flush redirect map
341  * @xdp_ring: XDP ring
342  * @xdp_res: Result of the receive batch
343  *
344  * This function bumps XDP Tx tail and/or flush redirect map, and
345  * should be called when a batch of packets has been processed in the
346  * napi loop.
347  */
ice_finalize_xdp_rx(struct ice_tx_ring * xdp_ring,unsigned int xdp_res)348 void ice_finalize_xdp_rx(struct ice_tx_ring *xdp_ring, unsigned int xdp_res)
349 {
350 	if (xdp_res & ICE_XDP_REDIR)
351 		xdp_do_flush_map();
352 
353 	if (xdp_res & ICE_XDP_TX) {
354 		if (static_branch_unlikely(&ice_xdp_locking_key))
355 			spin_lock(&xdp_ring->tx_lock);
356 		ice_xdp_ring_update_tail(xdp_ring);
357 		if (static_branch_unlikely(&ice_xdp_locking_key))
358 			spin_unlock(&xdp_ring->tx_lock);
359 	}
360 }
361