cxgb4: Changed FW check version to match FW binary version
[linux-2.6-block.git] / drivers / net / xen-netback / netback.c
CommitLineData
f942dc25
IC
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>
ae5e8127 42#include <net/ip6_checksum.h>
f942dc25 43
ca981633 44#include <xen/xen.h>
f942dc25
IC
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
e1f00a69
WL
51/* Provide an option to disable split event channels at load time as
52 * event channels are limited resource. Split event channels are
53 * enabled by default.
54 */
55bool separate_tx_rx_irq = 1;
56module_param(separate_tx_rx_irq, bool, 0644);
57
2810e5b9
WL
58/*
59 * This is the maximum slots a skb can have. If a guest sends a skb
60 * which exceeds this limit it is considered malicious.
61 */
37641494
WL
62#define FATAL_SKB_SLOTS_DEFAULT 20
63static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
64module_param(fatal_skb_slots, uint, 0444);
65
66/*
67 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
68 * the maximum slots a valid packet can use. Now this value is defined
69 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
70 * all backend.
71 */
72#define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
2810e5b9 73
2810e5b9
WL
74/*
75 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
76 * one or more merged tx requests, otherwise it is the continuation of
77 * previous tx request.
78 */
b3f980bd 79static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
f942dc25 80{
b3f980bd 81 return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
f942dc25
IC
82}
83
7376419a
WL
84static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
85 u8 status);
86
f942dc25
IC
87static void make_tx_response(struct xenvif *vif,
88 struct xen_netif_tx_request *txp,
89 s8 st);
b3f980bd
WL
90
91static inline int tx_work_todo(struct xenvif *vif);
92static inline int rx_work_todo(struct xenvif *vif);
93
f942dc25
IC
94static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
95 u16 id,
96 s8 st,
97 u16 offset,
98 u16 size,
99 u16 flags);
100
b3f980bd 101static inline unsigned long idx_to_pfn(struct xenvif *vif,
ea066ad1 102 u16 idx)
f942dc25 103{
b3f980bd 104 return page_to_pfn(vif->mmap_pages[idx]);
f942dc25
IC
105}
106
b3f980bd 107static inline unsigned long idx_to_kaddr(struct xenvif *vif,
ea066ad1 108 u16 idx)
f942dc25 109{
b3f980bd 110 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
f942dc25
IC
111}
112
2eba61d5
PD
113/* This is a miniumum size for the linear area to avoid lots of
114 * calls to __pskb_pull_tail() as we set up checksum offsets. The
115 * value 128 was chosen as it covers all IPv4 and most likely
116 * IPv6 headers.
f942dc25 117 */
2eba61d5 118#define PKT_PROT_LEN 128
f942dc25 119
ea066ad1
IC
120static u16 frag_get_pending_idx(skb_frag_t *frag)
121{
122 return (u16)frag->page_offset;
123}
124
125static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
126{
127 frag->page_offset = pending_idx;
128}
129
f942dc25
IC
130static inline pending_ring_idx_t pending_index(unsigned i)
131{
132 return i & (MAX_PENDING_REQS-1);
133}
134
b3f980bd 135static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
f942dc25
IC
136{
137 return MAX_PENDING_REQS -
b3f980bd 138 vif->pending_prod + vif->pending_cons;
f942dc25
IC
139}
140
ca2f09f2 141bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
f942dc25 142{
ca2f09f2 143 RING_IDX prod, cons;
f942dc25 144
ca2f09f2
PD
145 do {
146 prod = vif->rx.sring->req_prod;
147 cons = vif->rx.req_cons;
f942dc25 148
ca2f09f2
PD
149 if (prod - cons >= needed)
150 return true;
f942dc25 151
ca2f09f2 152 vif->rx.sring->req_event = prod + 1;
f942dc25 153
ca2f09f2
PD
154 /* Make sure event is visible before we check prod
155 * again.
156 */
157 mb();
158 } while (vif->rx.sring->req_prod != prod);
f942dc25 159
ca2f09f2 160 return false;
f942dc25
IC
161}
162
163/*
164 * Returns true if we should start a new receive buffer instead of
165 * adding 'size' bytes to a buffer which currently contains 'offset'
166 * bytes.
167 */
168static bool start_new_rx_buffer(int offset, unsigned long size, int head)
169{
170 /* simple case: we have completely filled the current buffer. */
171 if (offset == MAX_BUFFER_OFFSET)
172 return true;
173
174 /*
175 * complex case: start a fresh buffer if the current frag
176 * would overflow the current buffer but only if:
177 * (i) this frag would fit completely in the next buffer
178 * and (ii) there is already some data in the current buffer
179 * and (iii) this is not the head buffer.
180 *
181 * Where:
182 * - (i) stops us splitting a frag into two copies
183 * unless the frag is too large for a single buffer.
184 * - (ii) stops us from leaving a buffer pointlessly empty.
185 * - (iii) stops us leaving the first buffer
186 * empty. Strictly speaking this is already covered
187 * by (ii) but is explicitly checked because
188 * netfront relies on the first buffer being
189 * non-empty and can crash otherwise.
190 *
191 * This means we will effectively linearise small
192 * frags but do not needlessly split large buffers
193 * into multiple copies tend to give large frags their
194 * own buffers as before.
195 */
196 if ((offset + size > MAX_BUFFER_OFFSET) &&
197 (size <= MAX_BUFFER_OFFSET) && offset && !head)
198 return true;
199
200 return false;
201}
202
f942dc25
IC
203struct netrx_pending_operations {
204 unsigned copy_prod, copy_cons;
205 unsigned meta_prod, meta_cons;
206 struct gnttab_copy *copy;
b3f980bd 207 struct xenvif_rx_meta *meta;
f942dc25
IC
208 int copy_off;
209 grant_ref_t copy_gref;
210};
211
b3f980bd
WL
212static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
213 struct netrx_pending_operations *npo)
f942dc25 214{
b3f980bd 215 struct xenvif_rx_meta *meta;
f942dc25
IC
216 struct xen_netif_rx_request *req;
217
218 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
219
220 meta = npo->meta + npo->meta_prod++;
82cada22 221 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
f942dc25
IC
222 meta->gso_size = 0;
223 meta->size = 0;
224 meta->id = req->id;
225
226 npo->copy_off = 0;
227 npo->copy_gref = req->gref;
228
229 return meta;
230}
231
33bc801d
WL
232/*
233 * Set up the grant operations for this fragment. If it's a flipping
234 * interface, we also set up the unmap request from here.
235 */
7376419a
WL
236static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
237 struct netrx_pending_operations *npo,
238 struct page *page, unsigned long size,
33bc801d 239 unsigned long offset, int *head)
f942dc25
IC
240{
241 struct gnttab_copy *copy_gop;
b3f980bd 242 struct xenvif_rx_meta *meta;
f942dc25 243 unsigned long bytes;
82cada22 244 int gso_type;
f942dc25
IC
245
246 /* Data must not cross a page boundary. */
6a8ed462 247 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
f942dc25
IC
248
249 meta = npo->meta + npo->meta_prod - 1;
250
6a8ed462
IC
251 /* Skip unused frames from start of page */
252 page += offset >> PAGE_SHIFT;
253 offset &= ~PAGE_MASK;
254
f942dc25 255 while (size > 0) {
6a8ed462 256 BUG_ON(offset >= PAGE_SIZE);
f942dc25
IC
257 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
258
6a8ed462
IC
259 bytes = PAGE_SIZE - offset;
260
261 if (bytes > size)
262 bytes = size;
263
33bc801d 264 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
f942dc25
IC
265 /*
266 * Netfront requires there to be some data in the head
267 * buffer.
268 */
33bc801d 269 BUG_ON(*head);
f942dc25
IC
270
271 meta = get_next_rx_buffer(vif, npo);
272 }
273
f942dc25
IC
274 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
275 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
276
277 copy_gop = npo->copy + npo->copy_prod++;
278 copy_gop->flags = GNTCOPY_dest_gref;
b3f980bd
WL
279 copy_gop->len = bytes;
280
43e9d194
WL
281 copy_gop->source.domid = DOMID_SELF;
282 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
f942dc25 283 copy_gop->source.offset = offset;
f942dc25 284
b3f980bd 285 copy_gop->dest.domid = vif->domid;
f942dc25
IC
286 copy_gop->dest.offset = npo->copy_off;
287 copy_gop->dest.u.ref = npo->copy_gref;
f942dc25
IC
288
289 npo->copy_off += bytes;
290 meta->size += bytes;
291
292 offset += bytes;
293 size -= bytes;
294
6a8ed462
IC
295 /* Next frame */
296 if (offset == PAGE_SIZE && size) {
297 BUG_ON(!PageCompound(page));
298 page++;
299 offset = 0;
300 }
301
f942dc25 302 /* Leave a gap for the GSO descriptor. */
82cada22
PD
303 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
304 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
305 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
306 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
307 else
308 gso_type = XEN_NETIF_GSO_TYPE_NONE;
309
310 if (*head && ((1 << gso_type) & vif->gso_mask))
f942dc25
IC
311 vif->rx.req_cons++;
312
33bc801d 313 *head = 0; /* There must be something in this buffer now. */
f942dc25
IC
314
315 }
316}
317
318/*
319 * Prepare an SKB to be transmitted to the frontend.
320 *
321 * This function is responsible for allocating grant operations, meta
322 * structures, etc.
323 *
324 * It returns the number of meta structures consumed. The number of
325 * ring slots used is always equal to the number of meta slots used
326 * plus the number of GSO descriptors used. Currently, we use either
327 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
328 * frontend-side LRO).
329 */
7376419a
WL
330static int xenvif_gop_skb(struct sk_buff *skb,
331 struct netrx_pending_operations *npo)
f942dc25
IC
332{
333 struct xenvif *vif = netdev_priv(skb->dev);
334 int nr_frags = skb_shinfo(skb)->nr_frags;
335 int i;
336 struct xen_netif_rx_request *req;
b3f980bd 337 struct xenvif_rx_meta *meta;
f942dc25 338 unsigned char *data;
33bc801d 339 int head = 1;
f942dc25 340 int old_meta_prod;
82cada22
PD
341 int gso_type;
342 int gso_size;
f942dc25
IC
343
344 old_meta_prod = npo->meta_prod;
345
82cada22
PD
346 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
347 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
348 gso_size = skb_shinfo(skb)->gso_size;
349 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
350 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
351 gso_size = skb_shinfo(skb)->gso_size;
352 } else {
353 gso_type = XEN_NETIF_GSO_TYPE_NONE;
354 gso_size = 0;
355 }
356
f942dc25 357 /* Set up a GSO prefix descriptor, if necessary */
a3314f3d 358 if ((1 << gso_type) & vif->gso_prefix_mask) {
f942dc25
IC
359 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
360 meta = npo->meta + npo->meta_prod++;
82cada22
PD
361 meta->gso_type = gso_type;
362 meta->gso_size = gso_size;
f942dc25
IC
363 meta->size = 0;
364 meta->id = req->id;
365 }
366
367 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
368 meta = npo->meta + npo->meta_prod++;
369
82cada22
PD
370 if ((1 << gso_type) & vif->gso_mask) {
371 meta->gso_type = gso_type;
372 meta->gso_size = gso_size;
373 } else {
374 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
f942dc25 375 meta->gso_size = 0;
82cada22 376 }
f942dc25
IC
377
378 meta->size = 0;
379 meta->id = req->id;
380 npo->copy_off = 0;
381 npo->copy_gref = req->gref;
382
383 data = skb->data;
384 while (data < skb_tail_pointer(skb)) {
385 unsigned int offset = offset_in_page(data);
386 unsigned int len = PAGE_SIZE - offset;
387
388 if (data + len > skb_tail_pointer(skb))
389 len = skb_tail_pointer(skb) - data;
390
7376419a 391 xenvif_gop_frag_copy(vif, skb, npo,
33bc801d 392 virt_to_page(data), len, offset, &head);
f942dc25
IC
393 data += len;
394 }
395
396 for (i = 0; i < nr_frags; i++) {
7376419a
WL
397 xenvif_gop_frag_copy(vif, skb, npo,
398 skb_frag_page(&skb_shinfo(skb)->frags[i]),
399 skb_frag_size(&skb_shinfo(skb)->frags[i]),
400 skb_shinfo(skb)->frags[i].page_offset,
33bc801d 401 &head);
f942dc25
IC
402 }
403
404 return npo->meta_prod - old_meta_prod;
405}
406
407/*
7376419a 408 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
f942dc25
IC
409 * used to set up the operations on the top of
410 * netrx_pending_operations, which have since been done. Check that
411 * they didn't give any errors and advance over them.
412 */
7376419a
WL
413static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
414 struct netrx_pending_operations *npo)
f942dc25
IC
415{
416 struct gnttab_copy *copy_op;
417 int status = XEN_NETIF_RSP_OKAY;
418 int i;
419
420 for (i = 0; i < nr_meta_slots; i++) {
421 copy_op = npo->copy + npo->copy_cons++;
422 if (copy_op->status != GNTST_okay) {
423 netdev_dbg(vif->dev,
424 "Bad status %d from copy to DOM%d.\n",
425 copy_op->status, vif->domid);
426 status = XEN_NETIF_RSP_ERROR;
427 }
428 }
429
430 return status;
431}
432
7376419a
WL
433static void xenvif_add_frag_responses(struct xenvif *vif, int status,
434 struct xenvif_rx_meta *meta,
435 int nr_meta_slots)
f942dc25
IC
436{
437 int i;
438 unsigned long offset;
439
440 /* No fragments used */
441 if (nr_meta_slots <= 1)
442 return;
443
444 nr_meta_slots--;
445
446 for (i = 0; i < nr_meta_slots; i++) {
447 int flags;
448 if (i == nr_meta_slots - 1)
449 flags = 0;
450 else
451 flags = XEN_NETRXF_more_data;
452
453 offset = 0;
454 make_rx_response(vif, meta[i].id, status, offset,
455 meta[i].size, flags);
456 }
457}
458
33bc801d
WL
459struct skb_cb_overlay {
460 int meta_slots_used;
461};
462
ca2f09f2 463void xenvif_kick_thread(struct xenvif *vif)
b3f980bd
WL
464{
465 wake_up(&vif->wq);
466}
467
ca2f09f2 468static void xenvif_rx_action(struct xenvif *vif)
f942dc25 469{
f942dc25 470 s8 status;
e1f00a69 471 u16 flags;
f942dc25
IC
472 struct xen_netif_rx_response *resp;
473 struct sk_buff_head rxq;
474 struct sk_buff *skb;
475 LIST_HEAD(notify);
476 int ret;
f942dc25
IC
477 unsigned long offset;
478 struct skb_cb_overlay *sco;
b3f980bd 479 int need_to_notify = 0;
f942dc25
IC
480
481 struct netrx_pending_operations npo = {
b3f980bd
WL
482 .copy = vif->grant_copy_op,
483 .meta = vif->meta,
f942dc25
IC
484 };
485
486 skb_queue_head_init(&rxq);
487
b3f980bd 488 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
ca2f09f2
PD
489 int max_slots_needed;
490 int i;
491
492 /* We need a cheap worse case estimate for the number of
493 * slots we'll use.
494 */
495
496 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
497 skb_headlen(skb),
498 PAGE_SIZE);
499 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
500 unsigned int size;
501 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
502 max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
503 }
504 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
505 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
506 max_slots_needed++;
507
508 /* If the skb may not fit then bail out now */
509 if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
510 skb_queue_head(&vif->rx_queue, skb);
511 need_to_notify = 1;
512 break;
513 }
f942dc25
IC
514
515 sco = (struct skb_cb_overlay *)skb->cb;
7376419a 516 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
ca2f09f2 517 BUG_ON(sco->meta_slots_used > max_slots_needed);
f942dc25
IC
518
519 __skb_queue_tail(&rxq, skb);
f942dc25
IC
520 }
521
b3f980bd 522 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
f942dc25
IC
523
524 if (!npo.copy_prod)
ca2f09f2 525 goto done;
f942dc25 526
ac3d5ac2 527 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
b3f980bd 528 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
f942dc25
IC
529
530 while ((skb = __skb_dequeue(&rxq)) != NULL) {
531 sco = (struct skb_cb_overlay *)skb->cb;
532
82cada22
PD
533 if ((1 << vif->meta[npo.meta_cons].gso_type) &
534 vif->gso_prefix_mask) {
f942dc25 535 resp = RING_GET_RESPONSE(&vif->rx,
b3f980bd 536 vif->rx.rsp_prod_pvt++);
f942dc25
IC
537
538 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
539
b3f980bd
WL
540 resp->offset = vif->meta[npo.meta_cons].gso_size;
541 resp->id = vif->meta[npo.meta_cons].id;
f942dc25
IC
542 resp->status = sco->meta_slots_used;
543
544 npo.meta_cons++;
545 sco->meta_slots_used--;
546 }
547
548
549 vif->dev->stats.tx_bytes += skb->len;
550 vif->dev->stats.tx_packets++;
551
7376419a 552 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
f942dc25
IC
553
554 if (sco->meta_slots_used == 1)
555 flags = 0;
556 else
557 flags = XEN_NETRXF_more_data;
558
559 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
560 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
561 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
562 /* remote but checksummed. */
563 flags |= XEN_NETRXF_data_validated;
564
565 offset = 0;
b3f980bd 566 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
f942dc25 567 status, offset,
b3f980bd 568 vif->meta[npo.meta_cons].size,
f942dc25
IC
569 flags);
570
82cada22
PD
571 if ((1 << vif->meta[npo.meta_cons].gso_type) &
572 vif->gso_mask) {
f942dc25
IC
573 struct xen_netif_extra_info *gso =
574 (struct xen_netif_extra_info *)
575 RING_GET_RESPONSE(&vif->rx,
576 vif->rx.rsp_prod_pvt++);
577
578 resp->flags |= XEN_NETRXF_extra_info;
579
82cada22 580 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
b3f980bd 581 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
f942dc25
IC
582 gso->u.gso.pad = 0;
583 gso->u.gso.features = 0;
584
585 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
586 gso->flags = 0;
587 }
588
7376419a
WL
589 xenvif_add_frag_responses(vif, status,
590 vif->meta + npo.meta_cons + 1,
591 sco->meta_slots_used);
f942dc25
IC
592
593 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
f942dc25 594
b3f980bd
WL
595 if (ret)
596 need_to_notify = 1;
597
f942dc25
IC
598 npo.meta_cons += sco->meta_slots_used;
599 dev_kfree_skb(skb);
600 }
601
ca2f09f2 602done:
b3f980bd 603 if (need_to_notify)
e1f00a69 604 notify_remote_via_irq(vif->rx_irq);
f942dc25
IC
605}
606
7376419a 607void xenvif_check_rx_xenvif(struct xenvif *vif)
f942dc25
IC
608{
609 int more_to_do;
610
611 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
612
613 if (more_to_do)
b3f980bd 614 napi_schedule(&vif->napi);
f942dc25
IC
615}
616
617static void tx_add_credit(struct xenvif *vif)
618{
619 unsigned long max_burst, max_credit;
620
621 /*
622 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
623 * Otherwise the interface can seize up due to insufficient credit.
624 */
625 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
626 max_burst = min(max_burst, 131072UL);
627 max_burst = max(max_burst, vif->credit_bytes);
628
629 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
630 max_credit = vif->remaining_credit + vif->credit_bytes;
631 if (max_credit < vif->remaining_credit)
632 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
633
634 vif->remaining_credit = min(max_credit, max_burst);
635}
636
637static void tx_credit_callback(unsigned long data)
638{
639 struct xenvif *vif = (struct xenvif *)data;
640 tx_add_credit(vif);
7376419a 641 xenvif_check_rx_xenvif(vif);
f942dc25
IC
642}
643
7376419a
WL
644static void xenvif_tx_err(struct xenvif *vif,
645 struct xen_netif_tx_request *txp, RING_IDX end)
f942dc25
IC
646{
647 RING_IDX cons = vif->tx.req_cons;
648
649 do {
650 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
b9149729 651 if (cons == end)
f942dc25
IC
652 break;
653 txp = RING_GET_REQUEST(&vif->tx, cons++);
654 } while (1);
655 vif->tx.req_cons = cons;
f942dc25
IC
656}
657
7376419a 658static void xenvif_fatal_tx_err(struct xenvif *vif)
48856286
IC
659{
660 netdev_err(vif->dev, "fatal error; disabling device\n");
661 xenvif_carrier_off(vif);
48856286
IC
662}
663
7376419a
WL
664static int xenvif_count_requests(struct xenvif *vif,
665 struct xen_netif_tx_request *first,
666 struct xen_netif_tx_request *txp,
667 int work_to_do)
f942dc25
IC
668{
669 RING_IDX cons = vif->tx.req_cons;
2810e5b9
WL
670 int slots = 0;
671 int drop_err = 0;
59ccb4eb 672 int more_data;
f942dc25
IC
673
674 if (!(first->flags & XEN_NETTXF_more_data))
675 return 0;
676
677 do {
59ccb4eb
WL
678 struct xen_netif_tx_request dropped_tx = { 0 };
679
2810e5b9
WL
680 if (slots >= work_to_do) {
681 netdev_err(vif->dev,
682 "Asked for %d slots but exceeds this limit\n",
683 work_to_do);
7376419a 684 xenvif_fatal_tx_err(vif);
35876b5f 685 return -ENODATA;
f942dc25
IC
686 }
687
2810e5b9
WL
688 /* This guest is really using too many slots and
689 * considered malicious.
690 */
37641494 691 if (unlikely(slots >= fatal_skb_slots)) {
2810e5b9
WL
692 netdev_err(vif->dev,
693 "Malicious frontend using %d slots, threshold %u\n",
37641494 694 slots, fatal_skb_slots);
7376419a 695 xenvif_fatal_tx_err(vif);
35876b5f 696 return -E2BIG;
f942dc25
IC
697 }
698
2810e5b9 699 /* Xen network protocol had implicit dependency on
37641494
WL
700 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
701 * the historical MAX_SKB_FRAGS value 18 to honor the
702 * same behavior as before. Any packet using more than
703 * 18 slots but less than fatal_skb_slots slots is
704 * dropped
2810e5b9 705 */
37641494 706 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
2810e5b9
WL
707 if (net_ratelimit())
708 netdev_dbg(vif->dev,
709 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
37641494 710 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
2810e5b9
WL
711 drop_err = -E2BIG;
712 }
713
59ccb4eb
WL
714 if (drop_err)
715 txp = &dropped_tx;
716
2810e5b9 717 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
f942dc25 718 sizeof(*txp));
03393fd5
WL
719
720 /* If the guest submitted a frame >= 64 KiB then
721 * first->size overflowed and following slots will
722 * appear to be larger than the frame.
723 *
724 * This cannot be fatal error as there are buggy
725 * frontends that do this.
726 *
727 * Consume all slots and drop the packet.
728 */
729 if (!drop_err && txp->size > first->size) {
730 if (net_ratelimit())
731 netdev_dbg(vif->dev,
732 "Invalid tx request, slot size %u > remaining size %u\n",
733 txp->size, first->size);
734 drop_err = -EIO;
f942dc25
IC
735 }
736
737 first->size -= txp->size;
2810e5b9 738 slots++;
f942dc25
IC
739
740 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
2810e5b9 741 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
f942dc25 742 txp->offset, txp->size);
7376419a 743 xenvif_fatal_tx_err(vif);
35876b5f 744 return -EINVAL;
f942dc25 745 }
59ccb4eb
WL
746
747 more_data = txp->flags & XEN_NETTXF_more_data;
748
749 if (!drop_err)
750 txp++;
751
752 } while (more_data);
2810e5b9
WL
753
754 if (drop_err) {
7376419a 755 xenvif_tx_err(vif, first, cons + slots);
2810e5b9
WL
756 return drop_err;
757 }
758
759 return slots;
f942dc25
IC
760}
761
7376419a
WL
762static struct page *xenvif_alloc_page(struct xenvif *vif,
763 u16 pending_idx)
f942dc25
IC
764{
765 struct page *page;
b3f980bd
WL
766
767 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
f942dc25
IC
768 if (!page)
769 return NULL;
b3f980bd
WL
770 vif->mmap_pages[pending_idx] = page;
771
f942dc25
IC
772 return page;
773}
774
7376419a
WL
775static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
776 struct sk_buff *skb,
777 struct xen_netif_tx_request *txp,
778 struct gnttab_copy *gop)
f942dc25
IC
779{
780 struct skb_shared_info *shinfo = skb_shinfo(skb);
781 skb_frag_t *frags = shinfo->frags;
ea066ad1 782 u16 pending_idx = *((u16 *)skb->data);
2810e5b9
WL
783 u16 head_idx = 0;
784 int slot, start;
785 struct page *page;
786 pending_ring_idx_t index, start_idx = 0;
787 uint16_t dst_offset;
788 unsigned int nr_slots;
789 struct pending_tx_info *first = NULL;
790
791 /* At this point shinfo->nr_frags is in fact the number of
37641494 792 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
2810e5b9
WL
793 */
794 nr_slots = shinfo->nr_frags;
f942dc25
IC
795
796 /* Skip first skb fragment if it is on same page as header fragment. */
ea066ad1 797 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
f942dc25 798
2810e5b9
WL
799 /* Coalesce tx requests, at this point the packet passed in
800 * should be <= 64K. Any packets larger than 64K have been
7376419a 801 * handled in xenvif_count_requests().
2810e5b9
WL
802 */
803 for (shinfo->nr_frags = slot = start; slot < nr_slots;
804 shinfo->nr_frags++) {
f942dc25 805 struct pending_tx_info *pending_tx_info =
b3f980bd 806 vif->pending_tx_info;
f942dc25 807
b3f980bd 808 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
f942dc25 809 if (!page)
4cc7c1cb 810 goto err;
f942dc25 811
2810e5b9
WL
812 dst_offset = 0;
813 first = NULL;
814 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
815 gop->flags = GNTCOPY_source_gref;
816
817 gop->source.u.ref = txp->gref;
818 gop->source.domid = vif->domid;
819 gop->source.offset = txp->offset;
820
821 gop->dest.domid = DOMID_SELF;
822
823 gop->dest.offset = dst_offset;
824 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
825
826 if (dst_offset + txp->size > PAGE_SIZE) {
827 /* This page can only merge a portion
828 * of tx request. Do not increment any
829 * pointer / counter here. The txp
830 * will be dealt with in future
831 * rounds, eventually hitting the
832 * `else` branch.
833 */
834 gop->len = PAGE_SIZE - dst_offset;
835 txp->offset += gop->len;
836 txp->size -= gop->len;
837 dst_offset += gop->len; /* quit loop */
838 } else {
839 /* This tx request can be merged in the page */
840 gop->len = txp->size;
841 dst_offset += gop->len;
842
b3f980bd 843 index = pending_index(vif->pending_cons++);
2810e5b9 844
b3f980bd 845 pending_idx = vif->pending_ring[index];
2810e5b9
WL
846
847 memcpy(&pending_tx_info[pending_idx].req, txp,
848 sizeof(*txp));
2810e5b9
WL
849
850 /* Poison these fields, corresponding
851 * fields for head tx req will be set
852 * to correct values after the loop.
853 */
b3f980bd 854 vif->mmap_pages[pending_idx] = (void *)(~0UL);
2810e5b9
WL
855 pending_tx_info[pending_idx].head =
856 INVALID_PENDING_RING_IDX;
857
858 if (!first) {
859 first = &pending_tx_info[pending_idx];
860 start_idx = index;
861 head_idx = pending_idx;
862 }
863
864 txp++;
865 slot++;
866 }
f942dc25 867
2810e5b9
WL
868 gop++;
869 }
f942dc25 870
2810e5b9
WL
871 first->req.offset = 0;
872 first->req.size = dst_offset;
873 first->head = start_idx;
b3f980bd 874 vif->mmap_pages[head_idx] = page;
2810e5b9 875 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
f942dc25
IC
876 }
877
2810e5b9
WL
878 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
879
f942dc25 880 return gop;
4cc7c1cb
IC
881err:
882 /* Unwind, freeing all pages and sending error responses. */
2810e5b9 883 while (shinfo->nr_frags-- > start) {
7376419a 884 xenvif_idx_release(vif,
2810e5b9
WL
885 frag_get_pending_idx(&frags[shinfo->nr_frags]),
886 XEN_NETIF_RSP_ERROR);
4cc7c1cb
IC
887 }
888 /* The head too, if necessary. */
889 if (start)
7376419a 890 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
4cc7c1cb
IC
891
892 return NULL;
f942dc25
IC
893}
894
7376419a
WL
895static int xenvif_tx_check_gop(struct xenvif *vif,
896 struct sk_buff *skb,
897 struct gnttab_copy **gopp)
f942dc25
IC
898{
899 struct gnttab_copy *gop = *gopp;
ea066ad1 900 u16 pending_idx = *((u16 *)skb->data);
f942dc25 901 struct skb_shared_info *shinfo = skb_shinfo(skb);
2810e5b9 902 struct pending_tx_info *tx_info;
f942dc25
IC
903 int nr_frags = shinfo->nr_frags;
904 int i, err, start;
2810e5b9 905 u16 peek; /* peek into next tx request */
f942dc25
IC
906
907 /* Check status of header. */
908 err = gop->status;
7d5145d8 909 if (unlikely(err))
7376419a 910 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
f942dc25
IC
911
912 /* Skip first skb fragment if it is on same page as header fragment. */
ea066ad1 913 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
f942dc25
IC
914
915 for (i = start; i < nr_frags; i++) {
916 int j, newerr;
2810e5b9 917 pending_ring_idx_t head;
f942dc25 918
ea066ad1 919 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
b3f980bd 920 tx_info = &vif->pending_tx_info[pending_idx];
2810e5b9 921 head = tx_info->head;
f942dc25
IC
922
923 /* Check error status: if okay then remember grant handle. */
2810e5b9
WL
924 do {
925 newerr = (++gop)->status;
926 if (newerr)
927 break;
b3f980bd
WL
928 peek = vif->pending_ring[pending_index(++head)];
929 } while (!pending_tx_is_head(vif, peek));
2810e5b9 930
f942dc25
IC
931 if (likely(!newerr)) {
932 /* Had a previous error? Invalidate this fragment. */
933 if (unlikely(err))
7376419a
WL
934 xenvif_idx_release(vif, pending_idx,
935 XEN_NETIF_RSP_OKAY);
f942dc25
IC
936 continue;
937 }
938
939 /* Error on this fragment: respond to client with an error. */
7376419a 940 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
f942dc25
IC
941
942 /* Not the first error? Preceding frags already invalidated. */
943 if (err)
944 continue;
945
946 /* First error: invalidate header and preceding fragments. */
947 pending_idx = *((u16 *)skb->data);
7376419a 948 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
f942dc25 949 for (j = start; j < i; j++) {
5ccb3ea7 950 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
7376419a
WL
951 xenvif_idx_release(vif, pending_idx,
952 XEN_NETIF_RSP_OKAY);
f942dc25
IC
953 }
954
955 /* Remember the error: invalidate all subsequent fragments. */
956 err = newerr;
957 }
958
959 *gopp = gop + 1;
960 return err;
961}
962
7376419a 963static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
f942dc25
IC
964{
965 struct skb_shared_info *shinfo = skb_shinfo(skb);
966 int nr_frags = shinfo->nr_frags;
967 int i;
968
969 for (i = 0; i < nr_frags; i++) {
970 skb_frag_t *frag = shinfo->frags + i;
971 struct xen_netif_tx_request *txp;
ea066ad1
IC
972 struct page *page;
973 u16 pending_idx;
f942dc25 974
ea066ad1 975 pending_idx = frag_get_pending_idx(frag);
f942dc25 976
b3f980bd
WL
977 txp = &vif->pending_tx_info[pending_idx].req;
978 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
ea066ad1 979 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
f942dc25
IC
980 skb->len += txp->size;
981 skb->data_len += txp->size;
982 skb->truesize += txp->size;
983
7376419a 984 /* Take an extra reference to offset xenvif_idx_release */
b3f980bd 985 get_page(vif->mmap_pages[pending_idx]);
7376419a 986 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
f942dc25
IC
987 }
988}
989
7376419a 990static int xenvif_get_extras(struct xenvif *vif,
f942dc25
IC
991 struct xen_netif_extra_info *extras,
992 int work_to_do)
993{
994 struct xen_netif_extra_info extra;
995 RING_IDX cons = vif->tx.req_cons;
996
997 do {
998 if (unlikely(work_to_do-- <= 0)) {
48856286 999 netdev_err(vif->dev, "Missing extra info\n");
7376419a 1000 xenvif_fatal_tx_err(vif);
f942dc25
IC
1001 return -EBADR;
1002 }
1003
1004 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1005 sizeof(extra));
1006 if (unlikely(!extra.type ||
1007 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1008 vif->tx.req_cons = ++cons;
48856286 1009 netdev_err(vif->dev,
f942dc25 1010 "Invalid extra type: %d\n", extra.type);
7376419a 1011 xenvif_fatal_tx_err(vif);
f942dc25
IC
1012 return -EINVAL;
1013 }
1014
1015 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1016 vif->tx.req_cons = ++cons;
1017 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1018
1019 return work_to_do;
1020}
1021
7376419a
WL
1022static int xenvif_set_skb_gso(struct xenvif *vif,
1023 struct sk_buff *skb,
1024 struct xen_netif_extra_info *gso)
f942dc25
IC
1025{
1026 if (!gso->u.gso.size) {
48856286 1027 netdev_err(vif->dev, "GSO size must not be zero.\n");
7376419a 1028 xenvif_fatal_tx_err(vif);
f942dc25
IC
1029 return -EINVAL;
1030 }
1031
a9468587
PD
1032 switch (gso->u.gso.type) {
1033 case XEN_NETIF_GSO_TYPE_TCPV4:
1034 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1035 break;
1036 case XEN_NETIF_GSO_TYPE_TCPV6:
1037 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1038 break;
1039 default:
48856286 1040 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
7376419a 1041 xenvif_fatal_tx_err(vif);
f942dc25
IC
1042 return -EINVAL;
1043 }
1044
1045 skb_shinfo(skb)->gso_size = gso->u.gso.size;
b89587a7 1046 /* gso_segs will be calculated later */
f942dc25
IC
1047
1048 return 0;
1049}
1050
1431fb31
PD
1051static inline int maybe_pull_tail(struct sk_buff *skb, unsigned int len,
1052 unsigned int max)
2eba61d5 1053{
1431fb31
PD
1054 if (skb_headlen(skb) >= len)
1055 return 0;
1056
1057 /* If we need to pullup then pullup to the max, so we
1058 * won't need to do it again.
1059 */
1060 if (max > skb->len)
1061 max = skb->len;
1062
1063 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
1064 return -ENOMEM;
1065
1066 if (skb_headlen(skb) < len)
1067 return -EPROTO;
1068
1069 return 0;
2eba61d5
PD
1070}
1071
1431fb31
PD
1072/* This value should be large enough to cover a tagged ethernet header plus
1073 * maximally sized IP and TCP or UDP headers.
1074 */
1075#define MAX_IP_HDR_LEN 128
1076
2eba61d5
PD
1077static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb,
1078 int recalculate_partial_csum)
f942dc25 1079{
2eba61d5 1080 unsigned int off;
1431fb31
PD
1081 bool fragment;
1082 int err;
1083
1084 fragment = false;
1085
1086 err = maybe_pull_tail(skb,
1087 sizeof(struct iphdr),
1088 MAX_IP_HDR_LEN);
1089 if (err < 0)
1090 goto out;
f942dc25 1091
1431fb31
PD
1092 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
1093 fragment = true;
f942dc25 1094
1431fb31 1095 off = ip_hdrlen(skb);
f942dc25 1096
1431fb31 1097 err = -EPROTO;
f942dc25 1098
7022ef8b
WY
1099 if (fragment)
1100 goto out;
1101
1431fb31 1102 switch (ip_hdr(skb)->protocol) {
f942dc25 1103 case IPPROTO_TCP:
d52eb0d4
PD
1104 err = maybe_pull_tail(skb,
1105 off + sizeof(struct tcphdr),
1106 MAX_IP_HDR_LEN);
1107 if (err < 0)
1108 goto out;
1109
2eba61d5 1110 if (!skb_partial_csum_set(skb, off,
0c8d087c
WY
1111 offsetof(struct tcphdr, check))) {
1112 err = -EPROTO;
bea89336 1113 goto out;
0c8d087c 1114 }
f942dc25 1115
d52eb0d4 1116 if (recalculate_partial_csum)
1431fb31
PD
1117 tcp_hdr(skb)->check =
1118 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1119 ip_hdr(skb)->daddr,
1120 skb->len - off,
1121 IPPROTO_TCP, 0);
f942dc25
IC
1122 break;
1123 case IPPROTO_UDP:
d52eb0d4
PD
1124 err = maybe_pull_tail(skb,
1125 off + sizeof(struct udphdr),
1126 MAX_IP_HDR_LEN);
1127 if (err < 0)
1128 goto out;
1129
2eba61d5 1130 if (!skb_partial_csum_set(skb, off,
0c8d087c
WY
1131 offsetof(struct udphdr, check))) {
1132 err = -EPROTO;
bea89336 1133 goto out;
0c8d087c 1134 }
f942dc25 1135
d52eb0d4 1136 if (recalculate_partial_csum)
1431fb31
PD
1137 udp_hdr(skb)->check =
1138 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1139 ip_hdr(skb)->daddr,
1140 skb->len - off,
1141 IPPROTO_UDP, 0);
f942dc25
IC
1142 break;
1143 default:
f942dc25
IC
1144 goto out;
1145 }
1146
f942dc25
IC
1147 err = 0;
1148
1149out:
1150 return err;
1151}
1152
1431fb31
PD
1153/* This value should be large enough to cover a tagged ethernet header plus
1154 * an IPv6 header, all options, and a maximal TCP or UDP header.
1155 */
1156#define MAX_IPV6_HDR_LEN 256
1157
1158#define OPT_HDR(type, skb, off) \
1159 (type *)(skb_network_header(skb) + (off))
1160
2eba61d5
PD
1161static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb,
1162 int recalculate_partial_csum)
1163{
1431fb31 1164 int err;
2eba61d5 1165 u8 nexthdr;
2eba61d5 1166 unsigned int off;
1431fb31 1167 unsigned int len;
2eba61d5
PD
1168 bool fragment;
1169 bool done;
1170
1431fb31 1171 fragment = false;
2eba61d5
PD
1172 done = false;
1173
1174 off = sizeof(struct ipv6hdr);
1175
1431fb31
PD
1176 err = maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
1177 if (err < 0)
1178 goto out;
2eba61d5 1179
1431fb31 1180 nexthdr = ipv6_hdr(skb)->nexthdr;
2eba61d5 1181
1431fb31
PD
1182 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
1183 while (off <= len && !done) {
2eba61d5
PD
1184 switch (nexthdr) {
1185 case IPPROTO_DSTOPTS:
1186 case IPPROTO_HOPOPTS:
1187 case IPPROTO_ROUTING: {
1431fb31 1188 struct ipv6_opt_hdr *hp;
2eba61d5 1189
1431fb31
PD
1190 err = maybe_pull_tail(skb,
1191 off +
1192 sizeof(struct ipv6_opt_hdr),
1193 MAX_IPV6_HDR_LEN);
1194 if (err < 0)
1195 goto out;
2eba61d5 1196
1431fb31 1197 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
2eba61d5
PD
1198 nexthdr = hp->nexthdr;
1199 off += ipv6_optlen(hp);
1200 break;
1201 }
1202 case IPPROTO_AH: {
1431fb31
PD
1203 struct ip_auth_hdr *hp;
1204
1205 err = maybe_pull_tail(skb,
1206 off +
1207 sizeof(struct ip_auth_hdr),
1208 MAX_IPV6_HDR_LEN);
1209 if (err < 0)
1210 goto out;
1211
1212 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
1213 nexthdr = hp->nexthdr;
1214 off += ipv6_authlen(hp);
1215 break;
1216 }
1217 case IPPROTO_FRAGMENT: {
1218 struct frag_hdr *hp;
2eba61d5 1219
1431fb31
PD
1220 err = maybe_pull_tail(skb,
1221 off +
1222 sizeof(struct frag_hdr),
1223 MAX_IPV6_HDR_LEN);
1224 if (err < 0)
1225 goto out;
1226
1227 hp = OPT_HDR(struct frag_hdr, skb, off);
1228
1229 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
1230 fragment = true;
2eba61d5
PD
1231
1232 nexthdr = hp->nexthdr;
1431fb31 1233 off += sizeof(struct frag_hdr);
2eba61d5
PD
1234 break;
1235 }
2eba61d5
PD
1236 default:
1237 done = true;
1238 break;
1239 }
1240 }
1241
1431fb31 1242 err = -EPROTO;
2eba61d5 1243
1431fb31 1244 if (!done || fragment)
2eba61d5 1245 goto out;
2eba61d5
PD
1246
1247 switch (nexthdr) {
1248 case IPPROTO_TCP:
d52eb0d4
PD
1249 err = maybe_pull_tail(skb,
1250 off + sizeof(struct tcphdr),
1251 MAX_IPV6_HDR_LEN);
1252 if (err < 0)
1253 goto out;
1254
2eba61d5 1255 if (!skb_partial_csum_set(skb, off,
0c8d087c
WY
1256 offsetof(struct tcphdr, check))) {
1257 err = -EPROTO;
2eba61d5 1258 goto out;
0c8d087c 1259 }
2eba61d5 1260
d52eb0d4 1261 if (recalculate_partial_csum)
1431fb31
PD
1262 tcp_hdr(skb)->check =
1263 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1264 &ipv6_hdr(skb)->daddr,
1265 skb->len - off,
1266 IPPROTO_TCP, 0);
2eba61d5
PD
1267 break;
1268 case IPPROTO_UDP:
d52eb0d4
PD
1269 err = maybe_pull_tail(skb,
1270 off + sizeof(struct udphdr),
1271 MAX_IPV6_HDR_LEN);
1272 if (err < 0)
1273 goto out;
1274
2eba61d5 1275 if (!skb_partial_csum_set(skb, off,
0c8d087c
WY
1276 offsetof(struct udphdr, check))) {
1277 err = -EPROTO;
2eba61d5 1278 goto out;
0c8d087c 1279 }
2eba61d5 1280
d52eb0d4 1281 if (recalculate_partial_csum)
1431fb31
PD
1282 udp_hdr(skb)->check =
1283 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1284 &ipv6_hdr(skb)->daddr,
1285 skb->len - off,
1286 IPPROTO_UDP, 0);
2eba61d5
PD
1287 break;
1288 default:
2eba61d5
PD
1289 goto out;
1290 }
1291
1292 err = 0;
1293
1294out:
1295 return err;
1296}
1297
1298static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1299{
1300 int err = -EPROTO;
1301 int recalculate_partial_csum = 0;
1302
1303 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1304 * peers can fail to set NETRXF_csum_blank when sending a GSO
1305 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1306 * recalculate the partial checksum.
1307 */
1308 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1309 vif->rx_gso_checksum_fixup++;
1310 skb->ip_summed = CHECKSUM_PARTIAL;
1311 recalculate_partial_csum = 1;
1312 }
1313
1314 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1315 if (skb->ip_summed != CHECKSUM_PARTIAL)
1316 return 0;
1317
1318 if (skb->protocol == htons(ETH_P_IP))
1319 err = checksum_setup_ip(vif, skb, recalculate_partial_csum);
1320 else if (skb->protocol == htons(ETH_P_IPV6))
1321 err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum);
1322
1323 return err;
1324}
1325
f942dc25
IC
1326static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1327{
059dfa6a
WL
1328 u64 now = get_jiffies_64();
1329 u64 next_credit = vif->credit_window_start +
f942dc25
IC
1330 msecs_to_jiffies(vif->credit_usec / 1000);
1331
1332 /* Timer could already be pending in rare cases. */
1333 if (timer_pending(&vif->credit_timeout))
1334 return true;
1335
1336 /* Passed the point where we can replenish credit? */
059dfa6a
WL
1337 if (time_after_eq64(now, next_credit)) {
1338 vif->credit_window_start = now;
f942dc25
IC
1339 tx_add_credit(vif);
1340 }
1341
1342 /* Still too big to send right now? Set a callback. */
1343 if (size > vif->remaining_credit) {
1344 vif->credit_timeout.data =
1345 (unsigned long)vif;
1346 vif->credit_timeout.function =
1347 tx_credit_callback;
1348 mod_timer(&vif->credit_timeout,
1349 next_credit);
059dfa6a 1350 vif->credit_window_start = next_credit;
f942dc25
IC
1351
1352 return true;
1353 }
1354
1355 return false;
1356}
1357
10574059 1358static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
f942dc25 1359{
b3f980bd 1360 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
f942dc25
IC
1361 struct sk_buff *skb;
1362 int ret;
1363
b3f980bd 1364 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
10574059
PD
1365 < MAX_PENDING_REQS) &&
1366 (skb_queue_len(&vif->tx_queue) < budget)) {
f942dc25 1367 struct xen_netif_tx_request txreq;
37641494 1368 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
f942dc25
IC
1369 struct page *page;
1370 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1371 u16 pending_idx;
1372 RING_IDX idx;
1373 int work_to_do;
1374 unsigned int data_len;
1375 pending_ring_idx_t index;
1376
48856286
IC
1377 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1378 XEN_NETIF_TX_RING_SIZE) {
1379 netdev_err(vif->dev,
1380 "Impossible number of requests. "
1381 "req_prod %d, req_cons %d, size %ld\n",
1382 vif->tx.sring->req_prod, vif->tx.req_cons,
1383 XEN_NETIF_TX_RING_SIZE);
7376419a 1384 xenvif_fatal_tx_err(vif);
48856286
IC
1385 continue;
1386 }
1387
d9601a36 1388 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
b3f980bd
WL
1389 if (!work_to_do)
1390 break;
f942dc25
IC
1391
1392 idx = vif->tx.req_cons;
1393 rmb(); /* Ensure that we see the request before we copy it. */
1394 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1395
1396 /* Credit-based scheduling. */
1397 if (txreq.size > vif->remaining_credit &&
b3f980bd
WL
1398 tx_credit_exceeded(vif, txreq.size))
1399 break;
f942dc25
IC
1400
1401 vif->remaining_credit -= txreq.size;
1402
1403 work_to_do--;
1404 vif->tx.req_cons = ++idx;
1405
1406 memset(extras, 0, sizeof(extras));
1407 if (txreq.flags & XEN_NETTXF_extra_info) {
7376419a
WL
1408 work_to_do = xenvif_get_extras(vif, extras,
1409 work_to_do);
f942dc25 1410 idx = vif->tx.req_cons;
48856286 1411 if (unlikely(work_to_do < 0))
b3f980bd 1412 break;
f942dc25
IC
1413 }
1414
7376419a 1415 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
48856286 1416 if (unlikely(ret < 0))
b3f980bd 1417 break;
48856286 1418
f942dc25
IC
1419 idx += ret;
1420
1421 if (unlikely(txreq.size < ETH_HLEN)) {
1422 netdev_dbg(vif->dev,
1423 "Bad packet size: %d\n", txreq.size);
7376419a 1424 xenvif_tx_err(vif, &txreq, idx);
b3f980bd 1425 break;
f942dc25
IC
1426 }
1427
1428 /* No crossing a page as the payload mustn't fragment. */
1429 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
48856286 1430 netdev_err(vif->dev,
f942dc25
IC
1431 "txreq.offset: %x, size: %u, end: %lu\n",
1432 txreq.offset, txreq.size,
1433 (txreq.offset&~PAGE_MASK) + txreq.size);
7376419a 1434 xenvif_fatal_tx_err(vif);
b3f980bd 1435 break;
f942dc25
IC
1436 }
1437
b3f980bd
WL
1438 index = pending_index(vif->pending_cons);
1439 pending_idx = vif->pending_ring[index];
f942dc25
IC
1440
1441 data_len = (txreq.size > PKT_PROT_LEN &&
37641494 1442 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
f942dc25
IC
1443 PKT_PROT_LEN : txreq.size;
1444
1445 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1446 GFP_ATOMIC | __GFP_NOWARN);
1447 if (unlikely(skb == NULL)) {
1448 netdev_dbg(vif->dev,
1449 "Can't allocate a skb in start_xmit.\n");
7376419a 1450 xenvif_tx_err(vif, &txreq, idx);
f942dc25
IC
1451 break;
1452 }
1453
1454 /* Packets passed to netif_rx() must have some headroom. */
1455 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1456
1457 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1458 struct xen_netif_extra_info *gso;
1459 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1460
7376419a
WL
1461 if (xenvif_set_skb_gso(vif, skb, gso)) {
1462 /* Failure in xenvif_set_skb_gso is fatal. */
f942dc25 1463 kfree_skb(skb);
b3f980bd 1464 break;
f942dc25
IC
1465 }
1466 }
1467
1468 /* XXX could copy straight to head */
7376419a 1469 page = xenvif_alloc_page(vif, pending_idx);
f942dc25
IC
1470 if (!page) {
1471 kfree_skb(skb);
7376419a 1472 xenvif_tx_err(vif, &txreq, idx);
b3f980bd 1473 break;
f942dc25
IC
1474 }
1475
f942dc25
IC
1476 gop->source.u.ref = txreq.gref;
1477 gop->source.domid = vif->domid;
1478 gop->source.offset = txreq.offset;
1479
1480 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1481 gop->dest.domid = DOMID_SELF;
1482 gop->dest.offset = txreq.offset;
1483
1484 gop->len = txreq.size;
1485 gop->flags = GNTCOPY_source_gref;
1486
1487 gop++;
1488
b3f980bd 1489 memcpy(&vif->pending_tx_info[pending_idx].req,
f942dc25 1490 &txreq, sizeof(txreq));
b3f980bd 1491 vif->pending_tx_info[pending_idx].head = index;
f942dc25
IC
1492 *((u16 *)skb->data) = pending_idx;
1493
1494 __skb_put(skb, data_len);
1495
1496 skb_shinfo(skb)->nr_frags = ret;
1497 if (data_len < txreq.size) {
1498 skb_shinfo(skb)->nr_frags++;
ea066ad1
IC
1499 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1500 pending_idx);
f942dc25 1501 } else {
ea066ad1
IC
1502 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1503 INVALID_PENDING_IDX);
f942dc25
IC
1504 }
1505
b3f980bd 1506 vif->pending_cons++;
f942dc25 1507
7376419a 1508 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
f942dc25
IC
1509 if (request_gop == NULL) {
1510 kfree_skb(skb);
7376419a 1511 xenvif_tx_err(vif, &txreq, idx);
b3f980bd 1512 break;
f942dc25
IC
1513 }
1514 gop = request_gop;
1515
b3f980bd 1516 __skb_queue_tail(&vif->tx_queue, skb);
1e0b6eac 1517
f942dc25 1518 vif->tx.req_cons = idx;
f942dc25 1519
b3f980bd 1520 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
f942dc25
IC
1521 break;
1522 }
1523
b3f980bd 1524 return gop - vif->tx_copy_ops;
f942dc25
IC
1525}
1526
b3f980bd 1527
10574059 1528static int xenvif_tx_submit(struct xenvif *vif)
f942dc25 1529{
b3f980bd 1530 struct gnttab_copy *gop = vif->tx_copy_ops;
f942dc25 1531 struct sk_buff *skb;
b3f980bd 1532 int work_done = 0;
f942dc25 1533
10574059 1534 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
f942dc25 1535 struct xen_netif_tx_request *txp;
f942dc25
IC
1536 u16 pending_idx;
1537 unsigned data_len;
1538
1539 pending_idx = *((u16 *)skb->data);
b3f980bd 1540 txp = &vif->pending_tx_info[pending_idx].req;
f942dc25
IC
1541
1542 /* Check the remap error code. */
7376419a 1543 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
f942dc25
IC
1544 netdev_dbg(vif->dev, "netback grant failed.\n");
1545 skb_shinfo(skb)->nr_frags = 0;
1546 kfree_skb(skb);
1547 continue;
1548 }
1549
1550 data_len = skb->len;
1551 memcpy(skb->data,
b3f980bd 1552 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
f942dc25
IC
1553 data_len);
1554 if (data_len < txp->size) {
1555 /* Append the packet payload as a fragment. */
1556 txp->offset += data_len;
1557 txp->size -= data_len;
1558 } else {
1559 /* Schedule a response immediately. */
7376419a
WL
1560 xenvif_idx_release(vif, pending_idx,
1561 XEN_NETIF_RSP_OKAY);
f942dc25
IC
1562 }
1563
1564 if (txp->flags & XEN_NETTXF_csum_blank)
1565 skb->ip_summed = CHECKSUM_PARTIAL;
1566 else if (txp->flags & XEN_NETTXF_data_validated)
1567 skb->ip_summed = CHECKSUM_UNNECESSARY;
1568
7376419a 1569 xenvif_fill_frags(vif, skb);
f942dc25 1570
2eba61d5 1571 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
f942dc25
IC
1572 int target = min_t(int, skb->len, PKT_PROT_LEN);
1573 __pskb_pull_tail(skb, target - skb_headlen(skb));
1574 }
1575
1576 skb->dev = vif->dev;
1577 skb->protocol = eth_type_trans(skb, skb->dev);
f9ca8f74 1578 skb_reset_network_header(skb);
f942dc25
IC
1579
1580 if (checksum_setup(vif, skb)) {
1581 netdev_dbg(vif->dev,
1582 "Can't setup checksum in net_tx_action\n");
1583 kfree_skb(skb);
1584 continue;
1585 }
1586
40893fd0 1587 skb_probe_transport_header(skb, 0);
f9ca8f74 1588
b89587a7
PD
1589 /* If the packet is GSO then we will have just set up the
1590 * transport header offset in checksum_setup so it's now
1591 * straightforward to calculate gso_segs.
1592 */
1593 if (skb_is_gso(skb)) {
1594 int mss = skb_shinfo(skb)->gso_size;
1595 int hdrlen = skb_transport_header(skb) -
1596 skb_mac_header(skb) +
1597 tcp_hdrlen(skb);
1598
1599 skb_shinfo(skb)->gso_segs =
1600 DIV_ROUND_UP(skb->len - hdrlen, mss);
1601 }
1602
f942dc25
IC
1603 vif->dev->stats.rx_bytes += skb->len;
1604 vif->dev->stats.rx_packets++;
1605
b3f980bd
WL
1606 work_done++;
1607
1608 netif_receive_skb(skb);
f942dc25 1609 }
b3f980bd
WL
1610
1611 return work_done;
f942dc25
IC
1612}
1613
1614/* Called after netfront has transmitted */
7376419a 1615int xenvif_tx_action(struct xenvif *vif, int budget)
f942dc25
IC
1616{
1617 unsigned nr_gops;
b3f980bd 1618 int work_done;
f942dc25 1619
b3f980bd
WL
1620 if (unlikely(!tx_work_todo(vif)))
1621 return 0;
1622
10574059 1623 nr_gops = xenvif_tx_build_gops(vif, budget);
f942dc25
IC
1624
1625 if (nr_gops == 0)
b3f980bd
WL
1626 return 0;
1627
1628 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
f942dc25 1629
10574059 1630 work_done = xenvif_tx_submit(vif);
f942dc25 1631
b3f980bd 1632 return work_done;
f942dc25
IC
1633}
1634
7376419a
WL
1635static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1636 u8 status)
f942dc25 1637{
f942dc25 1638 struct pending_tx_info *pending_tx_info;
2810e5b9
WL
1639 pending_ring_idx_t head;
1640 u16 peek; /* peek into next tx request */
1641
b3f980bd 1642 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
f942dc25
IC
1643
1644 /* Already complete? */
b3f980bd 1645 if (vif->mmap_pages[pending_idx] == NULL)
f942dc25
IC
1646 return;
1647
b3f980bd 1648 pending_tx_info = &vif->pending_tx_info[pending_idx];
f942dc25 1649
2810e5b9 1650 head = pending_tx_info->head;
f942dc25 1651
b3f980bd
WL
1652 BUG_ON(!pending_tx_is_head(vif, head));
1653 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
f942dc25 1654
2810e5b9
WL
1655 do {
1656 pending_ring_idx_t index;
1657 pending_ring_idx_t idx = pending_index(head);
b3f980bd 1658 u16 info_idx = vif->pending_ring[idx];
f942dc25 1659
b3f980bd 1660 pending_tx_info = &vif->pending_tx_info[info_idx];
2810e5b9
WL
1661 make_tx_response(vif, &pending_tx_info->req, status);
1662
1663 /* Setting any number other than
1664 * INVALID_PENDING_RING_IDX indicates this slot is
1665 * starting a new packet / ending a previous packet.
1666 */
1667 pending_tx_info->head = 0;
1668
b3f980bd
WL
1669 index = pending_index(vif->pending_prod++);
1670 vif->pending_ring[index] = vif->pending_ring[info_idx];
f942dc25 1671
b3f980bd 1672 peek = vif->pending_ring[pending_index(++head)];
2810e5b9 1673
b3f980bd 1674 } while (!pending_tx_is_head(vif, peek));
2810e5b9 1675
b3f980bd
WL
1676 put_page(vif->mmap_pages[pending_idx]);
1677 vif->mmap_pages[pending_idx] = NULL;
f942dc25
IC
1678}
1679
2810e5b9 1680
f942dc25
IC
1681static void make_tx_response(struct xenvif *vif,
1682 struct xen_netif_tx_request *txp,
1683 s8 st)
1684{
1685 RING_IDX i = vif->tx.rsp_prod_pvt;
1686 struct xen_netif_tx_response *resp;
1687 int notify;
1688
1689 resp = RING_GET_RESPONSE(&vif->tx, i);
1690 resp->id = txp->id;
1691 resp->status = st;
1692
1693 if (txp->flags & XEN_NETTXF_extra_info)
1694 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1695
1696 vif->tx.rsp_prod_pvt = ++i;
1697 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1698 if (notify)
e1f00a69 1699 notify_remote_via_irq(vif->tx_irq);
f942dc25
IC
1700}
1701
1702static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1703 u16 id,
1704 s8 st,
1705 u16 offset,
1706 u16 size,
1707 u16 flags)
1708{
1709 RING_IDX i = vif->rx.rsp_prod_pvt;
1710 struct xen_netif_rx_response *resp;
1711
1712 resp = RING_GET_RESPONSE(&vif->rx, i);
1713 resp->offset = offset;
1714 resp->flags = flags;
1715 resp->id = id;
1716 resp->status = (s16)size;
1717 if (st < 0)
1718 resp->status = (s16)st;
1719
1720 vif->rx.rsp_prod_pvt = ++i;
1721
1722 return resp;
1723}
1724
b3f980bd 1725static inline int rx_work_todo(struct xenvif *vif)
f942dc25 1726{
ca2f09f2 1727 return !skb_queue_empty(&vif->rx_queue) || vif->rx_event;
f942dc25
IC
1728}
1729
b3f980bd 1730static inline int tx_work_todo(struct xenvif *vif)
f942dc25
IC
1731{
1732
b3f980bd
WL
1733 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1734 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1735 < MAX_PENDING_REQS))
f942dc25
IC
1736 return 1;
1737
1738 return 0;
1739}
1740
7376419a 1741void xenvif_unmap_frontend_rings(struct xenvif *vif)
f942dc25 1742{
c9d63699
DV
1743 if (vif->tx.sring)
1744 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1745 vif->tx.sring);
1746 if (vif->rx.sring)
1747 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1748 vif->rx.sring);
f942dc25
IC
1749}
1750
7376419a
WL
1751int xenvif_map_frontend_rings(struct xenvif *vif,
1752 grant_ref_t tx_ring_ref,
1753 grant_ref_t rx_ring_ref)
f942dc25 1754{
c9d63699 1755 void *addr;
f942dc25
IC
1756 struct xen_netif_tx_sring *txs;
1757 struct xen_netif_rx_sring *rxs;
1758
1759 int err = -ENOMEM;
1760
c9d63699
DV
1761 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1762 tx_ring_ref, &addr);
1763 if (err)
f942dc25
IC
1764 goto err;
1765
c9d63699 1766 txs = (struct xen_netif_tx_sring *)addr;
f942dc25
IC
1767 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1768
c9d63699
DV
1769 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1770 rx_ring_ref, &addr);
1771 if (err)
f942dc25 1772 goto err;
f942dc25 1773
c9d63699 1774 rxs = (struct xen_netif_rx_sring *)addr;
f942dc25
IC
1775 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1776
1777 return 0;
1778
1779err:
7376419a 1780 xenvif_unmap_frontend_rings(vif);
f942dc25
IC
1781 return err;
1782}
1783
ca2f09f2
PD
1784void xenvif_stop_queue(struct xenvif *vif)
1785{
1786 if (!vif->can_queue)
1787 return;
1788
1789 netif_stop_queue(vif->dev);
1790}
1791
1792static void xenvif_start_queue(struct xenvif *vif)
1793{
1794 if (xenvif_schedulable(vif))
1795 netif_wake_queue(vif->dev);
1796}
1797
7376419a 1798int xenvif_kthread(void *data)
b3f980bd
WL
1799{
1800 struct xenvif *vif = data;
ca2f09f2 1801 struct sk_buff *skb;
b3f980bd
WL
1802
1803 while (!kthread_should_stop()) {
1804 wait_event_interruptible(vif->wq,
1805 rx_work_todo(vif) ||
1806 kthread_should_stop());
1807 if (kthread_should_stop())
1808 break;
1809
ca2f09f2 1810 if (!skb_queue_empty(&vif->rx_queue))
7376419a 1811 xenvif_rx_action(vif);
b3f980bd 1812
ca2f09f2
PD
1813 vif->rx_event = false;
1814
1815 if (skb_queue_empty(&vif->rx_queue) &&
1816 netif_queue_stopped(vif->dev))
1817 xenvif_start_queue(vif);
1818
b3f980bd
WL
1819 cond_resched();
1820 }
1821
ca2f09f2
PD
1822 /* Bin any remaining skbs */
1823 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
1824 dev_kfree_skb(skb);
1825
b3f980bd
WL
1826 return 0;
1827}
1828
f942dc25
IC
1829static int __init netback_init(void)
1830{
f942dc25 1831 int rc = 0;
f942dc25 1832
2a14b244 1833 if (!xen_domain())
f942dc25
IC
1834 return -ENODEV;
1835
37641494 1836 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
383eda32
JP
1837 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1838 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
37641494 1839 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
2810e5b9
WL
1840 }
1841
f942dc25
IC
1842 rc = xenvif_xenbus_init();
1843 if (rc)
1844 goto failed_init;
1845
1846 return 0;
1847
1848failed_init:
f942dc25 1849 return rc;
f942dc25
IC
1850}
1851
1852module_init(netback_init);
1853
b103f358
WL
1854static void __exit netback_fini(void)
1855{
b103f358 1856 xenvif_xenbus_fini();
b103f358
WL
1857}
1858module_exit(netback_fini);
1859
f942dc25 1860MODULE_LICENSE("Dual BSD/GPL");
f984cec6 1861MODULE_ALIAS("xen-backend:vif");