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