KVM: x86 emulator: change address linearization to return an error code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / xen-netfront.c
blob5c8d9c385be0f05ffedc1bdceab8cb090a8749c9
1 /*
2 * Virtual network driver for conversing with remote driver backends.
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation; or, when distributed
10 * separately from the Linux kernel or incorporated into other
11 * software packages, subject to the following license:
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 * IN THE SOFTWARE.
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/ethtool.h>
38 #include <linux/if_ether.h>
39 #include <linux/tcp.h>
40 #include <linux/udp.h>
41 #include <linux/moduleparam.h>
42 #include <linux/mm.h>
43 #include <linux/slab.h>
44 #include <net/ip.h>
46 #include <xen/xen.h>
47 #include <xen/xenbus.h>
48 #include <xen/events.h>
49 #include <xen/page.h>
50 #include <xen/grant_table.h>
52 #include <xen/interface/io/netif.h>
53 #include <xen/interface/memory.h>
54 #include <xen/interface/grant_table.h>
56 static const struct ethtool_ops xennet_ethtool_ops;
58 struct netfront_cb {
59 struct page *page;
60 unsigned offset;
63 #define NETFRONT_SKB_CB(skb) ((struct netfront_cb *)((skb)->cb))
65 #define RX_COPY_THRESHOLD 256
67 #define GRANT_INVALID_REF 0
69 #define NET_TX_RING_SIZE __CONST_RING_SIZE(xen_netif_tx, PAGE_SIZE)
70 #define NET_RX_RING_SIZE __CONST_RING_SIZE(xen_netif_rx, PAGE_SIZE)
71 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
73 struct netfront_info {
74 struct list_head list;
75 struct net_device *netdev;
77 struct napi_struct napi;
79 unsigned int evtchn;
80 struct xenbus_device *xbdev;
82 spinlock_t tx_lock;
83 struct xen_netif_tx_front_ring tx;
84 int tx_ring_ref;
87 * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries
88 * are linked from tx_skb_freelist through skb_entry.link.
90 * NB. Freelist index entries are always going to be less than
91 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
92 * greater than PAGE_OFFSET: we use this property to distinguish
93 * them.
95 union skb_entry {
96 struct sk_buff *skb;
97 unsigned long link;
98 } tx_skbs[NET_TX_RING_SIZE];
99 grant_ref_t gref_tx_head;
100 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE];
101 unsigned tx_skb_freelist;
103 spinlock_t rx_lock ____cacheline_aligned_in_smp;
104 struct xen_netif_rx_front_ring rx;
105 int rx_ring_ref;
107 /* Receive-ring batched refills. */
108 #define RX_MIN_TARGET 8
109 #define RX_DFL_MIN_TARGET 64
110 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
111 unsigned rx_min_target, rx_max_target, rx_target;
112 struct sk_buff_head rx_batch;
114 struct timer_list rx_refill_timer;
116 struct sk_buff *rx_skbs[NET_RX_RING_SIZE];
117 grant_ref_t gref_rx_head;
118 grant_ref_t grant_rx_ref[NET_RX_RING_SIZE];
120 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
121 struct multicall_entry rx_mcl[NET_RX_RING_SIZE+1];
122 struct mmu_update rx_mmu[NET_RX_RING_SIZE];
124 /* Statistics */
125 unsigned long rx_gso_checksum_fixup;
128 struct netfront_rx_info {
129 struct xen_netif_rx_response rx;
130 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
133 static void skb_entry_set_link(union skb_entry *list, unsigned short id)
135 list->link = id;
138 static int skb_entry_is_link(const union skb_entry *list)
140 BUILD_BUG_ON(sizeof(list->skb) != sizeof(list->link));
141 return (unsigned long)list->skb < PAGE_OFFSET;
145 * Access macros for acquiring freeing slots in tx_skbs[].
148 static void add_id_to_freelist(unsigned *head, union skb_entry *list,
149 unsigned short id)
151 skb_entry_set_link(&list[id], *head);
152 *head = id;
155 static unsigned short get_id_from_freelist(unsigned *head,
156 union skb_entry *list)
158 unsigned int id = *head;
159 *head = list[id].link;
160 return id;
163 static int xennet_rxidx(RING_IDX idx)
165 return idx & (NET_RX_RING_SIZE - 1);
168 static struct sk_buff *xennet_get_rx_skb(struct netfront_info *np,
169 RING_IDX ri)
171 int i = xennet_rxidx(ri);
172 struct sk_buff *skb = np->rx_skbs[i];
173 np->rx_skbs[i] = NULL;
174 return skb;
177 static grant_ref_t xennet_get_rx_ref(struct netfront_info *np,
178 RING_IDX ri)
180 int i = xennet_rxidx(ri);
181 grant_ref_t ref = np->grant_rx_ref[i];
182 np->grant_rx_ref[i] = GRANT_INVALID_REF;
183 return ref;
186 #ifdef CONFIG_SYSFS
187 static int xennet_sysfs_addif(struct net_device *netdev);
188 static void xennet_sysfs_delif(struct net_device *netdev);
189 #else /* !CONFIG_SYSFS */
190 #define xennet_sysfs_addif(dev) (0)
191 #define xennet_sysfs_delif(dev) do { } while (0)
192 #endif
194 static int xennet_can_sg(struct net_device *dev)
196 return dev->features & NETIF_F_SG;
200 static void rx_refill_timeout(unsigned long data)
202 struct net_device *dev = (struct net_device *)data;
203 struct netfront_info *np = netdev_priv(dev);
204 napi_schedule(&np->napi);
207 static int netfront_tx_slot_available(struct netfront_info *np)
209 return (np->tx.req_prod_pvt - np->tx.rsp_cons) <
210 (TX_MAX_TARGET - MAX_SKB_FRAGS - 2);
213 static void xennet_maybe_wake_tx(struct net_device *dev)
215 struct netfront_info *np = netdev_priv(dev);
217 if (unlikely(netif_queue_stopped(dev)) &&
218 netfront_tx_slot_available(np) &&
219 likely(netif_running(dev)))
220 netif_wake_queue(dev);
223 static void xennet_alloc_rx_buffers(struct net_device *dev)
225 unsigned short id;
226 struct netfront_info *np = netdev_priv(dev);
227 struct sk_buff *skb;
228 struct page *page;
229 int i, batch_target, notify;
230 RING_IDX req_prod = np->rx.req_prod_pvt;
231 grant_ref_t ref;
232 unsigned long pfn;
233 void *vaddr;
234 struct xen_netif_rx_request *req;
236 if (unlikely(!netif_carrier_ok(dev)))
237 return;
240 * Allocate skbuffs greedily, even though we batch updates to the
241 * receive ring. This creates a less bursty demand on the memory
242 * allocator, so should reduce the chance of failed allocation requests
243 * both for ourself and for other kernel subsystems.
245 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
246 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
247 skb = __netdev_alloc_skb(dev, RX_COPY_THRESHOLD + NET_IP_ALIGN,
248 GFP_ATOMIC | __GFP_NOWARN);
249 if (unlikely(!skb))
250 goto no_skb;
252 /* Align ip header to a 16 bytes boundary */
253 skb_reserve(skb, NET_IP_ALIGN);
255 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
256 if (!page) {
257 kfree_skb(skb);
258 no_skb:
259 /* Any skbuffs queued for refill? Force them out. */
260 if (i != 0)
261 goto refill;
262 /* Could not allocate any skbuffs. Try again later. */
263 mod_timer(&np->rx_refill_timer,
264 jiffies + (HZ/10));
265 break;
268 skb_shinfo(skb)->frags[0].page = page;
269 skb_shinfo(skb)->nr_frags = 1;
270 __skb_queue_tail(&np->rx_batch, skb);
273 /* Is the batch large enough to be worthwhile? */
274 if (i < (np->rx_target/2)) {
275 if (req_prod > np->rx.sring->req_prod)
276 goto push;
277 return;
280 /* Adjust our fill target if we risked running out of buffers. */
281 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
282 ((np->rx_target *= 2) > np->rx_max_target))
283 np->rx_target = np->rx_max_target;
285 refill:
286 for (i = 0; ; i++) {
287 skb = __skb_dequeue(&np->rx_batch);
288 if (skb == NULL)
289 break;
291 skb->dev = dev;
293 id = xennet_rxidx(req_prod + i);
295 BUG_ON(np->rx_skbs[id]);
296 np->rx_skbs[id] = skb;
298 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
299 BUG_ON((signed short)ref < 0);
300 np->grant_rx_ref[id] = ref;
302 pfn = page_to_pfn(skb_shinfo(skb)->frags[0].page);
303 vaddr = page_address(skb_shinfo(skb)->frags[0].page);
305 req = RING_GET_REQUEST(&np->rx, req_prod + i);
306 gnttab_grant_foreign_access_ref(ref,
307 np->xbdev->otherend_id,
308 pfn_to_mfn(pfn),
311 req->id = id;
312 req->gref = ref;
315 wmb(); /* barrier so backend seens requests */
317 /* Above is a suitable barrier to ensure backend will see requests. */
318 np->rx.req_prod_pvt = req_prod + i;
319 push:
320 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->rx, notify);
321 if (notify)
322 notify_remote_via_irq(np->netdev->irq);
325 static int xennet_open(struct net_device *dev)
327 struct netfront_info *np = netdev_priv(dev);
329 napi_enable(&np->napi);
331 spin_lock_bh(&np->rx_lock);
332 if (netif_carrier_ok(dev)) {
333 xennet_alloc_rx_buffers(dev);
334 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
335 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
336 napi_schedule(&np->napi);
338 spin_unlock_bh(&np->rx_lock);
340 netif_start_queue(dev);
342 return 0;
345 static void xennet_tx_buf_gc(struct net_device *dev)
347 RING_IDX cons, prod;
348 unsigned short id;
349 struct netfront_info *np = netdev_priv(dev);
350 struct sk_buff *skb;
352 BUG_ON(!netif_carrier_ok(dev));
354 do {
355 prod = np->tx.sring->rsp_prod;
356 rmb(); /* Ensure we see responses up to 'rp'. */
358 for (cons = np->tx.rsp_cons; cons != prod; cons++) {
359 struct xen_netif_tx_response *txrsp;
361 txrsp = RING_GET_RESPONSE(&np->tx, cons);
362 if (txrsp->status == XEN_NETIF_RSP_NULL)
363 continue;
365 id = txrsp->id;
366 skb = np->tx_skbs[id].skb;
367 if (unlikely(gnttab_query_foreign_access(
368 np->grant_tx_ref[id]) != 0)) {
369 printk(KERN_ALERT "xennet_tx_buf_gc: warning "
370 "-- grant still in use by backend "
371 "domain.\n");
372 BUG();
374 gnttab_end_foreign_access_ref(
375 np->grant_tx_ref[id], GNTMAP_readonly);
376 gnttab_release_grant_reference(
377 &np->gref_tx_head, np->grant_tx_ref[id]);
378 np->grant_tx_ref[id] = GRANT_INVALID_REF;
379 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, id);
380 dev_kfree_skb_irq(skb);
383 np->tx.rsp_cons = prod;
386 * Set a new event, then check for race with update of tx_cons.
387 * Note that it is essential to schedule a callback, no matter
388 * how few buffers are pending. Even if there is space in the
389 * transmit ring, higher layers may be blocked because too much
390 * data is outstanding: in such cases notification from Xen is
391 * likely to be the only kick that we'll get.
393 np->tx.sring->rsp_event =
394 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
395 mb(); /* update shared area */
396 } while ((cons == prod) && (prod != np->tx.sring->rsp_prod));
398 xennet_maybe_wake_tx(dev);
401 static void xennet_make_frags(struct sk_buff *skb, struct net_device *dev,
402 struct xen_netif_tx_request *tx)
404 struct netfront_info *np = netdev_priv(dev);
405 char *data = skb->data;
406 unsigned long mfn;
407 RING_IDX prod = np->tx.req_prod_pvt;
408 int frags = skb_shinfo(skb)->nr_frags;
409 unsigned int offset = offset_in_page(data);
410 unsigned int len = skb_headlen(skb);
411 unsigned int id;
412 grant_ref_t ref;
413 int i;
415 /* While the header overlaps a page boundary (including being
416 larger than a page), split it it into page-sized chunks. */
417 while (len > PAGE_SIZE - offset) {
418 tx->size = PAGE_SIZE - offset;
419 tx->flags |= XEN_NETTXF_more_data;
420 len -= tx->size;
421 data += tx->size;
422 offset = 0;
424 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
425 np->tx_skbs[id].skb = skb_get(skb);
426 tx = RING_GET_REQUEST(&np->tx, prod++);
427 tx->id = id;
428 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
429 BUG_ON((signed short)ref < 0);
431 mfn = virt_to_mfn(data);
432 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
433 mfn, GNTMAP_readonly);
435 tx->gref = np->grant_tx_ref[id] = ref;
436 tx->offset = offset;
437 tx->size = len;
438 tx->flags = 0;
441 /* Grant backend access to each skb fragment page. */
442 for (i = 0; i < frags; i++) {
443 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
445 tx->flags |= XEN_NETTXF_more_data;
447 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
448 np->tx_skbs[id].skb = skb_get(skb);
449 tx = RING_GET_REQUEST(&np->tx, prod++);
450 tx->id = id;
451 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
452 BUG_ON((signed short)ref < 0);
454 mfn = pfn_to_mfn(page_to_pfn(frag->page));
455 gnttab_grant_foreign_access_ref(ref, np->xbdev->otherend_id,
456 mfn, GNTMAP_readonly);
458 tx->gref = np->grant_tx_ref[id] = ref;
459 tx->offset = frag->page_offset;
460 tx->size = frag->size;
461 tx->flags = 0;
464 np->tx.req_prod_pvt = prod;
467 static int xennet_start_xmit(struct sk_buff *skb, struct net_device *dev)
469 unsigned short id;
470 struct netfront_info *np = netdev_priv(dev);
471 struct xen_netif_tx_request *tx;
472 struct xen_netif_extra_info *extra;
473 char *data = skb->data;
474 RING_IDX i;
475 grant_ref_t ref;
476 unsigned long mfn;
477 int notify;
478 int frags = skb_shinfo(skb)->nr_frags;
479 unsigned int offset = offset_in_page(data);
480 unsigned int len = skb_headlen(skb);
482 frags += DIV_ROUND_UP(offset + len, PAGE_SIZE);
483 if (unlikely(frags > MAX_SKB_FRAGS + 1)) {
484 printk(KERN_ALERT "xennet: skb rides the rocket: %d frags\n",
485 frags);
486 dump_stack();
487 goto drop;
490 spin_lock_irq(&np->tx_lock);
492 if (unlikely(!netif_carrier_ok(dev) ||
493 (frags > 1 && !xennet_can_sg(dev)) ||
494 netif_needs_gso(skb, netif_skb_features(skb)))) {
495 spin_unlock_irq(&np->tx_lock);
496 goto drop;
499 i = np->tx.req_prod_pvt;
501 id = get_id_from_freelist(&np->tx_skb_freelist, np->tx_skbs);
502 np->tx_skbs[id].skb = skb;
504 tx = RING_GET_REQUEST(&np->tx, i);
506 tx->id = id;
507 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
508 BUG_ON((signed short)ref < 0);
509 mfn = virt_to_mfn(data);
510 gnttab_grant_foreign_access_ref(
511 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
512 tx->gref = np->grant_tx_ref[id] = ref;
513 tx->offset = offset;
514 tx->size = len;
515 extra = NULL;
517 tx->flags = 0;
518 if (skb->ip_summed == CHECKSUM_PARTIAL)
519 /* local packet? */
520 tx->flags |= XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated;
521 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
522 /* remote but checksummed. */
523 tx->flags |= XEN_NETTXF_data_validated;
525 if (skb_shinfo(skb)->gso_size) {
526 struct xen_netif_extra_info *gso;
528 gso = (struct xen_netif_extra_info *)
529 RING_GET_REQUEST(&np->tx, ++i);
531 if (extra)
532 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
533 else
534 tx->flags |= XEN_NETTXF_extra_info;
536 gso->u.gso.size = skb_shinfo(skb)->gso_size;
537 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
538 gso->u.gso.pad = 0;
539 gso->u.gso.features = 0;
541 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
542 gso->flags = 0;
543 extra = gso;
546 np->tx.req_prod_pvt = i + 1;
548 xennet_make_frags(skb, dev, tx);
549 tx->size = skb->len;
551 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
552 if (notify)
553 notify_remote_via_irq(np->netdev->irq);
555 dev->stats.tx_bytes += skb->len;
556 dev->stats.tx_packets++;
558 /* Note: It is not safe to access skb after xennet_tx_buf_gc()! */
559 xennet_tx_buf_gc(dev);
561 if (!netfront_tx_slot_available(np))
562 netif_stop_queue(dev);
564 spin_unlock_irq(&np->tx_lock);
566 return NETDEV_TX_OK;
568 drop:
569 dev->stats.tx_dropped++;
570 dev_kfree_skb(skb);
571 return NETDEV_TX_OK;
574 static int xennet_close(struct net_device *dev)
576 struct netfront_info *np = netdev_priv(dev);
577 netif_stop_queue(np->netdev);
578 napi_disable(&np->napi);
579 return 0;
582 static void xennet_move_rx_slot(struct netfront_info *np, struct sk_buff *skb,
583 grant_ref_t ref)
585 int new = xennet_rxidx(np->rx.req_prod_pvt);
587 BUG_ON(np->rx_skbs[new]);
588 np->rx_skbs[new] = skb;
589 np->grant_rx_ref[new] = ref;
590 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
591 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
592 np->rx.req_prod_pvt++;
595 static int xennet_get_extras(struct netfront_info *np,
596 struct xen_netif_extra_info *extras,
597 RING_IDX rp)
600 struct xen_netif_extra_info *extra;
601 struct device *dev = &np->netdev->dev;
602 RING_IDX cons = np->rx.rsp_cons;
603 int err = 0;
605 do {
606 struct sk_buff *skb;
607 grant_ref_t ref;
609 if (unlikely(cons + 1 == rp)) {
610 if (net_ratelimit())
611 dev_warn(dev, "Missing extra info\n");
612 err = -EBADR;
613 break;
616 extra = (struct xen_netif_extra_info *)
617 RING_GET_RESPONSE(&np->rx, ++cons);
619 if (unlikely(!extra->type ||
620 extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
621 if (net_ratelimit())
622 dev_warn(dev, "Invalid extra type: %d\n",
623 extra->type);
624 err = -EINVAL;
625 } else {
626 memcpy(&extras[extra->type - 1], extra,
627 sizeof(*extra));
630 skb = xennet_get_rx_skb(np, cons);
631 ref = xennet_get_rx_ref(np, cons);
632 xennet_move_rx_slot(np, skb, ref);
633 } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
635 np->rx.rsp_cons = cons;
636 return err;
639 static int xennet_get_responses(struct netfront_info *np,
640 struct netfront_rx_info *rinfo, RING_IDX rp,
641 struct sk_buff_head *list)
643 struct xen_netif_rx_response *rx = &rinfo->rx;
644 struct xen_netif_extra_info *extras = rinfo->extras;
645 struct device *dev = &np->netdev->dev;
646 RING_IDX cons = np->rx.rsp_cons;
647 struct sk_buff *skb = xennet_get_rx_skb(np, cons);
648 grant_ref_t ref = xennet_get_rx_ref(np, cons);
649 int max = MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD);
650 int frags = 1;
651 int err = 0;
652 unsigned long ret;
654 if (rx->flags & XEN_NETRXF_extra_info) {
655 err = xennet_get_extras(np, extras, rp);
656 cons = np->rx.rsp_cons;
659 for (;;) {
660 if (unlikely(rx->status < 0 ||
661 rx->offset + rx->status > PAGE_SIZE)) {
662 if (net_ratelimit())
663 dev_warn(dev, "rx->offset: %x, size: %u\n",
664 rx->offset, rx->status);
665 xennet_move_rx_slot(np, skb, ref);
666 err = -EINVAL;
667 goto next;
671 * This definitely indicates a bug, either in this driver or in
672 * the backend driver. In future this should flag the bad
673 * situation to the system controller to reboot the backed.
675 if (ref == GRANT_INVALID_REF) {
676 if (net_ratelimit())
677 dev_warn(dev, "Bad rx response id %d.\n",
678 rx->id);
679 err = -EINVAL;
680 goto next;
683 ret = gnttab_end_foreign_access_ref(ref, 0);
684 BUG_ON(!ret);
686 gnttab_release_grant_reference(&np->gref_rx_head, ref);
688 __skb_queue_tail(list, skb);
690 next:
691 if (!(rx->flags & XEN_NETRXF_more_data))
692 break;
694 if (cons + frags == rp) {
695 if (net_ratelimit())
696 dev_warn(dev, "Need more frags\n");
697 err = -ENOENT;
698 break;
701 rx = RING_GET_RESPONSE(&np->rx, cons + frags);
702 skb = xennet_get_rx_skb(np, cons + frags);
703 ref = xennet_get_rx_ref(np, cons + frags);
704 frags++;
707 if (unlikely(frags > max)) {
708 if (net_ratelimit())
709 dev_warn(dev, "Too many frags\n");
710 err = -E2BIG;
713 if (unlikely(err))
714 np->rx.rsp_cons = cons + frags;
716 return err;
719 static int xennet_set_skb_gso(struct sk_buff *skb,
720 struct xen_netif_extra_info *gso)
722 if (!gso->u.gso.size) {
723 if (net_ratelimit())
724 printk(KERN_WARNING "GSO size must not be zero.\n");
725 return -EINVAL;
728 /* Currently only TCPv4 S.O. is supported. */
729 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
730 if (net_ratelimit())
731 printk(KERN_WARNING "Bad GSO type %d.\n", gso->u.gso.type);
732 return -EINVAL;
735 skb_shinfo(skb)->gso_size = gso->u.gso.size;
736 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
738 /* Header must be checked, and gso_segs computed. */
739 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
740 skb_shinfo(skb)->gso_segs = 0;
742 return 0;
745 static RING_IDX xennet_fill_frags(struct netfront_info *np,
746 struct sk_buff *skb,
747 struct sk_buff_head *list)
749 struct skb_shared_info *shinfo = skb_shinfo(skb);
750 int nr_frags = shinfo->nr_frags;
751 RING_IDX cons = np->rx.rsp_cons;
752 skb_frag_t *frag = shinfo->frags + nr_frags;
753 struct sk_buff *nskb;
755 while ((nskb = __skb_dequeue(list))) {
756 struct xen_netif_rx_response *rx =
757 RING_GET_RESPONSE(&np->rx, ++cons);
759 frag->page = skb_shinfo(nskb)->frags[0].page;
760 frag->page_offset = rx->offset;
761 frag->size = rx->status;
763 skb->data_len += rx->status;
765 skb_shinfo(nskb)->nr_frags = 0;
766 kfree_skb(nskb);
768 frag++;
769 nr_frags++;
772 shinfo->nr_frags = nr_frags;
773 return cons;
776 static int checksum_setup(struct net_device *dev, struct sk_buff *skb)
778 struct iphdr *iph;
779 unsigned char *th;
780 int err = -EPROTO;
781 int recalculate_partial_csum = 0;
784 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
785 * peers can fail to set NETRXF_csum_blank when sending a GSO
786 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
787 * recalculate the partial checksum.
789 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
790 struct netfront_info *np = netdev_priv(dev);
791 np->rx_gso_checksum_fixup++;
792 skb->ip_summed = CHECKSUM_PARTIAL;
793 recalculate_partial_csum = 1;
796 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
797 if (skb->ip_summed != CHECKSUM_PARTIAL)
798 return 0;
800 if (skb->protocol != htons(ETH_P_IP))
801 goto out;
803 iph = (void *)skb->data;
804 th = skb->data + 4 * iph->ihl;
805 if (th >= skb_tail_pointer(skb))
806 goto out;
808 skb->csum_start = th - skb->head;
809 switch (iph->protocol) {
810 case IPPROTO_TCP:
811 skb->csum_offset = offsetof(struct tcphdr, check);
813 if (recalculate_partial_csum) {
814 struct tcphdr *tcph = (struct tcphdr *)th;
815 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
816 skb->len - iph->ihl*4,
817 IPPROTO_TCP, 0);
819 break;
820 case IPPROTO_UDP:
821 skb->csum_offset = offsetof(struct udphdr, check);
823 if (recalculate_partial_csum) {
824 struct udphdr *udph = (struct udphdr *)th;
825 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
826 skb->len - iph->ihl*4,
827 IPPROTO_UDP, 0);
829 break;
830 default:
831 if (net_ratelimit())
832 printk(KERN_ERR "Attempting to checksum a non-"
833 "TCP/UDP packet, dropping a protocol"
834 " %d packet", iph->protocol);
835 goto out;
838 if ((th + skb->csum_offset + 2) > skb_tail_pointer(skb))
839 goto out;
841 err = 0;
843 out:
844 return err;
847 static int handle_incoming_queue(struct net_device *dev,
848 struct sk_buff_head *rxq)
850 int packets_dropped = 0;
851 struct sk_buff *skb;
853 while ((skb = __skb_dequeue(rxq)) != NULL) {
854 struct page *page = NETFRONT_SKB_CB(skb)->page;
855 void *vaddr = page_address(page);
856 unsigned offset = NETFRONT_SKB_CB(skb)->offset;
858 memcpy(skb->data, vaddr + offset,
859 skb_headlen(skb));
861 if (page != skb_shinfo(skb)->frags[0].page)
862 __free_page(page);
864 /* Ethernet work: Delayed to here as it peeks the header. */
865 skb->protocol = eth_type_trans(skb, dev);
867 if (checksum_setup(dev, skb)) {
868 kfree_skb(skb);
869 packets_dropped++;
870 dev->stats.rx_errors++;
871 continue;
874 dev->stats.rx_packets++;
875 dev->stats.rx_bytes += skb->len;
877 /* Pass it up. */
878 netif_receive_skb(skb);
881 return packets_dropped;
884 static int xennet_poll(struct napi_struct *napi, int budget)
886 struct netfront_info *np = container_of(napi, struct netfront_info, napi);
887 struct net_device *dev = np->netdev;
888 struct sk_buff *skb;
889 struct netfront_rx_info rinfo;
890 struct xen_netif_rx_response *rx = &rinfo.rx;
891 struct xen_netif_extra_info *extras = rinfo.extras;
892 RING_IDX i, rp;
893 int work_done;
894 struct sk_buff_head rxq;
895 struct sk_buff_head errq;
896 struct sk_buff_head tmpq;
897 unsigned long flags;
898 unsigned int len;
899 int err;
901 spin_lock(&np->rx_lock);
903 skb_queue_head_init(&rxq);
904 skb_queue_head_init(&errq);
905 skb_queue_head_init(&tmpq);
907 rp = np->rx.sring->rsp_prod;
908 rmb(); /* Ensure we see queued responses up to 'rp'. */
910 i = np->rx.rsp_cons;
911 work_done = 0;
912 while ((i != rp) && (work_done < budget)) {
913 memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
914 memset(extras, 0, sizeof(rinfo.extras));
916 err = xennet_get_responses(np, &rinfo, rp, &tmpq);
918 if (unlikely(err)) {
919 err:
920 while ((skb = __skb_dequeue(&tmpq)))
921 __skb_queue_tail(&errq, skb);
922 dev->stats.rx_errors++;
923 i = np->rx.rsp_cons;
924 continue;
927 skb = __skb_dequeue(&tmpq);
929 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
930 struct xen_netif_extra_info *gso;
931 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
933 if (unlikely(xennet_set_skb_gso(skb, gso))) {
934 __skb_queue_head(&tmpq, skb);
935 np->rx.rsp_cons += skb_queue_len(&tmpq);
936 goto err;
940 NETFRONT_SKB_CB(skb)->page = skb_shinfo(skb)->frags[0].page;
941 NETFRONT_SKB_CB(skb)->offset = rx->offset;
943 len = rx->status;
944 if (len > RX_COPY_THRESHOLD)
945 len = RX_COPY_THRESHOLD;
946 skb_put(skb, len);
948 if (rx->status > len) {
949 skb_shinfo(skb)->frags[0].page_offset =
950 rx->offset + len;
951 skb_shinfo(skb)->frags[0].size = rx->status - len;
952 skb->data_len = rx->status - len;
953 } else {
954 skb_shinfo(skb)->frags[0].page = NULL;
955 skb_shinfo(skb)->nr_frags = 0;
958 i = xennet_fill_frags(np, skb, &tmpq);
961 * Truesize approximates the size of true data plus
962 * any supervisor overheads. Adding hypervisor
963 * overheads has been shown to significantly reduce
964 * achievable bandwidth with the default receive
965 * buffer size. It is therefore not wise to account
966 * for it here.
968 * After alloc_skb(RX_COPY_THRESHOLD), truesize is set
969 * to RX_COPY_THRESHOLD + the supervisor
970 * overheads. Here, we add the size of the data pulled
971 * in xennet_fill_frags().
973 * We also adjust for any unused space in the main
974 * data area by subtracting (RX_COPY_THRESHOLD -
975 * len). This is especially important with drivers
976 * which split incoming packets into header and data,
977 * using only 66 bytes of the main data area (see the
978 * e1000 driver for example.) On such systems,
979 * without this last adjustement, our achievable
980 * receive throughout using the standard receive
981 * buffer size was cut by 25%(!!!).
983 skb->truesize += skb->data_len - (RX_COPY_THRESHOLD - len);
984 skb->len += skb->data_len;
986 if (rx->flags & XEN_NETRXF_csum_blank)
987 skb->ip_summed = CHECKSUM_PARTIAL;
988 else if (rx->flags & XEN_NETRXF_data_validated)
989 skb->ip_summed = CHECKSUM_UNNECESSARY;
991 __skb_queue_tail(&rxq, skb);
993 np->rx.rsp_cons = ++i;
994 work_done++;
997 __skb_queue_purge(&errq);
999 work_done -= handle_incoming_queue(dev, &rxq);
1001 /* If we get a callback with very few responses, reduce fill target. */
1002 /* NB. Note exponential increase, linear decrease. */
1003 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
1004 ((3*np->rx_target) / 4)) &&
1005 (--np->rx_target < np->rx_min_target))
1006 np->rx_target = np->rx_min_target;
1008 xennet_alloc_rx_buffers(dev);
1010 if (work_done < budget) {
1011 int more_to_do = 0;
1013 local_irq_save(flags);
1015 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
1016 if (!more_to_do)
1017 __napi_complete(napi);
1019 local_irq_restore(flags);
1022 spin_unlock(&np->rx_lock);
1024 return work_done;
1027 static int xennet_change_mtu(struct net_device *dev, int mtu)
1029 int max = xennet_can_sg(dev) ? 65535 - ETH_HLEN : ETH_DATA_LEN;
1031 if (mtu > max)
1032 return -EINVAL;
1033 dev->mtu = mtu;
1034 return 0;
1037 static void xennet_release_tx_bufs(struct netfront_info *np)
1039 struct sk_buff *skb;
1040 int i;
1042 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1043 /* Skip over entries which are actually freelist references */
1044 if (skb_entry_is_link(&np->tx_skbs[i]))
1045 continue;
1047 skb = np->tx_skbs[i].skb;
1048 gnttab_end_foreign_access_ref(np->grant_tx_ref[i],
1049 GNTMAP_readonly);
1050 gnttab_release_grant_reference(&np->gref_tx_head,
1051 np->grant_tx_ref[i]);
1052 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1053 add_id_to_freelist(&np->tx_skb_freelist, np->tx_skbs, i);
1054 dev_kfree_skb_irq(skb);
1058 static void xennet_release_rx_bufs(struct netfront_info *np)
1060 struct mmu_update *mmu = np->rx_mmu;
1061 struct multicall_entry *mcl = np->rx_mcl;
1062 struct sk_buff_head free_list;
1063 struct sk_buff *skb;
1064 unsigned long mfn;
1065 int xfer = 0, noxfer = 0, unused = 0;
1066 int id, ref;
1068 dev_warn(&np->netdev->dev, "%s: fix me for copying receiver.\n",
1069 __func__);
1070 return;
1072 skb_queue_head_init(&free_list);
1074 spin_lock_bh(&np->rx_lock);
1076 for (id = 0; id < NET_RX_RING_SIZE; id++) {
1077 ref = np->grant_rx_ref[id];
1078 if (ref == GRANT_INVALID_REF) {
1079 unused++;
1080 continue;
1083 skb = np->rx_skbs[id];
1084 mfn = gnttab_end_foreign_transfer_ref(ref);
1085 gnttab_release_grant_reference(&np->gref_rx_head, ref);
1086 np->grant_rx_ref[id] = GRANT_INVALID_REF;
1088 if (0 == mfn) {
1089 skb_shinfo(skb)->nr_frags = 0;
1090 dev_kfree_skb(skb);
1091 noxfer++;
1092 continue;
1095 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1096 /* Remap the page. */
1097 struct page *page = skb_shinfo(skb)->frags[0].page;
1098 unsigned long pfn = page_to_pfn(page);
1099 void *vaddr = page_address(page);
1101 MULTI_update_va_mapping(mcl, (unsigned long)vaddr,
1102 mfn_pte(mfn, PAGE_KERNEL),
1104 mcl++;
1105 mmu->ptr = ((u64)mfn << PAGE_SHIFT)
1106 | MMU_MACHPHYS_UPDATE;
1107 mmu->val = pfn;
1108 mmu++;
1110 set_phys_to_machine(pfn, mfn);
1112 __skb_queue_tail(&free_list, skb);
1113 xfer++;
1116 dev_info(&np->netdev->dev, "%s: %d xfer, %d noxfer, %d unused\n",
1117 __func__, xfer, noxfer, unused);
1119 if (xfer) {
1120 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
1121 /* Do all the remapping work and M2P updates. */
1122 MULTI_mmu_update(mcl, np->rx_mmu, mmu - np->rx_mmu,
1123 NULL, DOMID_SELF);
1124 mcl++;
1125 HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
1129 __skb_queue_purge(&free_list);
1131 spin_unlock_bh(&np->rx_lock);
1134 static void xennet_uninit(struct net_device *dev)
1136 struct netfront_info *np = netdev_priv(dev);
1137 xennet_release_tx_bufs(np);
1138 xennet_release_rx_bufs(np);
1139 gnttab_free_grant_references(np->gref_tx_head);
1140 gnttab_free_grant_references(np->gref_rx_head);
1143 static const struct net_device_ops xennet_netdev_ops = {
1144 .ndo_open = xennet_open,
1145 .ndo_uninit = xennet_uninit,
1146 .ndo_stop = xennet_close,
1147 .ndo_start_xmit = xennet_start_xmit,
1148 .ndo_change_mtu = xennet_change_mtu,
1149 .ndo_set_mac_address = eth_mac_addr,
1150 .ndo_validate_addr = eth_validate_addr,
1153 static struct net_device * __devinit xennet_create_dev(struct xenbus_device *dev)
1155 int i, err;
1156 struct net_device *netdev;
1157 struct netfront_info *np;
1159 netdev = alloc_etherdev(sizeof(struct netfront_info));
1160 if (!netdev) {
1161 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1162 __func__);
1163 return ERR_PTR(-ENOMEM);
1166 np = netdev_priv(netdev);
1167 np->xbdev = dev;
1169 spin_lock_init(&np->tx_lock);
1170 spin_lock_init(&np->rx_lock);
1172 skb_queue_head_init(&np->rx_batch);
1173 np->rx_target = RX_DFL_MIN_TARGET;
1174 np->rx_min_target = RX_DFL_MIN_TARGET;
1175 np->rx_max_target = RX_MAX_TARGET;
1177 init_timer(&np->rx_refill_timer);
1178 np->rx_refill_timer.data = (unsigned long)netdev;
1179 np->rx_refill_timer.function = rx_refill_timeout;
1181 /* Initialise tx_skbs as a free chain containing every entry. */
1182 np->tx_skb_freelist = 0;
1183 for (i = 0; i < NET_TX_RING_SIZE; i++) {
1184 skb_entry_set_link(&np->tx_skbs[i], i+1);
1185 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1188 /* Clear out rx_skbs */
1189 for (i = 0; i < NET_RX_RING_SIZE; i++) {
1190 np->rx_skbs[i] = NULL;
1191 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1194 /* A grant for every tx ring slot */
1195 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1196 &np->gref_tx_head) < 0) {
1197 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1198 err = -ENOMEM;
1199 goto exit;
1201 /* A grant for every rx ring slot */
1202 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1203 &np->gref_rx_head) < 0) {
1204 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1205 err = -ENOMEM;
1206 goto exit_free_tx;
1209 netdev->netdev_ops = &xennet_netdev_ops;
1211 netif_napi_add(netdev, &np->napi, xennet_poll, 64);
1212 netdev->features = NETIF_F_IP_CSUM;
1214 SET_ETHTOOL_OPS(netdev, &xennet_ethtool_ops);
1215 SET_NETDEV_DEV(netdev, &dev->dev);
1217 np->netdev = netdev;
1219 netif_carrier_off(netdev);
1221 return netdev;
1223 exit_free_tx:
1224 gnttab_free_grant_references(np->gref_tx_head);
1225 exit:
1226 free_netdev(netdev);
1227 return ERR_PTR(err);
1231 * Entry point to this code when a new device is created. Allocate the basic
1232 * structures and the ring buffers for communication with the backend, and
1233 * inform the backend of the appropriate details for those.
1235 static int __devinit netfront_probe(struct xenbus_device *dev,
1236 const struct xenbus_device_id *id)
1238 int err;
1239 struct net_device *netdev;
1240 struct netfront_info *info;
1242 netdev = xennet_create_dev(dev);
1243 if (IS_ERR(netdev)) {
1244 err = PTR_ERR(netdev);
1245 xenbus_dev_fatal(dev, err, "creating netdev");
1246 return err;
1249 info = netdev_priv(netdev);
1250 dev_set_drvdata(&dev->dev, info);
1252 err = register_netdev(info->netdev);
1253 if (err) {
1254 printk(KERN_WARNING "%s: register_netdev err=%d\n",
1255 __func__, err);
1256 goto fail;
1259 err = xennet_sysfs_addif(info->netdev);
1260 if (err) {
1261 unregister_netdev(info->netdev);
1262 printk(KERN_WARNING "%s: add sysfs failed err=%d\n",
1263 __func__, err);
1264 goto fail;
1267 return 0;
1269 fail:
1270 free_netdev(netdev);
1271 dev_set_drvdata(&dev->dev, NULL);
1272 return err;
1275 static void xennet_end_access(int ref, void *page)
1277 /* This frees the page as a side-effect */
1278 if (ref != GRANT_INVALID_REF)
1279 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1282 static void xennet_disconnect_backend(struct netfront_info *info)
1284 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1285 spin_lock_bh(&info->rx_lock);
1286 spin_lock_irq(&info->tx_lock);
1287 netif_carrier_off(info->netdev);
1288 spin_unlock_irq(&info->tx_lock);
1289 spin_unlock_bh(&info->rx_lock);
1291 if (info->netdev->irq)
1292 unbind_from_irqhandler(info->netdev->irq, info->netdev);
1293 info->evtchn = info->netdev->irq = 0;
1295 /* End access and free the pages */
1296 xennet_end_access(info->tx_ring_ref, info->tx.sring);
1297 xennet_end_access(info->rx_ring_ref, info->rx.sring);
1299 info->tx_ring_ref = GRANT_INVALID_REF;
1300 info->rx_ring_ref = GRANT_INVALID_REF;
1301 info->tx.sring = NULL;
1302 info->rx.sring = NULL;
1306 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1307 * driver restart. We tear down our netif structure and recreate it, but
1308 * leave the device-layer structures intact so that this is transparent to the
1309 * rest of the kernel.
1311 static int netfront_resume(struct xenbus_device *dev)
1313 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1315 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1317 xennet_disconnect_backend(info);
1318 return 0;
1321 static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
1323 char *s, *e, *macstr;
1324 int i;
1326 macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
1327 if (IS_ERR(macstr))
1328 return PTR_ERR(macstr);
1330 for (i = 0; i < ETH_ALEN; i++) {
1331 mac[i] = simple_strtoul(s, &e, 16);
1332 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
1333 kfree(macstr);
1334 return -ENOENT;
1336 s = e+1;
1339 kfree(macstr);
1340 return 0;
1343 static irqreturn_t xennet_interrupt(int irq, void *dev_id)
1345 struct net_device *dev = dev_id;
1346 struct netfront_info *np = netdev_priv(dev);
1347 unsigned long flags;
1349 spin_lock_irqsave(&np->tx_lock, flags);
1351 if (likely(netif_carrier_ok(dev))) {
1352 xennet_tx_buf_gc(dev);
1353 /* Under tx_lock: protects access to rx shared-ring indexes. */
1354 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx))
1355 napi_schedule(&np->napi);
1358 spin_unlock_irqrestore(&np->tx_lock, flags);
1360 return IRQ_HANDLED;
1363 static int setup_netfront(struct xenbus_device *dev, struct netfront_info *info)
1365 struct xen_netif_tx_sring *txs;
1366 struct xen_netif_rx_sring *rxs;
1367 int err;
1368 struct net_device *netdev = info->netdev;
1370 info->tx_ring_ref = GRANT_INVALID_REF;
1371 info->rx_ring_ref = GRANT_INVALID_REF;
1372 info->rx.sring = NULL;
1373 info->tx.sring = NULL;
1374 netdev->irq = 0;
1376 err = xen_net_read_mac(dev, netdev->dev_addr);
1377 if (err) {
1378 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
1379 goto fail;
1382 txs = (struct xen_netif_tx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1383 if (!txs) {
1384 err = -ENOMEM;
1385 xenbus_dev_fatal(dev, err, "allocating tx ring page");
1386 goto fail;
1388 SHARED_RING_INIT(txs);
1389 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
1391 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
1392 if (err < 0) {
1393 free_page((unsigned long)txs);
1394 goto fail;
1397 info->tx_ring_ref = err;
1398 rxs = (struct xen_netif_rx_sring *)get_zeroed_page(GFP_NOIO | __GFP_HIGH);
1399 if (!rxs) {
1400 err = -ENOMEM;
1401 xenbus_dev_fatal(dev, err, "allocating rx ring page");
1402 goto fail;
1404 SHARED_RING_INIT(rxs);
1405 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
1407 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
1408 if (err < 0) {
1409 free_page((unsigned long)rxs);
1410 goto fail;
1412 info->rx_ring_ref = err;
1414 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1415 if (err)
1416 goto fail;
1418 err = bind_evtchn_to_irqhandler(info->evtchn, xennet_interrupt,
1419 IRQF_SAMPLE_RANDOM, netdev->name,
1420 netdev);
1421 if (err < 0)
1422 goto fail;
1423 netdev->irq = err;
1424 return 0;
1426 fail:
1427 return err;
1430 /* Common code used when first setting up, and when resuming. */
1431 static int talk_to_netback(struct xenbus_device *dev,
1432 struct netfront_info *info)
1434 const char *message;
1435 struct xenbus_transaction xbt;
1436 int err;
1438 /* Create shared ring, alloc event channel. */
1439 err = setup_netfront(dev, info);
1440 if (err)
1441 goto out;
1443 again:
1444 err = xenbus_transaction_start(&xbt);
1445 if (err) {
1446 xenbus_dev_fatal(dev, err, "starting transaction");
1447 goto destroy_ring;
1450 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref", "%u",
1451 info->tx_ring_ref);
1452 if (err) {
1453 message = "writing tx ring-ref";
1454 goto abort_transaction;
1456 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref", "%u",
1457 info->rx_ring_ref);
1458 if (err) {
1459 message = "writing rx ring-ref";
1460 goto abort_transaction;
1462 err = xenbus_printf(xbt, dev->nodename,
1463 "event-channel", "%u", info->evtchn);
1464 if (err) {
1465 message = "writing event-channel";
1466 goto abort_transaction;
1469 err = xenbus_printf(xbt, dev->nodename, "request-rx-copy", "%u",
1471 if (err) {
1472 message = "writing request-rx-copy";
1473 goto abort_transaction;
1476 err = xenbus_printf(xbt, dev->nodename, "feature-rx-notify", "%d", 1);
1477 if (err) {
1478 message = "writing feature-rx-notify";
1479 goto abort_transaction;
1482 err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", 1);
1483 if (err) {
1484 message = "writing feature-sg";
1485 goto abort_transaction;
1488 err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4", "%d", 1);
1489 if (err) {
1490 message = "writing feature-gso-tcpv4";
1491 goto abort_transaction;
1494 err = xenbus_transaction_end(xbt, 0);
1495 if (err) {
1496 if (err == -EAGAIN)
1497 goto again;
1498 xenbus_dev_fatal(dev, err, "completing transaction");
1499 goto destroy_ring;
1502 return 0;
1504 abort_transaction:
1505 xenbus_transaction_end(xbt, 1);
1506 xenbus_dev_fatal(dev, err, "%s", message);
1507 destroy_ring:
1508 xennet_disconnect_backend(info);
1509 out:
1510 return err;
1513 static int xennet_set_sg(struct net_device *dev, u32 data)
1515 if (data) {
1516 struct netfront_info *np = netdev_priv(dev);
1517 int val;
1519 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend, "feature-sg",
1520 "%d", &val) < 0)
1521 val = 0;
1522 if (!val)
1523 return -ENOSYS;
1524 } else if (dev->mtu > ETH_DATA_LEN)
1525 dev->mtu = ETH_DATA_LEN;
1527 return ethtool_op_set_sg(dev, data);
1530 static int xennet_set_tso(struct net_device *dev, u32 data)
1532 if (data) {
1533 struct netfront_info *np = netdev_priv(dev);
1534 int val;
1536 if (xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1537 "feature-gso-tcpv4", "%d", &val) < 0)
1538 val = 0;
1539 if (!val)
1540 return -ENOSYS;
1543 return ethtool_op_set_tso(dev, data);
1546 static void xennet_set_features(struct net_device *dev)
1548 /* Turn off all GSO bits except ROBUST. */
1549 dev->features &= ~NETIF_F_GSO_MASK;
1550 dev->features |= NETIF_F_GSO_ROBUST;
1551 xennet_set_sg(dev, 0);
1553 /* We need checksum offload to enable scatter/gather and TSO. */
1554 if (!(dev->features & NETIF_F_IP_CSUM))
1555 return;
1557 if (!xennet_set_sg(dev, 1))
1558 xennet_set_tso(dev, 1);
1561 static int xennet_connect(struct net_device *dev)
1563 struct netfront_info *np = netdev_priv(dev);
1564 int i, requeue_idx, err;
1565 struct sk_buff *skb;
1566 grant_ref_t ref;
1567 struct xen_netif_rx_request *req;
1568 unsigned int feature_rx_copy;
1570 err = xenbus_scanf(XBT_NIL, np->xbdev->otherend,
1571 "feature-rx-copy", "%u", &feature_rx_copy);
1572 if (err != 1)
1573 feature_rx_copy = 0;
1575 if (!feature_rx_copy) {
1576 dev_info(&dev->dev,
1577 "backend does not support copying receive path\n");
1578 return -ENODEV;
1581 err = talk_to_netback(np->xbdev, np);
1582 if (err)
1583 return err;
1585 xennet_set_features(dev);
1587 spin_lock_bh(&np->rx_lock);
1588 spin_lock_irq(&np->tx_lock);
1590 /* Step 1: Discard all pending TX packet fragments. */
1591 xennet_release_tx_bufs(np);
1593 /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1594 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1595 if (!np->rx_skbs[i])
1596 continue;
1598 skb = np->rx_skbs[requeue_idx] = xennet_get_rx_skb(np, i);
1599 ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1600 req = RING_GET_REQUEST(&np->rx, requeue_idx);
1602 gnttab_grant_foreign_access_ref(
1603 ref, np->xbdev->otherend_id,
1604 pfn_to_mfn(page_to_pfn(skb_shinfo(skb)->
1605 frags->page)),
1607 req->gref = ref;
1608 req->id = requeue_idx;
1610 requeue_idx++;
1613 np->rx.req_prod_pvt = requeue_idx;
1616 * Step 3: All public and private state should now be sane. Get
1617 * ready to start sending and receiving packets and give the driver
1618 * domain a kick because we've probably just requeued some
1619 * packets.
1621 netif_carrier_on(np->netdev);
1622 notify_remote_via_irq(np->netdev->irq);
1623 xennet_tx_buf_gc(dev);
1624 xennet_alloc_rx_buffers(dev);
1626 spin_unlock_irq(&np->tx_lock);
1627 spin_unlock_bh(&np->rx_lock);
1629 return 0;
1633 * Callback received when the backend's state changes.
1635 static void netback_changed(struct xenbus_device *dev,
1636 enum xenbus_state backend_state)
1638 struct netfront_info *np = dev_get_drvdata(&dev->dev);
1639 struct net_device *netdev = np->netdev;
1641 dev_dbg(&dev->dev, "%s\n", xenbus_strstate(backend_state));
1643 switch (backend_state) {
1644 case XenbusStateInitialising:
1645 case XenbusStateInitialised:
1646 case XenbusStateReconfiguring:
1647 case XenbusStateReconfigured:
1648 case XenbusStateConnected:
1649 case XenbusStateUnknown:
1650 case XenbusStateClosed:
1651 break;
1653 case XenbusStateInitWait:
1654 if (dev->state != XenbusStateInitialising)
1655 break;
1656 if (xennet_connect(netdev) != 0)
1657 break;
1658 xenbus_switch_state(dev, XenbusStateConnected);
1659 netif_notify_peers(netdev);
1660 break;
1662 case XenbusStateClosing:
1663 xenbus_frontend_closed(dev);
1664 break;
1668 static const struct xennet_stat {
1669 char name[ETH_GSTRING_LEN];
1670 u16 offset;
1671 } xennet_stats[] = {
1673 "rx_gso_checksum_fixup",
1674 offsetof(struct netfront_info, rx_gso_checksum_fixup)
1678 static int xennet_get_sset_count(struct net_device *dev, int string_set)
1680 switch (string_set) {
1681 case ETH_SS_STATS:
1682 return ARRAY_SIZE(xennet_stats);
1683 default:
1684 return -EINVAL;
1688 static void xennet_get_ethtool_stats(struct net_device *dev,
1689 struct ethtool_stats *stats, u64 * data)
1691 void *np = netdev_priv(dev);
1692 int i;
1694 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
1695 data[i] = *(unsigned long *)(np + xennet_stats[i].offset);
1698 static void xennet_get_strings(struct net_device *dev, u32 stringset, u8 * data)
1700 int i;
1702 switch (stringset) {
1703 case ETH_SS_STATS:
1704 for (i = 0; i < ARRAY_SIZE(xennet_stats); i++)
1705 memcpy(data + i * ETH_GSTRING_LEN,
1706 xennet_stats[i].name, ETH_GSTRING_LEN);
1707 break;
1711 static const struct ethtool_ops xennet_ethtool_ops =
1713 .set_tx_csum = ethtool_op_set_tx_csum,
1714 .set_sg = xennet_set_sg,
1715 .set_tso = xennet_set_tso,
1716 .get_link = ethtool_op_get_link,
1718 .get_sset_count = xennet_get_sset_count,
1719 .get_ethtool_stats = xennet_get_ethtool_stats,
1720 .get_strings = xennet_get_strings,
1723 #ifdef CONFIG_SYSFS
1724 static ssize_t show_rxbuf_min(struct device *dev,
1725 struct device_attribute *attr, char *buf)
1727 struct net_device *netdev = to_net_dev(dev);
1728 struct netfront_info *info = netdev_priv(netdev);
1730 return sprintf(buf, "%u\n", info->rx_min_target);
1733 static ssize_t store_rxbuf_min(struct device *dev,
1734 struct device_attribute *attr,
1735 const char *buf, size_t len)
1737 struct net_device *netdev = to_net_dev(dev);
1738 struct netfront_info *np = netdev_priv(netdev);
1739 char *endp;
1740 unsigned long target;
1742 if (!capable(CAP_NET_ADMIN))
1743 return -EPERM;
1745 target = simple_strtoul(buf, &endp, 0);
1746 if (endp == buf)
1747 return -EBADMSG;
1749 if (target < RX_MIN_TARGET)
1750 target = RX_MIN_TARGET;
1751 if (target > RX_MAX_TARGET)
1752 target = RX_MAX_TARGET;
1754 spin_lock_bh(&np->rx_lock);
1755 if (target > np->rx_max_target)
1756 np->rx_max_target = target;
1757 np->rx_min_target = target;
1758 if (target > np->rx_target)
1759 np->rx_target = target;
1761 xennet_alloc_rx_buffers(netdev);
1763 spin_unlock_bh(&np->rx_lock);
1764 return len;
1767 static ssize_t show_rxbuf_max(struct device *dev,
1768 struct device_attribute *attr, char *buf)
1770 struct net_device *netdev = to_net_dev(dev);
1771 struct netfront_info *info = netdev_priv(netdev);
1773 return sprintf(buf, "%u\n", info->rx_max_target);
1776 static ssize_t store_rxbuf_max(struct device *dev,
1777 struct device_attribute *attr,
1778 const char *buf, size_t len)
1780 struct net_device *netdev = to_net_dev(dev);
1781 struct netfront_info *np = netdev_priv(netdev);
1782 char *endp;
1783 unsigned long target;
1785 if (!capable(CAP_NET_ADMIN))
1786 return -EPERM;
1788 target = simple_strtoul(buf, &endp, 0);
1789 if (endp == buf)
1790 return -EBADMSG;
1792 if (target < RX_MIN_TARGET)
1793 target = RX_MIN_TARGET;
1794 if (target > RX_MAX_TARGET)
1795 target = RX_MAX_TARGET;
1797 spin_lock_bh(&np->rx_lock);
1798 if (target < np->rx_min_target)
1799 np->rx_min_target = target;
1800 np->rx_max_target = target;
1801 if (target < np->rx_target)
1802 np->rx_target = target;
1804 xennet_alloc_rx_buffers(netdev);
1806 spin_unlock_bh(&np->rx_lock);
1807 return len;
1810 static ssize_t show_rxbuf_cur(struct device *dev,
1811 struct device_attribute *attr, char *buf)
1813 struct net_device *netdev = to_net_dev(dev);
1814 struct netfront_info *info = netdev_priv(netdev);
1816 return sprintf(buf, "%u\n", info->rx_target);
1819 static struct device_attribute xennet_attrs[] = {
1820 __ATTR(rxbuf_min, S_IRUGO|S_IWUSR, show_rxbuf_min, store_rxbuf_min),
1821 __ATTR(rxbuf_max, S_IRUGO|S_IWUSR, show_rxbuf_max, store_rxbuf_max),
1822 __ATTR(rxbuf_cur, S_IRUGO, show_rxbuf_cur, NULL),
1825 static int xennet_sysfs_addif(struct net_device *netdev)
1827 int i;
1828 int err;
1830 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++) {
1831 err = device_create_file(&netdev->dev,
1832 &xennet_attrs[i]);
1833 if (err)
1834 goto fail;
1836 return 0;
1838 fail:
1839 while (--i >= 0)
1840 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1841 return err;
1844 static void xennet_sysfs_delif(struct net_device *netdev)
1846 int i;
1848 for (i = 0; i < ARRAY_SIZE(xennet_attrs); i++)
1849 device_remove_file(&netdev->dev, &xennet_attrs[i]);
1852 #endif /* CONFIG_SYSFS */
1854 static struct xenbus_device_id netfront_ids[] = {
1855 { "vif" },
1856 { "" }
1860 static int __devexit xennet_remove(struct xenbus_device *dev)
1862 struct netfront_info *info = dev_get_drvdata(&dev->dev);
1864 dev_dbg(&dev->dev, "%s\n", dev->nodename);
1866 unregister_netdev(info->netdev);
1868 xennet_disconnect_backend(info);
1870 del_timer_sync(&info->rx_refill_timer);
1872 xennet_sysfs_delif(info->netdev);
1874 free_netdev(info->netdev);
1876 return 0;
1879 static struct xenbus_driver netfront_driver = {
1880 .name = "vif",
1881 .owner = THIS_MODULE,
1882 .ids = netfront_ids,
1883 .probe = netfront_probe,
1884 .remove = __devexit_p(xennet_remove),
1885 .resume = netfront_resume,
1886 .otherend_changed = netback_changed,
1889 static int __init netif_init(void)
1891 if (!xen_domain())
1892 return -ENODEV;
1894 if (xen_initial_domain())
1895 return 0;
1897 printk(KERN_INFO "Initialising Xen virtual ethernet driver.\n");
1899 return xenbus_register_frontend(&netfront_driver);
1901 module_init(netif_init);
1904 static void __exit netif_exit(void)
1906 if (xen_initial_domain())
1907 return;
1909 xenbus_unregister_driver(&netfront_driver);
1911 module_exit(netif_exit);
1913 MODULE_DESCRIPTION("Xen virtual network device frontend");
1914 MODULE_LICENSE("GPL");
1915 MODULE_ALIAS("xen:vif");
1916 MODULE_ALIAS("xennet");