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