xref: /linux/drivers/net/ethernet/sfc/tx_tso.c (revision a23e1966932464e1c5226cb9ac4ce1d5fc10ba22)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e9117e50SBert Kenward /****************************************************************************
3e9117e50SBert Kenward  * Driver for Solarflare network controllers and boards
4e9117e50SBert Kenward  * Copyright 2005-2006 Fen Systems Ltd.
5e9117e50SBert Kenward  * Copyright 2005-2015 Solarflare Communications Inc.
6e9117e50SBert Kenward  */
7e9117e50SBert Kenward 
8e9117e50SBert Kenward #include <linux/pci.h>
9e9117e50SBert Kenward #include <linux/tcp.h>
10e9117e50SBert Kenward #include <linux/ip.h>
11e9117e50SBert Kenward #include <linux/in.h>
12e9117e50SBert Kenward #include <linux/ipv6.h>
13e9117e50SBert Kenward #include <linux/slab.h>
14e9117e50SBert Kenward #include <net/ipv6.h>
15e9117e50SBert Kenward #include <linux/if_ether.h>
16e9117e50SBert Kenward #include <linux/highmem.h>
17e9117e50SBert Kenward #include <linux/moduleparam.h>
18e9117e50SBert Kenward #include <linux/cache.h>
19e9117e50SBert Kenward #include "net_driver.h"
20e9117e50SBert Kenward #include "efx.h"
21e9117e50SBert Kenward #include "io.h"
22e9117e50SBert Kenward #include "nic.h"
23e9117e50SBert Kenward #include "tx.h"
24e9117e50SBert Kenward #include "workarounds.h"
25e9117e50SBert Kenward #include "ef10_regs.h"
26e9117e50SBert Kenward 
27e9117e50SBert Kenward /* Efx legacy TCP segmentation acceleration.
28e9117e50SBert Kenward  *
2946d1efd8SEdward Cree  * Utilises firmware support to go faster than GSO (but not as fast as TSOv2).
30e9117e50SBert Kenward  *
31e9117e50SBert Kenward  * Requires TX checksum offload support.
32e9117e50SBert Kenward  */
33e9117e50SBert Kenward 
34e9117e50SBert Kenward #define PTR_DIFF(p1, p2)  ((u8 *)(p1) - (u8 *)(p2))
35e9117e50SBert Kenward 
36e9117e50SBert Kenward /**
37e9117e50SBert Kenward  * struct tso_state - TSO state for an SKB
38e9117e50SBert Kenward  * @out_len: Remaining length in current segment
39e9117e50SBert Kenward  * @seqnum: Current sequence number
40e9117e50SBert Kenward  * @ipv4_id: Current IPv4 ID, host endian
41e9117e50SBert Kenward  * @packet_space: Remaining space in current packet
42e9117e50SBert Kenward  * @dma_addr: DMA address of current position
43e9117e50SBert Kenward  * @in_len: Remaining length in current SKB fragment
44e9117e50SBert Kenward  * @unmap_len: Length of SKB fragment
45e9117e50SBert Kenward  * @unmap_addr: DMA address of SKB fragment
46e9117e50SBert Kenward  * @protocol: Network protocol (after any VLAN header)
47e9117e50SBert Kenward  * @ip_off: Offset of IP header
48e9117e50SBert Kenward  * @tcp_off: Offset of TCP header
49e9117e50SBert Kenward  * @header_len: Number of bytes of header
50e9117e50SBert Kenward  * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload
5146d1efd8SEdward Cree  * @header_dma_addr: Header DMA address
5246d1efd8SEdward Cree  * @header_unmap_len: Header DMA mapped length
53e9117e50SBert Kenward  *
54e9117e50SBert Kenward  * The state used during segmentation.  It is put into this data structure
55e9117e50SBert Kenward  * just to make it easy to pass into inline functions.
56e9117e50SBert Kenward  */
57e9117e50SBert Kenward struct tso_state {
58e9117e50SBert Kenward 	/* Output position */
59e9117e50SBert Kenward 	unsigned int out_len;
60e9117e50SBert Kenward 	unsigned int seqnum;
61e9117e50SBert Kenward 	u16 ipv4_id;
62e9117e50SBert Kenward 	unsigned int packet_space;
63e9117e50SBert Kenward 
64e9117e50SBert Kenward 	/* Input position */
65e9117e50SBert Kenward 	dma_addr_t dma_addr;
66e9117e50SBert Kenward 	unsigned int in_len;
67e9117e50SBert Kenward 	unsigned int unmap_len;
68e9117e50SBert Kenward 	dma_addr_t unmap_addr;
69e9117e50SBert Kenward 
70e9117e50SBert Kenward 	__be16 protocol;
71e9117e50SBert Kenward 	unsigned int ip_off;
72e9117e50SBert Kenward 	unsigned int tcp_off;
73e9117e50SBert Kenward 	unsigned int header_len;
74e9117e50SBert Kenward 	unsigned int ip_base_len;
75e9117e50SBert Kenward 	dma_addr_t header_dma_addr;
76e9117e50SBert Kenward 	unsigned int header_unmap_len;
77e9117e50SBert Kenward };
78e9117e50SBert Kenward 
prefetch_ptr(struct efx_tx_queue * tx_queue)79e9117e50SBert Kenward static inline void prefetch_ptr(struct efx_tx_queue *tx_queue)
80e9117e50SBert Kenward {
81e9117e50SBert Kenward 	unsigned int insert_ptr = efx_tx_queue_get_insert_index(tx_queue);
82e9117e50SBert Kenward 	char *ptr;
83e9117e50SBert Kenward 
84e9117e50SBert Kenward 	ptr = (char *) (tx_queue->buffer + insert_ptr);
85e9117e50SBert Kenward 	prefetch(ptr);
86e9117e50SBert Kenward 	prefetch(ptr + 0x80);
87e9117e50SBert Kenward 
88d73e7715SMartin Habets 	ptr = (char *)(((efx_qword_t *)tx_queue->txd.addr) + insert_ptr);
89e9117e50SBert Kenward 	prefetch(ptr);
90e9117e50SBert Kenward 	prefetch(ptr + 0x80);
91e9117e50SBert Kenward }
92e9117e50SBert Kenward 
93e9117e50SBert Kenward /**
94e9117e50SBert Kenward  * efx_tx_queue_insert - push descriptors onto the TX queue
95e9117e50SBert Kenward  * @tx_queue:		Efx TX queue
96e9117e50SBert Kenward  * @dma_addr:		DMA address of fragment
97e9117e50SBert Kenward  * @len:		Length of fragment
98e9117e50SBert Kenward  * @final_buffer:	The final buffer inserted into the queue
99e9117e50SBert Kenward  *
100e9117e50SBert Kenward  * Push descriptors onto the TX queue.
101e9117e50SBert Kenward  */
efx_tx_queue_insert(struct efx_tx_queue * tx_queue,dma_addr_t dma_addr,unsigned int len,struct efx_tx_buffer ** final_buffer)102e9117e50SBert Kenward static void efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
103e9117e50SBert Kenward 				dma_addr_t dma_addr, unsigned int len,
104e9117e50SBert Kenward 				struct efx_tx_buffer **final_buffer)
105e9117e50SBert Kenward {
106e9117e50SBert Kenward 	struct efx_tx_buffer *buffer;
107e9117e50SBert Kenward 	unsigned int dma_len;
108e9117e50SBert Kenward 
109e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(len <= 0);
110e9117e50SBert Kenward 
111e9117e50SBert Kenward 	while (1) {
112e9117e50SBert Kenward 		buffer = efx_tx_queue_get_insert_buffer(tx_queue);
113e9117e50SBert Kenward 		++tx_queue->insert_count;
114e9117e50SBert Kenward 
115e01b16a7SEdward Cree 		EFX_WARN_ON_ONCE_PARANOID(tx_queue->insert_count -
116e9117e50SBert Kenward 					  tx_queue->read_count >=
117e9117e50SBert Kenward 					  tx_queue->efx->txq_entries);
118e9117e50SBert Kenward 
119e9117e50SBert Kenward 		buffer->dma_addr = dma_addr;
120e9117e50SBert Kenward 
121e9117e50SBert Kenward 		dma_len = tx_queue->efx->type->tx_limit_len(tx_queue,
122e9117e50SBert Kenward 				dma_addr, len);
123e9117e50SBert Kenward 
124e9117e50SBert Kenward 		/* If there's space for everything this is our last buffer. */
125e9117e50SBert Kenward 		if (dma_len >= len)
126e9117e50SBert Kenward 			break;
127e9117e50SBert Kenward 
128e9117e50SBert Kenward 		buffer->len = dma_len;
129e9117e50SBert Kenward 		buffer->flags = EFX_TX_BUF_CONT;
130e9117e50SBert Kenward 		dma_addr += dma_len;
131e9117e50SBert Kenward 		len -= dma_len;
132e9117e50SBert Kenward 	}
133e9117e50SBert Kenward 
134e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(!len);
135e9117e50SBert Kenward 	buffer->len = len;
136e9117e50SBert Kenward 	*final_buffer = buffer;
137e9117e50SBert Kenward }
138e9117e50SBert Kenward 
139e9117e50SBert Kenward /*
140e9117e50SBert Kenward  * Verify that our various assumptions about sk_buffs and the conditions
141e9117e50SBert Kenward  * under which TSO will be attempted hold true.  Return the protocol number.
142e9117e50SBert Kenward  */
efx_tso_check_protocol(struct sk_buff * skb)143e9117e50SBert Kenward static __be16 efx_tso_check_protocol(struct sk_buff *skb)
144e9117e50SBert Kenward {
145e9117e50SBert Kenward 	__be16 protocol = skb->protocol;
146e9117e50SBert Kenward 
147e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
148e9117e50SBert Kenward 				  protocol);
149e9117e50SBert Kenward 	if (protocol == htons(ETH_P_8021Q)) {
1501f5020acSVladimir Oltean 		struct vlan_ethhdr *veh = skb_vlan_eth_hdr(skb);
151e9117e50SBert Kenward 
152e9117e50SBert Kenward 		protocol = veh->h_vlan_encapsulated_proto;
153e9117e50SBert Kenward 	}
154e9117e50SBert Kenward 
155e9117e50SBert Kenward 	if (protocol == htons(ETH_P_IP)) {
156e01b16a7SEdward Cree 		EFX_WARN_ON_ONCE_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
157e9117e50SBert Kenward 	} else {
158e01b16a7SEdward Cree 		EFX_WARN_ON_ONCE_PARANOID(protocol != htons(ETH_P_IPV6));
159e01b16a7SEdward Cree 		EFX_WARN_ON_ONCE_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
160e9117e50SBert Kenward 	}
161e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data) +
162e01b16a7SEdward Cree 				   (tcp_hdr(skb)->doff << 2u)) >
163e9117e50SBert Kenward 				  skb_headlen(skb));
164e9117e50SBert Kenward 
165e9117e50SBert Kenward 	return protocol;
166e9117e50SBert Kenward }
167e9117e50SBert Kenward 
168e9117e50SBert Kenward /* Parse the SKB header and initialise state. */
tso_start(struct tso_state * st,struct efx_nic * efx,struct efx_tx_queue * tx_queue,const struct sk_buff * skb)169e9117e50SBert Kenward static int tso_start(struct tso_state *st, struct efx_nic *efx,
170e9117e50SBert Kenward 		     struct efx_tx_queue *tx_queue,
171e9117e50SBert Kenward 		     const struct sk_buff *skb)
172e9117e50SBert Kenward {
173e9117e50SBert Kenward 	struct device *dma_dev = &efx->pci_dev->dev;
174e9117e50SBert Kenward 	unsigned int header_len, in_len;
175e9117e50SBert Kenward 	dma_addr_t dma_addr;
176e9117e50SBert Kenward 
17780bfab79SEric Dumazet 	st->ip_off = skb_network_offset(skb);
17880bfab79SEric Dumazet 	st->tcp_off = skb_transport_offset(skb);
179e9117e50SBert Kenward 	header_len = st->tcp_off + (tcp_hdr(skb)->doff << 2u);
180e9117e50SBert Kenward 	in_len = skb_headlen(skb) - header_len;
181e9117e50SBert Kenward 	st->header_len = header_len;
182e9117e50SBert Kenward 	st->in_len = in_len;
183e9117e50SBert Kenward 	if (st->protocol == htons(ETH_P_IP)) {
184e9117e50SBert Kenward 		st->ip_base_len = st->header_len - st->ip_off;
185e9117e50SBert Kenward 		st->ipv4_id = ntohs(ip_hdr(skb)->id);
186e9117e50SBert Kenward 	} else {
187e9117e50SBert Kenward 		st->ip_base_len = st->header_len - st->tcp_off;
188e9117e50SBert Kenward 		st->ipv4_id = 0;
189e9117e50SBert Kenward 	}
190e9117e50SBert Kenward 	st->seqnum = ntohl(tcp_hdr(skb)->seq);
191e9117e50SBert Kenward 
192e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(tcp_hdr(skb)->urg);
193e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(tcp_hdr(skb)->syn);
194e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(tcp_hdr(skb)->rst);
195e9117e50SBert Kenward 
196e9117e50SBert Kenward 	st->out_len = skb->len - header_len;
197e9117e50SBert Kenward 
198e9117e50SBert Kenward 	dma_addr = dma_map_single(dma_dev, skb->data,
199e9117e50SBert Kenward 				  skb_headlen(skb), DMA_TO_DEVICE);
200e9117e50SBert Kenward 	st->header_dma_addr = dma_addr;
201e9117e50SBert Kenward 	st->header_unmap_len = skb_headlen(skb);
202e9117e50SBert Kenward 	st->dma_addr = dma_addr + header_len;
203e9117e50SBert Kenward 	st->unmap_len = 0;
204e9117e50SBert Kenward 
205e9117e50SBert Kenward 	return unlikely(dma_mapping_error(dma_dev, dma_addr)) ? -ENOMEM : 0;
206e9117e50SBert Kenward }
207e9117e50SBert Kenward 
tso_get_fragment(struct tso_state * st,struct efx_nic * efx,skb_frag_t * frag)208e9117e50SBert Kenward static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
209e9117e50SBert Kenward 			    skb_frag_t *frag)
210e9117e50SBert Kenward {
211e9117e50SBert Kenward 	st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
212e9117e50SBert Kenward 					  skb_frag_size(frag), DMA_TO_DEVICE);
213e9117e50SBert Kenward 	if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
214e9117e50SBert Kenward 		st->unmap_len = skb_frag_size(frag);
215e9117e50SBert Kenward 		st->in_len = skb_frag_size(frag);
216e9117e50SBert Kenward 		st->dma_addr = st->unmap_addr;
217e9117e50SBert Kenward 		return 0;
218e9117e50SBert Kenward 	}
219e9117e50SBert Kenward 	return -ENOMEM;
220e9117e50SBert Kenward }
221e9117e50SBert Kenward 
222e9117e50SBert Kenward 
223e9117e50SBert Kenward /**
224e9117e50SBert Kenward  * tso_fill_packet_with_fragment - form descriptors for the current fragment
225e9117e50SBert Kenward  * @tx_queue:		Efx TX queue
226e9117e50SBert Kenward  * @skb:		Socket buffer
227e9117e50SBert Kenward  * @st:			TSO state
228e9117e50SBert Kenward  *
229e9117e50SBert Kenward  * Form descriptors for the current fragment, until we reach the end
230e9117e50SBert Kenward  * of fragment or end-of-packet.
231e9117e50SBert Kenward  */
tso_fill_packet_with_fragment(struct efx_tx_queue * tx_queue,const struct sk_buff * skb,struct tso_state * st)232e9117e50SBert Kenward static void tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
233e9117e50SBert Kenward 					  const struct sk_buff *skb,
234e9117e50SBert Kenward 					  struct tso_state *st)
235e9117e50SBert Kenward {
236e9117e50SBert Kenward 	struct efx_tx_buffer *buffer;
237e9117e50SBert Kenward 	int n;
238e9117e50SBert Kenward 
239e9117e50SBert Kenward 	if (st->in_len == 0)
240e9117e50SBert Kenward 		return;
241e9117e50SBert Kenward 	if (st->packet_space == 0)
242e9117e50SBert Kenward 		return;
243e9117e50SBert Kenward 
244e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(st->in_len <= 0);
245e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(st->packet_space <= 0);
246e9117e50SBert Kenward 
247e9117e50SBert Kenward 	n = min(st->in_len, st->packet_space);
248e9117e50SBert Kenward 
249e9117e50SBert Kenward 	st->packet_space -= n;
250e9117e50SBert Kenward 	st->out_len -= n;
251e9117e50SBert Kenward 	st->in_len -= n;
252e9117e50SBert Kenward 
253e9117e50SBert Kenward 	efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
254e9117e50SBert Kenward 
255e9117e50SBert Kenward 	if (st->out_len == 0) {
256e9117e50SBert Kenward 		/* Transfer ownership of the skb */
257e9117e50SBert Kenward 		buffer->skb = skb;
258e9117e50SBert Kenward 		buffer->flags = EFX_TX_BUF_SKB;
259e9117e50SBert Kenward 	} else if (st->packet_space != 0) {
260e9117e50SBert Kenward 		buffer->flags = EFX_TX_BUF_CONT;
261e9117e50SBert Kenward 	}
262e9117e50SBert Kenward 
263e9117e50SBert Kenward 	if (st->in_len == 0) {
264e9117e50SBert Kenward 		/* Transfer ownership of the DMA mapping */
265e9117e50SBert Kenward 		buffer->unmap_len = st->unmap_len;
266e9117e50SBert Kenward 		buffer->dma_offset = buffer->unmap_len - buffer->len;
267e9117e50SBert Kenward 		st->unmap_len = 0;
268e9117e50SBert Kenward 	}
269e9117e50SBert Kenward 
270e9117e50SBert Kenward 	st->dma_addr += n;
271e9117e50SBert Kenward }
272e9117e50SBert Kenward 
273e9117e50SBert Kenward 
274e9117e50SBert Kenward #define TCP_FLAGS_OFFSET 13
275e9117e50SBert Kenward 
276e9117e50SBert Kenward /**
277e9117e50SBert Kenward  * tso_start_new_packet - generate a new header and prepare for the new packet
278e9117e50SBert Kenward  * @tx_queue:		Efx TX queue
279e9117e50SBert Kenward  * @skb:		Socket buffer
280e9117e50SBert Kenward  * @st:			TSO state
281e9117e50SBert Kenward  *
282e9117e50SBert Kenward  * Generate a new header and prepare for the new packet.  Return 0 on
28346d1efd8SEdward Cree  * success, or -%ENOMEM if failed to alloc header, or other negative error.
284e9117e50SBert Kenward  */
tso_start_new_packet(struct efx_tx_queue * tx_queue,const struct sk_buff * skb,struct tso_state * st)285e9117e50SBert Kenward static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
286e9117e50SBert Kenward 				const struct sk_buff *skb,
287e9117e50SBert Kenward 				struct tso_state *st)
288e9117e50SBert Kenward {
289e9117e50SBert Kenward 	struct efx_tx_buffer *buffer =
290e9117e50SBert Kenward 		efx_tx_queue_get_insert_buffer(tx_queue);
291e9117e50SBert Kenward 	bool is_last = st->out_len <= skb_shinfo(skb)->gso_size;
29246d1efd8SEdward Cree 	u8 tcp_flags_mask, tcp_flags;
293e9117e50SBert Kenward 
294e9117e50SBert Kenward 	if (!is_last) {
295e9117e50SBert Kenward 		st->packet_space = skb_shinfo(skb)->gso_size;
296e9117e50SBert Kenward 		tcp_flags_mask = 0x09; /* mask out FIN and PSH */
297e9117e50SBert Kenward 	} else {
298e9117e50SBert Kenward 		st->packet_space = st->out_len;
299e9117e50SBert Kenward 		tcp_flags_mask = 0x00;
300e9117e50SBert Kenward 	}
301e9117e50SBert Kenward 
30246d1efd8SEdward Cree 	if (WARN_ON(!st->header_unmap_len))
30346d1efd8SEdward Cree 		return -EINVAL;
304e9117e50SBert Kenward 	/* Send the original headers with a TSO option descriptor
305e9117e50SBert Kenward 	 * in front
306e9117e50SBert Kenward 	 */
30746d1efd8SEdward Cree 	tcp_flags = ((u8 *)tcp_hdr(skb))[TCP_FLAGS_OFFSET] & ~tcp_flags_mask;
308e9117e50SBert Kenward 
309e9117e50SBert Kenward 	buffer->flags = EFX_TX_BUF_OPTION;
310e9117e50SBert Kenward 	buffer->len = 0;
311e9117e50SBert Kenward 	buffer->unmap_len = 0;
312e9117e50SBert Kenward 	EFX_POPULATE_QWORD_5(buffer->option,
313e9117e50SBert Kenward 			     ESF_DZ_TX_DESC_IS_OPT, 1,
314e9117e50SBert Kenward 			     ESF_DZ_TX_OPTION_TYPE,
315e9117e50SBert Kenward 			     ESE_DZ_TX_OPTION_DESC_TSO,
316e9117e50SBert Kenward 			     ESF_DZ_TX_TSO_TCP_FLAGS, tcp_flags,
317e9117e50SBert Kenward 			     ESF_DZ_TX_TSO_IP_ID, st->ipv4_id,
318e9117e50SBert Kenward 			     ESF_DZ_TX_TSO_TCP_SEQNO, st->seqnum);
319e9117e50SBert Kenward 	++tx_queue->insert_count;
320e9117e50SBert Kenward 
321e9117e50SBert Kenward 	/* We mapped the headers in tso_start().  Unmap them
322e9117e50SBert Kenward 	 * when the last segment is completed.
323e9117e50SBert Kenward 	 */
324e9117e50SBert Kenward 	buffer = efx_tx_queue_get_insert_buffer(tx_queue);
325e9117e50SBert Kenward 	buffer->dma_addr = st->header_dma_addr;
326e9117e50SBert Kenward 	buffer->len = st->header_len;
327e9117e50SBert Kenward 	if (is_last) {
328e9117e50SBert Kenward 		buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_MAP_SINGLE;
329e9117e50SBert Kenward 		buffer->unmap_len = st->header_unmap_len;
330e9117e50SBert Kenward 		buffer->dma_offset = 0;
331e9117e50SBert Kenward 		/* Ensure we only unmap them once in case of a
332e9117e50SBert Kenward 		 * later DMA mapping error and rollback
333e9117e50SBert Kenward 		 */
334e9117e50SBert Kenward 		st->header_unmap_len = 0;
335e9117e50SBert Kenward 	} else {
336e9117e50SBert Kenward 		buffer->flags = EFX_TX_BUF_CONT;
337e9117e50SBert Kenward 		buffer->unmap_len = 0;
338e9117e50SBert Kenward 	}
339e9117e50SBert Kenward 	++tx_queue->insert_count;
340e9117e50SBert Kenward 
341e9117e50SBert Kenward 	st->seqnum += skb_shinfo(skb)->gso_size;
342e9117e50SBert Kenward 
343e9117e50SBert Kenward 	/* Linux leaves suitable gaps in the IP ID space for us to fill. */
344e9117e50SBert Kenward 	++st->ipv4_id;
345e9117e50SBert Kenward 
346e9117e50SBert Kenward 	return 0;
347e9117e50SBert Kenward }
348e9117e50SBert Kenward 
349e9117e50SBert Kenward /**
350e9117e50SBert Kenward  * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
351e9117e50SBert Kenward  * @tx_queue:		Efx TX queue
352e9117e50SBert Kenward  * @skb:		Socket buffer
353e9117e50SBert Kenward  * @data_mapped:        Did we map the data? Always set to true
354e9117e50SBert Kenward  *                      by this on success.
355e9117e50SBert Kenward  *
356e9117e50SBert Kenward  * Context: You must hold netif_tx_lock() to call this function.
357e9117e50SBert Kenward  *
358e9117e50SBert Kenward  * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
35946d1efd8SEdward Cree  * @skb was not enqueued.  @skb is consumed unless return value is
36046d1efd8SEdward Cree  * %EINVAL.
361e9117e50SBert Kenward  */
efx_enqueue_skb_tso(struct efx_tx_queue * tx_queue,struct sk_buff * skb,bool * data_mapped)362e9117e50SBert Kenward int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
363e9117e50SBert Kenward 			struct sk_buff *skb,
364e9117e50SBert Kenward 			bool *data_mapped)
365e9117e50SBert Kenward {
366e9117e50SBert Kenward 	struct efx_nic *efx = tx_queue->efx;
367e9117e50SBert Kenward 	int frag_i, rc;
368e9117e50SBert Kenward 	struct tso_state state;
369e9117e50SBert Kenward 
37046d1efd8SEdward Cree 	if (tx_queue->tso_version != 1)
37146d1efd8SEdward Cree 		return -EINVAL;
37246d1efd8SEdward Cree 
373e9117e50SBert Kenward 	prefetch(skb->data);
374e9117e50SBert Kenward 
375e9117e50SBert Kenward 	/* Find the packet protocol and sanity-check it */
376e9117e50SBert Kenward 	state.protocol = efx_tso_check_protocol(skb);
377e9117e50SBert Kenward 
378e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(tx_queue->write_count != tx_queue->insert_count);
379e9117e50SBert Kenward 
380e9117e50SBert Kenward 	rc = tso_start(&state, efx, tx_queue, skb);
381e9117e50SBert Kenward 	if (rc)
38246d1efd8SEdward Cree 		goto fail;
383e9117e50SBert Kenward 
384e9117e50SBert Kenward 	if (likely(state.in_len == 0)) {
385e9117e50SBert Kenward 		/* Grab the first payload fragment. */
386e01b16a7SEdward Cree 		EFX_WARN_ON_ONCE_PARANOID(skb_shinfo(skb)->nr_frags < 1);
387e9117e50SBert Kenward 		frag_i = 0;
388e9117e50SBert Kenward 		rc = tso_get_fragment(&state, efx,
389e9117e50SBert Kenward 				      skb_shinfo(skb)->frags + frag_i);
390e9117e50SBert Kenward 		if (rc)
39146d1efd8SEdward Cree 			goto fail;
392e9117e50SBert Kenward 	} else {
393e9117e50SBert Kenward 		/* Payload starts in the header area. */
394e9117e50SBert Kenward 		frag_i = -1;
395e9117e50SBert Kenward 	}
396e9117e50SBert Kenward 
39746d1efd8SEdward Cree 	rc = tso_start_new_packet(tx_queue, skb, &state);
39846d1efd8SEdward Cree 	if (rc)
39946d1efd8SEdward Cree 		goto fail;
400e9117e50SBert Kenward 
401e9117e50SBert Kenward 	prefetch_ptr(tx_queue);
402e9117e50SBert Kenward 
403e9117e50SBert Kenward 	while (1) {
404e9117e50SBert Kenward 		tso_fill_packet_with_fragment(tx_queue, skb, &state);
405e9117e50SBert Kenward 
406e9117e50SBert Kenward 		/* Move onto the next fragment? */
407e9117e50SBert Kenward 		if (state.in_len == 0) {
408e9117e50SBert Kenward 			if (++frag_i >= skb_shinfo(skb)->nr_frags)
409e9117e50SBert Kenward 				/* End of payload reached. */
410e9117e50SBert Kenward 				break;
411e9117e50SBert Kenward 			rc = tso_get_fragment(&state, efx,
412e9117e50SBert Kenward 					      skb_shinfo(skb)->frags + frag_i);
413e9117e50SBert Kenward 			if (rc)
41446d1efd8SEdward Cree 				goto fail;
415e9117e50SBert Kenward 		}
416e9117e50SBert Kenward 
417e9117e50SBert Kenward 		/* Start at new packet? */
41846d1efd8SEdward Cree 		if (state.packet_space == 0) {
41946d1efd8SEdward Cree 			rc = tso_start_new_packet(tx_queue, skb, &state);
42046d1efd8SEdward Cree 			if (rc)
42146d1efd8SEdward Cree 				goto fail;
42246d1efd8SEdward Cree 		}
423e9117e50SBert Kenward 	}
424e9117e50SBert Kenward 
425e9117e50SBert Kenward 	*data_mapped = true;
426e9117e50SBert Kenward 
427e9117e50SBert Kenward 	return 0;
428e9117e50SBert Kenward 
42946d1efd8SEdward Cree fail:
43046d1efd8SEdward Cree 	if (rc == -ENOMEM)
431e9117e50SBert Kenward 		netif_err(efx, tx_err, efx->net_dev,
432e9117e50SBert Kenward 			  "Out of memory for TSO headers, or DMA mapping error\n");
43346d1efd8SEdward Cree 	else
43446d1efd8SEdward Cree 		netif_err(efx, tx_err, efx->net_dev, "TSO failed, rc = %d\n", rc);
435e9117e50SBert Kenward 
436e9117e50SBert Kenward 	/* Free the DMA mapping we were in the process of writing out */
437e9117e50SBert Kenward 	if (state.unmap_len) {
438e9117e50SBert Kenward 		dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr,
439e9117e50SBert Kenward 			       state.unmap_len, DMA_TO_DEVICE);
440e9117e50SBert Kenward 	}
441e9117e50SBert Kenward 
44246d1efd8SEdward Cree 	/* Free the header DMA mapping */
443e9117e50SBert Kenward 	if (state.header_unmap_len)
444e9117e50SBert Kenward 		dma_unmap_single(&efx->pci_dev->dev, state.header_dma_addr,
445e9117e50SBert Kenward 				 state.header_unmap_len, DMA_TO_DEVICE);
446e9117e50SBert Kenward 
44746d1efd8SEdward Cree 	return rc;
448e9117e50SBert Kenward }
449