]> Pileus Git - ~andy/linux/blob - drivers/net/xen-netback/netback.c
xen-netback: switch to use skb_partial_csum_set()
[~andy/linux] / drivers / net / xen-netback / netback.c
1 /*
2  * Back-end of the driver for virtual network devices. This portion of the
3  * driver exports a 'unified' network-device interface that can be accessed
4  * by any operating system that implements a compatible front end. A
5  * reference front-end implementation can be found in:
6  *  drivers/net/xen-netfront.c
7  *
8  * Copyright (c) 2002-2005, K A Fraser
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation; or, when distributed
13  * separately from the Linux kernel or incorporated into other
14  * software packages, subject to the following license:
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a copy
17  * of this source file (the "Software"), to deal in the Software without
18  * restriction, including without limitation the rights to use, copy, modify,
19  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20  * and to permit persons to whom the Software is furnished to do so, subject to
21  * the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be included in
24  * all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32  * IN THE SOFTWARE.
33  */
34
35 #include "common.h"
36
37 #include <linux/kthread.h>
38 #include <linux/if_vlan.h>
39 #include <linux/udp.h>
40
41 #include <net/tcp.h>
42
43 #include <xen/xen.h>
44 #include <xen/events.h>
45 #include <xen/interface/memory.h>
46
47 #include <asm/xen/hypercall.h>
48 #include <asm/xen/page.h>
49
50 struct pending_tx_info {
51         struct xen_netif_tx_request req;
52         struct xenvif *vif;
53 };
54 typedef unsigned int pending_ring_idx_t;
55
56 struct netbk_rx_meta {
57         int id;
58         int size;
59         int gso_size;
60 };
61
62 #define MAX_PENDING_REQS 256
63
64 /* Discriminate from any valid pending_idx value. */
65 #define INVALID_PENDING_IDX 0xFFFF
66
67 #define MAX_BUFFER_OFFSET PAGE_SIZE
68
69 /* extra field used in struct page */
70 union page_ext {
71         struct {
72 #if BITS_PER_LONG < 64
73 #define IDX_WIDTH   8
74 #define GROUP_WIDTH (BITS_PER_LONG - IDX_WIDTH)
75                 unsigned int group:GROUP_WIDTH;
76                 unsigned int idx:IDX_WIDTH;
77 #else
78                 unsigned int group, idx;
79 #endif
80         } e;
81         void *mapping;
82 };
83
84 struct xen_netbk {
85         wait_queue_head_t wq;
86         struct task_struct *task;
87
88         struct sk_buff_head rx_queue;
89         struct sk_buff_head tx_queue;
90
91         struct timer_list net_timer;
92
93         struct page *mmap_pages[MAX_PENDING_REQS];
94
95         pending_ring_idx_t pending_prod;
96         pending_ring_idx_t pending_cons;
97         struct list_head net_schedule_list;
98
99         /* Protect the net_schedule_list in netif. */
100         spinlock_t net_schedule_list_lock;
101
102         atomic_t netfront_count;
103
104         struct pending_tx_info pending_tx_info[MAX_PENDING_REQS];
105         struct gnttab_copy tx_copy_ops[MAX_PENDING_REQS];
106
107         u16 pending_ring[MAX_PENDING_REQS];
108
109         /*
110          * Given MAX_BUFFER_OFFSET of 4096 the worst case is that each
111          * head/fragment page uses 2 copy operations because it
112          * straddles two buffers in the frontend.
113          */
114         struct gnttab_copy grant_copy_op[2*XEN_NETIF_RX_RING_SIZE];
115         struct netbk_rx_meta meta[2*XEN_NETIF_RX_RING_SIZE];
116 };
117
118 static struct xen_netbk *xen_netbk;
119 static int xen_netbk_group_nr;
120
121 void xen_netbk_add_xenvif(struct xenvif *vif)
122 {
123         int i;
124         int min_netfront_count;
125         int min_group = 0;
126         struct xen_netbk *netbk;
127
128         min_netfront_count = atomic_read(&xen_netbk[0].netfront_count);
129         for (i = 0; i < xen_netbk_group_nr; i++) {
130                 int netfront_count = atomic_read(&xen_netbk[i].netfront_count);
131                 if (netfront_count < min_netfront_count) {
132                         min_group = i;
133                         min_netfront_count = netfront_count;
134                 }
135         }
136
137         netbk = &xen_netbk[min_group];
138
139         vif->netbk = netbk;
140         atomic_inc(&netbk->netfront_count);
141 }
142
143 void xen_netbk_remove_xenvif(struct xenvif *vif)
144 {
145         struct xen_netbk *netbk = vif->netbk;
146         vif->netbk = NULL;
147         atomic_dec(&netbk->netfront_count);
148 }
149
150 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
151                                   u8 status);
152 static void make_tx_response(struct xenvif *vif,
153                              struct xen_netif_tx_request *txp,
154                              s8       st);
155 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
156                                              u16      id,
157                                              s8       st,
158                                              u16      offset,
159                                              u16      size,
160                                              u16      flags);
161
162 static inline unsigned long idx_to_pfn(struct xen_netbk *netbk,
163                                        u16 idx)
164 {
165         return page_to_pfn(netbk->mmap_pages[idx]);
166 }
167
168 static inline unsigned long idx_to_kaddr(struct xen_netbk *netbk,
169                                          u16 idx)
170 {
171         return (unsigned long)pfn_to_kaddr(idx_to_pfn(netbk, idx));
172 }
173
174 /* extra field used in struct page */
175 static inline void set_page_ext(struct page *pg, struct xen_netbk *netbk,
176                                 unsigned int idx)
177 {
178         unsigned int group = netbk - xen_netbk;
179         union page_ext ext = { .e = { .group = group + 1, .idx = idx } };
180
181         BUILD_BUG_ON(sizeof(ext) > sizeof(ext.mapping));
182         pg->mapping = ext.mapping;
183 }
184
185 static int get_page_ext(struct page *pg,
186                         unsigned int *pgroup, unsigned int *pidx)
187 {
188         union page_ext ext = { .mapping = pg->mapping };
189         struct xen_netbk *netbk;
190         unsigned int group, idx;
191
192         group = ext.e.group - 1;
193
194         if (group < 0 || group >= xen_netbk_group_nr)
195                 return 0;
196
197         netbk = &xen_netbk[group];
198
199         idx = ext.e.idx;
200
201         if ((idx < 0) || (idx >= MAX_PENDING_REQS))
202                 return 0;
203
204         if (netbk->mmap_pages[idx] != pg)
205                 return 0;
206
207         *pgroup = group;
208         *pidx = idx;
209
210         return 1;
211 }
212
213 /*
214  * This is the amount of packet we copy rather than map, so that the
215  * guest can't fiddle with the contents of the headers while we do
216  * packet processing on them (netfilter, routing, etc).
217  */
218 #define PKT_PROT_LEN    (ETH_HLEN + \
219                          VLAN_HLEN + \
220                          sizeof(struct iphdr) + MAX_IPOPTLEN + \
221                          sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE)
222
223 static u16 frag_get_pending_idx(skb_frag_t *frag)
224 {
225         return (u16)frag->page_offset;
226 }
227
228 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
229 {
230         frag->page_offset = pending_idx;
231 }
232
233 static inline pending_ring_idx_t pending_index(unsigned i)
234 {
235         return i & (MAX_PENDING_REQS-1);
236 }
237
238 static inline pending_ring_idx_t nr_pending_reqs(struct xen_netbk *netbk)
239 {
240         return MAX_PENDING_REQS -
241                 netbk->pending_prod + netbk->pending_cons;
242 }
243
244 static void xen_netbk_kick_thread(struct xen_netbk *netbk)
245 {
246         wake_up(&netbk->wq);
247 }
248
249 static int max_required_rx_slots(struct xenvif *vif)
250 {
251         int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE);
252
253         if (vif->can_sg || vif->gso || vif->gso_prefix)
254                 max += MAX_SKB_FRAGS + 1; /* extra_info + frags */
255
256         return max;
257 }
258
259 int xen_netbk_rx_ring_full(struct xenvif *vif)
260 {
261         RING_IDX peek   = vif->rx_req_cons_peek;
262         RING_IDX needed = max_required_rx_slots(vif);
263
264         return ((vif->rx.sring->req_prod - peek) < needed) ||
265                ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed);
266 }
267
268 int xen_netbk_must_stop_queue(struct xenvif *vif)
269 {
270         if (!xen_netbk_rx_ring_full(vif))
271                 return 0;
272
273         vif->rx.sring->req_event = vif->rx_req_cons_peek +
274                 max_required_rx_slots(vif);
275         mb(); /* request notification /then/ check the queue */
276
277         return xen_netbk_rx_ring_full(vif);
278 }
279
280 /*
281  * Returns true if we should start a new receive buffer instead of
282  * adding 'size' bytes to a buffer which currently contains 'offset'
283  * bytes.
284  */
285 static bool start_new_rx_buffer(int offset, unsigned long size, int head)
286 {
287         /* simple case: we have completely filled the current buffer. */
288         if (offset == MAX_BUFFER_OFFSET)
289                 return true;
290
291         /*
292          * complex case: start a fresh buffer if the current frag
293          * would overflow the current buffer but only if:
294          *     (i)   this frag would fit completely in the next buffer
295          * and (ii)  there is already some data in the current buffer
296          * and (iii) this is not the head buffer.
297          *
298          * Where:
299          * - (i) stops us splitting a frag into two copies
300          *   unless the frag is too large for a single buffer.
301          * - (ii) stops us from leaving a buffer pointlessly empty.
302          * - (iii) stops us leaving the first buffer
303          *   empty. Strictly speaking this is already covered
304          *   by (ii) but is explicitly checked because
305          *   netfront relies on the first buffer being
306          *   non-empty and can crash otherwise.
307          *
308          * This means we will effectively linearise small
309          * frags but do not needlessly split large buffers
310          * into multiple copies tend to give large frags their
311          * own buffers as before.
312          */
313         if ((offset + size > MAX_BUFFER_OFFSET) &&
314             (size <= MAX_BUFFER_OFFSET) && offset && !head)
315                 return true;
316
317         return false;
318 }
319
320 /*
321  * Figure out how many ring slots we're going to need to send @skb to
322  * the guest. This function is essentially a dry run of
323  * netbk_gop_frag_copy.
324  */
325 unsigned int xen_netbk_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
326 {
327         unsigned int count;
328         int i, copy_off;
329
330         count = DIV_ROUND_UP(skb_headlen(skb), PAGE_SIZE);
331
332         copy_off = skb_headlen(skb) % PAGE_SIZE;
333
334         if (skb_shinfo(skb)->gso_size)
335                 count++;
336
337         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
338                 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
339                 unsigned long offset = skb_shinfo(skb)->frags[i].page_offset;
340                 unsigned long bytes;
341
342                 offset &= ~PAGE_MASK;
343
344                 while (size > 0) {
345                         BUG_ON(offset >= PAGE_SIZE);
346                         BUG_ON(copy_off > MAX_BUFFER_OFFSET);
347
348                         bytes = PAGE_SIZE - offset;
349
350                         if (bytes > size)
351                                 bytes = size;
352
353                         if (start_new_rx_buffer(copy_off, bytes, 0)) {
354                                 count++;
355                                 copy_off = 0;
356                         }
357
358                         if (copy_off + bytes > MAX_BUFFER_OFFSET)
359                                 bytes = MAX_BUFFER_OFFSET - copy_off;
360
361                         copy_off += bytes;
362
363                         offset += bytes;
364                         size -= bytes;
365
366                         if (offset == PAGE_SIZE)
367                                 offset = 0;
368                 }
369         }
370         return count;
371 }
372
373 struct netrx_pending_operations {
374         unsigned copy_prod, copy_cons;
375         unsigned meta_prod, meta_cons;
376         struct gnttab_copy *copy;
377         struct netbk_rx_meta *meta;
378         int copy_off;
379         grant_ref_t copy_gref;
380 };
381
382 static struct netbk_rx_meta *get_next_rx_buffer(struct xenvif *vif,
383                                                 struct netrx_pending_operations *npo)
384 {
385         struct netbk_rx_meta *meta;
386         struct xen_netif_rx_request *req;
387
388         req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
389
390         meta = npo->meta + npo->meta_prod++;
391         meta->gso_size = 0;
392         meta->size = 0;
393         meta->id = req->id;
394
395         npo->copy_off = 0;
396         npo->copy_gref = req->gref;
397
398         return meta;
399 }
400
401 /*
402  * Set up the grant operations for this fragment. If it's a flipping
403  * interface, we also set up the unmap request from here.
404  */
405 static void netbk_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
406                                 struct netrx_pending_operations *npo,
407                                 struct page *page, unsigned long size,
408                                 unsigned long offset, int *head)
409 {
410         struct gnttab_copy *copy_gop;
411         struct netbk_rx_meta *meta;
412         /*
413          * These variables are used iff get_page_ext returns true,
414          * in which case they are guaranteed to be initialized.
415          */
416         unsigned int uninitialized_var(group), uninitialized_var(idx);
417         int foreign = get_page_ext(page, &group, &idx);
418         unsigned long bytes;
419
420         /* Data must not cross a page boundary. */
421         BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
422
423         meta = npo->meta + npo->meta_prod - 1;
424
425         /* Skip unused frames from start of page */
426         page += offset >> PAGE_SHIFT;
427         offset &= ~PAGE_MASK;
428
429         while (size > 0) {
430                 BUG_ON(offset >= PAGE_SIZE);
431                 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
432
433                 bytes = PAGE_SIZE - offset;
434
435                 if (bytes > size)
436                         bytes = size;
437
438                 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
439                         /*
440                          * Netfront requires there to be some data in the head
441                          * buffer.
442                          */
443                         BUG_ON(*head);
444
445                         meta = get_next_rx_buffer(vif, npo);
446                 }
447
448                 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
449                         bytes = MAX_BUFFER_OFFSET - npo->copy_off;
450
451                 copy_gop = npo->copy + npo->copy_prod++;
452                 copy_gop->flags = GNTCOPY_dest_gref;
453                 if (foreign) {
454                         struct xen_netbk *netbk = &xen_netbk[group];
455                         struct pending_tx_info *src_pend;
456
457                         src_pend = &netbk->pending_tx_info[idx];
458
459                         copy_gop->source.domid = src_pend->vif->domid;
460                         copy_gop->source.u.ref = src_pend->req.gref;
461                         copy_gop->flags |= GNTCOPY_source_gref;
462                 } else {
463                         void *vaddr = page_address(page);
464                         copy_gop->source.domid = DOMID_SELF;
465                         copy_gop->source.u.gmfn = virt_to_mfn(vaddr);
466                 }
467                 copy_gop->source.offset = offset;
468                 copy_gop->dest.domid = vif->domid;
469
470                 copy_gop->dest.offset = npo->copy_off;
471                 copy_gop->dest.u.ref = npo->copy_gref;
472                 copy_gop->len = bytes;
473
474                 npo->copy_off += bytes;
475                 meta->size += bytes;
476
477                 offset += bytes;
478                 size -= bytes;
479
480                 /* Next frame */
481                 if (offset == PAGE_SIZE && size) {
482                         BUG_ON(!PageCompound(page));
483                         page++;
484                         offset = 0;
485                 }
486
487                 /* Leave a gap for the GSO descriptor. */
488                 if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
489                         vif->rx.req_cons++;
490
491                 *head = 0; /* There must be something in this buffer now. */
492
493         }
494 }
495
496 /*
497  * Prepare an SKB to be transmitted to the frontend.
498  *
499  * This function is responsible for allocating grant operations, meta
500  * structures, etc.
501  *
502  * It returns the number of meta structures consumed. The number of
503  * ring slots used is always equal to the number of meta slots used
504  * plus the number of GSO descriptors used. Currently, we use either
505  * zero GSO descriptors (for non-GSO packets) or one descriptor (for
506  * frontend-side LRO).
507  */
508 static int netbk_gop_skb(struct sk_buff *skb,
509                          struct netrx_pending_operations *npo)
510 {
511         struct xenvif *vif = netdev_priv(skb->dev);
512         int nr_frags = skb_shinfo(skb)->nr_frags;
513         int i;
514         struct xen_netif_rx_request *req;
515         struct netbk_rx_meta *meta;
516         unsigned char *data;
517         int head = 1;
518         int old_meta_prod;
519
520         old_meta_prod = npo->meta_prod;
521
522         /* Set up a GSO prefix descriptor, if necessary */
523         if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
524                 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
525                 meta = npo->meta + npo->meta_prod++;
526                 meta->gso_size = skb_shinfo(skb)->gso_size;
527                 meta->size = 0;
528                 meta->id = req->id;
529         }
530
531         req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
532         meta = npo->meta + npo->meta_prod++;
533
534         if (!vif->gso_prefix)
535                 meta->gso_size = skb_shinfo(skb)->gso_size;
536         else
537                 meta->gso_size = 0;
538
539         meta->size = 0;
540         meta->id = req->id;
541         npo->copy_off = 0;
542         npo->copy_gref = req->gref;
543
544         data = skb->data;
545         while (data < skb_tail_pointer(skb)) {
546                 unsigned int offset = offset_in_page(data);
547                 unsigned int len = PAGE_SIZE - offset;
548
549                 if (data + len > skb_tail_pointer(skb))
550                         len = skb_tail_pointer(skb) - data;
551
552                 netbk_gop_frag_copy(vif, skb, npo,
553                                     virt_to_page(data), len, offset, &head);
554                 data += len;
555         }
556
557         for (i = 0; i < nr_frags; i++) {
558                 netbk_gop_frag_copy(vif, skb, npo,
559                                     skb_frag_page(&skb_shinfo(skb)->frags[i]),
560                                     skb_frag_size(&skb_shinfo(skb)->frags[i]),
561                                     skb_shinfo(skb)->frags[i].page_offset,
562                                     &head);
563         }
564
565         return npo->meta_prod - old_meta_prod;
566 }
567
568 /*
569  * This is a twin to netbk_gop_skb.  Assume that netbk_gop_skb was
570  * used to set up the operations on the top of
571  * netrx_pending_operations, which have since been done.  Check that
572  * they didn't give any errors and advance over them.
573  */
574 static int netbk_check_gop(struct xenvif *vif, int nr_meta_slots,
575                            struct netrx_pending_operations *npo)
576 {
577         struct gnttab_copy     *copy_op;
578         int status = XEN_NETIF_RSP_OKAY;
579         int i;
580
581         for (i = 0; i < nr_meta_slots; i++) {
582                 copy_op = npo->copy + npo->copy_cons++;
583                 if (copy_op->status != GNTST_okay) {
584                         netdev_dbg(vif->dev,
585                                    "Bad status %d from copy to DOM%d.\n",
586                                    copy_op->status, vif->domid);
587                         status = XEN_NETIF_RSP_ERROR;
588                 }
589         }
590
591         return status;
592 }
593
594 static void netbk_add_frag_responses(struct xenvif *vif, int status,
595                                      struct netbk_rx_meta *meta,
596                                      int nr_meta_slots)
597 {
598         int i;
599         unsigned long offset;
600
601         /* No fragments used */
602         if (nr_meta_slots <= 1)
603                 return;
604
605         nr_meta_slots--;
606
607         for (i = 0; i < nr_meta_slots; i++) {
608                 int flags;
609                 if (i == nr_meta_slots - 1)
610                         flags = 0;
611                 else
612                         flags = XEN_NETRXF_more_data;
613
614                 offset = 0;
615                 make_rx_response(vif, meta[i].id, status, offset,
616                                  meta[i].size, flags);
617         }
618 }
619
620 struct skb_cb_overlay {
621         int meta_slots_used;
622 };
623
624 static void xen_netbk_rx_action(struct xen_netbk *netbk)
625 {
626         struct xenvif *vif = NULL, *tmp;
627         s8 status;
628         u16 irq, flags;
629         struct xen_netif_rx_response *resp;
630         struct sk_buff_head rxq;
631         struct sk_buff *skb;
632         LIST_HEAD(notify);
633         int ret;
634         int nr_frags;
635         int count;
636         unsigned long offset;
637         struct skb_cb_overlay *sco;
638
639         struct netrx_pending_operations npo = {
640                 .copy  = netbk->grant_copy_op,
641                 .meta  = netbk->meta,
642         };
643
644         skb_queue_head_init(&rxq);
645
646         count = 0;
647
648         while ((skb = skb_dequeue(&netbk->rx_queue)) != NULL) {
649                 vif = netdev_priv(skb->dev);
650                 nr_frags = skb_shinfo(skb)->nr_frags;
651
652                 sco = (struct skb_cb_overlay *)skb->cb;
653                 sco->meta_slots_used = netbk_gop_skb(skb, &npo);
654
655                 count += nr_frags + 1;
656
657                 __skb_queue_tail(&rxq, skb);
658
659                 /* Filled the batch queue? */
660                 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
661                         break;
662         }
663
664         BUG_ON(npo.meta_prod > ARRAY_SIZE(netbk->meta));
665
666         if (!npo.copy_prod)
667                 return;
668
669         BUG_ON(npo.copy_prod > ARRAY_SIZE(netbk->grant_copy_op));
670         gnttab_batch_copy(netbk->grant_copy_op, npo.copy_prod);
671
672         while ((skb = __skb_dequeue(&rxq)) != NULL) {
673                 sco = (struct skb_cb_overlay *)skb->cb;
674
675                 vif = netdev_priv(skb->dev);
676
677                 if (netbk->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
678                         resp = RING_GET_RESPONSE(&vif->rx,
679                                                 vif->rx.rsp_prod_pvt++);
680
681                         resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
682
683                         resp->offset = netbk->meta[npo.meta_cons].gso_size;
684                         resp->id = netbk->meta[npo.meta_cons].id;
685                         resp->status = sco->meta_slots_used;
686
687                         npo.meta_cons++;
688                         sco->meta_slots_used--;
689                 }
690
691
692                 vif->dev->stats.tx_bytes += skb->len;
693                 vif->dev->stats.tx_packets++;
694
695                 status = netbk_check_gop(vif, sco->meta_slots_used, &npo);
696
697                 if (sco->meta_slots_used == 1)
698                         flags = 0;
699                 else
700                         flags = XEN_NETRXF_more_data;
701
702                 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
703                         flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
704                 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
705                         /* remote but checksummed. */
706                         flags |= XEN_NETRXF_data_validated;
707
708                 offset = 0;
709                 resp = make_rx_response(vif, netbk->meta[npo.meta_cons].id,
710                                         status, offset,
711                                         netbk->meta[npo.meta_cons].size,
712                                         flags);
713
714                 if (netbk->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
715                         struct xen_netif_extra_info *gso =
716                                 (struct xen_netif_extra_info *)
717                                 RING_GET_RESPONSE(&vif->rx,
718                                                   vif->rx.rsp_prod_pvt++);
719
720                         resp->flags |= XEN_NETRXF_extra_info;
721
722                         gso->u.gso.size = netbk->meta[npo.meta_cons].gso_size;
723                         gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
724                         gso->u.gso.pad = 0;
725                         gso->u.gso.features = 0;
726
727                         gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
728                         gso->flags = 0;
729                 }
730
731                 netbk_add_frag_responses(vif, status,
732                                          netbk->meta + npo.meta_cons + 1,
733                                          sco->meta_slots_used);
734
735                 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
736                 irq = vif->irq;
737                 if (ret && list_empty(&vif->notify_list))
738                         list_add_tail(&vif->notify_list, &notify);
739
740                 xenvif_notify_tx_completion(vif);
741
742                 xenvif_put(vif);
743                 npo.meta_cons += sco->meta_slots_used;
744                 dev_kfree_skb(skb);
745         }
746
747         list_for_each_entry_safe(vif, tmp, &notify, notify_list) {
748                 notify_remote_via_irq(vif->irq);
749                 list_del_init(&vif->notify_list);
750         }
751
752         /* More work to do? */
753         if (!skb_queue_empty(&netbk->rx_queue) &&
754                         !timer_pending(&netbk->net_timer))
755                 xen_netbk_kick_thread(netbk);
756 }
757
758 void xen_netbk_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
759 {
760         struct xen_netbk *netbk = vif->netbk;
761
762         skb_queue_tail(&netbk->rx_queue, skb);
763
764         xen_netbk_kick_thread(netbk);
765 }
766
767 static void xen_netbk_alarm(unsigned long data)
768 {
769         struct xen_netbk *netbk = (struct xen_netbk *)data;
770         xen_netbk_kick_thread(netbk);
771 }
772
773 static int __on_net_schedule_list(struct xenvif *vif)
774 {
775         return !list_empty(&vif->schedule_list);
776 }
777
778 /* Must be called with net_schedule_list_lock held */
779 static void remove_from_net_schedule_list(struct xenvif *vif)
780 {
781         if (likely(__on_net_schedule_list(vif))) {
782                 list_del_init(&vif->schedule_list);
783                 xenvif_put(vif);
784         }
785 }
786
787 static struct xenvif *poll_net_schedule_list(struct xen_netbk *netbk)
788 {
789         struct xenvif *vif = NULL;
790
791         spin_lock_irq(&netbk->net_schedule_list_lock);
792         if (list_empty(&netbk->net_schedule_list))
793                 goto out;
794
795         vif = list_first_entry(&netbk->net_schedule_list,
796                                struct xenvif, schedule_list);
797         if (!vif)
798                 goto out;
799
800         xenvif_get(vif);
801
802         remove_from_net_schedule_list(vif);
803 out:
804         spin_unlock_irq(&netbk->net_schedule_list_lock);
805         return vif;
806 }
807
808 void xen_netbk_schedule_xenvif(struct xenvif *vif)
809 {
810         unsigned long flags;
811         struct xen_netbk *netbk = vif->netbk;
812
813         if (__on_net_schedule_list(vif))
814                 goto kick;
815
816         spin_lock_irqsave(&netbk->net_schedule_list_lock, flags);
817         if (!__on_net_schedule_list(vif) &&
818             likely(xenvif_schedulable(vif))) {
819                 list_add_tail(&vif->schedule_list, &netbk->net_schedule_list);
820                 xenvif_get(vif);
821         }
822         spin_unlock_irqrestore(&netbk->net_schedule_list_lock, flags);
823
824 kick:
825         smp_mb();
826         if ((nr_pending_reqs(netbk) < (MAX_PENDING_REQS/2)) &&
827             !list_empty(&netbk->net_schedule_list))
828                 xen_netbk_kick_thread(netbk);
829 }
830
831 void xen_netbk_deschedule_xenvif(struct xenvif *vif)
832 {
833         struct xen_netbk *netbk = vif->netbk;
834         spin_lock_irq(&netbk->net_schedule_list_lock);
835         remove_from_net_schedule_list(vif);
836         spin_unlock_irq(&netbk->net_schedule_list_lock);
837 }
838
839 void xen_netbk_check_rx_xenvif(struct xenvif *vif)
840 {
841         int more_to_do;
842
843         RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
844
845         if (more_to_do)
846                 xen_netbk_schedule_xenvif(vif);
847 }
848
849 static void tx_add_credit(struct xenvif *vif)
850 {
851         unsigned long max_burst, max_credit;
852
853         /*
854          * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
855          * Otherwise the interface can seize up due to insufficient credit.
856          */
857         max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
858         max_burst = min(max_burst, 131072UL);
859         max_burst = max(max_burst, vif->credit_bytes);
860
861         /* Take care that adding a new chunk of credit doesn't wrap to zero. */
862         max_credit = vif->remaining_credit + vif->credit_bytes;
863         if (max_credit < vif->remaining_credit)
864                 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
865
866         vif->remaining_credit = min(max_credit, max_burst);
867 }
868
869 static void tx_credit_callback(unsigned long data)
870 {
871         struct xenvif *vif = (struct xenvif *)data;
872         tx_add_credit(vif);
873         xen_netbk_check_rx_xenvif(vif);
874 }
875
876 static void netbk_tx_err(struct xenvif *vif,
877                          struct xen_netif_tx_request *txp, RING_IDX end)
878 {
879         RING_IDX cons = vif->tx.req_cons;
880
881         do {
882                 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
883                 if (cons == end)
884                         break;
885                 txp = RING_GET_REQUEST(&vif->tx, cons++);
886         } while (1);
887         vif->tx.req_cons = cons;
888         xen_netbk_check_rx_xenvif(vif);
889         xenvif_put(vif);
890 }
891
892 static void netbk_fatal_tx_err(struct xenvif *vif)
893 {
894         netdev_err(vif->dev, "fatal error; disabling device\n");
895         xenvif_carrier_off(vif);
896         xenvif_put(vif);
897 }
898
899 static int netbk_count_requests(struct xenvif *vif,
900                                 struct xen_netif_tx_request *first,
901                                 struct xen_netif_tx_request *txp,
902                                 int work_to_do)
903 {
904         RING_IDX cons = vif->tx.req_cons;
905         int frags = 0;
906
907         if (!(first->flags & XEN_NETTXF_more_data))
908                 return 0;
909
910         do {
911                 if (frags >= work_to_do) {
912                         netdev_err(vif->dev, "Need more frags\n");
913                         netbk_fatal_tx_err(vif);
914                         return -ENODATA;
915                 }
916
917                 if (unlikely(frags >= MAX_SKB_FRAGS)) {
918                         netdev_err(vif->dev, "Too many frags\n");
919                         netbk_fatal_tx_err(vif);
920                         return -E2BIG;
921                 }
922
923                 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + frags),
924                        sizeof(*txp));
925                 if (txp->size > first->size) {
926                         netdev_err(vif->dev, "Frag is bigger than frame.\n");
927                         netbk_fatal_tx_err(vif);
928                         return -EIO;
929                 }
930
931                 first->size -= txp->size;
932                 frags++;
933
934                 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
935                         netdev_err(vif->dev, "txp->offset: %x, size: %u\n",
936                                  txp->offset, txp->size);
937                         netbk_fatal_tx_err(vif);
938                         return -EINVAL;
939                 }
940         } while ((txp++)->flags & XEN_NETTXF_more_data);
941         return frags;
942 }
943
944 static struct page *xen_netbk_alloc_page(struct xen_netbk *netbk,
945                                          u16 pending_idx)
946 {
947         struct page *page;
948         page = alloc_page(GFP_KERNEL|__GFP_COLD);
949         if (!page)
950                 return NULL;
951         set_page_ext(page, netbk, pending_idx);
952         netbk->mmap_pages[pending_idx] = page;
953         return page;
954 }
955
956 static struct gnttab_copy *xen_netbk_get_requests(struct xen_netbk *netbk,
957                                                   struct xenvif *vif,
958                                                   struct sk_buff *skb,
959                                                   struct xen_netif_tx_request *txp,
960                                                   struct gnttab_copy *gop)
961 {
962         struct skb_shared_info *shinfo = skb_shinfo(skb);
963         skb_frag_t *frags = shinfo->frags;
964         u16 pending_idx = *((u16 *)skb->data);
965         int i, start;
966
967         /* Skip first skb fragment if it is on same page as header fragment. */
968         start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
969
970         for (i = start; i < shinfo->nr_frags; i++, txp++) {
971                 struct page *page;
972                 pending_ring_idx_t index;
973                 struct pending_tx_info *pending_tx_info =
974                         netbk->pending_tx_info;
975
976                 index = pending_index(netbk->pending_cons++);
977                 pending_idx = netbk->pending_ring[index];
978                 page = xen_netbk_alloc_page(netbk, pending_idx);
979                 if (!page)
980                         goto err;
981
982                 gop->source.u.ref = txp->gref;
983                 gop->source.domid = vif->domid;
984                 gop->source.offset = txp->offset;
985
986                 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
987                 gop->dest.domid = DOMID_SELF;
988                 gop->dest.offset = txp->offset;
989
990                 gop->len = txp->size;
991                 gop->flags = GNTCOPY_source_gref;
992
993                 gop++;
994
995                 memcpy(&pending_tx_info[pending_idx].req, txp, sizeof(*txp));
996                 xenvif_get(vif);
997                 pending_tx_info[pending_idx].vif = vif;
998                 frag_set_pending_idx(&frags[i], pending_idx);
999         }
1000
1001         return gop;
1002 err:
1003         /* Unwind, freeing all pages and sending error responses. */
1004         while (i-- > start) {
1005                 xen_netbk_idx_release(netbk, frag_get_pending_idx(&frags[i]),
1006                                       XEN_NETIF_RSP_ERROR);
1007         }
1008         /* The head too, if necessary. */
1009         if (start)
1010                 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1011
1012         return NULL;
1013 }
1014
1015 static int xen_netbk_tx_check_gop(struct xen_netbk *netbk,
1016                                   struct sk_buff *skb,
1017                                   struct gnttab_copy **gopp)
1018 {
1019         struct gnttab_copy *gop = *gopp;
1020         u16 pending_idx = *((u16 *)skb->data);
1021         struct skb_shared_info *shinfo = skb_shinfo(skb);
1022         int nr_frags = shinfo->nr_frags;
1023         int i, err, start;
1024
1025         /* Check status of header. */
1026         err = gop->status;
1027         if (unlikely(err))
1028                 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1029
1030         /* Skip first skb fragment if it is on same page as header fragment. */
1031         start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
1032
1033         for (i = start; i < nr_frags; i++) {
1034                 int j, newerr;
1035
1036                 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
1037
1038                 /* Check error status: if okay then remember grant handle. */
1039                 newerr = (++gop)->status;
1040                 if (likely(!newerr)) {
1041                         /* Had a previous error? Invalidate this fragment. */
1042                         if (unlikely(err))
1043                                 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1044                         continue;
1045                 }
1046
1047                 /* Error on this fragment: respond to client with an error. */
1048                 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_ERROR);
1049
1050                 /* Not the first error? Preceding frags already invalidated. */
1051                 if (err)
1052                         continue;
1053
1054                 /* First error: invalidate header and preceding fragments. */
1055                 pending_idx = *((u16 *)skb->data);
1056                 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1057                 for (j = start; j < i; j++) {
1058                         pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
1059                         xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1060                 }
1061
1062                 /* Remember the error: invalidate all subsequent fragments. */
1063                 err = newerr;
1064         }
1065
1066         *gopp = gop + 1;
1067         return err;
1068 }
1069
1070 static void xen_netbk_fill_frags(struct xen_netbk *netbk, struct sk_buff *skb)
1071 {
1072         struct skb_shared_info *shinfo = skb_shinfo(skb);
1073         int nr_frags = shinfo->nr_frags;
1074         int i;
1075
1076         for (i = 0; i < nr_frags; i++) {
1077                 skb_frag_t *frag = shinfo->frags + i;
1078                 struct xen_netif_tx_request *txp;
1079                 struct page *page;
1080                 u16 pending_idx;
1081
1082                 pending_idx = frag_get_pending_idx(frag);
1083
1084                 txp = &netbk->pending_tx_info[pending_idx].req;
1085                 page = virt_to_page(idx_to_kaddr(netbk, pending_idx));
1086                 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
1087                 skb->len += txp->size;
1088                 skb->data_len += txp->size;
1089                 skb->truesize += txp->size;
1090
1091                 /* Take an extra reference to offset xen_netbk_idx_release */
1092                 get_page(netbk->mmap_pages[pending_idx]);
1093                 xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1094         }
1095 }
1096
1097 static int xen_netbk_get_extras(struct xenvif *vif,
1098                                 struct xen_netif_extra_info *extras,
1099                                 int work_to_do)
1100 {
1101         struct xen_netif_extra_info extra;
1102         RING_IDX cons = vif->tx.req_cons;
1103
1104         do {
1105                 if (unlikely(work_to_do-- <= 0)) {
1106                         netdev_err(vif->dev, "Missing extra info\n");
1107                         netbk_fatal_tx_err(vif);
1108                         return -EBADR;
1109                 }
1110
1111                 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1112                        sizeof(extra));
1113                 if (unlikely(!extra.type ||
1114                              extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1115                         vif->tx.req_cons = ++cons;
1116                         netdev_err(vif->dev,
1117                                    "Invalid extra type: %d\n", extra.type);
1118                         netbk_fatal_tx_err(vif);
1119                         return -EINVAL;
1120                 }
1121
1122                 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1123                 vif->tx.req_cons = ++cons;
1124         } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1125
1126         return work_to_do;
1127 }
1128
1129 static int netbk_set_skb_gso(struct xenvif *vif,
1130                              struct sk_buff *skb,
1131                              struct xen_netif_extra_info *gso)
1132 {
1133         if (!gso->u.gso.size) {
1134                 netdev_err(vif->dev, "GSO size must not be zero.\n");
1135                 netbk_fatal_tx_err(vif);
1136                 return -EINVAL;
1137         }
1138
1139         /* Currently only TCPv4 S.O. is supported. */
1140         if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
1141                 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
1142                 netbk_fatal_tx_err(vif);
1143                 return -EINVAL;
1144         }
1145
1146         skb_shinfo(skb)->gso_size = gso->u.gso.size;
1147         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1148
1149         /* Header must be checked, and gso_segs computed. */
1150         skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1151         skb_shinfo(skb)->gso_segs = 0;
1152
1153         return 0;
1154 }
1155
1156 static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1157 {
1158         struct iphdr *iph;
1159         int err = -EPROTO;
1160         int recalculate_partial_csum = 0;
1161
1162         /*
1163          * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1164          * peers can fail to set NETRXF_csum_blank when sending a GSO
1165          * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1166          * recalculate the partial checksum.
1167          */
1168         if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1169                 vif->rx_gso_checksum_fixup++;
1170                 skb->ip_summed = CHECKSUM_PARTIAL;
1171                 recalculate_partial_csum = 1;
1172         }
1173
1174         /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1175         if (skb->ip_summed != CHECKSUM_PARTIAL)
1176                 return 0;
1177
1178         if (skb->protocol != htons(ETH_P_IP))
1179                 goto out;
1180
1181         iph = (void *)skb->data;
1182         switch (iph->protocol) {
1183         case IPPROTO_TCP:
1184                 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1185                                           offsetof(struct tcphdr, check)))
1186                         goto out;
1187
1188                 if (recalculate_partial_csum) {
1189                         struct tcphdr *tcph = tcp_hdr(skb);
1190                         tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1191                                                          skb->len - iph->ihl*4,
1192                                                          IPPROTO_TCP, 0);
1193                 }
1194                 break;
1195         case IPPROTO_UDP:
1196                 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1197                                           offsetof(struct udphdr, check)))
1198                         goto out;
1199
1200                 if (recalculate_partial_csum) {
1201                         struct udphdr *udph = udp_hdr(skb);
1202                         udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1203                                                          skb->len - iph->ihl*4,
1204                                                          IPPROTO_UDP, 0);
1205                 }
1206                 break;
1207         default:
1208                 if (net_ratelimit())
1209                         netdev_err(vif->dev,
1210                                    "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1211                                    iph->protocol);
1212                 goto out;
1213         }
1214
1215         err = 0;
1216
1217 out:
1218         return err;
1219 }
1220
1221 static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1222 {
1223         unsigned long now = jiffies;
1224         unsigned long next_credit =
1225                 vif->credit_timeout.expires +
1226                 msecs_to_jiffies(vif->credit_usec / 1000);
1227
1228         /* Timer could already be pending in rare cases. */
1229         if (timer_pending(&vif->credit_timeout))
1230                 return true;
1231
1232         /* Passed the point where we can replenish credit? */
1233         if (time_after_eq(now, next_credit)) {
1234                 vif->credit_timeout.expires = now;
1235                 tx_add_credit(vif);
1236         }
1237
1238         /* Still too big to send right now? Set a callback. */
1239         if (size > vif->remaining_credit) {
1240                 vif->credit_timeout.data     =
1241                         (unsigned long)vif;
1242                 vif->credit_timeout.function =
1243                         tx_credit_callback;
1244                 mod_timer(&vif->credit_timeout,
1245                           next_credit);
1246
1247                 return true;
1248         }
1249
1250         return false;
1251 }
1252
1253 static unsigned xen_netbk_tx_build_gops(struct xen_netbk *netbk)
1254 {
1255         struct gnttab_copy *gop = netbk->tx_copy_ops, *request_gop;
1256         struct sk_buff *skb;
1257         int ret;
1258
1259         while (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1260                 !list_empty(&netbk->net_schedule_list)) {
1261                 struct xenvif *vif;
1262                 struct xen_netif_tx_request txreq;
1263                 struct xen_netif_tx_request txfrags[MAX_SKB_FRAGS];
1264                 struct page *page;
1265                 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1266                 u16 pending_idx;
1267                 RING_IDX idx;
1268                 int work_to_do;
1269                 unsigned int data_len;
1270                 pending_ring_idx_t index;
1271
1272                 /* Get a netif from the list with work to do. */
1273                 vif = poll_net_schedule_list(netbk);
1274                 /* This can sometimes happen because the test of
1275                  * list_empty(net_schedule_list) at the top of the
1276                  * loop is unlocked.  Just go back and have another
1277                  * look.
1278                  */
1279                 if (!vif)
1280                         continue;
1281
1282                 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1283                     XEN_NETIF_TX_RING_SIZE) {
1284                         netdev_err(vif->dev,
1285                                    "Impossible number of requests. "
1286                                    "req_prod %d, req_cons %d, size %ld\n",
1287                                    vif->tx.sring->req_prod, vif->tx.req_cons,
1288                                    XEN_NETIF_TX_RING_SIZE);
1289                         netbk_fatal_tx_err(vif);
1290                         continue;
1291                 }
1292
1293                 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
1294                 if (!work_to_do) {
1295                         xenvif_put(vif);
1296                         continue;
1297                 }
1298
1299                 idx = vif->tx.req_cons;
1300                 rmb(); /* Ensure that we see the request before we copy it. */
1301                 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1302
1303                 /* Credit-based scheduling. */
1304                 if (txreq.size > vif->remaining_credit &&
1305                     tx_credit_exceeded(vif, txreq.size)) {
1306                         xenvif_put(vif);
1307                         continue;
1308                 }
1309
1310                 vif->remaining_credit -= txreq.size;
1311
1312                 work_to_do--;
1313                 vif->tx.req_cons = ++idx;
1314
1315                 memset(extras, 0, sizeof(extras));
1316                 if (txreq.flags & XEN_NETTXF_extra_info) {
1317                         work_to_do = xen_netbk_get_extras(vif, extras,
1318                                                           work_to_do);
1319                         idx = vif->tx.req_cons;
1320                         if (unlikely(work_to_do < 0))
1321                                 continue;
1322                 }
1323
1324                 ret = netbk_count_requests(vif, &txreq, txfrags, work_to_do);
1325                 if (unlikely(ret < 0))
1326                         continue;
1327
1328                 idx += ret;
1329
1330                 if (unlikely(txreq.size < ETH_HLEN)) {
1331                         netdev_dbg(vif->dev,
1332                                    "Bad packet size: %d\n", txreq.size);
1333                         netbk_tx_err(vif, &txreq, idx);
1334                         continue;
1335                 }
1336
1337                 /* No crossing a page as the payload mustn't fragment. */
1338                 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
1339                         netdev_err(vif->dev,
1340                                    "txreq.offset: %x, size: %u, end: %lu\n",
1341                                    txreq.offset, txreq.size,
1342                                    (txreq.offset&~PAGE_MASK) + txreq.size);
1343                         netbk_fatal_tx_err(vif);
1344                         continue;
1345                 }
1346
1347                 index = pending_index(netbk->pending_cons);
1348                 pending_idx = netbk->pending_ring[index];
1349
1350                 data_len = (txreq.size > PKT_PROT_LEN &&
1351                             ret < MAX_SKB_FRAGS) ?
1352                         PKT_PROT_LEN : txreq.size;
1353
1354                 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1355                                 GFP_ATOMIC | __GFP_NOWARN);
1356                 if (unlikely(skb == NULL)) {
1357                         netdev_dbg(vif->dev,
1358                                    "Can't allocate a skb in start_xmit.\n");
1359                         netbk_tx_err(vif, &txreq, idx);
1360                         break;
1361                 }
1362
1363                 /* Packets passed to netif_rx() must have some headroom. */
1364                 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1365
1366                 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1367                         struct xen_netif_extra_info *gso;
1368                         gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1369
1370                         if (netbk_set_skb_gso(vif, skb, gso)) {
1371                                 /* Failure in netbk_set_skb_gso is fatal. */
1372                                 kfree_skb(skb);
1373                                 continue;
1374                         }
1375                 }
1376
1377                 /* XXX could copy straight to head */
1378                 page = xen_netbk_alloc_page(netbk, pending_idx);
1379                 if (!page) {
1380                         kfree_skb(skb);
1381                         netbk_tx_err(vif, &txreq, idx);
1382                         continue;
1383                 }
1384
1385                 gop->source.u.ref = txreq.gref;
1386                 gop->source.domid = vif->domid;
1387                 gop->source.offset = txreq.offset;
1388
1389                 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1390                 gop->dest.domid = DOMID_SELF;
1391                 gop->dest.offset = txreq.offset;
1392
1393                 gop->len = txreq.size;
1394                 gop->flags = GNTCOPY_source_gref;
1395
1396                 gop++;
1397
1398                 memcpy(&netbk->pending_tx_info[pending_idx].req,
1399                        &txreq, sizeof(txreq));
1400                 netbk->pending_tx_info[pending_idx].vif = vif;
1401                 *((u16 *)skb->data) = pending_idx;
1402
1403                 __skb_put(skb, data_len);
1404
1405                 skb_shinfo(skb)->nr_frags = ret;
1406                 if (data_len < txreq.size) {
1407                         skb_shinfo(skb)->nr_frags++;
1408                         frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1409                                              pending_idx);
1410                 } else {
1411                         frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1412                                              INVALID_PENDING_IDX);
1413                 }
1414
1415                 netbk->pending_cons++;
1416
1417                 request_gop = xen_netbk_get_requests(netbk, vif,
1418                                                      skb, txfrags, gop);
1419                 if (request_gop == NULL) {
1420                         kfree_skb(skb);
1421                         netbk_tx_err(vif, &txreq, idx);
1422                         continue;
1423                 }
1424                 gop = request_gop;
1425
1426                 __skb_queue_tail(&netbk->tx_queue, skb);
1427
1428                 vif->tx.req_cons = idx;
1429                 xen_netbk_check_rx_xenvif(vif);
1430
1431                 if ((gop-netbk->tx_copy_ops) >= ARRAY_SIZE(netbk->tx_copy_ops))
1432                         break;
1433         }
1434
1435         return gop - netbk->tx_copy_ops;
1436 }
1437
1438 static void xen_netbk_tx_submit(struct xen_netbk *netbk)
1439 {
1440         struct gnttab_copy *gop = netbk->tx_copy_ops;
1441         struct sk_buff *skb;
1442
1443         while ((skb = __skb_dequeue(&netbk->tx_queue)) != NULL) {
1444                 struct xen_netif_tx_request *txp;
1445                 struct xenvif *vif;
1446                 u16 pending_idx;
1447                 unsigned data_len;
1448
1449                 pending_idx = *((u16 *)skb->data);
1450                 vif = netbk->pending_tx_info[pending_idx].vif;
1451                 txp = &netbk->pending_tx_info[pending_idx].req;
1452
1453                 /* Check the remap error code. */
1454                 if (unlikely(xen_netbk_tx_check_gop(netbk, skb, &gop))) {
1455                         netdev_dbg(vif->dev, "netback grant failed.\n");
1456                         skb_shinfo(skb)->nr_frags = 0;
1457                         kfree_skb(skb);
1458                         continue;
1459                 }
1460
1461                 data_len = skb->len;
1462                 memcpy(skb->data,
1463                        (void *)(idx_to_kaddr(netbk, pending_idx)|txp->offset),
1464                        data_len);
1465                 if (data_len < txp->size) {
1466                         /* Append the packet payload as a fragment. */
1467                         txp->offset += data_len;
1468                         txp->size -= data_len;
1469                 } else {
1470                         /* Schedule a response immediately. */
1471                         xen_netbk_idx_release(netbk, pending_idx, XEN_NETIF_RSP_OKAY);
1472                 }
1473
1474                 if (txp->flags & XEN_NETTXF_csum_blank)
1475                         skb->ip_summed = CHECKSUM_PARTIAL;
1476                 else if (txp->flags & XEN_NETTXF_data_validated)
1477                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1478
1479                 xen_netbk_fill_frags(netbk, skb);
1480
1481                 /*
1482                  * If the initial fragment was < PKT_PROT_LEN then
1483                  * pull through some bytes from the other fragments to
1484                  * increase the linear region to PKT_PROT_LEN bytes.
1485                  */
1486                 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1487                         int target = min_t(int, skb->len, PKT_PROT_LEN);
1488                         __pskb_pull_tail(skb, target - skb_headlen(skb));
1489                 }
1490
1491                 skb->dev      = vif->dev;
1492                 skb->protocol = eth_type_trans(skb, skb->dev);
1493                 skb_reset_network_header(skb);
1494
1495                 if (checksum_setup(vif, skb)) {
1496                         netdev_dbg(vif->dev,
1497                                    "Can't setup checksum in net_tx_action\n");
1498                         kfree_skb(skb);
1499                         continue;
1500                 }
1501
1502                 skb_probe_transport_header(skb, 0);
1503
1504                 vif->dev->stats.rx_bytes += skb->len;
1505                 vif->dev->stats.rx_packets++;
1506
1507                 xenvif_receive_skb(vif, skb);
1508         }
1509 }
1510
1511 /* Called after netfront has transmitted */
1512 static void xen_netbk_tx_action(struct xen_netbk *netbk)
1513 {
1514         unsigned nr_gops;
1515
1516         nr_gops = xen_netbk_tx_build_gops(netbk);
1517
1518         if (nr_gops == 0)
1519                 return;
1520
1521         gnttab_batch_copy(netbk->tx_copy_ops, nr_gops);
1522
1523         xen_netbk_tx_submit(netbk);
1524 }
1525
1526 static void xen_netbk_idx_release(struct xen_netbk *netbk, u16 pending_idx,
1527                                   u8 status)
1528 {
1529         struct xenvif *vif;
1530         struct pending_tx_info *pending_tx_info;
1531         pending_ring_idx_t index;
1532
1533         /* Already complete? */
1534         if (netbk->mmap_pages[pending_idx] == NULL)
1535                 return;
1536
1537         pending_tx_info = &netbk->pending_tx_info[pending_idx];
1538
1539         vif = pending_tx_info->vif;
1540
1541         make_tx_response(vif, &pending_tx_info->req, status);
1542
1543         index = pending_index(netbk->pending_prod++);
1544         netbk->pending_ring[index] = pending_idx;
1545
1546         xenvif_put(vif);
1547
1548         netbk->mmap_pages[pending_idx]->mapping = NULL;
1549         put_page(netbk->mmap_pages[pending_idx]);
1550         netbk->mmap_pages[pending_idx] = NULL;
1551 }
1552
1553 static void make_tx_response(struct xenvif *vif,
1554                              struct xen_netif_tx_request *txp,
1555                              s8       st)
1556 {
1557         RING_IDX i = vif->tx.rsp_prod_pvt;
1558         struct xen_netif_tx_response *resp;
1559         int notify;
1560
1561         resp = RING_GET_RESPONSE(&vif->tx, i);
1562         resp->id     = txp->id;
1563         resp->status = st;
1564
1565         if (txp->flags & XEN_NETTXF_extra_info)
1566                 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1567
1568         vif->tx.rsp_prod_pvt = ++i;
1569         RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1570         if (notify)
1571                 notify_remote_via_irq(vif->irq);
1572 }
1573
1574 static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1575                                              u16      id,
1576                                              s8       st,
1577                                              u16      offset,
1578                                              u16      size,
1579                                              u16      flags)
1580 {
1581         RING_IDX i = vif->rx.rsp_prod_pvt;
1582         struct xen_netif_rx_response *resp;
1583
1584         resp = RING_GET_RESPONSE(&vif->rx, i);
1585         resp->offset     = offset;
1586         resp->flags      = flags;
1587         resp->id         = id;
1588         resp->status     = (s16)size;
1589         if (st < 0)
1590                 resp->status = (s16)st;
1591
1592         vif->rx.rsp_prod_pvt = ++i;
1593
1594         return resp;
1595 }
1596
1597 static inline int rx_work_todo(struct xen_netbk *netbk)
1598 {
1599         return !skb_queue_empty(&netbk->rx_queue);
1600 }
1601
1602 static inline int tx_work_todo(struct xen_netbk *netbk)
1603 {
1604
1605         if (((nr_pending_reqs(netbk) + MAX_SKB_FRAGS) < MAX_PENDING_REQS) &&
1606                         !list_empty(&netbk->net_schedule_list))
1607                 return 1;
1608
1609         return 0;
1610 }
1611
1612 static int xen_netbk_kthread(void *data)
1613 {
1614         struct xen_netbk *netbk = data;
1615         while (!kthread_should_stop()) {
1616                 wait_event_interruptible(netbk->wq,
1617                                 rx_work_todo(netbk) ||
1618                                 tx_work_todo(netbk) ||
1619                                 kthread_should_stop());
1620                 cond_resched();
1621
1622                 if (kthread_should_stop())
1623                         break;
1624
1625                 if (rx_work_todo(netbk))
1626                         xen_netbk_rx_action(netbk);
1627
1628                 if (tx_work_todo(netbk))
1629                         xen_netbk_tx_action(netbk);
1630         }
1631
1632         return 0;
1633 }
1634
1635 void xen_netbk_unmap_frontend_rings(struct xenvif *vif)
1636 {
1637         if (vif->tx.sring)
1638                 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1639                                         vif->tx.sring);
1640         if (vif->rx.sring)
1641                 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1642                                         vif->rx.sring);
1643 }
1644
1645 int xen_netbk_map_frontend_rings(struct xenvif *vif,
1646                                  grant_ref_t tx_ring_ref,
1647                                  grant_ref_t rx_ring_ref)
1648 {
1649         void *addr;
1650         struct xen_netif_tx_sring *txs;
1651         struct xen_netif_rx_sring *rxs;
1652
1653         int err = -ENOMEM;
1654
1655         err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1656                                      tx_ring_ref, &addr);
1657         if (err)
1658                 goto err;
1659
1660         txs = (struct xen_netif_tx_sring *)addr;
1661         BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1662
1663         err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1664                                      rx_ring_ref, &addr);
1665         if (err)
1666                 goto err;
1667
1668         rxs = (struct xen_netif_rx_sring *)addr;
1669         BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1670
1671         vif->rx_req_cons_peek = 0;
1672
1673         return 0;
1674
1675 err:
1676         xen_netbk_unmap_frontend_rings(vif);
1677         return err;
1678 }
1679
1680 static int __init netback_init(void)
1681 {
1682         int i;
1683         int rc = 0;
1684         int group;
1685
1686         if (!xen_domain())
1687                 return -ENODEV;
1688
1689         xen_netbk_group_nr = num_online_cpus();
1690         xen_netbk = vzalloc(sizeof(struct xen_netbk) * xen_netbk_group_nr);
1691         if (!xen_netbk)
1692                 return -ENOMEM;
1693
1694         for (group = 0; group < xen_netbk_group_nr; group++) {
1695                 struct xen_netbk *netbk = &xen_netbk[group];
1696                 skb_queue_head_init(&netbk->rx_queue);
1697                 skb_queue_head_init(&netbk->tx_queue);
1698
1699                 init_timer(&netbk->net_timer);
1700                 netbk->net_timer.data = (unsigned long)netbk;
1701                 netbk->net_timer.function = xen_netbk_alarm;
1702
1703                 netbk->pending_cons = 0;
1704                 netbk->pending_prod = MAX_PENDING_REQS;
1705                 for (i = 0; i < MAX_PENDING_REQS; i++)
1706                         netbk->pending_ring[i] = i;
1707
1708                 init_waitqueue_head(&netbk->wq);
1709                 netbk->task = kthread_create(xen_netbk_kthread,
1710                                              (void *)netbk,
1711                                              "netback/%u", group);
1712
1713                 if (IS_ERR(netbk->task)) {
1714                         printk(KERN_ALERT "kthread_create() fails at netback\n");
1715                         del_timer(&netbk->net_timer);
1716                         rc = PTR_ERR(netbk->task);
1717                         goto failed_init;
1718                 }
1719
1720                 kthread_bind(netbk->task, group);
1721
1722                 INIT_LIST_HEAD(&netbk->net_schedule_list);
1723
1724                 spin_lock_init(&netbk->net_schedule_list_lock);
1725
1726                 atomic_set(&netbk->netfront_count, 0);
1727
1728                 wake_up_process(netbk->task);
1729         }
1730
1731         rc = xenvif_xenbus_init();
1732         if (rc)
1733                 goto failed_init;
1734
1735         return 0;
1736
1737 failed_init:
1738         while (--group >= 0) {
1739                 struct xen_netbk *netbk = &xen_netbk[group];
1740                 for (i = 0; i < MAX_PENDING_REQS; i++) {
1741                         if (netbk->mmap_pages[i])
1742                                 __free_page(netbk->mmap_pages[i]);
1743                 }
1744                 del_timer(&netbk->net_timer);
1745                 kthread_stop(netbk->task);
1746         }
1747         vfree(xen_netbk);
1748         return rc;
1749
1750 }
1751
1752 module_init(netback_init);
1753
1754 MODULE_LICENSE("Dual BSD/GPL");
1755 MODULE_ALIAS("xen-backend:vif");