2 * linux/drivers/s390/net/qeth_eddp.c
4 * Enhanced Device Driver Packing (EDDP) support for the qeth driver.
6 * Copyright 2004 IBM Corporation
8 * Author(s): Thomas Spatzier <tspat@de.ibm.com>
11 #include <linux/config.h>
12 #include <linux/errno.h>
14 #include <linux/inetdevice.h>
15 #include <linux/netdevice.h>
16 #include <linux/kernel.h>
17 #include <linux/tcp.h>
19 #include <linux/skbuff.h>
25 #include "qeth_eddp.h"
28 qeth_eddp_check_buffers_for_context(struct qeth_qdio_out_q *queue,
29 struct qeth_eddp_context *ctx)
31 int index = queue->next_buf_to_fill;
32 int elements_needed = ctx->num_elements;
33 int elements_in_buffer;
35 int buffers_needed = 0;
37 QETH_DBF_TEXT(trace, 5, "eddpcbfc");
38 while(elements_needed > 0) {
40 if (atomic_read(&queue->bufs[index].state) !=
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;
50 return buffers_needed;
54 qeth_eddp_free_context(struct qeth_eddp_context *ctx)
58 QETH_DBF_TEXT(trace, 5, "eddpfctx");
59 for (i = 0; i < ctx->num_pages; ++i)
60 free_page((unsigned long)ctx->pages[i]);
62 if (ctx->elements != NULL)
69 qeth_eddp_get_context(struct qeth_eddp_context *ctx)
71 atomic_inc(&ctx->refcnt);
75 qeth_eddp_put_context(struct qeth_eddp_context *ctx)
77 if (atomic_dec_return(&ctx->refcnt) == 0)
78 qeth_eddp_free_context(ctx);
82 qeth_eddp_buf_release_contexts(struct qeth_qdio_out_buffer *buf)
84 struct qeth_eddp_context_reference *ref;
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);
97 qeth_eddp_buf_ref_context(struct qeth_qdio_out_buffer *buf,
98 struct qeth_eddp_context *ctx)
100 struct qeth_eddp_context_reference *ref;
102 QETH_DBF_TEXT(trace, 6, "eddprfcx");
103 ref = kmalloc(sizeof(struct qeth_eddp_context_reference), GFP_ATOMIC);
106 qeth_eddp_get_context(ctx);
108 list_add_tail(&ref->list, &buf->ctx_list);
113 qeth_eddp_fill_buffer(struct qeth_qdio_out_q *queue,
114 struct qeth_eddp_context *ctx,
117 struct qeth_qdio_out_buffer *buf = NULL;
118 struct qdio_buffer *buffer;
119 int elements = ctx->num_elements;
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
135 PRINT_WARN("could only partially fill eddp "
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;
148 /* new buffer, so we have to add ctx to buffer'ctx_list
149 * and increment ctx's refcnt */
155 if (qeth_eddp_buf_ref_context(buf, ctx)){
156 PRINT_WARN("no memory to create eddp context "
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++;
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);
184 #ifdef CONFIG_QETH_PERF_STATS
185 queue->card->perf_stats.skbs_sent_pack++;
187 QETH_DBF_TEXT(trace, 6, "fillbfpa");
188 if (buf->next_element_to_fill >=
189 QETH_MAX_BUFFER_ELEMENTS(queue->card)) {
191 * packed buffer if full -> set state PRIMED
194 atomic_set(&buf->state, QETH_QDIO_BUF_PRIMED);
203 qeth_eddp_create_segment_hdrs(struct qeth_eddp_context *ctx,
204 struct qeth_eddp_data *eddp, int data_len)
210 struct qeth_eddp_element *element;
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)
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];
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;
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;
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;
263 qeth_eddp_copy_data_tcp(char *dst, struct qeth_eddp_data *eddp, int len,
266 struct skb_frag_struct *frag;
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,
276 eddp->skb_offset += len;
279 if (eddp->frag < 0) {
280 /* we're in skb->data */
281 left_in_frag = (eddp->skb->len - eddp->skb->data_len)
283 src = eddp->skb->data + eddp->skb_offset;
285 frag = &skb_shinfo(eddp->skb)->
287 left_in_frag = frag->size - eddp->frag_offset;
289 (page_to_pfn(frag->page) << PAGE_SHIFT)+
290 frag->page_offset + eddp->frag_offset);
292 if (left_in_frag <= 0) {
294 eddp->frag_offset = 0;
297 copy_len = min(left_in_frag, len);
298 memcpy(dst, src, copy_len);
299 *hcsum = csum_partial(src, copy_len, *hcsum);
301 eddp->frag_offset += copy_len;
302 eddp->skb_offset += copy_len;
309 qeth_eddp_create_segment_data_tcp(struct qeth_eddp_context *ctx,
310 struct qeth_eddp_data *eddp, int data_len,
316 struct qeth_eddp_element *element;
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];
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;
330 element->flags = SBAL_FLAGS_FIRST_FRAG;
332 element->flags = SBAL_FLAGS_MIDDLE_FRAG;
335 data_len -= page_remainder;
336 ctx->offset += page_remainder;
337 page = ctx->pages[ctx->offset >> PAGE_SHIFT];
339 element->addr = page + page_offset;
341 qeth_eddp_copy_data_tcp(page + page_offset, eddp,
343 element->length += data_len;
345 element->flags = SBAL_FLAGS_LAST_FRAG;
347 ctx->offset += data_len;
352 ((struct tcphdr *)eddp->th_in_ctx)->check = csum_fold(hcsum);
356 qeth_eddp_check_tcp4_hdr(struct qeth_eddp_data *eddp, int data_len)
358 u32 phcsum; /* pseudo header checksum */
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);
370 qeth_eddp_check_tcp6_hdr(struct qeth_eddp_data *eddp, int data_len)
373 u32 phcsum; /* pseudo header checksum */
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);
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)
390 struct qeth_eddp_data *eddp;
392 QETH_DBF_TEXT(trace, 5, "eddpcrda");
393 eddp = kmalloc(sizeof(struct qeth_eddp_data), GFP_ATOMIC);
395 memset(eddp, 0, sizeof(struct qeth_eddp_data));
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 */
407 __qeth_eddp_fill_context_tcp(struct qeth_eddp_context *ctx,
408 struct qeth_eddp_data *eddp)
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 */
430 eddp->qh.hdr.l3.length = data_len + eddp->nhl +
433 if (eddp->skb->protocol == ETH_P_IP){
434 eddp->nh.ip4.h.tot_len = data_len + eddp->nhl +
436 eddp->nh.ip4.h.check = 0;
437 eddp->nh.ip4.h.check =
438 ip_fast_csum((u8 *)&eddp->nh.ip4.h,
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;
448 if (eddp->skb->protocol == ETH_P_IP)
449 hcsum = qeth_eddp_check_tcp4_hdr(eddp, data_len);
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)
457 /* prepare headers for next round */
458 if (eddp->skb->protocol == ETH_P_IP)
460 eddp->th.tcp.h.seq += data_len;
465 qeth_eddp_fill_context_tcp(struct qeth_eddp_context *ctx,
466 struct sk_buff *skb, struct qeth_hdr *qhdr)
468 struct qeth_eddp_data *eddp = NULL;
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,
475 (u8 *)skb->h.th, skb->h.th->doff*4);
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);
482 QETH_DBF_TEXT(trace, 2, "eddpfcnm");
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));
492 #endif /* CONFIG_QETH_VLAN */
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;
498 /* begin segmentation and fill context */
499 __qeth_eddp_fill_context_tcp(ctx, eddp);
505 qeth_eddp_calc_num_pages(struct qeth_eddp_context *ctx, struct sk_buff *skb,
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) /
516 ctx->elements_per_skb = 1;
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);
524 ctx->num_elements = ctx->elements_per_skb *
525 (skb_shinfo(skb)->tso_segs + 1);
528 static inline struct qeth_eddp_context *
529 qeth_eddp_create_context_generic(struct qeth_card *card, struct sk_buff *skb,
532 struct qeth_eddp_context *ctx = NULL;
536 QETH_DBF_TEXT(trace, 5, "creddpcg");
537 /* create the context and allocate pages */
538 ctx = kmalloc(sizeof(struct qeth_eddp_context), GFP_ATOMIC);
540 QETH_DBF_TEXT(trace, 2, "ceddpcn1");
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");
551 ctx->pages = kmalloc(ctx->num_pages * sizeof(u8 *), GFP_ATOMIC);
552 if (ctx->pages == NULL){
553 QETH_DBF_TEXT(trace, 2, "ceddpcn2");
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);
561 QETH_DBF_TEXT(trace, 2, "ceddpcn3");
563 qeth_eddp_free_context(ctx);
566 memset(addr, 0, PAGE_SIZE);
567 ctx->pages[i] = addr;
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);
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;
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)
588 struct qeth_eddp_context *ctx = NULL;
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 +
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) +
600 QETH_DBF_TEXT(trace, 2, "cetcpinv");
603 QETH_DBF_TEXT(trace, 2, "creddpnl");
606 if (qeth_eddp_fill_context_tcp(ctx, skb, qhdr)){
607 QETH_DBF_TEXT(trace, 2, "ceddptfe");
608 qeth_eddp_free_context(ctx);
611 atomic_set(&ctx->refcnt, 1);
615 struct qeth_eddp_context *
616 qeth_eddp_create_context(struct qeth_card *card, struct sk_buff *skb,
617 struct qeth_hdr *qhdr)
619 QETH_DBF_TEXT(trace, 5, "creddpc");
620 switch (skb->sk->sk_protocol){
622 return qeth_eddp_create_context_tcp(card, skb, qhdr);
624 QETH_DBF_TEXT(trace, 2, "eddpinvp");