1 /****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2005-2015 Solarflare Communications Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
11 #include <linux/pci.h>
12 #include <linux/tcp.h>
15 #include <linux/ipv6.h>
16 #include <linux/slab.h>
18 #include <linux/if_ether.h>
19 #include <linux/highmem.h>
20 #include <linux/moduleparam.h>
21 #include <linux/cache.h>
22 #include "net_driver.h"
27 #include "workarounds.h"
28 #include "ef10_regs.h"
30 /* Efx legacy TCP segmentation acceleration.
32 * Utilises firmware support to go faster than GSO (but not as fast as TSOv2).
34 * Requires TX checksum offload support.
37 #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
40 * struct tso_state - TSO state for an SKB
41 * @out_len: Remaining length in current segment
42 * @seqnum: Current sequence number
43 * @ipv4_id: Current IPv4 ID, host endian
44 * @packet_space: Remaining space in current packet
45 * @dma_addr: DMA address of current position
46 * @in_len: Remaining length in current SKB fragment
47 * @unmap_len: Length of SKB fragment
48 * @unmap_addr: DMA address of SKB fragment
49 * @protocol: Network protocol (after any VLAN header)
50 * @ip_off: Offset of IP header
51 * @tcp_off: Offset of TCP header
52 * @header_len: Number of bytes of header
53 * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload
54 * @header_dma_addr: Header DMA address
55 * @header_unmap_len: Header DMA mapped length
57 * The state used during segmentation. It is put into this data structure
58 * just to make it easy to pass into inline functions.
65 unsigned int packet_space
;
70 unsigned int unmap_len
;
71 dma_addr_t unmap_addr
;
76 unsigned int header_len
;
77 unsigned int ip_base_len
;
78 dma_addr_t header_dma_addr
;
79 unsigned int header_unmap_len
;
82 static inline void prefetch_ptr(struct efx_tx_queue
*tx_queue
)
84 unsigned int insert_ptr
= efx_tx_queue_get_insert_index(tx_queue
);
87 ptr
= (char *) (tx_queue
->buffer
+ insert_ptr
);
91 ptr
= (char *) (((efx_qword_t
*)tx_queue
->txd
.buf
.addr
) + insert_ptr
);
97 * efx_tx_queue_insert - push descriptors onto the TX queue
98 * @tx_queue: Efx TX queue
99 * @dma_addr: DMA address of fragment
100 * @len: Length of fragment
101 * @final_buffer: The final buffer inserted into the queue
103 * Push descriptors onto the TX queue.
105 static void efx_tx_queue_insert(struct efx_tx_queue
*tx_queue
,
106 dma_addr_t dma_addr
, unsigned int len
,
107 struct efx_tx_buffer
**final_buffer
)
109 struct efx_tx_buffer
*buffer
;
110 unsigned int dma_len
;
112 EFX_WARN_ON_ONCE_PARANOID(len
<= 0);
115 buffer
= efx_tx_queue_get_insert_buffer(tx_queue
);
116 ++tx_queue
->insert_count
;
118 EFX_WARN_ON_ONCE_PARANOID(tx_queue
->insert_count
-
119 tx_queue
->read_count
>=
120 tx_queue
->efx
->txq_entries
);
122 buffer
->dma_addr
= dma_addr
;
124 dma_len
= tx_queue
->efx
->type
->tx_limit_len(tx_queue
,
127 /* If there's space for everything this is our last buffer. */
131 buffer
->len
= dma_len
;
132 buffer
->flags
= EFX_TX_BUF_CONT
;
137 EFX_WARN_ON_ONCE_PARANOID(!len
);
139 *final_buffer
= buffer
;
143 * Verify that our various assumptions about sk_buffs and the conditions
144 * under which TSO will be attempted hold true. Return the protocol number.
146 static __be16
efx_tso_check_protocol(struct sk_buff
*skb
)
148 __be16 protocol
= skb
->protocol
;
150 EFX_WARN_ON_ONCE_PARANOID(((struct ethhdr
*)skb
->data
)->h_proto
!=
152 if (protocol
== htons(ETH_P_8021Q
)) {
153 struct vlan_ethhdr
*veh
= (struct vlan_ethhdr
*)skb
->data
;
155 protocol
= veh
->h_vlan_encapsulated_proto
;
158 if (protocol
== htons(ETH_P_IP
)) {
159 EFX_WARN_ON_ONCE_PARANOID(ip_hdr(skb
)->protocol
!= IPPROTO_TCP
);
161 EFX_WARN_ON_ONCE_PARANOID(protocol
!= htons(ETH_P_IPV6
));
162 EFX_WARN_ON_ONCE_PARANOID(ipv6_hdr(skb
)->nexthdr
!= NEXTHDR_TCP
);
164 EFX_WARN_ON_ONCE_PARANOID((PTR_DIFF(tcp_hdr(skb
), skb
->data
) +
165 (tcp_hdr(skb
)->doff
<< 2u)) >
171 /* Parse the SKB header and initialise state. */
172 static int tso_start(struct tso_state
*st
, struct efx_nic
*efx
,
173 struct efx_tx_queue
*tx_queue
,
174 const struct sk_buff
*skb
)
176 struct device
*dma_dev
= &efx
->pci_dev
->dev
;
177 unsigned int header_len
, in_len
;
180 st
->ip_off
= skb_network_header(skb
) - skb
->data
;
181 st
->tcp_off
= skb_transport_header(skb
) - skb
->data
;
182 header_len
= st
->tcp_off
+ (tcp_hdr(skb
)->doff
<< 2u);
183 in_len
= skb_headlen(skb
) - header_len
;
184 st
->header_len
= header_len
;
186 if (st
->protocol
== htons(ETH_P_IP
)) {
187 st
->ip_base_len
= st
->header_len
- st
->ip_off
;
188 st
->ipv4_id
= ntohs(ip_hdr(skb
)->id
);
190 st
->ip_base_len
= st
->header_len
- st
->tcp_off
;
193 st
->seqnum
= ntohl(tcp_hdr(skb
)->seq
);
195 EFX_WARN_ON_ONCE_PARANOID(tcp_hdr(skb
)->urg
);
196 EFX_WARN_ON_ONCE_PARANOID(tcp_hdr(skb
)->syn
);
197 EFX_WARN_ON_ONCE_PARANOID(tcp_hdr(skb
)->rst
);
199 st
->out_len
= skb
->len
- header_len
;
201 dma_addr
= dma_map_single(dma_dev
, skb
->data
,
202 skb_headlen(skb
), DMA_TO_DEVICE
);
203 st
->header_dma_addr
= dma_addr
;
204 st
->header_unmap_len
= skb_headlen(skb
);
205 st
->dma_addr
= dma_addr
+ header_len
;
208 return unlikely(dma_mapping_error(dma_dev
, dma_addr
)) ? -ENOMEM
: 0;
211 static int tso_get_fragment(struct tso_state
*st
, struct efx_nic
*efx
,
214 st
->unmap_addr
= skb_frag_dma_map(&efx
->pci_dev
->dev
, frag
, 0,
215 skb_frag_size(frag
), DMA_TO_DEVICE
);
216 if (likely(!dma_mapping_error(&efx
->pci_dev
->dev
, st
->unmap_addr
))) {
217 st
->unmap_len
= skb_frag_size(frag
);
218 st
->in_len
= skb_frag_size(frag
);
219 st
->dma_addr
= st
->unmap_addr
;
227 * tso_fill_packet_with_fragment - form descriptors for the current fragment
228 * @tx_queue: Efx TX queue
229 * @skb: Socket buffer
232 * Form descriptors for the current fragment, until we reach the end
233 * of fragment or end-of-packet.
235 static void tso_fill_packet_with_fragment(struct efx_tx_queue
*tx_queue
,
236 const struct sk_buff
*skb
,
237 struct tso_state
*st
)
239 struct efx_tx_buffer
*buffer
;
244 if (st
->packet_space
== 0)
247 EFX_WARN_ON_ONCE_PARANOID(st
->in_len
<= 0);
248 EFX_WARN_ON_ONCE_PARANOID(st
->packet_space
<= 0);
250 n
= min(st
->in_len
, st
->packet_space
);
252 st
->packet_space
-= n
;
256 efx_tx_queue_insert(tx_queue
, st
->dma_addr
, n
, &buffer
);
258 if (st
->out_len
== 0) {
259 /* Transfer ownership of the skb */
261 buffer
->flags
= EFX_TX_BUF_SKB
;
262 } else if (st
->packet_space
!= 0) {
263 buffer
->flags
= EFX_TX_BUF_CONT
;
266 if (st
->in_len
== 0) {
267 /* Transfer ownership of the DMA mapping */
268 buffer
->unmap_len
= st
->unmap_len
;
269 buffer
->dma_offset
= buffer
->unmap_len
- buffer
->len
;
277 #define TCP_FLAGS_OFFSET 13
280 * tso_start_new_packet - generate a new header and prepare for the new packet
281 * @tx_queue: Efx TX queue
282 * @skb: Socket buffer
285 * Generate a new header and prepare for the new packet. Return 0 on
286 * success, or -%ENOMEM if failed to alloc header, or other negative error.
288 static int tso_start_new_packet(struct efx_tx_queue
*tx_queue
,
289 const struct sk_buff
*skb
,
290 struct tso_state
*st
)
292 struct efx_tx_buffer
*buffer
=
293 efx_tx_queue_get_insert_buffer(tx_queue
);
294 bool is_last
= st
->out_len
<= skb_shinfo(skb
)->gso_size
;
295 u8 tcp_flags_mask
, tcp_flags
;
298 st
->packet_space
= skb_shinfo(skb
)->gso_size
;
299 tcp_flags_mask
= 0x09; /* mask out FIN and PSH */
301 st
->packet_space
= st
->out_len
;
302 tcp_flags_mask
= 0x00;
305 if (WARN_ON(!st
->header_unmap_len
))
307 /* Send the original headers with a TSO option descriptor
310 tcp_flags
= ((u8
*)tcp_hdr(skb
))[TCP_FLAGS_OFFSET
] & ~tcp_flags_mask
;
312 buffer
->flags
= EFX_TX_BUF_OPTION
;
314 buffer
->unmap_len
= 0;
315 EFX_POPULATE_QWORD_5(buffer
->option
,
316 ESF_DZ_TX_DESC_IS_OPT
, 1,
317 ESF_DZ_TX_OPTION_TYPE
,
318 ESE_DZ_TX_OPTION_DESC_TSO
,
319 ESF_DZ_TX_TSO_TCP_FLAGS
, tcp_flags
,
320 ESF_DZ_TX_TSO_IP_ID
, st
->ipv4_id
,
321 ESF_DZ_TX_TSO_TCP_SEQNO
, st
->seqnum
);
322 ++tx_queue
->insert_count
;
324 /* We mapped the headers in tso_start(). Unmap them
325 * when the last segment is completed.
327 buffer
= efx_tx_queue_get_insert_buffer(tx_queue
);
328 buffer
->dma_addr
= st
->header_dma_addr
;
329 buffer
->len
= st
->header_len
;
331 buffer
->flags
= EFX_TX_BUF_CONT
| EFX_TX_BUF_MAP_SINGLE
;
332 buffer
->unmap_len
= st
->header_unmap_len
;
333 buffer
->dma_offset
= 0;
334 /* Ensure we only unmap them once in case of a
335 * later DMA mapping error and rollback
337 st
->header_unmap_len
= 0;
339 buffer
->flags
= EFX_TX_BUF_CONT
;
340 buffer
->unmap_len
= 0;
342 ++tx_queue
->insert_count
;
344 st
->seqnum
+= skb_shinfo(skb
)->gso_size
;
346 /* Linux leaves suitable gaps in the IP ID space for us to fill. */
353 * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
354 * @tx_queue: Efx TX queue
355 * @skb: Socket buffer
356 * @data_mapped: Did we map the data? Always set to true
357 * by this on success.
359 * Context: You must hold netif_tx_lock() to call this function.
361 * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
362 * @skb was not enqueued. @skb is consumed unless return value is
365 int efx_enqueue_skb_tso(struct efx_tx_queue
*tx_queue
,
369 struct efx_nic
*efx
= tx_queue
->efx
;
371 struct tso_state state
;
373 if (tx_queue
->tso_version
!= 1)
378 /* Find the packet protocol and sanity-check it */
379 state
.protocol
= efx_tso_check_protocol(skb
);
381 EFX_WARN_ON_ONCE_PARANOID(tx_queue
->write_count
!= tx_queue
->insert_count
);
383 rc
= tso_start(&state
, efx
, tx_queue
, skb
);
387 if (likely(state
.in_len
== 0)) {
388 /* Grab the first payload fragment. */
389 EFX_WARN_ON_ONCE_PARANOID(skb_shinfo(skb
)->nr_frags
< 1);
391 rc
= tso_get_fragment(&state
, efx
,
392 skb_shinfo(skb
)->frags
+ frag_i
);
396 /* Payload starts in the header area. */
400 rc
= tso_start_new_packet(tx_queue
, skb
, &state
);
404 prefetch_ptr(tx_queue
);
407 tso_fill_packet_with_fragment(tx_queue
, skb
, &state
);
409 /* Move onto the next fragment? */
410 if (state
.in_len
== 0) {
411 if (++frag_i
>= skb_shinfo(skb
)->nr_frags
)
412 /* End of payload reached. */
414 rc
= tso_get_fragment(&state
, efx
,
415 skb_shinfo(skb
)->frags
+ frag_i
);
420 /* Start at new packet? */
421 if (state
.packet_space
== 0) {
422 rc
= tso_start_new_packet(tx_queue
, skb
, &state
);
434 netif_err(efx
, tx_err
, efx
->net_dev
,
435 "Out of memory for TSO headers, or DMA mapping error\n");
437 netif_err(efx
, tx_err
, efx
->net_dev
, "TSO failed, rc = %d\n", rc
);
439 /* Free the DMA mapping we were in the process of writing out */
440 if (state
.unmap_len
) {
441 dma_unmap_page(&efx
->pci_dev
->dev
, state
.unmap_addr
,
442 state
.unmap_len
, DMA_TO_DEVICE
);
445 /* Free the header DMA mapping */
446 if (state
.header_unmap_len
)
447 dma_unmap_single(&efx
->pci_dev
->dev
, state
.header_dma_addr
,
448 state
.header_unmap_len
, DMA_TO_DEVICE
);