blob: 43f4eba6193397cce5d0e87e3de4e37e294b02c7 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Sachin Kamat484611e2014-05-26 14:52:46 +05302#include <linux/export.h>
emmanuel.grumbach@intel.com8941faa2015-10-26 10:31:29 +02003#include <linux/if_vlan.h>
Ezequiel Garciae876f202014-05-19 13:59:52 -03004#include <net/ip.h>
5#include <net/tso.h>
Karl Beldana63ba132014-10-21 16:06:05 +02006#include <asm/unaligned.h>
Ezequiel Garciae876f202014-05-19 13:59:52 -03007
8/* Calculate expected number of TX descriptors */
9int tso_count_descs(struct sk_buff *skb)
10{
11 /* The Marvell Way */
12 return skb_shinfo(skb)->gso_segs * 2 + skb_shinfo(skb)->nr_frags;
13}
Sachin Kamat484611e2014-05-26 14:52:46 +053014EXPORT_SYMBOL(tso_count_descs);
Ezequiel Garciae876f202014-05-19 13:59:52 -030015
16void tso_build_hdr(struct sk_buff *skb, char *hdr, struct tso_t *tso,
17 int size, bool is_last)
18{
Ezequiel Garciae876f202014-05-19 13:59:52 -030019 struct tcphdr *tcph;
20 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
21 int mac_hdr_len = skb_network_offset(skb);
22
23 memcpy(hdr, skb->data, hdr_len);
emmanuel.grumbach@intel.com8941faa2015-10-26 10:31:29 +020024 if (!tso->ipv6) {
25 struct iphdr *iph = (void *)(hdr + mac_hdr_len);
26
27 iph->id = htons(tso->ip_id);
28 iph->tot_len = htons(size + hdr_len - mac_hdr_len);
29 tso->ip_id++;
30 } else {
31 struct ipv6hdr *iph = (void *)(hdr + mac_hdr_len);
32
33 iph->payload_len = htons(size + tcp_hdrlen(skb));
34 }
Ezequiel Garciae876f202014-05-19 13:59:52 -030035 tcph = (struct tcphdr *)(hdr + skb_transport_offset(skb));
Karl Beldana63ba132014-10-21 16:06:05 +020036 put_unaligned_be32(tso->tcp_seq, &tcph->seq);
Ezequiel Garciae876f202014-05-19 13:59:52 -030037
38 if (!is_last) {
39 /* Clear all special flags for not last packet */
40 tcph->psh = 0;
41 tcph->fin = 0;
42 tcph->rst = 0;
43 }
44}
Sachin Kamat484611e2014-05-26 14:52:46 +053045EXPORT_SYMBOL(tso_build_hdr);
Ezequiel Garciae876f202014-05-19 13:59:52 -030046
47void tso_build_data(struct sk_buff *skb, struct tso_t *tso, int size)
48{
49 tso->tcp_seq += size;
50 tso->size -= size;
51 tso->data += size;
52
53 if ((tso->size == 0) &&
54 (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) {
55 skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
56
57 /* Move to next segment */
58 tso->size = frag->size;
59 tso->data = page_address(frag->page.p) + frag->page_offset;
60 tso->next_frag_idx++;
61 }
62}
Sachin Kamat484611e2014-05-26 14:52:46 +053063EXPORT_SYMBOL(tso_build_data);
Ezequiel Garciae876f202014-05-19 13:59:52 -030064
65void tso_start(struct sk_buff *skb, struct tso_t *tso)
66{
67 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
68
69 tso->ip_id = ntohs(ip_hdr(skb)->id);
70 tso->tcp_seq = ntohl(tcp_hdr(skb)->seq);
71 tso->next_frag_idx = 0;
emmanuel.grumbach@intel.com8941faa2015-10-26 10:31:29 +020072 tso->ipv6 = vlan_get_protocol(skb) == htons(ETH_P_IPV6);
Ezequiel Garciae876f202014-05-19 13:59:52 -030073
74 /* Build first data */
75 tso->size = skb_headlen(skb) - hdr_len;
76 tso->data = skb->data + hdr_len;
77 if ((tso->size == 0) &&
78 (tso->next_frag_idx < skb_shinfo(skb)->nr_frags)) {
79 skb_frag_t *frag = &skb_shinfo(skb)->frags[tso->next_frag_idx];
80
81 /* Move to next segment */
82 tso->size = frag->size;
83 tso->data = page_address(frag->page.p) + frag->page_offset;
84 tso->next_frag_idx++;
85 }
86}
Sachin Kamat484611e2014-05-26 14:52:46 +053087EXPORT_SYMBOL(tso_start);