Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[linux-2.6] / drivers / s390 / net / qeth_core_offl.c
1 /*
2  *  drivers/s390/net/qeth_core_offl.c
3  *
4  *    Copyright IBM Corp. 2007
5  *    Author(s): Thomas Spatzier <tspat@de.ibm.com>,
6  *               Frank Blaschka <frank.blaschka@de.ibm.com>
7  */
8
9 #include <linux/errno.h>
10 #include <linux/ip.h>
11 #include <linux/inetdevice.h>
12 #include <linux/netdevice.h>
13 #include <linux/kernel.h>
14 #include <linux/tcp.h>
15 #include <net/tcp.h>
16 #include <linux/skbuff.h>
17
18 #include <net/ip.h>
19 #include <net/ip6_checksum.h>
20
21 #include "qeth_core.h"
22 #include "qeth_core_mpc.h"
23 #include "qeth_core_offl.h"
24
25 int qeth_eddp_check_buffers_for_context(struct qeth_qdio_out_q *queue,
26                 struct qeth_eddp_context *ctx)
27 {
28         int index = queue->next_buf_to_fill;
29         int elements_needed = ctx->num_elements;
30         int elements_in_buffer;
31         int skbs_in_buffer;
32         int buffers_needed = 0;
33
34         QETH_DBF_TEXT(TRACE, 5, "eddpcbfc");
35         while (elements_needed > 0) {
36                 buffers_needed++;
37                 if (atomic_read(&queue->bufs[index].state) !=
38                                 QETH_QDIO_BUF_EMPTY)
39                         return -EBUSY;
40
41                 elements_in_buffer = QETH_MAX_BUFFER_ELEMENTS(queue->card) -
42                                      queue->bufs[index].next_element_to_fill;
43                 skbs_in_buffer = elements_in_buffer / ctx->elements_per_skb;
44                 elements_needed -= skbs_in_buffer * ctx->elements_per_skb;
45                 index = (index + 1) % QDIO_MAX_BUFFERS_PER_Q;
46         }
47         return buffers_needed;
48 }
49
50 static void qeth_eddp_free_context(struct qeth_eddp_context *ctx)
51 {
52         int i;
53
54         QETH_DBF_TEXT(TRACE, 5, "eddpfctx");
55         for (i = 0; i < ctx->num_pages; ++i)
56                 free_page((unsigned long)ctx->pages[i]);
57         kfree(ctx->pages);
58         kfree(ctx->elements);
59         kfree(ctx);
60 }
61
62
63 static void qeth_eddp_get_context(struct qeth_eddp_context *ctx)
64 {
65         atomic_inc(&ctx->refcnt);
66 }
67
68 void qeth_eddp_put_context(struct qeth_eddp_context *ctx)
69 {
70         if (atomic_dec_return(&ctx->refcnt) == 0)
71                 qeth_eddp_free_context(ctx);
72 }
73 EXPORT_SYMBOL_GPL(qeth_eddp_put_context);
74
75 void qeth_eddp_buf_release_contexts(struct qeth_qdio_out_buffer *buf)
76 {
77         struct qeth_eddp_context_reference *ref;
78
79         QETH_DBF_TEXT(TRACE, 6, "eddprctx");
80         while (!list_empty(&buf->ctx_list)) {
81                 ref = list_entry(buf->ctx_list.next,
82                                  struct qeth_eddp_context_reference, list);
83                 qeth_eddp_put_context(ref->ctx);
84                 list_del(&ref->list);
85                 kfree(ref);
86         }
87 }
88
89 static int qeth_eddp_buf_ref_context(struct qeth_qdio_out_buffer *buf,
90                 struct qeth_eddp_context *ctx)
91 {
92         struct qeth_eddp_context_reference *ref;
93
94         QETH_DBF_TEXT(TRACE, 6, "eddprfcx");
95         ref = kmalloc(sizeof(struct qeth_eddp_context_reference), GFP_ATOMIC);
96         if (ref == NULL)
97                 return -ENOMEM;
98         qeth_eddp_get_context(ctx);
99         ref->ctx = ctx;
100         list_add_tail(&ref->list, &buf->ctx_list);
101         return 0;
102 }
103
104 int qeth_eddp_fill_buffer(struct qeth_qdio_out_q *queue,
105                 struct qeth_eddp_context *ctx, int index)
106 {
107         struct qeth_qdio_out_buffer *buf = NULL;
108         struct qdio_buffer *buffer;
109         int elements = ctx->num_elements;
110         int element = 0;
111         int flush_cnt = 0;
112         int must_refcnt = 1;
113         int i;
114
115         QETH_DBF_TEXT(TRACE, 5, "eddpfibu");
116         while (elements > 0) {
117                 buf = &queue->bufs[index];
118                 if (atomic_read(&buf->state) != QETH_QDIO_BUF_EMPTY) {
119                         /* normally this should not happen since we checked for
120                          * available elements in qeth_check_elements_for_context
121                          */
122                         if (element == 0)
123                                 return -EBUSY;
124                         else {
125                                 PRINT_WARN("could only partially fill eddp "
126                                            "buffer!\n");
127                                 goto out;
128                         }
129                 }
130                 /* check if the whole next skb fits into current buffer */
131                 if ((QETH_MAX_BUFFER_ELEMENTS(queue->card) -
132                                         buf->next_element_to_fill)
133                                 < ctx->elements_per_skb){
134                         /* no -> go to next buffer */
135                         atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
136                         index = (index + 1) % QDIO_MAX_BUFFERS_PER_Q;
137                         flush_cnt++;
138                         /* new buffer, so we have to add ctx to buffer'ctx_list
139                          * and increment ctx's refcnt */
140                         must_refcnt = 1;
141                         continue;
142                 }
143                 if (must_refcnt) {
144                         must_refcnt = 0;
145                         if (qeth_eddp_buf_ref_context(buf, ctx)) {
146                                 PRINT_WARN("no memory to create eddp context "
147                                            "reference\n");
148                                 goto out_check;
149                         }
150                 }
151                 buffer = buf->buffer;
152                 /* fill one skb into buffer */
153                 for (i = 0; i < ctx->elements_per_skb; ++i) {
154                         if (ctx->elements[element].length != 0) {
155                                 buffer->element[buf->next_element_to_fill].
156                                 addr = ctx->elements[element].addr;
157                                 buffer->element[buf->next_element_to_fill].
158                                 length = ctx->elements[element].length;
159                                 buffer->element[buf->next_element_to_fill].
160                                 flags = ctx->elements[element].flags;
161                                 buf->next_element_to_fill++;
162                         }
163                         element++;
164                         elements--;
165                 }
166         }
167 out_check:
168         if (!queue->do_pack) {
169                 QETH_DBF_TEXT(TRACE, 6, "fillbfnp");
170                 /* set state to PRIMED -> will be flushed */
171                 if (buf->next_element_to_fill > 0) {
172                         atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
173                         flush_cnt++;
174                 }
175         } else {
176                 if (queue->card->options.performance_stats)
177                         queue->card->perf_stats.skbs_sent_pack++;
178                 QETH_DBF_TEXT(TRACE, 6, "fillbfpa");
179                 if (buf->next_element_to_fill >=
180                                 QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
181                         /*
182                          * packed buffer if full -> set state PRIMED
183                          * -> will be flushed
184                          */
185                         atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
186                         flush_cnt++;
187                 }
188         }
189 out:
190         return flush_cnt;
191 }
192
193 static void qeth_eddp_create_segment_hdrs(struct qeth_eddp_context *ctx,
194                 struct qeth_eddp_data *eddp, int data_len)
195 {
196         u8 *page;
197         int page_remainder;
198         int page_offset;
199         int pkt_len;
200         struct qeth_eddp_element *element;
201
202         QETH_DBF_TEXT(TRACE, 5, "eddpcrsh");
203         page = ctx->pages[ctx->offset >> PAGE_SHIFT];
204         page_offset = ctx->offset % PAGE_SIZE;
205         element = &ctx->elements[ctx->num_elements];
206         pkt_len = eddp->nhl + eddp->thl + data_len;
207         /* FIXME: layer2 and VLAN !!! */
208         if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2)
209                 pkt_len += ETH_HLEN;
210         if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q))
211                 pkt_len += VLAN_HLEN;
212         /* does complete packet fit in current page ? */
213         page_remainder = PAGE_SIZE - page_offset;
214         if (page_remainder < (sizeof(struct qeth_hdr) + pkt_len)) {
215                 /* no -> go to start of next page */
216                 ctx->offset += page_remainder;
217                 page = ctx->pages[ctx->offset >> PAGE_SHIFT];
218                 page_offset = 0;
219         }
220         memcpy(page + page_offset, &eddp->qh, sizeof(struct qeth_hdr));
221         element->addr = page + page_offset;
222         element->length = sizeof(struct qeth_hdr);
223         ctx->offset += sizeof(struct qeth_hdr);
224         page_offset += sizeof(struct qeth_hdr);
225         /* add mac header (?) */
226         if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2) {
227                 memcpy(page + page_offset, &eddp->mac, ETH_HLEN);
228                 element->length += ETH_HLEN;
229                 ctx->offset += ETH_HLEN;
230                 page_offset += ETH_HLEN;
231         }
232         /* add VLAN tag */
233         if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q)) {
234                 memcpy(page + page_offset, &eddp->vlan, VLAN_HLEN);
235                 element->length += VLAN_HLEN;
236                 ctx->offset += VLAN_HLEN;
237                 page_offset += VLAN_HLEN;
238         }
239         /* add network header */
240         memcpy(page + page_offset, (u8 *)&eddp->nh, eddp->nhl);
241         element->length += eddp->nhl;
242         eddp->nh_in_ctx = page + page_offset;
243         ctx->offset += eddp->nhl;
244         page_offset += eddp->nhl;
245         /* add transport header */
246         memcpy(page + page_offset, (u8 *)&eddp->th, eddp->thl);
247         element->length += eddp->thl;
248         eddp->th_in_ctx = page + page_offset;
249         ctx->offset += eddp->thl;
250 }
251
252 static void qeth_eddp_copy_data_tcp(char *dst, struct qeth_eddp_data *eddp,
253                 int len, __wsum *hcsum)
254 {
255         struct skb_frag_struct *frag;
256         int left_in_frag;
257         int copy_len;
258         u8 *src;
259
260         QETH_DBF_TEXT(TRACE, 5, "eddpcdtc");
261         if (skb_shinfo(eddp->skb)->nr_frags == 0) {
262                 skb_copy_from_linear_data_offset(eddp->skb, eddp->skb_offset,
263                                                  dst, len);
264                 *hcsum = csum_partial(eddp->skb->data + eddp->skb_offset, len,
265                                       *hcsum);
266                 eddp->skb_offset += len;
267         } else {
268                 while (len > 0) {
269                         if (eddp->frag < 0) {
270                                 /* we're in skb->data */
271                                 left_in_frag = (eddp->skb->len -
272                                                 eddp->skb->data_len)
273                                                 - eddp->skb_offset;
274                                 src = eddp->skb->data + eddp->skb_offset;
275                         } else {
276                                 frag = &skb_shinfo(eddp->skb)->frags[
277                                         eddp->frag];
278                                 left_in_frag = frag->size - eddp->frag_offset;
279                                 src = (u8 *)((page_to_pfn(frag->page) <<
280                                         PAGE_SHIFT) + frag->page_offset +
281                                         eddp->frag_offset);
282                         }
283                         if (left_in_frag <= 0) {
284                                 eddp->frag++;
285                                 eddp->frag_offset = 0;
286                                 continue;
287                         }
288                         copy_len = min(left_in_frag, len);
289                         memcpy(dst, src, copy_len);
290                         *hcsum = csum_partial(src, copy_len, *hcsum);
291                         dst += copy_len;
292                         eddp->frag_offset += copy_len;
293                         eddp->skb_offset += copy_len;
294                         len -= copy_len;
295                 }
296         }
297 }
298
299 static void qeth_eddp_create_segment_data_tcp(struct qeth_eddp_context *ctx,
300                 struct qeth_eddp_data *eddp, int data_len, __wsum hcsum)
301 {
302         u8 *page;
303         int page_remainder;
304         int page_offset;
305         struct qeth_eddp_element *element;
306         int first_lap = 1;
307
308         QETH_DBF_TEXT(TRACE, 5, "eddpcsdt");
309         page = ctx->pages[ctx->offset >> PAGE_SHIFT];
310         page_offset = ctx->offset % PAGE_SIZE;
311         element = &ctx->elements[ctx->num_elements];
312         while (data_len) {
313                 page_remainder = PAGE_SIZE - page_offset;
314                 if (page_remainder < data_len) {
315                         qeth_eddp_copy_data_tcp(page + page_offset, eddp,
316                                                 page_remainder, &hcsum);
317                         element->length += page_remainder;
318                         if (first_lap)
319                                 element->flags = SBAL_FLAGS_FIRST_FRAG;
320                         else
321                                 element->flags = SBAL_FLAGS_MIDDLE_FRAG;
322                         ctx->num_elements++;
323                         element++;
324                         data_len -= page_remainder;
325                         ctx->offset += page_remainder;
326                         page = ctx->pages[ctx->offset >> PAGE_SHIFT];
327                         page_offset = 0;
328                         element->addr = page + page_offset;
329                 } else {
330                         qeth_eddp_copy_data_tcp(page + page_offset, eddp,
331                                                 data_len, &hcsum);
332                         element->length += data_len;
333                         if (!first_lap)
334                                 element->flags = SBAL_FLAGS_LAST_FRAG;
335                         ctx->num_elements++;
336                         ctx->offset += data_len;
337                         data_len = 0;
338                 }
339                 first_lap = 0;
340         }
341         ((struct tcphdr *)eddp->th_in_ctx)->check = csum_fold(hcsum);
342 }
343
344 static __wsum qeth_eddp_check_tcp4_hdr(struct qeth_eddp_data *eddp,
345                 int data_len)
346 {
347         __wsum phcsum; /* pseudo header checksum */
348
349         QETH_DBF_TEXT(TRACE, 5, "eddpckt4");
350         eddp->th.tcp.h.check = 0;
351         /* compute pseudo header checksum */
352         phcsum = csum_tcpudp_nofold(eddp->nh.ip4.h.saddr, eddp->nh.ip4.h.daddr,
353                                     eddp->thl + data_len, IPPROTO_TCP, 0);
354         /* compute checksum of tcp header */
355         return csum_partial((u8 *)&eddp->th, eddp->thl, phcsum);
356 }
357
358 static __wsum qeth_eddp_check_tcp6_hdr(struct qeth_eddp_data *eddp,
359                 int data_len)
360 {
361         __be32 proto;
362         __wsum phcsum; /* pseudo header checksum */
363
364         QETH_DBF_TEXT(TRACE, 5, "eddpckt6");
365         eddp->th.tcp.h.check = 0;
366         /* compute pseudo header checksum */
367         phcsum = csum_partial((u8 *)&eddp->nh.ip6.h.saddr,
368                               sizeof(struct in6_addr), 0);
369         phcsum = csum_partial((u8 *)&eddp->nh.ip6.h.daddr,
370                               sizeof(struct in6_addr), phcsum);
371         proto = htonl(IPPROTO_TCP);
372         phcsum = csum_partial((u8 *)&proto, sizeof(u32), phcsum);
373         return phcsum;
374 }
375
376 static struct qeth_eddp_data *qeth_eddp_create_eddp_data(struct qeth_hdr *qh,
377                 u8 *nh, u8 nhl, u8 *th, u8 thl)
378 {
379         struct qeth_eddp_data *eddp;
380
381         QETH_DBF_TEXT(TRACE, 5, "eddpcrda");
382         eddp = kzalloc(sizeof(struct qeth_eddp_data), GFP_ATOMIC);
383         if (eddp) {
384                 eddp->nhl = nhl;
385                 eddp->thl = thl;
386                 memcpy(&eddp->qh, qh, sizeof(struct qeth_hdr));
387                 memcpy(&eddp->nh, nh, nhl);
388                 memcpy(&eddp->th, th, thl);
389                 eddp->frag = -1; /* initially we're in skb->data */
390         }
391         return eddp;
392 }
393
394 static void __qeth_eddp_fill_context_tcp(struct qeth_eddp_context *ctx,
395                 struct qeth_eddp_data *eddp)
396 {
397         struct tcphdr *tcph;
398         int data_len;
399         __wsum hcsum;
400
401         QETH_DBF_TEXT(TRACE, 5, "eddpftcp");
402         eddp->skb_offset = sizeof(struct qeth_hdr) + eddp->nhl + eddp->thl;
403         if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2) {
404                 eddp->skb_offset += sizeof(struct ethhdr);
405                 if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q))
406                         eddp->skb_offset += VLAN_HLEN;
407         }
408         tcph = tcp_hdr(eddp->skb);
409         while (eddp->skb_offset < eddp->skb->len) {
410                 data_len = min((int)skb_shinfo(eddp->skb)->gso_size,
411                                (int)(eddp->skb->len - eddp->skb_offset));
412                 /* prepare qdio hdr */
413                 if (eddp->qh.hdr.l2.id == QETH_HEADER_TYPE_LAYER2) {
414                         eddp->qh.hdr.l2.pkt_length = data_len + ETH_HLEN +
415                                                      eddp->nhl + eddp->thl;
416                         if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q))
417                                 eddp->qh.hdr.l2.pkt_length += VLAN_HLEN;
418                 } else
419                         eddp->qh.hdr.l3.length = data_len + eddp->nhl +
420                                                  eddp->thl;
421                 /* prepare ip hdr */
422                 if (eddp->skb->protocol == htons(ETH_P_IP)) {
423                         eddp->nh.ip4.h.tot_len = htons(data_len + eddp->nhl +
424                                                  eddp->thl);
425                         eddp->nh.ip4.h.check = 0;
426                         eddp->nh.ip4.h.check =
427                                 ip_fast_csum((u8 *)&eddp->nh.ip4.h,
428                                                 eddp->nh.ip4.h.ihl);
429                 } else
430                         eddp->nh.ip6.h.payload_len = htons(data_len +
431                                                                 eddp->thl);
432                 /* prepare tcp hdr */
433                 if (data_len == (eddp->skb->len - eddp->skb_offset)) {
434                         /* last segment -> set FIN and PSH flags */
435                         eddp->th.tcp.h.fin = tcph->fin;
436                         eddp->th.tcp.h.psh = tcph->psh;
437                 }
438                 if (eddp->skb->protocol == htons(ETH_P_IP))
439                         hcsum = qeth_eddp_check_tcp4_hdr(eddp, data_len);
440                 else
441                         hcsum = qeth_eddp_check_tcp6_hdr(eddp, data_len);
442                 /* fill the next segment into the context */
443                 qeth_eddp_create_segment_hdrs(ctx, eddp, data_len);
444                 qeth_eddp_create_segment_data_tcp(ctx, eddp, data_len, hcsum);
445                 if (eddp->skb_offset >= eddp->skb->len)
446                         break;
447                 /* prepare headers for next round */
448                 if (eddp->skb->protocol == htons(ETH_P_IP))
449                         eddp->nh.ip4.h.id = htons(ntohs(eddp->nh.ip4.h.id) + 1);
450                 eddp->th.tcp.h.seq = htonl(ntohl(eddp->th.tcp.h.seq) +
451                         data_len);
452         }
453 }
454
455 static int qeth_eddp_fill_context_tcp(struct qeth_eddp_context *ctx,
456                 struct sk_buff *skb, struct qeth_hdr *qhdr)
457 {
458         struct qeth_eddp_data *eddp = NULL;
459
460         QETH_DBF_TEXT(TRACE, 5, "eddpficx");
461         /* create our segmentation headers and copy original headers */
462         if (skb->protocol == htons(ETH_P_IP))
463                 eddp = qeth_eddp_create_eddp_data(qhdr,
464                                                   skb_network_header(skb),
465                                                   ip_hdrlen(skb),
466                                                   skb_transport_header(skb),
467                                                   tcp_hdrlen(skb));
468         else
469                 eddp = qeth_eddp_create_eddp_data(qhdr,
470                                                   skb_network_header(skb),
471                                                   sizeof(struct ipv6hdr),
472                                                   skb_transport_header(skb),
473                                                   tcp_hdrlen(skb));
474
475         if (eddp == NULL) {
476                 QETH_DBF_TEXT(TRACE, 2, "eddpfcnm");
477                 return -ENOMEM;
478         }
479         if (qhdr->hdr.l2.id == QETH_HEADER_TYPE_LAYER2) {
480                 skb_set_mac_header(skb, sizeof(struct qeth_hdr));
481                 memcpy(&eddp->mac, eth_hdr(skb), ETH_HLEN);
482                 if (eddp->mac.h_proto == __constant_htons(ETH_P_8021Q)) {
483                         eddp->vlan[0] = skb->protocol;
484                         eddp->vlan[1] = htons(vlan_tx_tag_get(skb));
485                 }
486         }
487         /* the next flags will only be set on the last segment */
488         eddp->th.tcp.h.fin = 0;
489         eddp->th.tcp.h.psh = 0;
490         eddp->skb = skb;
491         /* begin segmentation and fill context */
492         __qeth_eddp_fill_context_tcp(ctx, eddp);
493         kfree(eddp);
494         return 0;
495 }
496
497 static void qeth_eddp_calc_num_pages(struct qeth_eddp_context *ctx,
498                 struct sk_buff *skb, int hdr_len)
499 {
500         int skbs_per_page;
501
502         QETH_DBF_TEXT(TRACE, 5, "eddpcanp");
503         /* can we put multiple skbs in one page? */
504         skbs_per_page = PAGE_SIZE / (skb_shinfo(skb)->gso_size + hdr_len);
505         if (skbs_per_page > 1) {
506                 ctx->num_pages = (skb_shinfo(skb)->gso_segs + 1) /
507                                  skbs_per_page + 1;
508                 ctx->elements_per_skb = 1;
509         } else {
510                 /* no -> how many elements per skb? */
511                 ctx->elements_per_skb = (skb_shinfo(skb)->gso_size + hdr_len +
512                                      PAGE_SIZE) >> PAGE_SHIFT;
513                 ctx->num_pages = ctx->elements_per_skb *
514                                  (skb_shinfo(skb)->gso_segs + 1);
515         }
516         ctx->num_elements = ctx->elements_per_skb *
517                             (skb_shinfo(skb)->gso_segs + 1);
518 }
519
520 static struct qeth_eddp_context *qeth_eddp_create_context_generic(
521                 struct qeth_card *card, struct sk_buff *skb, int hdr_len)
522 {
523         struct qeth_eddp_context *ctx = NULL;
524         u8 *addr;
525         int i;
526
527         QETH_DBF_TEXT(TRACE, 5, "creddpcg");
528         /* create the context and allocate pages */
529         ctx = kzalloc(sizeof(struct qeth_eddp_context), GFP_ATOMIC);
530         if (ctx == NULL) {
531                 QETH_DBF_TEXT(TRACE, 2, "ceddpcn1");
532                 return NULL;
533         }
534         ctx->type = QETH_LARGE_SEND_EDDP;
535         qeth_eddp_calc_num_pages(ctx, skb, hdr_len);
536         if (ctx->elements_per_skb > QETH_MAX_BUFFER_ELEMENTS(card)) {
537                 QETH_DBF_TEXT(TRACE, 2, "ceddpcis");
538                 kfree(ctx);
539                 return NULL;
540         }
541         ctx->pages = kcalloc(ctx->num_pages, sizeof(u8 *), GFP_ATOMIC);
542         if (ctx->pages == NULL) {
543                 QETH_DBF_TEXT(TRACE, 2, "ceddpcn2");
544                 kfree(ctx);
545                 return NULL;
546         }
547         for (i = 0; i < ctx->num_pages; ++i) {
548                 addr = (u8 *)get_zeroed_page(GFP_ATOMIC);
549                 if (addr == NULL) {
550                         QETH_DBF_TEXT(TRACE, 2, "ceddpcn3");
551                         ctx->num_pages = i;
552                         qeth_eddp_free_context(ctx);
553                         return NULL;
554                 }
555                 ctx->pages[i] = addr;
556         }
557         ctx->elements = kcalloc(ctx->num_elements,
558                                 sizeof(struct qeth_eddp_element), GFP_ATOMIC);
559         if (ctx->elements == NULL) {
560                 QETH_DBF_TEXT(TRACE, 2, "ceddpcn4");
561                 qeth_eddp_free_context(ctx);
562                 return NULL;
563         }
564         /* reset num_elements; will be incremented again in fill_buffer to
565          * reflect number of actually used elements */
566         ctx->num_elements = 0;
567         return ctx;
568 }
569
570 static struct qeth_eddp_context *qeth_eddp_create_context_tcp(
571                 struct qeth_card *card, struct sk_buff *skb,
572                 struct qeth_hdr *qhdr)
573 {
574         struct qeth_eddp_context *ctx = NULL;
575
576         QETH_DBF_TEXT(TRACE, 5, "creddpct");
577         if (skb->protocol == htons(ETH_P_IP))
578                 ctx = qeth_eddp_create_context_generic(card, skb,
579                                                 (sizeof(struct qeth_hdr) +
580                                                 ip_hdrlen(skb) +
581                                                 tcp_hdrlen(skb)));
582         else if (skb->protocol == htons(ETH_P_IPV6))
583                 ctx = qeth_eddp_create_context_generic(card, skb,
584                         sizeof(struct qeth_hdr) + sizeof(struct ipv6hdr) +
585                         tcp_hdrlen(skb));
586         else
587                 QETH_DBF_TEXT(TRACE, 2, "cetcpinv");
588
589         if (ctx == NULL) {
590                 QETH_DBF_TEXT(TRACE, 2, "creddpnl");
591                 return NULL;
592         }
593         if (qeth_eddp_fill_context_tcp(ctx, skb, qhdr)) {
594                 QETH_DBF_TEXT(TRACE, 2, "ceddptfe");
595                 qeth_eddp_free_context(ctx);
596                 return NULL;
597         }
598         atomic_set(&ctx->refcnt, 1);
599         return ctx;
600 }
601
602 struct qeth_eddp_context *qeth_eddp_create_context(struct qeth_card *card,
603                 struct sk_buff *skb, struct qeth_hdr *qhdr,
604                 unsigned char sk_protocol)
605 {
606         QETH_DBF_TEXT(TRACE, 5, "creddpc");
607         switch (sk_protocol) {
608         case IPPROTO_TCP:
609                 return qeth_eddp_create_context_tcp(card, skb, qhdr);
610         default:
611                 QETH_DBF_TEXT(TRACE, 2, "eddpinvp");
612         }
613         return NULL;
614 }
615 EXPORT_SYMBOL_GPL(qeth_eddp_create_context);
616
617 void qeth_tso_fill_header(struct qeth_card *card, struct qeth_hdr *qhdr,
618                 struct sk_buff *skb)
619 {
620         struct qeth_hdr_tso *hdr = (struct qeth_hdr_tso *)qhdr;
621         struct tcphdr *tcph = tcp_hdr(skb);
622         struct iphdr *iph = ip_hdr(skb);
623         struct ipv6hdr *ip6h = ipv6_hdr(skb);
624
625         QETH_DBF_TEXT(TRACE, 5, "tsofhdr");
626
627         /*fix header to TSO values ...*/
628         hdr->hdr.hdr.l3.id = QETH_HEADER_TYPE_TSO;
629         /*set values which are fix for the first approach ...*/
630         hdr->ext.hdr_tot_len = (__u16) sizeof(struct qeth_hdr_ext_tso);
631         hdr->ext.imb_hdr_no  = 1;
632         hdr->ext.hdr_type    = 1;
633         hdr->ext.hdr_version = 1;
634         hdr->ext.hdr_len     = 28;
635         /*insert non-fix values */
636         hdr->ext.mss = skb_shinfo(skb)->gso_size;
637         hdr->ext.dg_hdr_len = (__u16)(iph->ihl*4 + tcph->doff*4);
638         hdr->ext.payload_len = (__u16)(skb->len - hdr->ext.dg_hdr_len -
639                                        sizeof(struct qeth_hdr_tso));
640         tcph->check = 0;
641         if (skb->protocol == ETH_P_IPV6) {
642                 ip6h->payload_len = 0;
643                 tcph->check = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
644                                                0, IPPROTO_TCP, 0);
645         } else {
646                 /*OSA want us to set these values ...*/
647                 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
648                                          0, IPPROTO_TCP, 0);
649                 iph->tot_len = 0;
650                 iph->check = 0;
651         }
652 }
653 EXPORT_SYMBOL_GPL(qeth_tso_fill_header);
654
655 void qeth_tx_csum(struct sk_buff *skb)
656 {
657         int tlen;
658         if (skb->protocol == htons(ETH_P_IP)) {
659                 tlen = ntohs(ip_hdr(skb)->tot_len) - (ip_hdr(skb)->ihl << 2);
660                 switch (ip_hdr(skb)->protocol) {
661                 case IPPROTO_TCP:
662                         tcp_hdr(skb)->check = 0;
663                         tcp_hdr(skb)->check = csum_tcpudp_magic(
664                                 ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
665                                 tlen, ip_hdr(skb)->protocol,
666                                 skb_checksum(skb, skb_transport_offset(skb),
667                                         tlen, 0));
668                         break;
669                 case IPPROTO_UDP:
670                         udp_hdr(skb)->check = 0;
671                         udp_hdr(skb)->check = csum_tcpudp_magic(
672                                 ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
673                                 tlen, ip_hdr(skb)->protocol,
674                                 skb_checksum(skb, skb_transport_offset(skb),
675                                         tlen, 0));
676                         break;
677                 }
678         } else if (skb->protocol == htons(ETH_P_IPV6)) {
679                 switch (ipv6_hdr(skb)->nexthdr) {
680                 case IPPROTO_TCP:
681                         tcp_hdr(skb)->check = 0;
682                         tcp_hdr(skb)->check = csum_ipv6_magic(
683                                 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
684                                 ipv6_hdr(skb)->payload_len,
685                                 ipv6_hdr(skb)->nexthdr,
686                                 skb_checksum(skb, skb_transport_offset(skb),
687                                         ipv6_hdr(skb)->payload_len, 0));
688                         break;
689                 case IPPROTO_UDP:
690                         udp_hdr(skb)->check = 0;
691                         udp_hdr(skb)->check = csum_ipv6_magic(
692                                 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
693                                 ipv6_hdr(skb)->payload_len,
694                                 ipv6_hdr(skb)->nexthdr,
695                                 skb_checksum(skb, skb_transport_offset(skb),
696                                         ipv6_hdr(skb)->payload_len, 0));
697                         break;
698                 }
699         }
700 }
701 EXPORT_SYMBOL_GPL(qeth_tx_csum);