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